ClawPressor: 会话上下文与 Token 优化 - Openclaw Skills

作者:互联网

2026-03-27

AI教程

什么是 ClawPressor?

ClawPressor 是专为管理 Openclaw Skills 生态系统中 AI 智能体上下文窗口而设计的专用工具。通过使用 Sumy 库和 LexRank 算法执行自然语言处理(NLP),它能够智能地总结历史对话数据,同时保持最近的交互完整。这可以防止性能下降,并避免因会话文件超过安全大小阈值而触发的强制压缩。

对于希望在不牺牲 AI 响应质量或产生巨额 API 开销的情况下维持长期运行会话的开发人员来说,这项技能至关重要。通过将庞大的 JSONL 会话日志转换为紧凑的摘要,ClawPressor 确保您的智能体在整个开发生命周期中保持响应灵敏且具成本效益。

下载入口:https://github.com/openclaw/skills/tree/main/skills/beboxos/clawpressor

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install clawpressor

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 clawpressor。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

ClawPressor 应用场景

  • 降低由于长期开发会话中过多的 Token 消耗而产生的 API 成本。
  • 当智能体接近其 50% 上下文限制时,延长会话寿命。
  • 改善大上下文文件导致系统延迟时的智能体响应时间。
  • 作为日常开发者心跳工作流的一部分,自动执行会话维护。
  • 在丢弃冗余对话噪音的同时,保留核心项目上下文。
ClawPressor 工作原理
  1. 该工具扫描智能体会话目录以识别活动会话文件。
  2. 它保留用户定义数量的最近消息(默认为 5 条),以维持即时对话的连贯性。
  3. 旧的消息历史记录通过 Sumy 库使用 LexRank 算法进行处理,以提取关键语义信息。
  4. 使用 .backup 扩展名创建原始会话的备份,以确保数据安全。
  5. 历史消息被替换为包含摘要上下文的单个紧凑系统消息,从而大幅减少总 Token 计数。

ClawPressor 配置指南

要将 ClawPressor 集成到您的环境中,请安装所需的 NLP 依赖项和 NLTK 数据集:

pip install sumy
python -c "import nltk; nltk.download('punkt_tab'); nltk.download('stopwords')"

然后,您可以运行干跑(dry-run)模式来预览潜在的节省,而不修改您的文件:

python3 scripts/compress.py --dry-run

要应用压缩并优化您的 Openclaw Skills 性能:

python3 scripts/compress.py --apply

ClawPressor 数据架构与分类体系

ClawPressor 使用以下结构管理会话数据:

数据组件 格式 描述
会话文件 .jsonl 智能体使用的活动消息历史文件。
备份文件 .backup NLP 压缩前的完整会话副本。
摘要块 系统消息 包含总结历史的单个注入消息。
统计数据 控制台输出 关于消息数量减少和 KB 节省的实时报告。
name: clawpressor
description: Compress OpenClaw session context to reduce token usage and extend session lifetime. Uses NLP summarization (Sumy) to intelligently compact conversation history while preserving essential context. Triggers on mentions of session compression, token reduction, context cleanup, or when session size exceeds safe thresholds (~300KB). Use when (1) OpenClaw approaches 50% context limit, (2) Sessions are slowing down due to large context, (3) Reducing API costs from excessive token consumption, (4) Extending session lifetime without forced reboots.

ClawPressor - Session Context Compressor

Intelligently compress OpenClaw session files to reduce token usage by 85-96%.

Author: JARVIS (AI Coder) | Managed by: BeBoX License: MIT | Version: 1.0.0

Quick Start

# Preview compression without changes
python3 scripts/compress.py --dry-run

# Apply compression
python3 scripts/compress.py --apply

# Restore from backup
python3 scripts/compress.py --restore

When to Use

Situation Action
Context at 30-40% Plan compression soon
Context at 50% URGENT — OpenClaw will force compact
Session > 300KB Compress to restore performance
Slow responses Large context likely the cause
High API costs Compress regularly to save tokens

How It Works

  1. Preserves recent context — Keeps last 5 messages intact for immediate context
  2. Summarizes old messages — Uses LexRank algorithm to extract key information
  3. Replaces with compact block — Single system message containing summary
  4. Creates backup — Original preserved as .backup file

Prerequisites

pip install sumy
python -c "import nltk; nltk.download('punkt_tab'); nltk.download('stopwords')"

Command Reference

# Find and compress latest session (dry-run)
python3 scripts/compress.py

# Compress specific session
python3 scripts/compress.py --session /path/to/session.jsonl --apply

# Keep more recent messages (default: 5)
python3 scripts/compress.py --keep 10 --apply

# Restore if something went wrong
python3 scripts/compress.py --restore

# View compression statistics
python3 scripts/compress.py --stats

Typical Results

Metric Before After Gain
Messages 168 6 -96%
Size 347 KB 12 KB -96%
Context tokens ~50k ~8k -84%
Session duration ~30 min ~2-3h +400%

Integration with Workflows

In HEARTBEAT.md:

## Context Maintenance (1x/jour)
- Check session size: `ls -lh ~/.openclaw/agents/main/sessions/*.jsonl`
- If > 200KB: `python3 skills/clawpressor/scripts/compress.py --apply`

Manual check:

# See current session stats
ls -lh ~/.openclaw/agents/main/sessions/*.jsonl | head -1

Safety

  • Always creates .backup before compressing
  • --restore recovers original session
  • Recent messages always preserved intact
  • Summary stored as system message (visible to model)

Troubleshooting

Issue Solution
"Sumy not installed" Run pip install sumy and NLTK downloads
No session found Check ~/.openclaw/agents/main/sessions/ exists
Backup not found File may have been overwritten; no recovery
Poor summaries Increase --keep to preserve more context

Credits

  • Coding: JARVIS (AI Assistant)
  • Project Management: BeBoX
  • Technique: NLP summarization via Sumy (LexRank algorithm)
  • See memory/openclaw-context-optimization.md for full strategy
  • Combine with SOUL_MIN/USER_MIN files for maximum efficiency