diff --git a/ui/apps/chat/demo/fixtures/traces.ts b/ui/apps/chat/demo/fixtures/traces.ts index cc7ed8979..dc766458e 100644 --- a/ui/apps/chat/demo/fixtures/traces.ts +++ b/ui/apps/chat/demo/fixtures/traces.ts @@ -301,9 +301,39 @@ function agenticDebugging(): Trace { return trace; } +/** A reply with a diagram, for diagram preview demos. */ +function diagrams(): Trace { + const trace = new Trace('How a request flows through llama-server'); + + trace.user('Can you draw how a chat request flows through llama-server?'); + trace.assistant( + [ + 'Here is the path a request takes:', + '', + '```mermaid', + 'flowchart LR', + ' U[Browser] -->|POST /v1/chat/completions| S(llama-server)', + ' S --> Q{Slot free?}', + ' Q -->|yes| P[Prompt processing]', + ' Q -->|no| W[Queue]', + ' W --> P', + ' P --> G[Sample token]', + ' G -->|more| G', + ' G -->|SSE tokens| U', + ' click S href "https://github.com/ggml-org/llama.cpp" "llama.cpp on GitHub" _blank', + '```', + '', + 'Requests wait in the queue until a slot is free, then tokens stream back as they are sampled.' + ].join('\n') + ); + + return trace; +} + /** Built-in traces, by name. */ export const DEMO_TRACES: Record Trace> = { 'agentic-debugging': agenticDebugging, + diagrams, 'long-chat': longChat }; diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts index ba607bfbc..ec3e35dd9 100644 --- a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts +++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts @@ -43,6 +43,13 @@ function diagramWidth(node: HTMLElement): number | undefined { return width > 0 ? width : undefined; } +// Pele wraps a node with a `click ... href` directive in a link. Diagrams come from model +// output, so they stay inert, as they were under Mermaid's strict security level: each link +// gives way to its children. The preview dialog and the downloaded file copy this svg. +function unwrapLinks(root: HTMLElement) { + for (const link of root.querySelectorAll('a')) link.replaceWith(...link.childNodes); +} + /** * Renders the mermaid diagrams in `container` that haven't been rendered yet, with Pele. * Marks nodes immediately to prevent duplicate renders during streaming. Colors come @@ -77,6 +84,7 @@ export async function renderMermaidDiagrams(container: HTMLElement) { }); node.innerHTML = svg; + unwrapLinks(node); } catch (error) { // the message quotes the diagram source, so it goes in as text const message = document.createElement('div'); diff --git a/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte b/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte index d981f67d1..934802581 100644 --- a/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte +++ b/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte @@ -1,7 +1,16 @@ + + +
-
zoomBy(DIAGRAM_ZOOM_FACTOR)} + onZoomOut={() => zoomBy(1 / DIAGRAM_ZOOM_FACTOR)} + scale={view.scale} {svgHtml} />
diff --git a/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte b/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte index 1899db389..18f96c529 100644 --- a/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte +++ b/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte @@ -31,7 +31,7 @@