Refactor hooks to standardize response format and improve error handling

- Introduced a new `hook-response.ts` module to create standardized hook responses.
- Updated `context-hook.ts`, `new.ts`, `save.ts`, and `summary.ts` to utilize the new response format.
- Enhanced error handling in `context-hook.ts` to check for input from stdin.
- Refactored database interaction in hooks to ensure consistent session management.
- Improved readability and maintainability of hook implementations by restructuring code.
- Updated database queries to use consistent variable naming and formatting.
- Modified the handling of socket connections in `save.ts` and `summary.ts` to ensure proper response on close and error events.
This commit is contained in:
Alex Newman
2025-10-16 20:50:04 -04:00
parent 6f62a569df
commit cedb635176
14 changed files with 187 additions and 77 deletions
+5 -4
View File
@@ -1,6 +1,7 @@
import { HooksDatabase } from '../services/sqlite/HooksDatabase.js';
import path from 'path';
import { spawn } from 'child_process';
import path from 'path';
import { HooksDatabase } from '../services/sqlite/HooksDatabase.js';
import { createHookResponse } from './hook-response.js';
export interface UserPromptSubmitInput {
session_id: string;
@@ -26,7 +27,7 @@ export function newHook(input?: UserPromptSubmitInput): void {
const existing = db.findActiveSDKSession(session_id);
if (existing) {
console.log('{"continue": true, "suppressOutput": true}');
console.log(createHookResponse('UserPromptSubmit', true));
return;
}
@@ -46,7 +47,7 @@ export function newHook(input?: UserPromptSubmitInput): void {
child.unref();
console.log('{"continue": true, "suppressOutput": true}');
console.log(createHookResponse('UserPromptSubmit', true));
} finally {
db.close();
}