@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.
Files changed (65) hide show
  1. package/MIGRATION.md +472 -0
  2. package/README.md +634 -0
  3. package/__tests__/consensus.test.ts +577 -0
  4. package/__tests__/coordinator.test.ts +501 -0
  5. package/__tests__/queen-coordinator.test.ts +1335 -0
  6. package/__tests__/topology.test.ts +621 -0
  7. package/package.json +32 -0
  8. package/src/agent-pool.ts +476 -0
  9. package/src/application/commands/create-task.command.ts +124 -0
  10. package/src/application/commands/spawn-agent.command.ts +122 -0
  11. package/src/application/index.ts +30 -0
  12. package/src/application/services/swarm-application-service.ts +200 -0
  13. package/src/attention-coordinator.ts +1000 -0
  14. package/src/consensus/byzantine.ts +431 -0
  15. package/src/consensus/gossip.ts +513 -0
  16. package/src/consensus/index.ts +267 -0
  17. package/src/consensus/raft.ts +443 -0
  18. package/src/coordination/agent-registry.ts +544 -0
  19. package/src/coordination/index.ts +23 -0
  20. package/src/coordination/swarm-hub.ts +776 -0
  21. package/src/coordination/task-orchestrator.ts +605 -0
  22. package/src/domain/entities/agent.d.ts +151 -0
  23. package/src/domain/entities/agent.d.ts.map +1 -0
  24. package/src/domain/entities/agent.js +280 -0
  25. package/src/domain/entities/agent.js.map +1 -0
  26. package/src/domain/entities/agent.ts +370 -0
  27. package/src/domain/entities/task.d.ts +133 -0
  28. package/src/domain/entities/task.d.ts.map +1 -0
  29. package/src/domain/entities/task.js +261 -0
  30. package/src/domain/entities/task.js.map +1 -0
  31. package/src/domain/entities/task.ts +319 -0
  32. package/src/domain/index.ts +41 -0
  33. package/src/domain/repositories/agent-repository.interface.d.ts +57 -0
  34. package/src/domain/repositories/agent-repository.interface.d.ts.map +1 -0
  35. package/src/domain/repositories/agent-repository.interface.js +9 -0
  36. package/src/domain/repositories/agent-repository.interface.js.map +1 -0
  37. package/src/domain/repositories/agent-repository.interface.ts +69 -0
  38. package/src/domain/repositories/task-repository.interface.d.ts +61 -0
  39. package/src/domain/repositories/task-repository.interface.d.ts.map +1 -0
  40. package/src/domain/repositories/task-repository.interface.js +9 -0
  41. package/src/domain/repositories/task-repository.interface.js.map +1 -0
  42. package/src/domain/repositories/task-repository.interface.ts +75 -0
  43. package/src/domain/services/coordination-service.ts +320 -0
  44. package/src/federation-hub.d.ts +284 -0
  45. package/src/federation-hub.d.ts.map +1 -0
  46. package/src/federation-hub.js +692 -0
  47. package/src/federation-hub.js.map +1 -0
  48. package/src/federation-hub.ts +979 -0
  49. package/src/index.ts +348 -0
  50. package/src/message-bus.ts +607 -0
  51. package/src/queen-coordinator.ts +2025 -0
  52. package/src/shared/events.ts +285 -0
  53. package/src/shared/types.ts +389 -0
  54. package/src/topology-manager.ts +656 -0
  55. package/src/types.ts +545 -0
  56. package/src/unified-coordinator.ts +1844 -0
  57. package/src/workers/index.ts +65 -0
  58. package/src/workers/worker-dispatch.d.ts +234 -0
  59. package/src/workers/worker-dispatch.d.ts.map +1 -0
  60. package/src/workers/worker-dispatch.js +842 -0
  61. package/src/workers/worker-dispatch.js.map +1 -0
  62. package/src/workers/worker-dispatch.ts +1076 -0
  63. package/tmp.json +0 -0
  64. package/tsconfig.json +9 -0
  65. package/vitest.config.ts +20 -0
@@ -0,0 +1,261 @@
1
+ /**
2
+ * Task Entity - Domain Layer
3
+ *
4
+ * Core domain entity representing a task in the swarm.
5
+ * Tasks are units of work assigned to agents.
6
+ *
7
+ * @module v3/swarm/domain/entities
8
+ */
9
+ import { randomUUID } from 'crypto';
10
+ /**
11
+ * Task - Entity
12
+ *
13
+ * Represents a unit of work with lifecycle management,
14
+ * dependency tracking, and result storage.
15
+ */
16
+ export class Task {
17
+ _id;
18
+ _title;
19
+ _description;
20
+ _type;
21
+ _priority;
22
+ _status;
23
+ _assignedAgentId;
24
+ _dependencies;
25
+ _metadata;
26
+ _input;
27
+ _output;
28
+ _error;
29
+ _retryCount;
30
+ _maxRetries;
31
+ _timeout;
32
+ _createdAt;
33
+ _startedAt;
34
+ _completedAt;
35
+ constructor(props) {
36
+ const now = new Date();
37
+ this._id = props.id ?? randomUUID();
38
+ this._title = props.title;
39
+ this._description = props.description;
40
+ this._type = props.type;
41
+ this._priority = props.priority ?? 'normal';
42
+ this._status = props.status ?? 'pending';
43
+ this._assignedAgentId = props.assignedAgentId;
44
+ this._dependencies = new Set(props.dependencies ?? []);
45
+ this._metadata = props.metadata ?? {};
46
+ this._input = props.input;
47
+ this._output = props.output;
48
+ this._error = props.error;
49
+ this._retryCount = props.retryCount ?? 0;
50
+ this._maxRetries = props.maxRetries ?? 3;
51
+ this._timeout = props.timeout ?? 300000; // 5 minutes default
52
+ this._createdAt = props.createdAt ?? now;
53
+ this._startedAt = props.startedAt;
54
+ this._completedAt = props.completedAt;
55
+ }
56
+ static create(props) {
57
+ return new Task(props);
58
+ }
59
+ static fromPersistence(props) {
60
+ return new Task(props);
61
+ }
62
+ // Getters
63
+ get id() {
64
+ return this._id;
65
+ }
66
+ get title() {
67
+ return this._title;
68
+ }
69
+ get description() {
70
+ return this._description;
71
+ }
72
+ get type() {
73
+ return this._type;
74
+ }
75
+ get priority() {
76
+ return this._priority;
77
+ }
78
+ get status() {
79
+ return this._status;
80
+ }
81
+ get assignedAgentId() {
82
+ return this._assignedAgentId;
83
+ }
84
+ get dependencies() {
85
+ return Array.from(this._dependencies);
86
+ }
87
+ get metadata() {
88
+ return { ...this._metadata };
89
+ }
90
+ get input() {
91
+ return this._input;
92
+ }
93
+ get output() {
94
+ return this._output;
95
+ }
96
+ get error() {
97
+ return this._error;
98
+ }
99
+ get retryCount() {
100
+ return this._retryCount;
101
+ }
102
+ get maxRetries() {
103
+ return this._maxRetries;
104
+ }
105
+ get timeout() {
106
+ return this._timeout;
107
+ }
108
+ get createdAt() {
109
+ return new Date(this._createdAt);
110
+ }
111
+ get startedAt() {
112
+ return this._startedAt ? new Date(this._startedAt) : undefined;
113
+ }
114
+ get completedAt() {
115
+ return this._completedAt ? new Date(this._completedAt) : undefined;
116
+ }
117
+ // ============================================================================
118
+ // Business Logic
119
+ // ============================================================================
120
+ /**
121
+ * Queue the task for execution
122
+ */
123
+ queue() {
124
+ if (this._status !== 'pending') {
125
+ throw new Error('Can only queue pending tasks');
126
+ }
127
+ this._status = 'queued';
128
+ }
129
+ /**
130
+ * Assign task to an agent
131
+ */
132
+ assign(agentId) {
133
+ if (this._status !== 'queued' && this._status !== 'pending') {
134
+ throw new Error('Can only assign queued or pending tasks');
135
+ }
136
+ this._assignedAgentId = agentId;
137
+ this._status = 'assigned';
138
+ }
139
+ /**
140
+ * Start task execution
141
+ */
142
+ start() {
143
+ if (this._status !== 'assigned') {
144
+ throw new Error('Can only start assigned tasks');
145
+ }
146
+ this._status = 'running';
147
+ this._startedAt = new Date();
148
+ }
149
+ /**
150
+ * Complete the task successfully
151
+ */
152
+ complete(output) {
153
+ if (this._status !== 'running') {
154
+ throw new Error('Can only complete running tasks');
155
+ }
156
+ this._status = 'completed';
157
+ this._output = output;
158
+ this._completedAt = new Date();
159
+ }
160
+ /**
161
+ * Mark task as failed
162
+ */
163
+ fail(error) {
164
+ if (this._status !== 'running' && this._status !== 'assigned') {
165
+ throw new Error('Can only fail running or assigned tasks');
166
+ }
167
+ this._error = error;
168
+ this._retryCount++;
169
+ if (this._retryCount >= this._maxRetries) {
170
+ this._status = 'failed';
171
+ this._completedAt = new Date();
172
+ }
173
+ else {
174
+ // Reset for retry
175
+ this._status = 'queued';
176
+ this._assignedAgentId = undefined;
177
+ }
178
+ }
179
+ /**
180
+ * Cancel the task
181
+ */
182
+ cancel() {
183
+ if (this._status === 'completed' || this._status === 'failed') {
184
+ throw new Error('Cannot cancel finished tasks');
185
+ }
186
+ this._status = 'cancelled';
187
+ this._completedAt = new Date();
188
+ }
189
+ /**
190
+ * Check if all dependencies are satisfied
191
+ */
192
+ areDependenciesSatisfied(completedTaskIds) {
193
+ for (const depId of this._dependencies) {
194
+ if (!completedTaskIds.has(depId)) {
195
+ return false;
196
+ }
197
+ }
198
+ return true;
199
+ }
200
+ /**
201
+ * Check if task can be retried
202
+ */
203
+ canRetry() {
204
+ return this._retryCount < this._maxRetries;
205
+ }
206
+ /**
207
+ * Get execution duration in milliseconds
208
+ */
209
+ getExecutionDuration() {
210
+ if (!this._startedAt)
211
+ return null;
212
+ const endTime = this._completedAt ?? new Date();
213
+ return endTime.getTime() - this._startedAt.getTime();
214
+ }
215
+ /**
216
+ * Check if task is timed out
217
+ */
218
+ isTimedOut() {
219
+ if (this._status !== 'running' || !this._startedAt)
220
+ return false;
221
+ return Date.now() - this._startedAt.getTime() > this._timeout;
222
+ }
223
+ /**
224
+ * Priority comparison (for sorting)
225
+ */
226
+ comparePriority(other) {
227
+ const priorityOrder = {
228
+ critical: 0,
229
+ high: 1,
230
+ normal: 2,
231
+ low: 3,
232
+ };
233
+ return priorityOrder[this._priority] - priorityOrder[other.priority];
234
+ }
235
+ toPersistence() {
236
+ return {
237
+ id: this._id,
238
+ title: this._title,
239
+ description: this._description,
240
+ type: this._type,
241
+ priority: this._priority,
242
+ status: this._status,
243
+ assignedAgentId: this._assignedAgentId,
244
+ dependencies: Array.from(this._dependencies),
245
+ metadata: this._metadata,
246
+ input: this._input,
247
+ output: this._output,
248
+ error: this._error,
249
+ retryCount: this._retryCount,
250
+ maxRetries: this._maxRetries,
251
+ timeout: this._timeout,
252
+ createdAt: this._createdAt.toISOString(),
253
+ startedAt: this._startedAt?.toISOString(),
254
+ completedAt: this._completedAt?.toISOString(),
255
+ };
256
+ }
257
+ toJSON() {
258
+ return this.toPersistence();
259
+ }
260
+ }
261
+ //# sourceMappingURL=task.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.js","sourceRoot":"","sources":["task.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AA2CpC;;;;;GAKG;AACH,MAAM,OAAO,IAAI;IACP,GAAG,CAAS;IACZ,MAAM,CAAS;IAC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AQ,CAAC,CAAC;IACvE,CAAC;IAED,aAAa;QACX,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,GAAG;YACZ,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,WAAW,EAAE,IAAI,CAAC,YAAY;YAC9B,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,QAAQ,EAAE,IAAI,CAAC,SAAS;YACxB,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,eAAe,EAAE,IAAI,CAAC,gBAAgB;YACtC,YAAY,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC;YAC5C,QAAQ,EAAE,IAAI,CAAC,SAAS;YACxB,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,UAAU,EAAE,IAAI,CAAC,WAAW;YAC5B,UAAU,EAAE,IAAI,CAAC,WAAW;YAC5B,OAAO,EAAE,IAAI,CAAC,QAAQ;YACtB,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,WAAW,EAAE;YACxC,SAAS,EAAE,IAAI,CAAC,UAAU,EAAE,WAAW,EAAE;YACzC,WAAW,EAAE,IAAI,CAAC,YAAY,EAAE,WAAW,EAAE;SAC9C,CAAC;IACJ,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC;IAC9B,CAAC;CACF"}
@@ -0,0 +1,319 @@
1
+ /**
2
+ * Task Entity - Domain Layer
3
+ *
4
+ * Core domain entity representing a task in the swarm.
5
+ * Tasks are units of work assigned to agents.
6
+ *
7
+ * @module v3/swarm/domain/entities
8
+ */
9
+
10
+ import { randomUUID } from 'crypto';
11
+
12
+ /**
13
+ * Task status types
14
+ */
15
+ export type TaskStatus =
16
+ | 'pending'
17
+ | 'queued'
18
+ | 'assigned'
19
+ | 'running'
20
+ | 'completed'
21
+ | 'failed'
22
+ | 'cancelled';
23
+
24
+ /**
25
+ * Task priority levels
26
+ */
27
+ export type TaskPriority = 'critical' | 'high' | 'normal' | 'low';
28
+
29
+ /**
30
+ * Task properties
31
+ */
32
+ export interface TaskProps {
33
+ id?: string;
34
+ title: string;
35
+ description: string;
36
+ type: string;
37
+ priority?: TaskPriority;
38
+ status?: TaskStatus;
39
+ assignedAgentId?: string;
40
+ dependencies?: string[];
41
+ metadata?: Record<string, unknown>;
42
+ input?: unknown;
43
+ output?: unknown;
44
+ error?: string;
45
+ retryCount?: number;
46
+ maxRetries?: number;
47
+ timeout?: number;
48
+ createdAt?: Date;
49
+ startedAt?: Date;
50
+ completedAt?: Date;
51
+ }
52
+
53
+ /**
54
+ * Task - Entity
55
+ *
56
+ * Represents a unit of work with lifecycle management,
57
+ * dependency tracking, and result storage.
58
+ */
59
+ export class Task {
60
+ private _id: string;
61
+ private _title: string;
62
+ private _description: string;
63
+ private _type: string;
64
+ private _priority: TaskPriority;
65
+ private _status: TaskStatus;
66
+ private _assignedAgentId?: string;
67
+ private _dependencies: Set<string>;
68
+ private _metadata: Record<string, unknown>;
69
+ private _input?: unknown;
70
+ private _output?: unknown;
71
+ private _error?: string;
72
+ private _retryCount: number;
73
+ private _maxRetries: number;
74
+ private _timeout: number;
75
+ private _createdAt: Date;
76
+ private _startedAt?: Date;
77
+ private _completedAt?: Date;
78
+
79
+ private constructor(props: TaskProps) {
80
+ const now = new Date();
81
+ this._id = props.id ?? randomUUID();
82
+ this._title = props.title;
83
+ this._description = props.description;
84
+ this._type = props.type;
85
+ this._priority = props.priority ?? 'normal';
86
+ this._status = props.status ?? 'pending';
87
+ this._assignedAgentId = props.assignedAgentId;
88
+ this._dependencies = new Set(props.dependencies ?? []);
89
+ this._metadata = props.metadata ?? {};
90
+ this._input = props.input;
91
+ this._output = props.output;
92
+ this._error = props.error;
93
+ this._retryCount = props.retryCount ?? 0;
94
+ this._maxRetries = props.maxRetries ?? 3;
95
+ this._timeout = props.timeout ?? 300000; // 5 minutes default
96
+ this._createdAt = props.createdAt ?? now;
97
+ this._startedAt = props.startedAt;
98
+ this._completedAt = props.completedAt;
99
+ }
100
+
101
+ static create(props: TaskProps): Task {
102
+ return new Task(props);
103
+ }
104
+
105
+ static fromPersistence(props: TaskProps): Task {
106
+ return new Task(props);
107
+ }
108
+
109
+ // Getters
110
+ get id(): string {
111
+ return this._id;
112
+ }
113
+ get title(): string {
114
+ return this._title;
115
+ }
116
+ get description(): string {
117
+ return this._description;
118
+ }
119
+ get type(): string {
120
+ return this._type;
121
+ }
122
+ get priority(): TaskPriority {
123
+ return this._priority;
124
+ }
125
+ get status(): TaskStatus {
126
+ return this._status;
127
+ }
128
+ get assignedAgentId(): string | undefined {
129
+ return this._assignedAgentId;
130
+ }
131
+ get dependencies(): string[] {
132
+ return Array.from(this._dependencies);
133
+ }
134
+ get metadata(): Record<string, unknown> {
135
+ return { ...this._metadata };
136
+ }
137
+ get input(): unknown {
138
+ return this._input;
139
+ }
140
+ get output(): unknown {
141
+ return this._output;
142
+ }
143
+ get error(): string | undefined {
144
+ return this._error;
145
+ }
146
+ get retryCount(): number {
147
+ return this._retryCount;
148
+ }
149
+ get maxRetries(): number {
150
+ return this._maxRetries;
151
+ }
152
+ get timeout(): number {
153
+ return this._timeout;
154
+ }
155
+ get createdAt(): Date {
156
+ return new Date(this._createdAt);
157
+ }
158
+ get startedAt(): Date | undefined {
159
+ return this._startedAt ? new Date(this._startedAt) : undefined;
160
+ }
161
+ get completedAt(): Date | undefined {
162
+ return this._completedAt ? new Date(this._completedAt) : undefined;
163
+ }
164
+
165
+ // ============================================================================
166
+ // Business Logic
167
+ // ============================================================================
168
+
169
+ /**
170
+ * Queue the task for execution
171
+ */
172
+ queue(): void {
173
+ if (this._status !== 'pending') {
174
+ throw new Error('Can only queue pending tasks');
175
+ }
176
+ this._status = 'queued';
177
+ }
178
+
179
+ /**
180
+ * Assign task to an agent
181
+ */
182
+ assign(agentId: string): void {
183
+ if (this._status !== 'queued' && this._status !== 'pending') {
184
+ throw new Error('Can only assign queued or pending tasks');
185
+ }
186
+ this._assignedAgentId = agentId;
187
+ this._status = 'assigned';
188
+ }
189
+
190
+ /**
191
+ * Start task execution
192
+ */
193
+ start(): void {
194
+ if (this._status !== 'assigned') {
195
+ throw new Error('Can only start assigned tasks');
196
+ }
197
+ this._status = 'running';
198
+ this._startedAt = new Date();
199
+ }
200
+
201
+ /**
202
+ * Complete the task successfully
203
+ */
204
+ complete(output?: unknown): void {
205
+ if (this._status !== 'running') {
206
+ throw new Error('Can only complete running tasks');
207
+ }
208
+ this._status = 'completed';
209
+ this._output = output;
210
+ this._completedAt = new Date();
211
+ }
212
+
213
+ /**
214
+ * Mark task as failed
215
+ */
216
+ fail(error: string): void {
217
+ if (this._status !== 'running' && this._status !== 'assigned') {
218
+ throw new Error('Can only fail running or assigned tasks');
219
+ }
220
+ this._error = error;
221
+ this._retryCount++;
222
+
223
+ if (this._retryCount >= this._maxRetries) {
224
+ this._status = 'failed';
225
+ this._completedAt = new Date();
226
+ } else {
227
+ // Reset for retry
228
+ this._status = 'queued';
229
+ this._assignedAgentId = undefined;
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Cancel the task
235
+ */
236
+ cancel(): void {
237
+ if (this._status === 'completed' || this._status === 'failed') {
238
+ throw new Error('Cannot cancel finished tasks');
239
+ }
240
+ this._status = 'cancelled';
241
+ this._completedAt = new Date();
242
+ }
243
+
244
+ /**
245
+ * Check if all dependencies are satisfied
246
+ */
247
+ areDependenciesSatisfied(completedTaskIds: Set<string>): boolean {
248
+ for (const depId of this._dependencies) {
249
+ if (!completedTaskIds.has(depId)) {
250
+ return false;
251
+ }
252
+ }
253
+ return true;
254
+ }
255
+
256
+ /**
257
+ * Check if task can be retried
258
+ */
259
+ canRetry(): boolean {
260
+ return this._retryCount < this._maxRetries;
261
+ }
262
+
263
+ /**
264
+ * Get execution duration in milliseconds
265
+ */
266
+ getExecutionDuration(): number | null {
267
+ if (!this._startedAt) return null;
268
+ const endTime = this._completedAt ?? new Date();
269
+ return endTime.getTime() - this._startedAt.getTime();
270
+ }
271
+
272
+ /**
273
+ * Check if task is timed out
274
+ */
275
+ isTimedOut(): boolean {
276
+ if (this._status !== 'running' || !this._startedAt) return false;
277
+ return Date.now() - this._startedAt.getTime() > this._timeout;
278
+ }
279
+
280
+ /**
281
+ * Priority comparison (for sorting)
282
+ */
283
+ comparePriority(other: Task): number {
284
+ const priorityOrder: Record<TaskPriority, number> = {
285
+ critical: 0,
286
+ high: 1,
287
+ normal: 2,
288
+ low: 3,
289
+ };
290
+ return priorityOrder[this._priority] - priorityOrder[other.priority];
291
+ }
292
+
293
+ toPersistence(): Record<string, unknown> {
294
+ return {
295
+ id: this._id,
296
+ title: this._title,
297
+ description: this._description,
298
+ type: this._type,
299
+ priority: this._priority,
300
+ status: this._status,
301
+ assignedAgentId: this._assignedAgentId,
302
+ dependencies: Array.from(this._dependencies),
303
+ metadata: this._metadata,
304
+ input: this._input,
305
+ output: this._output,
306
+ error: this._error,
307
+ retryCount: this._retryCount,
308
+ maxRetries: this._maxRetries,
309
+ timeout: this._timeout,
310
+ createdAt: this._createdAt.toISOString(),
311
+ startedAt: this._startedAt?.toISOString(),
312
+ completedAt: this._completedAt?.toISOString(),
313
+ };
314
+ }
315
+
316
+ toJSON(): Record<string, unknown> {
317
+ return this.toPersistence();
318
+ }
319
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Swarm Domain Layer - Public Exports
3
+ *
4
+ * @module v3/swarm/domain
5
+ */
6
+
7
+ // Entities
8
+ export {
9
+ Agent,
10
+ type AgentStatus,
11
+ type AgentRole,
12
+ type AgentProps,
13
+ } from './entities/agent.js';
14
+
15
+ export {
16
+ Task,
17
+ type TaskStatus,
18
+ type TaskPriority,
19
+ type TaskProps,
20
+ } from './entities/task.js';
21
+
22
+ // Repository Interfaces
23
+ export {
24
+ type IAgentRepository,
25
+ type AgentQueryOptions,
26
+ type AgentStatistics,
27
+ } from './repositories/agent-repository.interface.js';
28
+
29
+ export {
30
+ type ITaskRepository,
31
+ type TaskQueryOptions,
32
+ type TaskStatistics,
33
+ } from './repositories/task-repository.interface.js';
34
+
35
+ // Domain Services
36
+ export {
37
+ CoordinationService,
38
+ type LoadBalancingStrategy,
39
+ type TaskAssignmentResult,
40
+ type SwarmHealth,
41
+ } from './services/coordination-service.js';
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Agent Repository Interface - Domain Layer
3
+ *
4
+ * Defines the contract for agent persistence.
5
+ *
6
+ * @module v3/swarm/domain/repositories
7
+ */
8
+ import { Agent, AgentStatus, AgentRole } from '../entities/agent.js';
9
+ /**
10
+ * Agent query options
11
+ */
12
+ export interface AgentQueryOptions {
13
+ status?: AgentStatus;
14
+ role?: AgentRole;
15
+ domain?: string;
16
+ parentId?: string;
17
+ capability?: string;
18
+ limit?: number;
19
+ offset?: number;
20
+ }
21
+ /**
22
+ * Agent statistics
23
+ */
24
+ export interface AgentStatistics {
25
+ total: number;
26
+ byStatus: Record<AgentStatus, number>;
27
+ byRole: Record<string, number>;
28
+ byDomain: Record<string, number>;
29
+ totalTasksCompleted: number;
30
+ averageUtilization: number;
31
+ }
32
+ /**
33
+ * Agent Repository Interface
34
+ */
35
+ export interface IAgentRepository {
36
+ save(agent: Agent): Promise<void>;
37
+ findById(id: string): Promise<Agent | null>;
38
+ findByName(name: string): Promise<Agent | null>;
39
+ delete(id: string): Promise<boolean>;
40
+ exists(id: string): Promise<boolean>;
41
+ saveMany(agents: Agent[]): Promise<void>;
42
+ findByIds(ids: string[]): Promise<Agent[]>;
43
+ deleteMany(ids: string[]): Promise<number>;
44
+ findAll(options?: AgentQueryOptions): Promise<Agent[]>;
45
+ findByStatus(status: AgentStatus): Promise<Agent[]>;
46
+ findByRole(role: AgentRole): Promise<Agent[]>;
47
+ findByDomain(domain: string): Promise<Agent[]>;
48
+ findByParent(parentId: string): Promise<Agent[]>;
49
+ findByCapability(capability: string): Promise<Agent[]>;
50
+ findAvailable(): Promise<Agent[]>;
51
+ getStatistics(): Promise<AgentStatistics>;
52
+ count(options?: AgentQueryOptions): Promise<number>;
53
+ initialize(): Promise<void>;
54
+ shutdown(): Promise<void>;
55
+ clear(): Promise<void>;
56
+ }
57
+ //# sourceMappingURL=agent-repository.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-repository.interface.d.ts","sourceRoot":"","sources":["agent-repository.interface.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACtC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,mBAAmB,EAAE,MAAM,CAAC;IAC5B,kBAAkB,EAAE,MAAM,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAE/B,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;IAC5C,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;IAChD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
+ /**
2
+ * Agent Repository Interface - Domain Layer
3
+ *
4
+ * Defines the contract for agent persistence.
5
+ *
6
+ * @module v3/swarm/domain/repositories
7
+ */
8
+ export {};
9
+ //# sourceMappingURL=agent-repository.interface.js.map