Fix: Restore hookSpecificOutput JSON format for SessionStart hook

The previous 'fix' to output plain text broke context injection.
SessionStart hooks require hookSpecificOutput JSON format.

Changes:
- Restore JSON output with hookSpecificOutput wrapper
- Keep improved error handling with JSON error output
- Tested with real SessionStart hook input format
This commit is contained in:
Alex Newman
2025-10-25 23:53:14 -04:00
parent c06abbc6f2
commit d363dfd668
2 changed files with 50 additions and 39 deletions
+16 -7
View File
@@ -22,21 +22,30 @@ if (stdin.isTTY) {
process.exit(1);
}
} else {
// Running from hook - output plain text to stdout (for SessionStart hooks)
// Running from hook - wrap in hookSpecificOutput JSON format
let input = '';
stdin.on('data', (chunk) => input += chunk);
stdin.on('end', () => {
try {
const parsed = input.trim() ? JSON.parse(input) : undefined;
const contextOutput = contextHook(parsed, false, useIndexView);
// SessionStart hooks add stdout directly to context
console.log(contextOutput);
const result = {
hookSpecificOutput: {
hookEventName: "SessionStart",
additionalContext: contextOutput
}
};
console.log(JSON.stringify(result));
process.exit(0);
} catch (error: any) {
// Output error to stdout so Claude sees it
console.log(`[claude-mem context-hook ERROR: ${error.message}]`);
console.log(`Input received: ${input.substring(0, 200)}...`);
console.log(error.stack);
// Output error in JSON format so hook doesn't fail
const errorResult = {
hookSpecificOutput: {
hookEventName: "SessionStart",
additionalContext: `[claude-mem ERROR: ${error.message}]\nInput: ${input.substring(0, 200)}...\n${error.stack}`
}
};
console.log(JSON.stringify(errorResult));
process.exit(1);
}
});