agent-swarm-kit 1.0.39 → 1.0.41

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
@@ -396,24 +396,25 @@ var CC_SWARM_DEFAULT_AGENT = function (_a, _b, defaultAgent_1) { return __awaite
396
396
  }); };
397
397
  var CC_AGENT_DEFAULT_VALIDATION = validateDefault;
398
398
  var CC_AGENT_DISALLOWED_TAGS = ["tool_call", "toolcall", "tool"];
399
- var CC_AGENT_DISALLOWED_SYMBOLS = [
400
- "{",
401
- "}",
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
- }; };
399
+ var CC_AGENT_DISALLOWED_SYMBOLS = ["{", "}"];
400
+ var CC_AGENT_HISTORY_FILTER = function (agentName) {
401
+ 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
+ };
413
412
  var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
414
413
  var CC_KEEP_MESSAGES = 5;
415
414
  var CC_GET_AGENT_HISTORY = function () { return new functoolsKit.PubsubArrayAdapter(); };
415
+ var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
416
416
  var CC_AGENT_SEPARATE_HISTORY = false;
417
+ var CC_AGENT_SYSTEM_PROMPT = undefined;
417
418
  var GLOBAL_CONFIG = {
418
419
  CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
419
420
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
@@ -424,6 +425,8 @@ var GLOBAL_CONFIG = {
424
425
  CC_AGENT_DEFAULT_VALIDATION: CC_AGENT_DEFAULT_VALIDATION,
425
426
  CC_AGENT_HISTORY_FILTER: CC_AGENT_HISTORY_FILTER,
426
427
  CC_AGENT_OUTPUT_TRANSFORM: CC_AGENT_OUTPUT_TRANSFORM,
428
+ CC_AGENT_OUTPUT_MAP: CC_AGENT_OUTPUT_MAP,
429
+ CC_AGENT_SYSTEM_PROMPT: CC_AGENT_SYSTEM_PROMPT,
427
430
  CC_AGENT_SEPARATE_HISTORY: CC_AGENT_SEPARATE_HISTORY,
428
431
  CC_AGENT_DISALLOWED_TAGS: CC_AGENT_DISALLOWED_TAGS,
429
432
  CC_AGENT_DISALLOWED_SYMBOLS: CC_AGENT_DISALLOWED_SYMBOLS,
@@ -459,33 +462,36 @@ var ClientAgent = /** @class */ (function () {
459
462
  var result, validation, rawResult_1, result_1;
460
463
  return __generator(this, function (_a) {
461
464
  switch (_a.label) {
462
- case 0:
463
- result = this.params.transform(rawResult);
465
+ case 0: return [4 /*yield*/, this.params.transform(rawResult, this.params.clientId, this.params.agentName)];
466
+ case 1:
467
+ result = _a.sent();
464
468
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"), { mode: mode, result: result, rawResult: rawResult });
465
469
  validation = null;
466
470
  return [4 /*yield*/, this.params.validate(result)];
467
- case 1:
468
- if (!(validation = _a.sent())) return [3 /*break*/, 5];
469
- return [4 /*yield*/, this._resurrectModel(mode, validation)];
470
471
  case 2:
472
+ if (!(validation = _a.sent())) return [3 /*break*/, 7];
473
+ return [4 /*yield*/, this._resurrectModel(mode, validation)];
474
+ case 3:
471
475
  rawResult_1 = _a.sent();
472
- result_1 = this.params.transform(rawResult_1);
476
+ return [4 /*yield*/, this.params.transform(rawResult_1, this.params.clientId, this.params.agentName)];
477
+ case 4:
478
+ result_1 = _a.sent();
473
479
  return [4 /*yield*/, this.params.validate(result_1)];
474
- case 3:
480
+ case 5:
475
481
  if ((validation = _a.sent())) {
476
482
  throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " model ressurect failed: ").concat(validation));
477
483
  }
478
484
  this.params.onOutput &&
479
485
  this.params.onOutput(this.params.clientId, this.params.agentName, result_1);
480
486
  return [4 /*yield*/, this._outputSubject.next(result_1)];
481
- case 4:
487
+ case 6:
482
488
  _a.sent();
483
489
  return [2 /*return*/];
484
- case 5:
490
+ case 7:
485
491
  this.params.onOutput &&
486
492
  this.params.onOutput(this.params.clientId, this.params.agentName, result);
487
493
  return [4 /*yield*/, this._outputSubject.next(result)];
488
- case 6:
494
+ case 8:
489
495
  _a.sent();
490
496
  return [2 /*return*/];
491
497
  }
@@ -498,7 +504,7 @@ var ClientAgent = /** @class */ (function () {
498
504
  * @private
499
505
  */
500
506
  this._resurrectModel = function (mode, reason) { return __awaiter(_this, void 0, void 0, function () {
501
- var message, result, validation, content;
507
+ var rawMessage, message, result, validation, content;
502
508
  return __generator(this, function (_a) {
503
509
  switch (_a.label) {
504
510
  case 0:
@@ -523,12 +529,15 @@ var ClientAgent = /** @class */ (function () {
523
529
  _a.sent();
524
530
  return [4 /*yield*/, this.getCompletion(mode)];
525
531
  case 3:
532
+ rawMessage = _a.sent();
533
+ return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
534
+ case 4:
526
535
  message = _a.sent();
527
536
  result = message.content;
528
537
  validation = null;
529
538
  return [4 /*yield*/, this.params.validate(result)];
530
- case 4:
531
- if (!(validation = _a.sent())) return [3 /*break*/, 6];
539
+ case 5:
540
+ if (!(validation = _a.sent())) return [3 /*break*/, 7];
532
541
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
533
542
  content = getPlaceholder();
534
543
  return [4 /*yield*/, this.params.history.push({
@@ -537,11 +546,11 @@ var ClientAgent = /** @class */ (function () {
537
546
  mode: "tool",
538
547
  content: content,
539
548
  })];
540
- case 5:
549
+ case 6:
541
550
  _a.sent();
542
551
  return [2 /*return*/, content];
543
- case 6: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
544
- case 7:
552
+ case 7: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
553
+ case 8:
545
554
  _a.sent();
546
555
  return [2 /*return*/, result];
547
556
  }
@@ -698,8 +707,9 @@ var ClientAgent = /** @class */ (function () {
698
707
  * @returns {Promise<void>}
699
708
  */
700
709
  this.execute = functoolsKit.queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
701
- var message, toolCalls, _loop_1, this_1, toolCalls_1, toolCalls_1_1, tool, state_1, e_1_1, result, validation, result1;
710
+ var rawMessage, message, toolCalls, _loop_1, this_1, toolCalls_1, toolCalls_1_1, tool, state_1, e_1_1, result, validation, result1;
702
711
  var e_1, _a;
712
+ var _this = this;
703
713
  var _b, _c, _d, _e, _f, _g;
704
714
  return __generator(this, function (_h) {
705
715
  switch (_h.label) {
@@ -717,8 +727,11 @@ var ClientAgent = /** @class */ (function () {
717
727
  _h.sent();
718
728
  return [4 /*yield*/, this.getCompletion(mode)];
719
729
  case 2:
730
+ rawMessage = _h.sent();
731
+ return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
732
+ case 3:
720
733
  message = _h.sent();
721
- if (!message.tool_calls) return [3 /*break*/, 12];
734
+ if (!message.tool_calls) return [3 /*break*/, 13];
722
735
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
723
736
  toolCalls = message.tool_calls.map(function (call) {
724
737
  var _a, _b;
@@ -729,7 +742,7 @@ var ClientAgent = /** @class */ (function () {
729
742
  });
730
743
  });
731
744
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
732
- case 3:
745
+ case 4:
733
746
  _h.sent();
734
747
  _loop_1 = function (tool) {
735
748
  var targetFn, result_2, result_3;
@@ -763,12 +776,16 @@ var ClientAgent = /** @class */ (function () {
763
776
  _j.sent();
764
777
  return [2 /*return*/, { value: void 0 }];
765
778
  case 7:
766
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onCall) &&
767
- ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
779
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall) &&
780
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onBeforeCall(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
768
781
  /**
769
782
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
770
783
  */
771
- targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
784
+ Promise.resolve(targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments)).then(function () {
785
+ var _a, _b;
786
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
787
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments));
788
+ });
772
789
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
773
790
  return [4 /*yield*/, Promise.race([
774
791
  this_1._toolCommitSubject.toPromise(),
@@ -782,62 +799,62 @@ var ClientAgent = /** @class */ (function () {
782
799
  });
783
800
  };
784
801
  this_1 = this;
785
- _h.label = 4;
786
- case 4:
787
- _h.trys.push([4, 9, 10, 11]);
788
- toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
789
802
  _h.label = 5;
790
803
  case 5:
791
- if (!!toolCalls_1_1.done) return [3 /*break*/, 8];
804
+ _h.trys.push([5, 10, 11, 12]);
805
+ toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
806
+ _h.label = 6;
807
+ case 6:
808
+ if (!!toolCalls_1_1.done) return [3 /*break*/, 9];
792
809
  tool = toolCalls_1_1.value;
793
810
  return [5 /*yield**/, _loop_1(tool)];
794
- case 6:
811
+ case 7:
795
812
  state_1 = _h.sent();
796
813
  if (typeof state_1 === "object")
797
814
  return [2 /*return*/, state_1.value];
798
- _h.label = 7;
799
- case 7:
815
+ _h.label = 8;
816
+ case 8:
800
817
  toolCalls_1_1 = toolCalls_1.next();
801
- return [3 /*break*/, 5];
802
- case 8: return [3 /*break*/, 11];
803
- case 9:
818
+ return [3 /*break*/, 6];
819
+ case 9: return [3 /*break*/, 12];
820
+ case 10:
804
821
  e_1_1 = _h.sent();
805
822
  e_1 = { error: e_1_1 };
806
- return [3 /*break*/, 11];
807
- case 10:
823
+ return [3 /*break*/, 12];
824
+ case 11:
808
825
  try {
809
826
  if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
810
827
  }
811
828
  finally { if (e_1) throw e_1.error; }
812
829
  return [7 /*endfinally*/];
813
- case 11:
830
+ case 12:
814
831
  ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
815
832
  this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
816
833
  return [2 /*return*/];
817
- case 12:
834
+ case 13:
818
835
  if (!message.tool_calls) {
819
836
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
820
837
  }
821
838
  result = message.content;
822
839
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
823
- case 13:
840
+ case 14:
824
841
  _h.sent();
825
842
  validation = null;
826
843
  return [4 /*yield*/, this.params.validate(result)];
827
- case 14:
828
- if (!(validation = _h.sent())) return [3 /*break*/, 17];
844
+ case 15:
845
+ if (!(validation = _h.sent())) return [3 /*break*/, 18];
829
846
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
830
847
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
831
- case 15:
848
+ case 16:
832
849
  result1 = _h.sent();
833
850
  return [4 /*yield*/, this._emitOuput(mode, result1)];
834
- case 16:
851
+ case 17:
835
852
  _h.sent();
836
853
  return [2 /*return*/];
837
- case 17:
854
+ case 18:
838
855
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
839
856
  return [4 /*yield*/, this._emitOuput(mode, result)];
840
- case 18:
857
+ case 19:
841
858
  _h.sent();
842
859
  return [2 /*return*/];
843
860
  }
@@ -887,10 +904,10 @@ var AgentConnectionService = /** @class */ (function () {
887
904
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
888
905
  return "".concat(clientId, "-").concat(agentName);
889
906
  }, function (clientId, agentName) {
890
- 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;
907
+ 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, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
891
908
  var completion = _this.completionSchemaService.get(completionName);
892
909
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
893
- 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));
910
+ 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, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
894
911
  });
895
912
  /**
896
913
  * Executes an input command.
@@ -1111,20 +1128,21 @@ var ClientHistory = /** @class */ (function () {
1111
1128
  var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, promptMessages;
1112
1129
  var _this = this;
1113
1130
  var _d, e_2, _e, _f;
1114
- return __generator(this, function (_g) {
1115
- switch (_g.label) {
1131
+ var _g;
1132
+ return __generator(this, function (_h) {
1133
+ switch (_h.label) {
1116
1134
  case 0:
1117
1135
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " toArrayForAgent"));
1118
1136
  commonMessagesRaw = [];
1119
1137
  systemMessagesRaw = [];
1120
- _g.label = 1;
1138
+ _h.label = 1;
1121
1139
  case 1:
1122
- _g.trys.push([1, 6, 7, 12]);
1140
+ _h.trys.push([1, 6, 7, 12]);
1123
1141
  _a = true, _b = __asyncValues(this.params.items);
1124
- _g.label = 2;
1142
+ _h.label = 2;
1125
1143
  case 2: return [4 /*yield*/, _b.next()];
1126
1144
  case 3:
1127
- if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
1145
+ if (!(_c = _h.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
1128
1146
  _f = _c.value;
1129
1147
  _a = false;
1130
1148
  content = _f;
@@ -1145,22 +1163,22 @@ var ClientHistory = /** @class */ (function () {
1145
1163
  else {
1146
1164
  commonMessagesRaw.push(message);
1147
1165
  }
1148
- _g.label = 4;
1166
+ _h.label = 4;
1149
1167
  case 4:
1150
1168
  _a = true;
1151
1169
  return [3 /*break*/, 2];
1152
1170
  case 5: return [3 /*break*/, 12];
1153
1171
  case 6:
1154
- e_2_1 = _g.sent();
1172
+ e_2_1 = _h.sent();
1155
1173
  e_2 = { error: e_2_1 };
1156
1174
  return [3 /*break*/, 12];
1157
1175
  case 7:
1158
- _g.trys.push([7, , 10, 11]);
1176
+ _h.trys.push([7, , 10, 11]);
1159
1177
  if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
1160
1178
  return [4 /*yield*/, _e.call(_b)];
1161
1179
  case 8:
1162
- _g.sent();
1163
- _g.label = 9;
1180
+ _h.sent();
1181
+ _h.label = 9;
1164
1182
  case 9: return [3 /*break*/, 11];
1165
1183
  case 10:
1166
1184
  if (e_2) throw e_2.error;
@@ -1178,19 +1196,26 @@ var ClientHistory = /** @class */ (function () {
1178
1196
  {
1179
1197
  promptMessages.push({
1180
1198
  agentName: this.params.agentName,
1181
- mode: 'tool',
1199
+ mode: "tool",
1182
1200
  content: prompt,
1183
1201
  role: "system",
1184
1202
  });
1185
- system &&
1186
- system.forEach(function (content) {
1187
- return promptMessages.push({
1188
- agentName: _this.params.agentName,
1189
- mode: 'tool',
1190
- content: content,
1191
- role: "system",
1192
- });
1203
+ (_g = GLOBAL_CONFIG.CC_AGENT_SYSTEM_PROMPT) === null || _g === void 0 ? void 0 : _g.forEach(function (content) {
1204
+ return promptMessages.push({
1205
+ agentName: _this.params.agentName,
1206
+ mode: "tool",
1207
+ content: content,
1208
+ role: "system",
1193
1209
  });
1210
+ });
1211
+ system === null || system === void 0 ? void 0 : system.forEach(function (content) {
1212
+ return promptMessages.push({
1213
+ agentName: _this.params.agentName,
1214
+ mode: "tool",
1215
+ content: content,
1216
+ role: "system",
1217
+ });
1218
+ });
1194
1219
  }
1195
1220
  return [2 /*return*/, __spreadArray(__spreadArray(__spreadArray([], __read(promptMessages), false), __read(systemMessages), false), __read(commonMessages), false)];
1196
1221
  }
package/build/index.mjs CHANGED
@@ -394,24 +394,25 @@ var CC_SWARM_DEFAULT_AGENT = function (_a, _b, defaultAgent_1) { return __awaite
394
394
  }); };
395
395
  var CC_AGENT_DEFAULT_VALIDATION = validateDefault;
396
396
  var CC_AGENT_DISALLOWED_TAGS = ["tool_call", "toolcall", "tool"];
397
- var CC_AGENT_DISALLOWED_SYMBOLS = [
398
- "{",
399
- "}",
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
- }; };
397
+ var CC_AGENT_DISALLOWED_SYMBOLS = ["{", "}"];
398
+ var CC_AGENT_HISTORY_FILTER = function (agentName) {
399
+ return function (message) {
400
+ var isOk = true;
401
+ if (message.role === "tool") {
402
+ isOk = isOk && message.agentName === agentName;
403
+ }
404
+ if (message.tool_calls) {
405
+ isOk = isOk && message.agentName === agentName;
406
+ }
407
+ return isOk;
408
+ };
409
+ };
411
410
  var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
412
411
  var CC_KEEP_MESSAGES = 5;
413
412
  var CC_GET_AGENT_HISTORY = function () { return new PubsubArrayAdapter(); };
413
+ var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
414
414
  var CC_AGENT_SEPARATE_HISTORY = false;
415
+ var CC_AGENT_SYSTEM_PROMPT = undefined;
415
416
  var GLOBAL_CONFIG = {
416
417
  CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
417
418
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
@@ -422,6 +423,8 @@ var GLOBAL_CONFIG = {
422
423
  CC_AGENT_DEFAULT_VALIDATION: CC_AGENT_DEFAULT_VALIDATION,
423
424
  CC_AGENT_HISTORY_FILTER: CC_AGENT_HISTORY_FILTER,
424
425
  CC_AGENT_OUTPUT_TRANSFORM: CC_AGENT_OUTPUT_TRANSFORM,
426
+ CC_AGENT_OUTPUT_MAP: CC_AGENT_OUTPUT_MAP,
427
+ CC_AGENT_SYSTEM_PROMPT: CC_AGENT_SYSTEM_PROMPT,
425
428
  CC_AGENT_SEPARATE_HISTORY: CC_AGENT_SEPARATE_HISTORY,
426
429
  CC_AGENT_DISALLOWED_TAGS: CC_AGENT_DISALLOWED_TAGS,
427
430
  CC_AGENT_DISALLOWED_SYMBOLS: CC_AGENT_DISALLOWED_SYMBOLS,
@@ -457,33 +460,36 @@ var ClientAgent = /** @class */ (function () {
457
460
  var result, validation, rawResult_1, result_1;
458
461
  return __generator(this, function (_a) {
459
462
  switch (_a.label) {
460
- case 0:
461
- result = this.params.transform(rawResult);
463
+ case 0: return [4 /*yield*/, this.params.transform(rawResult, this.params.clientId, this.params.agentName)];
464
+ case 1:
465
+ result = _a.sent();
462
466
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"), { mode: mode, result: result, rawResult: rawResult });
463
467
  validation = null;
464
468
  return [4 /*yield*/, this.params.validate(result)];
465
- case 1:
466
- if (!(validation = _a.sent())) return [3 /*break*/, 5];
467
- return [4 /*yield*/, this._resurrectModel(mode, validation)];
468
469
  case 2:
470
+ if (!(validation = _a.sent())) return [3 /*break*/, 7];
471
+ return [4 /*yield*/, this._resurrectModel(mode, validation)];
472
+ case 3:
469
473
  rawResult_1 = _a.sent();
470
- result_1 = this.params.transform(rawResult_1);
474
+ return [4 /*yield*/, this.params.transform(rawResult_1, this.params.clientId, this.params.agentName)];
475
+ case 4:
476
+ result_1 = _a.sent();
471
477
  return [4 /*yield*/, this.params.validate(result_1)];
472
- case 3:
478
+ case 5:
473
479
  if ((validation = _a.sent())) {
474
480
  throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " model ressurect failed: ").concat(validation));
475
481
  }
476
482
  this.params.onOutput &&
477
483
  this.params.onOutput(this.params.clientId, this.params.agentName, result_1);
478
484
  return [4 /*yield*/, this._outputSubject.next(result_1)];
479
- case 4:
485
+ case 6:
480
486
  _a.sent();
481
487
  return [2 /*return*/];
482
- case 5:
488
+ case 7:
483
489
  this.params.onOutput &&
484
490
  this.params.onOutput(this.params.clientId, this.params.agentName, result);
485
491
  return [4 /*yield*/, this._outputSubject.next(result)];
486
- case 6:
492
+ case 8:
487
493
  _a.sent();
488
494
  return [2 /*return*/];
489
495
  }
@@ -496,7 +502,7 @@ var ClientAgent = /** @class */ (function () {
496
502
  * @private
497
503
  */
498
504
  this._resurrectModel = function (mode, reason) { return __awaiter(_this, void 0, void 0, function () {
499
- var message, result, validation, content;
505
+ var rawMessage, message, result, validation, content;
500
506
  return __generator(this, function (_a) {
501
507
  switch (_a.label) {
502
508
  case 0:
@@ -521,12 +527,15 @@ var ClientAgent = /** @class */ (function () {
521
527
  _a.sent();
522
528
  return [4 /*yield*/, this.getCompletion(mode)];
523
529
  case 3:
530
+ rawMessage = _a.sent();
531
+ return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
532
+ case 4:
524
533
  message = _a.sent();
525
534
  result = message.content;
526
535
  validation = null;
527
536
  return [4 /*yield*/, this.params.validate(result)];
528
- case 4:
529
- if (!(validation = _a.sent())) return [3 /*break*/, 6];
537
+ case 5:
538
+ if (!(validation = _a.sent())) return [3 /*break*/, 7];
530
539
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
531
540
  content = getPlaceholder();
532
541
  return [4 /*yield*/, this.params.history.push({
@@ -535,11 +544,11 @@ var ClientAgent = /** @class */ (function () {
535
544
  mode: "tool",
536
545
  content: content,
537
546
  })];
538
- case 5:
547
+ case 6:
539
548
  _a.sent();
540
549
  return [2 /*return*/, content];
541
- case 6: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
542
- case 7:
550
+ case 7: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
551
+ case 8:
543
552
  _a.sent();
544
553
  return [2 /*return*/, result];
545
554
  }
@@ -696,8 +705,9 @@ var ClientAgent = /** @class */ (function () {
696
705
  * @returns {Promise<void>}
697
706
  */
698
707
  this.execute = queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
699
- var message, toolCalls, _loop_1, this_1, toolCalls_1, toolCalls_1_1, tool, state_1, e_1_1, result, validation, result1;
708
+ var rawMessage, message, toolCalls, _loop_1, this_1, toolCalls_1, toolCalls_1_1, tool, state_1, e_1_1, result, validation, result1;
700
709
  var e_1, _a;
710
+ var _this = this;
701
711
  var _b, _c, _d, _e, _f, _g;
702
712
  return __generator(this, function (_h) {
703
713
  switch (_h.label) {
@@ -715,8 +725,11 @@ var ClientAgent = /** @class */ (function () {
715
725
  _h.sent();
716
726
  return [4 /*yield*/, this.getCompletion(mode)];
717
727
  case 2:
728
+ rawMessage = _h.sent();
729
+ return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
730
+ case 3:
718
731
  message = _h.sent();
719
- if (!message.tool_calls) return [3 /*break*/, 12];
732
+ if (!message.tool_calls) return [3 /*break*/, 13];
720
733
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
721
734
  toolCalls = message.tool_calls.map(function (call) {
722
735
  var _a, _b;
@@ -727,7 +740,7 @@ var ClientAgent = /** @class */ (function () {
727
740
  });
728
741
  });
729
742
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
730
- case 3:
743
+ case 4:
731
744
  _h.sent();
732
745
  _loop_1 = function (tool) {
733
746
  var targetFn, result_2, result_3;
@@ -761,12 +774,16 @@ var ClientAgent = /** @class */ (function () {
761
774
  _j.sent();
762
775
  return [2 /*return*/, { value: void 0 }];
763
776
  case 7:
764
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onCall) &&
765
- ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
777
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall) &&
778
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onBeforeCall(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
766
779
  /**
767
780
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
768
781
  */
769
- targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
782
+ Promise.resolve(targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments)).then(function () {
783
+ var _a, _b;
784
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
785
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments));
786
+ });
770
787
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
771
788
  return [4 /*yield*/, Promise.race([
772
789
  this_1._toolCommitSubject.toPromise(),
@@ -780,62 +797,62 @@ var ClientAgent = /** @class */ (function () {
780
797
  });
781
798
  };
782
799
  this_1 = this;
783
- _h.label = 4;
784
- case 4:
785
- _h.trys.push([4, 9, 10, 11]);
786
- toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
787
800
  _h.label = 5;
788
801
  case 5:
789
- if (!!toolCalls_1_1.done) return [3 /*break*/, 8];
802
+ _h.trys.push([5, 10, 11, 12]);
803
+ toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
804
+ _h.label = 6;
805
+ case 6:
806
+ if (!!toolCalls_1_1.done) return [3 /*break*/, 9];
790
807
  tool = toolCalls_1_1.value;
791
808
  return [5 /*yield**/, _loop_1(tool)];
792
- case 6:
809
+ case 7:
793
810
  state_1 = _h.sent();
794
811
  if (typeof state_1 === "object")
795
812
  return [2 /*return*/, state_1.value];
796
- _h.label = 7;
797
- case 7:
813
+ _h.label = 8;
814
+ case 8:
798
815
  toolCalls_1_1 = toolCalls_1.next();
799
- return [3 /*break*/, 5];
800
- case 8: return [3 /*break*/, 11];
801
- case 9:
816
+ return [3 /*break*/, 6];
817
+ case 9: return [3 /*break*/, 12];
818
+ case 10:
802
819
  e_1_1 = _h.sent();
803
820
  e_1 = { error: e_1_1 };
804
- return [3 /*break*/, 11];
805
- case 10:
821
+ return [3 /*break*/, 12];
822
+ case 11:
806
823
  try {
807
824
  if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
808
825
  }
809
826
  finally { if (e_1) throw e_1.error; }
810
827
  return [7 /*endfinally*/];
811
- case 11:
828
+ case 12:
812
829
  ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
813
830
  this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
814
831
  return [2 /*return*/];
815
- case 12:
832
+ case 13:
816
833
  if (!message.tool_calls) {
817
834
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
818
835
  }
819
836
  result = message.content;
820
837
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
821
- case 13:
838
+ case 14:
822
839
  _h.sent();
823
840
  validation = null;
824
841
  return [4 /*yield*/, this.params.validate(result)];
825
- case 14:
826
- if (!(validation = _h.sent())) return [3 /*break*/, 17];
842
+ case 15:
843
+ if (!(validation = _h.sent())) return [3 /*break*/, 18];
827
844
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
828
845
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
829
- case 15:
846
+ case 16:
830
847
  result1 = _h.sent();
831
848
  return [4 /*yield*/, this._emitOuput(mode, result1)];
832
- case 16:
849
+ case 17:
833
850
  _h.sent();
834
851
  return [2 /*return*/];
835
- case 17:
852
+ case 18:
836
853
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
837
854
  return [4 /*yield*/, this._emitOuput(mode, result)];
838
- case 18:
855
+ case 19:
839
856
  _h.sent();
840
857
  return [2 /*return*/];
841
858
  }
@@ -885,10 +902,10 @@ var AgentConnectionService = /** @class */ (function () {
885
902
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
886
903
  return "".concat(clientId, "-").concat(agentName);
887
904
  }, function (clientId, agentName) {
888
- 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;
905
+ 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, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
889
906
  var completion = _this.completionSchemaService.get(completionName);
890
907
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
891
- 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));
908
+ 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, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
892
909
  });
893
910
  /**
894
911
  * Executes an input command.
@@ -1109,20 +1126,21 @@ var ClientHistory = /** @class */ (function () {
1109
1126
  var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, promptMessages;
1110
1127
  var _this = this;
1111
1128
  var _d, e_2, _e, _f;
1112
- return __generator(this, function (_g) {
1113
- switch (_g.label) {
1129
+ var _g;
1130
+ return __generator(this, function (_h) {
1131
+ switch (_h.label) {
1114
1132
  case 0:
1115
1133
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " toArrayForAgent"));
1116
1134
  commonMessagesRaw = [];
1117
1135
  systemMessagesRaw = [];
1118
- _g.label = 1;
1136
+ _h.label = 1;
1119
1137
  case 1:
1120
- _g.trys.push([1, 6, 7, 12]);
1138
+ _h.trys.push([1, 6, 7, 12]);
1121
1139
  _a = true, _b = __asyncValues(this.params.items);
1122
- _g.label = 2;
1140
+ _h.label = 2;
1123
1141
  case 2: return [4 /*yield*/, _b.next()];
1124
1142
  case 3:
1125
- if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
1143
+ if (!(_c = _h.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
1126
1144
  _f = _c.value;
1127
1145
  _a = false;
1128
1146
  content = _f;
@@ -1143,22 +1161,22 @@ var ClientHistory = /** @class */ (function () {
1143
1161
  else {
1144
1162
  commonMessagesRaw.push(message);
1145
1163
  }
1146
- _g.label = 4;
1164
+ _h.label = 4;
1147
1165
  case 4:
1148
1166
  _a = true;
1149
1167
  return [3 /*break*/, 2];
1150
1168
  case 5: return [3 /*break*/, 12];
1151
1169
  case 6:
1152
- e_2_1 = _g.sent();
1170
+ e_2_1 = _h.sent();
1153
1171
  e_2 = { error: e_2_1 };
1154
1172
  return [3 /*break*/, 12];
1155
1173
  case 7:
1156
- _g.trys.push([7, , 10, 11]);
1174
+ _h.trys.push([7, , 10, 11]);
1157
1175
  if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
1158
1176
  return [4 /*yield*/, _e.call(_b)];
1159
1177
  case 8:
1160
- _g.sent();
1161
- _g.label = 9;
1178
+ _h.sent();
1179
+ _h.label = 9;
1162
1180
  case 9: return [3 /*break*/, 11];
1163
1181
  case 10:
1164
1182
  if (e_2) throw e_2.error;
@@ -1176,19 +1194,26 @@ var ClientHistory = /** @class */ (function () {
1176
1194
  {
1177
1195
  promptMessages.push({
1178
1196
  agentName: this.params.agentName,
1179
- mode: 'tool',
1197
+ mode: "tool",
1180
1198
  content: prompt,
1181
1199
  role: "system",
1182
1200
  });
1183
- system &&
1184
- system.forEach(function (content) {
1185
- return promptMessages.push({
1186
- agentName: _this.params.agentName,
1187
- mode: 'tool',
1188
- content: content,
1189
- role: "system",
1190
- });
1201
+ (_g = GLOBAL_CONFIG.CC_AGENT_SYSTEM_PROMPT) === null || _g === void 0 ? void 0 : _g.forEach(function (content) {
1202
+ return promptMessages.push({
1203
+ agentName: _this.params.agentName,
1204
+ mode: "tool",
1205
+ content: content,
1206
+ role: "system",
1191
1207
  });
1208
+ });
1209
+ system === null || system === void 0 ? void 0 : system.forEach(function (content) {
1210
+ return promptMessages.push({
1211
+ agentName: _this.params.agentName,
1212
+ mode: "tool",
1213
+ content: content,
1214
+ role: "system",
1215
+ });
1216
+ });
1192
1217
  }
1193
1218
  return [2 /*return*/, __spreadArray(__spreadArray(__spreadArray([], __read(promptMessages), false), __read(systemMessages), false), __read(commonMessages), false)];
1194
1219
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.39",
3
+ "version": "1.0.41",
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
@@ -497,13 +497,23 @@ type CompletionName = string;
497
497
  */
498
498
  interface IAgentToolCallbacks<T = Record<string, unknown>> {
499
499
  /**
500
- * Callback triggered when the tool is called.
500
+ * Callback triggered before the tool is called.
501
+ * @param toolId - The `tool_call_id` for openai history
501
502
  * @param clientId - The ID of the client.
502
503
  * @param agentName - The name of the agent.
503
504
  * @param params - The parameters for the tool.
504
505
  * @returns A promise that resolves when the tool call is complete.
505
506
  */
506
- onCall?: (clientId: string, agentName: AgentName, params: T) => Promise<void>;
507
+ onBeforeCall?: (toolId: string, clientId: string, agentName: AgentName, params: T) => Promise<void>;
508
+ /**
509
+ * Callback triggered after the tool is called.
510
+ * @param toolId - The `tool_call_id` for openai history
511
+ * @param clientId - The ID of the client.
512
+ * @param agentName - The name of the agent.
513
+ * @param params - The parameters for the tool.
514
+ * @returns A promise that resolves when the tool call is complete.
515
+ */
516
+ onAfterCall?: (toolId: string, clientId: string, agentName: AgentName, params: T) => Promise<void>;
507
517
  /**
508
518
  * Callback triggered when the tool parameters are validated.
509
519
  * @param clientId - The ID of the client.
@@ -522,6 +532,7 @@ interface IAgentTool<T = Record<string, unknown>> extends ITool {
522
532
  toolName: ToolName;
523
533
  /**
524
534
  * Calls the tool with the specified parameters.
535
+ * @param toolId - The `tool_call_id` for openai history
525
536
  * @param clientId - The ID of the client.
526
537
  * @param agentName - The name of the agent.
527
538
  * @param params - The parameters for the tool.
@@ -660,7 +671,9 @@ interface IAgentSchema {
660
671
  */
661
672
  validate?: (output: string) => Promise<string | null>;
662
673
  /** The transform function for model output */
663
- transform?: (input: string) => string;
674
+ transform?: (input: string, clientId: string, agentName: AgentName) => Promise<string> | string;
675
+ /** The map function for assistant messages. Use to transform json to tool_call for deepseek r1 on ollama*/
676
+ map?: (message: IModelMessage, clientId: string, agentName: AgentName) => Promise<IModelMessage> | IModelMessage;
664
677
  /** The lifecycle calbacks of the agent. */
665
678
  callbacks?: Partial<IAgentSchemaCallbacks>;
666
679
  }
@@ -2075,6 +2088,8 @@ declare const GLOBAL_CONFIG: {
2075
2088
  CC_AGENT_DEFAULT_VALIDATION: (output: string) => Promise<string | null>;
2076
2089
  CC_AGENT_HISTORY_FILTER: (agentName: AgentName) => (message: IModelMessage) => boolean;
2077
2090
  CC_AGENT_OUTPUT_TRANSFORM: (input: string) => string;
2091
+ CC_AGENT_OUTPUT_MAP: (message: IModelMessage) => IModelMessage | Promise<IModelMessage>;
2092
+ CC_AGENT_SYSTEM_PROMPT: string[];
2078
2093
  CC_AGENT_SEPARATE_HISTORY: boolean;
2079
2094
  CC_AGENT_DISALLOWED_TAGS: string[];
2080
2095
  CC_AGENT_DISALLOWED_SYMBOLS: string[];