agent-swarm-kit 1.0.57 → 1.0.59

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
@@ -460,6 +460,7 @@ var setConfig = function (config) {
460
460
  Object.assign(GLOBAL_CONFIG, config);
461
461
  };
462
462
 
463
+ var AGENT_CHANGE_SYMBOL = Symbol('agent-change');
463
464
  var getPlaceholder = function () {
464
465
  return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
465
466
  };
@@ -475,8 +476,9 @@ var ClientAgent = /** @class */ (function () {
475
476
  function ClientAgent(params) {
476
477
  var _this = this;
477
478
  this.params = params;
478
- this._toolErrorSubject = new functoolsKit.Subject();
479
+ this._agentChangeSubject = new functoolsKit.Subject();
479
480
  this._toolCommitSubject = new functoolsKit.Subject();
481
+ this._toolErrorSubject = new functoolsKit.Subject();
480
482
  this._outputSubject = new functoolsKit.Subject();
481
483
  /**
482
484
  * Emits the output result after validation.
@@ -677,6 +679,22 @@ var ClientAgent = /** @class */ (function () {
677
679
  }
678
680
  });
679
681
  }); };
682
+ /**
683
+ * Commits change of agent to prevent the next tool execution from being called.
684
+ * @returns {Promise<void>}
685
+ */
686
+ this.commitAgentChange = function () { return __awaiter(_this, void 0, void 0, function () {
687
+ return __generator(this, function (_a) {
688
+ switch (_a.label) {
689
+ case 0:
690
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitAgentChange"));
691
+ return [4 /*yield*/, this._agentChangeSubject.next(AGENT_CHANGE_SYMBOL)];
692
+ case 1:
693
+ _a.sent();
694
+ return [2 /*return*/];
695
+ }
696
+ });
697
+ }); };
680
698
  /**
681
699
  * Commits a system message to the history.
682
700
  * @param {string} message - The system message to commit.
@@ -737,9 +755,9 @@ var ClientAgent = /** @class */ (function () {
737
755
  this.execute = functoolsKit.queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
738
756
  var rawMessage, message, toolCalls, _loop_1, this_1, idx, state_1, result, validation, result1;
739
757
  var _this = this;
740
- var _a, _b, _c, _d, _e, _f;
741
- return __generator(this, function (_g) {
742
- switch (_g.label) {
758
+ var _a, _b, _c, _d, _e, _f, _g;
759
+ return __generator(this, function (_h) {
760
+ switch (_h.label) {
743
761
  case 0:
744
762
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
745
763
  this.params.onExecute &&
@@ -751,13 +769,13 @@ var ClientAgent = /** @class */ (function () {
751
769
  content: incoming.trim(),
752
770
  })];
753
771
  case 1:
754
- _g.sent();
772
+ _h.sent();
755
773
  return [4 /*yield*/, this.getCompletion(mode)];
756
774
  case 2:
757
- rawMessage = _g.sent();
775
+ rawMessage = _h.sent();
758
776
  return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
759
777
  case 3:
760
- message = _g.sent();
778
+ message = _h.sent();
761
779
  if (!message.tool_calls) return [3 /*break*/, 9];
762
780
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
763
781
  toolCalls = message.tool_calls.map(function (call) {
@@ -770,11 +788,11 @@ var ClientAgent = /** @class */ (function () {
770
788
  });
771
789
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
772
790
  case 4:
773
- _g.sent();
791
+ _h.sent();
774
792
  _loop_1 = function (idx) {
775
- var tool, targetFn, result_2, result_3;
776
- return __generator(this, function (_h) {
777
- switch (_h.label) {
793
+ var tool, targetFn, result_2, result_3, status;
794
+ return __generator(this, function (_j) {
795
+ switch (_j.label) {
778
796
  case 0:
779
797
  tool = toolCalls[idx];
780
798
  targetFn = (_a = this_1.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
@@ -782,11 +800,11 @@ var ClientAgent = /** @class */ (function () {
782
800
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool function not found"), this_1.params.tools);
783
801
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
784
802
  case 1:
785
- result_2 = _h.sent();
803
+ result_2 = _j.sent();
786
804
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
787
805
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
788
806
  case 2:
789
- _h.sent();
807
+ _j.sent();
790
808
  return [2 /*return*/, { value: void 0 }];
791
809
  case 3:
792
810
  ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
@@ -798,15 +816,15 @@ var ClientAgent = /** @class */ (function () {
798
816
  toolCalls: toolCalls,
799
817
  }))];
800
818
  case 4:
801
- if (!_h.sent()) return [3 /*break*/, 7];
819
+ if (!_j.sent()) return [3 /*break*/, 7];
802
820
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
803
821
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
804
822
  case 5:
805
- result_3 = _h.sent();
823
+ result_3 = _j.sent();
806
824
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
807
825
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
808
826
  case 6:
809
- _h.sent();
827
+ _j.sent();
810
828
  return [2 /*return*/, { value: void 0 }];
811
829
  case 7:
812
830
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
@@ -835,33 +853,40 @@ var ClientAgent = /** @class */ (function () {
835
853
  });
836
854
  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"));
837
855
  return [4 /*yield*/, Promise.race([
856
+ this_1._agentChangeSubject.toPromise(),
838
857
  this_1._toolCommitSubject.toPromise(),
839
858
  this_1._toolErrorSubject.toPromise(),
840
859
  this_1._outputSubject.toPromise(),
841
860
  ])];
842
861
  case 8:
843
- _h.sent();
862
+ status = _j.sent();
844
863
  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 end"));
864
+ if (status === AGENT_CHANGE_SYMBOL) {
865
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the agent changed"));
866
+ ((_f = this_1.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
867
+ this_1.params.callbacks.onAfterToolCalls(this_1.params.clientId, this_1.params.agentName, toolCalls);
868
+ return [2 /*return*/, { value: void 0 }];
869
+ }
845
870
  return [2 /*return*/];
846
871
  }
847
872
  });
848
873
  };
849
874
  this_1 = this;
850
875
  idx = 0;
851
- _g.label = 5;
876
+ _h.label = 5;
852
877
  case 5:
853
878
  if (!(idx !== toolCalls.length)) return [3 /*break*/, 8];
854
879
  return [5 /*yield**/, _loop_1(idx)];
855
880
  case 6:
856
- state_1 = _g.sent();
881
+ state_1 = _h.sent();
857
882
  if (typeof state_1 === "object")
858
883
  return [2 /*return*/, state_1.value];
859
- _g.label = 7;
884
+ _h.label = 7;
860
885
  case 7:
861
886
  idx++;
862
887
  return [3 /*break*/, 5];
863
888
  case 8:
864
- ((_f = this.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
889
+ ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
865
890
  this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
866
891
  return [2 /*return*/];
867
892
  case 9:
@@ -870,27 +895,27 @@ var ClientAgent = /** @class */ (function () {
870
895
  }
871
896
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
872
897
  case 10:
873
- result = _g.sent();
898
+ result = _h.sent();
874
899
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
875
900
  case 11:
876
- _g.sent();
901
+ _h.sent();
877
902
  validation = null;
878
903
  return [4 /*yield*/, this.params.validate(result)];
879
904
  case 12:
880
- if (!(validation = _g.sent())) return [3 /*break*/, 15];
905
+ if (!(validation = _h.sent())) return [3 /*break*/, 15];
881
906
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
882
907
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
883
908
  case 13:
884
- result1 = _g.sent();
909
+ result1 = _h.sent();
885
910
  return [4 /*yield*/, this._emitOuput(mode, result1)];
886
911
  case 14:
887
- _g.sent();
912
+ _h.sent();
888
913
  return [2 /*return*/];
889
914
  case 15:
890
915
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
891
916
  return [4 /*yield*/, this._emitOuput(mode, result)];
892
917
  case 16:
893
- _g.sent();
918
+ _h.sent();
894
919
  return [2 /*return*/];
895
920
  }
896
921
  });
@@ -926,6 +951,7 @@ var AgentConnectionService = /** @class */ (function () {
926
951
  this.contextService = inject(TYPES.contextService);
927
952
  this.sessionValidationService = inject(TYPES.sessionValidationService);
928
953
  this.historyConnectionService = inject(TYPES.historyConnectionService);
954
+ this.storageConnectionService = inject(TYPES.storageConnectionService);
929
955
  this.agentSchemaService = inject(TYPES.agentSchemaService);
930
956
  this.toolSchemaService = inject(TYPES.toolSchemaService);
931
957
  this.completionSchemaService = inject(TYPES.completionSchemaService);
@@ -939,9 +965,14 @@ var AgentConnectionService = /** @class */ (function () {
939
965
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
940
966
  return "".concat(clientId, "-").concat(agentName);
941
967
  }, function (clientId, agentName) {
942
- 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;
968
+ 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, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
943
969
  var completion = _this.completionSchemaService.get(completionName);
944
970
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
971
+ storages === null || storages === void 0 ? void 0 : storages.forEach(function (storageName) {
972
+ return _this.storageConnectionService
973
+ .getStorage(clientId, storageName)
974
+ .waitForInit();
975
+ });
945
976
  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));
946
977
  });
947
978
  /**
@@ -1035,6 +1066,22 @@ var AgentConnectionService = /** @class */ (function () {
1035
1066
  }
1036
1067
  });
1037
1068
  }); };
1069
+ /**
1070
+ * Commits agent change to prevent the next tool execution from being called.
1071
+ * @returns {Promise<any>} The commit result.
1072
+ */
1073
+ this.commitAgentChange = function () { return __awaiter(_this, void 0, void 0, function () {
1074
+ return __generator(this, function (_a) {
1075
+ switch (_a.label) {
1076
+ case 0:
1077
+ this.loggerService.log("agentConnectionService commitAgentChange", {
1078
+ context: this.contextService.context,
1079
+ });
1080
+ return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitAgentChange()];
1081
+ case 1: return [2 /*return*/, _a.sent()];
1082
+ }
1083
+ });
1084
+ }); };
1038
1085
  /**
1039
1086
  * Commits flush of agent history
1040
1087
  * @returns {Promise<any>} The commit result.
@@ -2484,6 +2531,38 @@ var AgentPublicService = /** @class */ (function () {
2484
2531
  }
2485
2532
  });
2486
2533
  }); };
2534
+ /**
2535
+ * Commits change of agent to prevent the next tool execution from being called.
2536
+ * @param {string} clientId - The client ID.
2537
+ * @param {AgentName} agentName - The name of the agent.
2538
+ * @returns {Promise<unknown>} The commit result.
2539
+ */
2540
+ this.commitAgentChange = function (clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
2541
+ var _this = this;
2542
+ return __generator(this, function (_a) {
2543
+ switch (_a.label) {
2544
+ case 0:
2545
+ this.loggerService.log("agentPublicService commitAgentChange", {
2546
+ clientId: clientId,
2547
+ agentName: agentName,
2548
+ });
2549
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
2550
+ return __generator(this, function (_a) {
2551
+ switch (_a.label) {
2552
+ case 0: return [4 /*yield*/, this.agentConnectionService.commitAgentChange()];
2553
+ case 1: return [2 /*return*/, _a.sent()];
2554
+ }
2555
+ });
2556
+ }); }, {
2557
+ clientId: clientId,
2558
+ agentName: agentName,
2559
+ swarmName: "",
2560
+ storageName: "",
2561
+ })];
2562
+ case 1: return [2 /*return*/, _a.sent()];
2563
+ }
2564
+ });
2565
+ }); };
2487
2566
  /**
2488
2567
  * Disposes of the agent.
2489
2568
  * @param {string} clientId - The client ID.
@@ -4845,22 +4924,36 @@ var createChangeAgent = functoolsKit.ttl(function (clientId) {
4845
4924
  swarm.sessionValidationService.validate(clientId, "changeAgent");
4846
4925
  swarm.agentValidationService.validate(agentName, "changeAgent");
4847
4926
  swarmName = swarm.sessionValidationService.getSwarm(clientId);
4848
- return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
4927
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
4928
+ .getAgentList(swarmName)
4929
+ .map(function (agentName) { return __awaiter(void 0, void 0, void 0, function () {
4930
+ return __generator(this, function (_a) {
4931
+ switch (_a.label) {
4932
+ case 0: return [4 /*yield*/, swarm.agentPublicService.commitAgentChange(clientId, agentName)];
4933
+ case 1:
4934
+ _a.sent();
4935
+ return [2 /*return*/];
4936
+ }
4937
+ });
4938
+ }); }))];
4849
4939
  case 1:
4850
4940
  _d.sent();
4851
- return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
4941
+ return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
4852
4942
  case 2:
4943
+ _d.sent();
4944
+ return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
4945
+ case 3:
4853
4946
  _d.sent();
4854
4947
  _b = (_a = swarm.swarmPublicService).setAgentRef;
4855
4948
  _c = [clientId,
4856
4949
  swarmName,
4857
4950
  agentName];
4858
4951
  return [4 /*yield*/, swarm.agentPublicService.createAgentRef(clientId, agentName)];
4859
- case 3: return [4 /*yield*/, _b.apply(_a, _c.concat([_d.sent()]))];
4860
- case 4:
4952
+ case 4: return [4 /*yield*/, _b.apply(_a, _c.concat([_d.sent()]))];
4953
+ case 5:
4861
4954
  _d.sent();
4862
4955
  return [4 /*yield*/, swarm.swarmPublicService.setAgentName(agentName, clientId, swarmName)];
4863
- case 5:
4956
+ case 6:
4864
4957
  _d.sent();
4865
4958
  return [2 /*return*/];
4866
4959
  }
@@ -4897,7 +4990,7 @@ var changeAgent = function (agentName, clientId) { return __awaiter(void 0, void
4897
4990
  return __generator(this, function (_a) {
4898
4991
  switch (_a.label) {
4899
4992
  case 0:
4900
- swarm.loggerService.log('function changeAgent', {
4993
+ swarm.loggerService.log("function changeAgent", {
4901
4994
  agentName: agentName,
4902
4995
  clientId: clientId,
4903
4996
  });
package/build/index.mjs CHANGED
@@ -458,6 +458,7 @@ var setConfig = function (config) {
458
458
  Object.assign(GLOBAL_CONFIG, config);
459
459
  };
460
460
 
461
+ var AGENT_CHANGE_SYMBOL = Symbol('agent-change');
461
462
  var getPlaceholder = function () {
462
463
  return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
463
464
  };
@@ -473,8 +474,9 @@ var ClientAgent = /** @class */ (function () {
473
474
  function ClientAgent(params) {
474
475
  var _this = this;
475
476
  this.params = params;
476
- this._toolErrorSubject = new Subject();
477
+ this._agentChangeSubject = new Subject();
477
478
  this._toolCommitSubject = new Subject();
479
+ this._toolErrorSubject = new Subject();
478
480
  this._outputSubject = new Subject();
479
481
  /**
480
482
  * Emits the output result after validation.
@@ -675,6 +677,22 @@ var ClientAgent = /** @class */ (function () {
675
677
  }
676
678
  });
677
679
  }); };
680
+ /**
681
+ * Commits change of agent to prevent the next tool execution from being called.
682
+ * @returns {Promise<void>}
683
+ */
684
+ this.commitAgentChange = function () { return __awaiter(_this, void 0, void 0, function () {
685
+ return __generator(this, function (_a) {
686
+ switch (_a.label) {
687
+ case 0:
688
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitAgentChange"));
689
+ return [4 /*yield*/, this._agentChangeSubject.next(AGENT_CHANGE_SYMBOL)];
690
+ case 1:
691
+ _a.sent();
692
+ return [2 /*return*/];
693
+ }
694
+ });
695
+ }); };
678
696
  /**
679
697
  * Commits a system message to the history.
680
698
  * @param {string} message - The system message to commit.
@@ -735,9 +753,9 @@ var ClientAgent = /** @class */ (function () {
735
753
  this.execute = queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
736
754
  var rawMessage, message, toolCalls, _loop_1, this_1, idx, state_1, result, validation, result1;
737
755
  var _this = this;
738
- var _a, _b, _c, _d, _e, _f;
739
- return __generator(this, function (_g) {
740
- switch (_g.label) {
756
+ var _a, _b, _c, _d, _e, _f, _g;
757
+ return __generator(this, function (_h) {
758
+ switch (_h.label) {
741
759
  case 0:
742
760
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
743
761
  this.params.onExecute &&
@@ -749,13 +767,13 @@ var ClientAgent = /** @class */ (function () {
749
767
  content: incoming.trim(),
750
768
  })];
751
769
  case 1:
752
- _g.sent();
770
+ _h.sent();
753
771
  return [4 /*yield*/, this.getCompletion(mode)];
754
772
  case 2:
755
- rawMessage = _g.sent();
773
+ rawMessage = _h.sent();
756
774
  return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
757
775
  case 3:
758
- message = _g.sent();
776
+ message = _h.sent();
759
777
  if (!message.tool_calls) return [3 /*break*/, 9];
760
778
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
761
779
  toolCalls = message.tool_calls.map(function (call) {
@@ -768,11 +786,11 @@ var ClientAgent = /** @class */ (function () {
768
786
  });
769
787
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
770
788
  case 4:
771
- _g.sent();
789
+ _h.sent();
772
790
  _loop_1 = function (idx) {
773
- var tool, targetFn, result_2, result_3;
774
- return __generator(this, function (_h) {
775
- switch (_h.label) {
791
+ var tool, targetFn, result_2, result_3, status;
792
+ return __generator(this, function (_j) {
793
+ switch (_j.label) {
776
794
  case 0:
777
795
  tool = toolCalls[idx];
778
796
  targetFn = (_a = this_1.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
@@ -780,11 +798,11 @@ var ClientAgent = /** @class */ (function () {
780
798
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool function not found"), this_1.params.tools);
781
799
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
782
800
  case 1:
783
- result_2 = _h.sent();
801
+ result_2 = _j.sent();
784
802
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
785
803
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
786
804
  case 2:
787
- _h.sent();
805
+ _j.sent();
788
806
  return [2 /*return*/, { value: void 0 }];
789
807
  case 3:
790
808
  ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
@@ -796,15 +814,15 @@ var ClientAgent = /** @class */ (function () {
796
814
  toolCalls: toolCalls,
797
815
  }))];
798
816
  case 4:
799
- if (!_h.sent()) return [3 /*break*/, 7];
817
+ if (!_j.sent()) return [3 /*break*/, 7];
800
818
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
801
819
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
802
820
  case 5:
803
- result_3 = _h.sent();
821
+ result_3 = _j.sent();
804
822
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
805
823
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
806
824
  case 6:
807
- _h.sent();
825
+ _j.sent();
808
826
  return [2 /*return*/, { value: void 0 }];
809
827
  case 7:
810
828
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
@@ -833,33 +851,40 @@ var ClientAgent = /** @class */ (function () {
833
851
  });
834
852
  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"));
835
853
  return [4 /*yield*/, Promise.race([
854
+ this_1._agentChangeSubject.toPromise(),
836
855
  this_1._toolCommitSubject.toPromise(),
837
856
  this_1._toolErrorSubject.toPromise(),
838
857
  this_1._outputSubject.toPromise(),
839
858
  ])];
840
859
  case 8:
841
- _h.sent();
860
+ status = _j.sent();
842
861
  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 end"));
862
+ if (status === AGENT_CHANGE_SYMBOL) {
863
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the agent changed"));
864
+ ((_f = this_1.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
865
+ this_1.params.callbacks.onAfterToolCalls(this_1.params.clientId, this_1.params.agentName, toolCalls);
866
+ return [2 /*return*/, { value: void 0 }];
867
+ }
843
868
  return [2 /*return*/];
844
869
  }
845
870
  });
846
871
  };
847
872
  this_1 = this;
848
873
  idx = 0;
849
- _g.label = 5;
874
+ _h.label = 5;
850
875
  case 5:
851
876
  if (!(idx !== toolCalls.length)) return [3 /*break*/, 8];
852
877
  return [5 /*yield**/, _loop_1(idx)];
853
878
  case 6:
854
- state_1 = _g.sent();
879
+ state_1 = _h.sent();
855
880
  if (typeof state_1 === "object")
856
881
  return [2 /*return*/, state_1.value];
857
- _g.label = 7;
882
+ _h.label = 7;
858
883
  case 7:
859
884
  idx++;
860
885
  return [3 /*break*/, 5];
861
886
  case 8:
862
- ((_f = this.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
887
+ ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
863
888
  this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
864
889
  return [2 /*return*/];
865
890
  case 9:
@@ -868,27 +893,27 @@ var ClientAgent = /** @class */ (function () {
868
893
  }
869
894
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
870
895
  case 10:
871
- result = _g.sent();
896
+ result = _h.sent();
872
897
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
873
898
  case 11:
874
- _g.sent();
899
+ _h.sent();
875
900
  validation = null;
876
901
  return [4 /*yield*/, this.params.validate(result)];
877
902
  case 12:
878
- if (!(validation = _g.sent())) return [3 /*break*/, 15];
903
+ if (!(validation = _h.sent())) return [3 /*break*/, 15];
879
904
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
880
905
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
881
906
  case 13:
882
- result1 = _g.sent();
907
+ result1 = _h.sent();
883
908
  return [4 /*yield*/, this._emitOuput(mode, result1)];
884
909
  case 14:
885
- _g.sent();
910
+ _h.sent();
886
911
  return [2 /*return*/];
887
912
  case 15:
888
913
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
889
914
  return [4 /*yield*/, this._emitOuput(mode, result)];
890
915
  case 16:
891
- _g.sent();
916
+ _h.sent();
892
917
  return [2 /*return*/];
893
918
  }
894
919
  });
@@ -924,6 +949,7 @@ var AgentConnectionService = /** @class */ (function () {
924
949
  this.contextService = inject(TYPES.contextService);
925
950
  this.sessionValidationService = inject(TYPES.sessionValidationService);
926
951
  this.historyConnectionService = inject(TYPES.historyConnectionService);
952
+ this.storageConnectionService = inject(TYPES.storageConnectionService);
927
953
  this.agentSchemaService = inject(TYPES.agentSchemaService);
928
954
  this.toolSchemaService = inject(TYPES.toolSchemaService);
929
955
  this.completionSchemaService = inject(TYPES.completionSchemaService);
@@ -937,9 +963,14 @@ var AgentConnectionService = /** @class */ (function () {
937
963
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
938
964
  return "".concat(clientId, "-").concat(agentName);
939
965
  }, function (clientId, agentName) {
940
- 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;
966
+ 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, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
941
967
  var completion = _this.completionSchemaService.get(completionName);
942
968
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
969
+ storages === null || storages === void 0 ? void 0 : storages.forEach(function (storageName) {
970
+ return _this.storageConnectionService
971
+ .getStorage(clientId, storageName)
972
+ .waitForInit();
973
+ });
943
974
  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));
944
975
  });
945
976
  /**
@@ -1033,6 +1064,22 @@ var AgentConnectionService = /** @class */ (function () {
1033
1064
  }
1034
1065
  });
1035
1066
  }); };
1067
+ /**
1068
+ * Commits agent change to prevent the next tool execution from being called.
1069
+ * @returns {Promise<any>} The commit result.
1070
+ */
1071
+ this.commitAgentChange = function () { return __awaiter(_this, void 0, void 0, function () {
1072
+ return __generator(this, function (_a) {
1073
+ switch (_a.label) {
1074
+ case 0:
1075
+ this.loggerService.log("agentConnectionService commitAgentChange", {
1076
+ context: this.contextService.context,
1077
+ });
1078
+ return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitAgentChange()];
1079
+ case 1: return [2 /*return*/, _a.sent()];
1080
+ }
1081
+ });
1082
+ }); };
1036
1083
  /**
1037
1084
  * Commits flush of agent history
1038
1085
  * @returns {Promise<any>} The commit result.
@@ -2482,6 +2529,38 @@ var AgentPublicService = /** @class */ (function () {
2482
2529
  }
2483
2530
  });
2484
2531
  }); };
2532
+ /**
2533
+ * Commits change of agent to prevent the next tool execution from being called.
2534
+ * @param {string} clientId - The client ID.
2535
+ * @param {AgentName} agentName - The name of the agent.
2536
+ * @returns {Promise<unknown>} The commit result.
2537
+ */
2538
+ this.commitAgentChange = function (clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
2539
+ var _this = this;
2540
+ return __generator(this, function (_a) {
2541
+ switch (_a.label) {
2542
+ case 0:
2543
+ this.loggerService.log("agentPublicService commitAgentChange", {
2544
+ clientId: clientId,
2545
+ agentName: agentName,
2546
+ });
2547
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
2548
+ return __generator(this, function (_a) {
2549
+ switch (_a.label) {
2550
+ case 0: return [4 /*yield*/, this.agentConnectionService.commitAgentChange()];
2551
+ case 1: return [2 /*return*/, _a.sent()];
2552
+ }
2553
+ });
2554
+ }); }, {
2555
+ clientId: clientId,
2556
+ agentName: agentName,
2557
+ swarmName: "",
2558
+ storageName: "",
2559
+ })];
2560
+ case 1: return [2 /*return*/, _a.sent()];
2561
+ }
2562
+ });
2563
+ }); };
2485
2564
  /**
2486
2565
  * Disposes of the agent.
2487
2566
  * @param {string} clientId - The client ID.
@@ -4843,22 +4922,36 @@ var createChangeAgent = ttl(function (clientId) {
4843
4922
  swarm.sessionValidationService.validate(clientId, "changeAgent");
4844
4923
  swarm.agentValidationService.validate(agentName, "changeAgent");
4845
4924
  swarmName = swarm.sessionValidationService.getSwarm(clientId);
4846
- return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
4925
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
4926
+ .getAgentList(swarmName)
4927
+ .map(function (agentName) { return __awaiter(void 0, void 0, void 0, function () {
4928
+ return __generator(this, function (_a) {
4929
+ switch (_a.label) {
4930
+ case 0: return [4 /*yield*/, swarm.agentPublicService.commitAgentChange(clientId, agentName)];
4931
+ case 1:
4932
+ _a.sent();
4933
+ return [2 /*return*/];
4934
+ }
4935
+ });
4936
+ }); }))];
4847
4937
  case 1:
4848
4938
  _d.sent();
4849
- return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
4939
+ return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
4850
4940
  case 2:
4941
+ _d.sent();
4942
+ return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
4943
+ case 3:
4851
4944
  _d.sent();
4852
4945
  _b = (_a = swarm.swarmPublicService).setAgentRef;
4853
4946
  _c = [clientId,
4854
4947
  swarmName,
4855
4948
  agentName];
4856
4949
  return [4 /*yield*/, swarm.agentPublicService.createAgentRef(clientId, agentName)];
4857
- case 3: return [4 /*yield*/, _b.apply(_a, _c.concat([_d.sent()]))];
4858
- case 4:
4950
+ case 4: return [4 /*yield*/, _b.apply(_a, _c.concat([_d.sent()]))];
4951
+ case 5:
4859
4952
  _d.sent();
4860
4953
  return [4 /*yield*/, swarm.swarmPublicService.setAgentName(agentName, clientId, swarmName)];
4861
- case 5:
4954
+ case 6:
4862
4955
  _d.sent();
4863
4956
  return [2 /*return*/];
4864
4957
  }
@@ -4895,7 +4988,7 @@ var changeAgent = function (agentName, clientId) { return __awaiter(void 0, void
4895
4988
  return __generator(this, function (_a) {
4896
4989
  switch (_a.label) {
4897
4990
  case 0:
4898
- swarm.loggerService.log('function changeAgent', {
4991
+ swarm.loggerService.log("function changeAgent", {
4899
4992
  agentName: agentName,
4900
4993
  clientId: clientId,
4901
4994
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
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
@@ -783,6 +783,11 @@ interface IAgent {
783
783
  * @returns A promise that resolves when the flush is committed.
784
784
  */
785
785
  commitFlush(): Promise<void>;
786
+ /**
787
+ * Unlock the queue on agent change. Stop the next tool execution
788
+ * @returns A promise that resolves when the agent change is committed.
789
+ */
790
+ commitAgentChange(): Promise<void>;
786
791
  }
787
792
  /** Type representing the name of an agent. */
788
793
  type AgentName = string;
@@ -832,14 +837,16 @@ declare class LoggerService implements ILogger {
832
837
  setLogger: (logger: ILogger) => void;
833
838
  }
834
839
 
840
+ declare const AGENT_CHANGE_SYMBOL: unique symbol;
835
841
  /**
836
842
  * Represents a client agent that interacts with the system.
837
843
  * @implements {IAgent}
838
844
  */
839
845
  declare class ClientAgent implements IAgent {
840
846
  readonly params: IAgentParams;
841
- readonly _toolErrorSubject: Subject<void>;
847
+ readonly _agentChangeSubject: Subject<typeof AGENT_CHANGE_SYMBOL>;
842
848
  readonly _toolCommitSubject: Subject<void>;
849
+ readonly _toolErrorSubject: Subject<void>;
843
850
  readonly _outputSubject: Subject<string>;
844
851
  /**
845
852
  * Creates an instance of ClientAgent.
@@ -881,6 +888,11 @@ declare class ClientAgent implements IAgent {
881
888
  * @returns {Promise<void>}
882
889
  */
883
890
  commitFlush: () => Promise<void>;
891
+ /**
892
+ * Commits change of agent to prevent the next tool execution from being called.
893
+ * @returns {Promise<void>}
894
+ */
895
+ commitAgentChange: () => Promise<void>;
884
896
  /**
885
897
  * Commits a system message to the history.
886
898
  * @param {string} message - The system message to commit.
@@ -915,6 +927,7 @@ declare class AgentConnectionService implements IAgent {
915
927
  private readonly contextService;
916
928
  private readonly sessionValidationService;
917
929
  private readonly historyConnectionService;
930
+ private readonly storageConnectionService;
918
931
  private readonly agentSchemaService;
919
932
  private readonly toolSchemaService;
920
933
  private readonly completionSchemaService;
@@ -955,6 +968,11 @@ declare class AgentConnectionService implements IAgent {
955
968
  * @returns {Promise<any>} The commit result.
956
969
  */
957
970
  commitUserMessage: (message: string) => Promise<void>;
971
+ /**
972
+ * Commits agent change to prevent the next tool execution from being called.
973
+ * @returns {Promise<any>} The commit result.
974
+ */
975
+ commitAgentChange: () => Promise<void>;
958
976
  /**
959
977
  * Commits flush of agent history
960
978
  * @returns {Promise<any>} The commit result.
@@ -1417,6 +1435,13 @@ declare class AgentPublicService implements TAgentConnectionService {
1417
1435
  * @returns {Promise<unknown>} The commit result.
1418
1436
  */
1419
1437
  commitFlush: (clientId: string, agentName: AgentName) => Promise<void>;
1438
+ /**
1439
+ * Commits change of agent to prevent the next tool execution from being called.
1440
+ * @param {string} clientId - The client ID.
1441
+ * @param {AgentName} agentName - The name of the agent.
1442
+ * @returns {Promise<unknown>} The commit result.
1443
+ */
1444
+ commitAgentChange: (clientId: string, agentName: AgentName) => Promise<void>;
1420
1445
  /**
1421
1446
  * Disposes of the agent.
1422
1447
  * @param {string} clientId - The client ID.