Skip to content

feat(tui): render mermaid code blocks as diagrams in the terminal - #3851

Merged
Grapedge merged 1 commit into
mainfrom
tui-mermaid
Sep 17, 2026
Merged

Grapedge merged 1 commit into
mainfrom
tui-mermaid

Conversation

@Grapedge

Copy link
Copy Markdown
Collaborator

Related Issue

Internal feature work (spec-driven); no linked issue.

Problem

When an assistant reply contains a ```mermaid fenced code block, the TUI previously rendered it as raw source text. Users had to read mermaid syntax mentally or copy it into an external renderer to see the diagram.

What changed

  • Mermaid blocks render as terminal diagrams. A wrapper component (src/tui/components/markdown/markdown.ts) around pi-tui's Markdown splits top-level mermaid fences out of the token stream and renders each block as text-art via lovely-mermaid. The wrapper tokenizes with the Marked instance pi-tui already re-exports, so pi-tui itself is untouched, and with the feature off (or while a reply is still streaming) the output is byte-equivalent to the previous Markdown rendering.
  • Configurable, default final. Diagrams draw once the reply settles (final), and can be disabled under /settings → Mermaid diagrams or via [markdown].mermaid in tui.toml. Unknown values soft-warn and fall back to the default.
  • Graceful degradation. Diagrams wider than the terminal, diagram types the renderer does not support, and parse/layout failures fall back to the raw code block plus a one-line reason (e.g. mermaid diagram too wide to render (needs N columns)), so no reply content is ever lost.
  • Copy Source chip. In fullscreen mode each rendered block gets a [Copy Source] action (accent-colored text, press/capture/release mouse interaction) that copies the original mermaid fence to the clipboard, with [Copied] / [Copy failed] feedback.
  • [email protected] is pinned as a devDependency so tsdown bundles it into the CLI artifact with zero runtime imports.

Tests

  • New suites: markdown wrapper (31 tests incl. off/transient byte-equivalence, fence splitting, wide/unsupported/failure degradation, button interaction), mermaid preference selector + settings wiring, config parsing/writing, reload.
  • Updated: assistant-message (13), update-preferences.
  • cd apps/kimi-code && pnpm test: 3809 passed; the 5 failures are pre-existing on main (verified against a stashed baseline: 2× release-artifacts needing zstd, 3 existing TUI tests) and are unrelated to this PR.
  • typecheck, oxlint, smoke, sherif all green.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: the issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9e3e532

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

❌ Nix build failed

Hash mismatch in pnpmDeps:

Hash
specified sha256-LWpsB1Z9nAIFF2/sHv30F2Y9n+87wmg+ypiMFwOQ6M0=
got sha256-xrn34bQ76s+ouOZPHZ4TBkpTHxG7gZejmx8RqSii2uA=

Please update flake.nix with the got hash.

@pkg-pr-new

pkg-pr-new Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@9e3e532
npx https://pkg.pr.new/@moonshot-ai/kimi-code@9e3e532

commit: 9e3e532

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8c8b17d007

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/tui-mermaid-diagrams.md Resolved
@Grapedge

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: daf2f8cd3e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Top-level mermaid fences in Markdown surfaces render as Unicode
box-drawing art via lovely-mermaid (pinned 0.3.3, bundled into the
CLI). Diagrams that cannot be drawn fall back to a reason line plus
the original fence; fullscreen mode offers a Copy Source chip per
block (accent text, press/capture/release interaction).

The rendering mode is "final" by default (draw once the reply
settles) and can be turned off under /settings → Mermaid diagrams or
via [markdown].mermaid in tui.toml. The wrapper tokenizes with the
Marked instance pi-tui already re-exports, so pi-tui itself is
untouched.
@Grapedge

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 9e3e532227

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@Grapedge
Grapedge merged commit faec32c into main Sep 17, 2026
15 checks passed
@Grapedge
Grapedge deleted the tui-mermaid branch September 17, 2026 04:56
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Oct 5, 2026
Step 6 of the incremental sync to 2.1.1: 29 upstream commits over 370
files. 35 conflicts, concentrated in the TUI layer where the fork's
i18n and pi-tui work sits on top of upstream's message components.

Upstream's changes in this step, all absorbed:
- mermaid code blocks render as terminal diagrams, with a new
  `MermaidRenderMode` preference and a settings entry (MoonshotAI#3851)
- Windows CLI binaries are signed with Azure Artifact Signing (MoonshotAI#3858)
- MCP servers flip to needs-auth when a tool call returns 401, and the
  rejected grant's tokens are invalidated (MoonshotAI#3846)
- the `/desktop` slash command and the `install-app` subcommand (MoonshotAI#3849)
- skill scopes: `SkillScope` joins the catalog types and custom-theme
  becomes tui-only (MoonshotAI#3843)
- Windows 8.3 short paths are watched through their long form (MoonshotAI#3840)
- images upload as file references for Kimi models (MoonshotAI#3784)
- `/usage` and `/status` no longer prompt for login before the first
  message (MoonshotAI#3818)
- completed subagent scopes are evicted behind an LRU cache (MoonshotAI#3778)
- the human layer's request/recovery stage names were aligned (MoonshotAI#3766)
  and its names clarified (MoonshotAI#3727)
- WebBridge's display name became "Kimi Browser Extension" (MoonshotAI#3803)
- diff code blocks are highlighted (MoonshotAI#3802)
- feedback surveys are gated by the resolved kfc model id (MoonshotAI#3795)

Fork behaviour that had to survive:
- the i18n layer: every new upstream string went through `t()` with
  en/zh locale entries (mermaid preference, WebBridge intro, usage
  panel), and the fork's locale JSONs were regenerated
- the pi-tui intent cards and the fork's local Markdown component, kept
  over upstream's pi-tui Markdown import in the message components
- the 128 KiB `max_completion_tokens` cap on the kimi trait
- the fork's media tool folding (read-media-file) and the
  `isMediaUploadAuthError` rename carried through
- the Bun toolchain: flake.nix keeps `impureEnvVars`, the native build
  workflow keeps the Bun steps while taking upstream's Azure signing
  env block, and 05-verify.mjs verifies the checksum before the
  Authenticode branch
- proactiveMediaEviction in the LLM requester, after upstream's warning
  dispatch
- the fork's stricter `isUnauthorizedLikeError` (no message-regex
  fallback) and its OAuth token transaction

This merge also fixes a fork bug the new upstream tests exposed: the
TUI's background-task handler had been switched to the v2 `task.*`
event spelling during the v1→v2 migration, but the SDK's event mapper
translates those back to `background.task.*` before delivery (the
fork's own `notify.ts` already listened for the translated names), so
the handler had been dead code at runtime. The handler and the three
test fixtures that fed it now use `background.task.*` again.

Test expectations re-pinned where the fork's shape differs from
upstream's:
- `assistant-message.test.ts` reads the local Markdown's `sourceText`
  instead of pi-tui's `text`
- the new `markdown.test.ts` calls `setPalette(palette, family)`, the
  fork's two-argument signature
- `choice-picker.test.ts` asserts the mermaid entry, because the fork's
  longer settings list pushes `upgrade` past the 8-row page
- "invalidates stored OAuth tokens when a runtime 401 fails the
  refresh": the fork's token transaction removes the revoked tokens
  while the refresh fails, and the transport closes before the caller
  can flip the entry into needs-auth, so the server ends up failed
  rather than needs-auth. The security-relevant half (stale tokens are
  gone) still holds; the status difference is a known gap to revisit.

Verified: typecheck clean across every package and app; full suite
17594 passed / 81 skipped / 1 todo. The four failures under parallel
load (rpc-events ×2, taskManager ×2) pass in isolation — load-induced
flakes, not regressions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant