-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add session command and improve payload type definitions #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add new `session` command to open latest session logs in default editor - Improve payload type definitions with specific tool input/response types - Add hook_event_name property to all payload interfaces for better type discrimination - Support opening session logs by ID or latest session - Add proper error handling and user feedback 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
WalkthroughA new CLI command for managing Claude session log files was introduced, allowing users to list and open session logs via platform-specific editors. Additionally, all exported hook payload interfaces were updated to include a fixed event name property, and the Changes
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/commands/session.ts (1)
94-99: Improve ID matching with better user feedback.The current ID matching implementation could provide better user feedback when multiple sessions match the partial ID.
Consider improving the ID matching logic:
if (flags.id) { - targetFile = fileStats.find((stat) => stat.sessionId.toLowerCase().includes(flags.id!.toLowerCase())) + const matches = fileStats.filter((stat) => stat.sessionId.toLowerCase().includes(flags.id!.toLowerCase())) + if (matches.length === 0) { - if (!targetFile) { console.log(chalk.red(`No session found matching ID: ${flags.id}`)) return - } + } else if (matches.length > 1) { + console.log(chalk.yellow(`Multiple sessions found matching "${flags.id}". Using the most recent:`)) + matches.slice(0, 3).forEach((match, index) => { + const marker = index === 0 ? chalk.green('→') : ' ' + console.log(`${marker} ${chalk.cyan(match.sessionId)} ${chalk.gray(match.mtime.toLocaleString())}`) + }) + if (matches.length > 3) { + console.log(chalk.gray(`... and ${matches.length - 3} more`)) + } + } + targetFile = matches[0] } else {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/commands/session.ts(1 hunks)templates/hooks/lib.ts(2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
templates/**/*
Instructions used from:
Sources:
📄 CodeRabbit Inference Engine
- CLAUDE.md
src/commands/**/*
Instructions used from:
Sources:
📄 CodeRabbit Inference Engine
- CLAUDE.md
🧠 Learnings (2)
📓 Common learnings
Learnt from: CR
PR: johnlindquist/claude-hooks#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-07T16:54:50.723Z
Learning: Applies to **/*.integration.{ts,js} : Integration tests should test the full CLI behavior.
Learnt from: CR
PR: johnlindquist/claude-hooks#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-07T16:54:50.723Z
Learning: Applies to .claude/{settings.json,hooks/index.ts,hooks/lib.ts,hooks/session.ts} : The CLI generates a '.claude/' directory containing 'settings.json', 'hooks/index.ts', 'hooks/lib.ts', and 'hooks/session.ts' when initialized.
src/commands/session.ts (1)
Learnt from: CR
PR: johnlindquist/claude-hooks#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-07T16:54:50.723Z
Learning: Applies to .claude/{settings.json,hooks/index.ts,hooks/lib.ts,hooks/session.ts} : The CLI generates a '.claude/' directory containing 'settings.json', 'hooks/index.ts', 'hooks/lib.ts', and 'hooks/session.ts' when initialized.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: test (windows-latest, 18)
- GitHub Check: test (windows-latest, 20)
🔇 Additional comments (3)
templates/hooks/lib.ts (1)
10-10: LGTM! Excellent type discrimination enhancement.The addition of
hook_event_namewith string literal types to all payload interfaces is a well-designed improvement that enables proper type discrimination at runtime. The consistent naming pattern across all interfaces enhances maintainability.Also applies to: 18-18, 29-29, 36-36, 43-43
src/commands/session.ts (2)
8-37: Well-structured command implementation.The command class is well-organized with clear documentation, appropriate flags, and helpful examples. The static properties follow oclif conventions properly.
42-44: Critical Issue: Session directory mismatch.The session command is looking for session files in the system temp directory (
claude-hooks-sessions), but the session management utilities intemplates/hooks/lib.tssave sessions to.claude/hooks/sessionsrelative to the current working directory. This mismatch will cause the command to never find any session files.Apply this fix to use the correct sessions directory:
- // Get the sessions directory from temp - const tempDir = os.tmpdir() - const sessionsDir = path.join(tempDir, 'claude-hooks-sessions') + // Get the sessions directory from .claude/hooks/sessions + const sessionsDir = path.join(process.cwd(), '.claude', 'hooks', 'sessions')⛔ Skipped due to learnings
Learnt from: CR PR: johnlindquist/claude-hooks#0 File: CLAUDE.md:0-0 Timestamp: 2025-07-07T16:54:50.723Z Learning: Applies to .claude/{settings.json,hooks/index.ts,hooks/lib.ts,hooks/session.ts} : The CLI generates a '.claude/' directory containing 'settings.json', 'hooks/index.ts', 'hooks/lib.ts', and 'hooks/session.ts' when initialized.Learnt from: CR PR: johnlindquist/claude-hooks#0 File: CLAUDE.md:0-0 Timestamp: 2025-07-07T16:54:50.723Z Learning: Applies to **/*.integration.{ts,js} : Integration tests should test the full CLI behavior.Learnt from: CR PR: johnlindquist/claude-hooks#0 File: CLAUDE.md:0-0 Timestamp: 2025-07-07T16:54:50.723Z Learning: Applies to src/commands/**/* : Commands must be implemented in the 'src/commands/' directory; currently, only the main 'init' command exists there.Learnt from: CR PR: johnlindquist/claude-hooks#0 File: CLAUDE.md:0-0 Timestamp: 2025-07-07T16:54:50.723Z Learning: Session logs are written to the system temp directory.Learnt from: CR PR: johnlindquist/claude-hooks#0 File: CLAUDE.md:0-0 Timestamp: 2025-07-07T16:54:50.723Z Learning: Applies to templates/**/* : Hook templates must be stored in the 'templates/' directory and are copied to the user's '.claude/' directory during initialization.
| return new Promise((resolve, reject) => { | ||
| const child = spawn(command, args, { | ||
| stdio: 'ignore', | ||
| detached: true, | ||
| }) | ||
|
|
||
| child.on('error', (error) => { | ||
| console.error(chalk.red('Failed to open file:'), error.message) | ||
| console.log(chalk.gray('You can manually open the file at:')) | ||
| console.log(chalk.cyan(filePath)) | ||
| reject(error) | ||
| }) | ||
|
|
||
| child.unref() | ||
| resolve() | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve error handling and process management.
The file opening logic has a few issues:
- The Promise resolves immediately without waiting for the process to actually start
- Error handling could provide better user feedback
- The process management could be more robust
Apply this improvement:
- return new Promise((resolve, reject) => {
+ return new Promise((resolve, reject) => {
const child = spawn(command, args, {
stdio: 'ignore',
detached: true,
})
+ child.on('spawn', () => {
+ child.unref()
+ resolve()
+ })
+
child.on('error', (error) => {
console.error(chalk.red('Failed to open file:'), error.message)
console.log(chalk.gray('You can manually open the file at:'))
console.log(chalk.cyan(filePath))
reject(error)
})
-
- child.unref()
- resolve()
})📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| return new Promise((resolve, reject) => { | |
| const child = spawn(command, args, { | |
| stdio: 'ignore', | |
| detached: true, | |
| }) | |
| child.on('error', (error) => { | |
| console.error(chalk.red('Failed to open file:'), error.message) | |
| console.log(chalk.gray('You can manually open the file at:')) | |
| console.log(chalk.cyan(filePath)) | |
| reject(error) | |
| }) | |
| child.unref() | |
| resolve() | |
| }) | |
| return new Promise((resolve, reject) => { | |
| const child = spawn(command, args, { | |
| stdio: 'ignore', | |
| detached: true, | |
| }) | |
| child.on('spawn', () => { | |
| child.unref() | |
| resolve() | |
| }) | |
| child.on('error', (error) => { | |
| console.error(chalk.red('Failed to open file:'), error.message) | |
| console.log(chalk.gray('You can manually open the file at:')) | |
| console.log(chalk.cyan(filePath)) | |
| reject(error) | |
| }) | |
| }) |
🤖 Prompt for AI Agents
In src/commands/session.ts lines 132 to 147, the Promise resolves immediately
after spawning the child process without confirming it started successfully, and
error handling lacks detailed user feedback. Modify the code to resolve the
Promise only after the child process has successfully started, enhance error
handling to provide clearer user messages including the file path, and improve
process management by ensuring the child process is properly detached and
unreferenced. This will make the file opening logic more reliable and
user-friendly.
|
🎉 This PR is included in version 1.4.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
sessioncommand to open latest session logs in the default editorChanges
New Session Command
src/commands/session.tsimplementing a new command to view session logs--idflag to open a specific session by partial ID match--listflag to display all available sessions with timestampsImproved Type Definitions
templates/hooks/lib.tswith better payload type definitionshook_event_nameproperty to all payload interfaces (PreToolUse, PostToolUse, Notification, Stop, SubagentStop)Test plan
sessioncommand functionality🤖 Generated with Claude Code
Summary by CodeRabbit