/** * 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 observing a development session to create searchable memory FOR FUTURE SESSIONS. CRITICAL: Record what was BUILT/FIXED/DEPLOYED/CONFIGURED, not what you (the observer) are doing. User's Goal: ${userPrompt} Date: ${new Date().toISOString().split('T')[0]} WHAT TO RECORD -------------- Focus on deliverables and capabilities: - What the system NOW DOES differently (new capabilities) - What shipped to users/production (features, fixes, configs, docs) - Changes in technical domains (auth, data, UI, infra, DevOps, docs) Use verbs like: implemented, fixed, deployed, configured, migrated, optimized, added, refactored ✅ GOOD EXAMPLES (describes what was built): - "Authentication now supports OAuth2 with PKCE flow" - "Deployment pipeline runs canary releases with auto-rollback" - "Database indexes optimized for common query patterns" ❌ BAD EXAMPLES (describes observation process - DO NOT DO THIS): - "Analyzed authentication implementation and stored findings" - "Tracked deployment steps and logged outcomes" - "Monitored database performance and recorded metrics" 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 [ bugfix | feature | refactor | 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 prompt to generate request summary */ export function buildSummaryPrompt(session: SDKSession): string { return `THIS REQUEST'S SUMMARY =============== Think about the observations you just wrote for this request, and write a summary of what was done, what was learned, and what's next. IMPORTANT! DO NOT summarize the observation process itself - you are summarizing a DIFFERENT claude code session, not this one. User's Original Request: ${session.user_prompt} WHEN NOT TO SUMMARIZE ---------------------- Do not summarize if the request is conversational and unrelated to the work that was just completed. If skipping, **output only**: ✅ GOOD - Describes deliverables: Fix authentication timeout bug Add three-tier verbosity system to session summaries Deploy Kubernetes cluster with auto-scaling ❌ BAD - Describes meta-operations (DO NOT DO THIS): Process tool executions and store observations Analyze session data and generate summaries Track file modifications across sessions Output this XML: [What did the user request? Use their original sentiment from: ${session.user_prompt}] [What was explored?] [What was discovered about how things work?] [What shipped? What does the system now do?] [What are the next steps?] [Additional insights] **Required fields**: request, investigated, learned, completed, next_steps **Optional fields**: notes`; }