mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(memory): add DialogRollingService to control message rolling logic
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
package work.slhaf.partner.module.communication;
|
||||||
|
|
||||||
|
import kotlin.Unit;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
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.abstracts.ActivateModel;
|
||||||
|
import work.slhaf.partner.api.chat.pojo.Message;
|
||||||
|
import work.slhaf.partner.core.cognition.BlockContent;
|
||||||
|
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||||
|
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||||
|
import work.slhaf.partner.module.TaskBlock;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class DialogRollingService extends AbstractAgentModule.Standalone implements ActivateModel {
|
||||||
|
|
||||||
|
@InjectCapability
|
||||||
|
private CognitionCapability cognitionCapability;
|
||||||
|
|
||||||
|
public void rollMessages(List<Message> snapshotMessages, int rollingSize, int retainSize) {
|
||||||
|
rollMessages(snapshotMessages, rollingSize, retainSize, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rollMessages(List<Message> snapshotMessages, int rollingSize, int retainSize, @Nullable String unitId, @Nullable String sliceId, @Nullable String summary) {
|
||||||
|
summary = summary == null ? summarize(snapshotMessages) : summary;
|
||||||
|
cognitionCapability.contextWorkspace().register(new ContextBlock(
|
||||||
|
buildDialogAbstractBlock(summary, unitId, sliceId),
|
||||||
|
Set.of(ContextBlock.VisibleDomain.MEMORY, ContextBlock.VisibleDomain.COMMUNICATION),
|
||||||
|
35,
|
||||||
|
8,
|
||||||
|
10
|
||||||
|
));
|
||||||
|
cognitionCapability.rollChatMessagesWithSnapshot(rollingSize, retainSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String summarize(List<Message> snapshotMessages) {
|
||||||
|
List<Message> messages = List.of(
|
||||||
|
resolveTaskBlock(snapshotMessages)
|
||||||
|
);
|
||||||
|
return chat(messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NotNull BlockContent buildDialogAbstractBlock(String summary, @Nullable String unitId, @Nullable String sliceId) {
|
||||||
|
return new BlockContent("dialog_history", "dialog_rolling_service") {
|
||||||
|
@Override
|
||||||
|
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||||
|
if (unitId != null) root.setAttribute("related_memory_unit_id", unitId);
|
||||||
|
if (sliceId != null) root.setAttribute("related_memory_slice_id", sliceId);
|
||||||
|
root.setAttribute("datetime", ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||||
|
|
||||||
|
appendTextElement(document, root, "summary", summary);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private Message resolveTaskBlock(List<Message> snapshotMessages) {
|
||||||
|
return new TaskBlock() {
|
||||||
|
@Override
|
||||||
|
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||||
|
appendRepeatedElements(document, root, "message", snapshotMessages, (messageElement, message) -> {
|
||||||
|
messageElement.setAttribute("role", message.getRole().name().toLowerCase(Locale.ROOT));
|
||||||
|
messageElement.setTextContent(message.getContent());
|
||||||
|
return Unit.INSTANCE;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}.encodeToMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ import work.slhaf.partner.core.memory.pojo.MemorySlice;
|
|||||||
import work.slhaf.partner.core.memory.pojo.MemoryUnit;
|
import work.slhaf.partner.core.memory.pojo.MemoryUnit;
|
||||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||||
import work.slhaf.partner.module.action.scheduler.ActionScheduler;
|
import work.slhaf.partner.module.action.scheduler.ActionScheduler;
|
||||||
|
import work.slhaf.partner.module.communication.DialogRollingService;
|
||||||
import work.slhaf.partner.module.memory.runtime.MemoryRuntime;
|
import work.slhaf.partner.module.memory.runtime.MemoryRuntime;
|
||||||
import work.slhaf.partner.module.memory.updater.summarizer.MultiSummarizer;
|
import work.slhaf.partner.module.memory.updater.summarizer.MultiSummarizer;
|
||||||
import work.slhaf.partner.module.memory.updater.summarizer.SingleSummarizer;
|
import work.slhaf.partner.module.memory.updater.summarizer.SingleSummarizer;
|
||||||
@@ -55,6 +56,8 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
|||||||
private SingleSummarizer singleSummarizer;
|
private SingleSummarizer singleSummarizer;
|
||||||
@InjectModule
|
@InjectModule
|
||||||
private ActionScheduler actionScheduler;
|
private ActionScheduler actionScheduler;
|
||||||
|
@InjectModule
|
||||||
|
private DialogRollingService dialogRollingService;
|
||||||
|
|
||||||
private final AtomicBoolean updating = new AtomicBoolean(false);
|
private final AtomicBoolean updating = new AtomicBoolean(false);
|
||||||
private InteractionThreadPoolExecutor executor;
|
private InteractionThreadPoolExecutor executor;
|
||||||
@@ -112,8 +115,12 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
|||||||
if (chatSnapshot.size() <= 1) {
|
if (chatSnapshot.size() <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
updateMemory(chatSnapshot);
|
|
||||||
cognitionCapability.rollChatMessagesWithSnapshot(chatSnapshot.size(), CONTEXT_RETAIN_DIVISOR);
|
RollingRecord record = updateMemory(chatSnapshot);
|
||||||
|
if (record != null) {
|
||||||
|
dialogRollingService.rollMessages(chatSnapshot, chatSnapshot.size(), CONTEXT_RETAIN_DIVISOR, record.unitId, record.sliceId, record.summary);
|
||||||
|
}
|
||||||
|
|
||||||
if (refreshMemoryId) {
|
if (refreshMemoryId) {
|
||||||
memoryCapability.refreshMemorySession();
|
memoryCapability.refreshMemorySession();
|
||||||
}
|
}
|
||||||
@@ -124,10 +131,10 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMemory(List<Message> chatSnapshot) {
|
private RollingRecord updateMemory(List<Message> chatSnapshot) {
|
||||||
log.debug("[MemoryUpdater] 记忆更新流程开始...");
|
log.debug("[MemoryUpdater] 记忆更新流程开始...");
|
||||||
if (chatSnapshot.isEmpty()) {
|
if (chatSnapshot.isEmpty()) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
SummarizeInput summarizeInput = new SummarizeInput(chatSnapshot, memoryRuntime.getTopicTree());
|
SummarizeInput summarizeInput = new SummarizeInput(chatSnapshot, memoryRuntime.getTopicTree());
|
||||||
log.debug("[MemoryUpdater] 记忆更新-总结流程-输入: {}", JSONObject.toJSONString(summarizeInput));
|
log.debug("[MemoryUpdater] 记忆更新-总结流程-输入: {}", JSONObject.toJSONString(summarizeInput));
|
||||||
@@ -141,6 +148,8 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
|||||||
summarizeResult.getSummary()
|
summarizeResult.getSummary()
|
||||||
);
|
);
|
||||||
log.debug("[MemoryUpdater] 记忆更新流程结束...");
|
log.debug("[MemoryUpdater] 记忆更新流程结束...");
|
||||||
|
MemorySlice newSlice = memoryUnit.getSlices().getLast();
|
||||||
|
return new RollingRecord(memoryUnit.getId(), newSlice.getId(), newSlice.getSummary());
|
||||||
}
|
}
|
||||||
|
|
||||||
private SummarizeResult summarize(SummarizeInput summarizeInput) {
|
private SummarizeResult summarize(SummarizeInput summarizeInput) {
|
||||||
@@ -184,4 +193,7 @@ public class MemoryUpdater extends AbstractAgentModule.Running<PartnerRunningFlo
|
|||||||
public int order() {
|
public int order() {
|
||||||
return 7;
|
return 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record RollingRecord(String unitId, String sliceId, String summary) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user