collect-session
Captures every session when /new or /reset is issued and writes a rich Markdown summary to disk. Useful for session findability, cost tracking, and building a searchable history of agent work.
What it produces
For each session:
- -
<output-dir>/sessions/YYYY-MM-DD-<slug>.md — full session report (turns, tool calls, model usage, cost, LLM-generated name + summary) - INLINECODE3 — append-only table of all collected sessions
- INLINECODE4 — machine-readable record for analytics
Prerequisites
- - LiteLLM running locally — required for cost derivation and LLM enrichment. Verify with: INLINECODE5
- Node.js —
node must be in PATH - OpenClaw
workspace.dir — must be set in openclaw.json so the hook can find the script
Installation
Step 1 — Copy the script
Copy scripts/collect-session.mjs to your workspace scripts directory:
CODEBLOCK0
Step 2 — Configure the script
Edit the CONFIG block at the top of collect-session.mjs. Three values must be set:
CODEBLOCK1
Alternatively, set environment variables: COLLECT_SESSION_OUTPUT_DIR, LITELLM_API_KEY, LITELLM_BASE_URL.
Step 3 — Install the hook
Copy scripts/hook-handler.ts to your OpenClaw hooks directory and register it:
CODEBLOCK2
Create ~/.openclaw/hooks/collect-session/HOOK.md:
CODEBLOCK3
Then enable it in openclaw.json under hooks.internal.entries:
CODEBLOCK4
Restart the gateway after making config changes.
Step 4 — Verify
Issue /new in any session. You should see [collect-session] ✅ Session collected in gateway logs.
To check manually:
CODEBLOCK5
Output path
Default output is ~/workspace/memory/. Override with:
- - Environment variable: INLINECODE21
- CLI flag: INLINECODE22
- Edit
OUTPUT_DIR_DEFAULT in the script CONFIG block
Backfill sweep
To collect all existing uncollected sessions:
CODEBLOCK6
Add --no-llm to skip LLM enrichment (faster, uses heuristic names).
CLI reference
| Flag | Description |
|---|
| (no args) | Collect most recent completed session |
| INLINECODE25 |
Collect specific session by ID or path |
|
--current | Collect the currently active session (hook use) |
|
--sweep | Collect all uncollected sessions |
|
--no-llm | Skip LLM enrichment, use heuristic title |
|
--force | Re-collect even if already in session-log.jsonl |
|
--output-dir <path> | Override output directory |
Troubleshooting
Hook not firing on /new: Check that collect-session is enabled in openclaw.json and the gateway was restarted. Look for [collect-session] lines in gateway logs.
LITELLM_API_KEY not configured warning: Set LITELLM_API_KEY env var or edit the CONFIG block. The script will fall back to heuristic titles but still write session files.
Sessions directory not found: The default sessions path is ~/.openclaw/agents/main/sessions. If you use a custom agent name, update SESSIONS_DIR in the CONFIG block.
Cost shows $0.0000: LiteLLM returns zero cost for some providers. The script derives cost from token counts using a built-in pricing table. Add missing models to MODEL_PRICING in the CONFIG block.
collect-session
在每次发出 /new 或 /reset 命令时捕获当前会话,并将丰富的 Markdown 摘要写入磁盘。有助于会话的可查找性、成本追踪以及构建可搜索的代理工作历史记录。
输出内容
每个会话会生成:
- - /sessions/YYYY-MM-DD-.md — 完整会话报告(轮次、工具调用、模型使用情况、成本、LLM 生成的名称+摘要)
- /SESSION-INDEX.md — 所有已收集会话的仅追加表格
- /session-log.jsonl — 用于分析的机器可读记录
前置条件
- - 本地运行 LiteLLM — 用于成本计算和 LLM 增强。使用以下命令验证:curl http://localhost:4000/health
- Node.js — node 必须在 PATH 环境变量中
- OpenClaw workspace.dir — 必须在 openclaw.json 中设置,以便钩子能找到脚本
安装
第一步 — 复制脚本
将 scripts/collect-session.mjs 复制到工作区脚本目录:
bash
cp collect-session.mjs /scripts/collect-session.mjs
chmod +x /scripts/collect-session.mjs
第二步 — 配置脚本
编辑 collect-session.mjs 顶部的 CONFIG 块。必须设置三个值:
js
// 代理:设置为工作区内存目录
const OUTPUTDIRDEFAULT = process.env.COLLECTSESSIONOUTPUT_DIR
|| path.join(os.homedir(), workspace, memory);
// 代理:确认此端口与 LiteLLM 端口匹配(默认 4000)
const LITELLMBASE = process.env.LITELLMBASE_URL || http://localhost:4000;
// 代理:找到你的 LiteLLM 虚拟密钥。
// 在 http://localhost:4000/ui 查看 LiteLLM 仪表板,或从 1Password/密钥管理器获取。
// 优先使用低成本密钥(cron/批处理预算)——使用 Gemini Flash 每次会话增强成本约 $0.0001。
const LITELLMKEY = process.env.LITELLMAPIKEY || YOURLITELLMVIRTUALKEY;
或者,设置环境变量:COLLECTSESSIONOUTPUTDIR、LITELLMAPIKEY、LITELLMBASE_URL。
第三步 — 安装钩子
将 scripts/hook-handler.ts 复制到 OpenClaw 钩子目录并注册:
bash
mkdir -p ~/.openclaw/hooks/collect-session
cp hook-handler.ts ~/.openclaw/hooks/collect-session/handler.ts
创建 ~/.openclaw/hooks/collect-session/HOOK.md:
markdown
name: collect-session
description: 在发出 /new 或 /reset 命令时收集并持久化当前会话到 memory/sessions/
metadata:
{ openclaw: { emoji: 📦, events: [command:new, command:reset], requires: { bins: [node], config: [workspace.dir] } } }
然后在 openclaw.json 的 hooks.internal.entries 下启用它:
json
collect-session: {
enabled: true
}
配置更改后重启网关。
第四步 — 验证
在任何会话中发出 /new 命令。你应该在网关日志中看到 [collect-session] ✅ Session collected。
手动检查:
bash
node /scripts/collect-session.mjs --no-llm
输出路径
默认输出路径为 ~/workspace/memory/。可通过以下方式覆盖:
- - 环境变量:COLLECTSESSIONOUTPUTDIR=/path/to/dir
- CLI 标志:node collect-session.mjs --output-dir /path/to/dir
- 编辑脚本 CONFIG 块中的 OUTPUTDIR_DEFAULT
回溯扫描
收集所有现有未收集的会话:
bash
node /scripts/collect-session.mjs --sweep
添加 --no-llm 跳过 LLM 增强(更快,使用启发式名称)。
CLI 参考
| 标志 | 描述 |
|---|
| (无参数) | 收集最近完成的会话 |
| <session-id> |
按 ID 或路径收集特定会话 |
| --current | 收集当前活跃的会话(钩子使用) |
| --sweep | 收集所有未收集的会话 |
| --no-llm | 跳过 LLM 增强,使用启发式标题 |
| --force | 即使已在 session-log.jsonl 中也重新收集 |
| --output-dir
| 覆盖输出目录 |
故障排除
钩子在 /new 时未触发: 检查 openclaw.json 中是否启用了 collect-session,并确认网关已重启。在网关日志中查找 [collect-session] 行。
LITELLMAPIKEY not configured 警告: 设置 LITELLMAPIKEY 环境变量或编辑 CONFIG 块。脚本会回退到启发式标题,但仍会写入会话文件。
找不到会话目录: 默认会话路径为 ~/.openclaw/agents/main/sessions。如果你使用自定义代理名称,请更新 CONFIG 块中的 SESSIONS_DIR。
成本显示 $0.0000: LiteLLM 对某些提供商返回零成本。脚本使用内置定价表从 token 数量推导成本。在 CONFIG 块的 MODEL_PRICING 中添加缺失的模型。