/** * zCode Swarm Orchestrator * Main entry point — coordinates all swarm features */ const SwarmUtils = require('./swarm-utils.cjs'); const AgentSpawner = require('./agent-spawner.cjs'); const HierarchicalCoordinator = require('./coordinator/hierarchical.cjs'); const MeshCoordinator = require('./coordinator/mesh.cjs'); const GossipCoordinator = require('./coordinator/gossip.cjs'); const ConsensusCoordinator = require('./coordinator/consensus.cjs'); const FederatedMemory = require('./memory/federated.cjs'); const PerformanceMetricsCollector = require('../lib/performance-metrics.cjs'); const RealTimeDashboard = require('./dashboard/index.cjs'); const NeuralNetworkIntegration = require('./neural-network.cjs'); const AgentMarketplace = require('./marketplace.cjs'); class SwarmOrchestrator { constructor(configPath = '.zcode/config/coordinator.yaml') { this.swarm = new SwarmUtils(configPath); this.spawner = new AgentSpawner(this.swarm); this.memory = new FederatedMemory(this.swarm); this.currentCoordinator = null; this.metricsCollector = null; this.dashboard = null; this.neuralNetwork = null; this.marketplace = null; this.monitoringInterval = null; } async initialize(config = null) { const swarmConfig = config || this.swarm.config; console.log('šŸš€ zCode Swarm Orchestrator initializing...'); console.log(` Mode: ${swarmConfig.coordination?.mode || 'hierarchical'}`); // Coordinators this.initCoordinator(swarmConfig.coordination?.mode || 'hierarchical'); // Agents const agentTypes = swarmConfig.agents?.enabled || []; this.spawner.initializeSwarm(agentTypes); // Neural network this.neuralNetwork = new NeuralNetworkIntegration(this.swarm); this.neuralNetwork.initialize(); // Metrics this.metricsCollector = new PerformanceMetricsCollector(this.swarm); if (swarmConfig.performance?.metrics?.enabled) { this.metricsCollector.startCollection(); } // Dashboard this.dashboard = new RealTimeDashboard(this.swarm); if (swarmConfig.dashboard?.enabled) { this.dashboard.start(); } // Marketplace this.marketplace = new AgentMarketplace(); this.marketplace.swarm = this.swarm; this.marketplace.initialize(); // Monitoring this.monitoringInterval = this.swarm.monitorSwarm(); console.log('āœ… Swarm initialized successfully\n'); } initCoordinator(mode) { const modes = { hierarchical: HierarchicalCoordinator, mesh: MeshCoordinator, gossip: GossipCoordinator, consensus: ConsensusCoordinator }; const Coordinator = modes[mode] || HierarchicalCoordinator; this.currentCoordinator = new Coordinator(this.swarm); this.currentCoordinator.initialize([]); } async coordinate(task) { console.log(`\nšŸŽÆ Task: ${typeof task === 'string' ? task : task.type}`); // Neural recommendation if (this.neuralNetwork) { const rec = await this.neuralNetwork.recommendAgent(task); console.log(`🧠 Recommended: ${rec.recommendedAgent} (${(rec.confidence * 100).toFixed(0)}%)`); } // Store in memory this.memory.store('coordination', `task:${Date.now()}`, { task, status: 'pending', ts: Date.now() }); // Execute const result = await this.currentCoordinator.coordinate(task); // Learn if (this.neuralNetwork) await this.neuralNetwork.learnFromTask(task, result); return result; } // Marketplace API searchAgents(query, caps) { return this.marketplace.search(query, caps); } installAgent(id) { return this.marketplace.installAgent(id); } listInstalled() { return this.marketplace.listInstalled(); } listAvailable() { return this.marketplace.listAvailable(); } // Status getStatus() { return { coordinator: this.currentCoordinator?.constructor?.name, agents: this.swarm.getPerformanceReport(), memory: this.memory.stats(), neural: this.neuralNetwork?.getModelPerformance(), marketplace: { installed: this.marketplace.listInstalled().length, available: this.marketplace.listAvailable().length } }; } async shutdown() { console.log('\nšŸ›‘ Shutting down swarm...'); if (this.monitoringInterval) clearInterval(this.monitoringInterval); this.dashboard?.stop(); this.metricsCollector?.stopCollection(); await this.currentCoordinator?.stopSync?.(); this.spawner.shutdown(); console.log('āœ… Swarm shutdown complete'); } } module.exports = SwarmOrchestrator;