diff --git a/src/main/java/com/ioa/IoASystem.java b/src/main/java/com/ioa/IoASystem.java index c775c23..940fd7f 100644 --- a/src/main/java/com/ioa/IoASystem.java +++ b/src/main/java/com/ioa/IoASystem.java @@ -13,10 +13,26 @@ import com.ioa.model.BedrockLanguageModel; import com.ioa.service.WebSocketService; import com.ioa.util.TreeOfThought; +import com.ioa.tool.common.AppointmentSchedulerTool; +import com.ioa.tool.common.DistanceCalculatorTool; +import com.ioa.tool.common.FinancialAdviceTool; +import com.ioa.tool.common.FitnessClassFinderTool; +import com.ioa.tool.common.MovieRecommendationTool; +import com.ioa.tool.common.NewsUpdateTool; +import com.ioa.tool.common.PriceComparisonTool; +import com.ioa.tool.common.RecipeTool; +import com.ioa.tool.common.ReminderTool; +import com.ioa.tool.common.RestaurantFinderTool; +import com.ioa.tool.common.TranslationTool; +import com.ioa.tool.common.TravelBookingTool; +import com.ioa.tool.common.WeatherTool; +import com.ioa.tool.common.WebSearchTool; + import org.springframework.context.ConfigurableApplicationContext; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Lazy; import org.springframework.messaging.simp.SimpMessagingTemplate; import java.util.Arrays; @@ -26,12 +42,12 @@ import java.util.List; public class IoASystem { @Bean - public WebSocketService webSocketService(SimpMessagingTemplate messagingTemplate) { - return new WebSocketService(messagingTemplate); + public WebSocketService webSocketService(SimpMessagingTemplate messagingTemplate, @Lazy ConversationManager conversationManager) { + return new WebSocketService(messagingTemplate, conversationManager); } @Bean - public ConversationManager conversationManager(BedrockLanguageModel model, WebSocketService webSocketService) { + public ConversationManager conversationManager(BedrockLanguageModel model, @Lazy WebSocketService webSocketService) { return new ConversationManager(model, webSocketService); } diff --git a/src/main/java/com/ioa/conversation/ConversationFSM.java b/src/main/java/com/ioa/conversation/ConversationFSM.java index 2048686..1547e48 100644 --- a/src/main/java/com/ioa/conversation/ConversationFSM.java +++ b/src/main/java/com/ioa/conversation/ConversationFSM.java @@ -61,10 +61,23 @@ public class ConversationFSM { } private void handleMessage(Message message) { - if (message.getContent().startsWith("/vote")) { + if (message == null) { + System.out.println("DEBUG: Received null message"); + return; + } + + String content = message.getContent(); + if (content == null) { + System.out.println("DEBUG: Message content is null"); + return; + } + + System.out.println("DEBUG: Received message: " + content); + + if (content.startsWith("/vote")) { handleVote(message.getSender()); } else { - String stateTransitionTask = "Decide the next conversation state based on this message: " + message.getContent() + + String stateTransitionTask = "Decide the next conversation state based on this message: " + content + "\nCurrent state: " + currentState + "\nParticipants: " + participants + "\nPossible states: " + Arrays.toString(ConversationState.values()); @@ -86,7 +99,7 @@ public class ConversationFSM { agent.receiveMessage(message); } } - + webSocketService.sendUpdate("conversation_message", message); } } diff --git a/src/main/java/com/ioa/conversation/Message.java b/src/main/java/com/ioa/conversation/Message.java index 1e02b49..48567dc 100644 --- a/src/main/java/com/ioa/conversation/Message.java +++ b/src/main/java/com/ioa/conversation/Message.java @@ -22,4 +22,13 @@ public class Message { public String getContent() { return content; } + + @Override + public String toString() { + return "Message{" + + "conversationId='" + conversationId + '\'' + + ", sender='" + sender + '\'' + + ", content='" + content + '\'' + + '}'; + } } \ No newline at end of file diff --git a/src/main/java/com/ioa/service/WebSocketService.java b/src/main/java/com/ioa/service/WebSocketService.java index a4ee469..a593391 100644 --- a/src/main/java/com/ioa/service/WebSocketService.java +++ b/src/main/java/com/ioa/service/WebSocketService.java @@ -1,21 +1,62 @@ package com.ioa.service; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.JsonNode; +import com.ioa.conversation.ConversationManager; +import com.ioa.conversation.Message; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Service; +import org.springframework.context.annotation.Lazy; @Service public class WebSocketService { private final SimpMessagingTemplate messagingTemplate; + private final ConversationManager conversationManager; + private final ObjectMapper objectMapper; - public WebSocketService(SimpMessagingTemplate messagingTemplate) { + @Autowired + public WebSocketService(SimpMessagingTemplate messagingTemplate, @Lazy ConversationManager conversationManager) { this.messagingTemplate = messagingTemplate; + this.conversationManager = conversationManager; + this.objectMapper = new ObjectMapper(); } public void sendUpdate(String topic, Object payload) { - if (payload != null) { - messagingTemplate.convertAndSend("/topic/" + topic, payload); - } else { - System.out.println("Warning: Attempted to send null payload to topic: " + topic); + messagingTemplate.convertAndSend("/topic/" + topic, payload); + } + + public void handleWebSocketMessage(String message) { + System.out.println("DEBUG: Received WebSocket message: " + message); + + // Parse the WebSocket frame + String[] parts = message.split("\n\n", 2); + if (parts.length < 2) { + System.out.println("DEBUG: Invalid WebSocket message format"); + return; + } + + String headers = parts[0]; + String payload = parts[1]; + + // Parse the JSON payload + try { + JsonNode jsonNode = objectMapper.readTree(payload); + + // Extract relevant information from the JSON + // Adjust this based on the actual structure of your WebSocket messages + String conversationId = jsonNode.path("conversationId").asText(); + String sender = jsonNode.path("sender").asText(); + String content = jsonNode.path("content").asText(); + + // Create a new Message object + Message parsedMessage = new Message(conversationId, sender, content); + + // Process the message + conversationManager.postMessage(conversationId, sender, content); + } catch (Exception e) { + System.out.println("DEBUG: Error parsing WebSocket message: " + e.getMessage()); + e.printStackTrace(); } } } \ No newline at end of file diff --git a/target/classes/com/ioa/IoASystem.class b/target/classes/com/ioa/IoASystem.class index 3c4d13c..df0d0f8 100644 Binary files a/target/classes/com/ioa/IoASystem.class and b/target/classes/com/ioa/IoASystem.class differ diff --git a/target/classes/com/ioa/conversation/ConversationFSM$ConversationStateUpdate.class b/target/classes/com/ioa/conversation/ConversationFSM$ConversationStateUpdate.class index 7d069d0..2c2c017 100644 Binary files a/target/classes/com/ioa/conversation/ConversationFSM$ConversationStateUpdate.class and b/target/classes/com/ioa/conversation/ConversationFSM$ConversationStateUpdate.class differ diff --git a/target/classes/com/ioa/conversation/ConversationFSM.class b/target/classes/com/ioa/conversation/ConversationFSM.class index 7850dc0..da0dd84 100644 Binary files a/target/classes/com/ioa/conversation/ConversationFSM.class and b/target/classes/com/ioa/conversation/ConversationFSM.class differ diff --git a/target/classes/com/ioa/conversation/Message.class b/target/classes/com/ioa/conversation/Message.class index e65d262..7ea1be2 100644 Binary files a/target/classes/com/ioa/conversation/Message.class and b/target/classes/com/ioa/conversation/Message.class differ diff --git a/target/classes/com/ioa/service/WebSocketService.class b/target/classes/com/ioa/service/WebSocketService.class index cbdc7d7..8918a6b 100644 Binary files a/target/classes/com/ioa/service/WebSocketService.class and b/target/classes/com/ioa/service/WebSocketService.class differ