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