import { b as Cmd, c as Sub, M as Machine } from '../core-sEWVgnpI.js'; import '@standard-schema/spec'; /** * Sanitize a discriminant string into a Mermaid-safe node identifier. * * Any run of non-identifier characters becomes `_`; a leading digit is * prefixed so the id is always a valid Mermaid token; the empty string gets a * stable placeholder. Deterministic and idempotent — `safeId(safeId(x))` is * `safeId(x)`, so a diagram may sanitize the same name at several sites. */ declare function safeId(raw: string): string; /** * Sanitize text used as a Mermaid edge/transition label (the part after `:`). * * Strips the characters that terminate or confuse a label: newlines, the `:` * that separates label from target, and Mermaid's `"`/`;` delimiters. */ declare function safeLabel(raw: string): string; /** * @packageDocumentation * @demlik/tea/machine-viz — turn a `Machine` into a Mermaid diagram string. * * PURE. No deps, no AST parsing, no file I/O. The function reads only the * `Machine` value it is handed plus optional sample states/msgs/ctx. * * The trick: TEA update cells are PURE (invariant 2 — they return * `[nextState, cmds]` as data, no effects). So we can SAFELY EXECUTE a cell to * discover its target state — exactly what `replay` does. That gives us REAL * edges (`red --> green : go`) without parsing source. When no sample is * provided for a `(state, msg)` pair we cannot resolve the target, so we fall * back to a STRUCTURAL self-edge annotated with `?` — visually distinct from a * resolved edge and documented in the diagram's legend. * * Form detection is `formOf(machine)` — the same reader `run`/`replay` use, * honoring the authoritative `__form` tag `defineMachine` stamps (falling back * to the structural heuristic for machines built without it; #275). * * What we MUST NOT do (these are the effectful paths, off-limits here): * - call `interpret[*]` handlers (perform I/O) * - call `subscribe[*]` runners (install listeners) * - call `run` / the runtime * We MAY call `init`, `update[*]` cells, and each `subs` entry's `deps(state)` * — all pure by the invariants (same set `replay` is allowed to touch). */ /** * Options controlling diagram emission. * * `samples` is the lever that turns STRUCTURAL edges into RESOLVED edges. For a * Transitions machine, providing a sample state for `stateType` AND a sample * msg for `msgType` lets us execute `update[stateType][msgType](state, msg)` and * read the real target `next._tag`. Sample maps are keyed by the discriminant * string (`state._tag` / `msg._tag`). * * `samples.ctx` is the argument to `init(null, ctx)` — provide it to draw the * `[*] -->` initial-state edge. Omitted ⇒ no init edge (annotated as such), * because `init`'s signature requires a ctx and we will not invent one. */ interface MachineVizOptions { samples?: { /** Sample state per `state._tag` — unlocks RESOLVED edges + sub annotations. */ states?: Partial>; /** Sample msg per `msg._tag` — unlocks RESOLVED edges. */ msgs?: Partial>; /** Ctx for `init(null, ctx)` — unlocks the `[*] -->` initial-state edge. */ ctx?: unknown; }; /** Mermaid layout direction. Default `"TB"` (top-to-bottom). */ direction?: "TB" | "LR"; /** Optional diagram title, emitted as a Mermaid front-matter `title:` block. */ title?: string; } /** * Render a `Machine` as a Mermaid `stateDiagram-v2` string. * * - **Transitions form** (`update` is a record-of-records): nodes are the * `state._tag` keys; each `(stateType × msgType)` cell becomes an edge. With * samples for both, the edge is RESOLVED by executing the (pure) cell. Without * samples it is STRUCTURAL — a `from --> from : msg?` self-loop (the `?` * suffix marks "target unresolved"). A legend note documents the distinction. * The `[*] -->` init edge is drawn only when `samples.ctx` is provided. * * - **Reducer form** (`update` values are functions): there is no static phase * graph (state is not a discriminated union over phases). We emit a single * node listing the handled `msg._tag`s and an honest note saying so. No edges * are invented. * * Output is deterministic for the same input (keys iterated in insertion order, * no clocks, no randomness). */ declare function toMermaid(machine: Machine, opts?: MachineVizOptions, NoInfer>): string; export { type MachineVizOptions, safeId, safeLabel, toMermaid };