claude-flow-tbowman01
Version:
Enterprise-grade AI agent orchestration with ruv-swarm integration (Alpha Release)
356 lines โข 17.2 kB
JavaScript
/**
* Comprehensive Agent management commands - Simplified version
*/
import { AgentManager } from '../../agents/agent-manager.js';
import { AgentRegistry } from '../../agents/agent-registry.js';
import { DistributedMemorySystem } from '../../memory/distributed-memory.js';
import { EventBus } from '../../core/event-bus.js';
import { Logger } from '../../core/logger.js';
// Global instances
let agentManager = null;
let agentRegistry = null;
// Initialize agent management system
async function initializeAgentSystem() {
if (agentManager && agentRegistry) {
return { manager: agentManager, registry: agentRegistry };
}
try {
const logger = new Logger({
level: 'info',
format: 'text',
destination: 'console',
});
const eventBus = EventBus.getInstance();
const memorySystem = new DistributedMemorySystem({
namespace: 'agents',
distributed: false,
consistency: 'eventual',
replicationFactor: 1,
syncInterval: 60000,
maxMemorySize: 100,
compressionEnabled: false,
encryptionEnabled: false,
backupEnabled: true,
persistenceEnabled: true,
shardingEnabled: false,
cacheSize: 50,
cacheTtl: 300000,
}, logger, eventBus);
await memorySystem.initialize();
agentRegistry = new AgentRegistry(memorySystem, 'agents');
await agentRegistry.initialize();
agentManager = new AgentManager({
maxAgents: 50,
defaultTimeout: 60000,
heartbeatInterval: 15000,
healthCheckInterval: 30000,
autoRestart: true,
resourceLimits: {
memory: 1024 * 1024 * 1024, // 1GB
cpu: 2.0,
disk: 2 * 1024 * 1024 * 1024, // 2GB
},
}, logger, eventBus, memorySystem);
await agentManager.initialize();
return { manager: agentManager, registry: agentRegistry };
}
catch (error) {
throw new Error(`Failed to initialize agent system: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Agent Management Commands
export const agentCommands = {
async spawn(args, options = {}) {
try {
const { manager } = await initializeAgentSystem();
const templateName = args[0] || 'researcher';
const name = options.name || `${templateName}-${Date.now().toString(36)}`;
console.log(`๐ Creating agent with template: ${templateName}`);
const agentId = await manager.createAgent(templateName, {
name,
config: {
autonomyLevel: options.autonomy || 0.7,
maxConcurrentTasks: options.maxTasks || 5,
timeoutThreshold: options.timeout || 300000,
},
});
if (options.start !== false) {
console.log('โก Starting agent...');
await manager.startAgent(agentId);
}
console.log('โ
Agent created successfully!');
console.log(` ID: ${agentId}`);
console.log(` Name: ${name}`);
console.log(` Template: ${templateName}`);
}
catch (error) {
console.error('โ Error creating agent:', error instanceof Error ? error.message : String(error));
}
},
async list(args, options = {}) {
try {
const { manager } = await initializeAgentSystem();
let agents = manager.getAllAgents();
// Apply filters
if (options.type) {
agents = agents.filter((agent) => agent.type === options.type);
}
if (options.status) {
agents = agents.filter((agent) => agent.status === options.status);
}
if (options.unhealthy) {
agents = agents.filter((agent) => agent.health < 0.7);
}
if (agents.length === 0) {
console.log('๐ No agents found matching the criteria');
return;
}
console.log(`๐ค Agent List (${agents.length} agents)`);
console.log('='.repeat(60));
for (const agent of agents) {
const healthEmoji = agent.health > 0.8 ? '๐ข' : agent.health > 0.5 ? '๐ก' : '๐ด';
const statusEmoji = agent.status === 'idle'
? 'โญ'
: agent.status === 'busy'
? '๐ต'
: agent.status === 'error'
? '๐ด'
: 'โช';
console.log(`${healthEmoji} ${agent.name}`);
console.log(` ID: ${agent.id.id.slice(-8)}`);
console.log(` Type: ${agent.type}`);
console.log(` Status: ${statusEmoji} ${agent.status.toUpperCase()}`);
console.log(` Health: ${Math.round(agent.health * 100)}%`);
console.log(` Workload: ${agent.workload} tasks`);
if (options.detailed) {
console.log(` Tasks Completed: ${agent.metrics.tasksCompleted}`);
console.log(` Success Rate: ${Math.round(agent.metrics.successRate * 100)}%`);
console.log(` Memory: ${Math.round(agent.metrics.memoryUsage / 1024 / 1024)}MB`);
}
console.log('');
}
// System stats
const stats = manager.getSystemStats();
console.log('๐ System Overview:');
console.log(` Total: ${stats.totalAgents} | Active: ${stats.activeAgents} | Healthy: ${stats.healthyAgents}`);
console.log(` Average Health: ${Math.round(stats.averageHealth * 100)}%`);
}
catch (error) {
console.error('โ Error listing agents:', error instanceof Error ? error.message : String(error));
}
},
async info(args, options = {}) {
try {
const { manager } = await initializeAgentSystem();
const agentId = args[0];
if (!agentId) {
console.error('โ Agent ID is required');
console.log('Usage: agent info <agent-id>');
return;
}
const agent = manager.getAgent(agentId);
if (!agent) {
console.error(`โ Agent '${agentId}' not found`);
// Suggest similar agents
const allAgents = manager.getAllAgents();
const similar = allAgents.filter((a) => a.id.id.includes(agentId) || a.name.toLowerCase().includes(agentId.toLowerCase()));
if (similar.length > 0) {
console.log('\\nDid you mean one of these agents?');
similar.forEach((a) => console.log(` ${a.id.id} - ${a.name}`));
}
return;
}
console.log(`๐ค Agent Information: ${agent.name}`);
console.log('='.repeat(50));
// Basic info
console.log(`ID: ${agent.id.id}`);
console.log(`Name: ${agent.name}`);
console.log(`Type: ${agent.type}`);
console.log(`Status: ${agent.status.toUpperCase()}`);
console.log(`Health: ${Math.round(agent.health * 100)}%`);
console.log(`Workload: ${agent.workload} active tasks`);
// Configuration
console.log('\\nโ๏ธ Configuration:');
console.log(` Autonomy Level: ${agent.config.autonomyLevel}`);
console.log(` Max Concurrent Tasks: ${agent.config.maxConcurrentTasks}`);
console.log(` Timeout Threshold: ${agent.config.timeoutThreshold}ms`);
console.log(` Runtime: ${agent.environment.runtime}`);
// Metrics
console.log('\\n๐ Performance Metrics:');
console.log(` Tasks Completed: ${agent.metrics.tasksCompleted}`);
console.log(` Tasks Failed: ${agent.metrics.tasksFailed}`);
console.log(` Success Rate: ${Math.round(agent.metrics.successRate * 100)}%`);
console.log(` Average Execution Time: ${agent.metrics.averageExecutionTime}ms`);
console.log(` Memory Usage: ${Math.round(agent.metrics.memoryUsage / 1024 / 1024)}MB`);
console.log(` CPU Usage: ${Math.round(agent.metrics.cpuUsage * 100)}%`);
// Health details
const health = manager.getAgentHealth(agentId);
if (health) {
console.log('\\n๐ฅ Health Details:');
console.log(` Responsiveness: ${Math.round(health.components.responsiveness * 100)}%`);
console.log(` Performance: ${Math.round(health.components.performance * 100)}%`);
console.log(` Reliability: ${Math.round(health.components.reliability * 100)}%`);
console.log(` Resource Usage: ${Math.round(health.components.resourceUsage * 100)}%`);
if (health.issues.length > 0) {
console.log('\\nโ ๏ธ Active Issues:');
health.issues.forEach((issue, index) => {
console.log(` ${index + 1}. [${issue.severity.toUpperCase()}] ${issue.message}`);
});
}
}
// Capabilities
console.log('\\n๐ ๏ธ Capabilities:');
console.log(` Code Generation: ${agent.capabilities.codeGeneration ? 'โ
' : 'โ'}`);
console.log(` Code Review: ${agent.capabilities.codeReview ? 'โ
' : 'โ'}`);
console.log(` Testing: ${agent.capabilities.testing ? 'โ
' : 'โ'}`);
console.log(` Research: ${agent.capabilities.research ? 'โ
' : 'โ'}`);
console.log(` Web Search: ${agent.capabilities.webSearch ? 'โ
' : 'โ'}`);
if (agent.capabilities.languages.length > 0) {
console.log(` Languages: ${agent.capabilities.languages.join(', ')}`);
}
if (agent.capabilities.frameworks.length > 0) {
console.log(` Frameworks: ${agent.capabilities.frameworks.join(', ')}`);
}
}
catch (error) {
console.error('โ Error getting agent info:', error instanceof Error ? error.message : String(error));
}
},
async terminate(args, options = {}) {
try {
const { manager } = await initializeAgentSystem();
const agentId = args[0];
if (!agentId) {
console.error('โ Agent ID is required');
console.log('Usage: agent terminate <agent-id>');
return;
}
const agent = manager.getAgent(agentId);
if (!agent) {
console.error(`โ Agent '${agentId}' not found`);
return;
}
console.log(`๐ Terminating agent: ${agent.name} (${agentId})`);
const reason = options.reason || 'user_request';
// Graceful or force termination
if (options.force) {
console.log('โก Force terminating agent...');
}
else {
console.log('๐ Gracefully shutting down agent...');
}
await manager.stopAgent(agentId, reason);
if (options.cleanup) {
console.log('๐งน Cleaning up agent data...');
await manager.removeAgent(agentId);
}
console.log('โ
Agent terminated successfully');
// Show final stats
if (agent.metrics) {
console.log('\\n๐ Final Statistics:');
console.log(` Tasks Completed: ${agent.metrics.tasksCompleted}`);
console.log(` Success Rate: ${Math.round(agent.metrics.successRate * 100)}%`);
console.log(` Total Uptime: ${Math.round(agent.metrics.totalUptime / 1000)}s`);
}
}
catch (error) {
console.error('โ Error terminating agent:', error instanceof Error ? error.message : String(error));
}
},
async start(args, options = {}) {
try {
const { manager } = await initializeAgentSystem();
const agentId = args[0];
if (!agentId) {
console.error('โ Agent ID is required');
console.log('Usage: agent start <agent-id>');
return;
}
console.log(`๐ Starting agent ${agentId}...`);
await manager.startAgent(agentId);
console.log('โ
Agent started successfully');
}
catch (error) {
console.error('โ Error starting agent:', error instanceof Error ? error.message : String(error));
}
},
async restart(args, options = {}) {
try {
const { manager } = await initializeAgentSystem();
const agentId = args[0];
if (!agentId) {
console.error('โ Agent ID is required');
console.log('Usage: agent restart <agent-id>');
return;
}
console.log(`๐ Restarting agent ${agentId}...`);
const reason = options.reason || 'manual_restart';
await manager.restartAgent(agentId, reason);
console.log('โ
Agent restarted successfully');
}
catch (error) {
console.error('โ Error restarting agent:', error instanceof Error ? error.message : String(error));
}
},
async health(args, options = {}) {
try {
const { manager } = await initializeAgentSystem();
const agents = manager.getAllAgents();
const stats = manager.getSystemStats();
const threshold = options.threshold || 0.7;
console.log('๐ฅ Agent Health Dashboard');
console.log('='.repeat(50));
console.log(`Time: ${new Date().toLocaleString()}`);
console.log(`Total Agents: ${stats.totalAgents} | Active: ${stats.activeAgents} | Healthy: ${stats.healthyAgents}`);
console.log(`Average Health: ${Math.round(stats.averageHealth * 100)}%`);
const unhealthyAgents = agents.filter((a) => a.health < threshold);
if (unhealthyAgents.length > 0) {
console.log(`\\nโ ๏ธ ${unhealthyAgents.length} agents below health threshold (${Math.round(threshold * 100)}%):`);
unhealthyAgents.forEach((agent) => {
const healthPercent = Math.round(agent.health * 100);
console.log(` ๐ด ${agent.name}: ${healthPercent}% (${agent.status})`);
});
}
else {
console.log('\\nโ
All agents are healthy!');
}
// Resource utilization
console.log('\\n๐ป Resource Utilization:');
console.log(` CPU: ${Math.round(stats.resourceUtilization.cpu * 100)}%`);
console.log(` Memory: ${Math.round(stats.resourceUtilization.memory / 1024 / 1024)}MB`);
console.log(` Disk: ${Math.round(stats.resourceUtilization.disk / 1024 / 1024)}MB`);
}
catch (error) {
console.error('โ Error getting health status:', error instanceof Error ? error.message : String(error));
}
},
async help() {
console.log('๐ค Agent Management Commands:');
console.log('');
console.log('Available commands:');
console.log(' spawn <template> - Create and start new agents');
console.log(' list - Display all agents with status');
console.log(' info <agent-id> - Get detailed agent information');
console.log(' terminate <agent-id> - Safely terminate agents');
console.log(' start <agent-id> - Start a created agent');
console.log(' restart <agent-id> - Restart an agent');
console.log(' health - Monitor agent health');
console.log('');
console.log('Common options:');
console.log(' --type <type> - Filter by agent type');
console.log(' --status <status> - Filter by agent status');
console.log(' --detailed - Show detailed information');
console.log(' --force - Force operation');
console.log(' --cleanup - Clean up data after operation');
console.log('');
console.log('Examples:');
console.log(' agent spawn researcher --name "research-bot"');
console.log(' agent list --type researcher --detailed');
console.log(' agent info agent-123');
console.log(' agent terminate agent-123 --cleanup');
console.log(' agent health --threshold 0.8');
},
};
// Export individual command functions for use in CLI
export const { spawn: spawnAgent, list: listAgents, info: agentInfo, terminate: terminateAgent, start: startAgent, restart: restartAgent, health: agentHealth, } = agentCommands;
//# sourceMappingURL=agent-simple.js.map