@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,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite Worker Thread for CPU-intensive database operations
|
|
3
|
+
*
|
|
4
|
+
* This worker handles long-running queries and CPU-intensive operations
|
|
5
|
+
* to prevent blocking the main thread during heavy database workloads.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { parentPort, workerData } from 'worker_threads';
|
|
9
|
+
import Database from 'better-sqlite3';
|
|
10
|
+
|
|
11
|
+
class SQLiteWorker {
|
|
12
|
+
constructor(dbPath, options = {}) {
|
|
13
|
+
this.dbPath = dbPath;
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.db = null;
|
|
16
|
+
this.preparedStatements = new Map();
|
|
17
|
+
|
|
18
|
+
this.initialize();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
initialize() {
|
|
22
|
+
try {
|
|
23
|
+
this.db = new Database(this.dbPath, { readonly: true });
|
|
24
|
+
this.configureConnection();
|
|
25
|
+
|
|
26
|
+
// Test connection
|
|
27
|
+
this.db.prepare('SELECT 1').get();
|
|
28
|
+
|
|
29
|
+
// Setup message handling
|
|
30
|
+
if (parentPort) {
|
|
31
|
+
parentPort.on('message', (message) => {
|
|
32
|
+
this.handleMessage(message);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (parentPort) {
|
|
38
|
+
parentPort.postMessage({
|
|
39
|
+
error: `Worker initialization failed: ${error.message}`
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
configureConnection() {
|
|
46
|
+
// Configure for optimal read performance
|
|
47
|
+
this.db.pragma('journal_mode = WAL');
|
|
48
|
+
this.db.pragma('synchronous = NORMAL');
|
|
49
|
+
this.db.pragma('temp_store = MEMORY');
|
|
50
|
+
this.db.pragma('mmap_size = ' + (this.options.mmapSize || 268435456));
|
|
51
|
+
this.db.pragma('cache_size = ' + (this.options.cacheSize || -64000));
|
|
52
|
+
this.db.pragma('busy_timeout = 5000');
|
|
53
|
+
|
|
54
|
+
// Optimize for queries
|
|
55
|
+
this.db.pragma('optimize');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
handleMessage(message) {
|
|
59
|
+
try {
|
|
60
|
+
if (message.health) {
|
|
61
|
+
// Health check
|
|
62
|
+
this.db.prepare('SELECT 1').get();
|
|
63
|
+
parentPort.postMessage({ health: 'ok' });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (message.sql && message.params !== undefined) {
|
|
68
|
+
// Execute query
|
|
69
|
+
const result = this.executeQuery(message.sql, message.params);
|
|
70
|
+
parentPort.postMessage({
|
|
71
|
+
data: result,
|
|
72
|
+
error: null
|
|
73
|
+
});
|
|
74
|
+
} else {
|
|
75
|
+
parentPort.postMessage({
|
|
76
|
+
error: 'Invalid message format'
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
} catch (error) {
|
|
81
|
+
parentPort.postMessage({
|
|
82
|
+
error: error.message,
|
|
83
|
+
data: null
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
executeQuery(sql, params) {
|
|
89
|
+
const stmt = this.getPreparedStatement(sql);
|
|
90
|
+
|
|
91
|
+
// Determine operation type
|
|
92
|
+
const operation = sql.trim().toUpperCase().split(' ')[0];
|
|
93
|
+
|
|
94
|
+
switch (operation) {
|
|
95
|
+
case 'SELECT':
|
|
96
|
+
return stmt.all(params);
|
|
97
|
+
case 'INSERT':
|
|
98
|
+
case 'UPDATE':
|
|
99
|
+
case 'DELETE':
|
|
100
|
+
return stmt.run(params);
|
|
101
|
+
default:
|
|
102
|
+
// For other operations, try to execute and return result
|
|
103
|
+
try {
|
|
104
|
+
return stmt.all(params);
|
|
105
|
+
} catch {
|
|
106
|
+
return stmt.run(params);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
getPreparedStatement(sql) {
|
|
112
|
+
if (!this.preparedStatements.has(sql)) {
|
|
113
|
+
this.preparedStatements.set(sql, this.db.prepare(sql));
|
|
114
|
+
}
|
|
115
|
+
return this.preparedStatements.get(sql);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
cleanup() {
|
|
119
|
+
if (this.db) {
|
|
120
|
+
this.db.close();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Initialize worker
|
|
126
|
+
const worker = new SQLiteWorker(workerData.dbPath, workerData.options);
|
|
127
|
+
|
|
128
|
+
// Cleanup on exit
|
|
129
|
+
process.on('exit', () => {
|
|
130
|
+
worker.cleanup();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
process.on('SIGINT', () => {
|
|
134
|
+
worker.cleanup();
|
|
135
|
+
process.exit(0);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
process.on('SIGTERM', () => {
|
|
139
|
+
worker.cleanup();
|
|
140
|
+
process.exit(0);
|
|
141
|
+
});
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types and interfaces for RuvSwarm
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface SwarmOptions {
|
|
6
|
+
topology?: SwarmTopology;
|
|
7
|
+
maxAgents?: number;
|
|
8
|
+
connectionDensity?: number;
|
|
9
|
+
syncInterval?: number;
|
|
10
|
+
wasmPath?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type SwarmTopology = 'mesh' | 'hierarchical' | 'distributed' | 'centralized' | 'hybrid';
|
|
14
|
+
|
|
15
|
+
export interface AgentConfig {
|
|
16
|
+
id: string;
|
|
17
|
+
type: AgentType;
|
|
18
|
+
cognitiveProfile?: CognitiveProfile;
|
|
19
|
+
capabilities?: string[];
|
|
20
|
+
memory?: AgentMemory;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type AgentType =
|
|
24
|
+
| 'researcher'
|
|
25
|
+
| 'coder'
|
|
26
|
+
| 'analyst'
|
|
27
|
+
| 'architect'
|
|
28
|
+
| 'reviewer'
|
|
29
|
+
| 'debugger'
|
|
30
|
+
| 'tester'
|
|
31
|
+
| 'documenter'
|
|
32
|
+
| 'optimizer'
|
|
33
|
+
| 'custom';
|
|
34
|
+
|
|
35
|
+
export interface CognitiveProfile {
|
|
36
|
+
analytical: number;
|
|
37
|
+
creative: number;
|
|
38
|
+
systematic: number;
|
|
39
|
+
intuitive: number;
|
|
40
|
+
collaborative: number;
|
|
41
|
+
independent: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface AgentMemory {
|
|
45
|
+
shortTerm: Map<string, any>;
|
|
46
|
+
longTerm: Map<string, any>;
|
|
47
|
+
episodic: EpisodicMemory[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface EpisodicMemory {
|
|
51
|
+
timestamp: number;
|
|
52
|
+
context: string;
|
|
53
|
+
data: any;
|
|
54
|
+
importance: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface Task {
|
|
58
|
+
id: string;
|
|
59
|
+
description: string;
|
|
60
|
+
priority: TaskPriority;
|
|
61
|
+
dependencies?: string[];
|
|
62
|
+
assignedAgents?: string[];
|
|
63
|
+
status: TaskStatus;
|
|
64
|
+
result?: any;
|
|
65
|
+
error?: Error;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type TaskPriority = 'low' | 'medium' | 'high' | 'critical';
|
|
69
|
+
export type TaskStatus = 'pending' | 'assigned' | 'in_progress' | 'completed' | 'failed';
|
|
70
|
+
|
|
71
|
+
export interface SwarmState {
|
|
72
|
+
agents: Map<string, Agent>;
|
|
73
|
+
tasks: Map<string, Task>;
|
|
74
|
+
topology: SwarmTopology;
|
|
75
|
+
connections: Connection[];
|
|
76
|
+
metrics: SwarmMetrics;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface Connection {
|
|
80
|
+
from: string;
|
|
81
|
+
to: string;
|
|
82
|
+
weight: number;
|
|
83
|
+
type: ConnectionType;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type ConnectionType = 'data' | 'control' | 'feedback' | 'coordination';
|
|
87
|
+
|
|
88
|
+
export interface SwarmMetrics {
|
|
89
|
+
totalTasks: number;
|
|
90
|
+
completedTasks: number;
|
|
91
|
+
failedTasks: number;
|
|
92
|
+
averageCompletionTime: number;
|
|
93
|
+
agentUtilization: Map<string, number>;
|
|
94
|
+
throughput: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface Agent {
|
|
98
|
+
id: string;
|
|
99
|
+
config: AgentConfig;
|
|
100
|
+
state: AgentState;
|
|
101
|
+
connections: string[];
|
|
102
|
+
execute(task: Task): Promise<any>;
|
|
103
|
+
communicate(message: Message): Promise<void>;
|
|
104
|
+
update(state: Partial<AgentState>): void;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface AgentState {
|
|
108
|
+
status: AgentStatus;
|
|
109
|
+
currentTask?: string;
|
|
110
|
+
load: number;
|
|
111
|
+
performance: AgentPerformance;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type AgentStatus = 'idle' | 'busy' | 'error' | 'offline';
|
|
115
|
+
|
|
116
|
+
export interface AgentPerformance {
|
|
117
|
+
tasksCompleted: number;
|
|
118
|
+
tasksFailed: number;
|
|
119
|
+
averageExecutionTime: number;
|
|
120
|
+
successRate: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface Message {
|
|
124
|
+
id: string;
|
|
125
|
+
from: string;
|
|
126
|
+
to: string | string[];
|
|
127
|
+
type: MessageType;
|
|
128
|
+
payload: any;
|
|
129
|
+
timestamp: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type MessageType =
|
|
133
|
+
| 'task_assignment'
|
|
134
|
+
| 'task_result'
|
|
135
|
+
| 'status_update'
|
|
136
|
+
| 'coordination'
|
|
137
|
+
| 'knowledge_share'
|
|
138
|
+
| 'error';
|
|
139
|
+
|
|
140
|
+
export interface SwarmEventEmitter {
|
|
141
|
+
on(event: SwarmEvent, handler: (data: any) => void): void;
|
|
142
|
+
off(event: SwarmEvent, handler: (data: any) => void): void;
|
|
143
|
+
emit(event: SwarmEvent, data: any): void;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type SwarmEvent =
|
|
147
|
+
| 'agent:added'
|
|
148
|
+
| 'agent:removed'
|
|
149
|
+
| 'agent:status_changed'
|
|
150
|
+
| 'task:created'
|
|
151
|
+
| 'task:assigned'
|
|
152
|
+
| 'task:completed'
|
|
153
|
+
| 'task:failed'
|
|
154
|
+
| 'swarm:topology_changed'
|
|
155
|
+
| 'swarm:error';
|
|
156
|
+
|
|
157
|
+
export interface WasmModule {
|
|
158
|
+
init(): Promise<void>;
|
|
159
|
+
createSwarm(options: SwarmOptions): number;
|
|
160
|
+
addAgent(swarmId: number, config: AgentConfig): number;
|
|
161
|
+
assignTask(swarmId: number, task: Task): void;
|
|
162
|
+
getState(swarmId: number): SwarmState;
|
|
163
|
+
destroy(swarmId: number): void;
|
|
164
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for RuvSwarm
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { CognitiveProfile, SwarmTopology, AgentType, TaskPriority } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Generate a unique ID for agents, tasks, and messages
|
|
9
|
+
*/
|
|
10
|
+
export function generateId(prefix: string = ''): string {
|
|
11
|
+
const timestamp = Date.now().toString(36);
|
|
12
|
+
const random = Math.random().toString(36).substring(2, 9);
|
|
13
|
+
return prefix ? `${prefix}_${timestamp}_${random}` : `${timestamp}_${random}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Create a default cognitive profile based on agent type
|
|
18
|
+
*/
|
|
19
|
+
export function getDefaultCognitiveProfile(type: AgentType): CognitiveProfile {
|
|
20
|
+
const profiles: Record<AgentType, CognitiveProfile> = {
|
|
21
|
+
researcher: {
|
|
22
|
+
analytical: 0.9,
|
|
23
|
+
creative: 0.6,
|
|
24
|
+
systematic: 0.8,
|
|
25
|
+
intuitive: 0.5,
|
|
26
|
+
collaborative: 0.7,
|
|
27
|
+
independent: 0.8,
|
|
28
|
+
},
|
|
29
|
+
coder: {
|
|
30
|
+
analytical: 0.8,
|
|
31
|
+
creative: 0.7,
|
|
32
|
+
systematic: 0.9,
|
|
33
|
+
intuitive: 0.4,
|
|
34
|
+
collaborative: 0.6,
|
|
35
|
+
independent: 0.7,
|
|
36
|
+
},
|
|
37
|
+
analyst: {
|
|
38
|
+
analytical: 0.95,
|
|
39
|
+
creative: 0.4,
|
|
40
|
+
systematic: 0.9,
|
|
41
|
+
intuitive: 0.3,
|
|
42
|
+
collaborative: 0.6,
|
|
43
|
+
independent: 0.8,
|
|
44
|
+
},
|
|
45
|
+
architect: {
|
|
46
|
+
analytical: 0.8,
|
|
47
|
+
creative: 0.8,
|
|
48
|
+
systematic: 0.85,
|
|
49
|
+
intuitive: 0.7,
|
|
50
|
+
collaborative: 0.8,
|
|
51
|
+
independent: 0.6,
|
|
52
|
+
},
|
|
53
|
+
reviewer: {
|
|
54
|
+
analytical: 0.85,
|
|
55
|
+
creative: 0.5,
|
|
56
|
+
systematic: 0.9,
|
|
57
|
+
intuitive: 0.4,
|
|
58
|
+
collaborative: 0.7,
|
|
59
|
+
independent: 0.7,
|
|
60
|
+
},
|
|
61
|
+
debugger: {
|
|
62
|
+
analytical: 0.9,
|
|
63
|
+
creative: 0.6,
|
|
64
|
+
systematic: 0.85,
|
|
65
|
+
intuitive: 0.6,
|
|
66
|
+
collaborative: 0.5,
|
|
67
|
+
independent: 0.8,
|
|
68
|
+
},
|
|
69
|
+
tester: {
|
|
70
|
+
analytical: 0.8,
|
|
71
|
+
creative: 0.6,
|
|
72
|
+
systematic: 0.95,
|
|
73
|
+
intuitive: 0.3,
|
|
74
|
+
collaborative: 0.6,
|
|
75
|
+
independent: 0.7,
|
|
76
|
+
},
|
|
77
|
+
documenter: {
|
|
78
|
+
analytical: 0.7,
|
|
79
|
+
creative: 0.7,
|
|
80
|
+
systematic: 0.85,
|
|
81
|
+
intuitive: 0.4,
|
|
82
|
+
collaborative: 0.8,
|
|
83
|
+
independent: 0.6,
|
|
84
|
+
},
|
|
85
|
+
optimizer: {
|
|
86
|
+
analytical: 0.9,
|
|
87
|
+
creative: 0.6,
|
|
88
|
+
systematic: 0.8,
|
|
89
|
+
intuitive: 0.5,
|
|
90
|
+
collaborative: 0.5,
|
|
91
|
+
independent: 0.8,
|
|
92
|
+
},
|
|
93
|
+
custom: {
|
|
94
|
+
analytical: 0.5,
|
|
95
|
+
creative: 0.5,
|
|
96
|
+
systematic: 0.5,
|
|
97
|
+
intuitive: 0.5,
|
|
98
|
+
collaborative: 0.5,
|
|
99
|
+
independent: 0.5,
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return profiles[type];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Calculate cognitive diversity score between two profiles
|
|
108
|
+
*/
|
|
109
|
+
export function calculateCognitiveDiversity(
|
|
110
|
+
profile1: CognitiveProfile,
|
|
111
|
+
profile2: CognitiveProfile,
|
|
112
|
+
): number {
|
|
113
|
+
const dimensions = Object.keys(profile1) as (keyof CognitiveProfile)[];
|
|
114
|
+
let totalDifference = 0;
|
|
115
|
+
|
|
116
|
+
for (const dimension of dimensions) {
|
|
117
|
+
const diff = Math.abs(profile1[dimension] - profile2[dimension]);
|
|
118
|
+
totalDifference += diff;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return totalDifference / dimensions.length;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Determine optimal topology based on swarm characteristics
|
|
126
|
+
*/
|
|
127
|
+
export function recommendTopology(
|
|
128
|
+
agentCount: number,
|
|
129
|
+
taskComplexity: 'low' | 'medium' | 'high',
|
|
130
|
+
coordinationNeeds: 'minimal' | 'moderate' | 'extensive',
|
|
131
|
+
): SwarmTopology {
|
|
132
|
+
if (agentCount <= 5) {
|
|
133
|
+
return 'mesh';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (coordinationNeeds === 'extensive') {
|
|
137
|
+
return 'hierarchical';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (taskComplexity === 'high' && agentCount > 10) {
|
|
141
|
+
return 'hybrid';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (coordinationNeeds === 'minimal') {
|
|
145
|
+
return 'distributed';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return 'centralized';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Convert task priority to numeric value for sorting
|
|
153
|
+
*/
|
|
154
|
+
export function priorityToNumber(priority: TaskPriority): number {
|
|
155
|
+
const priorityMap: Record<TaskPriority, number> = {
|
|
156
|
+
low: 1,
|
|
157
|
+
medium: 2,
|
|
158
|
+
high: 3,
|
|
159
|
+
critical: 4,
|
|
160
|
+
};
|
|
161
|
+
return priorityMap[priority];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Format swarm metrics for display
|
|
166
|
+
*/
|
|
167
|
+
export function formatMetrics(metrics: {
|
|
168
|
+
totalTasks: number;
|
|
169
|
+
completedTasks: number;
|
|
170
|
+
failedTasks: number;
|
|
171
|
+
averageCompletionTime: number;
|
|
172
|
+
throughput: number;
|
|
173
|
+
}): string {
|
|
174
|
+
const successRate = metrics.totalTasks > 0
|
|
175
|
+
? ((metrics.completedTasks / metrics.totalTasks) * 100).toFixed(1)
|
|
176
|
+
: '0.0';
|
|
177
|
+
|
|
178
|
+
return `
|
|
179
|
+
Swarm Metrics:
|
|
180
|
+
- Total Tasks: ${metrics.totalTasks}
|
|
181
|
+
- Completed: ${metrics.completedTasks}
|
|
182
|
+
- Failed: ${metrics.failedTasks}
|
|
183
|
+
- Success Rate: ${successRate}%
|
|
184
|
+
- Avg Completion Time: ${metrics.averageCompletionTime.toFixed(2)}ms
|
|
185
|
+
- Throughput: ${metrics.throughput.toFixed(2)} tasks/sec
|
|
186
|
+
`.trim();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Validate swarm options
|
|
191
|
+
*/
|
|
192
|
+
export function validateSwarmOptions(options: any): string[] {
|
|
193
|
+
const errors: string[] = [];
|
|
194
|
+
|
|
195
|
+
if (options.maxAgents !== undefined) {
|
|
196
|
+
if (typeof options.maxAgents !== 'number' || options.maxAgents < 1) {
|
|
197
|
+
errors.push('maxAgents must be a positive number');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (options.connectionDensity !== undefined) {
|
|
202
|
+
if (
|
|
203
|
+
typeof options.connectionDensity !== 'number' ||
|
|
204
|
+
options.connectionDensity < 0 ||
|
|
205
|
+
options.connectionDensity > 1
|
|
206
|
+
) {
|
|
207
|
+
errors.push('connectionDensity must be a number between 0 and 1');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (options.topology !== undefined) {
|
|
212
|
+
const validTopologies = ['mesh', 'hierarchical', 'distributed', 'centralized', 'hybrid'];
|
|
213
|
+
if (!validTopologies.includes(options.topology)) {
|
|
214
|
+
errors.push(`topology must be one of: ${validTopologies.join(', ')}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return errors;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Deep clone an object
|
|
223
|
+
*/
|
|
224
|
+
export function deepClone<T>(obj: T): T {
|
|
225
|
+
if (obj === null || typeof obj !== 'object') {
|
|
226
|
+
return obj;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (obj instanceof Date) {
|
|
230
|
+
return new Date(obj.getTime()) as any;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (obj instanceof Array) {
|
|
234
|
+
return obj.map(item => deepClone(item)) as any;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (obj instanceof Map) {
|
|
238
|
+
const cloned = new Map();
|
|
239
|
+
obj.forEach((value, key) => {
|
|
240
|
+
cloned.set(key, deepClone(value));
|
|
241
|
+
});
|
|
242
|
+
return cloned as any;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (obj instanceof Set) {
|
|
246
|
+
const cloned = new Set();
|
|
247
|
+
obj.forEach(value => {
|
|
248
|
+
cloned.add(deepClone(value));
|
|
249
|
+
});
|
|
250
|
+
return cloned as any;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const cloned = {} as T;
|
|
254
|
+
for (const key in obj) {
|
|
255
|
+
if (obj.hasOwnProperty(key)) {
|
|
256
|
+
cloned[key] = deepClone(obj[key]);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return cloned;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Retry a function with exponential backoff
|
|
265
|
+
*/
|
|
266
|
+
export async function retryWithBackoff<T>(
|
|
267
|
+
fn: () => Promise<T>,
|
|
268
|
+
maxRetries: number = 3,
|
|
269
|
+
initialDelay: number = 100,
|
|
270
|
+
): Promise<T> {
|
|
271
|
+
let lastError: Error;
|
|
272
|
+
|
|
273
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
274
|
+
try {
|
|
275
|
+
return await fn();
|
|
276
|
+
} catch (error) {
|
|
277
|
+
lastError = error as Error;
|
|
278
|
+
if (i < maxRetries - 1) {
|
|
279
|
+
const delay = initialDelay * Math.pow(2, i);
|
|
280
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
throw lastError!;
|
|
286
|
+
}
|