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: keep unfinished chat links as text instead of [blocked] (#12104)

UmranPros7aded1f86fb4
diff --git a/studio/frontend/src/components/assistant-ui/markdown-text.tsx b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
index aba670ec370..566a64a038c 100644
--- a/studio/frontend/src/components/assistant-ui/markdown-text.tsx
+++ b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
@@ -111,6 +111,8 @@ import { unslothDarkTheme, unslothLightTheme } from "./code-themes";
import { stabilizeStreamingMarkdown } from "./streaming-markdown";
import {
IncrementalMarkdownCache,
+ LITERAL_LINK_REMEND,
+ hasIncompleteLinkRepair,
markdownRenderKey,
parseMarkdownIntoRenderableBlocks,
withoutStreamdownAnimationPlugin,
@@ -1232,6 +1234,10 @@ function MarkdownTextRenderer({
const incrementalRender = isStreaming
? incrementalCache.update(processedText)
: null;
+ const pendingLinkRepair = useMemo(
+ () => !isStreaming && hasIncompleteLinkRepair(processedText),
+ [isStreaming, processedText],
+ );
const renderKey = markdownRenderKey(processedText);
const audioMatch = displayText.match(AUDIO_PLAYER_RE);
@@ -1249,6 +1255,7 @@ function MarkdownTextRenderer({
key={`${messageId}:${incrementalCache.renderGeneration}:${renderKey}:${sandboxScopeKey}`}
mode="streaming"
parseIncompleteMarkdown={!incrementalRender}
+ remend={pendingLinkRepair ? LITERAL_LINK_REMEND : undefined}
parseMarkdownIntoBlocksFn={
incrementalRender?.parseMarkdownIntoBlocks ??
parseMarkdownIntoRenderableBlocks
diff --git a/studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts b/studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts
index 64ce20140be..8ba8ed48ba7 100644
--- a/studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts
+++ b/studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts
@@ -910,6 +910,31 @@ export type IncrementalMarkdownRender = {
parseMarkdownIntoBlocks: (markdown: string) => string[];
};
+const INCOMPLETE_LINK_REPAIR = "](streamdown:incomplete-link)";
+
+export function hasIncompleteLinkRepair(
+ source: string,
+ repaired?: string,
+): boolean {
+ // Only an unclosed `[` gets the placeholder, so bracket-free replies skip the remend pass.
+ if (!source.includes("[")) return false;
+ const after = repaired ?? remend(source);
+ return (
+ after.split(INCOMPLETE_LINK_REPAIR).length >
+ source.split(INCOMPLETE_LINK_REPAIR).length
+ );
+}
+
+// Skips only remend's link pass, whose placeholder renders as "[blocked]"; every other repair still runs.
+export const LITERAL_LINK_REMEND = { links: false, images: false } as const;
+
+export function repairStreamingMarkdown(source: string): string {
+ const repaired = remend(source);
+ return hasIncompleteLinkRepair(source, repaired)
+ ? remend(source, LITERAL_LINK_REMEND)
+ : repaired;
+}
+
// Marker facts the retained prefix carries into the tail repair.
type RetainedContext = {
multilineKatex: boolean;
@@ -977,12 +1002,26 @@ function repairContextPrefix(context: RetainedContext): string {
);
}
-function repairTail(tail: string, context: RetainedContext): string {
+function repairTail(
+ tail: string,
+ context: RetainedContext,
+ options?: typeof LITERAL_LINK_REMEND,
+): string {
const prefix = repairContextPrefix(context);
if (!prefix) {
- return remend(tail);
+ return remend(tail, options);
}
- return remend(prefix + tail).slice(prefix.length);
+ return remend(prefix + tail, options).slice(prefix.length);
+}
+
+function repairTailKeepingLinks(
+ tail: string,
+ context: RetainedContext,
+ repaired = repairTail(tail, context),
+): string {
+ return hasIncompleteLinkRepair(tail, repaired)
+ ? repairTail(tail, context, LITERAL_LINK_REMEND)
+ : repaired;
}
// Where remend believes a fence is open. It toggles on any ``` run, wherever on the line that run
@@ -1349,7 +1388,7 @@ export class IncrementalMarkdownCache {
private renderFullDocument(markdown: string): IncrementalMarkdownRender {
this.resetIncrementalState(markdown);
this.fullDocumentMode = true;
- return this.render(remend(markdown));
+ return this.render(repairStreamingMarkdown(markdown));
}
// The text handed to the cache is not always an extension of the last one: `preprocessLaTeX`
@@ -1454,8 +1493,11 @@ export class IncrementalMarkdownCache {
this.updateTail(markdown);
- const repaired =
- this.repairOpenFence() ?? repairTail(this.tail, this.context);
+ const repaired = repairTailKeepingLinks(
+ this.tail,
+ this.context,
+ this.repairOpenFence() ?? undefined,
+ );
// globally scoped definitions must stay in the same rendered document as
// their uses, so neither construct can retain an independently parsed prefix.
@@ -1474,7 +1516,6 @@ export class IncrementalMarkdownCache {
) {
return this.renderFullDocument(markdown);
}
-
const blocks = parseMarkdownIntoBlocks(repaired);
const candidateCount = Math.max(0, blocks.length - ROLLBACK_BLOCKS);
@@ -1507,7 +1548,7 @@ export class IncrementalMarkdownCache {
committedText,
);
const nextTail = this.tail.slice(commit.length);
- const nextMarkdown = repairTail(nextTail, nextContext);
+ const nextMarkdown = repairTailKeepingLinks(nextTail, nextContext);
// A repeating reply can leave the tail unchanged once a block is retained.
// Streamdown would then see the Markdown it already holds and skip the
diff --git a/studio/frontend/tests/markdown-streaming-scheduling.test.ts b/studio/frontend/tests/markdown-streaming-scheduling.test.ts
index f7a944def2b..17f1c8b6aa9 100644
--- a/studio/frontend/tests/markdown-streaming-scheduling.test.ts
+++ b/studio/frontend/tests/markdown-streaming-scheduling.test.ts
@@ -163,6 +163,10 @@ test("streaming reparses only the active Markdown tail", () => {
),
"{!incrementalRender}",
);
+ assert.equal(
+ jsxAttribute(streamdown, "remend")?.initializer?.getText(source),
+ "{pendingLinkRepair ? LITERAL_LINK_REMEND : undefined}",
+ );
assert.equal(
jsxAttribute(streamdown, "parseMarkdownIntoBlocksFn")?.initializer?.getText(
source,
diff --git a/studio/frontend/tests/remend-complete-markdown.test.ts b/studio/frontend/tests/remend-complete-markdown.test.ts
index d2c63dee317..d941a72424c 100644
--- a/studio/frontend/tests/remend-complete-markdown.test.ts
+++ b/studio/frontend/tests/remend-complete-markdown.test.ts
@@ -11,6 +11,10 @@ import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import remend from "remend";
import { Streamdown } from "streamdown";
+import {
+ LITERAL_LINK_REMEND,
+ hasIncompleteLinkRepair,
+} from "../src/components/assistant-ui/streaming-render-schedule.ts";
/**
* WHY THIS FILE EXISTS. `remend` is the incomplete-markdown repair Streamdown runs over a message
@@ -148,12 +152,27 @@ function renderSettled(markdown: string): string {
{
mode: "streaming",
parseIncompleteMarkdown: settledParseIncompleteMarkdown(),
+ remend: hasIncompleteLinkRepair(markdown)
+ ? LITERAL_LINK_REMEND
+ : undefined,
},
markdown,
),
);
}
+test("a settled unfinished link remains text without a blocked placeholder", () => {
+ const html = renderSettled("See [example](https://exa");
+ assert.match(html, /See \[example\]\(/);
+ assert.match(html, /https:\/\/exa/);
+ assert.doesNotMatch(html, /\[blocked\]|streamdown:incomplete-link/);
+ assert.match(renderSettled("See [example](javascript:alert)"), /\[blocked\]/);
+ // The repairs remend makes before its link pass still apply.
+ const list = renderSettled("- >= 16 GB\n\nSee [foo");
+ assert.match(list, /<li[^>]*>&gt;= 16 GB<\/li>/);
+ assert.doesNotMatch(list, /blockquote|\[blocked\]/);
+});
+
test("the settled render path runs the repair, not just the package", () => {
// THE TESTS ABOVE CALL `remend` THEMSELVES, so all of them pass while the UI has the repair
// switched off: mutating `parseIncompleteMarkdown={!incrementalRender}` to `{false}` in
diff --git a/studio/frontend/tests/streaming-crlf-retention.test.ts b/studio/frontend/tests/streaming-crlf-retention.test.ts
index bd035c18255..856f42d02af 100644
--- a/studio/frontend/tests/streaming-crlf-retention.test.ts
+++ b/studio/frontend/tests/streaming-crlf-retention.test.ts
@@ -3,11 +3,13 @@
import assert from "node:assert/strict";
import test from "node:test";
-import remend from "remend";
import { parseMarkdownIntoBlocks } from "streamdown";
import { stabilizeStreamingMarkdown } from "../src/components/assistant-ui/streaming-markdown.ts";
-import { IncrementalMarkdownCache } from "../src/components/assistant-ui/streaming-render-schedule.ts";
+import {
+ IncrementalMarkdownCache,
+ repairStreamingMarkdown,
+} from "../src/components/assistant-ui/streaming-render-schedule.ts";
import { preprocessLaTeX } from "../src/lib/latex.ts";
const processStreamingText = (text: string): string =>
@@ -97,7 +99,7 @@ test("a CRLF reply matches a whole-document split at every prefix", () => {
const render = cache.update(input);
assert.deepEqual(
render.parseMarkdownIntoBlocks(render.markdown),
- parseMarkdownIntoBlocks(remend(asLf(input))),
+ parseMarkdownIntoBlocks(repairStreamingMarkdown(asLf(input))),
`block mismatch at prefix ${length} of ${JSON.stringify(source.slice(0, 60))}`,
);
}
@@ -117,7 +119,7 @@ test("an LF reply is untouched by the line-ending handling", () => {
const render = cache.update(input);
assert.deepEqual(
render.parseMarkdownIntoBlocks(render.markdown),
- parseMarkdownIntoBlocks(remend(input)),
+ parseMarkdownIntoBlocks(repairStreamingMarkdown(input)),
`block mismatch at prefix ${length}`,
);
}
diff --git a/studio/frontend/tests/streaming-latex-corpus.test.ts b/studio/frontend/tests/streaming-latex-corpus.test.ts
index 0a035cf9996..3a1e8790033 100644
--- a/studio/frontend/tests/streaming-latex-corpus.test.ts
+++ b/studio/frontend/tests/streaming-latex-corpus.test.ts
@@ -25,7 +25,10 @@ import remend from "remend";
import { parseMarkdownIntoBlocks } from "streamdown";
import { stabilizeStreamingMarkdown } from "../src/components/assistant-ui/streaming-markdown.ts";
-import { IncrementalMarkdownCache } from "../src/components/assistant-ui/streaming-render-schedule.ts";
+import {
+ IncrementalMarkdownCache,
+ repairStreamingMarkdown,
+} from "../src/components/assistant-ui/streaming-render-schedule.ts";
import { preprocessLaTeX } from "../src/lib/latex.ts";
const processStreamingText = (text: string): string =>
@@ -173,7 +176,7 @@ function assertMatchesFullSplit(
);
assert.deepEqual(
render.parseMarkdownIntoBlocks(render.markdown),
- parseMarkdownIntoBlocks(remend(input)),
+ parseMarkdownIntoBlocks(repairStreamingMarkdown(input)),
`block mismatch at prefix ${length} of ${name}: ${JSON.stringify(source.slice(0, 200))}`,
);
}
diff --git a/studio/frontend/tests/streaming-latex-retro-edit.test.ts b/studio/frontend/tests/streaming-latex-retro-edit.test.ts
index f02c65c64df..463e444b293 100644
--- a/studio/frontend/tests/streaming-latex-retro-edit.test.ts
+++ b/studio/frontend/tests/streaming-latex-retro-edit.test.ts
@@ -3,11 +3,13 @@
import assert from "node:assert/strict";
import test from "node:test";
-import remend from "remend";
import { parseMarkdownIntoBlocks } from "streamdown";
import { stabilizeStreamingMarkdown } from "../src/components/assistant-ui/streaming-markdown.ts";
-import { IncrementalMarkdownCache } from "../src/components/assistant-ui/streaming-render-schedule.ts";
+import {
+ IncrementalMarkdownCache,
+ repairStreamingMarkdown,
+} from "../src/components/assistant-ui/streaming-render-schedule.ts";
import { preprocessLaTeX } from "../src/lib/latex.ts";
const processStreamingText = (text: string): string =>
@@ -246,7 +248,7 @@ test("retaining across a rewrite still matches a full Streamdown split", () => {
const render = cache.update(input);
assert.deepEqual(
render.parseMarkdownIntoBlocks(render.markdown),
- parseMarkdownIntoBlocks(remend(input)),
+ parseMarkdownIntoBlocks(repairStreamingMarkdown(input)),
`block mismatch at prefix ${length}`,
);
}
@@ -275,7 +277,7 @@ test("a rewind restores the repair context of the commit it lands on", () => {
const render = cache.update(input);
assert.deepEqual(
render.parseMarkdownIntoBlocks(render.markdown),
- parseMarkdownIntoBlocks(remend(input)),
+ parseMarkdownIntoBlocks(repairStreamingMarkdown(input)),
`block mismatch at prefix ${length}`,
);
}
@@ -314,7 +316,7 @@ test("an edit that closes up a blank line cannot keep the block before it", () =
const render = cache.update(edited);
assert.deepEqual(
render.parseMarkdownIntoBlocks(render.markdown),
- parseMarkdownIntoBlocks(remend(edited)),
+ parseMarkdownIntoBlocks(repairStreamingMarkdown(edited)),
`block mismatch after ${JSON.stringify(edited.slice(0, 24))}`,
);
}
diff --git a/studio/frontend/tests/streaming-open-fence-repair.test.ts b/studio/frontend/tests/streaming-open-fence-repair.test.ts
index 1452564ee31..babc4c975fd 100644
--- a/studio/frontend/tests/streaming-open-fence-repair.test.ts
+++ b/studio/frontend/tests/streaming-open-fence-repair.test.ts
@@ -5,7 +5,10 @@ import assert from "node:assert/strict";
import test from "node:test";
import remend from "remend";
-import { IncrementalMarkdownCache } from "../src/components/assistant-ui/streaming-render-schedule.ts";
+import {
+ IncrementalMarkdownCache,
+ repairStreamingMarkdown,
+} from "../src/components/assistant-ui/streaming-render-schedule.ts";
/**
* A fence that has opened and not yet closed pins the cache: the whole fence
@@ -68,7 +71,7 @@ function assertRepairMatches(text: string, chunk = 192): void {
);
assert.equal(
render.markdown,
- remend(tailOf(cache)),
+ repairStreamingMarkdown(tailOf(cache)),
`frame at ${end} characters diverged from the whole-tail repair`,
);
}
@@ -205,7 +208,7 @@ test("a fence that never closes stays live and is never committed away", () => {
}
render = cache.update(text);
const blocks = render.parseMarkdownIntoBlocks(render.markdown);
- assert.equal(blocks.join(""), remend(text));
+ assert.equal(blocks.join(""), repairStreamingMarkdown(text));
});
/**
diff --git a/studio/frontend/tests/streaming-render-schedule.test.ts b/studio/frontend/tests/streaming-render-schedule.test.ts
index 892ad1f40ce..a3bb9101724 100644
--- a/studio/frontend/tests/streaming-render-schedule.test.ts
+++ b/studio/frontend/tests/streaming-render-schedule.test.ts
@@ -3,19 +3,86 @@
import assert from "node:assert/strict";
import test from "node:test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
import remend from "remend";
import { Streamdown, parseMarkdownIntoBlocks } from "streamdown";
import { stabilizeStreamingMarkdown } from "../src/components/assistant-ui/streaming-markdown.ts";
import {
IncrementalMarkdownCache,
+ hasIncompleteLinkRepair,
markdownRenderKey,
markdownRenderScope,
parseMarkdownIntoRenderableBlocks,
+ repairStreamingMarkdown,
withoutStreamdownAnimationPlugin,
} from "../src/components/assistant-ui/streaming-render-schedule.ts";
import { preprocessLaTeX } from "../src/lib/latex.ts";
+test("an unfinished link stays literal instead of showing Streamdown's blocked placeholder", () => {
+ const cache = new IncrementalMarkdownCache();
+ for (const source of [
+ "See [example",
+ "See [example](",
+ "See [example](https://exa",
+ ]) {
+ const render = cache.update(source);
+ assert.equal(render.markdown, source);
+ assert.equal(hasIncompleteLinkRepair(source), true);
+ const html = renderToStaticMarkup(
+ createElement(
+ Streamdown,
+ {
+ mode: "streaming",
+ parseIncompleteMarkdown: false,
+ parseMarkdownIntoBlocksFn: render.parseMarkdownIntoBlocks,
+ },
+ render.markdown,
+ ),
+ );
+ assert.doesNotMatch(html, /\[blocked\]|streamdown:incomplete-link/);
+ assert.match(html, /See \[example/);
+ }
+
+ const list = new IncrementalMarkdownCache().update("- >= 16 GB\n\nSee [foo");
+ assert.equal(list.markdown, "- \\>= 16 GB\n\nSee [foo");
+
+ const scoped = new IncrementalMarkdownCache();
+ const body = Array.from({ length: 8 }, (_, i) => `Paragraph ${i}.`).join(
+ "\n\n",
+ );
+ const head = `Read [docs][r] first.\n\n${body}\n\n`;
+ scoped.update(head);
+ const both = `${head}[r]: https://example.com\n\nSee [foo`;
+ const scopedRender = scoped.update(both);
+ assert.equal(
+ (scoped as unknown as { fullDocumentMode: boolean }).fullDocumentMode,
+ true,
+ );
+ assert.deepEqual(
+ scopedRender.parseMarkdownIntoBlocks(scopedRender.markdown),
+ parseMarkdownIntoRenderableBlocks(repairStreamingMarkdown(both)),
+ );
+
+ const complete = "See [example](https://example.com)";
+ assert.equal(cache.update(complete).markdown, complete);
+ assert.equal(hasIncompleteLinkRepair(complete), false);
+ assert.equal(
+ hasIncompleteLinkRepair("literal streamdown:incomplete-link"),
+ false,
+ );
+ const unsafe = "See [example](javascript:alert)";
+ const html = renderToStaticMarkup(
+ createElement(
+ Streamdown,
+ { mode: "streaming", parseIncompleteMarkdown: false },
+ unsafe,
+ ),
+ );
+ assert.match(html, /\[blocked\]/);
+});
+
test("only Streamdown's animation transformer is removed", () => {
const first = () => undefined;
const animation = () => undefined;
@@ -133,7 +200,7 @@ test("incremental blocks match a full Streamdown split at every prefix", () => {
const render = cache.update(input);
assert.deepEqual(
render.parseMarkdownIntoBlocks(render.markdown),
- parseMarkdownIntoRenderableBlocks(remend(input)),
+ parseMarkdownIntoRenderableBlocks(repairStreamingMarkdown(input)),
`block mismatch at prefix ${length} of ${JSON.stringify(source)}`,
);
}