fix: Refactor context hook to use proper SessionStart hookSpecificOutput format

The context hook was not appearing in Claude Code sessions because it was
outputting plain text to stdout instead of using the required JSON structure
for SessionStart hooks.

Changes:
- src/hooks/context.ts: Changed contextHook to return string instead of void,
  removing direct console.log calls to make it more reusable
- src/bin/hooks/context-hook.ts: Wrap contextHook output in hookSpecificOutput
  JSON structure with hookEventName "SessionStart" and additionalContext field
- Both TTY and stdin code paths now properly format and exit with code 0

Fixes the issue where recent session context was not being injected at session
start. Tested with npm run test:context - hook now properly outputs JSON with
recent sessions formatted as markdown.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Alex Newman
2025-10-18 21:03:57 -04:00
parent 56167c47a2
commit 5dd663730b
3 changed files with 27 additions and 15 deletions
+17 -2
View File
@@ -9,13 +9,28 @@ import { stdin } from 'process';
try {
if (stdin.isTTY) {
contextHook();
const contextOutput = contextHook();
const result = {
hookSpecificOutput: {
hookEventName: "SessionStart",
additionalContext: contextOutput
}
};
console.log(JSON.stringify(result));
process.exit(0);
} else {
let input = '';
stdin.on('data', (chunk) => input += chunk);
stdin.on('end', () => {
const parsed = input.trim() ? JSON.parse(input) : undefined;
contextHook(parsed);
const contextOutput = contextHook(parsed);
const result = {
hookSpecificOutput: {
hookEventName: "SessionStart",
additionalContext: contextOutput
}
};
console.log(JSON.stringify(result));
process.exit(0);
});
}