mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(memory): remove legacy dialogMap in MemoryRuntime and related modules
This commit is contained in:
@@ -25,7 +25,6 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
@@ -47,7 +46,6 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
|||||||
private final ReentrantLock runtimeLock = new ReentrantLock();
|
private final ReentrantLock runtimeLock = new ReentrantLock();
|
||||||
private Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = new HashMap<>();
|
private Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = new HashMap<>();
|
||||||
private Map<LocalDate, CopyOnWriteArrayList<SliceRef>> dateIndex = new HashMap<>();
|
private Map<LocalDate, CopyOnWriteArrayList<SliceRef>> dateIndex = new HashMap<>();
|
||||||
private HashMap<LocalDateTime, String> dialogMap = new HashMap<>();
|
|
||||||
|
|
||||||
@Init
|
@Init
|
||||||
public void init() {
|
public void init() {
|
||||||
@@ -79,7 +77,7 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void recordMemory(MemoryUnit memoryUnit, String topicPath, List<String> relatedTopicPaths, String dialogSummary) {
|
public void recordMemory(MemoryUnit memoryUnit, String topicPath, List<String> relatedTopicPaths) {
|
||||||
memoryCapability.saveMemoryUnit(memoryUnit);
|
memoryCapability.saveMemoryUnit(memoryUnit);
|
||||||
MemorySlice memorySlice = memoryUnit.getSlices().getLast();
|
MemorySlice memorySlice = memoryUnit.getSlices().getLast();
|
||||||
SliceRef sliceRef = new SliceRef(memoryUnit.getId(), memorySlice.getId());
|
SliceRef sliceRef = new SliceRef(memoryUnit.getId(), memorySlice.getId());
|
||||||
@@ -90,7 +88,6 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
|||||||
bindTopic(relatedTopicPath, sliceRef);
|
bindTopic(relatedTopicPath, sliceRef);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateDialogMap(LocalDateTime.now(), dialogSummary);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void indexMemoryUnit(MemoryUnit memoryUnit) {
|
private void indexMemoryUnit(MemoryUnit memoryUnit) {
|
||||||
@@ -139,40 +136,6 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
|||||||
return buildActivatedMemorySlices(findByDate(date));
|
return buildActivatedMemorySlices(findByDate(date));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDialogMap(LocalDateTime dateTime, String newDialogCache) {
|
|
||||||
runtimeLock.lock();
|
|
||||||
try {
|
|
||||||
List<LocalDateTime> keysToRemove = new ArrayList<>();
|
|
||||||
dialogMap.forEach((k, v) -> {
|
|
||||||
if (dateTime.minusDays(2).isAfter(k)) {
|
|
||||||
keysToRemove.add(k);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
for (LocalDateTime temp : keysToRemove) {
|
|
||||||
dialogMap.remove(temp);
|
|
||||||
}
|
|
||||||
dialogMap.put(dateTime, newDialogCache);
|
|
||||||
saveState();
|
|
||||||
} finally {
|
|
||||||
runtimeLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDialogMapStr() {
|
|
||||||
StringBuilder str = new StringBuilder();
|
|
||||||
dialogMap.entrySet().stream()
|
|
||||||
.sorted(Map.Entry.comparingByKey())
|
|
||||||
.forEach(entry -> str.append("\n\n[")
|
|
||||||
.append(entry.getKey())
|
|
||||||
.append("]\n")
|
|
||||||
.append(entry.getValue()));
|
|
||||||
return str.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean containsDialogSummary(String summary) {
|
|
||||||
return dialogMap.containsValue(summary);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTopicTree() {
|
public String getTopicTree() {
|
||||||
TopicTreeNode root = new TopicTreeNode();
|
TopicTreeNode root = new TopicTreeNode();
|
||||||
for (Map.Entry<String, CopyOnWriteArrayList<SliceRef>> entry : topicSlices.entrySet()) {
|
for (Map.Entry<String, CopyOnWriteArrayList<SliceRef>> entry : topicSlices.entrySet()) {
|
||||||
@@ -267,12 +230,10 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
|||||||
RuntimeState state = (RuntimeState) ois.readObject();
|
RuntimeState state = (RuntimeState) ois.readObject();
|
||||||
topicSlices = state.topicSlices;
|
topicSlices = state.topicSlices;
|
||||||
dateIndex = state.dateIndex;
|
dateIndex = state.dateIndex;
|
||||||
dialogMap = state.dialogMap;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[MemoryRuntime] 加载运行态失败", e);
|
log.error("[MemoryRuntime] 加载运行态失败", e);
|
||||||
topicSlices = new HashMap<>();
|
topicSlices = new HashMap<>();
|
||||||
dateIndex = new HashMap<>();
|
dateIndex = new HashMap<>();
|
||||||
dialogMap = new HashMap<>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,7 +256,6 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
|||||||
RuntimeState state = new RuntimeState();
|
RuntimeState state = new RuntimeState();
|
||||||
state.topicSlices = new HashMap<>(topicSlices);
|
state.topicSlices = new HashMap<>(topicSlices);
|
||||||
state.dateIndex = new HashMap<>(dateIndex);
|
state.dateIndex = new HashMap<>(dateIndex);
|
||||||
state.dialogMap = new HashMap<>(dialogMap);
|
|
||||||
oos.writeObject(state);
|
oos.writeObject(state);
|
||||||
}
|
}
|
||||||
Files.move(tempPath, filePath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
Files.move(tempPath, filePath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
||||||
@@ -325,6 +285,5 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
|||||||
|
|
||||||
private Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = new HashMap<>();
|
private Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = new HashMap<>();
|
||||||
private Map<LocalDate, CopyOnWriteArrayList<SliceRef>> dateIndex = new HashMap<>();
|
private Map<LocalDate, CopyOnWriteArrayList<SliceRef>> dateIndex = new HashMap<>();
|
||||||
private HashMap<LocalDateTime, String> dialogMap = new HashMap<>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -210,7 +210,6 @@ public class MemorySelector extends AbstractAgentModule.Running<PartnerRunningFl
|
|||||||
log.debug("[MemorySelector] 触发记忆回溯...");
|
log.debug("[MemorySelector] 触发记忆回溯...");
|
||||||
LinkedHashMap<String, ActivatedMemorySlice> candidates = new LinkedHashMap<>();
|
LinkedHashMap<String, ActivatedMemorySlice> candidates = new LinkedHashMap<>();
|
||||||
setMemoryCandidates(candidates, extractorResult.getMatches());
|
setMemoryCandidates(candidates, extractorResult.getMatches());
|
||||||
removeDuplicateSlice(candidates.values());
|
|
||||||
EvaluatorInput evaluatorInput = EvaluatorInput.builder()
|
EvaluatorInput evaluatorInput = EvaluatorInput.builder()
|
||||||
.inputs(snapshotInputs)
|
.inputs(snapshotInputs)
|
||||||
.memorySlices(new ArrayList<>(candidates.values()))
|
.memorySlices(new ArrayList<>(candidates.values()))
|
||||||
@@ -239,10 +238,6 @@ public class MemorySelector extends AbstractAgentModule.Running<PartnerRunningFl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDuplicateSlice(Collection<ActivatedMemorySlice> candidates) {
|
|
||||||
candidates.removeIf(m -> memoryRuntime.containsDialogSummary(m.getSummary()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int order() {
|
public int order() {
|
||||||
return 2;
|
return 2;
|
||||||
|
|||||||
@@ -172,8 +172,7 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
|||||||
memoryRuntime.recordMemory(
|
memoryRuntime.recordMemory(
|
||||||
memoryUnit,
|
memoryUnit,
|
||||||
summarizeResult.getTopicPath(),
|
summarizeResult.getTopicPath(),
|
||||||
summarizeResult.getRelatedTopicPath(),
|
summarizeResult.getRelatedTopicPath()
|
||||||
summarizeResult.getSummary()
|
|
||||||
);
|
);
|
||||||
log.debug("[MemoryUpdater] 记忆更新流程结束...");
|
log.debug("[MemoryUpdater] 记忆更新流程结束...");
|
||||||
MemorySlice newSlice = memoryUnit.getSlices().getLast();
|
MemorySlice newSlice = memoryUnit.getSlices().getLast();
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ class MemoryRuntimeTest {
|
|||||||
|
|
||||||
unit.setSlices(new ArrayList<>(List.of(firstSlice, secondSlice)));
|
unit.setSlices(new ArrayList<>(List.of(firstSlice, secondSlice)));
|
||||||
|
|
||||||
runtime.recordMemory(unit, "topic/main", List.of("topic/related"), "dialog-summary");
|
runtime.recordMemory(unit, "topic/main", List.of("topic/related"));
|
||||||
|
|
||||||
Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = topicSlices(runtime);
|
Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = topicSlices(runtime);
|
||||||
assertEquals(List.of("slice-2"),
|
assertEquals(List.of("slice-2"),
|
||||||
|
|||||||
Reference in New Issue
Block a user