refactor(cognition): remove duplicate recent chat block building logic, refresh which in cognition core uniformly

This commit is contained in:
2026-04-14 12:40:56 +08:00
parent a847f3bff8
commit 33ffd782c4
3 changed files with 34 additions and 32 deletions

View File

@@ -19,6 +19,8 @@ public interface CognitionCapability {
void rollChatMessagesWithSnapshot(int snapshotSize, int retainDivisor);
void refreshRecentChatMessagesContext();
Lock getMessageLock();
}

View File

@@ -99,6 +99,32 @@ public class CognitionCore implements StateSerializable {
return messageLock;
}
@CapabilityMethod
public void refreshRecentChatMessagesContext() {
ContextBlock block = new ContextBlock(
new BlockContent("recent_chat_messages", "communication_producer") {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendRepeatedElements(document, root, "chat_message", resolveRecentChatMessages());
}
},
Set.of(ContextBlock.VisibleDomain.COGNITION),
100,
10,
4
);
contextWorkspace.register(block);
}
private List<Message> resolveRecentChatMessages() {
int exclusiveEnd = Math.max(chatMessages.size() - 1, 0);
if (exclusiveEnd == 0) {
return List.of();
}
int start = Math.max(exclusiveEnd - 6, 0);
return chatMessages.subList(start, exclusiveEnd);
}
@Override
public @NotNull Path statePath() {
return Path.of("core", "cognition.json");
@@ -128,19 +154,7 @@ public class CognitionCore implements StateSerializable {
this.chatMessages.add(new Message(Message.Character.fromValue(role), content));
}
ContextBlock block = new ContextBlock(
new BlockContent("recent_chat_messages", "communication_producer") {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendRepeatedElements(document, root, "chat_message", List.of(chatMessages.subList(chatMessages.size() - 7, chatMessages.size() - 1)));
}
},
Set.of(ContextBlock.VisibleDomain.COGNITION),
100,
10,
4
);
contextWorkspace.register(block);
refreshRecentChatMessagesContext();
}
@Override

View File

@@ -21,7 +21,10 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@EqualsAndHashCode(callSuper = true)
@@ -71,24 +74,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
consumer.onDelta(INTERRUPTED_MARKER);
});
updateChatMessages(runningFlowContext, consumer.collectResponse());
updateContext();
}
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() - 7, chatMessages.size() - 1)));
}
},
Set.of(ContextBlock.VisibleDomain.COGNITION),
100,
5,
4
);
cognitionCapability.contextWorkspace().register(block);
cognitionCapability.refreshRecentChatMessagesContext();
}
private List<Message> buildChatMessages(PartnerRunningFlowContext runningFlowContext) {