@sparkleideas/swarm 3.0.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION.md +472 -0
- package/README.md +634 -0
- package/__tests__/consensus.test.ts +577 -0
- package/__tests__/coordinator.test.ts +501 -0
- package/__tests__/queen-coordinator.test.ts +1335 -0
- package/__tests__/topology.test.ts +621 -0
- package/package.json +32 -0
- package/src/agent-pool.ts +476 -0
- package/src/application/commands/create-task.command.ts +124 -0
- package/src/application/commands/spawn-agent.command.ts +122 -0
- package/src/application/index.ts +30 -0
- package/src/application/services/swarm-application-service.ts +200 -0
- package/src/attention-coordinator.ts +1000 -0
- package/src/consensus/byzantine.ts +431 -0
- package/src/consensus/gossip.ts +513 -0
- package/src/consensus/index.ts +267 -0
- package/src/consensus/raft.ts +443 -0
- package/src/coordination/agent-registry.ts +544 -0
- package/src/coordination/index.ts +23 -0
- package/src/coordination/swarm-hub.ts +776 -0
- package/src/coordination/task-orchestrator.ts +605 -0
- package/src/domain/entities/agent.d.ts +151 -0
- package/src/domain/entities/agent.d.ts.map +1 -0
- package/src/domain/entities/agent.js +280 -0
- package/src/domain/entities/agent.js.map +1 -0
- package/src/domain/entities/agent.ts +370 -0
- package/src/domain/entities/task.d.ts +133 -0
- package/src/domain/entities/task.d.ts.map +1 -0
- package/src/domain/entities/task.js +261 -0
- package/src/domain/entities/task.js.map +1 -0
- package/src/domain/entities/task.ts +319 -0
- package/src/domain/index.ts +41 -0
- package/src/domain/repositories/agent-repository.interface.d.ts +57 -0
- package/src/domain/repositories/agent-repository.interface.d.ts.map +1 -0
- package/src/domain/repositories/agent-repository.interface.js +9 -0
- package/src/domain/repositories/agent-repository.interface.js.map +1 -0
- package/src/domain/repositories/agent-repository.interface.ts +69 -0
- package/src/domain/repositories/task-repository.interface.d.ts +61 -0
- package/src/domain/repositories/task-repository.interface.d.ts.map +1 -0
- package/src/domain/repositories/task-repository.interface.js +9 -0
- package/src/domain/repositories/task-repository.interface.js.map +1 -0
- package/src/domain/repositories/task-repository.interface.ts +75 -0
- package/src/domain/services/coordination-service.ts +320 -0
- package/src/federation-hub.d.ts +284 -0
- package/src/federation-hub.d.ts.map +1 -0
- package/src/federation-hub.js +692 -0
- package/src/federation-hub.js.map +1 -0
- package/src/federation-hub.ts +979 -0
- package/src/index.ts +348 -0
- package/src/message-bus.ts +607 -0
- package/src/queen-coordinator.ts +2025 -0
- package/src/shared/events.ts +285 -0
- package/src/shared/types.ts +389 -0
- package/src/topology-manager.ts +656 -0
- package/src/types.ts +545 -0
- package/src/unified-coordinator.ts +1844 -0
- package/src/workers/index.ts +65 -0
- package/src/workers/worker-dispatch.d.ts +234 -0
- package/src/workers/worker-dispatch.d.ts.map +1 -0
- package/src/workers/worker-dispatch.js +842 -0
- package/src/workers/worker-dispatch.js.map +1 -0
- package/src/workers/worker-dispatch.ts +1076 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -0
- package/vitest.config.ts +20 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swarm Application Layer - Public Exports
|
|
3
|
+
*
|
|
4
|
+
* @module v3/swarm/application
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Commands
|
|
8
|
+
export {
|
|
9
|
+
SpawnAgentCommandHandler,
|
|
10
|
+
TerminateAgentCommandHandler,
|
|
11
|
+
type SpawnAgentInput,
|
|
12
|
+
type SpawnAgentResult,
|
|
13
|
+
type TerminateAgentInput,
|
|
14
|
+
type TerminateAgentResult,
|
|
15
|
+
} from './commands/spawn-agent.command.js';
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
CreateTaskCommandHandler,
|
|
19
|
+
CancelTaskCommandHandler,
|
|
20
|
+
type CreateTaskInput,
|
|
21
|
+
type CreateTaskResult,
|
|
22
|
+
type CancelTaskInput,
|
|
23
|
+
type CancelTaskResult,
|
|
24
|
+
} from './commands/create-task.command.js';
|
|
25
|
+
|
|
26
|
+
// Application Service
|
|
27
|
+
export {
|
|
28
|
+
SwarmApplicationService,
|
|
29
|
+
type SwarmConfig,
|
|
30
|
+
} from './services/swarm-application-service.js';
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swarm Application Service - Application Layer
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates swarm operations and provides simplified interface.
|
|
5
|
+
*
|
|
6
|
+
* @module v3/swarm/application/services
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Agent, AgentRole, AgentStatus } from '../../domain/entities/agent.js';
|
|
10
|
+
import { Task, TaskStatus, TaskPriority } from '../../domain/entities/task.js';
|
|
11
|
+
import { IAgentRepository, AgentStatistics } from '../../domain/repositories/agent-repository.interface.js';
|
|
12
|
+
import { ITaskRepository, TaskStatistics } from '../../domain/repositories/task-repository.interface.js';
|
|
13
|
+
import { CoordinationService, LoadBalancingStrategy, SwarmHealth } from '../../domain/services/coordination-service.js';
|
|
14
|
+
import { SpawnAgentCommandHandler, SpawnAgentInput, TerminateAgentCommandHandler } from '../commands/spawn-agent.command.js';
|
|
15
|
+
import { CreateTaskCommandHandler, CreateTaskInput, CancelTaskCommandHandler } from '../commands/create-task.command.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Swarm configuration
|
|
19
|
+
*/
|
|
20
|
+
export interface SwarmConfig {
|
|
21
|
+
loadBalancingStrategy?: LoadBalancingStrategy;
|
|
22
|
+
autoScaling?: boolean;
|
|
23
|
+
minAgents?: number;
|
|
24
|
+
maxAgents?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Swarm Application Service
|
|
29
|
+
*/
|
|
30
|
+
export class SwarmApplicationService {
|
|
31
|
+
private readonly coordinationService: CoordinationService;
|
|
32
|
+
private readonly spawnHandler: SpawnAgentCommandHandler;
|
|
33
|
+
private readonly terminateHandler: TerminateAgentCommandHandler;
|
|
34
|
+
private readonly createTaskHandler: CreateTaskCommandHandler;
|
|
35
|
+
private readonly cancelTaskHandler: CancelTaskCommandHandler;
|
|
36
|
+
|
|
37
|
+
constructor(
|
|
38
|
+
private readonly agentRepository: IAgentRepository,
|
|
39
|
+
private readonly taskRepository: ITaskRepository,
|
|
40
|
+
private readonly config: SwarmConfig = {}
|
|
41
|
+
) {
|
|
42
|
+
this.coordinationService = new CoordinationService(agentRepository, taskRepository);
|
|
43
|
+
this.spawnHandler = new SpawnAgentCommandHandler(agentRepository);
|
|
44
|
+
this.terminateHandler = new TerminateAgentCommandHandler(agentRepository);
|
|
45
|
+
this.createTaskHandler = new CreateTaskCommandHandler(taskRepository);
|
|
46
|
+
this.cancelTaskHandler = new CancelTaskCommandHandler(taskRepository);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ============================================================================
|
|
50
|
+
// Agent Operations
|
|
51
|
+
// ============================================================================
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Spawn a new agent
|
|
55
|
+
*/
|
|
56
|
+
async spawnAgent(input: SpawnAgentInput): Promise<Agent> {
|
|
57
|
+
const result = await this.spawnHandler.execute(input);
|
|
58
|
+
return result.agent;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Terminate an agent
|
|
63
|
+
*/
|
|
64
|
+
async terminateAgent(agentId: string, force = false): Promise<void> {
|
|
65
|
+
await this.terminateHandler.execute({ agentId, force });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get agent by ID
|
|
70
|
+
*/
|
|
71
|
+
async getAgent(agentId: string): Promise<Agent | null> {
|
|
72
|
+
return this.agentRepository.findById(agentId);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* List all agents
|
|
77
|
+
*/
|
|
78
|
+
async listAgents(options?: {
|
|
79
|
+
status?: AgentStatus;
|
|
80
|
+
role?: AgentRole;
|
|
81
|
+
domain?: string;
|
|
82
|
+
}): Promise<Agent[]> {
|
|
83
|
+
return this.agentRepository.findAll(options);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Get agent statistics
|
|
88
|
+
*/
|
|
89
|
+
async getAgentStatistics(): Promise<AgentStatistics> {
|
|
90
|
+
return this.agentRepository.getStatistics();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ============================================================================
|
|
94
|
+
// Task Operations
|
|
95
|
+
// ============================================================================
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Create a new task
|
|
99
|
+
*/
|
|
100
|
+
async createTask(input: CreateTaskInput): Promise<Task> {
|
|
101
|
+
const result = await this.createTaskHandler.execute(input);
|
|
102
|
+
return result.task;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Cancel a task
|
|
107
|
+
*/
|
|
108
|
+
async cancelTask(taskId: string): Promise<void> {
|
|
109
|
+
await this.cancelTaskHandler.execute({ taskId });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Get task by ID
|
|
114
|
+
*/
|
|
115
|
+
async getTask(taskId: string): Promise<Task | null> {
|
|
116
|
+
return this.taskRepository.findById(taskId);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* List tasks
|
|
121
|
+
*/
|
|
122
|
+
async listTasks(options?: {
|
|
123
|
+
status?: TaskStatus;
|
|
124
|
+
priority?: TaskPriority;
|
|
125
|
+
assignedAgentId?: string;
|
|
126
|
+
}): Promise<Task[]> {
|
|
127
|
+
return this.taskRepository.findAll(options);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Get task statistics
|
|
132
|
+
*/
|
|
133
|
+
async getTaskStatistics(): Promise<TaskStatistics> {
|
|
134
|
+
return this.taskRepository.getStatistics();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ============================================================================
|
|
138
|
+
// Orchestration
|
|
139
|
+
// ============================================================================
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Assign pending tasks to available agents
|
|
143
|
+
*/
|
|
144
|
+
async processPendingTasks(): Promise<number> {
|
|
145
|
+
const queuedTasks = await this.taskRepository.findQueued();
|
|
146
|
+
let assigned = 0;
|
|
147
|
+
|
|
148
|
+
for (const task of queuedTasks) {
|
|
149
|
+
const result = await this.coordinationService.assignTask(
|
|
150
|
+
task.id,
|
|
151
|
+
this.config.loadBalancingStrategy ?? 'capability-match'
|
|
152
|
+
);
|
|
153
|
+
if (result.success) assigned++;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return assigned;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Complete a task
|
|
161
|
+
*/
|
|
162
|
+
async completeTask(taskId: string, output?: unknown): Promise<void> {
|
|
163
|
+
await this.coordinationService.processTaskCompletion(taskId, output);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Fail a task
|
|
168
|
+
*/
|
|
169
|
+
async failTask(taskId: string, error: string): Promise<boolean> {
|
|
170
|
+
return this.coordinationService.processTaskFailure(taskId, error);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get swarm health
|
|
175
|
+
*/
|
|
176
|
+
async getHealth(): Promise<SwarmHealth> {
|
|
177
|
+
return this.coordinationService.getSwarmHealth();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Get scaling recommendation
|
|
182
|
+
*/
|
|
183
|
+
async getScalingRecommendation() {
|
|
184
|
+
return this.coordinationService.calculateScalingRecommendation();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ============================================================================
|
|
188
|
+
// Lifecycle
|
|
189
|
+
// ============================================================================
|
|
190
|
+
|
|
191
|
+
async initialize(): Promise<void> {
|
|
192
|
+
await this.agentRepository.initialize();
|
|
193
|
+
await this.taskRepository.initialize();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async shutdown(): Promise<void> {
|
|
197
|
+
await this.agentRepository.shutdown();
|
|
198
|
+
await this.taskRepository.shutdown();
|
|
199
|
+
}
|
|
200
|
+
}
|