ConversationCompactor
public class ConversationCompactor
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_NAMEstaticfinal | 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
摘要消息名称字段。声明为 public static final String SUMMARY_MSG_NAME。
Marker stored in message name to identify injected summary messages.
io/agentscope/harness/agent/memory/compaction/ConversationCompactor.java:65构造器详情Constructor Detail
ConversationCompactor
public ConversationCompactor(Model model, MemoryFlushManager flushManager)
初始化 ConversationCompactor 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
model | Model | 模型实例(类型 `Model`)。The model. |
flushManager | MemoryFlushManager | 刷新管理器(类型 `MemoryFlushManager`)。The flush manager. |
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
rc | RuntimeContext | rc(类型 `RuntimeContext`)。The rc. |
conversationMessages | List<Msg> | 会话消息(类型 `List`)。non-SYSTEM messages (USER / ASSISTANT / TOOL) |
config | CompactionConfig | 配置对象(类型 `CompactionConfig`)。compaction configuration |
agentId | String | 智能体标识(类型 `String`)。agent identifier used for the memory offload path |
sessionId | String | 会话标识(类型 `String`)。session identifier used for the memory offload path |
Mono<Optional<List<Msg>>>io/agentscope/harness/agent/memory/compaction/ConversationCompactor.java:92–211