@jagreehal/workflow 1.1.0 → 1.3.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.
@@ -0,0 +1,805 @@
1
+ import { ScopeType, WorkflowEvent } from './core.js';
2
+
3
+ /**
4
+ * Workflow Visualization - Intermediate Representation Types
5
+ *
6
+ * The IR (Intermediate Representation) is a DSL that represents workflow
7
+ * execution structure. Events are converted to IR, which can then be
8
+ * rendered to various output formats (ASCII, Mermaid, JSON, etc.).
9
+ */
10
+ /**
11
+ * Execution state of a step with semantic meaning for visualization.
12
+ *
13
+ * Color mapping:
14
+ * - pending → white/clear (not started)
15
+ * - running → yellow (currently executing)
16
+ * - success → green (completed successfully)
17
+ * - error → red (failed with error)
18
+ * - aborted → gray (cancelled, e.g., in race)
19
+ * - cached → blue (served from cache)
20
+ * - skipped → dim gray (not executed due to conditional logic)
21
+ */
22
+ type StepState = "pending" | "running" | "success" | "error" | "aborted" | "cached" | "skipped";
23
+ /**
24
+ * Base properties shared by all IR nodes.
25
+ */
26
+ interface BaseNode {
27
+ /** Unique identifier for this node */
28
+ id: string;
29
+ /** Human-readable name (from step options or inferred) */
30
+ name?: string;
31
+ /** Cache key if this is a keyed step */
32
+ key?: string;
33
+ /** Current execution state */
34
+ state: StepState;
35
+ /** Timestamp when execution started */
36
+ startTs?: number;
37
+ /** Timestamp when execution ended */
38
+ endTs?: number;
39
+ /** Duration in milliseconds */
40
+ durationMs?: number;
41
+ /** Error value if state is 'error' */
42
+ error?: unknown;
43
+ /** Input value that triggered this step (for decision understanding) */
44
+ input?: unknown;
45
+ /** Output value from this step (for decision understanding) */
46
+ output?: unknown;
47
+ }
48
+ /**
49
+ * A single step execution node.
50
+ */
51
+ interface StepNode extends BaseNode {
52
+ type: "step";
53
+ }
54
+ /**
55
+ * Sequential execution - steps run one after another.
56
+ * This is the implicit structure when steps are awaited in sequence.
57
+ */
58
+ interface SequenceNode extends BaseNode {
59
+ type: "sequence";
60
+ children: FlowNode[];
61
+ }
62
+ /**
63
+ * Parallel execution - all branches run simultaneously.
64
+ * Created by allAsync() or allSettledAsync().
65
+ */
66
+ interface ParallelNode extends BaseNode {
67
+ type: "parallel";
68
+ children: FlowNode[];
69
+ /**
70
+ * Execution mode:
71
+ * - 'all': Fails on first error (allAsync)
72
+ * - 'allSettled': Collects all results (allSettledAsync)
73
+ */
74
+ mode: "all" | "allSettled";
75
+ }
76
+ /**
77
+ * Race execution - first to complete wins.
78
+ * Created by anyAsync().
79
+ */
80
+ interface RaceNode extends BaseNode {
81
+ type: "race";
82
+ children: FlowNode[];
83
+ /** ID of the winning branch (first to succeed) */
84
+ winnerId?: string;
85
+ }
86
+ /**
87
+ * Decision point - conditional branch (if/switch).
88
+ * Shows which branch was taken and why.
89
+ */
90
+ interface DecisionNode extends BaseNode {
91
+ type: "decision";
92
+ /** Condition that was evaluated (e.g., "user.role === 'admin'") */
93
+ condition?: string;
94
+ /** Value that was evaluated (the input to the decision) */
95
+ decisionValue?: unknown;
96
+ /** Which branch was taken (true/false, or the matched case) */
97
+ branchTaken?: string | boolean;
98
+ /** All possible branches (including skipped ones) */
99
+ branches: DecisionBranch[];
100
+ }
101
+ /**
102
+ * A branch in a decision node.
103
+ */
104
+ interface DecisionBranch {
105
+ /** Label for this branch (e.g., "if", "else", "case 'admin'") */
106
+ label: string;
107
+ /** Condition that would trigger this branch */
108
+ condition?: string;
109
+ /** Whether this branch was taken */
110
+ taken: boolean;
111
+ /** Steps in this branch */
112
+ children: FlowNode[];
113
+ }
114
+ /**
115
+ * Union of all flow node types.
116
+ */
117
+ type FlowNode = StepNode | SequenceNode | ParallelNode | RaceNode | DecisionNode;
118
+ /**
119
+ * Root node representing the entire workflow.
120
+ */
121
+ interface WorkflowNode extends BaseNode {
122
+ type: "workflow";
123
+ /** Correlation ID from the workflow execution */
124
+ workflowId: string;
125
+ /** Child nodes (steps, parallel blocks, etc.) */
126
+ children: FlowNode[];
127
+ }
128
+ /**
129
+ * Complete workflow intermediate representation.
130
+ * This is the main data structure produced by the IR builder.
131
+ */
132
+ interface WorkflowIR {
133
+ /** Root workflow node */
134
+ root: WorkflowNode;
135
+ /** Metadata about the IR */
136
+ metadata: {
137
+ /** When the IR was first created */
138
+ createdAt: number;
139
+ /** When the IR was last updated */
140
+ lastUpdatedAt: number;
141
+ };
142
+ }
143
+
144
+ /**
145
+ * Event emitted when entering a parallel/race scope.
146
+ * This matches the scope_start event in WorkflowEvent.
147
+ */
148
+ interface ScopeStartEvent {
149
+ type: "scope_start";
150
+ workflowId: string;
151
+ scopeId: string;
152
+ scopeType: ScopeType;
153
+ name?: string;
154
+ ts: number;
155
+ }
156
+ /**
157
+ * Event emitted when exiting a parallel/race scope.
158
+ */
159
+ interface ScopeEndEvent {
160
+ type: "scope_end";
161
+ workflowId: string;
162
+ scopeId: string;
163
+ ts: number;
164
+ durationMs: number;
165
+ /** For race scopes, the ID of the winning branch */
166
+ winnerId?: string;
167
+ }
168
+ /**
169
+ * Event emitted when a decision point is encountered.
170
+ * Use this to track conditional logic (if/switch).
171
+ */
172
+ interface DecisionStartEvent {
173
+ type: "decision_start";
174
+ workflowId: string;
175
+ decisionId: string;
176
+ /** Condition being evaluated (e.g., "user.role === 'admin'") */
177
+ condition?: string;
178
+ /** Value being evaluated */
179
+ decisionValue?: unknown;
180
+ /** Name/label for this decision point */
181
+ name?: string;
182
+ ts: number;
183
+ }
184
+ /**
185
+ * Event emitted when a decision branch is taken.
186
+ */
187
+ interface DecisionBranchEvent {
188
+ type: "decision_branch";
189
+ workflowId: string;
190
+ decisionId: string;
191
+ /** Label for this branch (e.g., "if", "else", "case 'admin'") */
192
+ branchLabel: string;
193
+ /** Condition for this branch */
194
+ condition?: string;
195
+ /** Whether this branch was taken */
196
+ taken: boolean;
197
+ ts: number;
198
+ }
199
+ /**
200
+ * Event emitted when a decision point completes.
201
+ */
202
+ interface DecisionEndEvent {
203
+ type: "decision_end";
204
+ workflowId: string;
205
+ decisionId: string;
206
+ /** Which branch was taken */
207
+ branchTaken?: string | boolean;
208
+ ts: number;
209
+ durationMs: number;
210
+ }
211
+ /**
212
+ * Event emitted when a step is skipped due to conditional logic.
213
+ */
214
+ interface StepSkippedEvent {
215
+ type: "step_skipped";
216
+ workflowId: string;
217
+ stepKey?: string;
218
+ name?: string;
219
+ /** Reason why this step was skipped (e.g., "condition was false") */
220
+ reason?: string;
221
+ /** The decision that caused this skip */
222
+ decisionId?: string;
223
+ ts: number;
224
+ }
225
+ /**
226
+ * Union of scope-related events.
227
+ */
228
+ type ScopeEvent = ScopeStartEvent | ScopeEndEvent;
229
+ /**
230
+ * Union of decision-related events.
231
+ */
232
+ type DecisionEvent = DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent;
233
+ /**
234
+ * Color scheme for rendering step states.
235
+ */
236
+ interface ColorScheme {
237
+ pending: string;
238
+ running: string;
239
+ success: string;
240
+ error: string;
241
+ aborted: string;
242
+ cached: string;
243
+ skipped: string;
244
+ }
245
+ /**
246
+ * Options passed to renderers.
247
+ */
248
+ interface RenderOptions {
249
+ /** Show timing information (duration) */
250
+ showTimings: boolean;
251
+ /** Show step cache keys */
252
+ showKeys: boolean;
253
+ /** Terminal width for ASCII renderer */
254
+ terminalWidth?: number;
255
+ /** Color scheme */
256
+ colors: ColorScheme;
257
+ }
258
+ /**
259
+ * Renderer interface - transforms IR to output format.
260
+ */
261
+ interface Renderer {
262
+ /** Unique identifier for this renderer */
263
+ readonly name: string;
264
+ /** Render IR to string output */
265
+ render(ir: WorkflowIR, options: RenderOptions): string;
266
+ /** Whether this renderer supports live (incremental) updates */
267
+ supportsLive?: boolean;
268
+ /** Render incremental update (optional) */
269
+ renderUpdate?(ir: WorkflowIR, changedNodes: FlowNode[], options: RenderOptions): string;
270
+ }
271
+ /**
272
+ * Output format for rendering.
273
+ */
274
+ type OutputFormat = "ascii" | "mermaid" | "json";
275
+ /**
276
+ * Options for creating a visualizer.
277
+ */
278
+ interface VisualizerOptions {
279
+ /** Name for the workflow in visualizations */
280
+ workflowName?: string;
281
+ /** Enable parallel detection heuristics (default: true) */
282
+ detectParallel?: boolean;
283
+ /** Show timing information (default: true) */
284
+ showTimings?: boolean;
285
+ /** Show step keys (default: false) */
286
+ showKeys?: boolean;
287
+ /** Custom color scheme */
288
+ colors?: Partial<ColorScheme>;
289
+ }
290
+ /**
291
+ * Options for live visualization.
292
+ */
293
+ interface LiveVisualizerOptions extends VisualizerOptions {
294
+ /** Output stream (default: process.stdout) */
295
+ stream?: NodeJS.WriteStream;
296
+ /** Update interval in ms (default: 100) */
297
+ updateInterval?: number;
298
+ }
299
+ /**
300
+ * Check if a node is a StepNode.
301
+ */
302
+ declare function isStepNode(node: FlowNode): node is StepNode;
303
+ /**
304
+ * Check if a node is a SequenceNode.
305
+ */
306
+ declare function isSequenceNode(node: FlowNode): node is SequenceNode;
307
+ /**
308
+ * Check if a node is a ParallelNode.
309
+ */
310
+ declare function isParallelNode(node: FlowNode): node is ParallelNode;
311
+ /**
312
+ * Check if a node is a RaceNode.
313
+ */
314
+ declare function isRaceNode(node: FlowNode): node is RaceNode;
315
+ /**
316
+ * Check if a node is a DecisionNode.
317
+ */
318
+ declare function isDecisionNode(node: FlowNode): node is DecisionNode;
319
+ /**
320
+ * Check if a node has children.
321
+ */
322
+ declare function hasChildren(node: FlowNode): node is SequenceNode | ParallelNode | RaceNode | DecisionNode;
323
+
324
+ /**
325
+ * Parallel Detection - Heuristic detection of parallel execution from timing.
326
+ *
327
+ * When steps overlap in time (one starts before another ends), they are
328
+ * likely running in parallel. This module detects such patterns and
329
+ * groups overlapping steps into ParallelNode structures.
330
+ */
331
+
332
+ /**
333
+ * Options for parallel detection.
334
+ */
335
+ interface ParallelDetectorOptions {
336
+ /**
337
+ * Minimum overlap in milliseconds to consider steps parallel.
338
+ * Default: 0 (any overlap counts)
339
+ */
340
+ minOverlapMs?: number;
341
+ /**
342
+ * Maximum gap in milliseconds to still consider steps as part of same parallel group.
343
+ * Default: 5 (steps starting within 5ms are grouped)
344
+ */
345
+ maxGapMs?: number;
346
+ }
347
+ /**
348
+ * Group overlapping steps into parallel nodes.
349
+ *
350
+ * Algorithm:
351
+ * 1. Sort steps by start time
352
+ * 2. For each step, check if it overlaps with existing parallel groups
353
+ * 3. If it overlaps, add to group; otherwise start new sequence
354
+ * 4. Merge overlapping groups when step bridges them
355
+ *
356
+ * Note: If real scope nodes (from scope_start/scope_end) are present,
357
+ * heuristic detection is skipped to avoid conflicts.
358
+ */
359
+ declare function detectParallelGroups(nodes: FlowNode[], options?: ParallelDetectorOptions): FlowNode[];
360
+ /**
361
+ * Create a parallel detector that processes nodes.
362
+ */
363
+ declare function createParallelDetector(options?: ParallelDetectorOptions): {
364
+ /**
365
+ * Process nodes and group overlapping ones into parallel nodes.
366
+ */
367
+ detect: (nodes: FlowNode[]) => FlowNode[];
368
+ };
369
+
370
+ /**
371
+ * IR Builder - Converts workflow events to Intermediate Representation.
372
+ *
373
+ * The builder maintains state as events arrive, constructing a tree
374
+ * representation of the workflow execution that can be rendered.
375
+ */
376
+
377
+ /**
378
+ * Options for the IR builder.
379
+ */
380
+ interface IRBuilderOptions {
381
+ /**
382
+ * Enable heuristic parallel detection based on timing.
383
+ * When true, overlapping steps are grouped into ParallelNodes.
384
+ * Default: true
385
+ */
386
+ detectParallel?: boolean;
387
+ /**
388
+ * Options for parallel detection.
389
+ */
390
+ parallelDetection?: ParallelDetectorOptions;
391
+ }
392
+ /**
393
+ * Creates an IR builder that processes workflow events.
394
+ */
395
+ declare function createIRBuilder(options?: IRBuilderOptions): {
396
+ handleEvent: (event: WorkflowEvent<unknown>) => void;
397
+ handleScopeEvent: (event: ScopeStartEvent | ScopeEndEvent) => void;
398
+ handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;
399
+ getIR: () => WorkflowIR;
400
+ reset: () => void;
401
+ /** Check if there are active (running) steps */
402
+ readonly hasActiveSteps: boolean;
403
+ /** Get the current workflow state */
404
+ readonly state: StepState;
405
+ };
406
+
407
+ /**
408
+ * ANSI color utilities for terminal output.
409
+ */
410
+
411
+ /**
412
+ * Default ANSI color scheme for step states.
413
+ */
414
+ declare const defaultColorScheme: ColorScheme;
415
+
416
+ /**
417
+ * ASCII Terminal Renderer
418
+ *
419
+ * Renders the workflow IR as ASCII art with box-drawing characters
420
+ * and ANSI colors for terminal display.
421
+ */
422
+
423
+ /**
424
+ * Create the ASCII terminal renderer.
425
+ */
426
+ declare function asciiRenderer(): Renderer;
427
+
428
+ /**
429
+ * Mermaid Diagram Renderer
430
+ *
431
+ * Renders the workflow IR as a Mermaid flowchart diagram.
432
+ * Supports sequential flows, parallel (subgraph), and race patterns.
433
+ */
434
+
435
+ /**
436
+ * Create the Mermaid diagram renderer.
437
+ */
438
+ declare function mermaidRenderer(): Renderer;
439
+
440
+ /**
441
+ * Live Visualizer - Real-time terminal updates during workflow execution.
442
+ *
443
+ * Uses ANSI escape codes to update the terminal in-place, showing
444
+ * workflow progress as it happens.
445
+ */
446
+
447
+ /**
448
+ * Live visualizer with real-time terminal updates.
449
+ */
450
+ interface LiveVisualizer {
451
+ /** Process a workflow event */
452
+ handleEvent: (event: WorkflowEvent<unknown>) => void;
453
+ /** Process a scope event */
454
+ handleScopeEvent: (event: ScopeStartEvent | ScopeEndEvent) => void;
455
+ /** Process a decision event */
456
+ handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;
457
+ /** Get current IR state */
458
+ getIR: () => WorkflowIR;
459
+ /** Render current state to string (without terminal output) */
460
+ render: () => string;
461
+ /** Start live rendering to terminal */
462
+ start: () => void;
463
+ /** Stop live rendering */
464
+ stop: () => void;
465
+ /** Force an immediate redraw */
466
+ refresh: () => void;
467
+ /** Reset state for a new workflow */
468
+ reset: () => void;
469
+ }
470
+ /**
471
+ * Create a live visualizer for real-time terminal updates.
472
+ *
473
+ * @example
474
+ * ```typescript
475
+ * const live = createLiveVisualizer({ workflowName: 'my-workflow' });
476
+ * const workflow = createWorkflow(deps, { onEvent: live.handleEvent });
477
+ *
478
+ * live.start();
479
+ * await workflow(async (step) => { ... });
480
+ * live.stop();
481
+ * ```
482
+ */
483
+ declare function createLiveVisualizer(options?: LiveVisualizerOptions): LiveVisualizer;
484
+
485
+ /**
486
+ * Decision Tracker - Helper for tracking conditional logic in workflows.
487
+ *
488
+ * This module provides utilities to track decision points (if/switch statements)
489
+ * so they can be visualized in workflow diagrams.
490
+ *
491
+ * @example
492
+ * ```typescript
493
+ * import { trackDecision } from '@jagreehal/workflow/visualize';
494
+ *
495
+ * const workflow = createWorkflow(deps, {
496
+ * onEvent: (event) => {
497
+ * // Pass events to visualizer
498
+ * viz.handleEvent(event);
499
+ * }
500
+ * });
501
+ *
502
+ * await workflow(async (step) => {
503
+ * const user = await step(fetchUser(id));
504
+ *
505
+ * // Track a decision point
506
+ * const decision = trackDecision('user-role-check', {
507
+ * condition: 'user.role === "admin"',
508
+ * value: user.role
509
+ * });
510
+ *
511
+ * if (user.role === 'admin') {
512
+ * decision.takeBranch('admin', true);
513
+ * await step(processAdminAction(user));
514
+ * } else {
515
+ * decision.takeBranch('user', false);
516
+ * await step(processUserAction(user));
517
+ * }
518
+ *
519
+ * decision.end();
520
+ * });
521
+ * ```
522
+ */
523
+
524
+ /**
525
+ * Options for creating a decision tracker.
526
+ */
527
+ interface DecisionTrackerOptions {
528
+ /** Condition being evaluated (e.g., "user.role === 'admin'") */
529
+ condition?: string;
530
+ /** Value being evaluated */
531
+ value?: unknown;
532
+ /** Name/label for this decision point */
533
+ name?: string;
534
+ /** Workflow ID (auto-generated if not provided) */
535
+ workflowId?: string;
536
+ /** Event emitter function */
537
+ emit?: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;
538
+ }
539
+ /**
540
+ * Track a decision point in your workflow.
541
+ *
542
+ * Use this to annotate conditional logic (if/switch) so it appears
543
+ * in workflow visualizations.
544
+ *
545
+ * @param decisionId - Unique identifier for this decision
546
+ * @param options - Decision tracking options
547
+ * @returns A tracker object with methods to track branches
548
+ *
549
+ * @example
550
+ * ```typescript
551
+ * const decision = trackDecision('check-role', {
552
+ * condition: 'user.role === "admin"',
553
+ * value: user.role,
554
+ * emit: (event) => viz.handleDecisionEvent(event)
555
+ * });
556
+ *
557
+ * if (user.role === 'admin') {
558
+ * decision.takeBranch('admin', true);
559
+ * // ... admin logic
560
+ * } else {
561
+ * decision.takeBranch('user', false);
562
+ * // ... user logic
563
+ * }
564
+ *
565
+ * decision.end();
566
+ * ```
567
+ */
568
+ declare function trackDecision(decisionId: string, options?: DecisionTrackerOptions): DecisionTracker;
569
+ /**
570
+ * Decision tracker instance.
571
+ */
572
+ interface DecisionTracker {
573
+ /**
574
+ * Mark that a branch was taken or skipped.
575
+ * @param label - Label for this branch (e.g., "if", "else", "case 'admin'")
576
+ * @param taken - Whether this branch was executed
577
+ * @param branchCondition - Optional condition for this specific branch
578
+ */
579
+ takeBranch(label: string, taken: boolean, branchCondition?: string): void;
580
+ /**
581
+ * End the decision tracking.
582
+ * Call this after all branches have been evaluated.
583
+ */
584
+ end(): void;
585
+ /**
586
+ * Get which branch was taken.
587
+ */
588
+ getBranchTaken(): string | boolean | undefined;
589
+ /**
590
+ * Get all branches (taken and skipped).
591
+ */
592
+ getBranches(): Array<{
593
+ label: string;
594
+ condition?: string;
595
+ taken: boolean;
596
+ }>;
597
+ }
598
+ /**
599
+ * Track a simple if/else decision.
600
+ *
601
+ * @example
602
+ * ```typescript
603
+ * const decision = trackIf('check-admin', user.role === 'admin', {
604
+ * condition: 'user.role === "admin"',
605
+ * value: user.role,
606
+ * emit: (e) => viz.handleDecisionEvent(e)
607
+ * });
608
+ *
609
+ * if (decision.condition) {
610
+ * decision.then();
611
+ * // admin logic
612
+ * } else {
613
+ * decision.else();
614
+ * // user logic
615
+ * }
616
+ *
617
+ * decision.end();
618
+ * ```
619
+ */
620
+ declare function trackIf(decisionId: string, condition: boolean, options?: Omit<DecisionTrackerOptions, "value"> & {
621
+ value?: unknown;
622
+ }): IfTracker;
623
+ /**
624
+ * If tracker with convenience methods.
625
+ */
626
+ interface IfTracker extends DecisionTracker {
627
+ /** The condition value */
628
+ condition: boolean;
629
+ /** Mark the "if" branch as taken */
630
+ then(): void;
631
+ /** Mark the "else" branch as taken */
632
+ else(): void;
633
+ }
634
+ /**
635
+ * Track a switch statement decision.
636
+ *
637
+ * @example
638
+ * ```typescript
639
+ * const decision = trackSwitch('process-type', user.type, {
640
+ * emit: (e) => viz.handleDecisionEvent(e)
641
+ * });
642
+ *
643
+ * switch (user.type) {
644
+ * case 'admin':
645
+ * decision.case('admin', true);
646
+ * // admin logic
647
+ * break;
648
+ * case 'user':
649
+ * decision.case('user', true);
650
+ * // user logic
651
+ * break;
652
+ * default:
653
+ * decision.default(true);
654
+ * // default logic
655
+ * }
656
+ *
657
+ * decision.end();
658
+ * ```
659
+ */
660
+ declare function trackSwitch(decisionId: string, value: unknown, options?: Omit<DecisionTrackerOptions, "value">): SwitchTracker;
661
+ /**
662
+ * Switch tracker with convenience methods.
663
+ */
664
+ interface SwitchTracker extends DecisionTracker {
665
+ /** The value being switched on */
666
+ value: unknown;
667
+ /** Mark a case branch */
668
+ case(caseValue: string | number, taken: boolean): void;
669
+ /** Mark the default branch */
670
+ default(taken: boolean): void;
671
+ }
672
+
673
+ /**
674
+ * Workflow Visualization Module
675
+ *
676
+ * Provides tools for visualizing workflow execution with color-coded
677
+ * step states and support for parallel/race operations.
678
+ *
679
+ * @example
680
+ * ```typescript
681
+ * import { createVisualizer } from '@jagreehal/workflow/visualize';
682
+ *
683
+ * const viz = createVisualizer({ workflowName: 'checkout' });
684
+ * const workflow = createWorkflow(deps, { onEvent: viz.handleEvent });
685
+ *
686
+ * await workflow(async (step) => {
687
+ * await step(() => validateCart(cart), 'Validate cart');
688
+ * await step(() => processPayment(payment), 'Process payment');
689
+ * });
690
+ *
691
+ * console.log(viz.render());
692
+ * ```
693
+ */
694
+
695
+ /**
696
+ * Workflow visualizer that processes events and renders output.
697
+ */
698
+ interface WorkflowVisualizer {
699
+ /** Process a workflow event */
700
+ handleEvent: (event: WorkflowEvent<unknown>) => void;
701
+ /** Process a scope event (parallel/race) */
702
+ handleScopeEvent: (event: ScopeStartEvent | ScopeEndEvent) => void;
703
+ /** Process a decision event (conditional branches) */
704
+ handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;
705
+ /** Get current IR state */
706
+ getIR: () => WorkflowIR;
707
+ /** Render current state using the default renderer */
708
+ render: () => string;
709
+ /** Render to a specific format */
710
+ renderAs: (format: OutputFormat) => string;
711
+ /** Reset state for a new workflow */
712
+ reset: () => void;
713
+ /** Subscribe to IR updates (for live visualization) */
714
+ onUpdate: (callback: (ir: WorkflowIR) => void) => () => void;
715
+ }
716
+ /**
717
+ * Create a workflow visualizer.
718
+ *
719
+ * @example
720
+ * ```typescript
721
+ * const viz = createVisualizer({ workflowName: 'my-workflow' });
722
+ *
723
+ * const workflow = createWorkflow(deps, {
724
+ * onEvent: viz.handleEvent,
725
+ * });
726
+ *
727
+ * await workflow(async (step) => { ... });
728
+ *
729
+ * console.log(viz.render());
730
+ * ```
731
+ */
732
+ declare function createVisualizer(options?: VisualizerOptions): WorkflowVisualizer;
733
+ /**
734
+ * Union type for all collectable/visualizable events (workflow + decision).
735
+ */
736
+ type CollectableEvent = WorkflowEvent<unknown> | DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent;
737
+ /**
738
+ * Visualize collected events (post-execution).
739
+ *
740
+ * Supports both workflow events (from onEvent) and decision events
741
+ * (from trackDecision/trackIf/trackSwitch).
742
+ *
743
+ * @example
744
+ * ```typescript
745
+ * const events: CollectableEvent[] = [];
746
+ * const workflow = createWorkflow(deps, {
747
+ * onEvent: (e) => events.push(e),
748
+ * });
749
+ *
750
+ * await workflow(async (step) => {
751
+ * const decision = trackIf('check', condition, {
752
+ * emit: (e) => events.push(e),
753
+ * });
754
+ * // ...
755
+ * });
756
+ *
757
+ * console.log(visualizeEvents(events));
758
+ * ```
759
+ */
760
+ declare function visualizeEvents(events: CollectableEvent[], options?: VisualizerOptions): string;
761
+ /**
762
+ * Create an event collector for later visualization.
763
+ *
764
+ * Supports both workflow events (from onEvent) and decision events
765
+ * (from trackDecision/trackIf/trackSwitch).
766
+ *
767
+ * @example
768
+ * ```typescript
769
+ * const collector = createEventCollector();
770
+ *
771
+ * const workflow = createWorkflow(deps, {
772
+ * onEvent: collector.handleEvent,
773
+ * });
774
+ *
775
+ * await workflow(async (step) => {
776
+ * // Decision events can also be collected
777
+ * const decision = trackIf('check', condition, {
778
+ * emit: collector.handleDecisionEvent,
779
+ * });
780
+ * // ...
781
+ * });
782
+ *
783
+ * console.log(collector.visualize());
784
+ * ```
785
+ */
786
+ declare function createEventCollector(options?: VisualizerOptions): {
787
+ /** Handle a workflow event */
788
+ handleEvent: (event: WorkflowEvent<unknown>) => void;
789
+ /** Handle a decision event */
790
+ handleDecisionEvent: (event: DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent) => void;
791
+ /** Get all collected events */
792
+ getEvents: () => CollectableEvent[];
793
+ /** Get workflow events only */
794
+ getWorkflowEvents: () => WorkflowEvent<unknown>[];
795
+ /** Get decision events only */
796
+ getDecisionEvents: () => (DecisionStartEvent | DecisionBranchEvent | DecisionEndEvent)[];
797
+ /** Clear collected events */
798
+ clear: () => void;
799
+ /** Visualize collected events */
800
+ visualize: () => string;
801
+ /** Visualize in a specific format */
802
+ visualizeAs: (format: OutputFormat) => string;
803
+ };
804
+
805
+ export { type BaseNode, type CollectableEvent, type ColorScheme, type DecisionBranch, type DecisionBranchEvent, type DecisionEndEvent, type DecisionEvent, type DecisionNode, type DecisionStartEvent, type DecisionTracker, type FlowNode, type IRBuilderOptions, type IfTracker, type LiveVisualizer, type LiveVisualizerOptions, type OutputFormat, type ParallelDetectorOptions, type ParallelNode, type RaceNode, type RenderOptions, type Renderer, type ScopeEndEvent, type ScopeEvent, type ScopeStartEvent, ScopeType, type SequenceNode, type StepNode, type StepSkippedEvent, type StepState, type SwitchTracker, type VisualizerOptions, type WorkflowIR, type WorkflowNode, type WorkflowVisualizer, asciiRenderer, createEventCollector, createIRBuilder, createLiveVisualizer, createParallelDetector, createVisualizer, defaultColorScheme, detectParallelGroups, hasChildren, isDecisionNode, isParallelNode, isRaceNode, isSequenceNode, isStepNode, mermaidRenderer, trackDecision, trackIf, trackSwitch, visualizeEvents };