feat(tui): render Mermaid diagrams as terminal images (#6320)

* feat(tui): render Mermaid diagrams as terminal images

* perf(tui): streamline Mermaid rendering and resource ownership

* fix(tui): clarify diagram navigation hints

* fix(tui): release viewer layout leases and retain retry targets

* docs(tui): keep Mermaid guide focused on usage
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-20 17:28:36 +08:00
1 parent 9b5d552f40
commit 9adbcc2cef
31 files changed
+1983 -21

No files matched your search

+23 -1
View File
@@ -376,7 +376,7 @@ export class NanobotTui {
},
{
onPrimaryMouseDown: () => {
const restoreComposerFocus = !this.diffViewer.visible
const restoreComposerFocus = !this.diffViewer.visible && !this.transcript.diagrams.viewer.visible
if (restoreComposerFocus) this.composer.focus()
// Runtime pickers are transient popovers. A primary click anywhere
// outside their trigger or body dismisses them; hide() restores the
@@ -405,6 +405,16 @@ export class NanobotTui {
this.status = layout.status
this.meta = layout.meta
this.renderer.root.add(this.shell)
this.renderer.root.add(this.transcript.diagrams.viewer.root)
this.transcript.diagrams.onOpen = () => {
this.composer.blur()
}
this.transcript.diagrams.viewer.onClose = () => {
this.composer.focus()
if (this.activeTurn) this.renderActiveStatus()
else this.status.content = this.readyStatus()
this.updateMeta()
}
this.shell.renderBefore = () => {
const selection = this.renderer.getSelection()
if (selection) applySelectionColors(selection, this.palette.selection)
@@ -562,6 +572,7 @@ export class NanobotTui {
else if (command.command.action === "context") void this.openContext()
else if (command.command.action === "usage") this.openUsage()
else if (command.command.action === "diff") this.openDiff()
else if (command.command.action === "diagrams") { this.clearComposer(); this.transcript.diagrams.open() }
else if (command.command.action === "branch") void this.openBranch()
else if (command.command.action === "detach") this.quit(true)
else if (command.command.action === "exit") this.quit()
@@ -1313,6 +1324,17 @@ export class NanobotTui {
return
}
}
if (this.transcript.diagrams.viewer.visible) {
this.transcript.diagrams.viewer.handleKey(key)
key.preventDefault()
return
}
if (key.ctrl && key.name === "g") {
this.commandMenu.hide()
this.transcript.diagrams.open()
key.preventDefault()
return
}
if (this.diffViewer.visible) {
if (key.ctrl && key.name === "c") {
const selected = this.renderer.getSelection()?.getSelectedText()
+6
View File
@@ -25,6 +25,12 @@ export const LOCAL_COMMANDS: TuiCommand[] = [
description: "Show context occupancy and recent model-call input tokens",
action: "usage",
},
{
command: "/diagrams",
title: "Mermaid diagrams",
description: "View, zoom and copy diagrams in this conversation (Ctrl+G)",
action: "diagrams",
},
{
command: "/diff",
title: "Last turn diff",
+292
View File
@@ -0,0 +1,292 @@
import { afterEach, expect, spyOn, test } from "bun:test"
import { existsSync } from "node:fs"
import { ImageRenderable, TextareaRenderable, TextRenderable, type Renderable } from "@opentui/core"
import { createTestRenderer, type TestRendererSetup } from "@opentui/core/testing"
import type { Transcript } from "../rendering/transcript"
import { mount, waitUntil } from "./test-support"
let setup: TestRendererSetup | undefined
afterEach(() => { if (setup && !setup.renderer.isDestroyed) setup.renderer.destroy(); setup = undefined })
function images(root: Renderable): ImageRenderable[] {
return root.getChildren().flatMap(child => [ ...(child instanceof ImageRenderable ? [child] : []), ...images(child) ])
}
const source = "graph TD\n A[中文长标签 English] --> B[完成]"
const content = `\`\`\`mermaid\n${source}\n\`\`\`\n\nTrailing paragraph`
test("streamed stable Mermaid fences stay source until reply completion, including the viewer", async () => {
setup = await createTestRenderer({ width: 80, height: 35 })
const app = mount(setup)
const transcript = (app as unknown as { transcript: Transcript }).transcript
app.accept({ event: "delta", chat_id: "chat", text: content })
await setup.flush()
expect(setup.captureCharFrame()).toContain("source while replying")
setup.mockInput.pressKey("\x07")
await setup.flush()
expect(setup.captureCharFrame()).toContain("Replying · original source")
expect(images(transcript.diagrams.viewer.root)).toHaveLength(0)
setup.mockInput.pressKey("q")
app.accept({ event: "stream_end", chat_id: "chat", text: content })
await setup.flush()
expect(setup.captureCharFrame()).toContain("no image protocol")
expect(setup.captureCharFrame()).toContain("A[中文长标签 English]")
expect(images(transcript.root)).toHaveLength(0)
})
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("viewer preserves preview footprint, transcript position and draft; session reset releases images", async () => {
setup = await createTestRenderer({ width: 100, height: 42 })
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
const app = mount(setup)
const transcript = (app as unknown as { transcript: Transcript }).transcript
app.accept({ event: "delta", chat_id: "chat", text: content })
app.accept({ event: "stream_end", chat_id: "chat", text: content })
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
await setup.flush()
const preview = images(transcript.root)[0]!
const render = spyOn(transcript.diagrams.jobs, "render")
const raster = spyOn(transcript.diagrams.jobs, "raster")
const height = transcript.root.scrollHeight
const top = transcript.root.scrollTop
const composer = setup.renderer.root.findDescendantById("nanobot-tui-composer") as TextareaRenderable
composer.setText("keep draft 中文")
setup.mockInput.pressKey("\x07")
await setup.flush()
await setup.flush()
expect(transcript.root.scrollHeight).toBe(height)
expect(images(transcript.root)[0]).toBe(preview)
expect(preview.visible).toBe(false)
await waitUntil(() => images(transcript.diagrams.viewer.root).some(image => image.image !== null), 5000)
await setup.flush()
const dialogImage = images(transcript.diagrams.viewer.root)[0]
const rasters = raster.mock.calls.length
expect(raster).not.toHaveBeenCalled()
setup.renderer.copyToClipboardOSC52 = () => true
setup.mockInput.pressKey("c")
await setup.flush()
expect(setup.captureCharFrame()).toContain("Source copied")
setup.mockInput.pressKey("q")
await setup.flush()
expect(images(transcript.root)[0]).toBe(preview)
expect(preview.visible).toBe(true)
setup.mockInput.pressKey("\x07")
await setup.flush()
expect(images(transcript.diagrams.viewer.root)[0]).toBe(dialogImage)
expect(setup.captureCharFrame()).toContain("100% fit")
expect(render).not.toHaveBeenCalled()
expect(raster.mock.calls.length).toBe(rasters)
setup.mockInput.pressKey("q")
await setup.flush()
render.mockRestore()
raster.mockRestore()
expect(composer.plainText).toBe("keep draft 中文")
expect(transcript.root.scrollTop).toBe(top)
expect(transcript.root.scrollHeight).toBe(height)
transcript.reset({ workspace: "next session", version: "test" })
await setup.flush()
expect(images(transcript.root)).toHaveLength(0)
expect(transcript.diagrams.viewer.visible).toBe(false)
}, 15_000)
test("diagram source copy uses the original fence text and cancelled viewer work stays source", async () => {
setup = await createTestRenderer({ width: 90, height: 30 })
const app = mount(setup)
let copied = ""
setup.renderer.copyToClipboardOSC52 = text => { copied = text; return true }
app.accept({ event: "delta", chat_id: "chat", text: content })
app.accept({ event: "stream_end", chat_id: "chat", text: content })
await setup.flush()
setup.mockInput.pressKey("\x07")
await setup.flush()
setup.mockInput.pressKey("c")
await setup.flush()
expect(copied).toBe(source)
setup.mockInput.pressKey("x")
await setup.flush()
expect(setup.captureCharFrame()).toContain("Rendering cancelled")
expect(setup.captureCharFrame()).toContain("A[中文长标签 English]")
})
test("diagram command on ordinary Markdown shows an empty viewer and returns to the draft", async () => {
setup = await createTestRenderer({ width: 80, height: 35 })
const app = mount(setup)
app.accept({ event: "stream_end", chat_id: "chat", text: "Ordinary **Markdown**" })
const composer = setup.renderer.root.findDescendantById("nanobot-tui-composer") as TextareaRenderable
composer.setText("draft")
setup.mockInput.pressKey("\x07")
await setup.flush()
const header = setup.renderer.root.findDescendantById("diagrams-header") as TextRenderable
expect(header.content.chunks.map(chunk => chunk.text).join("")).toBe("Mermaid")
expect(setup.captureCharFrame()).toContain("No Mermaid diagrams in this conversation.")
expect(setup.captureCharFrame()).toContain("q/esc back")
setup.mockInput.pressKey("q")
await setup.flush()
expect(setup.captureCharFrame()).toContain("draft")
})
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("inline image uses real terminal pixels without upscaling small diagrams", async () => {
setup = await createTestRenderer({ width: 100, height: 42 })
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
Object.defineProperty(setup.renderer, "resolution", { value: { width: 1300, height: 1008 } })
const app = mount(setup)
const transcript = (app as unknown as { transcript: Transcript }).transcript
app.accept({ event: "stream_end", chat_id: "chat", text: content })
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
await setup.flush()
const image = images(transcript.root)[0]!
expect(image.width).toBeLessThan(80)
expect(image.image!.width).toBe(image.width * 13)
expect(image.image!.height).toBe(image.height * 24)
expect(image.height).toBeLessThanOrEqual(24)
}, 10_000)
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("source-only and cancelled viewer release the shared worker; image demand resumes explicitly", async () => {
setup = await createTestRenderer({ width: 100, height: 42 })
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
const app = mount(setup)
const transcript = (app as unknown as { transcript: Transcript }).transcript
const spawn = spyOn(Bun, "spawn")
try {
app.accept({ event: "stream_end", chat_id: "chat", text: content })
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
const first = spawn.mock.results[0]!.value as Bun.Subprocess
setup.mockInput.pressKey("\x07")
await setup.flush()
await waitUntil(() => images(transcript.diagrams.viewer.root).some(image => image.image !== null), 5000)
expect(spawn).toHaveBeenCalledTimes(1)
expect(first.killed).toBe(false)
setup.mockInput.pressKey("s")
await setup.flush()
await first.exited
expect(first.killed).toBe(true)
setup.mockInput.pressKey("+")
await setup.flush()
expect(spawn).toHaveBeenCalledTimes(1)
setup.mockInput.pressKey("s")
await setup.flush()
setup.mockInput.pressKey("+")
await setup.flush()
await waitUntil(() => spawn.mock.calls.length === 2 && transcript.diagrams.jobs.idle, 5000)
const second = spawn.mock.results[1]!.value as Bun.Subprocess
expect(second.killed).toBe(false)
setup.mockInput.pressKey("x")
await setup.flush()
await second.exited
expect(second.killed).toBe(true)
expect(setup.captureCharFrame()).toContain("Rendering cancelled")
expect(setup.captureCharFrame()).toContain("A[中文长标签 English]")
setup.mockInput.pressKey("s")
await setup.flush()
await waitUntil(() => images(transcript.diagrams.viewer.root).some(image => image.image !== null), 5000)
await setup.flush()
expect(setup.captureCharFrame()).toContain("150% fit")
expect(spawn).toHaveBeenCalledTimes(3)
transcript.reset({ workspace: "next session", version: "test" })
expect(transcript.diagrams.layouts.retainedBytes).toBe(0)
} finally { transcript.diagrams.jobs.dispose(); spawn.mockRestore() }
}, 20_000)
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("offscreen return and cell-height changes reuse SVG; theme and session changes invalidate it", async () => {
setup = await createTestRenderer({ width: 100, height: 42 })
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
const resolution = { width: 1000, height: 840 }
Object.defineProperty(setup.renderer, "resolution", { value: resolution })
const app = mount(setup)
const transcript = (app as unknown as { transcript: Transcript }).transcript
app.accept({ event: "stream_end", chat_id: "chat", text: content })
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
await setup.flush()
const render = spyOn(transcript.diagrams.jobs, "render")
const raster = spyOn(transcript.diagrams.jobs, "raster")
try {
const bytes = transcript.diagrams.layouts.retainedBytes
app.accept({ event: "stream_end", chat_id: "chat", text: Array.from({ length: 60 }, (_, i) => `Paragraph ${i}\n`).join("\n") })
await setup.flush()
expect(images(transcript.root)).toHaveLength(0)
expect(transcript.diagrams.layouts.retainedBytes).toBe(bytes)
transcript.root.scrollTo(0)
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
await setup.flush()
expect(render).not.toHaveBeenCalled()
expect(raster).toHaveBeenCalledTimes(1)
resolution.height = 1008
setup.renderer.requestRender()
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
expect(render).not.toHaveBeenCalled()
expect(raster).toHaveBeenCalledTimes(2)
expect(transcript.diagrams.layouts.retainedBytes).toBe(bytes)
transcript.diagrams.setTheme({ ...transcript.diagrams.colors(), accent: "#dd8800" })
expect(transcript.diagrams.layouts.retainedBytes).toBe(0)
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
expect(render).toHaveBeenCalledTimes(1)
transcript.reset({ workspace: "next session", version: "test" })
expect(transcript.diagrams.layouts.retainedBytes).toBe(0)
} finally { render.mockRestore(); raster.mockRestore() }
}, 20_000)
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("viewer navigation releases old layout pins under the real transcript budget", async () => {
setup = await createTestRenderer({ width: 100, height: 42 })
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
Object.defineProperty(setup.renderer, "resolution", { value: { width: 1000, height: 840 } })
const app = mount(setup)
const transcript = (app as unknown as { transcript: Transcript }).transcript
for (let index = 0; index < 8; index++) {
const source = `flowchart TD\n${Array.from({ length: 40 }, (_, i) => `A${i}["数据库请求处理权限验证任务执行记录完成 ${index}-${i}"]`).join("\n")}\n${Array.from({ length: 39 }, (_, i) => `A${i} --> A${i + 1}`).join("\n")}`
app.accept({ event: "stream_end", chat_id: "chat", text: `Graph ${index + 1}\n\n\`\`\`mermaid\n${source}\n\`\`\`\n\nEnd of graph ${index + 1}` })
}
await setup.flush()
await waitUntil(() => images(transcript.root).some(image => image.image !== null), 5000)
const preview = images(transcript.root)[0]!
setup.mockInput.pressKey("\x07")
await setup.flush()
const note = setup.renderer.root.findDescendantById("diagrams-note") as TextRenderable
const ready = () => images(transcript.diagrams.viewer.root).some(image => image.image !== null)
|| note.content.chunks.some(chunk => chunk.text.includes("original source"))
await waitUntil(ready, 5000)
expect(images(transcript.diagrams.viewer.root)).toHaveLength(1)
for (const direction of ["[", "]"]) {
for (let index = 0; index < 7; index++) {
setup.mockInput.pressKey(direction)
await setup.flush()
await waitUntil(ready, 5000)
expect(images(transcript.diagrams.viewer.root)).toHaveLength(1)
expect(transcript.diagrams.layouts.retainedBytes).toBeLessThanOrEqual(4_000_000)
}
}
expect(preview.visible).toBe(false)
setup.mockInput.pressKey("q")
await setup.flush()
expect(images(transcript.root)[0]).toBe(preview)
expect(preview.visible).toBe(true)
}, 25_000)
test.skipIf(!existsSync("C:/Windows/Fonts/msyh.ttc"))("source/image retry after a parser failure keeps the original source visible", async () => {
setup = await createTestRenderer({ width: 100, height: 42 })
Object.defineProperty(setup.renderer, "capabilities", { value: { kitty_graphics: true, multiplexer: "none", image_protocol: "auto" } })
const app = mount(setup)
const transcript = (app as unknown as { transcript: Transcript }).transcript
app.accept({ event: "stream_end", chat_id: "chat", text: "```mermaid\ngraph TD\n A -->\n```" })
await setup.flush()
setup.mockInput.pressKey("\x07")
await setup.flush()
const sourceScroll = setup.renderer.root.findDescendantById("diagrams-source-scroll")!
await waitUntil(() => sourceScroll.visible && transcript.diagrams.jobs.idle, 5000)
setup.mockInput.pressKey("s")
await setup.flush()
await waitUntil(() => sourceScroll.visible && transcript.diagrams.jobs.idle, 5000)
await setup.flush()
expect(setup.captureCharFrame()).toContain("original source")
expect(setup.captureCharFrame()).toContain("A -->")
}, 15_000)
+3 -1
View File
@@ -48,9 +48,11 @@ describe("NanobotTui with a Herdr pane title reporter", () => {
await setup.mockInput.typeText("/")
await setup.flush()
const commandFrame = setup.captureCharFrame()
expect(commandFrame).toContain("/sessions")
expect(commandFrame).toContain("/new-chat")
expect(commandFrame).toContain("/branch")
await setup.mockInput.typeText("sessions")
await setup.flush()
expect(setup.captureCharFrame()).toContain("/sessions")
setup.mockInput.pressEscape()
ui.composer.setText("")
+8
View File
@@ -4,6 +4,7 @@ import {
SyntaxStyle,
TextAttributes,
parseColor,
rgbToHex,
type ColorInput,
type TextChunk,
} from "@opentui/core"
@@ -142,6 +143,13 @@ export function transcriptTheme(palette: Palette, backgroundKnown: boolean): Tra
userBackground: backgroundKnown ? palette.userBackground : null,
border: palette.border,
syntax: syntaxStyle(palette),
diagramColors: {
foreground: rgbToHex(parseColor(palette.text)),
muted: rgbToHex(parseColor(palette.muted)),
surface: rgbToHex(parseColor(palette.userBackground)),
border: rgbToHex(parseColor(palette.border)),
accent: rgbToHex(parseColor(palette.accent)),
},
}
}
+11 -2
View File
@@ -1,10 +1,19 @@
import { NanobotTui, sessionExitMessage, type AppOptions } from "./app"
import type { NanobotTui as App, AppOptions } from "./app"
import { desktopConnectionSource } from "./platform/desktop"
if (process.argv.slice(2).join(" ") === "--render-mermaid") {
const { runDiagramWorker } = await import("./rendering/mermaid/worker")
await runDiagramWorker()
process.exit(0)
}
if (process.argv.slice(2).join(" ") === "--desktop-protocol") {
process.stdout.write("1\n")
process.exit(0)
}
// Initialize OpenTUI before the UI graph: compiled async cycles otherwise expose an
// uninitialized CliRenderer binding. Raster children never enter this graph.
await import("@opentui/core")
const { NanobotTui, sessionExitMessage } = await import("./app")
const desktop = desktopConnectionSource()
// Keep in sync with _TUI_DETACH_EXIT_CODE in nanobot/cli/tui_launcher.py.
@@ -53,7 +62,7 @@ const options: AppOptions = {
},
}
let app: NanobotTui | undefined
let app: App | undefined
let shuttingDown = false
const shutdown = (code = 0) => {
+1
View File
@@ -11,6 +11,7 @@ type TuiCommandAction =
| "context"
| "usage"
| "diff"
| "diagrams"
| "branch"
| "detach"
| "exit"
+238
View File
@@ -0,0 +1,238 @@
import {
BoxRenderable, CodeRenderable, ImageRenderable, NativeImage, TextRenderable,
resolveImageRenderProtocol, type CliRenderer,
} from "@opentui/core"
import type { DiagramJobs } from "./jobs"
import { diagramLayoutKey, type DiagramLayoutLease, type DiagramLayouts } from "./cache"
import { diagramFrame } from "./geometry"
import type { DiagramFrame, DiagramInput, DiagramLayout } from "./types"
export interface DiagramOwner {
jobs: DiagramJobs
layouts: DiagramLayouts
colors(): DiagramInput["colors"]
inViewport(block: MermaidBlock): boolean
readonly viewerOpen: boolean
restorePreviews(): void
reserve(pixels: number): boolean
release(pixels: number): void
open(block: MermaidBlock): void
released(block: MermaidBlock): void
removed(block: MermaidBlock): void
}
export function diagramProtocol(renderer: CliRenderer): "kitty" | "sixel" | null {
const protocol = resolveImageRenderProtocol("auto", renderer.capabilities, renderer.resolution !== null)
return protocol === "blocks" ? null : protocol
}
export function diagramCellSize(renderer: CliRenderer): { width: number; height: number } {
return renderer.resolution
? { width: renderer.resolution.width / renderer.width, height: renderer.resolution.height / renderer.height }
: { width: 10, height: 20 }
}
/** Source remains authoritative; the native image exists only while this block is on screen. */
export class MermaidBlock extends BoxRenderable {
private readonly label: TextRenderable
private readonly sourceCode: CodeRenderable
private picture: ImageRenderable | null = null
private native: NativeImage | null = null
private request: AbortController | null = null
private releaseWorker: (() => void) | null = null
private pixels = 0
private layoutLease: DiagramLayoutLease | null = null
private frame: DiagramFrame | null = null
private failure = ""
private renderedGeometry = ""
private previewHeight = 0
private wanted = false
constructor(
private readonly renderer: CliRenderer,
private readonly owner: DiagramOwner,
readonly source: string,
code: CodeRenderable,
private readonly state: { settled: boolean },
id: string,
) {
super(renderer, { id, width: "100%", minWidth: 0, flexDirection: "column", flexShrink: 0 })
this.sourceCode = code
this.label = new TextRenderable(renderer, {
id: `${id}-label`, content: "Mermaid · source while replying", fg: owner.colors().muted,
width: "100%", minHeight: 1, flexShrink: 0,
onMouseDown: () => owner.open(this),
})
this.add(this.label)
this.add(code)
this.onLifecyclePass = () => this.sync()
}
get settled(): boolean { return this.state.settled }
get renderedLayout(): DiagramLayout | null { return this.layoutLease?.layout ?? null }
borrowImage(frame: DiagramFrame): NativeImage | null {
return this.native && this.frame && Object.entries(this.frame).every(([key, value]) => frame[key as keyof DiagramFrame] === value)
? this.native.retain() : null
}
borrowLayout(): DiagramLayoutLease | null {
return this.layoutLease ? this.owner.layouts.acquire(this.layoutLease.key) : null
}
private layoutFor(width: number): DiagramLayout | null {
const key = diagramLayoutKey({ source: this.source, width, colors: this.owner.colors() })
if (this.layoutLease?.key !== key) {
this.layoutLease?.release()
this.layoutLease = this.owner.layouts.acquire(key)
}
return this.renderedLayout
}
private keepLayout(layout: DiagramLayout, width: number): DiagramLayout {
if (this.renderedLayout === layout) return layout
this.layoutLease?.release()
this.layoutLease = null
this.layoutLease = this.owner.layouts.store(diagramLayoutKey({ source: this.source, width, colors: this.owner.colors() }), layout)
return this.layoutLease.layout
}
suspend(): void { this.releaseImage() }
hidePreview(): void {
this.request?.abort()
this.request = null
this.setWorkerDemand(false)
if (!this.picture?.visible) return
this.previewHeight = this.height
this.minHeight = this.previewHeight
this.picture.visible = false
}
restorePreview(): void {
if (!this.picture) return
this.picture.visible = true
this.minHeight = 0
this.setWorkerDemand(this.owner.inViewport(this))
}
private setWorkerDemand(wanted: boolean): void {
if (wanted) this.releaseWorker ??= this.owner.jobs.retainWorker()
else { this.releaseWorker?.(); this.releaseWorker = null }
}
invalidate(): void {
this.failure = ""
this.releaseImage()
this.label.fg = this.owner.colors().muted
this.sourceCode.visible = true
this.requestRender()
}
private sync(): void {
if (!this.state.settled) return
if (this.owner.viewerOpen) { this.hidePreview(); return }
if (this.picture && !this.picture.visible) {
this.picture.visible = true
this.minHeight = 0
}
const wanted = this.owner.inViewport(this)
const protocol = diagramProtocol(this.renderer)
const cells = diagramCellSize(this.renderer)
const width = Math.max(1, Math.floor(this.width)) * cells.width
const geometry = `${width}:${cells.width}:${cells.height}`
if (this.renderedGeometry && this.renderedGeometry !== geometry) {
this.failure = ""
this.releaseFrame()
}
this.wanted = wanted
if (!protocol) {
this.releaseImage()
this.sourceCode.visible = true
this.label.content = "Mermaid · no image protocol · Ctrl+G source"
return
}
if (!wanted) { this.releaseImage(); return }
if (this.native || this.request || this.failure) return
this.renderedGeometry = geometry
const request = new AbortController()
this.request = request
this.setWorkerDemand(true)
this.label.content = "Mermaid · rendering… · Ctrl+G view / cancel"
const viewport = { width, height: 24 * cells.height, cellWidth: cells.width, cellHeight: cells.height }
const layout = this.layoutFor(width)
const work = layout
? this.owner.jobs.raster(layout, diagramFrame(layout, viewport), request.signal).then(image => ({ ...image, layout }))
: this.owner.jobs.render({ source: this.source, width, scale: 1, viewport, colors: this.owner.colors() }, request.signal)
void work.then(image => {
if (this.isDestroyed || this.request !== request || !this.wanted) return
this.keepLayout(image.layout, width)
const pixels = image.width * image.height
if (!this.owner.reserve(pixels)) throw new Error("Visible image memory limit")
this.pixels = pixels
try { this.native = NativeImage.decode(image.png) }
catch (error) { this.owner.release(this.pixels); this.pixels = 0; throw error }
this.frame = diagramFrame(image.layout, viewport)
this.picture = new ImageRenderable(this.renderer, {
id: `${this.id}-image`, source: this.native,
width: Math.round(image.width / cells.width), height: Math.round(image.height / cells.height),
alignSelf: "center", fit: "fill", protocol,
onError: () => this.fail("Terminal image load failed"),
})
this.add(this.picture)
this.sourceCode.visible = false
this.minHeight = 0
this.label.content = "Mermaid · Ctrl+G view / source / copy"
})
.catch(error => {
if (this.isDestroyed || this.request !== request || request.signal.aborted) return
this.fail(error instanceof Error ? error.message : "Rendering failed")
})
.finally(() => {
if (this.request === request) this.request = null
if (!this.isDestroyed) this.requestRender()
})
}
private fail(message: string): void {
this.failure = message
this.releaseImage()
this.sourceCode.visible = true
// Only controlled renderer errors, not untrusted Mermaid source, enter the status line.
this.label.content = `Mermaid · ${message.replace(/[\r\n\x00-\x1f]/g, " ").slice(0, 120)} · Ctrl+G source`
}
private releaseImage(): void {
this.setWorkerDemand(false)
this.releaseFrame()
this.layoutLease?.release()
this.layoutLease = null
}
private releaseFrame(): void {
this.request?.abort()
this.request = null
if (this.picture) this.previewHeight = Math.max(this.height, this.picture.height + this.label.height)
if (this.picture) {
this.remove(this.picture)
this.picture.destroyRecursively()
this.picture = null
}
this.native?.dispose()
this.native = null
this.frame = null
if (this.pixels) this.owner.release(this.pixels)
this.pixels = 0
// Keep the known preview footprint while culled; scrolling back does not jump.
if (this.previewHeight && !this.sourceCode.visible) {
this.minHeight = this.previewHeight
}
this.owner.released(this)
}
protected override destroySelf(): void {
this.releaseImage()
this.owner.removed(this)
super.destroySelf()
}
}
+57
View File
@@ -0,0 +1,57 @@
import { expect, test } from "bun:test"
import { DiagramLayouts, diagramLayoutKey } from "./cache"
import type { DiagramInput, DiagramLayout } from "./types"
const input: DiagramInput = {
source: "graph TD\n A[中文] --> B", width: 800, scale: 1,
colors: { foreground: "#111", muted: "#666", surface: "#eee", border: "#888", accent: "#36c" },
}
const layout = (text: string): DiagramLayout => ({ svg: `<svg>${text}</svg>`, width: 100, height: 50 })
test("layout identity includes source, physical width, fonts and palette, not raster geometry", () => {
const key = diagramLayoutKey(input)
const zoomed: DiagramInput = { ...input, scale: 4, viewport: { height: 720, cellWidth: 9, cellHeight: 24 } }
expect(diagramLayoutKey(zoomed)).toBe(key)
expect(diagramLayoutKey({ ...input, source: "graph TD\n B --> C" })).not.toBe(key)
expect(diagramLayoutKey({ ...input, width: 799 })).not.toBe(key)
expect(diagramLayoutKey({ ...input, colors: { ...input.colors, foreground: "#eee" } })).not.toBe(key)
const font = { path: "font.ttf", postscriptName: "Font" }
const withFonts = { ...input, fonts: { regular: font, bold: font, mono: font } }
expect(diagramLayoutKey(withFonts)).not.toBe(key)
expect(diagramLayoutKey({ ...withFonts, fonts: { ...withFonts.fonts, bold: { ...font, postscriptName: "Bold" } } })).not.toBe(diagramLayoutKey(withFonts))
})
test("offscreen layouts reuse one bounded LRU while visible pins cannot be evicted", () => {
const first = layout("first"), second = layout("second"), third = layout("third")
const cache = new DiagramLayouts(Buffer.byteLength(first.svg) + Buffer.byteLength(second.svg))
const a = cache.store("a", first), b = cache.store("b", second)
expect(() => cache.store("c", third)).toThrow("Visible diagram layout limit")
a.release()
b.release()
const returned = cache.acquire("a")!
expect(returned.layout).toBe(first)
returned.release()
const c = cache.store("c", third)
expect(cache.acquire("b")).toBeNull()
const pinned = cache.acquire("a")!
expect(pinned.layout).toBe(first)
expect(cache.retainedBytes).toBe(Buffer.byteLength(first.svg) + Buffer.byteLength(third.svg))
pinned.release()
pinned.release()
c.release()
})
test("reset removes old layouts and releases cannot resurrect the previous cache", () => {
const cache = new DiagramLayouts()
const old = cache.store("diagram", layout("old"))
old.release()
cache.clear()
const current = cache.store("diagram", layout("new"))
old.release()
expect(cache.acquire("diagram")!.layout).toBe(current.layout)
expect(cache.retainedBytes).toBe(Buffer.byteLength(current.layout.svg))
current.release()
cache.clear()
expect(cache.retainedBytes).toBe(0)
expect(cache.acquire("diagram")).toBeNull()
})
+66
View File
@@ -0,0 +1,66 @@
import { createHash } from "node:crypto"
import { DIAGRAM_FONT_SIZE, type DiagramInput, type DiagramLayout } from "./types"
export function diagramLayoutKey(input: Pick<DiagramInput, "source" | "width" | "fonts" | "colors">): string {
const { source, width, fonts, colors } = input
return createHash("sha256").update(JSON.stringify([
source, width, DIAGRAM_FONT_SIZE,
fonts ? [fonts.regular.path, fonts.regular.postscriptName, fonts.bold.path, fonts.bold.postscriptName, fonts.mono.path, fonts.mono.postscriptName] : null,
colors.foreground, colors.muted, colors.surface, colors.border, colors.accent,
])).digest("hex")
}
export interface DiagramLayoutLease {
key: string
layout: DiagramLayout
release(): void
}
type Entry = { layout: DiagramLayout; bytes: number; users: number }
/** Visible pins and offscreen LRU entries share one transcript's vector budget. */
export class DiagramLayouts {
private readonly entries = new Map<string, Entry>()
private bytes = 0
constructor(private readonly limit = 4_000_000) {}
get retainedBytes(): number { return this.bytes }
acquire(key: string): DiagramLayoutLease | null {
const entry = this.entries.get(key)
return entry ? this.pin(key, entry) : null
}
store(key: string, layout: DiagramLayout): DiagramLayoutLease {
const existing = this.acquire(key)
if (existing) return existing
const bytes = Buffer.byteLength(layout.svg)
if (bytes > this.limit) throw new Error("Visible diagram layout limit")
for (const [oldKey, entry] of this.entries) {
if (this.bytes + bytes <= this.limit) break
if (entry.users) continue
this.entries.delete(oldKey)
this.bytes -= entry.bytes
}
if (this.bytes + bytes > this.limit) throw new Error("Visible diagram layout limit")
const entry = { layout, bytes, users: 0 }
this.entries.set(key, entry)
this.bytes += bytes
return this.pin(key, entry)
}
clear(): void { this.entries.clear(); this.bytes = 0 }
private pin(key: string, entry: Entry): DiagramLayoutLease {
this.entries.delete(key)
this.entries.set(key, entry)
entry.users++
let released = false
return { key, layout: entry.layout, release: () => {
if (released) return
released = true
entry.users--
} }
}
}
+87
View File
@@ -0,0 +1,87 @@
import { CodeRenderable, type CliRenderer, type ScrollBoxRenderable } from "@opentui/core"
import { MermaidBlock, type DiagramOwner } from "./block"
import { DiagramJobs } from "./jobs"
import { DiagramLayouts } from "./cache"
import type { DiagramInput } from "./types"
import { DiagramViewer } from "./viewer"
const RETAINED_PIXEL_LIMIT = 16_000_000
/** One transcript owns all render jobs and native images, including its overlay. */
export class TranscriptDiagrams implements DiagramOwner {
readonly jobs = new DiagramJobs()
readonly layouts = new DiagramLayouts()
readonly viewer: DiagramViewer
onOpen?: () => void
private readonly blocks = new Set<MermaidBlock>()
private retainedPixels = 0
constructor(
private readonly renderer: CliRenderer,
private readonly viewport: ScrollBoxRenderable,
private palette: DiagramInput["colors"],
) {
this.viewer = new DiagramViewer(renderer, this)
}
colors(): DiagramInput["colors"] { return this.palette }
create(source: string, code: CodeRenderable, state: { settled: boolean }, id: string): MermaidBlock {
const block = new MermaidBlock(this.renderer, this, source, code, state, id)
this.blocks.add(block)
return block
}
inViewport(block: MermaidBlock): boolean {
const view = this.viewport.viewport
return !this.viewer.visible && block.width > 0 && view.height > 0
&& block.screenY < view.screenY + view.height && block.screenY + block.height > view.screenY
}
get viewerOpen(): boolean { return this.viewer.visible }
reserve(pixels: number): boolean {
if (pixels + this.retainedPixels > RETAINED_PIXEL_LIMIT) return false
this.retainedPixels += pixels
return true
}
release(pixels: number): void { this.retainedPixels -= pixels }
released(block: MermaidBlock): void { this.viewer.evict(block) }
removed(block: MermaidBlock): void { this.blocks.delete(block) }
open(block?: MermaidBlock): void {
const blocks = [...this.blocks].filter(item => !item.isDestroyed).sort((a, b) => a.screenY - b.screenY)
const selected = block ? blocks.indexOf(block) : blocks.indexOf(blocks.filter(item => this.inViewport(item)).at(-1)!)
const index = selected >= 0 ? selected : Math.max(0, blocks.length - 1)
// Transfer worker ownership before culling previews; the dialog shares the selected image.
this.viewer.show(blocks, index)
for (const item of blocks) {
if (item === blocks[index]) item.hidePreview()
else item.suspend()
}
this.onOpen?.()
}
restorePreviews(): void { for (const block of this.blocks) block.restorePreview() }
setTheme(colors: DiagramInput["colors"]): void {
this.palette = colors
for (const block of this.blocks) block.invalidate()
this.layouts.clear()
this.viewer.setTheme()
}
reset(): void {
this.viewer.clear()
for (const block of this.blocks) block.suspend()
this.layouts.clear()
this.jobs.reset()
}
destroy(): void {
this.reset()
this.jobs.dispose()
if (!this.viewer.root.isDestroyed) this.viewer.root.destroyRecursively()
}
}
+12
View File
@@ -0,0 +1,12 @@
import { DOMParser, XMLSerializer } from "@xmldom/xmldom"
import type { DiagramFrame } from "./types"
/** Transform only the already validated image document; frame jobs need no CSS resolver. */
export function frameDiagramSvg(svg: string, frame: DiagramFrame): string {
const document = new DOMParser().parseFromString(svg, "image/svg+xml")
const root = document.documentElement
root.setAttribute("width", String(frame.width))
root.setAttribute("height", String(frame.height))
root.setAttribute("viewBox", `${frame.x} ${frame.y} ${frame.viewWidth} ${frame.viewHeight}`)
return new XMLSerializer().serializeToString(document)
}
+26
View File
@@ -0,0 +1,26 @@
import { MAX_DIMENSION, MAX_PIXELS, OUTPUT_LIMIT, type DiagramFrame, type DiagramLayout } from "./types"
export function validateDiagramFrame(layout: DiagramLayout, frame: DiagramFrame): void {
if (![frame.width, frame.height].every(value => Number.isSafeInteger(value) && value > 0 && value <= MAX_DIMENSION)
|| frame.width * frame.height > MAX_PIXELS || ![frame.x, frame.y, frame.viewWidth, frame.viewHeight].every(Number.isFinite)
|| frame.viewWidth <= 0 || frame.viewHeight <= 0 || Buffer.byteLength(layout.svg) > OUTPUT_LIMIT) throw new Error("Diagram pixel limit")
}
/** Rasterize the final cell footprint; padding never stretches the diagram itself. */
export function diagramFrame(
natural: { width: number; height: number },
viewport: { width: number; height: number; cellWidth: number; cellHeight: number },
zoom = 1, panX = 0, panY = 0,
): DiagramFrame {
const { width, height, cellWidth, cellHeight } = viewport
if (![width, height, cellWidth, cellHeight, zoom].every(value => Number.isFinite(value) && value > 0)) throw new Error("Diagram viewport limit")
const scale = Math.min(1, width / natural.width, height / natural.height) * zoom
const pixelsWide = Math.round(Math.min(Math.round(width / cellWidth), Math.ceil(natural.width * scale / cellWidth)) * cellWidth)
const pixelsHigh = Math.round(Math.min(Math.round(height / cellHeight), Math.ceil(natural.height * scale / cellHeight)) * cellHeight)
const viewWidth = pixelsWide / scale, viewHeight = pixelsHigh / scale
return {
width: pixelsWide, height: pixelsHigh, viewWidth, viewHeight,
x: viewWidth >= natural.width ? -(viewWidth - natural.width) / 2 : (natural.width - viewWidth) * panX,
y: viewHeight >= natural.height ? -(viewHeight - natural.height) / 2 : (natural.height - viewHeight) * panY,
}
}
+182
View File
@@ -0,0 +1,182 @@
import { fileURLToPath } from "node:url"
import { jsonLines, REPLY_LIMIT, REQUEST_LIMIT } from "./protocol"
import { MAX_DIMENSION, MAX_PIXELS, OUTPUT_LIMIT, SOURCE_LIMIT, type DiagramAsset, type DiagramFrame, type DiagramImage, type DiagramInput, type DiagramLayout, type DiagramWork } from "./types"
declare const NANOBOT_COMPILED: boolean
function launch() {
const args = typeof NANOBOT_COMPILED !== "undefined" && NANOBOT_COMPILED
? [process.execPath, "--render-mermaid"]
: [process.execPath, fileURLToPath(new URL("../../index.ts", import.meta.url)), "--render-mermaid"]
return Bun.spawn(args, { stdin: "pipe", stdout: "pipe", stderr: "pipe", windowsHide: true })
}
type RasterProcess = ReturnType<typeof launch>
interface Job {
work: DiagramWork
signal: AbortSignal
resolve: (image: DiagramImage | DiagramAsset) => void
reject: (error: Error) => void
cancel: () => void
}
async function readBounded(stream: ReadableStream<Uint8Array>, limit: number): Promise<string> {
const reader = stream.getReader()
const chunks: Uint8Array[] = []
let size = 0
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
size += value.length
if (size > limit) throw new Error("Diagram worker output limit")
chunks.push(value)
}
return Buffer.concat(chunks).toString("utf8")
} finally { reader.releaseLock() }
}
/** One terminable child, retained only by visible diagrams or their viewer. */
export class DiagramJobs {
private queue: Job[] = []
private active: { job: Job; timer: ReturnType<typeof setTimeout> } | null = null
private process: { child: RasterProcess; closing: boolean; error?: Error } | null = null
private retainers = new Set<object>()
private disposed = false
get idle(): boolean { return !this.active && !this.queue.length }
render(input: DiagramInput, signal: AbortSignal): Promise<DiagramAsset> {
if (Buffer.byteLength(input.source) > SOURCE_LIMIT) return Promise.reject(new Error("Diagram source limit"))
return this.enqueue<DiagramAsset>({ kind: "render", input }, signal)
}
raster(layout: DiagramLayout, frame: DiagramFrame, signal: AbortSignal): Promise<DiagramImage> {
return this.enqueue<DiagramImage>({ kind: "raster", layout, frame }, signal)
}
reset(): void {
for (const job of [...this.queue]) job.cancel()
this.stop(new Error("Diagram cancelled"))
}
dispose(): void { this.disposed = true; this.reset() }
retainWorker(): () => void {
const owner = {}
this.retainers.add(owner)
return () => { this.retainers.delete(owner); this.start() }
}
private enqueue<T extends DiagramImage>(work: DiagramWork, signal: AbortSignal): Promise<T> {
if (this.disposed || signal.aborted) return Promise.reject(new Error("Diagram cancelled"))
if (this.queue.length >= 8) return Promise.reject(new Error("Diagram render queue full"))
return new Promise((resolve, reject) => {
const job: Job = { work, signal, resolve: image => resolve(image as T), reject, cancel: () => {
if (this.active?.job === job) this.stop(new Error("Diagram cancelled"))
else {
this.queue = this.queue.filter(queued => queued !== job)
signal.removeEventListener("abort", job.cancel)
reject(new Error("Diagram cancelled"))
}
} }
signal.addEventListener("abort", job.cancel, { once: true })
this.queue.push(job)
this.start()
})
}
private start(): void {
if (this.disposed || this.active || this.process?.error || this.process?.closing) return
const job = this.queue[0]
if (!job) {
if (this.process && !this.retainers.size) this.finishProcess()
return
}
this.queue.shift()
try {
const input = JSON.stringify(job.work)
if (Buffer.byteLength(input) > REQUEST_LIMIT) throw new Error("Diagram input limit")
if (!this.process) {
const process = { child: launch(), closing: false, error: undefined as Error | undefined }
this.process = process
void this.observe(process)
}
const process = this.process
this.active = { job, timer: setTimeout(() => {
if (this.active?.job === job) this.stop(new Error("Diagram render timed out"))
}, 10_000) }
process.child.stdin.write(input + "\n")
void Promise.resolve(process.child.stdin.flush()).catch(() => {
if (this.process === process) this.stop(new Error("Diagram worker pipe failed"))
})
} catch (error) {
if (this.active?.job === job) this.stop(error instanceof Error ? error : new Error("Diagram worker unavailable"))
else {
job.signal.removeEventListener("abort", job.cancel)
job.reject(error instanceof Error ? error : new Error("Diagram worker unavailable"))
this.start()
}
}
}
private async observe(process: NonNullable<DiagramJobs["process"]>): Promise<void> {
const { child } = process
const stderr = readBounded(child.stderr, 8192)
const stdout = (async () => {
for await (const record of jsonLines(child.stdout, REPLY_LIMIT)) {
if (process.error) break
const active = this.active
if (!active) throw new Error("Unexpected diagram worker reply")
const reply = record as { error?: string; image?: { png: string; width: number; height: number; layout?: DiagramLayout } }
let failure: Error | undefined, image: DiagramImage | DiagramAsset | undefined
if (typeof reply.error === "string") failure = new Error(reply.error)
else {
const result = reply.image
if (!result || typeof result.png !== "string" || !Number.isSafeInteger(result.width) || !Number.isSafeInteger(result.height)) throw new Error("Invalid diagram worker result")
if (result.width < 1 || result.height < 1 || result.width > MAX_DIMENSION || result.height > MAX_DIMENSION || result.width * result.height > MAX_PIXELS) throw new Error("Diagram pixel limit")
const png = new Uint8Array(Buffer.from(result.png, "base64"))
if (png.byteLength > OUTPUT_LIMIT) throw new Error("Diagram image limit")
if (active.job.work.kind === "render" && (!result.layout || Buffer.byteLength(result.layout.svg) > OUTPUT_LIMIT)) throw new Error("Invalid diagram layout")
image = { ...result, png }
}
clearTimeout(active.timer)
active.job.signal.removeEventListener("abort", active.job.cancel)
this.active = null
if (failure) active.job.reject(failure)
else active.job.resolve(image!)
this.start()
}
})()
try { await Promise.all([stdout, stderr, child.exited]) }
catch (error) { this.stop(error instanceof Error ? error : new Error("Diagram worker failed")) }
finally {
child.stdin.end()
// Replacement admission includes exit plus both pipe collectors, even on failure.
await Promise.allSettled([stdout, stderr, child.exited])
if (this.process === process) {
if (this.active) {
clearTimeout(this.active.timer)
this.active.job.signal.removeEventListener("abort", this.active.job.cancel)
this.active.job.reject(process.error ?? new Error("Diagram worker exited"))
this.active = null
}
this.process = null
this.start()
}
}
}
private finishProcess(): void {
const process = this.process!
process.closing = true
process.child.kill()
}
private stop(error: Error): void {
const process = this.process
if (!process || process.error) return
process.error = error
process.child.kill()
}
}
+29
View File
@@ -0,0 +1,29 @@
import { OUTPUT_LIMIT, SOURCE_LIMIT } from "./types"
export const REQUEST_LIMIT = OUTPUT_LIMIT * 6 + SOURCE_LIMIT * 6 + 8192
export const REPLY_LIMIT = OUTPUT_LIMIT * 6 + Math.ceil(OUTPUT_LIMIT * 4 / 3) + 8192
/** Bounded records, including incomplete records and UTF-8 split across pipe reads. */
export async function* jsonLines(stream: ReadableStream<Uint8Array>, limit: number): AsyncGenerator<unknown> {
const reader = stream.getReader()
let parts: Uint8Array[] = [], size = 0
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
let start = 0
while (start < value.length) {
const end = value.indexOf(10, start)
const part = value.subarray(start, end < 0 ? value.length : end)
size += part.length
if (size > limit) throw new Error("Diagram worker pipe limit")
parts.push(part)
if (end < 0) break
yield JSON.parse(Buffer.concat(parts).toString("utf8"))
parts = []; size = 0
start = end + 1
}
}
if (size) throw new Error("Incomplete diagram worker reply")
} finally { reader.releaseLock() }
}
+13
View File
@@ -0,0 +1,13 @@
import { Resvg } from "@resvg/resvg-js"
import { validateDiagramFrame } from "./geometry"
import { frameDiagramSvg } from "./frame"
import { OUTPUT_LIMIT, type DiagramFrame, type DiagramImage, type DiagramLayout } from "./types"
/** Initial and interactive frames rasterize the same measured glyph outlines, without font loading. */
export function rasterDiagram(layout: DiagramLayout, frame: DiagramFrame): DiagramImage {
validateDiagramFrame(layout, frame)
const raster = new Resvg(frameDiagramSvg(layout.svg, frame), { font: { loadSystemFonts: false } }).render()
const png = raster.asPng()
if (png.byteLength > OUTPUT_LIMIT) throw new Error("Diagram image limit")
return { png, width: raster.width, height: raster.height }
}
+155
View File
@@ -0,0 +1,155 @@
import { describe, expect, spyOn, test } from "bun:test"
import { NativeImage } from "@opentui/core"
import { existsSync } from "node:fs"
import { renderDiagram } from "./render"
import { rasterDiagram } from "./raster"
import { diagramFrame } from "./geometry"
import { resolveDiagramSvg } from "./svg"
import { DiagramJobs } from "./jobs"
import { jsonLines } from "./protocol"
import { SOURCE_LIMIT, type DiagramInput } from "./types"
const input: DiagramInput = {
source: "graph TD\n A[中文:开始处理长标签] --> B{检查数据}\n B -->|通过| C[完成]",
width: 800, scale: 2,
fonts: {
regular: { path: "C:/Windows/Fonts/msyh.ttc", postscriptName: "MicrosoftYaHei" },
bold: { path: "C:/Windows/Fonts/msyhbd.ttc", postscriptName: "MicrosoftYaHei-Bold" },
mono: { path: "C:/Windows/Fonts/consola.ttf", postscriptName: "Consolas" },
},
colors: { foreground: "#202020", muted: "#666666", surface: "#eeeeee", border: "#888888", accent: "#3366cc" },
}
const svg = (body: string) => `<svg xmlns="http://www.w3.org/2000/svg">${body}</svg>`
test("SVG image policy rejects external resources and executable elements", () => {
for (const body of ['<image href="file:///secret.png"/>', '<script/>', '<foreignObject/>', '<path fill="url(https://example.org/image)"/>']) {
expect(() => resolveDiagramSvg(svg(body), input, "sans-serif", "monospace")).toThrow()
}
})
test("SVG variables resolve nested Pele aliases into concrete theme values", () => {
const result = resolveDiagramSvg('<svg xmlns="http://www.w3.org/2000/svg" style="--_fg:var(--pele-fg,#111)"><text fill="var(--_fg)">Hello</text></svg>', input, "Microsoft YaHei", "Consolas")
expect(result).toContain('fill="#202020"')
expect(result).not.toContain("var(")
})
test("worker records preserve split UTF-8 and multiple replies across pipe reads", async () => {
const bytes = Buffer.from('{"label":"中文"}\n{"width":9}\n')
const stream = new ReadableStream<Uint8Array>({ start(controller) {
controller.enqueue(bytes.subarray(0, 11))
controller.enqueue(bytes.subarray(11, 20))
controller.enqueue(bytes.subarray(20))
controller.close()
} })
const records = []
for await (const record of jsonLines(stream, 100)) records.push(record)
expect(records).toEqual([{ label: "中文" }, { width: 9 }])
})
describe.skipIf(!existsSync(input.fonts!.regular.path))("Windows raster integration", () => {
test("Pele renders Chinese flowchart and sequence diagram to bounded PNGs", async () => {
for (const source of [input.source, "sequenceDiagram\n participant A as 用户\n participant B as 服务\n A->>B: 长标签消息\n B-->>A: 完成"]) {
const image = await renderDiagram({ ...input, source })
expect(Array.from(image.png.subarray(0, 8))).toEqual([137, 80, 78, 71, 13, 10, 26, 10])
expect(image.width).toBeGreaterThan(100)
expect(image.width * image.height).toBeLessThanOrEqual(8_000_000)
expect(image.layout.svg).not.toContain("<text")
const native = NativeImage.decode(image.png)
const again = rasterDiagram(image.layout, { width: image.width, height: image.height, x: 0, y: 0, viewWidth: image.layout.width, viewHeight: image.layout.height })
const raster = NativeImage.decode(again.png)
try {
expect(native.info().hasAlpha).toBe(true)
expect(native.raw().data[3]).toBe(0)
expect(new Bun.CryptoHasher("sha256").update(native.raw().data).digest("hex"))
.toBe(new Bun.CryptoHasher("sha256").update(raster.raw().data).digest("hex"))
} finally { native.dispose(); raster.dispose() }
}
})
test("C4 technology labels and mixed Markdown italics render with the measured fonts", async () => {
const sources = [
'C4Container\n Person(user, "用户")\n Container(api, "API 服务", "TypeScript")\n Rel(user, api, "调用", "HTTPS")',
'C4Component\n Component(api, "API 服务", "TypeScript")\n Component(db, "数据库", "SQL")\n Rel(api, db, "查询", "SQL")',
'C4Deployment\n Deployment_Node(server, "服务器") {\n Container(api, "API 服务", "TypeScript")\n }\n Deployment_Node(data, "数据层") {\n ContainerDb(db, "数据库", "SQL")\n }\n Rel(api, db, "查询", "SQL")',
'flowchart LR\n A["`**执行任务**\n调用 *模型与工具*`"] --> B["`***保存记录***`"]',
]
for (const source of sources) {
const image = await renderDiagram({ ...input, source, scale: 1 })
expect(image.png.byteLength).toBeGreaterThan(100)
const native = NativeImage.decode(image.png)
try { expect(native.info().hasAlpha).toBe(true) } finally { native.dispose() }
}
const plain = await renderDiagram({ ...input, source: 'flowchart LR\n A["`模型与工具`"]', scale: 1 })
const italic = await renderDiagram({ ...input, source: 'flowchart LR\n A["`*模型与工具*`"]', scale: 1 })
expect([italic.width, italic.height]).toEqual([plain.width, plain.height])
expect(new Bun.CryptoHasher("sha256").update(italic.png).digest("hex"))
.not.toBe(new Bun.CryptoHasher("sha256").update(plain.png).digest("hex"))
})
test("inline raster preserves natural size and pads to terminal pixels instead of stretching", async () => {
const natural = await renderDiagram({ ...input, width: 1400, scale: 1 })
const cellWidth = 9, cellHeight = 18
const preview = await renderDiagram({ ...input, width: 1404, scale: 1, viewport: { height: 432, cellWidth, cellHeight } })
expect(preview.width).toBeLessThanOrEqual(natural.width + cellWidth)
expect(preview.height).toBeLessThanOrEqual(natural.height + cellHeight)
expect(preview.width % cellWidth).toBe(0)
expect(preview.height % cellHeight).toBe(0)
const narrow = await renderDiagram({ ...input, width: 360, scale: 1, viewport: { height: 216, cellWidth, cellHeight } })
expect(narrow.width).toBeLessThanOrEqual(360)
expect(narrow.height).toBeLessThanOrEqual(216)
})
test("zoom rasterizes the vector viewport at final pixels rather than enlarging a bitmap", async () => {
const asset = await renderDiagram({ ...input, scale: 1 })
const viewport = { width: 648, height: 432, cellWidth: 9, cellHeight: 18 }
const fit = diagramFrame(asset.layout, viewport)
const zoomed = diagramFrame(asset.layout, viewport, 4, 0.5, 1)
expect(zoomed.viewWidth / zoomed.width).toBeCloseTo(fit.viewWidth / fit.width / 4)
expect(zoomed.y).toBeGreaterThan(0)
const image = await rasterDiagram(asset.layout, zoomed)
expect(image.width).toBe(zoomed.width)
expect(image.height).toBe(zoomed.height)
expect(image.width % 9).toBe(0)
expect(image.height % 18).toBe(0)
})
test("source limit and parser failures are explicit", async () => {
await expect(renderDiagram({ ...input, source: "x".repeat(SOURCE_LIMIT + 1) })).rejects.toThrow("limit")
await expect(renderDiagram({ ...input, source: "graph TD\n A -->" })).rejects.toThrow()
})
test("visible preview and viewer share one worker until both owners release it", async () => {
const spawn = spyOn(Bun, "spawn")
const jobs = new DiagramJobs()
try {
const releasePreview = jobs.retainWorker()
const signal = new AbortController().signal
const batch = [jobs.render(input, signal), jobs.render({ ...input, source: "sequenceDiagram\n A->>B: 中文消息" }, signal)]
const [first] = await Promise.all(batch)
const releaseViewer = jobs.retainWorker()
releasePreview()
const frame = diagramFrame(first!.layout, { width: 648, height: 432, cellWidth: 9, cellHeight: 18 }, 2)
expect((await jobs.raster(first!.layout, frame, signal)).width).toBe(frame.width)
expect((await jobs.raster(first!.layout, frame, signal)).width).toBe(frame.width)
expect(spawn).toHaveBeenCalledTimes(1)
const child = spawn.mock.results[0]!.value as Bun.Subprocess
expect(child.killed).toBe(false)
releaseViewer()
expect(Number.isInteger(await child.exited)).toBe(true)
expect(child.killed).toBe(true)
} finally { jobs.dispose(); spawn.mockRestore() }
}, 20_000)
test("worker cancellation is isolated and the next job completes", async () => {
const jobs = new DiagramJobs()
try {
const cancel = new AbortController()
const first = jobs.render(input, cancel.signal)
const second = jobs.render(input, new AbortController().signal)
cancel.abort()
await expect(first).rejects.toThrow("cancelled")
expect((await second).png.byteLength).toBeGreaterThan(100)
} finally { jobs.dispose() }
}, 20_000)
})
+80
View File
@@ -0,0 +1,80 @@
import { existsSync, readFileSync } from "node:fs"
import { join } from "node:path"
import { create, type Font } from "fontkit"
import { detectType, load, render } from "pele/lazy"
import { diagramFrame } from "./geometry"
import { resolveDiagramSvg } from "./svg"
import { rasterDiagram } from "./raster"
import { outlineDiagramText, shapeDiagramText } from "./text"
import { DIAGRAM_FONT_SIZE, MAX_DIMENSION, MAX_PIXELS, OUTPUT_LIMIT, SOURCE_LIMIT, type DiagramAsset, type DiagramFont, type DiagramInput, type DiagramLayout } from "./types"
type FontFile = ReturnType<typeof create>
function systemFonts(file: (path: string) => FontFile): NonNullable<DiagramInput["fonts"]> {
const windows = join(process.env.SystemRoot || "C:/Windows", "Fonts")
const candidates = process.platform === "win32"
? [[join(windows, "msyh.ttc"), join(windows, "msyhbd.ttc")]]
: process.platform === "darwin"
? [["/System/Library/Fonts/PingFang.ttc", "/System/Library/Fonts/PingFang.ttc"]]
: [["/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", "/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc"],
["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"]]
for (const [regularPath, boldPath] of candidates) {
if (!regularPath || !boldPath || !existsSync(regularPath) || !existsSync(boldPath)) continue
const select = (path: string, bold: boolean): DiagramFont => {
const opened = file(path)
const faces = "fonts" in opened ? opened.fonts : [opened]
const font = faces.find(face => /(?:YaHei$|CJKsc-Regular$|SC-Regular$|DejaVuSans$)/i.test(face.postscriptName) && !bold)
?? faces.find(face => bold && /(?:YaHei-Bold$|CJKsc-Bold$|SC-Semibold$|DejaVuSans-Bold$)/i.test(face.postscriptName))
?? faces[0]!
return { path, postscriptName: font.postscriptName }
}
const regular = select(regularPath, false)
// Use the same CJK-capable family for monospace labels rather than let resvg
// silently pick a different system font for glyphs fontkit did not measure.
return { regular, bold: select(boldPath, true), mono: regular }
}
throw new Error("Diagram font unavailable (install Noto Sans CJK)")
}
type FontSet = { selected: NonNullable<DiagramInput["fonts"]>; regular: Font; bold: Font; mono: Font; system: boolean }
let activeFonts: FontSet | undefined
function getFonts(selected?: DiagramInput["fonts"]): FontSet {
if (activeFonts && (selected ? JSON.stringify(selected) === JSON.stringify(activeFonts.selected) : activeFonts.system)) return activeFonts
const files = new Map<string, FontFile>()
const file = (path: string) => {
if (!files.has(path)) files.set(path, create(readFileSync(path)))
return files.get(path)!
}
const faces = selected ?? systemFonts(file)
const font = (face: DiagramFont): Font => {
const loaded = file(face.path)
const result = "fonts" in loaded ? loaded.fonts.find(font => font.postscriptName === face.postscriptName) : loaded
if (!result || result.postscriptName !== face.postscriptName) throw new Error("Diagram font unavailable")
return result
}
activeFonts = { selected: faces, regular: font(faces.regular), bold: font(faces.bold), mono: font(faces.mono), system: !selected }
return activeFonts
}
// Only the isolated child imports this module. Measurement and outlines share one font set.
export async function renderDiagram(input: DiagramInput): Promise<DiagramAsset> {
if (Buffer.byteLength(input.source) > SOURCE_LIMIT || !Number.isFinite(input.width)
|| input.width < 1 || input.width > MAX_DIMENSION || !Number.isFinite(input.scale)
|| input.scale < 1 || input.scale > 2) throw new Error("Diagram input limit")
if (!await load(detectType(input.source))) throw new Error("Unsupported Mermaid diagram")
const fonts = getFonts(input.fonts)
const result = render(input.source, {
fontFamily: fonts.regular.familyName, fontFamilyMono: fonts.mono.familyName, fontSize: DIAGRAM_FONT_SIZE,
maxWidth: input.width, responsive: false, links: false, images: false,
limit: SOURCE_LIMIT, outputLimit: OUTPUT_LIMIT, maxEdges: 500,
measurer: { width: (text, size, style) => shapeDiagramText(fonts, text, size, style).width },
})
if (!Number.isFinite(result.width) || !Number.isFinite(result.height) || result.width < 1 || result.height < 1
|| result.width > MAX_DIMENSION || result.height > MAX_DIMENSION || result.width * result.height > MAX_PIXELS) throw new Error("Diagram pixel limit")
const resolved = resolveDiagramSvg(result.svg, input, fonts.regular.familyName, fonts.mono.familyName)
const layout: DiagramLayout = { svg: outlineDiagramText(resolved, fonts), width: result.width, height: result.height }
const frame = input.viewport ? diagramFrame(layout, { width: input.width, ...input.viewport }) : {
width: Math.ceil(result.width * input.scale), height: Math.ceil(result.height * input.scale),
x: 0, y: 0, viewWidth: result.width, viewHeight: result.height,
}
return { ...rasterDiagram(layout, frame), layout }
}
+93
View File
@@ -0,0 +1,93 @@
import { DOMParser, XMLSerializer } from "@xmldom/xmldom"
import type { CssNode, ListItem, List } from "css-tree"
// css-tree's split runtime exports avoid its Node-only syntax-data loader in compiled workers.
// @ts-expect-error The package types declare only the barrel entrypoint.
import parseRuntime from "css-tree/parser"
// @ts-expect-error The package types declare only the barrel entrypoint.
import walkRuntime from "css-tree/walker"
// @ts-expect-error The package types declare only the barrel entrypoint.
import generateRuntime from "css-tree/generator"
const parse: typeof import("css-tree").parse = parseRuntime
const walk: typeof import("css-tree").walk = walkRuntime
const generate: typeof import("css-tree").generate = generateRuntime
import type { DiagramInput } from "./types"
import { DIAGRAM_FONT_SIZE, OUTPUT_LIMIT } from "./types"
const elements = new Set([
"svg", "g", "path", "rect", "circle", "ellipse", "line", "polyline", "polygon",
"text", "tspan", "title", "desc", "defs", "marker", "clipPath",
])
// A closed image document for resvg, never HTML or an executable SVG surface.
export function resolveDiagramSvg(svg: string, input: DiagramInput, family: string, mono: string): string {
if (Buffer.byteLength(svg) > OUTPUT_LIMIT || /<!DOCTYPE|<!ENTITY|<\?/i.test(svg)) throw new Error("SVG policy")
const document = new DOMParser({ errorHandler: () => { throw new Error("Invalid SVG") } }).parseFromString(svg, "image/svg+xml")
const root = document.documentElement
if (root.tagName !== "svg") throw new Error("Invalid SVG root")
const colors = input.colors
for (const color of Object.values(colors)) {
if (!/^#[\da-f]{6}$/i.test(color)) throw new Error("Invalid diagram color")
}
const variables = new Map<string, string>([
["--pele-bg", "transparent"], ["--pele-fg", colors.foreground],
["--pele-muted", colors.muted], ["--pele-line", colors.foreground],
["--pele-surface", colors.surface], ["--pele-surface-alt", colors.surface],
["--pele-border", colors.border], ["--pele-accent", colors.accent],
["--pele-font", JSON.stringify(family)], ["--pele-font-mono", JSON.stringify(mono)],
])
const rootStyle = parse(root.getAttribute("style") || "", { context: "declarationList", parseCustomProperty: true })
walk(rootStyle, node => {
if (node.type === "Declaration" && node.property.startsWith("--_")) variables.set(node.property, generate(node.value))
})
const resolve = (value: string, depth = 0): string => {
if (depth > 16) throw new Error("Cyclic SVG variables")
const ast = parse(value, { context: "value", parseCustomProperty: true })
walk(ast, {
leave(node: CssNode, item: ListItem<CssNode>, list: List<CssNode>) {
if (node.type === "Raw") throw new Error("Unsupported SVG CSS")
if (node.type === "Url" && !/^#[\w-]+$/.test(node.value)) throw new Error("External SVG resource")
if (node.type !== "Function" || node.name.toLowerCase() !== "var") return
const args = node.children.toArray()
const name = args[0]
if (name?.type !== "Identifier") throw new Error("Invalid SVG variable")
const comma = args.findIndex(arg => arg.type === "Operator" && arg.value === ",")
const fallback = comma < 0 ? undefined : args.slice(comma + 1).map(node => generate(node)).join(" ")
const replacement = variables.get(name.name) ?? fallback
if (replacement === undefined) throw new Error("Unresolved SVG variable")
const expanded = parse(resolve(replacement, depth + 1), { context: "value" })
if (expanded.type !== "Value" || !item || !list) throw new Error("Invalid SVG variable")
list.replace(item, expanded.children)
},
})
return generate(ast)
}
const nodes = document.getElementsByTagName("*")
if (nodes.length > 10_000) throw new Error("SVG element limit")
for (let i = 0; i < nodes.length; i++) {
const element = nodes.item(i)!
if (!elements.has(element.tagName) || element.namespaceURI !== "http://www.w3.org/2000/svg") throw new Error("SVG element policy")
for (const attribute of Array.from(element.attributes)) {
const name = attribute.name
if (/^on|href$/i.test(name) || (name.includes(":") && name !== "xml:space")) throw new Error(`SVG attribute policy: ${name}`)
if (name === "style") {
const ast = parse(attribute.value, { context: "declarationList", parseCustomProperty: true })
if (ast.type !== "DeclarationList") throw new Error("Invalid SVG style")
ast.children.forEach((node, item, list) => {
if (node.type !== "Declaration") throw new Error("Invalid SVG declaration")
if (node.property.startsWith("--")) { list.remove(item); return }
const value = parse(resolve(generate(node.value)), { context: "value" })
if (value.type !== "Value") throw new Error("Invalid SVG value")
node.value = value
})
element.setAttribute(name, generate(ast))
} else if (/\b(?:var|url)\s*\(/i.test(attribute.value)) {
element.setAttribute(name, resolve(attribute.value))
}
}
}
root.setAttribute("font-family", family)
root.setAttribute("font-size", String(DIAGRAM_FONT_SIZE))
const output = new XMLSerializer().serializeToString(document)
if (Buffer.byteLength(output) > OUTPUT_LIMIT) throw new Error("SVG output limit")
return output
}
+107
View File
@@ -0,0 +1,107 @@
import { DOMParser, XMLSerializer } from "@xmldom/xmldom"
import type { Font } from "fontkit"
import { OUTPUT_LIMIT } from "./types"
export interface DiagramFonts { regular: Font; bold: Font; mono: Font }
export function shapeDiagramText(fonts: DiagramFonts, text: string, size: number, style: number) {
if (!Number.isFinite(size) || size <= 0 || size > 200 || (style & ~7)) throw new Error("Unsupported diagram font style")
const face = style & 1 ? fonts.bold : style & 4 ? fonts.mono : fonts.regular
for (const char of text) {
if (!face.hasGlyphForCodePoint(char.codePointAt(0)!)) throw new Error("Diagram font missing glyph")
}
const run = face.layout(text)
const scale = size / face.unitsPerEm
return { run, scale, width: run.positions.reduce((width, position) => width + position.xAdvance, 0) * scale }
}
/** Use the measured glyphs for every label, including synthetic CJK italics.
* Rasterizers then receive paths only and never reinterpret the font files. */
export function outlineDiagramText(svg: string, fonts: DiagramFonts): string {
const document = new DOMParser().parseFromString(svg, "image/svg+xml")
const property = (element: Element, name: string, fallback = ""): string => {
let current: Node | null = element
while (current?.nodeType === 1) {
const owner = current as Element
// The image policy has already parsed and serialized these declarations.
const inline = (owner.getAttribute("style") || "").split(";").find(value => value.startsWith(`${name}:`))
if (inline) return inline.slice(name.length + 1)
if (owner.hasAttribute(name)) return owner.getAttribute(name)!
current = owner.parentNode
}
return fallback
}
const style = (element: Element) => {
const weight = property(element, "font-weight")
const family = property(element, "font-family").replace(/^["']|["']$/g, "")
return (/^(?:bold|bolder)$/.test(weight) || Number(weight) >= 600 ? 1 : 0)
| (/^(?:italic|oblique)$/.test(property(element, "font-style")) ? 2 : 0)
| (family === fonts.mono.familyName ? 4 : 0)
}
const coordinate = (element: Element, name: string, fallback: number) => element.hasAttribute(name) ? Number(element.getAttribute(name)) : fallback
const group = (element: Element) => {
const result = document.createElementNS("http://www.w3.org/2000/svg", "g")
for (const attribute of Array.from(element.attributes)) {
if (!["x", "y", "text-anchor"].includes(attribute.name)) result.setAttribute(attribute.name, attribute.value)
}
return result
}
type Run = ReturnType<typeof shapeDiagramText> & { owner: Element; italic: boolean }
type Chunk = { x: number; y: number; anchor: string; runs: Run[] }
let bytes = 0
for (const text of Array.from(document.getElementsByTagName("text"))) {
const chunks: Chunk[] = []
let chunk: Chunk = { x: coordinate(text, "x", 0), y: coordinate(text, "y", 0), anchor: property(text, "text-anchor", "start"), runs: [] }
const collect = (owner: Element) => {
if (owner !== text && (owner.hasAttribute("x") || owner.hasAttribute("y"))) {
if (chunk.runs.length) chunks.push(chunk)
chunk = { x: coordinate(owner, "x", chunk.x), y: coordinate(owner, "y", chunk.y), anchor: property(owner, "text-anchor", "start"), runs: [] }
}
for (const child of Array.from(owner.childNodes)) {
if (child.nodeType === 1) collect(child as Element)
else if (child.nodeType === 3 || child.nodeType === 4) {
const flags = style(owner)
const size = Number.parseFloat(property(owner, "font-size", "16"))
chunk.runs.push({ ...shapeDiagramText(fonts, child.textContent || "", size, flags), owner, italic: !!(flags & 2) })
}
}
}
collect(text)
if (chunk.runs.length) chunks.push(chunk)
const outlined = group(text)
for (const chunk of chunks) {
const width = chunk.runs.reduce((sum, run) => sum + run.width, 0)
let x = chunk.x - (chunk.anchor === "middle" ? width / 2 : chunk.anchor === "end" ? width : 0)
for (const run of chunk.runs) {
let parent = outlined
const ancestors: Element[] = []
for (let owner = run.owner; owner !== text; owner = owner.parentNode as Element) ancestors.unshift(owner)
for (const owner of ancestors) {
const nested = group(owner)
parent.appendChild(nested)
parent = nested
}
let advanceX = 0, advanceY = 0
for (let i = 0; i < run.run.glyphs.length; i++) {
const glyph = run.run.glyphs[i]!, position = run.run.positions[i]!
const path = document.createElementNS("http://www.w3.org/2000/svg", "path")
const data = glyph.path.toSVG()
bytes += Buffer.byteLength(data) + 200
if (bytes > OUTPUT_LIMIT) throw new Error("Diagram vector limit")
path.setAttribute("d", data)
const shear = run.italic ? Math.tan(14 * Math.PI / 180) : 0
path.setAttribute("transform", `translate(${x} ${chunk.y}) matrix(${run.scale} 0 ${shear * run.scale} ${-run.scale} 0 0) translate(${advanceX + position.xOffset} ${advanceY + position.yOffset})`)
path.setAttribute("vector-effect", "non-scaling-stroke")
parent.appendChild(path)
advanceX += position.xAdvance
advanceY += position.yAdvance
}
x += run.width
}
}
text.parentNode!.replaceChild(outlined, text)
}
const result = new XMLSerializer().serializeToString(document)
if (Buffer.byteLength(result) > OUTPUT_LIMIT) throw new Error("Diagram vector limit")
return result
}
+47
View File
@@ -0,0 +1,47 @@
export const SOURCE_LIMIT = 50_000
export const OUTPUT_LIMIT = 2_000_000
export const MAX_PIXELS = 8_000_000
export const MAX_DIMENSION = 8192
export const DIAGRAM_FONT_SIZE = 16
export interface DiagramFont {
path: string
postscriptName: string
}
export interface DiagramInput {
source: string
width: number
scale: number
viewport?: { height: number; cellWidth: number; cellHeight: number }
fonts?: { regular: DiagramFont; bold: DiagramFont; mono: DiagramFont }
colors: { foreground: string; muted: string; surface: string; border: string; accent: string }
}
export interface DiagramImage {
png: Uint8Array
width: number
height: number
}
export interface DiagramLayout {
svg: string
width: number
height: number
}
export interface DiagramFrame {
width: number
height: number
x: number
y: number
viewWidth: number
viewHeight: number
}
export interface DiagramAsset extends DiagramImage {
layout: DiagramLayout
}
export type DiagramWork = { kind: "render"; input: DiagramInput }
| { kind: "raster"; layout: DiagramLayout; frame: DiagramFrame }
+289
View File
@@ -0,0 +1,289 @@
import {
BoxRenderable, ImageRenderable, NativeImage, RGBA, ScrollBoxRenderable, TextRenderable,
type CliRenderer, type KeyEvent,
} from "@opentui/core"
import { copyWithSystemClipboard } from "../../platform/clipboard"
import { hideScrollbars } from "../scrollbox"
import { diagramCellSize, diagramProtocol, type DiagramOwner, type MermaidBlock } from "./block"
import { diagramFrame } from "./geometry"
import { diagramLayoutKey, type DiagramLayoutLease } from "./cache"
import type { DiagramImage, DiagramLayout } from "./types"
/** A read-only overlay: opening it never edits the composer or transcript scroll. */
export class DiagramViewer {
readonly root: BoxRenderable
private readonly header: TextRenderable
private readonly note: TextRenderable
private readonly viewport: BoxRenderable
private readonly sourceScroll: ScrollBoxRenderable
private readonly sourceText: TextRenderable
private readonly footer: TextRenderable
private items: MermaidBlock[] = []
private selected = 0
private request: AbortController | null = null
private releaseWorker: (() => void) | null = null
private native: NativeImage | null = null
private item: MermaidBlock | null = null
private layoutLease: DiagramLayoutLease | null = null
private frameKey = ""
private wantedKey = ""
private picture: ImageRenderable | null = null
private pixels = 0
private zoom = 1
private panX = 0
private panY = 0
private sourceMode = false
private revision = 0
onClose?: () => void
constructor(private readonly renderer: CliRenderer, private readonly owner: DiagramOwner) {
const colors = owner.colors()
this.root = new BoxRenderable(renderer, {
id: "nanobot-tui-diagrams", position: "absolute", top: 0, left: 0,
width: "100%", height: "100%", zIndex: 110, padding: 1,
flexDirection: "column", backgroundColor: RGBA.defaultBackground(), visible: false,
})
this.header = new TextRenderable(renderer, { id: "diagrams-header", content: "Mermaid", fg: colors.foreground, height: 1, flexShrink: 0 })
this.note = new TextRenderable(renderer, { id: "diagrams-note", content: "", fg: colors.muted, minHeight: 1, flexShrink: 0, width: "100%" })
this.viewport = new BoxRenderable(renderer, {
id: "diagrams-viewport", width: "100%", minHeight: 0, flexGrow: 1,
overflow: "hidden", alignItems: "center", justifyContent: "center",
onSizeChange: () => this.paint(),
})
this.sourceScroll = new ScrollBoxRenderable(renderer, {
id: "diagrams-source-scroll", width: "100%", minHeight: 0, flexGrow: 1,
scrollY: true, scrollX: false, visible: false,
verticalScrollbarOptions: { visible: false }, horizontalScrollbarOptions: { visible: false },
})
hideScrollbars(this.sourceScroll)
this.sourceText = new TextRenderable(renderer, { id: "diagrams-source", content: "", fg: colors.foreground, width: "100%", wrapMode: "char", selectable: true })
this.sourceScroll.add(this.sourceText)
this.footer = new TextRenderable(renderer, { id: "diagrams-footer", content: "", fg: colors.muted, minHeight: 1, width: "100%", flexShrink: 0 })
this.root.add(this.header)
this.root.add(this.note)
this.root.add(this.viewport)
this.root.add(this.sourceScroll)
this.root.add(this.footer)
}
get visible(): boolean { return this.root.visible }
private get layout(): DiagramLayout | null { return this.layoutLease?.layout ?? null }
show(items: MermaidBlock[], selected: number): void {
this.items = items
this.selected = selected
this.root.visible = true
this.load()
}
hide(): void {
this.root.visible = false
this.revision++
this.request?.abort()
this.request = null
this.owner.restorePreviews()
this.releaseWorker?.()
this.releaseWorker = null
this.items = []
this.sourceText.content = ""
}
clear(): void { this.hide(); this.releaseImage(); this.item = null }
evict(item: MermaidBlock): void { if (this.item === item) { this.item = null; this.releaseImage(); this.showMode() } }
setTheme(): void {
const colors = this.owner.colors()
this.header.fg = colors.foreground
this.sourceText.fg = colors.foreground
this.note.fg = this.footer.fg = colors.muted
if (this.visible) this.load(true)
}
handleKey(key: KeyEvent): void {
if (key.name === "escape" || (!key.ctrl && key.name === "q")) { this.hide(); this.onClose?.(); return }
if (key.ctrl || key.meta) return
if (key.name === "c") { void this.copy(); return }
if (key.name === "s") { this.sourceMode = !this.sourceMode; this.showMode(); this.paint(); return }
if (key.name === "x") {
this.releaseImage()
this.sourceMode = true
this.note.content = "Rendering cancelled · original source retained · r retry"
this.showMode()
return
}
if (key.name === "r") { this.load(true); return }
if (this.items.length && (key.name === "[" || key.name === "]")) {
this.selected = (this.selected + (key.name === "[" ? -1 : 1) + this.items.length) % this.items.length
this.load()
return
}
if (this.sourceMode) {
if (["up", "down", "pageup", "pagedown", "home", "end"].includes(key.name)) this.sourceScroll.handleKeyPress(key)
return
}
if (key.sequence === "+" || key.name === "=" || key.name === "+") this.zoom = Math.min(8, this.zoom * 1.5)
else if (key.name === "-") this.zoom = Math.max(0.5, this.zoom / 1.5)
else if (key.name === "0") { this.zoom = 1; this.panX = this.panY = 0 }
else if (key.name === "left") this.panX -= 0.12
else if (key.name === "right") this.panX += 0.12
else if (key.name === "up") this.panY -= 0.12
else if (key.name === "down") this.panY += 0.12
else return
this.paint()
}
private load(force = false): void {
const item = this.items[this.selected]
this.revision++
const reuse = !force && item === this.item && this.layout === item?.renderedLayout && this.picture
if (!reuse) {
this.releaseImage()
this.item = item ?? null
this.layoutLease = item?.borrowLayout() ?? null
this.zoom = 1
this.panX = this.panY = 0
}
this.sourceMode = false
this.header.content = this.items.length ? `Mermaid ${this.selected + 1}/${this.items.length}` : "Mermaid"
this.sourceText.content = item?.source || "No Mermaid diagrams in this conversation."
this.sourceScroll.scrollTo(0)
this.showMode()
if (!item) { this.sourceMode = true; this.showMode(); this.note.content = ""; return }
if (!item.settled) {
this.sourceMode = true
this.note.content = "Replying · original source (r refresh after completion)"
this.showMode()
return
}
if (!diagramProtocol(this.renderer)) {
this.sourceMode = true
this.note.content = "No image protocol · original Mermaid source"
this.showMode()
return
}
if (reuse) this.describeFrame()
else this.note.content = "Rendering… · x cancel · s source"
this.paint()
}
private showMode(): void {
if (this.visible && !this.sourceMode && this.item?.settled && diagramProtocol(this.renderer)) {
this.releaseWorker ??= this.owner.jobs.retainWorker()
} else {
this.request?.abort()
this.request = null
this.releaseWorker?.()
this.releaseWorker = null
}
this.sourceScroll.visible = this.sourceMode
this.viewport.visible = !this.sourceMode
this.footer.content = !this.items.length ? "q/esc back" : this.renderer.width < 85
? "+/- zoom arrows pan s source c copy [ previous ] next q/esc back"
: "+/- zoom arrows pan 0 fit s source/image c copy source [ previous ] next x cancel q/esc back"
this.root.requestRender()
}
private paint(): void {
const item = this.item
const protocol = diagramProtocol(this.renderer)
if (!item?.settled || !protocol || !this.visible || this.sourceMode || this.viewport.width < 1 || this.viewport.height < 1) return
const { width: cellWidth, height: cellHeight } = diagramCellSize(this.renderer)
const viewport = { width: this.viewport.width * cellWidth, height: this.viewport.height * cellHeight, cellWidth, cellHeight }
this.panX = Math.max(0, Math.min(1, this.panX))
this.panY = Math.max(0, Math.min(1, this.panY))
this.layoutLease ??= this.owner.layouts.acquire(diagramLayoutKey({ source: item.source, width: viewport.width, colors: this.owner.colors() }))
const frame = this.layout ? diagramFrame(this.layout, viewport, this.zoom, this.panX, this.panY) : null
const key = JSON.stringify(frame ?? [viewport, this.zoom, this.panX, this.panY])
this.wantedKey = key
if (this.frameKey === key || this.request) return
const borrowed = frame && item.borrowImage(frame)
if (borrowed) {
this.installImage(borrowed, cellWidth, cellHeight, protocol)
this.frameKey = key
this.describeFrame()
return
}
const request = new AbortController()
this.request = request
const layout = this.layout
const initialFit = this.zoom === 1 && this.panX === 0 && this.panY === 0
const work = layout
? this.owner.jobs.raster(layout, diagramFrame(layout, viewport, this.zoom, this.panX, this.panY), request.signal)
: this.owner.jobs.render({ source: item.source, width: viewport.width, scale: 1, viewport, colors: this.owner.colors() }, request.signal)
void work.then(image => {
if (this.request !== request || !this.visible) return
if ("layout" in image) {
const rendered = image.layout as DiagramLayout
this.layoutLease = this.owner.layouts.store(diagramLayoutKey({ source: item.source, width: viewport.width, colors: this.owner.colors() }), rendered)
}
if (this.wantedKey === key && (layout || initialFit)) {
this.installImage(image, cellWidth, cellHeight, protocol)
this.frameKey = JSON.stringify(frame ?? diagramFrame(this.layout!, viewport, this.zoom, this.panX, this.panY))
this.describeFrame()
}
}).catch(error => {
if (this.request !== request || request.signal.aborted || !this.visible) return
this.fail(error instanceof Error ? error.message : "Rendering failed")
}).finally(() => {
if (this.request !== request) return
this.request = null
// At most one frame in flight. Rapid keys replace demand, not the process or queue.
this.paint()
})
}
private describeFrame(): void {
this.note.content = `${Math.round(this.zoom * 100)}% fit · ${this.layout!.width}×${this.layout!.height}px · pan ${Math.round(this.panX * 100)}%,${Math.round(this.panY * 100)}%`
}
private installImage(image: DiagramImage | NativeImage, cellWidth: number, cellHeight: number, protocol: "kitty" | "sixel"): void {
const pixels = image instanceof NativeImage ? 0 : image.width * image.height
if (!this.owner.reserve(pixels)) throw new Error("Visible image memory limit")
let native: NativeImage
try { native = image instanceof NativeImage ? image : NativeImage.decode(image.png) }
catch (error) { this.owner.release(pixels); throw error }
if (this.picture) { this.viewport.remove(this.picture); this.picture.destroyRecursively() }
this.native?.dispose()
this.owner.release(this.pixels)
this.native = native
this.pixels = pixels
this.picture = new ImageRenderable(this.renderer, {
id: "diagrams-picture", source: native,
width: Math.round(image.width / cellWidth), height: Math.round(image.height / cellHeight),
protocol, fit: "fill", onError: () => this.fail("Terminal image load failed"),
})
this.viewport.add(this.picture)
}
private fail(message: string): void {
this.releaseImage()
this.sourceMode = true
this.note.content = `Mermaid · ${message.replace(/[\r\n\x00-\x1f]/g, " ").slice(0, 120)} · original source`
this.showMode()
}
private async copy(): Promise<void> {
const item = this.items[this.selected]
if (!item) return
const revision = this.revision
try {
if (!this.renderer.copyToClipboardOSC52(item.source)) await copyWithSystemClipboard(item.source)
if (revision === this.revision && this.visible) this.note.content = "Source copied"
} catch {
if (revision === this.revision && this.visible) this.note.content = "Copy unavailable · source remains selectable"
}
}
private releaseImage(): void {
this.request?.abort()
this.request = null
if (this.picture) { this.viewport.remove(this.picture); this.picture.destroyRecursively(); this.picture = null }
this.native?.dispose()
this.native = null
this.layoutLease?.release()
this.layoutLease = null
this.frameKey = this.wantedKey = ""
if (this.pixels) this.owner.release(this.pixels)
this.pixels = 0
}
}
+19
View File
@@ -0,0 +1,19 @@
import { jsonLines, REQUEST_LIMIT } from "./protocol"
import type { DiagramWork } from "./types"
/** Engine imports stay in the owning batch; the parent retains only vectors and images. */
export async function runDiagramWorker(): Promise<void> {
for await (const record of jsonLines(Bun.stdin.stream(), REQUEST_LIMIT)) {
let reply: object
try {
const work = record as DiagramWork
const image = work.kind === "render"
? await (await import("./render")).renderDiagram(work.input)
: (await import("./raster")).rasterDiagram(work.layout, work.frame)
reply = { image: { ...image, png: Buffer.from(image.png).toString("base64") } }
} catch (error) {
reply = { error: error instanceof Error ? error.message.slice(0, 400) : "Diagram rendering failed" }
}
await Bun.write(Bun.stdout, JSON.stringify(reply) + "\n")
}
}
+19 -1
View File
@@ -24,6 +24,8 @@ import type {
ToolProgressEvent,
} from "../client"
import { renderLatexAsUnicode } from "./latex"
import { TranscriptDiagrams } from "./mermaid/controller"
import type { DiagramInput } from "./mermaid/types"
import { hideScrollbars } from "./scrollbox"
import { mergeToolEvent, renderToolEvent } from "./tool-renderers"
@@ -35,6 +37,7 @@ export interface TranscriptTheme {
userBackground: ColorInput | null
border: ColorInput
syntax: SyntaxStyle
diagramColors: DiagramInput["colors"]
}
export interface TranscriptHeader {
@@ -108,6 +111,8 @@ function projectUserMessage(media: readonly UserMessageMedia[]): UserMessageProj
/** Projects gateway events into retained, reflowable conversation cells. */
export class Transcript {
readonly root: ScrollBoxRenderable
readonly diagrams: TranscriptDiagrams
private readonly diagramStates = new WeakMap<MarkdownRenderable, { settled: boolean }>()
private live: { row: BoxRenderable; markdown: MarkdownRenderable; content: string } | null = null
private activity: Activity | null = null
private readonly styledText: Array<{
@@ -168,11 +173,13 @@ export class Transcript {
onMouseScroll: () => this.scheduleNavigationUpdate(),
})
hideScrollbars(this.root)
this.diagrams = new TranscriptDiagrams(renderer, this.root, theme.diagramColors)
}
setTheme(theme: TranscriptTheme): void {
const previousSyntax = this.theme.syntax
this.theme = theme
this.diagrams.setTheme(theme.diagramColors)
this.codeRailColor = parseColor(theme.border)
for (const { renderable, tone } of this.styledText) renderable.fg = theme[tone]
for (const { text, phase } of this.compactions.values()) {
@@ -222,6 +229,7 @@ export class Transcript {
}
reset(header: TranscriptHeader): void {
this.diagrams.reset()
if (this.navigationTimer) clearTimeout(this.navigationTimer)
this.navigationTimer = null
this.clearStreamTimer()
@@ -381,6 +389,9 @@ export class Transcript {
// anchors and avoids the one-frame jump caused by replacing the row.
this.live.markdown.content = renderLatexAsUnicode(content)
this.live.markdown.streaming = false
const state = this.diagramStates.get(this.live.markdown)
if (state) state.settled = true
this.live.markdown.requestRender()
this.live = null
} else if (fallback.trim()) {
this.assistant(fallback)
@@ -437,6 +448,7 @@ export class Transcript {
}
destroy(): void {
this.diagrams.destroy()
if (this.navigationTimer) clearTimeout(this.navigationTimer)
this.clearStreamTimer()
this.pendingStream = ""
@@ -709,6 +721,7 @@ export class Transcript {
}
private createMarkdown(content: string, streaming: boolean, id = "markdown"): MarkdownRenderable {
const state = { settled: !streaming }
const markdown = new MarkdownRenderable(this.renderer, {
id: this.id(id),
content: renderLatexAsUnicode(content),
@@ -724,7 +737,11 @@ export class Transcript {
if (token.type !== "code") return undefined
const code = context.defaultRender()
if (!(code instanceof CodeRenderable)) return code
return this.decorateCodeBlock(code)
this.decorateCodeBlock(code)
if (token.lang?.trim().toLowerCase() === "mermaid") {
return this.diagrams.create(token.text, code, state, this.id("mermaid"))
}
return code
},
tableOptions: {
style: "columns",
@@ -735,6 +752,7 @@ export class Transcript {
treeSitterClient: this.treeSitterClient,
})
this.markdown.add(markdown)
this.diagramStates.set(markdown, state)
return markdown
}