diff --git a/packages/utils/package.json b/packages/utils/package.json
index 9685cb2d7..d9fed5882 100644
--- a/packages/utils/package.json
+++ b/packages/utils/package.json
@@ -4,7 +4,8 @@
"private": true,
"type": "module",
"exports": {
- ".": "./src/index.ts"
+ ".": "./src/index.ts",
+ "./ai-formatting": "./src/ai-formatting/index.ts"
},
"scripts": {
"lint": "eslint .",
diff --git a/packages/utils/src/ai-formatting/index.ts b/packages/utils/src/ai-formatting/index.ts
new file mode 100644
index 000000000..353160663
--- /dev/null
+++ b/packages/utils/src/ai-formatting/index.ts
@@ -0,0 +1 @@
+export * from './math';
diff --git a/packages/utils/src/ai-formatting/math.test.ts b/packages/utils/src/ai-formatting/math.test.ts
new file mode 100644
index 000000000..d9cb2b944
--- /dev/null
+++ b/packages/utils/src/ai-formatting/math.test.ts
@@ -0,0 +1,204 @@
+import { describe, expect, test } from 'bun:test';
+
+import {
+ balanceLatexBraces,
+ decodeHtmlEntities,
+ escapeHtml,
+ findInlineMathDollarSpans,
+ isInlineMathSpan,
+ isKaTeXErrorOutput,
+ isPlainNumericMathContent,
+ looksLikeLatex,
+ normalizeLatex,
+ protectCodeRegions,
+ replaceInlineMathDollarSpans,
+ wrapBareLatexSetBraces,
+} from './math';
+
+function mathIn(text: string): string[] {
+ return findInlineMathDollarSpans(text).map((span) => span.inner);
+}
+
+describe(findInlineMathDollarSpans.name, () => {
+ test('Should only find math between prices, not the prices themselves.', () => {
+ expect(mathIn('Costs $5, $10, $20 and $x$.')).toEqual(['x']);
+ });
+
+ test.each([
+ '$5 and $10',
+ '$5-$10',
+ '$100,000 is twice $50,000',
+ 'It costs $5 or $10 per month.',
+ ])('Should treat "%s" as currency.', (text) => {
+ expect(mathIn(text)).toEqual([]);
+ });
+
+ test.each([
+ ['$3x$', '3x'],
+ ['$f(x)$', 'f(x)'],
+ ["$f''(t)$", "f''(t)"],
+ ['$|x - 3|$', '|x - 3|'],
+ ["$A'$", "A'"],
+ ['$2x < 4$', '2x < 4'],
+ ['$x >= 0$', 'x >= 0'],
+ ['$a != b$', 'a != b'],
+ ['$O(n \\log n)$', 'O(n \\log n)'],
+ ])('Should treat "%s" as math.', (text, inner) => {
+ expect(mathIn(text)).toEqual([inner]);
+ });
+
+ test.each(['$x-axis$', '$p-value$', '$low-level$'])(
+ 'Should treat "%s" as prose.',
+ (text) => {
+ expect(mathIn(text)).toEqual([]);
+ }
+ );
+
+ test('A hard math signal should win over the spacing rule.', () => {
+ expect(mathIn('$ E = mc^2 $')).toEqual([' E = mc^2 ']);
+ });
+
+ test('A padded simple span should stay literal (same as pandoc).', () => {
+ expect(mathIn('$ x $')).toEqual([]);
+ });
+
+ test('A lone price on one line should not stop math on later lines.', () => {
+ expect(mathIn('It costs $5.\nSolve $x^2 = 4$.')).toEqual(['x^2 = 4']);
+ });
+
+ test('Should skip escaped dollar signs and display math.', () => {
+ expect(mathIn('\\$x\\$ and $$y^2$$')).toEqual([]);
+ });
+});
+
+describe(replaceInlineMathDollarSpans.name, () => {
+ test('Should replace only the math and keep the currency as is.', () => {
+ const result = replaceInlineMathDollarSpans(
+ 'Pay $5 to get $x^2$.',
+ (latex) => `[${latex}]`
+ );
+
+ expect(result).toEqual('Pay $5 to get [x^2].');
+ });
+});
+
+describe(isInlineMathSpan.name, () => {
+ test('A span containing an HTML tag is never math.', () => {
+ expect(isInlineMathSpan('5
', undefined)).toBe(false);
+ });
+
+ test('A digit right after the closer means it is currency.', () => {
+ expect(isInlineMathSpan('x', '1')).toBe(false);
+ });
+});
+
+describe(looksLikeLatex.name, () => {
+ test('A sentence is not LaTeX.', () => {
+ expect(looksLikeLatex('this is just a long sentence')).toBe(false);
+ });
+
+ test('A bare number is only math when allowed.', () => {
+ expect(looksLikeLatex('12')).toBe(false);
+ expect(looksLikeLatex('12', { allowBareNumbers: true })).toBe(true);
+ });
+
+ test('An interval is LaTeX.', () => {
+ expect(looksLikeLatex('(0, 1]')).toBe(true);
+ });
+});
+
+describe(isPlainNumericMathContent.name, () => {
+ test.each(['7.23', '1,234.56', ' 10 '])('"%s" is a number.', (value) => {
+ expect(isPlainNumericMathContent(value)).toBe(true);
+ });
+
+ test.each(['', '3x', '1/2'])('"%s" is not a number.', (value) => {
+ expect(isPlainNumericMathContent(value)).toBe(false);
+ });
+});
+
+describe(normalizeLatex.name, () => {
+ test('Should map Unicode math letters to ASCII.', () => {
+ expect(normalizeLatex('𝑥 + 𝐀')).toEqual('x + A');
+ });
+
+ test('Should fix "x 2" written instead of "x^2".', () => {
+ expect(normalizeLatex('x 2 + y 2 = 1')).toEqual('x^2 + y^2 = 1');
+ });
+
+ test('Should replace the Unicode minus and collapse "++".', () => {
+ expect(normalizeLatex('a − b ++ c')).toEqual('a - b + c');
+ });
+
+ test('Should drop a duplicated equation tail.', () => {
+ expect(normalizeLatex('x^2 + y^2 = 1. x 2 + y 2')).toEqual('x^2 + y^2 = 1');
+ });
+
+ test('Should keep primes.', () => {
+ expect(normalizeLatex("A'")).toEqual("A'");
+ });
+});
+
+describe(balanceLatexBraces.name, () => {
+ test('Should drop a stray closing brace.', () => {
+ expect(balanceLatexBraces('w = abba \\in \\Sigma^*}')).toEqual(
+ 'w = abba \\in \\Sigma^*'
+ );
+ });
+
+ test('Should close an unclosed group.', () => {
+ expect(balanceLatexBraces('\\frac{1}{2')).toEqual('\\frac{1}{2}');
+ });
+
+ test('Should ignore escaped braces.', () => {
+ expect(balanceLatexBraces('\\{1, 2\\}')).toEqual('\\{1, 2\\}');
+ });
+});
+
+describe(wrapBareLatexSetBraces.name, () => {
+ test('Should wrap bare set braces in $.', () => {
+ expect(wrapBareLatexSetBraces('S = \\{1, 2\\}')).toEqual(
+ 'S = $\\{1, 2\\}$'
+ );
+ });
+
+ test('Should leave wrapped set braces alone.', () => {
+ expect(wrapBareLatexSetBraces('$\\{1, 2\\}$')).toEqual('$\\{1, 2\\}$');
+ });
+});
+
+describe(protectCodeRegions.name, () => {
+ test('Should mask code so math passes cannot touch it.', () => {
+ const text = 'Use `price = $5` and\n```\n$x$\n```\nthen $y^2$.';
+ const code = protectCodeRegions(text);
+
+ expect(code.hasCode).toBe(true);
+ expect(code.masked).not.toContain('price');
+ expect(mathIn(code.masked)).toEqual(['y^2']);
+ expect(code.restore(code.masked)).toEqual(text);
+ });
+
+ test('Should only mask fences when asked to.', () => {
+ const code = protectCodeRegions('`a` and\n```\nb\n```', {
+ fencesOnly: true,
+ });
+
+ expect(code.masked).toStartWith('`a` and\n');
+ });
+});
+
+describe(escapeHtml.name, () => {
+ test('Should escape HTML and decode back to the same text.', () => {
+ const text = `Tom's & Jerry's`;
+
+ expect(escapeHtml(text)).not.toContain('<');
+ expect(decodeHtmlEntities(escapeHtml(text))).toEqual(text);
+ });
+});
+
+describe(isKaTeXErrorOutput.name, () => {
+ test('Should detect KaTeX error markup.', () => {
+ expect(isKaTeXErrorOutput('')).toBe(true);
+ expect(isKaTeXErrorOutput('')).toBe(false);
+ });
+});
diff --git a/packages/utils/src/ai-formatting/math.ts b/packages/utils/src/ai-formatting/math.ts
new file mode 100644
index 000000000..8e39c3d80
--- /dev/null
+++ b/packages/utils/src/ai-formatting/math.ts
@@ -0,0 +1,475 @@
+// Math core for rendering AI output. This module has no KaTeX import, so it
+// can be used on the server and in tests.
+
+export function escapeHtml(s: string): string {
+ return s
+ .replace(/&/g, '&')
+ .replace(//g, '>')
+ .replace(/"/g, '"')
+ .replace(/'/g, ''');
+}
+
+export function decodeHtmlEntities(text: string): string {
+ return text
+ .replace(/</g, '<')
+ .replace(/>/g, '>')
+ .replace(/"/g, '"')
+ .replace(/'/g, "'")
+ .replace(/&/g, '&');
+}
+
+/**
+ * Drops a duplicated equation tail that models sometimes append after a
+ * period (ie: "... = 1. x 2 + y 2").
+ */
+export function trimSloppyDuplicateEquationSuffix(latex: string): string {
+ const parts = latex.split(/\.\s+(?=[a-zA-Z]\s*2\b)/u);
+
+ return parts.length <= 1 ? latex : (parts[0]?.trim() ?? latex);
+}
+
+// Unicode Mathematical Alphanumeric ranges (bold, italic, bold italic, sans,
+// sans bold, sans italic) mapped to the ASCII letter they start at.
+const MATH_LATIN_RANGES: Array<[number, number]> = [
+ [0x1d400, 0x41],
+ [0x1d41a, 0x61],
+ [0x1d434, 0x41],
+ [0x1d44e, 0x61],
+ [0x1d468, 0x41],
+ [0x1d482, 0x61],
+ [0x1d5a0, 0x41],
+ [0x1d5ba, 0x61],
+ [0x1d5d4, 0x41],
+ [0x1d5ee, 0x61],
+ [0x1d608, 0x41],
+ [0x1d622, 0x61],
+];
+
+/**
+ * Maps Unicode Mathematical Alphanumeric letters (𝑥, 𝐀, 𝗮...) to ASCII.
+ */
+export function normalizeMathematicalLatinLetters(text: string): string {
+ let out = '';
+
+ for (const ch of text) {
+ const cp = ch.codePointAt(0)!;
+
+ const range = MATH_LATIN_RANGES.find(([start]) => {
+ return cp >= start && cp <= start + 25;
+ });
+
+ out += range ? String.fromCharCode(range[1] + (cp - range[0])) : ch;
+ }
+
+ return out;
+}
+
+/**
+ * Cleans LaTeX right before KaTeX: zero-width characters, "++", wrapping
+ * quotes, the Unicode minus and "x 2" written instead of "x^2".
+ */
+export function normalizeLatex(latex: string): string {
+ let s = latex
+ .replace(/[\u200B-\u200D\uFEFF\u00A0]/g, ' ')
+ .replace(/\+\s*\+/g, '+')
+ // ASCII `'` is left alone, since it is a prime (ie: `A'`, `f''(x)`).
+ .replace(/^\s*[‘’`]\s*|\s*[‘’`]\s*$/g, '')
+ .trim();
+
+ s = normalizeMathematicalLatinLetters(s);
+ s = s.replace(/−/g, '-');
+ s = trimSloppyDuplicateEquationSuffix(s);
+
+ let prev = '';
+
+ while (prev !== s) {
+ prev = s;
+ s = s.replace(/\b([a-zA-Z])\s+2\b/g, '$1^2');
+ }
+
+ return s.replace(/\s+/g, ' ').trim();
+}
+
+function hasLetterLike(s: string): boolean {
+ return /[a-zA-Z]|\p{L}/u.test(s);
+}
+
+function hasDigitLike(s: string): boolean {
+ return /\d|\p{N}/u.test(s);
+}
+
+/**
+ * A comparison with an operand on BOTH sides (`2x < 4`), so that `5<` (from
+ * `$5<$10`) and `` are excluded.
+ */
+function hasComparisonWithOperands(s: string): boolean {
+ return /(?:[A-Za-z0-9)\]}]|\p{L}|\p{N})\s*(?:<=|>=|!=|[<>≤≥≠])\s*(?:[-+]?(?:[A-Za-z0-9([{\\]|\p{L}|\p{N}))/u.test(
+ s
+ );
+}
+
+/**
+ * Returns true for a plain number (ie: `7.23`, `1,234.56`), which is not
+ * LaTeX.
+ */
+export function isPlainNumericMathContent(content: string): boolean {
+ const t = content.trim();
+
+ return !!t && /^\d+(?:[.,]\d+)*$/.test(t);
+}
+
+export type InlineMathOptions = {
+ /**
+ * Treats a paired bare number (`$12$`) as math rather than currency. Only
+ * use this for math-heavy documents.
+ */
+ allowBareNumbers?: boolean;
+
+ /**
+ * Classifies the entity-decoded text (ie: for stored HTML like `$3 < 5$`).
+ */
+ decodeEntities?: boolean;
+};
+
+/**
+ * Returns true if the content between a `$` pair looks like LaTeX rather than
+ * prose or currency.
+ */
+export function looksLikeLatex(
+ content: string,
+ options?: InlineMathOptions
+): boolean {
+ const t = content.trim();
+
+ if (!t) {
+ return false;
+ }
+
+ // Unicode math letters, or LaTeX syntax: \cmd, ^, _, {}.
+ if (/[\u{1D400}-\u{1D7FF}]/u.test(t) || /\\|[\^_{}]/.test(t)) {
+ return true;
+ }
+
+ // Hyphenated words: "low-level", "x-axis", "p-value".
+ if (/[a-zA-Z]{2,}-[a-zA-Z]+|[a-zA-Z]+-[a-zA-Z]{2,}/.test(t)) {
+ return false;
+ }
+
+ // A sentence.
+ if (t.split(/\s+/).filter((w) => /^[a-zA-Z]{3,}$/.test(w)).length >= 4) {
+ return false;
+ }
+
+ // Prose with an equals sign: "the mean is 5 when...".
+ if (/=/.test(t) && /\b[a-zA-Z]{4,}\b/.test(t)) {
+ return false;
+ }
+
+ if (
+ /^\p{L}{1,3}$/u.test(t) || // x, sin, KE
+ /^\d+(?:\.\d+)?\s*\p{L}$/u.test(t) || // 3x
+ /^[a-zA-Z]\s*[\^_]\s*\{?[^}]*\}?$/.test(t) ||
+ /^[a-zA-Z]{1,8}(?:['′″]+|_\w+|_\{[^}]+\}|\^\w+|\^\{[^}]+\})*\s*\([^()\n]{0,60}\)\s*$/.test(
+ t
+ ) // f(x), f'(x)
+ ) {
+ return true;
+ }
+
+ // |x - 3|
+ if (/^\|[^|\n]{1,24}\|$/.test(t) && (hasLetterLike(t) || hasDigitLike(t))) {
+ return true;
+ }
+
+ // "$10" is currency.
+ if (isPlainNumericMathContent(t)) {
+ return options?.allowBareNumbers === true;
+ }
+
+ if (/=/.test(t) && (hasLetterLike(t) || hasDigitLike(t))) {
+ return true;
+ }
+
+ if (hasComparisonWithOperands(t)) {
+ return true;
+ }
+
+ if (hasLetterLike(t) && /[+\-*/=−]/.test(t)) {
+ return true;
+ }
+
+ if (hasDigitLike(t) && /[+\-*/−]/.test(t)) {
+ return true;
+ }
+
+ // Intervals: [a, b], (0, 1].
+ return /^[[(].*[\])]$/.test(t) && (hasLetterLike(t) || hasDigitLike(t));
+}
+
+const HTML_TAG_IN_SPAN =
+ /<\/[A-Za-z][\w:-]*\s*>|<[A-Za-z][\w:-]*(?:\s[^<>]*)?\/?>/;
+
+const HARD_MATH_SIGNAL = /[\\^_{}]|[\u{1D400}-\u{1D7FF}]/u;
+
+/**
+ * Returns true if the text between a `$` pair is math.
+ *
+ * @param inner - NOT trimmed, since the spacing is a signal (same flanking
+ * rules as pandoc and markdown-it-texmath).
+ * @param after - The character right after the closing `$`.
+ */
+export function isInlineMathSpan(
+ inner: string,
+ after?: string,
+ options?: InlineMathOptions
+): boolean {
+ if (!inner) {
+ return false;
+ }
+
+ const probe = options?.decodeEntities ? decodeHtmlEntities(inner) : inner;
+
+ // A span containing an HTML tag is never math.
+ if (HTML_TAG_IN_SPAN.test(probe)) {
+ return false;
+ }
+
+ // `$ E = mc^2 $` still works.
+ if (HARD_MATH_SIGNAL.test(probe)) {
+ return true;
+ }
+
+ // A', f''
+ if (/^[A-Za-z]'+$/.test(probe)) {
+ return true;
+ }
+
+ // "$5 and $10"
+ if (/^\s/.test(probe) || /\s$/.test(probe)) {
+ return false;
+ }
+
+ // "$5-$10": the closer is really the next amount's opener.
+ if (after !== undefined && /\d/.test(after)) {
+ return false;
+ }
+
+ return looksLikeLatex(probe.trim(), options);
+}
+
+export type InlineMathSpan = {
+ end: number;
+ inner: string;
+ start: number;
+};
+
+/**
+ * Finds real `$...$` math spans. This is a scanner and not a regex replace,
+ * because a rejected span must re-anchor at `open + 1` so that its closing
+ * `$` can serve as the next span's opener (ie: `Costs $5, $10 and $x$.` must
+ * still find `$x$`).
+ *
+ * `$$` is skipped (display math has its own pass), and `\$` is a literal
+ * dollar sign.
+ */
+export function findInlineMathDollarSpans(
+ text: string,
+ options?: InlineMathOptions
+): InlineMathSpan[] {
+ const spans: InlineMathSpan[] = [];
+
+ let i = 0;
+
+ while (i < text.length) {
+ const open = text.indexOf('$', i);
+
+ if (open < 0) {
+ break;
+ }
+
+ if (text[open - 1] === '\\') {
+ i = open + 1;
+ continue;
+ }
+
+ if (text[open + 1] === '$') {
+ i = open + 2;
+ continue;
+ }
+
+ let close = -1;
+ let lineEnd = -1;
+
+ for (let j = open + 1; j < text.length; j++) {
+ // Inline math never spans lines.
+ if (text[j] === '\n') {
+ lineEnd = j;
+ break;
+ }
+
+ if (text[j] === '$' && text[j - 1] !== '\\') {
+ close = j;
+ break;
+ }
+ }
+
+ if (close < 0) {
+ if (lineEnd < 0) {
+ break;
+ }
+
+ // A lone price: skip the rest of the line, but keep scanning.
+ i = lineEnd + 1;
+ continue;
+ }
+
+ const inner = text.slice(open + 1, close);
+
+ if (isInlineMathSpan(inner, text[close + 1], options)) {
+ spans.push({ end: close, inner, start: open });
+ i = close + 1;
+ } else {
+ // Re-anchor, and do NOT skip the closer.
+ i = open + 1;
+ }
+ }
+
+ return spans;
+}
+
+/**
+ * Renders the accepted math spans. Every other character (including currency
+ * `$`) stays the same.
+ */
+export function replaceInlineMathDollarSpans(
+ text: string,
+ render: (inner: string) => string,
+ options?: InlineMathOptions
+): string {
+ const spans = findInlineMathDollarSpans(text, options);
+
+ if (!spans.length) {
+ return text;
+ }
+
+ let out = '';
+ let cursor = 0;
+
+ for (const span of spans) {
+ out += text.slice(cursor, span.start) + render(span.inner.trim());
+ cursor = span.end + 1;
+ }
+
+ return out + text.slice(cursor);
+}
+
+/**
+ * Wraps `\{ ... \}` in `$` when the model omitted the delimiters.
+ */
+export function wrapBareLatexSetBraces(text: string): string {
+ return text.replace(/(? {
+ return `$${full}$`;
+ });
+}
+
+/**
+ * Balances `{}` groups by dropping stray closers and closing unclosed groups.
+ * Escaped `\{` and `\}` are ignored.
+ */
+export function balanceLatexBraces(latex: string): string {
+ let depth = 0;
+ const strays: number[] = [];
+
+ for (let i = 0; i < latex.length; i++) {
+ const c = latex[i];
+
+ if (c === '\\') {
+ i++;
+ } else if (c === '{') {
+ depth++;
+ } else if (c === '}') {
+ if (depth === 0) {
+ strays.push(i);
+ } else {
+ depth--;
+ }
+ }
+ }
+
+ if (!strays.length && depth === 0) {
+ return latex;
+ }
+
+ let out = '';
+ let cursor = 0;
+
+ for (const i of strays) {
+ out += latex.slice(cursor, i);
+ cursor = i + 1;
+ }
+
+ return out + latex.slice(cursor) + '}'.repeat(depth);
+}
+
+// Code regions are where `$`, `*`, `_` and `<` are literal.
+const CODE_REGION_PATTERNS: RegExp[] = [
+ /```[\s\S]*?```/g,
+ /~~~[\s\S]*?~~~/g,
+ //gi,
+ //gi,
+ /`[^`\n]*`/g,
+];
+
+// Letters and digits only, so that no math pass mistakes it for notation.
+function sentinel(i: number): string {
+ return `CODEREGION${i}ENDCODEREGION`;
+}
+
+/**
+ * Masks code fences and inline code with sentinels, so that math and HTML
+ * passes can't touch them. Call `restore` on the processed text afterwards.
+ */
+export function protectCodeRegions(
+ text: string,
+ options?: { fencesOnly?: boolean }
+) {
+ const stash: string[] = [];
+
+ const patterns = options?.fencesOnly
+ ? CODE_REGION_PATTERNS.slice(0, 2)
+ : CODE_REGION_PATTERNS;
+
+ let masked = text;
+
+ for (const pattern of patterns) {
+ masked = masked.replace(pattern, (match) => {
+ stash.push(match);
+
+ return sentinel(stash.length - 1);
+ });
+ }
+
+ return {
+ hasCode: stash.length > 0,
+ masked,
+ restore(processed: string): string {
+ let out = processed;
+
+ // Outermost first, since later regions can be nested in earlier ones.
+ for (let i = stash.length - 1; i >= 0; i--) {
+ out = out.split(sentinel(i)).join(stash[i]!);
+ }
+
+ return out;
+ },
+ };
+}
+
+export function isKaTeXErrorOutput(html: string): boolean {
+ return (
+ html.includes('parse-error') ||
+ html.includes('#cc0000') ||
+ html.includes('in math mode at position')
+ );
+}
diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json
index 118911ac3..5f7392e81 100644
--- a/packages/utils/tsconfig.json
+++ b/packages/utils/tsconfig.json
@@ -3,6 +3,7 @@
"include": ["src"],
"compilerOptions": {
"module": "ESNext",
- "moduleResolution": "Bundler"
+ "moduleResolution": "Bundler",
+ "target": "ESNext"
}
}