9dbf63f5d4
The SDK agent's conversation history is heavily biased toward <observation> output. By the time a summarize prompt arrives, the in-context conditioning can cause the LLM to respond with <observation> tags instead of the expected <summary> tags. parseSummary() then returns null and the summary is silently lost. Two changes: - Add explicit mode-switch instructions at the top of the summary prompt telling the LLM not to use <observation> tags and that only <summary> output will be accepted - Add a warning log in parseSummary() when <observation> tags are found in a response that has no <summary> block, making the issue visible in logs instead of silently discarding Fixes #1312 Signed-off-by: umut-polat <52835619+umut-polat@users.noreply.github.com>
238 lines
7.3 KiB
TypeScript
238 lines
7.3 KiB
TypeScript
/**
|
|
* SDK Prompts Module
|
|
* Generates prompts for the Claude Agent SDK memory worker
|
|
*/
|
|
|
|
import { logger } from '../utils/logger.js';
|
|
import type { ModeConfig } from '../services/domain/types.js';
|
|
|
|
export interface Observation {
|
|
id: number;
|
|
tool_name: string;
|
|
tool_input: string;
|
|
tool_output: string;
|
|
created_at_epoch: number;
|
|
cwd?: string;
|
|
}
|
|
|
|
export interface SDKSession {
|
|
id: number;
|
|
memory_session_id: string | null;
|
|
project: string;
|
|
user_prompt: string;
|
|
last_assistant_message?: string;
|
|
}
|
|
|
|
/**
|
|
* Build initial prompt to initialize the SDK agent
|
|
*/
|
|
export function buildInitPrompt(project: string, sessionId: string, userPrompt: string, mode: ModeConfig): string {
|
|
return `${mode.prompts.system_identity}
|
|
|
|
<observed_from_primary_session>
|
|
<user_request>${userPrompt}</user_request>
|
|
<requested_at>${new Date().toISOString().split('T')[0]}</requested_at>
|
|
</observed_from_primary_session>
|
|
|
|
${mode.prompts.observer_role}
|
|
|
|
${mode.prompts.spatial_awareness}
|
|
|
|
${mode.prompts.recording_focus}
|
|
|
|
${mode.prompts.skip_guidance}
|
|
|
|
${mode.prompts.output_format_header}
|
|
|
|
\`\`\`xml
|
|
<observation>
|
|
<type>[ ${mode.observation_types.map(t => t.id).join(' | ')} ]</type>
|
|
<!--
|
|
${mode.prompts.type_guidance}
|
|
-->
|
|
<title>${mode.prompts.xml_title_placeholder}</title>
|
|
<subtitle>${mode.prompts.xml_subtitle_placeholder}</subtitle>
|
|
<facts>
|
|
<fact>${mode.prompts.xml_fact_placeholder}</fact>
|
|
<fact>${mode.prompts.xml_fact_placeholder}</fact>
|
|
<fact>${mode.prompts.xml_fact_placeholder}</fact>
|
|
</facts>
|
|
<!--
|
|
${mode.prompts.field_guidance}
|
|
-->
|
|
<narrative>${mode.prompts.xml_narrative_placeholder}</narrative>
|
|
<concepts>
|
|
<concept>${mode.prompts.xml_concept_placeholder}</concept>
|
|
<concept>${mode.prompts.xml_concept_placeholder}</concept>
|
|
</concepts>
|
|
<!--
|
|
${mode.prompts.concept_guidance}
|
|
-->
|
|
<files_read>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
</files_read>
|
|
<files_modified>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
</files_modified>
|
|
</observation>
|
|
\`\`\`
|
|
${mode.prompts.format_examples}
|
|
|
|
${mode.prompts.footer}
|
|
|
|
${mode.prompts.header_memory_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 (error) {
|
|
logger.debug('SDK', 'Tool input is plain string, using as-is', {
|
|
toolName: obs.tool_name
|
|
}, error as Error);
|
|
toolInput = obs.tool_input;
|
|
}
|
|
|
|
try {
|
|
toolOutput = typeof obs.tool_output === 'string' ? JSON.parse(obs.tool_output) : obs.tool_output;
|
|
} catch (error) {
|
|
logger.debug('SDK', 'Tool output is plain string, using as-is', {
|
|
toolName: obs.tool_name
|
|
}, error as Error);
|
|
toolOutput = obs.tool_output;
|
|
}
|
|
|
|
return `<observed_from_primary_session>
|
|
<what_happened>${obs.tool_name}</what_happened>
|
|
<occurred_at>${new Date(obs.created_at_epoch).toISOString()}</occurred_at>${obs.cwd ? `\n <working_directory>${obs.cwd}</working_directory>` : ''}
|
|
<parameters>${JSON.stringify(toolInput, null, 2)}</parameters>
|
|
<outcome>${JSON.stringify(toolOutput, null, 2)}</outcome>
|
|
</observed_from_primary_session>`;
|
|
}
|
|
|
|
/**
|
|
* Build prompt to generate progress summary
|
|
*/
|
|
export function buildSummaryPrompt(session: SDKSession, mode: ModeConfig): string {
|
|
const lastAssistantMessage = session.last_assistant_message || (() => {
|
|
logger.error('SDK', 'Missing last_assistant_message in session for summary prompt', {
|
|
sessionId: session.id
|
|
});
|
|
return '';
|
|
})();
|
|
|
|
return `--- MODE SWITCH: PROGRESS SUMMARY ---
|
|
Do NOT output <observation> tags. This is a summary request, not an observation request.
|
|
Your response MUST use <summary> tags ONLY. Any <observation> output will be discarded.
|
|
|
|
${mode.prompts.header_summary_checkpoint}
|
|
${mode.prompts.summary_instruction}
|
|
|
|
${mode.prompts.summary_context_label}
|
|
${lastAssistantMessage}
|
|
|
|
${mode.prompts.summary_format_instruction}
|
|
<summary>
|
|
<request>${mode.prompts.xml_summary_request_placeholder}</request>
|
|
<investigated>${mode.prompts.xml_summary_investigated_placeholder}</investigated>
|
|
<learned>${mode.prompts.xml_summary_learned_placeholder}</learned>
|
|
<completed>${mode.prompts.xml_summary_completed_placeholder}</completed>
|
|
<next_steps>${mode.prompts.xml_summary_next_steps_placeholder}</next_steps>
|
|
<notes>${mode.prompts.xml_summary_notes_placeholder}</notes>
|
|
</summary>
|
|
|
|
${mode.prompts.summary_footer}`;
|
|
}
|
|
|
|
/**
|
|
* Build prompt for continuation of existing session
|
|
*
|
|
* CRITICAL: Why contentSessionId Parameter is Required
|
|
* ====================================================
|
|
* This function receives contentSessionId from SDKAgent.ts, which comes from:
|
|
* - SessionManager.initializeSession (fetched from database)
|
|
* - SessionStore.createSDKSession (stored by new-hook.ts)
|
|
* - new-hook.ts receives it from Claude Code's hook context
|
|
*
|
|
* The contentSessionId is the SAME session_id used by:
|
|
* - NEW hook (to create/fetch session)
|
|
* - SAVE hook (to store observations)
|
|
* - This continuation prompt (to maintain session context)
|
|
*
|
|
* This is how everything stays connected - ONE session_id threading through
|
|
* all hooks and prompts in the same conversation.
|
|
*
|
|
* Called when: promptNumber > 1 (see SDKAgent.ts line 150)
|
|
* First prompt: Uses buildInitPrompt instead (promptNumber === 1)
|
|
*/
|
|
export function buildContinuationPrompt(userPrompt: string, promptNumber: number, contentSessionId: string, mode: ModeConfig): string {
|
|
return `${mode.prompts.continuation_greeting}
|
|
|
|
<observed_from_primary_session>
|
|
<user_request>${userPrompt}</user_request>
|
|
<requested_at>${new Date().toISOString().split('T')[0]}</requested_at>
|
|
</observed_from_primary_session>
|
|
|
|
${mode.prompts.system_identity}
|
|
|
|
${mode.prompts.observer_role}
|
|
|
|
${mode.prompts.spatial_awareness}
|
|
|
|
${mode.prompts.recording_focus}
|
|
|
|
${mode.prompts.skip_guidance}
|
|
|
|
${mode.prompts.continuation_instruction}
|
|
|
|
${mode.prompts.output_format_header}
|
|
|
|
\`\`\`xml
|
|
<observation>
|
|
<type>[ ${mode.observation_types.map(t => t.id).join(' | ')} ]</type>
|
|
<!--
|
|
${mode.prompts.type_guidance}
|
|
-->
|
|
<title>${mode.prompts.xml_title_placeholder}</title>
|
|
<subtitle>${mode.prompts.xml_subtitle_placeholder}</subtitle>
|
|
<facts>
|
|
<fact>${mode.prompts.xml_fact_placeholder}</fact>
|
|
<fact>${mode.prompts.xml_fact_placeholder}</fact>
|
|
<fact>${mode.prompts.xml_fact_placeholder}</fact>
|
|
</facts>
|
|
<!--
|
|
${mode.prompts.field_guidance}
|
|
-->
|
|
<narrative>${mode.prompts.xml_narrative_placeholder}</narrative>
|
|
<concepts>
|
|
<concept>${mode.prompts.xml_concept_placeholder}</concept>
|
|
<concept>${mode.prompts.xml_concept_placeholder}</concept>
|
|
</concepts>
|
|
<!--
|
|
${mode.prompts.concept_guidance}
|
|
-->
|
|
<files_read>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
</files_read>
|
|
<files_modified>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
<file>${mode.prompts.xml_file_placeholder}</file>
|
|
</files_modified>
|
|
</observation>
|
|
\`\`\`
|
|
${mode.prompts.format_examples}
|
|
|
|
${mode.prompts.footer}
|
|
|
|
${mode.prompts.header_memory_continued}`;
|
|
}
|