From f51401e2f220447fab36e6df47387465cf78fef9 Mon Sep 17 00:00:00 2001 From: slhafzjw Date: Tue, 10 Mar 2026 19:53:20 +0800 Subject: [PATCH] refactor(CommunicationProducer): snapshot chat history when assembling conversation --- .../modules/core/CommunicationProducer.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Partner-Core/src/main/java/work/slhaf/partner/module/modules/core/CommunicationProducer.java b/Partner-Core/src/main/java/work/slhaf/partner/module/modules/core/CommunicationProducer.java index 963f55ce..c1159b0f 100644 --- a/Partner-Core/src/main/java/work/slhaf/partner/module/modules/core/CommunicationProducer.java +++ b/Partner-Core/src/main/java/work/slhaf/partner/module/modules/core/CommunicationProducer.java @@ -44,12 +44,9 @@ public class CommunicationProducer extends AbstractAgentModule.Running chatMessages = new ArrayList<>(); @Init public void init() { - this.chatMessages.clear(); - this.chatMessages.addAll(this.cognationCapability.getChatMessages()); log.info("CommunicationProducer 注册完毕..."); } @@ -104,12 +101,13 @@ public class CommunicationProducer extends AbstractAgentModule.Running buildChatMessages(PartnerRunningFlowContext runningFlowContext) { - List temp = new ArrayList<>(chatMessages.size() + 2); + List historyMessages = snapshotConversationMessages(); + List temp = new ArrayList<>(historyMessages.size() + 2); Message contextMessage = buildContextMessage(runningFlowContext); if (contextMessage != null) { temp.add(contextMessage); } - temp.addAll(chatMessages); + temp.addAll(historyMessages); temp.add(buildInputMessage(runningFlowContext)); return temp; } @@ -117,6 +115,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running chatMessages = cognationCapability.getChatMessages(); chatMessages.removeIf(this::isStructuredUserMessage); Message primaryUserMessage = new Message( Message.Character.USER, @@ -130,6 +129,12 @@ public class CommunicationProducer extends AbstractAgentModule.Running snapshotConversationMessages() { + List snapshot = new ArrayList<>(cognationCapability.snapshotChatMessages()); + snapshot.removeIf(this::isStructuredUserMessage); + return snapshot; + } + private Message buildContextMessage(PartnerRunningFlowContext runningFlowContext) { List contextBlocks = filterContextBlocks( runningFlowContext.getContextBlocks(),