feat(ai): export conversation as markdown or html
This commit is contained in:
1 parent
d0832f17f4
commit
2b2d1f0d95
20 files changed
+506
-5
No files matched your search
@@ -92,7 +92,7 @@ export function handleAiMarkdownLinkClick(event: AiMarkdownLinkClickEvent, openU
|
||||
return true;
|
||||
}
|
||||
|
||||
function escapeHtml(value: string): string {
|
||||
export function escapeHtml(value: string): string {
|
||||
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||||
}
|
||||
|
||||
|
||||
@@ -1030,6 +1030,8 @@ export interface AiChatMessage {
|
||||
mentions?: unknown[];
|
||||
reasoning?: string;
|
||||
kind?: "contextSummary" | "writeSqlConfirmation" | "productionWriteBlocked";
|
||||
/** Set on the assistant message whose generation failed; persisted (mirrors dbx-core `AiChatMessage.failed`). */
|
||||
failed?: boolean;
|
||||
}
|
||||
|
||||
export interface AiConversation {
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildAiConversationExport, type AiConversationExportLabels, type AiConversationExportMessage } from "../aiConversationExport";
|
||||
|
||||
const LABELS: AiConversationExportLabels = {
|
||||
analysisLabel: "AI Analysis",
|
||||
userLabel: "User",
|
||||
assistantLabel: "AI",
|
||||
failedLabel: "This reply failed to generate",
|
||||
};
|
||||
|
||||
const BASE = {
|
||||
connectionName: "prod-mysql",
|
||||
dateLabel: "2026-09-08 10:00:00",
|
||||
labels: LABELS,
|
||||
};
|
||||
|
||||
function msg(partial: Partial<AiConversationExportMessage> & Pick<AiConversationExportMessage, "role" | "content">): AiConversationExportMessage {
|
||||
return partial;
|
||||
}
|
||||
|
||||
describe("buildAiConversationExport", () => {
|
||||
it("returns null for an empty conversation (no messages or whitespace-only content)", () => {
|
||||
expect(buildAiConversationExport({ ...BASE, messages: [], format: "markdown" })).toBeNull();
|
||||
expect(buildAiConversationExport({ ...BASE, messages: [msg({ role: "assistant", content: " \n " })], format: "markdown" })).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when every message is a hidden contextSummary entry", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
messages: [msg({ role: "assistant", content: "internal summary", kind: "contextSummary" })],
|
||||
format: "markdown",
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("markdown export writes header, role headings, and message content verbatim", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
messages: [msg({ role: "user", content: "按地区统计销售额" }), msg({ role: "assistant", content: '```chart-json\n{"version":1,"type":"bar"}\n```\n华东最高。' })],
|
||||
format: "markdown",
|
||||
});
|
||||
expect(result).not.toBeNull();
|
||||
const md = result!.content;
|
||||
expect(md).toContain("# prod-mysql · AI Analysis");
|
||||
expect(md).toContain("2026-09-08 10:00:00");
|
||||
expect(md).toContain("## User");
|
||||
expect(md).toContain("## AI");
|
||||
expect(md).toContain("按地区统计销售额");
|
||||
// chart-json fence survives byte-identical (zero data loss, round-tripable)
|
||||
expect(md).toContain('```chart-json\n{"version":1,"type":"bar"}\n```');
|
||||
expect(result!.defaultFileName).toMatch(/^prod-mysql_\d{12}\.md$/);
|
||||
});
|
||||
|
||||
it("markdown export marks failed assistant replies", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
messages: [msg({ role: "assistant", content: "AI request failed\n\nboom", failed: true })],
|
||||
format: "markdown",
|
||||
});
|
||||
expect(result!.content).toContain(`**${LABELS.failedLabel}**`);
|
||||
});
|
||||
|
||||
it("html export escapes raw HTML in message content", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
messages: [msg({ role: "assistant", content: '<script>alert("x")</script>' }), msg({ role: "user", content: '<img src=x onerror="alert(1)">' }), msg({ role: "assistant", content: "<!doctype html><html><body>full doc</body></html>" })],
|
||||
format: "html",
|
||||
});
|
||||
expect(result).not.toBeNull();
|
||||
const html = result!.content;
|
||||
// No raw script/img tag may survive into the report body.
|
||||
expect(html).not.toContain("<script>");
|
||||
expect(html).not.toContain("<img");
|
||||
expect(html).not.toContain("<!doctype html><html>");
|
||||
// The escaped forms are present so the user still sees the original text.
|
||||
expect(html).toContain("<script>");
|
||||
expect(html).toContain("<img src=x onerror="alert(1)">");
|
||||
expect(html).toContain("<!doctype html>");
|
||||
});
|
||||
|
||||
it("html export escapes title and connection name in the document head", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
connectionName: "db</title><script>alert(1)</script>",
|
||||
messages: [msg({ role: "user", content: "hi" })],
|
||||
format: "html",
|
||||
});
|
||||
const html = result!.content;
|
||||
expect(html).toContain("<title>db</title><script>alert(1)</script> · AI Analysis</title>");
|
||||
expect(html).not.toContain("<script>alert(1)");
|
||||
// `<>/"` become underscores (same rule as every export file name); trailing underscores are trimmed.
|
||||
expect(result!.defaultFileName).toMatch(/^db__title__script_alert\(1\)__script_\d{12}\.html$/);
|
||||
});
|
||||
|
||||
it("html export carries the shared preview CSP and renders fences as escaped code blocks", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
messages: [msg({ role: "assistant", content: "```html\n<b>hi</b>\n```" }), msg({ role: "assistant", content: "see " })],
|
||||
format: "html",
|
||||
});
|
||||
const html = result!.content;
|
||||
expect(html).toContain('http-equiv="Content-Security-Policy"');
|
||||
expect(html).toContain("default-src 'none'");
|
||||
// The ```html fence body is escaped text inside a code block, never live markup.
|
||||
expect(html).toContain("<b>hi</b>");
|
||||
expect(html).not.toContain("<b>hi</b>");
|
||||
expect(result!.defaultFileName).toMatch(/\.html$/);
|
||||
});
|
||||
|
||||
it("html export labels roles and marks failed replies", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
messages: [msg({ role: "user", content: "q" }), msg({ role: "assistant", content: "AI request failed", failed: true })],
|
||||
format: "html",
|
||||
});
|
||||
const html = result!.content;
|
||||
expect(html).toContain('<article class="user">');
|
||||
expect(html).toContain('<article class="assistant">');
|
||||
expect(html).toContain(`<p class="role">User</p>`);
|
||||
expect(html).toContain(`<p class="failed">${LABELS.failedLabel}</p>`);
|
||||
});
|
||||
|
||||
it("sanitizes the connection name for the file name while keeping the display name", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
connectionName: 'prod:<db>"*?"',
|
||||
messages: [msg({ role: "user", content: "hi" })],
|
||||
format: "markdown",
|
||||
});
|
||||
expect(result!.content).toContain('# prod:<db>"*?" · AI Analysis');
|
||||
// `<>:"*?` become underscores; the trailing run of underscores is trimmed.
|
||||
expect(result!.defaultFileName).toMatch(/^prod__db_\d{12}\.md$/);
|
||||
});
|
||||
|
||||
it("falls back to the ai prefix when no connection name is given", () => {
|
||||
const result = buildAiConversationExport({
|
||||
...BASE,
|
||||
connectionName: undefined,
|
||||
messages: [msg({ role: "user", content: "hi" })],
|
||||
format: "markdown",
|
||||
});
|
||||
expect(result!.defaultFileName).toMatch(/^ai_\d{12}\.md$/);
|
||||
expect(result!.content).toContain("# AI · AI Analysis");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,135 @@
|
||||
import { 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<p class="failed">${escapeHtml(labels.failedLabel)}</p>` : "";
|
||||
return `<article class="${msg.role}">\n<p class="role">${roleLabel}</p>\n<div class="content">\n${body}\n</div>${failedMark}\n</article>`;
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
return [
|
||||
"<!doctype html>",
|
||||
"<html>",
|
||||
"<head>",
|
||||
'<meta charset="utf-8">',
|
||||
`<meta http-equiv="Content-Security-Policy" content="${AI_HTML_PREVIEW_CSP}">`,
|
||||
`<title>${escapedTitle}</title>`,
|
||||
"<style>",
|
||||
AI_CONVERSATION_REPORT_CSS,
|
||||
"</style>",
|
||||
"</head>",
|
||||
"<body>",
|
||||
`<header><h1>${escapedTitle}</h1><p class="meta">${escapeHtml(dateLabel)}</p></header>`,
|
||||
`<main>${articles}</main>`,
|
||||
"</body>",
|
||||
"</html>",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/** Static, light-only report styling. The CSP only allows inline `style-src`, so the sheet rides in the document. */
|
||||
const AI_CONVERSATION_REPORT_CSS = `
|
||||
body { margin: 0 auto; max-width: 52rem; padding: 2rem 1rem 4rem; color: #1c1917; background: #ffffff; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; line-height: 1.6; }
|
||||
header h1 { margin: 0 0 0.25rem; font-size: 1.25rem; }
|
||||
header .meta { margin: 0 0 1.5rem; color: #78716c; font-size: 0.85rem; }
|
||||
article { border: 1px solid #e7e5e4; border-radius: 8px; padding: 0.75rem 1rem; margin: 0 0 1rem; overflow-wrap: break-word; }
|
||||
article.user { background: #fafaf9; }
|
||||
article .role { margin: 0 0 0.25rem; color: #78716c; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
|
||||
article .content > :first-child { margin-top: 0; }
|
||||
article .content > :last-child { margin-bottom: 0; }
|
||||
article .failed { margin: 0.75rem 0 0; color: #dc2626; font-size: 0.85rem; font-weight: 600; }
|
||||
pre { background: #f5f5f4; border: 1px solid #e7e5e4; border-radius: 6px; padding: 0.75rem; overflow: auto; font-size: 0.8rem; }
|
||||
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
||||
:not(pre) > code { background: #f5f5f4; border-radius: 4px; padding: 0.1rem 0.3rem; font-size: 0.85em; }
|
||||
table { border-collapse: collapse; width: 100%; margin: 0.5rem 0; font-size: 0.85rem; }
|
||||
th, td { border: 1px solid #e7e5e4; padding: 0.35rem 0.5rem; text-align: left; }
|
||||
.ai-markdown-table-wrap { overflow-x: auto; }
|
||||
a { color: #2563eb; }
|
||||
blockquote { margin: 0.5rem 0; padding: 0.25rem 0.75rem; border-left: 3px solid #e7e5e4; color: #57534e; }
|
||||
`.trim();
|
||||
Reference in new issue
Block a user