${roleLabel}
\nimport { formatAiInlineMarkdown, escapeHtml } from "@/lib/ai/aiMarkdown"; import { AI_HTML_PREVIEW_CSP } from "@/lib/ai/richContent/aiHtmlPreview"; import { compactLocalTimestamp, sanitizeExportBaseName } from "./saveTextFile"; /** * Conversation-level export (#6467 PR3). * * Source boundary: the caller passes the **visible transcript** * (`visibleMessages`) โ the same messages the chat renders. Internal entries * (`contextSummary`, reasoning, tool arguments/results, attachment payloads) * never reach this module; `kind === "contextSummary"` is additionally skipped * here as defense in depth. * * Rendering rules (V1): * - Markdown export keeps every message's content **verbatim** โ fenced * ```chart-json / ```html blocks survive byte-identical. * - HTML export renders each message through `formatAiInlineMarkdown`, whose * renderer escapes raw HTML, so ```chart-json / ```html fences come out as * escaped code blocks โ original HTML is never injected into the report. * - The report document carries the same CSP meta as the safe-HTML preview * shell (`AI_HTML_PREVIEW_CSP`): markdown links/images are the one path * where a report could reference remote resources, and the CSP blocks those * fetches in the standalone file. */ export type AiConversationExportFormat = "markdown" | "html"; export interface AiConversationExportMessage { role: "user" | "assistant"; content: string; /** Hidden system-generated entries; `contextSummary` is never exported. */ kind?: string; /** Marks assistant replies whose generation failed; rendered with a marker. */ failed?: boolean; } export interface AiConversationExportLabels { analysisLabel: string; userLabel: string; assistantLabel: string; failedLabel: string; } export interface BuildAiConversationExportInput { connectionName?: string; dateLabel: string; messages: AiConversationExportMessage[]; format: AiConversationExportFormat; labels: AiConversationExportLabels; } export interface BuildAiConversationExportOutput { content: string; defaultFileName: string; } function exportableMessages(messages: AiConversationExportMessage[]): AiConversationExportMessage[] { return messages.filter((msg) => msg.kind !== "contextSummary" && !!msg.content.trim()); } export function buildAiConversationExport(input: BuildAiConversationExportInput): BuildAiConversationExportOutput | null { const messages = exportableMessages(input.messages); if (!messages.length) return null; const rawName = input.connectionName || ""; const sanitizedName = sanitizeExportBaseName(rawName) || "ai"; const displayName = rawName || "AI"; const extension = input.format === "markdown" ? "md" : "html"; const title = `${displayName} ยท ${input.labels.analysisLabel}`; const content = input.format === "markdown" ? buildMarkdownReport(title, input.dateLabel, messages, input.labels) : buildHtmlReport(title, input.dateLabel, messages, input.labels); return { content, defaultFileName: `${sanitizedName}_${compactLocalTimestamp()}.${extension}` }; } function buildMarkdownReport(title: string, dateLabel: string, messages: AiConversationExportMessage[], labels: AiConversationExportLabels): string { const lines: string[] = [`# ${title}`, dateLabel, ""]; for (const msg of messages) { lines.push(`## ${msg.role === "user" ? labels.userLabel : labels.assistantLabel}`, ""); if (msg.failed) lines.push(`**${labels.failedLabel}**`, ""); lines.push(msg.content, ""); } return lines.join("\n"); } function buildHtmlReport(title: string, dateLabel: string, messages: AiConversationExportMessage[], labels: AiConversationExportLabels): string { const escapedTitle = escapeHtml(title); const articles = messages .map((msg) => { const roleLabel = escapeHtml(msg.role === "user" ? labels.userLabel : labels.assistantLabel); const body = formatAiInlineMarkdown(msg.content); const failedMark = msg.failed ? `\n
${escapeHtml(labels.failedLabel)}
` : ""; return `${roleLabel}
\n