Lots of bug squashing

This commit is contained in:
Mahesh Kommareddi 2024-07-17 04:12:16 -04:00
parent fcd29de36a
commit a68d6fd419
9 changed files with 90 additions and 11 deletions

View File

@ -13,10 +13,26 @@ import com.ioa.model.BedrockLanguageModel;
import com.ioa.service.WebSocketService; import com.ioa.service.WebSocketService;
import com.ioa.util.TreeOfThought; 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.context.ConfigurableApplicationContext;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
import java.util.Arrays; import java.util.Arrays;
@ -26,12 +42,12 @@ import java.util.List;
public class IoASystem { public class IoASystem {
@Bean @Bean
public WebSocketService webSocketService(SimpMessagingTemplate messagingTemplate) { public WebSocketService webSocketService(SimpMessagingTemplate messagingTemplate, @Lazy ConversationManager conversationManager) {
return new WebSocketService(messagingTemplate); return new WebSocketService(messagingTemplate, conversationManager);
} }
@Bean @Bean
public ConversationManager conversationManager(BedrockLanguageModel model, WebSocketService webSocketService) { public ConversationManager conversationManager(BedrockLanguageModel model, @Lazy WebSocketService webSocketService) {
return new ConversationManager(model, webSocketService); return new ConversationManager(model, webSocketService);
} }

View File

@ -61,10 +61,23 @@ public class ConversationFSM {
} }
private void handleMessage(Message message) { 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()); handleVote(message.getSender());
} else { } 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 + "\nCurrent state: " + currentState +
"\nParticipants: " + participants + "\nParticipants: " + participants +
"\nPossible states: " + Arrays.toString(ConversationState.values()); "\nPossible states: " + Arrays.toString(ConversationState.values());

View File

@ -22,4 +22,13 @@ public class Message {
public String getContent() { public String getContent() {
return content; return content;
} }
@Override
public String toString() {
return "Message{" +
"conversationId='" + conversationId + '\'' +
", sender='" + sender + '\'' +
", content='" + content + '\'' +
'}';
}
} }

View File

@ -1,21 +1,62 @@
package com.ioa.service; 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.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Lazy;
@Service @Service
public class WebSocketService { public class WebSocketService {
private final SimpMessagingTemplate messagingTemplate; 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.messagingTemplate = messagingTemplate;
this.conversationManager = conversationManager;
this.objectMapper = new ObjectMapper();
} }
public void sendUpdate(String topic, Object payload) { public void sendUpdate(String topic, Object payload) {
if (payload != null) {
messagingTemplate.convertAndSend("/topic/" + topic, payload); messagingTemplate.convertAndSend("/topic/" + topic, payload);
} else { }
System.out.println("Warning: Attempted to send null payload to topic: " + topic);
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();
} }
} }
} }