Community编程与开发github.com

会话日志搜索

使用 jq 搜索和分析您自己的会话日志(旧的/父级对话)。

会话日志搜索 是什么?

该技能使 AI 代理能够搜索和分析其完整的会话历史记录,这些记录存储在 JSONL 文件中。当用户引用较早的对话、父级对话或在内存文件中找不到的历史上下文时,它尤其有用。该技能支持 OpenClaw 平台,并利用 `OPENCLAW_STATE_DIR` 环境变量定位存储在 `sessions/` 目录下的会话日志。它提供了许多使用 `jq` 和 `rg` 的实用查询示例,用于提取用户消息、搜索关键字、计算成本、分析工具使用情况以及跨所有会话搜索短语。该技能还特别强调了包含存档的 `.reset.*` 和 `.deleted.*` 文件的重要性,这些文件仍包含可搜索的完整记录,并提供了相关代码片段来确保全面召回。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/clawdbot/clawdbot/tree/main/skills/session-logs

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

会话日志搜索 是做什么的?

搜索存储在会话 JSONL 文件中的完整对话历史。当用户引用较旧的/父级对话或询问之前说过什么时,请使用此技能。

触发条件

当用户询问先前的聊天记录、父级对话或不在内存文件中的历史上下文时,请使用此技能。

位置

会话日志位于活动状态目录下: $OPENCLAW_STATE_DIR/agents/<agentId>/sessions/(默认:~/.openclaw/agents/<agentId>/sessions/)。 使用系统提示的 Runtime 行中的 agent=<id> 值。

  • sessions.json - 将会话密钥映射到会话 ID 的索引
  • <session-id>.jsonl - 每个会话的完整对话记录
  • <session-id>.jsonl.reset.<timestamp>Z - 由 /new 或 /reset 归档的记录
  • <session-id>.jsonl.deleted.<timestamp>Z - 会话删除时归档的记录

搜索历史记录时,请同时包含归档的(.reset.*、.deleted.*)变体——它们仍然包含真实的会话内容。下面的简单 glob 示例仅匹配当前的 *.jsonl 文件;当需要完全召回时,请使用“包含归档记录”代码片段。

结构

每个 .jsonl 文件包含以下消息:

  • type:"session"(元数据)或 "message"
  • timestamp:ISO 时间戳
  • message.role:"user"、"assistant" 或 "toolResult"
  • message.content[]:文本、思考或工具调用(过滤 type=="text" 以获得人类可读内容)
  • message.usage.cost.total:每次响应的成本

常见查询

包含归档记录(.reset.*、.deleted.*)

# Bash 辅助函数,输出所有可搜索的记录路径——当前的和归档的。
# 局部保存和恢复 nullglob,以免影响调用者的 shell 选项。
AGENT_ID="<agentId>"
SESSION_DIR="${OPENCLAW_STATE_DIR:-$HOME/.openclaw}/agents/$AGENT_ID/sessions"
list_session_transcripts() {
  local _nullglob_state
  _nullglob_state=$(shopt -p nullglob 2>/dev/null)
  shopt -s nullglob
  for f in "$SESSION_DIR"/*.jsonl \
           "$SESSION_DIR"/*.jsonl.reset.*Z \
           "$SESSION_DIR"/*.jsonl.deleted.*Z; do
    [ -f "$f" ] && printf '%s\n' "$f"
  done
  eval "$_nullglob_state"
}

在下面显示简单的 *.jsonl glob 的地方,如果需要包含已归档的会话,请使用 list_session_transcripts(或等效的 find 调用):

find "$SESSION_DIR" -maxdepth 1 -type f \
  \( -name '*.jsonl' -o -name '*.jsonl.reset.*Z' -o -name '*.jsonl.deleted.*Z' \) -print

按日期和大小列出所有会话

AGENT_ID="<agentId>"
SESSION_DIR="${OPENCLAW_STATE_DIR:-$HOME/.openclaw}/agents/$AGENT_ID/sessions"
for f in "$SESSION_DIR"/*.jsonl; do
  date=$(head -1 "$f" | jq -r '.timestamp' | cut -dT -f1)
  size=$(ls -lh "$f" | awk '{print $5}')
  echo "$date $size $(basename $f)"
done | sort -r

提示: 如果也想在列表中包含已归档的 .reset / .deleted 文件,请将 for f in ... 行替换为 while-read list_session_transcripts(参见上面的代码片段)。while-read 模式对于包含空格或其他 IFS 字符的路径是安全的:

while IFS= read -r f; do
  date=$(head -1 "$f" | jq -r '.timestamp' | cut -dT -f1)
  size=$(ls -lh "$f" | awk '{print $5}')
  echo "$date $size $(basename "$f")"
done < <(list_session_transcripts) | sort -r

查找特定日期的会话

AGENT_ID="<agentId>"
SESSION_DIR="${OPENCLAW_STATE_DIR:-$HOME/.openclaw}/agents/$AGENT_ID/sessions"
for f in "$SESSION_DIR"/*.jsonl; do
  head -1 "$f" | jq -r '.timestamp' | grep -q "2026-01-06" && echo "$f"
done

从会话中提取用户消息

jq -r 'select(.message.role == "user") | .message.content[]? | select(.type == "text") | .text' <session>.jsonl

在助手回复中搜索关键字

jq -r 'select(.message.role == "assistant") | .message.content[]? | select(.type == "text") | .text' <session>.jsonl | rg -i "keyword"

获取会话总成本

jq -s '[.[] | .message.usage.cost.total // 0] | add' <session>.jsonl

每日成本汇总

AGENT_ID="<agentId>"
SESSION_DIR="${OPENCLAW_STATE_DIR:-$HOME/.openclaw}/agents/$AGENT_ID/sessions"
for f in "$SESSION_DIR"/*.jsonl; do
  date=$(head -1 "$f" | jq -r '.timestamp' | cut -dT -f1)
  cost=$(jq -s '[.[] | .message.usage.cost.total // 0] | add' "$f")
  echo "$date $cost"
done | awk '{a[$1]+=$2} END {for(d in a) print d, "$"a[d]}' | sort -r

统计会话中的消息和令牌数

jq -s '{
  messages: length,
  user: [.[] | select(.message.role == "user")] | length,
  assistant: [.[] | select(.message.role == "assistant")] | length,
  first: .[0].timestamp,
  last: .[-1].timestamp
}' <session>.jsonl

工具使用情况明细

jq -r '.message.content[]? | select(.type == "toolCall") | .name' <session>.jsonl | sort | uniq -c | sort -rn

跨所有会话搜索短语

AGENT_ID="<agentId>"
SESSION_DIR="${OPENCLAW_STATE_DIR:-$HOME/.openclaw}/agents/$AGENT_ID/sessions"

# 仅当前会话:
rg -l "phrase" "$SESSION_DIR"/*.jsonl

# 当前 + 已归档(`.reset.*`、`.deleted.*`)——在检查可能已被压缩/重置/删除的内容时使用此命令:
rg -l "phrase" "$SESSION_DIR"/*.jsonl \
               "$SESSION_DIR"/*.jsonl.reset.*Z \
               "$SESSION_DIR"/*.jsonl.deleted.*Z 2>/dev/null

提示

  • 会话是仅追加的 JSONL(每行一个 JSON 对象)
  • 大型会话可能达到几 MB - 使用 head/tail 进行采样
  • sessions.json 索引将聊天提供者(discord、whatsapp 等)映射到会话 ID
  • 已重置/压缩的会话 具有 .jsonl.reset.<timestamp>Z 后缀——仍然包含完整记录且可搜索。
  • 已删除的会话 具有 .jsonl.deleted.<timestamp>Z 后缀——仍然可搜索。
  • 简单的 *.jsonl glob 会遗漏这两种归档形式。需要完整历史时,请显式包含它们(参见上面的“包含归档记录”代码片段)。

快速纯文本提示(低噪音)

AGENT_ID="<agentId>"
SESSION_DIR="${OPENCLAW_STATE_DIR:-$HOME/.openclaw}/agents/$AGENT_ID/sessions"
jq -r 'select(.type=="message") | .message.content[]? | select(.type=="text") | .text' "$SESSION_DIR"/<id>.jsonl | rg 'keyword'

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

1password

Set up and use 1Password CLI for sign-in, desktop integration, and reading or injecting secrets.

acp-router

Route plain-language requests for Claude Code, Cursor, Copilot, OpenClaw ACP, OpenCode, Gemini CLI, Grok Build, Qwen, Kiro, Kimi, iFlow, Factory Droid, Kilocode, or explicit ACP harness work into either OpenClaw ACP runtime sessions or direct acpx-driven sessions ("telephone game" flow). For coding-agent thread requests, read this skill first, then use only `sessions_spawn` for thread creation. Codex chat binding defaults to the native Codex app-server plugin unless ACP is explicit or background spawn needs ACP.

add-model-provider

Add and live-prove a model provider with non-interactive config one-liners, without exposing credentials.

agent-transcript

Requested GitHub PR/issue agent transcripts: redact, trim, preview, and insert safely.

apple-notes

Create, view, edit, delete, search, move, or export Apple Notes via the memo CLI on macOS.

apple-reminders

List, add, edit, complete, or delete Apple Reminders and reminder lists via remindctl.

auto-qa

Run OpenClaw-wide autonomous QA and live/stress campaigns across independent subsystem lanes, with verified fixes and a resumable evidence report.

autoreview

Structured code review through the shared OpenClaw agent-skills installation.

bear-notes

Create, search, and manage Bear notes via grizzly CLI.

blacksmith-testbox

Run Blacksmith Testbox for CI-parity checks, secrets, hosted services, migrations, or builds local cannot reproduce.

block-kit

Use proactively for structured or interactive Slack replies, and when asked to author or validate native Slack Block Kit JSON.

blogwatcher

Monitor blogs and RSS/Atom feeds for updates using the blogwatcher CLI.

blucli

BluOS CLI (blu) for discovery, playback, grouping, and volume.

bluebubbles

Send and manage iMessages via BlueBubbles, including attachments, tapbacks, edits, replies, and groups.

browser-automation

Use when controlling web pages with the OpenClaw browser tool, especially multi-step flows, login checks, tab management, or recovery from stale refs/timeouts.

camsnap

Capture frames or clips from RTSP/ONVIF cameras and local webcams, including USB pan/tilt/zoom control.

canvas

Present HTML on connected OpenClaw node canvases, navigate/eval/snapshot, and debug canvas host URLs.

channel-message-flows

Use when running QA Lab channel message flow evidence.

clawdtributor

Clawtributor PRs here, last week or another window: discover conversation refs, recheck GitHub, rank by impact.

clawhub

Search ClawHub for plugins by default, or skills and agent Templates when requested; add Templates; install, verify, update, uninstall, publish, or sync skills.

相关技能