mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(CommunicationProducer): snapshot chat history when assembling conversation
This commit is contained in:
@@ -44,12 +44,9 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
private final List<Message> 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<PartnerRu
|
||||
}
|
||||
|
||||
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);
|
||||
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<PartnerRu
|
||||
private void updateModuleContextAndChatMessages(PartnerRunningFlowContext runningFlowContext, String response) {
|
||||
cognationCapability.getMessageLock().lock();
|
||||
try {
|
||||
List<Message> chatMessages = cognationCapability.getChatMessages();
|
||||
chatMessages.removeIf(this::isStructuredUserMessage);
|
||||
Message primaryUserMessage = new Message(
|
||||
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) {
|
||||
List<ContextBlock> contextBlocks = filterContextBlocks(
|
||||
runningFlowContext.getContextBlocks(),
|
||||
|
||||
Reference in New Issue
Block a user