/** * prune — format extract output to summary string */ import type { ExtractOutput, PruneOutput, TranscriptEntry } from './types.ts'; import { SECTION_SESSION, SECTION_FILES, SECTION_CONTEXT, SECTION_PREFS, SEPARATOR, DEFAULT_SECTION_LIMIT, DEFAULT_TRANSCRIPT_LIMIT, RECALL_NOTE, } from './constants.ts'; export interface FormatOptions { sectionLimit?: number; transcriptLimit?: number; sourceEntriesProcessed?: number; tailKept?: number; tokenEstimate?: number; } /** * Format extract output into a summary string for LLM context. */ export function format( data: ExtractOutput, options: FormatOptions = {}, ): PruneOutput { const start = performance.now(); const { sectionLimit = DEFAULT_SECTION_LIMIT, transcriptLimit = DEFAULT_TRANSCRIPT_LIMIT, sourceEntriesProcessed, tailKept, tokenEstimate, } = options; const sections: string[] = []; const sessionLines: string[] = []; for (const goal of data.goals.slice(0, sectionLimit)) { sessionLines.push(`- ${goal}`); } if (data.scopeChanges.length > 0) { sessionLines.push('- [Scope change]'); for (const change of data.scopeChanges.slice(0, sectionLimit)) { sessionLines.push(`- ${change}`); } } if (sessionLines.length > 0) { sections.push(`${SECTION_SESSION}\n${sessionLines.join('\n')}`); } const fileLines = formatFileLines(data.files); if (fileLines.length > 0) { sections.push(`${SECTION_FILES}\n${fileLines.join('\n')}`); } if (data.context.length > 0) { sections.push(`${SECTION_CONTEXT}\n${data.context.slice(0, sectionLimit).map(i => `- ${i}`).join('\n')}`); } if (data.preferences.length > 0) { sections.push(`${SECTION_PREFS}\n${data.preferences.slice(0, sectionLimit).map(i => `- ${i}`).join('\n')}`); } const transcriptLines = capTranscript( data.transcript.map(formatTranscriptEntry).filter(Boolean), transcriptLimit, ); const parts: string[] = []; if (sections.length > 0) parts.push(sections.join('\n\n')); if (transcriptLines.length > 0) parts.push(transcriptLines.join('\n')); const footer = formatFooter({ sourceEntriesProcessed, tailKept, tokenEstimate }); if (footer) parts.push(footer); parts.push(RECALL_NOTE); const summary = parts.join(SEPARATOR); const elapsedMs = performance.now() - start; return { summary, stats: { messagesIn: data.transcript.length, messagesOut: summary.split('\n').length, reduction: 0, elapsedMs, }, }; } function formatFileLines(files: ExtractOutput['files']): string[] { const lines: string[] = []; const modified = capSet(files.modified, 10); const created = capSet(files.created, 10); const read = capSet(files.read, 10); if (modified) lines.push(`- Modified: ${modified}`); if (created) lines.push(`- Created: ${created}`); if (read) lines.push(`- Read: ${read}`); return lines; } function capSet(set: Set, limit: number): string { const arr = [...set]; if (arr.length === 0) return ''; if (arr.length <= limit) return arr.join(', '); return `${arr.slice(0, limit).join(', ')} (+${arr.length - limit} more)`; } function capTranscript(lines: string[], limit: number): string[] { if (lines.length <= limit) return lines; const omitted = lines.length - limit; return [`...(${omitted} earlier lines omitted)`, '', ...lines.slice(-limit)]; } function formatFooter(options: { sourceEntriesProcessed?: number; tailKept?: number; tokenEstimate?: number; }): string { if (options.sourceEntriesProcessed == null || options.tailKept == null) return ''; const tok = options.tokenEstimate == null ? '' : ` (~${formatTokenEstimate(options.tokenEstimate)} tok)`; return `prune: ${options.sourceEntriesProcessed} source entries processed; tail kept ${options.tailKept}${tok}`; } function formatTokenEstimate(tokens: number): string { if (tokens >= 1000) return `${(tokens / 1000).toFixed(1)}k`; return String(tokens); } /** * Format a single transcript entry. */ function formatTranscriptEntry(entry: TranscriptEntry): string { switch (entry.role) { case 'user': return `[user]\n${entry.text}${entry.ref ? ` (${entry.ref})` : ''}`; case 'assistant': if (entry.tool) { const args = entry.cmd ? ` "${entry.cmd}"` : ''; return `* ${entry.tool}${args}${entry.ref ? ` (${entry.ref})` : ''}`; } return `[assistant]\n${entry.text}${entry.ref ? ` (${entry.ref})` : ''}`; case 'error': return `[tool_error] ${entry.text}${entry.ref ? ` (${entry.ref})` : ''}`; default: return ''; } }