agent-swarm-kit 1.0.33 → 1.0.34

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
@@ -548,28 +548,28 @@ var ClientAgent = /** @class */ (function () {
548
548
  */
549
549
  this.getCompletion = function (mode) { return __awaiter(_this, void 0, void 0, function () {
550
550
  var messages, args, output;
551
- var _a;
552
- return __generator(this, function (_b) {
553
- switch (_b.label) {
551
+ var _a, _b, _c;
552
+ return __generator(this, function (_d) {
553
+ switch (_d.label) {
554
554
  case 0:
555
555
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
556
556
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
557
557
  case 1:
558
- messages = _b.sent();
558
+ messages = _d.sent();
559
559
  args = {
560
560
  clientId: this.params.clientId,
561
561
  agentName: this.params.agentName,
562
562
  messages: messages,
563
563
  mode: mode,
564
564
  tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
565
- return lodashEs.omit(t, "toolName", "call", "validate", "onCall", "onValidate");
565
+ return lodashEs.omit(t, "toolName", "call", "validate", "callbacks");
566
566
  }),
567
567
  };
568
568
  return [4 /*yield*/, this.params.completion.getCompletion(args)];
569
569
  case 2:
570
- output = _b.sent();
571
- this.params.completion.onComplete &&
572
- this.params.completion.onComplete(args, output);
570
+ output = _d.sent();
571
+ ((_b = this.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
572
+ ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, output));
573
573
  return [2 /*return*/, output];
574
574
  }
575
575
  });
@@ -680,9 +680,9 @@ var ClientAgent = /** @class */ (function () {
680
680
  this.execute = functoolsKit.queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
681
681
  var message, _loop_1, this_1, _a, _b, tool, state_1, e_1_1, result, validation, result1;
682
682
  var e_1, _c;
683
- var _d;
684
- return __generator(this, function (_e) {
685
- switch (_e.label) {
683
+ var _d, _e, _f, _g, _h;
684
+ return __generator(this, function (_j) {
685
+ switch (_j.label) {
686
686
  case 0:
687
687
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
688
688
  this.params.onExecute &&
@@ -694,48 +694,49 @@ var ClientAgent = /** @class */ (function () {
694
694
  content: incoming.trim(),
695
695
  })];
696
696
  case 1:
697
- _e.sent();
697
+ _j.sent();
698
698
  return [4 /*yield*/, this.getCompletion(mode)];
699
699
  case 2:
700
- message = _e.sent();
700
+ message = _j.sent();
701
701
  if (!message.tool_calls) return [3 /*break*/, 11];
702
702
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
703
703
  _loop_1 = function (tool) {
704
704
  var targetFn, result_2, result_3;
705
- return __generator(this, function (_f) {
706
- switch (_f.label) {
705
+ return __generator(this, function (_k) {
706
+ switch (_k.label) {
707
707
  case 0:
708
708
  targetFn = (_d = this_1.params.tools) === null || _d === void 0 ? void 0 : _d.find(function (t) { return t.function.name === tool.function.name; });
709
709
  return [4 /*yield*/, this_1.params.history.push(__assign(__assign({}, message), { agentName: this_1.params.agentName }))];
710
710
  case 1:
711
- _f.sent();
711
+ _k.sent();
712
712
  if (!!targetFn) return [3 /*break*/, 4];
713
713
  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);
714
714
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
715
715
  case 2:
716
- result_2 = _f.sent();
716
+ result_2 = _k.sent();
717
717
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
718
718
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
719
719
  case 3:
720
- _f.sent();
720
+ _k.sent();
721
721
  return [2 /*return*/, { value: void 0 }];
722
722
  case 4:
723
- targetFn.onValidate && targetFn.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
723
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onValidate) &&
724
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
724
725
  return [4 /*yield*/, functoolsKit.not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
725
726
  case 5:
726
- if (!_f.sent()) return [3 /*break*/, 8];
727
+ if (!_k.sent()) return [3 /*break*/, 8];
727
728
  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"));
728
729
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
729
730
  case 6:
730
- result_3 = _f.sent();
731
+ result_3 = _k.sent();
731
732
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
732
733
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
733
734
  case 7:
734
- _f.sent();
735
+ _k.sent();
735
736
  return [2 /*return*/, { value: void 0 }];
736
737
  case 8:
737
- targetFn.onCall &&
738
- targetFn.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
738
+ ((_g = targetFn.callbacks) === null || _g === void 0 ? void 0 : _g.onCall) &&
739
+ ((_h = targetFn.callbacks) === null || _h === void 0 ? void 0 : _h.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
739
740
  /**
740
741
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
741
742
  */
@@ -746,33 +747,33 @@ var ClientAgent = /** @class */ (function () {
746
747
  this_1._outputSubject.toPromise(),
747
748
  ])];
748
749
  case 9:
749
- _f.sent();
750
+ _k.sent();
750
751
  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"));
751
752
  return [2 /*return*/];
752
753
  }
753
754
  });
754
755
  };
755
756
  this_1 = this;
756
- _e.label = 3;
757
+ _j.label = 3;
757
758
  case 3:
758
- _e.trys.push([3, 8, 9, 10]);
759
+ _j.trys.push([3, 8, 9, 10]);
759
760
  _a = __values(message.tool_calls), _b = _a.next();
760
- _e.label = 4;
761
+ _j.label = 4;
761
762
  case 4:
762
763
  if (!!_b.done) return [3 /*break*/, 7];
763
764
  tool = _b.value;
764
765
  return [5 /*yield**/, _loop_1(tool)];
765
766
  case 5:
766
- state_1 = _e.sent();
767
+ state_1 = _j.sent();
767
768
  if (typeof state_1 === "object")
768
769
  return [2 /*return*/, state_1.value];
769
- _e.label = 6;
770
+ _j.label = 6;
770
771
  case 6:
771
772
  _b = _a.next();
772
773
  return [3 /*break*/, 4];
773
774
  case 7: return [3 /*break*/, 10];
774
775
  case 8:
775
- e_1_1 = _e.sent();
776
+ e_1_1 = _j.sent();
776
777
  e_1 = { error: e_1_1 };
777
778
  return [3 /*break*/, 10];
778
779
  case 9:
@@ -789,31 +790,44 @@ var ClientAgent = /** @class */ (function () {
789
790
  result = message.content;
790
791
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
791
792
  case 12:
792
- _e.sent();
793
+ _j.sent();
793
794
  validation = null;
794
795
  return [4 /*yield*/, this.params.validate(result)];
795
796
  case 13:
796
- if (!(validation = _e.sent())) return [3 /*break*/, 16];
797
+ if (!(validation = _j.sent())) return [3 /*break*/, 16];
797
798
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
798
799
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
799
800
  case 14:
800
- result1 = _e.sent();
801
+ result1 = _j.sent();
801
802
  return [4 /*yield*/, this._emitOuput(mode, result1)];
802
803
  case 15:
803
- _e.sent();
804
+ _j.sent();
804
805
  return [2 /*return*/];
805
806
  case 16:
806
807
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
807
808
  return [4 /*yield*/, this._emitOuput(mode, result)];
808
809
  case 17:
809
- _e.sent();
810
+ _j.sent();
810
811
  return [2 /*return*/];
811
812
  }
812
813
  });
813
814
  }); });
815
+ /**
816
+ * Should call on agent dispose
817
+ * @returns {Promise<void>}
818
+ */
819
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
820
+ return __generator(this, function (_a) {
821
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " dispose"));
822
+ this.params.onDispose &&
823
+ this.params.onDispose(this.params.clientId, this.params.agentName);
824
+ return [2 /*return*/];
825
+ });
826
+ }); };
814
827
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
815
828
  params: params,
816
829
  });
830
+ this.params.onInit && this.params.onInit(params.clientId, params.agentName);
817
831
  }
818
832
  return ClientAgent;
819
833
  }());
@@ -842,26 +856,10 @@ var AgentConnectionService = /** @class */ (function () {
842
856
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
843
857
  return "".concat(clientId, "-").concat(agentName);
844
858
  }, function (clientId, agentName) {
845
- var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, onExecute = _a.onExecute, onOutput = _a.onOutput, onResurrect = _a.onResurrect, onSystemMessage = _a.onSystemMessage, onToolOutput = _a.onToolOutput, onUserMessage = _a.onUserMessage, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
859
+ var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, callbacks = _a.callbacks, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
846
860
  var completion = _this.completionSchemaService.get(completionName);
847
861
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
848
- return new ClientAgent({
849
- clientId: clientId,
850
- agentName: agentName,
851
- validate: validate,
852
- logger: _this.loggerService,
853
- history: _this.historyConnectionService.getHistory(clientId, agentName),
854
- prompt: prompt,
855
- system: system,
856
- tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get),
857
- completion: completion,
858
- onExecute: onExecute,
859
- onOutput: onOutput,
860
- onResurrect: onResurrect,
861
- onSystemMessage: onSystemMessage,
862
- onToolOutput: onToolOutput,
863
- onUserMessage: onUserMessage,
864
- });
862
+ return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
865
863
  });
866
864
  /**
867
865
  * Executes an input command.
@@ -974,12 +972,18 @@ var AgentConnectionService = /** @class */ (function () {
974
972
  */
975
973
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
976
974
  return __generator(this, function (_a) {
977
- this.loggerService.log("agentConnectionService dispose", {
978
- context: this.contextService.context,
979
- });
980
- this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
981
- this.sessionValidationService.removeAgentUsage(this.contextService.context.clientId, this.contextService.context.agentName);
982
- return [2 /*return*/];
975
+ switch (_a.label) {
976
+ case 0:
977
+ this.loggerService.log("agentConnectionService dispose", {
978
+ context: this.contextService.context,
979
+ });
980
+ return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).dispose()];
981
+ case 1:
982
+ _a.sent();
983
+ this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
984
+ this.sessionValidationService.removeAgentUsage(this.contextService.context.clientId, this.contextService.context.agentName);
985
+ return [2 /*return*/];
986
+ }
983
987
  });
984
988
  }); };
985
989
  }
@@ -1491,7 +1495,7 @@ var SwarmConnectionService = /** @class */ (function () {
1491
1495
  return "".concat(clientId, "-").concat(swarmName);
1492
1496
  }, function (clientId, swarmName) {
1493
1497
  var e_1, _a;
1494
- var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent, onAgentChanged = _b.onAgentChanged;
1498
+ var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent, callbacks = _b.callbacks;
1495
1499
  var agentMap = {};
1496
1500
  try {
1497
1501
  for (var agentList_1 = __values(agentList), agentList_1_1 = agentList_1.next(); !agentList_1_1.done; agentList_1_1 = agentList_1.next()) {
@@ -1517,7 +1521,9 @@ var SwarmConnectionService = /** @class */ (function () {
1517
1521
  return __generator(this, function (_a) {
1518
1522
  switch (_a.label) {
1519
1523
  case 0:
1520
- onAgentChanged && onAgentChanged(clientId, agentName, swarmName);
1524
+ if (callbacks && callbacks.onAgentChanged) {
1525
+ callbacks.onAgentChanged(clientId, agentName, swarmName);
1526
+ }
1521
1527
  return [4 /*yield*/, GLOBAL_CONFIG.CC_SWARM_AGENT_CHANGED(clientId, agentName, swarmName)];
1522
1528
  case 1:
1523
1529
  _a.sent();
@@ -1882,9 +1888,22 @@ var ClientSession = /** @class */ (function () {
1882
1888
  });
1883
1889
  }); };
1884
1890
  };
1891
+ /**
1892
+ * Should call on session dispose
1893
+ * @returns {Promise<void>}
1894
+ */
1895
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
1896
+ return __generator(this, function (_a) {
1897
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " dispose"));
1898
+ this.params.onDispose &&
1899
+ this.params.onDispose(this.params.clientId, this.params.swarmName);
1900
+ return [2 /*return*/];
1901
+ });
1902
+ }); };
1885
1903
  this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " CTOR"), {
1886
1904
  params: params,
1887
1905
  });
1906
+ this.params.onInit && this.params.onInit(params.clientId, params.swarmName);
1888
1907
  }
1889
1908
  return ClientSession;
1890
1909
  }());
@@ -1910,16 +1929,8 @@ var SessionConnectionService = /** @class */ (function () {
1910
1929
  var _b = __read(_a, 2), clientId = _b[0], swarmName = _b[1];
1911
1930
  return "".concat(clientId, "-").concat(swarmName);
1912
1931
  }, function (clientId, swarmName) {
1913
- var _a = _this.swarmSchemaService.get(swarmName), onConnect = _a.onConnect, onEmit = _a.onEmit, onExecute = _a.onExecute;
1914
- return new ClientSession({
1915
- clientId: clientId,
1916
- logger: _this.loggerService,
1917
- swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName),
1918
- swarmName: swarmName,
1919
- onConnect: onConnect,
1920
- onEmit: onEmit,
1921
- onExecute: onExecute,
1922
- });
1932
+ var callbacks = _this.swarmSchemaService.get(swarmName).callbacks;
1933
+ return new ClientSession(__assign({ clientId: clientId, logger: _this.loggerService, swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName), swarmName: swarmName }, callbacks));
1923
1934
  });
1924
1935
  /**
1925
1936
  * Emits a message to the session.
@@ -2045,11 +2056,17 @@ var SessionConnectionService = /** @class */ (function () {
2045
2056
  */
2046
2057
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
2047
2058
  return __generator(this, function (_a) {
2048
- this.loggerService.log("sessionConnectionService dispose", {
2049
- context: this.contextService.context,
2050
- });
2051
- this.getSession.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
2052
- return [2 /*return*/];
2059
+ switch (_a.label) {
2060
+ case 0:
2061
+ this.loggerService.log("sessionConnectionService dispose", {
2062
+ context: this.contextService.context,
2063
+ });
2064
+ return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).dispose()];
2065
+ case 1:
2066
+ _a.sent();
2067
+ this.getSession.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
2068
+ return [2 /*return*/];
2069
+ }
2053
2070
  });
2054
2071
  }); };
2055
2072
  }
package/build/index.mjs CHANGED
@@ -546,28 +546,28 @@ var ClientAgent = /** @class */ (function () {
546
546
  */
547
547
  this.getCompletion = function (mode) { return __awaiter(_this, void 0, void 0, function () {
548
548
  var messages, args, output;
549
- var _a;
550
- return __generator(this, function (_b) {
551
- switch (_b.label) {
549
+ var _a, _b, _c;
550
+ return __generator(this, function (_d) {
551
+ switch (_d.label) {
552
552
  case 0:
553
553
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
554
554
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
555
555
  case 1:
556
- messages = _b.sent();
556
+ messages = _d.sent();
557
557
  args = {
558
558
  clientId: this.params.clientId,
559
559
  agentName: this.params.agentName,
560
560
  messages: messages,
561
561
  mode: mode,
562
562
  tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
563
- return omit(t, "toolName", "call", "validate", "onCall", "onValidate");
563
+ return omit(t, "toolName", "call", "validate", "callbacks");
564
564
  }),
565
565
  };
566
566
  return [4 /*yield*/, this.params.completion.getCompletion(args)];
567
567
  case 2:
568
- output = _b.sent();
569
- this.params.completion.onComplete &&
570
- this.params.completion.onComplete(args, output);
568
+ output = _d.sent();
569
+ ((_b = this.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
570
+ ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, output));
571
571
  return [2 /*return*/, output];
572
572
  }
573
573
  });
@@ -678,9 +678,9 @@ var ClientAgent = /** @class */ (function () {
678
678
  this.execute = queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
679
679
  var message, _loop_1, this_1, _a, _b, tool, state_1, e_1_1, result, validation, result1;
680
680
  var e_1, _c;
681
- var _d;
682
- return __generator(this, function (_e) {
683
- switch (_e.label) {
681
+ var _d, _e, _f, _g, _h;
682
+ return __generator(this, function (_j) {
683
+ switch (_j.label) {
684
684
  case 0:
685
685
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
686
686
  this.params.onExecute &&
@@ -692,48 +692,49 @@ var ClientAgent = /** @class */ (function () {
692
692
  content: incoming.trim(),
693
693
  })];
694
694
  case 1:
695
- _e.sent();
695
+ _j.sent();
696
696
  return [4 /*yield*/, this.getCompletion(mode)];
697
697
  case 2:
698
- message = _e.sent();
698
+ message = _j.sent();
699
699
  if (!message.tool_calls) return [3 /*break*/, 11];
700
700
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
701
701
  _loop_1 = function (tool) {
702
702
  var targetFn, result_2, result_3;
703
- return __generator(this, function (_f) {
704
- switch (_f.label) {
703
+ return __generator(this, function (_k) {
704
+ switch (_k.label) {
705
705
  case 0:
706
706
  targetFn = (_d = this_1.params.tools) === null || _d === void 0 ? void 0 : _d.find(function (t) { return t.function.name === tool.function.name; });
707
707
  return [4 /*yield*/, this_1.params.history.push(__assign(__assign({}, message), { agentName: this_1.params.agentName }))];
708
708
  case 1:
709
- _f.sent();
709
+ _k.sent();
710
710
  if (!!targetFn) return [3 /*break*/, 4];
711
711
  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);
712
712
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
713
713
  case 2:
714
- result_2 = _f.sent();
714
+ result_2 = _k.sent();
715
715
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
716
716
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
717
717
  case 3:
718
- _f.sent();
718
+ _k.sent();
719
719
  return [2 /*return*/, { value: void 0 }];
720
720
  case 4:
721
- targetFn.onValidate && targetFn.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
721
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onValidate) &&
722
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
722
723
  return [4 /*yield*/, not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
723
724
  case 5:
724
- if (!_f.sent()) return [3 /*break*/, 8];
725
+ if (!_k.sent()) return [3 /*break*/, 8];
725
726
  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"));
726
727
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
727
728
  case 6:
728
- result_3 = _f.sent();
729
+ result_3 = _k.sent();
729
730
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
730
731
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
731
732
  case 7:
732
- _f.sent();
733
+ _k.sent();
733
734
  return [2 /*return*/, { value: void 0 }];
734
735
  case 8:
735
- targetFn.onCall &&
736
- targetFn.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
736
+ ((_g = targetFn.callbacks) === null || _g === void 0 ? void 0 : _g.onCall) &&
737
+ ((_h = targetFn.callbacks) === null || _h === void 0 ? void 0 : _h.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
737
738
  /**
738
739
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
739
740
  */
@@ -744,33 +745,33 @@ var ClientAgent = /** @class */ (function () {
744
745
  this_1._outputSubject.toPromise(),
745
746
  ])];
746
747
  case 9:
747
- _f.sent();
748
+ _k.sent();
748
749
  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"));
749
750
  return [2 /*return*/];
750
751
  }
751
752
  });
752
753
  };
753
754
  this_1 = this;
754
- _e.label = 3;
755
+ _j.label = 3;
755
756
  case 3:
756
- _e.trys.push([3, 8, 9, 10]);
757
+ _j.trys.push([3, 8, 9, 10]);
757
758
  _a = __values(message.tool_calls), _b = _a.next();
758
- _e.label = 4;
759
+ _j.label = 4;
759
760
  case 4:
760
761
  if (!!_b.done) return [3 /*break*/, 7];
761
762
  tool = _b.value;
762
763
  return [5 /*yield**/, _loop_1(tool)];
763
764
  case 5:
764
- state_1 = _e.sent();
765
+ state_1 = _j.sent();
765
766
  if (typeof state_1 === "object")
766
767
  return [2 /*return*/, state_1.value];
767
- _e.label = 6;
768
+ _j.label = 6;
768
769
  case 6:
769
770
  _b = _a.next();
770
771
  return [3 /*break*/, 4];
771
772
  case 7: return [3 /*break*/, 10];
772
773
  case 8:
773
- e_1_1 = _e.sent();
774
+ e_1_1 = _j.sent();
774
775
  e_1 = { error: e_1_1 };
775
776
  return [3 /*break*/, 10];
776
777
  case 9:
@@ -787,31 +788,44 @@ var ClientAgent = /** @class */ (function () {
787
788
  result = message.content;
788
789
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
789
790
  case 12:
790
- _e.sent();
791
+ _j.sent();
791
792
  validation = null;
792
793
  return [4 /*yield*/, this.params.validate(result)];
793
794
  case 13:
794
- if (!(validation = _e.sent())) return [3 /*break*/, 16];
795
+ if (!(validation = _j.sent())) return [3 /*break*/, 16];
795
796
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
796
797
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
797
798
  case 14:
798
- result1 = _e.sent();
799
+ result1 = _j.sent();
799
800
  return [4 /*yield*/, this._emitOuput(mode, result1)];
800
801
  case 15:
801
- _e.sent();
802
+ _j.sent();
802
803
  return [2 /*return*/];
803
804
  case 16:
804
805
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
805
806
  return [4 /*yield*/, this._emitOuput(mode, result)];
806
807
  case 17:
807
- _e.sent();
808
+ _j.sent();
808
809
  return [2 /*return*/];
809
810
  }
810
811
  });
811
812
  }); });
813
+ /**
814
+ * Should call on agent dispose
815
+ * @returns {Promise<void>}
816
+ */
817
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
818
+ return __generator(this, function (_a) {
819
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " dispose"));
820
+ this.params.onDispose &&
821
+ this.params.onDispose(this.params.clientId, this.params.agentName);
822
+ return [2 /*return*/];
823
+ });
824
+ }); };
812
825
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
813
826
  params: params,
814
827
  });
828
+ this.params.onInit && this.params.onInit(params.clientId, params.agentName);
815
829
  }
816
830
  return ClientAgent;
817
831
  }());
@@ -840,26 +854,10 @@ var AgentConnectionService = /** @class */ (function () {
840
854
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
841
855
  return "".concat(clientId, "-").concat(agentName);
842
856
  }, function (clientId, agentName) {
843
- var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, onExecute = _a.onExecute, onOutput = _a.onOutput, onResurrect = _a.onResurrect, onSystemMessage = _a.onSystemMessage, onToolOutput = _a.onToolOutput, onUserMessage = _a.onUserMessage, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
857
+ var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, callbacks = _a.callbacks, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
844
858
  var completion = _this.completionSchemaService.get(completionName);
845
859
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
846
- return new ClientAgent({
847
- clientId: clientId,
848
- agentName: agentName,
849
- validate: validate,
850
- logger: _this.loggerService,
851
- history: _this.historyConnectionService.getHistory(clientId, agentName),
852
- prompt: prompt,
853
- system: system,
854
- tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get),
855
- completion: completion,
856
- onExecute: onExecute,
857
- onOutput: onOutput,
858
- onResurrect: onResurrect,
859
- onSystemMessage: onSystemMessage,
860
- onToolOutput: onToolOutput,
861
- onUserMessage: onUserMessage,
862
- });
860
+ return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
863
861
  });
864
862
  /**
865
863
  * Executes an input command.
@@ -972,12 +970,18 @@ var AgentConnectionService = /** @class */ (function () {
972
970
  */
973
971
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
974
972
  return __generator(this, function (_a) {
975
- this.loggerService.log("agentConnectionService dispose", {
976
- context: this.contextService.context,
977
- });
978
- this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
979
- this.sessionValidationService.removeAgentUsage(this.contextService.context.clientId, this.contextService.context.agentName);
980
- return [2 /*return*/];
973
+ switch (_a.label) {
974
+ case 0:
975
+ this.loggerService.log("agentConnectionService dispose", {
976
+ context: this.contextService.context,
977
+ });
978
+ return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).dispose()];
979
+ case 1:
980
+ _a.sent();
981
+ this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
982
+ this.sessionValidationService.removeAgentUsage(this.contextService.context.clientId, this.contextService.context.agentName);
983
+ return [2 /*return*/];
984
+ }
981
985
  });
982
986
  }); };
983
987
  }
@@ -1489,7 +1493,7 @@ var SwarmConnectionService = /** @class */ (function () {
1489
1493
  return "".concat(clientId, "-").concat(swarmName);
1490
1494
  }, function (clientId, swarmName) {
1491
1495
  var e_1, _a;
1492
- var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent, onAgentChanged = _b.onAgentChanged;
1496
+ var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent, callbacks = _b.callbacks;
1493
1497
  var agentMap = {};
1494
1498
  try {
1495
1499
  for (var agentList_1 = __values(agentList), agentList_1_1 = agentList_1.next(); !agentList_1_1.done; agentList_1_1 = agentList_1.next()) {
@@ -1515,7 +1519,9 @@ var SwarmConnectionService = /** @class */ (function () {
1515
1519
  return __generator(this, function (_a) {
1516
1520
  switch (_a.label) {
1517
1521
  case 0:
1518
- onAgentChanged && onAgentChanged(clientId, agentName, swarmName);
1522
+ if (callbacks && callbacks.onAgentChanged) {
1523
+ callbacks.onAgentChanged(clientId, agentName, swarmName);
1524
+ }
1519
1525
  return [4 /*yield*/, GLOBAL_CONFIG.CC_SWARM_AGENT_CHANGED(clientId, agentName, swarmName)];
1520
1526
  case 1:
1521
1527
  _a.sent();
@@ -1880,9 +1886,22 @@ var ClientSession = /** @class */ (function () {
1880
1886
  });
1881
1887
  }); };
1882
1888
  };
1889
+ /**
1890
+ * Should call on session dispose
1891
+ * @returns {Promise<void>}
1892
+ */
1893
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
1894
+ return __generator(this, function (_a) {
1895
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " dispose"));
1896
+ this.params.onDispose &&
1897
+ this.params.onDispose(this.params.clientId, this.params.swarmName);
1898
+ return [2 /*return*/];
1899
+ });
1900
+ }); };
1883
1901
  this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " CTOR"), {
1884
1902
  params: params,
1885
1903
  });
1904
+ this.params.onInit && this.params.onInit(params.clientId, params.swarmName);
1886
1905
  }
1887
1906
  return ClientSession;
1888
1907
  }());
@@ -1908,16 +1927,8 @@ var SessionConnectionService = /** @class */ (function () {
1908
1927
  var _b = __read(_a, 2), clientId = _b[0], swarmName = _b[1];
1909
1928
  return "".concat(clientId, "-").concat(swarmName);
1910
1929
  }, function (clientId, swarmName) {
1911
- var _a = _this.swarmSchemaService.get(swarmName), onConnect = _a.onConnect, onEmit = _a.onEmit, onExecute = _a.onExecute;
1912
- return new ClientSession({
1913
- clientId: clientId,
1914
- logger: _this.loggerService,
1915
- swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName),
1916
- swarmName: swarmName,
1917
- onConnect: onConnect,
1918
- onEmit: onEmit,
1919
- onExecute: onExecute,
1920
- });
1930
+ var callbacks = _this.swarmSchemaService.get(swarmName).callbacks;
1931
+ return new ClientSession(__assign({ clientId: clientId, logger: _this.loggerService, swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName), swarmName: swarmName }, callbacks));
1921
1932
  });
1922
1933
  /**
1923
1934
  * Emits a message to the session.
@@ -2043,11 +2054,17 @@ var SessionConnectionService = /** @class */ (function () {
2043
2054
  */
2044
2055
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
2045
2056
  return __generator(this, function (_a) {
2046
- this.loggerService.log("sessionConnectionService dispose", {
2047
- context: this.contextService.context,
2048
- });
2049
- this.getSession.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
2050
- return [2 /*return*/];
2057
+ switch (_a.label) {
2058
+ case 0:
2059
+ this.loggerService.log("sessionConnectionService dispose", {
2060
+ context: this.contextService.context,
2061
+ });
2062
+ return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).dispose()];
2063
+ case 1:
2064
+ _a.sent();
2065
+ this.getSession.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
2066
+ return [2 /*return*/];
2067
+ }
2051
2068
  });
2052
2069
  }); };
2053
2070
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
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
@@ -53,7 +53,7 @@ interface ILogger {
53
53
  debug(...args: any[]): void;
54
54
  }
55
55
 
56
- interface ISwarmSession {
56
+ interface ISwarmSessionCallbacks {
57
57
  /**
58
58
  * Callback triggered when a client connects.
59
59
  * @param clientId - The ID of the client.
@@ -75,6 +75,25 @@ interface ISwarmSession {
75
75
  * @param message - The message to emit.
76
76
  */
77
77
  onEmit?: (clientId: string, swarmName: SwarmName, message: string) => void;
78
+ /**
79
+ * Callback triggered when a session being connected
80
+ * @param clientId - The ID of the client.
81
+ * @param swarmName - The name of the swarm.
82
+ */
83
+ onInit?: (clientId: string, swarmName: SwarmName) => void;
84
+ /**
85
+ * Callback triggered when a session being disponnected
86
+ * @param clientId - The ID of the client.
87
+ * @param swarmName - The name of the swarm.
88
+ */
89
+ onDispose?: (clientId: string, swarmName: SwarmName) => void;
90
+ }
91
+ /**
92
+ * Lifecycle callbacks of initialized swarm
93
+ */
94
+ interface ISwarmCallbacks extends ISwarmSessionCallbacks {
95
+ /** Emit the callback on agent change */
96
+ onAgentChanged: (clientId: string, agentName: AgentName, swarmName: SwarmName) => Promise<void>;
78
97
  }
79
98
  /**
80
99
  * Parameters for initializing a swarm.
@@ -84,29 +103,27 @@ interface ISwarmSession {
84
103
  interface ISwarmParams extends Omit<ISwarmSchema, keyof {
85
104
  agentList: never;
86
105
  onAgentChanged: never;
87
- }> {
106
+ }>, ISwarmCallbacks {
88
107
  /** Client identifier */
89
108
  clientId: string;
90
109
  /** Logger instance */
91
110
  logger: ILogger;
92
111
  /** Map of agent names to agent instances */
93
112
  agentMap: Record<AgentName, IAgent>;
94
- /** Emit the callback on agent change */
95
- onAgentChanged: (clientId: string, agentName: AgentName, swarmName: SwarmName) => Promise<void>;
96
113
  }
97
114
  /**
98
115
  * Schema for defining a swarm.
99
116
  * @interface
100
117
  */
101
- interface ISwarmSchema extends ISwarmSession {
118
+ interface ISwarmSchema {
102
119
  /** Default agent name */
103
120
  defaultAgent: AgentName;
104
121
  /** Name of the swarm */
105
122
  swarmName: string;
106
123
  /** List of agent names */
107
124
  agentList: string[];
108
- /** Emit the callback on agent change */
109
- onAgentChanged?: (clientId: string, agentName: AgentName, swarmName: SwarmName) => void;
125
+ /** Lifecycle callbacks*/
126
+ callbacks?: Partial<ISwarmCallbacks>;
110
127
  }
111
128
  /**
112
129
  * Interface for a swarm.
@@ -149,7 +166,7 @@ type SwarmName = string;
149
166
  * Parameters required to create a session.
150
167
  * @interface
151
168
  */
152
- interface ISessionParams extends ISessionSchema, ISwarmSession {
169
+ interface ISessionParams extends ISessionSchema, ISwarmSessionCallbacks {
153
170
  clientId: string;
154
171
  logger: ILogger;
155
172
  swarm: ISwarm;
@@ -430,6 +447,17 @@ interface ICompletionArgs {
430
447
  */
431
448
  tools?: ITool[];
432
449
  }
450
+ /**
451
+ * Completion lifecycle callbacks
452
+ */
453
+ interface ICompletionCallbacks {
454
+ /**
455
+ * Callback fired after complete.
456
+ * @param args - Arguments passed to complete
457
+ * @param output - Output of the model
458
+ */
459
+ onComplete?: (args: ICompletionArgs, output: IModelMessage) => void;
460
+ }
433
461
  /**
434
462
  * Schema for a completion.
435
463
  */
@@ -445,11 +473,9 @@ interface ICompletionSchema {
445
473
  */
446
474
  getCompletion(args: ICompletionArgs): Promise<IModelMessage>;
447
475
  /**
448
- * Callback fired after complete.
449
- * @param args - Arguments passed to complete
450
- * @param output - Output of the model
476
+ * Completion lifecycle callbacks
451
477
  */
452
- onComplete?: (args: ICompletionArgs, output: IModelMessage) => void;
478
+ callbacks?: Partial<ICompletionCallbacks>;
453
479
  }
454
480
  /**
455
481
  * Type representing the name of a completion.
@@ -457,44 +483,52 @@ interface ICompletionSchema {
457
483
  type CompletionName = string;
458
484
 
459
485
  /**
460
- * Interface representing a tool used by an agent.
486
+ * Interface representing lifecycle callbacks of a tool
461
487
  * @template T - The type of the parameters for the tool.
462
488
  */
463
- interface IAgentTool<T = Record<string, unknown>> extends ITool {
464
- /** The name of the tool. */
465
- toolName: ToolName;
489
+ interface IAgentToolCallbacks<T = Record<string, unknown>> {
466
490
  /**
467
- * Calls the tool with the specified parameters.
491
+ * Callback triggered when the tool is called.
468
492
  * @param clientId - The ID of the client.
469
493
  * @param agentName - The name of the agent.
470
494
  * @param params - The parameters for the tool.
471
495
  * @returns A promise that resolves when the tool call is complete.
472
496
  */
473
- call(clientId: string, agentName: AgentName, params: T): Promise<void>;
497
+ onCall?: (clientId: string, agentName: AgentName, params: T) => Promise<void>;
474
498
  /**
475
- * Validates the parameters for the tool.
499
+ * Callback triggered when the tool parameters are validated.
476
500
  * @param clientId - The ID of the client.
477
501
  * @param agentName - The name of the agent.
478
502
  * @param params - The parameters for the tool.
479
- * @returns A promise that resolves to a boolean indicating whether the parameters are valid, or a boolean.
503
+ * @returns A promise that resolves to a boolean indicating whether the parameters are valid.
480
504
  */
481
- validate(clientId: string, agentName: AgentName, params: T): Promise<boolean> | boolean;
505
+ onValidate?: (clientId: string, agentName: AgentName, params: T) => Promise<boolean>;
506
+ }
507
+ /**
508
+ * Interface representing a tool used by an agent.
509
+ * @template T - The type of the parameters for the tool.
510
+ */
511
+ interface IAgentTool<T = Record<string, unknown>> extends ITool {
512
+ /** The name of the tool. */
513
+ toolName: ToolName;
482
514
  /**
483
- * Callback triggered when the tool is called.
515
+ * Calls the tool with the specified parameters.
484
516
  * @param clientId - The ID of the client.
485
517
  * @param agentName - The name of the agent.
486
518
  * @param params - The parameters for the tool.
487
519
  * @returns A promise that resolves when the tool call is complete.
488
520
  */
489
- onCall?: (clientId: string, agentName: AgentName, params: T) => Promise<void>;
521
+ call(clientId: string, agentName: AgentName, params: T): Promise<void>;
490
522
  /**
491
- * Callback triggered when the tool parameters are validated.
523
+ * Validates the parameters for the tool.
492
524
  * @param clientId - The ID of the client.
493
525
  * @param agentName - The name of the agent.
494
526
  * @param params - The parameters for the tool.
495
- * @returns A promise that resolves to a boolean indicating whether the parameters are valid.
527
+ * @returns A promise that resolves to a boolean indicating whether the parameters are valid, or a boolean.
496
528
  */
497
- onValidate?: (clientId: string, agentName: AgentName, params: T) => Promise<boolean>;
529
+ validate(clientId: string, agentName: AgentName, params: T): Promise<boolean> | boolean;
530
+ /** The name of the tool. */
531
+ callbacks?: Partial<IAgentToolCallbacks>;
498
532
  }
499
533
  /**
500
534
  * Interface representing the parameters for an agent.
@@ -503,7 +537,7 @@ interface IAgentParams extends Omit<IAgentSchema, keyof {
503
537
  tools: never;
504
538
  completion: never;
505
539
  validate: never;
506
- }> {
540
+ }>, IAgentSchemaCallbacks {
507
541
  /** The ID of the client. */
508
542
  clientId: string;
509
543
  /** The logger instance. */
@@ -522,25 +556,9 @@ interface IAgentParams extends Omit<IAgentSchema, keyof {
522
556
  validate: (output: string) => Promise<string | null>;
523
557
  }
524
558
  /**
525
- * Interface representing the schema for an agent.
559
+ * Interface representing the lifecycle callbacks of an agent
526
560
  */
527
- interface IAgentSchema {
528
- /** The name of the agent. */
529
- agentName: AgentName;
530
- /** The name of the completion. */
531
- completion: CompletionName;
532
- /** The prompt for the agent. */
533
- prompt: string;
534
- /** The system prompt. Usually used for tool calling protocol. */
535
- system?: string[];
536
- /** The names of the tools used by the agent. */
537
- tools?: ToolName[];
538
- /**
539
- * Validates the output.
540
- * @param output - The output to validate.
541
- * @returns A promise that resolves to a string or null.
542
- */
543
- validate?: (output: string) => Promise<string | null>;
561
+ interface IAgentSchemaCallbacks {
544
562
  /**
545
563
  * Callback triggered when the agent executes.
546
564
  * @param clientId - The ID of the client.
@@ -591,6 +609,41 @@ interface IAgentSchema {
591
609
  * @param reason - The reason for the resurrection.
592
610
  */
593
611
  onResurrect?: (clientId: string, agentName: AgentName, mode: ExecutionMode, reason?: string) => void;
612
+ /**
613
+ * Callback triggered when agent is initialized
614
+ * @param clientId - The ID of the client.
615
+ * @param agentName - The name of the agent.
616
+ */
617
+ onInit?: (clientId: string, agentName: AgentName) => void;
618
+ /**
619
+ * Callback triggered when agent is disposed
620
+ * @param clientId - The ID of the client.
621
+ * @param agentName - The name of the agent.
622
+ */
623
+ onDispose?: (clientId: string, agentName: AgentName) => void;
624
+ }
625
+ /**
626
+ * Interface representing the schema for an agent.
627
+ */
628
+ interface IAgentSchema {
629
+ /** The name of the agent. */
630
+ agentName: AgentName;
631
+ /** The name of the completion. */
632
+ completion: CompletionName;
633
+ /** The prompt for the agent. */
634
+ prompt: string;
635
+ /** The system prompt. Usually used for tool calling protocol. */
636
+ system?: string[];
637
+ /** The names of the tools used by the agent. */
638
+ tools?: ToolName[];
639
+ /**
640
+ * Validates the output.
641
+ * @param output - The output to validate.
642
+ * @returns A promise that resolves to a string or null.
643
+ */
644
+ validate?: (output: string) => Promise<string | null>;
645
+ /** The lifecycle calbacks of the agent. */
646
+ callbacks?: Partial<IAgentSchemaCallbacks>;
594
647
  }
595
648
  /**
596
649
  * Interface representing an agent.
@@ -745,6 +798,11 @@ declare class ClientAgent implements IAgent {
745
798
  * @returns {Promise<void>}
746
799
  */
747
800
  execute: IAgent["execute"];
801
+ /**
802
+ * Should call on agent dispose
803
+ * @returns {Promise<void>}
804
+ */
805
+ dispose: () => Promise<void>;
748
806
  }
749
807
 
750
808
  /**
@@ -1110,6 +1168,11 @@ declare class ClientSession implements ISession {
1110
1168
  * @returns {ReceiveMessageFn} - The function to receive messages.
1111
1169
  */
1112
1170
  connect: (connector: SendMessageFn$1) => ReceiveMessageFn;
1171
+ /**
1172
+ * Should call on session dispose
1173
+ * @returns {Promise<void>}
1174
+ */
1175
+ dispose: () => Promise<void>;
1113
1176
  }
1114
1177
 
1115
1178
  /**