Last active
April 9, 2026 18:37
-
-
Save stephenfeather/11429dbb842073276edeb32adad9c5f5 to your computer and use it in GitHub Desktop.
SessionID Injection Hook (SessionStart) - injects Claude Code session ID into every prompt as additionalContext
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
| // src/session-id-inject.ts | |
| import { readFileSync } from "fs"; | |
| /*! | |
| * SessionID Injection Hook (SessionStart) | |
| * | |
| * Injects the current session ID into SessionStart as additionalContext, | |
| * enabling session-aware capabilities | |
| * in your hooks. This is especially useful for tracking and debugging | |
| * conversations across multiple interactions. | |
| */ | |
| function readStdin() { | |
| return readFileSync(0, "utf-8"); | |
| } | |
| function main() { | |
| let input; | |
| try { | |
| input = JSON.parse(readStdin()); | |
| } catch { | |
| return; | |
| } | |
| if (!input || typeof input !== "object" || !input.session_id) { | |
| return; | |
| } | |
| if (process.env.CLAUDE_AGENT_ID) { | |
| return; | |
| } | |
| let SessionId = `SessionId: ${input.session_id}`; | |
| console.log(JSON.stringify({ | |
| hookSpecificOutput: { | |
| hookEventName: "SessionStart", | |
| additionalContext: SessionId | |
| } | |
| })); | |
| } | |
| main(); |
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
| /*! | |
| * SessionID Injection Hook (SessionStart) | |
| * | |
| * Injects the current session ID into SessionStart as additionalContext, | |
| * enabling session-aware capabilities | |
| * in your hooks. This is especially useful for tracking and debugging | |
| * conversations across multiple interactions. | |
| */ | |
| import { readFileSync } from 'fs'; | |
| interface SessionStartInput { | |
| session_id: string; | |
| hook_event_name: string; | |
| prompt: string; | |
| cwd: string; | |
| } | |
| function readStdin(): string { | |
| return readFileSync(0, 'utf-8'); | |
| } | |
| function main(): void { | |
| let input: SessionStartInput; | |
| try { | |
| input = JSON.parse(readStdin()); | |
| } catch { | |
| // Malformed or empty stdin — no-op to avoid killing the hook | |
| return; | |
| } | |
| if (!input || typeof input !== 'object' || !input.session_id) { | |
| return; | |
| } | |
| // Skip for subagents — they don't need session IDs | |
| if (process.env.CLAUDE_AGENT_ID) { | |
| return; | |
| } | |
| let SessionId: string = `SessionId: ${input.session_id}`; | |
| console.log(JSON.stringify({ | |
| hookSpecificOutput: { | |
| hookEventName: 'SessionStart', | |
| additionalContext: SessionId, | |
| }, | |
| })); | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment