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: skip the escaped-math parse when no escaped dollar pair can be math (#15808)
Daniel Han15b866a05c9c
diff --git a/studio/frontend/src/lib/escaped-inline-math.ts b/studio/frontend/src/lib/escaped-inline-math.tsindex 87857f1cf98..556cf8c01f9 100644--- a/studio/frontend/src/lib/escaped-inline-math.ts+++ b/studio/frontend/src/lib/escaped-inline-math.ts@@ -51,6 +51,8 @@ const HTML_LITERAL_TAG_START_RE = /^<\/?(code|pre|textarea)(?=[\s/>])/i;const CURRENCY_OPENER_RE =/(?<![\\$])\$(?!\$)(?=\d+(?:,\d{3})*(?:\.\d+)?[KMBkmb]?(?:\s|$|[^a-zA-Z\d]))/g;const DIGIT_RE = /\d/;+const LINE_BREAK_RE = /[\r\n]/;+const PAIR_BUDGET = 4096;const MASKED_DOLLAR = "$";const TEXT_COMMANDS = new Set(["emph",@@ -479,9 +481,53 @@ function escapedMathRanges(markdown: string): EscapedMathRange[] {return ranges.sort((left, right) => left.start - right.start);}+/** False only when no `\$` pair can tokenize as math; past PAIR_BUDGET pairs it defers to the parse. */+export function mayHoldEscapedInlineMath(markdown: string): boolean {+ const budget = { pairs: PAIR_BUDGET };+ for (+ let open = markdown.indexOf("\\$");+ open >= 0;+ open = markdown.indexOf("\\$", open + 1)+ ) {+ if (mayCloseEscapedMath(markdown, open, budget)) {+ return true;+ }+ }+ return false;+}++function mayCloseEscapedMath(+ markdown: string,+ open: number,+ budget: { pairs: number },+): boolean {+ for (+ let close = markdown.indexOf("\\$", open + 2);+ close >= 0 && close - open - 2 <= MAX_BODY_LENGTH;+ close = markdown.indexOf("\\$", close + 1)+ ) {+ budget.pairs -= 1;+ if (budget.pairs < 0) {+ return true;+ }+ const body = markdown.slice(open + 2, close);+ if (LINE_BREAK_RE.test(body)) {+ return false;+ }+ if (looksLikeEscapedInlineMath(body)) {+ return true;+ }+ // The tokenizer reads past a `\$` only while a brace is open; otherwise it ends here.+ if (braceBalance(body) <= 0) {+ return false;+ }+ }+ return false;+}+/** Runs before currency and streaming repair. */export function normalizeEscapedInlineMath(markdown: string): string {- if (!markdown.includes("\\$")) {+ if (!mayHoldEscapedInlineMath(markdown)) {return markdown;}const ranges = escapedMathRanges(markdown);diff --git a/studio/frontend/tests/escaped-inline-math.test.ts b/studio/frontend/tests/escaped-inline-math.test.tsindex 8b38a9d45c0..5e61a5fbedb 100644--- a/studio/frontend/tests/escaped-inline-math.test.ts+++ b/studio/frontend/tests/escaped-inline-math.test.ts@@ -10,6 +10,7 @@ import { Streamdown } from "streamdown";import { parseMarkdownIntoRenderableBlocks } from "../src/components/assistant-ui/streaming-render-schedule.ts";import {looksLikeEscapedInlineMath,+ mayHoldEscapedInlineMath,normalizeEscapedInlineMath,} from "../src/lib/escaped-inline-math.ts";import { preprocessLaTeX } from "../src/lib/latex.ts";@@ -283,3 +284,23 @@ test("incomplete code spans preserve escaped dollars while streaming", () => {}}});++test("escaped currency with no math pair skips the parse, every rewrite still runs it", () => {+ const currency = String.raw`Revenue grew from \$5.2M to \$7.9M, about \$1,200 a unit and \$3 more.`;+ for (let end = 0; end <= currency.length; end += 1) {+ const prefix = currency.slice(0, end);+ assert.equal(mayHoldEscapedInlineMath(prefix), false, prefix);+ assert.equal(normalizeEscapedInlineMath(prefix), prefix);+ }+ for (const markdown of [+ String.raw`- \$v_s\$ and \$f(r_s)\$`,+ String.raw`cost \$5 and \$x + y\$`,+ String.raw`\${a \$ b}\$`,+ String.raw`\$5 then \$v_s\$ and \$f(x)\$`,+ ]) {+ assert.notEqual(normalizeEscapedInlineMath(markdown), markdown, markdown);+ assert.equal(mayHoldEscapedInlineMath(markdown), true, markdown);+ }+ assert.equal(mayHoldEscapedInlineMath(`\\$${"x_".repeat(101)}\\$`), false);+ assert.equal(mayHoldEscapedInlineMath("\\$x\n\\$"), false);+});