@sparkleideas/ruv-swarm 1.0.18-patch.1
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/README.md +1565 -0
- package/bin/ruv-swarm-clean.js +1872 -0
- package/bin/ruv-swarm-memory.js +119 -0
- package/bin/ruv-swarm-secure-heartbeat.js +1549 -0
- package/bin/ruv-swarm-secure.js +1689 -0
- package/package.json +221 -0
- package/src/agent.ts +342 -0
- package/src/benchmark.js +267 -0
- package/src/claude-flow-enhanced.js +839 -0
- package/src/claude-integration/advanced-commands.js +561 -0
- package/src/claude-integration/core.js +112 -0
- package/src/claude-integration/docs.js +1548 -0
- package/src/claude-integration/env-template.js +39 -0
- package/src/claude-integration/index.js +209 -0
- package/src/claude-integration/remote.js +408 -0
- package/src/cli-diagnostics.js +364 -0
- package/src/cognitive-pattern-evolution.js +1317 -0
- package/src/daa-cognition.js +977 -0
- package/src/daa-service.d.ts +298 -0
- package/src/daa-service.js +1116 -0
- package/src/diagnostics.js +533 -0
- package/src/errors.js +528 -0
- package/src/github-coordinator/README.md +193 -0
- package/src/github-coordinator/claude-hooks.js +162 -0
- package/src/github-coordinator/gh-cli-coordinator.js +260 -0
- package/src/hooks/cli.js +82 -0
- package/src/hooks/index.js +1900 -0
- package/src/index-enhanced.d.ts +371 -0
- package/src/index-enhanced.js +734 -0
- package/src/index.d.ts +287 -0
- package/src/index.js +405 -0
- package/src/index.ts +457 -0
- package/src/logger.js +182 -0
- package/src/logging-config.js +179 -0
- package/src/mcp-daa-tools.js +735 -0
- package/src/mcp-tools-benchmarks.js +328 -0
- package/src/mcp-tools-enhanced.js +2863 -0
- package/src/memory-config.js +42 -0
- package/src/meta-learning-framework.js +1359 -0
- package/src/neural-agent.js +830 -0
- package/src/neural-coordination-protocol.js +1363 -0
- package/src/neural-models/README.md +118 -0
- package/src/neural-models/autoencoder.js +543 -0
- package/src/neural-models/base.js +269 -0
- package/src/neural-models/cnn.js +497 -0
- package/src/neural-models/gnn.js +447 -0
- package/src/neural-models/gru.js +536 -0
- package/src/neural-models/index.js +273 -0
- package/src/neural-models/lstm.js +551 -0
- package/src/neural-models/neural-presets-complete.js +1306 -0
- package/src/neural-models/presets/graph.js +392 -0
- package/src/neural-models/presets/index.js +279 -0
- package/src/neural-models/presets/nlp.js +328 -0
- package/src/neural-models/presets/timeseries.js +368 -0
- package/src/neural-models/presets/vision.js +387 -0
- package/src/neural-models/resnet.js +534 -0
- package/src/neural-models/transformer.js +515 -0
- package/src/neural-models/vae.js +489 -0
- package/src/neural-network-manager.js +1938 -0
- package/src/neural-network.ts +296 -0
- package/src/neural.js +574 -0
- package/src/performance-benchmarks.js +898 -0
- package/src/performance.js +458 -0
- package/src/persistence-pooled.js +695 -0
- package/src/persistence.js +480 -0
- package/src/schemas.js +864 -0
- package/src/security.js +218 -0
- package/src/singleton-container.js +183 -0
- package/src/sqlite-pool.js +587 -0
- package/src/sqlite-worker.js +141 -0
- package/src/types.ts +164 -0
- package/src/utils.ts +286 -0
- package/src/wasm-loader.js +601 -0
- package/src/wasm-loader2.js +404 -0
- package/src/wasm-memory-optimizer.js +783 -0
- package/src/wasm-types.d.ts +63 -0
- package/wasm/README.md +347 -0
- package/wasm/neuro-divergent.wasm +0 -0
- package/wasm/package.json +18 -0
- package/wasm/ruv-fann.wasm +0 -0
- package/wasm/ruv_swarm_simd.wasm +0 -0
- package/wasm/ruv_swarm_wasm.d.ts +391 -0
- package/wasm/ruv_swarm_wasm.js +2164 -0
- package/wasm/ruv_swarm_wasm_bg.wasm +0 -0
- package/wasm/ruv_swarm_wasm_bg.wasm.d.ts +123 -0
- package/wasm/wasm-bindings-loader.mjs +435 -0
- package/wasm/wasm-updates.md +684 -0
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced RuvSwarm Main Class
|
|
3
|
+
* Provides full WASM capabilities with progressive loading,
|
|
4
|
+
* neural networks, forecasting, and swarm orchestration
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { WasmModuleLoader } from './wasm-loader.js';
|
|
8
|
+
import { SwarmPersistencePooled } from './persistence-pooled.js';
|
|
9
|
+
import { getContainer } from './singleton-container.js';
|
|
10
|
+
// import { NeuralAgentFactory } from './neural-agent.js';
|
|
11
|
+
// import path from 'path';
|
|
12
|
+
// import fs from 'fs';
|
|
13
|
+
|
|
14
|
+
class RuvSwarm {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.wasmLoader = new WasmModuleLoader();
|
|
17
|
+
this.persistence = null;
|
|
18
|
+
this.activeSwarms = new Map();
|
|
19
|
+
this.globalAgents = new Map();
|
|
20
|
+
this.isInitialized = false;
|
|
21
|
+
this.metrics = {
|
|
22
|
+
totalSwarms: 0,
|
|
23
|
+
totalAgents: 0,
|
|
24
|
+
totalTasks: 0,
|
|
25
|
+
memoryUsage: 0,
|
|
26
|
+
performance: {},
|
|
27
|
+
};
|
|
28
|
+
this.features = {
|
|
29
|
+
neural_networks: false,
|
|
30
|
+
forecasting: false,
|
|
31
|
+
cognitive_diversity: false,
|
|
32
|
+
simd_support: false,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Cleanup method for proper resource disposal
|
|
38
|
+
*/
|
|
39
|
+
destroy() {
|
|
40
|
+
console.log('🧹 Cleaning up RuvSwarm instance...');
|
|
41
|
+
|
|
42
|
+
// Terminate all active swarms
|
|
43
|
+
for (const swarm of this.activeSwarms.values()) {
|
|
44
|
+
if (typeof swarm.terminate === 'function') {
|
|
45
|
+
swarm.terminate();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.activeSwarms.clear();
|
|
50
|
+
this.globalAgents.clear();
|
|
51
|
+
|
|
52
|
+
// Cleanup persistence
|
|
53
|
+
if (this.persistence && typeof this.persistence.close === 'function') {
|
|
54
|
+
this.persistence.close();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Cleanup WASM loader
|
|
58
|
+
if (this.wasmLoader && typeof this.wasmLoader.cleanup === 'function') {
|
|
59
|
+
this.wasmLoader.cleanup();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
this.isInitialized = false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static async initialize(options = {}) {
|
|
66
|
+
const container = getContainer();
|
|
67
|
+
|
|
68
|
+
// Register RuvSwarm factory if not already registered
|
|
69
|
+
if (!container.has('RuvSwarm')) {
|
|
70
|
+
container.register('RuvSwarm', () => new RuvSwarm(), {
|
|
71
|
+
singleton: true,
|
|
72
|
+
lazy: false,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Get or create singleton instance through container
|
|
77
|
+
const instance = container.get('RuvSwarm');
|
|
78
|
+
|
|
79
|
+
const {
|
|
80
|
+
// wasmPath = './wasm',
|
|
81
|
+
loadingStrategy = 'progressive',
|
|
82
|
+
enablePersistence = true,
|
|
83
|
+
enableNeuralNetworks = true,
|
|
84
|
+
enableForecasting = false,
|
|
85
|
+
useSIMD = true,
|
|
86
|
+
debug = false,
|
|
87
|
+
} = options;
|
|
88
|
+
|
|
89
|
+
// Check if already initialized through container
|
|
90
|
+
if (instance.isInitialized) {
|
|
91
|
+
if (debug) {
|
|
92
|
+
console.log('[DEBUG] RuvSwarm already initialized through container');
|
|
93
|
+
}
|
|
94
|
+
return instance;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
console.log('🧠 Initializing @sparkleideas/ruv-swarm with WASM capabilities...');
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
// Initialize WASM modules
|
|
101
|
+
await instance.wasmLoader.initialize(loadingStrategy);
|
|
102
|
+
|
|
103
|
+
// Detect and enable features
|
|
104
|
+
await instance.detectFeatures(useSIMD);
|
|
105
|
+
|
|
106
|
+
// Initialize pooled persistence if enabled
|
|
107
|
+
if (enablePersistence) {
|
|
108
|
+
try {
|
|
109
|
+
// Configure pool settings based on environment or defaults
|
|
110
|
+
const poolOptions = {
|
|
111
|
+
maxReaders: parseInt(process.env.POOL_MAX_READERS) || 6,
|
|
112
|
+
maxWorkers: parseInt(process.env.POOL_MAX_WORKERS) || 3,
|
|
113
|
+
mmapSize: parseInt(process.env.POOL_MMAP_SIZE) || 268435456, // 256MB
|
|
114
|
+
cacheSize: parseInt(process.env.POOL_CACHE_SIZE) || -64000, // 64MB
|
|
115
|
+
enableBackup: process.env.POOL_ENABLE_BACKUP === 'true',
|
|
116
|
+
healthCheckInterval: 60000, // 1 minute
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
instance.persistence = new SwarmPersistencePooled(undefined, poolOptions);
|
|
120
|
+
await instance.persistence.initialize();
|
|
121
|
+
console.log('💾 High-availability pooled persistence layer initialized');
|
|
122
|
+
console.log(`📊 Pool configuration: ${poolOptions.maxReaders} readers, ${poolOptions.maxWorkers} workers`);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.warn('⚠️ Pooled persistence not available:', error.message);
|
|
125
|
+
instance.persistence = null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Pre-load neural networks if enabled
|
|
130
|
+
if (enableNeuralNetworks) {
|
|
131
|
+
try {
|
|
132
|
+
await instance.wasmLoader.loadModule('neural');
|
|
133
|
+
instance.features.neural_networks = true;
|
|
134
|
+
console.log('🧠 Neural network capabilities loaded');
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.warn('⚠️ Neural network module not available:', error.message);
|
|
137
|
+
instance.features.neural_networks = false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Pre-load forecasting if enabled
|
|
142
|
+
if (enableForecasting && enableNeuralNetworks) {
|
|
143
|
+
try {
|
|
144
|
+
await instance.wasmLoader.loadModule('forecasting');
|
|
145
|
+
instance.features.forecasting = true;
|
|
146
|
+
console.log('📈 Forecasting capabilities loaded');
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.warn('⚠️ Forecasting module not available:', error.message);
|
|
149
|
+
instance.features.forecasting = false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
console.log('✅ @sparkleideas/ruv-swarm initialized successfully');
|
|
154
|
+
console.log('📊 Features:', instance.features);
|
|
155
|
+
|
|
156
|
+
// Mark as initialized
|
|
157
|
+
instance.isInitialized = true;
|
|
158
|
+
|
|
159
|
+
return instance;
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.error('❌ Failed to initialize @sparkleideas/ruv-swarm:', error);
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async detectFeatures(useSIMD = true) {
|
|
167
|
+
try {
|
|
168
|
+
// Load core module to detect basic features
|
|
169
|
+
const coreModule = await this.wasmLoader.loadModule('core');
|
|
170
|
+
|
|
171
|
+
// Detect SIMD support
|
|
172
|
+
if (useSIMD) {
|
|
173
|
+
this.features.simd_support = RuvSwarm.detectSIMDSupport();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Check if core module has the expected exports
|
|
177
|
+
if (coreModule.exports) {
|
|
178
|
+
// Check for neural network support
|
|
179
|
+
this.features.neural_networks = true; // Will be validated when module loads
|
|
180
|
+
|
|
181
|
+
// Check for cognitive diversity support
|
|
182
|
+
this.features.cognitive_diversity = true; // Default enabled
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
console.log('🔍 Feature detection complete');
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.warn('⚠️ Feature detection failed:', error.message);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async createSwarm(config) {
|
|
192
|
+
const {
|
|
193
|
+
id = null, // Allow existing ID for persistence loading
|
|
194
|
+
name = 'default-swarm',
|
|
195
|
+
topology = 'mesh',
|
|
196
|
+
strategy = 'balanced',
|
|
197
|
+
maxAgents = 10,
|
|
198
|
+
enableCognitiveDiversity = true,
|
|
199
|
+
// enableNeuralAgents = true,
|
|
200
|
+
} = config;
|
|
201
|
+
|
|
202
|
+
// Ensure core module is loaded
|
|
203
|
+
const coreModule = await this.wasmLoader.loadModule('core');
|
|
204
|
+
|
|
205
|
+
// Create swarm configuration
|
|
206
|
+
const swarmConfig = {
|
|
207
|
+
name,
|
|
208
|
+
topology_type: topology,
|
|
209
|
+
max_agents: maxAgents,
|
|
210
|
+
enable_cognitive_diversity: enableCognitiveDiversity && this.features.cognitive_diversity,
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// Use the core module exports to create swarm
|
|
214
|
+
let wasmSwarm;
|
|
215
|
+
if (coreModule.exports && coreModule.exports.RuvSwarm) {
|
|
216
|
+
try {
|
|
217
|
+
wasmSwarm = new coreModule.exports.RuvSwarm();
|
|
218
|
+
// Store swarm config - use existing ID if provided
|
|
219
|
+
wasmSwarm.id = id || `swarm-${Date.now()}`;
|
|
220
|
+
wasmSwarm.name = name;
|
|
221
|
+
wasmSwarm.config = swarmConfig;
|
|
222
|
+
} catch (error) {
|
|
223
|
+
console.warn('Failed to create WASM swarm:', error.message);
|
|
224
|
+
// Fallback to JavaScript implementation
|
|
225
|
+
wasmSwarm = {
|
|
226
|
+
id: id || `swarm-${Date.now()}`,
|
|
227
|
+
name,
|
|
228
|
+
config: swarmConfig,
|
|
229
|
+
agents: new Map(),
|
|
230
|
+
tasks: new Map(),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
// Fallback for placeholder or different module structure
|
|
235
|
+
wasmSwarm = {
|
|
236
|
+
id: id || `swarm-${Date.now()}`,
|
|
237
|
+
name,
|
|
238
|
+
config: swarmConfig,
|
|
239
|
+
agents: new Map(),
|
|
240
|
+
tasks: new Map(),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Create JavaScript wrapper
|
|
245
|
+
const swarm = new Swarm(wasmSwarm.id || wasmSwarm.name, wasmSwarm, this);
|
|
246
|
+
|
|
247
|
+
// Persist swarm if persistence is enabled and this is a new swarm
|
|
248
|
+
if (this.persistence && !id) {
|
|
249
|
+
try {
|
|
250
|
+
this.persistence.createSwarm({
|
|
251
|
+
id: swarm.id,
|
|
252
|
+
name,
|
|
253
|
+
topology,
|
|
254
|
+
strategy,
|
|
255
|
+
maxAgents,
|
|
256
|
+
created: new Date().toISOString(),
|
|
257
|
+
});
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if (!error.message.includes('UNIQUE constraint failed')) {
|
|
260
|
+
console.warn('Failed to persist swarm:', error.message);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
this.activeSwarms.set(swarm.id, swarm);
|
|
266
|
+
this.metrics.totalSwarms++;
|
|
267
|
+
|
|
268
|
+
console.log(`🐝 Created swarm: ${name} (${swarm.id})`);
|
|
269
|
+
return swarm;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async getSwarmStatus(swarmId, detailed = false) {
|
|
273
|
+
const swarm = this.activeSwarms.get(swarmId);
|
|
274
|
+
if (!swarm) {
|
|
275
|
+
throw new Error(`Swarm not found: ${swarmId}`);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return swarm.getStatus(detailed);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async getAllSwarms() {
|
|
282
|
+
const swarms = [];
|
|
283
|
+
for (const [id, swarm] of this.activeSwarms) {
|
|
284
|
+
swarms.push({
|
|
285
|
+
id,
|
|
286
|
+
status: await swarm.getStatus(false),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
return swarms;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async getGlobalMetrics() {
|
|
293
|
+
this.metrics.memoryUsage = this.wasmLoader.getTotalMemoryUsage();
|
|
294
|
+
|
|
295
|
+
// Aggregate metrics from all swarms
|
|
296
|
+
let totalAgents = 0;
|
|
297
|
+
let totalTasks = 0;
|
|
298
|
+
|
|
299
|
+
for (const swarm of this.activeSwarms.values()) {
|
|
300
|
+
const status = await swarm.getStatus(false);
|
|
301
|
+
totalAgents += status.agents?.total || 0;
|
|
302
|
+
totalTasks += status.tasks?.total || 0;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
this.metrics.totalAgents = totalAgents;
|
|
306
|
+
this.metrics.totalTasks = totalTasks;
|
|
307
|
+
this.metrics.totalSwarms = this.activeSwarms.size;
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
...this.metrics,
|
|
311
|
+
features: this.features,
|
|
312
|
+
wasm_modules: this.wasmLoader.getModuleStatus(),
|
|
313
|
+
timestamp: new Date().toISOString(),
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Feature detection helpers
|
|
318
|
+
static detectSIMDSupport() {
|
|
319
|
+
try {
|
|
320
|
+
// Check for WebAssembly SIMD support using v128 type validation
|
|
321
|
+
// This is more compatible across Node.js versions
|
|
322
|
+
const simdTestModule = new Uint8Array([
|
|
323
|
+
0x00, 0x61, 0x73, 0x6d, // WASM magic
|
|
324
|
+
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
325
|
+
0x01, 0x05, 0x01, // Type section: 1 type
|
|
326
|
+
0x60, 0x00, 0x01, 0x7b, // Function type: () -> v128 (SIMD type)
|
|
327
|
+
]);
|
|
328
|
+
|
|
329
|
+
// If v128 type is supported, SIMD is available
|
|
330
|
+
return WebAssembly.validate(simdTestModule);
|
|
331
|
+
} catch {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static getVersion() {
|
|
337
|
+
return '0.2.0'; // Enhanced version with full WASM capabilities
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
static getMemoryUsage() {
|
|
341
|
+
if (typeof performance !== 'undefined' && performance.memory) {
|
|
342
|
+
return {
|
|
343
|
+
used: performance.memory.usedJSHeapSize,
|
|
344
|
+
total: performance.memory.totalJSHeapSize,
|
|
345
|
+
limit: performance.memory.jsHeapSizeLimit,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
static getRuntimeFeatures() {
|
|
352
|
+
return {
|
|
353
|
+
webassembly: typeof WebAssembly !== 'undefined',
|
|
354
|
+
simd: RuvSwarm.detectSIMDSupport(),
|
|
355
|
+
workers: typeof Worker !== 'undefined',
|
|
356
|
+
shared_array_buffer: typeof SharedArrayBuffer !== 'undefined',
|
|
357
|
+
bigint: typeof BigInt !== 'undefined',
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Instance method that delegates to static method for API convenience
|
|
362
|
+
detectSIMDSupport() {
|
|
363
|
+
return RuvSwarm.detectSIMDSupport();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Enhanced Swarm wrapper class
|
|
368
|
+
class Swarm {
|
|
369
|
+
constructor(id, wasmInstance, ruvSwarmInstance) {
|
|
370
|
+
this.id = id;
|
|
371
|
+
this.wasmSwarm = wasmInstance;
|
|
372
|
+
this.ruvSwarm = ruvSwarmInstance;
|
|
373
|
+
this.agents = new Map();
|
|
374
|
+
this.tasks = new Map();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async spawn(config) {
|
|
378
|
+
const {
|
|
379
|
+
id = null, // Allow existing ID for persistence loading
|
|
380
|
+
type = 'researcher',
|
|
381
|
+
name = null,
|
|
382
|
+
capabilities = null,
|
|
383
|
+
enableNeuralNetwork = true,
|
|
384
|
+
} = config;
|
|
385
|
+
|
|
386
|
+
// Ensure neural networks are loaded if requested
|
|
387
|
+
if (enableNeuralNetwork && this.ruvSwarm.features.neural_networks) {
|
|
388
|
+
await this.ruvSwarm.wasmLoader.loadModule('neural');
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const agentConfig = {
|
|
392
|
+
agent_type: type,
|
|
393
|
+
name: name || `${type}-${Date.now()}`,
|
|
394
|
+
capabilities: capabilities || [],
|
|
395
|
+
max_agents: 100, // Default limit
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
let result;
|
|
399
|
+
if (this.wasmSwarm.spawn) {
|
|
400
|
+
result = this.wasmSwarm.spawn(agentConfig);
|
|
401
|
+
} else {
|
|
402
|
+
// Fallback for placeholder - use existing ID if provided
|
|
403
|
+
result = {
|
|
404
|
+
agent_id: id || `agent-${Date.now()}`,
|
|
405
|
+
name: agentConfig.name,
|
|
406
|
+
type: agentConfig.agent_type,
|
|
407
|
+
capabilities: agentConfig.capabilities,
|
|
408
|
+
cognitive_pattern: 'adaptive',
|
|
409
|
+
neural_network_id: enableNeuralNetwork ? `nn-${Date.now()}` : null,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const agentId = id || result.agent_id || result.id;
|
|
414
|
+
|
|
415
|
+
// Create JavaScript wrapper
|
|
416
|
+
const agent = new Agent(agentId, result, this);
|
|
417
|
+
this.agents.set(agentId, agent);
|
|
418
|
+
|
|
419
|
+
// Persist agent if persistence is enabled and this is a new agent
|
|
420
|
+
if (this.ruvSwarm.persistence && !id) {
|
|
421
|
+
try {
|
|
422
|
+
this.ruvSwarm.persistence.createAgent({
|
|
423
|
+
id: agentId,
|
|
424
|
+
swarmId: this.id,
|
|
425
|
+
name: result.name,
|
|
426
|
+
type,
|
|
427
|
+
capabilities: result.capabilities,
|
|
428
|
+
cognitive_pattern: result.cognitive_pattern,
|
|
429
|
+
created: new Date().toISOString(),
|
|
430
|
+
});
|
|
431
|
+
} catch (error) {
|
|
432
|
+
if (!error.message.includes('UNIQUE constraint failed')) {
|
|
433
|
+
console.warn('Failed to persist agent:', error.message);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
console.log(`🤖 Spawned agent: ${result.name} (${type})`);
|
|
439
|
+
return agent;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async orchestrate(taskConfig) {
|
|
443
|
+
const {
|
|
444
|
+
description,
|
|
445
|
+
priority = 'medium',
|
|
446
|
+
dependencies = [],
|
|
447
|
+
maxAgents = null,
|
|
448
|
+
estimatedDuration = null,
|
|
449
|
+
requiredCapabilities = [],
|
|
450
|
+
} = taskConfig;
|
|
451
|
+
|
|
452
|
+
const config = {
|
|
453
|
+
description,
|
|
454
|
+
priority,
|
|
455
|
+
dependencies,
|
|
456
|
+
max_agents: maxAgents,
|
|
457
|
+
estimated_duration_ms: estimatedDuration,
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
let result;
|
|
461
|
+
if (this.wasmSwarm.orchestrate) {
|
|
462
|
+
result = this.wasmSwarm.orchestrate(config);
|
|
463
|
+
} else {
|
|
464
|
+
// Enhanced fallback with proper agent assignment
|
|
465
|
+
const availableAgents = this.selectAvailableAgents(requiredCapabilities, maxAgents);
|
|
466
|
+
|
|
467
|
+
if (availableAgents.length === 0) {
|
|
468
|
+
throw new Error('No agents available for task orchestration. Please spawn agents first.');
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// Assign task to selected agents
|
|
472
|
+
const assignedAgentIds = availableAgents.map(agent => agent.id);
|
|
473
|
+
|
|
474
|
+
// Update agent status to busy
|
|
475
|
+
for (const agent of availableAgents) {
|
|
476
|
+
await agent.updateStatus('busy');
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
result = {
|
|
480
|
+
task_id: `task-${Date.now()}`,
|
|
481
|
+
task_description: description,
|
|
482
|
+
description,
|
|
483
|
+
status: 'orchestrated',
|
|
484
|
+
assigned_agents: assignedAgentIds,
|
|
485
|
+
priority,
|
|
486
|
+
estimated_duration_ms: estimatedDuration,
|
|
487
|
+
agent_selection_strategy: 'capability_and_load_based',
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const taskId = result.task_id || result.id;
|
|
492
|
+
|
|
493
|
+
// Create JavaScript wrapper
|
|
494
|
+
const task = new Task(taskId, result, this);
|
|
495
|
+
this.tasks.set(taskId, task);
|
|
496
|
+
|
|
497
|
+
// Persist task if persistence is enabled
|
|
498
|
+
if (this.ruvSwarm.persistence) {
|
|
499
|
+
await this.ruvSwarm.persistence.createTask({
|
|
500
|
+
id: taskId,
|
|
501
|
+
swarmId: this.id,
|
|
502
|
+
description,
|
|
503
|
+
priority,
|
|
504
|
+
assigned_agents: result.assigned_agents,
|
|
505
|
+
created: new Date().toISOString(),
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
console.log(`📋 Orchestrated task: ${description} (${taskId}) - Assigned to ${result.assigned_agents.length} agents`);
|
|
510
|
+
return task;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Helper method to select available agents for task assignment
|
|
514
|
+
selectAvailableAgents(requiredCapabilities = [], maxAgents = null) {
|
|
515
|
+
const availableAgents = Array.from(this.agents.values()).filter(agent => {
|
|
516
|
+
// Agent must be idle or active (not busy)
|
|
517
|
+
if (agent.status === 'busy') {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Check if agent has required capabilities
|
|
522
|
+
if (requiredCapabilities.length > 0) {
|
|
523
|
+
const hasCapabilities = requiredCapabilities.some(capability =>
|
|
524
|
+
agent.capabilities.includes(capability),
|
|
525
|
+
);
|
|
526
|
+
if (!hasCapabilities) {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return true;
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
// Apply maxAgents limit if specified
|
|
535
|
+
if (maxAgents && maxAgents > 0) {
|
|
536
|
+
return availableAgents.slice(0, maxAgents);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return availableAgents;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
async getStatus(detailed = false) {
|
|
543
|
+
if (this.wasmSwarm.get_status) {
|
|
544
|
+
return this.wasmSwarm.get_status(detailed);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// Fallback status
|
|
548
|
+
return {
|
|
549
|
+
id: this.id,
|
|
550
|
+
agents: {
|
|
551
|
+
total: this.agents.size,
|
|
552
|
+
active: Array.from(this.agents.values()).filter(a => a.status === 'active').length,
|
|
553
|
+
idle: Array.from(this.agents.values()).filter(a => a.status === 'idle').length,
|
|
554
|
+
},
|
|
555
|
+
tasks: {
|
|
556
|
+
total: this.tasks.size,
|
|
557
|
+
pending: Array.from(this.tasks.values()).filter(t => t.status === 'pending').length,
|
|
558
|
+
in_progress: Array.from(this.tasks.values()).filter(t => t.status === 'in_progress').length,
|
|
559
|
+
completed: Array.from(this.tasks.values()).filter(t => t.status === 'completed').length,
|
|
560
|
+
},
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
async monitor(duration = 10000, interval = 1000) {
|
|
565
|
+
if (this.wasmSwarm.monitor) {
|
|
566
|
+
return this.wasmSwarm.monitor(duration, interval);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// Fallback monitoring
|
|
570
|
+
console.log(`📊 Monitoring swarm ${this.id} for ${duration}ms...`);
|
|
571
|
+
return {
|
|
572
|
+
duration,
|
|
573
|
+
interval,
|
|
574
|
+
snapshots: [],
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
async terminate() {
|
|
579
|
+
console.log(`🛑 Terminating swarm: ${this.id}`);
|
|
580
|
+
this.ruvSwarm.activeSwarms.delete(this.id);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// Enhanced Agent wrapper class
|
|
585
|
+
class Agent {
|
|
586
|
+
constructor(id, wasmResult, swarm) {
|
|
587
|
+
this.id = id;
|
|
588
|
+
this.name = wasmResult.name;
|
|
589
|
+
this.type = wasmResult.type || wasmResult.agent_type;
|
|
590
|
+
this.cognitivePattern = wasmResult.cognitive_pattern || 'adaptive';
|
|
591
|
+
this.capabilities = wasmResult.capabilities || [];
|
|
592
|
+
this.neuralNetworkId = wasmResult.neural_network_id;
|
|
593
|
+
this.status = 'idle';
|
|
594
|
+
this.swarm = swarm;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
async execute(_task) {
|
|
598
|
+
console.log(`🏃 Agent ${this.name} executing task`);
|
|
599
|
+
this.status = 'busy';
|
|
600
|
+
|
|
601
|
+
// Simulate task execution
|
|
602
|
+
const result = {
|
|
603
|
+
status: 'completed',
|
|
604
|
+
result: 'Task execution placeholder',
|
|
605
|
+
executionTime: 500,
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
this.status = 'idle';
|
|
609
|
+
return result;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
async getMetrics() {
|
|
613
|
+
return {
|
|
614
|
+
tasksCompleted: 0,
|
|
615
|
+
averageExecutionTime: 0,
|
|
616
|
+
successRate: 1.0,
|
|
617
|
+
memoryUsage: 5.0,
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
async updateStatus(status) {
|
|
622
|
+
this.status = status;
|
|
623
|
+
console.log(`📊 Agent ${this.name} status: ${status}`);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// Enhanced Task wrapper class
|
|
628
|
+
class Task {
|
|
629
|
+
constructor(id, wasmResult, swarm) {
|
|
630
|
+
this.id = id;
|
|
631
|
+
this.description = wasmResult.task_description || wasmResult.description;
|
|
632
|
+
this.status = wasmResult.status || 'pending';
|
|
633
|
+
this.assignedAgents = wasmResult.assigned_agents || [];
|
|
634
|
+
this.result = null;
|
|
635
|
+
this.swarm = swarm;
|
|
636
|
+
this.startTime = null;
|
|
637
|
+
this.endTime = null;
|
|
638
|
+
this.progress = 0;
|
|
639
|
+
|
|
640
|
+
// Start task execution if agents are assigned
|
|
641
|
+
if (this.assignedAgents.length > 0 && this.status === 'orchestrated') {
|
|
642
|
+
this.executeTask();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
async executeTask() {
|
|
647
|
+
this.status = 'in_progress';
|
|
648
|
+
this.startTime = Date.now();
|
|
649
|
+
this.progress = 0.1;
|
|
650
|
+
|
|
651
|
+
console.log(`🏃 Executing task: ${this.description} with ${this.assignedAgents.length} agents`);
|
|
652
|
+
|
|
653
|
+
try {
|
|
654
|
+
// Execute task with all assigned agents
|
|
655
|
+
const agentResults = [];
|
|
656
|
+
|
|
657
|
+
for (const agentId of this.assignedAgents) {
|
|
658
|
+
const agent = this.swarm.agents.get(agentId);
|
|
659
|
+
if (agent) {
|
|
660
|
+
const agentResult = await agent.execute(this);
|
|
661
|
+
agentResults.push({
|
|
662
|
+
agentId,
|
|
663
|
+
agentType: agent.type,
|
|
664
|
+
result: agentResult,
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
this.progress = Math.min(0.9, this.progress + (0.8 / this.assignedAgents.length));
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// Aggregate results
|
|
671
|
+
this.result = {
|
|
672
|
+
task_id: this.id,
|
|
673
|
+
description: this.description,
|
|
674
|
+
agent_results: agentResults,
|
|
675
|
+
execution_summary: {
|
|
676
|
+
total_agents: this.assignedAgents.length,
|
|
677
|
+
successful_executions: agentResults.filter(r => r.result.status === 'completed').length,
|
|
678
|
+
execution_time_ms: Date.now() - this.startTime,
|
|
679
|
+
average_agent_time_ms: agentResults.reduce((sum, r) => sum + (r.result.executionTime || 0), 0) / agentResults.length,
|
|
680
|
+
},
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
this.status = 'completed';
|
|
684
|
+
this.progress = 1.0;
|
|
685
|
+
this.endTime = Date.now();
|
|
686
|
+
|
|
687
|
+
// Mark agents as idle again
|
|
688
|
+
for (const agentId of this.assignedAgents) {
|
|
689
|
+
const agent = this.swarm.agents.get(agentId);
|
|
690
|
+
if (agent) {
|
|
691
|
+
await agent.updateStatus('idle');
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
console.log(`✅ Task completed: ${this.description} (${this.endTime - this.startTime}ms)`);
|
|
696
|
+
|
|
697
|
+
} catch (error) {
|
|
698
|
+
this.status = 'failed';
|
|
699
|
+
this.result = {
|
|
700
|
+
error: error.message,
|
|
701
|
+
execution_time_ms: Date.now() - this.startTime,
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
// Mark agents as idle on failure too
|
|
705
|
+
for (const agentId of this.assignedAgents) {
|
|
706
|
+
const agent = this.swarm.agents.get(agentId);
|
|
707
|
+
if (agent) {
|
|
708
|
+
await agent.updateStatus('idle');
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
console.error(`❌ Task failed: ${this.description} - ${error.message}`);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
async getStatus() {
|
|
717
|
+
return {
|
|
718
|
+
id: this.id,
|
|
719
|
+
status: this.status,
|
|
720
|
+
assignedAgents: this.assignedAgents,
|
|
721
|
+
progress: this.progress,
|
|
722
|
+
execution_time_ms: this.startTime ? (this.endTime || Date.now()) - this.startTime : 0,
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
async getResults() {
|
|
727
|
+
return this.result;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// Import DAA service for comprehensive agent management
|
|
732
|
+
import { DAAService, daaService } from './daa-service.js';
|
|
733
|
+
|
|
734
|
+
export { RuvSwarm, Swarm, Agent, Task, DAAService, daaService };
|