Files
Agentic-Compaction-and-Pipl…/examples/04-state-machine-pipeline.ts
admin 8329636696 Add Delegation System with 3rd Party AI Tool Integration
NEW: Delegation System (v1.2.0)
- Request Classifier for fast request analysis (<50ms)
- Agent Pool Manager with auto-scaling (8 agent types)
- Delegation Engine with 4 strategies (full, parallel, hierarchical, hybrid)
- Progress Streamer for real-time updates
- Context Handoff Protocol for inter-agent communication
- Quality Gate with confidence thresholds and auto-escalation

NEW: 3rd Party Integration Adapters
- OpenClaw adapter with parallel execution support
- Claude Code CLI adapter with tool registration
- Generic adapter for custom integrations
- Standardized IntegrationAdapter interface

Agent Types Added:
- fast-responder (quick answers < 2s)
- explorer (code navigation)
- researcher (deep analysis)
- coder (implementation)
- reviewer (quality checks)
- planner (architecture)
- executor (commands)
- analyzer (debugging)

Tests: All 6 tests passing

This project was 100% autonomously built by Z.AI GLM-5
2026-03-04 10:06:17 +00:00

151 lines
4.3 KiB
TypeScript
Executable File

/**
* 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);