Fix bot runtime

This commit is contained in:
2026-04-10 07:20:15 -06:00
parent 998f476319
commit c0c984d82e
3 changed files with 32 additions and 9 deletions

View File

@@ -91,7 +91,7 @@ public class ChatController {
MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request);
chatRealtimeService.publishMessage(id, message);
chatRealtimeService.publishConversationUpdate(id);
openRouterAiService.generateAndSendReply(id);
openRouterAiService.generateAndSendReply(id, message.getId());
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);
chatRealtimeService.publishMessage(id, message);
chatRealtimeService.publishConversationUpdate(id);
openRouterAiService.generateAndSendReply(id);
openRouterAiService.generateAndSendReply(id, message.getId());
return ResponseEntity.status(HttpStatus.CREATED).body(message);
}

View File

@@ -57,7 +57,7 @@ public class ChatWebSocketController {
MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request);
chatRealtimeService.publishMessage(id, message);
chatRealtimeService.publishConversationUpdate(id);
openRouterAiService.generateAndSendReply(id);
openRouterAiService.generateAndSendReply(id, message.getId());
}
@MessageExceptionHandler({IllegalArgumentException.class, RuntimeException.class})

View File

@@ -2,6 +2,7 @@ package com.petshop.backend.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.petshop.backend.entity.Conversation;
import com.petshop.backend.entity.Message;
import com.petshop.backend.entity.User;
@@ -45,25 +46,27 @@ public class OpenRouterAiService {
ChatService chatService,
ChatRealtimeService chatRealtimeService,
MessageRepository messageRepository,
UserRepository userRepository,
ObjectMapper objectMapper
UserRepository userRepository
) {
this.chatService = chatService;
this.chatRealtimeService = chatRealtimeService;
this.messageRepository = messageRepository;
this.userRepository = userRepository;
this.objectMapper = objectMapper;
this.objectMapper = JsonMapper.builder().findAndAddModules().build();
this.httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
}
public void generateAndSendReply(Long conversationId) {
CompletableFuture.runAsync(() -> generateReply(conversationId));
public void generateAndSendReply(Long conversationId, Long triggerMessageId) {
CompletableFuture.runAsync(() -> generateReply(conversationId, triggerMessageId));
}
private void generateReply(Long conversationId) {
private void generateReply(Long conversationId, Long triggerMessageId) {
try {
if (triggerMessageId == null) {
return;
}
if (apiKey == null || apiKey.isBlank()) {
log.debug("Skipping OpenRouter reply for conversation {} because no API key is configured", conversationId);
return;
@@ -87,12 +90,20 @@ public class OpenRouterAiService {
User botUser = userRepository.findByUsername(BOT_USERNAME)
.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);
if (history.isEmpty()) {
return;
}
Message lastMessage = history.get(history.size() - 1);
if (!triggerMessageId.equals(lastMessage.getId())) {
return;
}
String lastContent = normalizeContent(lastMessage.getContent());
if (lastContent.isBlank()) {
return;
@@ -146,6 +157,18 @@ public class OpenRouterAiService {
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);
chatRealtimeService.publishMessage(conversationId, messageResponse);
chatRealtimeService.publishConversationUpdate(conversationId);