claude-flow
Version:
Ruflo - Enterprise AI agent orchestration for Claude Code. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration
74 lines • 2.21 kB
JavaScript
/**
* Spawn Agent Command - Application Layer (CQRS)
*
* Command for spawning a new agent in the swarm.
*
* @module v3/swarm/application/commands
*/
import { Agent } from '../../domain/entities/agent.js';
/**
* Spawn Agent Command Handler
*/
export class SpawnAgentCommandHandler {
repository;
constructor(repository) {
this.repository = repository;
}
async execute(input) {
// Check if agent with same name exists
const existing = await this.repository.findByName(input.name);
if (existing) {
throw new Error(`Agent with name '${input.name}' already exists`);
}
// Create agent
const agent = Agent.create({
name: input.name,
role: input.role,
domain: input.domain,
capabilities: input.capabilities,
parentId: input.parentId,
metadata: input.metadata,
maxConcurrentTasks: input.maxConcurrentTasks,
});
// Auto-start if requested
let startedAutomatically = false;
if (input.autoStart) {
agent.start();
startedAutomatically = true;
}
await this.repository.save(agent);
return {
success: true,
agentId: agent.id,
agent,
startedAutomatically,
};
}
}
/**
* Terminate Agent Command Handler
*/
export class TerminateAgentCommandHandler {
repository;
constructor(repository) {
this.repository = repository;
}
async execute(input) {
const agent = await this.repository.findById(input.agentId);
if (!agent) {
throw new Error(`Agent '${input.agentId}' not found`);
}
const currentTasks = agent.currentTaskCount;
if (currentTasks > 0 && !input.force) {
throw new Error(`Agent has ${currentTasks} active tasks. Use force=true to terminate anyway.`);
}
agent.terminate();
await this.repository.save(agent);
return {
success: true,
agentId: input.agentId,
tasksReassigned: currentTasks,
};
}
}
//# sourceMappingURL=spawn-agent.command.js.map