feat(memory): 实现记忆切片持久化并优化记忆存储结构- 新增 ChatClient 类实现与大模型的交互

- 添加了chat包,用于后续大模型对接
- 更新 MemoryGraph 类,增加用户对话缓存和当前对话压缩上下文
- 修改 MemoryNode 类,实现记忆切片的序列化和反序列化
- 更新 MemorySlice 类,增加多用户相关字段和方法,将切片内容从SliceData移动至MemorySlice
- 删除未使用的 SliceData 类
- 添加日志依赖和异常处理,新的异常类NullSliceListException
This commit is contained in:
2025-04-11 21:50:11 +08:00
parent 24d4510270
commit c28979b495
14 changed files with 413 additions and 45 deletions

View File

@@ -7,8 +7,8 @@ import work.slhaf.memory.content.MemorySlice;
import work.slhaf.memory.node.MemoryNode;
import work.slhaf.memory.node.TopicNode;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
@@ -28,7 +28,7 @@ public class InsertTest {
}
@Test
public void testInsertMemory_NewRootTopic() {
public void testInsertMemory_NewRootTopic() throws IOException, ClassNotFoundException {
// 准备测试数据
List<String> topicPath = new LinkedList<>(Arrays.asList("Programming", "Java", "Collections"));
MemorySlice slice = createTestMemorySlice("slice1");
@@ -54,7 +54,7 @@ public class InsertTest {
}
@Test
public void testInsertMemory_ExistingTopicPath() {
public void testInsertMemory_ExistingTopicPath() throws IOException, ClassNotFoundException {
// 准备初始数据
List<String> topicPath1 = new LinkedList<>(Arrays.asList("Programming", "Java", "Collections"));
MemorySlice slice1 = createTestMemorySlice("slice1");
@@ -75,7 +75,7 @@ public class InsertTest {
}
@Test
public void testInsertMemory_DifferentDays() {
public void testInsertMemory_DifferentDays() throws IOException, ClassNotFoundException {
// 准备测试数据
List<String> topicPath = new LinkedList<>(Arrays.asList("Math", "Algebra"));
MemorySlice slice1 = createTestMemorySlice("slice1");
@@ -101,7 +101,7 @@ public class InsertTest {
}
@Test
public void testInsertMemory_PartialExistingPath() {
public void testInsertMemory_PartialExistingPath() throws IOException, ClassNotFoundException {
// 准备初始数据 - 创建部分路径
List<String> topicPath1 = new LinkedList<>(Arrays.asList("Science", "Physics"));
MemorySlice slice1 = createTestMemorySlice("slice1");
@@ -129,11 +129,10 @@ public class InsertTest {
}
@Test
public void testSerializationConsistency() {
public void testSerializationConsistency() throws IOException, ClassNotFoundException {
// 构造 MemorySlice
MemorySlice slice = new MemorySlice();
slice.setMemoryId("001");
slice.setSlicePath("/demo/path");
List<String> topicPath = Arrays.asList("生活", "学习", "Java");
@@ -160,7 +159,6 @@ public class InsertTest {
// 校验MemorySlice 内容一致
MemorySlice deserializedSlice = javaNode.getMemoryNodes().get(0).getMemorySliceList().get(0);
assertEquals("001", deserializedSlice.getMemoryId());
assertEquals("/demo/path", deserializedSlice.getSlicePath());
}
}

View File

@@ -8,6 +8,7 @@ import work.slhaf.memory.exception.UnExistedTopicException;
import work.slhaf.memory.node.MemoryNode;
import work.slhaf.memory.node.TopicNode;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@@ -21,7 +22,7 @@ class SearchTest {
// 初始化测试环境,模拟插入基础数据
@BeforeEach
void setUp() {
void setUp() throws IOException, ClassNotFoundException {
memoryGraph = new MemoryGraph("testGraph");
// 构建基础主题路径:根主题 -> 编程 -> Java
@@ -42,7 +43,7 @@ class SearchTest {
// 场景1查询存在的完整主题路径含相关主题
@Test
void selectMemory_shouldReturnTargetAndRelatedAndParentMemories() {
void selectMemory_shouldReturnTargetAndRelatedAndParentMemories() throws IOException, ClassNotFoundException {
// 准备相关主题数据:根主题 -> 算法 -> 排序
List<String> sortPath = new ArrayList<>();
sortPath.add("算法");
@@ -81,7 +82,7 @@ class SearchTest {
// 场景3无相关主题时仅返回目标节点和父节点记忆
@Test
void selectMemory_withoutRelatedTopics_shouldReturnTargetAndParent() {
void selectMemory_withoutRelatedTopics_shouldReturnTargetAndParent() throws IOException, ClassNotFoundException {
// 插入父级记忆:根主题 -> 编程
List<String> parentPath = new ArrayList<>();
parentPath.add("编程");
@@ -102,7 +103,7 @@ class SearchTest {
// 场景4验证日期排序应优先取最新日期的邻近记忆
@Test
void selectMemory_shouldGetLatestRelatedMemory() {
void selectMemory_shouldGetLatestRelatedMemory() throws IOException, ClassNotFoundException {
// 准备相关主题路径:根主题 -> 数据库
List<String> dbPath = new ArrayList<>();
dbPath.add("数据库");