feat: 新增报告协作页,并强化 Deep Research 运行诊断与报告兜底

支持浏览器侧素材快照撰写、拒绝报告读时修复、Mermaid PNG 导出与 Word 导出加固。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-09-03 11:16:50 +08:00
1 parent 65d6b61d5d
commit f109a95535
65 files changed
+10342 -245

No files matched your search

+29 -1
View File
@@ -28,6 +28,8 @@ interface AssistantResearchApprovalGroup extends GenericMessageGroup<"assistant:
interface AssistantTaskReportImportGroup extends GenericMessageGroup<"assistant:task-report-import"> {}
interface AssistantBusinessCardGroup extends GenericMessageGroup<"assistant:business-card"> {}
interface AssistantSubagentGroup extends GenericMessageGroup<"assistant:subagent"> {}
type MessageGroup =
@@ -41,6 +43,7 @@ type MessageGroup =
| AssistantSetupReportStructureApprovalGroup
| AssistantResearchApprovalGroup
| AssistantTaskReportImportGroup
| AssistantBusinessCardGroup
| AssistantSubagentGroup;
export function groupMessages<T>(
@@ -65,7 +68,8 @@ export function groupMessages<T>(
last.type !== "assistant:setup-agent-approval" &&
last.type !== "assistant:setup-writing-approval" &&
last.type !== "assistant:setup-report-structure-approval" &&
last.type !== "assistant:research-approval"
last.type !== "assistant:research-approval" &&
last.type !== "assistant:business-card"
) {
return last;
}
@@ -159,6 +163,17 @@ export function groupMessages<T>(
});
continue;
}
// 通用业务卡(报告协作方案组/命令确认/报告入口等):虚拟 ai 消息
// (additional_kwargs.report_collaboration_card),由宿主注入并经
// businessCardSlot 渲染;不传插槽的页面零渲染、零行为变化。
if (hasBusinessCard(message)) {
groups.push({
id: message.id,
type: "assistant:business-card",
messages: [message],
});
continue;
}
if (hasPresentFiles(message)) {
groups.push({
id: message.id,
@@ -487,6 +502,19 @@ export function hasTaskReportImportApproval(message: Message) {
);
}
/**
* 通用业务卡(报告协作等页面):宿主注入的虚拟 ai 消息
* (additional_kwargs.report_collaboration_card),经 MessageList 的
* businessCardSlot 渲染。集中在此识别,业务卡 payload 结构由各页面自定义。
*/
export function hasBusinessCard(message: Message) {
return (
message.type === "ai" &&
typeof message.additional_kwargs?.report_collaboration_card === "object" &&
message.additional_kwargs.report_collaboration_card !== null
);
}
export function extractPresentFilesFromMessage(message: Message) {
if (message.type !== "ai" || !hasPresentFiles(message)) {
return [];
@@ -10,6 +10,7 @@
import { apiFetch } from "@/core/api/fetch-client";
import { getBackendBaseURL } from "@/core/config";
import { OptType, writeYLog } from "@/core/log/y-log";
import { replaceMermaidFencesForWordExport } from "@/core/utils/mermaid";
const DOCX_MEDIA_TYPE =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
@@ -38,13 +39,15 @@ export async function markdownToDocxBlob(
const source = markdown?.trim();
if (!source) throw new Error("文档内容为空");
const prepared = await replaceMermaidFencesForWordExport(source);
const endpoint = `${getBackendBaseURL()}/api/writing/export/docx`;
const response = await apiFetch(endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
title: title?.trim() || undefined,
markdown: source,
markdown: prepared.markdown,
embedded_images: prepared.embeddedImages,
}),
});
if (!response.ok) throw await responseError(response);
@@ -1 +1,2 @@
export * from "./plugins";
export * from "./mermaid-png-download";
@@ -0,0 +1,56 @@
import { useEffect, type RefObject } from "react";
import { toast } from "sonner";
import { downloadSvgElementAsPng } from "@/core/utils/mermaid";
function findStreamdownMermaidDownloadBlock(target: EventTarget | null): HTMLElement | null {
if (!(target instanceof Element)) return null;
const button = target.closest("button");
if (!(button instanceof HTMLButtonElement)) return null;
if (button.getAttribute("title") !== "Download file") return null;
const block = button.closest('[data-streamdown="mermaid-block"]');
return block instanceof HTMLElement ? block : null;
}
function findRenderedMermaidChartSvg(block: HTMLElement): SVGSVGElement | null {
const fromChart = block.querySelector('[aria-label="Mermaid chart"] svg, [role="img"] svg');
if (fromChart instanceof SVGSVGElement && !fromChart.closest("button")) {
return fromChart;
}
for (const svg of block.querySelectorAll("svg")) {
if (svg instanceof SVGSVGElement && !svg.closest("button")) {
return svg;
}
}
return null;
}
/** Intercept Streamdown mermaid download buttons and save a PNG of the chart. */
export function useStreamdownMermaidPngDownload(rootRef: RefObject<HTMLElement | null>) {
useEffect(() => {
const onClickCapture = (event: MouseEvent) => {
const root = rootRef.current;
if (!root || !root.contains(event.target as Node)) return;
const block = findStreamdownMermaidDownloadBlock(event.target);
if (!block) return;
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
const svg = findRenderedMermaidChartSvg(block);
if (!svg) {
toast.error("流程图尚未渲染完成,请稍后再下载");
return;
}
void downloadSvgElementAsPng(svg, "flowchart.png").catch((error) => {
console.error("Failed to download mermaid image:", error);
toast.error(error instanceof Error ? error.message : "下载流程图失败");
});
};
document.addEventListener("click", onClickCapture, true);
return () => document.removeEventListener("click", onClickCapture, true);
}, [rootRef]);
}
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { sanitizeMermaidSource } from "./mermaid";
import { findMermaidFenceMatches, getMermaidImageFileName, sanitizeMermaidSource } from "./mermaid";
test("inserts a missing arrow before a bare edge label", () => {
const input = "E --> G|价值输出 V_s| H[PPO 损失]";
@@ -69,3 +69,17 @@ test("repairs the DAC-PPO architecture diagram class block", () => {
assert.doesNotMatch(output, /class,/);
assert.doesNotMatch(output, /input,module/);
});
test("turns mermaid source filenames into png downloads", () => {
assert.equal(getMermaidImageFileName("outputs/图2.mmd"), "图2.png");
assert.equal(getMermaidImageFileName("chart.mermaid", "png"), "chart.png");
assert.equal(getMermaidImageFileName("flowchart.png"), "flowchart.png");
});
test("finds mermaid fences for Word image replacement", () => {
const markdown = "前文\n```mermaid\nflowchart TB\nA-->B\n```\n后文\n```mmd\ngraph LR\nC-->D\n```";
const fences = findMermaidFenceMatches(markdown);
assert.equal(fences.length, 2);
assert.equal(fences[0].source, "flowchart TB\nA-->B");
assert.equal(fences[1].source, "graph LR\nC-->D");
});
+202 -14
View File
@@ -96,12 +96,209 @@ function sanitizeMermaidClassStatements(source: string): string {
);
}
const MERMAID_FILE_EXT = /\.(mmd|mermaid)$/i;
export function isMermaidFile(filepath: string): boolean {
return /\.mmd$/i.test(filepath);
return MERMAID_FILE_EXT.test(filepath);
}
export function getMermaidImageFileName(filepath: string, extension = "svg"): string {
return getFileName(filepath).replace(/\.mmd$/i, `.${extension}`);
export function getMermaidImageFileName(filepath: string, extension = "png"): string {
const name = getFileName(filepath) || "mermaid-diagram";
if (MERMAID_FILE_EXT.test(name)) {
return name.replace(MERMAID_FILE_EXT, `.${extension}`);
}
if (/\.(svg|png|jpe?g|webp)$/i.test(name)) {
return name.replace(/\.(svg|png|jpe?g|webp)$/i, `.${extension}`);
}
return `${name}.${extension}`;
}
function triggerBlobDownload(blob: Blob, filename: string): void {
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = objectUrl;
a.download = filename;
a.rel = "noopener";
document.body.appendChild(a);
a.click();
a.remove();
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 30_000);
}
function readPositiveNumber(value: string | null): number | null {
if (!value) return null;
const parsed = Number.parseFloat(value);
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
}
function getSvgExportSize(svg: SVGSVGElement): { width: number; height: number } {
const viewBox = svg.viewBox?.baseVal;
if (viewBox && viewBox.width > 0 && viewBox.height > 0) {
return { width: viewBox.width, height: viewBox.height };
}
const attrWidth = readPositiveNumber(svg.getAttribute("width"));
const attrHeight = readPositiveNumber(svg.getAttribute("height"));
if (attrWidth && attrHeight) {
return { width: attrWidth, height: attrHeight };
}
try {
const box = svg.getBBox();
if (box.width > 0 && box.height > 0) {
return { width: box.width, height: box.height };
}
} catch {
// Detached SVGs may throw on getBBox().
}
return {
width: Math.max(1, svg.clientWidth),
height: Math.max(1, svg.clientHeight),
};
}
function serializeSvg(svg: SVGSVGElement): string {
const clone = svg.cloneNode(true) as SVGSVGElement;
if (!clone.getAttribute("xmlns")) {
clone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
}
if (!clone.getAttribute("xmlns:xlink")) {
clone.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
}
return new XMLSerializer().serializeToString(clone);
}
export async function renderSvgElementToPngBlob(svg: SVGSVGElement): Promise<Blob> {
const { width, height } = getSvgExportSize(svg);
const scale = 2;
const markup = serializeSvg(svg);
const blob = new Blob([markup], { type: "image/svg+xml;charset=utf-8" });
const objectUrl = URL.createObjectURL(blob);
try {
const image = new Image();
await new Promise<void>((resolve, reject) => {
image.onload = () => resolve();
image.onerror = () => reject(new Error("流程图转图片失败"));
image.src = objectUrl;
});
const canvas = document.createElement("canvas");
canvas.width = Math.max(1, Math.ceil(width * scale));
canvas.height = Math.max(1, Math.ceil(height * scale));
const context = canvas.getContext("2d");
if (!context) {
throw new Error("无法创建画布");
}
context.fillStyle = "#ffffff";
context.fillRect(0, 0, canvas.width, canvas.height);
context.drawImage(image, 0, 0, canvas.width, canvas.height);
const png = await new Promise<Blob>((resolve, reject) => {
canvas.toBlob(
(result) => (result ? resolve(result) : reject(new Error("生成 PNG 失败"))),
"image/png",
);
});
return png;
} finally {
URL.revokeObjectURL(objectUrl);
}
}
export async function downloadSvgElementAsPng(
svg: SVGSVGElement,
filename: string,
): Promise<void> {
const png = await renderSvgElementToPngBlob(svg);
triggerBlobDownload(png, getMermaidImageFileName(filename, "png"));
}
function blobToBase64(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const result = String(reader.result || "");
const comma = result.indexOf(",");
resolve(comma >= 0 ? result.slice(comma + 1) : result);
};
reader.onerror = () => reject(new Error("图片编码失败"));
reader.readAsDataURL(blob);
});
}
const MERMAID_FENCE_RE = /```(?:mermaid|mmd)[^\n]*\r?\n([\s\S]*?)```/gi;
export function findMermaidFenceMatches(markdown: string): Array<{
index: number;
length: number;
source: string;
}> {
const matches: Array<{ index: number; length: number; source: string }> = [];
const matcher = new RegExp(MERMAID_FENCE_RE.source, MERMAID_FENCE_RE.flags);
let match: RegExpExecArray | null;
while ((match = matcher.exec(markdown)) !== null) {
matches.push({
index: match.index,
length: match[0].length,
source: (match[1] ?? "").trim(),
});
}
return matches;
}
export async function renderMermaidToPngBlob(source: string): Promise<Blob> {
const markup = await renderMermaidToSvg(source);
const host = document.createElement("div");
host.style.position = "fixed";
host.style.left = "-10000px";
host.style.top = "-10000px";
host.style.pointerEvents = "none";
host.style.opacity = "0";
host.innerHTML = markup;
document.body.appendChild(host);
try {
const svg = host.querySelector("svg");
if (!(svg instanceof SVGSVGElement)) {
throw new Error("无法生成流程图图片");
}
return await renderSvgElementToPngBlob(svg);
} finally {
host.remove();
}
}
export async function replaceMermaidFencesForWordExport(markdown: string): Promise<{
markdown: string;
embeddedImages: Record<string, string>;
}> {
const fences = findMermaidFenceMatches(markdown);
if (fences.length === 0) {
return { markdown, embeddedImages: {} };
}
const embeddedImages: Record<string, string> = {};
const replacements: Array<{ index: number; length: number; text: string }> = [];
for (const [offset, fence] of fences.entries()) {
if (!fence.source) continue;
try {
const png = await renderMermaidToPngBlob(fence.source);
const id = `mermaid-${offset + 1}`;
embeddedImages[id] = await blobToBase64(png);
replacements.push({
index: fence.index,
length: fence.length,
text: `\n![流程图](embedded:${id})\n`,
});
} catch (error) {
console.error("Failed to render mermaid for Word export:", error);
}
}
let next = markdown;
for (const piece of replacements.sort((left, right) => right.index - left.index)) {
next = `${next.slice(0, piece.index)}${piece.text}${next.slice(piece.index + piece.length)}`;
}
return { markdown: next, embeddedImages };
}
async function getMermaidModule() {
@@ -152,15 +349,6 @@ export async function openMermaidSvgInNewTab(source: string): Promise<void> {
}
export async function downloadMermaidSvgFile(source: string, filepath: string): Promise<void> {
const svg = await renderMermaidToSvg(source);
const blob = new Blob([svg], { type: "image/svg+xml;charset=utf-8" });
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = objectUrl;
a.download = getMermaidImageFileName(filepath);
a.rel = "noopener";
document.body.appendChild(a);
a.click();
a.remove();
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 30_000);
const png = await renderMermaidToPngBlob(source);
triggerBlobDownload(png, getMermaidImageFileName(filepath, "png"));
}