|
@@ -47,7 +47,7 @@ public class RedisBasedChatMemory implements ChatMemory {
|
|
|
String key = buildKey(conversationId);
|
|
String key = buildKey(conversationId);
|
|
|
|
|
|
|
|
// 获取现有消息
|
|
// 获取现有消息
|
|
|
- List<Message> conversationMessages = get(conversationId, Integer.MAX_VALUE);
|
|
|
|
|
|
|
+ List<Message> conversationMessages = get(conversationId);
|
|
|
|
|
|
|
|
// 添加新消息
|
|
// 添加新消息
|
|
|
conversationMessages.addAll(messages);
|
|
conversationMessages.addAll(messages);
|
|
@@ -63,8 +63,29 @@ public class RedisBasedChatMemory implements ChatMemory {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@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 {
|
|
try {
|
|
|
String key = buildKey(conversationId);
|
|
String key = buildKey(conversationId);
|
|
|
byte[] serialized = redisTemplate.opsForValue().get(key);
|
|
byte[] serialized = redisTemplate.opsForValue().get(key);
|