UPGRADE 16-12

This commit is contained in:
PAVEL PALMA
2025-12-16 12:35:54 -06:00
parent 379dd769ad
commit a15af59668
7 changed files with 277 additions and 46 deletions

View File

@@ -5,6 +5,7 @@
package com.example.controller;
import com.example.dto.dialogflow.base.DetectIntentResponseDTO;
import com.example.dto.quickreplies.QuickReplyScreenRequestDTO;
import com.example.service.quickreplies.QuickRepliesManagerService;
import jakarta.validation.Valid;
@@ -29,9 +30,8 @@ public class QuickRepliesController {
}
@PostMapping("/screen")
public Mono<Map<String, String>> startSessionAndGetReplies(@Valid @RequestBody QuickReplyScreenRequestDTO request) {
public Mono<DetectIntentResponseDTO> startSessionAndGetReplies(@Valid @RequestBody QuickReplyScreenRequestDTO request) {
return quickRepliesManagerService.startQuickReplySession(request)
.map(response -> Map.of("responseId", response.responseId()))
.doOnSuccess(response -> logger.info("Successfully processed quick reply request"))
.doOnError(error -> logger.error("Error processing quick reply request: {}", error.getMessage(), error));
}

View File

@@ -23,6 +23,8 @@ public class ConversationContextMapper {
private static final int MAX_HISTORY_BYTES = 50 * 1024; // 50 KB
private static final String NOTIFICATION_TEXT_PARAM = "notification_text";
public String toText(ConversationSessionDTO session, List<ConversationMessageDTO> messages) {
if (messages == null || messages.isEmpty()) {
return "";
@@ -75,23 +77,34 @@ public class ConversationContextMapper {
private String formatEntry(ConversationMessageDTO entry) {
String prefix = "User: ";
String content = entry.text();
if (entry.type() != null) {
switch (entry.type()) {
case MessageType.AGENT:
case AGENT:
prefix = "Agent: ";
break;
case MessageType.SYSTEM:
case SYSTEM:
prefix = "System: ";
// fix: add notification in the conversation.
if (entry.parameters() != null && entry.parameters().containsKey(NOTIFICATION_TEXT_PARAM)) {
Object paramText = entry.parameters().get(NOTIFICATION_TEXT_PARAM);
if (paramText != null && !paramText.toString().isBlank()) {
content = paramText.toString();
}
}
break;
case MessageType.USER:
case LLM:
prefix = "System: ";
break;
case USER:
default:
prefix = "User: ";
break;
}
}
String text = prefix + entry.text();
String text = prefix + content;
if (entry.type() == MessageType.AGENT) {
text = cleanAgentMessage(text);

View File

@@ -119,7 +119,6 @@ public class MessageEntryFilter {
yield CATEGORY_UNKNOWN;
}
};
resultCategory = CATEGORY_CONVERSATION;
return resultCategory;
} catch (Exception e) {
logger.error("An error occurred during Gemini content generation for message classification.", e);

View File

@@ -161,40 +161,7 @@ public class ConversationManagerService {
String userMessageText = request.queryInput().text().text();
final ConversationContext context = new ConversationContext(resolvedUserId, null, userMessageText, primaryPhoneNumber);
return handleMessageClassification(context, request);
}
private Mono<DetectIntentResponseDTO> handleMessageClassification(ConversationContext context,
DetectIntentRequestDTO request) {
final String userPhoneNumber = context.primaryPhoneNumber();
final String userMessageText = context.userMessageText();
return memoryStoreConversationService.getSessionByTelefono(userPhoneNumber)
.flatMap(session -> memoryStoreConversationService.getMessages(session.sessionId()).collectList()
.map(conversationContextMapper::toTextFromMessages)
.defaultIfEmpty("")
.flatMap(conversationHistory -> {
return memoryStoreNotificationService.getNotificationIdForPhone(userPhoneNumber)
.flatMap(notificationId -> memoryStoreNotificationService
.getCachedNotificationSession(notificationId))
.map(notificationSession -> notificationSession.notificaciones().stream()
.filter(notification -> "active".equalsIgnoreCase(notification.status()))
.max(java.util.Comparator.comparing(NotificationDTO::timestampCreacion))
.orElse(null))
.filter(Objects::nonNull)
.flatMap((NotificationDTO notification) -> {
String notificationText = notificationContextMapper.toText(notification);
String classification = messageEntryFilter.classifyMessage(userMessageText,
notificationText, conversationHistory);
if (MessageEntryFilter.CATEGORY_NOTIFICATION.equals(classification)) {
return startNotificationConversation(context, request, notification);
} else {
return continueConversationFlow(context, request);
}
})
.switchIfEmpty(continueConversationFlow(context, request));
}))
.switchIfEmpty(continueConversationFlow(context, request));
return continueConversationFlow(context, request);
}
private Mono<DetectIntentResponseDTO> continueConversationFlow(ConversationContext context,
@@ -357,7 +324,6 @@ public class ConversationManagerService {
String resolvedContext = notificationContextResolver.resolveContext(userMessageText,
notificationText, conversationHistory, filteredParams.toString(), userId, sessionId,
userPhoneNumber);
if (!resolvedContext.trim().toUpperCase().contains(NotificationContextResolver.CATEGORY_DIALOGFLOW)) {
String uuid = UUID.randomUUID().toString();
llmResponseTunerService.setValue(uuid, resolvedContext).subscribe();