/** * SDK Prompts Module * Generates prompts for the Claude Agent SDK memory worker */ export interface Observation { id: number; tool_name: string; tool_input: string; tool_output: string; created_at_epoch: number; } export interface SDKSession { id: number; sdk_session_id: string | null; project: string; user_prompt: string; } /** * Build initial prompt to initialize the SDK agent */ export function buildInitPrompt(project: string, sessionId: string, userPrompt: string): string { return `You are a memory processor for a Claude Code session. Your job is to analyze tool executions and create structured observations for information worth remembering. You are processing tool executions from a Claude Code session with the following context: User's Goal: ${userPrompt} Date: ${new Date().toISOString().split('T')[0]} WHEN TO STORE ------------- Store observations when the tool output contains information worth remembering about: - How things work - Why things exist or were chosen - What changed - Problems and their solutions - Important patterns or gotchas WHEN TO SKIP ------------ Skip routine operations: - Empty status checks - Package installations with no errors - Simple file listings - Repetitive operations you've already documented - **No output necessary if skipping.** OUTPUT FORMAT ------------- Output observations using this XML structure: \`\`\`xml [ change | discovery | decision ] [**title**: Short title capturing the core action or topic] [**subtitle**: One sentence explanation (max 24 words)] [Concise, self-contained statement] [Concise, self-contained statement] [Concise, self-contained statement] [**narrative**: Full context: What was done, how it works, why it matters] [knowledge-type-category] [knowledge-type-category] [path/to/file] [path/to/file] [path/to/file] [path/to/file] \`\`\` Process the following tool executions. MEMORY PROCESSING SESSION START ===============================`; } /** * Build prompt to send tool observation to SDK agent */ export function buildObservationPrompt(obs: Observation): string { // Safely parse tool_input and tool_output - they're already JSON strings let toolInput: any; let toolOutput: any; try { toolInput = typeof obs.tool_input === 'string' ? JSON.parse(obs.tool_input) : obs.tool_input; } catch { toolInput = obs.tool_input; // If parse fails, use raw value } try { toolOutput = typeof obs.tool_output === 'string' ? JSON.parse(obs.tool_output) : obs.tool_output; } catch { toolOutput = obs.tool_output; // If parse fails, use raw value } return ` ${obs.tool_name} ${new Date(obs.created_at_epoch).toISOString()} ${JSON.stringify(toolInput, null, 2)} ${JSON.stringify(toolOutput, null, 2)} `; } /** * Build finalization prompt to generate session summary */ export function buildFinalizePrompt(session: SDKSession): string { return `MEMORY PROCESSING SESSION COMPLETED =================================== This session has completed. Review the observations you generated and create a session summary. Output this XML: [What did the user request?] [What code and systems did you explore?] [What did you learn about the codebase?] [What was accomplished in this session?] [What should be done next?] [Additional insights or context] **Required fields**: request, investigated, learned, completed, next_steps **Optional fields**: notes`; }