AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): render inline $…$ math in chat replies and markdown artifacts (#14790)
Reinier van der Leerf1664e5cb276
diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/OutputRenderers/renderers/MarkdownRenderer.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/OutputRenderers/renderers/MarkdownRenderer.tsxindex b7f96d6c32..3865f7b242 100644--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/OutputRenderers/renderers/MarkdownRenderer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/OutputRenderers/renderers/MarkdownRenderer.tsx@@ -16,6 +16,7 @@ import {} from "../types";import "highlight.js/styles/github-dark.css";import "katex/dist/katex.min.css";+import { escapeCurrencyAmounts } from "@/lib/markdown-math";const markdownPatterns = [/```[\s\S]*?```/u, // Fenced code blocks (check first)@@ -158,7 +159,7 @@ function renderMarkdown(value: unknown,_metadata?: OutputMetadata,): React.ReactNode {- const markdownContent = String(value);+ const markdownContent = escapeCurrencyAmounts(String(value));return (<div className="markdown-output">@@ -166,7 +167,7 @@ function renderMarkdown(className="prose prose-sm dark:prose-invert max-w-none"remarkPlugins={[remarkGfm, // GitHub Flavored Markdown (tables, task lists, strikethrough)- [remarkMath, { singleDollarTextMath: false }], // Math support for LaTeX+ [remarkMath, { singleDollarTextMath: true }], // Math support for LaTeX]}rehypePlugins={[[rehypeKatex, { strict: false }], // Render math with KaTeXdiff --git a/autogpt_platform/frontend/src/app/globals.css b/autogpt_platform/frontend/src/app/globals.cssindex 534f7a4944..ec04cefa04 100644--- a/autogpt_platform/frontend/src/app/globals.css+++ b/autogpt_platform/frontend/src/app/globals.css@@ -167,3 +167,9 @@ body[data-google-picker-open="true"] [data-dialog-content] {[data-streamdown="table-wrapper"] td {padding: 0.875rem 1rem; /* py-3.5 px-4 */}++/* KaTeX sets .katex to 1.21em, which makes inline formulas outgrow the prose+ around them; display blocks keep KaTeX's size. */+:not(.katex-display) > .katex {+ font-size: 1.1em;+}diff --git a/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-math.test.tsx b/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-math.test.tsxnew file mode 100644index 0000000000..4588ea204e--- /dev/null+++ b/autogpt_platform/frontend/src/components/ai-elements/__tests__/message-math.test.tsx@@ -0,0 +1,75 @@+import { cleanup, render, waitFor } from "@testing-library/react";+import { afterEach, describe, expect, it } from "vitest";+import { MessageResponse } from "../message";++// KaTeX keeps the TeX source in a MathML annotation, so prose assertions have to+// read the document with the rendered formulas taken out.+function proseOf(container: HTMLElement): string {+ const copy = container.cloneNode(true) as HTMLElement;+ copy.querySelectorAll(".katex").forEach((node) => node.remove());+ return copy.textContent ?? "";+}++function texOf(container: HTMLElement): string[] {+ return Array.from(+ container.querySelectorAll('annotation[encoding="application/x-tex"]'),+ ).map((node) => node.textContent ?? "");+}++async function renderReply(markdown: string) {+ const { container } = render(<MessageResponse>{markdown}</MessageResponse>);+ await waitFor(() => expect(container.textContent).not.toBe(""));+ return container;+}++describe("MessageResponse math", () => {+ afterEach(() => {+ cleanup();+ });++ it("renders inline $…$ math through KaTeX", async () => {+ const container = await renderReply(+ "The condition is $\\deg(\\text{num}) \\geq \\deg(\\text{den})$ here.",+ );++ await waitFor(() => expect(container.querySelector(".katex")).toBeTruthy());+ expect(texOf(container)).toEqual([+ "\\deg(\\text{num}) \\geq \\deg(\\text{den})",+ ]);+ expect(proseOf(container)).toBe("The condition is here.");+ });++ it("still renders display $$…$$ math", async () => {+ const container = await renderReply("Result:\n\n$$\n\\frac{1}{s+1}\n$$\n");++ await waitFor(() =>+ expect(container.querySelector(".katex-display")).toBeTruthy(),+ );+ expect(texOf(container)).toEqual(["\\frac{1}{s+1}"]);+ });++ it("leaves two currency amounts in one sentence as literal text", async () => {+ const container = await renderReply("$5 and $10 buys lunch.");++ expect(container.querySelector(".katex")).toBeNull();+ expect(proseOf(container)).toBe("$5 and $10 buys lunch.");+ });++ it("renders the math and keeps the price in a sentence holding both", async () => {+ const container = await renderReply(+ "It costs $5 to compute $(s+1)^{2}$ now.",+ );++ await waitFor(() => expect(texOf(container)).toEqual(["(s+1)^{2}"]));+ expect(proseOf(container)).toBe("It costs $5 to compute now.");+ });++ it("leaves dollars inside a fenced code block untouched", async () => {+ const container = await renderReply("```bash\necho $5 and $10\n```\n");++ expect(container.querySelector(".katex")).toBeNull();+ await waitFor(() =>+ expect(container.textContent).toContain("echo $5 and $10"),+ );+ });+});diff --git a/autogpt_platform/frontend/src/components/ai-elements/message.tsx b/autogpt_platform/frontend/src/components/ai-elements/message.tsxindex 59f8a34e49..d63a458640 100644--- a/autogpt_platform/frontend/src/components/ai-elements/message.tsx+++ b/autogpt_platform/frontend/src/components/ai-elements/message.tsx@@ -13,7 +13,8 @@ import { Dialog } from "@/components/molecules/Dialog/Dialog";import { cn } from "@/lib/utils";import { cjk } from "@streamdown/cjk";import { code } from "@/lib/streamdown-code-plugin";-import { math } from "@streamdown/math";+import { createMathPlugin } from "@streamdown/math";+import { escapeCurrencyAmounts } from "@/lib/markdown-math";import { mermaid } from "@streamdown/mermaid";import type { UIMessage } from "ai";import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";@@ -351,8 +352,12 @@ function ExternalLinkModal({);}+// Single-dollar math is off by default in @streamdown/math, so "$x^2$" reached the+// reader raw; escapeCurrencyAmounts keeps prices out of the formulas it enables.+const math = createMathPlugin({ singleDollarTextMath: true });+export const MessageResponse = memo(- ({ className, ...props }: MessageResponseProps) => (+ ({ className, children, ...props }: MessageResponseProps) => (<StreamdownclassName={cn("size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0 [&_pre]:!bg-white",@@ -384,7 +389,11 @@ export const MessageResponse = memo(renderModal: (modalProps) => <ExternalLinkModal {...modalProps} />,}}{...props}- />+ >+ {typeof children === "string"+ ? escapeCurrencyAmounts(children)+ : children}+ </Streamdown>),(prevProps, nextProps) => prevProps.children === nextProps.children,);diff --git a/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/MarkdownRenderer.tsx b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/MarkdownRenderer.tsxindex 9217be1105..4f68baaa36 100644--- a/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/MarkdownRenderer.tsx+++ b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/MarkdownRenderer.tsx@@ -16,6 +16,7 @@ import {} from "../types";import "highlight.js/styles/github-dark.css";import "katex/dist/katex.min.css";+import { escapeCurrencyAmounts } from "@/lib/markdown-math";const markdownPatterns = [/```[\s\S]*?```/u, // Fenced code blocks (check first)@@ -158,7 +159,7 @@ function renderMarkdown(value: unknown,_metadata?: OutputMetadata,): React.ReactNode {- const markdownContent = String(value);+ const markdownContent = escapeCurrencyAmounts(String(value));return (<div className="markdown-output">@@ -166,7 +167,7 @@ function renderMarkdown(className="prose prose-sm dark:prose-invert max-w-none"remarkPlugins={[remarkGfm, // GitHub Flavored Markdown (tables, task lists, strikethrough)- [remarkMath, { singleDollarTextMath: false }], // Math support for LaTeX+ [remarkMath, { singleDollarTextMath: true }], // Math support for LaTeX]}rehypePlugins={[[rehypeKatex, { strict: false }], // Render math with KaTeXdiff --git a/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/__tests__/MarkdownRenderer.test.tsx b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/__tests__/MarkdownRenderer.test.tsxnew file mode 100644index 0000000000..045ecd9efe--- /dev/null+++ b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/__tests__/MarkdownRenderer.test.tsx@@ -0,0 +1,83 @@+import { cleanup, render } from "@testing-library/react";+import { afterEach, describe, expect, it } from "vitest";+import { markdownRenderer } from "../MarkdownRenderer";++function renderMarkdown(markdown: string) {+ return render(+ <>+ {markdownRenderer.render(markdown, {+ mimeType: "text/markdown",+ filename: "notes.md",+ })}+ </>,+ ).container;+}++// KaTeX keeps the TeX source in a MathML annotation, so prose assertions have to+// read the document with the rendered formulas taken out.+function proseOf(container: HTMLElement): string {+ const copy = container.cloneNode(true) as HTMLElement;+ copy.querySelectorAll(".katex").forEach((node) => node.remove());+ return copy.textContent ?? "";+}++function texOf(container: HTMLElement): string[] {+ return Array.from(+ container.querySelectorAll('annotation[encoding="application/x-tex"]'),+ ).map((node) => node.textContent ?? "");+}++describe("MarkdownRenderer math", () => {+ afterEach(() => {+ cleanup();+ });++ it("renders inline $…$ math through KaTeX", () => {+ const container = renderMarkdown(+ "The condition is $\\deg(\\text{num}) \\geq \\deg(\\text{den})$ here.",+ );++ expect(container.querySelector(".katex")).toBeTruthy();+ expect(texOf(container)).toEqual([+ "\\deg(\\text{num}) \\geq \\deg(\\text{den})",+ ]);+ expect(proseOf(container)).toBe("The condition is here.");+ });++ it("still renders display $$…$$ math", () => {+ const container = renderMarkdown("Result:\n\n$$\n\\frac{1}{s+1}\n$$\n");++ expect(container.querySelector(".katex-display")).toBeTruthy();+ expect(texOf(container)).toEqual(["\\frac{1}{s+1}"]);+ });++ it("leaves two currency amounts in one sentence as literal text", () => {+ const container = renderMarkdown("$5 and $10 buys lunch.");++ expect(container.querySelector(".katex")).toBeNull();+ expect(proseOf(container)).toBe("$5 and $10 buys lunch.");+ });++ it("renders the math and keeps the price in a sentence holding both", () => {+ const container = renderMarkdown("It costs $5 to compute $(s+1)^{2}$ now.");++ expect(texOf(container)).toEqual(["(s+1)^{2}"]);+ expect(proseOf(container)).toBe("It costs $5 to compute now.");+ });++ it("keeps digit-leading math with LaTeX syntax as math", () => {+ const container = renderMarkdown("Growth follows $5x^2$ exactly.");++ expect(texOf(container)).toEqual(["5x^2"]);+ expect(proseOf(container)).toBe("Growth follows exactly.");+ });++ it("leaves dollars inside a fenced code block untouched", () => {+ const container = renderMarkdown("```bash\necho $5 and $10\n```\n");++ expect(container.querySelector(".katex")).toBeNull();+ expect(container.querySelector("code")?.textContent).toContain(+ "echo $5 and $10",+ );+ });+});diff --git a/autogpt_platform/frontend/src/lib/markdown-math.test.ts b/autogpt_platform/frontend/src/lib/markdown-math.test.tsnew file mode 100644index 0000000000..de2b115aa6--- /dev/null+++ b/autogpt_platform/frontend/src/lib/markdown-math.test.ts@@ -0,0 +1,45 @@+import { describe, expect, it } from "vitest";+import { escapeCurrencyAmounts } from "./markdown-math";++describe("escapeCurrencyAmounts", () => {+ it("escapes a price that would otherwise open a formula", () => {+ expect(escapeCurrencyAmounts("$5 and $10 buys lunch.")).toBe(+ "\\$5 and \\$10 buys lunch.",+ );+ });++ it("leaves math alone, including math that opens with a digit", () => {+ expect(escapeCurrencyAmounts("Growth is $5x^2$ and $(s+1)^{2}$.")).toBe(+ "Growth is $5x^2$ and $(s+1)^{2}$.",+ );+ });++ it("leaves $$ delimiters alone", () => {+ expect(escapeCurrencyAmounts("$$\n5x\n$$")).toBe("$$\n5x\n$$");+ expect(escapeCurrencyAmounts("Inline $$5x$$ here.")).toBe(+ "Inline $$5x$$ here.",+ );+ });++ it("skips fenced and inline code", () => {+ expect(+ escapeCurrencyAmounts("```\n$5 and $10\n```\nprose $5 and $10"),+ ).toBe("```\n$5 and $10\n```\nprose \\$5 and \\$10");+ expect(escapeCurrencyAmounts("Run `$5 and $10` now.")).toBe(+ "Run `$5 and $10` now.",+ );+ });++ it("keeps a fence open past a shorter closing fence", () => {+ const markdown = "````\n```\n$5 and $10\n````\nprose $5 and $10";+ expect(escapeCurrencyAmounts(markdown)).toBe(+ "````\n```\n$5 and $10\n````\nprose \\$5 and \\$10",+ );+ });++ it("is idempotent, so an already-escaped price is left as one", () => {+ const once = escapeCurrencyAmounts("It costs $5.");+ expect(once).toBe("It costs \\$5.");+ expect(escapeCurrencyAmounts(once)).toBe(once);+ });+});diff --git a/autogpt_platform/frontend/src/lib/markdown-math.ts b/autogpt_platform/frontend/src/lib/markdown-math.tsnew file mode 100644index 0000000000..a7d49f234d--- /dev/null+++ b/autogpt_platform/frontend/src/lib/markdown-math.ts@@ -0,0 +1,84 @@+const FENCE_RE = /^\s{0,3}(`{3,}|~{3,})/;+const LATEX_SYNTAX_RE = /[\\^_{}]/;++// With single-dollar math on, remark-math reads "$5 and $10" as one formula. A "$"+// before a digit is therefore currency unless the span up to the next "$" on that+// line carries LaTeX syntax, which keeps "$5x^2$" math; escaping the rest leaves+// them literal and every other delimiter to remark-math.+export function escapeCurrencyAmounts(markdown: string): string {+ let openFence: string | null = null;++ return markdown+ .split("\n")+ .map((line) => {+ const fence = FENCE_RE.exec(line)?.[1];++ if (openFence) {+ if (+ fence &&+ fence[0] === openFence[0] &&+ fence.length >= openFence.length+ ) {+ openFence = null;+ }+ return line;+ }++ if (fence) {+ openFence = fence;+ return line;+ }++ return escapeCurrencyAmountsInLine(line);+ })+ .join("\n");+}++function escapeCurrencyAmountsInLine(line: string): string {+ let result = "";+ let index = 0;++ while (index < line.length) {+ const char = line[index];++ if (char === "\\") {+ result += line.slice(index, index + 2);+ index += 2;+ } else if (char === "`") {+ const end = endOfCodeSpan(line, index);+ result += line.slice(index, end);+ index = end;+ } else if (char === "$" && line[index + 1] === "$") {+ result += "$$";+ index += 2;+ } else if (char === "$" && isCurrencyAmount(line, index)) {+ result += "\\$";+ index += 1;+ } else {+ result += char;+ index += 1;+ }+ }++ return result;+}++function endOfCodeSpan(line: string, start: number): number {+ let runEnd = start;+ while (line[runEnd] === "`") {+ runEnd += 1;+ }++ const close = line.indexOf(line.slice(start, runEnd), runEnd);+ return close === -1 ? runEnd : close + runEnd - start;+}++function isCurrencyAmount(line: string, index: number): boolean {+ if (!/\d/.test(line[index + 1] ?? "")) {+ return false;+ }++ const rest = line.slice(index + 1);+ const close = rest.search(/(?<!\\)\$/);+ return close === -1 || !LATEX_SYNTAX_RE.test(rest.slice(0, close));+}