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:
+13
-4
@@ -1,6 +1,7 @@
|
||||
import net from 'net';
|
||||
import { HooksDatabase } from '../services/sqlite/HooksDatabase.js';
|
||||
import { getWorkerSocketPath } from '../shared/paths.js';
|
||||
import { createHookResponse } from './hook-response.js';
|
||||
|
||||
export interface StopInput {
|
||||
session_id: string;
|
||||
@@ -23,7 +24,7 @@ export function summaryHook(input?: StopInput): void {
|
||||
db.close();
|
||||
|
||||
if (!session) {
|
||||
console.log('{"continue": true, "suppressOutput": true}');
|
||||
console.log(createHookResponse('Stop', true));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -37,7 +38,15 @@ export function summaryHook(input?: StopInput): void {
|
||||
client.end();
|
||||
});
|
||||
|
||||
client.on('close', () => {
|
||||
console.log('{"continue": true, "suppressOutput": true}');
|
||||
});
|
||||
let responded = false;
|
||||
const respond = () => {
|
||||
if (responded) {
|
||||
return;
|
||||
}
|
||||
responded = true;
|
||||
console.log(createHookResponse('Stop', true));
|
||||
};
|
||||
|
||||
client.on('close', respond);
|
||||
client.on('error', respond);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user