AS AgentScopeAPI 手册 / API Reference

ConversationCompactor

public class ConversationCompactor
源码Source io/agentscope/harness/agent/memory/compaction/ConversationCompactor.java:60–728

会话 Compactor,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.harness.agent.memory.compaction 包。核心成员包括 compactIfNeeded、ConversationCompactor、SUMMARY_MSG_NAME。

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

  • Message offload (optional) — persist the full conversation to the session JSONL via MemoryFlushManager#offloadMessages

  • 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

字段Field类型Type说明Description
SUMMARY_MSG_NAME
staticfinal
String
摘要消息名称字段。
Marker stored in message name to identify injected summary messages.

构造器摘要Constructor Summary

构造器Constructor说明Description
public ConversationCompactor(Model model, MemoryFlushManager flushManager)
初始化 `ConversationCompactor` 实例。
Creates a new instance.

方法摘要Method Summary

方法Method说明Description
public Mono<Optional<List<Msg>>> compactIfNeeded(RuntimeContext rc, List<Msg> conversationMessages, CompactionConfig config, String agentId, String sessionId)
执行 `compactIfNeeded` 方法。
Runs compaction on the supplied conversation messages if a trigger condition is met.

字段详情Field Detail

SUMMARY_MSG_NAME

public static final String SUMMARY_MSG_NAME
staticfinal

摘要消息名称字段。声明为 public static final String SUMMARY_MSG_NAME。

Marker stored in message name to identify injected summary messages.

源码Source io/agentscope/harness/agent/memory/compaction/ConversationCompactor.java:65

构造器详情Constructor Detail

ConversationCompactor

public ConversationCompactor(Model model, MemoryFlushManager flushManager)

初始化 ConversationCompactor 实例。

Creates a new instance.

参数Parameters
名称Name类型Type说明Description
modelModel模型实例(类型 `Model`)。The model.
flushManagerMemoryFlushManager刷新管理器(类型 `MemoryFlushManager`)。The flush manager.
源码Source io/agentscope/harness/agent/memory/compaction/ConversationCompactor.java:70–73

方法详情Method Detail

compactIfNeeded

public Mono<Optional<List<Msg>>> compactIfNeeded(RuntimeContext rc, List<Msg> conversationMessages, CompactionConfig config, String agentId, String sessionId)

执行 compactIfNeeded 方法。

Runs compaction on the supplied conversation messages if a trigger condition is met.

参数Parameters
名称Name类型Type说明Description
rcRuntimeContextrc(类型 `RuntimeContext`)。The rc.
conversationMessagesList<Msg>会话消息(类型 `List`)。non-SYSTEM messages (USER / ASSISTANT / TOOL)
configCompactionConfig配置对象(类型 `CompactionConfig`)。compaction configuration
agentIdString智能体标识(类型 `String`)。agent identifier used for the memory offload path
sessionIdString会话标识(类型 `String`)。session identifier used for the memory offload path
返回Returns Mono<Optional<List<Msg>>>
返回Mono>>。`Optional.empty()` when no compaction was needed; otherwise the replacement message list consisting of `[summaryUserMsg] + preservedTail`
源码Source io/agentscope/harness/agent/memory/compaction/ConversationCompactor.java:92–211
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.