feat: add CLI session scanning and sdkSessionId support#92
Open
GasonW wants to merge 2 commits intoop7418:mainfrom
Open
feat: add CLI session scanning and sdkSessionId support#92GasonW wants to merge 2 commits intoop7418:mainfrom
GasonW wants to merge 2 commits intoop7418:mainfrom
Conversation
Add support for listing and binding to CLI sessions (sessions started directly from terminal with `claude` command). Changes: - Add `CliSession` interface for CLI session metadata - Add `listCliSessions()` method to scan ~/.claude/sessions/*.json - Add `getCliSession()` method with prefix matching support - Fix `upsertChannelBinding()` to use provided `sdkSessionId` parameter instead of hardcoding empty string The CLI session scanning reads session files created by Claude Code CLI and checks if the process is still active using `kill(pid, 0)`. This enables the bridge to: 1. List all sessions (both bridge-created and CLI-created) in /sessions 2. Bind to CLI sessions using /bind <sdkSessionId> 3. Resume sessions in terminal using `claude --resume <sdkSessionId>` Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
新增功能:
- SessionState 接口:用于桥接服务与 CLI session 之间的状态同步
- 状态文件管理:getSessionState(), writeSessionState()
- 接管标记:markSessionTakenOver() - 当从飞书绑定 CLI session 时标记状态
- 活动记录:recordBridgeActivity() - 记录飞书侧的消息活动
- TTY 实时通知:writeToTty(), notifyCliSession() - 向 CLI 终端发送实时消息
- 进程控制:terminateCliSession() - 终止关联的 CLI 进程
- 扩展查询:getCliSessionWithState() - 获取包含状态信息的 CLI session
状态文件存储位置:~/.claude-to-im/data/session-states/{sdkSessionId}.json
这个实现为 session 接管功能提供了完整的状态同步机制,包括:
1. 双向状态标记(CLI 启动时写入,飞书绑定时标记)
2. 实时 TTY 通知(当飞书操作时,CLI 终端能看到提示)
3. 进程终止能力(支持 /terminate 命令)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add support for listing and binding to CLI sessions (sessions started directly from terminal with
claudecommand).This PR enables the bridge to:
/sessions/bind <sdkSessionId>claude --resume <sdkSessionId>Changes
CliSessioninterface for CLI session metadatalistCliSessions()method to scan~/.claude/sessions/*.jsongetCliSession()method with prefix matching supportupsertChannelBinding()to use providedsdkSessionIdparameter instead of hardcoding empty stringDetails
CLI Session Scanning
The
listCliSessions()method:~/.claude/sessions/{pid}.jsonkill(pid, 0)startedAtdescending (newest first)Session Data Structure
CLI sessions contain:
sessionId: SDK session ID (used for--resume)pid: Process IDcwd: Working directorystartedAt: Start timestampkind: Session kind (e.g., "interactive")entrypoint: Entry point (e.g., "cli")name: Session name/aliasisActive: Whether the process is still runningsdkSessionId Support
Fixed
upsertChannelBinding()to:sdkSessionIdas an optional parametersdkSessionIdwhen providedRelated Work
This is part 1 of 2. The companion changes in the core
claude-to-impackage include:bindToSession()support for CLI session IDs/sessionscommand enhancement to show both Bridge and CLI sessions/bindcommand enhancement to showsdkSessionIdand warnings