fix(communication): fix errors while import external xml node into input xml

This commit is contained in:
2026-04-20 16:40:36 +08:00
parent ac715602a6
commit d90c514159
5 changed files with 132 additions and 5 deletions

View File

@@ -127,7 +127,7 @@ public class CognitionCore implements StateSerializable {
new BlockContent("recent_chat_messages", "communication_producer") {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
document.appendChild(document.importNode(messageNotesElement(), true));
root.appendChild(document.importNode(messageNotesElement(), true));
Element chatMessagesElement = document.createElement("chat_messages");
root.appendChild(chatMessagesElement);
appendRepeatedElements(document, chatMessagesElement, "chat_message", resolveRecentChatMessages(), (messageElement, message) -> {

View File

@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import work.slhaf.partner.core.cognition.*;
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
@@ -171,7 +172,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
Element root = document.createElement("input");
document.appendChild(root);
document.appendChild(document.importNode(runningFlowContext.encodeInputsBlock().encodeToXml(), true));
root.appendChild(importExternalNode(document, runningFlowContext.encodeInputsBlock().encodeToXml()));
appendTextElement(document, root, "source", runningFlowContext.getSource());
for (Map.Entry<String, String> entry : runningFlowContext.getAdditionalUserInfo().entrySet()) {
appendTextElement(document, root, sanitizeTagName(entry.getKey()), entry.getValue());
@@ -242,11 +243,25 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
inputRoot.appendChild(groupElement);
for (CommunicationBlockContent block : entry.getValue()) {
Element blockElement = block.encodeToXml();
groupElement.appendChild(document.importNode(blockElement, true));
groupElement.appendChild(importExternalNode(document, blockElement));
}
}
}
private Node importExternalNode(Document targetDocument, Node externalNode) {
Node nodeToImport = externalNode;
if (externalNode != null && externalNode.getNodeType() == Node.DOCUMENT_NODE) {
nodeToImport = ((Document) externalNode).getDocumentElement();
}
if (nodeToImport == null) {
throw new IllegalArgumentException("待导入节点不能为空");
}
if (nodeToImport.getOwnerDocument() == targetDocument) {
return nodeToImport.cloneNode(true);
}
return targetDocument.importNode(nodeToImport, true);
}
private String sanitizeTagName(String rawTagName) {
if (rawTagName == null || rawTagName.isBlank()) {
return "meta";

View File

@@ -65,7 +65,7 @@ public class MessageSummarizer extends AbstractAgentModule.Sub<List<Message>, Re
return new TaskBlock() {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
document.appendChild(document.importNode(cognitionCapability.messageNotesElement(), true));
root.appendChild(document.importNode(cognitionCapability.messageNotesElement(), true));
appendListElement(document, root, "chat_messages", "message", messages, (element, message) -> {
element.setAttribute("role", message.roleValue());
element.setTextContent(message.getContent());