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 chat replies (#12376)
Nilay28d8a04d129f
diff --git a/studio/frontend/src/lib/markdown-data-images.ts b/studio/frontend/src/lib/markdown-data-images.tsindex 4a6ee970672..59c55b6e213 100644--- a/studio/frontend/src/lib/markdown-data-images.ts+++ b/studio/frontend/src/lib/markdown-data-images.ts@@ -7,7 +7,11 @@* allow data images (`allowDataImages: true`), so the message renders "[Image blocked: …]" instead* 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.*/+import type { Element, Root, RootContent } from "hast";import type { Pluggable, Plugin } from "unified";import { defaultRehypePlugins } from "streamdown";@@ -17,7 +21,51 @@ interface SanitizeSchema {protocols?: Record<string, string[]>;}-/** Keep data images and resolve sandbox paths before URL hardening. */+const HTML_TAG_NAME = /^\s*<\/?([a-z][^\s/<>]*)/i;+const INNER_TAG = /<(\/?)([a-z][^\s/<>]*)/gi;++const rehypeLiteralUnknownTags: Plugin<[string[]], Root> =+ function rehypeLiteralUnknownTags(tagNames) {+ const known = new Set(tagNames);+ return (tree) => {+ function walk(node: Root | Element): void {+ const children: RootContent[] = node.children;+ children.forEach((child, index) => {+ if (child.type === "element") {+ walk(child);+ return;+ }+ if (child.type !== "raw") return;+ const tag = HTML_TAG_NAME.exec(child.value)?.[1];+ if (!tag) return;+ if (known.has(tag.toLowerCase())) {+ child.value = child.value.replace(+ INNER_TAG,+ (match, slash, name) =>+ known.has(name.toLowerCase()) ? match : `<${slash}${name}`,+ );+ return;+ }+ // Streamdown's memoised components only re-render when the node position changes.+ const { position } = child;+ const text = { type: "text" as const, value: child.value, position };+ children[index] =+ node.type === "root"+ ? {+ type: "element",+ tagName: "p",+ properties: {},+ children: [text],+ position,+ }+ : text;+ });+ }+ walk(tree);+ };+ };++/** Keep data images, show disallowed tags as text, resolve sandbox paths before URL hardening. */export function withDataImageSupport(allowedTags: Record<string, string[]>,beforeHarden: Pluggable[] = [],@@ -28,13 +76,15 @@ export function withDataImageSupport(// same object, so spreading it in the same order reproduces that pipeline exactly. Naming the keys// would pin OUR order instead of theirs.const [raw, , harden] = Object.values(defaultRehypePlugins);+ const tagNames = [...(schema.tagNames ?? []), ...Object.keys(allowedTags)];return [+ [rehypeLiteralUnknownTags, tagNames],raw,[sanitizePlugin,{...schema,- tagNames: [...(schema.tagNames ?? []), ...Object.keys(allowedTags)],+ tagNames,attributes: { ...schema.attributes, ...allowedTags },protocols: {...schema.protocols,diff --git a/studio/frontend/tests/markdown-data-images.test.ts b/studio/frontend/tests/markdown-data-images.test.tsindex 3bb612e0609..34722e7fb07 100644--- a/studio/frontend/tests/markdown-data-images.test.ts+++ b/studio/frontend/tests/markdown-data-images.test.ts@@ -10,10 +10,10 @@ const TAG = "search-image";test("withDataImageSupport allows data: image sources through sanitize", () => {const pipeline = withDataImageSupport({ [TAG]: ["token"] });- assert.equal(pipeline.length, 3);- const [, harden] = pipeline[2] as [(...args: never[]) => unknown, Record<string, unknown>];+ assert.equal(pipeline.length, 4);+ const [, harden] = pipeline[3] as [(...args: never[]) => unknown, Record<string, unknown>];assert.equal(harden.allowDataImages, true, "harden stage must be the one that gates data images");- const [, schema] = pipeline[1] as [unknown, { tagNames: string[]; attributes: Record<string, string[]>; protocols: Record<string, string[]> }];+ const [, schema] = pipeline[2] as [unknown, { tagNames: string[]; attributes: Record<string, string[]>; protocols: Record<string, string[]> }];assert.ok(schema.protocols.src.includes("data"), "sanitize must let data: through so harden can decide");for (const scheme of ["http", "https"]) assert.ok(schema.protocols.src.includes(scheme));assert.ok(schema.tagNames.includes(TAG), "caller allowed tags survive the pipeline swap");@@ -24,7 +24,7 @@ test("withDataImageSupport carries Streamdown's own schema widening, not just de// The pipeline is derived from `defaultRehypePlugins.sanitize[1]`, which is Streamdown's OWN// schema (defaultSchema PLUS its own widenings), not bare hast-util-sanitize defaultSchema.// Re-deriving from defaultSchema would silently drop these and read as a no-op change.- const [, schema] = withDataImageSupport({ [TAG]: ["token"] })[1] as [+ const [, schema] = withDataImageSupport({ [TAG]: ["token"] })[2] as [unknown,{ protocols: Record<string, string[]>; attributes: Record<string, unknown> },];diff --git a/studio/frontend/tests/markdown-literal-tags.test.ts b/studio/frontend/tests/markdown-literal-tags.test.tsnew file mode 100644index 00000000000..8555c1d6055--- /dev/null+++ b/studio/frontend/tests/markdown-literal-tags.test.ts@@ -0,0 +1,155 @@+// 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 { withDataImageSupport } from "../src/lib/markdown-data-images.ts";++const ALLOWED_TAGS = { "search-image": ["token"] };++function render(markdown: string, mode: "static" | "streaming" = "static") {+ return renderToStaticMarkup(+ createElement(Streamdown, {+ mode,+ children: markdown,+ allowedTags: ALLOWED_TAGS,+ rehypePlugins: withDataImageSupport(ALLOWED_TAGS),+ }),+ );+}++function escaped(text: string) {+ return text+ .replaceAll("&", "&")+ .replaceAll("<", "<")+ .replaceAll(">", ">");+}++test("placeholders and generic types in prose stay visible", () => {+ for (const mode of ["static", "streaming"] as const) {+ for (const line of [+ "Replace <your-api-key> with the key from your dashboard.",+ "Usage: git clone <repo-url> <directory>",+ "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.",+ "For all 0<x and x>1 we have a bound.",+ "<your-api-key>",+ "Use Vec<T> and close </T> here.",+ " <your-api-key>",+ ]) {+ const html = render(line, mode);+ assert.ok(html.includes(`>${escaped(line)}<`), `${mode}: ${html}`);+ }+ }+ assert.match(+ render("Set the key:\n\n<your-api-key>\n\nThen restart."),+ /<p><your-api-key><\/p>/,+ );+ for (const [block, line] of [+ ["<div>Use <your-api-key> here</div>", "Use <your-api-key> here"],+ ["<div>Use <code_snippet> here</div>", "Use <code_snippet> here"],+ [+ "<div>Use <T>x</T> in Vec<Vec<T>> here</div>",+ "Use <T>x</T> in Vec<Vec<T>> here",+ ],+ [+ "<details>\n<summary>Setup</summary>\nReplace <your-api-key> in /home/<user>/.env\n</details>",+ "Replace <your-api-key> in /home/<user>/.env",+ ],+ ]) {+ const html = render(block);+ assert.ok(html.includes(escaped(line)), html);+ assert.doesNotMatch(html, /<(your-api-key|code_snippet)/);+ }+});++test("allowed HTML tags still render as elements", () => {+ assert.match(+ render("Press <kbd>Ctrl</kbd>+<kbd>C</kbd>."),+ /<kbd[^>]*>Ctrl<\/kbd>/,+ );+ assert.match(render("H<sub>2</sub>O"), /<sub[^>]*>2<\/sub>/);+ assert.match(render("Press <KBD>Ctrl</KBD>."), /<kbd[^>]*>Ctrl<\/kbd>/);+ assert.match(render("| a |\n| - |\n| x<br>y |"), /x<br[^>]*>y/);+ const details = render(+ "<details>\n<summary>More</summary>\n\nHidden\n\n</details>",+ );+ assert.match(details, /<details[^>]*>/);+ assert.match(details, /<summary[^>]*>More<\/summary>/);+ assert.match(+ render('<search-image token="t1"></search-image>'),+ /<search-image token="t1"/,+ );+});++test("hostile markup never renders as markup", () => {+ for (const markdown of [+ "a <script>alert(1)</script> b",+ "<ScRiPt\n>alert(1)</script >",+ "x <img src=x onerror=alert(1)> y",+ 'x <iframe src="javascript:alert(1)"></iframe> y',+ "<svg onload=alert(1)><circle r=5></circle></svg>",+ '<a href="javascript:alert(1)">x</a>',+ "<style>*{display:none}</style>",+ "<div><script>alert(1)</script><img src=x onerror=alert(1)><svg onload=alert(1)></div>",+ '<details open ontoggle=alert(1)><summary>s</summary><iframe srcdoc="<script>alert(1)</script>"></iframe></details>',+ "<custom-wrapper>\n<b>b</b> <img src=x onerror=alert(1)>\n</custom-wrapper>",+ '<p style="position:fixed;inset:0">overlay</p>',+ '<div>x <kbd style="position:fixed">k</kbd> <T></div>',+ ]) {+ for (const mode of ["static", "streaming"] as const) {+ const html = render(markdown, mode);+ assert.doesNotMatch(+ html,+ /<(script|iframe|svg|style|object|embed|form|math)\b/i,+ html,+ );+ assert.doesNotMatch(+ html,+ /<[a-z][^>]*\s(on[a-z]+|srcdoc|style)=|<[a-z][^>]*="\s*javascript:/i,+ html,+ );+ }+ }+});++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 position = {+ start: { line: 3, column: 1, offset: 9 },+ end: { line: 4, column: 20, offset: 43 },+ };+ const tree = {+ type: "root",+ children: [+ {+ type: "raw",+ value: "<your-api-key>\nwhere the key comes from",+ position,+ },+ {+ type: "element",+ tagName: "p",+ properties: {},+ children: [{ type: "raw", value: "<T>", position }],+ },+ ],+ };+ plugin(tagNames)(tree);+ const [block, paragraph] = tree.children as {+ position?: unknown;+ children: { type: string; position?: unknown }[];+ }[];+ assert.deepEqual(block.position, position);+ assert.deepEqual(block.children[0].position, position);+ assert.equal(paragraph.children[0].type, "text");+ assert.deepEqual(paragraph.children[0].position, position);+});