From b6c7bdbd0d4106365635885536b9d72faf57f2c5 Mon Sep 17 00:00:00 2001 From: PAVEL PALMA Date: Tue, 27 Jan 2026 13:02:33 -0600 Subject: [PATCH] UPDATE sessions --- .../conversation/ConversationManagerService.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/service/conversation/ConversationManagerService.java b/src/main/java/com/example/service/conversation/ConversationManagerService.java index 3486a32..1de7df9 100644 --- a/src/main/java/com/example/service/conversation/ConversationManagerService.java +++ b/src/main/java/com/example/service/conversation/ConversationManagerService.java @@ -235,15 +235,24 @@ public class ConversationManagerService { private Mono proceedWithConversation(ConversationContext context, DetectIntentRequestDTO request, ConversationSessionDTO session) { Instant now = Instant.now(); - if (Duration.between(session.lastModified(), now).toMinutes() > SESSION_RESET_THRESHOLD_MINUTES) { + if (Duration.between(session.lastModified(), now).toMinutes() < SESSION_RESET_THRESHOLD_MINUTES) { logger.info("Recent Session Found: Session {} is within the 30-minute threshold. Proceeding to Dialogflow.", session.sessionId()); return processDialogflowRequest(session, request, context.userId(), context.userMessageText(), context.primaryPhoneNumber(), false); } else { + + logger.info( - "Old Session Found: Session {} is older than the 30-minute threshold. Fetching history and continuing with same session.", + "Old Session Found: Session {} is older than the 30-minute threshold.", session.sessionId()); + // Generar un nuevo ID de sesión + String newSessionId = SessionIdGenerator.generateStandardSessionId(); + logger.info("Creating new session {} from old session {} due to timeout.", newSessionId, session.sessionId()); + + // Crear un nuevo DTO de sesión basado en la antigua, pero con el nuevo ID + ConversationSessionDTO newSession = ConversationSessionDTO.create(newSessionId, context.userId(), context.primaryPhoneNumber()); + return memoryStoreConversationService.getMessages(session.sessionId()) .collectList() // Adding use the TextWithLimits to truncate according to business rule 30 days/60 messages @@ -252,7 +261,7 @@ public class ConversationManagerService { .flatMap(conversationHistory -> { // Inject historial (max 60 msgs / 30 días / 50KB) DetectIntentRequestDTO newRequest = request.withParameter(CONV_HISTORY_PARAM, conversationHistory); - return processDialogflowRequest(session, newRequest, context.userId(), context.userMessageText(), + return processDialogflowRequest(newSession, newRequest, context.userId(), context.userMessageText(), context.primaryPhoneNumber(), false); }); }