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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep <placeholder> and Vec<T> text in research reports and file previews (#12489)
Nilay47ecafb7a446
diff --git a/studio/frontend/src/components/assistant-ui/composer-draft-preview.tsx b/studio/frontend/src/components/assistant-ui/composer-draft-preview.tsxindex 0115e356458..67fd4387e20 100644--- a/studio/frontend/src/components/assistant-ui/composer-draft-preview.tsx+++ b/studio/frontend/src/components/assistant-ui/composer-draft-preview.tsx@@ -5,6 +5,9 @@ import { useDeferredValue, type ComponentProps } from "react";import { Streamdown } from "streamdown";import { useChatPreferencesStore } from "@/features/chat";import { useT } from "@/i18n";+import { withLiteralUnknownTags } from "@/lib/markdown-data-images";++const REHYPE_PLUGINS = withLiteralUnknownTags();// Render draft URLs without navigation or image requests.const components: NonNullable<ComponentProps<typeof Streamdown>["components"]> =@@ -37,7 +40,7 @@ export function ComposerDraftPreview({ text }: { text: string }) {mode="static"controls={false}components={components}- skipHtml+ rehypePlugins={REHYPE_PLUGINS}className="min-w-0 space-y-2 [overflow-wrap:anywhere] [&_pre]:whitespace-pre-wrap [&_table]:block [&_table]:overflow-x-auto">{draft}diff --git a/studio/frontend/src/components/markdown/markdown-preview.tsx b/studio/frontend/src/components/markdown/markdown-preview.tsxindex dda01b1ce9e..539b0fc1584 100644--- a/studio/frontend/src/components/markdown/markdown-preview.tsx+++ b/studio/frontend/src/components/markdown/markdown-preview.tsx@@ -1,6 +1,7 @@// 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 { withLiteralUnknownTags } from "@/lib/markdown-data-images";import { markdownPluginNeeds } from "@/lib/markdown-plugins";import { openLink } from "@/lib/open-link";import { safeMarkdownUrl } from "@/lib/safe-markdown-url";@@ -24,6 +25,7 @@ import "katex/dist/katex.min.css";type MarkdownPlugins = NonNullable<ComponentProps<typeof Streamdown>["plugins"]>;+const REHYPE_PLUGINS = withLiteralUnknownTags();const MARKDOWN_COMPONENTS = {a: ({ href, children, ...props }: ComponentProps<"a">) => (<a@@ -100,6 +102,7 @@ function MarkdownPreviewImpl({<Streamdownmode="static"plugins={plugins}+ rehypePlugins={REHYPE_PLUGINS}components={inert ? INERT_MARKDOWN_COMPONENTS : MARKDOWN_COMPONENTS}urlTransform={safeMarkdownUrl}controls={false}diff --git a/studio/frontend/src/features/hub/catalog/model-readme.tsx b/studio/frontend/src/features/hub/catalog/model-readme.tsxindex ec3f5a7cda8..0d909fd55db 100644--- a/studio/frontend/src/features/hub/catalog/model-readme.tsx+++ b/studio/frontend/src/features/hub/catalog/model-readme.tsx@@ -7,6 +7,7 @@ import { useOnlineStatus } from "@/features/hub/hooks/use-online-status";import { LruMap } from "@/features/hub/lib/lru-map";import { isHuggingFaceOffline } from "@/features/hub/lib/network";import { fingerprintToken } from "@/features/hub/lib/token-fingerprint";+import { withLiteralUnknownTags } from "@/lib/markdown-data-images";import {type MarkdownPluginNeeds,markdownPluginNeeds,@@ -68,6 +69,7 @@ const README_ALLOWED_TAGS: NonNullable<source: ["src", "type", "media"],track: ["src", "kind", "srclang", "label", "default"],};+const README_REHYPE_PLUGINS = withLiteralUnknownTags(README_ALLOWED_TAGS);const README_RENDER_CHAR_LIMIT = 120_000;const README_CACHE_TTL_MS = 60_000;@@ -536,6 +538,7 @@ export function ModelReadme({controls={false}components={README_COMPONENTS}allowedTags={README_ALLOWED_TAGS}+ rehypePlugins={README_REHYPE_PLUGINS}urlTransform={urlTransform}>{current.body}diff --git a/studio/frontend/src/lib/markdown-data-images.ts b/studio/frontend/src/lib/markdown-data-images.tsindex 59c55b6e213..bf16b08b910 100644--- a/studio/frontend/src/lib/markdown-data-images.ts+++ b/studio/frontend/src/lib/markdown-data-images.ts@@ -8,8 +8,8 @@* of the image. Streamdown extends its own schema with caller `allowedTags` only when it receives* its default pipeline (identity check), so callers that pass one must carry that merge themselves.*- * Sanitize also drops every tag outside the schema, which erases `<placeholder>` and `Vec<T>` from- * replies, so those become text before `raw`. Sanitize and harden still decide every element.+ * Sanitize also drops every tag outside the schema, which erases `<placeholder>` and `Vec<T>`, so+ * those become text before `raw`. Sanitize and harden still decide every element.*/import type { Element, Root, RootContent } from "hast";import type { Pluggable, Plugin } from "unified";@@ -23,13 +23,64 @@ interface SanitizeSchema {const HTML_TAG_NAME = /^\s*<\/?([a-z][^\s/<>]*)/i;const INNER_TAG = /<(\/?)([a-z][^\s/<>]*)/gi;+// Formatting tags outside the schema that documents use as markup. Where a pipeline opts in, they are+// left for sanitize to unwrap so their text shows: always in a raw HTML block, which closes them+// implicitly, and in prose only when matched, so "the <small> tag" stays text like any unknown tag.+const UNWRAPPED_TAGS = new Set([+ "abbr",+ "big",+ "center",+ "cite",+ "dfn",+ "figcaption",+ "figure",+ "font",+ "mark",+ "nobr",+ "small",+ "u",+]);-const rehypeLiteralUnknownTags: Plugin<[string[]], Root> =- function rehypeLiteralUnknownTags(tagNames) {- const known = new Set(tagNames);+/** `child:offset` of every matched formatting-tag opener and closer among the raw children. */+function matchedFormattingTags(children: RootContent[]): Set<string> {+ const matched = new Set<string>();+ const open = new Map<string, string[]>();+ children.forEach((child, index) => {+ if (child.type !== "raw") return;+ for (const tag of child.value.matchAll(INNER_TAG)) {+ const name = tag[2].toLowerCase();+ if (!UNWRAPPED_TAGS.has(name)) continue;+ const key = `${index}:${tag.index}`;+ const openers = open.get(name) ?? [];+ open.set(name, openers);+ if (!tag[1]) {+ openers.push(key);+ continue;+ }+ const opener = openers.pop();+ if (opener) matched.add(opener).add(key);+ }+ });+ return matched;+}++interface LiteralTagOptions {+ tagNames: string[];+ unwrapFormatting: boolean;+}++// One options object: Streamdown keys its processor cache on a plugin's first option only.+const rehypeLiteralUnknownTags: Plugin<[LiteralTagOptions], Root> =+ function rehypeLiteralUnknownTags({ tagNames, unwrapFormatting }) {+ const schemaTags = new Set(tagNames);return (tree) => {function walk(node: Root | Element): void {const children: RootContent[] = node.children;+ const matched = unwrapFormatting+ ? matchedFormattingTags(children)+ : new Set<string>();+ const isSchemaTag = (name: string) =>+ schemaTags.has(name.toLowerCase());children.forEach((child, index) => {if (child.type === "element") {walk(child);@@ -38,11 +89,20 @@ const rehypeLiteralUnknownTags: Plugin<[string[]], Root> =if (child.type !== "raw") return;const tag = HTML_TAG_NAME.exec(child.value)?.[1];if (!tag) return;- if (known.has(tag.toLowerCase())) {+ if (+ isSchemaTag(tag) ||+ (unwrapFormatting &&+ node.type === "root" &&+ UNWRAPPED_TAGS.has(tag.toLowerCase())) ||+ matched.has(`${index}:${child.value.indexOf("<")}`)+ ) {child.value = child.value.replace(INNER_TAG,(match, slash, name) =>- known.has(name.toLowerCase()) ? match : `<${slash}${name}`,+ isSchemaTag(name) ||+ (unwrapFormatting && UNWRAPPED_TAGS.has(name.toLowerCase()))+ ? match+ : `<${slash}${name}`,);return;}@@ -65,12 +125,16 @@ const rehypeLiteralUnknownTags: Plugin<[string[]], Root> =};};-/** Keep data images, show disallowed tags as text, resolve sandbox paths before URL hardening. */-export function withDataImageSupport(+function literalTagPipeline(allowedTags: Record<string, string[]>,- beforeHarden: Pluggable[] = [],+ srcProtocols: string[],+ beforeHarden: Pluggable[],+ unwrapFormatting: boolean,): Pluggable[] {- const sanitize = defaultRehypePlugins.sanitize as [Plugin<[SanitizeSchema]>, SanitizeSchema];+ const sanitize = defaultRehypePlugins.sanitize as [+ Plugin<[SanitizeSchema]>,+ SanitizeSchema,+ ];const [sanitizePlugin, schema] = sanitize;// Positional by design: Streamdown itself builds its default pipeline as `Object.values` of this// same object, so spreading it in the same order reproduces that pipeline exactly. Naming the keys@@ -78,7 +142,7 @@ export function withDataImageSupport(const [raw, , harden] = Object.values(defaultRehypePlugins);const tagNames = [...(schema.tagNames ?? []), ...Object.keys(allowedTags)];return [- [rehypeLiteralUnknownTags, tagNames],+ [rehypeLiteralUnknownTags, { tagNames, unwrapFormatting }],raw,[sanitizePlugin,@@ -88,8 +152,7 @@ export function withDataImageSupport(attributes: { ...schema.attributes, ...allowedTags },protocols: {...schema.protocols,- // Harden still gates the scheme on `allowDataImages` and only honors `data:image/*`.- src: [...(schema.protocols?.src ?? []), "data"],+ src: [...(schema.protocols?.src ?? []), ...srcProtocols],},},],@@ -97,3 +160,21 @@ export function withDataImageSupport(harden,];}++/** Streamdown's default pipeline plus `allowedTags` for documents: tags outside the schema stay text,+ * formatting tags used as markup are unwrapped. */+export function withLiteralUnknownTags(+ allowedTags: Record<string, string[]> = {},+): Pluggable[] {+ return literalTagPipeline(allowedTags, [], [], true);+}++/** Keep data images, keep tags outside the schema as text, resolve sandbox paths before URL hardening. */+export function withDataImageSupport(+ allowedTags: Record<string, string[]>,+ beforeHarden: Pluggable[] = [],+): Pluggable[] {+ // Harden still gates the scheme on `allowDataImages` and only honors `data:image/*`.+ // Replies mention tags in prose more often than they format with them, so none are unwrapped.+ return literalTagPipeline(allowedTags, ["data"], beforeHarden, false);+}diff --git a/studio/frontend/tests/markdown-literal-tags.test.ts b/studio/frontend/tests/markdown-literal-tags.test.tsindex 8555c1d6055..945b8cb76b8 100644--- a/studio/frontend/tests/markdown-literal-tags.test.ts+++ b/studio/frontend/tests/markdown-literal-tags.test.ts@@ -7,7 +7,10 @@ import { createElement } from "react";import { renderToStaticMarkup } from "react-dom/server";import { Streamdown } from "streamdown";-import { withDataImageSupport } from "../src/lib/markdown-data-images.ts";+import {+ withDataImageSupport,+ withLiteralUnknownTags,+} from "../src/lib/markdown-data-images.ts";const ALLOWED_TAGS = { "search-image": ["token"] };@@ -22,6 +25,16 @@ function render(markdown: string, mode: "static" | "streaming" = "static") {);}+function renderDocument(markdown: string) {+ return renderToStaticMarkup(+ createElement(Streamdown, {+ mode: "static",+ children: markdown,+ rehypePlugins: withLiteralUnknownTags(),+ }),+ );+}+function escaped(text: string) {return text.replaceAll("&", "&")@@ -37,6 +50,8 @@ test("placeholders and generic types in prose stay visible", () => {"Use Vec<T> here, or List<String> in Java.","Save it to /home/<user>/models/<model-name>/ then restart.","The <script> tag loads JS. More text follows.",+ "The <small> tag shrinks text, <u> underlines it.",+ "Wrap the image in a <figure> with a <figcaption>.","For all 0<x and x>1 we have a bound.","<your-api-key>","Use Vec<T> and close </T> here.",@@ -87,6 +102,87 @@ test("allowed HTML tags still render as elements", () => {);});+test("documents unwrap formatting tags used as markup", () => {+ assert.match(+ renderDocument("MMBench<sub><small>EN-DEV</small></sub> and <U>under</U>"),+ /<p>MMBench<sub[^>]*>EN-DEV<\/sub> and under<\/p>/,+ );+ for (const [block, line] of [+ ["<figure>\n<figcaption>Caption</figcaption>\n</figure>", "Caption"],+ ["<center>Use <your-api-key> here</center>", "Use <your-api-key> here"],+ ["<center>\nCentered text", "Centered text"],+ ["Intro\n\n<figure>\n<img src=x>", "Intro"],+ ["<table><tr><td>MMBench<sub><small>EN</td></tr></table>", "MMBench"],+ [+ '<div>The <abbr title="x">API</abbr> takes <T></div>',+ "The API takes <T>",+ ],+ ]) {+ const html = renderDocument(block);+ assert.ok(html.includes(escaped(line)), html);+ assert.doesNotMatch(+ html,+ /(<|<)\/?(figure|figcaption|center|abbr|small)\b/,+ html,+ );+ }+ for (const [markdown, expected] of [+ [+ "Use <mark>text</mark>, e.g. <mark>.",+ "<p>Use text, e.g. <mark>.</p>",+ ],+ ["The <u> tag, as in <u>x</u>.", "<p>The <u> tag, as in x.</p>"],+ ["<u>a <u>b</u> c</u> and </u>", "<p>a b c and </u></p>"],+ [+ "The <small> tag shrinks text.",+ "<p>The <small> tag shrinks text.</p>",+ ],+ [+ "The <small> tag.\n\nLater: </small> here.",+ "<p>The <small> tag.</p>\n<p>Later: </small> here.</p>",+ ],+ ]) {+ const html = renderDocument(markdown);+ assert.ok(html.includes(expected), html);+ }+});++test("chat replies keep formatting tags as text", () => {+ for (const line of [+ "Use <cite>Title</cite> for works.",+ "Open it with <small> and close it with </small>.",+ ]) {+ const html = render(line);+ assert.ok(html.includes(`>${escaped(line)}<`), html);+ }+ for (const [block, line] of [+ [+ "<div>The <small> tag shrinks text.</div>",+ "The <small> tag shrinks text.",+ ],+ [+ "<p>Wrap it in <figure> with a <figcaption>.</p>",+ "Wrap it in <figure> with a <figcaption>.",+ ],+ ]) {+ const html = render(block);+ assert.ok(html.includes(escaped(line)), html);+ }+});++test("withLiteralUnknownTags merges caller tags into the schema", () => {+ const html = renderToStaticMarkup(+ createElement(Streamdown, {+ mode: "static",+ children:+ '<video src="https://example.com/v.mp4" controls></video>\n\nPad with <unk> tokens.',+ rehypePlugins: withLiteralUnknownTags({ video: ["src", "controls"] }),+ }),+ );+ assert.match(html, /<video src="https:\/\/example\.com\/v\.mp4" controls/);+ assert.match(html, /Pad with <unk> tokens\./);+});+test("hostile markup never renders as markup", () => {for (const markdown of ["a <script>alert(1)</script> b",@@ -119,9 +215,9 @@ test("hostile markup never renders as markup", () => {});test("literal tags keep their source position so streamed blocks re-render", () => {- const [plugin, tagNames] = withDataImageSupport(ALLOWED_TAGS)[0] as [- (names: string[]) => (tree: unknown) => void,- string[],+ const [plugin, options] = withDataImageSupport(ALLOWED_TAGS)[0] as [+ (options: unknown) => (tree: unknown) => void,+ unknown,];const position = {start: { line: 3, column: 1, offset: 9 },@@ -143,7 +239,7 @@ test("literal tags keep their source position so streamed blocks re-render", ()},],};- plugin(tagNames)(tree);+ plugin(options)(tree);const [block, paragraph] = tree.children as {position?: unknown;children: { type: string; position?: unknown }[];diff --git a/studio/frontend/tests/markdown-preview-literal-tags.test.ts b/studio/frontend/tests/markdown-preview-literal-tags.test.tsnew file mode 100644index 00000000000..66f92b12f52--- /dev/null+++ b/studio/frontend/tests/markdown-preview-literal-tags.test.ts@@ -0,0 +1,188 @@+// 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 * as React from "react";+import { renderToStaticMarkup } from "react-dom/server";+import * as jsxRuntime from "react/jsx-runtime";+import * as streamdown from "streamdown";+import * as streamdownCode from "@streamdown/code";+import * as streamdownMath from "@streamdown/math";+import * as streamdownMermaid from "@streamdown/mermaid";+import ts from "typescript";++import type * as ComposerDraftPreviewModule from "../src/components/assistant-ui/composer-draft-preview.tsx";+import * as inertComponents from "../src/components/markdown/inert-components.ts";+import type * as MarkdownPreviewModule from "../src/components/markdown/markdown-preview.tsx";+import * as markdownDataImages from "../src/lib/markdown-data-images.ts";+import * as markdownPlugins from "../src/lib/markdown-plugins.ts";+import * as safeMarkdownUrl from "../src/lib/safe-markdown-url.ts";+import * as scheduleIdleTask from "../src/lib/schedule-idle-task.ts";+import * as utils from "../src/lib/utils.ts";+import { readSrc } from "./helpers/kit.ts";+import { loadWithStubs } from "./helpers/module-stubs.ts";+import { openingTag } from "./helpers/tsx-ast.ts";++const shared = {+ react: React,+ "react/jsx-runtime": jsxRuntime,+ streamdown,+ "@/lib/markdown-data-images": markdownDataImages,+};+const { MarkdownPreview } = loadWithStubs<typeof MarkdownPreviewModule>(+ new URL("../src/components/markdown/markdown-preview.tsx", import.meta.url),+ {+ ...shared,+ "@streamdown/code": streamdownCode,+ "@streamdown/math": streamdownMath,+ "@streamdown/mermaid": streamdownMermaid,+ "@/lib/markdown-plugins": markdownPlugins,+ "@/lib/open-link": { openLink: () => false },+ "@/lib/safe-markdown-url": safeMarkdownUrl,+ "@/lib/schedule-idle-task": scheduleIdleTask,+ "@/lib/utils": utils,+ "./inert-components": inertComponents,+ "katex/dist/katex.min.css": {},+ },+);+const { ComposerDraftPreview } = loadWithStubs<+ typeof ComposerDraftPreviewModule+>(+ new URL(+ "../src/components/assistant-ui/composer-draft-preview.tsx",+ import.meta.url,+ ),+ {+ ...shared,+ "@/features/chat": {+ useChatPreferencesStore: (select: (state: object) => unknown) =>+ select({ plainTextComposer: false }),+ },+ "@/i18n": { useT: () => (key: string) => key },+ },+);++const SURFACES = {+ preview: (markdown: string) =>+ renderToStaticMarkup(React.createElement(MarkdownPreview, { markdown })),+ inertPreview: (markdown: string) =>+ renderToStaticMarkup(+ React.createElement(MarkdownPreview, { markdown, inert: true }),+ ),+ composerDraft: (markdown: string) =>+ renderToStaticMarkup(+ React.createElement(ComposerDraftPreview, { text: markdown }),+ ),+};++test("markdown previews keep placeholders and generic types as text", () => {+ for (const [name, render] of Object.entries(SURFACES)) {+ const html = render(+ "Replace <placeholder> with your key.\n\nVec<T> holds values of type T. Option<Box<T>> too.\n\n<your-api-key>",+ );+ assert.match(html, /Replace <placeholder> with your key\./, name);+ assert.match(+ html,+ /Vec<T> holds values of type T\. Option<Box<T>> too\./,+ name,+ );+ assert.match(html, /<p><your-api-key><\/p>/, name);+ }+});++test("markdown previews still render rich markdown", () => {+ const html = SURFACES.preview(+ "Press <kbd>Ctrl</kbd>, see [the docs](https://unsloth.ai).\n\n| a |\n| - |\n| x<br>y |\n\n```rust\nlet v: Vec<T> = vec![];\n```\n\n$$x^2$$",+ );+ assert.match(html, /<kbd[^>]*>Ctrl<\/kbd>/);+ assert.match(html, /<a href="https:\/\/unsloth\.ai\/"[^>]*>the docs<\/a>/);+ assert.match(html, /<table[\s\S]*x<br\/>y/);+ assert.match(html, /let v: Vec<T> = vec!\[\];/);+ assert.match(html, /class="katex/);+ const inert = SURFACES.inertPreview("See [the docs](https://unsloth.ai).");+ assert.doesNotMatch(inert, /<a[\s>]/);+ assert.match(inert, /the docs/);+ assert.match(+ SURFACES.composerDraft("Press <kbd>Ctrl</kbd>."),+ /<kbd[^>]*>Ctrl<\/kbd>/,+ );+});++test("markdown previews keep blocking data: images", () => {+ for (const [name, render] of Object.entries({+ preview: SURFACES.preview,+ inertPreview: SURFACES.inertPreview,+ })) {+ const html = render("");+ assert.doesNotMatch(html, /src="data:/, name);+ }+});++test("hostile markup stays inert in markdown previews", () => {+ for (const [name, render] of Object.entries(SURFACES)) {+ for (const markdown of [+ "a <ScRiPt>alert(1)</ScRiPt> b",+ "<script\nalert(1)",+ "x <IMG SRC=x OnError=alert(1)> y",+ "<svg onload=alert(1)>",+ '<iframe src="https://example.com"></iframe>',+ "<style>*{display:none}</style>",+ '[x](javascript:alert(1)) <a href="jav	ascript:alert(1)">y</a>',+ "<div><svg onload=alert(1)></svg><img src=x onerror=alert(1)></div>",+ "<script>alert(1)</script>",+ ]) {+ const html = render(markdown);+ assert.doesNotMatch(+ html,+ /<(script|iframe|svg|style|object|embed|form|math)\b/i,+ `${name}: ${html}`,+ );+ assert.doesNotMatch(+ html,+ /<[a-z][^>]*\s(on[a-z]+|srcdoc|style)=|<[a-z][^>]*="\s*javascript:/i,+ `${name}: ${html}`,+ );+ }+ }+});++test("Hub model cards are wired to the literal-tag pipeline", () => {+ const path = "features/hub/catalog/model-readme.tsx";+ const source = ts.createSourceFile(+ path,+ readSrc(path),+ ts.ScriptTarget.Latest,+ true,+ ts.ScriptKind.TSX,+ );+ const initializers = new Map<string, string>();+ let rehypePlugins: string | undefined;+ (function visit(node: ts.Node) {+ if (ts.isVariableDeclaration(node) && node.initializer) {+ initializers.set(+ node.name.getText(source),+ node.initializer.getText(source),+ );+ }+ const tag = openingTag(node);+ if (tag?.tagName.getText(source) === "Streamdown") {+ for (const attr of tag.attributes.properties) {+ if (+ ts.isJsxAttribute(attr) &&+ attr.name.getText(source) === "rehypePlugins"+ ) {+ rehypePlugins = attr.initializer+ ?.getText(source)+ .replace(/^\{|\}$/g, "");+ }+ }+ }+ ts.forEachChild(node, visit);+ })(source);+ assert.ok(rehypePlugins, `${path}: Streamdown has no rehypePlugins`);+ assert.equal(+ initializers.get(rehypePlugins) ?? rehypePlugins,+ "withLiteralUnknownTags(README_ALLOWED_TAGS)",+ );+});