Install
openclaw skills install @paudyyin/context-compressoropenclaw skills install @paudyyin/context-compressor五策略上下文压缩引擎,在长任务中智能压缩上下文,减少 token 消耗 40-60%。
| # | 策略 | 触发条件 | 效果 |
|---|---|---|---|
| 1 | 基于时间清理 | 工具输出 > 5 分钟 | 替换为占位符,保留结构 |
| 2 | 对话摘要 | 消息数 > 20 条 | 旧消息生成摘要,保留最近 20 条 |
| 3 | 观察遮蔽 | 旧工具输出 | 隐藏内容,保留调用记录 |
| 4 | 结构化笔记 | 含决策/结论的消息 | 提取关键信息注入 system prompt |
| 5 | 子Agent委托(预留) | 工具 > 10 个 | 复杂任务交给子 Agent |
import sys
sys.path.insert(0, "<skill-dir>/scripts")
from compressor import ContextCompressor
compressor = ContextCompressor(max_tokens=128000)
messages, stats = compressor.check_and_compress(messages)
print(f"压缩前: {stats['before_tokens']} tokens")
print(f"压缩后: {stats['after_tokens']} tokens")
print(f"应用策略: {stats['strategies_applied']}")
print(f"压缩率: {stats['reduction_percent']:.1f}%")
输入消息需为标准 OpenAI 格式:
{
"role": "user" | "assistant" | "system" | "tool",
"content": "消息内容",
"timestamp": 1234567890, # Unix 时间戳(可选,策略1需要)
"tool_calls": [...], # 工具调用记录(可选)
"tool_call_id": "xxx", # 工具调用 ID(可选)
"name": "tool_name" # 工具名称(可选)
}
参见 references/thresholds.md 获取详细阈值说明。
默认阈值:
cd <skill-dir>
python -m pytest scripts/test_compressor.py -v
# 或
python scripts/test_compressor.py