FIX BUG_679
This commit is contained in:
@@ -306,10 +306,10 @@ public class ConversationManagerService {
|
||||
final String userMessageText = context.userMessageText();
|
||||
final String userPhoneNumber = context.primaryPhoneNumber();
|
||||
|
||||
return memoryStoreNotificationService.getSessionByTelefono(userPhoneNumber)
|
||||
return memoryStoreConversationService.getSessionByTelefono(userPhoneNumber)
|
||||
.switchIfEmpty(Mono.defer(() -> {
|
||||
String newSessionId = SessionIdGenerator.generateStandardSessionId();
|
||||
logger.info("No existing notification session found for phone number {}. Creating new session: {}",
|
||||
logger.warn("No existing conversation session found for notification reply on phone {}. This is unexpected. Creating new session: {}",
|
||||
userPhoneNumber, newSessionId);
|
||||
return Mono.just(ConversationSessionDTO.create(newSessionId, userId, userPhoneNumber));
|
||||
}))
|
||||
@@ -326,7 +326,7 @@ public class ConversationManagerService {
|
||||
notificationText, conversationHistory, filteredParams.toString(), userId, sessionId,
|
||||
userPhoneNumber);
|
||||
|
||||
if (!NotificationContextResolver.CATEGORY_DIALOGFLOW.equals(resolvedContext)) {
|
||||
if (!resolvedContext.trim().toUpperCase().contains(NotificationContextResolver.CATEGORY_DIALOGFLOW)) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
llmResponseTunerService.setValue(uuid, resolvedContext).subscribe();
|
||||
|
||||
@@ -335,8 +335,8 @@ public class ConversationManagerService {
|
||||
ConversationEntryDTO llmEntry = ConversationEntryDTO.forLlmConversation(resolvedContext,
|
||||
notification.parametros());
|
||||
|
||||
return persistNotificationTurn(userId, sessionId, userEntry, userPhoneNumber)
|
||||
.then(persistNotificationTurn(userId, sessionId, llmEntry, userPhoneNumber))
|
||||
return persistConversationTurn(userId, sessionId, userEntry, userPhoneNumber)
|
||||
.then(persistConversationTurn(userId, sessionId, llmEntry, userPhoneNumber))
|
||||
.then(Mono.defer(() -> {
|
||||
EventInputDTO eventInput = new EventInputDTO("LLM_RESPONSE_PROCESSED");
|
||||
QueryInputDTO queryInput = new QueryInputDTO(null, eventInput,
|
||||
@@ -344,17 +344,17 @@ public class ConversationManagerService {
|
||||
DetectIntentRequestDTO newRequest = new DetectIntentRequestDTO(queryInput,
|
||||
request.queryParams())
|
||||
.withParameter("llm_reponse_uuid", uuid);
|
||||
|
||||
return dialogflowServiceClient.detectIntent(sessionId, newRequest)
|
||||
.flatMap(response -> {
|
||||
ConversationEntryDTO agentEntry = ConversationEntryDTO
|
||||
.forAgent(response.queryResult());
|
||||
return persistNotificationTurn(userId, sessionId, agentEntry,
|
||||
return persistConversationTurn(userId, sessionId, agentEntry,
|
||||
userPhoneNumber)
|
||||
.thenReturn(response);
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
} else {
|
||||
ConversationEntryDTO userEntry = ConversationEntryDTO.forUser(userMessageText,
|
||||
notification.parametros());
|
||||
|
||||
@@ -366,16 +366,15 @@ public class ConversationManagerService {
|
||||
finalRequest = request.withParameter(CONV_HISTORY_PARAM, conversationHistory)
|
||||
.withParameters(notification.parametros());
|
||||
}
|
||||
|
||||
return memoryStoreNotificationService.saveEntry(userId, sessionId, userEntry, userPhoneNumber)
|
||||
return persistConversationTurn(userId, sessionId, userEntry, userPhoneNumber)
|
||||
.then(dialogflowServiceClient.detectIntent(sessionId, finalRequest)
|
||||
.flatMap(response -> {
|
||||
ConversationEntryDTO agentEntry = ConversationEntryDTO
|
||||
.forAgent(response.queryResult());
|
||||
return memoryStoreNotificationService
|
||||
.saveEntry(userId, sessionId, agentEntry, userPhoneNumber)
|
||||
return persistConversationTurn(userId, sessionId, agentEntry, userPhoneNumber)
|
||||
.thenReturn(response);
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -397,19 +396,4 @@ public class ConversationManagerService {
|
||||
.doOnError(e -> logger.error("Error during primary Redis write for session {}. Type: {}: {}", sessionId,
|
||||
entry.type().name(), e.getMessage(), e));
|
||||
}
|
||||
|
||||
private Mono<Void> persistNotificationTurn(String userId, String sessionId, ConversationEntryDTO entry,
|
||||
String userPhoneNumber) {
|
||||
logger.debug("Starting Write-Back persistence for notification session {}. Type: {}. Writing to Redis first.",
|
||||
sessionId,
|
||||
entry.type().name());
|
||||
return memoryStoreNotificationService.saveEntry(userId, sessionId, entry, userPhoneNumber)
|
||||
.doOnSuccess(v -> logger.info(
|
||||
"Entry saved to Redis for notification session {}. Type: {}. Kicking off async Firestore write-back.",
|
||||
sessionId, entry.type().name()))
|
||||
.doOnError(e -> logger.error(
|
||||
"Error during primary Redis write for notification session {}. Type: {}: {}", sessionId,
|
||||
entry.type().name(), e.getMessage(), e));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,13 +14,14 @@ import com.example.dto.dialogflow.notification.NotificationDTO;
|
||||
import com.example.mapper.notification.ExternalNotRequestMapper;
|
||||
import com.example.service.base.DialogflowClientService;
|
||||
import com.example.service.conversation.DataLossPrevention;
|
||||
import com.example.service.conversation.FirestoreConversationService;
|
||||
import com.example.service.conversation.MemoryStoreConversationService;
|
||||
import com.example.util.SessionIdGenerator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -36,9 +37,9 @@ public class NotificationManagerService {
|
||||
private final DialogflowClientService dialogflowClientService;
|
||||
private final FirestoreNotificationService firestoreNotificationService;
|
||||
private final MemoryStoreNotificationService memoryStoreNotificationService;
|
||||
private final FirestoreNotificationConvService firestoreConversationService;
|
||||
private final ExternalNotRequestMapper externalNotRequestMapper;
|
||||
|
||||
private final MemoryStoreConversationService memoryStoreConversationService;
|
||||
private final FirestoreConversationService firestoreConversationService;
|
||||
private final DataLossPrevention dataLossPrevention;
|
||||
private final String dlpTemplateCompleteFlow;
|
||||
|
||||
@@ -49,17 +50,21 @@ public class NotificationManagerService {
|
||||
DialogflowClientService dialogflowClientService,
|
||||
FirestoreNotificationService firestoreNotificationService,
|
||||
MemoryStoreNotificationService memoryStoreNotificationService,
|
||||
FirestoreNotificationConvService firestoreConversationService,
|
||||
MemoryStoreConversationService memoryStoreConversationService,
|
||||
FirestoreConversationService firestoreConversationService,
|
||||
|
||||
ExternalNotRequestMapper externalNotRequestMapper,
|
||||
DataLossPrevention dataLossPrevention,
|
||||
@Value("${google.cloud.dlp.dlpTemplateCompleteFlow}") String dlpTemplateCompleteFlow) {
|
||||
|
||||
this.dialogflowClientService = dialogflowClientService;
|
||||
this.firestoreNotificationService = firestoreNotificationService;
|
||||
this.memoryStoreNotificationService = memoryStoreNotificationService;
|
||||
this.firestoreConversationService = firestoreConversationService;
|
||||
this.externalNotRequestMapper = externalNotRequestMapper;
|
||||
this.dataLossPrevention = dataLossPrevention;
|
||||
this.dlpTemplateCompleteFlow = dlpTemplateCompleteFlow;
|
||||
this.memoryStoreConversationService = memoryStoreConversationService;
|
||||
this.firestoreConversationService = firestoreConversationService;
|
||||
}
|
||||
|
||||
public Mono<DetectIntentResponseDTO> processNotification(ExternalNotRequestDTO externalRequest) {
|
||||
@@ -78,7 +83,7 @@ public class NotificationManagerService {
|
||||
externalRequest.phoneNumber(),
|
||||
externalRequest.hiddenParameters()
|
||||
);
|
||||
// 1. Persist the incoming notification entry
|
||||
|
||||
String newNotificationId = SessionIdGenerator.generateStandardSessionId();
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
if (obfuscatedRequest.hiddenParameters() != null) {
|
||||
@@ -89,19 +94,19 @@ public class NotificationManagerService {
|
||||
obfuscatedRequest.text(), eventName, defaultLanguageCode, parameters, "active");
|
||||
Mono<Void> persistenceMono = memoryStoreNotificationService.saveOrAppendNotificationEntry(newNotificationEntry)
|
||||
.doOnSuccess(v -> {
|
||||
logger.info("Notification for phone cached. Kicking off async Firestore write-back.");
|
||||
logger.info("Notification for phone {} cached. Kicking off async Firestore write-back.", telefono);
|
||||
firestoreNotificationService.saveOrAppendNotificationEntry(newNotificationEntry)
|
||||
.subscribe(
|
||||
ignored -> logger.debug(
|
||||
"Background: Notification entry persistence initiated for phone in Firestore."),
|
||||
"Background: Notification entry persistence initiated for phone {} in Firestore.", telefono),
|
||||
e -> logger.error(
|
||||
"Background: Error during notification entry persistence for phone in Firestore: {}", e.getMessage(), e));
|
||||
"Background: Error during notification entry persistence for phone {} in Firestore: {}",
|
||||
telefono, e.getMessage(), e));
|
||||
});
|
||||
|
||||
// 2. Resolve or create a conversation session
|
||||
Mono<ConversationSessionDTO> sessionMono = memoryStoreNotificationService.getSessionByTelefono(telefono)
|
||||
.doOnNext(session -> logger.info("Found existing conversation session {} for phone number",
|
||||
session.sessionId()))
|
||||
Mono<ConversationSessionDTO> sessionMono = memoryStoreConversationService.getSessionByTelefono(telefono)
|
||||
.doOnNext(session -> logger.info("Found existing conversation session {} for phone number {}",
|
||||
session.sessionId(), telefono))
|
||||
.flatMap(session -> {
|
||||
Map<String, Object> prefixedParameters = new HashMap<>();
|
||||
if (obfuscatedRequest.hiddenParameters() != null) {
|
||||
@@ -115,7 +120,8 @@ public class NotificationManagerService {
|
||||
})
|
||||
.switchIfEmpty(Mono.defer(() -> {
|
||||
String newSessionId = SessionIdGenerator.generateStandardSessionId();
|
||||
logger.info("No existing conversation session found for phone number. Creating new session: {}", newSessionId);
|
||||
logger.info("No existing conversation session found for phone number {}. Creating new session: {}",
|
||||
telefono, newSessionId);
|
||||
String userId = "user_by_phone_" + telefono;
|
||||
Map<String, Object> prefixedParameters = new HashMap<>();
|
||||
if (obfuscatedRequest.hiddenParameters() != null) {
|
||||
@@ -128,8 +134,6 @@ public class NotificationManagerService {
|
||||
.then(Mono.just(ConversationSessionDTO.create(newSessionId, userId, telefono)));
|
||||
}));
|
||||
|
||||
// 3. Send notification text to Dialogflow using the resolved conversation
|
||||
// session
|
||||
return persistenceMono.then(sessionMono)
|
||||
.flatMap(session -> {
|
||||
final String sessionId = session.sessionId();
|
||||
@@ -140,7 +144,7 @@ public class NotificationManagerService {
|
||||
return dialogflowClientService.detectIntent(sessionId, detectIntentRequest);
|
||||
})
|
||||
.doOnSuccess(response -> logger
|
||||
.info("Finished processing notification. Dialogflow response received for phone"))
|
||||
.info("Finished processing notification. Dialogflow response received for phone {}.", telefono))
|
||||
.doOnError(e -> logger.error("Overall error in NotificationManagerService: {}", e.getMessage(), e));
|
||||
});
|
||||
}
|
||||
@@ -150,11 +154,12 @@ public class NotificationManagerService {
|
||||
logger.debug("Starting Write-Back persistence for session {}. Type: {}. Writing to Redis first.", sessionId,
|
||||
entry.type().name());
|
||||
|
||||
return memoryStoreNotificationService.saveEntry(userId, sessionId, entry, userPhoneNumber)
|
||||
return memoryStoreConversationService.saveEntry(userId, sessionId, entry, userPhoneNumber)
|
||||
.doOnSuccess(v -> {
|
||||
logger.info(
|
||||
"Entry saved to Redis for session {}. Type: {}. Kicking off async Firestore write-back.",
|
||||
sessionId, entry.type().name());
|
||||
|
||||
firestoreConversationService.saveEntry(userId, sessionId, entry, userPhoneNumber)
|
||||
.subscribe(
|
||||
fsVoid -> logger.debug(
|
||||
|
||||
Reference in New Issue
Block a user