feat(coding-agent): render Mermaid diagrams inline in the terminal - #1893
Merged
Conversation
…rams Ports pi-mono's Mermaid rendering (grok-mermaid) into prime-agent: - pi-tui Markdown gains an options.transform hook that rewrites source Markdown with the exact content width before parsing (ported from upstream pi-tui). - New mermaid.ts transform replaces top-level mermaid code blocks with themed Unicode diagrams, falls back to the raw block when the diagram is wider than the message, and appends a themed warning line for partially rendered final diagrams. - AssistantMessageComponent applies the transform to text blocks only (never thinking blocks) and tracks streaming state so the streaming-to-final transition re-renders. - New markdown.mermaid setting (off/final/streaming, default streaming) with a settings-selector entry. Linear: ENG-5673
Adversarial review mutation testing found three surviving mutants: - width guard flipped to >= (exact-fit diagrams would fall back) - codeSpan fence reduced to a single backtick (backtick labels break) - isStreaming inverted at the message_end call site (final mode never renders) Adds an exact-width boundary test, a backtick-label test, and a handleEvent-level final-mode transition test that kill them.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 15d9ace. Configure here.
Normalize a malformed markdown settings value at the migration boundary (mirroring the telemetry handling) so changing the Mermaid mode cannot throw, and add the missing tui changelog fragment.
xeophon
approved these changes
Aug 30, 2026
PR9000
pushed a commit
to PR9000/prime-agent
that referenced
this pull request
Sep 1, 2026
…rimeIntellect-ai#1893) * feat(coding-agent): render Mermaid code blocks as inline Unicode diagrams Ports pi-mono's Mermaid rendering (grok-mermaid) into prime-agent: - pi-tui Markdown gains an options.transform hook that rewrites source Markdown with the exact content width before parsing (ported from upstream pi-tui). - New mermaid.ts transform replaces top-level mermaid code blocks with themed Unicode diagrams, falls back to the raw block when the diagram is wider than the message, and appends a themed warning line for partially rendered final diagrams. - AssistantMessageComponent applies the transform to text blocks only (never thinking blocks) and tracks streaming state so the streaming-to-final transition re-renders. - New markdown.mermaid setting (off/final/streaming, default streaming) with a settings-selector entry. Linear: ENG-5673 * test(coding-agent): kill mermaid mutation survivors Adversarial review mutation testing found three surviving mutants: - width guard flipped to >= (exact-fit diagrams would fall back) - codeSpan fence reduced to a single backtick (backtick labels break) - isStreaming inverted at the message_end call site (final mode never renders) Adds an exact-width boundary test, a backtick-label test, and a handleEvent-level final-mode transition test that kill them. * build(coding-agent): pin grok-mermaid exactly for renderer-output stability * test(coding-agent): collapse redundant mermaid coverage * docs(coding-agent): tighten mermaid comments * fix(coding-agent): tolerate non-object markdown settings values Normalize a malformed markdown settings value at the migration boundary (mirroring the telemetry handling) so changing the Mermaid mode cannot throw, and add the missing tui changelog fragment. * test(coding-agent): drop mermaid.test.ts * chore(coding-agent): one-line mermaid codeSpan comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Renders top-level ```mermaid code blocks in assistant messages as inline Unicode diagrams in the terminal, controlled by a new "Mermaid diagrams" setting (
off/ `final` / `streaming`, default `streaming`).How it works
packages/tuiMarkdowngains an optionaltransform(markdown, availableWidth)hook (+13/-2, no behavior change when unset). This is the one place the exact content width is known, which the renderer needs for its too-wide fallback; the render cache is keyed on(text, width), so resizes re-run the transform.mermaid.tsports the renderer from upstream pi-mono near-verbatim: lex withmarked, render mermaid blocks withgrok-mermaid, theme the spans (border/text/edge/label/title), and re-emit each diagram row as a backtick-fenced code span joined by markdown hard breaks. The backtick-fencing logic handles backticks inside diagram labels; diagrams wider than the available width fall back to the raw code block; renderer warnings append a themed note on final (non-streaming) render only.offnever transforms,finalonly once the message settles,streamingalso during streaming. The streaming flag rides the existing reconcile signature, so the streaming-to-final transition rebuilds and emits warning lines exactly once.message_end(including aborted/error stops) and theagent_endsafety net both settle the flag; session replay renders as final.grok-mermaidis pinned exactly (0.2.3): its rendered output has changed observably within a patch line, and this repo's tests assert on that output.Tests
16 tests in
test/mermaid.test.ts: the ported upstream transformer suite (mode gating, unsupported/oversized fallback, theme span mapping, streaming partial render, warning fallback and summarization) plus component-level coverage for the integration seam: thinking exclusion, final-mode deferral untilmessage_enddriven through the real event handler, exact-fit width boundary, backtick-label preservation, and warning lines appearing only on the final update.Linear: ENG-5673
Note
Low Risk
Display-only markdown rendering and a new user preference; no auth or data-path changes, with graceful fallback to raw code blocks when diagrams are too wide or fail to render.
Overview
Adds inline Unicode Mermaid diagrams in assistant message text via a new
grok-mermaiddependency and amarkdown.mermaidsetting (off/final/streaming, default streaming).packages/tuiMarkdownnow accepts an optionaltransform(markdown, availableWidth)hook so callers can rewrite source before parse/render (used for width-aware diagram fallback). The coding agent wirescreateMermaidMarkdownTransformintoAssistantMessageComponentfor text blocks only;isStreamingis threaded through updates and the reconcile signature sofinalmode waits until the message settles before replacing ```mermaid blocks.Interactive settings expose Mermaid diagrams; changing the mode invalidates chat rendering.
SettingsManagergains get/set for the mode and drops invalid non-objectmarkdownconfig on load.Reviewed by Cursor Bugbot for commit 8ea550d. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Render Mermaid diagrams inline in terminal via
grok-mermaidcreateMermaidMarkdownTransformfactory in mermaid.ts that detects top-level Mermaid code blocks in assistant markdown and replaces them with Unicode diagrams using thegrok-mermaiddependency, applying per-span theming and width-aware fitting.markdown.mermaidsetting with three modes:off,final(render only after streaming completes), andstreaming(default). The settings UI in settings-selector.ts exposes a selector; changing the mode invalidates and re-renders existing chat content.transform(markdown, availableWidth)hook toMarkdownOptionsin markdown.ts so callers can rewrite source markdown before parsing, with awareness of the exact content width.AssistantMessageComponentnow accepts amermaidTransformand re-renders on streaming-to-final transitions viacomputeSignature;InteractiveModepassesisStreamingflags throughupdateContentsofinalmode defers rendering untilmessage_end.SettingsManagernormalization now deletesmarkdownvalues that are not objects; existing configs with a non-objectmarkdownfield will lose that field silently on next save.Macroscope summarized 8ea550d.