import { getFiletypeFromFileName } from '@pierre/diffs'; // Content the agent showed with `display(content, title=, description=)`. // `albedo_trace.py` records each call in the cell's trace as a `display` // activity (`target` the label, `detail` the optional `**title**` and // description header followed by the content). export type DisplayMode = 'markdown' | 'code' | 'diff'; export interface DiffPart { path: string; diff: string; added: number; removed: number; } export interface ParsedDisplay { /** Optional description above a diff, code block, or markdown document, with the redundant `**title**` stripped. */ header: string; /** The body to render: diff text, code text, or markdown text. */ body: string; /** The natural way to read the block: a unified diff, syntax-highlighted code, or rendered markdown. */ natural: DisplayMode; /** File name passed to Pierre's highlighter so it picks the language from the title when known. */ fileName: string; /** Individual file diffs when `natural === 'diff'`. */ files: DiffPart[]; } const DIFF_LINE = /^(?:@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@|diff --git )/m; function stripTitle(target: string, text: string): string { const prefix = `**${target.trim()}**`; if (text === prefix) return ''; if (text.startsWith(`${prefix}\n\n`)) return text.slice(prefix.length + 2); if (text.startsWith(`${prefix}\n`)) return text.slice(prefix.length + 1); return text; } function splitDiff( target: string, detail: string, ): { header: string; diff: string } | null { const lines = detail.split('\n'); for (let i = 0; i < lines.length; i++) { const line = lines[i]; if ( DIFF_LINE.test(line) || (line.startsWith('--- ') && i + 1 < lines.length && lines[i + 1].startsWith('+++ ')) ) { return { header: stripTitle(target, lines.slice(0, i).join('\n').trim()), diff: lines.slice(i).join('\n'), }; } } return null; } /** Splits a unified diff or git patch into one entry per file, with line counts. */ function splitPatchFiles(patch: string, fallbackName: string): DiffPart[] { const lines = patch.split('\n'); const chunks: string[][] = []; for (let i = 0; i < lines.length; i++) { const line = lines[i]; const isGitHeader = line.startsWith('diff --git '); const isUnifiedHeader = line.startsWith('--- ') && i + 1 < lines.length && lines[i + 1].startsWith('+++ ') && chunks.length > 0 && chunks.at(-1)!.some((prev) => prev.startsWith('@@ ')); if ((isGitHeader || isUnifiedHeader) && chunks.length > 0) chunks.push([line]); else if (chunks.length === 0) chunks.push([line]); else chunks.at(-1)!.push(line); } return chunks.map((chunk, index) => { const diff = chunk.join('\n'); const plusMatch = /^\+\+\+ (?:b\/)?([^\n\t]+)/m.exec(diff); const minusMatch = /^--- (?:a\/)?([^\n\t]+)/m.exec(diff); const gitMatch = /^diff --git a\/\S+ b\/([^\n\t]+)/m.exec(diff); const candidate = [plusMatch?.[1], gitMatch?.[1], minusMatch?.[1]] .find((name) => name && name !== '/dev/null') ?.trim(); const path = candidate || (chunks.length === 1 ? fallbackName : `file-${index + 1}.patch`); let added = 0; let removed = 0; for (const line of chunk) { if (line.startsWith('+') && !line.startsWith('+++')) added++; else if (line.startsWith('-') && !line.startsWith('---')) removed++; } return { path, diff, added, removed }; }); } /** Strips the `**title**` (and optional description) prefix `albedo_trace.py` adds before code content. */ function splitCodeHeader( target: string, detail: string, ): { header: string; body: string } { const prefix = `**${target.trim()}**`; if (!detail.startsWith(prefix)) return { header: '', body: detail }; const rest = detail.slice(prefix.length); if (rest.startsWith('\n\n')) return { header: '', body: rest.slice(2) }; if (rest.startsWith('\n')) { const after = rest.slice(1); const sep = after.indexOf('\n\n'); if (sep >= 0) return { header: after.slice(0, sep).trim(), body: after.slice(sep + 2) }; } return { header: '', body: detail }; } /** Whether `target` names a source file whose language Pierre highlights as code rather than prose. */ function codeFile(target: string): boolean { const name = target.trim().split(/[/\\]/).pop() ?? ''; if (!name || /\s/.test(name)) return false; const kind = getFiletypeFromFileName(name); return ( kind !== 'text' && kind !== 'markdown' && kind !== 'mdx' && kind !== 'mdc' ); } export function parseDisplay( target: string, detail: string, description = '', ): ParsedDisplay { const trimmed = target.trim() || 'display'; const content = description ? `${description}\n\n${detail}` : detail; const diff = splitDiff(trimmed, content); if (diff) { const fallback = codeFile(trimmed) ? trimmed : 'display.patch'; const files = splitPatchFiles(diff.diff, fallback); const fileName = files[0]?.path ?? fallback; return { header: diff.header, body: diff.diff, natural: 'diff', fileName, files, }; } if (codeFile(trimmed)) { const { header, body } = splitCodeHeader(trimmed, content); return { header, body, natural: 'code', fileName: trimmed, files: [] }; } const { header, body } = splitCodeHeader(trimmed, content); return { header, body, natural: 'markdown', fileName: trimmed, files: [] }; }