Install
openclaw skills install ctx-compressIntelligently compress context — conversations, code, logs. Preserve key information while reducing token usage. Auto-detects content type and applies optimal compression.
openclaw skills install ctx-compress在有限的上下文窗口里装下无限的对话记忆。
Fit more context in less tokens. Not truncation — intelligent compression.
普通截断: "这是很长的对话..." → "这是很..." (丢了信息)
智能压缩: "这是很长的对话..." → "讨论了XX,决定用YY" (保留核心)
不同内容,不同压缩策略:
COMPRESS = python <skill_dir>/scripts/ctxcompress.py
# 从文件
$COMPRESS compress --input conversation.txt --level medium --type chat
# 从管道
echo "很长的文本..." | $COMPRESS compress --type text
# 内联文本
$COMPRESS compress --text "你的文本内容..." --level aggressive
压缩级别:
light — 去掉空行和注释,保留所有内容medium — 去噪音 + 摘要 + 去重(默认)aggressive — 只保留核心信息内容类型(自动检测或手动指定):
chat — 对话/聊天记录code — 代码log — 日志文件text — 普通文本比较两个文件的差异:
$COMPRESS diff --old v1.txt --new v2.txt
输出:新增了什么、删除了什么、修改了什么。
从文本中提取结构化信息:
$COMPRESS extract --input chat_history.txt --type chat
输出 JSON:
{
"decisions": ["决定用 Flask 框架"],
"errors_and_fixes": [{"error": "port 5000 in use", "fix": "改用 8080"}],
"commands": ["pip install flask"],
"urls": ["https://flask.palletsprojects.com"],
"key_terms": ["Flask", "deploy", "gunicorn"]
}
把多个文件压缩成一个摘要:
$COMPRESS chain file1.py file2.log conversation.txt --level medium
适用于:一个任务涉及多个文件,想一次性了解全貌。
$COMPRESS stats --input big_file.txt
显示:总行数、空行占比、注释占比、预估可压缩比例。
当对话变长时,压缩旧消息以腾出上下文:
# 导出旧对话
cat old_messages.txt | $COMPRESS compress --type chat --level medium > compressed.txt
# 用压缩后的版本替换原文
压缩后保留:
20 轮试错 → 压缩成 1 段:
$COMPRESS compress --input debug_session.log --type log --level aggressive
输出类似:
📊 Log Summary: 15 errors, 3 warnings, 8 key events
ERROR: Connection refused on port 5432
ERROR: Permission denied /var/log/app.log
(above error repeated 8x)
SUCCESS: Service started on port 8080
把上一个 session 的精华传递给新 session:
$COMPRESS chain session1_notes.md session1_code.py session1_chat.txt --level aggressive
输出:一个紧凑的上下文摘要,注入新 session。
长文件压缩后便于快速理解:
$COMPRESS compress --input big_module.py --type code --level medium
去掉了空行、注释、docstring,只留逻辑代码。
根据文件扩展名和内容自动判断:
| 扩展名 | 类型 |
|---|---|
.py, .js, .ts, .go | code |
.log, .out, .err | log |
| 包含 "User:" / "Agent:" | chat |
| 其他 | text |
压缩结果包含元信息:
🗜️ Compressed: 5234 → 876 chars (83% reduction)
Type: chat | Level: medium
────────────────────────────────────────
## 决策 / Decisions
- 决定用 PostgreSQL 而不是 MySQL
## 问题 / Issues
- Error: port 5432 already in use
- Fix: 改用 5433
## 代码 / Code
- [342 chars of code]
context-compressor/
├── SKILL.md # 本文件
└── scripts/
└── ctxcompress.py # CLI 工具