refactor(CommunicationProducer): snapshot chat history when assembling conversation

This commit is contained in:
2026-03-10 19:53:20 +08:00
parent 5ad80d8b86
commit f51401e2f2

View File

@@ -44,12 +44,9 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
@InjectCapability @InjectCapability
private CognationCapability cognationCapability; private CognationCapability cognationCapability;
private final List<Message> chatMessages = new ArrayList<>();
@Init @Init
public void init() { public void init() {
this.chatMessages.clear();
this.chatMessages.addAll(this.cognationCapability.getChatMessages());
log.info("CommunicationProducer 注册完毕..."); log.info("CommunicationProducer 注册完毕...");
} }
@@ -104,12 +101,13 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
} }
private List<Message> buildChatMessages(PartnerRunningFlowContext runningFlowContext) { private List<Message> buildChatMessages(PartnerRunningFlowContext runningFlowContext) {
List<Message> temp = new ArrayList<>(chatMessages.size() + 2); List<Message> historyMessages = snapshotConversationMessages();
List<Message> temp = new ArrayList<>(historyMessages.size() + 2);
Message contextMessage = buildContextMessage(runningFlowContext); Message contextMessage = buildContextMessage(runningFlowContext);
if (contextMessage != null) { if (contextMessage != null) {
temp.add(contextMessage); temp.add(contextMessage);
} }
temp.addAll(chatMessages); temp.addAll(historyMessages);
temp.add(buildInputMessage(runningFlowContext)); temp.add(buildInputMessage(runningFlowContext));
return temp; return temp;
} }
@@ -117,6 +115,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
private void updateModuleContextAndChatMessages(PartnerRunningFlowContext runningFlowContext, String response) { private void updateModuleContextAndChatMessages(PartnerRunningFlowContext runningFlowContext, String response) {
cognationCapability.getMessageLock().lock(); cognationCapability.getMessageLock().lock();
try { try {
List<Message> chatMessages = cognationCapability.getChatMessages();
chatMessages.removeIf(this::isStructuredUserMessage); chatMessages.removeIf(this::isStructuredUserMessage);
Message primaryUserMessage = new Message( Message primaryUserMessage = new Message(
Message.Character.USER, Message.Character.USER,
@@ -130,6 +129,12 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
} }
} }
private List<Message> snapshotConversationMessages() {
List<Message> snapshot = new ArrayList<>(cognationCapability.snapshotChatMessages());
snapshot.removeIf(this::isStructuredUserMessage);
return snapshot;
}
private Message buildContextMessage(PartnerRunningFlowContext runningFlowContext) { private Message buildContextMessage(PartnerRunningFlowContext runningFlowContext) {
List<ContextBlock> contextBlocks = filterContextBlocks( List<ContextBlock> contextBlocks = filterContextBlocks(
runningFlowContext.getContextBlocks(), runningFlowContext.getContextBlocks(),