Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: stop list items flashing as horizontal rules (#8378)

oobabooga735ef68437df
diff --git a/studio/frontend/package-lock.json b/studio/frontend/package-lock.json
index bb8d6aa7855..13b33128fd7 100644
--- a/studio/frontend/package-lock.json
+++ b/studio/frontend/package-lock.json
@@ -53,6 +53,11 @@
"katex": "^0.16.28",
"lucide-react": "^1.7.0",
"mammoth": "^1.11.0",
+ "mdast-util-from-markdown": "2.0.3",
+ "mdast-util-gfm": "3.1.0",
+ "mdast-util-math": "3.0.0",
+ "micromark-extension-gfm": "3.0.0",
+ "micromark-extension-math": "3.1.0",
"motion": "^12.34.0",
"node-forge": "^1.4.0",
"radix-ui": "^1.4.3",
@@ -63,6 +68,7 @@
"react-qr-code": "2.2.0",
"react-resizable-panels": "^4.6.4",
"recharts": "3.7.0",
+ "remend": "1.3.0",
"shadcn": "^4.2.0",
"sonner": "^2.0.7",
"streamdown": "2.5.0",
@@ -1708,7 +1714,6 @@
"os": [
"android"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1729,7 +1734,6 @@
"os": [
"darwin"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1750,7 +1754,6 @@
"os": [
"darwin"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1771,7 +1774,6 @@
"os": [
"linux"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1792,7 +1794,6 @@
"os": [
"linux"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1813,7 +1814,6 @@
"os": [
"linux"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1834,7 +1834,6 @@
"os": [
"linux"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1855,7 +1854,6 @@
"os": [
"linux"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1876,7 +1874,6 @@
"os": [
"linux"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1897,7 +1894,6 @@
"os": [
"win32"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
@@ -1918,7 +1914,6 @@
"os": [
"win32"
],
- "peer": true,
"engines": {
"node": ">= 10"
},
diff --git a/studio/frontend/package.json b/studio/frontend/package.json
index 602ba6aee29..c6a744043e2 100644
--- a/studio/frontend/package.json
+++ b/studio/frontend/package.json
@@ -66,6 +66,11 @@
"katex": "^0.16.28",
"lucide-react": "^1.7.0",
"mammoth": "^1.11.0",
+ "mdast-util-from-markdown": "2.0.3",
+ "mdast-util-gfm": "3.1.0",
+ "mdast-util-math": "3.0.0",
+ "micromark-extension-gfm": "3.0.0",
+ "micromark-extension-math": "3.1.0",
"motion": "^12.34.0",
"node-forge": "^1.4.0",
"radix-ui": "^1.4.3",
@@ -76,6 +81,7 @@
"react-qr-code": "2.2.0",
"react-resizable-panels": "^4.6.4",
"recharts": "3.7.0",
+ "remend": "1.3.0",
"shadcn": "^4.2.0",
"sonner": "^2.0.7",
"streamdown": "2.5.0",
diff --git a/studio/frontend/src/components/assistant-ui/markdown-text.tsx b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
index 2b7b9711632..2fa6f9e373c 100644
--- a/studio/frontend/src/components/assistant-ui/markdown-text.tsx
+++ b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
@@ -35,6 +35,7 @@ import { createCodePlugin } from "./code-plugin";
import "katex/dist/katex.min.css";
import { AudioPlayer } from "./audio-player";
import { unslothDarkTheme, unslothLightTheme } from "./code-themes";
+import { stabilizeStreamingMarkdown } from "./streaming-markdown";
const math = createMathPlugin({ singleDollarTextMath: true });
const code = createCodePlugin({
@@ -399,10 +400,11 @@ const MarkdownTextImpl = () => {
// Parts are keyed by index, so switching conversations hands this instance a different
// message, and Streamdown only extends its parsed blocks: key it per message instead.
const messageId = useAuiState(({ message }) => message.id);
- const displayText = useRafCoalescedText(text, status.type === "running");
+ const isStreaming = status.type === "running";
+ const displayText = useRafCoalescedText(text, isStreaming);
const processedText = useMemo(
- () => preprocessLaTeX(displayText),
- [displayText],
+ () => stabilizeStreamingMarkdown(preprocessLaTeX(displayText), isStreaming),
+ [displayText, isStreaming],
);
const audioMatch = displayText.match(AUDIO_PLAYER_RE);
@@ -415,7 +417,7 @@ const MarkdownTextImpl = () => {
<Streamdown
key={messageId}
mode="streaming"
- isAnimating={status.type === "running"}
+ isAnimating={isStreaming}
animated={STREAMDOWN_IMMEDIATE_UPDATES}
plugins={{ code, math, mermaid }}
components={STREAMDOWN_COMPONENTS}
diff --git a/studio/frontend/src/components/assistant-ui/streaming-markdown.ts b/studio/frontend/src/components/assistant-ui/streaming-markdown.ts
new file mode 100644
index 00000000000..a657f6aa7b7
--- /dev/null
+++ b/studio/frontend/src/components/assistant-ui/streaming-markdown.ts
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { fromMarkdown } from "mdast-util-from-markdown";
+import { gfmFromMarkdown } from "mdast-util-gfm";
+import { mathFromMarkdown } from "mdast-util-math";
+import { gfm } from "micromark-extension-gfm";
+import { math } from "micromark-extension-math";
+import remend from "remend";
+import { parseMarkdownIntoBlocks } from "streamdown";
+
+// Cheap gate: a bullet marker then only thematic-break punctuation. Not
+// asterisk specific, `- --verbose` and `* ___under___` flash too.
+const AMBIGUOUS_BREAK_ITEM_RE = /^[ \t]*([*+-])[ \t]+[*\-_][*\-_ \t]*$/;
+const BLOCKQUOTE_PREFIX_RE = /^(?:[ \t]*>[ \t]?)+/;
+// Real prefixes are short, and parsing a long run is quadratic (100k dashes
+// cost 8s), so a runaway line is left alone.
+const MAX_AMBIGUOUS_LINE = 120;
+
+type MarkdownNode = {
+ readonly type: string;
+ readonly position?: {
+ readonly start: { readonly offset?: number };
+ readonly end: { readonly offset?: number };
+ };
+ readonly children?: readonly MarkdownNode[];
+};
+
+// Streamdown parses with GFM and the math plugin; plain CommonMark disagrees
+// on footnotes and on dollar signs.
+function parse(text: string): MarkdownNode {
+ return fromMarkdown(text, {
+ extensions: [gfm(), math({ singleDollarTextMath: true })],
+ mdastExtensions: [gfmFromMarkdown(), mathFromMarkdown()],
+ }) as MarkdownNode;
+}
+
+// Index of the trailing line's bullet marker, or -1 if it is not ambiguous.
+function ambiguousMarkerIndex(text: string): number {
+ const lineStart =
+ Math.max(text.lastIndexOf("\n"), text.lastIndexOf("\r")) + 1;
+ const line = text.slice(lineStart);
+ if (line.length > MAX_AMBIGUOUS_LINE) {
+ return -1;
+ }
+ const blockquotePrefix = line.match(BLOCKQUOTE_PREFIX_RE)?.[0] ?? "";
+ const content = line.slice(blockquotePrefix.length);
+ const marker = content.match(AMBIGUOUS_BREAK_ITEM_RE)?.[1];
+ return marker === undefined
+ ? -1
+ : lineStart + blockquotePrefix.length + content.indexOf(marker);
+}
+
+// Is a rule on screen now? After an unclosed construct the repair alone turns
+// this frame into a list, so unrepaired text is the wrong thing to test.
+function rendersTrailingThematicBreak(block: string): boolean {
+ let node = parse(block);
+ while (node.children?.length) {
+ node = node.children[node.children.length - 1];
+ }
+ return node.type === "thematicBreak";
+}
+
+// Will this marker hold text? The offset match separates a nested item from
+// `* * *`, whose completed form is nested lists with no paragraph of its own.
+function completesAsTrailingParagraphListItem(
+ block: string,
+ markerIndex: number,
+): boolean {
+ const completedText = `${block}x`;
+ const pending: MarkdownNode[] = [parse(completedText)];
+ while (pending.length > 0) {
+ const node = pending.pop();
+ if (!node) {
+ continue;
+ }
+ if (
+ node.type === "listItem" &&
+ node.position?.start.offset === markerIndex &&
+ node.position.end.offset === completedText.length &&
+ node.children?.some(
+ (child) =>
+ child.type === "paragraph" &&
+ child.position?.end.offset === completedText.length,
+ )
+ ) {
+ return true;
+ }
+ if (node.children) {
+ pending.push(...node.children);
+ }
+ }
+ return false;
+}
+
+export function stabilizeStreamingMarkdown(
+ text: string,
+ isStreaming: boolean,
+): string {
+ if (!isStreaming || ambiguousMarkerIndex(text) < 0) {
+ return text;
+ }
+
+ // Run what Streamdown runs: repair, split, then read only the trailing block
+ // so the cost does not grow with the response.
+ const block = parseMarkdownIntoBlocks(remend(text)).at(-1);
+ const markerIndex = block === undefined ? -1 : ambiguousMarkerIndex(block);
+ if (
+ block === undefined ||
+ markerIndex < 0 ||
+ !rendersTrailingThematicBreak(block) ||
+ !completesAsTrailingParagraphListItem(block, markerIndex)
+ ) {
+ return text;
+ }
+
+ // Both a valid thematic break and a list-item prefix: hold the line back
+ // until content arrives instead of rendering the wrong block.
+ return text.slice(
+ 0,
+ Math.max(text.lastIndexOf("\n"), text.lastIndexOf("\r")) + 1,
+ );
+}
diff --git a/studio/frontend/tests/streaming-horizontal-rule.test.ts b/studio/frontend/tests/streaming-horizontal-rule.test.ts
new file mode 100644
index 00000000000..5bf40c01afe
--- /dev/null
+++ b/studio/frontend/tests/streaming-horizontal-rule.test.ts
@@ -0,0 +1,295 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { Streamdown } from "streamdown";
+
+import { stabilizeStreamingMarkdown } from "../src/components/assistant-ui/streaming-markdown.ts";
+
+const HORIZONTAL_RULE = 'data-streamdown="horizontal-rule"';
+const UNORDERED_LIST = 'data-streamdown="unordered-list"';
+const STRONG = 'data-streamdown="strong"';
+
+function render(markdown: string): string {
+ return renderToStaticMarkup(
+ createElement(Streamdown, { mode: "streaming" }, markdown),
+ );
+}
+
+test("buffers the thematic-break prefix of a bold asterisk list item", () => {
+ const markdown = "* **";
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+
+ const stabilized = stabilizeStreamingMarkdown(markdown, true);
+ assert.equal(stabilized, "");
+ assert.ok(!render(stabilized).includes(HORIZONTAL_RULE));
+});
+
+test("buffers an ambiguous item after a long accumulated response", () => {
+ const preceding = `${"word ".repeat(20_000)}\n\n`;
+ const markdown = `${preceding}* **`;
+
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), preceding);
+});
+
+test("buffers triple-emphasis list prefixes", () => {
+ for (const markdown of ["* ***", "* ****", "* *****"]) {
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), "");
+ }
+
+ const markdown = "* ***Heading***";
+ const stabilized = stabilizeStreamingMarkdown(markdown, true);
+ assert.equal(stabilized, markdown);
+ assert.ok(render(stabilized).includes(UNORDERED_LIST));
+ assert.ok(!render(stabilized).includes(HORIZONTAL_RULE));
+});
+
+test("buffers tab-separated list prefixes", () => {
+ for (const separator of ["\t", " \t", " \t", "\t ", " \t "]) {
+ const markdown = `*${separator}**`;
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), "");
+
+ const completed = `*${separator}**Heading**`;
+ const stabilized = stabilizeStreamingMarkdown(completed, true);
+ const html = render(stabilized);
+ assert.equal(stabilized, completed);
+ assert.ok(html.includes(UNORDERED_LIST));
+ assert.ok(html.includes(STRONG));
+ assert.ok(!html.includes(HORIZONTAL_RULE));
+ }
+});
+
+test("keeps tab-indented code prefixes unchanged", () => {
+ for (const markdown of ["*\t\t**", "* \t**", "*\t **"]) {
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), markdown);
+ }
+});
+
+test("reveals the list as soon as bold content arrives", () => {
+ const markdown = "* **Heading";
+ const stabilized = stabilizeStreamingMarkdown(markdown, true);
+ const html = render(stabilized);
+
+ assert.equal(stabilized, markdown);
+ assert.ok(html.includes(UNORDERED_LIST));
+ assert.ok(html.includes(STRONG));
+ assert.ok(!html.includes(HORIZONTAL_RULE));
+});
+
+test("keeps completed thematic breaks unchanged", () => {
+ const markdown = "* **";
+ assert.equal(stabilizeStreamingMarkdown(markdown, false), markdown);
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+});
+
+test("buffers only the ambiguous trailing line", () => {
+ assert.equal(stabilizeStreamingMarkdown("* First\n* **", true), "* First\n");
+ assert.equal(
+ stabilizeStreamingMarkdown("* Parent\n * **", true),
+ "* Parent\n",
+ );
+ assert.equal(
+ stabilizeStreamingMarkdown("- Parent\n * **", true),
+ "- Parent\n",
+ );
+ assert.equal(
+ stabilizeStreamingMarkdown("- Parent\n * **", true),
+ "- Parent\n * **",
+ );
+});
+
+test("buffers ambiguous items relative to their blockquote container", () => {
+ for (const markdown of [
+ "> * **",
+ "> > * **",
+ " > * **",
+ "- Parent\n > * **",
+ ]) {
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(
+ stabilizeStreamingMarkdown(markdown, true),
+ markdown.includes("\n") ? "- Parent\n" : "",
+ );
+ }
+
+ const markdown = "> * **Heading";
+ const stabilized = stabilizeStreamingMarkdown(markdown, true);
+ const html = render(stabilized);
+ assert.equal(stabilized, markdown);
+ assert.ok(html.includes(UNORDERED_LIST));
+ assert.ok(html.includes(STRONG));
+ assert.ok(!html.includes(HORIZONTAL_RULE));
+});
+
+test("recognizes CommonMark line endings", () => {
+ for (const [markdown, expected] of [
+ ["First\r* **", "First\r"],
+ ["First\r\n* **", "First\r\n"],
+ ["First\n* **", "First\n"],
+ ] as const) {
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), expected);
+ }
+});
+
+test("preserves ambiguous-looking content in list-contained fences", () => {
+ const unchanged = [
+ "- ~~~markdown\n * **",
+ "1. ```markdown\n * **",
+ "> - ~~~markdown\n> * **",
+ ];
+
+ for (const markdown of unchanged) {
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), markdown);
+ }
+});
+
+test("preserves ambiguous-looking content in raw HTML blocks", () => {
+ const unchanged = [
+ "<pre>\n* **",
+ "<SCRIPT>\n* **",
+ "<!-- example\n* **",
+ "<div>\n* **",
+ ];
+
+ for (const markdown of unchanged) {
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), markdown);
+ }
+
+ assert.equal(
+ stabilizeStreamingMarkdown("<div>\ntext\n\n* **", true),
+ "<div>\ntext\n\n",
+ );
+});
+
+test("preserves GFM footnote content that does not render a rule", () => {
+ // An empty footnote renders nothing, so buffering it is invisible. Assert
+ // the rendered output, not the string.
+ const markdown = "See[^1]\n\n[^1]:\n * **";
+ const html = render(markdown);
+
+ assert.ok(!html.includes(HORIZONTAL_RULE));
+ assert.equal(render(stabilizeStreamingMarkdown(markdown, true)), html);
+
+ const completed = `${markdown}Heading**`;
+ const completedHtml = render(completed);
+ assert.ok(completedHtml.includes(UNORDERED_LIST));
+ assert.ok(completedHtml.includes(STRONG));
+ assert.ok(!completedHtml.includes(HORIZONTAL_RULE));
+});
+
+test("buffers a populated GFM footnote that does render a rule", () => {
+ const markdown = "See[^1]\n\n[^1]: text\n\n * **";
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+
+ const stabilized = stabilizeStreamingMarkdown(markdown, true);
+ assert.equal(stabilized, "See[^1]\n\n[^1]: text\n\n");
+ assert.ok(!render(stabilized).includes(HORIZONTAL_RULE));
+});
+
+test("is not defeated by literal dollar signs", () => {
+ // Dollar parity mistook prices, shell vars and code spans for math, which
+ // disabled buffering for the rest of the response.
+ for (const preceding of [
+ "Price is $5",
+ "Use `$HOME`",
+ "```sh\necho $HOME\n```",
+ ]) {
+ const markdown = `${preceding}\n\n* **`;
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(
+ stabilizeStreamingMarkdown(markdown, true),
+ `${preceding}\n\n`,
+ );
+ }
+
+ // Real math is still left alone. `render` has no math plugin, unlike the app,
+ // so only the decision can be asserted here.
+ assert.equal(stabilizeStreamingMarkdown("$$\n* **", true), "$$\n* **");
+});
+
+test("keeps lines that Streamdown's incomplete-Markdown repair already fixes", () => {
+ // remend closes the open construct, so these already render as a list and
+ // buffering would hide a correctly displayed line.
+ for (const markdown of ["[open\n* **", "`open\n* **", "[open](\n* **"]) {
+ assert.ok(!render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), markdown);
+ }
+
+ // An unclosed bold still leaves a real rule to buffer.
+ assert.ok(render("**open\n* **").includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown("**open\n* **", true), "**open\n");
+});
+
+test("does not parse a degenerate punctuation run", () => {
+ // Parsing a long run is quadratic: unbounded, 100k dashes took over 8s.
+ for (const run of ["-", "*", "_"]) {
+ const markdown = `Intro.\n\n* ${run.repeat(100_000)}`;
+ const started = performance.now();
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), markdown);
+ assert.ok(performance.now() - started < 250);
+ }
+
+ // A plausible run is still inside the bound.
+ assert.equal(stabilizeStreamingMarkdown(`* ${"*".repeat(60)}`, true), "");
+});
+
+test("does not reinterpret adjacent Markdown constructs", () => {
+ const unchanged = [
+ "- **",
+ "+ **",
+ "1. **",
+ "***",
+ "* *",
+ "* __",
+ "released in\n1976.",
+ " * **",
+ "```markdown\n* **",
+ "~~~markdown\n* **",
+ "$$\n* **",
+ ];
+
+ for (const markdown of unchanged) {
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), markdown);
+ }
+});
+
+test("buffers dash and underscore runs, not only asterisks", () => {
+ // Same ambiguity: CLI flag lists ("- --verbose: ...") stream through a
+ // thematic-break frame.
+ for (const markdown of [
+ "- --",
+ "- ---",
+ "+ ---",
+ "* ---",
+ "- ___",
+ "* ___",
+ ]) {
+ assert.ok(render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), "");
+ }
+
+ const markdown = "- --verbose";
+ const stabilized = stabilizeStreamingMarkdown(markdown, true);
+ const html = render(stabilized);
+ assert.equal(stabilized, markdown);
+ assert.ok(html.includes(UNORDERED_LIST));
+ assert.ok(!html.includes(HORIZONTAL_RULE));
+});
+
+test("keeps runs that do not currently render a rule", () => {
+ // Two characters never make a break, and mixed runs are not breaks, so
+ // nothing is flashing and nothing may be hidden.
+ for (const markdown of ["- **", "+ **", "* --", "- *-*", "* -_-", "+ ++"]) {
+ assert.ok(!render(markdown).includes(HORIZONTAL_RULE));
+ assert.equal(stabilizeStreamingMarkdown(markdown, true), markdown);
+ }
+});