feat: add zCode Swarm — multi-agent orchestration system

- 6 agent skills: code-review, performance, security, architecture, test, git
- 4 coordinator modes: hierarchical, mesh, gossip, consensus
- Federated memory system (6 namespaces)
- Neural network agent recommendation
- Agent marketplace (plugin discovery/install)
- Real-time dashboard + performance metrics
- CRDT-based sync for decentralized modes
- 22 files, ~1400 lines total

Inspired by ruflo distributed multi-agent patterns.
This commit is contained in:
admin
2026-05-06 07:59:19 +00:00
Unverified
parent 129d4a6def
commit 68cfeb5cba
24 changed files with 1513 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
/**
* 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;