claude-flow
Version:
Enterprise-grade AI agent orchestration with ruv-swarm integration (Alpha Release)
254 lines (207 loc) โข 7.58 kB
JavaScript
/**
* Simple Swarm Executor - Provides basic swarm functionality without TypeScript dependencies
*/
import { promises as fs } from 'fs';
import path from 'path';
// Simple ID generator to avoid build dependencies
function generateId(prefix = 'id') {
const timestamp = Date.now().toString(36);
const random = Math.random().toString(36).substring(2, 9);
return `${prefix}_${timestamp}_${random}`;
}
// Simple SwarmCoordinator implementation
export class SwarmCoordinator {
constructor(config) {
this.config = config;
this.id = config.name || generateId('swarm');
this.agents = [];
this.tasks = [];
this.status = 'initializing';
this.startTime = Date.now();
}
async initialize() {
console.log(`\n๐ Swarm initialized: ${this.id}`);
console.log(`๐ Description: ${this.config.description}`);
console.log(`๐ฏ Strategy: ${this.config.strategy}`);
console.log(`๐๏ธ Mode: ${this.config.mode}`);
console.log(`๐ค Max Agents: ${this.config.maxAgents}`);
this.status = 'active';
// Create swarm directory
const swarmDir = `./swarm-runs/${this.id}`;
await fs.mkdir(swarmDir, { recursive: true });
// Save configuration
await fs.writeFile(path.join(swarmDir, 'config.json'), JSON.stringify(this.config, null, 2));
return this;
}
async addAgent(type, name) {
const agent = {
id: generateId('agent'),
type,
name: name || `${type}-${this.agents.length + 1}`,
status: 'active',
tasks: [],
};
this.agents.push(agent);
console.log(` ๐ค Agent spawned: ${agent.name} (${agent.type})`);
return agent;
}
async executeTask(task) {
const taskObj = {
id: generateId('task'),
description: task,
status: 'in_progress',
startTime: Date.now(),
};
this.tasks.push(taskObj);
console.log(`\n๐ Executing task: ${task}`);
// Simulate task execution with progress
console.log(` โณ Processing...`);
// Simulate different types of tasks
if (task.toLowerCase().includes('api')) {
await this.createAPIProject();
} else if (task.toLowerCase().includes('test')) {
await this.runTests();
} else {
await this.genericTaskExecution(task);
}
taskObj.status = 'completed';
taskObj.endTime = Date.now();
console.log(` โ
Task completed in ${(taskObj.endTime - taskObj.startTime) / 1000}s`);
return taskObj;
}
async createAPIProject() {
console.log(` ๐๏ธ Creating API project structure...`);
const projectDir = './api-project';
await fs.mkdir(projectDir, { recursive: true });
// Create basic Express server
const serverCode = `const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.get('/health', (req, res) => {
res.json({ status: 'healthy', swarm: '${this.id}' });
});
app.get('/api/items', (req, res) => {
res.json({ items: [], count: 0 });
});
app.listen(port, () => {
console.log(\`API server running on port \${port}\`);
});
module.exports = app;
`;
await fs.writeFile(path.join(projectDir, 'server.js'), serverCode);
// Create package.json
const packageJson = {
name: 'api-project',
version: '1.0.0',
description: 'API created by Claude Flow Swarm',
main: 'server.js',
scripts: {
start: 'node server.js',
dev: 'nodemon server.js',
},
dependencies: {
express: '^4.18.2',
},
devDependencies: {
nodemon: '^3.0.1',
},
};
await fs.writeFile(path.join(projectDir, 'package.json'), JSON.stringify(packageJson, null, 2));
console.log(` โ
Created API project in ${projectDir}`);
}
async runTests() {
console.log(` ๐งช Running tests...`);
console.log(` โ
All tests passed (0 tests)`);
}
async genericTaskExecution(task) {
console.log(` ๐ Executing: ${task}`);
// Simulate work being done
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(` โ
Generic task completed`);
}
async getStatus() {
return {
id: this.id,
status: this.status,
agents: this.agents.length,
tasks: {
total: this.tasks.length,
completed: this.tasks.filter((t) => t.status === 'completed').length,
in_progress: this.tasks.filter((t) => t.status === 'in_progress').length,
},
runtime: Math.floor((Date.now() - this.startTime) / 1000),
};
}
async complete() {
this.status = 'completed';
const summary = await this.getStatus();
console.log(`\nโ
Swarm completed successfully!`);
console.log(`๐ Summary:`);
console.log(` โข Swarm ID: ${summary.id}`);
console.log(` โข Total Agents: ${summary.agents}`);
console.log(` โข Tasks Completed: ${summary.tasks.completed}`);
console.log(` โข Runtime: ${summary.runtime}s`);
// Save summary
const swarmDir = `./swarm-runs/${this.id}`;
await fs.writeFile(path.join(swarmDir, 'summary.json'), JSON.stringify(summary, null, 2));
return summary;
}
}
// Main execution function
export async function executeSwarm(objective, flags = {}) {
try {
// Parse configuration from flags
const config = {
name: generateId('swarm'),
description: objective,
mode: flags.mode || 'centralized',
strategy: flags.strategy || 'auto',
maxAgents: parseInt(flags['max-agents']) || 5,
maxTasks: parseInt(flags['max-tasks']) || 100,
timeout: (parseInt(flags.timeout) || 60) * 60 * 1000,
taskTimeoutMinutes: parseInt(flags['task-timeout-minutes']) || 59,
qualityThreshold: parseFloat(flags['quality-threshold']) || 0.8,
reviewRequired: flags.review || false,
testingRequired: flags.testing || false,
monitoring: {
enabled: flags.monitor || false,
},
memory: {
namespace: flags['memory-namespace'] || 'swarm',
persistent: flags.persistence !== false,
},
security: {
encryptionEnabled: flags.encryption || false,
},
};
// Initialize swarm coordinator
const coordinator = new SwarmCoordinator(config);
await coordinator.initialize();
// Spawn agents based on strategy
if (config.strategy === 'development' || config.strategy === 'auto') {
await coordinator.addAgent('architect', 'System Architect');
await coordinator.addAgent('coder', 'Backend Developer');
await coordinator.addAgent('coder', 'Frontend Developer');
await coordinator.addAgent('tester', 'QA Engineer');
await coordinator.addAgent('reviewer', 'Code Reviewer');
} else if (config.strategy === 'research') {
await coordinator.addAgent('researcher', 'Lead Researcher');
await coordinator.addAgent('analyst', 'Data Analyst');
await coordinator.addAgent('researcher', 'Research Assistant');
} else if (config.strategy === 'testing') {
await coordinator.addAgent('tester', 'Test Lead');
await coordinator.addAgent('tester', 'Integration Tester');
await coordinator.addAgent('tester', 'Performance Tester');
}
// Execute the main objective
await coordinator.executeTask(objective);
// Complete and return summary
const summary = await coordinator.complete();
return { success: true, summary };
} catch (error) {
console.error(`โ Swarm execution failed: ${error.message}`);
return { success: false, error: error.message };
}
}
// Exports are already declared inline above