agent-swarm-kit 1.0.34 → 1.0.36
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/build/index.cjs +75 -55
- package/build/index.mjs +76 -56
- package/package.json +1 -1
- package/types.d.ts +10 -2
package/build/index.cjs
CHANGED
|
@@ -355,6 +355,10 @@ var validateDefault = function (output) { return __awaiter(void 0, void 0, void
|
|
|
355
355
|
});
|
|
356
356
|
}); };
|
|
357
357
|
|
|
358
|
+
var removeXmlTags = function (input) {
|
|
359
|
+
return input.replace(/<[^>]*>.*?<\/[^>]*>/g, '');
|
|
360
|
+
};
|
|
361
|
+
|
|
358
362
|
/**
|
|
359
363
|
* @description `ask for agent function` in `llama3.1:8b` to troubleshoot (need CC_OLLAMA_EMIT_TOOL_PROTOCOL to be turned off)
|
|
360
364
|
*/
|
|
@@ -396,18 +400,29 @@ var CC_AGENT_DISALLOWED_SYMBOLS = [
|
|
|
396
400
|
"{",
|
|
397
401
|
"}",
|
|
398
402
|
];
|
|
403
|
+
var CC_AGENT_HISTORY_FILTER = function (agentName) { return function (message) {
|
|
404
|
+
var isOk = true;
|
|
405
|
+
if (message.role === "tool") {
|
|
406
|
+
isOk = isOk && message.agentName === agentName;
|
|
407
|
+
}
|
|
408
|
+
if (message.tool_calls) {
|
|
409
|
+
isOk = isOk && message.agentName === agentName;
|
|
410
|
+
}
|
|
411
|
+
return isOk;
|
|
412
|
+
}; };
|
|
413
|
+
var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
|
|
399
414
|
var CC_KEEP_MESSAGES = 5;
|
|
400
|
-
var CC_ANSWER_TIMEOUT = 120000;
|
|
401
415
|
var CC_GET_AGENT_HISTORY = function () { return new functoolsKit.PubsubArrayAdapter(); };
|
|
402
416
|
var GLOBAL_CONFIG = {
|
|
403
417
|
CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
|
|
404
418
|
CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
|
|
405
419
|
CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
|
|
406
|
-
CC_ANSWER_TIMEOUT: CC_ANSWER_TIMEOUT,
|
|
407
420
|
CC_GET_AGENT_HISTORY: CC_GET_AGENT_HISTORY,
|
|
408
421
|
CC_SWARM_AGENT_CHANGED: CC_SWARM_AGENT_CHANGED,
|
|
409
422
|
CC_SWARM_DEFAULT_AGENT: CC_SWARM_DEFAULT_AGENT,
|
|
410
423
|
CC_AGENT_DEFAULT_VALIDATION: CC_AGENT_DEFAULT_VALIDATION,
|
|
424
|
+
CC_AGENT_HISTORY_FILTER: CC_AGENT_HISTORY_FILTER,
|
|
425
|
+
CC_AGENT_OUTPUT_TRANSFORM: CC_AGENT_OUTPUT_TRANSFORM,
|
|
411
426
|
CC_AGENT_DISALLOWED_TAGS: CC_AGENT_DISALLOWED_TAGS,
|
|
412
427
|
CC_AGENT_DISALLOWED_SYMBOLS: CC_AGENT_DISALLOWED_SYMBOLS,
|
|
413
428
|
};
|
|
@@ -438,19 +453,21 @@ var ClientAgent = /** @class */ (function () {
|
|
|
438
453
|
* @returns {Promise<void>}
|
|
439
454
|
* @private
|
|
440
455
|
*/
|
|
441
|
-
this._emitOuput = function (mode,
|
|
442
|
-
var validation, result_1;
|
|
456
|
+
this._emitOuput = function (mode, rawResult) { return __awaiter(_this, void 0, void 0, function () {
|
|
457
|
+
var result, validation, rawResult_1, result_1;
|
|
443
458
|
return __generator(this, function (_a) {
|
|
444
459
|
switch (_a.label) {
|
|
445
460
|
case 0:
|
|
446
|
-
|
|
461
|
+
result = this.params.transform(rawResult);
|
|
462
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"), { mode: mode, result: result, rawResult: rawResult });
|
|
447
463
|
validation = null;
|
|
448
464
|
return [4 /*yield*/, this.params.validate(result)];
|
|
449
465
|
case 1:
|
|
450
466
|
if (!(validation = _a.sent())) return [3 /*break*/, 5];
|
|
451
467
|
return [4 /*yield*/, this._resurrectModel(mode, validation)];
|
|
452
468
|
case 2:
|
|
453
|
-
|
|
469
|
+
rawResult_1 = _a.sent();
|
|
470
|
+
result_1 = this.params.transform(rawResult_1);
|
|
454
471
|
return [4 /*yield*/, this.params.validate(result_1)];
|
|
455
472
|
case 3:
|
|
456
473
|
if ((validation = _a.sent())) {
|
|
@@ -856,10 +873,10 @@ var AgentConnectionService = /** @class */ (function () {
|
|
|
856
873
|
var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
|
|
857
874
|
return "".concat(clientId, "-").concat(agentName);
|
|
858
875
|
}, function (clientId, agentName) {
|
|
859
|
-
var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, callbacks = _a.callbacks, completionName = _a.completion,
|
|
876
|
+
var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, _b = _a.transform, transform = _b === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_TRANSFORM : _b, callbacks = _a.callbacks, completionName = _a.completion, _c = _a.validate, validate = _c === void 0 ? validateDefault : _c;
|
|
860
877
|
var completion = _this.completionSchemaService.get(completionName);
|
|
861
878
|
_this.sessionValidationService.addAgentUsage(clientId, agentName);
|
|
862
|
-
return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
|
|
879
|
+
return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
|
|
863
880
|
});
|
|
864
881
|
/**
|
|
865
882
|
* Executes an input command.
|
|
@@ -1139,17 +1156,7 @@ var ClientHistory = /** @class */ (function () {
|
|
|
1139
1156
|
return agentName === _this.params.agentName;
|
|
1140
1157
|
});
|
|
1141
1158
|
commonMessages = commonMessagesRaw
|
|
1142
|
-
.filter(
|
|
1143
|
-
var role = _a.role, agentName = _a.agentName, tool_calls = _a.tool_calls;
|
|
1144
|
-
var isOk = true;
|
|
1145
|
-
if (role === "tool") {
|
|
1146
|
-
isOk = isOk && agentName === _this.params.agentName;
|
|
1147
|
-
}
|
|
1148
|
-
if (tool_calls) {
|
|
1149
|
-
isOk = isOk && agentName === _this.params.agentName;
|
|
1150
|
-
}
|
|
1151
|
-
return isOk;
|
|
1152
|
-
})
|
|
1159
|
+
.filter(this._filterCondition)
|
|
1153
1160
|
.slice(-GLOBAL_CONFIG.CC_KEEP_MESSAGES);
|
|
1154
1161
|
promptMessages = [];
|
|
1155
1162
|
{
|
|
@@ -1176,6 +1183,7 @@ var ClientHistory = /** @class */ (function () {
|
|
|
1176
1183
|
this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
|
|
1177
1184
|
params: params,
|
|
1178
1185
|
});
|
|
1186
|
+
this._filterCondition = GLOBAL_CONFIG.CC_AGENT_HISTORY_FILTER(this.params.agentName);
|
|
1179
1187
|
}
|
|
1180
1188
|
return ClientHistory;
|
|
1181
1189
|
}());
|
|
@@ -1325,7 +1333,6 @@ var ToolSchemaService = /** @class */ (function () {
|
|
|
1325
1333
|
return ToolSchemaService;
|
|
1326
1334
|
}());
|
|
1327
1335
|
|
|
1328
|
-
var AGENT_REF_CHANGED = Symbol("agent-ref-changed");
|
|
1329
1336
|
var AGENT_NEED_FETCH = Symbol("agent-need-fetch");
|
|
1330
1337
|
/**
|
|
1331
1338
|
* ClientSwarm class implements the ISwarm interface and manages agents within a swarm.
|
|
@@ -1346,47 +1353,53 @@ var ClientSwarm = /** @class */ (function () {
|
|
|
1346
1353
|
* @throws {Error} - If the timeout is reached.
|
|
1347
1354
|
*/
|
|
1348
1355
|
this.waitForOutput = functoolsKit.queued(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1349
|
-
var
|
|
1356
|
+
var _a, awaiter, resolve, getOutput, handleOutput, un, _b, agentName, output, expectAgent;
|
|
1350
1357
|
var _this = this;
|
|
1351
1358
|
return __generator(this, function (_c) {
|
|
1352
1359
|
switch (_c.label) {
|
|
1353
1360
|
case 0:
|
|
1354
1361
|
this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForOutput"));
|
|
1355
|
-
|
|
1356
|
-
|
|
1362
|
+
_a = __read(functoolsKit.createAwaiter(), 2), awaiter = _a[0], resolve = _a[1].resolve;
|
|
1363
|
+
getOutput = functoolsKit.cancelable(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1364
|
+
var _this = this;
|
|
1365
|
+
return __generator(this, function (_a) {
|
|
1366
|
+
switch (_a.label) {
|
|
1367
|
+
case 0: return [4 /*yield*/, Promise.race(this._agentList.map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
1368
|
+
var _c;
|
|
1369
|
+
var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
|
|
1370
|
+
return __generator(this, function (_e) {
|
|
1371
|
+
switch (_e.label) {
|
|
1372
|
+
case 0:
|
|
1373
|
+
_c = { agentName: agentName };
|
|
1374
|
+
return [4 /*yield*/, agent.waitForOutput()];
|
|
1375
|
+
case 1: return [2 /*return*/, (_c.output = _e.sent(), _c)];
|
|
1376
|
+
}
|
|
1377
|
+
});
|
|
1378
|
+
}); }))];
|
|
1379
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1380
|
+
}
|
|
1381
|
+
});
|
|
1382
|
+
}); });
|
|
1383
|
+
handleOutput = function () {
|
|
1384
|
+
getOutput.cancel();
|
|
1385
|
+
getOutput().then(function (value) {
|
|
1386
|
+
if (value === functoolsKit.CANCELED_PROMISE_SYMBOL) {
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
1389
|
+
resolve(value);
|
|
1390
|
+
});
|
|
1391
|
+
};
|
|
1392
|
+
un = this._agentChangedSubject.subscribe(handleOutput);
|
|
1393
|
+
handleOutput();
|
|
1394
|
+
return [4 /*yield*/, awaiter];
|
|
1357
1395
|
case 1:
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
}
|
|
1361
|
-
return [4 /*yield*/, Promise.race(__spreadArray(__spreadArray([], __read(Object.entries(this.params.agentMap).map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
1362
|
-
var _c;
|
|
1363
|
-
var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
|
|
1364
|
-
return __generator(this, function (_e) {
|
|
1365
|
-
switch (_e.label) {
|
|
1366
|
-
case 0:
|
|
1367
|
-
_c = [agentName];
|
|
1368
|
-
return [4 /*yield*/, agent.waitForOutput()];
|
|
1369
|
-
case 1: return [2 /*return*/, _c.concat([_e.sent()])];
|
|
1370
|
-
}
|
|
1371
|
-
});
|
|
1372
|
-
}); })), false), [
|
|
1373
|
-
this._agentChangedSubject
|
|
1374
|
-
.toPromise()
|
|
1375
|
-
.then(function () { return [AGENT_REF_CHANGED]; }),
|
|
1376
|
-
], false))];
|
|
1377
|
-
case 2:
|
|
1378
|
-
_a = __read.apply(void 0, [_c.sent(), 2]), agentName = _a[0], output = _a[1];
|
|
1379
|
-
if (agentName === AGENT_REF_CHANGED) {
|
|
1380
|
-
return [3 /*break*/, 1];
|
|
1381
|
-
}
|
|
1382
|
-
_b = agentName;
|
|
1396
|
+
_b = _c.sent(), agentName = _b.agentName, output = _b.output;
|
|
1397
|
+
un();
|
|
1383
1398
|
return [4 /*yield*/, this.getAgentName()];
|
|
1384
|
-
case
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
return [3 /*break*/, 1];
|
|
1389
|
-
case 4: return [2 /*return*/];
|
|
1399
|
+
case 2:
|
|
1400
|
+
expectAgent = _c.sent();
|
|
1401
|
+
agentName !== expectAgent && this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
|
|
1402
|
+
return [2 /*return*/, output];
|
|
1390
1403
|
}
|
|
1391
1404
|
});
|
|
1392
1405
|
}); });
|
|
@@ -1442,7 +1455,7 @@ var ClientSwarm = /** @class */ (function () {
|
|
|
1442
1455
|
throw new Error("agent-swarm agent ".concat(agentName, " not in the swarm"));
|
|
1443
1456
|
}
|
|
1444
1457
|
this.params.agentMap[agentName] = agent;
|
|
1445
|
-
return [4 /*yield*/, this._agentChangedSubject.next()];
|
|
1458
|
+
return [4 /*yield*/, this._agentChangedSubject.next([agentName, agent])];
|
|
1446
1459
|
case 1:
|
|
1447
1460
|
_a.sent();
|
|
1448
1461
|
return [2 /*return*/];
|
|
@@ -1470,6 +1483,13 @@ var ClientSwarm = /** @class */ (function () {
|
|
|
1470
1483
|
params: params,
|
|
1471
1484
|
});
|
|
1472
1485
|
}
|
|
1486
|
+
Object.defineProperty(ClientSwarm.prototype, "_agentList", {
|
|
1487
|
+
get: function () {
|
|
1488
|
+
return Object.entries(this.params.agentMap);
|
|
1489
|
+
},
|
|
1490
|
+
enumerable: false,
|
|
1491
|
+
configurable: true
|
|
1492
|
+
});
|
|
1473
1493
|
return ClientSwarm;
|
|
1474
1494
|
}());
|
|
1475
1495
|
|
package/build/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { scoped } from 'di-scoped';
|
|
2
|
-
import { ToolRegistry, trycatch, sleep, PubsubArrayAdapter, Subject, queued, not, memoize, schedule, ttl, singleshot, Source } from 'functools-kit';
|
|
2
|
+
import { ToolRegistry, trycatch, sleep, PubsubArrayAdapter, Subject, queued, not, memoize, createAwaiter, cancelable, CANCELED_PROMISE_SYMBOL, schedule, ttl, singleshot, Source } from 'functools-kit';
|
|
3
3
|
import { createActivator } from 'di-kit';
|
|
4
4
|
import { omit } from 'lodash-es';
|
|
5
5
|
import xml2js from 'xml2js';
|
|
@@ -353,6 +353,10 @@ var validateDefault = function (output) { return __awaiter(void 0, void 0, void
|
|
|
353
353
|
});
|
|
354
354
|
}); };
|
|
355
355
|
|
|
356
|
+
var removeXmlTags = function (input) {
|
|
357
|
+
return input.replace(/<[^>]*>.*?<\/[^>]*>/g, '');
|
|
358
|
+
};
|
|
359
|
+
|
|
356
360
|
/**
|
|
357
361
|
* @description `ask for agent function` in `llama3.1:8b` to troubleshoot (need CC_OLLAMA_EMIT_TOOL_PROTOCOL to be turned off)
|
|
358
362
|
*/
|
|
@@ -394,18 +398,29 @@ var CC_AGENT_DISALLOWED_SYMBOLS = [
|
|
|
394
398
|
"{",
|
|
395
399
|
"}",
|
|
396
400
|
];
|
|
401
|
+
var CC_AGENT_HISTORY_FILTER = function (agentName) { return function (message) {
|
|
402
|
+
var isOk = true;
|
|
403
|
+
if (message.role === "tool") {
|
|
404
|
+
isOk = isOk && message.agentName === agentName;
|
|
405
|
+
}
|
|
406
|
+
if (message.tool_calls) {
|
|
407
|
+
isOk = isOk && message.agentName === agentName;
|
|
408
|
+
}
|
|
409
|
+
return isOk;
|
|
410
|
+
}; };
|
|
411
|
+
var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
|
|
397
412
|
var CC_KEEP_MESSAGES = 5;
|
|
398
|
-
var CC_ANSWER_TIMEOUT = 120000;
|
|
399
413
|
var CC_GET_AGENT_HISTORY = function () { return new PubsubArrayAdapter(); };
|
|
400
414
|
var GLOBAL_CONFIG = {
|
|
401
415
|
CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
|
|
402
416
|
CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
|
|
403
417
|
CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
|
|
404
|
-
CC_ANSWER_TIMEOUT: CC_ANSWER_TIMEOUT,
|
|
405
418
|
CC_GET_AGENT_HISTORY: CC_GET_AGENT_HISTORY,
|
|
406
419
|
CC_SWARM_AGENT_CHANGED: CC_SWARM_AGENT_CHANGED,
|
|
407
420
|
CC_SWARM_DEFAULT_AGENT: CC_SWARM_DEFAULT_AGENT,
|
|
408
421
|
CC_AGENT_DEFAULT_VALIDATION: CC_AGENT_DEFAULT_VALIDATION,
|
|
422
|
+
CC_AGENT_HISTORY_FILTER: CC_AGENT_HISTORY_FILTER,
|
|
423
|
+
CC_AGENT_OUTPUT_TRANSFORM: CC_AGENT_OUTPUT_TRANSFORM,
|
|
409
424
|
CC_AGENT_DISALLOWED_TAGS: CC_AGENT_DISALLOWED_TAGS,
|
|
410
425
|
CC_AGENT_DISALLOWED_SYMBOLS: CC_AGENT_DISALLOWED_SYMBOLS,
|
|
411
426
|
};
|
|
@@ -436,19 +451,21 @@ var ClientAgent = /** @class */ (function () {
|
|
|
436
451
|
* @returns {Promise<void>}
|
|
437
452
|
* @private
|
|
438
453
|
*/
|
|
439
|
-
this._emitOuput = function (mode,
|
|
440
|
-
var validation, result_1;
|
|
454
|
+
this._emitOuput = function (mode, rawResult) { return __awaiter(_this, void 0, void 0, function () {
|
|
455
|
+
var result, validation, rawResult_1, result_1;
|
|
441
456
|
return __generator(this, function (_a) {
|
|
442
457
|
switch (_a.label) {
|
|
443
458
|
case 0:
|
|
444
|
-
|
|
459
|
+
result = this.params.transform(rawResult);
|
|
460
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"), { mode: mode, result: result, rawResult: rawResult });
|
|
445
461
|
validation = null;
|
|
446
462
|
return [4 /*yield*/, this.params.validate(result)];
|
|
447
463
|
case 1:
|
|
448
464
|
if (!(validation = _a.sent())) return [3 /*break*/, 5];
|
|
449
465
|
return [4 /*yield*/, this._resurrectModel(mode, validation)];
|
|
450
466
|
case 2:
|
|
451
|
-
|
|
467
|
+
rawResult_1 = _a.sent();
|
|
468
|
+
result_1 = this.params.transform(rawResult_1);
|
|
452
469
|
return [4 /*yield*/, this.params.validate(result_1)];
|
|
453
470
|
case 3:
|
|
454
471
|
if ((validation = _a.sent())) {
|
|
@@ -854,10 +871,10 @@ var AgentConnectionService = /** @class */ (function () {
|
|
|
854
871
|
var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
|
|
855
872
|
return "".concat(clientId, "-").concat(agentName);
|
|
856
873
|
}, function (clientId, agentName) {
|
|
857
|
-
var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, callbacks = _a.callbacks, completionName = _a.completion,
|
|
874
|
+
var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, _b = _a.transform, transform = _b === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_TRANSFORM : _b, callbacks = _a.callbacks, completionName = _a.completion, _c = _a.validate, validate = _c === void 0 ? validateDefault : _c;
|
|
858
875
|
var completion = _this.completionSchemaService.get(completionName);
|
|
859
876
|
_this.sessionValidationService.addAgentUsage(clientId, agentName);
|
|
860
|
-
return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
|
|
877
|
+
return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
|
|
861
878
|
});
|
|
862
879
|
/**
|
|
863
880
|
* Executes an input command.
|
|
@@ -1137,17 +1154,7 @@ var ClientHistory = /** @class */ (function () {
|
|
|
1137
1154
|
return agentName === _this.params.agentName;
|
|
1138
1155
|
});
|
|
1139
1156
|
commonMessages = commonMessagesRaw
|
|
1140
|
-
.filter(
|
|
1141
|
-
var role = _a.role, agentName = _a.agentName, tool_calls = _a.tool_calls;
|
|
1142
|
-
var isOk = true;
|
|
1143
|
-
if (role === "tool") {
|
|
1144
|
-
isOk = isOk && agentName === _this.params.agentName;
|
|
1145
|
-
}
|
|
1146
|
-
if (tool_calls) {
|
|
1147
|
-
isOk = isOk && agentName === _this.params.agentName;
|
|
1148
|
-
}
|
|
1149
|
-
return isOk;
|
|
1150
|
-
})
|
|
1157
|
+
.filter(this._filterCondition)
|
|
1151
1158
|
.slice(-GLOBAL_CONFIG.CC_KEEP_MESSAGES);
|
|
1152
1159
|
promptMessages = [];
|
|
1153
1160
|
{
|
|
@@ -1174,6 +1181,7 @@ var ClientHistory = /** @class */ (function () {
|
|
|
1174
1181
|
this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
|
|
1175
1182
|
params: params,
|
|
1176
1183
|
});
|
|
1184
|
+
this._filterCondition = GLOBAL_CONFIG.CC_AGENT_HISTORY_FILTER(this.params.agentName);
|
|
1177
1185
|
}
|
|
1178
1186
|
return ClientHistory;
|
|
1179
1187
|
}());
|
|
@@ -1323,7 +1331,6 @@ var ToolSchemaService = /** @class */ (function () {
|
|
|
1323
1331
|
return ToolSchemaService;
|
|
1324
1332
|
}());
|
|
1325
1333
|
|
|
1326
|
-
var AGENT_REF_CHANGED = Symbol("agent-ref-changed");
|
|
1327
1334
|
var AGENT_NEED_FETCH = Symbol("agent-need-fetch");
|
|
1328
1335
|
/**
|
|
1329
1336
|
* ClientSwarm class implements the ISwarm interface and manages agents within a swarm.
|
|
@@ -1344,47 +1351,53 @@ var ClientSwarm = /** @class */ (function () {
|
|
|
1344
1351
|
* @throws {Error} - If the timeout is reached.
|
|
1345
1352
|
*/
|
|
1346
1353
|
this.waitForOutput = queued(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1347
|
-
var
|
|
1354
|
+
var _a, awaiter, resolve, getOutput, handleOutput, un, _b, agentName, output, expectAgent;
|
|
1348
1355
|
var _this = this;
|
|
1349
1356
|
return __generator(this, function (_c) {
|
|
1350
1357
|
switch (_c.label) {
|
|
1351
1358
|
case 0:
|
|
1352
1359
|
this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForOutput"));
|
|
1353
|
-
|
|
1354
|
-
|
|
1360
|
+
_a = __read(createAwaiter(), 2), awaiter = _a[0], resolve = _a[1].resolve;
|
|
1361
|
+
getOutput = cancelable(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1362
|
+
var _this = this;
|
|
1363
|
+
return __generator(this, function (_a) {
|
|
1364
|
+
switch (_a.label) {
|
|
1365
|
+
case 0: return [4 /*yield*/, Promise.race(this._agentList.map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
1366
|
+
var _c;
|
|
1367
|
+
var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
|
|
1368
|
+
return __generator(this, function (_e) {
|
|
1369
|
+
switch (_e.label) {
|
|
1370
|
+
case 0:
|
|
1371
|
+
_c = { agentName: agentName };
|
|
1372
|
+
return [4 /*yield*/, agent.waitForOutput()];
|
|
1373
|
+
case 1: return [2 /*return*/, (_c.output = _e.sent(), _c)];
|
|
1374
|
+
}
|
|
1375
|
+
});
|
|
1376
|
+
}); }))];
|
|
1377
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1378
|
+
}
|
|
1379
|
+
});
|
|
1380
|
+
}); });
|
|
1381
|
+
handleOutput = function () {
|
|
1382
|
+
getOutput.cancel();
|
|
1383
|
+
getOutput().then(function (value) {
|
|
1384
|
+
if (value === CANCELED_PROMISE_SYMBOL) {
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
resolve(value);
|
|
1388
|
+
});
|
|
1389
|
+
};
|
|
1390
|
+
un = this._agentChangedSubject.subscribe(handleOutput);
|
|
1391
|
+
handleOutput();
|
|
1392
|
+
return [4 /*yield*/, awaiter];
|
|
1355
1393
|
case 1:
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
}
|
|
1359
|
-
return [4 /*yield*/, Promise.race(__spreadArray(__spreadArray([], __read(Object.entries(this.params.agentMap).map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
1360
|
-
var _c;
|
|
1361
|
-
var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
|
|
1362
|
-
return __generator(this, function (_e) {
|
|
1363
|
-
switch (_e.label) {
|
|
1364
|
-
case 0:
|
|
1365
|
-
_c = [agentName];
|
|
1366
|
-
return [4 /*yield*/, agent.waitForOutput()];
|
|
1367
|
-
case 1: return [2 /*return*/, _c.concat([_e.sent()])];
|
|
1368
|
-
}
|
|
1369
|
-
});
|
|
1370
|
-
}); })), false), [
|
|
1371
|
-
this._agentChangedSubject
|
|
1372
|
-
.toPromise()
|
|
1373
|
-
.then(function () { return [AGENT_REF_CHANGED]; }),
|
|
1374
|
-
], false))];
|
|
1375
|
-
case 2:
|
|
1376
|
-
_a = __read.apply(void 0, [_c.sent(), 2]), agentName = _a[0], output = _a[1];
|
|
1377
|
-
if (agentName === AGENT_REF_CHANGED) {
|
|
1378
|
-
return [3 /*break*/, 1];
|
|
1379
|
-
}
|
|
1380
|
-
_b = agentName;
|
|
1394
|
+
_b = _c.sent(), agentName = _b.agentName, output = _b.output;
|
|
1395
|
+
un();
|
|
1381
1396
|
return [4 /*yield*/, this.getAgentName()];
|
|
1382
|
-
case
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
return [3 /*break*/, 1];
|
|
1387
|
-
case 4: return [2 /*return*/];
|
|
1397
|
+
case 2:
|
|
1398
|
+
expectAgent = _c.sent();
|
|
1399
|
+
agentName !== expectAgent && this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
|
|
1400
|
+
return [2 /*return*/, output];
|
|
1388
1401
|
}
|
|
1389
1402
|
});
|
|
1390
1403
|
}); });
|
|
@@ -1440,7 +1453,7 @@ var ClientSwarm = /** @class */ (function () {
|
|
|
1440
1453
|
throw new Error("agent-swarm agent ".concat(agentName, " not in the swarm"));
|
|
1441
1454
|
}
|
|
1442
1455
|
this.params.agentMap[agentName] = agent;
|
|
1443
|
-
return [4 /*yield*/, this._agentChangedSubject.next()];
|
|
1456
|
+
return [4 /*yield*/, this._agentChangedSubject.next([agentName, agent])];
|
|
1444
1457
|
case 1:
|
|
1445
1458
|
_a.sent();
|
|
1446
1459
|
return [2 /*return*/];
|
|
@@ -1468,6 +1481,13 @@ var ClientSwarm = /** @class */ (function () {
|
|
|
1468
1481
|
params: params,
|
|
1469
1482
|
});
|
|
1470
1483
|
}
|
|
1484
|
+
Object.defineProperty(ClientSwarm.prototype, "_agentList", {
|
|
1485
|
+
get: function () {
|
|
1486
|
+
return Object.entries(this.params.agentMap);
|
|
1487
|
+
},
|
|
1488
|
+
enumerable: false,
|
|
1489
|
+
configurable: true
|
|
1490
|
+
});
|
|
1471
1491
|
return ClientSwarm;
|
|
1472
1492
|
}());
|
|
1473
1493
|
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -642,6 +642,8 @@ interface IAgentSchema {
|
|
|
642
642
|
* @returns A promise that resolves to a string or null.
|
|
643
643
|
*/
|
|
644
644
|
validate?: (output: string) => Promise<string | null>;
|
|
645
|
+
/** The transform function for model output */
|
|
646
|
+
transform?: (input: string) => string;
|
|
645
647
|
/** The lifecycle calbacks of the agent. */
|
|
646
648
|
callbacks?: Partial<IAgentSchemaCallbacks>;
|
|
647
649
|
}
|
|
@@ -751,7 +753,7 @@ declare class ClientAgent implements IAgent {
|
|
|
751
753
|
* @returns {Promise<void>}
|
|
752
754
|
* @private
|
|
753
755
|
*/
|
|
754
|
-
_emitOuput: (mode: ExecutionMode,
|
|
756
|
+
_emitOuput: (mode: ExecutionMode, rawResult: string) => Promise<void>;
|
|
755
757
|
/**
|
|
756
758
|
* Resurrects the model based on the given reason.
|
|
757
759
|
* @param {string} [reason] - The reason for resurrecting the model.
|
|
@@ -871,6 +873,10 @@ declare class AgentConnectionService implements IAgent {
|
|
|
871
873
|
*/
|
|
872
874
|
declare class ClientHistory implements IHistory {
|
|
873
875
|
readonly params: IHistoryParams;
|
|
876
|
+
/**
|
|
877
|
+
* Filter condition for `toArrayForAgent`
|
|
878
|
+
*/
|
|
879
|
+
_filterCondition: (message: IModelMessage) => boolean;
|
|
874
880
|
/**
|
|
875
881
|
* Creates an instance of ClientHistory.
|
|
876
882
|
* @param {IHistoryParams} params - The parameters for the history.
|
|
@@ -989,6 +995,7 @@ declare class ClientSwarm implements ISwarm {
|
|
|
989
995
|
readonly params: ISwarmParams;
|
|
990
996
|
private _agentChangedSubject;
|
|
991
997
|
private _activeAgent;
|
|
998
|
+
get _agentList(): [string, IAgent][];
|
|
992
999
|
/**
|
|
993
1000
|
* Creates an instance of ClientSwarm.
|
|
994
1001
|
* @param {ISwarmParams} params - The parameters for the swarm.
|
|
@@ -2039,11 +2046,12 @@ declare const GLOBAL_CONFIG: {
|
|
|
2039
2046
|
CC_TOOL_CALL_EXCEPTION_PROMPT: string;
|
|
2040
2047
|
CC_EMPTY_OUTPUT_PLACEHOLDERS: string[];
|
|
2041
2048
|
CC_KEEP_MESSAGES: number;
|
|
2042
|
-
CC_ANSWER_TIMEOUT: number;
|
|
2043
2049
|
CC_GET_AGENT_HISTORY: (clientId: string, agentName: AgentName) => IPubsubArray<IModelMessage>;
|
|
2044
2050
|
CC_SWARM_AGENT_CHANGED: (clientId: string, agentName: AgentName, swarmName: SwarmName) => Promise<void>;
|
|
2045
2051
|
CC_SWARM_DEFAULT_AGENT: (clientId: string, swarmName: SwarmName, defaultAgent: AgentName) => Promise<AgentName>;
|
|
2046
2052
|
CC_AGENT_DEFAULT_VALIDATION: (output: string) => Promise<string | null>;
|
|
2053
|
+
CC_AGENT_HISTORY_FILTER: (agentName: AgentName) => (message: IModelMessage) => boolean;
|
|
2054
|
+
CC_AGENT_OUTPUT_TRANSFORM: (input: string) => string;
|
|
2047
2055
|
CC_AGENT_DISALLOWED_TAGS: string[];
|
|
2048
2056
|
CC_AGENT_DISALLOWED_SYMBOLS: string[];
|
|
2049
2057
|
};
|