Fix bot runtime
This commit is contained in:
@@ -91,7 +91,7 @@ public class ChatController {
|
|||||||
MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request);
|
MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request);
|
||||||
chatRealtimeService.publishMessage(id, message);
|
chatRealtimeService.publishMessage(id, message);
|
||||||
chatRealtimeService.publishConversationUpdate(id);
|
chatRealtimeService.publishConversationUpdate(id);
|
||||||
openRouterAiService.generateAndSendReply(id);
|
openRouterAiService.generateAndSendReply(id, message.getId());
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(message);
|
return ResponseEntity.status(HttpStatus.CREATED).body(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ public class ChatController {
|
|||||||
MessageResponse message = chatService.sendMessageWithAttachment(id, user.getId(), user.getRole(), file, content);
|
MessageResponse message = chatService.sendMessageWithAttachment(id, user.getId(), user.getRole(), file, content);
|
||||||
chatRealtimeService.publishMessage(id, message);
|
chatRealtimeService.publishMessage(id, message);
|
||||||
chatRealtimeService.publishConversationUpdate(id);
|
chatRealtimeService.publishConversationUpdate(id);
|
||||||
openRouterAiService.generateAndSendReply(id);
|
openRouterAiService.generateAndSendReply(id, message.getId());
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(message);
|
return ResponseEntity.status(HttpStatus.CREATED).body(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ public class ChatWebSocketController {
|
|||||||
MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request);
|
MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request);
|
||||||
chatRealtimeService.publishMessage(id, message);
|
chatRealtimeService.publishMessage(id, message);
|
||||||
chatRealtimeService.publishConversationUpdate(id);
|
chatRealtimeService.publishConversationUpdate(id);
|
||||||
openRouterAiService.generateAndSendReply(id);
|
openRouterAiService.generateAndSendReply(id, message.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@MessageExceptionHandler({IllegalArgumentException.class, RuntimeException.class})
|
@MessageExceptionHandler({IllegalArgumentException.class, RuntimeException.class})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.petshop.backend.service;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
import com.petshop.backend.entity.Conversation;
|
import com.petshop.backend.entity.Conversation;
|
||||||
import com.petshop.backend.entity.Message;
|
import com.petshop.backend.entity.Message;
|
||||||
import com.petshop.backend.entity.User;
|
import com.petshop.backend.entity.User;
|
||||||
@@ -45,25 +46,27 @@ public class OpenRouterAiService {
|
|||||||
ChatService chatService,
|
ChatService chatService,
|
||||||
ChatRealtimeService chatRealtimeService,
|
ChatRealtimeService chatRealtimeService,
|
||||||
MessageRepository messageRepository,
|
MessageRepository messageRepository,
|
||||||
UserRepository userRepository,
|
UserRepository userRepository
|
||||||
ObjectMapper objectMapper
|
|
||||||
) {
|
) {
|
||||||
this.chatService = chatService;
|
this.chatService = chatService;
|
||||||
this.chatRealtimeService = chatRealtimeService;
|
this.chatRealtimeService = chatRealtimeService;
|
||||||
this.messageRepository = messageRepository;
|
this.messageRepository = messageRepository;
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = JsonMapper.builder().findAndAddModules().build();
|
||||||
this.httpClient = HttpClient.newBuilder()
|
this.httpClient = HttpClient.newBuilder()
|
||||||
.connectTimeout(Duration.ofSeconds(10))
|
.connectTimeout(Duration.ofSeconds(10))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateAndSendReply(Long conversationId) {
|
public void generateAndSendReply(Long conversationId, Long triggerMessageId) {
|
||||||
CompletableFuture.runAsync(() -> generateReply(conversationId));
|
CompletableFuture.runAsync(() -> generateReply(conversationId, triggerMessageId));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateReply(Long conversationId) {
|
private void generateReply(Long conversationId, Long triggerMessageId) {
|
||||||
try {
|
try {
|
||||||
|
if (triggerMessageId == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (apiKey == null || apiKey.isBlank()) {
|
if (apiKey == null || apiKey.isBlank()) {
|
||||||
log.debug("Skipping OpenRouter reply for conversation {} because no API key is configured", conversationId);
|
log.debug("Skipping OpenRouter reply for conversation {} because no API key is configured", conversationId);
|
||||||
return;
|
return;
|
||||||
@@ -87,12 +90,20 @@ public class OpenRouterAiService {
|
|||||||
User botUser = userRepository.findByUsername(BOT_USERNAME)
|
User botUser = userRepository.findByUsername(BOT_USERNAME)
|
||||||
.orElseThrow(() -> new IllegalStateException("Bot user not found"));
|
.orElseThrow(() -> new IllegalStateException("Bot user not found"));
|
||||||
|
|
||||||
|
Message triggerMessage = messageRepository.findById(triggerMessageId).orElse(null);
|
||||||
|
if (triggerMessage == null || !conversationId.equals(triggerMessage.getConversationId())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
List<Message> history = messageRepository.findByConversationIdOrderByTimestampAsc(conversationId);
|
List<Message> history = messageRepository.findByConversationIdOrderByTimestampAsc(conversationId);
|
||||||
if (history.isEmpty()) {
|
if (history.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Message lastMessage = history.get(history.size() - 1);
|
Message lastMessage = history.get(history.size() - 1);
|
||||||
|
if (!triggerMessageId.equals(lastMessage.getId())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String lastContent = normalizeContent(lastMessage.getContent());
|
String lastContent = normalizeContent(lastMessage.getContent());
|
||||||
if (lastContent.isBlank()) {
|
if (lastContent.isBlank()) {
|
||||||
return;
|
return;
|
||||||
@@ -146,6 +157,18 @@ public class OpenRouterAiService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Conversation latestConversation = chatService.getConversationEntity(conversationId);
|
||||||
|
if (latestConversation.getStatus() == Conversation.ConversationStatus.CLOSED
|
||||||
|
|| latestConversation.getHumanRequestedAt() != null
|
||||||
|
|| latestConversation.getMode() != Conversation.ConversationMode.AUTOMATED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Message> latestHistory = messageRepository.findByConversationIdOrderByTimestampAsc(conversationId);
|
||||||
|
if (latestHistory.isEmpty() || !triggerMessageId.equals(latestHistory.get(latestHistory.size() - 1).getId())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var messageResponse = chatService.saveBotMessage(conversationId, replyContent);
|
var messageResponse = chatService.saveBotMessage(conversationId, replyContent);
|
||||||
chatRealtimeService.publishMessage(conversationId, messageResponse);
|
chatRealtimeService.publishMessage(conversationId, messageResponse);
|
||||||
chatRealtimeService.publishConversationUpdate(conversationId);
|
chatRealtimeService.publishConversationUpdate(conversationId);
|
||||||
|
|||||||
Reference in New Issue
Block a user