/** * Swarm Utils - Core Utilities * Shared utilities for zCode Swarm */ class SwarmUtils { constructor(configPath = '.zcode/config/coordinator.yaml') { this.config = this.loadConfig(configPath); this.performanceReport = {}; this.memory = new Map(); } loadConfig(path) { try { const fs = require('fs'); const yaml = require('js-yaml'); const config = yaml.load(fs.readFileSync(path, 'utf8')); return config; } catch (err) { console.error('❌ Error loading config:', err.message); process.exit(1); } } // Monitor swarm performance monitorSwarm() { return setInterval(() => { this.collectMetrics(); }, this.config.performance?.metrics?.collection_interval || 60000); } collectMetrics() { // Simulate metrics collection this.performanceReport = { timestamp: Date.now(), agents: { total: this.config.agents?.enabled?.length || 0, active: Math.floor(Math.random() * 3) + 1, idle: this.config.agents?.enabled?.length - 2 }, memory: { used: Math.floor(Math.random() * 100) + 'MB', total: '512MB', usage: (Math.random() * 30 + 10).toFixed(1) + '%' }, cpu: { usage: (Math.random() * 40 + 20).toFixed(1) + '%' }, coordination: { mode: this.config.coordination?.mode || 'hierarchical' } }; } getPerformanceReport() { return this.performanceReport; } // Memory management store(namespace, key, value) { if (!this.memory.has(namespace)) { this.memory.set(namespace, new Map()); } this.memory.get(namespace).set(key, { value, timestamp: Date.now() }); } get(namespace, key) { if (this.memory.has(namespace)) { return this.memory.get(namespace).get(key); } return null; } // Log message with type log(type, message, data = null) { const timestamp = new Date().toISOString(); const logEntry = { timestamp, type, message, data }; switch (type) { case 'info': console.log(`ℹ️ [${timestamp}] ${message}`); break; case 'success': console.log(`✅ [${timestamp}] ${message}`); break; case 'warning': console.log(`⚠️ [${timestamp}] ${message}`); break; case 'error': console.log(`❌ [${timestamp}] ${message}`); break; case 'debug': console.log(`🔍 [${timestamp}] ${message}`, data || ''); break; default: console.log(`📝 [${timestamp}] ${message}`, data || ''); } } } module.exports = SwarmUtils;