@gotza02/seq-thinking 1.1.5 → 1.1.6
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/mcp-server.js +1 -1
- package/package.json +8 -2
- package/agents_test.log +0 -15
- package/data/agents/1770106504306-dljh9ef.json +0 -68
- package/data/agents/1770106504310-4oarrst.json +0 -58
- package/data/agents/1770106540588-pvitt55.json +0 -68
- package/data/agents/1770106540595-z2ya871.json +0 -58
- package/data/agents/1770106710890-0e2naq1.json +0 -68
- package/data/agents/1770106710893-r076yxx.json +0 -58
- package/data/agents/1770109212161-4ybd0i7.json +0 -68
- package/data/agents/1770109212166-gkhya8h.json +0 -58
- package/data/agents/1770117726716-lrnm415.json +0 -68
- package/data/agents/1770117726719-w6hsf3v.json +0 -58
- package/data/sessions/1770100622009-5afiuyv.json +0 -499
- package/data/sessions/1770106504312-75zk750.json +0 -107
- package/data/sessions/1770106540597-z8e8soo.json +0 -150
- package/data/sessions/1770106710894-0kxgy5x.json +0 -150
- package/data/sessions/1770109212169-zpddeb9.json +0 -150
- package/data/sessions/1770117726720-frcwj99.json +0 -150
- package/real_world_test.log +0 -200
- package/real_world_test_dynamic.log +0 -184
- package/real_world_test_real.log +0 -184
- package/src/__tests__/agents.test.ts +0 -858
- package/src/__tests__/mcp-server.test.ts +0 -380
- package/src/__tests__/sequential-thinking.test.ts +0 -687
- package/src/__tests__/swarm-coordinator.test.ts +0 -903
- package/src/__tests__/types.test.ts +0 -839
- package/src/__tests__/utils.test.ts +0 -322
- package/src/agents/base-agent.ts +0 -288
- package/src/agents/critic-agent.ts +0 -582
- package/src/agents/index.ts +0 -11
- package/src/agents/meta-reasoning-agent.ts +0 -314
- package/src/agents/reasoner-agent.ts +0 -312
- package/src/agents/synthesizer-agent.ts +0 -641
- package/src/index.ts +0 -118
- package/src/mcp-server.ts +0 -391
- package/src/real_world_test.ts +0 -89
- package/src/sequential-thinking.ts +0 -614
- package/src/swarm-coordinator.ts +0 -772
- package/src/types/index.ts +0 -915
- package/src/utils/index.ts +0 -1004
- package/src/utils/llm-adapter.ts +0 -110
- package/src/utils/logger.ts +0 -56
- package/src/utils/persistence.ts +0 -109
- package/test_output.log +0 -0
- package/tsconfig.json +0 -21
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utils Test Suite
|
|
3
|
-
* Tests for utility functions including ID generation, similarity calculations,
|
|
4
|
-
* statistical functions, formatting, and validation
|
|
5
|
-
*/
|
|
6
|
-
import { test, describe } from 'node:test';
|
|
7
|
-
import assert from 'node:assert';
|
|
8
|
-
import {
|
|
9
|
-
generateId,
|
|
10
|
-
clamp,
|
|
11
|
-
calculateSimilarity,
|
|
12
|
-
calculateMean,
|
|
13
|
-
formatDuration,
|
|
14
|
-
} from '../utils/index.js';
|
|
15
|
-
|
|
16
|
-
describe('generateId', () => {
|
|
17
|
-
test('should generate unique IDs', () => {
|
|
18
|
-
const id1 = generateId();
|
|
19
|
-
const id2 = generateId();
|
|
20
|
-
assert.notStrictEqual(id1, id2);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('should generate string IDs', () => {
|
|
24
|
-
const id = generateId();
|
|
25
|
-
assert.strictEqual(typeof id, 'string');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('should generate non-empty IDs', () => {
|
|
29
|
-
const id = generateId();
|
|
30
|
-
assert.ok(id.length > 0);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('should generate IDs with consistent length', () => {
|
|
34
|
-
const ids = Array.from({ length: 100 }, generateId);
|
|
35
|
-
const lengths = new Set(ids.map(id => id.length));
|
|
36
|
-
assert.strictEqual(lengths.size, 1, 'All IDs should have the same length');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('should generate IDs containing only valid characters', () => {
|
|
40
|
-
const id = generateId();
|
|
41
|
-
assert.ok(/^[a-zA-Z0-9_-]+$/.test(id), 'ID should contain only alphanumeric, underscore, and hyphen characters');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('should generate many unique IDs without collision', () => {
|
|
45
|
-
const ids = new Set(Array.from({ length: 1000 }, generateId));
|
|
46
|
-
assert.strictEqual(ids.size, 1000, 'All 1000 IDs should be unique');
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('clamp', () => {
|
|
51
|
-
test('should return value when within range', () => {
|
|
52
|
-
assert.strictEqual(clamp(5, 0, 10), 5);
|
|
53
|
-
assert.strictEqual(clamp(0, 0, 10), 0);
|
|
54
|
-
assert.strictEqual(clamp(10, 0, 10), 10);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('should clamp to min when below range', () => {
|
|
58
|
-
assert.strictEqual(clamp(-5, 0, 10), 0);
|
|
59
|
-
assert.strictEqual(clamp(-100, 0, 10), 0);
|
|
60
|
-
assert.strictEqual(clamp(-0.1, 0, 10), 0);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('should clamp to max when above range', () => {
|
|
64
|
-
assert.strictEqual(clamp(15, 0, 10), 10);
|
|
65
|
-
assert.strictEqual(clamp(100, 0, 10), 10);
|
|
66
|
-
assert.strictEqual(clamp(10.1, 0, 10), 10);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('should handle negative ranges', () => {
|
|
70
|
-
assert.strictEqual(clamp(-5, -10, -1), -5);
|
|
71
|
-
assert.strictEqual(clamp(-15, -10, -1), -10);
|
|
72
|
-
assert.strictEqual(clamp(0, -10, -1), -1);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('should handle decimal ranges', () => {
|
|
76
|
-
assert.strictEqual(clamp(0.5, 0, 1), 0.5);
|
|
77
|
-
assert.strictEqual(clamp(-0.5, 0, 1), 0);
|
|
78
|
-
assert.strictEqual(clamp(1.5, 0, 1), 1);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('should handle zero range (min equals max)', () => {
|
|
82
|
-
assert.strictEqual(clamp(5, 5, 5), 5);
|
|
83
|
-
assert.strictEqual(clamp(0, 5, 5), 5);
|
|
84
|
-
assert.strictEqual(clamp(10, 5, 5), 5);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('should handle very large numbers', () => {
|
|
88
|
-
assert.strictEqual(clamp(1e20, 0, 1e18), 1e18);
|
|
89
|
-
assert.strictEqual(clamp(-1e20, -1e18, 0), -1e18);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('should handle very small numbers', () => {
|
|
93
|
-
assert.strictEqual(clamp(1e-20, 1e-18, 1), 1e-18);
|
|
94
|
-
assert.strictEqual(clamp(-1e-20, -1, -1e-18), -1e-18);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe('calculateSimilarity', () => {
|
|
99
|
-
test('should return 1 for identical strings', () => {
|
|
100
|
-
assert.strictEqual(calculateSimilarity('hello', 'hello'), 1);
|
|
101
|
-
assert.strictEqual(calculateSimilarity('', ''), 1);
|
|
102
|
-
assert.strictEqual(calculateSimilarity('The quick brown fox', 'The quick brown fox'), 1);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('should return 0 for completely different strings', () => {
|
|
106
|
-
assert.strictEqual(calculateSimilarity('abc', 'xyz'), 0);
|
|
107
|
-
assert.strictEqual(calculateSimilarity('abc', 'def'), 0);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('should return value between 0 and 1 for similar strings', () => {
|
|
111
|
-
const similarity = calculateSimilarity('hello', 'hallo');
|
|
112
|
-
assert.ok(similarity > 0 && similarity < 1);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test('should be case sensitive', () => {
|
|
116
|
-
const similarity = calculateSimilarity('Hello', 'hello');
|
|
117
|
-
assert.ok(similarity < 1, 'Case-sensitive comparison should not return 1');
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test('should handle empty strings', () => {
|
|
121
|
-
assert.strictEqual(calculateSimilarity('', ''), 1);
|
|
122
|
-
assert.strictEqual(calculateSimilarity('', 'hello'), 0);
|
|
123
|
-
assert.strictEqual(calculateSimilarity('hello', ''), 0);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('should handle whitespace differences', () => {
|
|
127
|
-
const similarity1 = calculateSimilarity('hello world', 'hello world');
|
|
128
|
-
const similarity2 = calculateSimilarity('hello world', 'helloworld');
|
|
129
|
-
assert.ok(similarity1 > similarity2, 'Extra space should be more similar than no space');
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
test('should handle substring relationships', () => {
|
|
133
|
-
const similarity = calculateSimilarity('hello world', 'hello');
|
|
134
|
-
assert.ok(similarity > 0 && similarity < 1);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('should be symmetric', () => {
|
|
138
|
-
const sim1 = calculateSimilarity('kitten', 'sitting');
|
|
139
|
-
const sim2 = calculateSimilarity('sitting', 'kitten');
|
|
140
|
-
assert.strictEqual(sim1, sim2);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test('should handle special characters', () => {
|
|
144
|
-
assert.ok(calculateSimilarity('hello!', 'hello') > 0);
|
|
145
|
-
assert.ok(calculateSimilarity('hello@#$', 'hello') > 0);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test('should handle unicode characters', () => {
|
|
149
|
-
assert.strictEqual(calculateSimilarity('héllo', 'héllo'), 1);
|
|
150
|
-
assert.ok(calculateSimilarity('héllo', 'hello') >= 0);
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test('should handle very long strings', () => {
|
|
154
|
-
const long1 = 'a'.repeat(10000);
|
|
155
|
-
const long2 = 'a'.repeat(9999) + 'b';
|
|
156
|
-
const similarity = calculateSimilarity(long1, long2);
|
|
157
|
-
assert.ok(similarity > 0.99, 'Very similar long strings should have high similarity');
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
test('should handle common typos', () => {
|
|
161
|
-
const similarity = calculateSimilarity('accommodation', 'accomodation');
|
|
162
|
-
assert.ok(similarity > 0.9, 'Common typos should have high similarity');
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
describe('calculateMean', () => {
|
|
167
|
-
test('should calculate mean of positive numbers', () => {
|
|
168
|
-
assert.strictEqual(calculateMean([1, 2, 3, 4, 5]), 3);
|
|
169
|
-
assert.strictEqual(calculateMean([10, 20, 30]), 20);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
test('should calculate mean of negative numbers', () => {
|
|
173
|
-
assert.strictEqual(calculateMean([-1, -2, -3, -4, -5]), -3);
|
|
174
|
-
assert.strictEqual(calculateMean([-10, -20, -30]), -20);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test('should calculate mean of mixed numbers', () => {
|
|
178
|
-
assert.strictEqual(calculateMean([-5, 0, 5]), 0);
|
|
179
|
-
assert.strictEqual(calculateMean([-10, 10]), 0);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
test('should handle single element array', () => {
|
|
183
|
-
assert.strictEqual(calculateMean([42]), 42);
|
|
184
|
-
assert.strictEqual(calculateMean([0]), 0);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
test('should handle decimal numbers', () => {
|
|
188
|
-
assert.strictEqual(calculateMean([1.5, 2.5, 3.5]), 2.5);
|
|
189
|
-
assert.ok(Math.abs(calculateMean([0.1, 0.2, 0.3]) - 0.2) < 0.0001);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test('should handle empty array', () => {
|
|
193
|
-
assert.ok(Number.isNaN(calculateMean([])));
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
test('should handle array with zeros', () => {
|
|
197
|
-
assert.strictEqual(calculateMean([0, 0, 0]), 0);
|
|
198
|
-
assert.strictEqual(calculateMean([0, 10, 0]), 10 / 3);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
test('should handle very large numbers', () => {
|
|
202
|
-
const result = calculateMean([1e10, 2e10, 3e10]);
|
|
203
|
-
assert.strictEqual(result, 2e10);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
test('should handle very small numbers', () => {
|
|
207
|
-
const result = calculateMean([1e-10, 2e-10, 3e-10]);
|
|
208
|
-
assert.strictEqual(result, 2e-10);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
test('should handle large arrays', () => {
|
|
212
|
-
const largeArray = Array.from({ length: 10000 }, (_, i) => i + 1);
|
|
213
|
-
const result = calculateMean(largeArray);
|
|
214
|
-
assert.strictEqual(result, 5000.5);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
test('should be precise with floating point', () => {
|
|
218
|
-
// Test for floating point precision issues
|
|
219
|
-
const result = calculateMean([0.1, 0.2]);
|
|
220
|
-
assert.ok(Math.abs(result - 0.15) < 0.0001);
|
|
221
|
-
});
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
describe('formatDuration', () => {
|
|
225
|
-
test('should format milliseconds', () => {
|
|
226
|
-
assert.strictEqual(formatDuration(500), '500ms');
|
|
227
|
-
assert.strictEqual(formatDuration(0), '0ms');
|
|
228
|
-
assert.strictEqual(formatDuration(999), '999ms');
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
test('should format seconds', () => {
|
|
232
|
-
assert.strictEqual(formatDuration(1000), '1s');
|
|
233
|
-
assert.strictEqual(formatDuration(5000), '5s');
|
|
234
|
-
assert.strictEqual(formatDuration(59000), '59s');
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
test('should format minutes', () => {
|
|
238
|
-
assert.strictEqual(formatDuration(60000), '1m');
|
|
239
|
-
assert.strictEqual(formatDuration(120000), '2m');
|
|
240
|
-
assert.strictEqual(formatDuration(3540000), '59m');
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
test('should format hours', () => {
|
|
244
|
-
assert.strictEqual(formatDuration(3600000), '1h');
|
|
245
|
-
assert.strictEqual(formatDuration(7200000), '2h');
|
|
246
|
-
assert.strictEqual(formatDuration(86400000), '24h');
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
test('should format days', () => {
|
|
250
|
-
assert.strictEqual(formatDuration(86400000), '24h');
|
|
251
|
-
assert.strictEqual(formatDuration(172800000), '48h');
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
test('should handle negative durations', () => {
|
|
255
|
-
assert.strictEqual(formatDuration(-500), '-500ms');
|
|
256
|
-
assert.strictEqual(formatDuration(-1000), '-1s');
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
test('should handle decimal milliseconds', () => {
|
|
260
|
-
assert.strictEqual(formatDuration(500.5), '500ms');
|
|
261
|
-
assert.strictEqual(formatDuration(1000.9), '1s');
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
test('should handle very large durations', () => {
|
|
265
|
-
assert.strictEqual(formatDuration(31536000000), '8760h'); // 1 year in hours
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
test('should handle boundary values', () => {
|
|
269
|
-
assert.strictEqual(formatDuration(59999), '59s');
|
|
270
|
-
assert.strictEqual(formatDuration(60000), '1m');
|
|
271
|
-
assert.strictEqual(formatDuration(59999.9), '59s');
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
describe('Utility Functions Integration', () => {
|
|
276
|
-
test('clamp and calculateMean work together', () => {
|
|
277
|
-
const values = [5, 15, 25, 35, 45];
|
|
278
|
-
const mean = calculateMean(values);
|
|
279
|
-
const clampedMean = clamp(mean, 10, 30);
|
|
280
|
-
assert.ok(clampedMean >= 10 && clampedMean <= 30);
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
test('generateId and calculateSimilarity work together', () => {
|
|
284
|
-
const id1 = generateId();
|
|
285
|
-
const id2 = generateId();
|
|
286
|
-
const similarity = calculateSimilarity(id1, id2);
|
|
287
|
-
assert.ok(similarity >= 0 && similarity <= 1);
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
test('formatDuration handles processing time from TaskResult', () => {
|
|
291
|
-
const processingTimes = [100, 500, 1000, 5000, 60000];
|
|
292
|
-
processingTimes.forEach(time => {
|
|
293
|
-
const formatted = formatDuration(time);
|
|
294
|
-
assert.ok(formatted.length > 0);
|
|
295
|
-
assert.ok(/\d+(ms|s|m|h)/.test(formatted));
|
|
296
|
-
});
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
describe('Edge Cases and Error Conditions', () => {
|
|
301
|
-
test('clamp with Infinity values', () => {
|
|
302
|
-
assert.strictEqual(clamp(5, -Infinity, Infinity), 5);
|
|
303
|
-
assert.strictEqual(clamp(Infinity, 0, 10), 10);
|
|
304
|
-
assert.strictEqual(clamp(-Infinity, 0, 10), 0);
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
test('calculateMean with Infinity values', () => {
|
|
308
|
-
assert.strictEqual(calculateMean([1, 2, Infinity]), Infinity);
|
|
309
|
-
assert.strictEqual(calculateMean([-Infinity, 0, 1]), -Infinity);
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
test('calculateSimilarity with null/undefined should handle gracefully', () => {
|
|
313
|
-
// These should not throw errors
|
|
314
|
-
assert.doesNotThrow(() => calculateSimilarity('', ''));
|
|
315
|
-
assert.doesNotThrow(() => calculateSimilarity('test', 'test'));
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
test('formatDuration with very small values', () => {
|
|
319
|
-
assert.strictEqual(formatDuration(0.1), '0ms');
|
|
320
|
-
assert.strictEqual(formatDuration(0.9), '0ms');
|
|
321
|
-
});
|
|
322
|
-
});
|
package/src/agents/base-agent.ts
DELETED
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base Agent Class
|
|
3
|
-
* @module agents/base-agent
|
|
4
|
-
* @version 1.0.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
AgentType,
|
|
9
|
-
AgentStatus,
|
|
10
|
-
type AgentConfig,
|
|
11
|
-
type AgentCapability,
|
|
12
|
-
type Task,
|
|
13
|
-
type TaskResult,
|
|
14
|
-
type SwarmMessage
|
|
15
|
-
} from '../types/index.js';
|
|
16
|
-
import { Logger } from '../utils/logger.js';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Abstract base class for all agents
|
|
20
|
-
*/
|
|
21
|
-
export abstract class BaseAgent {
|
|
22
|
-
/** Agent ID */
|
|
23
|
-
readonly id: string;
|
|
24
|
-
|
|
25
|
-
/** Agent configuration */
|
|
26
|
-
config: AgentConfig;
|
|
27
|
-
|
|
28
|
-
/** Current status */
|
|
29
|
-
protected status: AgentStatus = AgentStatus.IDLE;
|
|
30
|
-
|
|
31
|
-
/** Current task IDs */
|
|
32
|
-
protected currentTasks: Set<string> = new Set();
|
|
33
|
-
|
|
34
|
-
/** Performance history */
|
|
35
|
-
protected performanceHistory: Array<{
|
|
36
|
-
taskId: string;
|
|
37
|
-
quality: number;
|
|
38
|
-
timeMs: number;
|
|
39
|
-
timestamp: Date;
|
|
40
|
-
}> = [];
|
|
41
|
-
|
|
42
|
-
/** Message handlers */
|
|
43
|
-
protected messageHandlers: Array<(message: SwarmMessage) => void | Promise<void>> = [];
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Create a new base agent
|
|
47
|
-
* @param config - Agent configuration
|
|
48
|
-
*/
|
|
49
|
-
constructor(config: Partial<AgentConfig> & { name: string }) {
|
|
50
|
-
this.id = config.id || `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
51
|
-
this.config = {
|
|
52
|
-
id: this.id,
|
|
53
|
-
name: config.name,
|
|
54
|
-
type: config.type || AgentType.UTILITY,
|
|
55
|
-
subtype: config.subtype,
|
|
56
|
-
capabilities: config.capabilities || [],
|
|
57
|
-
maxConcurrentTasks: config.maxConcurrentTasks || 3,
|
|
58
|
-
confidenceThreshold: config.confidenceThreshold || 0.7,
|
|
59
|
-
metadata: {
|
|
60
|
-
createdAt: new Date(),
|
|
61
|
-
version: '1.0.0',
|
|
62
|
-
config: config.metadata?.config || {}
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Execute a task
|
|
69
|
-
* @param task - Task to execute
|
|
70
|
-
* @returns Task result
|
|
71
|
-
*/
|
|
72
|
-
async execute(task: Task): Promise<TaskResult> {
|
|
73
|
-
const startTime = Date.now();
|
|
74
|
-
this.status = AgentStatus.BUSY;
|
|
75
|
-
this.currentTasks.add(task.id);
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
const result = await this.process(task);
|
|
79
|
-
const processingTime = Date.now() - startTime;
|
|
80
|
-
|
|
81
|
-
this.performanceHistory.push({
|
|
82
|
-
taskId: task.id,
|
|
83
|
-
quality: result.confidence,
|
|
84
|
-
timeMs: processingTime,
|
|
85
|
-
timestamp: new Date()
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
this.currentTasks.delete(task.id);
|
|
89
|
-
this.status = this.currentTasks.size === 0 ? AgentStatus.IDLE : AgentStatus.BUSY;
|
|
90
|
-
|
|
91
|
-
return result;
|
|
92
|
-
} catch (error) {
|
|
93
|
-
this.currentTasks.delete(task.id);
|
|
94
|
-
this.status = this.currentTasks.size === 0 ? AgentStatus.IDLE : AgentStatus.BUSY;
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
taskId: task.id,
|
|
98
|
-
agentId: this.id,
|
|
99
|
-
success: false,
|
|
100
|
-
output: null,
|
|
101
|
-
confidence: 0,
|
|
102
|
-
processingTimeMs: Date.now() - startTime,
|
|
103
|
-
metadata: {
|
|
104
|
-
tokensUsed: 0,
|
|
105
|
-
reasoningSteps: 0,
|
|
106
|
-
intermediateResults: [],
|
|
107
|
-
error: error instanceof Error ? error.message : String(error)
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Process a task (implemented by subclasses)
|
|
115
|
-
* @param task - Task to process
|
|
116
|
-
* @returns Task result
|
|
117
|
-
*/
|
|
118
|
-
abstract process(task: Task): Promise<TaskResult>;
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Register a message handler
|
|
122
|
-
* @param handler - Message handler function
|
|
123
|
-
* @returns Unsubscribe function
|
|
124
|
-
*/
|
|
125
|
-
onMessage(handler: (message: SwarmMessage) => void | Promise<void>): () => void {
|
|
126
|
-
this.messageHandlers.push(handler);
|
|
127
|
-
|
|
128
|
-
return () => {
|
|
129
|
-
const index = this.messageHandlers.indexOf(handler);
|
|
130
|
-
if (index > -1) {
|
|
131
|
-
this.messageHandlers.splice(index, 1);
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Receive a message
|
|
138
|
-
* @param message - Message to receive
|
|
139
|
-
*/
|
|
140
|
-
receiveMessage(message: SwarmMessage): void {
|
|
141
|
-
for (const handler of this.messageHandlers) {
|
|
142
|
-
try {
|
|
143
|
-
const result = handler(message);
|
|
144
|
-
if (result instanceof Promise) {
|
|
145
|
-
result.catch(error => {
|
|
146
|
-
Logger.error(`Error in message handler for agent ${this.id}`, { error });
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
} catch (error) {
|
|
150
|
-
Logger.error(`Error in message handler for agent ${this.id}`, { error });
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Get agent status
|
|
157
|
-
* @returns Current status
|
|
158
|
-
*/
|
|
159
|
-
getStatus(): AgentStatus {
|
|
160
|
-
return this.status;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Set agent status
|
|
165
|
-
* @param status - New status
|
|
166
|
-
*/
|
|
167
|
-
setStatus(status: AgentStatus): void {
|
|
168
|
-
this.status = status;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Get current task IDs
|
|
173
|
-
* @returns Array of task IDs
|
|
174
|
-
*/
|
|
175
|
-
getCurrentTasks(): string[] {
|
|
176
|
-
return Array.from(this.currentTasks);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Get performance metrics
|
|
181
|
-
* @returns Performance metrics
|
|
182
|
-
*/
|
|
183
|
-
getPerformanceMetrics(): {
|
|
184
|
-
tasksCompleted: number;
|
|
185
|
-
averageQuality: number;
|
|
186
|
-
averageTimeMs: number;
|
|
187
|
-
successRate: number;
|
|
188
|
-
} {
|
|
189
|
-
if (this.performanceHistory.length === 0) {
|
|
190
|
-
return {
|
|
191
|
-
tasksCompleted: 0,
|
|
192
|
-
averageQuality: 0,
|
|
193
|
-
averageTimeMs: 0,
|
|
194
|
-
successRate: 0
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const completed = this.performanceHistory.length;
|
|
199
|
-
const avgQuality = this.performanceHistory.reduce((sum, p) => sum + p.quality, 0) / completed;
|
|
200
|
-
const avgTime = this.performanceHistory.reduce((sum, p) => sum + p.timeMs, 0) / completed;
|
|
201
|
-
const successRate = this.performanceHistory.filter(p => p.quality > 0.5).length / completed;
|
|
202
|
-
|
|
203
|
-
return {
|
|
204
|
-
tasksCompleted: completed,
|
|
205
|
-
averageQuality: avgQuality,
|
|
206
|
-
averageTimeMs: avgTime,
|
|
207
|
-
successRate
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Check if agent has a capability
|
|
213
|
-
* @param capabilityName - Capability name
|
|
214
|
-
* @returns True if agent has capability
|
|
215
|
-
*/
|
|
216
|
-
hasCapability(capabilityName: string): boolean {
|
|
217
|
-
return this.getCapabilities().some(c => c.name === capabilityName);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Get capability confidence
|
|
222
|
-
* @param capabilityName - Capability name
|
|
223
|
-
* @returns Confidence level (0-1)
|
|
224
|
-
*/
|
|
225
|
-
getCapabilityConfidence(capabilityName: string): number {
|
|
226
|
-
const cap = this.getCapabilities().find(c => c.name === capabilityName);
|
|
227
|
-
return cap?.confidence || 0;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Check if agent can handle a task
|
|
232
|
-
* @param task - Task to check
|
|
233
|
-
* @returns True if agent can handle task
|
|
234
|
-
*/
|
|
235
|
-
canHandleTask(task: Task): boolean {
|
|
236
|
-
const requiredCaps = task.requirements.requiredCapabilities;
|
|
237
|
-
return requiredCaps.every(cap => this.hasCapability(cap));
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Create a task result
|
|
242
|
-
* @param taskId - Task ID
|
|
243
|
-
* @param output - Output data
|
|
244
|
-
* @param confidence - Confidence level
|
|
245
|
-
* @param processingTimeMs - Processing time in ms
|
|
246
|
-
* @param metadata - Additional metadata
|
|
247
|
-
* @returns Task result
|
|
248
|
-
*/
|
|
249
|
-
protected createTaskResult(
|
|
250
|
-
taskId: string,
|
|
251
|
-
output: unknown,
|
|
252
|
-
confidence: number,
|
|
253
|
-
processingTimeMs: number,
|
|
254
|
-
metadata: {
|
|
255
|
-
tokensUsed?: number;
|
|
256
|
-
reasoningSteps?: number;
|
|
257
|
-
intermediateResults?: unknown[];
|
|
258
|
-
error?: string;
|
|
259
|
-
} = {}
|
|
260
|
-
): TaskResult {
|
|
261
|
-
return {
|
|
262
|
-
taskId,
|
|
263
|
-
agentId: this.id,
|
|
264
|
-
success: confidence > 0.5,
|
|
265
|
-
output,
|
|
266
|
-
confidence,
|
|
267
|
-
processingTimeMs,
|
|
268
|
-
metadata: {
|
|
269
|
-
tokensUsed: metadata.tokensUsed || 0,
|
|
270
|
-
reasoningSteps: metadata.reasoningSteps || 1,
|
|
271
|
-
intermediateResults: metadata.intermediateResults || [],
|
|
272
|
-
error: metadata.error
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Get agent type
|
|
279
|
-
* @returns Agent type
|
|
280
|
-
*/
|
|
281
|
-
abstract getType(): string;
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Get agent capabilities
|
|
285
|
-
* @returns Array of capabilities
|
|
286
|
-
*/
|
|
287
|
-
abstract getCapabilities(): AgentCapability[];
|
|
288
|
-
}
|