mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(memory): manage state serialization via StateCenter in MemoryCore, and normalize slice and unit building
This commit is contained in:
@@ -11,7 +11,6 @@ import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -57,14 +56,13 @@ class MemoryCoreTest {
|
||||
slice.setStartIndex(1);
|
||||
slice.setEndIndex(99);
|
||||
|
||||
MemoryUnit unit = new MemoryUnit();
|
||||
unit.setId("unit-1");
|
||||
unit.setConversationMessages(new ArrayList<>(List.of(
|
||||
MemoryUnit unit = new MemoryUnit("unit-1");
|
||||
unit.getConversationMessages().addAll(List.of(
|
||||
new Message(Message.Character.USER, "m0"),
|
||||
new Message(Message.Character.USER, "m1"),
|
||||
new Message(Message.Character.USER, "m2")
|
||||
)));
|
||||
unit.setSlices(new ArrayList<>(List.of(slice)));
|
||||
));
|
||||
unit.getSlices().add(slice);
|
||||
|
||||
memoryCore.saveMemoryUnit(unit);
|
||||
|
||||
|
||||
@@ -74,13 +74,13 @@ class MemoryRuntimeTest {
|
||||
@Test
|
||||
void shouldSliceMessagesUsingLeftClosedRightOpenRange() throws Exception {
|
||||
MemoryRuntime runtime = new MemoryRuntime();
|
||||
MemoryUnit unit = new MemoryUnit();
|
||||
unit.setConversationMessages(new ArrayList<>(List.of(
|
||||
MemoryUnit unit = new MemoryUnit("unit-1");
|
||||
unit.getConversationMessages().addAll(List.of(
|
||||
message("m0"),
|
||||
message("m1"),
|
||||
message("m2"),
|
||||
message("m3")
|
||||
)));
|
||||
));
|
||||
|
||||
MemorySlice slice = new MemorySlice();
|
||||
slice.setStartIndex(1);
|
||||
@@ -105,14 +105,13 @@ class MemoryRuntimeTest {
|
||||
MemoryRuntime runtime = new MemoryRuntime();
|
||||
setField(runtime, "memoryCapability", memoryCapability);
|
||||
|
||||
MemoryUnit unit = new MemoryUnit();
|
||||
unit.setId("unit-1");
|
||||
unit.setConversationMessages(new ArrayList<>(List.of(
|
||||
MemoryUnit unit = new MemoryUnit("unit-1");
|
||||
unit.getConversationMessages().addAll(List.of(
|
||||
message("m0"),
|
||||
message("m1"),
|
||||
message("m2"),
|
||||
message("m3")
|
||||
)));
|
||||
));
|
||||
|
||||
MemorySlice firstSlice = new MemorySlice();
|
||||
firstSlice.setId("slice-1");
|
||||
@@ -128,7 +127,7 @@ class MemoryRuntimeTest {
|
||||
secondSlice.setSummary("second");
|
||||
secondSlice.setTimestamp(2L);
|
||||
|
||||
unit.setSlices(new ArrayList<>(List.of(firstSlice, secondSlice)));
|
||||
unit.getSlices().addAll(List.of(firstSlice, secondSlice));
|
||||
|
||||
runtime.recordMemory(unit, "topic/main", List.of("topic/related"));
|
||||
|
||||
|
||||
@@ -9,7 +9,10 @@ import work.slhaf.partner.module.memory.updater.summarizer.entity.SummarizeResul
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -55,19 +58,18 @@ class MemoryUpdaterTest {
|
||||
setField(updater, "memoryCapability", memoryCapability);
|
||||
|
||||
String sessionId = memoryCapability.getMemorySessionId();
|
||||
MemoryUnit existingUnit = new MemoryUnit();
|
||||
existingUnit.setId(sessionId);
|
||||
existingUnit.setConversationMessages(new ArrayList<>(List.of(
|
||||
MemoryUnit existingUnit = new MemoryUnit(sessionId);
|
||||
existingUnit.getConversationMessages().addAll(List.of(
|
||||
message(Message.Character.USER, "old-user"),
|
||||
message(Message.Character.ASSISTANT, "old-assistant")
|
||||
)));
|
||||
));
|
||||
MemorySlice existingSlice = new MemorySlice();
|
||||
existingSlice.setId("slice-1");
|
||||
existingSlice.setStartIndex(0);
|
||||
existingSlice.setEndIndex(2);
|
||||
existingSlice.setSummary("old-summary");
|
||||
existingSlice.setTimestamp(1L);
|
||||
existingUnit.setSlices(new ArrayList<>(List.of(existingSlice)));
|
||||
existingUnit.getSlices().add(existingSlice);
|
||||
memoryCapability.saveMemoryUnit(existingUnit);
|
||||
|
||||
MemoryUnit merged = invokeBuildMemoryUnit(
|
||||
@@ -121,14 +123,13 @@ class MemoryUpdaterTest {
|
||||
MemoryUpdater updater = new MemoryUpdater();
|
||||
setField(updater, "memoryCapability", memoryCapability);
|
||||
|
||||
MemoryUnit existingUnit = new MemoryUnit();
|
||||
existingUnit.setId("session-3");
|
||||
existingUnit.setConversationMessages(new ArrayList<>(List.of(
|
||||
MemoryUnit existingUnit = new MemoryUnit("session-3");
|
||||
existingUnit.getConversationMessages().addAll(List.of(
|
||||
message(Message.Character.USER, "m1"),
|
||||
message(Message.Character.ASSISTANT, "m2"),
|
||||
message(Message.Character.USER, "m3"),
|
||||
message(Message.Character.ASSISTANT, "m4")
|
||||
)));
|
||||
));
|
||||
memoryCapability.saveMemoryUnit(existingUnit);
|
||||
|
||||
List<Message> increment = invokeResolveChatIncrement(
|
||||
@@ -150,13 +151,12 @@ class MemoryUpdaterTest {
|
||||
MemoryUpdater updater = new MemoryUpdater();
|
||||
setField(updater, "memoryCapability", memoryCapability);
|
||||
|
||||
MemoryUnit existingUnit = new MemoryUnit();
|
||||
existingUnit.setId("session-4");
|
||||
existingUnit.setConversationMessages(new ArrayList<>(List.of(
|
||||
MemoryUnit existingUnit = new MemoryUnit("session-4");
|
||||
existingUnit.getConversationMessages().addAll(List.of(
|
||||
message(Message.Character.USER, "m1"),
|
||||
message(Message.Character.ASSISTANT, "m2"),
|
||||
message(Message.Character.USER, "m3")
|
||||
)));
|
||||
));
|
||||
memoryCapability.saveMemoryUnit(existingUnit);
|
||||
|
||||
List<Message> increment = invokeResolveChatIncrement(
|
||||
|
||||
Reference in New Issue
Block a user