Pedro Gonzalez

Smart Context Manager

AI 智慧體v1.0.3
作者:Pedro Gonzalez更新於 2026-05-11共 3 個版本 · 最新版本釋出於 2026-02-01context-manager@clawhub_plgonzalezrx8/context-manager
Agent 框架上下文管理記憶增強#Context Management

據 AlphaMoat 統計,截至 2026-10,Smart Context Manager 累計下載 10,780 次、獲 5 個星標,按累計下載量在本次參與排名的 10,914 個 AI 技能中排名第 2,532 位,在「AI 智慧體」分類(1,632 個)中排名第 345 位。

列表預設按累計下載量排序,也可按星標排序;詳情頁排名按累計下載量計算。

累計下載
1.1萬
星標
5
全站下載量排名
#2,532
全站前 24% · 「AI 智慧體」前 22%
平台累計下載星標評論
ClawHub ↗63755-
SkillHub ↗1.1萬5—

各平台的計數口徑不同,統計時間不一致。

這個技能是做什麼的

讓 agent 先給自己的會話生成摘要,備份原會話檔案,再用官方重置方式清空,並把摘要作為新會話的第一條訊息注入,從而在同一個會話鍵下繼續工作。適合上下文佔用接近 70% 到 80%、會話變慢或開始失去連貫時使用;帶 --replace 的壓縮會刪除原會話檔案,屬破壞性操作。

資料釋出於 2026-10-27

可以用來做什麼

  • 長會話變慢前先壓縮上下文再繼續
  • 只跑摘要不重置,先看壓縮會丟掉什麼

什麼時候用

  • 上下文佔用接近上限時
  • 要長時間連續對話且不想中途失憶時

示例提示詞

以下是根據技能文件生成的示例,不代表實際執行結果。

幫我把這個長會話壓縮一下再繼續
先只生成摘要,別重置會話

月度資料趨勢

累計下載1.1萬
449078681.1萬2026-03 · 累計下載 49562026-05 · 累計下載 57372026-06 · 累計下載 59342026-07 · 累計下載 61792026-08 · 累計下載 63352026-09 · 累計下載 70472026-10 · 累計下載 1.1萬26-0326-0926-10
星標5
4552026-03 · 星標 42026-05 · 星標 52026-06 · 星標 52026-07 · 星標 52026-08 · 星標 52026-09 · 星標 52026-10 · 星標 526-0326-0926-10

2026-09 起統計方法調整:同名技能按作者分別統計,並覆蓋全部技能。圖中虛線為調整前的月份,與之後的資料不直接比較,也不計算跨越調整的增長。

月份累計下載環比星標
2026-101.1萬—5
2026-097047—5
2026-086335+2.5%5
2026-076179+4.1%5
2026-065934+3.4%5
2026-055737—5
2026-034956—4

SKILL.md 原文

以下是作者釋出的原始檔案,按純文字顯示,本站未做修改或稽核;其中的連結不可點選,命令請核實後再執行。抓取於 2026-10-23。

8.0 KB · ClawHub v1.0.3
---
name: context-manager
description: AI-powered context management for OpenClaw sessions
user-invocable: true
---

# Context Manager Skill

AI-powered context management for OpenClaw sessions. Uses the agent itself to generate intelligent summaries, then resets the session with compressed context.

展開全部(共 226 行)
## Quick Start

```bash
# List all sessions with usage stats
~/openclaw/skills/context-manager/compress.sh list

# Check status of a specific session
~/openclaw/skills/context-manager/compress.sh status agent:main:main

# Generate AI summary (read-only, safe)
~/openclaw/skills/context-manager/compress.sh summarize agent:main:main

# Compress session: generate summary, reset, inject (DESTRUCTIVE)
~/openclaw/skills/context-manager/compress.sh summarize agent:main:main --replace
```

## When to Use

- Context usage approaching 70-80%+
- Long sessions with extensive conversation history  
- Before the session becomes slow or loses coherence
- Proactively to maintain fast, focused sessions

## How It Works

1. **AI Summarization**: Sends a prompt to the agent asking it to summarize its own context
2. **Backup**: Saves the original JSONL session file to `memory/compressed/`
3. **Reset**: Deletes the JSONL file (official reset method)
4. **Inject**: Sends the AI-generated summary as the first message in the fresh session
5. **Result**: Same session key, new session ID, compressed context

**Key insight**: The agent has full visibility into its own context, so it generates the best possible summary.

## Commands

### Session Commands

| Command | Description |
|---------|-------------|
| `list` | List all sessions with token usage |
| `status [KEY]` | Show detailed status for a session |
| `summarize [KEY]` | Generate AI summary (read-only) |
| `summarize [KEY] --replace` | Summarize AND reset session with compressed context |
| `compress [KEY]` | Legacy grep-based extraction (not recommended) |
| `check [KEY]` | Check if session exceeds threshold |
| `check-all` | Check all sessions at once |

### Configuration Commands

| Command | Description |
|---------|-------------|
| `set-threshold N` | Set compression threshold (50-99%, default: 80) |
| `set-depth LEVEL` | Set depth: brief/balanced/comprehensive |
| `set-quiet-hours HH` | Set quiet hours (e.g., "23:00-07:00") |
| `help` | Show help and usage examples |

## Examples

### List All Sessions

```bash
$ ~/openclaw/skills/context-manager/compress.sh list
📋 Available Sessions (4 total)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#    SESSION KEY                              KIND       TOKENS    USAGE
1    agent:main:main                          direct      70188      70%
2    agent:main:slack:channel:c0aaruq2en9     group       20854      20%
3    agent:main:cron:0d02af4b-...             direct      18718      18%
```

### Check Session Status

```bash
$ ~/openclaw/skills/context-manager/compress.sh status agent:main:main
📊 Context Manager Status
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  Session Key: agent:main:main
  Session ID:  fc192a2d-091c-48c7-9fad-12bf34687454
  Kind:        direct
  Model:       gemini-3-flash
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  Threshold:   80%
  Tokens:      70188 / 100000
  Usage:       70%
```

### Generate AI Summary (Safe, Read-Only)

```bash
$ ~/openclaw/skills/context-manager/compress.sh summarize agent:main:main
🧠 Requesting AI summary for session: agent:main:main
  Session ID: fc192a2d-091c-48c7-9fad-12bf34687454

✅ AI Summary generated!
  Saved to: memory/compressed/20260127-123146.ai-summary.md

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
### Session Summary: January 27, 2026

#### 1. What was accomplished
- System audit completed
- Essay generation with sub-agents
...
```

### Full Compression (Summarize + Reset + Inject)

```bash
$ ~/openclaw/skills/context-manager/compress.sh summarize agent:main:main --replace
🧠 Requesting AI summary for session: agent:main:main
  Session ID: fc192a2d-091c-48c7-9fad-12bf34687454
  Mode: REPLACE (will reset session after summary)

✅ AI Summary generated!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[AI-generated summary displayed]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

🔄 Resetting session and injecting compressed context...
  Backing up session file...
  Backup saved: memory/compressed/20260127-123146.session-backup.jsonl
  Deleting session JSONL to reset...
  Injecting compressed context into fresh session...
✅ Session compressed successfully!
  Old session ID: fc192a2d-091c-48c7-9fad-12bf34687454
  New session ID: a1b2c3d4-...
  Session is ready to continue with compressed context
```

**Result**: 70k tokens → 16k tokens (77% reduction)

## Output Files

When compression occurs, these files are created in `memory/compressed/`:

| File | Description |
|------|-------------|
| `{timestamp}.ai-summary.md` | AI-generated session summary |
| `{timestamp}.session-backup.jsonl` | Full backup of original session (can restore if needed) |
| `{timestamp}.transcript.md` | Raw transcript extraction (legacy) |
| `{timestamp}.summary.md` | Grep-based summary (legacy) |

## Requirements

- **openclaw** - Gateway must be running
- **jq** - JSON parsing (`brew install jq`)
- **Gateway access** - Script uses `openclaw agent` and `openclaw sessions`

## Technical Details

### Session Reset Method

The script uses JSONL deletion to reset sessions (official method):

1. Backup JSONL to `memory/compressed/`
2. Delete `~/.openclaw/agents/{agent}/sessions/{sessionId}.jsonl`
3. Send compressed context via `openclaw agent --to main`
4. New session is created automatically with summary as first message

### Why Not /reset?

The `/reset` slash command only works in the chat interface. When sent via `openclaw agent --session-id`, it's treated as a regular message and the agent tries to interpret it as a task.

### AI Summarization Prompt

The script asks the agent to provide:
1. What was accomplished (key tasks)
2. Key decisions made (with rationale)
3. Current state (where we left off)
4. Pending tasks (what still needs doing)
5. Important context (critical info to remember)

## Troubleshooting

### Summary Text Empty

If the AI summary extraction fails, check stderr redirect:
```bash
# The script uses 2>/dev/null to avoid Node deprecation warnings breaking JSON
openclaw agent --session-id $ID -m "..." --json 2>/dev/null
```

### Session Not Resetting

Verify the JSONL file path:
```bash
ls ~/.openclaw/agents/main/sessions/
```

### Restore From Backup

If something goes wrong:
```bash
cp memory/compressed/{timestamp}.session-backup.jsonl \
   ~/.openclaw/agents/main/sessions/{sessionId}.jsonl
```

### Check Logs

Use `openclaw logs` to troubleshoot:
```bash
openclaw logs --limit 50 --json | grep -i "error\|fail"
```

## Best Practices

1. **Backup first**: The script auto-backs up, but you can also manually backup before testing
2. **Test on non-critical sessions first**: Try on a Slack channel or cron session before main
3. **Check the summary**: Run `summarize` without `--replace` first to verify the summary quality
4. **Monitor token count**: Use `status` to verify compression worked

## See Also

- `openclaw sessions --help`
- `openclaw agent --help`

「AI 智慧體」分類下的熱門技能

Pedro Gonzalez 的其他技能

資料月份:2026-10 · 下載/星標數聚合自公開技能註冊庫(ClawHub、SkillHub),按月更新。口徑說明見資料方法。