agent-swarm-kit 1.0.150 → 1.0.152

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 CHANGED
@@ -1501,7 +1501,7 @@ var CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT = functoolsKit.str.newline("Please
1501
1501
  /**
1502
1502
  * @description custom function to fix the model
1503
1503
  */
1504
- var CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION = function () { return Promise.resolve(); };
1504
+ var CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION = function () { return Promise.resolve(null); };
1505
1505
  /**
1506
1506
  * @description When the model output is empty just say hello to the customer
1507
1507
  */
@@ -1546,7 +1546,8 @@ var CC_AGENT_HISTORY_FILTER = function (agentName) {
1546
1546
  };
1547
1547
  var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
1548
1548
  var CC_KEEP_MESSAGES = 15;
1549
- var CC_MAX_TOOLS = 1;
1549
+ var CC_MAX_TOOL_CALLS = 1;
1550
+ var CC_AGENT_MAP_TOOLS = function (tools) { return tools; };
1550
1551
  var CC_GET_AGENT_HISTORY_ADAPTER = function () { return HistoryAdapter; };
1551
1552
  var CC_GET_CLIENT_LOGGER_ADAPTER = function () { return LoggerAdapter; };
1552
1553
  var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
@@ -1559,7 +1560,9 @@ var CC_LOGGER_ENABLE_LOG = true;
1559
1560
  var CC_LOGGER_ENABLE_CONSOLE = false;
1560
1561
  var CC_RESQUE_STRATEGY;
1561
1562
  var CC_NAME_TO_TITLE = nameToTitle;
1562
- var CC_FN_PLANTUML = function () { return Promise.resolve(""); };
1563
+ var CC_FN_PLANTUML = function () {
1564
+ return Promise.resolve("");
1565
+ };
1563
1566
  var CC_PROCESS_UUID = functoolsKit.randomString();
1564
1567
  var CC_BANHAMMER_PLACEHOLDER = "You have been banned! To continue conversation, please contact the administrator.";
1565
1568
  var GLOBAL_CONFIG = {
@@ -1567,7 +1570,8 @@ var GLOBAL_CONFIG = {
1567
1570
  CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT: CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
1568
1571
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
1569
1572
  CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
1570
- CC_MAX_TOOLS: CC_MAX_TOOLS,
1573
+ CC_MAX_TOOL_CALLS: CC_MAX_TOOL_CALLS,
1574
+ CC_AGENT_MAP_TOOLS: CC_AGENT_MAP_TOOLS,
1571
1575
  CC_GET_AGENT_HISTORY_ADAPTER: CC_GET_AGENT_HISTORY_ADAPTER,
1572
1576
  CC_GET_CLIENT_LOGGER_ADAPTER: CC_GET_CLIENT_LOGGER_ADAPTER,
1573
1577
  CC_SWARM_AGENT_CHANGED: CC_SWARM_AGENT_CHANGED,
@@ -1957,21 +1961,22 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1957
1961
  return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1958
1962
  case 3:
1959
1963
  message = _g.sent();
1960
- if (!message.tool_calls) return [3 /*break*/, 9];
1964
+ if (!message.tool_calls) return [3 /*break*/, 10];
1961
1965
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1962
1966
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " tool call begin"));
1963
- toolCalls_1 = message.tool_calls
1964
- .map(function (call) {
1965
- var _a, _b;
1966
- return ({
1967
- function: call.function,
1968
- id: (_a = call.id) !== null && _a !== void 0 ? _a : functoolsKit.randomString(),
1969
- type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
1970
- });
1971
- })
1972
- .slice(0, GLOBAL_CONFIG.CC_MAX_TOOLS);
1973
- return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1967
+ return [4 /*yield*/, self.params.mapToolCalls(message.tool_calls.map(function (call) {
1968
+ var _a, _b;
1969
+ return ({
1970
+ function: call.function,
1971
+ id: (_a = call.id) !== null && _a !== void 0 ? _a : functoolsKit.randomString(),
1972
+ type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
1973
+ });
1974
+ }), self.params.clientId, self.params.agentName)];
1974
1975
  case 4:
1976
+ toolCalls_1 = _g.sent();
1977
+ toolCalls_1 = toolCalls_1.slice(0, self.params.maxToolCalls);
1978
+ return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1979
+ case 5:
1975
1980
  _g.sent();
1976
1981
  lastToolStatusRef = Promise.resolve(null);
1977
1982
  _a = __read(functoolsKit.createAwaiter(), 2), runAwaiter_1 = _a[0], run = _a[1].resolve;
@@ -2108,19 +2113,19 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2108
2113
  });
2109
2114
  };
2110
2115
  idx = 0;
2111
- _g.label = 5;
2112
- case 5:
2113
- if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 8];
2114
- return [5 /*yield**/, _loop_1(idx)];
2116
+ _g.label = 6;
2115
2117
  case 6:
2118
+ if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 9];
2119
+ return [5 /*yield**/, _loop_1(idx)];
2120
+ case 7:
2116
2121
  state_1 = _g.sent();
2117
2122
  if (typeof state_1 === "object")
2118
2123
  return [2 /*return*/, state_1.value];
2119
- _g.label = 7;
2120
- case 7:
2121
- idx++;
2122
- return [3 /*break*/, 5];
2124
+ _g.label = 8;
2123
2125
  case 8:
2126
+ idx++;
2127
+ return [3 /*break*/, 6];
2128
+ case 9:
2124
2129
  lastToolStatusRef.finally(function () {
2125
2130
  var _a;
2126
2131
  ((_a = self.params.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterToolCalls) &&
@@ -2128,35 +2133,35 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2128
2133
  });
2129
2134
  run(true);
2130
2135
  return [2 /*return*/];
2131
- case 9:
2136
+ case 10:
2132
2137
  if (!message.tool_calls) {
2133
2138
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2134
2139
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute no tool calls detected"));
2135
2140
  }
2136
2141
  return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
2137
- case 10:
2142
+ case 11:
2138
2143
  result = _g.sent();
2139
2144
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
2140
- case 11:
2145
+ case 12:
2141
2146
  _g.sent();
2142
2147
  validation = null;
2143
2148
  return [4 /*yield*/, self.params.validate(result)];
2144
- case 12:
2145
- if (!(validation = _g.sent())) return [3 /*break*/, 15];
2149
+ case 13:
2150
+ if (!(validation = _g.sent())) return [3 /*break*/, 16];
2146
2151
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2147
2152
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
2148
2153
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2149
- case 13:
2154
+ case 14:
2150
2155
  result1 = _g.sent();
2151
2156
  return [4 /*yield*/, self._emitOutput(mode, result1)];
2152
- case 14:
2157
+ case 15:
2153
2158
  _g.sent();
2154
2159
  return [2 /*return*/];
2155
- case 15:
2160
+ case 16:
2156
2161
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2157
2162
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2158
2163
  return [4 /*yield*/, self._emitOutput(mode, result)];
2159
- case 16:
2164
+ case 17:
2160
2165
  _g.sent();
2161
2166
  return [2 /*return*/];
2162
2167
  }
@@ -2300,7 +2305,7 @@ var ClientAgent = /** @class */ (function () {
2300
2305
  */
2301
2306
  ClientAgent.prototype._resurrectModel = function (mode_1) {
2302
2307
  return __awaiter(this, arguments, void 0, function (mode, reason) {
2303
- var rawMessage, content, message, result, validation, content;
2308
+ var placeholder, rawMessage, message, result, validation;
2304
2309
  var _a;
2305
2310
  if (reason === void 0) { reason = "unknown"; }
2306
2311
  return __generator(this, function (_b) {
@@ -2309,27 +2314,22 @@ var ClientAgent = /** @class */ (function () {
2309
2314
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2310
2315
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
2311
2316
  console.warn("agent-swarm model ressurect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " reason=").concat(reason));
2317
+ placeholder = createPlaceholder();
2312
2318
  this.params.onResurrect &&
2313
2319
  this.params.onResurrect(this.params.clientId, this.params.agentName, mode, reason);
2314
- if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete")) return [3 /*break*/, 2];
2315
- return [4 /*yield*/, this.params.history.push({
2316
- role: "user",
2317
- mode: "tool",
2318
- agentName: this.params.agentName,
2319
- content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2320
- })];
2321
- case 1:
2322
- _b.sent();
2323
- return [3 /*break*/, 8];
2324
- case 2:
2325
- if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush")) return [3 /*break*/, 5];
2320
+ if (GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete") {
2321
+ console.warn("agent-swarm model ressurect did not solved the problem (still being called after recomplete patch) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2322
+ }
2323
+ else if (GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom") {
2324
+ console.warn("agent-swarm model ressurect did not solved the problem (still being called after patchedMessage emit) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2325
+ }
2326
2326
  return [4 /*yield*/, this.params.history.push({
2327
2327
  role: "resque",
2328
2328
  mode: "tool",
2329
2329
  agentName: this.params.agentName,
2330
- content: reason || "Unknown error",
2330
+ content: reason || "_resurrectModel call",
2331
2331
  })];
2332
- case 3:
2332
+ case 1:
2333
2333
  _b.sent();
2334
2334
  return [4 /*yield*/, this.params.history.push({
2335
2335
  role: "user",
@@ -2337,85 +2337,57 @@ var ClientAgent = /** @class */ (function () {
2337
2337
  agentName: this.params.agentName,
2338
2338
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_FLUSH_PROMPT,
2339
2339
  })];
2340
- case 4:
2341
- _b.sent();
2342
- return [3 /*break*/, 8];
2343
- case 5:
2344
- if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 7];
2345
- return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2346
- case 6:
2340
+ case 2:
2347
2341
  _b.sent();
2348
- return [3 /*break*/, 8];
2349
- case 7: throw new Error("agent-swarm _resurrectModel invalid strategy value=".concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " agentName=").concat(this.params.agentName, " clientId=").concat(this.params.clientId));
2350
- case 8: return [4 /*yield*/, this.getCompletion(mode)];
2351
- case 9:
2342
+ return [4 /*yield*/, this.getCompletion(mode)];
2343
+ case 3:
2352
2344
  rawMessage = _b.sent();
2353
- if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 13];
2345
+ if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
2354
2346
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2355
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel failed due to tool_calls"));
2356
- console.warn("agent-swarm model ressurect did not solved the problem (tool_calls) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2357
- content = createPlaceholder();
2347
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel should not emit tool_calls"));
2348
+ console.warn("agent-swarm _resurrectModel should not emit tool_calls for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2358
2349
  return [4 /*yield*/, this.params.history.push({
2359
2350
  role: "resque",
2360
2351
  mode: "tool",
2361
2352
  agentName: this.params.agentName,
2362
- content: reason || "Unknown error",
2363
- })];
2364
- case 10:
2365
- _b.sent();
2366
- return [4 /*yield*/, this.params.history.push({
2367
- agentName: this.params.agentName,
2368
- role: "assistant",
2369
- mode: "tool",
2370
- content: content,
2353
+ content: "_resurrectModel should not emit tool calls",
2371
2354
  })];
2372
- case 11:
2355
+ case 4:
2373
2356
  _b.sent();
2374
2357
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2375
- case 12:
2358
+ case 5:
2376
2359
  _b.sent();
2377
- return [2 /*return*/, content];
2378
- case 13: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2379
- case 14:
2360
+ return [2 /*return*/, placeholder];
2361
+ case 6: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2362
+ case 7:
2380
2363
  message = _b.sent();
2381
2364
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2382
- case 15:
2365
+ case 8:
2383
2366
  result = _b.sent();
2384
2367
  validation = null;
2385
2368
  return [4 /*yield*/, this.params.validate(result)];
2386
- case 16:
2387
- if (!(validation = _b.sent())) return [3 /*break*/, 20];
2369
+ case 9:
2370
+ if (!(validation = _b.sent())) return [3 /*break*/, 11];
2388
2371
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2389
2372
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2390
- console.warn("agent-swarm model ressurect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2391
- content = createPlaceholder();
2373
+ console.warn("agent-swarm model ressurect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
2392
2374
  return [4 /*yield*/, this.params.history.push({
2393
2375
  role: "resque",
2394
2376
  mode: "tool",
2395
2377
  agentName: this.params.agentName,
2396
- content: reason || "Unknown error",
2378
+ content: "_resurrectModel failed validation: ".concat(validation),
2397
2379
  })];
2398
- case 17:
2399
- _b.sent();
2400
- return [4 /*yield*/, this.params.history.push({
2401
- agentName: this.params.agentName,
2402
- role: "assistant",
2403
- mode: "tool",
2404
- content: content,
2405
- })];
2406
- case 18:
2407
- _b.sent();
2408
- return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2409
- case 19:
2380
+ case 10:
2410
2381
  _b.sent();
2411
- return [2 /*return*/, content];
2412
- case 20: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2413
- case 21:
2382
+ return [3 /*break*/, 13];
2383
+ case 11: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2384
+ case 12:
2414
2385
  _b.sent();
2415
- return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2416
- case 22:
2386
+ _b.label = 13;
2387
+ case 13: return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2388
+ case 14:
2417
2389
  _b.sent();
2418
- return [2 /*return*/, result];
2390
+ return [2 /*return*/, placeholder];
2419
2391
  }
2420
2392
  });
2421
2393
  });
@@ -2443,16 +2415,16 @@ var ClientAgent = /** @class */ (function () {
2443
2415
  */
2444
2416
  ClientAgent.prototype.getCompletion = function (mode) {
2445
2417
  return __awaiter(this, void 0, void 0, function () {
2446
- var messages, args, output;
2447
- var _a, _b, _c;
2448
- return __generator(this, function (_d) {
2449
- switch (_d.label) {
2418
+ var messages, args, output, message, result, validation, messages_1, args_1, output_1, output_2;
2419
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2420
+ return __generator(this, function (_k) {
2421
+ switch (_k.label) {
2450
2422
  case 0:
2451
2423
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2452
2424
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
2453
2425
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2454
2426
  case 1:
2455
- messages = _d.sent();
2427
+ messages = _k.sent();
2456
2428
  args = {
2457
2429
  clientId: this.params.clientId,
2458
2430
  agentName: this.params.agentName,
@@ -2464,9 +2436,63 @@ var ClientAgent = /** @class */ (function () {
2464
2436
  };
2465
2437
  return [4 /*yield*/, this.params.completion.getCompletion(args)];
2466
2438
  case 2:
2467
- output = _d.sent();
2468
- ((_b = this.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
2469
- ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, output));
2439
+ output = _k.sent();
2440
+ return [4 /*yield*/, this.params.map(output, this.params.clientId, this.params.agentName)];
2441
+ case 3:
2442
+ message = _k.sent();
2443
+ return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2444
+ case 4:
2445
+ result = _k.sent();
2446
+ if ((_b = message.tool_calls) === null || _b === void 0 ? void 0 : _b.length) {
2447
+ return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2448
+ }
2449
+ return [4 /*yield*/, this.params.validate(result)];
2450
+ case 5:
2451
+ validation = _k.sent();
2452
+ if (!!validation) return [3 /*break*/, 6];
2453
+ ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete) &&
2454
+ ((_d = this.params.completion.callbacks) === null || _d === void 0 ? void 0 : _d.onComplete(args, output));
2455
+ return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2456
+ case 6:
2457
+ if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete")) return [3 /*break*/, 10];
2458
+ console.warn("agent-swarm model using recomplete resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " validation=").concat(validation));
2459
+ return [4 /*yield*/, this.params.history.push({
2460
+ role: "user",
2461
+ mode: "tool",
2462
+ agentName: this.params.agentName,
2463
+ content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2464
+ })];
2465
+ case 7:
2466
+ _k.sent();
2467
+ return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2468
+ case 8:
2469
+ messages_1 = _k.sent();
2470
+ args_1 = {
2471
+ clientId: this.params.clientId,
2472
+ agentName: this.params.agentName,
2473
+ messages: messages_1,
2474
+ mode: mode,
2475
+ tools: (_e = this.params.tools) === null || _e === void 0 ? void 0 : _e.map(function (t) {
2476
+ return lodashEs.omit(t, "toolName", "docNote", "call", "validate", "callbacks");
2477
+ }),
2478
+ };
2479
+ return [4 /*yield*/, this.params.completion.getCompletion(args_1)];
2480
+ case 9:
2481
+ output_1 = _k.sent();
2482
+ return [2 /*return*/, __assign(__assign({}, output_1), { content: output_1.content || "" })];
2483
+ case 10:
2484
+ if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 12];
2485
+ console.warn("agent-swarm model using custom resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " validation=").concat(validation));
2486
+ return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2487
+ case 11:
2488
+ output_2 = _k.sent();
2489
+ ((_f = this.params.completion.callbacks) === null || _f === void 0 ? void 0 : _f.onComplete) &&
2490
+ ((_g = this.params.completion.callbacks) === null || _g === void 0 ? void 0 : _g.onComplete(args, output_2));
2491
+ return [2 /*return*/, output_2];
2492
+ case 12:
2493
+ console.warn("agent-swarm model completion pending resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
2494
+ ((_h = this.params.completion.callbacks) === null || _h === void 0 ? void 0 : _h.onComplete) &&
2495
+ ((_j = this.params.completion.callbacks) === null || _j === void 0 ? void 0 : _j.onComplete(args, output));
2470
2496
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2471
2497
  }
2472
2498
  });
@@ -2786,7 +2812,7 @@ var AgentConnectionService = /** @class */ (function () {
2786
2812
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
2787
2813
  return "".concat(clientId, "-").concat(agentName);
2788
2814
  }, function (clientId, agentName) {
2789
- 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, _c = _a.map, map = _c === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP : _c, callbacks = _a.callbacks, storages = _a.storages, states = _a.states, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
2815
+ 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, _c = _a.map, map = _c === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP : _c, _d = _a.maxToolCalls, maxToolCalls = _d === void 0 ? GLOBAL_CONFIG.CC_MAX_TOOL_CALLS : _d, _e = _a.mapToolCalls, mapToolCalls = _e === void 0 ? GLOBAL_CONFIG.CC_AGENT_MAP_TOOLS : _e, callbacks = _a.callbacks, storages = _a.storages, states = _a.states, completionName = _a.completion, _f = _a.validate, validate = _f === void 0 ? validateDefault : _f;
2790
2816
  var completion = _this.completionSchemaService.get(completionName);
2791
2817
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
2792
2818
  storages === null || storages === void 0 ? void 0 : storages.forEach(function (storageName) {
@@ -2799,7 +2825,7 @@ var AgentConnectionService = /** @class */ (function () {
2799
2825
  .getStateRef(clientId, stateName)
2800
2826
  .waitForInit();
2801
2827
  });
2802
- return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, bus: _this.busService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
2828
+ return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, maxToolCalls: maxToolCalls, mapToolCalls: mapToolCalls, logger: _this.loggerService, bus: _this.busService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
2803
2829
  });
2804
2830
  /**
2805
2831
  * Executes an input command.
@@ -3052,7 +3078,7 @@ var ClientHistory = /** @class */ (function () {
3052
3078
  });
3053
3079
  };
3054
3080
  /**
3055
- * Pushes a message to the history.
3081
+ * Pop a message from the history.
3056
3082
  * @returns {Promise<IModelMessage | null>}
3057
3083
  */
3058
3084
  ClientHistory.prototype.pop = function () {
package/build/index.mjs CHANGED
@@ -1499,7 +1499,7 @@ var CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT = str.newline("Please analyze the l
1499
1499
  /**
1500
1500
  * @description custom function to fix the model
1501
1501
  */
1502
- var CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION = function () { return Promise.resolve(); };
1502
+ var CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION = function () { return Promise.resolve(null); };
1503
1503
  /**
1504
1504
  * @description When the model output is empty just say hello to the customer
1505
1505
  */
@@ -1544,7 +1544,8 @@ var CC_AGENT_HISTORY_FILTER = function (agentName) {
1544
1544
  };
1545
1545
  var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
1546
1546
  var CC_KEEP_MESSAGES = 15;
1547
- var CC_MAX_TOOLS = 1;
1547
+ var CC_MAX_TOOL_CALLS = 1;
1548
+ var CC_AGENT_MAP_TOOLS = function (tools) { return tools; };
1548
1549
  var CC_GET_AGENT_HISTORY_ADAPTER = function () { return HistoryAdapter; };
1549
1550
  var CC_GET_CLIENT_LOGGER_ADAPTER = function () { return LoggerAdapter; };
1550
1551
  var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
@@ -1557,7 +1558,9 @@ var CC_LOGGER_ENABLE_LOG = true;
1557
1558
  var CC_LOGGER_ENABLE_CONSOLE = false;
1558
1559
  var CC_RESQUE_STRATEGY;
1559
1560
  var CC_NAME_TO_TITLE = nameToTitle;
1560
- var CC_FN_PLANTUML = function () { return Promise.resolve(""); };
1561
+ var CC_FN_PLANTUML = function () {
1562
+ return Promise.resolve("");
1563
+ };
1561
1564
  var CC_PROCESS_UUID = randomString();
1562
1565
  var CC_BANHAMMER_PLACEHOLDER = "You have been banned! To continue conversation, please contact the administrator.";
1563
1566
  var GLOBAL_CONFIG = {
@@ -1565,7 +1568,8 @@ var GLOBAL_CONFIG = {
1565
1568
  CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT: CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
1566
1569
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
1567
1570
  CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
1568
- CC_MAX_TOOLS: CC_MAX_TOOLS,
1571
+ CC_MAX_TOOL_CALLS: CC_MAX_TOOL_CALLS,
1572
+ CC_AGENT_MAP_TOOLS: CC_AGENT_MAP_TOOLS,
1569
1573
  CC_GET_AGENT_HISTORY_ADAPTER: CC_GET_AGENT_HISTORY_ADAPTER,
1570
1574
  CC_GET_CLIENT_LOGGER_ADAPTER: CC_GET_CLIENT_LOGGER_ADAPTER,
1571
1575
  CC_SWARM_AGENT_CHANGED: CC_SWARM_AGENT_CHANGED,
@@ -1955,21 +1959,22 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1955
1959
  return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1956
1960
  case 3:
1957
1961
  message = _g.sent();
1958
- if (!message.tool_calls) return [3 /*break*/, 9];
1962
+ if (!message.tool_calls) return [3 /*break*/, 10];
1959
1963
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1960
1964
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " tool call begin"));
1961
- toolCalls_1 = message.tool_calls
1962
- .map(function (call) {
1963
- var _a, _b;
1964
- return ({
1965
- function: call.function,
1966
- id: (_a = call.id) !== null && _a !== void 0 ? _a : randomString(),
1967
- type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
1968
- });
1969
- })
1970
- .slice(0, GLOBAL_CONFIG.CC_MAX_TOOLS);
1971
- return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1965
+ return [4 /*yield*/, self.params.mapToolCalls(message.tool_calls.map(function (call) {
1966
+ var _a, _b;
1967
+ return ({
1968
+ function: call.function,
1969
+ id: (_a = call.id) !== null && _a !== void 0 ? _a : randomString(),
1970
+ type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
1971
+ });
1972
+ }), self.params.clientId, self.params.agentName)];
1972
1973
  case 4:
1974
+ toolCalls_1 = _g.sent();
1975
+ toolCalls_1 = toolCalls_1.slice(0, self.params.maxToolCalls);
1976
+ return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1977
+ case 5:
1973
1978
  _g.sent();
1974
1979
  lastToolStatusRef = Promise.resolve(null);
1975
1980
  _a = __read(createAwaiter(), 2), runAwaiter_1 = _a[0], run = _a[1].resolve;
@@ -2106,19 +2111,19 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2106
2111
  });
2107
2112
  };
2108
2113
  idx = 0;
2109
- _g.label = 5;
2110
- case 5:
2111
- if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 8];
2112
- return [5 /*yield**/, _loop_1(idx)];
2114
+ _g.label = 6;
2113
2115
  case 6:
2116
+ if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 9];
2117
+ return [5 /*yield**/, _loop_1(idx)];
2118
+ case 7:
2114
2119
  state_1 = _g.sent();
2115
2120
  if (typeof state_1 === "object")
2116
2121
  return [2 /*return*/, state_1.value];
2117
- _g.label = 7;
2118
- case 7:
2119
- idx++;
2120
- return [3 /*break*/, 5];
2122
+ _g.label = 8;
2121
2123
  case 8:
2124
+ idx++;
2125
+ return [3 /*break*/, 6];
2126
+ case 9:
2122
2127
  lastToolStatusRef.finally(function () {
2123
2128
  var _a;
2124
2129
  ((_a = self.params.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterToolCalls) &&
@@ -2126,35 +2131,35 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2126
2131
  });
2127
2132
  run(true);
2128
2133
  return [2 /*return*/];
2129
- case 9:
2134
+ case 10:
2130
2135
  if (!message.tool_calls) {
2131
2136
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2132
2137
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute no tool calls detected"));
2133
2138
  }
2134
2139
  return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
2135
- case 10:
2140
+ case 11:
2136
2141
  result = _g.sent();
2137
2142
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
2138
- case 11:
2143
+ case 12:
2139
2144
  _g.sent();
2140
2145
  validation = null;
2141
2146
  return [4 /*yield*/, self.params.validate(result)];
2142
- case 12:
2143
- if (!(validation = _g.sent())) return [3 /*break*/, 15];
2147
+ case 13:
2148
+ if (!(validation = _g.sent())) return [3 /*break*/, 16];
2144
2149
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2145
2150
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
2146
2151
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2147
- case 13:
2152
+ case 14:
2148
2153
  result1 = _g.sent();
2149
2154
  return [4 /*yield*/, self._emitOutput(mode, result1)];
2150
- case 14:
2155
+ case 15:
2151
2156
  _g.sent();
2152
2157
  return [2 /*return*/];
2153
- case 15:
2158
+ case 16:
2154
2159
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2155
2160
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2156
2161
  return [4 /*yield*/, self._emitOutput(mode, result)];
2157
- case 16:
2162
+ case 17:
2158
2163
  _g.sent();
2159
2164
  return [2 /*return*/];
2160
2165
  }
@@ -2298,7 +2303,7 @@ var ClientAgent = /** @class */ (function () {
2298
2303
  */
2299
2304
  ClientAgent.prototype._resurrectModel = function (mode_1) {
2300
2305
  return __awaiter(this, arguments, void 0, function (mode, reason) {
2301
- var rawMessage, content, message, result, validation, content;
2306
+ var placeholder, rawMessage, message, result, validation;
2302
2307
  var _a;
2303
2308
  if (reason === void 0) { reason = "unknown"; }
2304
2309
  return __generator(this, function (_b) {
@@ -2307,27 +2312,22 @@ var ClientAgent = /** @class */ (function () {
2307
2312
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2308
2313
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
2309
2314
  console.warn("agent-swarm model ressurect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " reason=").concat(reason));
2315
+ placeholder = createPlaceholder();
2310
2316
  this.params.onResurrect &&
2311
2317
  this.params.onResurrect(this.params.clientId, this.params.agentName, mode, reason);
2312
- if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete")) return [3 /*break*/, 2];
2313
- return [4 /*yield*/, this.params.history.push({
2314
- role: "user",
2315
- mode: "tool",
2316
- agentName: this.params.agentName,
2317
- content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2318
- })];
2319
- case 1:
2320
- _b.sent();
2321
- return [3 /*break*/, 8];
2322
- case 2:
2323
- if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush")) return [3 /*break*/, 5];
2318
+ if (GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete") {
2319
+ console.warn("agent-swarm model ressurect did not solved the problem (still being called after recomplete patch) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2320
+ }
2321
+ else if (GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom") {
2322
+ console.warn("agent-swarm model ressurect did not solved the problem (still being called after patchedMessage emit) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2323
+ }
2324
2324
  return [4 /*yield*/, this.params.history.push({
2325
2325
  role: "resque",
2326
2326
  mode: "tool",
2327
2327
  agentName: this.params.agentName,
2328
- content: reason || "Unknown error",
2328
+ content: reason || "_resurrectModel call",
2329
2329
  })];
2330
- case 3:
2330
+ case 1:
2331
2331
  _b.sent();
2332
2332
  return [4 /*yield*/, this.params.history.push({
2333
2333
  role: "user",
@@ -2335,85 +2335,57 @@ var ClientAgent = /** @class */ (function () {
2335
2335
  agentName: this.params.agentName,
2336
2336
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_FLUSH_PROMPT,
2337
2337
  })];
2338
- case 4:
2339
- _b.sent();
2340
- return [3 /*break*/, 8];
2341
- case 5:
2342
- if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 7];
2343
- return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2344
- case 6:
2338
+ case 2:
2345
2339
  _b.sent();
2346
- return [3 /*break*/, 8];
2347
- case 7: throw new Error("agent-swarm _resurrectModel invalid strategy value=".concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " agentName=").concat(this.params.agentName, " clientId=").concat(this.params.clientId));
2348
- case 8: return [4 /*yield*/, this.getCompletion(mode)];
2349
- case 9:
2340
+ return [4 /*yield*/, this.getCompletion(mode)];
2341
+ case 3:
2350
2342
  rawMessage = _b.sent();
2351
- if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 13];
2343
+ if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
2352
2344
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2353
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel failed due to tool_calls"));
2354
- console.warn("agent-swarm model ressurect did not solved the problem (tool_calls) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2355
- content = createPlaceholder();
2345
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel should not emit tool_calls"));
2346
+ console.warn("agent-swarm _resurrectModel should not emit tool_calls for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2356
2347
  return [4 /*yield*/, this.params.history.push({
2357
2348
  role: "resque",
2358
2349
  mode: "tool",
2359
2350
  agentName: this.params.agentName,
2360
- content: reason || "Unknown error",
2361
- })];
2362
- case 10:
2363
- _b.sent();
2364
- return [4 /*yield*/, this.params.history.push({
2365
- agentName: this.params.agentName,
2366
- role: "assistant",
2367
- mode: "tool",
2368
- content: content,
2351
+ content: "_resurrectModel should not emit tool calls",
2369
2352
  })];
2370
- case 11:
2353
+ case 4:
2371
2354
  _b.sent();
2372
2355
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2373
- case 12:
2356
+ case 5:
2374
2357
  _b.sent();
2375
- return [2 /*return*/, content];
2376
- case 13: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2377
- case 14:
2358
+ return [2 /*return*/, placeholder];
2359
+ case 6: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2360
+ case 7:
2378
2361
  message = _b.sent();
2379
2362
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2380
- case 15:
2363
+ case 8:
2381
2364
  result = _b.sent();
2382
2365
  validation = null;
2383
2366
  return [4 /*yield*/, this.params.validate(result)];
2384
- case 16:
2385
- if (!(validation = _b.sent())) return [3 /*break*/, 20];
2367
+ case 9:
2368
+ if (!(validation = _b.sent())) return [3 /*break*/, 11];
2386
2369
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2387
2370
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2388
- console.warn("agent-swarm model ressurect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2389
- content = createPlaceholder();
2371
+ console.warn("agent-swarm model ressurect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
2390
2372
  return [4 /*yield*/, this.params.history.push({
2391
2373
  role: "resque",
2392
2374
  mode: "tool",
2393
2375
  agentName: this.params.agentName,
2394
- content: reason || "Unknown error",
2376
+ content: "_resurrectModel failed validation: ".concat(validation),
2395
2377
  })];
2396
- case 17:
2397
- _b.sent();
2398
- return [4 /*yield*/, this.params.history.push({
2399
- agentName: this.params.agentName,
2400
- role: "assistant",
2401
- mode: "tool",
2402
- content: content,
2403
- })];
2404
- case 18:
2405
- _b.sent();
2406
- return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2407
- case 19:
2378
+ case 10:
2408
2379
  _b.sent();
2409
- return [2 /*return*/, content];
2410
- case 20: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2411
- case 21:
2380
+ return [3 /*break*/, 13];
2381
+ case 11: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2382
+ case 12:
2412
2383
  _b.sent();
2413
- return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2414
- case 22:
2384
+ _b.label = 13;
2385
+ case 13: return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2386
+ case 14:
2415
2387
  _b.sent();
2416
- return [2 /*return*/, result];
2388
+ return [2 /*return*/, placeholder];
2417
2389
  }
2418
2390
  });
2419
2391
  });
@@ -2441,16 +2413,16 @@ var ClientAgent = /** @class */ (function () {
2441
2413
  */
2442
2414
  ClientAgent.prototype.getCompletion = function (mode) {
2443
2415
  return __awaiter(this, void 0, void 0, function () {
2444
- var messages, args, output;
2445
- var _a, _b, _c;
2446
- return __generator(this, function (_d) {
2447
- switch (_d.label) {
2416
+ var messages, args, output, message, result, validation, messages_1, args_1, output_1, output_2;
2417
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2418
+ return __generator(this, function (_k) {
2419
+ switch (_k.label) {
2448
2420
  case 0:
2449
2421
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2450
2422
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
2451
2423
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2452
2424
  case 1:
2453
- messages = _d.sent();
2425
+ messages = _k.sent();
2454
2426
  args = {
2455
2427
  clientId: this.params.clientId,
2456
2428
  agentName: this.params.agentName,
@@ -2462,9 +2434,63 @@ var ClientAgent = /** @class */ (function () {
2462
2434
  };
2463
2435
  return [4 /*yield*/, this.params.completion.getCompletion(args)];
2464
2436
  case 2:
2465
- output = _d.sent();
2466
- ((_b = this.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
2467
- ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, output));
2437
+ output = _k.sent();
2438
+ return [4 /*yield*/, this.params.map(output, this.params.clientId, this.params.agentName)];
2439
+ case 3:
2440
+ message = _k.sent();
2441
+ return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2442
+ case 4:
2443
+ result = _k.sent();
2444
+ if ((_b = message.tool_calls) === null || _b === void 0 ? void 0 : _b.length) {
2445
+ return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2446
+ }
2447
+ return [4 /*yield*/, this.params.validate(result)];
2448
+ case 5:
2449
+ validation = _k.sent();
2450
+ if (!!validation) return [3 /*break*/, 6];
2451
+ ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete) &&
2452
+ ((_d = this.params.completion.callbacks) === null || _d === void 0 ? void 0 : _d.onComplete(args, output));
2453
+ return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2454
+ case 6:
2455
+ if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete")) return [3 /*break*/, 10];
2456
+ console.warn("agent-swarm model using recomplete resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " validation=").concat(validation));
2457
+ return [4 /*yield*/, this.params.history.push({
2458
+ role: "user",
2459
+ mode: "tool",
2460
+ agentName: this.params.agentName,
2461
+ content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2462
+ })];
2463
+ case 7:
2464
+ _k.sent();
2465
+ return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2466
+ case 8:
2467
+ messages_1 = _k.sent();
2468
+ args_1 = {
2469
+ clientId: this.params.clientId,
2470
+ agentName: this.params.agentName,
2471
+ messages: messages_1,
2472
+ mode: mode,
2473
+ tools: (_e = this.params.tools) === null || _e === void 0 ? void 0 : _e.map(function (t) {
2474
+ return omit(t, "toolName", "docNote", "call", "validate", "callbacks");
2475
+ }),
2476
+ };
2477
+ return [4 /*yield*/, this.params.completion.getCompletion(args_1)];
2478
+ case 9:
2479
+ output_1 = _k.sent();
2480
+ return [2 /*return*/, __assign(__assign({}, output_1), { content: output_1.content || "" })];
2481
+ case 10:
2482
+ if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 12];
2483
+ console.warn("agent-swarm model using custom resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " validation=").concat(validation));
2484
+ return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2485
+ case 11:
2486
+ output_2 = _k.sent();
2487
+ ((_f = this.params.completion.callbacks) === null || _f === void 0 ? void 0 : _f.onComplete) &&
2488
+ ((_g = this.params.completion.callbacks) === null || _g === void 0 ? void 0 : _g.onComplete(args, output_2));
2489
+ return [2 /*return*/, output_2];
2490
+ case 12:
2491
+ console.warn("agent-swarm model completion pending resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
2492
+ ((_h = this.params.completion.callbacks) === null || _h === void 0 ? void 0 : _h.onComplete) &&
2493
+ ((_j = this.params.completion.callbacks) === null || _j === void 0 ? void 0 : _j.onComplete(args, output));
2468
2494
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2469
2495
  }
2470
2496
  });
@@ -2784,7 +2810,7 @@ var AgentConnectionService = /** @class */ (function () {
2784
2810
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
2785
2811
  return "".concat(clientId, "-").concat(agentName);
2786
2812
  }, function (clientId, agentName) {
2787
- 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, _c = _a.map, map = _c === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP : _c, callbacks = _a.callbacks, storages = _a.storages, states = _a.states, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
2813
+ 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, _c = _a.map, map = _c === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP : _c, _d = _a.maxToolCalls, maxToolCalls = _d === void 0 ? GLOBAL_CONFIG.CC_MAX_TOOL_CALLS : _d, _e = _a.mapToolCalls, mapToolCalls = _e === void 0 ? GLOBAL_CONFIG.CC_AGENT_MAP_TOOLS : _e, callbacks = _a.callbacks, storages = _a.storages, states = _a.states, completionName = _a.completion, _f = _a.validate, validate = _f === void 0 ? validateDefault : _f;
2788
2814
  var completion = _this.completionSchemaService.get(completionName);
2789
2815
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
2790
2816
  storages === null || storages === void 0 ? void 0 : storages.forEach(function (storageName) {
@@ -2797,7 +2823,7 @@ var AgentConnectionService = /** @class */ (function () {
2797
2823
  .getStateRef(clientId, stateName)
2798
2824
  .waitForInit();
2799
2825
  });
2800
- return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, bus: _this.busService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
2826
+ return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, maxToolCalls: maxToolCalls, mapToolCalls: mapToolCalls, logger: _this.loggerService, bus: _this.busService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
2801
2827
  });
2802
2828
  /**
2803
2829
  * Executes an input command.
@@ -3050,7 +3076,7 @@ var ClientHistory = /** @class */ (function () {
3050
3076
  });
3051
3077
  };
3052
3078
  /**
3053
- * Pushes a message to the history.
3079
+ * Pop a message from the history.
3054
3080
  * @returns {Promise<IModelMessage | null>}
3055
3081
  */
3056
3082
  ClientHistory.prototype.pop = function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.150",
3
+ "version": "1.0.152",
4
4
  "description": "A TypeScript library for building orchestrated framework-agnostic multi-agent AI systems",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -1645,6 +1645,10 @@ interface IAgentSchemaCallbacks {
1645
1645
  * Interface representing the schema for an agent.
1646
1646
  */
1647
1647
  interface IAgentSchema {
1648
+ /** The map function to filter unwanted tool calls */
1649
+ mapToolCalls?: (tool: IToolCall[], clientId: string, agentName: AgentName) => IToolCall[] | Promise<IToolCall[]>;
1650
+ /** The maximum quantity of tool calls per complete */
1651
+ maxToolCalls?: number;
1648
1652
  /** The description for documentation */
1649
1653
  docDescription?: string;
1650
1654
  /** The name of the agent. */
@@ -2017,7 +2021,7 @@ declare class ClientHistory implements IHistory {
2017
2021
  */
2018
2022
  push(message: IModelMessage): Promise<void>;
2019
2023
  /**
2020
- * Pushes a message to the history.
2024
+ * Pop a message from the history.
2021
2025
  * @returns {Promise<IModelMessage | null>}
2022
2026
  */
2023
2027
  pop(): Promise<IModelMessage | null>;
@@ -5342,7 +5346,8 @@ declare const GLOBAL_CONFIG: {
5342
5346
  CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT: string;
5343
5347
  CC_EMPTY_OUTPUT_PLACEHOLDERS: string[];
5344
5348
  CC_KEEP_MESSAGES: number;
5345
- CC_MAX_TOOLS: number;
5349
+ CC_MAX_TOOL_CALLS: number;
5350
+ CC_AGENT_MAP_TOOLS: (tool: IToolCall[], clientId: string, agentName: AgentName) => IToolCall[] | Promise<IToolCall[]>;
5346
5351
  CC_GET_AGENT_HISTORY_ADAPTER: (clientId: string, agentName: AgentName) => IHistoryAdapter;
5347
5352
  CC_GET_CLIENT_LOGGER_ADAPTER: () => ILoggerAdapter;
5348
5353
  CC_SWARM_AGENT_CHANGED: (clientId: string, agentName: AgentName, swarmName: SwarmName) => Promise<void>;
@@ -5367,7 +5372,7 @@ declare const GLOBAL_CONFIG: {
5367
5372
  CC_FN_PLANTUML: (uml: string) => Promise<string>;
5368
5373
  CC_PROCESS_UUID: string;
5369
5374
  CC_BANHAMMER_PLACEHOLDER: string;
5370
- CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION: (clientId: string, agentName: AgentName) => Promise<void>;
5375
+ CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION: (clientId: string, agentName: AgentName) => Promise<IModelMessage | null>;
5371
5376
  };
5372
5377
  declare const setConfig: (config: Partial<typeof GLOBAL_CONFIG>) => void;
5373
5378