mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-14 09:43:03 +08:00
refactor(memory): correct memorySlice-topicPath binding behavior and adjust slice index into [startIndex,endIndex)
This commit is contained in:
@@ -100,7 +100,7 @@ public class MemoryCore extends PartnerCore<MemoryCore> {
|
||||
if (memoryUnit.getSlices() == null) {
|
||||
memoryUnit.setSlices(new ArrayList<>());
|
||||
}
|
||||
int maxIndex = Math.max(memoryUnit.getConversationMessages().size() - 1, 0);
|
||||
int maxEndExclusive = Math.max(memoryUnit.getConversationMessages().size(), 0);
|
||||
for (MemorySlice slice : memoryUnit.getSlices()) {
|
||||
if (slice.getId() == null || slice.getId().isBlank()) {
|
||||
slice.setId(UUID.randomUUID().toString());
|
||||
@@ -111,11 +111,14 @@ public class MemoryCore extends PartnerCore<MemoryCore> {
|
||||
if (slice.getStartIndex() == null || slice.getStartIndex() < 0) {
|
||||
slice.setStartIndex(0);
|
||||
}
|
||||
if (slice.getEndIndex() == null || slice.getEndIndex() < slice.getStartIndex()) {
|
||||
slice.setEndIndex(maxIndex);
|
||||
if (slice.getStartIndex() > maxEndExclusive) {
|
||||
slice.setStartIndex(maxEndExclusive);
|
||||
}
|
||||
if (slice.getEndIndex() > maxIndex) {
|
||||
slice.setEndIndex(maxIndex);
|
||||
if (slice.getEndIndex() == null || slice.getEndIndex() < slice.getStartIndex()) {
|
||||
slice.setEndIndex(maxEndExclusive);
|
||||
}
|
||||
if (slice.getEndIndex() > maxEndExclusive) {
|
||||
slice.setEndIndex(maxEndExclusive);
|
||||
}
|
||||
}
|
||||
memoryUnit.getSlices().sort(Comparator.naturalOrder());
|
||||
|
||||
@@ -7,6 +7,7 @@ import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapabili
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.agent.runtime.config.AgentConfigLoader;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.api.common.entity.PersistableObject;
|
||||
import work.slhaf.partner.common.config.PartnerAgentConfigLoader;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
@@ -80,7 +81,7 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
||||
|
||||
public void recordMemory(MemoryUnit memoryUnit, String topicPath, List<String> relatedTopicPaths, String dialogSummary) {
|
||||
memoryCapability.saveMemoryUnit(memoryUnit);
|
||||
MemorySlice memorySlice = memoryUnit.getSlices().getFirst();
|
||||
MemorySlice memorySlice = memoryUnit.getSlices().getLast();
|
||||
SliceRef sliceRef = new SliceRef(memoryUnit.getId(), memorySlice.getId());
|
||||
indexMemoryUnit(memoryUnit);
|
||||
bindTopic(topicPath, sliceRef);
|
||||
@@ -209,7 +210,7 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
||||
if (memoryUnit == null || memorySlice == null) {
|
||||
return null;
|
||||
}
|
||||
List<work.slhaf.partner.api.chat.pojo.Message> messages = sliceMessages(memoryUnit, memorySlice);
|
||||
List<Message> messages = sliceMessages(memoryUnit, memorySlice);
|
||||
LocalDate date = Instant.ofEpochMilli(memorySlice.getTimestamp())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
@@ -223,17 +224,18 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
|
||||
.build();
|
||||
}
|
||||
|
||||
private List<work.slhaf.partner.api.chat.pojo.Message> sliceMessages(MemoryUnit memoryUnit, MemorySlice memorySlice) {
|
||||
List<work.slhaf.partner.api.chat.pojo.Message> conversationMessages = memoryUnit.getConversationMessages();
|
||||
private List<Message> sliceMessages(MemoryUnit memoryUnit, MemorySlice memorySlice) {
|
||||
List<Message> conversationMessages = memoryUnit.getConversationMessages();
|
||||
if (conversationMessages == null || conversationMessages.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
int start = Math.max(0, memorySlice.getStartIndex());
|
||||
int end = Math.min(conversationMessages.size() - 1, memorySlice.getEndIndex());
|
||||
if (start > end) {
|
||||
int size = conversationMessages.size();
|
||||
int start = Math.max(0, Math.min(memorySlice.getStartIndex(), size));
|
||||
int end = Math.max(start, Math.min(memorySlice.getEndIndex(), size));
|
||||
if (start >= end) {
|
||||
return List.of();
|
||||
}
|
||||
return new ArrayList<>(conversationMessages.subList(start, end + 1));
|
||||
return new ArrayList<>(conversationMessages.subList(start, end));
|
||||
}
|
||||
|
||||
private void printSubTopicsTreeFormat(TopicTreeNode node, String prefix, StringBuilder stringBuilder) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import kotlin.Unit;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
@@ -81,7 +82,7 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(PartnerRunningFlowContext context) {
|
||||
public void execute(@NotNull PartnerRunningFlowContext context) {
|
||||
boolean trigger = cognitionCapability.getChatMessages().size() >= MEMORY_UPDATE_TRIGGER_ROLL_LIMIT;
|
||||
if (!trigger) {
|
||||
return;
|
||||
@@ -149,19 +150,33 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
|
||||
private MemoryUnit buildMemoryUnit(List<Message> chatMessages, SummarizeResult summarizeResult) {
|
||||
long now = System.currentTimeMillis();
|
||||
String memoryId = memoryCapability.getMemorySessionId();
|
||||
String resolvedMemoryId = memoryId == null || memoryId.isBlank() ? UUID.randomUUID().toString() : memoryId;
|
||||
MemoryUnit existingUnit = memoryCapability.getMemoryUnit(resolvedMemoryId);
|
||||
List<Message> existingMessages = existingUnit != null && existingUnit.getConversationMessages() != null
|
||||
? existingUnit.getConversationMessages()
|
||||
: List.of();
|
||||
int startIndex = existingMessages.size();
|
||||
|
||||
MemorySlice memorySlice = new MemorySlice();
|
||||
memorySlice.setId(UUID.randomUUID().toString());
|
||||
memorySlice.setStartIndex(0);
|
||||
memorySlice.setEndIndex(chatMessages.size());
|
||||
memorySlice.setStartIndex(startIndex);
|
||||
memorySlice.setEndIndex(startIndex + chatMessages.size());
|
||||
memorySlice.setSummary(summarizeResult.getSummary());
|
||||
memorySlice.setTimestamp(now);
|
||||
|
||||
MemoryUnit memoryUnit = new MemoryUnit();
|
||||
String memoryId = memoryCapability.getMemorySessionId();
|
||||
memoryUnit.setId(memoryId == null || memoryId.isBlank() ? UUID.randomUUID().toString() : memoryId);
|
||||
memoryUnit.setId(resolvedMemoryId);
|
||||
memoryUnit.setTimestamp(now);
|
||||
memoryUnit.setConversationMessages(new ArrayList<>(chatMessages));
|
||||
memoryUnit.setSlices(new ArrayList<>(List.of(memorySlice)));
|
||||
List<Message> conversationMessages = new ArrayList<>(existingMessages);
|
||||
conversationMessages.addAll(chatMessages);
|
||||
memoryUnit.setConversationMessages(conversationMessages);
|
||||
|
||||
List<MemorySlice> slices = existingUnit != null && existingUnit.getSlices() != null
|
||||
? new ArrayList<>(existingUnit.getSlices())
|
||||
: new ArrayList<>();
|
||||
slices.add(memorySlice);
|
||||
memoryUnit.setSlices(slices);
|
||||
return memoryUnit;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user