From 870dc2466f4968542cc3d97dc39dbea17ad513fc Mon Sep 17 00:00:00 2001 From: chengyongru Date: Wed, 30 Sep 2026 17:56:20 +0800 Subject: [PATCH] fix(webui): avoid empty inline TeX tokens --- webui/src/lib/remark-tex-math.ts | 33 ++- .../src/tests/fixtures/markdown-math-mixed.md | 247 ++++++++++++++++++ .../tests/markdown-streaming-blocks.test.ts | 26 ++ webui/src/tests/markdown-text-math.test.tsx | 36 +++ 4 files changed, 333 insertions(+), 9 deletions(-) create mode 100644 webui/src/tests/fixtures/markdown-math-mixed.md diff --git a/webui/src/lib/remark-tex-math.ts b/webui/src/lib/remark-tex-math.ts index 231c2e19a..4734f9552 100644 --- a/webui/src/lib/remark-tex-math.ts +++ b/webui/src/lib/remark-tex-math.ts @@ -204,21 +204,36 @@ function tokenizeTexMathText(effects: Effects, ok: State, nok: State): State { effects.consume(code); effects.exit("mathTextSequence"); + return between; + } + + function between(code: Code): State | undefined { + if (code === null) return nok(code); + + if (code === BACKSLASH) { + closeSequence = effects.enter("mathTextSequence"); + effects.consume(code); + return close; + } + + if (isLineEnding(code)) { + effects.enter("lineEnding"); + effects.consume(code); + effects.exit("lineEnding"); + return between; + } + + // Commands can immediately follow the opening delimiter. Only enter a data + // token when there is a character to consume; micromark rejects empty tokens. effects.enter("mathTextData"); + effects.consume(code); return data; } function data(code: Code): State | undefined { - if (code === null) { + if (code === null || code === BACKSLASH || isLineEnding(code)) { effects.exit("mathTextData"); - return nok(code); - } - - if (code === BACKSLASH) { - effects.exit("mathTextData"); - closeSequence = effects.enter("mathTextSequence"); - effects.consume(code); - return close; + return between(code); } effects.consume(code); diff --git a/webui/src/tests/fixtures/markdown-math-mixed.md b/webui/src/tests/fixtures/markdown-math-mixed.md new file mode 100644 index 000000000..c0187caa1 --- /dev/null +++ b/webui/src/tests/fixtures/markdown-math-mixed.md @@ -0,0 +1,247 @@ +# 行内公式 + +这是行内公式:\(E = mc^2\)。 + +这是另一种行内公式:$a^2+b^2=c^2$。 + +--- + +# 独立公式 + +\[ +\int_{-\infty}^{\infty} e^{-x^2}\,dx=\sqrt{\pi} +\] + +$$ +\sum_{i=1}^{n} i=\frac{n(n+1)}{2} +$$ + +--- + +# 分行公式 + +\[ +\begin{aligned} +f(x) +&= x^2+2x+1 \\ +&= (x+1)^2 +\end{aligned} +\] + +--- + +# 多行等式 + +\[ +\begin{aligned} +a &= b+c \\ + &= d+e \\ + &= f +\end{aligned} +\] + +--- + +# 分情况公式 + +\[ +f(x)= +\begin{cases} +x^2, & x\ge 0,\\ +-x, & x<0. +\end{cases} +\] + +--- + +# 矩阵 + +\[ +A= +\begin{pmatrix} +1 & 2 & 3\\ +4 & 5 & 6\\ +7 & 8 & 9 +\end{pmatrix} +\] + +\[ +\begin{bmatrix} +a & b\\ +c & d +\end{bmatrix} +\] + +--- + +# 行列式 + +\[ +\det(A)= +\begin{vmatrix} +a & b\\ +c & d +\end{vmatrix} +=ad-bc +\] + +--- + +# 分数与根式 + +\[ +x=\frac{-b\pm\sqrt{b^2-4ac}}{2a} +\] + +--- + +# 极限、导数与积分 + +\[ +\lim_{x\to 0}\frac{\sin x}{x}=1 +\] + +\[ +\frac{d}{dx}\left(\ln x\right)=\frac{1}{x} +\] + +\[ +\int_a^b f(x)\,dx=F(b)-F(a) +\] + +--- + +# 向量与范数 + +\[ +\vec{v}= +\begin{bmatrix} +v_1\\ +v_2\\ +v_3 +\end{bmatrix}, +\qquad +\|\vec{v}\|_2= +\sqrt{v_1^2+v_2^2+v_3^2} +\] + +--- + +# 概率 + +\[ +P(A\mid B)=\frac{P(B\mid A)P(A)}{P(B)} +\] + +\[ +X\sim\mathcal{N}(\mu,\sigma^2) +\] + +--- + +# 逻辑与集合 + +\[ +\forall x\in\mathbb{R},\quad x^2\ge 0 +\] + +\[ +A\subseteq B,\qquad +A\cap B=\varnothing,\qquad +A\cup B +\] + +--- + +# 颜色与字体 + +\[ +\color{red}{\text{红色文字}} +\qquad +\mathbf{bold} +\qquad +\mathit{italic} +\qquad +\mathrm{Roman} +\] + +--- + +# 上标与下标 + +\[ +x_1,x_2,\ldots,x_n +\qquad +e^{i\pi}+1=0 +\] + +--- + +# 大型运算符 + +\[ +\left(\sum_{i=1}^{n}x_i\right)^2 +\qquad +\prod_{k=1}^{m} k +\qquad +\bigcup_{i=1}^{n} A_i +\] + +--- + +# 物理公式 + +\[ +\mathrm{HFOV}=2\arctan\left(\frac{W}{2f_x}\right) +\] + +\[ +F=ma,\qquad +p=mv,\qquad +E=h\nu +\] + +--- + +# 化学与单位 + +\[ +\mathrm{H_2O},\qquad +\mathrm{CO_2},\qquad +\mathrm{C_6H_{12}O_6} +\] + +\[ +9.8\,\mathrm{m\,s^{-2}} +\qquad +3.0\times10^8\,\mathrm{m/s} +\] + +--- + +# Markdown 列表中的公式 + +1. $x=1$ +2. $y=2$ + +- \(\alpha+\beta=\gamma\) +- \(\Delta x\to 0\) +- \(\nabla\cdot\vec{E}=\frac{\rho}{\varepsilon_0}\) + +--- + +# 引用中的公式 + +> 公式:\[ +> \hat{y}=\sigma(Wx+b) +> \] + +--- + +# 表格中的公式 + +| 名称 | 公式 | +|---|---| +| 欧拉公式 | \(e^{i\pi}+1=0\) | +| 勾股定理 | \(a^2+b^2=c^2\) | +| 透视关系 | \(\displaystyle y=f\frac{Y}{Z}\) | \ No newline at end of file diff --git a/webui/src/tests/markdown-streaming-blocks.test.ts b/webui/src/tests/markdown-streaming-blocks.test.ts index 18ea39616..803c3a6a0 100644 --- a/webui/src/tests/markdown-streaming-blocks.test.ts +++ b/webui/src/tests/markdown-streaming-blocks.test.ts @@ -1,11 +1,13 @@ import remarkMath from "remark-math"; import remarkParse from "remark-parse"; +import remend from "remend"; import { parseMarkdownIntoBlocks } from "streamdown"; import { unified } from "unified"; import { describe, expect, it } from "vitest"; import { parseMathAwareMarkdownBlocks } from "@/lib/markdown-streaming-blocks"; import { remarkTexMath } from "@/lib/remark-tex-math"; +import mixedMath from "@/tests/fixtures/markdown-math-mixed.md?raw"; const formula = String.raw`\[ \tan\left(\frac{\mathrm{HFOV}}{2}\right) @@ -22,6 +24,30 @@ function expectIntact(source: string, protectedSource: string) { } describe("math-aware streaming blocks", () => { + it.each([ + [String.raw`\(\alpha+\beta=\gamma\)`, String.raw`\alpha+\beta=\gamma`], + [String.raw`\(\nabla\cdot\vec{E}=\frac{\rho}{\varepsilon_0}\)`, String.raw`\nabla\cdot\vec{E}=\frac{\rho}{\varepsilon_0}`], + [String.raw`\(a\\b\)`, String.raw`a\\b`], + ["\\(a\n\\alpha\\)", "a\n\\alpha"], + ])("retains inline TeX data beginning with commands: %s", (source, value) => { + const parser = unified().use(remarkParse).use(remarkMath).use(remarkTexMath); + const paragraph = parser.parse(source).children[0]; + expect(paragraph.type).toBe("paragraph"); + if (paragraph.type !== "paragraph") throw new Error("Expected a paragraph"); + expect(paragraph.children).toMatchObject([{ type: "inlineMath", value }]); + }); + + it("parses mixed math containers without creating empty inline-data tokens", () => { + expect(parseMathAwareMarkdownBlocks(mixedMath).join("")).toBe(mixedMath); + }); + + it("accepts every repaired prefix of a mixed math response", () => { + for (let end = 1; end <= mixedMath.length; end++) { + const source = remend(mixedMath.slice(0, end), { htmlTags: false }); + expect(parseMathAwareMarkdownBlocks(source).join("")).toBe(source); + } + }); + it("merges a TeX formula split at a Setext equals line and blank lines", () => { const source = "Before\n\n" + formula + "\n\nAfter"; expect(parseMarkdownIntoBlocks(source).some((block) => block.includes(formula))).toBe(false); diff --git a/webui/src/tests/markdown-text-math.test.tsx b/webui/src/tests/markdown-text-math.test.tsx index 7a24d8823..501d1c066 100644 --- a/webui/src/tests/markdown-text-math.test.tsx +++ b/webui/src/tests/markdown-text-math.test.tsx @@ -2,6 +2,7 @@ import { render, waitFor } from "@testing-library/react"; import { expect, it } from "vitest"; import { MarkdownText } from "@/components/MarkdownText"; +import mixedMath from "@/tests/fixtures/markdown-math-mixed.md?raw"; it("retains a rendered TeX formula when an assistant response completes in streaming layout", async () => { const source = String.raw`Before @@ -31,3 +32,38 @@ After`; expect(container).toHaveTextContent("Before"); expect(container).toHaveTextContent("After"); }); + +it("renders a mixed math response through streaming prefixes and completion", async () => { + const { container, rerender } = render( + {""}, + ); + // Load the lazy renderer before exercising updates through its error boundary. + rerender({"# Ready"}); + await waitFor(() => expect(container.querySelector("h1")).toHaveTextContent("Ready"), { + timeout: 10_000, + }); + + const commandInList = String.raw`\[ +x=1 +\] + +- \(\alpha+\beta=\gamma\)`; + for (let end = 1; end <= commandInList.length; end++) { + rerender( + {commandInList.slice(0, end)}, + ); + } + expect(container.querySelectorAll(".katex")).toHaveLength(2); + + rerender({mixedMath}); + expect(container.querySelectorAll("h1")).toHaveLength(20); + expect(container.querySelectorAll(".katex")).toHaveLength(34); + expect(container.querySelector(".katex-error")).toBeNull(); + + rerender({mixedMath}); + expect(container.querySelectorAll("h1")).toHaveLength(20); + expect(container.querySelectorAll(".katex")).toHaveLength(34); + expect(container.querySelector(".katex-error")).toBeNull(); + expect(container.querySelectorAll("table")).toHaveLength(1); + expect(container.querySelector("blockquote")).toBeInTheDocument(); +}, 30_000);