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: bound the code highlighter cache for tool cells, previews and READMEs (#10609)

Chaitanya Laxman8254d6e20d62
diff --git a/studio/frontend/src/components/assistant-ui/shared-code-plugin.ts b/studio/frontend/src/components/assistant-ui/shared-code-plugin.ts
new file mode 100644
index 00000000000..b791fbd97ba
--- /dev/null
+++ b/studio/frontend/src/components/assistant-ui/shared-code-plugin.ts
@@ -0,0 +1,8 @@
+// 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 { createCodePlugin } from "./code-plugin";
+
+// Replaces the `code` export of @streamdown/code, whose token cache never evicts. One instance so
+// these surfaces share a single set of Shiki highlighters, as they did with that export.
+export const codePlugin = createCodePlugin();
diff --git a/studio/frontend/src/components/assistant-ui/tool-code-cell.tsx b/studio/frontend/src/components/assistant-ui/tool-code-cell.tsx
index fdae32ab256..649b861d8f7 100644
--- a/studio/frontend/src/components/assistant-ui/tool-code-cell.tsx
+++ b/studio/frontend/src/components/assistant-ui/tool-code-cell.tsx
@@ -10,10 +10,10 @@ import { Tick02Icon } from "@/lib/tick-icon";
import { toast } from "@/lib/toast";
import { Copy01Icon, Download01Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
-import { code as codePlugin } from "@streamdown/code";
import { IconActionButton } from "./icon-action-button";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Streamdown } from "streamdown";
+import { codePlugin } from "./shared-code-plugin";
const COPY_RESET_MS = 2000;
const SHIKI_THEME = ["github-light", "github-dark"] as [
diff --git a/studio/frontend/src/components/markdown/markdown-preview.tsx b/studio/frontend/src/components/markdown/markdown-preview.tsx
index 539b0fc1584..8145238d2b8 100644
--- a/studio/frontend/src/components/markdown/markdown-preview.tsx
+++ b/studio/frontend/src/components/markdown/markdown-preview.tsx
@@ -7,7 +7,7 @@ import { openLink } from "@/lib/open-link";
import { safeMarkdownUrl } from "@/lib/safe-markdown-url";
import { scheduleIdleTask } from "@/lib/schedule-idle-task";
import { cn } from "@/lib/utils";
-import { code } from "@streamdown/code";
+import { codePlugin } from "@/components/assistant-ui/shared-code-plugin";
import { math } from "@streamdown/math";
import { mermaid } from "@streamdown/mermaid";
import {
@@ -70,7 +70,7 @@ function MarkdownPreviewImpl({
const plugins = useMemo<MarkdownPlugins>(() => {
const needs = markdownPluginNeeds(markdown);
const next: MarkdownPlugins = {};
- if (needs.code) next.code = code;
+ if (needs.code) next.code = codePlugin;
if (needs.math) next.math = math;
if (needs.mermaid) next.mermaid = mermaid;
return next;
diff --git a/studio/frontend/src/features/hub/catalog/model-readme.tsx b/studio/frontend/src/features/hub/catalog/model-readme.tsx
index 0d909fd55db..a3555df7976 100644
--- a/studio/frontend/src/features/hub/catalog/model-readme.tsx
+++ b/studio/frontend/src/features/hub/catalog/model-readme.tsx
@@ -16,7 +16,7 @@ import { scheduleIdleTask } from "@/lib/schedule-idle-task";
import { cn } from "@/lib/utils";
import { confirmExternalLink } from "../stores/external-link-confirm";
import { useHfTokenStore } from "@/features/hub/stores/hf-token-store";
-import { code as streamdownCode } from "@streamdown/code";
+import { codePlugin } from "@/components/assistant-ui/shared-code-plugin";
import { math as streamdownMath } from "@streamdown/math";
import { mermaid as streamdownMermaid } from "@streamdown/mermaid";
import {
@@ -340,7 +340,7 @@ async function loadReadmeFromCache({
}
function loadPlugins(needs: MarkdownPluginNeeds): ReadmePlugins {
- const plugins: ReadmePlugins = { code: streamdownCode };
+ const plugins: ReadmePlugins = { code: codePlugin };
if (needs.math) plugins.math = streamdownMath;
if (needs.mermaid) plugins.mermaid = streamdownMermaid;
return plugins;
diff --git a/studio/frontend/tests/markdown-preview-literal-tags.test.ts b/studio/frontend/tests/markdown-preview-literal-tags.test.ts
index 66f92b12f52..19194e87aea 100644
--- a/studio/frontend/tests/markdown-preview-literal-tags.test.ts
+++ b/studio/frontend/tests/markdown-preview-literal-tags.test.ts
@@ -7,12 +7,12 @@ 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 { createCodePlugin } from "../src/components/assistant-ui/code-plugin.ts";
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";
@@ -34,7 +34,9 @@ const { MarkdownPreview } = loadWithStubs<typeof MarkdownPreviewModule>(
new URL("../src/components/markdown/markdown-preview.tsx", import.meta.url),
{
...shared,
- "@streamdown/code": streamdownCode,
+ "@/components/assistant-ui/shared-code-plugin": {
+ codePlugin: createCodePlugin(),
+ },
"@streamdown/math": streamdownMath,
"@streamdown/mermaid": streamdownMermaid,
"@/lib/markdown-plugins": markdownPlugins,
diff --git a/studio/frontend/tests/streamdown-code-bounded.test.ts b/studio/frontend/tests/streamdown-code-bounded.test.ts
new file mode 100644
index 00000000000..85f944fcaf6
--- /dev/null
+++ b/studio/frontend/tests/streamdown-code-bounded.test.ts
@@ -0,0 +1,49 @@
+// 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 { readFileSync, readdirSync } from "node:fs";
+import { join, relative } from "node:path";
+import test from "node:test";
+import { fileURLToPath } from "node:url";
+
+const SRC = fileURLToPath(new URL("../src", import.meta.url));
+
+function sources(dir: string): string[] {
+ return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
+ const path = join(dir, entry.name);
+ if (entry.isDirectory()) return sources(path);
+ return /\.tsx?$/.test(entry.name) ? [path] : [];
+ });
+}
+
+// @streamdown/code 1.1.1 memoises every tokenisation in a module-level Map that never evicts
+// (unslothai/unsloth#9230); only its types may be imported.
+test("no source module loads @streamdown/code at runtime", () => {
+ const offenders = sources(SRC).filter((path) => {
+ const text = readFileSync(path, "utf8");
+ return (
+ /^import\s+(?!type\b)[^;]*?from\s+["']@streamdown\/code["']/m.test(
+ text,
+ ) || /import\(\s*["']@streamdown\/code["']\s*\)/.test(text)
+ );
+ });
+ assert.deepEqual(
+ offenders.map((path) => relative(SRC, path)),
+ [],
+ );
+});
+
+test("finished-code surfaces share the bounded plugin", () => {
+ for (const path of [
+ "components/assistant-ui/tool-code-cell.tsx",
+ "components/markdown/markdown-preview.tsx",
+ "features/hub/catalog/model-readme.tsx",
+ ]) {
+ assert.match(
+ readFileSync(join(SRC, path), "utf8"),
+ /import \{ codePlugin \} from "(?:\.|@\/components\/assistant-ui)\/shared-code-plugin";/,
+ path,
+ );
+ }
+});