/** * Example: Deterministic State Machine Pipeline * * Demonstrates how to use the deterministic state machine * and parallel execution engine for multi-agent workflows. */ import { DeterministicStateMachine, ParallelExecutionEngine, EventBus } from '../pipeline-system'; async function stateMachineExample() { // Create event bus for coordination const eventBus = new EventBus(); // Subscribe to events eventBus.subscribe('state:*', (event) => { console.log(`[Event] ${event.type}:`, event.data); }); // Define state machine configuration const smConfig = { initialState: 'idle', states: { idle: { onEnter: async (context: any) => { console.log('Entering IDLE state'); context.startTime = Date.now(); }, transitions: [ { target: 'initializing', event: 'start', condition: (ctx: any) => ctx.config !== null } ] }, initializing: { onEnter: async (context: any) => { console.log('Initializing...'); // Simulate async initialization await new Promise(resolve => setTimeout(resolve, 100)); }, onExit: async (context: any) => { console.log('Initialization complete'); context.initialized = true; }, transitions: [ { target: 'processing', event: 'initialized' } ] }, processing: { onEnter: async (context: any) => { console.log('Processing started'); }, transitions: [ { target: 'completed', event: 'done', condition: (ctx: any) => ctx.success }, { target: 'error', event: 'error' } ] }, completed: { onEnter: async (context: any) => { context.endTime = Date.now(); console.log(`Completed in ${context.endTime - context.startTime}ms`); }, final: true }, error: { onEnter: async (context: any) => { console.error('Error state reached'); }, final: true } } }; // Create state machine const stateMachine = new DeterministicStateMachine(smConfig); // Initialize with context const context = { config: { maxRetries: 3 }, success: true }; // Start the state machine console.log('--- Starting State Machine ---'); await stateMachine.start(context); console.log('Current state:', stateMachine.getState()); // Trigger transitions console.log('\n--- Triggering Transitions ---'); // Check if transition is valid if (stateMachine.canTransition('start')) { await stateMachine.transition('start', context); console.log('After start:', stateMachine.getState()); } if (stateMachine.canTransition('initialized')) { await stateMachine.transition('initialized', context); console.log('After initialized:', stateMachine.getState()); } if (stateMachine.canTransition('done')) { await stateMachine.transition('done', context); console.log('After done:', stateMachine.getState()); } // Check if final state console.log('\nIs final state:', stateMachine.isFinalState()); // Now demonstrate parallel execution console.log('\n--- Parallel Execution Engine ---'); const executor = new ParallelExecutionEngine({ maxConcurrency: 4, taskQueue: 'priority', timeout: 30000 }); // Define tasks const tasks = [ { id: 'task-1', type: 'analyze', priority: 1, data: { target: 'module-a' } }, { id: 'task-2', type: 'analyze', priority: 2, data: { target: 'module-b' } }, { id: 'task-3', type: 'analyze', priority: 1, data: { target: 'module-c' } }, { id: 'task-4', type: 'analyze', priority: 3, data: { target: 'module-d' } } ]; // Execute in parallel with custom handler const results = await executor.executeAll(tasks, async (task) => { console.log(`Processing ${task.id}...`); await new Promise(resolve => setTimeout(resolve, 100)); return { taskId: task.id, result: `Analyzed ${task.data.target}` }; }); console.log('\nResults:', results); // Get execution stats const stats = executor.getStats(); console.log('Execution stats:', stats); // Cleanup executor.shutdown(); eventBus.clearAllSubscriptions(); console.log('\n--- Example Complete ---'); } // Run the example stateMachineExample().catch(console.error);