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: render numbers wrapped in dollar signs as math, not prices (#13649)
Nilay525f3ceae3c3
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.tsindex 887c029d073..c24bea033bd 100644--- a/studio/frontend/scripts/check-bundle-budget.ts+++ b/studio/frontend/scripts/check-bundle-budget.ts@@ -102,8 +102,12 @@ export const BUDGET = {// gave back 0.4 KB raw). Raised by that growth so main's margin stays where it was.// Then the Audio API card in Settings (#12821) 2,019 B raw / 557 B transfer and #13630 132 B raw// (6,750,599 -> 6,752,750 B raw, 2,028,544 -> 2,029,099 B transfer to dd56fb3572, same machine and build).- transferBytes: 2_029_466,- rawBytes: 6_753_766,+ // Numbers wrapped tightly in dollars render as math (#13649): the shell-context checks that keep+ // `$1$?`-style shell text literal add 6,984 B raw / 2,836 B transfer to the eager chat chunk+ // (6,753,387 -> 6,760,371 B raw, 2,029,398 -> 2,032,234 B transfer against eba7641835, one machine+ // and build, chunk count unchanged at 95), raised by that.+ transferBytes: 2_032_302,+ rawBytes: 6_760_750,};// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering thediff --git a/studio/frontend/src/lib/latex.ts b/studio/frontend/src/lib/latex.tsindex e4eea83f29f..059eebc585a 100644--- a/studio/frontend/src/lib/latex.ts+++ b/studio/frontend/src/lib/latex.ts@@ -53,6 +53,611 @@ function startsLikeVariable(text: string, at: number): boolean {if (a === 95) return isWordChar(b);return a === 123 && (isAsciiLetter(b) || b === 95);}++const MAX_SHELL_EXPANSION_SPAN = 4096;+const MAX_MARKDOWN_EMPHASIS_SPAN = 256;+const SHELL_QUOTE_CONTEXT_CANDIDATE_RE =+ /\$(?:["'!(*\-?_]|\[)|\$\d+\$[A-Za-z0-9_{$]/;+const SHELL_COMMANDS = new Set([+ "awk",+ "bash",+ "builtin",+ "cat",+ "command",+ "dash",+ "echo",+ "env",+ "eval",+ "exec",+ "export",+ "find",+ "fish",+ "git",+ "grep",+ "ksh",+ "kubectl",+ "printf",+ "read",+ "sed",+ "set",+ "sh",+ "source",+ "sudo",+ "test",+ "unset",+ "xargs",+ "zsh",+]);+const SHELL_LEADIN_RE = /^(?:Run|Use|Try|Execute)$/i;+const PROSE_COMMAND_RE =+ /^(?:a|an|consider|imagine|note|suppose|the|this|that)$/i;+const SHELL_WORD_RE = /^(?:[a-z_][\w.-]*|\.{0,2}\/[^\s]+)$/;+const SHELL_POSITIONAL_WORD_RE = /^[A-Za-z0-9_][\w.-]*$/;+const SHELL_ARGUMENT_RE =+ /^(?!(?:\d+\.)+\d+$)(?:-{1,2}[\w-]+|[\w.-]+=\S*|\.{0,2}\/\S+|\S*[./*?[\]{}]\S*)$/;+const MARKDOWN_CONTAINER_PREFIX_RE =+ /^(?:(?: {0,3}>[ \t]?)|(?: {0,3}(?:[-+*]|\d{1,9}[.)])[ \t]+(?:\[[ xX]\][ \t]+)?))+/;+const SHELL_PROMPT_PREFIX_RE = /^\$[ \t]+/;+const SPACED_MATH_CLOSER_RE = /[ \t]\$(?![\w${(])|[^\\$\s]\$(?=[\s.,;:)\]*]|$)/;+const BLANK_LINE_RE = /\r?\n[ \t]*\r?\n|\r[ \t]*\r/;+const MARKDOWN_TABLE_PREFIX_RE = /^[ \t]*\|/;+const SHELL_ASSIGNMENT_PREFIX_RE = /(?:^|[;&|]\s*)[A-Za-z_]\w*=$/;+const SHELL_ASSIGNMENT_WORD_RE = /^[A-Za-z_]\w*=\S*$/;+const SHELL_OPERATOR_PREFIX_RE = /\S\s*[;&|]{1,2}$/;+const SHELL_QUOTE_BLOCK_START_RE =+ /^(?: {0,3}(?:#{1,6}(?=[ \t])|[-+*][ \t]+|\d{1,9}[.)][ \t]+)|[ \t]*\|| {0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,}|=+[ \t]*)$)/;++function hasCommandArguments(words: string[], at: number): boolean {+ if (at >= words.length) return false;+ let hasShellShapedArgument = false;+ for (let index = at; index < words.length; index++) {+ const word = words[index];+ if (SHELL_ARGUMENT_RE.test(word)) {+ hasShellShapedArgument = true;+ } else if (!SHELL_POSITIONAL_WORD_RE.test(word)) {+ return false;+ }+ }+ return hasShellShapedArgument;+}++function isShellWordBoundary(char: number): boolean {+ return (+ char <= 32 ||+ char === 34 ||+ char === 36 ||+ char === 38 ||+ char === 39 ||+ char === 40 ||+ char === 41 ||+ char === 59 ||+ char === 60 ||+ char === 61 ||+ char === 62 ||+ char === 92 ||+ char === 124+ );+}++function logicalShellLineStart(+ text: string,+ at: number,+ minimum: number,+): number {+ let start = at;+ while (start > minimum) {+ while (+ start > minimum &&+ text.charCodeAt(start - 1) !== 10 &&+ text.charCodeAt(start - 1) !== 13+ ) {+ start--;+ }+ if (start === minimum) return start;+ let newlineStart = start - 1;+ if (+ text.charCodeAt(newlineStart) === 10 &&+ text.charCodeAt(newlineStart - 1) === 13+ ) {+ newlineStart--;+ }+ let slash = newlineStart;+ while (slash > minimum && text.charCodeAt(slash - 1) === 92) slash--;+ if ((newlineStart - slash) % 2 === 0) return start;+ start = newlineStart;+ }+ return start;+}++function lastShellControlOperatorEnd(prefix: string): number {+ let quote = 0;+ let last = 0;+ for (let index = 0; index < prefix.length; index++) {+ const char = prefix.charCodeAt(index);+ if (quote !== 0) {+ if (char === 92 && quote !== 39) index++;+ else if (char === quote) quote = 0;+ continue;+ }+ if (char === 34 || char === 39) {+ quote = char;+ } else if (char === 92) {+ index++;+ } else if (char === 38 || char === 59 || char === 124) {+ if (prefix.charCodeAt(index + 1) === char) index++;+ last = index + 1;+ }+ }+ return last;+}++function isShellPromptMarker(text: string, at: number): boolean {+ const next = text.charCodeAt(at + 1);+ if (next !== 9 && next !== 32) return false;+ const lineStart =+ Math.max(text.lastIndexOf("\n", at - 1), text.lastIndexOf("\r", at - 1)) ++ 1;+ if (+ text.slice(lineStart, at).replace(MARKDOWN_CONTAINER_PREFIX_RE, "").trim()+ ) {+ return false;+ }+ // spaced inline math (`$ x = 3 $`) closes within its paragraph; a prompt does not.+ // A `$` opening a later line is the next prompt, so the closer pattern skips it.+ const rest = text.slice(at + 1, at + 1 + MAX_SHELL_EXPANSION_SPAN);+ const blank = BLANK_LINE_RE.exec(rest);+ return !SPACED_MATH_CLOSER_RE.test(blank ? rest.slice(0, blank.index) : rest);+}++function hasShellCommandPrefix(+ text: string,+ at: number,+ allowBareCommand = false,+): boolean {+ const previousChar = text.charCodeAt(at - 1);+ if (+ !allowBareCommand &&+ at > 0 &&+ previousChar > 32 &&+ previousChar !== 38 &&+ previousChar !== 59 &&+ previousChar !== 61 &&+ previousChar !== 124+ ) {+ if (isShellWordBoundary(previousChar)) return false;+ const minimum = Math.max(0, at - 256);+ let wordStart = at;+ while (wordStart > minimum) {+ const char = text.charCodeAt(wordStart - 1);+ if (isShellWordBoundary(char)) break;+ wordStart--;+ }+ if (wordStart === at - 1 && text.charCodeAt(wordStart - 1) === 36) {+ return false;+ }+ return (+ wordStart > minimum &&+ wordStart < at &&+ hasShellCommandPrefix(text, wordStart, true)+ );+ }+ const minimum = Math.max(0, at - 256);+ const lineStart = logicalShellLineStart(text, at, minimum);+ const completeLinePrefix =+ lineStart === 0 ||+ text.charCodeAt(lineStart - 1) === 10 ||+ text.charCodeAt(lineStart - 1) === 13;+ const rawPrefix = text+ .slice(lineStart, at)+ .replace(/\\(?:\r\n|\n|\r)[ \t]*/g, " ")+ .trimEnd();+ const prefix = rawPrefix+ .replace(MARKDOWN_CONTAINER_PREFIX_RE, "")+ .replace(SHELL_PROMPT_PREFIX_RE, "");+ if (!prefix) return false;+ if (+ MARKDOWN_TABLE_PREFIX_RE.test(rawPrefix) ||+ MARKDOWN_TABLE_PREFIX_RE.test(prefix)+ )+ return false;+ if (+ SHELL_ASSIGNMENT_PREFIX_RE.test(prefix) ||+ (!MARKDOWN_TABLE_PREFIX_RE.test(rawPrefix) &&+ SHELL_OPERATOR_PREFIX_RE.test(prefix))+ ) {+ return true;+ }+ if (/\b(?:shell|bash|command) examples?\b[^\n.]*$/i.test(prefix)) return true;+ const commandStart = lastShellControlOperatorEnd(prefix);+ const commandPrefix = prefix.slice(commandStart).trimStart();+ const words = commandPrefix.match(/[^\s]+/g) ?? [];+ let commandIndex = 0;+ while (+ commandIndex < words.length - 1 &&+ SHELL_LEADIN_RE.test(words[commandIndex])+ ) {+ commandIndex++;+ }+ while (+ commandIndex < words.length - 1 &&+ SHELL_ASSIGNMENT_WORD_RE.test(words[commandIndex])+ ) {+ commandIndex++;+ }+ const command = words[commandIndex] ?? "";+ if (SHELL_COMMANDS.has(command) || /^\.{0,2}\//.test(command)) return true;+ if (+ allowBareCommand &&+ completeLinePrefix &&+ command.length >= 2 &&+ !PROSE_COMMAND_RE.test(command) &&+ SHELL_WORD_RE.test(command) &&+ (commandStart > 0 || hasCommandArguments(words, commandIndex + 1))+ ) {+ return true;+ }+ return false;+}++function hasShellParameterContext(+ text: string,+ at: number,+ numericOffset: number,+ quoteState: number,+): boolean {+ if (quoteState > 0 || hasShellCommandPrefix(text, numericOffset)) return true;+ if (at !== numericOffset + 3) return false;+ const lineStart =+ Math.max(+ text.lastIndexOf("\n", numericOffset - 1),+ text.lastIndexOf("\r", numericOffset - 1),+ ) + 1;+ const prefix = text+ .slice(lineStart, numericOffset)+ .trimEnd()+ .replace(MARKDOWN_CONTAINER_PREFIX_RE, "")+ .replace(SHELL_PROMPT_PREFIX_RE, "");+ return SHELL_LEADIN_RE.test(prefix);+}++function startsNewMarkdownBlock(text: string, at: number): boolean {+ const char = text.charCodeAt(at);+ if (at >= text.length || char === 10 || char === 13) return true;+ const nextLf = text.indexOf("\n", at);+ const nextCr = text.indexOf("\r", at);+ const nextEnd =+ nextLf === -1 ? nextCr : nextCr === -1 ? nextLf : Math.min(nextLf, nextCr);+ const nextLine = text.slice(at, nextEnd === -1 ? text.length : nextEnd);+ const nextQuotes = quoteDepth(nextLine);+ if (nextQuotes === 0) return SHELL_QUOTE_BLOCK_START_RE.test(nextLine);++ let previousEnd = at - 1;+ if (+ text.charCodeAt(previousEnd) === 10 &&+ text.charCodeAt(previousEnd - 1) === 13+ ) {+ previousEnd--;+ }+ const previousStart =+ Math.max(+ text.lastIndexOf("\n", previousEnd - 1),+ text.lastIndexOf("\r", previousEnd - 1),+ ) + 1;+ const previousLine = text.slice(previousStart, previousEnd);+ if (quoteDepth(previousLine) !== nextQuotes) return true;+ const content = containerContent(nextLine, EMPTY_LIST_STATE, nextQuotes);+ return !content.trim() || SHELL_QUOTE_BLOCK_START_RE.test(content);+}++function findShellQuoteStates(+ text: string,+ resetRegions: ReadonlyArray<readonly [number, number]>,+): Map<number, number> {+ const states = new Map<number, number>();+ let quote = 0;+ let comment = false;+ let resetIndex = 0;+ for (let index = 0; index < text.length; index++) {+ while (+ resetIndex < resetRegions.length &&+ resetRegions[resetIndex][1] <= index+ ) {+ resetIndex++;+ }+ const resetRegion = resetRegions[resetIndex];+ if (resetRegion && index >= resetRegion[0] && index < resetRegion[1]) {+ quote = 0;+ index = resetRegion[1] - 1;+ continue;+ }+ const char = text.charCodeAt(index);+ if (comment) {+ if (char === 10 || char === 13) {+ comment = false;+ } else {+ if (char === 36) states.set(index, 34);+ continue;+ }+ }+ if (+ (char === 10 || (char === 13 && text.charCodeAt(index + 1) !== 10)) &&+ quote !== 0 &&+ startsNewMarkdownBlock(text, index + 1)+ ) {+ quote = 0;+ continue;+ }+ const activeQuote = Math.abs(quote);+ if (char === 36) states.set(index, quote);+ if (+ quote === 0 &&+ char === 35 &&+ (index === 0 || text.charCodeAt(index - 1) <= 32) &&+ hasShellCommandPrefix(text, index)+ ) {+ comment = true;+ continue;+ }+ if (char === 92 && activeQuote !== 39) {+ index++;+ } else if (quote === 0 && (char === 34 || char === 39)) {+ const shellQuote = hasShellCommandPrefix(text, index, true);+ if (+ char === 39 &&+ isWordChar(text.charCodeAt(index - 1)) &&+ !shellQuote+ ) {+ continue;+ }+ quote = shellQuote ? char : -char;+ } else if (char === activeQuote) {+ quote = 0;+ }+ }+ return states;+}++function startsMarkdownEmphasis(+ text: string,+ at: number,+ marker: number,+): boolean {+ const width = text.charCodeAt(at + 1) === marker ? 2 : 1;+ const bodyStart = at + width;+ const first = text.charCodeAt(bodyStart);+ if (first <= 32) return false;+ const token = width === 2 ? text.slice(at, at + 2) : text[at];+ const nextLf = text.indexOf("\n", bodyStart);+ const nextCr = text.indexOf("\r", bodyStart);+ const newline =+ nextLf === -1 ? nextCr : nextCr === -1 ? nextLf : Math.min(nextLf, nextCr);+ const limit = Math.min(+ text.length,+ at + MAX_MARKDOWN_EMPHASIS_SPAN,+ newline === -1 ? text.length : newline,+ );+ const close = text.indexOf(token, bodyStart + 1);+ return close > bodyStart && close < limit && text.charCodeAt(close - 1) > 32;+}++function enclosingMarkdownEmphasisOpener(+ text: string,+ at: number,+ before: number,+ marker: number,+): number {+ const width = text.charCodeAt(at + 1) === marker ? 2 : 1;+ const token = width === 2 ? text.slice(at, at + 2) : text[at];+ const lineStart =+ Math.max(text.lastIndexOf("\n", at - 1), text.lastIndexOf("\r", at - 1)) ++ 1;+ const minimum = Math.max(lineStart, at - MAX_MARKDOWN_EMPHASIS_SPAN);+ let opener = text.lastIndexOf(token, before - 1);+ while (opener >= minimum) {+ const bodyStart = opener + width;+ if (+ bodyStart <= before &&+ startsMarkdownEmphasis(text, opener, marker) &&+ text.indexOf(token, bodyStart + 1) === at+ ) {+ return opener;+ }+ // lastIndexOf clamps a negative start to 0, which would return the same opener forever.+ if (opener === 0) break;+ opener = text.lastIndexOf(token, opener - 1);+ }+ return -1;+}++function findLegacyArithmeticCloses(text: string): Int32Array {+ const closes = new Int32Array(text.length);+ let nextClose = -1;+ for (let index = text.length - 1; index >= 0; index--) {+ const char = text.charCodeAt(index);+ if (char === 93) nextClose = index;+ else if (char === 91 && text.charCodeAt(index - 1) === 36) {+ closes[index] = nextClose + 1;+ }+ }+ return closes;+}++function startsLegacyArithmetic(+ text: string,+ at: number,+ numericOffset: number,+ quoteState: number,+ closes: Int32Array | undefined,+ isStreaming: boolean,+): boolean {+ const limit = Math.min(text.length, at + MAX_SHELL_EXPANSION_SPAN);+ const close = (closes?.[at] ?? 0) - 1;+ if (close === -1) {+ return (+ isStreaming &&+ quoteState >= 0 &&+ (quoteState > 0 || hasShellCommandPrefix(text, numericOffset))+ );+ }+ if (close >= limit || text[at + 1] === "^") return false;+ if (text[close + 1] === "(" || text[close + 1] === "[") return false;+ if (quoteState < 0) return false;+ return quoteState > 0 || hasShellCommandPrefix(text, numericOffset);+}++/** valid Bash expansion starts after `$`, disambiguated from prose punctuation and closing quotes. */+function startsShellExpansion(+ text: string,+ at: number,+ numericOffset: number,+ quoteState: number,+ legacyArithmeticCloses: Int32Array | undefined,+ isStreaming: boolean,+): boolean {+ if (at >= text.length) return false;+ const char = text.charCodeAt(at);+ const quote = Math.abs(quoteState);+ if (char === 34 || char === 39) {+ return (+ quote !== char &&+ (quoteState > 0 || hasShellCommandPrefix(text, numericOffset))+ );+ }+ if (char === 33 || char === 45 || char === 63) {+ return quoteState > 0 || hasShellCommandPrefix(text, numericOffset);+ }+ if (char === 91)+ return startsLegacyArithmetic(+ text,+ at,+ numericOffset,+ quoteState,+ legacyArithmeticCloses,+ isStreaming,+ );+ if (char === 42 || char === 95) {+ const emphasisOpener = enclosingMarkdownEmphasisOpener(+ text,+ at,+ numericOffset,+ char,+ );+ if (+ emphasisOpener !== -1 &&+ quoteState <= 0 &&+ !hasShellCommandPrefix(text, emphasisOpener)+ ) {+ return false;+ }+ }+ if (+ (char === 42 || char === 95) &&+ startsMarkdownEmphasis(text, at, char) &&+ quoteState <= 0 &&+ !hasShellCommandPrefix(text, numericOffset)+ ) {+ return false;+ }+ if (char === 42) return true;+ if (char === 64 || char === 35) return true;+ if (char === 40) {+ return quoteState > 0 || hasShellCommandPrefix(text, numericOffset);+ }+ return (+ (isWordChar(char) || char === 123 || char === 36) &&+ hasShellParameterContext(text, at, numericOffset, quoteState)+ );+}++function numericShellParameterStart(text: string, at: number): number {+ let start = at;+ while (start > 0) {+ const char = text.charCodeAt(start - 1);+ if (char < 48 || char > 57) break;+ start--;+ }+ return start < at &&+ text[start - 1] === "$" &&+ text[start - 2] !== "\\" &&+ text[start - 2] !== "$"+ ? start - 1+ : -1;+}++function shellExpansionFallbackEnd(+ text: string,+ at: number,+ limit: number,+): number {+ const nextLf = text.indexOf("\n", at + 2);+ const nextCr = text.indexOf("\r", at + 2);+ const lineEnd =+ nextLf === -1 ? nextCr : nextCr === -1 ? nextLf : Math.min(nextLf, nextCr);+ return lineEnd !== -1 && lineEnd < limit ? lineEnd : limit;+}++function balancedShellExpansionEnd(+ text: string,+ at: number,+ open: number,+ close: number,+): number {+ let depth = 1;+ let quote = 0;+ const limit = Math.min(text.length, at + MAX_SHELL_EXPANSION_SPAN);+ for (let index = at + 2; index < limit; index++) {+ const char = text.charCodeAt(index);+ if (quote !== 0) {+ if (char === 92 && quote !== 39) index++;+ else if (char === quote) quote = 0;+ continue;+ }+ if (char === 34 || char === 39 || char === 96) {+ quote = char;+ continue;+ }+ if (char === 92) {+ index++;+ continue;+ }+ if (char === open) depth++;+ else if (char === close && --depth === 0) return index + 1;+ }+ return shellExpansionFallbackEnd(text, at, limit);+}++function quotedShellExpansionEnd(+ text: string,+ at: number,+ quote: number,+): number {+ const limit = Math.min(text.length, at + MAX_SHELL_EXPANSION_SPAN);+ for (let index = at + 2; index < limit; index++) {+ const char = text.charCodeAt(index);+ if (char === 92) index++;+ else if (char === quote) return index + 1;+ }+ return shellExpansionFallbackEnd(text, at, limit);+}++function shellExpansionEnd(text: string, at: number): number {+ const first = text.charCodeAt(at + 1);+ if (first === 123) return balancedShellExpansionEnd(text, at, 123, 125);+ if (first === 40) return balancedShellExpansionEnd(text, at, 40, 41);+ if (first === 91) return balancedShellExpansionEnd(text, at, 91, 93);+ if (first === 34 || first === 39)+ return quotedShellExpansionEnd(text, at, first);+ if (!isAsciiLetter(first) && first !== 95) return at + 2;+ let end = at + 2;+ while (end < text.length && isWordChar(text.charCodeAt(end))) end++;+ return end;+}++function encodeMarkdownLiteral(text: string): string {+ let encoded = "";+ for (const char of text) encoded += `&#${char.codePointAt(0)};`;+ return encoded;+}// an entity stays literal in Markdown without showing an escape slash in raw HTML.const VARIABLE_DOLLAR = "$";@@ -451,16 +1056,14 @@ export function isInRegion(return false;}-/** A whitespace-free token that looks purely like currency, e.g. `5`, `1,000`, `5.99`, `100K`, `3.5M`. */const CURRENCY_BODY_RE = /^\d+(?:,\d{3})*(?:\.\d+)?[KMBkmb]?$/;-/** Body characters that almost always indicate real LaTeX. */+const BARE_NUMBER_RE = /^\d+(?:,\d{3})*(?:\.\d+)?$/;++/** these characters almost always indicate real LaTeX. */const LATEX_CHAR_RE = /[\\^_{}]/;-/**- * Operators that strongly suggest math. Omits `^` and `_` since- * `LATEX_CHAR_RE` short-circuits on those before this regex runs.- */+/** math operators excluding `^` and `_`, which LATEX_CHAR_RE checks first. */const MATH_OP_RE = /[=+\-<>/*]/;/**@@ -509,18 +1112,14 @@ function looksLikeMathBody(body: string): boolean {return LONE_LETTER_RE.test(trimmed);}-/**- * True if the `$` at `offset` opens a balanced inline math span (`$...$`)- * on the same line. The closer must be unescaped, not part of `$$`, and- * within 200 chars. The body must look like LaTeX so we don't pair two- * currency tokens on a line (e.g. "$5 to $10"). Bold-wrapped spans- * (`**$X$**`, `__$X$__`) are always math: LLMs use that for "bold math"- * and the heuristic would otherwise reject prose-shaped bodies like "90 - x".- */+/** accepts bold-wrapped spans to preserve prose-shaped math and bare numbers unless the closer starts a shell parameter. */function hasInlineMathCloser(content: string,offset: number,mathRegions: Array<[number, number]>,+ shellQuoteStates: Map<number, number>,+ legacyArithmeticCloses: Int32Array | undefined,+ isStreaming: boolean,): boolean {const maxSpan = 200;const limit = Math.min(content.length, offset + 1 + maxSpan);@@ -529,19 +1128,31 @@ function hasInlineMathCloser(if (c === "\n") return false;if (c !== "$") continue;if (content[i - 1] === "\\") continue;- // A `$` opening a generated span (from `\(...\)`) is not a currency closer;- // pairing with it would swallow the price into math (`$5 + x \(y\)`).+ // generated `\(...\)` openers cannot close currency spans because they would consume the price as math.if (isInRegion(i, mathRegions)) return false;if (content[i + 1] === "$") {i++;continue;}- // A `$` followed by a digit is more likely another currency token than- // the closer. Keep scanning so prose like `$5 + a $10 add-on` doesn't- // pair the two currency markers as a math span.+ // a digit after `$` likely starts another currency token, so keep scanning.if (/\d/.test(content[i + 1] ?? "")) {continue;}+ const body = content.slice(offset + 1, i);+ if (BARE_NUMBER_RE.test(body)) {+ if (isStreaming && i + 1 === content.length) return false;+ if (+ !startsShellExpansion(+ content,+ i + 1,+ offset,+ shellQuoteStates.get(offset) ?? 0,+ legacyArithmeticCloses,+ isStreaming,+ )+ )+ return true;+ }if (offset >= 2) {const op = content[offset - 1];if (@@ -553,7 +1164,7 @@ function hasInlineMathCloser(return true;}}- return looksLikeMathBody(content.slice(offset + 1, i));+ return looksLikeMathBody(body);}return false;}@@ -710,6 +1321,12 @@ export function preprocessLaTeX(content: string, isStreaming = false): string {: [],);const rawTextRegions = findRawTextRegions(text);+ const shellQuoteStates = SHELL_QUOTE_CONTEXT_CANDIDATE_RE.test(text)+ ? findShellQuoteStates(text, mergeRegions(codeRegions, rawTextRegions))+ : new Map<number, number>();+ const legacyArithmeticCloses = text.includes("$[")+ ? findLegacyArithmeticCloses(text)+ : undefined;const inCode = regionCursor(codeRegions);const inLink = regionCursor(linkRegions);const inMath = regionCursor(mathRegions);@@ -724,17 +1341,31 @@ export function preprocessLaTeX(content: string, isStreaming = false): string {const code = inCode(offset);const link = inLink(offset);const math = inMath(offset);- if (code || link) {+ const rawText = inRawText(offset);+ if (code || link || rawText) {return "$";}// preserve converted numeric math to avoid currency re-escapingif (math) {return "$";}+ if (isShellPromptMarker(text, offset)) {+ return "\\$";+ }const digit = text.charCodeAt(offset + 1) - 48;CURRENCY_REGEX.lastIndex = offset + 1;const currency = digit >= 0 && digit <= 9 && CURRENCY_REGEX.test(text);- if (currency && !hasInlineMathCloser(text, offset, mathRegions)) {+ if (+ currency &&+ !hasInlineMathCloser(+ text,+ offset,+ mathRegions,+ shellQuoteStates,+ legacyArithmeticCloses,+ isStreaming,+ )+ ) {return "\\$";}if (offset === closer) {@@ -765,7 +1396,6 @@ export function preprocessLaTeX(content: string, isStreaming = false): string {? isStreaming: NEW_TOKEN_RE.test(text[next + 1])) &&next - offset - 1 <= MAX_VARIABLE_PROSE_SPAN &&- !inRawText(offset) &&(proseBudget -= next - offset - 1) >= 0 &&VARIABLE_PROSE_RE.test(text.slice(offset + 1, next))) {@@ -785,10 +1415,49 @@ export function preprocessLaTeX(content: string, isStreaming = false): string {offset = text.indexOf("$", offset + 1)) {const prev = text[offset - 1];+ if (offset === closer) continue;+ const numericOffset = numericShellParameterStart(text, offset);+ if (+ numericOffset !== -1 &&+ startsShellExpansion(+ text,+ offset + 1,+ numericOffset,+ shellQuoteStates.get(numericOffset) ?? 0,+ legacyArithmeticCloses,+ isStreaming,+ ) &&+ !inCode(offset) &&+ !inLink(offset) &&+ !inMath(offset) &&+ !inRawText(offset)+ ) {+ const end = shellExpansionEnd(text, offset);+ parts.push(+ text.slice(last, offset),+ encodeMarkdownLiteral(text.slice(offset, end)),+ );+ last = end;+ offset = end - 1;+ continue;+ }if (prev === "\\" || prev === "$" || text[offset + 1] === "$") continue;const out = rewrite(offset);if (out !== "$") {- parts.push(text.slice(last, offset), out);+ const marker = text.charCodeAt(offset - 1);+ if (+ out === "\\$" &&+ (marker === 42 || marker === 95) &&+ hasShellCommandPrefix(text, offset - 1)+ ) {+ parts.push(+ text.slice(last, offset - 1),+ encodeMarkdownLiteral(text[offset - 1]),+ out,+ );+ } else {+ parts.push(text.slice(last, offset), out);+ }last = offset + 1;}}diff --git a/studio/frontend/tests/inline-math-bare-numbers.test.ts b/studio/frontend/tests/inline-math-bare-numbers.test.tsnew file mode 100644index 00000000000..6f9ba5b6f39--- /dev/null+++ b/studio/frontend/tests/inline-math-bare-numbers.test.ts@@ -0,0 +1,366 @@+// 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 { createMathPlugin } from "@streamdown/math";+import React from "react";+import { renderToStaticMarkup } from "react-dom/server";+import { Streamdown } from "streamdown";+import { stabilizeStreamingMarkdown } from "../src/components/assistant-ui/streaming-markdown.ts";+import { normalizeEscapedInlineMath } from "../src/lib/escaped-inline-math.ts";+import { preprocessLaTeX } from "../src/lib/latex.ts";++const math = createMathPlugin({ singleDollarTextMath: true });++function render(source: string, isStreaming = false): string {+ return renderToStaticMarkup(+ React.createElement(+ Streamdown,+ { mode: "streaming", isAnimating: false, plugins: { math } },+ stabilizeStreamingMarkdown(+ preprocessLaTeX(normalizeEscapedInlineMath(source), isStreaming),+ isStreaming,+ ),+ ),+ );+}++function renderedMath(html: string): string[] {+ return [+ ...html.matchAll(+ /<annotation encoding="application\/x-tex">([\s\S]*?)<\/annotation>/g,+ ),+ ].map((match) => match[1]);+}++function visibleText(html: string): string {+ return html+ .replace(/<span class="katex">[\s\S]*?<\/annotation>/g, "")+ .replace(/<[^>]+>/g, "")+ .replaceAll("'", "'")+ .replaceAll("'", "'")+ .replaceAll(""", '"');+}++test("numbers written as inline maths render as maths", () => {+ const cases: Array<[string, string[]]> = [+ ["There are $3$ red balls and $5$ blue balls.", ["3", "5"]],+ ["The answer is $42$.", ["42"]],+ ["$$42$$", ["42"]],+ ["The answer is $42$", ["42"]],+ ["The answer is $42$!", ["42"]],+ ["Is it $3$?", ["3"]],+ ["It's $3$!", ["3"]],+ ["Users' answer is $3$!", ["3"]],+ ['He said "The answer is $42$!"', ["42"]],+ ['He said ("The answer is $42$!")', ["42"]],+ ['She asked "Is it $3$?"', ["3"]],+ ['the answer is "$42$!"', ["42"]],+ ['consider this "$3$?"', ["3"]],+ ['- please note "The answer is $42$!"', ["42"]],+ ['- chapter 1.2 notes "The answer is $42$!"', ["42"]],+ ["The mass is $42$kg.", ["42"]],+ ["a $10$-fold increase", ["10"]],+ ["Range $1$-$2$", ["1", "2"]],+ ["The number $42$'s factors are 2, 3, and 7.", ["42"]],+ ["There are $3$*red* balls.", ["3"]],+ ["There are $3$_red_ balls.", ["3"]],+ ["There are $3$**red** balls.", ["3"]],+ ["There are $3$__red__ balls.", ["3"]],+ ["*$3$*", ["3"]],+ ["_$3$_", ["3"]],+ ["*There are $3$*", ["3"]],+ ["_There are $3$_", ["3"]],+ ["**There are $3$**", ["3"]],+ ["__There are $3$__", ["3"]],+ ["| value |\n| --- |\n| $42$! |", ["42"]],+ ["| value |\n| --- |\n| $10$-fold |", ["10"]],+ ["| case | value |\n| --- | --- |\n| answer | $42$! |", ["42"]],+ ["answer = $42$!", ["42"]],+ ['- consider "Is it $3$?"', ["3"]],+ ["The result is $42$(the answer).", ["42"]],+ ["The answer is $42$[source](https://example.com).", ["42"]],+ ["The answer is $42$[^1].\n\n[^1]: Source.", ["42"]],+ ['He said "The answer is $42$[source](https://example.com)."', ["42"]],+ [+ 'Reference: "$42$[source](https://example.com)." Then he said "The answer is $42$!"',+ ["42", "42"],+ ],+ ['He said "$42$[^1]."\n\n[^1]: Source.', ["42"]],+ ["The answer is $42$[x+1][ref].\n\n[ref]: https://example.com", ["42"]],+ [+ "The answer is $42$[source-link].\n\n[source-link]: https://example.com",+ ["42"],+ ],+ ["Step 2: $8$ times $2$ is $16$.", ["8", "2", "16"]],+ [+ "Janet has $16$ eggs. She eats $3$ and bakes with $4$, so $16 - 3 - 4 = 9$ remain.",+ ["16", "3", "4", "16 - 3 - 4 = 9"],+ ],+ ["About $1,000$ rows and $0.5$ of them.", ["1,000", "0.5"]],+ ];+ for (const [source, expected] of cases) {+ const html = render(source);+ assert.deepEqual(renderedMath(html), expected, source);+ assert.ok(!visibleText(html).includes("$"), `${source} showed a dollar`);+ }+ assert.match(render("There are $3$*red* balls."), /<em>red<\/em>/);+ assert.match(render("There are $3$_red_ balls."), /<em>red<\/em>/);+ assert.match(+ render("There are $3$**red** balls."),+ /data-streamdown="strong">red<\/span>/,+ );+ assert.match(+ render("There are $3$__red__ balls."),+ /data-streamdown="strong">red<\/span>/,+ );+ assert.match(render("*$3$*"), /<em><span class="katex">/);+ assert.match(render("_$3$_"), /<em><span class="katex">/);+ assert.match(render("*There are $3$*"), /<em>There are <span class="katex">/);+ assert.match(+ render("**There are $3$**"),+ /data-streamdown="strong">There are <span class="katex">/,+ );+});++test("currency next to numbers stays text", () => {+ for (const source of [+ "Prices run from $5 to $10 in that range.",+ "It costs $5, $10 or $15.",+ "Between $5-$10 per run.",+ "It costs $1,000 today.",+ "Pay $3.50 for one and $1.25 for the next.",+ "Around $5K then $3.5M.",+ ]) {+ const html = render(source);+ assert.deepEqual(renderedMath(html), [], source);+ }+});++test("a shell parameter right after a positional one stays text", () => {+ for (const [source, literal] of [+ ['Run echo "$1$HOME" to print both.', "$1$HOME"],+ ["Use $1$ext in the replacement.", "$1$ext"],+ ['Run echo "$1$x" to print both.', "$1$x"],+ ['Run echo "$1$_" to print both.', "$1$_"],+ ['Run echo "$1$_name" to print both.', "$1$_name"],+ ['Run echo "$1$?" to print both.', "$1$?"],+ ['cat "$1$?" to print both.', "$1$?"],+ ['cat -n "$1$?" to print both.', "$1$?"],+ ['widget --mode fast "$1$?" to print both.', "$1$?"],+ ['- Run echo "$1$?" to print both.', "$1$?"],+ ['- [ ] Run echo "$1$?" to print both.', "$1$?"],+ ['- [x] Run echo "$1$?" to print both.', "$1$?"],+ ['> echo "$1$?" to print both.', "$1$?"],+ ['> 1. Run echo "$1$?" to print both.', "$1$?"],+ ['Run echo "$1$#" to print both.', "$1$#"],+ ['Run echo "$1$@" to print both.', "$1$@"],+ ['Run echo "$1$*" to print both.', "$1$*"],+ ['echo "$1$*glob*" now.', "$1$*glob*"],+ ['echo "$1$_name_" now.', "$1$_name_"],+ ['echo prefix"$1$?" now.', "$1$?"],+ ["echo prefix'$1$?' now.", "$1$?"],+ ["echo prefix$1$(date) now.", "$1$(date)"],+ ["echo status:$1$? now.", "$1$?"],+ ["echo status+$1$? now.", "$1$?"],+ ["echo status,$1$? now.", "$1$?"],+ ['git show "$1$?" now.', "$1$?"],+ ['git show HEAD "$1$?" now.', "$1$?"],+ ['kubectl get pods "$1$?" now.', "$1$?"],+ ['widget --mode HEAD "$1$?" now.', "$1$?"],+ ['python script.py | grep -n "$1$?" now.', "$1$?"],+ ["Run echo \\" + '\n "$1$?"', "$1$?"],+ ['sudo -u root echo "$1$?" now.', "$1$?"],+ ['env MODE=fast echo "$1$?" now.', "$1$?"],+ ['MODE=fast echo "$1$?" now.', "$1$?"],+ ['$ echo "$1$?" now.', "$1$?"],+ ["echo *$1$* now.", "*$1$*"],+ ["echo _$1$_ now.", "_$1$_"],+ ['Run echo "$1$-" to print both.', "$1$-"],+ ['Run echo "$1$!" to print both.', "$1$!"],+ ['Run echo "$1$0" to print both.', "$1$0"],+ ['Run echo "$1$$" to print both.', "$1$$"],+ ['Run echo "$1${2}" to print both.', "$1${2}"],+ ['Run echo "$1${?}" to print both.', "$1${?}"],+ ['Run echo "$1${_}" to print both.', "$1${_}"],+ ['Run echo "$1${foo_bar}" to print both.', "$1${foo_bar}"],+ ['Run echo "$1${foo:-${bar}}" to print both.', "$1${foo:-${bar}}"],+ ['Run echo "$1$(date)" to print both.', "$1$(date)"],+ ['Run echo "$1$((n + 1))" to print both.', "$1$((n + 1))"],+ ['Run echo "$1$[n + 1]" to print both.', "$1$[n + 1]"],+ [`Run echo "$1$'value'" to print both.`, `$1$'value'`],+ [`Run echo '$1$"localized"' to print both.`, `$1$"localized"`],+ ["Run echo $1$# to print both.", "$1$#"],+ ["Run echo $1$@ to print both.", "$1$@"],+ ["Run echo $1$* to print both.", "$1$*"],+ ["Run echo $1$- to print both.", "$1$-"],+ ["Run echo $1$? to print both.", "$1$?"],+ ["value=$1$?", "$1$?"],+ ["echo x | $1$?", "$1$?"],+ ["Run echo $1$! to print both.", "$1$!"],+ ["Run echo $1$[n] to print both.", "$1$[n]"],+ ["Run echo $1$[1] to print both.", "$1$[1]"],+ ["Run echo $1$[n+1] to print both.", "$1$[n+1]"],+ ]) {+ const html = render(source);+ assert.deepEqual(renderedMath(html), [], source);+ assert.ok(visibleText(html).includes(literal), source);+ }++ const combined =+ 'Shell examples "$1$x", "$1$?", "$1${2}", and "$1$$" stay literal.';+ const html = render(combined);+ assert.deepEqual(renderedMath(html), [], combined);+ for (const literal of ["$1$x", "$1$?", "$1${2}", "$1$$"]) {+ assert.ok(+ visibleText(html).includes(literal),+ `${combined} lost ${literal}`,+ );+ }+});++test("raw-text element bodies keep adjacent shell syntax byte-for-byte", () => {+ for (const source of [+ '<script>const value = "$1$x";</script>',+ '<style>var { value: "$1${2}"; }</style>',+ ]) {+ assert.equal(preprocessLaTeX(source), source);+ }+});++test("unterminated shell expansions stop at Markdown block boundaries", () => {+ const source = "Run echo $1${oops\n\n**later**";+ const html = render(source);+ assert.deepEqual(renderedMath(html), []);+ assert.ok(visibleText(html).includes("$1${oops"));+ assert.match(html, /data-streamdown="strong">later<\/span>/);+});++test("protected regions do not leak shell quote state into later prose", () => {+ for (const source of [+ '```sh\necho ok # \'\n```\n\nThe answer is "$42$" and **done**.',+ '<script>const fragment = "\'";</script>\n\nThe answer is "$42$" and **done**.',+ ]) {+ const html = render(source);+ assert.deepEqual(renderedMath(html), ["42"], source);+ assert.match(html, /data-streamdown="strong">done<\/span>/, source);+ }+});++test("link destinations do not reset surrounding prose quote state", () => {+ const source =+ 'There are $3$ red balls and $5$ blue balls, so $3 + 5 = 8$ in total. The answer is $8$. Shell examples "$1$x", "$1$?", "$1${2}", "$1$$", "$1$(date)", and "$1$((n+1))" stay literal. The answer is $42$! Is it $3$? Quote check: echo "$1$" and **then** echo "ok". Reference check: "$42$[source](https://example.com)." Prose check: He said "The answer is $42$!" after a $10$-fold increase across $1$-$2$.';+ const html = render(source);+ assert.deepEqual(renderedMath(html), [+ "3",+ "5",+ "3 + 5 = 8",+ "8",+ "42",+ "3",+ "1",+ "42",+ "42",+ "10",+ "1",+ "2",+ ]);+ assert.ok(visibleText(html).includes("$1$x"));+ assert.ok(visibleText(html).includes("$1$?"));+ assert.match(html, /data-streamdown="strong">then<\/span>/);+});++test("a closing shell-word quote does not consume later Markdown", () => {+ const source = 'echo "$1$" and **then** echo "ok"';+ const html = render(source);+ assert.deepEqual(renderedMath(html), ["1"]);+ assert.match(html, /data-streamdown="strong">then<\/span>/);+});++test("shell quote state carries across long and multiline words", () => {+ for (const source of [+ `echo "${"x".repeat(512)}$1$?"`,+ `echo "first line\n${"x".repeat(512)}$1$?"`,+ `echo "first line\r\n${"x".repeat(512)}$1$?"`,+ '> Run echo "first line\n> $1$?"',+ ]) {+ const html = render(source);+ assert.deepEqual(renderedMath(html), [], source);+ assert.ok(visibleText(html).includes("$1$?"), source);+ }+});++test("shell quote state resets at Markdown block boundaries", () => {+ for (const source of [+ '- Run echo "oops\n- The answer is $42$!',+ 'Run echo "oops\n\nThe answer is $42$!',+ 'Run echo "oops\n# The answer is $42$!',+ 'Run echo "oops\n> The answer is $42$!',+ 'Run echo "oops\n| value |\n| $42$! |',+ 'Run echo "oops\n***\nThe answer is $42$!',+ 'Run echo "oops\n---\nThe answer is $42$!',+ 'Run echo "oops\n_ _ _\nThe answer is $42$!',+ 'Run echo "oops\n===\nThe answer is $42$!',+ 'Run echo "oops\r\rThe answer is $42$!',+ ]) {+ const html = render(source);+ assert.deepEqual(renderedMath(html), ["42"], source);+ }+});++test("shell comments do not leak inert quotes into later prose", () => {+ const source = 'echo ok # "\nThe answer is $42$!';+ const html = render(source);+ assert.deepEqual(renderedMath(html), ["42"]);+});++test("a trailing numeric closer stays literal while streaming", () => {+ assert.equal(preprocessLaTeX("$1$", true), "\\$1$");+ assert.equal(preprocessLaTeX("$1$", false), "$1$");+ const source = "Run echo $1$[n+1]";+ for (+ let length = source.indexOf("$1$[") + 4;+ length < source.length;+ length+++ ) {+ const prefix = source.slice(0, length);+ const html = render(prefix, true);+ assert.deepEqual(renderedMath(html), [], prefix);+ assert.ok(visibleText(html).includes("$1$["), prefix);+ }+});++test("many unterminated substitutions stay within the streaming budget", () => {+ for (const source of ["$1$(".repeat(51_200), "$1$[".repeat(200_000)]) {+ const started = performance.now();+ preprocessLaTeX(source, true);+ // linear runs take ~200 ms on a loaded runner; a quadratic scan takes minutes.+ assert.ok(performance.now() - started < 2000);+ }+});++test("spaced inline math at a line start is not a shell prompt", () => {+ for (const [source, expected] of [+ ["$ x^2 + 1 $", ["x^2 + 1"]],+ ["The equation is:\n$ x = 3 $\nDone.", ["x = 3"]],+ ["$ x = 3 $!", ["x = 3"]],+ ["$ x = 3 $?", ["x = 3"]],+ ["$ x = 3 $[source](https://example.com)", ["x = 3"]],+ ["$ x = 3 $*", ["x = 3"]],+ ["$ x +\ny $", ["x +\ny"]],+ ] as Array<[string, string[]]>) {+ assert.deepEqual(renderedMath(render(source)), expected, source);+ }+ const prompt = render("$ ls -la\n$ cd /tmp\n\nDone $5 later.");+ assert.deepEqual(renderedMath(prompt), []);+ assert.ok(visibleText(prompt).includes("$ ls -la"));+});++test("nested emphasis at offset zero does not hang the emphasis scan", () => {+ for (const source of ["*There **are** $3$*", "_There __are__ $3$_"]) {+ const started = performance.now();+ preprocessLaTeX(source);+ assert.ok(performance.now() - started < 2000, source);+ }+});