|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Example: Full H&&S Protocol Workflow |
| 5 | + * |
| 6 | + * Demonstrates a complete multi-agent workflow: |
| 7 | + * User -> Grok -> Claude -> Repos |
| 8 | + */ |
| 9 | + |
| 10 | +const { HandshakeProtocol } = require('../dist/index'); |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | + |
| 14 | +async function main() { |
| 15 | + console.log('🌊 Wave Toolkit - H&&S Protocol Example\n'); |
| 16 | + |
| 17 | + // Initialize protocol with custom directories |
| 18 | + const protocol = new HandshakeProtocol( |
| 19 | + '.wave-example/handoffs', |
| 20 | + '.wave-example/atom-trail' |
| 21 | + ); |
| 22 | + |
| 23 | + const sessionId = `example-${Date.now()}`; |
| 24 | + console.log(`Session ID: ${sessionId}\n`); |
| 25 | + |
| 26 | + // Step 1: User initiates task with Grok |
| 27 | + console.log('Step 1: User → Grok (PASS)'); |
| 28 | + const step1 = await protocol.createHandoff( |
| 29 | + 'user', |
| 30 | + 'grok', |
| 31 | + 'PASS', |
| 32 | + { |
| 33 | + task: 'Explore architectural patterns for microservices', |
| 34 | + requirements: ['scalability', 'resilience', 'observability'] |
| 35 | + }, |
| 36 | + sessionId |
| 37 | + ); |
| 38 | + console.log(` ✓ Created handoff: ${step1.id}`); |
| 39 | + console.log(` ✓ Context: ${JSON.stringify(step1.context)}\n`); |
| 40 | + |
| 41 | + // Step 2: Grok completes exploration, passes to Claude with high coherence |
| 42 | + console.log('Step 2: Grok → Claude (WAVE with 88% coherence)'); |
| 43 | + const step2 = await protocol.createHandoff( |
| 44 | + 'grok', |
| 45 | + 'claude', |
| 46 | + 'WAVE', |
| 47 | + { |
| 48 | + insights: [ |
| 49 | + 'Event-driven architecture for loose coupling', |
| 50 | + 'API Gateway pattern for unified interface', |
| 51 | + 'Circuit breaker for fault tolerance' |
| 52 | + ], |
| 53 | + recommendedApproach: 'Hybrid synchronous/asynchronous communication' |
| 54 | + }, |
| 55 | + sessionId, |
| 56 | + 88 // High coherence score |
| 57 | + ); |
| 58 | + console.log(` ✓ Created handoff: ${step2.id}`); |
| 59 | + console.log(` ✓ Coherence Score: ${step2.coherenceScore}%`); |
| 60 | + console.log(` ✓ Insights: ${step2.context.insights.length} patterns identified\n`); |
| 61 | + |
| 62 | + // Step 3: Claude implements and commits |
| 63 | + console.log('Step 3: Claude → Repos (PASS)'); |
| 64 | + const step3 = await protocol.createHandoff( |
| 65 | + 'claude', |
| 66 | + 'repos', |
| 67 | + 'PASS', |
| 68 | + { |
| 69 | + status: 'committed', |
| 70 | + files: [ |
| 71 | + 'src/gateway/api-gateway.ts', |
| 72 | + 'src/services/event-bus.ts', |
| 73 | + 'src/middleware/circuit-breaker.ts', |
| 74 | + 'tests/integration.test.ts' |
| 75 | + ], |
| 76 | + branch: 'feature/microservices-architecture' |
| 77 | + }, |
| 78 | + sessionId |
| 79 | + ); |
| 80 | + console.log(` ✓ Created handoff: ${step3.id}`); |
| 81 | + console.log(` ✓ Files committed: ${step3.context.files.length}`); |
| 82 | + console.log(` ✓ Branch: ${step3.context.branch}\n`); |
| 83 | + |
| 84 | + // Display the complete chain |
| 85 | + console.log('📊 Complete Handoff Chain:'); |
| 86 | + console.log('─'.repeat(60)); |
| 87 | + const chain = await protocol.getHandoffChain(sessionId); |
| 88 | + chain.forEach((marker, index) => { |
| 89 | + console.log(`${index + 1}. [${new Date(marker.timestamp).toLocaleTimeString()}]`); |
| 90 | + console.log(` ${marker.fromAgent} --${marker.state}${marker.coherenceScore ? `(${marker.coherenceScore}%)` : ''}-> ${marker.toAgent}`); |
| 91 | + }); |
| 92 | + console.log('─'.repeat(60) + '\n'); |
| 93 | + |
| 94 | + // Generate Mermaid visualization |
| 95 | + console.log('📈 Workflow Visualization (Mermaid):'); |
| 96 | + console.log('─'.repeat(60)); |
| 97 | + const diagram = await protocol.visualizeWorkflow(sessionId); |
| 98 | + console.log(diagram); |
| 99 | + console.log('─'.repeat(60) + '\n'); |
| 100 | + |
| 101 | + // Validate all handoffs |
| 102 | + console.log('✅ Validation Results:'); |
| 103 | + for (let i = 0; i < chain.length; i++) { |
| 104 | + const result = await protocol.validateHandoff(chain[i]); |
| 105 | + console.log(` Step ${i + 1}: ${result.valid ? '✓ Valid' : '✗ Invalid'}`); |
| 106 | + if (result.warnings && result.warnings.length > 0) { |
| 107 | + result.warnings.forEach(warning => { |
| 108 | + console.log(` ⚠️ ${warning}`); |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + console.log(''); |
| 113 | + |
| 114 | + // Show ATOM trail |
| 115 | + console.log('📜 ATOM Trail Entries:'); |
| 116 | + console.log('─'.repeat(60)); |
| 117 | + const atomFile = path.join('.wave-example/atom-trail', `${sessionId}.atom.jsonl`); |
| 118 | + if (fs.existsSync(atomFile)) { |
| 119 | + const content = fs.readFileSync(atomFile, 'utf-8'); |
| 120 | + const entries = content.trim().split('\n').map(line => JSON.parse(line)); |
| 121 | + entries.forEach((entry, index) => { |
| 122 | + console.log(`${index + 1}. ${entry.actor} → ${entry.decision}`); |
| 123 | + console.log(` ${entry.rationale}`); |
| 124 | + }); |
| 125 | + } |
| 126 | + console.log('─'.repeat(60) + '\n'); |
| 127 | + |
| 128 | + console.log('✨ Example complete!'); |
| 129 | + console.log(`\n💾 Data stored in: .wave-example/`); |
| 130 | + console.log(` Handoffs: .wave-example/handoffs/${sessionId}.jsonl`); |
| 131 | + console.log(` ATOM Trail: .wave-example/atom-trail/${sessionId}.atom.jsonl`); |
| 132 | +} |
| 133 | + |
| 134 | +// Run the example |
| 135 | +main().catch(error => { |
| 136 | + console.error('Error:', error); |
| 137 | + process.exit(1); |
| 138 | +}); |
0 commit comments