[Feature] Native Mermaid diagram rendering in Web console and Desktop app
Self check
- I searched existing issues (incl. closed) — no duplicate.
What's the problem?
Feature Request: Native Mermaid diagram rendering in Web console and Desktop app
Background
CowAgent frequently outputs flowcharts, architecture diagrams, sequence diagrams, and state machines. Today, when the agent replies with a ```mermaid code block, the Web console renders it as a plain code block — the diagram source is shown verbatim, and no diagram appears. Users are left with a workaround: instruct the agent to render the diagram into a PNG and send that image instead.
That workaround has clear costs:
- Loss of vector fidelity and readability — images cannot be zoomed, text cannot be selected or copied, and they clash visually in dark mode;
- Wasted model round-trip — producing an image requires an extra tool/model call, adding latency and token cost;
- Asset overhead — generated images land on disk, need cleanup, and cannot follow theme changes.
Mermaid is the de facto standard for LLM-authored structured diagrams, and Markdown code blocks are its natural carrier. Rendering cost is minimal. It would be far better for this to be supported upstream than for every user to patch console.js on their own.
Desired behavior
In the Web console (channel/web) and the Desktop app (desktop), detect ```mermaid code blocks in message content and render them in place as diagrams:
- Render as vector (SVG) — zoomable, with selectable/copyable text;
- Follow light/dark theme changes and re-render automatically;
- Graceful degradation — on render failure, fall back to the existing code block so the source remains readable; never render blank or break the message;
- Wide diagrams scroll horizontally inside the message bubble instead of overflowing the layout;
- Preserve the existing copy button on code blocks so the source can still be retrieved.
Optional enhancements (not required)
- Lazy loading — only load the rendering library when a
mermaidblock actually appears, so plain-text conversations pay no first-paint cost; - Export — an "Export SVG / PNG" button;
- Click to zoom — consistent with the existing image lightbox experience;
- Streaming compatibility — while the agent is streaming, an unclosed code block should stay as a plain code block, then upgrade to a diagram once the fence closes, to avoid render thrashing.
Current-state investigation (v2.1.9)
Relevant code has been located, for implementation reference:
| Location | Notes |
|---|---|
channel/web/static/js/console.js:4368 |
renderMarkdown() — the single convergence point for all message rendering |
channel/web/static/js/console.js:4174 |
createMd() — where the markdown-it instance is built; already overrides highlight, link_open, and table_open/close. A fence rule could be added here to intercept mermaid |
channel/web/static/js/console.js:9471 |
applyHighlighting() — async post-processing hook after DOM insertion (currently used to add language labels and copy buttons to code blocks); a natural mount point for Mermaid rendering |
channel/web/static/js/console.js:4383 |
_addCodeBlockHeaders() — existing code-block wrapper; mermaid blocks must be skipped here |
channel/web/static/css/console.css |
Needs new container styles (scrolling, centering, theme-aware background) |
channel/web/chat.html:13 |
Existing local-vendor include pattern (markdown-it / highlight.js are both bundled locally, no CDN); Mermaid should likewise live under static/vendor/mermaid/ |
channel/web/web_channel.py:8993 |
AssetsHandler — /assets/(.*) already maps directly to static/, so new vendor files need no routing change |
channel/web/web_channel.py:2812 |
ChatHandler — already injects ?v=<timestamp> cache-busting into js/*.js and css/*.css, so changes take effect immediately |
desktop/src/renderer/src/components/Markdown.tsx |
The desktop renderer's own comment states it is "aligned 1:1 with the web console (markdown-it + highlight.js)" — both ends need this implemented together to stay in sync |
Technical considerations
- Do not render synchronously. Message content is appended token-by-token and
innerHTMLis rewritten repeatedly during streaming. Callingmermaid.render()synchronously causes thrashing and duplicate rendering. Recommended: thefencerule emits only a placeholder container carrying the source, and rendering is performed asynchronously afterapplyHighlighting(), guarded by adata-donemarker for idempotency. - Security level. Recommend
securityLevel: 'strict'to disable HTML tags and outbound requests, preventing message content from triggering unexpected network access. - Theme coupling. The existing
hljs-light/hljs-darkstylesheets are toggled via thedisabledattribute; Mermaid should hook into the same switch (cleardata-doneand re-render). - Dependency form.
mermaid@11'sdist/mermaid.min.jsis an IIFE build that assignsglobalThis.mermaidon execution, so it can be included via a plain<script>tag with no build-chain changes. Size is ~3.5 MB uncompressed — which is the main argument for lazy loading.
Environment
- CowAgent version:
2.1.9 - Deployment: bare-metal on a Linux server (
python3 app.py), channelfeishu+ Web console - Browser: Chromium
Appendix: Temporary workaround (for reference, not a substitute)
This machine currently renders Mermaid source to PNG via @mermaid-js/mermaid-cli and sends the image. It works well enough, but suffers from all the drawbacks above (raster, non-selectable, extra tool call). Once upstream support lands, the workaround can be retired.
What would you like?
Notes
Desktop is deliberately in scope: desktop/src/renderer/src/components/Markdown.tsx documents itself as 1:1-aligned with the web console — which is exactly why an upstream fix beats a local patch: a self-patch would cover only the web side and let the two drift apart permanently. No embellishment: every line number, path, function name, the 3.5 MB figure, and the IIFE globalThis.mermaid behavior were verified locally — maintainers can navigate straight to each anchor. “Already supported?” was ruled out: repo-wide grep -i mermaid returns zero hits, and neither docs nor release notes mention it. The workaround is stated honestly, so it doesn’t read as a complaint from someone who simply hasn’t found the right path — it’s a case of the path being wrong, not the user. Once the maintainers respond, paste the reply back to me and I’ll follow up.
Contribution
- I'd be interested in helping implement this.
Source: zhayujie/CowAgent