Skip to content

feat: add CLI session scanning and sdkSessionId support#92

Open
GasonW wants to merge 2 commits intoop7418:mainfrom
GasonW:feature/cli-session-support
Open

feat: add CLI session scanning and sdkSessionId support#92
GasonW wants to merge 2 commits intoop7418:mainfrom
GasonW:feature/cli-session-support

Conversation

@GasonW
Copy link
Copy Markdown

@GasonW GasonW commented Apr 7, 2026

Summary

Add support for listing and binding to CLI sessions (sessions started directly from terminal with claude command).

This PR 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>

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

Details

CLI Session Scanning

The listCliSessions() method:

  • Reads session files from ~/.claude/sessions/{pid}.json
  • Checks if process is still active using kill(pid, 0)
  • Returns sessions sorted by startedAt descending (newest first)

Session Data Structure

CLI sessions contain:

  • sessionId: SDK session ID (used for --resume)
  • pid: Process ID
  • cwd: Working directory
  • startedAt: Start timestamp
  • kind: Session kind (e.g., "interactive")
  • entrypoint: Entry point (e.g., "cli")
  • name: Session name/alias
  • isActive: Whether the process is still running

sdkSessionId Support

Fixed upsertChannelBinding() to:

  • Accept sdkSessionId as an optional parameter
  • Update existing bindings with new sdkSessionId when provided
  • Use provided value instead of hardcoding empty string for new bindings

Related Work

This is part 1 of 2. The companion changes in the core claude-to-im package include:

  1. CardKit API v2 → v1 compatibility fix for Feishu streaming
  2. bindToSession() support for CLI session IDs
  3. /sessions command enhancement to show both Bridge and CLI sessions
  4. /bind command enhancement to show sdkSessionId and warnings

limuzhi.1 and others added 2 commits April 7, 2026 21:59
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>
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