agent-swarm-kit 1.0.38 → 1.0.40

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
@@ -698,11 +698,12 @@ var ClientAgent = /** @class */ (function () {
698
698
  * @returns {Promise<void>}
699
699
  */
700
700
  this.execute = functoolsKit.queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
701
- var message, _loop_1, this_1, _a, _b, tool, state_1, e_1_1, result, validation, result1;
702
- var e_1, _c;
703
- var _d, _e, _f, _g, _h, _j;
704
- return __generator(this, function (_k) {
705
- switch (_k.label) {
701
+ var message, toolCalls, _loop_1, this_1, toolCalls_1, toolCalls_1_1, tool, state_1, e_1_1, result, validation, result1;
702
+ var e_1, _a;
703
+ var _this = this;
704
+ var _b, _c, _d, _e, _f, _g;
705
+ return __generator(this, function (_h) {
706
+ switch (_h.label) {
706
707
  case 0:
707
708
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
708
709
  this.params.onExecute &&
@@ -714,120 +715,135 @@ var ClientAgent = /** @class */ (function () {
714
715
  content: incoming.trim(),
715
716
  })];
716
717
  case 1:
717
- _k.sent();
718
+ _h.sent();
718
719
  return [4 /*yield*/, this.getCompletion(mode)];
719
720
  case 2:
720
- message = _k.sent();
721
- if (!message.tool_calls) return [3 /*break*/, 11];
721
+ message = _h.sent();
722
+ if (!message.tool_calls) return [3 /*break*/, 12];
722
723
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
724
+ toolCalls = message.tool_calls.map(function (call) {
725
+ var _a, _b;
726
+ return ({
727
+ function: call.function,
728
+ id: (_a = call.id) !== null && _a !== void 0 ? _a : functoolsKit.randomString(),
729
+ type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
730
+ });
731
+ });
732
+ return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
733
+ case 3:
734
+ _h.sent();
723
735
  _loop_1 = function (tool) {
724
736
  var targetFn, result_2, result_3;
725
- return __generator(this, function (_l) {
726
- switch (_l.label) {
737
+ return __generator(this, function (_j) {
738
+ switch (_j.label) {
727
739
  case 0:
728
- targetFn = (_d = this_1.params.tools) === null || _d === void 0 ? void 0 : _d.find(function (t) { return t.function.name === tool.function.name; });
729
- return [4 /*yield*/, this_1.params.history.push(__assign(__assign({}, message), { agentName: this_1.params.agentName }))];
730
- case 1:
731
- _l.sent();
732
- if (!!targetFn) return [3 /*break*/, 4];
740
+ targetFn = (_b = this_1.params.tools) === null || _b === void 0 ? void 0 : _b.find(function (t) { return t.function.name === tool.function.name; });
741
+ if (!!targetFn) return [3 /*break*/, 3];
733
742
  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);
734
743
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
735
- case 2:
736
- result_2 = _l.sent();
744
+ case 1:
745
+ result_2 = _j.sent();
737
746
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
738
747
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
739
- case 3:
740
- _l.sent();
748
+ case 2:
749
+ _j.sent();
741
750
  return [2 /*return*/, { value: void 0 }];
742
- case 4:
743
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onValidate) &&
744
- ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
751
+ case 3:
752
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate) &&
753
+ ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
745
754
  return [4 /*yield*/, functoolsKit.not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
746
- case 5:
747
- if (!_l.sent()) return [3 /*break*/, 8];
755
+ case 4:
756
+ if (!_j.sent()) return [3 /*break*/, 7];
748
757
  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"));
749
758
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
750
- case 6:
751
- result_3 = _l.sent();
759
+ case 5:
760
+ result_3 = _j.sent();
752
761
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
753
762
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
754
- case 7:
755
- _l.sent();
763
+ case 6:
764
+ _j.sent();
756
765
  return [2 /*return*/, { value: void 0 }];
757
- case 8:
758
- ((_g = targetFn.callbacks) === null || _g === void 0 ? void 0 : _g.onCall) &&
759
- ((_h = targetFn.callbacks) === null || _h === void 0 ? void 0 : _h.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
766
+ case 7:
767
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall) &&
768
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onBeforeCall(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
760
769
  /**
761
770
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
762
771
  */
763
- targetFn.call((_j = tool.id) !== null && _j !== void 0 ? _j : functoolsKit.randomString(), this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
772
+ Promise.resolve(targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments)).then(function () {
773
+ var _a, _b;
774
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
775
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments));
776
+ });
764
777
  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"));
765
778
  return [4 /*yield*/, Promise.race([
766
779
  this_1._toolCommitSubject.toPromise(),
767
780
  this_1._outputSubject.toPromise(),
768
781
  ])];
769
- case 9:
770
- _l.sent();
782
+ case 8:
783
+ _j.sent();
771
784
  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"));
772
785
  return [2 /*return*/];
773
786
  }
774
787
  });
775
788
  };
776
789
  this_1 = this;
777
- _k.label = 3;
778
- case 3:
779
- _k.trys.push([3, 8, 9, 10]);
780
- _a = __values(message.tool_calls), _b = _a.next();
781
- _k.label = 4;
790
+ _h.label = 4;
782
791
  case 4:
783
- if (!!_b.done) return [3 /*break*/, 7];
784
- tool = _b.value;
785
- return [5 /*yield**/, _loop_1(tool)];
792
+ _h.trys.push([4, 9, 10, 11]);
793
+ toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
794
+ _h.label = 5;
786
795
  case 5:
787
- state_1 = _k.sent();
796
+ if (!!toolCalls_1_1.done) return [3 /*break*/, 8];
797
+ tool = toolCalls_1_1.value;
798
+ return [5 /*yield**/, _loop_1(tool)];
799
+ case 6:
800
+ state_1 = _h.sent();
788
801
  if (typeof state_1 === "object")
789
802
  return [2 /*return*/, state_1.value];
790
- _k.label = 6;
791
- case 6:
792
- _b = _a.next();
793
- return [3 /*break*/, 4];
794
- case 7: return [3 /*break*/, 10];
795
- case 8:
796
- e_1_1 = _k.sent();
797
- e_1 = { error: e_1_1 };
798
- return [3 /*break*/, 10];
803
+ _h.label = 7;
804
+ case 7:
805
+ toolCalls_1_1 = toolCalls_1.next();
806
+ return [3 /*break*/, 5];
807
+ case 8: return [3 /*break*/, 11];
799
808
  case 9:
809
+ e_1_1 = _h.sent();
810
+ e_1 = { error: e_1_1 };
811
+ return [3 /*break*/, 11];
812
+ case 10:
800
813
  try {
801
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
814
+ if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
802
815
  }
803
816
  finally { if (e_1) throw e_1.error; }
804
817
  return [7 /*endfinally*/];
805
- case 10: return [2 /*return*/];
806
818
  case 11:
819
+ ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
820
+ this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
821
+ return [2 /*return*/];
822
+ case 12:
807
823
  if (!message.tool_calls) {
808
824
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
809
825
  }
810
826
  result = message.content;
811
827
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
812
- case 12:
813
- _k.sent();
828
+ case 13:
829
+ _h.sent();
814
830
  validation = null;
815
831
  return [4 /*yield*/, this.params.validate(result)];
816
- case 13:
817
- if (!(validation = _k.sent())) return [3 /*break*/, 16];
832
+ case 14:
833
+ if (!(validation = _h.sent())) return [3 /*break*/, 17];
818
834
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
819
835
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
820
- case 14:
821
- result1 = _k.sent();
822
- return [4 /*yield*/, this._emitOuput(mode, result1)];
823
836
  case 15:
824
- _k.sent();
825
- return [2 /*return*/];
837
+ result1 = _h.sent();
838
+ return [4 /*yield*/, this._emitOuput(mode, result1)];
826
839
  case 16:
840
+ _h.sent();
841
+ return [2 /*return*/];
842
+ case 17:
827
843
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
828
844
  return [4 /*yield*/, this._emitOuput(mode, result)];
829
- case 17:
830
- _k.sent();
845
+ case 18:
846
+ _h.sent();
831
847
  return [2 /*return*/];
832
848
  }
833
849
  });
package/build/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { scoped } from 'di-scoped';
2
- import { ToolRegistry, trycatch, sleep, PubsubArrayAdapter, Subject, queued, randomString, not, memoize, createAwaiter, cancelable, CANCELED_PROMISE_SYMBOL, schedule, ttl, singleshot, Source } from 'functools-kit';
2
+ import { ToolRegistry, trycatch, sleep, PubsubArrayAdapter, Subject, queued, not, randomString, memoize, createAwaiter, cancelable, CANCELED_PROMISE_SYMBOL, schedule, ttl, singleshot, Source } from 'functools-kit';
3
3
  import { createActivator } from 'di-kit';
4
4
  import { omit } from 'lodash-es';
5
5
  import xml2js from 'xml2js';
@@ -696,11 +696,12 @@ var ClientAgent = /** @class */ (function () {
696
696
  * @returns {Promise<void>}
697
697
  */
698
698
  this.execute = queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
699
- var message, _loop_1, this_1, _a, _b, tool, state_1, e_1_1, result, validation, result1;
700
- var e_1, _c;
701
- var _d, _e, _f, _g, _h, _j;
702
- return __generator(this, function (_k) {
703
- switch (_k.label) {
699
+ var message, toolCalls, _loop_1, this_1, toolCalls_1, toolCalls_1_1, tool, state_1, e_1_1, result, validation, result1;
700
+ var e_1, _a;
701
+ var _this = this;
702
+ var _b, _c, _d, _e, _f, _g;
703
+ return __generator(this, function (_h) {
704
+ switch (_h.label) {
704
705
  case 0:
705
706
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
706
707
  this.params.onExecute &&
@@ -712,120 +713,135 @@ var ClientAgent = /** @class */ (function () {
712
713
  content: incoming.trim(),
713
714
  })];
714
715
  case 1:
715
- _k.sent();
716
+ _h.sent();
716
717
  return [4 /*yield*/, this.getCompletion(mode)];
717
718
  case 2:
718
- message = _k.sent();
719
- if (!message.tool_calls) return [3 /*break*/, 11];
719
+ message = _h.sent();
720
+ if (!message.tool_calls) return [3 /*break*/, 12];
720
721
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
722
+ toolCalls = message.tool_calls.map(function (call) {
723
+ var _a, _b;
724
+ return ({
725
+ function: call.function,
726
+ id: (_a = call.id) !== null && _a !== void 0 ? _a : randomString(),
727
+ type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
728
+ });
729
+ });
730
+ return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
731
+ case 3:
732
+ _h.sent();
721
733
  _loop_1 = function (tool) {
722
734
  var targetFn, result_2, result_3;
723
- return __generator(this, function (_l) {
724
- switch (_l.label) {
735
+ return __generator(this, function (_j) {
736
+ switch (_j.label) {
725
737
  case 0:
726
- targetFn = (_d = this_1.params.tools) === null || _d === void 0 ? void 0 : _d.find(function (t) { return t.function.name === tool.function.name; });
727
- return [4 /*yield*/, this_1.params.history.push(__assign(__assign({}, message), { agentName: this_1.params.agentName }))];
728
- case 1:
729
- _l.sent();
730
- if (!!targetFn) return [3 /*break*/, 4];
738
+ targetFn = (_b = this_1.params.tools) === null || _b === void 0 ? void 0 : _b.find(function (t) { return t.function.name === tool.function.name; });
739
+ if (!!targetFn) return [3 /*break*/, 3];
731
740
  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);
732
741
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
733
- case 2:
734
- result_2 = _l.sent();
742
+ case 1:
743
+ result_2 = _j.sent();
735
744
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
736
745
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
737
- case 3:
738
- _l.sent();
746
+ case 2:
747
+ _j.sent();
739
748
  return [2 /*return*/, { value: void 0 }];
740
- case 4:
741
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onValidate) &&
742
- ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
749
+ case 3:
750
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate) &&
751
+ ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
743
752
  return [4 /*yield*/, not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
744
- case 5:
745
- if (!_l.sent()) return [3 /*break*/, 8];
753
+ case 4:
754
+ if (!_j.sent()) return [3 /*break*/, 7];
746
755
  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"));
747
756
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
748
- case 6:
749
- result_3 = _l.sent();
757
+ case 5:
758
+ result_3 = _j.sent();
750
759
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
751
760
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
752
- case 7:
753
- _l.sent();
761
+ case 6:
762
+ _j.sent();
754
763
  return [2 /*return*/, { value: void 0 }];
755
- case 8:
756
- ((_g = targetFn.callbacks) === null || _g === void 0 ? void 0 : _g.onCall) &&
757
- ((_h = targetFn.callbacks) === null || _h === void 0 ? void 0 : _h.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
764
+ case 7:
765
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall) &&
766
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onBeforeCall(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
758
767
  /**
759
768
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
760
769
  */
761
- targetFn.call((_j = tool.id) !== null && _j !== void 0 ? _j : randomString(), this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
770
+ Promise.resolve(targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments)).then(function () {
771
+ var _a, _b;
772
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
773
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments));
774
+ });
762
775
  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"));
763
776
  return [4 /*yield*/, Promise.race([
764
777
  this_1._toolCommitSubject.toPromise(),
765
778
  this_1._outputSubject.toPromise(),
766
779
  ])];
767
- case 9:
768
- _l.sent();
780
+ case 8:
781
+ _j.sent();
769
782
  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"));
770
783
  return [2 /*return*/];
771
784
  }
772
785
  });
773
786
  };
774
787
  this_1 = this;
775
- _k.label = 3;
776
- case 3:
777
- _k.trys.push([3, 8, 9, 10]);
778
- _a = __values(message.tool_calls), _b = _a.next();
779
- _k.label = 4;
788
+ _h.label = 4;
780
789
  case 4:
781
- if (!!_b.done) return [3 /*break*/, 7];
782
- tool = _b.value;
783
- return [5 /*yield**/, _loop_1(tool)];
790
+ _h.trys.push([4, 9, 10, 11]);
791
+ toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
792
+ _h.label = 5;
784
793
  case 5:
785
- state_1 = _k.sent();
794
+ if (!!toolCalls_1_1.done) return [3 /*break*/, 8];
795
+ tool = toolCalls_1_1.value;
796
+ return [5 /*yield**/, _loop_1(tool)];
797
+ case 6:
798
+ state_1 = _h.sent();
786
799
  if (typeof state_1 === "object")
787
800
  return [2 /*return*/, state_1.value];
788
- _k.label = 6;
789
- case 6:
790
- _b = _a.next();
791
- return [3 /*break*/, 4];
792
- case 7: return [3 /*break*/, 10];
793
- case 8:
794
- e_1_1 = _k.sent();
795
- e_1 = { error: e_1_1 };
796
- return [3 /*break*/, 10];
801
+ _h.label = 7;
802
+ case 7:
803
+ toolCalls_1_1 = toolCalls_1.next();
804
+ return [3 /*break*/, 5];
805
+ case 8: return [3 /*break*/, 11];
797
806
  case 9:
807
+ e_1_1 = _h.sent();
808
+ e_1 = { error: e_1_1 };
809
+ return [3 /*break*/, 11];
810
+ case 10:
798
811
  try {
799
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
812
+ if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
800
813
  }
801
814
  finally { if (e_1) throw e_1.error; }
802
815
  return [7 /*endfinally*/];
803
- case 10: return [2 /*return*/];
804
816
  case 11:
817
+ ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
818
+ this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
819
+ return [2 /*return*/];
820
+ case 12:
805
821
  if (!message.tool_calls) {
806
822
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
807
823
  }
808
824
  result = message.content;
809
825
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
810
- case 12:
811
- _k.sent();
826
+ case 13:
827
+ _h.sent();
812
828
  validation = null;
813
829
  return [4 /*yield*/, this.params.validate(result)];
814
- case 13:
815
- if (!(validation = _k.sent())) return [3 /*break*/, 16];
830
+ case 14:
831
+ if (!(validation = _h.sent())) return [3 /*break*/, 17];
816
832
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
817
833
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
818
- case 14:
819
- result1 = _k.sent();
820
- return [4 /*yield*/, this._emitOuput(mode, result1)];
821
834
  case 15:
822
- _k.sent();
823
- return [2 /*return*/];
835
+ result1 = _h.sent();
836
+ return [4 /*yield*/, this._emitOuput(mode, result1)];
824
837
  case 16:
838
+ _h.sent();
839
+ return [2 /*return*/];
840
+ case 17:
825
841
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
826
842
  return [4 /*yield*/, this._emitOuput(mode, result)];
827
- case 17:
828
- _k.sent();
843
+ case 18:
844
+ _h.sent();
829
845
  return [2 /*return*/];
830
846
  }
831
847
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.38",
3
+ "version": "1.0.40",
4
4
  "description": "A TypeScript library for building orchestrated framework-agnostic multi-agent AI systems",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -497,13 +497,23 @@ type CompletionName = string;
497
497
  */
498
498
  interface IAgentToolCallbacks<T = Record<string, unknown>> {
499
499
  /**
500
- * Callback triggered when the tool is called.
500
+ * Callback triggered before the tool is called.
501
+ * @param toolId - The `tool_call_id` for openai history
501
502
  * @param clientId - The ID of the client.
502
503
  * @param agentName - The name of the agent.
503
504
  * @param params - The parameters for the tool.
504
505
  * @returns A promise that resolves when the tool call is complete.
505
506
  */
506
- onCall?: (clientId: string, agentName: AgentName, params: T) => Promise<void>;
507
+ onBeforeCall?: (toolId: string, clientId: string, agentName: AgentName, params: T) => Promise<void>;
508
+ /**
509
+ * Callback triggered after the tool is called.
510
+ * @param toolId - The `tool_call_id` for openai history
511
+ * @param clientId - The ID of the client.
512
+ * @param agentName - The name of the agent.
513
+ * @param params - The parameters for the tool.
514
+ * @returns A promise that resolves when the tool call is complete.
515
+ */
516
+ onAfterCall?: (toolId: string, clientId: string, agentName: AgentName, params: T) => Promise<void>;
507
517
  /**
508
518
  * Callback triggered when the tool parameters are validated.
509
519
  * @param clientId - The ID of the client.
@@ -522,6 +532,7 @@ interface IAgentTool<T = Record<string, unknown>> extends ITool {
522
532
  toolName: ToolName;
523
533
  /**
524
534
  * Calls the tool with the specified parameters.
535
+ * @param toolId - The `tool_call_id` for openai history
525
536
  * @param clientId - The ID of the client.
526
537
  * @param agentName - The name of the agent.
527
538
  * @param params - The parameters for the tool.
@@ -631,6 +642,13 @@ interface IAgentSchemaCallbacks {
631
642
  * @param agentName - The name of the agent.
632
643
  */
633
644
  onDispose?: (clientId: string, agentName: AgentName) => void;
645
+ /**
646
+ * Callback triggered after all tools are called
647
+ * @param clientId - The ID of the client.
648
+ * @param agentName - The name of the agent.
649
+ * @param toolCalls - The array of tool calls
650
+ */
651
+ onAfterToolCalls?: (clientId: string, agentName: AgentName, toolCalls: IToolCall[]) => void;
634
652
  }
635
653
  /**
636
654
  * Interface representing the schema for an agent.