c03457d2d4
- Updated `save-hook.js`, `summary-hook.js`, `context-hook.ts`, `new-hook.ts`, and `save-hook.ts` to include a call to `ensureWorkerRunning()` at the beginning of their main functions. This ensures that the worker is active before any operations are performed. - Cleaned up import statements in the affected files to include the new utility function from `worker-utils.js`. - Minor adjustments to logging and error handling to improve robustness and clarity.
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
/**
|
|
* Summary Hook - Stop
|
|
* Consolidated entry point + logic
|
|
*/
|
|
|
|
import { stdin } from 'process';
|
|
import { SessionStore } from '../services/sqlite/SessionStore.js';
|
|
import { createHookResponse } from './hook-response.js';
|
|
import { logger } from '../utils/logger.js';
|
|
import { ensureWorkerRunning } from '../shared/worker-utils.js';
|
|
|
|
export interface StopInput {
|
|
session_id: string;
|
|
cwd: string;
|
|
[key: string]: any;
|
|
}
|
|
|
|
/**
|
|
* Summary Hook Main Logic
|
|
*/
|
|
async function summaryHook(input?: StopInput): Promise<void> {
|
|
if (!input) {
|
|
throw new Error('summaryHook requires input');
|
|
}
|
|
|
|
const { session_id } = input;
|
|
|
|
// Ensure worker is running
|
|
ensureWorkerRunning();
|
|
|
|
const db = new SessionStore();
|
|
|
|
// Get or create session
|
|
const sessionDbId = db.createSDKSession(session_id, '', '');
|
|
const promptNumber = db.getPromptCounter(sessionDbId);
|
|
db.close();
|
|
|
|
// Use fixed worker port
|
|
const FIXED_PORT = parseInt(process.env.CLAUDE_MEM_WORKER_PORT || '37777', 10);
|
|
|
|
logger.dataIn('HOOK', 'Stop: Requesting summary', {
|
|
sessionId: sessionDbId,
|
|
workerPort: FIXED_PORT,
|
|
promptNumber
|
|
});
|
|
|
|
try {
|
|
const response = await fetch(`http://127.0.0.1:${FIXED_PORT}/sessions/${sessionDbId}/summarize`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ prompt_number: promptNumber }),
|
|
signal: AbortSignal.timeout(2000)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
logger.failure('HOOK', 'Failed to generate summary', {
|
|
sessionId: sessionDbId,
|
|
status: response.status
|
|
}, errorText);
|
|
throw new Error(`Failed to request summary from worker: ${response.status} ${errorText}`);
|
|
}
|
|
|
|
logger.debug('HOOK', 'Summary request sent successfully', { sessionId: sessionDbId });
|
|
} catch (error: any) {
|
|
// Only show restart message for connection errors, not HTTP errors
|
|
if (error.cause?.code === 'ECONNREFUSED' || error.name === 'TimeoutError' || error.message.includes('fetch failed')) {
|
|
throw new Error("There's a problem with the worker. If you just updated, type `pm2 restart claude-mem-worker` in your terminal to continue");
|
|
}
|
|
// Re-throw HTTP errors and other errors as-is
|
|
throw error;
|
|
}
|
|
|
|
console.log(createHookResponse('Stop', true));
|
|
}
|
|
|
|
// Entry Point
|
|
let input = '';
|
|
stdin.on('data', (chunk) => input += chunk);
|
|
stdin.on('end', async () => {
|
|
const parsed = input ? JSON.parse(input) : undefined;
|
|
await summaryHook(parsed);
|
|
});
|