agent-swarm-kit 1.0.40 → 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,7 +707,7 @@ 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;
703
712
  var _this = this;
704
713
  var _b, _c, _d, _e, _f, _g;
@@ -718,8 +727,11 @@ var ClientAgent = /** @class */ (function () {
718
727
  _h.sent();
719
728
  return [4 /*yield*/, this.getCompletion(mode)];
720
729
  case 2:
730
+ rawMessage = _h.sent();
731
+ return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
732
+ case 3:
721
733
  message = _h.sent();
722
- if (!message.tool_calls) return [3 /*break*/, 12];
734
+ if (!message.tool_calls) return [3 /*break*/, 13];
723
735
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
724
736
  toolCalls = message.tool_calls.map(function (call) {
725
737
  var _a, _b;
@@ -730,7 +742,7 @@ var ClientAgent = /** @class */ (function () {
730
742
  });
731
743
  });
732
744
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
733
- case 3:
745
+ case 4:
734
746
  _h.sent();
735
747
  _loop_1 = function (tool) {
736
748
  var targetFn, result_2, result_3;
@@ -787,62 +799,62 @@ var ClientAgent = /** @class */ (function () {
787
799
  });
788
800
  };
789
801
  this_1 = this;
790
- _h.label = 4;
791
- case 4:
792
- _h.trys.push([4, 9, 10, 11]);
793
- toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
794
802
  _h.label = 5;
795
803
  case 5:
796
- 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];
797
809
  tool = toolCalls_1_1.value;
798
810
  return [5 /*yield**/, _loop_1(tool)];
799
- case 6:
811
+ case 7:
800
812
  state_1 = _h.sent();
801
813
  if (typeof state_1 === "object")
802
814
  return [2 /*return*/, state_1.value];
803
- _h.label = 7;
804
- case 7:
815
+ _h.label = 8;
816
+ case 8:
805
817
  toolCalls_1_1 = toolCalls_1.next();
806
- return [3 /*break*/, 5];
807
- case 8: return [3 /*break*/, 11];
808
- case 9:
818
+ return [3 /*break*/, 6];
819
+ case 9: return [3 /*break*/, 12];
820
+ case 10:
809
821
  e_1_1 = _h.sent();
810
822
  e_1 = { error: e_1_1 };
811
- return [3 /*break*/, 11];
812
- case 10:
823
+ return [3 /*break*/, 12];
824
+ case 11:
813
825
  try {
814
826
  if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
815
827
  }
816
828
  finally { if (e_1) throw e_1.error; }
817
829
  return [7 /*endfinally*/];
818
- case 11:
830
+ case 12:
819
831
  ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
820
832
  this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
821
833
  return [2 /*return*/];
822
- case 12:
834
+ case 13:
823
835
  if (!message.tool_calls) {
824
836
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
825
837
  }
826
838
  result = message.content;
827
839
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
828
- case 13:
840
+ case 14:
829
841
  _h.sent();
830
842
  validation = null;
831
843
  return [4 /*yield*/, this.params.validate(result)];
832
- case 14:
833
- if (!(validation = _h.sent())) return [3 /*break*/, 17];
844
+ case 15:
845
+ if (!(validation = _h.sent())) return [3 /*break*/, 18];
834
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 });
835
847
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
836
- case 15:
848
+ case 16:
837
849
  result1 = _h.sent();
838
850
  return [4 /*yield*/, this._emitOuput(mode, result1)];
839
- case 16:
851
+ case 17:
840
852
  _h.sent();
841
853
  return [2 /*return*/];
842
- case 17:
854
+ case 18:
843
855
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
844
856
  return [4 /*yield*/, this._emitOuput(mode, result)];
845
- case 18:
857
+ case 19:
846
858
  _h.sent();
847
859
  return [2 /*return*/];
848
860
  }
@@ -892,10 +904,10 @@ var AgentConnectionService = /** @class */ (function () {
892
904
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
893
905
  return "".concat(clientId, "-").concat(agentName);
894
906
  }, function (clientId, agentName) {
895
- 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;
896
908
  var completion = _this.completionSchemaService.get(completionName);
897
909
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
898
- 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));
899
911
  });
900
912
  /**
901
913
  * Executes an input command.
@@ -1116,20 +1128,21 @@ var ClientHistory = /** @class */ (function () {
1116
1128
  var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, promptMessages;
1117
1129
  var _this = this;
1118
1130
  var _d, e_2, _e, _f;
1119
- return __generator(this, function (_g) {
1120
- switch (_g.label) {
1131
+ var _g;
1132
+ return __generator(this, function (_h) {
1133
+ switch (_h.label) {
1121
1134
  case 0:
1122
1135
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " toArrayForAgent"));
1123
1136
  commonMessagesRaw = [];
1124
1137
  systemMessagesRaw = [];
1125
- _g.label = 1;
1138
+ _h.label = 1;
1126
1139
  case 1:
1127
- _g.trys.push([1, 6, 7, 12]);
1140
+ _h.trys.push([1, 6, 7, 12]);
1128
1141
  _a = true, _b = __asyncValues(this.params.items);
1129
- _g.label = 2;
1142
+ _h.label = 2;
1130
1143
  case 2: return [4 /*yield*/, _b.next()];
1131
1144
  case 3:
1132
- 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];
1133
1146
  _f = _c.value;
1134
1147
  _a = false;
1135
1148
  content = _f;
@@ -1150,22 +1163,22 @@ var ClientHistory = /** @class */ (function () {
1150
1163
  else {
1151
1164
  commonMessagesRaw.push(message);
1152
1165
  }
1153
- _g.label = 4;
1166
+ _h.label = 4;
1154
1167
  case 4:
1155
1168
  _a = true;
1156
1169
  return [3 /*break*/, 2];
1157
1170
  case 5: return [3 /*break*/, 12];
1158
1171
  case 6:
1159
- e_2_1 = _g.sent();
1172
+ e_2_1 = _h.sent();
1160
1173
  e_2 = { error: e_2_1 };
1161
1174
  return [3 /*break*/, 12];
1162
1175
  case 7:
1163
- _g.trys.push([7, , 10, 11]);
1176
+ _h.trys.push([7, , 10, 11]);
1164
1177
  if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
1165
1178
  return [4 /*yield*/, _e.call(_b)];
1166
1179
  case 8:
1167
- _g.sent();
1168
- _g.label = 9;
1180
+ _h.sent();
1181
+ _h.label = 9;
1169
1182
  case 9: return [3 /*break*/, 11];
1170
1183
  case 10:
1171
1184
  if (e_2) throw e_2.error;
@@ -1183,19 +1196,26 @@ var ClientHistory = /** @class */ (function () {
1183
1196
  {
1184
1197
  promptMessages.push({
1185
1198
  agentName: this.params.agentName,
1186
- mode: 'tool',
1199
+ mode: "tool",
1187
1200
  content: prompt,
1188
1201
  role: "system",
1189
1202
  });
1190
- system &&
1191
- system.forEach(function (content) {
1192
- return promptMessages.push({
1193
- agentName: _this.params.agentName,
1194
- mode: 'tool',
1195
- content: content,
1196
- role: "system",
1197
- });
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",
1198
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
+ });
1199
1219
  }
1200
1220
  return [2 /*return*/, __spreadArray(__spreadArray(__spreadArray([], __read(promptMessages), false), __read(systemMessages), false), __read(commonMessages), false)];
1201
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,7 +705,7 @@ 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;
701
710
  var _this = this;
702
711
  var _b, _c, _d, _e, _f, _g;
@@ -716,8 +725,11 @@ var ClientAgent = /** @class */ (function () {
716
725
  _h.sent();
717
726
  return [4 /*yield*/, this.getCompletion(mode)];
718
727
  case 2:
728
+ rawMessage = _h.sent();
729
+ return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
730
+ case 3:
719
731
  message = _h.sent();
720
- if (!message.tool_calls) return [3 /*break*/, 12];
732
+ if (!message.tool_calls) return [3 /*break*/, 13];
721
733
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
722
734
  toolCalls = message.tool_calls.map(function (call) {
723
735
  var _a, _b;
@@ -728,7 +740,7 @@ var ClientAgent = /** @class */ (function () {
728
740
  });
729
741
  });
730
742
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
731
- case 3:
743
+ case 4:
732
744
  _h.sent();
733
745
  _loop_1 = function (tool) {
734
746
  var targetFn, result_2, result_3;
@@ -785,62 +797,62 @@ var ClientAgent = /** @class */ (function () {
785
797
  });
786
798
  };
787
799
  this_1 = this;
788
- _h.label = 4;
789
- case 4:
790
- _h.trys.push([4, 9, 10, 11]);
791
- toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
792
800
  _h.label = 5;
793
801
  case 5:
794
- 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];
795
807
  tool = toolCalls_1_1.value;
796
808
  return [5 /*yield**/, _loop_1(tool)];
797
- case 6:
809
+ case 7:
798
810
  state_1 = _h.sent();
799
811
  if (typeof state_1 === "object")
800
812
  return [2 /*return*/, state_1.value];
801
- _h.label = 7;
802
- case 7:
813
+ _h.label = 8;
814
+ case 8:
803
815
  toolCalls_1_1 = toolCalls_1.next();
804
- return [3 /*break*/, 5];
805
- case 8: return [3 /*break*/, 11];
806
- case 9:
816
+ return [3 /*break*/, 6];
817
+ case 9: return [3 /*break*/, 12];
818
+ case 10:
807
819
  e_1_1 = _h.sent();
808
820
  e_1 = { error: e_1_1 };
809
- return [3 /*break*/, 11];
810
- case 10:
821
+ return [3 /*break*/, 12];
822
+ case 11:
811
823
  try {
812
824
  if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
813
825
  }
814
826
  finally { if (e_1) throw e_1.error; }
815
827
  return [7 /*endfinally*/];
816
- case 11:
828
+ case 12:
817
829
  ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
818
830
  this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
819
831
  return [2 /*return*/];
820
- case 12:
832
+ case 13:
821
833
  if (!message.tool_calls) {
822
834
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
823
835
  }
824
836
  result = message.content;
825
837
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
826
- case 13:
838
+ case 14:
827
839
  _h.sent();
828
840
  validation = null;
829
841
  return [4 /*yield*/, this.params.validate(result)];
830
- case 14:
831
- if (!(validation = _h.sent())) return [3 /*break*/, 17];
842
+ case 15:
843
+ if (!(validation = _h.sent())) return [3 /*break*/, 18];
832
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 });
833
845
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
834
- case 15:
846
+ case 16:
835
847
  result1 = _h.sent();
836
848
  return [4 /*yield*/, this._emitOuput(mode, result1)];
837
- case 16:
849
+ case 17:
838
850
  _h.sent();
839
851
  return [2 /*return*/];
840
- case 17:
852
+ case 18:
841
853
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
842
854
  return [4 /*yield*/, this._emitOuput(mode, result)];
843
- case 18:
855
+ case 19:
844
856
  _h.sent();
845
857
  return [2 /*return*/];
846
858
  }
@@ -890,10 +902,10 @@ var AgentConnectionService = /** @class */ (function () {
890
902
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
891
903
  return "".concat(clientId, "-").concat(agentName);
892
904
  }, function (clientId, agentName) {
893
- 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;
894
906
  var completion = _this.completionSchemaService.get(completionName);
895
907
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
896
- 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));
897
909
  });
898
910
  /**
899
911
  * Executes an input command.
@@ -1114,20 +1126,21 @@ var ClientHistory = /** @class */ (function () {
1114
1126
  var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, promptMessages;
1115
1127
  var _this = this;
1116
1128
  var _d, e_2, _e, _f;
1117
- return __generator(this, function (_g) {
1118
- switch (_g.label) {
1129
+ var _g;
1130
+ return __generator(this, function (_h) {
1131
+ switch (_h.label) {
1119
1132
  case 0:
1120
1133
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " toArrayForAgent"));
1121
1134
  commonMessagesRaw = [];
1122
1135
  systemMessagesRaw = [];
1123
- _g.label = 1;
1136
+ _h.label = 1;
1124
1137
  case 1:
1125
- _g.trys.push([1, 6, 7, 12]);
1138
+ _h.trys.push([1, 6, 7, 12]);
1126
1139
  _a = true, _b = __asyncValues(this.params.items);
1127
- _g.label = 2;
1140
+ _h.label = 2;
1128
1141
  case 2: return [4 /*yield*/, _b.next()];
1129
1142
  case 3:
1130
- 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];
1131
1144
  _f = _c.value;
1132
1145
  _a = false;
1133
1146
  content = _f;
@@ -1148,22 +1161,22 @@ var ClientHistory = /** @class */ (function () {
1148
1161
  else {
1149
1162
  commonMessagesRaw.push(message);
1150
1163
  }
1151
- _g.label = 4;
1164
+ _h.label = 4;
1152
1165
  case 4:
1153
1166
  _a = true;
1154
1167
  return [3 /*break*/, 2];
1155
1168
  case 5: return [3 /*break*/, 12];
1156
1169
  case 6:
1157
- e_2_1 = _g.sent();
1170
+ e_2_1 = _h.sent();
1158
1171
  e_2 = { error: e_2_1 };
1159
1172
  return [3 /*break*/, 12];
1160
1173
  case 7:
1161
- _g.trys.push([7, , 10, 11]);
1174
+ _h.trys.push([7, , 10, 11]);
1162
1175
  if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
1163
1176
  return [4 /*yield*/, _e.call(_b)];
1164
1177
  case 8:
1165
- _g.sent();
1166
- _g.label = 9;
1178
+ _h.sent();
1179
+ _h.label = 9;
1167
1180
  case 9: return [3 /*break*/, 11];
1168
1181
  case 10:
1169
1182
  if (e_2) throw e_2.error;
@@ -1181,19 +1194,26 @@ var ClientHistory = /** @class */ (function () {
1181
1194
  {
1182
1195
  promptMessages.push({
1183
1196
  agentName: this.params.agentName,
1184
- mode: 'tool',
1197
+ mode: "tool",
1185
1198
  content: prompt,
1186
1199
  role: "system",
1187
1200
  });
1188
- system &&
1189
- system.forEach(function (content) {
1190
- return promptMessages.push({
1191
- agentName: _this.params.agentName,
1192
- mode: 'tool',
1193
- content: content,
1194
- role: "system",
1195
- });
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",
1196
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
+ });
1197
1217
  }
1198
1218
  return [2 /*return*/, __spreadArray(__spreadArray(__spreadArray([], __read(promptMessages), false), __read(systemMessages), false), __read(commonMessages), false)];
1199
1219
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.40",
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
@@ -671,7 +671,9 @@ interface IAgentSchema {
671
671
  */
672
672
  validate?: (output: string) => Promise<string | null>;
673
673
  /** The transform function for model output */
674
- 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;
675
677
  /** The lifecycle calbacks of the agent. */
676
678
  callbacks?: Partial<IAgentSchemaCallbacks>;
677
679
  }
@@ -2086,6 +2088,8 @@ declare const GLOBAL_CONFIG: {
2086
2088
  CC_AGENT_DEFAULT_VALIDATION: (output: string) => Promise<string | null>;
2087
2089
  CC_AGENT_HISTORY_FILTER: (agentName: AgentName) => (message: IModelMessage) => boolean;
2088
2090
  CC_AGENT_OUTPUT_TRANSFORM: (input: string) => string;
2091
+ CC_AGENT_OUTPUT_MAP: (message: IModelMessage) => IModelMessage | Promise<IModelMessage>;
2092
+ CC_AGENT_SYSTEM_PROMPT: string[];
2089
2093
  CC_AGENT_SEPARATE_HISTORY: boolean;
2090
2094
  CC_AGENT_DISALLOWED_TAGS: string[];
2091
2095
  CC_AGENT_DISALLOWED_SYMBOLS: string[];