Skip to content

feat: add /resume command and fix /compact to preserve session ID#34

Open
UnknownJackMe wants to merge 1 commit into
Wechat-ggGitHub:mainfrom
UnknownJackMe:feat/resume-and-compact
Open

feat: add /resume command and fix /compact to preserve session ID#34
UnknownJackMe wants to merge 1 commit into
Wechat-ggGitHub:mainfrom
UnknownJackMe:feat/resume-and-compact

Conversation

@UnknownJackMe

@UnknownJackMe UnknownJackMe commented Jun 13, 2026

Copy link
Copy Markdown

改动内容

新增:/resume 命令

列出并恢复当前工作目录(/cwd 所指定的目录)下的历史对话。

用法:

  • /resume — 列出最近 15 条历史对话,按最后修改时间倒序排列
  • /resume <编号> — 恢复列表中第 N 条对话
  • /resume <uuid> — 通过完整 session ID 恢复

标题显示逻辑:
直接读取每个 session 的 .jsonl 文件来提取标签,优先级与 Claude Code 自带的 /resume 选择器保持一致:

  1. 通过 /rename 设置的自定义标题(读取最新的 custom-title 事件)
  2. 对话中第一条真实用户消息
  3. 回退到 sessions-index.jsonsummary 字段

系统注入的噪音(<local-command-caveat> 块、[csb injected] 标记等)会被自动过滤。

修复:/compact 现在保持 session ID 不变

原来的 /compact 实现只是清除了 sdkSessionId,效果等同于 /clear——开启一个没有任何上下文的全新会话。

修复后改为调用 claude -p /compact --resume <sessionId>,触发 Claude Code 的原生压缩机制

  • 对话内容在原 session 内被总结压缩
  • token 用量大幅下降(实测:177k → 7k tokens,减少约 96%)
  • session ID 保持不变,下一条消息直接接续同一对话

同时解析 stream-json 输出中的 compact_boundary 事件,提取压缩前后的 token 数,并通过微信反馈给用户:

✅ 上下文已压缩
压缩前: 177,638 tokens → 压缩后: 7,332 tokens(减少 96%)

Session ID 不变,对话继续。可直接发送下一条消息。

实现细节

  • src/commands/handlers.ts:新增 handleResume(),含 session 索引解析逻辑;重写 handleCompact() 返回 { compactSession: true }
  • src/commands/router.ts:注册 resume 分支;在 CommandResult 中新增 compactSession 字段
  • src/claude/provider.ts:解析 compact_boundary 系统事件,将 token 统计以哨兵字符串 __compact__:pre:post 注入返回值
  • src/main.ts:新增 compactSession 分发分支和对应的异步函数 compactSession()

/resume (no args): lists recent conversations for the current working
directory, showing custom /rename titles, first user message, timestamp,
and message count — matching what Claude Code's own /resume picker shows.

/resume <N>: switches the active session to entry N from the list.
/resume <uuid>: switches by full session ID.

Session labels are read directly from the .jsonl transcript files so
custom-title (set by /rename) takes priority; falls back to the first
real user message, then the summary field.

/compact is rewritten to call `claude -p /compact --resume <sessionId>`
instead of clearing the session ID. This triggers Claude Code's native
compaction: the conversation is summarised in-place, token usage drops
dramatically, and the session ID is preserved — the next message
continues the same session with full context compressed.

The stream-json output is parsed for the compact_boundary event to
extract pre/post token counts, which are reported back to WeChat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant