Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,243 changes: 60 additions & 1,183 deletions ui/apps/chat/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ui/apps/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
"http-server": "14.1.1",
"@ggml-org/llama-brand": "https://codeload.github.com/ggml-org/llama-brand/tar.gz/f05540643c1ca2a18d732ed6491c839686da9a3a",
"mdsvex": "0.12.7",
"mermaid": "11.17.2",
"mode-watcher": "1.1.0",
"pdfjs-dist": "5.4.54",
"pele": "1.13.0",
"playwright": "1.56.1",
"prettier": "3.8.3",
"prettier-plugin-svelte": "4.1.0",
Expand Down
18 changes: 16 additions & 2 deletions ui/apps/chat/src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,22 @@
}
}

.mermaidTooltip {
display: none !important;
/*
* Mermaid diagrams are drawn by Pele, which reads its colors from these. They sit on :root
* so the svg also inherits them inside the preview dialog's shadow root, and follow the theme.
*/
:root {
--pele-bg: var(--code-background);
--pele-fg: var(--code-foreground);
--pele-muted: var(--muted-foreground);
--pele-line: var(--muted-foreground);
--pele-surface: var(--background);
--pele-surface-alt: var(--muted);
--pele-border: var(--border);
--pele-accent: var(--primary);
--pele-critical: var(--destructive);
--pele-error: var(--destructive);
--pele-radius: calc(var(--radius) - 4px);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@
if ((hasRenderedBlocks || hasUnstableBlock) && containerRef) {
setupCodeBlockActions();
setupImageErrorHandlers();
renderMermaidDiagrams(containerRef, mode.current === ColorMode.DARK);
renderMermaidDiagrams(containerRef);
renderSvgDiagrams(containerRef);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,24 @@ div.markdown-user-content :global(.table-wrapper) {

/* Hide mermaid code text until rendered - prevents flash */
.markdown-content :global(pre.mermaid:not([data-mermaid-rendered])),
.markdown-content :global(pre.mermaid[data-mermaid-rendered]:not(:has(svg))) {
.markdown-content :global(pre.mermaid[data-mermaid-rendered]:not(:has(svg, .mermaid-error))) {
display: none;
}

/* A diagram Pele can't draw shows why, with the line it stopped at */
.markdown-content :global(.mermaid-error) {
max-width: 100%;
padding: 1rem;
color: var(--destructive);
font-family: var(--font-mono);
font-size: 0.8125rem;
/* the caret line under the source must line up character by character */
font-variant-ligatures: none;
text-align: left;
white-space: pre-wrap;
cursor: default;
}

/* Hide svg source until rendered - prevents flash. A rendered-but-unsanitized
block (oversized source) keeps its raw text visible as a safe fallback. */
.markdown-content :global(pre.svg-block:not([data-svg-rendered])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import {
DIAGRAM_VIEW_RENDERED,
DIAGRAM_VIEW_SOURCE,
MERMAID_BLOCK_CLASS,
MERMAID_ERROR_CLASS,
MERMAID_ID_ATTR,
MERMAID_RENDERED_ATTR,
MERMAID_SCROLL_CONTAINER_CLASS,
MERMAID_SVG_PADDING_X_REM,
MERMAID_SYNTAX_ATTR,
MERMAID_WRAPPER_CLASS,
PELE_SVG_CLASS,
PELE_THEME_VARIABLES,
SVG,
TOGGLE_SOURCE_BTN_CLASS
} from '#lib/constants/index.ts';
Expand All @@ -19,11 +25,30 @@ import { mountSvgShadow } from '#lib/utils/svg-shadow.ts';

export type DiagramAction = { copy: string } | { previewSvg: string };

// The width a diagram can take in its block: the scroll container's content box, less the
// svg's own horizontal padding. Undefined when the block isn't laid out (detached, hidden).
function diagramWidth(node: HTMLElement): number | undefined {
const scroll = node.closest<HTMLElement>(`.${MERMAID_SCROLL_CONTAINER_CLASS}`);

if (!scroll?.clientWidth) return undefined;

const box = getComputedStyle(scroll);
const rem = parseFloat(getComputedStyle(document.documentElement).fontSize);
const width =
scroll.clientWidth -
parseFloat(box.paddingLeft) -
parseFloat(box.paddingRight) -
MERMAID_SVG_PADDING_X_REM * rem;

return width > 0 ? width : undefined;
}

/**
* Renders the mermaid diagrams in `container` that haven't been rendered yet.
* Marks nodes immediately to prevent duplicate renders during streaming.
* 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
* from the `--pele-*` variables in app.css, so a diagram follows the theme without a redraw.
*/
export async function renderMermaidDiagrams(container: HTMLElement, isDark: boolean) {
export async function renderMermaidDiagrams(container: HTMLElement) {
const nodes = container.querySelectorAll<HTMLElement>(
`pre.${MERMAID_BLOCK_CLASS}:not([${MERMAID_RENDERED_ATTR}])`
);
Expand All @@ -34,32 +59,56 @@ export async function renderMermaidDiagrams(container: HTMLElement, isDark: bool
// This avoids needing a guard that would block node discovery.
nodes.forEach((node) => node.setAttribute(MERMAID_RENDERED_ATTR, BooleanString.TRUE));

// lazy load the mermaid dependecy only when needed to reduce bundle size.
const { default: mermaid } = await import('mermaid');

mermaid.initialize({
flowchart: {
htmlLabels: true,
useMaxWidth: false
},
gantt: {
useMaxWidth: false
},
securityLevel: 'strict',
sequence: {
useMaxWidth: false
},
startOnLoad: false,
theme: isDark ? 'dark' : 'default'
});
const { render } = await import('pele');

try {
await mermaid.run({ nodes: Array.from(nodes) as unknown as NodeListOf<HTMLElement> });
} catch (error) {
console.error('Failed to render mermaid diagram:', error);
// Pele measures labels with the font it is given, so wait until the page font is in
await document.fonts?.ready;

for (const node of nodes) {
const source = node.getAttribute(MERMAID_SYNTAX_ATTR) ?? node.textContent ?? '';
const id = node.getAttribute(MERMAID_ID_ATTR);

try {
const { svg } = render(source, {
fontFamily: getComputedStyle(node).fontFamily,
idPrefix: id ? id.replace(/[^\w-]/g, '_') : undefined,
// narrow blocks (phones) may turn a left-to-right diagram top-to-bottom
maxWidth: diagramWidth(node)
});

node.innerHTML = svg;
} catch (error) {
// the message quotes the diagram source, so it goes in as text
const message = document.createElement('div');

message.className = MERMAID_ERROR_CLASS;
message.textContent = error instanceof Error ? error.message : String(error);
node.replaceChildren(message);
}
}
}

/**
* A Pele diagram takes its colors from the page's `--pele-*` variables. Writes their current
* values into the svg, so a downloaded file keeps the colors it had on screen. Any other svg
* comes back unchanged.
*/
export function withThemeColors(svgHtml: string): string {
const svg = new DOMParser().parseFromString(svgHtml, 'image/svg+xml').documentElement;

if (!(svg instanceof SVGElement) || !svg.classList.contains(PELE_SVG_CLASS)) return svgHtml;

const theme = getComputedStyle(document.documentElement);

for (const name of PELE_THEME_VARIABLES) {
const value = theme.getPropertyValue(name).trim();

if (value) svg.style.setProperty(name, value);
}

return new XMLSerializer().serializeToString(svg);
}

/**
* Renders the svg diagrams in `container` that haven't been rendered yet.
* Sanitizes the source before injecting and marks each node so it renders once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import {
import { getMarkdownProcessor } from '#lib/components/app/content/MarkdownContent/markdown-processor.ts';
import { getCodeInfoFromTarget } from '#lib/components/app/content/MarkdownContent/markdown-utils.ts';
import { MERMAID_BLOCK_CLASS } from '#lib/constants/index.ts';
import { ColorMode } from '#lib/enums/index.ts';
import {
copyCodeToClipboard,
copyToClipboard,
preprocessLaTeX,
splitGluedClosingCodeFences
} from '#lib/utils/index.ts';
import { mode } from 'mode-watcher';

export interface MarkdownBlockWidgetOptions {
onPreviewCode?: (code: string, language: string) => void;
Expand Down Expand Up @@ -311,9 +309,7 @@ export class MarkdownBlockWidget extends WidgetType {

// mermaid draws asynchronously and changes the block's height when done
if (dom.querySelector(`pre.${MERMAID_BLOCK_CLASS}`)) {
void renderMermaidDiagrams(dom, mode.current === ColorMode.DARK).then(() =>
this.measure(dom, view, key)
);
void renderMermaidDiagrams(dom).then(() => this.measure(dom, view, key));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import RotateCcwIcon from '@lucide/svelte/icons/rotate-ccw';
import ZoomInIcon from '@lucide/svelte/icons/zoom-in';
import ZoomOutIcon from '@lucide/svelte/icons/zoom-out';
import { withThemeColors } from '#lib/components/app/content/MarkdownContent/markdown-diagrams.ts';
import { ICON_CLASS_DEFAULT } from '#lib/constants/index.ts';

interface Props {
Expand All @@ -18,7 +19,7 @@
function downloadSvg() {
if (!svgHtml) return;

const blob = new Blob([svgHtml], { type: 'image/svg+xml' });
const blob = new Blob([withThemeColors(svgHtml)], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');

Expand Down
19 changes: 19 additions & 0 deletions ui/apps/chat/src/lib/constants/mermaid-blocks.constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
export const MERMAID_WRAPPER_CLASS = 'mermaid-block-wrapper';
export const MERMAID_SCROLL_CONTAINER_CLASS = 'mermaid-scroll-container';
export const MERMAID_BLOCK_CLASS = 'mermaid';
export const MERMAID_ERROR_CLASS = 'mermaid-error';
// left plus right padding of a diagram's svg, see `pre.mermaid svg` in markdown-content.css
export const MERMAID_SVG_PADDING_X_REM = 2;

// class Pele puts on the root of every diagram it draws
export const PELE_SVG_CLASS = 'pele';
// the Pele colors app.css maps to the theme, written into a downloaded diagram
export const PELE_THEME_VARIABLES = [
'--pele-bg',
'--pele-fg',
'--pele-muted',
'--pele-line',
'--pele-surface',
'--pele-surface-alt',
'--pele-border',
'--pele-accent',
'--pele-critical',
'--pele-radius'
] as const;

export const MERMAID_LANGUAGE = 'mermaid';

Expand Down
22 changes: 22 additions & 0 deletions ui/apps/chat/tests/stories/content/MarkdownContent.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script lang="ts" module>
import { AI_TUTORIAL_MD } from '../fixtures/ai-tutorial.js';
import { MERMAID_DIAGRAMS_MD } from '../fixtures/mermaid-diagrams.js';
import { defineMeta } from '@storybook/addon-svelte-csf';
import { MarkdownContent } from '#lib/components/app/index.ts';
import { expect, waitFor } from 'storybook/test';

const { Story } = defineMeta({
component: MarkdownContent,
Expand All @@ -16,3 +18,23 @@
args={{ class: 'max-w-[56rem] w-[calc(100vw-2rem)]', content: AI_TUTORIAL_MD }}
name="Default"
/>

<Story
args={{ class: 'max-w-[56rem] w-[calc(100vw-2rem)]', content: MERMAID_DIAGRAMS_MD }}
name="Diagrams"
play={async ({ canvasElement }) => {
// MarkdownContent renders in batches, so the broken last block can trail the others
await waitFor(
() => {
expect(canvasElement.querySelectorAll('pre.mermaid svg.pele')).toHaveLength(4);
expect(canvasElement.querySelector('pre.mermaid .mermaid-error')).not.toBeNull();
},
{ timeout: 5000 }
);

const error = canvasElement.querySelector('pre.mermaid .mermaid-error');

await expect(error?.textContent).toContain('line 2');
await expect(error?.closest('pre')).toBeVisible();
}}
/>
54 changes: 54 additions & 0 deletions ui/apps/chat/tests/stories/fixtures/mermaid-diagrams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Mermaid blocks of several diagram types, and one that can't be drawn
const fence = '```';

export const MERMAID_DIAGRAMS_MD = String.raw`
How a request reaches the model:

${fence}mermaid
flowchart LR
U[Browser] -->|POST /v1/chat/completions| S(llama-server)
S --> Q{Slot free?}
Q -->|yes| D[Decode]
Q -->|no| W[Queue]
W --> D
D -->|SSE tokens| U
${fence}

The same exchange over time:

${fence}mermaid
sequenceDiagram
participant C as Llama Chat
participant S as llama-server
C->>S: POST /v1/chat/completions
S-->>C: data: {"choices":[...]}
S-->>C: data: [DONE]
Note over C,S: the stream ends with DONE
${fence}

A slot's life:

${fence}mermaid
stateDiagram-v2
[*] --> Idle
Idle --> Processing: prompt
Processing --> Generating: prompt evaluated
Generating --> Idle: stop or limit
${fence}

Where the time goes:

${fence}mermaid
pie title Time per request
"Prompt processing" : 35
"Token generation" : 60
"Sampling" : 5
${fence}

A diagram with a mistake in it:

${fence}mermaid
flowchart LR
A -->
${fence}
`;