Repository navigation
Expand file tree
/
Copy pathswarm.ts
More file actions
264 lines (214 loc) Β· 9.59 KB
/
Copy pathswarm.ts
File metadata and controls
264 lines (214 loc) Β· 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* Swarm Command Module
*
* Manages swarm initialization, monitoring, and coordination
*/
import { Command } from 'commander';
import chalk from 'chalk';
import ora from 'ora';
import { SwarmManager } from '../core/swarm-manager.js';
import { Logger } from '../utils/logger.js';
export class SwarmCommand extends Command {
private logger: Logger;
constructor() {
super('swarm');
this.logger = new Logger('SwarmCommand');
this
.description('Manage AI agent swarms')
.addCommand(this.createInitCommand())
.addCommand(this.createStatusCommand())
.addCommand(this.createMonitorCommand())
.addCommand(this.createScaleCommand())
.addCommand(this.createDestroyCommand());
}
private createInitCommand(): Command {
const init = new Command('init');
init
.description('Initialize a new swarm')
.option('-t, --topology <type>', 'Swarm topology (hierarchical|mesh|ring|star)', 'hierarchical')
.option('-a, --agents <number>', 'Number of agents to spawn', '8')
.option('-n, --name <name>', 'Swarm name')
.option('-q, --queen <type>', 'Queen type for hierarchical topology', 'strategic')
.option('--consensus <algorithm>', 'Consensus algorithm', 'majority')
.action(async (options) => {
const spinner = ora('Initializing swarm...').start();
try {
const swarmManager = new SwarmManager();
const swarmConfig = {
topology: options.topology,
maxAgents: parseInt(options.agents),
name: options.name || `swarm-${Date.now()}`,
queenType: options.queen,
consensus: options.consensus
};
const swarm = await swarmManager.initializeSwarm(swarmConfig);
spinner.succeed(chalk.green(`Swarm initialized successfully!`));
console.log('\n' + chalk.cyan('π Swarm Details:'));
console.log(chalk.white(` ID: ${swarm.id}`));
console.log(chalk.white(` Name: ${swarm.name}`));
console.log(chalk.white(` Topology: ${swarm.topology}`));
console.log(chalk.white(` Max Agents: ${swarm.maxAgents}`));
if (swarm.topology === 'hierarchical') {
console.log(chalk.white(` Queen Type: ${swarm.queenType}`));
}
console.log('\n' + chalk.yellow('Next steps:'));
console.log(chalk.gray(' 1. Spawn agents: gemini-flow agent spawn --swarm ' + swarm.id));
console.log(chalk.gray(' 2. Orchestrate tasks: gemini-flow task orchestrate "your task"'));
console.log(chalk.gray(' 3. Monitor swarm: gemini-flow swarm monitor --id ' + swarm.id));
} catch (error: any) {
spinner.fail(chalk.red('Failed to initialize swarm'));
this.logger.error('Swarm initialization failed', error);
console.error(chalk.red(error.message));
process.exit(1);
}
});
return init;
}
private createStatusCommand(): Command {
const status = new Command('status');
status
.description('Get swarm status')
.option('-i, --id <swarmId>', 'Swarm ID')
.option('-d, --detailed', 'Show detailed information')
.action(async (options) => {
try {
const swarmManager = new SwarmManager();
const status = await swarmManager.getSwarmStatus(options.id);
if (!status) {
console.log(chalk.yellow('No active swarms found'));
return;
}
console.log('\n' + chalk.cyan('π Swarm Status:'));
console.log(chalk.white(` ID: ${status.id}`));
console.log(chalk.white(` Status: ${this.getStatusColor(status.status)}`));
console.log(chalk.white(` Topology: ${status.topology}`));
console.log(chalk.white(` Active Agents: ${status.activeAgents}/${status.maxAgents}`));
console.log(chalk.white(` Tasks: ${status.completedTasks}/${status.totalTasks} completed`));
if (options.detailed && status.agents) {
console.log('\n' + chalk.cyan('π₯ Agent Details:'));
status.agents.forEach((agent: any) => {
console.log(chalk.white(` ${agent.name} (${agent.type}): ${this.getStatusColor(agent.status)}`));
});
}
} catch (error: any) {
this.logger.error('Failed to get swarm status', error);
console.error(chalk.red(error.message));
process.exit(1);
}
});
return status;
}
private createMonitorCommand(): Command {
const monitor = new Command('monitor');
monitor
.description('Monitor swarm activity in real-time')
.option('-i, --id <swarmId>', 'Swarm ID')
.option('-d, --duration <seconds>', 'Monitoring duration', '60')
.option('-r, --refresh <seconds>', 'Refresh interval', '1')
.action(async (options) => {
console.log(chalk.cyan('π Starting swarm monitoring...'));
console.log(chalk.gray('Press Ctrl+C to stop\n'));
try {
const swarmManager = new SwarmManager();
const duration = parseInt(options.duration) * 1000;
const interval = parseInt(options.refresh) * 1000;
await swarmManager.monitorSwarm(options.id, {
duration,
interval,
onUpdate: (metrics: any) => {
// Clear console and show updated metrics
console.clear();
this.displayMetrics(metrics);
}
});
} catch (error: any) {
this.logger.error('Monitoring failed', error);
console.error(chalk.red(error.message));
process.exit(1);
}
});
return monitor;
}
private createScaleCommand(): Command {
const scale = new Command('scale');
scale
.description('Scale swarm agents up or down')
.option('-i, --id <swarmId>', 'Swarm ID')
.option('-c, --count <number>', 'Target agent count')
.option('--type <agentType>', 'Specific agent type to scale')
.action(async (options) => {
const spinner = ora('Scaling swarm...').start();
try {
const swarmManager = new SwarmManager();
const targetCount = parseInt(options.count);
const result = await swarmManager.scaleSwarm(options.id, targetCount, options.type);
spinner.succeed(chalk.green('Swarm scaled successfully!'));
console.log('\n' + chalk.cyan('π Scaling Results:'));
console.log(chalk.white(` Previous Count: ${result.previousCount}`));
console.log(chalk.white(` Current Count: ${result.currentCount}`));
console.log(chalk.white(` Agents Added: ${result.added}`));
console.log(chalk.white(` Agents Removed: ${result.removed}`));
} catch (error: any) {
spinner.fail(chalk.red('Failed to scale swarm'));
this.logger.error('Scaling failed', error);
console.error(chalk.red(error.message));
process.exit(1);
}
});
return scale;
}
private createDestroyCommand(): Command {
const destroy = new Command('destroy');
destroy
.description('Destroy a swarm and cleanup resources')
.option('-i, --id <swarmId>', 'Swarm ID')
.option('-f, --force', 'Force destroy without confirmation')
.action(async (options) => {
if (!options.force) {
console.log(chalk.yellow('β οΈ This will destroy the swarm and all associated resources.'));
console.log(chalk.yellow(' Use --force to skip this confirmation.'));
return;
}
const spinner = ora('Destroying swarm...').start();
try {
const swarmManager = new SwarmManager();
await swarmManager.destroySwarm(options.id);
spinner.succeed(chalk.green('Swarm destroyed successfully!'));
} catch (error: any) {
spinner.fail(chalk.red('Failed to destroy swarm'));
this.logger.error('Destroy failed', error);
console.error(chalk.red(error.message));
process.exit(1);
}
});
return destroy;
}
private getStatusColor(status: string): string {
const colors: Record<string, (text: string) => string> = {
active: chalk.green,
idle: chalk.yellow,
busy: chalk.blue,
error: chalk.red,
initializing: chalk.cyan
};
return (colors[status] || chalk.white)(status);
}
private displayMetrics(metrics: any) {
console.log(chalk.cyan('π Swarm Metrics'));
console.log(chalk.gray('β'.repeat(50)));
console.log(chalk.white('Performance:'));
console.log(` Tasks/sec: ${chalk.green(metrics.tasksPerSecond.toFixed(2))}`);
console.log(` Avg Response: ${chalk.yellow(metrics.avgResponseTime.toFixed(2))}ms`);
console.log(` Success Rate: ${chalk.green(metrics.successRate.toFixed(1))}%`);
console.log(chalk.white('\nResource Usage:'));
console.log(` Active Agents: ${chalk.blue(metrics.activeAgents)}`);
console.log(` Memory: ${chalk.yellow(metrics.memoryUsage.toFixed(1))}MB`);
console.log(` Queue Size: ${chalk.cyan(metrics.queueSize)}`);
console.log(chalk.white('\nAgent Activity:'));
metrics.agentActivity.forEach((agent: any) => {
const status = agent.busy ? chalk.green('β') : chalk.gray('β');
console.log(` ${status} ${agent.name}: ${agent.currentTask || 'idle'}`);
});
console.log(chalk.gray('\nPress Ctrl+C to stop monitoring'));
}
}