4.8 KiB
4.8 KiB
render_mermaid
Convert Mermaid source into terminal-friendly ASCII/Unicode text.
Source
- Entry:
packages/coding-agent/src/tools/render-mermaid.ts - Model-facing prompt:
packages/coding-agent/src/prompts/tools/render-mermaid.md - Key collaborators:
packages/utils/src/mermaid-ascii.ts— thin wrapper over renderer package.packages/coding-agent/src/tools/index.ts— tool registration and enablement gate.packages/coding-agent/src/sdk.ts— session-facing artifact allocation hook.packages/coding-agent/src/session/session-manager.ts— persistent-session artifact path allocation.packages/coding-agent/src/session/artifacts.ts— artifact filename generation and writes.
- Related user/runtime doc:
docs/render-mermaid.md
Inputs
| Field | Type | Required | Description |
|---|---|---|---|
mermaid |
string |
Yes | Mermaid source text. Schema example: graph TD; A-->B. |
config |
object |
No | Optional renderer options. Sanitized before rendering; numeric fields are floored and clamped to >= 0. |
config fields:
| Field | Type | Required | Description |
|---|---|---|---|
useAscii |
boolean |
No | true for plain ASCII, false/omitted for Unicode box-drawing output. Passed through unchanged. |
paddingX |
number |
No | Horizontal spacing. Math.floor, then Math.max(0, value). |
paddingY |
number |
No | Vertical spacing. Math.floor, then Math.max(0, value). |
boxBorderPadding |
number |
No | Inner box padding. Math.floor, then Math.max(0, value). |
Outputs
The tool returns a single text content block:
- inline body: rendered diagram text
- optional trailer:
Saved artifact: artifact://<id>when artifact storage is available
details may include:
artifactId?: string
No image path, SVG, PNG, or binary payload is returned. Stored artifacts are plain text .log files; artifact filenames are allocated as <id>.render_mermaid.log by packages/coding-agent/src/session/artifacts.ts.
Flow
RenderMermaidTool.execute()inpackages/coding-agent/src/tools/render-mermaid.tsreceivesmermaidand optionalconfig.sanitizeRenderConfig()normalizespaddingX,paddingY, andboxBorderPaddingto non-negative integers;useAsciiis passed through.- The tool calls
renderMermaidAscii()from@oh-my-pi/pi-utils. packages/utils/src/mermaid-ascii.tsforwards directly torenderMermaidASCII()from thebeautiful-mermaidpackage.- The tool optionally asks the session for an artifact slot with
allocateOutputArtifact("render_mermaid"). - If a path is returned,
Bun.write()persists the full rendered text to that file. - The tool returns the rendered text, plus an
artifact://line anddetails.artifactIdwhen persistence succeeded.
Modes / Variants
- Default render: Unicode box-drawing output when
config.useAsciiis omitted or false. - ASCII render: plain ASCII output when
config.useAsciiis true. - Persistent-session path: artifact text is written when
allocateOutputArtifact()returns a path. - Ephemeral-session path: no artifact is written; the inline text result is still returned.
Side Effects
- Filesystem
- May write one session artifact via
Bun.write(). - Artifact filename format is
<id>.render_mermaid.log.
- May write one session artifact via
- Session state (transcript, memory, jobs, checkpoints, registries)
- Consumes the session artifact allocator hook.
- Returns
details.artifactIdfor the tool result.
Limits & Caps
- No tool-local timeout, retry, truncation, or streaming path.
- Numeric config fields are quantized to integers with
Math.floor()and clamped to0minimum insanitizeRenderConfig(). - Renderer engine is
beautiful-mermaid@1.1.3per rootpackage.json/bun.lock. - The tool is registered as discoverable and gated by
renderMermaid.enabledinpackages/coding-agent/src/tools/index.ts.
Errors
renderMermaidAscii()is not wrapped in a localtry/catch; renderer exceptions propagate out ofexecute().- Invalid Mermaid syntax therefore fails the tool call rather than returning partial output.
- Artifact allocation failures inside the SDK hook are swallowed there and converted to
{}inpackages/coding-agent/src/sdk.ts; rendering still succeeds, just without a saved artifact. - Artifact write failures from
Bun.write()are not caught in the tool and will fail the call.
Notes
- The tool summary string says
Render a Mermaid diagram to an image, but the implementation and prompt both produce text, not images. - Despite the name, this tool does not use Puppeteer, browser rendering, Mermaid CLI, or native bindings; rendering stays in-process through the JS package wrapper.
docs/render-mermaid.mdcovers operator-facing behavior and enablement; keep this file focused on the tool contract and runtime path.