UPDATE 01-sep

This commit is contained in:
PAVEL PALMA
2025-09-01 15:31:30 -06:00
parent a782f6c284
commit 4278541fff
18 changed files with 146 additions and 59 deletions

View File

@@ -11,7 +11,6 @@ import com.example.dto.dialogflow.conversation.ConversationEntryEntity;
import com.example.dto.dialogflow.conversation.ConversationEntryType;
import com.example.dto.dialogflow.conversation.ExternalConvRequestDTO;
import com.example.dto.quickreplies.QuickReplyScreenRequestDTO;
import com.example.service.conversation.DataLossPrevention;
import com.example.dto.quickreplies.QuestionDTO;
import com.example.dto.quickreplies.QuickReplyDTO;
import com.example.service.conversation.FirestoreConversationService;
@@ -20,10 +19,14 @@ import com.example.util.SessionIdGenerator;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.example.dto.dialogflow.conversation.QueryResultDTO;
import com.example.service.conversation.ConversationManagerService;
import org.springframework.context.annotation.Lazy;
import reactor.core.publisher.Mono;
@Service
@@ -32,20 +35,17 @@ public class QuickRepliesManagerService {
private final MemoryStoreConversationService memoryStoreConversationService;
private final FirestoreConversationService firestoreConversationService;
private final QuickReplyContentService quickReplyContentService;
private final DataLossPrevention dataLossPrevention;
private final String dlpTemplatePersistFlow;
private final ConversationManagerService conversationManagerService;
public QuickRepliesManagerService(
@Lazy ConversationManagerService conversationManagerService,
MemoryStoreConversationService memoryStoreConversationService,
FirestoreConversationService firestoreConversationService,
QuickReplyContentService quickReplyContentService,
DataLossPrevention dataLossPrevention,
@Value("${google.cloud.dlp.dlpTemplatePersistFlow}") String dlpTemplatePersistFlow) {
QuickReplyContentService quickReplyContentService) {
this.conversationManagerService = conversationManagerService;
this.memoryStoreConversationService = memoryStoreConversationService;
this.firestoreConversationService = firestoreConversationService;
this.quickReplyContentService = quickReplyContentService;
this.dataLossPrevention = dataLossPrevention;
this.dlpTemplatePersistFlow = dlpTemplatePersistFlow;
}
public Mono<DetectIntentResponseDTO> startQuickReplySession(QuickReplyScreenRequestDTO externalRequest) {
@@ -88,13 +88,27 @@ public class QuickRepliesManagerService {
.flatMap(session -> {
String userId = session.userId();
String sessionId = session.sessionId();
return dataLossPrevention.getObfuscatedString(externalRequest.message(), dlpTemplatePersistFlow)
.flatMap(obfuscatedMessage -> {
ConversationEntryDTO userEntry = ConversationEntryDTO.forUser(obfuscatedMessage);
long userMessagesCount = session.entries().stream()
.filter(e -> e.entity() == ConversationEntryEntity.USUARIO)
.count();
ConversationEntryDTO userEntry = ConversationEntryDTO.forUser(externalRequest.message());
List<ConversationEntryDTO> entries = session.entries();
int lastInitIndex = IntStream.range(0, entries.size())
.map(i -> entries.size() - 1 - i)
.filter(i -> {
ConversationEntryDTO entry = entries.get(i);
return entry.entity() == ConversationEntryEntity.SISTEMA && entry.type() == ConversationEntryType.INICIO;
})
.findFirst()
.orElse(-1);
long userMessagesCount;
if (lastInitIndex != -1) {
userMessagesCount = entries.subList(lastInitIndex + 1, entries.size()).stream()
.filter(e -> e.entity() == ConversationEntryEntity.USUARIO)
.count();
} else {
userMessagesCount = 0;
}
if (userMessagesCount == 0) { // Is the first user message in the Quick-Replies flow
// This is the second message of the flow. Return the full list.
@@ -118,33 +132,32 @@ public class QuickRepliesManagerService {
.filter(p -> p.titulo().equalsIgnoreCase(externalRequest.message().trim()))
.toList();
QuickReplyDTO responseQuickReplyDTO;
if (!matchedPreguntas.isEmpty()) {
responseQuickReplyDTO = new QuickReplyDTO(quickReplyDTO.header(),
matchedPreguntas);
} else {
responseQuickReplyDTO = new QuickReplyDTO(quickReplyDTO.header(),
Collections.emptyList());
}
// Matched question, return the answer
String respuesta = matchedPreguntas.get(0).respuesta();
QueryResultDTO queryResult = new QueryResultDTO(respuesta, null);
DetectIntentResponseDTO response = new DetectIntentResponseDTO(sessionId, queryResult, null);
// End the quick reply flow by clearing the pantallaContexto
return memoryStoreConversationService
return memoryStoreConversationService
.updateSession(session.withPantallaContexto(null))
.then(persistConversationTurn(userId, sessionId,
ConversationEntryDTO.forAgentWithMessage(
responseQuickReplyDTO.toString()),
ConversationEntryDTO.forAgentWithMessage(respuesta),
userPhoneNumber, null))
.thenReturn(new DetectIntentResponseDTO(sessionId, null,
responseQuickReplyDTO));
.thenReturn(response);
} else {
// No match, delegate to Dialogflow
return memoryStoreConversationService
.updateSession(session.withPantallaContexto(null))
.then(conversationManagerService.manageConversation(externalRequest));
}
});
} else {
// Should not happen. End the flow.
return memoryStoreConversationService.updateSession(session.withPantallaContexto(null))
.then(Mono.just(new DetectIntentResponseDTO(session.sessionId(), null,
new QuickReplyDTO("Flow Error", Collections.emptyList()))));
new QuickReplyDTO("Flow Error", null, null, null, Collections.emptyList()))));
}
});
});
}
private Mono<Void> persistConversationTurn(String userId, String sessionId, ConversationEntryDTO entry,