@jagreehal/workflow 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/visualize/index.ts","../src/visualize/utils/timing.ts","../src/visualize/parallel-detector.ts","../src/visualize/ir-builder.ts","../src/visualize/types.ts","../src/visualize/renderers/colors.ts","../src/visualize/renderers/ascii.ts","../src/visualize/renderers/mermaid.ts","../src/visualize/live-visualizer.ts","../src/visualize/decision-tracker.ts"],"sourcesContent":["/**\n * Workflow Visualization Module\n *\n * Provides tools for visualizing workflow execution with color-coded\n * step states and support for parallel/race operations.\n *\n * @example\n * ```typescript\n * import { createVisualizer } from '@jagreehal/workflow/visualize';\n *\n * const viz = createVisualizer({ workflowName: 'checkout' });\n * const workflow = createWorkflow(deps, { onEvent: viz.handleEvent });\n *\n * await workflow(async (step) => {\n * await step(() => validateCart(cart), 'Validate cart');\n * await step(() => processPayment(payment), 'Process payment');\n * });\n *\n * console.log(viz.render());\n * ```\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n OutputFormat,\n RenderOptions,\n ScopeEndEvent,\n ScopeStartEvent,\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n VisualizerOptions,\n WorkflowIR,\n} from \"./types\";\nimport { createIRBuilder } from \"./ir-builder\";\nimport { asciiRenderer, mermaidRenderer, defaultColorScheme } from \"./renderers\";\n\n// =============================================================================\n// Re-exports\n// =============================================================================\n\nexport * from \"./types\";\nexport { createIRBuilder, type IRBuilderOptions } from \"./ir-builder\";\nexport { asciiRenderer, mermaidRenderer, defaultColorScheme } from \"./renderers\";\nexport { detectParallelGroups, createParallelDetector, type ParallelDetectorOptions } from \"./parallel-detector\";\nexport { createLiveVisualizer, type LiveVisualizer } from \"./live-visualizer\";\nexport { trackDecision, trackIf, trackSwitch, type DecisionTracker, type IfTracker, type SwitchTracker } from \"./decision-tracker\";\n\n// =============================================================================\n// Visualizer Interface\n// =============================================================================\n\n/**\n * Workflow visualizer that processes events and renders output.\n */\nexport interface WorkflowVisualizer {\n /** Process a workflow event */\n handleEvent: (event: WorkflowEvent<unknown>) => void;\n\n /** Process a scope event (parallel/race) */\n handleScopeEvent: (event: ScopeStartEvent | ScopeEndEvent) => void;\n\n /** Process a decision event (conditional branches) */\n handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;\n\n /** Get current IR state */\n getIR: () => WorkflowIR;\n\n /** Render current state using the default renderer */\n render: () => string;\n\n /** Render to a specific format */\n renderAs: (format: OutputFormat) => string;\n\n /** Reset state for a new workflow */\n reset: () => void;\n\n /** Subscribe to IR updates (for live visualization) */\n onUpdate: (callback: (ir: WorkflowIR) => void) => () => void;\n}\n\n// =============================================================================\n// Create Visualizer\n// =============================================================================\n\n/**\n * Create a workflow visualizer.\n *\n * @example\n * ```typescript\n * const viz = createVisualizer({ workflowName: 'my-workflow' });\n *\n * const workflow = createWorkflow(deps, {\n * onEvent: viz.handleEvent,\n * });\n *\n * await workflow(async (step) => { ... });\n *\n * console.log(viz.render());\n * ```\n */\nexport function createVisualizer(\n options: VisualizerOptions = {}\n): WorkflowVisualizer {\n const {\n workflowName,\n detectParallel = true,\n showTimings = true,\n showKeys = false,\n colors: customColors,\n } = options;\n\n const builder = createIRBuilder({ detectParallel });\n const updateCallbacks: Set<(ir: WorkflowIR) => void> = new Set();\n\n // Renderers\n const ascii = asciiRenderer();\n const mermaid = mermaidRenderer();\n\n // Build render options\n const renderOptions: RenderOptions = {\n showTimings,\n showKeys,\n terminalWidth: process.stdout?.columns ?? 80,\n colors: { ...defaultColorScheme, ...customColors },\n };\n\n function notifyUpdate(): void {\n if (updateCallbacks.size > 0) {\n const ir = builder.getIR();\n for (const callback of updateCallbacks) {\n callback(ir);\n }\n }\n }\n\n function handleEvent(event: WorkflowEvent<unknown>): void {\n // Route scope events to handleScopeEvent for proper IR building\n if (event.type === \"scope_start\" || event.type === \"scope_end\") {\n handleScopeEvent(event as ScopeStartEvent | ScopeEndEvent);\n return;\n }\n\n builder.handleEvent(event);\n\n // Set workflow name if provided\n if (event.type === \"workflow_start\" && workflowName) {\n // Note: We'd need to extend the builder to support setting name\n // For now, the name is passed in render options\n }\n\n notifyUpdate();\n }\n\n function handleScopeEvent(event: ScopeStartEvent | ScopeEndEvent): void {\n builder.handleScopeEvent(event);\n notifyUpdate();\n }\n\n function handleDecisionEvent(\n event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent\n ): void {\n builder.handleDecisionEvent(event);\n notifyUpdate();\n }\n\n function getIR(): WorkflowIR {\n const ir = builder.getIR();\n // Apply workflow name if provided\n if (workflowName && !ir.root.name) {\n ir.root.name = workflowName;\n }\n return ir;\n }\n\n function render(): string {\n const ir = getIR();\n return ascii.render(ir, renderOptions);\n }\n\n function renderAs(format: OutputFormat): string {\n const ir = getIR();\n\n switch (format) {\n case \"ascii\":\n return ascii.render(ir, renderOptions);\n\n case \"mermaid\":\n return mermaid.render(ir, renderOptions);\n\n case \"json\":\n return JSON.stringify(ir, null, 2);\n\n default:\n throw new Error(`Unknown format: ${format}`);\n }\n }\n\n function reset(): void {\n builder.reset();\n notifyUpdate();\n }\n\n function onUpdate(callback: (ir: WorkflowIR) => void): () => void {\n updateCallbacks.add(callback);\n return () => updateCallbacks.delete(callback);\n }\n\n return {\n handleEvent,\n handleScopeEvent,\n handleDecisionEvent,\n getIR,\n render,\n renderAs,\n reset,\n onUpdate,\n };\n}\n\n// =============================================================================\n// Convenience Functions\n// =============================================================================\n\n/**\n * Union type for all collectable/visualizable events (workflow + decision).\n */\nexport type CollectableEvent =\n | WorkflowEvent<unknown>\n | DecisionStartEvent\n | DecisionBranchEvent\n | DecisionEndEvent;\n\n/**\n * Visualize collected events (post-execution).\n *\n * Supports both workflow events (from onEvent) and decision events\n * (from trackDecision/trackIf/trackSwitch).\n *\n * @example\n * ```typescript\n * const events: CollectableEvent[] = [];\n * const workflow = createWorkflow(deps, {\n * onEvent: (e) => events.push(e),\n * });\n *\n * await workflow(async (step) => {\n * const decision = trackIf('check', condition, {\n * emit: (e) => events.push(e),\n * });\n * // ...\n * });\n *\n * console.log(visualizeEvents(events));\n * ```\n */\nexport function visualizeEvents(\n events: CollectableEvent[],\n options: VisualizerOptions = {}\n): string {\n const viz = createVisualizer(options);\n\n for (const event of events) {\n if (event.type.startsWith(\"decision_\")) {\n viz.handleDecisionEvent(event as DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent);\n } else {\n viz.handleEvent(event as WorkflowEvent<unknown>);\n }\n }\n\n return viz.render();\n}\n\n/**\n * Create an event collector for later visualization.\n *\n * Supports both workflow events (from onEvent) and decision events\n * (from trackDecision/trackIf/trackSwitch).\n *\n * @example\n * ```typescript\n * const collector = createEventCollector();\n *\n * const workflow = createWorkflow(deps, {\n * onEvent: collector.handleEvent,\n * });\n *\n * await workflow(async (step) => {\n * // Decision events can also be collected\n * const decision = trackIf('check', condition, {\n * emit: collector.handleDecisionEvent,\n * });\n * // ...\n * });\n *\n * console.log(collector.visualize());\n * ```\n */\nexport function createEventCollector(options: VisualizerOptions = {}) {\n const events: CollectableEvent[] = [];\n\n return {\n /** Handle a workflow event */\n handleEvent: (event: WorkflowEvent<unknown>) => {\n events.push(event);\n },\n\n /** Handle a decision event */\n handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => {\n events.push(event);\n },\n\n /** Get all collected events */\n getEvents: () => [...events],\n\n /** Get workflow events only */\n getWorkflowEvents: () => events.filter((e): e is WorkflowEvent<unknown> =>\n !e.type.startsWith(\"decision_\")\n ),\n\n /** Get decision events only */\n getDecisionEvents: () => events.filter((e): e is DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent =>\n e.type.startsWith(\"decision_\")\n ),\n\n /** Clear collected events */\n clear: () => {\n events.length = 0;\n },\n\n /** Visualize collected events */\n visualize: () => {\n const viz = createVisualizer(options);\n for (const event of events) {\n if (event.type.startsWith(\"decision_\")) {\n viz.handleDecisionEvent(event as DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent);\n } else {\n viz.handleEvent(event as WorkflowEvent<unknown>);\n }\n }\n return viz.render();\n },\n\n /** Visualize in a specific format */\n visualizeAs: (format: OutputFormat) => {\n const viz = createVisualizer(options);\n for (const event of events) {\n if (event.type.startsWith(\"decision_\")) {\n viz.handleDecisionEvent(event as DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent);\n } else {\n viz.handleEvent(event as WorkflowEvent<unknown>);\n }\n }\n return viz.renderAs(format);\n },\n };\n}\n\n","/**\n * Timing utilities for workflow visualization.\n */\n\n/**\n * Format duration in milliseconds to a human-readable string.\n *\n * @example\n * formatDuration(23) // \"23ms\"\n * formatDuration(1500) // \"1.5s\"\n * formatDuration(65000) // \"1m 5s\"\n */\nexport function formatDuration(ms: number): string {\n if (ms < 1000) {\n return `${Math.round(ms)}ms`;\n }\n\n if (ms < 60000) {\n const seconds = ms / 1000;\n // Show one decimal for seconds\n return `${seconds.toFixed(1).replace(/\\.0$/, \"\")}s`;\n }\n\n const minutes = Math.floor(ms / 60000);\n const seconds = Math.round((ms % 60000) / 1000);\n\n if (seconds === 0) {\n return `${minutes}m`;\n }\n\n return `${minutes}m ${seconds}s`;\n}\n\n/**\n * Generate a unique ID for nodes.\n */\nexport function generateId(): string {\n return `node_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;\n}\n","/**\n * Parallel Detection - Heuristic detection of parallel execution from timing.\n *\n * When steps overlap in time (one starts before another ends), they are\n * likely running in parallel. This module detects such patterns and\n * groups overlapping steps into ParallelNode structures.\n */\n\nimport type { FlowNode, ParallelNode, StepNode } from \"./types\";\n\n/**\n * Options for parallel detection.\n */\nexport interface ParallelDetectorOptions {\n /**\n * Minimum overlap in milliseconds to consider steps parallel.\n * Default: 0 (any overlap counts)\n */\n minOverlapMs?: number;\n\n /**\n * Maximum gap in milliseconds to still consider steps as part of same parallel group.\n * Default: 5 (steps starting within 5ms are grouped)\n */\n maxGapMs?: number;\n}\n\n/**\n * Step timing information for overlap detection.\n */\ninterface StepTiming {\n node: StepNode;\n startTs: number;\n endTs: number;\n}\n\n/**\n * Check if nodes contain real scope nodes (from scope_start/scope_end events).\n * When real scope nodes exist, heuristic detection should be skipped to avoid\n * duplicating or conflicting with the explicit structure.\n */\nfunction hasRealScopeNodes(nodes: FlowNode[]): boolean {\n for (const node of nodes) {\n // Real scope nodes are parallel/race/sequence that came from scope events\n // (not from heuristic detection, which uses ids starting with \"detected_\")\n if (\n (node.type === \"parallel\" || node.type === \"race\" || node.type === \"sequence\") &&\n !node.id.startsWith(\"detected_\")\n ) {\n return true;\n }\n // Also check for decision nodes if present\n if (\"decisionId\" in node) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Group overlapping steps into parallel nodes.\n *\n * Algorithm:\n * 1. Sort steps by start time\n * 2. For each step, check if it overlaps with existing parallel groups\n * 3. If it overlaps, add to group; otherwise start new sequence\n * 4. Merge overlapping groups when step bridges them\n *\n * Note: If real scope nodes (from scope_start/scope_end) are present,\n * heuristic detection is skipped to avoid conflicts.\n */\nexport function detectParallelGroups(\n nodes: FlowNode[],\n options: ParallelDetectorOptions = {}\n): FlowNode[] {\n // If real scope nodes exist, skip heuristic detection\n // The explicit scope events provide accurate structure\n if (hasRealScopeNodes(nodes)) {\n return nodes;\n }\n\n const { minOverlapMs = 0, maxGapMs = 5 } = options;\n\n // Extract step nodes with timing info, preserving indices for position restoration\n const stepsWithTiming: (StepTiming & { originalIndex: number })[] = [];\n const nonStepNodes: { node: FlowNode; originalIndex: number }[] = [];\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n if (node.type === \"step\" && node.startTs !== undefined) {\n stepsWithTiming.push({\n node,\n startTs: node.startTs,\n endTs: node.endTs ?? node.startTs + (node.durationMs ?? 0),\n originalIndex: i,\n });\n } else {\n // Keep non-step nodes with their original position\n nonStepNodes.push({ node, originalIndex: i });\n }\n }\n\n if (stepsWithTiming.length <= 1) {\n return nodes; // Nothing to group\n }\n\n // Sort by start time\n stepsWithTiming.sort((a, b) => a.startTs - b.startTs);\n\n // Group overlapping steps\n type StepTimingWithIndex = StepTiming & { originalIndex: number };\n const groups: StepTimingWithIndex[][] = [];\n let currentGroup: StepTimingWithIndex[] = [stepsWithTiming[0]];\n\n for (let i = 1; i < stepsWithTiming.length; i++) {\n const step = stepsWithTiming[i];\n const groupStart = Math.min(...currentGroup.map((s) => s.startTs));\n const groupEnd = Math.max(...currentGroup.map((s) => s.endTs));\n\n // Two ways steps can be parallel:\n // 1. They started together (within maxGapMs) - handles timing jitter\n // 2. They genuinely overlap (step starts before group ends)\n const startedTogether = step.startTs <= groupStart + maxGapMs;\n const hasTrueOverlap = step.startTs < groupEnd;\n\n if (!startedTogether && !hasTrueOverlap) {\n // Sequential: step started after group ended AND not with the group\n groups.push(currentGroup);\n currentGroup = [step];\n continue;\n }\n\n // Check minOverlapMs threshold for overlap duration\n // For steps that started together, overlap is measured from step start to group end\n // For steps with true overlap, it's from step start to min(step end, group end)\n const overlapDuration = hasTrueOverlap\n ? Math.min(step.endTs, groupEnd) - step.startTs\n : 0;\n\n // Started together bypasses minOverlapMs (they're parallel by definition)\n // True overlap must meet the minOverlapMs threshold\n if (startedTogether || overlapDuration >= minOverlapMs) {\n currentGroup.push(step);\n } else {\n // Overlap too small - treat as sequential\n groups.push(currentGroup);\n currentGroup = [step];\n }\n }\n groups.push(currentGroup);\n\n // Convert groups to nodes with position tracking\n const groupedNodes: { node: FlowNode; position: number }[] = [];\n\n for (const group of groups) {\n // Use the minimum original index as the position for the group\n const position = Math.min(...group.map((s) => s.originalIndex));\n\n if (group.length === 1) {\n // Single step - no parallel grouping needed\n groupedNodes.push({ node: group[0].node, position });\n } else {\n // Multiple overlapping steps - create parallel node\n const children = group.map((s) => s.node);\n const startTs = Math.min(...group.map((s) => s.startTs));\n const endTs = Math.max(...group.map((s) => s.endTs));\n\n const parallelNode: ParallelNode = {\n type: \"parallel\",\n id: `detected_parallel_${startTs}`,\n name: `${children.length} parallel steps`,\n state: deriveGroupState(children),\n mode: \"all\",\n children,\n startTs,\n endTs,\n durationMs: endTs - startTs,\n };\n\n groupedNodes.push({ node: parallelNode, position });\n }\n }\n\n // Add non-step nodes with their original positions\n for (const { node, originalIndex } of nonStepNodes) {\n groupedNodes.push({ node, position: originalIndex });\n }\n\n // Sort by original position to preserve ordering\n groupedNodes.sort((a, b) => a.position - b.position);\n\n return groupedNodes.map((g) => g.node);\n}\n\n/**\n * Derive the state of a group from its children.\n */\nfunction deriveGroupState(\n children: FlowNode[]\n): \"pending\" | \"running\" | \"success\" | \"error\" | \"aborted\" | \"cached\" {\n const hasError = children.some((c) => c.state === \"error\");\n if (hasError) return \"error\";\n\n const hasRunning = children.some((c) => c.state === \"running\");\n if (hasRunning) return \"running\";\n\n const hasPending = children.some((c) => c.state === \"pending\");\n if (hasPending) return \"pending\";\n\n const allSuccess = children.every(\n (c) => c.state === \"success\" || c.state === \"cached\"\n );\n if (allSuccess) return \"success\";\n\n return \"success\";\n}\n\n/**\n * Create a parallel detector that processes nodes.\n */\nexport function createParallelDetector(options: ParallelDetectorOptions = {}) {\n return {\n /**\n * Process nodes and group overlapping ones into parallel nodes.\n */\n detect: (nodes: FlowNode[]) => detectParallelGroups(nodes, options),\n };\n}\n","/**\n * IR Builder - Converts workflow events to Intermediate Representation.\n *\n * The builder maintains state as events arrive, constructing a tree\n * representation of the workflow execution that can be rendered.\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n FlowNode,\n ScopeEndEvent,\n ScopeStartEvent,\n ScopeType,\n StepNode,\n StepState,\n WorkflowIR,\n WorkflowNode,\n ParallelNode,\n RaceNode,\n DecisionNode,\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n DecisionBranch,\n} from \"./types\";\nimport { generateId } from \"./utils/timing\";\nimport { detectParallelGroups, type ParallelDetectorOptions } from \"./parallel-detector\";\n\n// =============================================================================\n// Builder Options\n// =============================================================================\n\n/**\n * Options for the IR builder.\n */\nexport interface IRBuilderOptions {\n /**\n * Enable heuristic parallel detection based on timing.\n * When true, overlapping steps are grouped into ParallelNodes.\n * Default: true\n */\n detectParallel?: boolean;\n\n /**\n * Options for parallel detection.\n */\n parallelDetection?: ParallelDetectorOptions;\n}\n\n// =============================================================================\n// Builder State\n// =============================================================================\n\ninterface ActiveStep {\n id: string;\n name?: string;\n key?: string;\n startTs: number;\n retryCount: number;\n timedOut: boolean;\n timeoutMs?: number;\n}\n\ninterface ActiveScope {\n id: string;\n name?: string;\n type: ScopeType;\n startTs: number;\n children: FlowNode[];\n}\n\ninterface ActiveDecision {\n id: string;\n name?: string;\n condition?: string;\n decisionValue?: unknown;\n startTs: number;\n branches: Map<string, DecisionBranch>;\n branchTaken?: string | boolean;\n}\n\n// =============================================================================\n// IR Builder\n// =============================================================================\n\n/**\n * Creates an IR builder that processes workflow events.\n */\nexport function createIRBuilder(options: IRBuilderOptions = {}) {\n const { detectParallel = true, parallelDetection } = options;\n\n // Current workflow state\n let workflowId: string | undefined;\n let workflowStartTs: number | undefined;\n let workflowState: StepState = \"pending\";\n let workflowError: unknown;\n let workflowDurationMs: number | undefined;\n\n // Active steps (currently running)\n const activeSteps = new Map<string, ActiveStep>();\n\n // Active scopes (parallel/race blocks)\n const scopeStack: ActiveScope[] = [];\n\n // Active decisions (conditional branches)\n const decisionStack: ActiveDecision[] = [];\n\n // Completed nodes at the current scope level\n let currentNodes: FlowNode[] = [];\n\n // Metadata\n let createdAt = Date.now();\n let lastUpdatedAt = createdAt;\n\n /**\n * Get the step ID from an event.\n * Uses stepId if available (new events), then falls back to stepKey or name,\n * and finally generates a random ID for backwards compatibility.\n */\n function getStepId(event: { stepId?: string; stepKey?: string; name?: string }): string {\n return event.stepId ?? event.stepKey ?? event.name ?? generateId();\n }\n\n /**\n * Add a completed node to the current scope or decision branch.\n */\n function addNode(node: FlowNode): void {\n // If we're in a decision, add to the taken branch\n if (decisionStack.length > 0) {\n const decision = decisionStack[decisionStack.length - 1];\n // Find the taken branch\n for (const branch of decision.branches.values()) {\n if (branch.taken) {\n branch.children.push(node);\n lastUpdatedAt = Date.now();\n return;\n }\n }\n // If no branch is marked as taken yet, add to the first branch\n // (this handles cases where steps execute before branch is marked)\n const firstBranch = Array.from(decision.branches.values())[0];\n if (firstBranch) {\n firstBranch.children.push(node);\n lastUpdatedAt = Date.now();\n return;\n }\n }\n\n // If we're in a scope, add to the scope\n if (scopeStack.length > 0) {\n // Add to the innermost scope\n scopeStack[scopeStack.length - 1].children.push(node);\n } else {\n // Add to the root level\n currentNodes.push(node);\n }\n lastUpdatedAt = Date.now();\n }\n\n /**\n * Handle a workflow event and update the IR.\n */\n function handleEvent(event: WorkflowEvent<unknown>): void {\n switch (event.type) {\n case \"workflow_start\":\n workflowId = event.workflowId;\n workflowStartTs = event.ts;\n workflowState = \"running\";\n createdAt = Date.now();\n lastUpdatedAt = createdAt;\n break;\n\n case \"workflow_success\":\n workflowState = \"success\";\n workflowDurationMs = event.durationMs;\n lastUpdatedAt = Date.now();\n break;\n\n case \"workflow_error\":\n workflowState = \"error\";\n workflowError = event.error;\n workflowDurationMs = event.durationMs;\n lastUpdatedAt = Date.now();\n break;\n\n case \"step_start\": {\n const id = getStepId(event);\n activeSteps.set(id, {\n id,\n name: event.name,\n key: event.stepKey,\n startTs: event.ts,\n retryCount: 0,\n timedOut: false,\n });\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"step_success\": {\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n const node: StepNode = {\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"success\",\n startTs: active.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n };\n addNode(node);\n activeSteps.delete(id);\n }\n break;\n }\n\n case \"step_error\": {\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n const node: StepNode = {\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"error\",\n startTs: active.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n error: event.error,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n };\n addNode(node);\n activeSteps.delete(id);\n }\n break;\n }\n\n case \"step_aborted\": {\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n const node: StepNode = {\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"aborted\",\n startTs: active.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n };\n addNode(node);\n activeSteps.delete(id);\n }\n break;\n }\n\n case \"step_cache_hit\": {\n const id = getStepId(event);\n const node: StepNode = {\n type: \"step\",\n id,\n name: event.name,\n key: event.stepKey,\n state: \"cached\",\n startTs: event.ts,\n endTs: event.ts,\n durationMs: 0,\n };\n addNode(node);\n break;\n }\n\n case \"step_cache_miss\":\n // Cache miss just means the step will execute normally\n // We'll get a step_start event next\n break;\n\n case \"step_complete\":\n // step_complete is for state persistence, not visualization\n // We already handled the step via step_success/step_error\n break;\n\n case \"step_timeout\": {\n // Timeout is an intermediate event - step may retry or will get step_error\n // Track timeout info on the active step\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n active.timedOut = true;\n active.timeoutMs = event.timeoutMs;\n }\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"step_retry\": {\n // Retry is an intermediate event - increment retry counter\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n active.retryCount = (event.attempt ?? 1) - 1; // attempt is 1-indexed, retryCount is 0-indexed\n }\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"step_retries_exhausted\":\n // All retries exhausted - step_error will follow\n // The error state will be set by step_error handler\n lastUpdatedAt = Date.now();\n break;\n\n case \"step_skipped\": {\n const id = getStepId(event);\n const node: StepNode = {\n type: \"step\",\n id,\n name: event.name,\n key: event.stepKey,\n state: \"skipped\",\n startTs: event.ts,\n endTs: event.ts,\n durationMs: 0,\n };\n addNode(node);\n break;\n }\n }\n }\n\n /**\n * Handle a scope event (parallel/race start/end).\n */\n function handleScopeEvent(event: ScopeStartEvent | ScopeEndEvent): void {\n if (event.type === \"scope_start\") {\n scopeStack.push({\n id: event.scopeId,\n name: event.name,\n type: event.scopeType,\n startTs: event.ts,\n children: [],\n });\n lastUpdatedAt = Date.now();\n } else if (event.type === \"scope_end\") {\n const scope = scopeStack.pop();\n if (scope) {\n const node: ParallelNode | RaceNode =\n scope.type === \"race\"\n ? {\n type: \"race\",\n id: scope.id,\n name: scope.name,\n state: deriveState(scope.children),\n startTs: scope.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n children: scope.children,\n winnerId: event.winnerId,\n }\n : {\n type: \"parallel\",\n id: scope.id,\n name: scope.name,\n state: deriveState(scope.children),\n startTs: scope.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n children: scope.children,\n mode: scope.type === \"allSettled\" ? \"allSettled\" : \"all\",\n };\n addNode(node);\n }\n }\n }\n\n /**\n * Handle a decision event (conditional branch start/branch/end).\n */\n function handleDecisionEvent(\n event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent\n ): void {\n if (event.type === \"decision_start\") {\n decisionStack.push({\n id: event.decisionId,\n name: event.name,\n condition: event.condition,\n decisionValue: event.decisionValue,\n startTs: event.ts,\n branches: new Map(),\n });\n lastUpdatedAt = Date.now();\n } else if (event.type === \"decision_branch\") {\n const decision = decisionStack[decisionStack.length - 1];\n if (decision && decision.id === event.decisionId) {\n // Find or create branch\n const branchKey = event.branchLabel;\n const existing = decision.branches.get(branchKey);\n if (existing) {\n // Update existing branch\n existing.taken = event.taken;\n } else {\n // Create new branch\n decision.branches.set(branchKey, {\n label: event.branchLabel,\n condition: event.condition,\n taken: event.taken,\n children: [],\n });\n }\n lastUpdatedAt = Date.now();\n }\n } else if (event.type === \"decision_end\") {\n const decision = decisionStack.pop();\n if (decision && decision.id === event.decisionId) {\n // Convert branches map to array\n const branches: DecisionBranch[] = Array.from(decision.branches.values());\n\n const node: DecisionNode = {\n type: \"decision\",\n id: decision.id,\n name: decision.name,\n state: deriveState(\n branches.flatMap((b) => (b.taken ? b.children : []))\n ),\n startTs: decision.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n condition: decision.condition,\n decisionValue: decision.decisionValue,\n branchTaken: event.branchTaken ?? decision.branchTaken,\n branches,\n };\n addNode(node);\n }\n }\n }\n\n /**\n * Derive the state of a parent node from its children.\n */\n function deriveState(children: FlowNode[]): StepState {\n if (children.length === 0) return \"success\";\n\n const hasError = children.some((c) => c.state === \"error\");\n if (hasError) return \"error\";\n\n const allSuccess = children.every(\n (c) => c.state === \"success\" || c.state === \"cached\"\n );\n if (allSuccess) return \"success\";\n\n const hasRunning = children.some((c) => c.state === \"running\");\n if (hasRunning) return \"running\";\n\n return \"pending\";\n }\n\n /**\n * Get the current nodes including any active (running) steps.\n */\n function getCurrentNodes(): FlowNode[] {\n const nodes = [...currentNodes];\n\n // Add active steps as running nodes\n for (const [, active] of activeSteps) {\n nodes.push({\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"running\",\n startTs: active.startTs,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n });\n }\n\n return nodes;\n }\n\n /**\n * Build and return the current IR state.\n */\n function getIR(): WorkflowIR {\n let children = getCurrentNodes();\n\n // Apply parallel detection if enabled\n if (detectParallel) {\n children = detectParallelGroups(children, parallelDetection);\n }\n\n const root: WorkflowNode = {\n type: \"workflow\",\n id: workflowId ?? generateId(),\n workflowId: workflowId ?? \"unknown\",\n state: workflowState,\n startTs: workflowStartTs,\n durationMs: workflowDurationMs,\n children,\n error: workflowError,\n };\n\n return {\n root,\n metadata: {\n createdAt,\n lastUpdatedAt,\n },\n };\n }\n\n /**\n * Reset the builder state.\n */\n function reset(): void {\n workflowId = undefined;\n workflowStartTs = undefined;\n workflowState = \"pending\";\n workflowError = undefined;\n workflowDurationMs = undefined;\n activeSteps.clear();\n scopeStack.length = 0;\n decisionStack.length = 0;\n currentNodes = [];\n createdAt = Date.now();\n lastUpdatedAt = createdAt;\n }\n\n return {\n handleEvent,\n handleScopeEvent,\n handleDecisionEvent,\n getIR,\n reset,\n /** Check if there are active (running) steps */\n get hasActiveSteps() {\n return activeSteps.size > 0;\n },\n /** Get the current workflow state */\n get state() {\n return workflowState;\n },\n };\n}\n\n/**\n * Type for the IR builder instance.\n */\nexport type IRBuilder = ReturnType<typeof createIRBuilder>;\n","/**\n * Workflow Visualization - Intermediate Representation Types\n *\n * The IR (Intermediate Representation) is a DSL that represents workflow\n * execution structure. Events are converted to IR, which can then be\n * rendered to various output formats (ASCII, Mermaid, JSON, etc.).\n */\n\n// =============================================================================\n// Step States\n// =============================================================================\n\n/**\n * Execution state of a step with semantic meaning for visualization.\n *\n * Color mapping:\n * - pending → white/clear (not started)\n * - running → yellow (currently executing)\n * - success → green (completed successfully)\n * - error → red (failed with error)\n * - aborted → gray (cancelled, e.g., in race)\n * - cached → blue (served from cache)\n * - skipped → dim gray (not executed due to conditional logic)\n */\nexport type StepState =\n | \"pending\"\n | \"running\"\n | \"success\"\n | \"error\"\n | \"aborted\"\n | \"cached\"\n | \"skipped\";\n\n// =============================================================================\n// Node Types\n// =============================================================================\n\n/**\n * Base properties shared by all IR nodes.\n */\nexport interface BaseNode {\n /** Unique identifier for this node */\n id: string;\n /** Human-readable name (from step options or inferred) */\n name?: string;\n /** Cache key if this is a keyed step */\n key?: string;\n /** Current execution state */\n state: StepState;\n /** Timestamp when execution started */\n startTs?: number;\n /** Timestamp when execution ended */\n endTs?: number;\n /** Duration in milliseconds */\n durationMs?: number;\n /** Error value if state is 'error' */\n error?: unknown;\n /** Input value that triggered this step (for decision understanding) */\n input?: unknown;\n /** Output value from this step (for decision understanding) */\n output?: unknown;\n /** Number of retry attempts made (0 = no retries, 1 = one retry, etc.) */\n retryCount?: number;\n /** Whether this step experienced a timeout (may have retried after) */\n timedOut?: boolean;\n /** Timeout duration in ms (if timed out) */\n timeoutMs?: number;\n}\n\n/**\n * A single step execution node.\n */\nexport interface StepNode extends BaseNode {\n type: \"step\";\n}\n\n/**\n * Sequential execution - steps run one after another.\n * This is the implicit structure when steps are awaited in sequence.\n */\nexport interface SequenceNode extends BaseNode {\n type: \"sequence\";\n children: FlowNode[];\n}\n\n/**\n * Parallel execution - all branches run simultaneously.\n * Created by allAsync() or allSettledAsync().\n */\nexport interface ParallelNode extends BaseNode {\n type: \"parallel\";\n children: FlowNode[];\n /**\n * Execution mode:\n * - 'all': Fails on first error (allAsync)\n * - 'allSettled': Collects all results (allSettledAsync)\n */\n mode: \"all\" | \"allSettled\";\n}\n\n/**\n * Race execution - first to complete wins.\n * Created by anyAsync().\n */\nexport interface RaceNode extends BaseNode {\n type: \"race\";\n children: FlowNode[];\n /** ID of the winning branch (first to succeed) */\n winnerId?: string;\n}\n\n/**\n * Decision point - conditional branch (if/switch).\n * Shows which branch was taken and why.\n */\nexport interface DecisionNode extends BaseNode {\n type: \"decision\";\n /** Condition that was evaluated (e.g., \"user.role === 'admin'\") */\n condition?: string;\n /** Value that was evaluated (the input to the decision) */\n decisionValue?: unknown;\n /** Which branch was taken (true/false, or the matched case) */\n branchTaken?: string | boolean;\n /** All possible branches (including skipped ones) */\n branches: DecisionBranch[];\n}\n\n/**\n * A branch in a decision node.\n */\nexport interface DecisionBranch {\n /** Label for this branch (e.g., \"if\", \"else\", \"case 'admin'\") */\n label: string;\n /** Condition that would trigger this branch */\n condition?: string;\n /** Whether this branch was taken */\n taken: boolean;\n /** Steps in this branch */\n children: FlowNode[];\n}\n\n/**\n * Union of all flow node types.\n */\nexport type FlowNode = StepNode | SequenceNode | ParallelNode | RaceNode | DecisionNode;\n\n/**\n * Root node representing the entire workflow.\n */\nexport interface WorkflowNode extends BaseNode {\n type: \"workflow\";\n /** Correlation ID from the workflow execution */\n workflowId: string;\n /** Child nodes (steps, parallel blocks, etc.) */\n children: FlowNode[];\n}\n\n// =============================================================================\n// Workflow IR\n// =============================================================================\n\n/**\n * Complete workflow intermediate representation.\n * This is the main data structure produced by the IR builder.\n */\nexport interface WorkflowIR {\n /** Root workflow node */\n root: WorkflowNode;\n /** Metadata about the IR */\n metadata: {\n /** When the IR was first created */\n createdAt: number;\n /** When the IR was last updated */\n lastUpdatedAt: number;\n };\n}\n\n// =============================================================================\n// Scope Events (for parallel/race detection)\n// =============================================================================\n\n// Re-export ScopeType from core for consistency\nexport type { ScopeType } from \"../core\";\nimport type { ScopeType } from \"../core\";\n\n/**\n * Event emitted when entering a parallel/race scope.\n * This matches the scope_start event in WorkflowEvent.\n */\nexport interface ScopeStartEvent {\n type: \"scope_start\";\n workflowId: string;\n scopeId: string;\n scopeType: ScopeType;\n name?: string;\n ts: number;\n}\n\n/**\n * Event emitted when exiting a parallel/race scope.\n */\nexport interface ScopeEndEvent {\n type: \"scope_end\";\n workflowId: string;\n scopeId: string;\n ts: number;\n durationMs: number;\n /** For race scopes, the ID of the winning branch */\n winnerId?: string;\n}\n\n/**\n * Event emitted when a decision point is encountered.\n * Use this to track conditional logic (if/switch).\n */\nexport interface DecisionStartEvent {\n type: \"decision_start\";\n workflowId: string;\n decisionId: string;\n /** Condition being evaluated (e.g., \"user.role === 'admin'\") */\n condition?: string;\n /** Value being evaluated */\n decisionValue?: unknown;\n /** Name/label for this decision point */\n name?: string;\n ts: number;\n}\n\n/**\n * Event emitted when a decision branch is taken.\n */\nexport interface DecisionBranchEvent {\n type: \"decision_branch\";\n workflowId: string;\n decisionId: string;\n /** Label for this branch (e.g., \"if\", \"else\", \"case 'admin'\") */\n branchLabel: string;\n /** Condition for this branch */\n condition?: string;\n /** Whether this branch was taken */\n taken: boolean;\n ts: number;\n}\n\n/**\n * Event emitted when a decision point completes.\n */\nexport interface DecisionEndEvent {\n type: \"decision_end\";\n workflowId: string;\n decisionId: string;\n /** Which branch was taken */\n branchTaken?: string | boolean;\n ts: number;\n durationMs: number;\n}\n\n/**\n * Event emitted when a step is skipped due to conditional logic.\n */\nexport interface StepSkippedEvent {\n type: \"step_skipped\";\n workflowId: string;\n stepKey?: string;\n name?: string;\n /** Reason why this step was skipped (e.g., \"condition was false\") */\n reason?: string;\n /** The decision that caused this skip */\n decisionId?: string;\n ts: number;\n}\n\n/**\n * Union of scope-related events.\n */\nexport type ScopeEvent = ScopeStartEvent | ScopeEndEvent;\n\n/**\n * Union of decision-related events.\n */\nexport type DecisionEvent = DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent;\n\n// =============================================================================\n// Renderer Types\n// =============================================================================\n\n/**\n * Color scheme for rendering step states.\n */\nexport interface ColorScheme {\n pending: string;\n running: string;\n success: string;\n error: string;\n aborted: string;\n cached: string;\n skipped: string;\n}\n\n/**\n * Options passed to renderers.\n */\nexport interface RenderOptions {\n /** Show timing information (duration) */\n showTimings: boolean;\n /** Show step cache keys */\n showKeys: boolean;\n /** Terminal width for ASCII renderer */\n terminalWidth?: number;\n /** Color scheme */\n colors: ColorScheme;\n}\n\n/**\n * Renderer interface - transforms IR to output format.\n */\nexport interface Renderer {\n /** Unique identifier for this renderer */\n readonly name: string;\n /** Render IR to string output */\n render(ir: WorkflowIR, options: RenderOptions): string;\n /** Whether this renderer supports live (incremental) updates */\n supportsLive?: boolean;\n /** Render incremental update (optional) */\n renderUpdate?(\n ir: WorkflowIR,\n changedNodes: FlowNode[],\n options: RenderOptions\n ): string;\n}\n\n// =============================================================================\n// Visualizer Types\n// =============================================================================\n\n/**\n * Output format for rendering.\n */\nexport type OutputFormat = \"ascii\" | \"mermaid\" | \"json\";\n\n/**\n * Options for creating a visualizer.\n */\nexport interface VisualizerOptions {\n /** Name for the workflow in visualizations */\n workflowName?: string;\n /** Enable parallel detection heuristics (default: true) */\n detectParallel?: boolean;\n /** Show timing information (default: true) */\n showTimings?: boolean;\n /** Show step keys (default: false) */\n showKeys?: boolean;\n /** Custom color scheme */\n colors?: Partial<ColorScheme>;\n}\n\n/**\n * Options for live visualization.\n */\nexport interface LiveVisualizerOptions extends VisualizerOptions {\n /** Output stream (default: process.stdout) */\n stream?: NodeJS.WriteStream;\n /** Update interval in ms (default: 100) */\n updateInterval?: number;\n}\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\n/**\n * Check if a node is a StepNode.\n */\nexport function isStepNode(node: FlowNode): node is StepNode {\n return node.type === \"step\";\n}\n\n/**\n * Check if a node is a SequenceNode.\n */\nexport function isSequenceNode(node: FlowNode): node is SequenceNode {\n return node.type === \"sequence\";\n}\n\n/**\n * Check if a node is a ParallelNode.\n */\nexport function isParallelNode(node: FlowNode): node is ParallelNode {\n return node.type === \"parallel\";\n}\n\n/**\n * Check if a node is a RaceNode.\n */\nexport function isRaceNode(node: FlowNode): node is RaceNode {\n return node.type === \"race\";\n}\n\n/**\n * Check if a node is a DecisionNode.\n */\nexport function isDecisionNode(node: FlowNode): node is DecisionNode {\n return node.type === \"decision\";\n}\n\n/**\n * Check if a node has children.\n */\nexport function hasChildren(\n node: FlowNode\n): node is SequenceNode | ParallelNode | RaceNode | DecisionNode {\n return \"children\" in node || (node.type === \"decision\" && \"branches\" in node);\n}\n","/**\n * ANSI color utilities for terminal output.\n */\n\nimport type { ColorScheme, StepState } from \"../types\";\n\n// =============================================================================\n// ANSI Escape Codes\n// =============================================================================\n\nconst RESET = \"\\x1b[0m\";\nconst BOLD = \"\\x1b[1m\";\nconst DIM = \"\\x1b[2m\";\n\n// Foreground colors\nconst FG_RED = \"\\x1b[31m\";\nconst FG_GREEN = \"\\x1b[32m\";\nconst FG_YELLOW = \"\\x1b[33m\";\nconst FG_BLUE = \"\\x1b[34m\";\nconst FG_GRAY = \"\\x1b[90m\";\nconst FG_WHITE = \"\\x1b[37m\";\n\n// =============================================================================\n// Color Functions\n// =============================================================================\n\n/**\n * Apply ANSI color to text.\n */\nexport function colorize(text: string, color: string): string {\n if (!color) return text;\n return `${color}${text}${RESET}`;\n}\n\n/**\n * Make text bold.\n */\nexport function bold(text: string): string {\n return `${BOLD}${text}${RESET}`;\n}\n\n/**\n * Make text dim.\n */\nexport function dim(text: string): string {\n return `${DIM}${text}${RESET}`;\n}\n\n// =============================================================================\n// Default Color Scheme\n// =============================================================================\n\n/**\n * Default ANSI color scheme for step states.\n */\nexport const defaultColorScheme: ColorScheme = {\n pending: FG_WHITE,\n running: FG_YELLOW,\n success: FG_GREEN,\n error: FG_RED,\n aborted: FG_GRAY,\n cached: FG_BLUE,\n skipped: DIM + FG_GRAY, // Dim gray for skipped steps\n};\n\n// =============================================================================\n// State Symbols\n// =============================================================================\n\n/**\n * Get the symbol for a step state.\n */\nexport function getStateSymbol(state: StepState): string {\n switch (state) {\n case \"pending\":\n return \"○\"; // Empty circle\n case \"running\":\n return \"⟳\"; // Rotating arrows\n case \"success\":\n return \"✓\"; // Check mark\n case \"error\":\n return \"✗\"; // X mark\n case \"aborted\":\n return \"⊘\"; // Circled slash\n case \"cached\":\n return \"↺\"; // Cached/replay\n case \"skipped\":\n return \"⊘\"; // Circled slash (same as aborted, but different color)\n }\n}\n\n/**\n * Get the colored symbol for a step state.\n */\nexport function getColoredSymbol(state: StepState, colors: ColorScheme): string {\n const symbol = getStateSymbol(state);\n return colorize(symbol, colors[state]);\n}\n\n/**\n * Get colored text based on step state.\n */\nexport function colorByState(\n text: string,\n state: StepState,\n colors: ColorScheme\n): string {\n return colorize(text, colors[state]);\n}\n\n// =============================================================================\n// Strip ANSI\n// =============================================================================\n\n/**\n * Strip ANSI escape codes from a string.\n * Useful for calculating visible string length.\n */\nexport function stripAnsi(str: string): string {\n // eslint-disable-next-line no-control-regex\n return str.replace(/\\x1b\\[[0-9;]*m/g, \"\");\n}\n\n/**\n * Get the visible length of a string (without ANSI codes).\n */\nexport function visibleLength(str: string): string {\n return stripAnsi(str);\n}\n","/**\n * ASCII Terminal Renderer\n *\n * Renders the workflow IR as ASCII art with box-drawing characters\n * and ANSI colors for terminal display.\n */\n\nimport type {\n FlowNode,\n ParallelNode,\n RaceNode,\n DecisionNode,\n Renderer,\n RenderOptions,\n StepNode,\n WorkflowIR,\n} from \"../types\";\nimport { isParallelNode, isRaceNode, isStepNode, isDecisionNode } from \"../types\";\nimport { formatDuration } from \"../utils/timing\";\nimport {\n bold,\n colorByState,\n colorize,\n defaultColorScheme,\n dim,\n getColoredSymbol,\n stripAnsi,\n} from \"./colors\";\n\n// =============================================================================\n// Box Drawing Characters\n// =============================================================================\n\nconst BOX = {\n topLeft: \"┌\",\n topRight: \"┐\",\n bottomLeft: \"└\",\n bottomRight: \"┘\",\n horizontal: \"─\",\n vertical: \"│\",\n teeRight: \"├\",\n teeLeft: \"┤\",\n teeDown: \"┬\",\n teeUp: \"┴\",\n cross: \"┼\",\n} as const;\n\n// =============================================================================\n// Helper Functions\n// =============================================================================\n\n/**\n * Pad a string to a fixed width, accounting for ANSI codes.\n */\nfunction padEnd(str: string, width: number): string {\n const visibleLen = stripAnsi(str).length;\n const padding = Math.max(0, width - visibleLen);\n return str + \" \".repeat(padding);\n}\n\n/**\n * Create a horizontal line with optional title.\n */\nfunction horizontalLine(width: number, title?: string): string {\n if (!title) {\n return BOX.horizontal.repeat(width);\n }\n\n const titleText = ` ${title} `;\n const remainingWidth = width - titleText.length;\n if (remainingWidth < 4) {\n return BOX.horizontal.repeat(width);\n }\n\n const leftPad = 2;\n const rightPad = remainingWidth - leftPad;\n\n return (\n BOX.horizontal.repeat(leftPad) + titleText + BOX.horizontal.repeat(rightPad)\n );\n}\n\n// =============================================================================\n// ASCII Renderer\n// =============================================================================\n\n/**\n * Create the ASCII terminal renderer.\n */\nexport function asciiRenderer(): Renderer {\n return {\n name: \"ascii\",\n supportsLive: true,\n\n render(ir: WorkflowIR, options: RenderOptions): string {\n const colors = { ...defaultColorScheme, ...options.colors };\n const width = options.terminalWidth ?? 60;\n const innerWidth = width - 4; // Account for borders\n\n const lines: string[] = [];\n\n // Header\n const workflowName = ir.root.name ?? \"workflow\";\n const headerTitle = bold(workflowName);\n lines.push(\n `${BOX.topLeft}${horizontalLine(width - 2, headerTitle)}${BOX.topRight}`\n );\n lines.push(`${BOX.vertical}${\" \".repeat(width - 2)}${BOX.vertical}`);\n\n // Render children\n const childLines = renderNodes(ir.root.children, options, colors, 0);\n for (const line of childLines) {\n lines.push(\n `${BOX.vertical} ${padEnd(line, innerWidth)}${BOX.vertical}`\n );\n }\n\n // Footer with timing\n lines.push(`${BOX.vertical}${\" \".repeat(width - 2)}${BOX.vertical}`);\n\n if (ir.root.durationMs !== undefined && options.showTimings) {\n const status = ir.root.state === \"success\" ? \"Completed\" : \"Failed\";\n const statusColored = colorByState(status, ir.root.state, colors);\n const footer = `${statusColored} in ${formatDuration(ir.root.durationMs)}`;\n lines.push(\n `${BOX.vertical} ${padEnd(footer, innerWidth)}${BOX.vertical}`\n );\n lines.push(`${BOX.vertical}${\" \".repeat(width - 2)}${BOX.vertical}`);\n }\n\n lines.push(\n `${BOX.bottomLeft}${BOX.horizontal.repeat(width - 2)}${BOX.bottomRight}`\n );\n\n return lines.join(\"\\n\");\n },\n };\n}\n\n/**\n * Render a list of nodes.\n */\nfunction renderNodes(\n nodes: FlowNode[],\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number\n): string[] {\n const lines: string[] = [];\n\n for (const node of nodes) {\n if (isStepNode(node)) {\n lines.push(renderStepNode(node, options, colors));\n } else if (isParallelNode(node)) {\n lines.push(...renderParallelNode(node, options, colors, depth));\n } else if (isRaceNode(node)) {\n lines.push(...renderRaceNode(node, options, colors, depth));\n } else if (isDecisionNode(node)) {\n lines.push(...renderDecisionNode(node, options, colors, depth));\n }\n }\n\n return lines;\n}\n\n/**\n * Render a single step node.\n */\nfunction renderStepNode(\n node: StepNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>\n): string {\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? node.key ?? \"step\";\n const nameColored = colorByState(name, node.state, colors);\n\n let line = `${symbol} ${nameColored}`;\n\n // Add key if requested\n if (options.showKeys && node.key) {\n line += dim(` [key: ${node.key}]`);\n }\n\n // Add input/output if available (for decision understanding)\n if (node.input !== undefined) {\n const inputStr = typeof node.input === \"string\" \n ? node.input \n : JSON.stringify(node.input).slice(0, 30);\n line += dim(` [in: ${inputStr}${inputStr.length >= 30 ? \"...\" : \"\"}]`);\n }\n if (node.output !== undefined && node.state === \"success\") {\n const outputStr = typeof node.output === \"string\"\n ? node.output\n : JSON.stringify(node.output).slice(0, 30);\n line += dim(` [out: ${outputStr}${outputStr.length >= 30 ? \"...\" : \"\"}]`);\n }\n\n // Add timing if available and requested\n if (options.showTimings && node.durationMs !== undefined) {\n line += dim(` [${formatDuration(node.durationMs)}]`);\n }\n\n // Add retry indicator if retries occurred\n if (node.retryCount !== undefined && node.retryCount > 0) {\n line += dim(` [${node.retryCount} ${node.retryCount === 1 ? \"retry\" : \"retries\"}]`);\n }\n\n // Add timeout indicator if step timed out\n if (node.timedOut) {\n const timeoutInfo = node.timeoutMs !== undefined ? ` ${node.timeoutMs}ms` : \"\";\n line += dim(` [timeout${timeoutInfo}]`);\n }\n\n return line;\n}\n\n/**\n * Render a parallel node (allAsync).\n */\nfunction renderParallelNode(\n node: ParallelNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number\n): string[] {\n const lines: string[] = [];\n const indent = \" \".repeat(depth);\n\n // Header\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? \"parallel\";\n const mode = node.mode === \"allSettled\" ? \" (allSettled)\" : \"\";\n lines.push(`${indent}${BOX.teeRight}${BOX.teeDown}${BOX.horizontal} ${symbol} ${bold(name)}${mode}`);\n\n // Children\n if (node.children.length === 0) {\n // Empty parallel scope - operations inside allAsync/anyAsync weren't tracked as steps\n lines.push(`${indent}${BOX.vertical} ${dim(\"(operations not individually tracked)\")}`);\n lines.push(`${indent}${BOX.vertical} ${dim(\"(wrap each operation with step() to see individual steps)\")}`);\n } else {\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i];\n const isLast = i === node.children.length - 1;\n const prefix = isLast ? `${indent}${BOX.vertical} ${BOX.bottomLeft}` : `${indent}${BOX.vertical} ${BOX.teeRight}`;\n\n if (isStepNode(child)) {\n lines.push(`${prefix} ${renderStepNode(child, options, colors)}`);\n } else {\n // Nested structure - recurse\n const nestedLines = renderNodes([child], options, colors, depth + 1);\n for (const line of nestedLines) {\n lines.push(`${indent}${BOX.vertical} ${line}`);\n }\n }\n }\n }\n\n // Timing footer\n if (options.showTimings && node.durationMs !== undefined) {\n lines.push(`${indent}${BOX.bottomLeft}${BOX.horizontal}${BOX.horizontal} ${dim(`[${formatDuration(node.durationMs)}]`)}`);\n }\n\n return lines;\n}\n\n/**\n * Render a race node (anyAsync).\n */\nfunction renderRaceNode(\n node: RaceNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number\n): string[] {\n const lines: string[] = [];\n const indent = \" \".repeat(depth);\n\n // Header with lightning bolt for race\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? \"race\";\n lines.push(`${indent}${BOX.teeRight}⚡ ${symbol} ${bold(name)}`);\n\n // Children\n if (node.children.length === 0) {\n // Empty race scope - operations inside anyAsync weren't tracked as steps\n lines.push(`${indent}${BOX.vertical} ${dim(\"(operations not individually tracked)\")}`);\n lines.push(`${indent}${BOX.vertical} ${dim(\"(wrap each operation with step() to see individual steps)\")}`);\n } else {\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i];\n const isLast = i === node.children.length - 1;\n const prefix = isLast ? `${indent}${BOX.vertical} ${BOX.bottomLeft}` : `${indent}${BOX.vertical} ${BOX.teeRight}`;\n\n // Mark winner\n const isWinner = node.winnerId && child.id === node.winnerId;\n const winnerSuffix = isWinner ? dim(\" (winner)\") : \"\";\n\n if (isStepNode(child)) {\n lines.push(`${prefix} ${renderStepNode(child, options, colors)}${winnerSuffix}`);\n } else {\n const nestedLines = renderNodes([child], options, colors, depth + 1);\n for (const line of nestedLines) {\n lines.push(`${indent}${BOX.vertical} ${line}`);\n }\n }\n }\n }\n\n // Timing footer\n if (options.showTimings && node.durationMs !== undefined) {\n lines.push(`${indent}${BOX.bottomLeft}${BOX.horizontal}${BOX.horizontal} ${dim(`[${formatDuration(node.durationMs)}]`)}`);\n }\n\n return lines;\n}\n\n/**\n * Render a decision node (conditional branch).\n */\nfunction renderDecisionNode(\n node: DecisionNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number\n): string[] {\n const lines: string[] = [];\n const indent = \" \".repeat(depth);\n\n // Header with decision info\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? \"decision\";\n const condition = node.condition \n ? dim(` (${node.condition})`)\n : \"\";\n const decisionValue = node.decisionValue !== undefined\n ? dim(` = ${String(node.decisionValue)}`)\n : \"\";\n const branchTaken = node.branchTaken !== undefined\n ? dim(` → ${String(node.branchTaken)}`)\n : \"\";\n\n lines.push(\n `${indent}${BOX.teeRight}${BOX.teeDown}${BOX.horizontal} ${symbol} ${bold(name)}${condition}${decisionValue}${branchTaken}`\n );\n\n // Render branches\n for (let i = 0; i < node.branches.length; i++) {\n const branch = node.branches[i];\n const isLast = i === node.branches.length - 1;\n const prefix = isLast \n ? `${indent}${BOX.vertical} ${BOX.bottomLeft}` \n : `${indent}${BOX.vertical} ${BOX.teeRight}`;\n\n // Branch label with taken/skipped indicator\n const branchSymbol = branch.taken ? \"✓\" : \"⊘\";\n const branchColor = branch.taken ? colors.success : colors.skipped;\n const branchLabel = colorize(\n `${branchSymbol} ${branch.label}`,\n branchColor\n );\n const branchCondition = branch.condition\n ? dim(` (${branch.condition})`)\n : \"\";\n\n lines.push(`${prefix} ${branchLabel}${branchCondition}`);\n\n // Render children of this branch\n if (branch.children.length > 0) {\n const childLines = renderNodes(branch.children, options, colors, depth + 1);\n for (const line of childLines) {\n lines.push(`${indent}${BOX.vertical} ${line}`);\n }\n } else if (!branch.taken) {\n // Show that this branch was skipped\n lines.push(\n `${indent}${BOX.vertical} ${dim(\"(skipped)\")}`\n );\n }\n }\n\n // Timing footer\n if (options.showTimings && node.durationMs !== undefined) {\n lines.push(\n `${indent}${BOX.bottomLeft}${BOX.horizontal}${BOX.horizontal} ${dim(`[${formatDuration(node.durationMs)}]`)}`\n );\n }\n\n return lines;\n}\n\nexport { defaultColorScheme };\n","/**\n * Mermaid Diagram Renderer\n *\n * Renders the workflow IR as a Mermaid flowchart diagram.\n * Supports sequential flows, parallel (subgraph), and race patterns.\n */\n\nimport type {\n FlowNode,\n ParallelNode,\n RaceNode,\n DecisionNode,\n Renderer,\n RenderOptions,\n StepNode,\n StepState,\n WorkflowIR,\n} from \"../types\";\nimport { isParallelNode, isRaceNode, isStepNode, isDecisionNode } from \"../types\";\nimport { formatDuration } from \"../utils/timing\";\n\n// =============================================================================\n// Mermaid Style Definitions\n// =============================================================================\n\n/**\n * Get Mermaid class definition for step states.\n * Colors inspired by AWS Step Functions and XState visualizers for professional appearance.\n */\nfunction getStyleDefinitions(): string[] {\n return [\n // Pending - light gray, subtle\n \" classDef pending fill:#f3f4f6,stroke:#9ca3af,stroke-width:2px,color:#374151\",\n // Running - amber/yellow, indicates active execution\n \" classDef running fill:#fef3c7,stroke:#f59e0b,stroke-width:3px,color:#92400e\",\n // Success - green, clear positive indicator\n \" classDef success fill:#d1fae5,stroke:#10b981,stroke-width:3px,color:#065f46\",\n // Error - red, clear negative indicator\n \" classDef error fill:#fee2e2,stroke:#ef4444,stroke-width:3px,color:#991b1b\",\n // Aborted - gray, indicates cancellation\n \" classDef aborted fill:#f3f4f6,stroke:#6b7280,stroke-width:2px,color:#4b5563,stroke-dasharray: 5 5\",\n // Cached - blue, indicates cache hit\n \" classDef cached fill:#dbeafe,stroke:#3b82f6,stroke-width:3px,color:#1e40af\",\n // Skipped - light gray with dashed border\n \" classDef skipped fill:#f9fafb,stroke:#d1d5db,stroke-width:2px,color:#6b7280,stroke-dasharray: 5 5\",\n ];\n}\n\n/**\n * Get the Mermaid class name for a step state.\n */\nfunction getStateClass(state: StepState): string {\n return state;\n}\n\n// =============================================================================\n// Node ID Generation\n// =============================================================================\n\nlet nodeCounter = 0;\n\nfunction generateNodeId(prefix: string = \"node\"): string {\n return `${prefix}_${++nodeCounter}`;\n}\n\nfunction resetNodeCounter(): void {\n nodeCounter = 0;\n}\n\n// =============================================================================\n// Mermaid Text Escaping\n// =============================================================================\n\n/**\n * Escape text for use in Mermaid diagrams.\n * Removes characters that break Mermaid parsing.\n * \n * Characters removed:\n * - {}[]() - Brackets and parentheses break parsing in labels\n * - <> - Angle brackets can cause issues\n * - \" - Double quotes replaced with single quotes\n * \n * @param text - Text to escape\n * @returns Escaped text safe for Mermaid\n */\nfunction escapeMermaidText(text: string): string {\n return text\n .replace(/[{}[\\]()]/g, \"\") // Remove brackets and parentheses (they break parsing)\n .replace(/[<>]/g, \"\") // Remove angle brackets\n .replace(/\"/g, \"'\") // Replace double quotes with single\n .trim();\n}\n\n/**\n * Escape text for use in Mermaid subgraph names.\n * Subgraph names in brackets need special handling.\n * \n * @param text - Text to escape for subgraph name\n * @returns Escaped text safe for subgraph names\n */\nfunction escapeSubgraphName(text: string): string {\n return escapeMermaidText(text)\n .replace(/[[\\]]/g, \"\"); // Also remove brackets from subgraph names\n}\n\n// =============================================================================\n// Mermaid Renderer\n// =============================================================================\n\n/**\n * Create the Mermaid diagram renderer.\n */\nexport function mermaidRenderer(): Renderer {\n return {\n name: \"mermaid\",\n supportsLive: false,\n\n render(ir: WorkflowIR, options: RenderOptions): string {\n resetNodeCounter();\n const lines: string[] = [];\n\n // Diagram header\n lines.push(\"flowchart TD\");\n\n // Start node - more visually distinctive\n const startId = \"start\";\n lines.push(` ${startId}((\"▶ Start\"))`);\n\n // Track the last node for connections\n let prevNodeId = startId;\n\n // Render children\n for (const child of ir.root.children) {\n const result = renderNode(child, options, lines);\n lines.push(` ${prevNodeId} --> ${result.entryId}`);\n prevNodeId = result.exitId;\n }\n\n // End node (if workflow completed) - more visually distinctive\n if (ir.root.state === \"success\" || ir.root.state === \"error\") {\n const endId = \"finish\";\n const endIcon = ir.root.state === \"success\" ? \"✓\" : \"✗\";\n const endLabel = ir.root.state === \"success\" ? \"Done\" : \"Failed\";\n const endShape = `((\"${endIcon} ${endLabel}\"))`;\n const endClass =\n ir.root.state === \"success\" ? \":::success\" : \":::error\";\n lines.push(` ${endId}${endShape}${endClass}`);\n lines.push(` ${prevNodeId} --> ${endId}`);\n }\n\n // Add style definitions\n lines.push(\"\");\n lines.push(...getStyleDefinitions());\n\n return lines.join(\"\\n\");\n },\n };\n}\n\n/**\n * Render result with entry and exit node IDs.\n */\ninterface RenderResult {\n entryId: string;\n exitId: string;\n}\n\n/**\n * Render a node and return its entry/exit IDs.\n */\nfunction renderNode(\n node: FlowNode,\n options: RenderOptions,\n lines: string[]\n): RenderResult {\n if (isStepNode(node)) {\n return renderStepNode(node, options, lines);\n } else if (isParallelNode(node)) {\n return renderParallelNode(node, options, lines);\n } else if (isRaceNode(node)) {\n return renderRaceNode(node, options, lines);\n } else if (isDecisionNode(node)) {\n return renderDecisionNode(node, options, lines);\n }\n\n // Fallback for sequence or unknown nodes\n const id = generateNodeId(\"unknown\");\n lines.push(` ${id}[Unknown Node]`);\n return { entryId: id, exitId: id };\n}\n\n/**\n * Render a step node.\n */\nfunction renderStepNode(\n node: StepNode,\n options: RenderOptions,\n lines: string[]\n): RenderResult {\n const id = node.key\n ? `step_${node.key.replace(/[^a-zA-Z0-9]/g, \"_\")}`\n : generateNodeId(\"step\");\n\n const label = escapeMermaidText(node.name ?? node.key ?? \"Step\");\n \n // Format timing - use space instead of parentheses to avoid Mermaid parse errors\n const timing =\n options.showTimings && node.durationMs !== undefined\n ? ` ${formatDuration(node.durationMs)}`\n : \"\";\n\n // Add visual indicators based on state (like XState/AWS Step Functions)\n let stateIcon = \"\";\n switch (node.state) {\n case \"success\":\n stateIcon = \"✓ \";\n break;\n case \"error\":\n stateIcon = \"✗ \";\n break;\n case \"cached\":\n stateIcon = \"💾 \";\n break;\n case \"running\":\n stateIcon = \"⏳ \";\n break;\n case \"skipped\":\n stateIcon = \"⊘ \";\n break;\n }\n\n // Add input/output info if available\n // Use newlines for multi-line labels, but escape special characters\n let ioInfo = \"\";\n if (node.input !== undefined) {\n const inputStr = typeof node.input === \"string\"\n ? escapeMermaidText(node.input)\n : escapeMermaidText(JSON.stringify(node.input).slice(0, 20));\n ioInfo += `\\\\nin: ${inputStr}`;\n }\n if (node.output !== undefined && node.state === \"success\") {\n const outputStr = typeof node.output === \"string\"\n ? escapeMermaidText(node.output)\n : escapeMermaidText(JSON.stringify(node.output).slice(0, 20));\n ioInfo += `\\\\nout: ${outputStr}`;\n }\n\n // Add retry/timeout indicators with icons\n let retryInfo = \"\";\n if (node.retryCount !== undefined && node.retryCount > 0) {\n retryInfo += `\\\\n↻ ${node.retryCount} retr${node.retryCount === 1 ? \"y\" : \"ies\"}`;\n }\n if (node.timedOut) {\n const timeoutStr = node.timeoutMs !== undefined ? `${node.timeoutMs}ms` : \"\";\n retryInfo += `\\\\n⏱ timeout ${timeoutStr}`;\n }\n\n // Combine all label parts with icon\n const escapedLabel = (stateIcon + label + ioInfo + retryInfo + timing).trim();\n\n const stateClass = getStateClass(node.state);\n\n // Use different shapes based on state (like AWS Step Functions)\n let shape: string;\n switch (node.state) {\n case \"error\":\n // Hexagon for errors (more distinctive)\n shape = `{{${escapedLabel}}}`;\n break;\n case \"cached\":\n // Rounded rectangle with double border for cached\n shape = `[(${escapedLabel})]`;\n break;\n case \"skipped\":\n // Dashed border for skipped\n shape = `[${escapedLabel}]:::skipped`;\n break;\n default:\n // Standard rectangle for normal steps\n shape = `[${escapedLabel}]`;\n }\n\n lines.push(` ${id}${shape}:::${stateClass}`);\n\n return { entryId: id, exitId: id };\n}\n\n/**\n * Render a parallel node as a subgraph with fork/join.\n */\nfunction renderParallelNode(\n node: ParallelNode,\n options: RenderOptions,\n lines: string[]\n): RenderResult {\n const subgraphId = generateNodeId(\"parallel\");\n const forkId = `${subgraphId}_fork`;\n const joinId = `${subgraphId}_join`;\n const name = escapeSubgraphName(node.name ?? \"Parallel\");\n const modeLabel = node.mode === \"allSettled\" ? \" (allSettled)\" : \"\";\n\n // If no children, render as a simple step-like node with note\n if (node.children.length === 0) {\n const id = subgraphId;\n const label = escapeMermaidText(`${name}${modeLabel}`);\n const note = \"operations not individually tracked\";\n const timing = options.showTimings && node.durationMs !== undefined\n ? ` ${formatDuration(node.durationMs)}`\n : \"\";\n \n // Use a rounded rectangle to indicate it's a parallel operation\n lines.push(` ${id}[${label}${timing}\\\\n${note}]:::${getStateClass(node.state)}`);\n return { entryId: id, exitId: id };\n }\n\n // Subgraph for parallel block with proper visual hierarchy\n lines.push(` subgraph ${subgraphId}[\"${name}${modeLabel}\"]`);\n lines.push(` direction TB`);\n\n // Fork node (diamond) - more visually distinct\n lines.push(` ${forkId}{\"⚡ Fork\"}`);\n\n // Child branches - render in parallel columns\n const childExitIds: string[] = [];\n for (const child of node.children) {\n const result = renderNode(child, options, lines);\n lines.push(` ${forkId} --> ${result.entryId}`);\n childExitIds.push(result.exitId);\n }\n\n // Join node (diamond) - visually distinct\n lines.push(` ${joinId}{\"✓ Join\"}`);\n for (const exitId of childExitIds) {\n lines.push(` ${exitId} --> ${joinId}`);\n }\n\n lines.push(` end`);\n\n // Apply state styling to subgraph\n const stateClass = getStateClass(node.state);\n lines.push(` class ${subgraphId} ${stateClass}`);\n\n return { entryId: forkId, exitId: joinId };\n}\n\n/**\n * Render a race node as a subgraph with racing indicator.\n */\nfunction renderRaceNode(\n node: RaceNode,\n options: RenderOptions,\n lines: string[]\n): RenderResult {\n const subgraphId = generateNodeId(\"race\");\n const startId = `${subgraphId}_start`;\n const endId = `${subgraphId}_end`;\n const name = escapeSubgraphName(node.name ?? \"Race\");\n\n // If no children, render as a simple step-like node with note\n if (node.children.length === 0) {\n const id = subgraphId;\n const label = escapeMermaidText(name);\n const note = \"operations not individually tracked\";\n const timing = options.showTimings && node.durationMs !== undefined\n ? ` ${formatDuration(node.durationMs)}`\n : \"\";\n \n lines.push(` ${id}[⚡ ${label}${timing}\\\\n${note}]:::${getStateClass(node.state)}`);\n return { entryId: id, exitId: id };\n }\n\n // Subgraph for race block - escape name and emoji is safe in quoted strings\n lines.push(` subgraph ${subgraphId}[\"⚡ ${name}\"]`);\n lines.push(` direction TB`);\n\n // Start node - use a more distinctive shape\n lines.push(` ${startId}((\"🏁 Start\"))`);\n\n // Child branches\n const childExitIds: Array<{ exitId: string; isWinner: boolean }> = [];\n let winnerExitId: string | undefined;\n \n for (const child of node.children) {\n const result = renderNode(child, options, lines);\n const isWinner = isStepNode(child) && node.winnerId === child.id;\n lines.push(` ${startId} --> ${result.entryId}`);\n \n if (isWinner) {\n winnerExitId = result.exitId;\n }\n childExitIds.push({ exitId: result.exitId, isWinner });\n }\n\n // End node - more distinctive\n lines.push(` ${endId}((\"✓ First\"))`);\n \n // Connect winner with thick line, others with dashed (cancelled)\n for (const { exitId, isWinner } of childExitIds) {\n if (isWinner && winnerExitId) {\n lines.push(` ${exitId} ==>|🏆 Winner| ${endId}`);\n } else if (node.winnerId) {\n // Non-winner: show as cancelled\n lines.push(` ${exitId} -. cancelled .-> ${endId}`);\n } else {\n // No winner determined, normal connection\n lines.push(` ${exitId} --> ${endId}`);\n }\n }\n\n lines.push(` end`);\n\n const stateClass = getStateClass(node.state);\n lines.push(` class ${subgraphId} ${stateClass}`);\n\n return { entryId: startId, exitId: endId };\n}\n\n/**\n * Render a decision node as a diamond with branches.\n */\nfunction renderDecisionNode(\n node: DecisionNode,\n options: RenderOptions,\n lines: string[]\n): RenderResult {\n const decisionId = node.key\n ? `decision_${node.key.replace(/[^a-zA-Z0-9]/g, \"_\")}`\n : generateNodeId(\"decision\");\n\n // Escape condition and decision value - remove characters that break Mermaid\n const condition = escapeMermaidText(node.condition ?? \"condition\");\n const decisionValue = node.decisionValue !== undefined\n ? ` = ${escapeMermaidText(String(node.decisionValue)).slice(0, 30)}`\n : \"\";\n\n // Decision diamond - ensure no invalid characters\n const decisionLabel = `${condition}${decisionValue}`.trim();\n lines.push(` ${decisionId}{${decisionLabel}}`);\n\n // Render branches\n const branchExitIds: string[] = [];\n let takenBranchExitId: string | undefined;\n\n for (const branch of node.branches) {\n const branchId = `${decisionId}_${branch.label.replace(/[^a-zA-Z0-9]/g, \"_\")}`;\n // Escape branch label - remove parentheses and other special chars\n const branchLabelText = escapeMermaidText(branch.label);\n const branchLabel = branch.taken\n ? `${branchLabelText} ✓`\n : `${branchLabelText} skipped`;\n const branchClass = branch.taken ? \":::success\" : \":::skipped\";\n\n // Branch label node\n lines.push(` ${branchId}[${branchLabel}]${branchClass}`);\n\n // Connect decision to branch\n // Mermaid edge labels must be simple text - escape special characters\n // Also remove pipe character as it's used for edge label syntax\n const edgeLabel = branch.condition \n ? `|${escapeMermaidText(branch.condition).replace(/\\|/g, \"\")}|` \n : \"\";\n lines.push(` ${decisionId} -->${edgeLabel} ${branchId}`);\n\n // Render children of this branch\n if (branch.children.length > 0) {\n let prevId = branchId;\n for (const child of branch.children) {\n const result = renderNode(child, options, lines);\n lines.push(` ${prevId} --> ${result.entryId}`);\n prevId = result.exitId;\n }\n branchExitIds.push(prevId);\n if (branch.taken) {\n takenBranchExitId = prevId;\n }\n } else {\n branchExitIds.push(branchId);\n if (branch.taken) {\n takenBranchExitId = branchId;\n }\n }\n }\n\n // Join point (if we have a taken branch)\n if (takenBranchExitId) {\n return { entryId: decisionId, exitId: takenBranchExitId };\n }\n\n // If no branch was taken, return decision as exit\n return { entryId: decisionId, exitId: decisionId };\n}\n\nexport { mermaidRenderer as default };\n","/**\n * Live Visualizer - Real-time terminal updates during workflow execution.\n *\n * Uses ANSI escape codes to update the terminal in-place, showing\n * workflow progress as it happens.\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n LiveVisualizerOptions,\n RenderOptions,\n ScopeEndEvent,\n ScopeStartEvent,\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n WorkflowIR,\n} from \"./types\";\nimport { createIRBuilder } from \"./ir-builder\";\nimport { asciiRenderer, defaultColorScheme } from \"./renderers\";\n\n// =============================================================================\n// ANSI Escape Codes\n// =============================================================================\n\nconst ANSI = {\n /** Clear from cursor to end of screen */\n clearToEnd: \"\\x1b[J\",\n /** Move cursor up N lines */\n cursorUp: (n: number) => `\\x1b[${n}A`,\n /** Move cursor to beginning of line */\n cursorToStart: \"\\x1b[G\",\n /** Hide cursor */\n hideCursor: \"\\x1b[?25l\",\n /** Show cursor */\n showCursor: \"\\x1b[?25h\",\n /** Save cursor position */\n saveCursor: \"\\x1b[s\",\n /** Restore cursor position */\n restoreCursor: \"\\x1b[u\",\n};\n\n// =============================================================================\n// Live Visualizer Interface\n// =============================================================================\n\n/**\n * Live visualizer with real-time terminal updates.\n */\nexport interface LiveVisualizer {\n /** Process a workflow event */\n handleEvent: (event: WorkflowEvent<unknown>) => void;\n /** Process a scope event */\n handleScopeEvent: (event: ScopeStartEvent | ScopeEndEvent) => void;\n /** Process a decision event */\n handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;\n /** Get current IR state */\n getIR: () => WorkflowIR;\n /** Render current state to string (without terminal output) */\n render: () => string;\n /** Start live rendering to terminal */\n start: () => void;\n /** Stop live rendering */\n stop: () => void;\n /** Force an immediate redraw */\n refresh: () => void;\n /** Reset state for a new workflow */\n reset: () => void;\n}\n\n// =============================================================================\n// Create Live Visualizer\n// =============================================================================\n\n/**\n * Create a live visualizer for real-time terminal updates.\n *\n * @example\n * ```typescript\n * const live = createLiveVisualizer({ workflowName: 'my-workflow' });\n * const workflow = createWorkflow(deps, { onEvent: live.handleEvent });\n *\n * live.start();\n * await workflow(async (step) => { ... });\n * live.stop();\n * ```\n */\nexport function createLiveVisualizer(\n options: LiveVisualizerOptions = {}\n): LiveVisualizer {\n const {\n workflowName,\n detectParallel = true,\n showTimings = true,\n showKeys = false,\n colors: customColors,\n stream = process.stdout,\n updateInterval = 100,\n } = options;\n\n const builder = createIRBuilder({ detectParallel });\n const renderer = asciiRenderer();\n\n // Render options\n const renderOptions: RenderOptions = {\n showTimings,\n showKeys,\n terminalWidth: stream.columns ?? 80,\n colors: { ...defaultColorScheme, ...customColors },\n };\n\n // State\n let isRunning = false;\n let lastOutput = \"\";\n let lastLineCount = 0;\n let throttleTimeout: ReturnType<typeof setTimeout> | null = null;\n let pendingUpdate = false;\n\n /**\n * Write to the output stream.\n */\n function write(text: string): void {\n if (stream.writable) {\n stream.write(text);\n }\n }\n\n /**\n * Clear the previous output and write new content.\n */\n function redraw(): void {\n if (!isRunning) return;\n\n const ir = getIR();\n const output = renderer.render(ir, renderOptions);\n\n // If output hasn't changed, skip redraw\n if (output === lastOutput) return;\n\n // Clear previous output\n if (lastLineCount > 0) {\n // Move cursor up and clear\n write(ANSI.cursorUp(lastLineCount));\n write(ANSI.cursorToStart);\n write(ANSI.clearToEnd);\n }\n\n // Write new output\n write(output);\n write(\"\\n\");\n\n // Track line count for next clear\n lastOutput = output;\n lastLineCount = output.split(\"\\n\").length;\n }\n\n /**\n * Schedule a throttled redraw.\n */\n function scheduleRedraw(): void {\n if (!isRunning) return;\n\n pendingUpdate = true;\n\n if (throttleTimeout === null) {\n throttleTimeout = setTimeout(() => {\n throttleTimeout = null;\n if (pendingUpdate) {\n pendingUpdate = false;\n redraw();\n }\n }, updateInterval);\n }\n }\n\n /**\n * Handle a workflow event.\n */\n function handleEvent(event: WorkflowEvent<unknown>): void {\n // Route scope events to handleScopeEvent for proper IR building\n if (event.type === \"scope_start\" || event.type === \"scope_end\") {\n handleScopeEvent(event as ScopeStartEvent | ScopeEndEvent);\n return;\n }\n\n builder.handleEvent(event);\n\n if (isRunning) {\n // Immediate redraw for start/end events, throttled for others\n if (\n event.type === \"workflow_start\" ||\n event.type === \"workflow_success\" ||\n event.type === \"workflow_error\"\n ) {\n redraw();\n } else {\n scheduleRedraw();\n }\n }\n }\n\n /**\n * Handle a scope event.\n */\n function handleScopeEvent(event: ScopeStartEvent | ScopeEndEvent): void {\n builder.handleScopeEvent(event);\n if (isRunning) {\n scheduleRedraw();\n }\n }\n\n /**\n * Handle a decision event.\n */\n function handleDecisionEvent(\n event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent\n ): void {\n builder.handleDecisionEvent(event);\n if (isRunning) {\n scheduleRedraw();\n }\n }\n\n /**\n * Get the current IR state.\n */\n function getIR(): WorkflowIR {\n const ir = builder.getIR();\n if (workflowName && !ir.root.name) {\n ir.root.name = workflowName;\n }\n return ir;\n }\n\n /**\n * Render current state to string.\n */\n function render(): string {\n return renderer.render(getIR(), renderOptions);\n }\n\n /**\n * Start live rendering.\n */\n function start(): void {\n if (isRunning) return;\n\n isRunning = true;\n lastOutput = \"\";\n lastLineCount = 0;\n\n // Hide cursor during updates\n write(ANSI.hideCursor);\n\n // Initial render\n redraw();\n }\n\n /**\n * Stop live rendering.\n */\n function stop(): void {\n if (!isRunning) return;\n\n isRunning = false;\n\n // Clear any pending throttle\n if (throttleTimeout !== null) {\n clearTimeout(throttleTimeout);\n throttleTimeout = null;\n }\n\n // Final redraw to show completed state\n const ir = getIR();\n const output = renderer.render(ir, renderOptions);\n\n if (lastLineCount > 0) {\n write(ANSI.cursorUp(lastLineCount));\n write(ANSI.cursorToStart);\n write(ANSI.clearToEnd);\n }\n\n write(output);\n write(\"\\n\");\n\n // Show cursor again\n write(ANSI.showCursor);\n }\n\n /**\n * Force an immediate redraw.\n */\n function refresh(): void {\n if (throttleTimeout !== null) {\n clearTimeout(throttleTimeout);\n throttleTimeout = null;\n }\n pendingUpdate = false;\n redraw();\n }\n\n /**\n * Reset state for a new workflow.\n */\n function reset(): void {\n builder.reset();\n lastOutput = \"\";\n lastLineCount = 0;\n }\n\n return {\n handleEvent,\n handleScopeEvent,\n handleDecisionEvent,\n getIR,\n render,\n start,\n stop,\n refresh,\n reset,\n };\n}\n","/**\n * Decision Tracker - Helper for tracking conditional logic in workflows.\n *\n * This module provides utilities to track decision points (if/switch statements)\n * so they can be visualized in workflow diagrams.\n *\n * @example\n * ```typescript\n * import { trackDecision } from '@jagreehal/workflow/visualize';\n *\n * const workflow = createWorkflow(deps, {\n * onEvent: (event) => {\n * // Pass events to visualizer\n * viz.handleEvent(event);\n * }\n * });\n *\n * await workflow(async (step) => {\n * const user = await step(fetchUser(id));\n *\n * // Track a decision point\n * const decision = trackDecision('user-role-check', {\n * condition: 'user.role === \"admin\"',\n * value: user.role\n * });\n *\n * if (user.role === 'admin') {\n * decision.takeBranch('admin', true);\n * await step(processAdminAction(user));\n * } else {\n * decision.takeBranch('user', false);\n * await step(processUserAction(user));\n * }\n *\n * decision.end();\n * });\n * ```\n */\n\nimport type {\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n} from \"./types\";\n\n// =============================================================================\n// Decision Tracker\n// =============================================================================\n\n/**\n * Options for creating a decision tracker.\n */\nexport interface DecisionTrackerOptions {\n /** Condition being evaluated (e.g., \"user.role === 'admin'\") */\n condition?: string;\n /** Value being evaluated */\n value?: unknown;\n /** Name/label for this decision point */\n name?: string;\n /** Workflow ID (auto-generated if not provided) */\n workflowId?: string;\n /** Event emitter function */\n emit?: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;\n}\n\n/**\n * Track a decision point in your workflow.\n *\n * Use this to annotate conditional logic (if/switch) so it appears\n * in workflow visualizations.\n *\n * @param decisionId - Unique identifier for this decision\n * @param options - Decision tracking options\n * @returns A tracker object with methods to track branches\n *\n * @example\n * ```typescript\n * const decision = trackDecision('check-role', {\n * condition: 'user.role === \"admin\"',\n * value: user.role,\n * emit: (event) => viz.handleDecisionEvent(event)\n * });\n *\n * if (user.role === 'admin') {\n * decision.takeBranch('admin', true);\n * // ... admin logic\n * } else {\n * decision.takeBranch('user', false);\n * // ... user logic\n * }\n *\n * decision.end();\n * ```\n */\nexport function trackDecision(\n decisionId: string,\n options: DecisionTrackerOptions = {}\n): DecisionTracker {\n const {\n condition,\n value,\n name,\n workflowId = crypto.randomUUID(),\n emit,\n } = options;\n\n const startTs = Date.now();\n let branchTaken: string | boolean | undefined;\n const branches: Array<{ label: string; condition?: string; taken: boolean }> = [];\n\n function takeBranch(\n label: string,\n taken: boolean,\n branchCondition?: string\n ): void {\n branches.push({ label, condition: branchCondition, taken });\n if (taken) {\n branchTaken = label;\n }\n\n emit?.({\n type: \"decision_branch\",\n workflowId,\n decisionId,\n branchLabel: label,\n condition: branchCondition,\n taken,\n ts: Date.now(),\n });\n }\n\n function end(): void {\n const durationMs = Date.now() - startTs;\n emit?.({\n type: \"decision_end\",\n workflowId,\n decisionId,\n branchTaken,\n ts: Date.now(),\n durationMs,\n });\n }\n\n // Emit start event immediately\n emit?.({\n type: \"decision_start\",\n workflowId,\n decisionId,\n condition,\n decisionValue: value,\n name,\n ts: startTs,\n });\n\n return {\n takeBranch,\n end,\n getBranchTaken: () => branchTaken,\n getBranches: () => [...branches],\n };\n}\n\n/**\n * Decision tracker instance.\n */\nexport interface DecisionTracker {\n /**\n * Mark that a branch was taken or skipped.\n * @param label - Label for this branch (e.g., \"if\", \"else\", \"case 'admin'\")\n * @param taken - Whether this branch was executed\n * @param branchCondition - Optional condition for this specific branch\n */\n takeBranch(label: string, taken: boolean, branchCondition?: string): void;\n\n /**\n * End the decision tracking.\n * Call this after all branches have been evaluated.\n */\n end(): void;\n\n /**\n * Get which branch was taken.\n */\n getBranchTaken(): string | boolean | undefined;\n\n /**\n * Get all branches (taken and skipped).\n */\n getBranches(): Array<{ label: string; condition?: string; taken: boolean }>;\n}\n\n// =============================================================================\n// Convenience Helpers\n// =============================================================================\n\n/**\n * Track a simple if/else decision.\n *\n * @example\n * ```typescript\n * const decision = trackIf('check-admin', user.role === 'admin', {\n * condition: 'user.role === \"admin\"',\n * value: user.role,\n * emit: (e) => viz.handleDecisionEvent(e)\n * });\n *\n * if (decision.condition) {\n * decision.then();\n * // admin logic\n * } else {\n * decision.else();\n * // user logic\n * }\n *\n * decision.end();\n * ```\n */\nexport function trackIf(\n decisionId: string,\n condition: boolean,\n options: Omit<DecisionTrackerOptions, \"value\"> & { value?: unknown } = {}\n): IfTracker {\n const tracker = trackDecision(decisionId, {\n ...options,\n condition: options.condition ?? String(condition),\n value: options.value ?? condition,\n });\n\n return {\n ...tracker,\n condition,\n then: () => {\n tracker.takeBranch(\"if\", true);\n },\n else: () => {\n // Mark else branch as taken (true) when the else block executes\n tracker.takeBranch(\"else\", true);\n },\n };\n}\n\n/**\n * If tracker with convenience methods.\n */\nexport interface IfTracker extends DecisionTracker {\n /** The condition value */\n condition: boolean;\n /** Mark the \"if\" branch as taken */\n then(): void;\n /** Mark the \"else\" branch as taken */\n else(): void;\n}\n\n/**\n * Track a switch statement decision.\n *\n * @example\n * ```typescript\n * const decision = trackSwitch('process-type', user.type, {\n * emit: (e) => viz.handleDecisionEvent(e)\n * });\n *\n * switch (user.type) {\n * case 'admin':\n * decision.case('admin', true);\n * // admin logic\n * break;\n * case 'user':\n * decision.case('user', true);\n * // user logic\n * break;\n * default:\n * decision.default(true);\n * // default logic\n * }\n *\n * decision.end();\n * ```\n */\nexport function trackSwitch(\n decisionId: string,\n value: unknown,\n options: Omit<DecisionTrackerOptions, \"value\"> = {}\n): SwitchTracker {\n const tracker = trackDecision(decisionId, {\n ...options,\n condition: options.condition ?? `switch(${String(value)})`,\n value,\n });\n\n return {\n ...tracker,\n value,\n case: (caseValue: string | number, taken: boolean) => {\n tracker.takeBranch(`case '${caseValue}'`, taken, `value === '${caseValue}'`);\n },\n default: (taken: boolean) => {\n tracker.takeBranch(\"default\", taken);\n },\n };\n}\n\n/**\n * Switch tracker with convenience methods.\n */\nexport interface SwitchTracker extends DecisionTracker {\n /** The value being switched on */\n value: unknown;\n /** Mark a case branch */\n case(caseValue: string | number, taken: boolean): void;\n /** Mark the default branch */\n default(taken: boolean): void;\n}\n"],"mappings":"mbAAA,IAAAA,GAAA,G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V,EAAM,KAAK,WAAW,WAAW,EACnCW,EAAI,oBAAoBX,CAAoE,EAE5FW,EAAI,YAAYX,CAA+B,EAInD,OAAOW,EAAI,OAAO,CACpB,CA2BO,SAASC,GAAqB/B,EAA6B,CAAC,EAAG,CACpE,IAAM6B,EAA6B,CAAC,EAEpC,MAAO,CAEL,YAAcV,GAAkC,CAC9CU,EAAO,KAAKV,CAAK,CACnB,EAGA,oBAAsBA,GAAuE,CAC3FU,EAAO,KAAKV,CAAK,CACnB,EAGA,UAAW,IAAM,CAAC,GAAGU,CAAM,EAG3B,kBAAmB,IAAMA,EAAO,OAAQG,GACtC,CAACA,EAAE,KAAK,WAAW,WAAW,CAChC,EAGA,kBAAmB,IAAMH,EAAO,OAAQG,GACtCA,EAAE,KAAK,WAAW,WAAW,CAC/B,EAGA,MAAO,IAAM,CACXH,EAAO,OAAS,CAClB,EAGA,UAAW,IAAM,CACf,IAAMC,EAAM/B,EAAiBC,CAAO,EACpC,QAAWmB,KAASU,EACdV,EAAM,KAAK,WAAW,WAAW,EACnCW,EAAI,oBAAoBX,CAAoE,EAE5FW,EAAI,YAAYX,CAA+B,EAGnD,OAAOW,EAAI,OAAO,CACpB,EAGA,YAAcL,GAAyB,CACrC,IAAMK,EAAM/B,EAAiBC,CAAO,EACpC,QAAWmB,KAASU,EACdV,EAAM,KAAK,WAAW,WAAW,EACnCW,EAAI,oBAAoBX,CAAoE,EAE5FW,EAAI,YAAYX,CAA+B,EAGnD,OAAOW,EAAI,SAASL,CAAM,CAC5B,CACF,CACF","names":["visualize_exports","__export","asciiRenderer","createEventCollector","createIRBuilder","createLiveVisualizer","createParallelDetector","createVisualizer","defaultColorScheme","detectParallelGroups","hasChildren","isDecisionNode","isParallelNode","isRaceNode","isSequenceNode","isStepNode","mermaidRenderer","trackDecision","trackIf","trackSwitch","visualizeEvents","__toCommonJS","formatDuration","ms","minutes","seconds","generateId","hasRealScopeNodes","nodes","node","detectParallelGroups","options","minOverlapMs","maxGapMs","stepsWithTiming","nonStepNodes","i","a","b","groups","currentGroup","step","groupStart","s","groupEnd","startedTogether","hasTrueOverlap","overlapDuration","groupedNodes","group","position","children","startTs","endTs","parallelNode","deriveGroupState","originalIndex","g","c","createParallelDetector","createIRBuilder","options","detectParallel","parallelDetection","workflowId","workflowStartTs","workflowState","workflowError","workflowDurationMs","activeSteps","scopeStack","decisionStack","currentNodes","createdAt","lastUpdatedAt","getStepId","event","generateId","addNode","node","decision","branch","firstBranch","handleEvent","id","active","handleScopeEvent","scope","deriveState","handleDecisionEvent","branchKey","existing","branches","b","children","c","getCurrentNodes","nodes","getIR","detectParallelGroups","reset","isStepNode","node","isSequenceNode","isParallelNode","isRaceNode","isDecisionNode","hasChildren","RESET","BOLD","DIM","FG_RED","FG_GREEN","FG_YELLOW","FG_BLUE","FG_GRAY","FG_WHITE","colorize","text","color","bold","dim","defaultColorScheme","getStateSymbol","state","getColoredSymbol","colors","symbol","colorByState","stripAnsi","str","BOX","padEnd","str","width","visibleLen","stripAnsi","padding","horizontalLine","title","titleText","remainingWidth","leftPad","rightPad","asciiRenderer","ir","options","colors","defaultColorScheme","innerWidth","lines","workflowName","headerTitle","bold","childLines","renderNodes","line","status","footer","colorByState","formatDuration","nodes","depth","node","isStepNode","renderStepNode","isParallelNode","renderParallelNode","isRaceNode","renderRaceNode","isDecisionNode","renderDecisionNode","symbol","getColoredSymbol","name","nameColored","dim","inputStr","outputStr","timeoutInfo","indent","mode","i","child","prefix","nestedLines","winnerSuffix","condition","decisionValue","branchTaken","branch","branchSymbol","branchColor","branchLabel","colorize","branchCondition","getStyleDefinitions","nodeCounter","generateNodeId","prefix","resetNodeCounter","escapeMermaidText","text","escapeSubgraphName","mermaidRenderer","ir","options","lines","startId","prevNodeId","child","result","renderNode","endId","endIcon","endLabel","endShape","endClass","getStyleDefinitions","node","isStepNode","renderStepNode","isParallelNode","renderParallelNode","isRaceNode","renderRaceNode","isDecisionNode","renderDecisionNode","id","label","timing","formatDuration","stateIcon","ioInfo","inputStr","outputStr","retryInfo","timeoutStr","escapedLabel","stateClass","shape","subgraphId","forkId","joinId","name","modeLabel","note","childExitIds","exitId","winnerExitId","isWinner","decisionId","condition","decisionValue","decisionLabel","branchExitIds","takenBranchExitId","branch","branchId","branchLabelText","branchLabel","branchClass","edgeLabel","prevId","ANSI","n","createLiveVisualizer","options","workflowName","detectParallel","showTimings","showKeys","customColors","stream","updateInterval","builder","createIRBuilder","renderer","asciiRenderer","renderOptions","defaultColorScheme","isRunning","lastOutput","lastLineCount","throttleTimeout","pendingUpdate","write","text","redraw","ir","getIR","output","scheduleRedraw","handleEvent","event","handleScopeEvent","handleDecisionEvent","render","start","stop","refresh","reset","trackDecision","decisionId","options","condition","value","name","workflowId","emit","startTs","branchTaken","branches","takeBranch","label","taken","branchCondition","end","durationMs","trackIf","tracker","trackSwitch","caseValue","createVisualizer","options","workflowName","detectParallel","showTimings","showKeys","customColors","builder","createIRBuilder","updateCallbacks","ascii","asciiRenderer","mermaid","mermaidRenderer","renderOptions","defaultColorScheme","notifyUpdate","ir","callback","handleEvent","event","handleScopeEvent","handleDecisionEvent","getIR","render","renderAs","format","reset","onUpdate","visualizeEvents","events","viz","createEventCollector","e"]}
1
+ {"version":3,"sources":["../src/visualize/index.ts","../src/visualize/utils/timing.ts","../src/visualize/parallel-detector.ts","../src/visualize/ir-builder.ts","../src/visualize/types.ts","../src/visualize/renderers/colors.ts","../src/visualize/renderers/ascii.ts","../src/visualize/performance-analyzer.ts","../src/visualize/renderers/mermaid.ts","../src/visualize/renderers/html-styles.ts","../src/visualize/renderers/html-client.ts","../src/visualize/renderers/html.ts","../src/visualize/live-visualizer.ts","../src/visualize/decision-tracker.ts","../src/visualize/time-travel.ts","../src/visualize/dev-server.ts"],"sourcesContent":["/**\n * Workflow Visualization Module\n *\n * Provides tools for visualizing workflow execution with color-coded\n * step states and support for parallel/race operations.\n *\n * @example\n * ```typescript\n * import { createVisualizer } from '@jagreehal/workflow/visualize';\n *\n * const viz = createVisualizer({ workflowName: 'checkout' });\n * const workflow = createWorkflow(deps, { onEvent: viz.handleEvent });\n *\n * await workflow(async (step) => {\n * await step(() => validateCart(cart), 'Validate cart');\n * await step(() => processPayment(payment), 'Process payment');\n * });\n *\n * console.log(viz.render());\n * ```\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n OutputFormat,\n RenderOptions,\n ScopeEndEvent,\n ScopeStartEvent,\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n VisualizerOptions,\n WorkflowIR,\n} from \"./types\";\nimport { createIRBuilder } from \"./ir-builder\";\nimport { asciiRenderer, mermaidRenderer, defaultColorScheme } from \"./renderers\";\n\n// =============================================================================\n// Re-exports\n// =============================================================================\n\nexport * from \"./types\";\nexport { createIRBuilder, type IRBuilderOptions } from \"./ir-builder\";\nexport { asciiRenderer, mermaidRenderer, defaultColorScheme } from \"./renderers\";\nexport { htmlRenderer, renderToHTML } from \"./renderers/html\";\nexport { detectParallelGroups, createParallelDetector, type ParallelDetectorOptions } from \"./parallel-detector\";\nexport { createLiveVisualizer, type LiveVisualizer } from \"./live-visualizer\";\nexport { trackDecision, trackIf, trackSwitch, type DecisionTracker, type IfTracker, type SwitchTracker } from \"./decision-tracker\";\n\n// Time-travel debugging\nexport {\n createTimeTravelController,\n type TimeTravelController,\n type TimeTravelOptions,\n} from \"./time-travel\";\n\n// Performance analysis\nexport {\n createPerformanceAnalyzer,\n getHeatLevel,\n type PerformanceAnalyzer,\n type WorkflowRun,\n} from \"./performance-analyzer\";\n\n// Dev server (WebSocket-based live visualization)\n// Note: ws is an optional peer dependency\nexport {\n createDevServer,\n type DevServer,\n type DevServerOptions,\n} from \"./dev-server\";\n\n// =============================================================================\n// Visualizer Interface\n// =============================================================================\n\n/**\n * Workflow visualizer that processes events and renders output.\n */\nexport interface WorkflowVisualizer {\n /** Process a workflow event */\n handleEvent: (event: WorkflowEvent<unknown>) => void;\n\n /** Process a scope event (parallel/race) */\n handleScopeEvent: (event: ScopeStartEvent | ScopeEndEvent) => void;\n\n /** Process a decision event (conditional branches) */\n handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;\n\n /** Get current IR state */\n getIR: () => WorkflowIR;\n\n /** Render current state using the default renderer */\n render: () => string;\n\n /** Render to a specific format */\n renderAs: (format: OutputFormat) => string;\n\n /** Reset state for a new workflow */\n reset: () => void;\n\n /** Subscribe to IR updates (for live visualization) */\n onUpdate: (callback: (ir: WorkflowIR) => void) => () => void;\n}\n\n// =============================================================================\n// Create Visualizer\n// =============================================================================\n\n/**\n * Create a workflow visualizer.\n *\n * @example\n * ```typescript\n * const viz = createVisualizer({ workflowName: 'my-workflow' });\n *\n * const workflow = createWorkflow(deps, {\n * onEvent: viz.handleEvent,\n * });\n *\n * await workflow(async (step) => { ... });\n *\n * console.log(viz.render());\n * ```\n */\nexport function createVisualizer(\n options: VisualizerOptions = {}\n): WorkflowVisualizer {\n const {\n workflowName,\n detectParallel = true,\n showTimings = true,\n showKeys = false,\n colors: customColors,\n } = options;\n\n const builder = createIRBuilder({ detectParallel });\n const updateCallbacks: Set<(ir: WorkflowIR) => void> = new Set();\n\n // Renderers\n const ascii = asciiRenderer();\n const mermaid = mermaidRenderer();\n\n // Build render options\n const renderOptions: RenderOptions = {\n showTimings,\n showKeys,\n terminalWidth: process.stdout?.columns ?? 80,\n colors: { ...defaultColorScheme, ...customColors },\n };\n\n function notifyUpdate(): void {\n if (updateCallbacks.size > 0) {\n const ir = builder.getIR();\n for (const callback of updateCallbacks) {\n callback(ir);\n }\n }\n }\n\n function handleEvent(event: WorkflowEvent<unknown>): void {\n // Route scope events to handleScopeEvent for proper IR building\n if (event.type === \"scope_start\" || event.type === \"scope_end\") {\n handleScopeEvent(event as ScopeStartEvent | ScopeEndEvent);\n return;\n }\n\n builder.handleEvent(event);\n\n // Set workflow name if provided\n if (event.type === \"workflow_start\" && workflowName) {\n // Note: We'd need to extend the builder to support setting name\n // For now, the name is passed in render options\n }\n\n notifyUpdate();\n }\n\n function handleScopeEvent(event: ScopeStartEvent | ScopeEndEvent): void {\n builder.handleScopeEvent(event);\n notifyUpdate();\n }\n\n function handleDecisionEvent(\n event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent\n ): void {\n builder.handleDecisionEvent(event);\n notifyUpdate();\n }\n\n function getIR(): WorkflowIR {\n const ir = builder.getIR();\n // Apply workflow name if provided\n if (workflowName && !ir.root.name) {\n ir.root.name = workflowName;\n }\n return ir;\n }\n\n function render(): string {\n const ir = getIR();\n return ascii.render(ir, renderOptions);\n }\n\n function renderAs(format: OutputFormat): string {\n const ir = getIR();\n\n switch (format) {\n case \"ascii\":\n return ascii.render(ir, renderOptions);\n\n case \"mermaid\":\n return mermaid.render(ir, renderOptions);\n\n case \"json\":\n return JSON.stringify(ir, null, 2);\n\n default:\n throw new Error(`Unknown format: ${format}`);\n }\n }\n\n function reset(): void {\n builder.reset();\n notifyUpdate();\n }\n\n function onUpdate(callback: (ir: WorkflowIR) => void): () => void {\n updateCallbacks.add(callback);\n return () => updateCallbacks.delete(callback);\n }\n\n return {\n handleEvent,\n handleScopeEvent,\n handleDecisionEvent,\n getIR,\n render,\n renderAs,\n reset,\n onUpdate,\n };\n}\n\n// =============================================================================\n// Convenience Functions\n// =============================================================================\n\n/**\n * Union type for all collectable/visualizable events (workflow + decision).\n */\nexport type CollectableEvent =\n | WorkflowEvent<unknown>\n | DecisionStartEvent\n | DecisionBranchEvent\n | DecisionEndEvent;\n\n/**\n * Visualize collected events (post-execution).\n *\n * Supports both workflow events (from onEvent) and decision events\n * (from trackDecision/trackIf/trackSwitch).\n *\n * @example\n * ```typescript\n * const events: CollectableEvent[] = [];\n * const workflow = createWorkflow(deps, {\n * onEvent: (e) => events.push(e),\n * });\n *\n * await workflow(async (step) => {\n * const decision = trackIf('check', condition, {\n * emit: (e) => events.push(e),\n * });\n * // ...\n * });\n *\n * console.log(visualizeEvents(events));\n * ```\n */\nexport function visualizeEvents(\n events: CollectableEvent[],\n options: VisualizerOptions = {}\n): string {\n const viz = createVisualizer(options);\n\n for (const event of events) {\n if (event.type.startsWith(\"decision_\")) {\n viz.handleDecisionEvent(event as DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent);\n } else {\n viz.handleEvent(event as WorkflowEvent<unknown>);\n }\n }\n\n return viz.render();\n}\n\n/**\n * Create an event collector for later visualization.\n *\n * Supports both workflow events (from onEvent) and decision events\n * (from trackDecision/trackIf/trackSwitch).\n *\n * @example\n * ```typescript\n * const collector = createEventCollector();\n *\n * const workflow = createWorkflow(deps, {\n * onEvent: collector.handleEvent,\n * });\n *\n * await workflow(async (step) => {\n * // Decision events can also be collected\n * const decision = trackIf('check', condition, {\n * emit: collector.handleDecisionEvent,\n * });\n * // ...\n * });\n *\n * console.log(collector.visualize());\n * ```\n */\nexport function createEventCollector(options: VisualizerOptions = {}) {\n const events: CollectableEvent[] = [];\n\n return {\n /** Handle a workflow event */\n handleEvent: (event: WorkflowEvent<unknown>) => {\n events.push(event);\n },\n\n /** Handle a decision event */\n handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => {\n events.push(event);\n },\n\n /** Get all collected events */\n getEvents: () => [...events],\n\n /** Get workflow events only */\n getWorkflowEvents: () => events.filter((e): e is WorkflowEvent<unknown> =>\n !e.type.startsWith(\"decision_\")\n ),\n\n /** Get decision events only */\n getDecisionEvents: () => events.filter((e): e is DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent =>\n e.type.startsWith(\"decision_\")\n ),\n\n /** Clear collected events */\n clear: () => {\n events.length = 0;\n },\n\n /** Visualize collected events */\n visualize: () => {\n const viz = createVisualizer(options);\n for (const event of events) {\n if (event.type.startsWith(\"decision_\")) {\n viz.handleDecisionEvent(event as DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent);\n } else {\n viz.handleEvent(event as WorkflowEvent<unknown>);\n }\n }\n return viz.render();\n },\n\n /** Visualize in a specific format */\n visualizeAs: (format: OutputFormat) => {\n const viz = createVisualizer(options);\n for (const event of events) {\n if (event.type.startsWith(\"decision_\")) {\n viz.handleDecisionEvent(event as DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent);\n } else {\n viz.handleEvent(event as WorkflowEvent<unknown>);\n }\n }\n return viz.renderAs(format);\n },\n };\n}\n\n","/**\n * Timing utilities for workflow visualization.\n */\n\n/**\n * Format duration in milliseconds to a human-readable string.\n *\n * @example\n * formatDuration(23) // \"23ms\"\n * formatDuration(1500) // \"1.5s\"\n * formatDuration(65000) // \"1m 5s\"\n */\nexport function formatDuration(ms: number): string {\n if (ms < 1000) {\n return `${Math.round(ms)}ms`;\n }\n\n if (ms < 60000) {\n const seconds = ms / 1000;\n // Show one decimal for seconds\n return `${seconds.toFixed(1).replace(/\\.0$/, \"\")}s`;\n }\n\n const minutes = Math.floor(ms / 60000);\n const seconds = Math.round((ms % 60000) / 1000);\n\n if (seconds === 0) {\n return `${minutes}m`;\n }\n\n return `${minutes}m ${seconds}s`;\n}\n\n/**\n * Generate a unique ID for nodes.\n */\nexport function generateId(): string {\n return `node_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;\n}\n","/**\n * Parallel Detection - Heuristic detection of parallel execution from timing.\n *\n * When steps overlap in time (one starts before another ends), they are\n * likely running in parallel. This module detects such patterns and\n * groups overlapping steps into ParallelNode structures.\n */\n\nimport type { FlowNode, ParallelNode, StepNode } from \"./types\";\n\n/**\n * Options for parallel detection.\n */\nexport interface ParallelDetectorOptions {\n /**\n * Minimum overlap in milliseconds to consider steps parallel.\n * Default: 0 (any overlap counts)\n */\n minOverlapMs?: number;\n\n /**\n * Maximum gap in milliseconds to still consider steps as part of same parallel group.\n * Default: 5 (steps starting within 5ms are grouped)\n */\n maxGapMs?: number;\n}\n\n/**\n * Step timing information for overlap detection.\n */\ninterface StepTiming {\n node: StepNode;\n startTs: number;\n endTs: number;\n}\n\n/**\n * Check if nodes contain real scope nodes (from scope_start/scope_end events).\n * When real scope nodes exist, heuristic detection should be skipped to avoid\n * duplicating or conflicting with the explicit structure.\n */\nfunction hasRealScopeNodes(nodes: FlowNode[]): boolean {\n for (const node of nodes) {\n // Real scope nodes are parallel/race/sequence that came from scope events\n // (not from heuristic detection, which uses ids starting with \"detected_\")\n if (\n (node.type === \"parallel\" || node.type === \"race\" || node.type === \"sequence\") &&\n !node.id.startsWith(\"detected_\")\n ) {\n return true;\n }\n // Also check for decision nodes if present\n if (\"decisionId\" in node) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Group overlapping steps into parallel nodes.\n *\n * Algorithm:\n * 1. Sort steps by start time\n * 2. For each step, check if it overlaps with existing parallel groups\n * 3. If it overlaps, add to group; otherwise start new sequence\n * 4. Merge overlapping groups when step bridges them\n *\n * Note: If real scope nodes (from scope_start/scope_end) are present,\n * heuristic detection is skipped to avoid conflicts.\n */\nexport function detectParallelGroups(\n nodes: FlowNode[],\n options: ParallelDetectorOptions = {}\n): FlowNode[] {\n // If real scope nodes exist, skip heuristic detection\n // The explicit scope events provide accurate structure\n if (hasRealScopeNodes(nodes)) {\n return nodes;\n }\n\n const { minOverlapMs = 0, maxGapMs = 5 } = options;\n\n // Extract step nodes with timing info, preserving indices for position restoration\n const stepsWithTiming: (StepTiming & { originalIndex: number })[] = [];\n const nonStepNodes: { node: FlowNode; originalIndex: number }[] = [];\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n if (node.type === \"step\" && node.startTs !== undefined) {\n stepsWithTiming.push({\n node,\n startTs: node.startTs,\n endTs: node.endTs ?? node.startTs + (node.durationMs ?? 0),\n originalIndex: i,\n });\n } else {\n // Keep non-step nodes with their original position\n nonStepNodes.push({ node, originalIndex: i });\n }\n }\n\n if (stepsWithTiming.length <= 1) {\n return nodes; // Nothing to group\n }\n\n // Sort by start time\n stepsWithTiming.sort((a, b) => a.startTs - b.startTs);\n\n // Group overlapping steps\n type StepTimingWithIndex = StepTiming & { originalIndex: number };\n const groups: StepTimingWithIndex[][] = [];\n let currentGroup: StepTimingWithIndex[] = [stepsWithTiming[0]];\n\n for (let i = 1; i < stepsWithTiming.length; i++) {\n const step = stepsWithTiming[i];\n const groupStart = Math.min(...currentGroup.map((s) => s.startTs));\n const groupEnd = Math.max(...currentGroup.map((s) => s.endTs));\n\n // Two ways steps can be parallel:\n // 1. They started together (within maxGapMs) - handles timing jitter\n // 2. They genuinely overlap (step starts before group ends)\n const startedTogether = step.startTs <= groupStart + maxGapMs;\n const hasTrueOverlap = step.startTs < groupEnd;\n\n if (!startedTogether && !hasTrueOverlap) {\n // Sequential: step started after group ended AND not with the group\n groups.push(currentGroup);\n currentGroup = [step];\n continue;\n }\n\n // Check minOverlapMs threshold for overlap duration\n // For steps that started together, overlap is measured from step start to group end\n // For steps with true overlap, it's from step start to min(step end, group end)\n const overlapDuration = hasTrueOverlap\n ? Math.min(step.endTs, groupEnd) - step.startTs\n : 0;\n\n // Started together bypasses minOverlapMs (they're parallel by definition)\n // True overlap must meet the minOverlapMs threshold\n if (startedTogether || overlapDuration >= minOverlapMs) {\n currentGroup.push(step);\n } else {\n // Overlap too small - treat as sequential\n groups.push(currentGroup);\n currentGroup = [step];\n }\n }\n groups.push(currentGroup);\n\n // Convert groups to nodes with position tracking\n const groupedNodes: { node: FlowNode; position: number }[] = [];\n\n for (const group of groups) {\n // Use the minimum original index as the position for the group\n const position = Math.min(...group.map((s) => s.originalIndex));\n\n if (group.length === 1) {\n // Single step - no parallel grouping needed\n groupedNodes.push({ node: group[0].node, position });\n } else {\n // Multiple overlapping steps - create parallel node\n const children = group.map((s) => s.node);\n const startTs = Math.min(...group.map((s) => s.startTs));\n const endTs = Math.max(...group.map((s) => s.endTs));\n\n const parallelNode: ParallelNode = {\n type: \"parallel\",\n id: `detected_parallel_${startTs}`,\n name: `${children.length} parallel steps`,\n state: deriveGroupState(children),\n mode: \"all\",\n children,\n startTs,\n endTs,\n durationMs: endTs - startTs,\n };\n\n groupedNodes.push({ node: parallelNode, position });\n }\n }\n\n // Add non-step nodes with their original positions\n for (const { node, originalIndex } of nonStepNodes) {\n groupedNodes.push({ node, position: originalIndex });\n }\n\n // Sort by original position to preserve ordering\n groupedNodes.sort((a, b) => a.position - b.position);\n\n return groupedNodes.map((g) => g.node);\n}\n\n/**\n * Derive the state of a group from its children.\n */\nfunction deriveGroupState(\n children: FlowNode[]\n): \"pending\" | \"running\" | \"success\" | \"error\" | \"aborted\" | \"cached\" {\n const hasError = children.some((c) => c.state === \"error\");\n if (hasError) return \"error\";\n\n const hasRunning = children.some((c) => c.state === \"running\");\n if (hasRunning) return \"running\";\n\n const hasPending = children.some((c) => c.state === \"pending\");\n if (hasPending) return \"pending\";\n\n const allSuccess = children.every(\n (c) => c.state === \"success\" || c.state === \"cached\"\n );\n if (allSuccess) return \"success\";\n\n return \"success\";\n}\n\n/**\n * Create a parallel detector that processes nodes.\n */\nexport function createParallelDetector(options: ParallelDetectorOptions = {}) {\n return {\n /**\n * Process nodes and group overlapping ones into parallel nodes.\n */\n detect: (nodes: FlowNode[]) => detectParallelGroups(nodes, options),\n };\n}\n","/**\n * IR Builder - Converts workflow events to Intermediate Representation.\n *\n * The builder maintains state as events arrive, constructing a tree\n * representation of the workflow execution that can be rendered.\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n FlowNode,\n ScopeEndEvent,\n ScopeStartEvent,\n ScopeType,\n StepNode,\n StepState,\n WorkflowIR,\n WorkflowNode,\n ParallelNode,\n RaceNode,\n DecisionNode,\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n DecisionBranch,\n IRSnapshot,\n ActiveStepSnapshot,\n WorkflowHooks,\n HookExecution,\n} from \"./types\";\nimport { generateId } from \"./utils/timing\";\nimport { detectParallelGroups, type ParallelDetectorOptions } from \"./parallel-detector\";\n\n// =============================================================================\n// Builder Options\n// =============================================================================\n\n/**\n * Options for the IR builder.\n */\nexport interface IRBuilderOptions {\n /**\n * Enable heuristic parallel detection based on timing.\n * When true, overlapping steps are grouped into ParallelNodes.\n * Default: true\n */\n detectParallel?: boolean;\n\n /**\n * Options for parallel detection.\n */\n parallelDetection?: ParallelDetectorOptions;\n\n /**\n * Enable snapshot recording for time-travel debugging.\n * When true, the builder captures IR state after each event.\n * Default: false\n */\n enableSnapshots?: boolean;\n\n /**\n * Maximum number of snapshots to keep (ring buffer behavior).\n * When exceeded, oldest snapshots are discarded.\n * Default: 1000\n */\n maxSnapshots?: number;\n}\n\n// =============================================================================\n// Builder State\n// =============================================================================\n\ninterface ActiveStep {\n id: string;\n name?: string;\n key?: string;\n startTs: number;\n retryCount: number;\n timedOut: boolean;\n timeoutMs?: number;\n}\n\ninterface ActiveScope {\n id: string;\n name?: string;\n type: ScopeType;\n startTs: number;\n children: FlowNode[];\n}\n\ninterface ActiveDecision {\n id: string;\n name?: string;\n condition?: string;\n decisionValue?: unknown;\n startTs: number;\n branches: Map<string, DecisionBranch>;\n branchTaken?: string | boolean;\n}\n\n// =============================================================================\n// IR Builder\n// =============================================================================\n\n/**\n * Creates an IR builder that processes workflow events.\n */\nexport function createIRBuilder(options: IRBuilderOptions = {}) {\n const {\n detectParallel = true,\n parallelDetection,\n enableSnapshots = false,\n maxSnapshots = 1000,\n } = options;\n\n // Current workflow state\n let workflowId: string | undefined;\n let workflowStartTs: number | undefined;\n let workflowState: StepState = \"pending\";\n let workflowError: unknown;\n let workflowDurationMs: number | undefined;\n\n // Active steps (currently running)\n const activeSteps = new Map<string, ActiveStep>();\n\n // Active scopes (parallel/race blocks)\n const scopeStack: ActiveScope[] = [];\n\n // Active decisions (conditional branches)\n const decisionStack: ActiveDecision[] = [];\n\n // Completed nodes at the current scope level\n let currentNodes: FlowNode[] = [];\n\n // Metadata\n let createdAt = Date.now();\n let lastUpdatedAt = createdAt;\n\n // Hook executions\n let hookState: WorkflowHooks = {\n onAfterStep: new Map(),\n };\n\n // Snapshot state for time-travel debugging\n const snapshots: IRSnapshot[] = [];\n let eventIndex = 0;\n\n /**\n * Get the step ID from an event.\n * Uses stepId if available (new events), then falls back to stepKey or name,\n * and finally generates a random ID for backwards compatibility.\n */\n function getStepId(event: { stepId?: string; stepKey?: string; name?: string }): string {\n return event.stepId ?? event.stepKey ?? event.name ?? generateId();\n }\n\n /**\n * Add a completed node to the current scope or decision branch.\n */\n function addNode(node: FlowNode): void {\n // If we're in a decision, add to the taken branch\n if (decisionStack.length > 0) {\n const decision = decisionStack[decisionStack.length - 1];\n // Find the taken branch\n for (const branch of decision.branches.values()) {\n if (branch.taken) {\n branch.children.push(node);\n lastUpdatedAt = Date.now();\n return;\n }\n }\n // If no branch is marked as taken yet, add to the first branch\n // (this handles cases where steps execute before branch is marked)\n const firstBranch = Array.from(decision.branches.values())[0];\n if (firstBranch) {\n firstBranch.children.push(node);\n lastUpdatedAt = Date.now();\n return;\n }\n }\n\n // If we're in a scope, add to the scope\n if (scopeStack.length > 0) {\n // Add to the innermost scope\n scopeStack[scopeStack.length - 1].children.push(node);\n } else {\n // Add to the root level\n currentNodes.push(node);\n }\n lastUpdatedAt = Date.now();\n }\n\n /**\n * Capture a snapshot of the current IR state (for time-travel debugging).\n * Called after each event is processed.\n */\n function captureSnapshot(event: WorkflowEvent<unknown>): void {\n if (!enableSnapshots) return;\n\n // Deep clone the current IR state\n const ir = getIR();\n\n // Clone active steps for debugging\n const activeStepsCopy = new Map<string, ActiveStepSnapshot>();\n for (const [id, step] of activeSteps) {\n activeStepsCopy.set(id, {\n id: step.id,\n name: step.name,\n key: step.key,\n startTs: step.startTs,\n retryCount: step.retryCount,\n timedOut: step.timedOut,\n timeoutMs: step.timeoutMs,\n });\n }\n\n const snapshot: IRSnapshot = {\n id: `snapshot_${eventIndex}`,\n eventIndex,\n event: structuredClone(event),\n ir: structuredClone(ir),\n timestamp: Date.now(),\n activeSteps: activeStepsCopy,\n };\n\n snapshots.push(snapshot);\n\n // Ring buffer: remove oldest if we exceed max\n if (snapshots.length > maxSnapshots) {\n snapshots.shift();\n }\n\n eventIndex++;\n }\n\n /**\n * Handle a workflow event and update the IR.\n */\n function handleEvent(event: WorkflowEvent<unknown>): void {\n switch (event.type) {\n case \"workflow_start\":\n workflowId = event.workflowId;\n workflowStartTs = event.ts;\n workflowState = \"running\";\n createdAt = Date.now();\n lastUpdatedAt = createdAt;\n // Note: Don't clear hookState here - shouldRun and onBeforeStart\n // events are emitted BEFORE workflow_start. Only clear onAfterStep\n // since those are per-step and should reset for each workflow run.\n hookState.onAfterStep = new Map();\n break;\n\n case \"workflow_success\":\n workflowState = \"success\";\n workflowDurationMs = event.durationMs;\n lastUpdatedAt = Date.now();\n break;\n\n case \"workflow_error\":\n workflowState = \"error\";\n workflowError = event.error;\n workflowDurationMs = event.durationMs;\n lastUpdatedAt = Date.now();\n break;\n\n case \"step_start\": {\n const id = getStepId(event);\n activeSteps.set(id, {\n id,\n name: event.name,\n key: event.stepKey,\n startTs: event.ts,\n retryCount: 0,\n timedOut: false,\n });\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"step_success\": {\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n const node: StepNode = {\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"success\",\n startTs: active.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n };\n addNode(node);\n activeSteps.delete(id);\n }\n break;\n }\n\n case \"step_error\": {\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n const node: StepNode = {\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"error\",\n startTs: active.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n error: event.error,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n };\n addNode(node);\n activeSteps.delete(id);\n }\n break;\n }\n\n case \"step_aborted\": {\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n const node: StepNode = {\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"aborted\",\n startTs: active.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n };\n addNode(node);\n activeSteps.delete(id);\n }\n break;\n }\n\n case \"step_cache_hit\": {\n const id = getStepId(event);\n const node: StepNode = {\n type: \"step\",\n id,\n name: event.name,\n key: event.stepKey,\n state: \"cached\",\n startTs: event.ts,\n endTs: event.ts,\n durationMs: 0,\n };\n addNode(node);\n break;\n }\n\n case \"step_cache_miss\":\n // Cache miss just means the step will execute normally\n // We'll get a step_start event next\n break;\n\n case \"step_complete\":\n // step_complete is for state persistence, not visualization\n // We already handled the step via step_success/step_error\n break;\n\n case \"step_timeout\": {\n // Timeout is an intermediate event - step may retry or will get step_error\n // Track timeout info on the active step\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n active.timedOut = true;\n active.timeoutMs = event.timeoutMs;\n }\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"step_retry\": {\n // Retry is an intermediate event - increment retry counter\n const id = getStepId(event);\n const active = activeSteps.get(id);\n if (active) {\n active.retryCount = (event.attempt ?? 1) - 1; // attempt is 1-indexed, retryCount is 0-indexed\n }\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"step_retries_exhausted\":\n // All retries exhausted - step_error will follow\n // The error state will be set by step_error handler\n lastUpdatedAt = Date.now();\n break;\n\n case \"step_skipped\": {\n const id = getStepId(event);\n const node: StepNode = {\n type: \"step\",\n id,\n name: event.name,\n key: event.stepKey,\n state: \"skipped\",\n startTs: event.ts,\n endTs: event.ts,\n durationMs: 0,\n };\n addNode(node);\n break;\n }\n\n // Hook events\n case \"hook_should_run\": {\n const hookExec: HookExecution = {\n type: \"shouldRun\",\n state: \"success\",\n ts: event.ts,\n durationMs: event.durationMs,\n context: {\n result: event.result,\n skipped: event.skipped,\n },\n };\n hookState.shouldRun = hookExec;\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"hook_should_run_error\": {\n const hookExec: HookExecution = {\n type: \"shouldRun\",\n state: \"error\",\n ts: event.ts,\n durationMs: event.durationMs,\n error: event.error,\n };\n hookState.shouldRun = hookExec;\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"hook_before_start\": {\n const hookExec: HookExecution = {\n type: \"onBeforeStart\",\n state: \"success\",\n ts: event.ts,\n durationMs: event.durationMs,\n context: {\n result: event.result,\n skipped: event.skipped,\n },\n };\n hookState.onBeforeStart = hookExec;\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"hook_before_start_error\": {\n const hookExec: HookExecution = {\n type: \"onBeforeStart\",\n state: \"error\",\n ts: event.ts,\n durationMs: event.durationMs,\n error: event.error,\n };\n hookState.onBeforeStart = hookExec;\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"hook_after_step\": {\n const hookExec: HookExecution = {\n type: \"onAfterStep\",\n state: \"success\",\n ts: event.ts,\n durationMs: event.durationMs,\n context: {\n stepKey: event.stepKey,\n },\n };\n hookState.onAfterStep.set(event.stepKey, hookExec);\n lastUpdatedAt = Date.now();\n break;\n }\n\n case \"hook_after_step_error\": {\n const hookExec: HookExecution = {\n type: \"onAfterStep\",\n state: \"error\",\n ts: event.ts,\n durationMs: event.durationMs,\n error: event.error,\n context: {\n stepKey: event.stepKey,\n },\n };\n hookState.onAfterStep.set(event.stepKey, hookExec);\n lastUpdatedAt = Date.now();\n break;\n }\n }\n\n // Capture snapshot after processing event (for time-travel)\n captureSnapshot(event);\n }\n\n /**\n * Handle a scope event (parallel/race start/end).\n */\n function handleScopeEvent(event: ScopeStartEvent | ScopeEndEvent): void {\n if (event.type === \"scope_start\") {\n scopeStack.push({\n id: event.scopeId,\n name: event.name,\n type: event.scopeType,\n startTs: event.ts,\n children: [],\n });\n lastUpdatedAt = Date.now();\n } else if (event.type === \"scope_end\") {\n const scope = scopeStack.pop();\n if (scope) {\n const node: ParallelNode | RaceNode =\n scope.type === \"race\"\n ? {\n type: \"race\",\n id: scope.id,\n name: scope.name,\n state: deriveState(scope.children),\n startTs: scope.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n children: scope.children,\n winnerId: event.winnerId,\n }\n : {\n type: \"parallel\",\n id: scope.id,\n name: scope.name,\n state: deriveState(scope.children),\n startTs: scope.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n children: scope.children,\n mode: scope.type === \"allSettled\" ? \"allSettled\" : \"all\",\n };\n addNode(node);\n }\n }\n }\n\n /**\n * Handle a decision event (conditional branch start/branch/end).\n */\n function handleDecisionEvent(\n event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent\n ): void {\n if (event.type === \"decision_start\") {\n decisionStack.push({\n id: event.decisionId,\n name: event.name,\n condition: event.condition,\n decisionValue: event.decisionValue,\n startTs: event.ts,\n branches: new Map(),\n });\n lastUpdatedAt = Date.now();\n } else if (event.type === \"decision_branch\") {\n const decision = decisionStack[decisionStack.length - 1];\n if (decision && decision.id === event.decisionId) {\n // Find or create branch\n const branchKey = event.branchLabel;\n const existing = decision.branches.get(branchKey);\n if (existing) {\n // Update existing branch\n existing.taken = event.taken;\n } else {\n // Create new branch\n decision.branches.set(branchKey, {\n label: event.branchLabel,\n condition: event.condition,\n taken: event.taken,\n children: [],\n });\n }\n lastUpdatedAt = Date.now();\n }\n } else if (event.type === \"decision_end\") {\n const decision = decisionStack.pop();\n if (decision && decision.id === event.decisionId) {\n // Convert branches map to array\n const branches: DecisionBranch[] = Array.from(decision.branches.values());\n\n const node: DecisionNode = {\n type: \"decision\",\n id: decision.id,\n name: decision.name,\n state: deriveState(\n branches.flatMap((b) => (b.taken ? b.children : []))\n ),\n startTs: decision.startTs,\n endTs: event.ts,\n durationMs: event.durationMs,\n condition: decision.condition,\n decisionValue: decision.decisionValue,\n branchTaken: event.branchTaken ?? decision.branchTaken,\n branches,\n };\n addNode(node);\n }\n }\n }\n\n /**\n * Derive the state of a parent node from its children.\n */\n function deriveState(children: FlowNode[]): StepState {\n if (children.length === 0) return \"success\";\n\n const hasError = children.some((c) => c.state === \"error\");\n if (hasError) return \"error\";\n\n const allSuccess = children.every(\n (c) => c.state === \"success\" || c.state === \"cached\"\n );\n if (allSuccess) return \"success\";\n\n const hasRunning = children.some((c) => c.state === \"running\");\n if (hasRunning) return \"running\";\n\n return \"pending\";\n }\n\n /**\n * Get the current nodes including any active (running) steps.\n */\n function getCurrentNodes(): FlowNode[] {\n const nodes = [...currentNodes];\n\n // Add active steps as running nodes\n for (const [, active] of activeSteps) {\n nodes.push({\n type: \"step\",\n id: active.id,\n name: active.name,\n key: active.key,\n state: \"running\",\n startTs: active.startTs,\n ...(active.retryCount > 0 && { retryCount: active.retryCount }),\n ...(active.timedOut && { timedOut: true, timeoutMs: active.timeoutMs }),\n });\n }\n\n return nodes;\n }\n\n /**\n * Build and return the current IR state.\n */\n function getIR(): WorkflowIR {\n let children = getCurrentNodes();\n\n // Apply parallel detection if enabled\n if (detectParallel) {\n children = detectParallelGroups(children, parallelDetection);\n }\n\n const root: WorkflowNode = {\n type: \"workflow\",\n id: workflowId ?? generateId(),\n workflowId: workflowId ?? \"unknown\",\n state: workflowState,\n startTs: workflowStartTs,\n durationMs: workflowDurationMs,\n children,\n error: workflowError,\n };\n\n // Include hooks if any have been recorded\n const hasHooks =\n hookState.shouldRun !== undefined ||\n hookState.onBeforeStart !== undefined ||\n hookState.onAfterStep.size > 0;\n\n return {\n root,\n metadata: {\n createdAt,\n lastUpdatedAt,\n },\n ...(hasHooks && { hooks: hookState }),\n };\n }\n\n /**\n * Reset the builder state.\n */\n function reset(): void {\n workflowId = undefined;\n workflowStartTs = undefined;\n workflowState = \"pending\";\n workflowError = undefined;\n workflowDurationMs = undefined;\n activeSteps.clear();\n scopeStack.length = 0;\n decisionStack.length = 0;\n currentNodes = [];\n createdAt = Date.now();\n lastUpdatedAt = createdAt;\n // Clear hooks\n hookState = {\n onAfterStep: new Map(),\n };\n // Clear snapshots\n snapshots.length = 0;\n eventIndex = 0;\n }\n\n /**\n * Get all recorded snapshots.\n */\n function getSnapshots(): IRSnapshot[] {\n return [...snapshots];\n }\n\n /**\n * Get a snapshot at a specific index.\n */\n function getSnapshotAt(index: number): IRSnapshot | undefined {\n return snapshots[index];\n }\n\n /**\n * Get the IR state at a specific snapshot index.\n */\n function getIRAt(index: number): WorkflowIR | undefined {\n return snapshots[index]?.ir;\n }\n\n /**\n * Clear all recorded snapshots.\n */\n function clearSnapshots(): void {\n snapshots.length = 0;\n eventIndex = 0;\n }\n\n return {\n handleEvent,\n handleScopeEvent,\n handleDecisionEvent,\n getIR,\n reset,\n // Snapshot methods for time-travel\n getSnapshots,\n getSnapshotAt,\n getIRAt,\n clearSnapshots,\n /** Check if there are active (running) steps */\n get hasActiveSteps() {\n return activeSteps.size > 0;\n },\n /** Get the current workflow state */\n get state() {\n return workflowState;\n },\n /** Get the number of recorded snapshots */\n get snapshotCount() {\n return snapshots.length;\n },\n /** Check if snapshot recording is enabled */\n get snapshotsEnabled() {\n return enableSnapshots;\n },\n };\n}\n\n/**\n * Type for the IR builder instance.\n */\nexport type IRBuilder = ReturnType<typeof createIRBuilder>;\n","/**\n * Workflow Visualization - Intermediate Representation Types\n *\n * The IR (Intermediate Representation) is a DSL that represents workflow\n * execution structure. Events are converted to IR, which can then be\n * rendered to various output formats (ASCII, Mermaid, JSON, etc.).\n */\n\n// =============================================================================\n// Step States\n// =============================================================================\n\n/**\n * Execution state of a step with semantic meaning for visualization.\n *\n * Color mapping:\n * - pending → white/clear (not started)\n * - running → yellow (currently executing)\n * - success → green (completed successfully)\n * - error → red (failed with error)\n * - aborted → gray (cancelled, e.g., in race)\n * - cached → blue (served from cache)\n * - skipped → dim gray (not executed due to conditional logic)\n */\nexport type StepState =\n | \"pending\"\n | \"running\"\n | \"success\"\n | \"error\"\n | \"aborted\"\n | \"cached\"\n | \"skipped\";\n\n// =============================================================================\n// Node Types\n// =============================================================================\n\n/**\n * Base properties shared by all IR nodes.\n */\nexport interface BaseNode {\n /** Unique identifier for this node */\n id: string;\n /** Human-readable name (from step options or inferred) */\n name?: string;\n /** Cache key if this is a keyed step */\n key?: string;\n /** Current execution state */\n state: StepState;\n /** Timestamp when execution started */\n startTs?: number;\n /** Timestamp when execution ended */\n endTs?: number;\n /** Duration in milliseconds */\n durationMs?: number;\n /** Error value if state is 'error' */\n error?: unknown;\n /** Input value that triggered this step (for decision understanding) */\n input?: unknown;\n /** Output value from this step (for decision understanding) */\n output?: unknown;\n /** Number of retry attempts made (0 = no retries, 1 = one retry, etc.) */\n retryCount?: number;\n /** Whether this step experienced a timeout (may have retried after) */\n timedOut?: boolean;\n /** Timeout duration in ms (if timed out) */\n timeoutMs?: number;\n}\n\n/**\n * A single step execution node.\n */\nexport interface StepNode extends BaseNode {\n type: \"step\";\n}\n\n/**\n * Sequential execution - steps run one after another.\n * This is the implicit structure when steps are awaited in sequence.\n */\nexport interface SequenceNode extends BaseNode {\n type: \"sequence\";\n children: FlowNode[];\n}\n\n/**\n * Parallel execution - all branches run simultaneously.\n * Created by allAsync() or allSettledAsync().\n */\nexport interface ParallelNode extends BaseNode {\n type: \"parallel\";\n children: FlowNode[];\n /**\n * Execution mode:\n * - 'all': Fails on first error (allAsync)\n * - 'allSettled': Collects all results (allSettledAsync)\n */\n mode: \"all\" | \"allSettled\";\n}\n\n/**\n * Race execution - first to complete wins.\n * Created by anyAsync().\n */\nexport interface RaceNode extends BaseNode {\n type: \"race\";\n children: FlowNode[];\n /** ID of the winning branch (first to succeed) */\n winnerId?: string;\n}\n\n/**\n * Decision point - conditional branch (if/switch).\n * Shows which branch was taken and why.\n */\nexport interface DecisionNode extends BaseNode {\n type: \"decision\";\n /** Condition that was evaluated (e.g., \"user.role === 'admin'\") */\n condition?: string;\n /** Value that was evaluated (the input to the decision) */\n decisionValue?: unknown;\n /** Which branch was taken (true/false, or the matched case) */\n branchTaken?: string | boolean;\n /** All possible branches (including skipped ones) */\n branches: DecisionBranch[];\n}\n\n/**\n * A branch in a decision node.\n */\nexport interface DecisionBranch {\n /** Label for this branch (e.g., \"if\", \"else\", \"case 'admin'\") */\n label: string;\n /** Condition that would trigger this branch */\n condition?: string;\n /** Whether this branch was taken */\n taken: boolean;\n /** Steps in this branch */\n children: FlowNode[];\n}\n\n/**\n * Union of all flow node types.\n */\nexport type FlowNode = StepNode | SequenceNode | ParallelNode | RaceNode | DecisionNode;\n\n/**\n * Root node representing the entire workflow.\n */\nexport interface WorkflowNode extends BaseNode {\n type: \"workflow\";\n /** Correlation ID from the workflow execution */\n workflowId: string;\n /** Child nodes (steps, parallel blocks, etc.) */\n children: FlowNode[];\n}\n\n// =============================================================================\n// Workflow IR\n// =============================================================================\n\n/**\n * Complete workflow intermediate representation.\n * This is the main data structure produced by the IR builder.\n */\nexport interface WorkflowIR {\n /** Root workflow node */\n root: WorkflowNode;\n /** Metadata about the IR */\n metadata: {\n /** When the IR was first created */\n createdAt: number;\n /** When the IR was last updated */\n lastUpdatedAt: number;\n };\n /** Hook executions (if any hooks are configured) */\n hooks?: WorkflowHooks;\n}\n\n// =============================================================================\n// Scope Events (for parallel/race detection)\n// =============================================================================\n\n// Re-export ScopeType from core for consistency\nexport type { ScopeType } from \"../core\";\nimport type { ScopeType } from \"../core\";\n\n/**\n * Event emitted when entering a parallel/race scope.\n * This matches the scope_start event in WorkflowEvent.\n */\nexport interface ScopeStartEvent {\n type: \"scope_start\";\n workflowId: string;\n scopeId: string;\n scopeType: ScopeType;\n name?: string;\n ts: number;\n}\n\n/**\n * Event emitted when exiting a parallel/race scope.\n */\nexport interface ScopeEndEvent {\n type: \"scope_end\";\n workflowId: string;\n scopeId: string;\n ts: number;\n durationMs: number;\n /** For race scopes, the ID of the winning branch */\n winnerId?: string;\n}\n\n/**\n * Event emitted when a decision point is encountered.\n * Use this to track conditional logic (if/switch).\n */\nexport interface DecisionStartEvent {\n type: \"decision_start\";\n workflowId: string;\n decisionId: string;\n /** Condition being evaluated (e.g., \"user.role === 'admin'\") */\n condition?: string;\n /** Value being evaluated */\n decisionValue?: unknown;\n /** Name/label for this decision point */\n name?: string;\n ts: number;\n}\n\n/**\n * Event emitted when a decision branch is taken.\n */\nexport interface DecisionBranchEvent {\n type: \"decision_branch\";\n workflowId: string;\n decisionId: string;\n /** Label for this branch (e.g., \"if\", \"else\", \"case 'admin'\") */\n branchLabel: string;\n /** Condition for this branch */\n condition?: string;\n /** Whether this branch was taken */\n taken: boolean;\n ts: number;\n}\n\n/**\n * Event emitted when a decision point completes.\n */\nexport interface DecisionEndEvent {\n type: \"decision_end\";\n workflowId: string;\n decisionId: string;\n /** Which branch was taken */\n branchTaken?: string | boolean;\n ts: number;\n durationMs: number;\n}\n\n/**\n * Event emitted when a step is skipped due to conditional logic.\n */\nexport interface StepSkippedEvent {\n type: \"step_skipped\";\n workflowId: string;\n stepKey?: string;\n name?: string;\n /** Reason why this step was skipped (e.g., \"condition was false\") */\n reason?: string;\n /** The decision that caused this skip */\n decisionId?: string;\n ts: number;\n}\n\n/**\n * Union of scope-related events.\n */\nexport type ScopeEvent = ScopeStartEvent | ScopeEndEvent;\n\n/**\n * Union of decision-related events.\n */\nexport type DecisionEvent = DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent;\n\n// =============================================================================\n// Renderer Types\n// =============================================================================\n\n/**\n * Color scheme for rendering step states.\n */\nexport interface ColorScheme {\n pending: string;\n running: string;\n success: string;\n error: string;\n aborted: string;\n cached: string;\n skipped: string;\n}\n\n/**\n * Options passed to renderers.\n */\nexport interface RenderOptions {\n /** Show timing information (duration) */\n showTimings: boolean;\n /** Show step cache keys */\n showKeys: boolean;\n /** Terminal width for ASCII renderer */\n terminalWidth?: number;\n /** Color scheme */\n colors: ColorScheme;\n}\n\n/**\n * Renderer interface - transforms IR to output format.\n */\nexport interface Renderer {\n /** Unique identifier for this renderer */\n readonly name: string;\n /** Render IR to string output */\n render(ir: WorkflowIR, options: RenderOptions): string;\n /** Whether this renderer supports live (incremental) updates */\n supportsLive?: boolean;\n /** Render incremental update (optional) */\n renderUpdate?(\n ir: WorkflowIR,\n changedNodes: FlowNode[],\n options: RenderOptions\n ): string;\n}\n\n// =============================================================================\n// Visualizer Types\n// =============================================================================\n\n/**\n * Output format for rendering.\n */\nexport type OutputFormat = \"ascii\" | \"mermaid\" | \"json\";\n\n/**\n * Options for creating a visualizer.\n */\nexport interface VisualizerOptions {\n /** Name for the workflow in visualizations */\n workflowName?: string;\n /** Enable parallel detection heuristics (default: true) */\n detectParallel?: boolean;\n /** Show timing information (default: true) */\n showTimings?: boolean;\n /** Show step keys (default: false) */\n showKeys?: boolean;\n /** Custom color scheme */\n colors?: Partial<ColorScheme>;\n}\n\n/**\n * Options for live visualization.\n */\nexport interface LiveVisualizerOptions extends VisualizerOptions {\n /** Output stream (default: process.stdout) */\n stream?: NodeJS.WriteStream;\n /** Update interval in ms (default: 100) */\n updateInterval?: number;\n}\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\n/**\n * Check if a node is a StepNode.\n */\nexport function isStepNode(node: FlowNode): node is StepNode {\n return node.type === \"step\";\n}\n\n/**\n * Check if a node is a SequenceNode.\n */\nexport function isSequenceNode(node: FlowNode): node is SequenceNode {\n return node.type === \"sequence\";\n}\n\n/**\n * Check if a node is a ParallelNode.\n */\nexport function isParallelNode(node: FlowNode): node is ParallelNode {\n return node.type === \"parallel\";\n}\n\n/**\n * Check if a node is a RaceNode.\n */\nexport function isRaceNode(node: FlowNode): node is RaceNode {\n return node.type === \"race\";\n}\n\n/**\n * Check if a node is a DecisionNode.\n */\nexport function isDecisionNode(node: FlowNode): node is DecisionNode {\n return node.type === \"decision\";\n}\n\n/**\n * Check if a node has children.\n */\nexport function hasChildren(\n node: FlowNode\n): node is SequenceNode | ParallelNode | RaceNode | DecisionNode {\n return \"children\" in node || (node.type === \"decision\" && \"branches\" in node);\n}\n\n// =============================================================================\n// Time Travel Types\n// =============================================================================\n\n/**\n * Snapshot of an active step's state at a point in time.\n */\nexport interface ActiveStepSnapshot {\n id: string;\n name?: string;\n key?: string;\n startTs: number;\n retryCount: number;\n timedOut: boolean;\n timeoutMs?: number;\n}\n\n/**\n * A snapshot of the complete IR state at a specific point in time.\n * Used for time-travel debugging - each event creates a snapshot.\n */\nexport interface IRSnapshot {\n /** Unique identifier for this snapshot */\n id: string;\n /** Index in the event sequence (0-based) */\n eventIndex: number;\n /** The event that triggered this snapshot */\n event: unknown; // WorkflowEvent - avoid circular import\n /** Complete IR state at this moment */\n ir: WorkflowIR;\n /** Timestamp when snapshot was taken */\n timestamp: number;\n /** Active step states at this moment (for debugging) */\n activeSteps: Map<string, ActiveStepSnapshot>;\n}\n\n/**\n * State of the time-travel controller.\n */\nexport interface TimeTravelState {\n /** All recorded snapshots */\n snapshots: IRSnapshot[];\n /** Current snapshot index (for playback position) */\n currentIndex: number;\n /** Whether playback is active */\n isPlaying: boolean;\n /** Playback speed multiplier (1.0 = realtime, 2.0 = 2x speed) */\n playbackSpeed: number;\n /** Whether recording is active */\n isRecording: boolean;\n}\n\n// =============================================================================\n// Performance Analysis Types\n// =============================================================================\n\n/**\n * Performance metrics for a single node across multiple runs.\n */\nexport interface NodePerformance {\n /** Node identifier (name or step ID) */\n nodeId: string;\n /** Average duration across all samples */\n avgDurationMs: number;\n /** Minimum duration observed */\n minDurationMs: number;\n /** Maximum duration observed */\n maxDurationMs: number;\n /** Standard deviation of durations */\n stdDevMs: number;\n /** Number of timing samples collected */\n samples: number;\n /** Retry frequency (0-1, where 1 = always retries) */\n retryRate: number;\n /** Timeout frequency (0-1) */\n timeoutRate: number;\n /** Error rate (0-1) */\n errorRate: number;\n /** Percentile data for distribution analysis */\n percentiles: {\n p50: number;\n p90: number;\n p95: number;\n p99: number;\n };\n}\n\n/**\n * Heatmap data for visualizing performance across nodes.\n */\nexport interface HeatmapData {\n /** Map of node ID to heat level (0-1, where 1 is hottest/slowest) */\n heat: Map<string, number>;\n /** The metric used for heat calculation */\n metric: \"duration\" | \"retryRate\" | \"errorRate\";\n /** Statistics used to compute heat values */\n stats: {\n /** Minimum value in the dataset */\n min: number;\n /** Maximum value in the dataset */\n max: number;\n /** Mean value */\n mean: number;\n /** Threshold above which a node is considered \"hot\" */\n threshold: number;\n };\n}\n\n/**\n * Heat level for visual styling.\n */\nexport type HeatLevel = \"cold\" | \"cool\" | \"neutral\" | \"warm\" | \"hot\" | \"critical\";\n\n// =============================================================================\n// HTML Renderer Types\n// =============================================================================\n\n/**\n * Theme for the HTML visualizer.\n */\nexport type HTMLTheme = \"light\" | \"dark\" | \"auto\";\n\n/**\n * Layout direction for the workflow diagram.\n */\nexport type LayoutDirection = \"TB\" | \"LR\" | \"BT\" | \"RL\";\n\n/**\n * Options for the HTML renderer.\n */\nexport interface HTMLRenderOptions extends RenderOptions {\n /** Enable interactive features (click to inspect, zoom/pan) */\n interactive: boolean;\n /** Include time-travel controls */\n timeTravel: boolean;\n /** Include performance heatmap overlay */\n heatmap: boolean;\n /** Animation duration for transitions (ms) */\n animationDuration: number;\n /** Color theme */\n theme: HTMLTheme;\n /** Diagram layout direction */\n layout: LayoutDirection;\n /** Heatmap data (if heatmap is enabled) */\n heatmapData?: HeatmapData;\n /** WebSocket URL for live updates (if streaming) */\n wsUrl?: string;\n}\n\n/**\n * Message sent from the web visualizer to the dev server.\n */\nexport interface WebVisualizerMessage {\n type:\n | \"time_travel_seek\"\n | \"time_travel_play\"\n | \"time_travel_pause\"\n | \"time_travel_step_forward\"\n | \"time_travel_step_backward\"\n | \"request_snapshots\"\n | \"toggle_heatmap\"\n | \"set_heatmap_metric\";\n payload?: unknown;\n}\n\n/**\n * Message sent from the dev server to the web visualizer.\n */\nexport interface ServerMessage {\n type:\n | \"ir_update\"\n | \"snapshot\"\n | \"snapshots_list\"\n | \"performance_data\"\n | \"workflow_complete\"\n | \"time_travel_state\";\n payload: unknown;\n}\n\n// =============================================================================\n// Enhanced ASCII Renderer Types\n// =============================================================================\n\n/**\n * Extended render options for the enhanced ASCII renderer.\n */\nexport interface EnhancedRenderOptions extends RenderOptions {\n /** Show performance heatmap coloring */\n showHeatmap?: boolean;\n /** Heatmap data for coloring nodes */\n heatmapData?: HeatmapData;\n /** Show timing sparklines (requires historical data) */\n showSparklines?: boolean;\n /** Historical timing data for sparklines: nodeId → array of durations */\n timingHistory?: Map<string, number[]>;\n}\n\n// =============================================================================\n// Hook Execution Types\n// =============================================================================\n\n/**\n * State of a hook execution.\n */\nexport type HookState = \"pending\" | \"running\" | \"success\" | \"error\";\n\n/**\n * Execution record for a workflow hook.\n */\nexport interface HookExecution {\n /** Hook type identifier */\n type: \"shouldRun\" | \"onBeforeStart\" | \"onAfterStep\";\n /** Execution state */\n state: HookState;\n /** Timestamp when hook started */\n ts: number;\n /** Duration in milliseconds */\n durationMs?: number;\n /** Error if hook failed */\n error?: unknown;\n /** Additional context (e.g., stepKey for onAfterStep) */\n context?: {\n /** Step key for onAfterStep hooks */\n stepKey?: string;\n /** Result of shouldRun hook */\n result?: boolean;\n /** Whether workflow was skipped due to shouldRun returning false */\n skipped?: boolean;\n };\n}\n\n/**\n * Hook execution summary for the workflow.\n */\nexport interface WorkflowHooks {\n /** shouldRun hook execution (if configured) */\n shouldRun?: HookExecution;\n /** onBeforeStart hook execution (if configured) */\n onBeforeStart?: HookExecution;\n /** onAfterStep hook executions (keyed by stepKey) */\n onAfterStep: Map<string, HookExecution>;\n}\n","/**\n * ANSI color utilities for terminal output.\n */\n\nimport type { ColorScheme, StepState } from \"../types\";\n\n// =============================================================================\n// ANSI Escape Codes\n// =============================================================================\n\nconst RESET = \"\\x1b[0m\";\nconst BOLD = \"\\x1b[1m\";\nconst DIM = \"\\x1b[2m\";\n\n// Foreground colors\nconst FG_RED = \"\\x1b[31m\";\nconst FG_GREEN = \"\\x1b[32m\";\nconst FG_YELLOW = \"\\x1b[33m\";\nconst FG_BLUE = \"\\x1b[34m\";\nconst FG_GRAY = \"\\x1b[90m\";\nconst FG_WHITE = \"\\x1b[37m\";\n\n// =============================================================================\n// Color Functions\n// =============================================================================\n\n/**\n * Apply ANSI color to text.\n */\nexport function colorize(text: string, color: string): string {\n if (!color) return text;\n return `${color}${text}${RESET}`;\n}\n\n/**\n * Make text bold.\n */\nexport function bold(text: string): string {\n return `${BOLD}${text}${RESET}`;\n}\n\n/**\n * Make text dim.\n */\nexport function dim(text: string): string {\n return `${DIM}${text}${RESET}`;\n}\n\n// =============================================================================\n// Default Color Scheme\n// =============================================================================\n\n/**\n * Default ANSI color scheme for step states.\n */\nexport const defaultColorScheme: ColorScheme = {\n pending: FG_WHITE,\n running: FG_YELLOW,\n success: FG_GREEN,\n error: FG_RED,\n aborted: FG_GRAY,\n cached: FG_BLUE,\n skipped: DIM + FG_GRAY, // Dim gray for skipped steps\n};\n\n// =============================================================================\n// State Symbols\n// =============================================================================\n\n/**\n * Get the symbol for a step state.\n */\nexport function getStateSymbol(state: StepState): string {\n switch (state) {\n case \"pending\":\n return \"○\"; // Empty circle\n case \"running\":\n return \"⟳\"; // Rotating arrows\n case \"success\":\n return \"✓\"; // Check mark\n case \"error\":\n return \"✗\"; // X mark\n case \"aborted\":\n return \"⊘\"; // Circled slash\n case \"cached\":\n return \"↺\"; // Cached/replay\n case \"skipped\":\n return \"⊘\"; // Circled slash (same as aborted, but different color)\n }\n}\n\n/**\n * Get the colored symbol for a step state.\n */\nexport function getColoredSymbol(state: StepState, colors: ColorScheme): string {\n const symbol = getStateSymbol(state);\n return colorize(symbol, colors[state]);\n}\n\n/**\n * Get colored text based on step state.\n */\nexport function colorByState(\n text: string,\n state: StepState,\n colors: ColorScheme\n): string {\n return colorize(text, colors[state]);\n}\n\n// =============================================================================\n// Strip ANSI\n// =============================================================================\n\n/**\n * Strip ANSI escape codes from a string.\n * Useful for calculating visible string length.\n */\nexport function stripAnsi(str: string): string {\n // eslint-disable-next-line no-control-regex\n return str.replace(/\\x1b\\[[0-9;]*m/g, \"\");\n}\n\n/**\n * Get the visible length of a string (without ANSI codes).\n */\nexport function visibleLength(str: string): string {\n return stripAnsi(str);\n}\n","/**\n * ASCII Terminal Renderer\n *\n * Renders the workflow IR as ASCII art with box-drawing characters\n * and ANSI colors for terminal display.\n */\n\nimport type {\n FlowNode,\n ParallelNode,\n RaceNode,\n DecisionNode,\n Renderer,\n RenderOptions,\n StepNode,\n WorkflowIR,\n EnhancedRenderOptions,\n HeatLevel,\n WorkflowHooks,\n HookExecution,\n} from \"../types\";\nimport { isParallelNode, isRaceNode, isStepNode, isDecisionNode } from \"../types\";\nimport { formatDuration } from \"../utils/timing\";\nimport {\n bold,\n colorByState,\n colorize,\n defaultColorScheme,\n dim,\n getColoredSymbol,\n stripAnsi,\n} from \"./colors\";\n\n// =============================================================================\n// Box Drawing Characters\n// =============================================================================\n\nconst BOX = {\n topLeft: \"┌\",\n topRight: \"┐\",\n bottomLeft: \"└\",\n bottomRight: \"┘\",\n horizontal: \"─\",\n vertical: \"│\",\n teeRight: \"├\",\n teeLeft: \"┤\",\n teeDown: \"┬\",\n teeUp: \"┴\",\n cross: \"┼\",\n} as const;\n\n// =============================================================================\n// Heatmap Colors (ANSI)\n// =============================================================================\n\n/**\n * ANSI color codes for heatmap visualization.\n */\nconst HEAT_COLORS: Record<HeatLevel, string> = {\n cold: \"\\x1b[34m\", // Blue\n cool: \"\\x1b[36m\", // Cyan\n neutral: \"\", // Default (no color)\n warm: \"\\x1b[33m\", // Yellow\n hot: \"\\x1b[31m\", // Red\n critical: \"\\x1b[41m\", // Red background\n};\n\nconst RESET = \"\\x1b[0m\";\n\n/**\n * Get ANSI color code for a heat level.\n */\nfunction getHeatColor(heat: number): string {\n if (heat < 0.2) return HEAT_COLORS.cold;\n if (heat < 0.4) return HEAT_COLORS.cool;\n if (heat < 0.6) return HEAT_COLORS.neutral;\n if (heat < 0.8) return HEAT_COLORS.warm;\n if (heat < 0.95) return HEAT_COLORS.hot;\n return HEAT_COLORS.critical;\n}\n\n/**\n * Apply heat coloring to a string.\n */\nfunction applyHeatColor(text: string, heat: number): string {\n const color = getHeatColor(heat);\n if (!color) return text;\n return `${color}${text}${RESET}`;\n}\n\n// =============================================================================\n// Sparkline Characters\n// =============================================================================\n\nconst SPARK_CHARS = \"▁▂▃▄▅▆▇█\";\n\n/**\n * Render a sparkline from an array of values.\n *\n * @param values Array of numeric values\n * @param width Maximum characters to use (default: 10)\n * @returns Sparkline string\n */\nexport function renderSparkline(values: number[], width = 10): string {\n if (values.length === 0) return \"\";\n\n // Take last N values\n const subset = values.slice(-width);\n const min = Math.min(...subset);\n const max = Math.max(...subset);\n const range = max - min || 1;\n\n return subset\n .map((v) => {\n const normalized = (v - min) / range;\n const index = Math.floor(normalized * (SPARK_CHARS.length - 1));\n return SPARK_CHARS[index];\n })\n .join(\"\");\n}\n\n// =============================================================================\n// Helper Functions\n// =============================================================================\n\n/**\n * Pad a string to a fixed width, accounting for ANSI codes.\n */\nfunction padEnd(str: string, width: number): string {\n const visibleLen = stripAnsi(str).length;\n const padding = Math.max(0, width - visibleLen);\n return str + \" \".repeat(padding);\n}\n\n/**\n * Create a horizontal line with optional title.\n */\nfunction horizontalLine(width: number, title?: string): string {\n if (!title) {\n return BOX.horizontal.repeat(width);\n }\n\n const titleText = ` ${title} `;\n const remainingWidth = width - titleText.length;\n if (remainingWidth < 4) {\n return BOX.horizontal.repeat(width);\n }\n\n const leftPad = 2;\n const rightPad = remainingWidth - leftPad;\n\n return (\n BOX.horizontal.repeat(leftPad) + titleText + BOX.horizontal.repeat(rightPad)\n );\n}\n\n// =============================================================================\n// Hook Rendering\n// =============================================================================\n\n/**\n * Render a single hook execution.\n */\nfunction renderHookExecution(\n hook: HookExecution,\n label: string,\n colors: ReturnType<typeof Object.assign>\n): string {\n const symbol = hook.state === \"success\"\n ? colorize(\"⚙\", colors.success)\n : colorize(\"⚠\", colors.error);\n\n const timing = hook.durationMs !== undefined\n ? dim(` [${formatDuration(hook.durationMs)}]`)\n : \"\";\n\n let context = \"\";\n if (hook.type === \"shouldRun\" && hook.context?.skipped) {\n context = dim(\" → workflow skipped\");\n } else if (hook.type === \"shouldRun\" && hook.context?.result === true) {\n context = dim(\" → proceed\");\n } else if (hook.type === \"onBeforeStart\" && hook.context?.skipped) {\n context = dim(\" → workflow skipped\");\n } else if (hook.type === \"onAfterStep\" && hook.context?.stepKey) {\n context = dim(` (${hook.context.stepKey})`);\n }\n\n const error = hook.state === \"error\" && hook.error\n ? dim(` error: ${String(hook.error)}`)\n : \"\";\n\n return `${symbol} ${dim(label)}${context}${timing}${error}`;\n}\n\n/**\n * Render workflow hooks section.\n */\nfunction renderHooks(\n hooks: WorkflowHooks,\n colors: ReturnType<typeof Object.assign>\n): string[] {\n const lines: string[] = [];\n\n // Render shouldRun hook\n if (hooks.shouldRun) {\n lines.push(renderHookExecution(hooks.shouldRun, \"shouldRun\", colors));\n }\n\n // Render onBeforeStart hook\n if (hooks.onBeforeStart) {\n lines.push(renderHookExecution(hooks.onBeforeStart, \"onBeforeStart\", colors));\n }\n\n // We don't render onAfterStep hooks here - they're shown inline with steps\n // But if there are any, add a separator\n if (lines.length > 0) {\n lines.push(dim(\"────────────────────\")); // Separator between hooks and steps\n }\n\n return lines;\n}\n\n// =============================================================================\n// ASCII Renderer\n// =============================================================================\n\n/**\n * Create the ASCII terminal renderer.\n */\nexport function asciiRenderer(): Renderer {\n return {\n name: \"ascii\",\n supportsLive: true,\n\n render(ir: WorkflowIR, options: RenderOptions): string {\n const colors = { ...defaultColorScheme, ...options.colors };\n const width = options.terminalWidth ?? 60;\n const innerWidth = width - 4; // Account for borders\n\n const lines: string[] = [];\n\n // Header\n const workflowName = ir.root.name ?? \"workflow\";\n const headerTitle = bold(workflowName);\n lines.push(\n `${BOX.topLeft}${horizontalLine(width - 2, headerTitle)}${BOX.topRight}`\n );\n lines.push(`${BOX.vertical}${\" \".repeat(width - 2)}${BOX.vertical}`);\n\n // Render hooks (if any)\n if (ir.hooks) {\n const hookLines = renderHooks(ir.hooks, colors);\n for (const line of hookLines) {\n lines.push(\n `${BOX.vertical} ${padEnd(line, innerWidth)}${BOX.vertical}`\n );\n }\n }\n\n // Render children\n const childLines = renderNodes(ir.root.children, options, colors, 0, ir.hooks);\n for (const line of childLines) {\n lines.push(\n `${BOX.vertical} ${padEnd(line, innerWidth)}${BOX.vertical}`\n );\n }\n\n // Footer with timing\n lines.push(`${BOX.vertical}${\" \".repeat(width - 2)}${BOX.vertical}`);\n\n if (ir.root.durationMs !== undefined && options.showTimings) {\n const status = ir.root.state === \"success\" ? \"Completed\" : \"Failed\";\n const statusColored = colorByState(status, ir.root.state, colors);\n const footer = `${statusColored} in ${formatDuration(ir.root.durationMs)}`;\n lines.push(\n `${BOX.vertical} ${padEnd(footer, innerWidth)}${BOX.vertical}`\n );\n lines.push(`${BOX.vertical}${\" \".repeat(width - 2)}${BOX.vertical}`);\n }\n\n lines.push(\n `${BOX.bottomLeft}${BOX.horizontal.repeat(width - 2)}${BOX.bottomRight}`\n );\n\n return lines.join(\"\\n\");\n },\n };\n}\n\n/**\n * Render a list of nodes.\n */\nfunction renderNodes(\n nodes: FlowNode[],\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number,\n hooks?: WorkflowHooks\n): string[] {\n const lines: string[] = [];\n\n for (const node of nodes) {\n if (isStepNode(node)) {\n lines.push(renderStepNode(node, options, colors, hooks));\n } else if (isParallelNode(node)) {\n lines.push(...renderParallelNode(node, options, colors, depth, hooks));\n } else if (isRaceNode(node)) {\n lines.push(...renderRaceNode(node, options, colors, depth, hooks));\n } else if (isDecisionNode(node)) {\n lines.push(...renderDecisionNode(node, options, colors, depth, hooks));\n }\n }\n\n return lines;\n}\n\n/**\n * Render a single step node.\n */\nfunction renderStepNode(\n node: StepNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n hooks?: WorkflowHooks\n): string {\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? node.key ?? \"step\";\n\n // Check for enhanced options\n const enhanced = options as EnhancedRenderOptions;\n const nodeId = node.name ?? node.id;\n\n // Get heat value for this node (if heatmap is enabled)\n const heat = enhanced.showHeatmap && enhanced.heatmapData\n ? enhanced.heatmapData.heat.get(node.id) ?? enhanced.heatmapData.heat.get(nodeId)\n : undefined;\n\n // Apply heat coloring or default state coloring\n let nameColored: string;\n if (heat !== undefined) {\n nameColored = applyHeatColor(name, heat);\n } else {\n nameColored = colorByState(name, node.state, colors);\n }\n\n let line = `${symbol} ${nameColored}`;\n\n // Add key if requested\n if (options.showKeys && node.key) {\n line += dim(` [key: ${node.key}]`);\n }\n\n // Add input/output if available (for decision understanding)\n if (node.input !== undefined) {\n const inputStr = typeof node.input === \"string\"\n ? node.input\n : JSON.stringify(node.input).slice(0, 30);\n line += dim(` [in: ${inputStr}${inputStr.length >= 30 ? \"...\" : \"\"}]`);\n }\n if (node.output !== undefined && node.state === \"success\") {\n const outputStr = typeof node.output === \"string\"\n ? node.output\n : JSON.stringify(node.output).slice(0, 30);\n line += dim(` [out: ${outputStr}${outputStr.length >= 30 ? \"...\" : \"\"}]`);\n }\n\n // Add timing if available and requested\n if (options.showTimings && node.durationMs !== undefined) {\n // Apply heat coloring to timing if enabled\n const timingStr = formatDuration(node.durationMs);\n const timingDisplay = heat !== undefined\n ? applyHeatColor(`[${timingStr}]`, heat)\n : dim(`[${timingStr}]`);\n line += ` ${timingDisplay}`;\n }\n\n // Add sparkline if enabled and history available\n if (enhanced.showSparklines && enhanced.timingHistory) {\n const history = enhanced.timingHistory.get(nodeId);\n if (history && history.length > 1) {\n line += ` ${dim(renderSparkline(history))}`;\n }\n }\n\n // Add retry indicator if retries occurred\n if (node.retryCount !== undefined && node.retryCount > 0) {\n line += dim(` [${node.retryCount} ${node.retryCount === 1 ? \"retry\" : \"retries\"}]`);\n }\n\n // Add timeout indicator if step timed out\n if (node.timedOut) {\n const timeoutInfo = node.timeoutMs !== undefined ? ` ${node.timeoutMs}ms` : \"\";\n line += dim(` [timeout${timeoutInfo}]`);\n }\n\n // Add onAfterStep hook indicator if present\n if (hooks && node.key && hooks.onAfterStep.has(node.key)) {\n const hookExec = hooks.onAfterStep.get(node.key)!;\n const hookSymbol = hookExec.state === \"success\"\n ? colorize(\"⚙\", colors.success)\n : colorize(\"⚠\", colors.error);\n const hookTiming = hookExec.durationMs !== undefined\n ? dim(` ${formatDuration(hookExec.durationMs)}`)\n : \"\";\n line += ` ${hookSymbol}${hookTiming}`;\n }\n\n return line;\n}\n\n/**\n * Render a parallel node (allAsync).\n */\nfunction renderParallelNode(\n node: ParallelNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number,\n hooks?: WorkflowHooks\n): string[] {\n const lines: string[] = [];\n const indent = \" \".repeat(depth);\n\n // Header\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? \"parallel\";\n const mode = node.mode === \"allSettled\" ? \" (allSettled)\" : \"\";\n lines.push(`${indent}${BOX.teeRight}${BOX.teeDown}${BOX.horizontal} ${symbol} ${bold(name)}${mode}`);\n\n // Children\n if (node.children.length === 0) {\n // Empty parallel scope - operations inside allAsync/anyAsync weren't tracked as steps\n lines.push(`${indent}${BOX.vertical} ${dim(\"(operations not individually tracked)\")}`);\n lines.push(`${indent}${BOX.vertical} ${dim(\"(wrap each operation with step() to see individual steps)\")}`);\n } else {\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i];\n const isLast = i === node.children.length - 1;\n const prefix = isLast ? `${indent}${BOX.vertical} ${BOX.bottomLeft}` : `${indent}${BOX.vertical} ${BOX.teeRight}`;\n\n if (isStepNode(child)) {\n lines.push(`${prefix} ${renderStepNode(child, options, colors, hooks)}`);\n } else {\n // Nested structure - recurse\n const nestedLines = renderNodes([child], options, colors, depth + 1, hooks);\n for (const line of nestedLines) {\n lines.push(`${indent}${BOX.vertical} ${line}`);\n }\n }\n }\n }\n\n // Timing footer\n if (options.showTimings && node.durationMs !== undefined) {\n lines.push(`${indent}${BOX.bottomLeft}${BOX.horizontal}${BOX.horizontal} ${dim(`[${formatDuration(node.durationMs)}]`)}`);\n }\n\n return lines;\n}\n\n/**\n * Render a race node (anyAsync).\n */\nfunction renderRaceNode(\n node: RaceNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number,\n hooks?: WorkflowHooks\n): string[] {\n const lines: string[] = [];\n const indent = \" \".repeat(depth);\n\n // Header with lightning bolt for race\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? \"race\";\n lines.push(`${indent}${BOX.teeRight}⚡ ${symbol} ${bold(name)}`);\n\n // Children\n if (node.children.length === 0) {\n // Empty race scope - operations inside anyAsync weren't tracked as steps\n lines.push(`${indent}${BOX.vertical} ${dim(\"(operations not individually tracked)\")}`);\n lines.push(`${indent}${BOX.vertical} ${dim(\"(wrap each operation with step() to see individual steps)\")}`);\n } else {\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i];\n const isLast = i === node.children.length - 1;\n const prefix = isLast ? `${indent}${BOX.vertical} ${BOX.bottomLeft}` : `${indent}${BOX.vertical} ${BOX.teeRight}`;\n\n // Mark winner\n const isWinner = node.winnerId && child.id === node.winnerId;\n const winnerSuffix = isWinner ? dim(\" (winner)\") : \"\";\n\n if (isStepNode(child)) {\n lines.push(`${prefix} ${renderStepNode(child, options, colors, hooks)}${winnerSuffix}`);\n } else {\n const nestedLines = renderNodes([child], options, colors, depth + 1, hooks);\n for (const line of nestedLines) {\n lines.push(`${indent}${BOX.vertical} ${line}`);\n }\n }\n }\n }\n\n // Timing footer\n if (options.showTimings && node.durationMs !== undefined) {\n lines.push(`${indent}${BOX.bottomLeft}${BOX.horizontal}${BOX.horizontal} ${dim(`[${formatDuration(node.durationMs)}]`)}`);\n }\n\n return lines;\n}\n\n/**\n * Render a decision node (conditional branch).\n */\nfunction renderDecisionNode(\n node: DecisionNode,\n options: RenderOptions,\n colors: ReturnType<typeof Object.assign>,\n depth: number,\n hooks?: WorkflowHooks\n): string[] {\n const lines: string[] = [];\n const indent = \" \".repeat(depth);\n\n // Header with decision info\n const symbol = getColoredSymbol(node.state, colors);\n const name = node.name ?? \"decision\";\n const condition = node.condition\n ? dim(` (${node.condition})`)\n : \"\";\n const decisionValue = node.decisionValue !== undefined\n ? dim(` = ${String(node.decisionValue)}`)\n : \"\";\n const branchTaken = node.branchTaken !== undefined\n ? dim(` → ${String(node.branchTaken)}`)\n : \"\";\n\n lines.push(\n `${indent}${BOX.teeRight}${BOX.teeDown}${BOX.horizontal} ${symbol} ${bold(name)}${condition}${decisionValue}${branchTaken}`\n );\n\n // Render branches\n for (let i = 0; i < node.branches.length; i++) {\n const branch = node.branches[i];\n const isLast = i === node.branches.length - 1;\n const prefix = isLast\n ? `${indent}${BOX.vertical} ${BOX.bottomLeft}`\n : `${indent}${BOX.vertical} ${BOX.teeRight}`;\n\n // Branch label with taken/skipped indicator\n const branchSymbol = branch.taken ? \"✓\" : \"⊘\";\n const branchColor = branch.taken ? colors.success : colors.skipped;\n const branchLabel = colorize(\n `${branchSymbol} ${branch.label}`,\n branchColor\n );\n const branchCondition = branch.condition\n ? dim(` (${branch.condition})`)\n : \"\";\n\n lines.push(`${prefix} ${branchLabel}${branchCondition}`);\n\n // Render children of this branch\n if (branch.children.length > 0) {\n const childLines = renderNodes(branch.children, options, colors, depth + 1, hooks);\n for (const line of childLines) {\n lines.push(`${indent}${BOX.vertical} ${line}`);\n }\n } else if (!branch.taken) {\n // Show that this branch was skipped\n lines.push(\n `${indent}${BOX.vertical} ${dim(\"(skipped)\")}`\n );\n }\n }\n\n // Timing footer\n if (options.showTimings && node.durationMs !== undefined) {\n lines.push(\n `${indent}${BOX.bottomLeft}${BOX.horizontal}${BOX.horizontal} ${dim(`[${formatDuration(node.durationMs)}]`)}`\n );\n }\n\n return lines;\n}\n\nexport { defaultColorScheme };\n","/**\n * Performance Analyzer\n *\n * Analyzes workflow execution data to identify:\n * - Slow steps (bottlenecks)\n * - Retry patterns\n * - Error-prone steps\n * - Timing anomalies\n *\n * Aggregates metrics across multiple workflow runs to provide\n * statistical insights and heatmap visualization data.\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n NodePerformance,\n HeatmapData,\n WorkflowIR,\n FlowNode,\n HeatLevel,\n} from \"./types\";\n\n// =============================================================================\n// Types\n// =============================================================================\n\n/**\n * A recorded workflow run for analysis.\n */\nexport interface WorkflowRun {\n /** Unique identifier for this run */\n id: string;\n /** Workflow start timestamp */\n startTime: number;\n /** All events from the workflow execution */\n events: WorkflowEvent<unknown>[];\n}\n\n/**\n * Performance analyzer interface.\n */\nexport interface PerformanceAnalyzer {\n /** Add a completed workflow run for analysis */\n addRun: (run: WorkflowRun) => void;\n\n /** Add events incrementally (alternative to addRun) */\n addEvent: (event: WorkflowEvent<unknown>) => void;\n\n /** Finalize current run (when using addEvent) */\n finalizeRun: (runId: string) => void;\n\n /** Get performance stats for a specific node */\n getNodePerformance: (nodeId: string) => NodePerformance | undefined;\n\n /** Get heatmap data for an IR */\n getHeatmap: (\n ir: WorkflowIR,\n metric?: \"duration\" | \"retryRate\" | \"errorRate\"\n ) => HeatmapData;\n\n /** Get slowest nodes */\n getSlowestNodes: (limit?: number) => NodePerformance[];\n\n /** Get error-prone nodes */\n getErrorProneNodes: (limit?: number) => NodePerformance[];\n\n /** Get retry-prone nodes */\n getRetryProneNodes: (limit?: number) => NodePerformance[];\n\n /** Get all performance data */\n getAllPerformance: () => Map<string, NodePerformance>;\n\n /** Export performance data as JSON */\n exportData: () => string;\n\n /** Import performance data from JSON */\n importData: (json: string) => void;\n\n /** Clear all collected data */\n clear: () => void;\n}\n\n// =============================================================================\n// Helper Functions\n// =============================================================================\n\n/**\n * Flatten all nodes from an IR tree.\n */\nfunction flattenNodes(nodes: FlowNode[]): FlowNode[] {\n const result: FlowNode[] = [];\n for (const node of nodes) {\n result.push(node);\n if (\"children\" in node && Array.isArray(node.children)) {\n result.push(...flattenNodes(node.children));\n }\n if (\"branches\" in node) {\n for (const branch of node.branches) {\n result.push(...flattenNodes(branch.children));\n }\n }\n }\n return result;\n}\n\n/**\n * Calculate percentile value from sorted array.\n */\nfunction percentile(sortedValues: number[], p: number): number {\n if (sortedValues.length === 0) return 0;\n const index = Math.floor(sortedValues.length * p);\n return sortedValues[Math.min(index, sortedValues.length - 1)];\n}\n\n/**\n * Get heat level from normalized value (0-1).\n */\nexport function getHeatLevel(heat: number): HeatLevel {\n if (heat < 0.2) return \"cold\";\n if (heat < 0.4) return \"cool\";\n if (heat < 0.6) return \"neutral\";\n if (heat < 0.8) return \"warm\";\n if (heat < 0.95) return \"hot\";\n return \"critical\";\n}\n\n// =============================================================================\n// Implementation\n// =============================================================================\n\n/**\n * Create a performance analyzer for workflow metrics.\n *\n * @example\n * ```typescript\n * const analyzer = createPerformanceAnalyzer();\n *\n * // Add completed runs\n * analyzer.addRun({ id: 'run-1', startTime: Date.now(), events });\n *\n * // Get insights\n * const slowest = analyzer.getSlowestNodes(5);\n * const heatmap = analyzer.getHeatmap(ir, 'duration');\n * ```\n */\nexport function createPerformanceAnalyzer(): PerformanceAnalyzer {\n // Timing data: nodeId → array of durations (ms)\n const timingData = new Map<string, number[]>();\n\n // Retry data: nodeId → { retried runs, total runs }\n const retryData = new Map<string, { retried: number; total: number }>();\n\n // Error data: nodeId → { error runs, total runs }\n const errorData = new Map<string, { errors: number; total: number }>();\n\n // Timeout data: nodeId → { timed out, total }\n const timeoutData = new Map<string, { timedOut: number; total: number }>();\n\n // Current run state (for incremental event adding)\n let currentRunEvents: WorkflowEvent<unknown>[] = [];\n\n /**\n * Get node ID from event (uses name or stepId).\n */\n function getNodeId(event: {\n stepId?: string;\n stepKey?: string;\n name?: string;\n }): string {\n return event.name ?? event.stepKey ?? event.stepId ?? \"unknown\";\n }\n\n /**\n * Process events from a workflow run.\n */\n function processEvents(events: WorkflowEvent<unknown>[]): void {\n // Track step state during processing\n const stepState = new Map<\n string,\n {\n retried: boolean;\n timedOut: boolean;\n }\n >();\n\n for (const event of events) {\n switch (event.type) {\n case \"step_start\": {\n const id = getNodeId(event);\n stepState.set(id, { retried: false, timedOut: false });\n break;\n }\n\n case \"step_retry\": {\n const id = getNodeId(event);\n const state = stepState.get(id);\n if (state) {\n state.retried = true;\n }\n break;\n }\n\n case \"step_timeout\": {\n const id = getNodeId(event);\n const state = stepState.get(id);\n if (state) {\n state.timedOut = true;\n }\n // Track timeout stats\n const timeout = timeoutData.get(id) ?? { timedOut: 0, total: 0 };\n timeout.timedOut++;\n timeout.total++;\n timeoutData.set(id, timeout);\n break;\n }\n\n case \"step_success\": {\n const id = getNodeId(event);\n const state = stepState.get(id);\n\n // Record timing\n const timings = timingData.get(id) ?? [];\n timings.push(event.durationMs);\n timingData.set(id, timings);\n\n // Record retry status\n const retry = retryData.get(id) ?? { retried: 0, total: 0 };\n retry.total++;\n if (state?.retried) retry.retried++;\n retryData.set(id, retry);\n\n // Record success (no error)\n const error = errorData.get(id) ?? { errors: 0, total: 0 };\n error.total++;\n errorData.set(id, error);\n\n stepState.delete(id);\n break;\n }\n\n case \"step_error\": {\n const id = getNodeId(event);\n const state = stepState.get(id);\n\n // Record timing\n const timings = timingData.get(id) ?? [];\n timings.push(event.durationMs);\n timingData.set(id, timings);\n\n // Record retry status\n const retry = retryData.get(id) ?? { retried: 0, total: 0 };\n retry.total++;\n if (state?.retried) retry.retried++;\n retryData.set(id, retry);\n\n // Record error\n const error = errorData.get(id) ?? { errors: 0, total: 0 };\n error.total++;\n error.errors++;\n errorData.set(id, error);\n\n stepState.delete(id);\n break;\n }\n }\n }\n }\n\n /**\n * Add a completed workflow run.\n */\n function addRun(run: WorkflowRun): void {\n processEvents(run.events);\n }\n\n /**\n * Add an event incrementally.\n */\n function addEvent(event: WorkflowEvent<unknown>): void {\n currentRunEvents.push(event);\n }\n\n /**\n * Finalize current run (process accumulated events).\n */\n function finalizeRun(_runId: string): void {\n if (currentRunEvents.length > 0) {\n processEvents(currentRunEvents);\n currentRunEvents = [];\n }\n }\n\n /**\n * Compute performance metrics for a node.\n */\n function computePerformance(nodeId: string): NodePerformance | undefined {\n const timings = timingData.get(nodeId);\n if (!timings || timings.length === 0) return undefined;\n\n const sorted = [...timings].sort((a, b) => a - b);\n const sum = sorted.reduce((a, b) => a + b, 0);\n const mean = sum / sorted.length;\n const variance =\n sorted.reduce((acc, t) => acc + (t - mean) ** 2, 0) / sorted.length;\n\n const retry = retryData.get(nodeId) ?? { retried: 0, total: 1 };\n const error = errorData.get(nodeId) ?? { errors: 0, total: 1 };\n const timeout = timeoutData.get(nodeId) ?? { timedOut: 0, total: 1 };\n\n return {\n nodeId,\n avgDurationMs: mean,\n minDurationMs: sorted[0],\n maxDurationMs: sorted[sorted.length - 1],\n stdDevMs: Math.sqrt(variance),\n samples: sorted.length,\n retryRate: retry.total > 0 ? retry.retried / retry.total : 0,\n timeoutRate: timeout.total > 0 ? timeout.timedOut / timeout.total : 0,\n errorRate: error.total > 0 ? error.errors / error.total : 0,\n percentiles: {\n p50: percentile(sorted, 0.5),\n p90: percentile(sorted, 0.9),\n p95: percentile(sorted, 0.95),\n p99: percentile(sorted, 0.99),\n },\n };\n }\n\n /**\n * Get performance stats for a specific node.\n */\n function getNodePerformance(nodeId: string): NodePerformance | undefined {\n return computePerformance(nodeId);\n }\n\n /**\n * Get heatmap data for an IR.\n */\n function getHeatmap(\n ir: WorkflowIR,\n metric: \"duration\" | \"retryRate\" | \"errorRate\" = \"duration\"\n ): HeatmapData {\n const heat = new Map<string, number>();\n const allNodes = flattenNodes(ir.root.children);\n\n // Compute values for all nodes\n const values: Array<{ id: string; value: number }> = [];\n for (const node of allNodes) {\n // Try node.name first, then node.id\n const nodeId = node.name ?? node.id;\n const perf = computePerformance(nodeId);\n if (perf) {\n let value: number;\n switch (metric) {\n case \"duration\":\n value = perf.avgDurationMs;\n break;\n case \"retryRate\":\n value = perf.retryRate;\n break;\n case \"errorRate\":\n value = perf.errorRate;\n break;\n }\n values.push({ id: node.id, value });\n }\n }\n\n if (values.length === 0) {\n return {\n heat,\n metric,\n stats: { min: 0, max: 0, mean: 0, threshold: 0 },\n };\n }\n\n // Compute statistics\n const vals = values.map((v) => v.value);\n const min = Math.min(...vals);\n const max = Math.max(...vals);\n const mean = vals.reduce((a, b) => a + b, 0) / vals.length;\n const range = max - min || 1;\n\n // Normalize to 0-1 heat values\n for (const { id, value } of values) {\n heat.set(id, (value - min) / range);\n }\n\n return {\n heat,\n metric,\n stats: {\n min,\n max,\n mean,\n threshold: mean + (max - mean) * 0.5, // 50% above mean is \"hot\"\n },\n };\n }\n\n /**\n * Get all performance data.\n */\n function getAllPerformance(): Map<string, NodePerformance> {\n const result = new Map<string, NodePerformance>();\n for (const nodeId of timingData.keys()) {\n const perf = computePerformance(nodeId);\n if (perf) result.set(nodeId, perf);\n }\n return result;\n }\n\n /**\n * Get slowest nodes by average duration.\n */\n function getSlowestNodes(limit = 10): NodePerformance[] {\n const all = getAllPerformance();\n return [...all.values()]\n .sort((a, b) => b.avgDurationMs - a.avgDurationMs)\n .slice(0, limit);\n }\n\n /**\n * Get error-prone nodes by error rate.\n */\n function getErrorProneNodes(limit = 10): NodePerformance[] {\n const all = getAllPerformance();\n return [...all.values()]\n .filter((p) => p.errorRate > 0)\n .sort((a, b) => b.errorRate - a.errorRate)\n .slice(0, limit);\n }\n\n /**\n * Get retry-prone nodes by retry rate.\n */\n function getRetryProneNodes(limit = 10): NodePerformance[] {\n const all = getAllPerformance();\n return [...all.values()]\n .filter((p) => p.retryRate > 0)\n .sort((a, b) => b.retryRate - a.retryRate)\n .slice(0, limit);\n }\n\n /**\n * Export performance data as JSON.\n */\n function exportData(): string {\n return JSON.stringify({\n timingData: Object.fromEntries(timingData),\n retryData: Object.fromEntries(retryData),\n errorData: Object.fromEntries(errorData),\n timeoutData: Object.fromEntries(timeoutData),\n });\n }\n\n /**\n * Import performance data from JSON.\n */\n function importData(json: string): void {\n const data = JSON.parse(json) as {\n timingData?: Record<string, number[]>;\n retryData?: Record<string, { retried: number; total: number }>;\n errorData?: Record<string, { errors: number; total: number }>;\n timeoutData?: Record<string, { timedOut: number; total: number }>;\n };\n\n // Clear existing data\n timingData.clear();\n retryData.clear();\n errorData.clear();\n timeoutData.clear();\n\n // Import timing data\n for (const [k, v] of Object.entries(data.timingData ?? {})) {\n timingData.set(k, v);\n }\n\n // Import retry data\n for (const [k, v] of Object.entries(data.retryData ?? {})) {\n retryData.set(k, v);\n }\n\n // Import error data\n for (const [k, v] of Object.entries(data.errorData ?? {})) {\n errorData.set(k, v);\n }\n\n // Import timeout data\n for (const [k, v] of Object.entries(data.timeoutData ?? {})) {\n timeoutData.set(k, v);\n }\n }\n\n /**\n * Clear all collected data.\n */\n function clear(): void {\n timingData.clear();\n retryData.clear();\n errorData.clear();\n timeoutData.clear();\n currentRunEvents = [];\n }\n\n return {\n addRun,\n addEvent,\n finalizeRun,\n getNodePerformance,\n getHeatmap,\n getSlowestNodes,\n getErrorProneNodes,\n getRetryProneNodes,\n getAllPerformance,\n exportData,\n importData,\n clear,\n };\n}\n","/**\n * Mermaid Diagram Renderer\n *\n * Renders the workflow IR as a Mermaid flowchart diagram.\n * Supports sequential flows, parallel (subgraph), and race patterns.\n */\n\nimport type {\n FlowNode,\n ParallelNode,\n RaceNode,\n DecisionNode,\n Renderer,\n RenderOptions,\n StepNode,\n StepState,\n WorkflowIR,\n EnhancedRenderOptions,\n HeatLevel,\n WorkflowHooks,\n} from \"../types\";\nimport { isParallelNode, isRaceNode, isStepNode, isDecisionNode } from \"../types\";\nimport { formatDuration } from \"../utils/timing\";\nimport { getHeatLevel } from \"../performance-analyzer\";\n\n// =============================================================================\n// Mermaid Style Definitions\n// =============================================================================\n\n/**\n * Get Mermaid class definition for step states.\n * Colors inspired by AWS Step Functions and XState visualizers for professional appearance.\n */\nfunction getStyleDefinitions(): string[] {\n return [\n // Pending - light gray, subtle\n \" classDef pending fill:#f3f4f6,stroke:#9ca3af,stroke-width:2px,color:#374151\",\n // Running - amber/yellow, indicates active execution\n \" classDef running fill:#fef3c7,stroke:#f59e0b,stroke-width:3px,color:#92400e\",\n // Success - green, clear positive indicator\n \" classDef success fill:#d1fae5,stroke:#10b981,stroke-width:3px,color:#065f46\",\n // Error - red, clear negative indicator\n \" classDef error fill:#fee2e2,stroke:#ef4444,stroke-width:3px,color:#991b1b\",\n // Aborted - gray, indicates cancellation\n \" classDef aborted fill:#f3f4f6,stroke:#6b7280,stroke-width:2px,color:#4b5563,stroke-dasharray: 5 5\",\n // Cached - blue, indicates cache hit\n \" classDef cached fill:#dbeafe,stroke:#3b82f6,stroke-width:3px,color:#1e40af\",\n // Skipped - light gray with dashed border\n \" classDef skipped fill:#f9fafb,stroke:#d1d5db,stroke-width:2px,color:#6b7280,stroke-dasharray: 5 5\",\n ];\n}\n\n/**\n * Get Mermaid class definitions for heatmap visualization.\n */\nfunction getHeatmapStyleDefinitions(): string[] {\n return [\n // Heatmap colors - cold to hot\n \" classDef heat_cold fill:#dbeafe,stroke:#3b82f6,stroke-width:2px,color:#1e40af\",\n \" classDef heat_cool fill:#ccfbf1,stroke:#14b8a6,stroke-width:2px,color:#0f766e\",\n \" classDef heat_neutral fill:#f3f4f6,stroke:#6b7280,stroke-width:2px,color:#374151\",\n \" classDef heat_warm fill:#fef3c7,stroke:#f59e0b,stroke-width:2px,color:#92400e\",\n \" classDef heat_hot fill:#fed7aa,stroke:#f97316,stroke-width:3px,color:#c2410c\",\n \" classDef heat_critical fill:#fecaca,stroke:#ef4444,stroke-width:3px,color:#b91c1c\",\n ];\n}\n\n/**\n * Get the Mermaid class name for a heat level.\n */\nfunction getHeatClass(level: HeatLevel): string {\n return `heat_${level}`;\n}\n\n/**\n * Get the Mermaid class name for a step state.\n */\nfunction getStateClass(state: StepState): string {\n return state;\n}\n\n/**\n * Get Mermaid class definitions for hook visualization.\n */\nfunction getHookStyleDefinitions(): string[] {\n return [\n // Hook styles - gear icon aesthetic\n \" classDef hook_success fill:#e0f2fe,stroke:#0284c7,stroke-width:2px,color:#0c4a6e\",\n \" classDef hook_error fill:#fef2f2,stroke:#dc2626,stroke-width:2px,color:#7f1d1d\",\n ];\n}\n\n/**\n * Render hooks as nodes before the workflow starts.\n * Returns the ID of the last hook node (to connect to workflow start).\n */\nfunction renderHooks(\n hooks: WorkflowHooks,\n lines: string[],\n options: RenderOptions\n): { lastHookId: string | undefined } {\n let lastHookId: string | undefined;\n\n // Render shouldRun hook\n if (hooks.shouldRun) {\n const hookId = \"hook_shouldRun\";\n const state = hooks.shouldRun.state === \"success\" ? \"hook_success\" : \"hook_error\";\n const icon = hooks.shouldRun.state === \"success\" ? \"⚙\" : \"⚠\";\n const timing = options.showTimings && hooks.shouldRun.durationMs !== undefined\n ? ` ${formatDuration(hooks.shouldRun.durationMs)}`\n : \"\";\n const context = hooks.shouldRun.context?.skipped\n ? \"\\\\nskipped workflow\"\n : hooks.shouldRun.context?.result === true\n ? \"\\\\nproceed\"\n : \"\";\n\n lines.push(` ${hookId}[[\"${icon} shouldRun${context}${timing}\"]]:::${state}`);\n lastHookId = hookId;\n }\n\n // Render onBeforeStart hook\n if (hooks.onBeforeStart) {\n const hookId = \"hook_beforeStart\";\n const state = hooks.onBeforeStart.state === \"success\" ? \"hook_success\" : \"hook_error\";\n const icon = hooks.onBeforeStart.state === \"success\" ? \"⚙\" : \"⚠\";\n const timing = options.showTimings && hooks.onBeforeStart.durationMs !== undefined\n ? ` ${formatDuration(hooks.onBeforeStart.durationMs)}`\n : \"\";\n const context = hooks.onBeforeStart.context?.skipped\n ? \"\\\\nskipped workflow\"\n : \"\";\n\n lines.push(` ${hookId}[[\"${icon} onBeforeStart${context}${timing}\"]]:::${state}`);\n\n // Connect from previous hook if exists\n if (lastHookId) {\n lines.push(` ${lastHookId} --> ${hookId}`);\n }\n lastHookId = hookId;\n }\n\n return { lastHookId };\n}\n\n// =============================================================================\n// Node ID Generation\n// =============================================================================\n\nlet nodeCounter = 0;\n\nfunction generateNodeId(prefix: string = \"node\"): string {\n return `${prefix}_${++nodeCounter}`;\n}\n\nfunction resetNodeCounter(): void {\n nodeCounter = 0;\n}\n\n// =============================================================================\n// Mermaid Text Escaping\n// =============================================================================\n\n/**\n * Escape text for use in Mermaid diagrams.\n * Removes characters that break Mermaid parsing.\n * \n * Characters removed:\n * - {}[]() - Brackets and parentheses break parsing in labels\n * - <> - Angle brackets can cause issues\n * - \" - Double quotes replaced with single quotes\n * \n * @param text - Text to escape\n * @returns Escaped text safe for Mermaid\n */\nfunction escapeMermaidText(text: string): string {\n return text\n .replace(/[{}[\\]()]/g, \"\") // Remove brackets and parentheses (they break parsing)\n .replace(/[<>]/g, \"\") // Remove angle brackets\n .replace(/\"/g, \"'\") // Replace double quotes with single\n .trim();\n}\n\n/**\n * Escape text for use in Mermaid subgraph names.\n * Subgraph names in brackets need special handling.\n * \n * @param text - Text to escape for subgraph name\n * @returns Escaped text safe for subgraph names\n */\nfunction escapeSubgraphName(text: string): string {\n return escapeMermaidText(text)\n .replace(/[[\\]]/g, \"\"); // Also remove brackets from subgraph names\n}\n\n// =============================================================================\n// Mermaid Renderer\n// =============================================================================\n\n/**\n * Create the Mermaid diagram renderer.\n */\nexport function mermaidRenderer(): Renderer {\n return {\n name: \"mermaid\",\n supportsLive: false,\n\n render(ir: WorkflowIR, options: RenderOptions): string {\n resetNodeCounter();\n const lines: string[] = [];\n\n // Check for enhanced options (heatmap)\n const enhanced = options as EnhancedRenderOptions;\n\n // Diagram header\n lines.push(\"flowchart TD\");\n\n // Render hooks first (if any)\n let hookExitId: string | undefined;\n if (ir.hooks) {\n const hookResult = renderHooks(ir.hooks, lines, options);\n hookExitId = hookResult.lastHookId;\n }\n\n // Start node - more visually distinctive\n const startId = \"start\";\n lines.push(` ${startId}((\"▶ Start\"))`);\n\n // Connect hooks to start node\n if (hookExitId) {\n lines.push(` ${hookExitId} --> ${startId}`);\n }\n\n // Track the last node for connections\n let prevNodeId = startId;\n\n // Render children (passing hooks for onAfterStep annotations)\n for (const child of ir.root.children) {\n const result = renderNode(child, options, lines, enhanced, ir.hooks);\n lines.push(` ${prevNodeId} --> ${result.entryId}`);\n prevNodeId = result.exitId;\n }\n\n // End node (if workflow completed) - more visually distinctive\n if (ir.root.state === \"success\" || ir.root.state === \"error\") {\n const endId = \"finish\";\n const endIcon = ir.root.state === \"success\" ? \"✓\" : \"✗\";\n const endLabel = ir.root.state === \"success\" ? \"Done\" : \"Failed\";\n const endShape = `((\"${endIcon} ${endLabel}\"))`;\n const endClass =\n ir.root.state === \"success\" ? \":::success\" : \":::error\";\n lines.push(` ${endId}${endShape}${endClass}`);\n lines.push(` ${prevNodeId} --> ${endId}`);\n }\n\n // Add style definitions\n lines.push(\"\");\n lines.push(...getStyleDefinitions());\n\n // Add heatmap styles if enabled\n if (enhanced.showHeatmap) {\n lines.push(...getHeatmapStyleDefinitions());\n }\n\n // Add hook styles if hooks were rendered\n if (ir.hooks) {\n lines.push(...getHookStyleDefinitions());\n }\n\n return lines.join(\"\\n\");\n },\n };\n}\n\n/**\n * Render result with entry and exit node IDs.\n */\ninterface RenderResult {\n entryId: string;\n exitId: string;\n}\n\n/**\n * Render a node and return its entry/exit IDs.\n */\nfunction renderNode(\n node: FlowNode,\n options: RenderOptions,\n lines: string[],\n enhanced?: EnhancedRenderOptions,\n hooks?: WorkflowHooks\n): RenderResult {\n if (isStepNode(node)) {\n return renderStepNode(node, options, lines, enhanced, hooks);\n } else if (isParallelNode(node)) {\n return renderParallelNode(node, options, lines, enhanced, hooks);\n } else if (isRaceNode(node)) {\n return renderRaceNode(node, options, lines, enhanced, hooks);\n } else if (isDecisionNode(node)) {\n return renderDecisionNode(node, options, lines, enhanced, hooks);\n }\n\n // Fallback for sequence or unknown nodes\n const id = generateNodeId(\"unknown\");\n lines.push(` ${id}[Unknown Node]`);\n return { entryId: id, exitId: id };\n}\n\n/**\n * Render a step node.\n */\nfunction renderStepNode(\n node: StepNode,\n options: RenderOptions,\n lines: string[],\n enhanced?: EnhancedRenderOptions,\n hooks?: WorkflowHooks\n): RenderResult {\n const id = node.key\n ? `step_${node.key.replace(/[^a-zA-Z0-9]/g, \"_\")}`\n : generateNodeId(\"step\");\n\n const label = escapeMermaidText(node.name ?? node.key ?? \"Step\");\n\n // Format timing - use space instead of parentheses to avoid Mermaid parse errors\n const timing =\n options.showTimings && node.durationMs !== undefined\n ? ` ${formatDuration(node.durationMs)}`\n : \"\";\n\n // Add visual indicators based on state (like XState/AWS Step Functions)\n let stateIcon = \"\";\n switch (node.state) {\n case \"success\":\n stateIcon = \"✓ \";\n break;\n case \"error\":\n stateIcon = \"✗ \";\n break;\n case \"cached\":\n stateIcon = \"💾 \";\n break;\n case \"running\":\n stateIcon = \"⏳ \";\n break;\n case \"skipped\":\n stateIcon = \"⊘ \";\n break;\n }\n\n // Add input/output info if available\n // Use newlines for multi-line labels, but escape special characters\n let ioInfo = \"\";\n if (node.input !== undefined) {\n const inputStr = typeof node.input === \"string\"\n ? escapeMermaidText(node.input)\n : escapeMermaidText(JSON.stringify(node.input).slice(0, 20));\n ioInfo += `\\\\nin: ${inputStr}`;\n }\n if (node.output !== undefined && node.state === \"success\") {\n const outputStr = typeof node.output === \"string\"\n ? escapeMermaidText(node.output)\n : escapeMermaidText(JSON.stringify(node.output).slice(0, 20));\n ioInfo += `\\\\nout: ${outputStr}`;\n }\n\n // Add retry/timeout indicators with icons\n let retryInfo = \"\";\n if (node.retryCount !== undefined && node.retryCount > 0) {\n retryInfo += `\\\\n↻ ${node.retryCount} retr${node.retryCount === 1 ? \"y\" : \"ies\"}`;\n }\n if (node.timedOut) {\n const timeoutStr = node.timeoutMs !== undefined ? `${node.timeoutMs}ms` : \"\";\n retryInfo += `\\\\n⏱ timeout ${timeoutStr}`;\n }\n\n // Add onAfterStep hook info if present\n let hookInfo = \"\";\n if (hooks && node.key && hooks.onAfterStep.has(node.key)) {\n const hookExec = hooks.onAfterStep.get(node.key)!;\n const hookIcon = hookExec.state === \"success\" ? \"⚙\" : \"⚠\";\n const hookTiming = options.showTimings && hookExec.durationMs !== undefined\n ? ` ${formatDuration(hookExec.durationMs)}`\n : \"\";\n hookInfo = `\\\\n${hookIcon} hook${hookTiming}`;\n }\n\n // Combine all label parts with icon\n const escapedLabel = (stateIcon + label + ioInfo + retryInfo + hookInfo + timing).trim();\n\n // Determine class: use heatmap if enabled and data available, otherwise use state\n let nodeClass: string;\n const nodeId = node.name ?? node.id;\n const heat = enhanced?.showHeatmap && enhanced.heatmapData\n ? enhanced.heatmapData.heat.get(node.id) ?? enhanced.heatmapData.heat.get(nodeId)\n : undefined;\n\n if (heat !== undefined) {\n const level = getHeatLevel(heat);\n nodeClass = getHeatClass(level);\n } else {\n nodeClass = getStateClass(node.state);\n }\n\n // Use different shapes based on state (like AWS Step Functions)\n let shape: string;\n switch (node.state) {\n case \"error\":\n // Hexagon for errors (more distinctive)\n shape = `{{${escapedLabel}}}`;\n break;\n case \"cached\":\n // Rounded rectangle with double border for cached\n shape = `[(${escapedLabel})]`;\n break;\n case \"skipped\":\n // Dashed border for skipped\n shape = `[${escapedLabel}]:::skipped`;\n break;\n default:\n // Standard rectangle for normal steps\n shape = `[${escapedLabel}]`;\n }\n\n lines.push(` ${id}${shape}:::${nodeClass}`);\n\n return { entryId: id, exitId: id };\n}\n\n/**\n * Render a parallel node as a subgraph with fork/join.\n */\nfunction renderParallelNode(\n node: ParallelNode,\n options: RenderOptions,\n lines: string[],\n enhanced?: EnhancedRenderOptions,\n hooks?: WorkflowHooks\n): RenderResult {\n const subgraphId = generateNodeId(\"parallel\");\n const forkId = `${subgraphId}_fork`;\n const joinId = `${subgraphId}_join`;\n const name = escapeSubgraphName(node.name ?? \"Parallel\");\n const modeLabel = node.mode === \"allSettled\" ? \" (allSettled)\" : \"\";\n\n // If no children, render as a simple step-like node with note\n if (node.children.length === 0) {\n const id = subgraphId;\n const label = escapeMermaidText(`${name}${modeLabel}`);\n const note = \"operations not individually tracked\";\n const timing = options.showTimings && node.durationMs !== undefined\n ? ` ${formatDuration(node.durationMs)}`\n : \"\";\n \n // Use a rounded rectangle to indicate it's a parallel operation\n lines.push(` ${id}[${label}${timing}\\\\n${note}]:::${getStateClass(node.state)}`);\n return { entryId: id, exitId: id };\n }\n\n // Subgraph for parallel block with proper visual hierarchy\n lines.push(` subgraph ${subgraphId}[\"${name}${modeLabel}\"]`);\n lines.push(` direction TB`);\n\n // Fork node (diamond) - more visually distinct\n lines.push(` ${forkId}{\"⚡ Fork\"}`);\n\n // Child branches - render in parallel columns\n const childExitIds: string[] = [];\n for (const child of node.children) {\n const result = renderNode(child, options, lines, enhanced, hooks);\n lines.push(` ${forkId} --> ${result.entryId}`);\n childExitIds.push(result.exitId);\n }\n\n // Join node (diamond) - visually distinct\n lines.push(` ${joinId}{\"✓ Join\"}`);\n for (const exitId of childExitIds) {\n lines.push(` ${exitId} --> ${joinId}`);\n }\n\n lines.push(` end`);\n\n // Apply state styling to subgraph\n const stateClass = getStateClass(node.state);\n lines.push(` class ${subgraphId} ${stateClass}`);\n\n return { entryId: forkId, exitId: joinId };\n}\n\n/**\n * Render a race node as a subgraph with racing indicator.\n */\nfunction renderRaceNode(\n node: RaceNode,\n options: RenderOptions,\n lines: string[],\n enhanced?: EnhancedRenderOptions,\n hooks?: WorkflowHooks\n): RenderResult {\n const subgraphId = generateNodeId(\"race\");\n const startId = `${subgraphId}_start`;\n const endId = `${subgraphId}_end`;\n const name = escapeSubgraphName(node.name ?? \"Race\");\n\n // If no children, render as a simple step-like node with note\n if (node.children.length === 0) {\n const id = subgraphId;\n const label = escapeMermaidText(name);\n const note = \"operations not individually tracked\";\n const timing = options.showTimings && node.durationMs !== undefined\n ? ` ${formatDuration(node.durationMs)}`\n : \"\";\n \n lines.push(` ${id}[⚡ ${label}${timing}\\\\n${note}]:::${getStateClass(node.state)}`);\n return { entryId: id, exitId: id };\n }\n\n // Subgraph for race block - escape name and emoji is safe in quoted strings\n lines.push(` subgraph ${subgraphId}[\"⚡ ${name}\"]`);\n lines.push(` direction TB`);\n\n // Start node - use a more distinctive shape\n lines.push(` ${startId}((\"🏁 Start\"))`);\n\n // Child branches\n const childExitIds: Array<{ exitId: string; isWinner: boolean }> = [];\n let winnerExitId: string | undefined;\n\n for (const child of node.children) {\n const result = renderNode(child, options, lines, enhanced, hooks);\n const isWinner = isStepNode(child) && node.winnerId === child.id;\n lines.push(` ${startId} --> ${result.entryId}`);\n\n if (isWinner) {\n winnerExitId = result.exitId;\n }\n childExitIds.push({ exitId: result.exitId, isWinner });\n }\n\n // End node - more distinctive\n lines.push(` ${endId}((\"✓ First\"))`);\n \n // Connect winner with thick line, others with dashed (cancelled)\n for (const { exitId, isWinner } of childExitIds) {\n if (isWinner && winnerExitId) {\n lines.push(` ${exitId} ==>|🏆 Winner| ${endId}`);\n } else if (node.winnerId) {\n // Non-winner: show as cancelled\n lines.push(` ${exitId} -. cancelled .-> ${endId}`);\n } else {\n // No winner determined, normal connection\n lines.push(` ${exitId} --> ${endId}`);\n }\n }\n\n lines.push(` end`);\n\n const stateClass = getStateClass(node.state);\n lines.push(` class ${subgraphId} ${stateClass}`);\n\n return { entryId: startId, exitId: endId };\n}\n\n/**\n * Render a decision node as a diamond with branches.\n */\nfunction renderDecisionNode(\n node: DecisionNode,\n options: RenderOptions,\n lines: string[],\n enhanced?: EnhancedRenderOptions,\n hooks?: WorkflowHooks\n): RenderResult {\n const decisionId = node.key\n ? `decision_${node.key.replace(/[^a-zA-Z0-9]/g, \"_\")}`\n : generateNodeId(\"decision\");\n\n // Escape condition and decision value - remove characters that break Mermaid\n const condition = escapeMermaidText(node.condition ?? \"condition\");\n const decisionValue = node.decisionValue !== undefined\n ? ` = ${escapeMermaidText(String(node.decisionValue)).slice(0, 30)}`\n : \"\";\n\n // Decision diamond - ensure no invalid characters\n const decisionLabel = `${condition}${decisionValue}`.trim();\n lines.push(` ${decisionId}{${decisionLabel}}`);\n\n // Render branches\n const branchExitIds: string[] = [];\n let takenBranchExitId: string | undefined;\n\n for (const branch of node.branches) {\n const branchId = `${decisionId}_${branch.label.replace(/[^a-zA-Z0-9]/g, \"_\")}`;\n // Escape branch label - remove parentheses and other special chars\n const branchLabelText = escapeMermaidText(branch.label);\n const branchLabel = branch.taken\n ? `${branchLabelText} ✓`\n : `${branchLabelText} skipped`;\n const branchClass = branch.taken ? \":::success\" : \":::skipped\";\n\n // Branch label node\n lines.push(` ${branchId}[${branchLabel}]${branchClass}`);\n\n // Connect decision to branch\n // Mermaid edge labels must be simple text - escape special characters\n // Also remove pipe character as it's used for edge label syntax\n const edgeLabel = branch.condition \n ? `|${escapeMermaidText(branch.condition).replace(/\\|/g, \"\")}|` \n : \"\";\n lines.push(` ${decisionId} -->${edgeLabel} ${branchId}`);\n\n // Render children of this branch\n if (branch.children.length > 0) {\n let prevId = branchId;\n for (const child of branch.children) {\n const result = renderNode(child, options, lines, enhanced, hooks);\n lines.push(` ${prevId} --> ${result.entryId}`);\n prevId = result.exitId;\n }\n branchExitIds.push(prevId);\n if (branch.taken) {\n takenBranchExitId = prevId;\n }\n } else {\n branchExitIds.push(branchId);\n if (branch.taken) {\n takenBranchExitId = branchId;\n }\n }\n }\n\n // Join point (if we have a taken branch)\n if (takenBranchExitId) {\n return { entryId: decisionId, exitId: takenBranchExitId };\n }\n\n // If no branch was taken, return decision as exit\n return { entryId: decisionId, exitId: decisionId };\n}\n\nexport { mermaidRenderer as default };\n","/**\n * HTML Visualizer Styles\n *\n * CSS styles for the interactive HTML workflow visualizer.\n * Supports light, dark, and auto (system preference) themes.\n */\n\n/**\n * Generate CSS styles for the HTML visualizer.\n */\nexport function generateStyles(theme: \"light\" | \"dark\" | \"auto\"): string {\n const lightColors = {\n bg: \"#ffffff\",\n bgSecondary: \"#f8f9fa\",\n text: \"#212529\",\n textMuted: \"#6c757d\",\n border: \"#dee2e6\",\n primary: \"#0d6efd\",\n success: \"#198754\",\n error: \"#dc3545\",\n warning: \"#ffc107\",\n info: \"#0dcaf0\",\n muted: \"#6c757d\",\n // Node colors\n nodePending: \"#e9ecef\",\n nodeRunning: \"#fff3cd\",\n nodeSuccess: \"#d1e7dd\",\n nodeError: \"#f8d7da\",\n nodeAborted: \"#e9ecef\",\n nodeCached: \"#cfe2ff\",\n nodeSkipped: \"#f8f9fa\",\n // Heatmap colors\n heatCold: \"#0d6efd\",\n heatCool: \"#0dcaf0\",\n heatNeutral: \"#6c757d\",\n heatWarm: \"#ffc107\",\n heatHot: \"#fd7e14\",\n heatCritical: \"#dc3545\",\n };\n\n const darkColors = {\n bg: \"#212529\",\n bgSecondary: \"#343a40\",\n text: \"#f8f9fa\",\n textMuted: \"#adb5bd\",\n border: \"#495057\",\n primary: \"#0d6efd\",\n success: \"#198754\",\n error: \"#dc3545\",\n warning: \"#ffc107\",\n info: \"#0dcaf0\",\n muted: \"#6c757d\",\n // Node colors\n nodePending: \"#495057\",\n nodeRunning: \"#664d03\",\n nodeSuccess: \"#0f5132\",\n nodeError: \"#842029\",\n nodeAborted: \"#495057\",\n nodeCached: \"#084298\",\n nodeSkipped: \"#343a40\",\n // Heatmap colors\n heatCold: \"#0d6efd\",\n heatCool: \"#0dcaf0\",\n heatNeutral: \"#6c757d\",\n heatWarm: \"#ffc107\",\n heatHot: \"#fd7e14\",\n heatCritical: \"#dc3545\",\n };\n\n const generateThemeVars = (colors: typeof lightColors) => `\n --bg: ${colors.bg};\n --bg-secondary: ${colors.bgSecondary};\n --text: ${colors.text};\n --text-muted: ${colors.textMuted};\n --border: ${colors.border};\n --primary: ${colors.primary};\n --success: ${colors.success};\n --error: ${colors.error};\n --warning: ${colors.warning};\n --info: ${colors.info};\n --muted: ${colors.muted};\n --node-pending: ${colors.nodePending};\n --node-running: ${colors.nodeRunning};\n --node-success: ${colors.nodeSuccess};\n --node-error: ${colors.nodeError};\n --node-aborted: ${colors.nodeAborted};\n --node-cached: ${colors.nodeCached};\n --node-skipped: ${colors.nodeSkipped};\n --heat-cold: ${colors.heatCold};\n --heat-cool: ${colors.heatCool};\n --heat-neutral: ${colors.heatNeutral};\n --heat-warm: ${colors.heatWarm};\n --heat-hot: ${colors.heatHot};\n --heat-critical: ${colors.heatCritical};\n `;\n\n let themeCSS: string;\n\n if (theme === \"auto\") {\n themeCSS = `\n :root {\n ${generateThemeVars(lightColors)}\n }\n @media (prefers-color-scheme: dark) {\n :root {\n ${generateThemeVars(darkColors)}\n }\n }\n `;\n } else if (theme === \"dark\") {\n themeCSS = `\n :root {\n ${generateThemeVars(darkColors)}\n }\n `;\n } else {\n themeCSS = `\n :root {\n ${generateThemeVars(lightColors)}\n }\n `;\n }\n\n return `\n${themeCSS}\n\n* {\n box-sizing: border-box;\n margin: 0;\n padding: 0;\n}\n\nbody {\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, \"Helvetica Neue\", Arial, sans-serif;\n background-color: var(--bg);\n color: var(--text);\n line-height: 1.5;\n}\n\n.workflow-visualizer {\n display: flex;\n flex-direction: column;\n height: 100vh;\n overflow: hidden;\n}\n\n/* Header */\n.wv-header {\n display: flex;\n align-items: center;\n justify-content: space-between;\n padding: 12px 20px;\n background-color: var(--bg-secondary);\n border-bottom: 1px solid var(--border);\n}\n\n.wv-header h1 {\n font-size: 1.25rem;\n font-weight: 600;\n}\n\n.wv-controls {\n display: flex;\n gap: 8px;\n align-items: center;\n}\n\n.wv-btn {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n padding: 6px 12px;\n font-size: 0.875rem;\n font-weight: 500;\n border: 1px solid var(--border);\n border-radius: 6px;\n background-color: var(--bg);\n color: var(--text);\n cursor: pointer;\n transition: all 0.15s ease;\n}\n\n.wv-btn:hover {\n background-color: var(--bg-secondary);\n}\n\n.wv-btn:disabled {\n opacity: 0.5;\n cursor: not-allowed;\n}\n\n.wv-btn--primary {\n background-color: var(--primary);\n border-color: var(--primary);\n color: white;\n}\n\n.wv-btn--primary:hover {\n opacity: 0.9;\n}\n\n.wv-btn--icon {\n padding: 6px;\n min-width: 32px;\n}\n\n/* Main content area */\n.wv-main {\n display: flex;\n flex: 1;\n overflow: hidden;\n}\n\n/* Diagram container */\n.wv-diagram {\n flex: 1;\n position: relative;\n overflow: hidden;\n background-color: var(--bg);\n background-image:\n radial-gradient(circle, var(--border) 1px, transparent 1px);\n background-size: 20px 20px;\n}\n\n.wv-diagram svg {\n width: 100%;\n height: 100%;\n}\n\n/* SVG Node styles */\n.wv-node {\n cursor: pointer;\n transition: transform 0.15s ease;\n}\n\n.wv-node:hover {\n transform: scale(1.02);\n}\n\n.wv-node rect,\n.wv-node circle {\n stroke: var(--border);\n stroke-width: 2;\n transition: all 0.2s ease;\n}\n\n.wv-node--pending rect { fill: var(--node-pending); }\n.wv-node--running rect { fill: var(--node-running); stroke: var(--warning); }\n.wv-node--success rect { fill: var(--node-success); stroke: var(--success); }\n.wv-node--error rect { fill: var(--node-error); stroke: var(--error); }\n.wv-node--aborted rect { fill: var(--node-aborted); }\n.wv-node--cached rect { fill: var(--node-cached); stroke: var(--info); }\n.wv-node--skipped rect { fill: var(--node-skipped); opacity: 0.6; }\n\n.wv-node--selected rect {\n stroke: var(--primary);\n stroke-width: 3;\n}\n\n.wv-node text {\n fill: var(--text);\n font-size: 12px;\n font-weight: 500;\n dominant-baseline: middle;\n text-anchor: middle;\n}\n\n.wv-node .wv-node-timing {\n fill: var(--text-muted);\n font-size: 10px;\n}\n\n/* Edge styles */\n.wv-edge {\n fill: none;\n stroke: var(--border);\n stroke-width: 2;\n}\n\n.wv-edge--active {\n stroke: var(--success);\n stroke-width: 2.5;\n}\n\n.wv-edge-arrow {\n fill: var(--border);\n}\n\n/* Parallel/Race container */\n.wv-container {\n fill: transparent;\n stroke: var(--border);\n stroke-dasharray: 5 3;\n}\n\n.wv-container--parallel { stroke: var(--info); }\n.wv-container--race { stroke: var(--warning); }\n\n.wv-container-label {\n fill: var(--text-muted);\n font-size: 10px;\n font-weight: 600;\n text-transform: uppercase;\n letter-spacing: 0.5px;\n}\n\n/* Heatmap overlay */\n.wv-node--heat-cold rect { fill: var(--heat-cold) !important; opacity: 0.7; }\n.wv-node--heat-cool rect { fill: var(--heat-cool) !important; opacity: 0.7; }\n.wv-node--heat-neutral rect { fill: var(--heat-neutral) !important; opacity: 0.7; }\n.wv-node--heat-warm rect { fill: var(--heat-warm) !important; opacity: 0.7; }\n.wv-node--heat-hot rect { fill: var(--heat-hot) !important; opacity: 0.7; }\n.wv-node--heat-critical rect { fill: var(--heat-critical) !important; opacity: 0.85; }\n\n/* Timeline scrubber */\n.wv-timeline {\n height: 80px;\n padding: 12px 20px;\n background-color: var(--bg-secondary);\n border-top: 1px solid var(--border);\n}\n\n.wv-timeline-track {\n position: relative;\n height: 24px;\n background-color: var(--bg);\n border-radius: 4px;\n overflow: hidden;\n}\n\n.wv-timeline-progress {\n position: absolute;\n top: 0;\n left: 0;\n height: 100%;\n background-color: var(--primary);\n opacity: 0.3;\n transition: width 0.1s ease;\n}\n\n.wv-timeline-marker {\n position: absolute;\n top: 0;\n width: 3px;\n height: 100%;\n background-color: var(--primary);\n cursor: ew-resize;\n}\n\n.wv-timeline-events {\n display: flex;\n gap: 2px;\n height: 100%;\n align-items: center;\n padding: 4px;\n}\n\n.wv-timeline-event {\n width: 4px;\n height: 16px;\n border-radius: 2px;\n background-color: var(--muted);\n}\n\n.wv-timeline-event--success { background-color: var(--success); }\n.wv-timeline-event--error { background-color: var(--error); }\n.wv-timeline-event--running { background-color: var(--warning); }\n\n.wv-timeline-controls {\n display: flex;\n gap: 8px;\n margin-top: 8px;\n align-items: center;\n}\n\n.wv-timeline-time {\n font-size: 0.75rem;\n color: var(--text-muted);\n font-family: monospace;\n}\n\n/* Inspector panel */\n.wv-inspector {\n width: 320px;\n background-color: var(--bg-secondary);\n border-left: 1px solid var(--border);\n overflow-y: auto;\n transition: width 0.2s ease;\n}\n\n.wv-inspector--collapsed {\n width: 0;\n}\n\n.wv-inspector-header {\n display: flex;\n align-items: center;\n justify-content: space-between;\n padding: 12px 16px;\n border-bottom: 1px solid var(--border);\n}\n\n.wv-inspector-header h2 {\n font-size: 0.875rem;\n font-weight: 600;\n}\n\n.wv-inspector-content {\n padding: 16px;\n}\n\n.wv-inspector-section {\n margin-bottom: 20px;\n}\n\n.wv-inspector-section h3 {\n font-size: 0.75rem;\n font-weight: 600;\n text-transform: uppercase;\n letter-spacing: 0.5px;\n color: var(--text-muted);\n margin-bottom: 8px;\n}\n\n.wv-inspector-row {\n display: flex;\n justify-content: space-between;\n padding: 4px 0;\n font-size: 0.875rem;\n}\n\n.wv-inspector-label {\n color: var(--text-muted);\n}\n\n.wv-inspector-value {\n font-weight: 500;\n font-family: monospace;\n}\n\n.wv-inspector-value--success { color: var(--success); }\n.wv-inspector-value--error { color: var(--error); }\n.wv-inspector-value--running { color: var(--warning); }\n\n/* Status badge */\n.wv-badge {\n display: inline-flex;\n align-items: center;\n padding: 2px 8px;\n font-size: 0.75rem;\n font-weight: 500;\n border-radius: 4px;\n text-transform: capitalize;\n}\n\n.wv-badge--pending { background-color: var(--node-pending); }\n.wv-badge--running { background-color: var(--node-running); color: #664d03; }\n.wv-badge--success { background-color: var(--node-success); color: #0f5132; }\n.wv-badge--error { background-color: var(--node-error); color: #842029; }\n.wv-badge--cached { background-color: var(--node-cached); color: #084298; }\n\n/* Live indicator */\n.wv-live {\n display: flex;\n align-items: center;\n gap: 6px;\n padding: 4px 10px;\n background-color: var(--error);\n color: white;\n font-size: 0.75rem;\n font-weight: 600;\n border-radius: 4px;\n}\n\n.wv-live-dot {\n width: 8px;\n height: 8px;\n background-color: white;\n border-radius: 50%;\n animation: wv-pulse 1.5s infinite;\n}\n\n@keyframes wv-pulse {\n 0%, 100% { opacity: 1; }\n 50% { opacity: 0.5; }\n}\n\n/* Performance stats */\n.wv-perf-bar {\n height: 8px;\n background-color: var(--bg);\n border-radius: 4px;\n overflow: hidden;\n margin-top: 4px;\n}\n\n.wv-perf-bar-fill {\n height: 100%;\n border-radius: 4px;\n transition: width 0.3s ease;\n}\n\n.wv-perf-bar-fill--cold { background-color: var(--heat-cold); }\n.wv-perf-bar-fill--cool { background-color: var(--heat-cool); }\n.wv-perf-bar-fill--neutral { background-color: var(--heat-neutral); }\n.wv-perf-bar-fill--warm { background-color: var(--heat-warm); }\n.wv-perf-bar-fill--hot { background-color: var(--heat-hot); }\n.wv-perf-bar-fill--critical { background-color: var(--heat-critical); }\n\n/* Tooltip */\n.wv-tooltip {\n position: fixed;\n padding: 8px 12px;\n background-color: var(--text);\n color: var(--bg);\n font-size: 0.75rem;\n border-radius: 4px;\n pointer-events: none;\n z-index: 1000;\n max-width: 300px;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);\n}\n\n/* Loading state */\n.wv-loading {\n display: flex;\n align-items: center;\n justify-content: center;\n height: 100%;\n color: var(--text-muted);\n}\n\n.wv-spinner {\n width: 24px;\n height: 24px;\n border: 3px solid var(--border);\n border-top-color: var(--primary);\n border-radius: 50%;\n animation: wv-spin 1s linear infinite;\n margin-right: 8px;\n}\n\n@keyframes wv-spin {\n to { transform: rotate(360deg); }\n}\n\n/* Empty state */\n.wv-empty {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n height: 100%;\n color: var(--text-muted);\n text-align: center;\n padding: 40px;\n}\n\n.wv-empty svg {\n width: 64px;\n height: 64px;\n margin-bottom: 16px;\n opacity: 0.5;\n}\n\n/* Responsive */\n@media (max-width: 768px) {\n .wv-inspector {\n position: fixed;\n right: 0;\n top: 0;\n bottom: 0;\n z-index: 100;\n box-shadow: -4px 0 12px rgba(0, 0, 0, 0.1);\n }\n\n .wv-timeline {\n height: auto;\n }\n}\n`;\n}\n","/**\n * HTML Visualizer Client-Side JavaScript\n *\n * Handles interactivity in the browser:\n * - Zoom and pan\n * - Node selection and inspection\n * - Time-travel controls\n * - WebSocket live updates\n * - Heatmap toggle\n */\n\n/**\n * Generate client-side JavaScript for the HTML visualizer.\n */\nexport function generateClientScript(options: {\n wsUrl?: string;\n interactive: boolean;\n timeTravel: boolean;\n heatmap: boolean;\n}): string {\n return `\n(function() {\n 'use strict';\n\n // State\n let selectedNodeId = null;\n let transform = { x: 0, y: 0, scale: 1 };\n let isDragging = false;\n let dragStart = { x: 0, y: 0 };\n let ws = null;\n let snapshots = [];\n let currentSnapshotIndex = -1;\n let isPlaying = false;\n let playbackSpeed = 1;\n let heatmapEnabled = false;\n let heatmapMetric = 'duration';\n\n // DOM elements\n const diagram = document.getElementById('diagram');\n const svg = diagram?.querySelector('svg');\n const inspector = document.getElementById('inspector');\n const timeline = document.getElementById('timeline');\n\n // Initialize\n document.addEventListener('DOMContentLoaded', init);\n\n function init() {\n ${options.interactive ? \"setupInteractivity();\" : \"\"}\n ${options.timeTravel ? \"setupTimeTravel();\" : \"\"}\n ${options.wsUrl ? `setupWebSocket('${options.wsUrl}');` : \"\"}\n ${options.heatmap ? \"setupHeatmap();\" : \"\"}\n setupKeyboardShortcuts();\n }\n\n // Interactivity\n function setupInteractivity() {\n if (!diagram || !svg) return;\n\n // Zoom with mouse wheel\n diagram.addEventListener('wheel', (e) => {\n e.preventDefault();\n const delta = e.deltaY > 0 ? 0.9 : 1.1;\n const rect = diagram.getBoundingClientRect();\n const x = e.clientX - rect.left;\n const y = e.clientY - rect.top;\n\n // Zoom towards cursor position\n transform.scale *= delta;\n transform.scale = Math.max(0.1, Math.min(5, transform.scale));\n transform.x = x - (x - transform.x) * delta;\n transform.y = y - (y - transform.y) * delta;\n\n applyTransform();\n });\n\n // Pan with drag\n diagram.addEventListener('mousedown', (e) => {\n if (e.target === diagram || e.target === svg) {\n isDragging = true;\n dragStart = { x: e.clientX - transform.x, y: e.clientY - transform.y };\n diagram.style.cursor = 'grabbing';\n }\n });\n\n document.addEventListener('mousemove', (e) => {\n if (!isDragging) return;\n transform.x = e.clientX - dragStart.x;\n transform.y = e.clientY - dragStart.y;\n applyTransform();\n });\n\n document.addEventListener('mouseup', () => {\n isDragging = false;\n if (diagram) diagram.style.cursor = 'grab';\n });\n\n // Node selection\n document.querySelectorAll('.wv-node').forEach((node) => {\n node.addEventListener('click', (e) => {\n e.stopPropagation();\n selectNode(node.dataset.nodeId);\n });\n });\n\n // Deselect on background click\n diagram.addEventListener('click', (e) => {\n if (e.target === diagram || e.target === svg) {\n selectNode(null);\n }\n });\n\n // Zoom buttons\n document.getElementById('zoom-in')?.addEventListener('click', () => zoom(1.2));\n document.getElementById('zoom-out')?.addEventListener('click', () => zoom(0.8));\n document.getElementById('zoom-reset')?.addEventListener('click', resetZoom);\n\n // Set initial cursor\n diagram.style.cursor = 'grab';\n }\n\n function applyTransform() {\n if (!svg) return;\n const g = svg.querySelector('g.wv-root');\n if (g) {\n g.setAttribute('transform', 'translate(' + transform.x + ',' + transform.y + ') scale(' + transform.scale + ')');\n }\n }\n\n function zoom(factor) {\n if (!diagram) return;\n const rect = diagram.getBoundingClientRect();\n const centerX = rect.width / 2;\n const centerY = rect.height / 2;\n\n transform.scale *= factor;\n transform.scale = Math.max(0.1, Math.min(5, transform.scale));\n transform.x = centerX - (centerX - transform.x) * factor;\n transform.y = centerY - (centerY - transform.y) * factor;\n\n applyTransform();\n }\n\n function resetZoom() {\n transform = { x: 0, y: 0, scale: 1 };\n applyTransform();\n }\n\n function selectNode(nodeId) {\n // Remove previous selection\n document.querySelectorAll('.wv-node--selected').forEach((n) => {\n n.classList.remove('wv-node--selected');\n });\n\n selectedNodeId = nodeId;\n\n if (nodeId) {\n const node = document.querySelector('[data-node-id=\"' + nodeId + '\"]');\n if (node) {\n node.classList.add('wv-node--selected');\n updateInspector(nodeId);\n }\n } else {\n clearInspector();\n }\n }\n\n function updateInspector(nodeId) {\n const content = document.getElementById('inspector-content');\n if (!content) return;\n\n const nodeData = window.__WORKFLOW_DATA__?.nodes?.[nodeId];\n if (!nodeData) {\n // Clear and add empty message using safe DOM methods\n while (content.firstChild) content.removeChild(content.firstChild);\n const p = document.createElement('p');\n p.className = 'wv-empty';\n p.textContent = 'No data available';\n content.appendChild(p);\n return;\n }\n\n renderInspectorContent(content, nodeData);\n }\n\n function renderInspectorContent(container, node) {\n // Clear container using safe DOM method\n while (container.firstChild) container.removeChild(container.firstChild);\n\n // Node Info section\n const infoSection = createSection('Node Info');\n infoSection.appendChild(createRow('Name', node.name || node.id));\n infoSection.appendChild(createRow('Type', node.type));\n\n // State badge\n const stateRow = document.createElement('div');\n stateRow.className = 'wv-inspector-row';\n const stateLabel = document.createElement('span');\n stateLabel.className = 'wv-inspector-label';\n stateLabel.textContent = 'State';\n const stateBadge = document.createElement('span');\n stateBadge.className = 'wv-badge wv-badge--' + node.state;\n stateBadge.textContent = node.state;\n stateRow.appendChild(stateLabel);\n stateRow.appendChild(stateBadge);\n infoSection.appendChild(stateRow);\n\n if (node.key) {\n infoSection.appendChild(createRow('Key', node.key));\n }\n container.appendChild(infoSection);\n\n // Timing section\n if (node.durationMs !== undefined) {\n const timingSection = createSection('Timing');\n timingSection.appendChild(createRow('Duration', formatDuration(node.durationMs)));\n if (node.startTs) {\n timingSection.appendChild(createRow('Started', new Date(node.startTs).toLocaleTimeString()));\n }\n container.appendChild(timingSection);\n }\n\n // Retries section\n if (node.retryCount !== undefined && node.retryCount > 0) {\n const retrySection = createSection('Retries');\n retrySection.appendChild(createRow('Retry Count', String(node.retryCount)));\n container.appendChild(retrySection);\n }\n\n // Error section\n if (node.error) {\n const errorSection = createSection('Error');\n const pre = document.createElement('pre');\n pre.style.cssText = 'font-size:11px;overflow:auto;max-height:150px;background:var(--bg);padding:8px;border-radius:4px;';\n pre.textContent = String(node.error);\n errorSection.appendChild(pre);\n container.appendChild(errorSection);\n }\n }\n\n function createSection(title) {\n const section = document.createElement('div');\n section.className = 'wv-inspector-section';\n const h3 = document.createElement('h3');\n h3.textContent = title;\n section.appendChild(h3);\n return section;\n }\n\n function createRow(label, value) {\n const row = document.createElement('div');\n row.className = 'wv-inspector-row';\n const labelSpan = document.createElement('span');\n labelSpan.className = 'wv-inspector-label';\n labelSpan.textContent = label;\n const valueSpan = document.createElement('span');\n valueSpan.className = 'wv-inspector-value';\n valueSpan.textContent = value;\n row.appendChild(labelSpan);\n row.appendChild(valueSpan);\n return row;\n }\n\n function clearInspector() {\n const content = document.getElementById('inspector-content');\n if (content) {\n while (content.firstChild) content.removeChild(content.firstChild);\n const p = document.createElement('p');\n p.className = 'wv-empty';\n p.textContent = 'Select a node to inspect';\n content.appendChild(p);\n }\n }\n\n // Time Travel\n function setupTimeTravel() {\n const playBtn = document.getElementById('tt-play');\n const pauseBtn = document.getElementById('tt-pause');\n const prevBtn = document.getElementById('tt-prev');\n const nextBtn = document.getElementById('tt-next');\n const speedSelect = document.getElementById('tt-speed');\n const slider = document.getElementById('tt-slider');\n\n playBtn?.addEventListener('click', play);\n pauseBtn?.addEventListener('click', pause);\n prevBtn?.addEventListener('click', stepBackward);\n nextBtn?.addEventListener('click', stepForward);\n speedSelect?.addEventListener('change', (e) => {\n playbackSpeed = parseFloat(e.target.value);\n });\n slider?.addEventListener('input', (e) => {\n seek(parseInt(e.target.value, 10));\n });\n }\n\n function play() {\n if (snapshots.length === 0) return;\n isPlaying = true;\n updatePlaybackUI();\n playNext();\n }\n\n function pause() {\n isPlaying = false;\n updatePlaybackUI();\n }\n\n function playNext() {\n if (!isPlaying) return;\n if (currentSnapshotIndex < snapshots.length - 1) {\n const current = snapshots[currentSnapshotIndex];\n const next = snapshots[currentSnapshotIndex + 1];\n const delay = (next.timestamp - current.timestamp) / playbackSpeed;\n\n setTimeout(() => {\n stepForward();\n playNext();\n }, Math.max(16, delay));\n } else {\n pause();\n }\n }\n\n function stepForward() {\n if (currentSnapshotIndex < snapshots.length - 1) {\n seek(currentSnapshotIndex + 1);\n }\n }\n\n function stepBackward() {\n if (currentSnapshotIndex > 0) {\n seek(currentSnapshotIndex - 1);\n }\n }\n\n function seek(index) {\n if (index < 0 || index >= snapshots.length) return;\n currentSnapshotIndex = index;\n updateTimelineUI();\n\n // Request IR update from server or use cached\n if (ws && ws.readyState === WebSocket.OPEN) {\n ws.send(JSON.stringify({ type: 'time_travel_seek', payload: { index } }));\n } else if (snapshots[index]?.ir) {\n renderIR(snapshots[index].ir);\n }\n }\n\n function updatePlaybackUI() {\n const playBtn = document.getElementById('tt-play');\n const pauseBtn = document.getElementById('tt-pause');\n\n if (playBtn) playBtn.style.display = isPlaying ? 'none' : 'inline-flex';\n if (pauseBtn) pauseBtn.style.display = isPlaying ? 'inline-flex' : 'none';\n }\n\n function updateTimelineUI() {\n const slider = document.getElementById('tt-slider');\n const timeDisplay = document.getElementById('tt-time');\n\n if (slider) {\n slider.max = String(Math.max(0, snapshots.length - 1));\n slider.value = String(currentSnapshotIndex);\n }\n\n if (timeDisplay) {\n timeDisplay.textContent = (currentSnapshotIndex + 1) + ' / ' + snapshots.length;\n }\n }\n\n // WebSocket\n function setupWebSocket(url) {\n ws = new WebSocket(url);\n\n ws.onopen = () => {\n console.log('[Visualizer] Connected to server');\n showLiveIndicator(true);\n };\n\n ws.onclose = () => {\n console.log('[Visualizer] Disconnected from server');\n showLiveIndicator(false);\n // Attempt reconnect after 2 seconds\n setTimeout(() => setupWebSocket(url), 2000);\n };\n\n ws.onmessage = (event) => {\n try {\n const msg = JSON.parse(event.data);\n handleServerMessage(msg);\n } catch (e) {\n console.error('[Visualizer] Failed to parse message:', e);\n }\n };\n\n ws.onerror = (error) => {\n console.error('[Visualizer] WebSocket error:', error);\n };\n }\n\n function handleServerMessage(msg) {\n switch (msg.type) {\n case 'ir_update':\n renderIR(msg.payload);\n break;\n case 'snapshot':\n snapshots.push(msg.payload);\n currentSnapshotIndex = snapshots.length - 1;\n updateTimelineUI();\n break;\n case 'snapshots_list':\n snapshots = msg.payload;\n currentSnapshotIndex = snapshots.length - 1;\n updateTimelineUI();\n break;\n case 'performance_data':\n window.__PERFORMANCE_DATA__ = msg.payload;\n if (heatmapEnabled) applyHeatmap();\n break;\n case 'workflow_complete':\n showLiveIndicator(false);\n break;\n case 'time_travel_state':\n currentSnapshotIndex = msg.payload.currentIndex;\n isPlaying = msg.payload.isPlaying;\n playbackSpeed = msg.payload.playbackSpeed;\n updateTimelineUI();\n updatePlaybackUI();\n break;\n }\n }\n\n function showLiveIndicator(show) {\n const indicator = document.getElementById('live-indicator');\n if (indicator) {\n indicator.style.display = show ? 'flex' : 'none';\n }\n }\n\n // Heatmap\n function setupHeatmap() {\n const toggle = document.getElementById('heatmap-toggle');\n const metricSelect = document.getElementById('heatmap-metric');\n\n toggle?.addEventListener('click', () => {\n heatmapEnabled = !heatmapEnabled;\n toggle.classList.toggle('wv-btn--primary', heatmapEnabled);\n if (heatmapEnabled) {\n applyHeatmap();\n } else {\n removeHeatmap();\n }\n });\n\n metricSelect?.addEventListener('change', (e) => {\n heatmapMetric = e.target.value;\n if (heatmapEnabled) applyHeatmap();\n });\n }\n\n function applyHeatmap() {\n const perfData = window.__PERFORMANCE_DATA__;\n if (!perfData) return;\n\n document.querySelectorAll('.wv-node').forEach((node) => {\n const nodeId = node.dataset.nodeId;\n const heat = perfData.heat?.[nodeId];\n\n // Remove existing heat classes\n node.classList.remove(\n 'wv-node--heat-cold',\n 'wv-node--heat-cool',\n 'wv-node--heat-neutral',\n 'wv-node--heat-warm',\n 'wv-node--heat-hot',\n 'wv-node--heat-critical'\n );\n\n if (heat !== undefined) {\n const level = getHeatLevel(heat);\n node.classList.add('wv-node--heat-' + level);\n }\n });\n }\n\n function removeHeatmap() {\n document.querySelectorAll('.wv-node').forEach((node) => {\n node.classList.remove(\n 'wv-node--heat-cold',\n 'wv-node--heat-cool',\n 'wv-node--heat-neutral',\n 'wv-node--heat-warm',\n 'wv-node--heat-hot',\n 'wv-node--heat-critical'\n );\n });\n }\n\n function getHeatLevel(heat) {\n if (heat < 0.2) return 'cold';\n if (heat < 0.4) return 'cool';\n if (heat < 0.6) return 'neutral';\n if (heat < 0.8) return 'warm';\n if (heat < 0.95) return 'hot';\n return 'critical';\n }\n\n // Keyboard shortcuts\n function setupKeyboardShortcuts() {\n document.addEventListener('keydown', (e) => {\n // Don't trigger shortcuts when typing in inputs\n if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;\n\n switch (e.key) {\n case ' ':\n e.preventDefault();\n isPlaying ? pause() : play();\n break;\n case 'ArrowLeft':\n e.preventDefault();\n stepBackward();\n break;\n case 'ArrowRight':\n e.preventDefault();\n stepForward();\n break;\n case '0':\n resetZoom();\n break;\n case '+':\n case '=':\n zoom(1.2);\n break;\n case '-':\n zoom(0.8);\n break;\n case 'h':\n ${options.heatmap ? \"document.getElementById('heatmap-toggle')?.click();\" : \"\"}\n break;\n case 'Escape':\n selectNode(null);\n break;\n }\n });\n }\n\n // Render functions\n function renderIR(ir) {\n // Store node data for inspector\n window.__WORKFLOW_DATA__ = {\n nodes: flattenNodes(ir.root.children).reduce((acc, n) => {\n acc[n.id] = n;\n return acc;\n }, {})\n };\n\n // Update SVG node states\n document.querySelectorAll('.wv-node').forEach((node) => {\n const nodeId = node.dataset.nodeId;\n const nodeData = window.__WORKFLOW_DATA__.nodes[nodeId];\n if (nodeData) {\n // Update state class\n node.className = node.className.replace(/wv-node--\\\\w+/g, '');\n node.classList.add('wv-node', 'wv-node--' + nodeData.state);\n\n // Update timing text if present\n const timing = node.querySelector('.wv-node-timing');\n if (timing && nodeData.durationMs !== undefined) {\n timing.textContent = formatDuration(nodeData.durationMs);\n }\n }\n });\n\n // Update selected node inspector\n if (selectedNodeId) {\n updateInspector(selectedNodeId);\n }\n }\n\n function flattenNodes(nodes) {\n const result = [];\n for (const node of nodes) {\n result.push(node);\n if (node.children) {\n result.push(...flattenNodes(node.children));\n }\n if (node.branches) {\n for (const branch of node.branches) {\n result.push(...flattenNodes(branch.children));\n }\n }\n }\n return result;\n }\n\n // Utilities\n function formatDuration(ms) {\n if (ms < 1) return '<1ms';\n if (ms < 1000) return Math.round(ms) + 'ms';\n if (ms < 60000) return (ms / 1000).toFixed(2) + 's';\n return (ms / 60000).toFixed(1) + 'm';\n }\n})();\n`;\n}\n","/**\n * HTML Renderer\n *\n * Renders the workflow IR as an interactive HTML page with:\n * - SVG-based workflow diagram\n * - Zoom and pan\n * - Node inspection\n * - Time-travel controls\n * - Performance heatmap overlay\n */\n\nimport type {\n FlowNode,\n HTMLRenderOptions,\n Renderer,\n WorkflowIR,\n StepState,\n RenderOptions,\n LayoutDirection,\n} from \"../types\";\nimport {\n isStepNode,\n isParallelNode,\n isRaceNode,\n isDecisionNode,\n} from \"../types\";\nimport { generateStyles } from \"./html-styles\";\nimport { generateClientScript } from \"./html-client\";\nimport { formatDuration } from \"../utils/timing\";\n\n// =============================================================================\n// Constants\n// =============================================================================\n\nconst NODE_WIDTH = 160;\nconst NODE_HEIGHT = 50;\nconst NODE_SPACING_H = 40;\nconst NODE_SPACING_V = 30;\nconst CONTAINER_PADDING = 20;\n\n// =============================================================================\n// Layout Types\n// =============================================================================\n\ninterface LayoutNode {\n id: string;\n name: string;\n type: string;\n state: StepState;\n x: number;\n y: number;\n width: number;\n height: number;\n durationMs?: number;\n children?: LayoutNode[];\n containerType?: \"parallel\" | \"race\" | \"decision\";\n containerLabel?: string;\n}\n\ninterface LayoutResult {\n nodes: LayoutNode[];\n width: number;\n height: number;\n}\n\n// =============================================================================\n// Layout Functions\n// =============================================================================\n\n/**\n * Layout workflow nodes for SVG rendering.\n * Uses a simple top-to-bottom layout algorithm.\n */\nfunction layoutWorkflow(\n nodes: FlowNode[],\n direction: LayoutDirection = \"TB\"\n): LayoutResult {\n const isVertical = direction === \"TB\" || direction === \"BT\";\n const layoutNodes: LayoutNode[] = [];\n let currentX = CONTAINER_PADDING;\n let currentY = CONTAINER_PADDING;\n let maxWidth = 0;\n let maxHeight = 0;\n\n for (const node of nodes) {\n const result = layoutFlowNode(node, currentX, currentY, isVertical);\n layoutNodes.push(result.node);\n\n if (isVertical) {\n currentY += result.height + NODE_SPACING_V;\n maxWidth = Math.max(maxWidth, result.width);\n maxHeight = currentY;\n } else {\n currentX += result.width + NODE_SPACING_H;\n maxHeight = Math.max(maxHeight, result.height);\n maxWidth = currentX;\n }\n }\n\n return {\n nodes: layoutNodes,\n width: maxWidth + CONTAINER_PADDING,\n height: maxHeight + CONTAINER_PADDING,\n };\n}\n\n/**\n * Layout a single flow node.\n */\nfunction layoutFlowNode(\n node: FlowNode,\n x: number,\n y: number,\n _isVertical: boolean\n): { node: LayoutNode; width: number; height: number } {\n if (isStepNode(node)) {\n return {\n node: {\n id: node.id,\n name: node.name ?? node.key ?? \"step\",\n type: \"step\",\n state: node.state,\n x,\n y,\n width: NODE_WIDTH,\n height: NODE_HEIGHT,\n durationMs: node.durationMs,\n },\n width: NODE_WIDTH,\n height: NODE_HEIGHT,\n };\n }\n\n if (isParallelNode(node) || isRaceNode(node)) {\n const containerType = isParallelNode(node) ? \"parallel\" : \"race\";\n const label = node.name ?? containerType;\n const children: LayoutNode[] = [];\n\n let innerX = x + CONTAINER_PADDING;\n const innerY = y + CONTAINER_PADDING + 20; // Extra space for label\n let innerMaxWidth = 0;\n let innerMaxHeight = 0;\n\n // Layout children horizontally in parallel\n for (const child of node.children) {\n const result = layoutFlowNode(child, innerX, innerY, true);\n children.push(result.node);\n innerX += result.width + NODE_SPACING_H;\n innerMaxHeight = Math.max(innerMaxHeight, result.height);\n }\n\n innerMaxWidth = innerX - x - CONTAINER_PADDING;\n const containerWidth = Math.max(\n innerMaxWidth + CONTAINER_PADDING,\n NODE_WIDTH + CONTAINER_PADDING * 2\n );\n const containerHeight =\n innerMaxHeight + CONTAINER_PADDING * 2 + 20; // Extra for label\n\n return {\n node: {\n id: node.id,\n name: label,\n type: containerType,\n state: node.state,\n x,\n y,\n width: containerWidth,\n height: containerHeight,\n durationMs: node.durationMs,\n children,\n containerType,\n containerLabel: containerType === \"parallel\" ? \"PARALLEL\" : \"RACE\",\n },\n width: containerWidth,\n height: containerHeight,\n };\n }\n\n if (isDecisionNode(node)) {\n const label = node.name ?? \"decision\";\n const children: LayoutNode[] = [];\n\n let innerX = x + CONTAINER_PADDING;\n const innerY = y + CONTAINER_PADDING + 20;\n let innerMaxHeight = 0;\n\n // Layout branches horizontally\n for (const branch of node.branches) {\n for (const child of branch.children) {\n const result = layoutFlowNode(child, innerX, innerY, true);\n children.push(result.node);\n innerX += result.width + NODE_SPACING_H;\n innerMaxHeight = Math.max(innerMaxHeight, result.height);\n }\n }\n\n const containerWidth = Math.max(\n innerX - x,\n NODE_WIDTH + CONTAINER_PADDING * 2\n );\n const containerHeight = innerMaxHeight + CONTAINER_PADDING * 2 + 20;\n\n return {\n node: {\n id: node.id,\n name: label,\n type: \"decision\",\n state: node.state,\n x,\n y,\n width: containerWidth,\n height: containerHeight,\n durationMs: node.durationMs,\n children,\n containerType: \"decision\",\n containerLabel: \"DECISION\",\n },\n width: containerWidth,\n height: containerHeight,\n };\n }\n\n // Default fallback\n return {\n node: {\n id: node.id,\n name: node.name ?? \"unknown\",\n type: node.type,\n state: node.state,\n x,\n y,\n width: NODE_WIDTH,\n height: NODE_HEIGHT,\n },\n width: NODE_WIDTH,\n height: NODE_HEIGHT,\n };\n}\n\n// =============================================================================\n// SVG Rendering\n// =============================================================================\n\n/**\n * Render a layout node to SVG.\n */\nfunction renderLayoutNodeSVG(node: LayoutNode, showTimings: boolean): string {\n if (node.containerType) {\n return renderContainerSVG(node, showTimings);\n }\n return renderStepSVG(node, showTimings);\n}\n\n/**\n * Render a step node as SVG.\n */\nfunction renderStepSVG(node: LayoutNode, showTimings: boolean): string {\n const rx = 8; // Border radius\n const timing =\n showTimings && node.durationMs !== undefined\n ? formatDuration(node.durationMs)\n : \"\";\n\n return `\n <g class=\"wv-node wv-node--${node.state}\" data-node-id=\"${escapeAttr(node.id)}\" transform=\"translate(${node.x}, ${node.y})\">\n <rect width=\"${node.width}\" height=\"${node.height}\" rx=\"${rx}\" ry=\"${rx}\" />\n <text x=\"${node.width / 2}\" y=\"${node.height / 2 - (timing ? 4 : 0)}\">${escapeXml(truncate(node.name, 20))}</text>\n ${timing ? `<text class=\"wv-node-timing\" x=\"${node.width / 2}\" y=\"${node.height / 2 + 12}\">${timing}</text>` : \"\"}\n </g>\n `;\n}\n\n/**\n * Render a container (parallel/race/decision) as SVG.\n */\nfunction renderContainerSVG(node: LayoutNode, showTimings: boolean): string {\n const rx = 12;\n const childrenSVG =\n node.children?.map((c) => renderLayoutNodeSVG(c, showTimings)).join(\"\\n\") ??\n \"\";\n\n return `\n <g class=\"wv-container wv-container--${node.containerType}\" data-node-id=\"${escapeAttr(node.id)}\" transform=\"translate(${node.x}, ${node.y})\">\n <rect width=\"${node.width}\" height=\"${node.height}\" rx=\"${rx}\" ry=\"${rx}\" />\n <text class=\"wv-container-label\" x=\"${CONTAINER_PADDING}\" y=\"16\">${node.containerLabel}</text>\n <g transform=\"translate(${-node.x}, ${-node.y})\">\n ${childrenSVG}\n </g>\n </g>\n `;\n}\n\n/**\n * Render edges between sequential nodes.\n */\nfunction renderEdgesSVG(nodes: LayoutNode[]): string {\n const edges: string[] = [];\n\n for (let i = 0; i < nodes.length - 1; i++) {\n const from = nodes[i];\n const to = nodes[i + 1];\n\n const x1 = from.x + from.width / 2;\n const y1 = from.y + from.height;\n const x2 = to.x + to.width / 2;\n const y2 = to.y;\n\n // Simple straight line with arrow\n edges.push(`\n <path class=\"wv-edge\" d=\"M ${x1} ${y1} L ${x2} ${y2 - 8}\" />\n <polygon class=\"wv-edge-arrow\" points=\"${x2 - 4},${y2 - 8} ${x2 + 4},${y2 - 8} ${x2},${y2}\" />\n `);\n }\n\n return edges.join(\"\\n\");\n}\n\n// =============================================================================\n// HTML Generation\n// =============================================================================\n\n/**\n * Generate complete HTML page.\n */\nfunction generateHTML(\n ir: WorkflowIR,\n options: HTMLRenderOptions\n): string {\n const layout = layoutWorkflow(ir.root.children, options.layout);\n const svgWidth = Math.max(layout.width, 400);\n const svgHeight = Math.max(layout.height, 300);\n\n const nodesSVG = layout.nodes\n .map((n) => renderLayoutNodeSVG(n, options.showTimings))\n .join(\"\\n\");\n const edgesSVG = renderEdgesSVG(layout.nodes);\n\n const workflowName = ir.root.name ?? \"Workflow\";\n const css = generateStyles(options.theme);\n const js = generateClientScript({\n wsUrl: options.wsUrl,\n interactive: options.interactive,\n timeTravel: options.timeTravel,\n heatmap: options.heatmap,\n });\n\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeXml(workflowName)} - Workflow Visualizer</title>\n <style>${css}</style>\n</head>\n<body>\n <div class=\"workflow-visualizer\">\n <header class=\"wv-header\">\n <h1>${escapeXml(workflowName)}</h1>\n <div class=\"wv-controls\">\n ${options.wsUrl ? `<div id=\"live-indicator\" class=\"wv-live\" style=\"display:none\"><span class=\"wv-live-dot\"></span>LIVE</div>` : \"\"}\n ${options.heatmap ? `\n <button id=\"heatmap-toggle\" class=\"wv-btn\">Heatmap</button>\n <select id=\"heatmap-metric\" class=\"wv-btn\">\n <option value=\"duration\">Duration</option>\n <option value=\"retryRate\">Retry Rate</option>\n <option value=\"errorRate\">Error Rate</option>\n </select>\n ` : \"\"}\n ${options.interactive ? `\n <button id=\"zoom-out\" class=\"wv-btn wv-btn--icon\" title=\"Zoom out (-)\">−</button>\n <button id=\"zoom-reset\" class=\"wv-btn wv-btn--icon\" title=\"Reset zoom (0)\">⟲</button>\n <button id=\"zoom-in\" class=\"wv-btn wv-btn--icon\" title=\"Zoom in (+)\">+</button>\n ` : \"\"}\n </div>\n </header>\n\n <div class=\"wv-main\">\n <div id=\"diagram\" class=\"wv-diagram\">\n <svg viewBox=\"0 0 ${svgWidth} ${svgHeight}\" preserveAspectRatio=\"xMidYMid meet\">\n <g class=\"wv-root\">\n ${edgesSVG}\n ${nodesSVG}\n </g>\n </svg>\n </div>\n\n ${options.interactive ? `\n <aside id=\"inspector\" class=\"wv-inspector\">\n <div class=\"wv-inspector-header\">\n <h2>Inspector</h2>\n </div>\n <div id=\"inspector-content\" class=\"wv-inspector-content\">\n <p class=\"wv-empty\">Select a node to inspect</p>\n </div>\n </aside>\n ` : \"\"}\n </div>\n\n ${options.timeTravel ? `\n <div id=\"timeline\" class=\"wv-timeline\">\n <div class=\"wv-timeline-track\">\n <input type=\"range\" id=\"tt-slider\" min=\"0\" max=\"0\" value=\"0\" style=\"width:100%\">\n </div>\n <div class=\"wv-timeline-controls\">\n <button id=\"tt-prev\" class=\"wv-btn wv-btn--icon\" title=\"Step backward (←)\">⏮</button>\n <button id=\"tt-play\" class=\"wv-btn wv-btn--icon\" title=\"Play (Space)\">▶</button>\n <button id=\"tt-pause\" class=\"wv-btn wv-btn--icon\" style=\"display:none\" title=\"Pause (Space)\">⏸</button>\n <button id=\"tt-next\" class=\"wv-btn wv-btn--icon\" title=\"Step forward (→)\">⏭</button>\n <select id=\"tt-speed\" class=\"wv-btn\">\n <option value=\"0.5\">0.5x</option>\n <option value=\"1\" selected>1x</option>\n <option value=\"2\">2x</option>\n <option value=\"4\">4x</option>\n <option value=\"10\">10x</option>\n </select>\n <span id=\"tt-time\" class=\"wv-timeline-time\">0 / 0</span>\n </div>\n </div>\n ` : \"\"}\n </div>\n\n <script>\n window.__WORKFLOW_DATA__ = ${serializeWorkflowData(ir)};\n </script>\n <script>${js}</script>\n</body>\n</html>`;\n}\n\n/**\n * Build workflow data structure for client-side access.\n */\nfunction buildWorkflowData(ir: WorkflowIR): { nodes: Record<string, unknown> } {\n const nodes: Record<string, unknown> = {};\n\n function collectNodes(flowNodes: FlowNode[]): void {\n for (const node of flowNodes) {\n nodes[node.id] = {\n id: node.id,\n name: node.name,\n type: node.type,\n state: node.state,\n key: node.key,\n durationMs: node.durationMs,\n startTs: node.startTs,\n error: node.error ? String(node.error) : undefined,\n retryCount: node.retryCount,\n };\n\n if (\"children\" in node && Array.isArray(node.children)) {\n collectNodes(node.children);\n }\n if (\"branches\" in node) {\n for (const branch of node.branches) {\n collectNodes(branch.children);\n }\n }\n }\n }\n\n collectNodes(ir.root.children);\n return { nodes };\n}\n\n/**\n * Serialize workflow data for safe embedding inside a <script> tag.\n */\nfunction serializeWorkflowData(ir: WorkflowIR): string {\n const json = JSON.stringify(buildWorkflowData(ir));\n // Escape characters that could break out of the script context\n return json\n .replace(/</g, \"\\\\u003c\")\n .replace(/>/g, \"\\\\u003e\")\n .replace(/&/g, \"\\\\u0026\")\n .replace(/\\u2028/g, \"\\\\u2028\")\n .replace(/\\u2029/g, \"\\\\u2029\");\n}\n\n// =============================================================================\n// Utility Functions\n// =============================================================================\n\nfunction escapeXml(str: string): string {\n return str\n .replace(/&/g, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#039;\");\n}\n\nfunction escapeAttr(str: string): string {\n return str.replace(/\"/g, \"&quot;\").replace(/'/g, \"&#039;\");\n}\n\nfunction truncate(str: string, maxLen: number): string {\n if (str.length <= maxLen) return str;\n return str.slice(0, maxLen - 1) + \"…\";\n}\n\n// =============================================================================\n// Renderer Export\n// =============================================================================\n\n/**\n * Default HTML render options.\n */\nconst defaultHTMLOptions: Omit<HTMLRenderOptions, keyof RenderOptions> = {\n interactive: true,\n timeTravel: true,\n heatmap: true,\n animationDuration: 200,\n theme: \"auto\",\n layout: \"TB\",\n};\n\n/**\n * Create the HTML renderer.\n */\nexport function htmlRenderer(): Renderer {\n return {\n name: \"html\",\n supportsLive: true,\n\n render(ir: WorkflowIR, options: RenderOptions): string {\n const htmlOptions: HTMLRenderOptions = {\n ...options,\n ...defaultHTMLOptions,\n ...(options as Partial<HTMLRenderOptions>),\n };\n\n return generateHTML(ir, htmlOptions);\n },\n };\n}\n\n/**\n * Render workflow IR to HTML with custom options.\n */\nexport function renderToHTML(\n ir: WorkflowIR,\n options: Partial<HTMLRenderOptions> = {}\n): string {\n const fullOptions: HTMLRenderOptions = {\n showTimings: true,\n showKeys: false,\n colors: {\n pending: \"#6c757d\",\n running: \"#ffc107\",\n success: \"#198754\",\n error: \"#dc3545\",\n aborted: \"#6c757d\",\n cached: \"#0dcaf0\",\n skipped: \"#adb5bd\",\n },\n ...defaultHTMLOptions,\n ...options,\n };\n\n return generateHTML(ir, fullOptions);\n}\n","/**\n * Live Visualizer - Real-time terminal updates during workflow execution.\n *\n * Uses ANSI escape codes to update the terminal in-place, showing\n * workflow progress as it happens.\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n LiveVisualizerOptions,\n RenderOptions,\n ScopeEndEvent,\n ScopeStartEvent,\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n WorkflowIR,\n} from \"./types\";\nimport { createIRBuilder } from \"./ir-builder\";\nimport { asciiRenderer, defaultColorScheme } from \"./renderers\";\n\n// =============================================================================\n// ANSI Escape Codes\n// =============================================================================\n\nconst ANSI = {\n /** Clear from cursor to end of screen */\n clearToEnd: \"\\x1b[J\",\n /** Move cursor up N lines */\n cursorUp: (n: number) => `\\x1b[${n}A`,\n /** Move cursor to beginning of line */\n cursorToStart: \"\\x1b[G\",\n /** Hide cursor */\n hideCursor: \"\\x1b[?25l\",\n /** Show cursor */\n showCursor: \"\\x1b[?25h\",\n /** Save cursor position */\n saveCursor: \"\\x1b[s\",\n /** Restore cursor position */\n restoreCursor: \"\\x1b[u\",\n};\n\n// =============================================================================\n// Live Visualizer Interface\n// =============================================================================\n\n/**\n * Live visualizer with real-time terminal updates.\n */\nexport interface LiveVisualizer {\n /** Process a workflow event */\n handleEvent: (event: WorkflowEvent<unknown>) => void;\n /** Process a scope event */\n handleScopeEvent: (event: ScopeStartEvent | ScopeEndEvent) => void;\n /** Process a decision event */\n handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;\n /** Get current IR state */\n getIR: () => WorkflowIR;\n /** Render current state to string (without terminal output) */\n render: () => string;\n /** Start live rendering to terminal */\n start: () => void;\n /** Stop live rendering */\n stop: () => void;\n /** Force an immediate redraw */\n refresh: () => void;\n /** Reset state for a new workflow */\n reset: () => void;\n}\n\n// =============================================================================\n// Create Live Visualizer\n// =============================================================================\n\n/**\n * Create a live visualizer for real-time terminal updates.\n *\n * @example\n * ```typescript\n * const live = createLiveVisualizer({ workflowName: 'my-workflow' });\n * const workflow = createWorkflow(deps, { onEvent: live.handleEvent });\n *\n * live.start();\n * await workflow(async (step) => { ... });\n * live.stop();\n * ```\n */\nexport function createLiveVisualizer(\n options: LiveVisualizerOptions = {}\n): LiveVisualizer {\n const {\n workflowName,\n detectParallel = true,\n showTimings = true,\n showKeys = false,\n colors: customColors,\n stream = process.stdout,\n updateInterval = 100,\n } = options;\n\n const builder = createIRBuilder({ detectParallel });\n const renderer = asciiRenderer();\n\n // Render options\n const renderOptions: RenderOptions = {\n showTimings,\n showKeys,\n terminalWidth: stream.columns ?? 80,\n colors: { ...defaultColorScheme, ...customColors },\n };\n\n // State\n let isRunning = false;\n let lastOutput = \"\";\n let lastLineCount = 0;\n let throttleTimeout: ReturnType<typeof setTimeout> | null = null;\n let pendingUpdate = false;\n\n /**\n * Write to the output stream.\n */\n function write(text: string): void {\n if (stream.writable) {\n stream.write(text);\n }\n }\n\n /**\n * Clear the previous output and write new content.\n */\n function redraw(): void {\n if (!isRunning) return;\n\n const ir = getIR();\n const output = renderer.render(ir, renderOptions);\n\n // If output hasn't changed, skip redraw\n if (output === lastOutput) return;\n\n // Clear previous output\n if (lastLineCount > 0) {\n // Move cursor up and clear\n write(ANSI.cursorUp(lastLineCount));\n write(ANSI.cursorToStart);\n write(ANSI.clearToEnd);\n }\n\n // Write new output\n write(output);\n write(\"\\n\");\n\n // Track line count for next clear\n lastOutput = output;\n lastLineCount = output.split(\"\\n\").length;\n }\n\n /**\n * Schedule a throttled redraw.\n */\n function scheduleRedraw(): void {\n if (!isRunning) return;\n\n pendingUpdate = true;\n\n if (throttleTimeout === null) {\n throttleTimeout = setTimeout(() => {\n throttleTimeout = null;\n if (pendingUpdate) {\n pendingUpdate = false;\n redraw();\n }\n }, updateInterval);\n }\n }\n\n /**\n * Handle a workflow event.\n */\n function handleEvent(event: WorkflowEvent<unknown>): void {\n // Route scope events to handleScopeEvent for proper IR building\n if (event.type === \"scope_start\" || event.type === \"scope_end\") {\n handleScopeEvent(event as ScopeStartEvent | ScopeEndEvent);\n return;\n }\n\n builder.handleEvent(event);\n\n if (isRunning) {\n // Immediate redraw for start/end events, throttled for others\n if (\n event.type === \"workflow_start\" ||\n event.type === \"workflow_success\" ||\n event.type === \"workflow_error\"\n ) {\n redraw();\n } else {\n scheduleRedraw();\n }\n }\n }\n\n /**\n * Handle a scope event.\n */\n function handleScopeEvent(event: ScopeStartEvent | ScopeEndEvent): void {\n builder.handleScopeEvent(event);\n if (isRunning) {\n scheduleRedraw();\n }\n }\n\n /**\n * Handle a decision event.\n */\n function handleDecisionEvent(\n event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent\n ): void {\n builder.handleDecisionEvent(event);\n if (isRunning) {\n scheduleRedraw();\n }\n }\n\n /**\n * Get the current IR state.\n */\n function getIR(): WorkflowIR {\n const ir = builder.getIR();\n if (workflowName && !ir.root.name) {\n ir.root.name = workflowName;\n }\n return ir;\n }\n\n /**\n * Render current state to string.\n */\n function render(): string {\n return renderer.render(getIR(), renderOptions);\n }\n\n /**\n * Start live rendering.\n */\n function start(): void {\n if (isRunning) return;\n\n isRunning = true;\n lastOutput = \"\";\n lastLineCount = 0;\n\n // Hide cursor during updates\n write(ANSI.hideCursor);\n\n // Initial render\n redraw();\n }\n\n /**\n * Stop live rendering.\n */\n function stop(): void {\n if (!isRunning) return;\n\n isRunning = false;\n\n // Clear any pending throttle\n if (throttleTimeout !== null) {\n clearTimeout(throttleTimeout);\n throttleTimeout = null;\n }\n\n // Final redraw to show completed state\n const ir = getIR();\n const output = renderer.render(ir, renderOptions);\n\n if (lastLineCount > 0) {\n write(ANSI.cursorUp(lastLineCount));\n write(ANSI.cursorToStart);\n write(ANSI.clearToEnd);\n }\n\n write(output);\n write(\"\\n\");\n\n // Show cursor again\n write(ANSI.showCursor);\n }\n\n /**\n * Force an immediate redraw.\n */\n function refresh(): void {\n if (throttleTimeout !== null) {\n clearTimeout(throttleTimeout);\n throttleTimeout = null;\n }\n pendingUpdate = false;\n redraw();\n }\n\n /**\n * Reset state for a new workflow.\n */\n function reset(): void {\n builder.reset();\n lastOutput = \"\";\n lastLineCount = 0;\n }\n\n return {\n handleEvent,\n handleScopeEvent,\n handleDecisionEvent,\n getIR,\n render,\n start,\n stop,\n refresh,\n reset,\n };\n}\n","/**\n * Decision Tracker - Helper for tracking conditional logic in workflows.\n *\n * This module provides utilities to track decision points (if/switch statements)\n * so they can be visualized in workflow diagrams.\n *\n * @example\n * ```typescript\n * import { trackDecision } from '@jagreehal/workflow/visualize';\n *\n * const workflow = createWorkflow(deps, {\n * onEvent: (event) => {\n * // Pass events to visualizer\n * viz.handleEvent(event);\n * }\n * });\n *\n * await workflow(async (step) => {\n * const user = await step(fetchUser(id));\n *\n * // Track a decision point\n * const decision = trackDecision('user-role-check', {\n * condition: 'user.role === \"admin\"',\n * value: user.role\n * });\n *\n * if (user.role === 'admin') {\n * decision.takeBranch('admin', true);\n * await step(processAdminAction(user));\n * } else {\n * decision.takeBranch('user', false);\n * await step(processUserAction(user));\n * }\n *\n * decision.end();\n * });\n * ```\n */\n\nimport type {\n DecisionStartEvent,\n DecisionBranchEvent,\n DecisionEndEvent,\n} from \"./types\";\n\n// =============================================================================\n// Decision Tracker\n// =============================================================================\n\n/**\n * Options for creating a decision tracker.\n */\nexport interface DecisionTrackerOptions {\n /** Condition being evaluated (e.g., \"user.role === 'admin'\") */\n condition?: string;\n /** Value being evaluated */\n value?: unknown;\n /** Name/label for this decision point */\n name?: string;\n /** Workflow ID (auto-generated if not provided) */\n workflowId?: string;\n /** Event emitter function */\n emit?: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;\n}\n\n/**\n * Track a decision point in your workflow.\n *\n * Use this to annotate conditional logic (if/switch) so it appears\n * in workflow visualizations.\n *\n * @param decisionId - Unique identifier for this decision\n * @param options - Decision tracking options\n * @returns A tracker object with methods to track branches\n *\n * @example\n * ```typescript\n * const decision = trackDecision('check-role', {\n * condition: 'user.role === \"admin\"',\n * value: user.role,\n * emit: (event) => viz.handleDecisionEvent(event)\n * });\n *\n * if (user.role === 'admin') {\n * decision.takeBranch('admin', true);\n * // ... admin logic\n * } else {\n * decision.takeBranch('user', false);\n * // ... user logic\n * }\n *\n * decision.end();\n * ```\n */\nexport function trackDecision(\n decisionId: string,\n options: DecisionTrackerOptions = {}\n): DecisionTracker {\n const {\n condition,\n value,\n name,\n workflowId = crypto.randomUUID(),\n emit,\n } = options;\n\n const startTs = Date.now();\n let branchTaken: string | boolean | undefined;\n const branches: Array<{ label: string; condition?: string; taken: boolean }> = [];\n\n function takeBranch(\n label: string,\n taken: boolean,\n branchCondition?: string\n ): void {\n branches.push({ label, condition: branchCondition, taken });\n if (taken) {\n branchTaken = label;\n }\n\n emit?.({\n type: \"decision_branch\",\n workflowId,\n decisionId,\n branchLabel: label,\n condition: branchCondition,\n taken,\n ts: Date.now(),\n });\n }\n\n function end(): void {\n const durationMs = Date.now() - startTs;\n emit?.({\n type: \"decision_end\",\n workflowId,\n decisionId,\n branchTaken,\n ts: Date.now(),\n durationMs,\n });\n }\n\n // Emit start event immediately\n emit?.({\n type: \"decision_start\",\n workflowId,\n decisionId,\n condition,\n decisionValue: value,\n name,\n ts: startTs,\n });\n\n return {\n takeBranch,\n end,\n getBranchTaken: () => branchTaken,\n getBranches: () => [...branches],\n };\n}\n\n/**\n * Decision tracker instance.\n */\nexport interface DecisionTracker {\n /**\n * Mark that a branch was taken or skipped.\n * @param label - Label for this branch (e.g., \"if\", \"else\", \"case 'admin'\")\n * @param taken - Whether this branch was executed\n * @param branchCondition - Optional condition for this specific branch\n */\n takeBranch(label: string, taken: boolean, branchCondition?: string): void;\n\n /**\n * End the decision tracking.\n * Call this after all branches have been evaluated.\n */\n end(): void;\n\n /**\n * Get which branch was taken.\n */\n getBranchTaken(): string | boolean | undefined;\n\n /**\n * Get all branches (taken and skipped).\n */\n getBranches(): Array<{ label: string; condition?: string; taken: boolean }>;\n}\n\n// =============================================================================\n// Convenience Helpers\n// =============================================================================\n\n/**\n * Track a simple if/else decision.\n *\n * @example\n * ```typescript\n * const decision = trackIf('check-admin', user.role === 'admin', {\n * condition: 'user.role === \"admin\"',\n * value: user.role,\n * emit: (e) => viz.handleDecisionEvent(e)\n * });\n *\n * if (decision.condition) {\n * decision.then();\n * // admin logic\n * } else {\n * decision.else();\n * // user logic\n * }\n *\n * decision.end();\n * ```\n */\nexport function trackIf(\n decisionId: string,\n condition: boolean,\n options: Omit<DecisionTrackerOptions, \"value\"> & { value?: unknown } = {}\n): IfTracker {\n const tracker = trackDecision(decisionId, {\n ...options,\n condition: options.condition ?? String(condition),\n value: options.value ?? condition,\n });\n\n return {\n ...tracker,\n condition,\n then: () => {\n tracker.takeBranch(\"if\", true);\n },\n else: () => {\n // Mark else branch as taken (true) when the else block executes\n tracker.takeBranch(\"else\", true);\n },\n };\n}\n\n/**\n * If tracker with convenience methods.\n */\nexport interface IfTracker extends DecisionTracker {\n /** The condition value */\n condition: boolean;\n /** Mark the \"if\" branch as taken */\n then(): void;\n /** Mark the \"else\" branch as taken */\n else(): void;\n}\n\n/**\n * Track a switch statement decision.\n *\n * @example\n * ```typescript\n * const decision = trackSwitch('process-type', user.type, {\n * emit: (e) => viz.handleDecisionEvent(e)\n * });\n *\n * switch (user.type) {\n * case 'admin':\n * decision.case('admin', true);\n * // admin logic\n * break;\n * case 'user':\n * decision.case('user', true);\n * // user logic\n * break;\n * default:\n * decision.default(true);\n * // default logic\n * }\n *\n * decision.end();\n * ```\n */\nexport function trackSwitch(\n decisionId: string,\n value: unknown,\n options: Omit<DecisionTrackerOptions, \"value\"> = {}\n): SwitchTracker {\n const tracker = trackDecision(decisionId, {\n ...options,\n condition: options.condition ?? `switch(${String(value)})`,\n value,\n });\n\n return {\n ...tracker,\n value,\n case: (caseValue: string | number, taken: boolean) => {\n tracker.takeBranch(`case '${caseValue}'`, taken, `value === '${caseValue}'`);\n },\n default: (taken: boolean) => {\n tracker.takeBranch(\"default\", taken);\n },\n };\n}\n\n/**\n * Switch tracker with convenience methods.\n */\nexport interface SwitchTracker extends DecisionTracker {\n /** The value being switched on */\n value: unknown;\n /** Mark a case branch */\n case(caseValue: string | number, taken: boolean): void;\n /** Mark the default branch */\n default(taken: boolean): void;\n}\n","/**\n * Time Travel Debugger\n *\n * Records IR snapshots at each event, enabling:\n * - Step-by-step forward/backward navigation\n * - Seeking to any point in execution\n * - Playback at various speeds\n * - State inspection at any moment\n */\n\nimport type { WorkflowEvent } from \"../core\";\nimport type {\n IRSnapshot,\n TimeTravelState,\n WorkflowIR,\n} from \"./types\";\nimport { createIRBuilder, type IRBuilder, type IRBuilderOptions } from \"./ir-builder\";\n\n// =============================================================================\n// Types\n// =============================================================================\n\n/**\n * Options for the time-travel controller.\n */\nexport interface TimeTravelOptions {\n /** Maximum snapshots to store (ring buffer) */\n maxSnapshots?: number;\n /** Automatically record snapshots on each event */\n autoRecord?: boolean;\n /** IR builder options (passed through) */\n builderOptions?: Omit<IRBuilderOptions, \"enableSnapshots\" | \"maxSnapshots\">;\n}\n\n/**\n * Time-travel controller interface.\n */\nexport interface TimeTravelController {\n /** Handle a workflow event (records snapshot if recording) */\n handleEvent: (event: WorkflowEvent<unknown>) => void;\n\n /** Navigate to specific snapshot index */\n seek: (index: number) => WorkflowIR | undefined;\n\n /** Move one step forward */\n stepForward: () => WorkflowIR | undefined;\n\n /** Move one step backward */\n stepBackward: () => WorkflowIR | undefined;\n\n /** Start playback at given speed (1.0 = realtime) */\n play: (speed?: number) => void;\n\n /** Pause playback */\n pause: () => void;\n\n /** Get current IR state */\n getCurrentIR: () => WorkflowIR;\n\n /** Get IR at specific snapshot index */\n getIRAt: (index: number) => WorkflowIR | undefined;\n\n /** Get all snapshots */\n getSnapshots: () => IRSnapshot[];\n\n /** Get snapshot at specific index */\n getSnapshotAt: (index: number) => IRSnapshot | undefined;\n\n /** Get current time-travel state */\n getState: () => TimeTravelState;\n\n /** Subscribe to state changes */\n onStateChange: (callback: (state: TimeTravelState) => void) => () => void;\n\n /** Start/resume recording */\n startRecording: () => void;\n\n /** Stop recording */\n stopRecording: () => void;\n\n /** Reset to initial state */\n reset: () => void;\n\n /** Get the underlying IR builder */\n getBuilder: () => IRBuilder;\n}\n\n// =============================================================================\n// Implementation\n// =============================================================================\n\n/**\n * Create a time-travel controller for workflow debugging.\n *\n * @example\n * ```typescript\n * const controller = createTimeTravelController();\n *\n * // Feed events from workflow execution\n * workflow.onEvent(event => controller.handleEvent(event));\n *\n * // After execution, explore the timeline\n * controller.seek(0); // Go to start\n * controller.stepForward(); // Step through\n * controller.play(2); // Playback at 2x speed\n * ```\n */\nexport function createTimeTravelController(\n options: TimeTravelOptions = {}\n): TimeTravelController {\n const { maxSnapshots = 1000, autoRecord = true, builderOptions = {} } = options;\n\n // Create IR builder with snapshots enabled\n const builder = createIRBuilder({\n ...builderOptions,\n enableSnapshots: true,\n maxSnapshots,\n });\n\n // Controller state\n let state: TimeTravelState = {\n snapshots: [],\n currentIndex: -1,\n isPlaying: false,\n playbackSpeed: 1.0,\n isRecording: autoRecord,\n };\n\n // State change listeners\n const listeners = new Set<(state: TimeTravelState) => void>();\n\n // Playback timer\n let playbackTimer: ReturnType<typeof setTimeout> | null = null;\n\n /**\n * Notify all listeners of state change.\n */\n function notifyListeners(): void {\n const currentState = getState();\n for (const listener of listeners) {\n listener(currentState);\n }\n }\n\n /**\n * Sync state from builder snapshots.\n */\n function syncFromBuilder(): void {\n state.snapshots = builder.getSnapshots();\n if (state.isRecording && state.snapshots.length > 0) {\n state.currentIndex = state.snapshots.length - 1;\n }\n }\n\n /**\n * Handle a workflow event.\n */\n function handleEvent(event: WorkflowEvent<unknown>): void {\n // Always forward to builder\n builder.handleEvent(event);\n\n // Sync snapshots if recording\n if (state.isRecording) {\n syncFromBuilder();\n notifyListeners();\n }\n }\n\n /**\n * Seek to a specific snapshot index.\n */\n function seek(index: number): WorkflowIR | undefined {\n const snapshots = builder.getSnapshots();\n if (index < 0 || index >= snapshots.length) {\n return undefined;\n }\n\n state.currentIndex = index;\n state.snapshots = snapshots;\n notifyListeners();\n\n return snapshots[index].ir;\n }\n\n /**\n * Move one step forward.\n */\n function stepForward(): WorkflowIR | undefined {\n return seek(state.currentIndex + 1);\n }\n\n /**\n * Move one step backward.\n */\n function stepBackward(): WorkflowIR | undefined {\n return seek(state.currentIndex - 1);\n }\n\n /**\n * Start playback at given speed.\n */\n function play(speed = 1.0): void {\n state.playbackSpeed = speed;\n state.isPlaying = true;\n notifyListeners();\n\n const playNext = (): void => {\n if (!state.isPlaying) return;\n\n const snapshots = builder.getSnapshots();\n if (state.currentIndex < snapshots.length - 1) {\n const current = snapshots[state.currentIndex];\n const next = snapshots[state.currentIndex + 1];\n\n // Calculate delay based on actual event timing\n const realDelay = next.timestamp - current.timestamp;\n const scaledDelay = realDelay / state.playbackSpeed;\n\n playbackTimer = setTimeout(() => {\n stepForward();\n playNext();\n }, Math.max(16, scaledDelay)); // Minimum 16ms (~60fps) for smooth updates\n } else {\n // Reached end of timeline\n pause();\n }\n };\n\n playNext();\n }\n\n /**\n * Pause playback.\n */\n function pause(): void {\n state.isPlaying = false;\n if (playbackTimer) {\n clearTimeout(playbackTimer);\n playbackTimer = null;\n }\n notifyListeners();\n }\n\n /**\n * Get current IR state.\n */\n function getCurrentIR(): WorkflowIR {\n const snapshots = builder.getSnapshots();\n if (state.currentIndex >= 0 && state.currentIndex < snapshots.length) {\n return snapshots[state.currentIndex].ir;\n }\n // Return live IR if no valid snapshot\n return builder.getIR();\n }\n\n /**\n * Get IR at specific snapshot index.\n */\n function getIRAt(index: number): WorkflowIR | undefined {\n return builder.getIRAt(index);\n }\n\n /**\n * Get all snapshots.\n */\n function getSnapshots(): IRSnapshot[] {\n return builder.getSnapshots();\n }\n\n /**\n * Get snapshot at specific index.\n */\n function getSnapshotAt(index: number): IRSnapshot | undefined {\n return builder.getSnapshotAt(index);\n }\n\n /**\n * Get current time-travel state.\n */\n function getState(): TimeTravelState {\n return {\n snapshots: builder.getSnapshots(),\n currentIndex: state.currentIndex,\n isPlaying: state.isPlaying,\n playbackSpeed: state.playbackSpeed,\n isRecording: state.isRecording,\n };\n }\n\n /**\n * Subscribe to state changes.\n */\n function onStateChange(\n callback: (state: TimeTravelState) => void\n ): () => void {\n listeners.add(callback);\n return () => listeners.delete(callback);\n }\n\n /**\n * Start or resume recording.\n */\n function startRecording(): void {\n state.isRecording = true;\n notifyListeners();\n }\n\n /**\n * Stop recording.\n */\n function stopRecording(): void {\n state.isRecording = false;\n notifyListeners();\n }\n\n /**\n * Reset to initial state.\n */\n function reset(): void {\n pause();\n builder.reset();\n state = {\n snapshots: [],\n currentIndex: -1,\n isPlaying: false,\n playbackSpeed: 1.0,\n isRecording: autoRecord,\n };\n notifyListeners();\n }\n\n /**\n * Get the underlying IR builder.\n */\n function getBuilder(): IRBuilder {\n return builder;\n }\n\n return {\n handleEvent,\n seek,\n stepForward,\n stepBackward,\n play,\n pause,\n getCurrentIR,\n getIRAt,\n getSnapshots,\n getSnapshotAt,\n getState,\n onStateChange,\n startRecording,\n stopRecording,\n reset,\n getBuilder,\n };\n}\n","/**\n * Development Server\n *\n * Provides a local HTTP server with WebSocket support for:\n * - Serving the HTML visualizer\n * - Live streaming workflow events\n * - Time-travel control from the browser\n * - Performance data broadcasting\n *\n * The `ws` package is an optional peer dependency.\n * Install it with: npm install ws\n */\n\nimport type { Server as HTTPServer, IncomingMessage, ServerResponse } from \"node:http\";\nimport { createServer } from \"node:http\";\nimport { execFile } from \"node:child_process\";\nimport type { WorkflowEvent } from \"../core\";\nimport type { WorkflowIR, ServerMessage, WebVisualizerMessage, HeatmapData } from \"./types\";\nimport { createTimeTravelController, type TimeTravelController } from \"./time-travel\";\nimport { createPerformanceAnalyzer, type PerformanceAnalyzer } from \"./performance-analyzer\";\nimport { renderToHTML } from \"./renderers/html\";\n\n// =============================================================================\n// Types\n// =============================================================================\n\n/**\n * WebSocket interface (matches `ws` package).\n */\ninterface WebSocketLike {\n send(data: string): void;\n on(event: \"message\", listener: (data: Buffer | string) => void): void;\n on(event: \"close\", listener: () => void): void;\n on(event: \"error\", listener: (error: Error) => void): void;\n readyState: number;\n OPEN: number;\n}\n\n/**\n * WebSocket server interface (matches `ws` package).\n */\ninterface WebSocketServerLike {\n clients: Set<WebSocketLike>;\n on(event: \"connection\", listener: (ws: WebSocketLike, req: IncomingMessage) => void): void;\n close(): void;\n}\n\n/**\n * Options for the dev server.\n */\nexport interface DevServerOptions {\n /** Port to listen on (default: 3377) */\n port?: number;\n /** Hostname to bind to (default: localhost) */\n host?: string;\n /** Auto-open browser when starting (default: true) */\n autoOpen?: boolean;\n /** Workflow name for the visualizer title */\n workflowName?: string;\n /** Enable time-travel debugging (default: true) */\n timeTravel?: boolean;\n /** Enable performance heatmap (default: true) */\n heatmap?: boolean;\n /** Max snapshots for time-travel (default: 1000) */\n maxSnapshots?: number;\n}\n\n/**\n * Dev server interface.\n */\nexport interface DevServer {\n /** Start the server */\n start(): Promise<{ port: number; url: string }>;\n /** Stop the server */\n stop(): Promise<void>;\n /** Handle a workflow event (forwards to time-travel and broadcasts) */\n handleEvent(event: WorkflowEvent<unknown>): void;\n /** Push an IR update to all clients */\n pushUpdate(ir: WorkflowIR): void;\n /** Push heatmap data to all clients */\n pushHeatmap(data: HeatmapData): void;\n /** Mark workflow as complete */\n complete(): void;\n /** Get the time-travel controller */\n getTimeTravel(): TimeTravelController;\n /** Get the performance analyzer */\n getAnalyzer(): PerformanceAnalyzer;\n /** Get current IR */\n getCurrentIR(): WorkflowIR;\n}\n\n// =============================================================================\n// Implementation\n// =============================================================================\n\n/**\n * Create a development server for the workflow visualizer.\n *\n * @example\n * ```typescript\n * const server = createDevServer({ port: 3377 });\n * await server.start();\n *\n * // Forward events from workflow execution\n * workflow.onEvent(event => server.handleEvent(event));\n *\n * // When done\n * server.complete();\n * await server.stop();\n * ```\n */\nexport async function createDevServer(\n options: DevServerOptions = {}\n): Promise<DevServer> {\n const {\n port = 3377,\n host = \"localhost\",\n autoOpen = true,\n workflowName = \"Workflow\",\n timeTravel = true,\n heatmap = true,\n maxSnapshots = 1000,\n } = options;\n\n // Suppress unused variable warning - workflowName reserved for future use\n void workflowName;\n\n // Create time-travel controller and performance analyzer\n const ttController = createTimeTravelController({ maxSnapshots });\n const analyzer = createPerformanceAnalyzer();\n\n // Server state\n let httpServer: HTTPServer | null = null;\n let wsServer: WebSocketServerLike | null = null;\n let actualPort = port;\n let isRunning = false;\n\n /**\n * Broadcast a message to all connected WebSocket clients.\n */\n function broadcast(message: ServerMessage): void {\n if (!wsServer) return;\n const data = JSON.stringify(message);\n for (const client of wsServer.clients) {\n if (client.readyState === client.OPEN) {\n client.send(data);\n }\n }\n }\n\n /**\n * Handle incoming WebSocket message.\n */\n function handleClientMessage(ws: WebSocketLike, raw: Buffer | string): void {\n try {\n const msg = JSON.parse(\n typeof raw === \"string\" ? raw : raw.toString()\n ) as WebVisualizerMessage;\n\n switch (msg.type) {\n case \"time_travel_seek\": {\n const payload = msg.payload as { index: number };\n const ir = ttController.seek(payload.index);\n if (ir) {\n ws.send(JSON.stringify({ type: \"ir_update\", payload: ir }));\n }\n break;\n }\n case \"time_travel_play\": {\n const payload = msg.payload as { speed?: number } | undefined;\n ttController.play(payload?.speed);\n break;\n }\n case \"time_travel_pause\":\n ttController.pause();\n break;\n case \"time_travel_step_forward\": {\n const ir = ttController.stepForward();\n if (ir) {\n ws.send(JSON.stringify({ type: \"ir_update\", payload: ir }));\n }\n break;\n }\n case \"time_travel_step_backward\": {\n const ir = ttController.stepBackward();\n if (ir) {\n ws.send(JSON.stringify({ type: \"ir_update\", payload: ir }));\n }\n break;\n }\n case \"request_snapshots\": {\n const snapshots = ttController.getSnapshots().map((s) => ({\n id: s.id,\n eventIndex: s.eventIndex,\n timestamp: s.timestamp,\n }));\n ws.send(JSON.stringify({ type: \"snapshots_list\", payload: snapshots }));\n break;\n }\n case \"toggle_heatmap\":\n case \"set_heatmap_metric\": {\n // Client handles these locally, but we can use to sync state\n break;\n }\n }\n } catch (e) {\n console.error(\"[DevServer] Failed to handle message:\", e);\n }\n }\n\n /**\n * Generate HTML for the current state.\n */\n function generatePage(): string {\n const ir = ttController.getCurrentIR();\n const wsUrl = `ws://${host}:${actualPort}`;\n\n return renderToHTML(ir, {\n interactive: true,\n timeTravel,\n heatmap,\n theme: \"auto\",\n layout: \"TB\",\n wsUrl,\n });\n }\n\n /**\n * Handle HTTP requests.\n */\n function handleRequest(req: IncomingMessage, res: ServerResponse): void {\n const url = req.url ?? \"/\";\n\n if (url === \"/\" || url === \"/index.html\") {\n res.writeHead(200, { \"Content-Type\": \"text/html; charset=utf-8\" });\n res.end(generatePage());\n return;\n }\n\n if (url === \"/api/snapshots\") {\n const snapshots = ttController.getSnapshots().map((s) => ({\n id: s.id,\n eventIndex: s.eventIndex,\n timestamp: s.timestamp,\n }));\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(snapshots));\n return;\n }\n\n if (url === \"/api/performance\") {\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(analyzer.exportData());\n return;\n }\n\n if (url === \"/api/ir\") {\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(ttController.getCurrentIR()));\n return;\n }\n\n // 404\n res.writeHead(404, { \"Content-Type\": \"text/plain\" });\n res.end(\"Not Found\");\n }\n\n /**\n * Try to dynamically import `ws` package.\n */\n async function loadWebSocketServer(): Promise<WebSocketServerLike | null> {\n try {\n // Dynamic import of optional peer dependency (ws is optional)\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore - ws is an optional peer dependency\n const wsModule = await import(\"ws\");\n const ws = wsModule as {\n WebSocketServer?: new (options: { noServer: boolean }) => WebSocketServerLike;\n default?: {\n WebSocketServer?: new (options: { noServer: boolean }) => WebSocketServerLike;\n };\n };\n const WebSocketServer = ws.WebSocketServer ?? ws.default?.WebSocketServer;\n if (!WebSocketServer) {\n console.warn(\"[DevServer] WebSocket server class not found in ws module\");\n return null;\n }\n return new WebSocketServer({ noServer: true });\n } catch {\n console.warn(\n \"[DevServer] ws package not installed. Live updates disabled.\\n\" +\n \"Install with: npm install ws\"\n );\n return null;\n }\n }\n\n /**\n * Open URL in browser using platform-specific command.\n * Uses execFile for safety (no shell injection possible).\n */\n function openBrowser(url: string): void {\n const platform = process.platform;\n\n // Validate URL format to ensure it's a localhost URL\n if (!url.startsWith(\"http://localhost:\") && !url.startsWith(\"http://127.0.0.1:\")) {\n console.warn(\"[DevServer] Refusing to open non-localhost URL\");\n return;\n }\n\n if (platform === \"darwin\") {\n execFile(\"open\", [url], (err) => {\n if (err) console.warn(\"[DevServer] Failed to open browser:\", err.message);\n });\n } else if (platform === \"win32\") {\n // On Windows, use cmd.exe with /c start to handle URLs properly\n execFile(\"cmd.exe\", [\"/c\", \"start\", \"\", url], (err) => {\n if (err) console.warn(\"[DevServer] Failed to open browser:\", err.message);\n });\n } else {\n // Linux and other Unix-like systems\n execFile(\"xdg-open\", [url], (err) => {\n if (err) console.warn(\"[DevServer] Failed to open browser:\", err.message);\n });\n }\n }\n\n // =============================================================================\n // Public Interface\n // =============================================================================\n\n async function start(): Promise<{ port: number; url: string }> {\n if (isRunning) {\n return { port: actualPort, url: `http://${host}:${actualPort}` };\n }\n\n // Create HTTP server\n httpServer = createServer(handleRequest);\n\n // Try to load WebSocket server\n wsServer = await loadWebSocketServer();\n\n if (wsServer) {\n // Handle WebSocket upgrade\n httpServer.on(\"upgrade\", (request, socket, head) => {\n if (!wsServer) return;\n\n // Manual WebSocket handshake\n const ws = wsServer as unknown as {\n handleUpgrade: (\n req: IncomingMessage,\n socket: unknown,\n head: Buffer,\n callback: (ws: WebSocketLike) => void\n ) => void;\n };\n\n ws.handleUpgrade(request, socket, head, (client) => {\n // Send initial state\n client.send(\n JSON.stringify({\n type: \"ir_update\",\n payload: ttController.getCurrentIR(),\n })\n );\n\n // Handle messages\n client.on(\"message\", (data) => handleClientMessage(client, data));\n client.on(\"error\", (err) => console.error(\"[DevServer] WS error:\", err));\n });\n });\n\n // Subscribe to time-travel state changes\n ttController.onStateChange((state) => {\n broadcast({ type: \"time_travel_state\", payload: state });\n });\n }\n\n // Start listening\n return new Promise((resolve, reject) => {\n if (!httpServer) {\n reject(new Error(\"HTTP server not initialized\"));\n return;\n }\n\n httpServer.on(\"error\", (err: NodeJS.ErrnoException) => {\n if (err.code === \"EADDRINUSE\") {\n // Try next port\n actualPort++;\n httpServer?.listen(actualPort, host);\n } else {\n reject(err);\n }\n });\n\n httpServer.on(\"listening\", () => {\n isRunning = true;\n const url = `http://${host}:${actualPort}`;\n console.log(`[DevServer] Visualizer running at ${url}`);\n\n if (autoOpen) {\n openBrowser(url);\n }\n\n resolve({ port: actualPort, url });\n });\n\n httpServer.listen(actualPort, host);\n });\n }\n\n async function stop(): Promise<void> {\n if (!isRunning) return;\n\n return new Promise((resolve) => {\n if (wsServer) {\n wsServer.close();\n wsServer = null;\n }\n\n if (httpServer) {\n httpServer.close(() => {\n httpServer = null;\n isRunning = false;\n resolve();\n });\n } else {\n resolve();\n }\n });\n }\n\n function handleEvent(event: WorkflowEvent<unknown>): void {\n // Forward to time-travel controller\n ttController.handleEvent(event);\n\n // Forward to performance analyzer\n analyzer.addEvent(event);\n\n // Broadcast IR update\n broadcast({ type: \"ir_update\", payload: ttController.getCurrentIR() });\n }\n\n function pushUpdate(ir: WorkflowIR): void {\n broadcast({ type: \"ir_update\", payload: ir });\n }\n\n function pushHeatmap(data: HeatmapData): void {\n // Convert Map to object for JSON serialization\n const heat: Record<string, number> = {};\n for (const [k, v] of data.heat) {\n heat[k] = v;\n }\n broadcast({\n type: \"performance_data\",\n payload: { ...data, heat },\n });\n }\n\n function complete(): void {\n // Finalize analyzer run\n analyzer.finalizeRun(\"current\");\n broadcast({ type: \"workflow_complete\", payload: null });\n }\n\n function getTimeTravel(): TimeTravelController {\n return ttController;\n }\n\n function getAnalyzer(): PerformanceAnalyzer {\n return analyzer;\n }\n\n function getCurrentIR(): WorkflowIR {\n return ttController.getCurrentIR();\n }\n\n return {\n start,\n stop,\n handleEvent,\n pushUpdate,\n pushHeatmap,\n complete,\n getTimeTravel,\n getAnalyzer,\n getCurrentIR,\n };\n}\n"],"mappings":"+kBAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,mBAAAE,EAAA,oBAAAC,GAAA,yBAAAC,GAAA,oBAAAC,EAAA,yBAAAC,GAAA,2BAAAC,GAAA,8BAAAC,GAAA,+BAAAC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C/B,EAGA,MAAO,IAAM,CACXH,EAAO,OAAS,CAClB,EAGA,UAAW,IAAM,CACf,IAAMC,EAAM/B,GAAiBC,CAAO,EACpC,QAAWmB,KAASU,EACdV,EAAM,KAAK,WAAW,WAAW,EACnCW,EAAI,oBAAoBX,CAAoE,EAE5FW,EAAI,YAAYX,CAA+B,EAGnD,OAAOW,EAAI,OAAO,CACpB,EAGA,YAAcL,GAAyB,CACrC,IAAMK,EAAM/B,GAAiBC,CAAO,EACpC,QAAWmB,KAASU,EACdV,EAAM,KAAK,WAAW,WAAW,EACnCW,EAAI,oBAAoBX,CAAoE,EAE5FW,EAAI,YAAYX,CAA+B,EAGnD,OAAOW,EAAI,SAASL,CAAM,CAC5B,CACF,CACF","names":["visualize_exports","__export","asciiRenderer","createDevServer","createEventCollector","createIRBuilder","createLiveVisualizer","createParallelDetector","createPerformanceAnalyzer","createTimeTravelController","createVisualizer","defaultColorScheme","detectParallelGroups","getHeatLevel","hasChildren","htmlRenderer","isDecisionNode","isParallelNode","isRaceNode","isSequenceNode","isStepNode","mermaidRenderer","renderToHTML","trackDecision","trackIf","trackSwitch","visualizeEvents","__toCommonJS","formatDuration","ms","minutes","seconds","generateId","hasRealScopeNodes","nodes","node","detectParallelGroups","options","minOverlapMs","maxGapMs","stepsWithTiming","nonStepNodes","i","a","b","groups","currentGroup","step","groupStart","s","groupEnd","startedTogether","hasTrueOverlap","overlapDuration","groupedNodes","group","position","children","startTs","endTs","parallelNode","deriveGroupState","originalIndex","g","c","createParallelDetector","createIRBuilder","options","detectParallel","parallelDetection","enableSnapshots","maxSnapshots","workflowId","workflowStartTs","workflowState","workflowError","workflowDurationMs","activeSteps","scopeStack","decisionStack","currentNodes","createdAt","lastUpdatedAt","hookState","snapshots","eventIndex","getStepId","event","generateId","addNode","node","decision","branch","firstBranch","captureSnapshot","ir","getIR","activeStepsCopy","id","step","snapshot","handleEvent","active","hookExec","handleScopeEvent","scope","deriveState","handleDecisionEvent","branchKey","existing","branches","b","children","c","getCurrentNodes","nodes","detectParallelGroups","root","hasHooks","reset","getSnapshots","getSnapshotAt","index","getIRAt","clearSnapshots","isStepNode","node","isSequenceNode","isParallelNode","isRaceNode","isDecisionNode","hasChildren","RESET","BOLD","DIM","FG_RED","FG_GREEN","FG_YELLOW","FG_BLUE","FG_GRAY","FG_WHITE","colorize","text","color","bold","dim","defaultColorScheme","getStateSymbol","state","getColoredSymbol","colors","symbol","colorByState","stripAnsi","str","BOX","HEAT_COLORS","RESET","getHeatColor","heat","applyHeatColor","text","color","SPARK_CHARS","renderSparkline","values","width","subset","min","range","v","normalized","index","padEnd","str","visibleLen","stripAnsi","padding","horizontalLine","title","titleText","remainingWidth","leftPad","rightPad","renderHookExecution","hook","label","colors","symbol","colorize","timing","dim","formatDuration","context","error","renderHooks","hooks","lines","asciiRenderer","ir","options","defaultColorScheme","innerWidth","workflowName","headerTitle","bold","hookLines","line","childLines","renderNodes","status","footer","colorByState","nodes","depth","node","isStepNode","renderStepNode","isParallelNode","renderParallelNode","isRaceNode","renderRaceNode","isDecisionNode","renderDecisionNode","getColoredSymbol","name","enhanced","nodeId","nameColored","inputStr","outputStr","timingStr","timingDisplay","history","timeoutInfo","hookExec","hookSymbol","hookTiming","indent","mode","i","child","prefix","nestedLines","winnerSuffix","condition","decisionValue","branchTaken","branch","branchSymbol","branchColor","branchLabel","branchCondition","flattenNodes","nodes","result","node","branch","percentile","sortedValues","p","index","getHeatLevel","heat","createPerformanceAnalyzer","timingData","retryData","errorData","timeoutData","currentRunEvents","getNodeId","event","processEvents","events","stepState","id","state","timeout","timings","retry","error","addRun","run","addEvent","finalizeRun","_runId","computePerformance","nodeId","sorted","a","b","mean","variance","acc","t","getNodePerformance","getHeatmap","ir","metric","allNodes","values","perf","value","vals","v","min","max","range","getAllPerformance","getSlowestNodes","limit","getErrorProneNodes","getRetryProneNodes","exportData","importData","json","data","k","clear","getStyleDefinitions","getHeatmapStyleDefinitions","getHeatClass","level","getHookStyleDefinitions","renderHooks","hooks","lines","options","lastHookId","hookId","state","icon","timing","formatDuration","context","nodeCounter","generateNodeId","prefix","resetNodeCounter","escapeMermaidText","text","escapeSubgraphName","mermaidRenderer","ir","enhanced","hookExitId","startId","prevNodeId","child","result","renderNode","endId","endIcon","endLabel","endShape","endClass","getStyleDefinitions","getHeatmapStyleDefinitions","node","isStepNode","renderStepNode","isParallelNode","renderParallelNode","isRaceNode","renderRaceNode","isDecisionNode","renderDecisionNode","id","label","stateIcon","ioInfo","inputStr","outputStr","retryInfo","timeoutStr","hookInfo","hookExec","hookIcon","hookTiming","escapedLabel","nodeClass","nodeId","heat","level","getHeatLevel","getHeatClass","shape","subgraphId","forkId","joinId","name","modeLabel","note","childExitIds","exitId","stateClass","winnerExitId","isWinner","decisionId","condition","decisionValue","decisionLabel","branchExitIds","takenBranchExitId","branch","branchId","branchLabelText","branchLabel","branchClass","edgeLabel","prevId","generateStyles","theme","lightColors","darkColors","generateThemeVars","colors","themeCSS","generateClientScript","options","NODE_WIDTH","NODE_HEIGHT","NODE_SPACING_H","NODE_SPACING_V","CONTAINER_PADDING","layoutWorkflow","nodes","direction","isVertical","layoutNodes","currentX","currentY","maxWidth","maxHeight","node","result","layoutFlowNode","x","y","_isVertical","isStepNode","isParallelNode","isRaceNode","containerType","label","children","innerX","innerY","innerMaxWidth","innerMaxHeight","child","containerWidth","containerHeight","isDecisionNode","branch","renderLayoutNodeSVG","showTimings","renderContainerSVG","renderStepSVG","timing","formatDuration","escapeAttr","escapeXml","truncate","childrenSVG","c","renderEdgesSVG","edges","i","from","to","x1","y1","x2","y2","generateHTML","ir","options","layout","svgWidth","svgHeight","nodesSVG","n","edgesSVG","workflowName","css","generateStyles","js","generateClientScript","serializeWorkflowData","buildWorkflowData","collectNodes","flowNodes","str","maxLen","defaultHTMLOptions","htmlRenderer","htmlOptions","renderToHTML","fullOptions","ANSI","n","createLiveVisualizer","options","workflowName","detectParallel","showTimings","showKeys","customColors","stream","updateInterval","builder","createIRBuilder","renderer","asciiRenderer","renderOptions","defaultColorScheme","isRunning","lastOutput","lastLineCount","throttleTimeout","pendingUpdate","write","text","redraw","ir","getIR","output","scheduleRedraw","handleEvent","event","handleScopeEvent","handleDecisionEvent","render","start","stop","refresh","reset","trackDecision","decisionId","options","condition","value","name","workflowId","emit","startTs","branchTaken","branches","takeBranch","label","taken","branchCondition","end","durationMs","trackIf","tracker","trackSwitch","caseValue","createTimeTravelController","options","maxSnapshots","autoRecord","builderOptions","builder","createIRBuilder","state","listeners","playbackTimer","notifyListeners","currentState","getState","listener","syncFromBuilder","handleEvent","event","seek","index","snapshots","stepForward","stepBackward","play","speed","playNext","current","scaledDelay","pause","getCurrentIR","getIRAt","getSnapshots","getSnapshotAt","onStateChange","callback","startRecording","stopRecording","reset","getBuilder","import_node_http","import_node_child_process","createDevServer","options","port","host","autoOpen","workflowName","timeTravel","heatmap","maxSnapshots","ttController","createTimeTravelController","analyzer","createPerformanceAnalyzer","httpServer","wsServer","actualPort","isRunning","broadcast","message","data","client","handleClientMessage","ws","raw","msg","payload","ir","snapshots","s","e","generatePage","wsUrl","renderToHTML","handleRequest","req","res","url","loadWebSocketServer","WebSocketServer","openBrowser","platform","err","start","request","socket","head","state","resolve","reject","stop","handleEvent","event","pushUpdate","pushHeatmap","heat","k","v","complete","getTimeTravel","getAnalyzer","getCurrentIR","createVisualizer","options","workflowName","detectParallel","showTimings","showKeys","customColors","builder","createIRBuilder","updateCallbacks","ascii","asciiRenderer","mermaid","mermaidRenderer","renderOptions","defaultColorScheme","notifyUpdate","ir","callback","handleEvent","event","handleScopeEvent","handleDecisionEvent","getIR","render","renderAs","format","reset","onUpdate","visualizeEvents","events","viz","createEventCollector","e"]}