From f57018aa7fa45b5ac4001dcfae223562fc5a61b3 Mon Sep 17 00:00:00 2001 From: chengyongru <61816729+chengyongru@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:16:20 +0800 Subject: [PATCH] feat(webui): render Mermaid diagrams with Pele (#6302) * feat(webui): render Mermaid diagrams with Pele * fix(webui): reserve diagram replacement capacity * fix(webui): prebundle lazy diagram worker dependencies * fix(webui): account diagram capacity by encoded size * fix(webui): use shared hover hints for diagram actions * docs(webui): focus diagram guide on usage --- docs/webui.md | 4 + scripts/check_client_import_boundaries.mjs | 8 + .../check_client_import_boundaries.test.mjs | 4 + webui/browser/diagram-fixture.tsx | 37 + webui/browser/diagrams.spec.ts | 196 +++ webui/browser/fixture.tsx | 5 +- webui/bun.lock | 242 +--- webui/package-lock.json | 1143 +---------------- webui/package.json | 6 +- webui/public/licenses/diagram-parsers.txt | 60 + webui/public/licenses/pele.txt | 21 + webui/src/components/MarkdownTextRenderer.tsx | 40 +- webui/src/components/ZoomableImage.tsx | 127 +- .../markdown-diagrams/MarkdownCodeBlock.tsx | 27 + .../markdown-diagrams/MermaidBlock.tsx | 148 +++ .../components/markdown-diagrams/context.tsx | 14 + .../markdown-diagrams/pele.worker.ts | 42 + .../components/markdown-diagrams/render.ts | 29 + .../components/markdown-diagrams/runtime.ts | 311 +++++ .../components/markdown-diagrams/svg-image.ts | 342 +++++ .../src/components/markdown-diagrams/types.ts | 27 + .../src/components/thread/ThreadViewport.tsx | 3 + webui/src/components/ui/image-viewport.tsx | 155 +++ webui/src/dev/gallery/foundations.tsx | 10 + webui/src/i18n/locales/en/common.json | 19 + webui/src/i18n/locales/es/common.json | 19 + webui/src/i18n/locales/fr/common.json | 19 + webui/src/i18n/locales/id/common.json | 19 + webui/src/i18n/locales/ja/common.json | 19 + webui/src/i18n/locales/ko/common.json | 19 + webui/src/i18n/locales/pt-BR/common.json | 19 + webui/src/i18n/locales/vi/common.json | 19 + webui/src/i18n/locales/zh-CN/common.json | 19 + webui/src/i18n/locales/zh-TW/common.json | 19 + webui/src/tests/diagram-image.test.ts | 183 +++ webui/src/tests/diagram-runtime.test.ts | 213 +++ webui/src/tests/image-lightbox.test.tsx | 103 +- webui/src/tests/mermaid-block.test.tsx | 78 ++ webui/src/tests/vite-config.test.ts | 12 + webui/vite.config.ts | 13 +- 40 files changed, 2335 insertions(+), 1458 deletions(-) create mode 100644 webui/browser/diagram-fixture.tsx create mode 100644 webui/browser/diagrams.spec.ts create mode 100644 webui/public/licenses/diagram-parsers.txt create mode 100644 webui/public/licenses/pele.txt create mode 100644 webui/src/components/markdown-diagrams/MarkdownCodeBlock.tsx create mode 100644 webui/src/components/markdown-diagrams/MermaidBlock.tsx create mode 100644 webui/src/components/markdown-diagrams/context.tsx create mode 100644 webui/src/components/markdown-diagrams/pele.worker.ts create mode 100644 webui/src/components/markdown-diagrams/render.ts create mode 100644 webui/src/components/markdown-diagrams/runtime.ts create mode 100644 webui/src/components/markdown-diagrams/svg-image.ts create mode 100644 webui/src/components/markdown-diagrams/types.ts create mode 100644 webui/src/components/ui/image-viewport.tsx create mode 100644 webui/src/tests/diagram-image.test.ts create mode 100644 webui/src/tests/diagram-runtime.test.ts create mode 100644 webui/src/tests/mermaid-block.test.tsx diff --git a/docs/webui.md b/docs/webui.md index d39eb7260..c624dc36d 100644 --- a/docs/webui.md +++ b/docs/webui.md @@ -133,6 +133,10 @@ PNG, JPEG, GIF and WebP files up to 8 MiB can be previewed as images. Hover or f A plain image file link does not automatically insert a large image into the reply. Explicit Markdown images appear inline; image attachments use a compact gallery, showing at most four thumbnails before a count of the remaining images. An image already displayed inline is not duplicated in the attachment gallery. +Mermaid code fences show their source while a reply is being generated, then render a diagram when the reply finishes. Choose **Source** or **Diagram** to switch views, or **Copy source** to copy the original code. **Enlarge diagram** opens a viewer with zoom buttons, pinch-to-zoom and drag-to-pan. With the viewport focused, use **+**, **−** and **0** to zoom or reset, and arrow keys to pan; **Escape** closes the viewer. + +If a diagram cannot be rendered, its source stays available with an error message. Links, external images and executable HTML inside diagrams are disabled. + Select a file reference to preview it. Right-click the reference or its preview tab to copy its absolute or project-relative path. Keyboard users can focus a reference and press **Shift+F10**. Paths are resolved on the gateway machine, which may not be the computer running your browser. Files outside the project do not have a project-relative path. If a reference cannot be resolved, you can still copy the original reference; the menu does not invent an absolute path. Finder, external editor and terminal launching are not available from this browser menu. File previews follow the active topic's access mode. Restricted workspace access previews only files under the selected workspace. Full Access can preview files outside the workspace when that access mode is allowed by the gateway. diff --git a/scripts/check_client_import_boundaries.mjs b/scripts/check_client_import_boundaries.mjs index 2f004d7a7..1abf7a845 100644 --- a/scripts/check_client_import_boundaries.mjs +++ b/scripts/check_client_import_boundaries.mjs @@ -120,6 +120,14 @@ export function findViolations(compiler, root, file, source, options, project) { rules.push("tui-client"); } } + if (within(relative, "webui/src") && within(specifier, "pele") + && relative !== "webui/src/components/markdown-diagrams/pele.worker.ts") { + rules.push("diagram-worker-engine"); + } + if (within(relative, "webui/src") && ["saxes", "css-tree"].some((name) => within(specifier, name)) + && relative !== "webui/src/components/markdown-diagrams/svg-image.ts") { + rules.push("diagram-worker-output"); + } if (within(relative, "webui/src/lib") && WEBUI_VIEWS.some((prefix) => targetPath !== null && within(targetPath, prefix))) { rules.push("webui-lib"); } diff --git a/scripts/check_client_import_boundaries.test.mjs b/scripts/check_client_import_boundaries.test.mjs index 0498e7bd0..81d2f05c6 100644 --- a/scripts/check_client_import_boundaries.test.mjs +++ b/scripts/check_client_import_boundaries.test.mjs @@ -82,6 +82,8 @@ test.each([ ["webui/src/lib/model.ts", 'import "../i18n";', "webui-lib-bootstrap"], ["webui/src/lib/model.ts", 'import "../i18n/index.ts";', "webui-lib-bootstrap"], ["webui/src/lib/format.ts", 'import i18n from "i18next";', "pure-format-utils"], + ["webui/src/components/markdown-diagrams/runtime.ts", 'import { render } from "pele/lazy";', "diagram-worker-engine"], + ["webui/src/components/markdown-diagrams/MermaidBlock.tsx", 'import { SaxesParser } from "saxes";', "diagram-worker-output"], ])("rejects the owning boundary in %s", (relative, source, rule) => { expect(inspect(relative, source)).toHaveLength(1); expect(inspect(relative, source)[0]).toContain(`[${rule}] must not import`); @@ -92,6 +94,8 @@ test.each([ ["tui/src/client/api.ts", 'import "../../../packages/client-events/notifications";'], ["webui/src/lib/model.ts", 'import "../../../packages/client-events/notifications";'], ["webui/src/channel-plugins/types.ts", 'import type { ComponentType } from "react";'], + ["webui/src/components/markdown-diagrams/pele.worker.ts", 'import { render } from "pele/lazy";'], + ["webui/src/components/markdown-diagrams/svg-image.ts", 'import { parse } from "css-tree/parser";'], ])("permits the shared contract or contribution interface in %s", (relative, source) => { expect(inspect(relative, source)).toEqual([]); }); diff --git a/webui/browser/diagram-fixture.tsx b/webui/browser/diagram-fixture.tsx new file mode 100644 index 000000000..a91526dfe --- /dev/null +++ b/webui/browser/diagram-fixture.tsx @@ -0,0 +1,37 @@ +import { useState } from "react"; + +import { MessageBubble } from "@/components/MessageBubble"; +import { DiagramScope } from "@/components/markdown-diagrams/context"; +import { ThemeProvider, useTheme } from "@/hooks/useTheme"; + +export const diagramExamples = { + flowchart: 'flowchart LR\nA[开始:接收用户请求] --> B{检查权限}\nB -->|允许| C[执行任务 ✅]\nB -->|拒绝| D[返回说明]', + sequence: 'sequenceDiagram\nparticipant 用户\nparticipant 助手\n用户->>助手: 分析这个问题\n助手-->>用户: 返回结果 ✅', + state: 'stateDiagram-v2\n[*] --> 等待\n等待 --> 处理中: 收到请求\n处理中 --> 完成: 成功\n完成 --> [*]', + invalid: 'flowchart LR\nA[Unclosed', +}; + +export function DiagramFixture({ parameters }: { parameters: URLSearchParams }) { + const { theme, toggle } = useTheme(); + const [streaming, setStreaming] = useState(parameters.has("stream")); + const [identity, setIdentity] = useState("first"); + const [source, setSource] = useState(diagramExamples[parameters.get("type") as keyof typeof diagramExamples] ?? diagramExamples.flowchart); + const examples = parameters.has("many") + ? Array.from({ length: 12 }, (_, index) => `flowchart LR\nA[输入 ${index}] --> B[处理 ${index}] --> C[输出 ${index}]`) + : parameters.has("multiple") ? Object.values(diagramExamples).slice(0, 3) : [source]; + const replies = parameters.has("many") + ? [examples.map((example) => `\`\`\`mermaid\n${example}\n\`\`\``).join("\n\n")] + : examples.map((example, index) => `A ${index + 1} diagram.\n\n\`\`\`mermaid\n${example}\n\`\`\`\n\nThe source stays available.`); + return
+
+ + + +
+ {parameters.has("edit") &&