Ver Fonte

记忆调整

Grizzly há 5 meses atrás
pai
commit
94a7e0f927

+ 1 - 1
src/main/java/edu/nju/software/aipaasagent/client/mcp/tool/examples/WeatherTool.java → src/main/java/edu/nju/software/aipaasagent/mcp/tool/examples/WeatherTool.java

@@ -1,4 +1,4 @@
-package edu.nju.software.aipaasagent.client.mcp.tool.examples;
+package edu.nju.software.aipaasagent.mcp.tool.examples;
 
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.ai.tool.annotation.Tool;

+ 23 - 2
src/main/java/edu/nju/software/aipaasagent/memory/chat/RedisBasedChatMemory.java

@@ -47,7 +47,7 @@ public class RedisBasedChatMemory implements ChatMemory {
             String key = buildKey(conversationId);
 
             // 获取现有消息
-            List<Message> conversationMessages = get(conversationId, Integer.MAX_VALUE);
+            List<Message> conversationMessages = get(conversationId);
 
             // 添加新消息
             conversationMessages.addAll(messages);
@@ -63,8 +63,29 @@ public class RedisBasedChatMemory implements ChatMemory {
         }
     }
 
+
     @Override
-    public List<Message> get(String conversationId, int lastN) {
+    public List<Message> get(String conversationId){
+        try {
+            String key = buildKey(conversationId);
+            byte[] serialized = redisTemplate.opsForValue().get(key);
+
+            if (serialized == null) {
+                log.debug("对话不存在: {}", conversationId);
+                return new ArrayList<>();
+            }
+
+            // 反序列化
+            List<Message> allMessages = deserialize(serialized);
+            return allMessages;
+        } catch (Exception e) {
+            log.error("获取对话消息失败: {}", conversationId, e);
+            throw new RuntimeException("获取消息失败", e);
+        }
+    }
+
+
+    public List<Message> getLastMessage(String conversationId, Integer lastN) {
         try {
             String key = buildKey(conversationId);
             byte[] serialized = redisTemplate.opsForValue().get(key);