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") &&