feat: Implement Phase 2 of SDK Worker Process

- Added background agent architecture for processing tool observations and generating session summaries.
- Created SDK Prompts Module for generating prompts for the Claude Agent SDK.
- Developed XML Parser Module for parsing observation and summary XML blocks from SDK responses.
- Implemented SDK Worker Process to handle observation processing and session management.
- Updated newHook implementation to spawn the SDK worker as a detached process with path resolution for development and production.
- Created comprehensive test suite for SDK prompts, XML parsing, and HooksDatabase integration, ensuring all tests pass.
- Documented Phase 2 implementation details, architecture validation, and success criteria in PHASE2-COMPLETE.md.
This commit is contained in:
Alex Newman
2025-10-15 19:18:38 -04:00
parent d07a40616d
commit 78fd1368db
8 changed files with 1150 additions and 91 deletions
+24 -10
View File
@@ -1,6 +1,7 @@
import { HooksDatabase } from '../services/sqlite/HooksDatabase.js';
import path from 'path';
import { spawn } from 'child_process';
import fs from 'fs';
export interface UserPromptSubmitInput {
session_id: string;
@@ -35,17 +36,30 @@ export function newHook(input: UserPromptSubmitInput): void {
const sessionId = db.createSDKSession(session_id, project, prompt);
db.close();
// Start SDK worker in background
// The SDK worker will be implemented in a separate file
// For now, we just create the session record
// Start SDK worker in background as detached process
// Try source first (development), then fall back to dist (production)
const srcWorkerPath = path.join(__dirname, '..', 'sdk', 'worker.ts');
const distWorkerPath = path.join(__dirname, '..', 'sdk', 'worker.js');
// TODO: Spawn SDK worker as detached process
// const workerPath = path.join(__dirname, '..', 'sdk', 'worker.js');
// const child = spawn('bun', [workerPath, sessionId.toString()], {
// detached: true,
// stdio: 'ignore'
// });
// child.unref();
let workerPath: string;
if (fs.existsSync(srcWorkerPath)) {
workerPath = srcWorkerPath;
} else if (fs.existsSync(distWorkerPath)) {
workerPath = distWorkerPath;
} else {
// Fallback: assume we're in the bundled CLI
// In this case, we can't spawn the worker since it's bundled
// This is a limitation we'll need to address
console.error('[claude-mem] Worker not found, skipping background processing');
console.log('{"continue": true, "suppressOutput": true}');
process.exit(0);
}
const child = spawn('bun', [workerPath, sessionId.toString()], {
detached: true,
stdio: 'ignore'
});
child.unref();
// Output hook response
console.log('{"continue": true, "suppressOutput": true}');