/** Renderable view of a graph node (provider or step). */ export interface GraphNodeView { name: string; kind: 'provider' | 'step'; origin: string; needs: string[]; provides: string[]; } /** Renderable view of a route and the chain of nodes feeding its handler. */ export interface GraphRouteView { pattern: string; method: string; transport: string; chain: string[]; } /** Full graph payload consumed by the diagram renderers. */ export interface GraphView { nodes: GraphNodeView[]; routes: GraphRouteView[]; } /** Sanitizes a node name into a Mermaid/DOT-safe node id. */ const nid = (name: string) => 'n_' + name.replace(/[^a-zA-Z0-9]/g, '_'); /** Sanitizes a route pattern into a Mermaid/DOT-safe node id. */ const rid = (pattern: string) => 'r_' + pattern.replace(/[^a-zA-Z0-9]/g, '_'); /** Renders the graph view as Mermaid `flowchart` source. */ export function toMermaid(view: GraphView): string { const known = new Set(view.nodes.map((node) => node.name)); const out: string[] = ['flowchart LR']; for (const node of view.nodes) { out.push( node.kind === 'provider' ? ` ${nid(node.name)}(["${node.name}"])` : ` ${nid(node.name)}["${node.name}"]`, ); } for (const node of view.nodes) for (const need of node.needs) if (known.has(need)) out.push(` ${nid(need)} --> ${nid(node.name)}`); for (const route of view.routes) { out.push(` ${rid(route.pattern)}{{"${route.method} ${route.pattern}"}}`); for (const link of route.chain) if (known.has(link)) out.push(` ${nid(link)} --> ${rid(route.pattern)}`); } return out.join('\n'); } /** Renders the graph view as Graphviz DOT source. */ export function toDOT(view: GraphView): string { const known = new Set(view.nodes.map((node) => node.name)); const out: string[] = ['digraph green_tea {', ' rankdir=LR;']; for (const node of view.nodes) { out.push(` "${node.name}" [shape=${node.kind === 'provider' ? 'ellipse' : 'box'}];`); } for (const node of view.nodes) for (const need of node.needs) if (known.has(need)) out.push(` "${need}" -> "${node.name}";`); for (const route of view.routes) { const label = `${route.method} ${route.pattern}`; out.push(` "${label}" [shape=hexagon];`); for (const link of route.chain) if (known.has(link)) out.push(` "${link}" -> "${label}";`); } out.push('}'); return out.join('\n'); } /** * Renders the graph as a self-contained, interactive HTML page (Cytoscape.js + * dagre, both from a CDN — no runtime dependency is added). Clicking a route * lights up its exact transitive slice and dims everything else; clicking a * provider/step shows its blast radius (every route that depends on it). * * The whole {@link GraphView} is embedded as JSON, so all interaction is * client-side — the page needs nothing from the server after it loads. */ /** The Green Tea. mark (leaf whose venation is a dependency graph), inlined for the dev graph header. */ const BRAND_LOGO = ''; export function graphHtml(view: GraphView): string { const data = JSON.stringify(view).replace(/