Class ConversationCompactor
java.lang.Object
io.agentscope.harness.agent.memory.compaction.ConversationCompactor
Algorithm
- Check trigger — token count or message count exceeds threshold
- Determine cutoff — find the earliest index that keeps the tail within the "keep" budget; never split an ASSISTANT tool-call from its TOOL result(s)
- Memory flush (optional) — extract long-term memories from the prefix via
MemoryFlushManager.flushMemories(io.agentscope.core.agent.RuntimeContext, java.util.List<io.agentscope.core.message.Msg>) - Message offload (optional) — persist the full conversation to the session
JSONL via
MemoryFlushManager.offloadMessages(io.agentscope.core.agent.RuntimeContext, java.util.List<io.agentscope.core.message.Msg>, java.lang.String, java.lang.String) - Summarize — one LLM call to distill the prefix into a structured summary
- Rebuild — return
[summaryUserMsg] + preservedTail
The caller is responsible for updating both the agent's working memory and the LLM-facing
message list (see CompactionMiddleware).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringMarker stored in message name to identify injected summary messages. -
Constructor Summary
ConstructorsConstructorDescriptionConversationCompactor(io.agentscope.core.model.Model model, MemoryFlushManager flushManager) -
Method Summary
Modifier and TypeMethodDescriptioncompactIfNeeded(io.agentscope.core.agent.RuntimeContext rc, List<io.agentscope.core.message.Msg> conversationMessages, CompactionConfig config, String agentId, String sessionId) Runs compaction on the supplied conversation messages if a trigger condition is met.
-
Field Details
-
SUMMARY_MSG_NAME
Marker stored in message name to identify injected summary messages.- See Also:
-
-
Constructor Details
-
ConversationCompactor
-
-
Method Details
-
compactIfNeeded
public reactor.core.publisher.Mono<Optional<List<io.agentscope.core.message.Msg>>> compactIfNeeded(io.agentscope.core.agent.RuntimeContext rc, List<io.agentscope.core.message.Msg> conversationMessages, CompactionConfig config, String agentId, String sessionId) Runs compaction on the supplied conversation messages if a trigger condition is met.Only conversation messages (non-SYSTEM) should be passed. The caller must separate system messages before invoking this method and re-prepend them after.
- Parameters:
conversationMessages- non-SYSTEM messages (USER / ASSISTANT / TOOL)config- compaction configurationagentId- agent identifier used for the memory offload pathsessionId- session identifier used for the memory offload path- Returns:
Optional.empty()when no compaction was needed; otherwise the replacement message list consisting of[summaryUserMsg] + preservedTail
-