Skip to content

API Reference parrot.outputs.a2ui.graph.mermaid

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.outputs.a2ui.graph.mermaid

View source

Pure-Python Mermaid codec for the Graph vocabulary (FEAT-529 Module 3).

Mermaid is a deterministic IMPORT/EXPORT codec only — :class:~parrot. outputs.a2ui.graph.models.GraphSpec stays the single wire vocabulary (spec §7 "Patterns to Follow"). This module hand-writes a tokenizer/parser for a deliberately small subset of four mermaid dialects (flowchart, stateDiagram-v2, sequenceDiagram, C4); no external parsing dependency is introduced (spec Non-Goals — no mermaid/lark/pyparsing, etc.).

Supported subset (spec §3 Module 3):

  • flowchart: 6 node shapes ([rect], (rounded), {diamond}, ((circle)), {{hexagon}}, [[subroutine]]), 3 edge kinds (--> solid, -.-> dashed, ==> thick), labels via -- text --> (parse-only) and -->|text| (canonical emit), subgraph id [label] ... end groups.
  • stateDiagram-v2: [*] --> A / A --> [*] synthetic __start__/__end__ circle nodes, A --> B : label transitions, state "label" as id aliases, composite state X { ... } groups.
  • sequenceDiagram: participant A as Label aliases, A->>B: msg (solid) / A-->>B: msg (dashed) messages, in encountered order (GraphSpec.edges is already an ordered list — no separate ordering field is needed or exists on :class:~parrot.outputs.a2ui.graph.models. GraphEdge).
  • C4: C4Context / C4Container / C4Component / C4Dynamic source is emitted by :mod:.mermaid_c4.

%% lines are comments (ignored); %%{... init directives, classDef/click/style/linkStyle, and sequence loop/alt/par/note blocks are explicitly OUT of the supported subset and raise :class:MermaidCodecError naming the offending line (spec Non-Goals).

Object round-trip, not textual round-trip. from_mermaid(to_mermaid( spec)) == spec (modulo accessible_description/size, which have no mermaid representation) is the contract — NOT that re-serializing preserves the exact original source text. Two collapsing conventions make this practical without extra wire fields:

  • A node's label is omitted on export (and re-inferred as None on import) whenever it is exactly the node's id — the common "no explicit label" case.
  • A node's shape is omitted on export (and re-inferred as None on import) whenever it equals the dialect's DEFAULT shape (rect for flowchart, rounded for state) — an explicit shape matching the default is indistinguishable from "no shape given" once round-tripped through text, which is harmless: both render identically.

kind="dag" reuses the flowchart mermaid dialect on export (a DAG IS an acyclic flowchart); from_mermaid never infers kind="dag" back from text — that semantic promise cannot be recovered from syntax alone, so a flowchart-dialect text always parses to kind="flowchart".

Class MermaidCodecError

A mermaid source line falls outside the supported subset (spec §2 G4).

Subclasses :class:~parrot.outputs.a2ui.catalog.base.CatalogValidationError so the LLM producer's existing validate-retry-degrade loop handles a codec failure without any new plumbing.

Parameter Type Default Description
line_no int
line str
reason str

Attributes:

Attribute Type Description
line_no 1-based source line number of the offending line.
line The offending line's raw text.
reason Human-readable explanation.

Function from_mermaid

Parse mermaid source text into a :class:GraphSpec.

from_mermaid(text: str) -> GraphSpec

Parameter Type Default Description
text str Mermaid source (flowchart / stateDiagram-v2 / sequenceDiagram).

Returns: GraphSpec — The parsed :class:GraphSpec (accessible_description/size stay at their defaults — mermaid has no representation for them).

Raises:

  • MermaidCodecError — For an empty source, an unrecognized diagram header, or any line outside the supported subset — names the offending line and a reason.

Function to_mermaid

Emit spec as canonical mermaid source text.

Deterministic: the same spec always produces the same text (stable, input-preserving order for nodes/edges/groups).

to_mermaid(spec: GraphSpec) -> str

Parameter Type Default Description
spec GraphSpec The graph to serialize.

Returns: str — Mermaid source text (trailing newline included).

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally