Class ConversationCompactor

java.lang.Object
io.agentscope.harness.agent.memory.compaction.ConversationCompactor

public class ConversationCompactor extends Object

Algorithm

  1. Check trigger — token count or message count exceeds threshold
  2. 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)
  3. 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>)
  4. 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)
  5. Summarize — one LLM call to distill the prefix into a structured summary
  6. 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 Details

    • SUMMARY_MSG_NAME

      public static final String SUMMARY_MSG_NAME
      Marker stored in message name to identify injected summary messages.
      See Also:
  • Constructor Details

    • ConversationCompactor

      public ConversationCompactor(io.agentscope.core.model.Model model, MemoryFlushManager flushManager)
  • 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 configuration
      agentId - agent identifier used for the memory offload path
      sessionId - 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