Agent Zero AI framework
Render standard TeX in chat messages
Alessandro56e676579de6
diff --git a/tests/test_webui_latex_rendering.py b/tests/test_webui_latex_rendering.pynew file mode 100644index 000000000..253085d5a--- /dev/null+++ b/tests/test_webui_latex_rendering.py@@ -0,0 +1,63 @@+from pathlib import Path+import shutil+import subprocess++import pytest+++PROJECT_ROOT = Path(__file__).resolve().parents[1]+MESSAGES_JS = PROJECT_ROOT / "webui" / "js" / "messages.js"+++def test_standard_latex_delimiters_survive_markdown_preprocessing():+ if not shutil.which("node"):+ pytest.skip("Node.js is required to execute the LaTeX delimiter regression.")++ source = MESSAGES_JS.read_text(encoding="utf-8")+ converter_start = source.index("function convertLatexDelimiters(")+ converter_end = source.index("\nfunction renderLatexElements", converter_start)+ function_source = source[converter_start:converter_end]++ script = rf"""+{function_source}++function assertEqual(actual, expected) {{+ if (actual !== expected) {{+ throw new Error(`Expected ${{JSON.stringify(expected)}}, got ${{JSON.stringify(actual)}}`);+ }}+}}++const encode = (value) =>+ Array.from(value, (char) => `&#${{char.codePointAt(0)}};`).join('');++assertEqual(+ convertLatexDelimiters(String.raw`\[x^2 < y & z > 0\]`),+ `<latex data-display="true">${{encode('x^2 < y & z > 0')}}</latex>`,+);+assertEqual(+ convertLatexDelimiters(String.raw`Before \(x+y\) after`),+ `Before <latex>${{encode('x+y')}}</latex> after`,+);+assertEqual(+ convertLatexDelimiters(String.raw`$$\sum_n a_n$$`),+ `<latex data-display="true">${{encode(String.raw`\sum_n a_n`)}}</latex>`,+);+const protectedCode =+ 'Inline `' + String.raw`\(not_math\)` + '` and:\n' ++ '```tex\n' + String.raw`\[also_not_math\]` + '\n```';+assertEqual(convertLatexDelimiters(protectedCode), protectedCode);+"""+ subprocess.run(["node", "--input-type=module", "-e", script], check=True)+++def test_katex_renderer_uses_text_content_and_display_metadata():+ source = MESSAGES_JS.read_text(encoding="utf-8")++ assert "if (latex) processedContent = convertLatexDelimiters(processedContent)" in source+ assert "renderLatexElements(contentDiv)" in source+ assert "globalThis.katex.render(element.textContent, element" in source+ assert 'displayMode: element.dataset.display === "true"' in source+ assert "drawKvpsIncremental(stepDetailScroll, kvps)" in source+ assert "drawKvpsIncremental(stepDetailScroll, kvps, latex)" not in source+ assert "if (result.kvpsTable) renderLatexText(result.kvpsTable)" in source+ assert "globalThis.renderMathInElement(container" in sourcediff --git a/webui/js/AGENTS.md b/webui/js/AGENTS.mdindex 60497572f..ac6490801 100644--- a/webui/js/AGENTS.md+++ b/webui/js/AGENTS.md@@ -15,6 +15,7 @@- `modals.js` owns the stacked modal shell, `openModal`, `closeModal`, `scrollModal`, footer relocation, backdrop, and modal z-index behavior.- `surfaces.js` owns shared surface registration, right-canvas/modal mode routing, surface modal action rails, and reusable draggable/focus modal chrome.- `initFw.js` owns Alpine bootstrap and custom lifecycle directives such as `x-create`, `x-destroy`, and periodic `x-every-*` hooks.+- `messages.js` owns native message/process-step rendering, safe Markdown and HTML conversion, and KaTeX delimiter handling.- Other modules own focused UI utilities such as modals, messages, safe markdown, shortcuts, TTS/STT, surfaces, and initialization.## Local Contracts@@ -40,6 +41,7 @@- Every `<x-component>` instance must await cached module-load promises before markup is appended so Alpine bindings only run after imported stores exist.- Frontend extension hooks such as `confirm_dialog_after_render` and `get_tool_message_handler` must preserve their mutable context contracts.- Sanitize or safely render user/model-provided HTML and markdown.+- Convert standard TeX delimiters before Markdown parsing without touching inline or fenced code. Keep thought-card math rendering local to the agent-message handler rather than adding math flags to generic process-step or key/value rendering.- Do not expose secrets in localStorage, console logs, URLs, or WebSocket payloads.- Full message snapshots that start at backend log `no` 0 must replace the current message DOM before rendering; incremental snapshots should keep patching existing messages.@@ -59,6 +61,7 @@## Verification- Run targeted frontend/WebUI tests when available.+- For message math changes, smoke-test both response Markdown and agent thought cards with inline and display TeX.- Manually smoke-test startup, API calls, WebSocket state sync, and affected UI flows after infrastructure changes.- For modal infrastructure, verify duplicate paths can stack, missing paths stay closable, Escape closes only the top modal, and click-outside requires both mouse down and mouse up on the overlay container.diff --git a/webui/js/messages.js b/webui/js/messages.jsindex 95823993b..4910efe0e 100644--- a/webui/js/messages.js+++ b/webui/js/messages.js@@ -696,7 +696,7 @@ export function _drawMessage({const scroller = new Scroller(bodyDiv, { smooth: !isMassRender() });// Handle KVPs incrementally- drawKvpsIncremental(bodyDiv, kvps, false);+ drawKvpsIncremental(bodyDiv, kvps);// Handle contentif (content && content.trim().length > 0) {@@ -715,6 +715,7 @@ export function _drawMessage({// }let processedContent = content;+ if (latex) processedContent = convertLatexDelimiters(processedContent);processedContent = convertImageTags(processedContent);processedContent = convertImgFilePaths(processedContent);processedContent = convertFilePaths(processedContent);@@ -732,11 +733,7 @@ export function _drawMessage({// KaTeX rendering for markdownif (latex) {- contentDiv.querySelectorAll("latex").forEach((element) => {- globalThis.katex.render(element.innerHTML, element, {- throwOnError: false,- });- });+ renderLatexElements(contentDiv);}adjustMarkdownRender(contentDiv);@@ -854,7 +851,7 @@ export function drawMessageAgent({);}- return drawProcessStep({+ const result = drawProcessStep({id,title,code: "GEN",@@ -863,6 +860,8 @@ export function drawMessageAgent({actionButtons,log: arguments[0],});+ if (result.kvpsTable) renderLatexText(result.kvpsTable);+ return result;}/**@@ -1548,7 +1547,7 @@ export function drawMessageError({return { element };}-function drawKvpsIncremental(container, kvps, latex) {+function drawKvpsIncremental(container, kvps) {// existing KVPS tablelet table = container.querySelector(".msg-kvps");if (kvps) {@@ -1644,15 +1643,6 @@ function drawKvpsIncremental(container, kvps, latex) {const span = document.createElement("p");span.innerHTML = convertHTML(value);tdiv.appendChild(span);-- // KaTeX rendering for markdown- if (latex) {- span.querySelectorAll("latex").forEach((element) => {- globalThis.katex.render(element.innerHTML, element, {- throwOnError: false,- });- });- }}}} else {@@ -1696,6 +1686,41 @@ function convertHTML(str) {return result;}+function convertLatexDelimiters(content) {+ return content.replace(+ /(```[\s\S]*?```|~~~[\s\S]*?~~~|`[^`\n]*`)|\\\[([\s\S]*?)\\\]|\\\(([\s\S]*?)\\\)|\$\$([\s\S]*?)\$\$/g,+ (match, code, display, inline, dollars) => {+ if (code) return code;+ const tex = display ?? inline ?? dollars;+ const displayAttribute =+ display !== undefined || dollars !== undefined+ ? ' data-display="true"'+ : "";+ const encodedTex = Array.from(+ tex.trim(),+ (char) => `&#${char.codePointAt(0)};`,+ ).join("");+ return `<latex${displayAttribute}>${encodedTex}</latex>`;+ },+ );+}++function renderLatexElements(container) {+ container.querySelectorAll("latex").forEach((element) => {+ globalThis.katex.render(element.textContent, element, {+ displayMode: element.dataset.display === "true",+ throwOnError: false,+ });+ });+}++function renderLatexText(container) {+ globalThis.renderMathInElement(container, {+ throwOnError: false,+ errorCallback: () => {},+ });+}+function convertImgFilePaths(str) {return str.replace(/img:\/\//g, "/api/image_get?path=");}