refactor(communication): register recent chat messages as context into ContextWorkspace

This commit is contained in:
2026-03-24 11:28:40 +08:00
parent 4494d58ff9
commit 313cea0d3b
2 changed files with 23 additions and 6 deletions

View File

@@ -265,6 +265,7 @@ abstract class BlockContent protected constructor(
return element
}
@JvmOverloads
protected fun <T> appendListElement(
document: Document,
parent: Element,
@@ -287,6 +288,7 @@ abstract class BlockContent protected constructor(
return wrapper
}
@JvmOverloads
protected fun <T> appendRepeatedElements(
document: Document,
parent: Element,

View File

@@ -21,10 +21,7 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
@@ -82,7 +79,8 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
// TODO 为各模块提供 emit msg 能力后, 在这里统一接收并分发结构化输出.
responseText = this.chat(buildChatMessages(runningFlowContext));
log.debug("CommunicationProducer responses: {}", responseText);
updateModuleContextAndChatMessages(runningFlowContext, responseText);
updateChatMessages(runningFlowContext, responseText);
updateContext();
break;
} catch (Exception e) {
count++;
@@ -97,6 +95,23 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
}
}
private void updateContext() {
ContextBlock block = new ContextBlock(
new BlockContent("recent_chat_messages", "communication_producer") {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
List<Message> chatMessages = cognitionCapability.getChatMessages();
appendRepeatedElements(document, root, "chat_message", List.of(chatMessages.subList(chatMessages.size() - 5, chatMessages.size() - 1)));
}
},
Set.of(ContextBlock.VisibleDomain.COGNITION),
100,
5,
4
);
cognitionCapability.contextWorkspace().register(block);
}
private void updateCoreResponse(PartnerRunningFlowContext runningFlowContext, String responseText) {
runningFlowContext.getCoreResponse().put("text", responseText);
}
@@ -113,7 +128,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
return temp;
}
private void updateModuleContextAndChatMessages(PartnerRunningFlowContext runningFlowContext, String response) {
private void updateChatMessages(PartnerRunningFlowContext runningFlowContext, String response) {
cognitionCapability.getMessageLock().lock();
try {
List<Message> chatMessages = cognitionCapability.getChatMessages();