mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(cognition): remove duplicate recent chat block building logic, refresh which in cognition core uniformly
This commit is contained in:
@@ -19,6 +19,8 @@ public interface CognitionCapability {
|
|||||||
|
|
||||||
void rollChatMessagesWithSnapshot(int snapshotSize, int retainDivisor);
|
void rollChatMessagesWithSnapshot(int snapshotSize, int retainDivisor);
|
||||||
|
|
||||||
|
void refreshRecentChatMessagesContext();
|
||||||
|
|
||||||
Lock getMessageLock();
|
Lock getMessageLock();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,32 @@ public class CognitionCore implements StateSerializable {
|
|||||||
return messageLock;
|
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
|
@Override
|
||||||
public @NotNull Path statePath() {
|
public @NotNull Path statePath() {
|
||||||
return Path.of("core", "cognition.json");
|
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));
|
this.chatMessages.add(new Message(Message.Character.fromValue(role), content));
|
||||||
}
|
}
|
||||||
|
|
||||||
ContextBlock block = new ContextBlock(
|
refreshRecentChatMessagesContext();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ import javax.xml.transform.TransformerFactory;
|
|||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
import java.io.StringWriter;
|
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;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@@ -71,24 +74,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
|||||||
consumer.onDelta(INTERRUPTED_MARKER);
|
consumer.onDelta(INTERRUPTED_MARKER);
|
||||||
});
|
});
|
||||||
updateChatMessages(runningFlowContext, consumer.collectResponse());
|
updateChatMessages(runningFlowContext, consumer.collectResponse());
|
||||||
updateContext();
|
cognitionCapability.refreshRecentChatMessagesContext();
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Message> buildChatMessages(PartnerRunningFlowContext runningFlowContext) {
|
private List<Message> buildChatMessages(PartnerRunningFlowContext runningFlowContext) {
|
||||||
|
|||||||
Reference in New Issue
Block a user