agent-swarm-kit 1.0.37 → 1.0.39

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
@@ -669,18 +669,19 @@ var ClientAgent = /** @class */ (function () {
669
669
  * @param {string} content - The tool output content.
670
670
  * @returns {Promise<void>}
671
671
  */
672
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
672
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
673
673
  return __generator(this, function (_a) {
674
674
  switch (_a.label) {
675
675
  case 0:
676
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput"), { content: content });
676
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput"), { content: content, toolId: toolId });
677
677
  this.params.onToolOutput &&
678
- this.params.onToolOutput(this.params.clientId, this.params.agentName, content);
678
+ this.params.onToolOutput(toolId, this.params.clientId, this.params.agentName, content);
679
679
  return [4 /*yield*/, this.params.history.push({
680
680
  role: "tool",
681
681
  agentName: this.params.agentName,
682
682
  mode: "tool",
683
683
  content: content,
684
+ tool_call_id: toolId,
684
685
  })];
685
686
  case 1:
686
687
  _a.sent();
@@ -697,11 +698,11 @@ var ClientAgent = /** @class */ (function () {
697
698
  * @returns {Promise<void>}
698
699
  */
699
700
  this.execute = functoolsKit.queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
700
- var message, _loop_1, this_1, _a, _b, tool, state_1, e_1_1, result, validation, result1;
701
- var e_1, _c;
702
- var _d, _e, _f, _g, _h;
703
- return __generator(this, function (_j) {
704
- switch (_j.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 _b, _c, _d, _e, _f, _g;
704
+ return __generator(this, function (_h) {
705
+ switch (_h.label) {
705
706
  case 0:
706
707
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
707
708
  this.params.onExecute &&
@@ -713,120 +714,131 @@ var ClientAgent = /** @class */ (function () {
713
714
  content: incoming.trim(),
714
715
  })];
715
716
  case 1:
716
- _j.sent();
717
+ _h.sent();
717
718
  return [4 /*yield*/, this.getCompletion(mode)];
718
719
  case 2:
719
- message = _j.sent();
720
- if (!message.tool_calls) return [3 /*break*/, 11];
720
+ message = _h.sent();
721
+ if (!message.tool_calls) return [3 /*break*/, 12];
721
722
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
723
+ toolCalls = message.tool_calls.map(function (call) {
724
+ var _a, _b;
725
+ return ({
726
+ function: call.function,
727
+ id: (_a = call.id) !== null && _a !== void 0 ? _a : functoolsKit.randomString(),
728
+ type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
729
+ });
730
+ });
731
+ return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
732
+ case 3:
733
+ _h.sent();
722
734
  _loop_1 = function (tool) {
723
735
  var targetFn, result_2, result_3;
724
- return __generator(this, function (_k) {
725
- switch (_k.label) {
736
+ return __generator(this, function (_j) {
737
+ switch (_j.label) {
726
738
  case 0:
727
- targetFn = (_d = this_1.params.tools) === null || _d === void 0 ? void 0 : _d.find(function (t) { return t.function.name === tool.function.name; });
728
- return [4 /*yield*/, this_1.params.history.push(__assign(__assign({}, message), { agentName: this_1.params.agentName }))];
729
- case 1:
730
- _k.sent();
731
- if (!!targetFn) return [3 /*break*/, 4];
739
+ targetFn = (_b = this_1.params.tools) === null || _b === void 0 ? void 0 : _b.find(function (t) { return t.function.name === tool.function.name; });
740
+ if (!!targetFn) return [3 /*break*/, 3];
732
741
  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);
733
742
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
734
- case 2:
735
- result_2 = _k.sent();
743
+ case 1:
744
+ result_2 = _j.sent();
736
745
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
737
746
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
738
- case 3:
739
- _k.sent();
747
+ case 2:
748
+ _j.sent();
740
749
  return [2 /*return*/, { value: void 0 }];
741
- case 4:
742
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onValidate) &&
743
- ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
750
+ case 3:
751
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate) &&
752
+ ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
744
753
  return [4 /*yield*/, functoolsKit.not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
745
- case 5:
746
- if (!_k.sent()) return [3 /*break*/, 8];
754
+ case 4:
755
+ if (!_j.sent()) return [3 /*break*/, 7];
747
756
  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"));
748
757
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
749
- case 6:
750
- result_3 = _k.sent();
758
+ case 5:
759
+ result_3 = _j.sent();
751
760
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
752
761
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
753
- case 7:
754
- _k.sent();
762
+ case 6:
763
+ _j.sent();
755
764
  return [2 /*return*/, { value: void 0 }];
756
- case 8:
757
- ((_g = targetFn.callbacks) === null || _g === void 0 ? void 0 : _g.onCall) &&
758
- ((_h = targetFn.callbacks) === null || _h === void 0 ? void 0 : _h.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
765
+ case 7:
766
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onCall) &&
767
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
759
768
  /**
760
769
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
761
770
  */
762
- targetFn.call(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
771
+ targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
763
772
  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"));
764
773
  return [4 /*yield*/, Promise.race([
765
774
  this_1._toolCommitSubject.toPromise(),
766
775
  this_1._outputSubject.toPromise(),
767
776
  ])];
768
- case 9:
769
- _k.sent();
777
+ case 8:
778
+ _j.sent();
770
779
  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"));
771
780
  return [2 /*return*/];
772
781
  }
773
782
  });
774
783
  };
775
784
  this_1 = this;
776
- _j.label = 3;
777
- case 3:
778
- _j.trys.push([3, 8, 9, 10]);
779
- _a = __values(message.tool_calls), _b = _a.next();
780
- _j.label = 4;
785
+ _h.label = 4;
781
786
  case 4:
782
- if (!!_b.done) return [3 /*break*/, 7];
783
- tool = _b.value;
784
- return [5 /*yield**/, _loop_1(tool)];
787
+ _h.trys.push([4, 9, 10, 11]);
788
+ toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
789
+ _h.label = 5;
785
790
  case 5:
786
- state_1 = _j.sent();
791
+ if (!!toolCalls_1_1.done) return [3 /*break*/, 8];
792
+ tool = toolCalls_1_1.value;
793
+ return [5 /*yield**/, _loop_1(tool)];
794
+ case 6:
795
+ state_1 = _h.sent();
787
796
  if (typeof state_1 === "object")
788
797
  return [2 /*return*/, state_1.value];
789
- _j.label = 6;
790
- case 6:
791
- _b = _a.next();
792
- return [3 /*break*/, 4];
793
- case 7: return [3 /*break*/, 10];
794
- case 8:
795
- e_1_1 = _j.sent();
796
- e_1 = { error: e_1_1 };
797
- return [3 /*break*/, 10];
798
+ _h.label = 7;
799
+ case 7:
800
+ toolCalls_1_1 = toolCalls_1.next();
801
+ return [3 /*break*/, 5];
802
+ case 8: return [3 /*break*/, 11];
798
803
  case 9:
804
+ e_1_1 = _h.sent();
805
+ e_1 = { error: e_1_1 };
806
+ return [3 /*break*/, 11];
807
+ case 10:
799
808
  try {
800
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
809
+ if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
801
810
  }
802
811
  finally { if (e_1) throw e_1.error; }
803
812
  return [7 /*endfinally*/];
804
- case 10: return [2 /*return*/];
805
813
  case 11:
814
+ ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
815
+ this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
816
+ return [2 /*return*/];
817
+ case 12:
806
818
  if (!message.tool_calls) {
807
819
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
808
820
  }
809
821
  result = message.content;
810
822
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
811
- case 12:
812
- _j.sent();
823
+ case 13:
824
+ _h.sent();
813
825
  validation = null;
814
826
  return [4 /*yield*/, this.params.validate(result)];
815
- case 13:
816
- if (!(validation = _j.sent())) return [3 /*break*/, 16];
827
+ case 14:
828
+ if (!(validation = _h.sent())) return [3 /*break*/, 17];
817
829
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
818
830
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
819
- case 14:
820
- result1 = _j.sent();
821
- return [4 /*yield*/, this._emitOuput(mode, result1)];
822
831
  case 15:
823
- _j.sent();
824
- return [2 /*return*/];
832
+ result1 = _h.sent();
833
+ return [4 /*yield*/, this._emitOuput(mode, result1)];
825
834
  case 16:
835
+ _h.sent();
836
+ return [2 /*return*/];
837
+ case 17:
826
838
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
827
839
  return [4 /*yield*/, this._emitOuput(mode, result)];
828
- case 17:
829
- _j.sent();
840
+ case 18:
841
+ _h.sent();
830
842
  return [2 /*return*/];
831
843
  }
832
844
  });
@@ -918,17 +930,19 @@ var AgentConnectionService = /** @class */ (function () {
918
930
  /**
919
931
  * Commits tool output.
920
932
  * @param {string} content - The tool output content.
933
+ * @param {string} toolId - The `tool_call_id` for openai history
921
934
  * @returns {Promise<any>} The commit result.
922
935
  */
923
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
936
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
924
937
  return __generator(this, function (_a) {
925
938
  switch (_a.label) {
926
939
  case 0:
927
940
  this.loggerService.log("agentConnectionService commitToolOutput", {
928
941
  content: content,
942
+ toolId: toolId,
929
943
  context: this.contextService.context,
930
944
  });
931
- return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitToolOutput(content)];
945
+ return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitToolOutput(toolId, content)];
932
946
  case 1: return [2 /*return*/, _a.sent()];
933
947
  }
934
948
  });
@@ -1784,21 +1798,23 @@ var ClientSession = /** @class */ (function () {
1784
1798
  }); };
1785
1799
  /**
1786
1800
  * Commits tool output.
1801
+ * @param {string} toolId - The `tool_call_id` for openai history
1787
1802
  * @param {string} content - The content to commit.
1788
1803
  * @returns {Promise<void>}
1789
1804
  */
1790
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
1805
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
1791
1806
  var agent;
1792
1807
  return __generator(this, function (_a) {
1793
1808
  switch (_a.label) {
1794
1809
  case 0:
1795
1810
  this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " commitToolOutput"), {
1796
1811
  content: content,
1812
+ toolId: toolId,
1797
1813
  });
1798
1814
  return [4 /*yield*/, this.params.swarm.getAgent()];
1799
1815
  case 1:
1800
1816
  agent = _a.sent();
1801
- return [4 /*yield*/, agent.commitToolOutput(content)];
1817
+ return [4 /*yield*/, agent.commitToolOutput(toolId, content)];
1802
1818
  case 2: return [2 /*return*/, _a.sent()];
1803
1819
  }
1804
1820
  });
@@ -2006,18 +2022,20 @@ var SessionConnectionService = /** @class */ (function () {
2006
2022
  };
2007
2023
  /**
2008
2024
  * Commits tool output to the session.
2025
+ * @param {string} toolId - The `tool_call_id` for openai history
2009
2026
  * @param {string} content - The content to commit.
2010
2027
  * @returns {Promise<void>} A promise that resolves when the content is committed.
2011
2028
  */
2012
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
2029
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
2013
2030
  return __generator(this, function (_a) {
2014
2031
  switch (_a.label) {
2015
2032
  case 0:
2016
2033
  this.loggerService.log("sessionConnectionService commitToolOutput", {
2017
2034
  context: this.contextService.context,
2018
2035
  content: content,
2036
+ toolId: toolId,
2019
2037
  });
2020
- return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).commitToolOutput(content)];
2038
+ return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).commitToolOutput(toolId, content)];
2021
2039
  case 1: return [2 /*return*/, _a.sent()];
2022
2040
  }
2023
2041
  });
@@ -2204,12 +2222,13 @@ var AgentPublicService = /** @class */ (function () {
2204
2222
  }); };
2205
2223
  /**
2206
2224
  * Commits tool output to the agent.
2225
+ * @param {string} toolId - The `tool_call_id` for openai history
2207
2226
  * @param {string} content - The content to commit.
2208
2227
  * @param {string} clientId - The client ID.
2209
2228
  * @param {AgentName} agentName - The name of the agent.
2210
2229
  * @returns {Promise<unknown>} The commit result.
2211
2230
  */
2212
- this.commitToolOutput = function (content, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
2231
+ this.commitToolOutput = function (toolId, content, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
2213
2232
  var _this = this;
2214
2233
  return __generator(this, function (_a) {
2215
2234
  switch (_a.label) {
@@ -2217,12 +2236,13 @@ var AgentPublicService = /** @class */ (function () {
2217
2236
  this.loggerService.log("agentPublicService commitToolOutput", {
2218
2237
  content: content,
2219
2238
  clientId: clientId,
2239
+ toolId: toolId,
2220
2240
  agentName: agentName,
2221
2241
  });
2222
2242
  return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
2223
2243
  return __generator(this, function (_a) {
2224
2244
  switch (_a.label) {
2225
- case 0: return [4 /*yield*/, this.agentConnectionService.commitToolOutput(content)];
2245
+ case 0: return [4 /*yield*/, this.agentConnectionService.commitToolOutput(toolId, content)];
2226
2246
  case 1: return [2 /*return*/, _a.sent()];
2227
2247
  }
2228
2248
  });
@@ -2632,17 +2652,19 @@ var SessionPublicService = /** @class */ (function () {
2632
2652
  };
2633
2653
  /**
2634
2654
  * Commits tool output to the session.
2655
+ * @param {string} toolId - The `tool_call_id` for openai history
2635
2656
  * @param {string} content - The content to commit.
2636
2657
  * @param {string} clientId - The client ID.
2637
2658
  * @param {SwarmName} swarmName - The swarm name.
2638
2659
  * @returns {Promise<void>}
2639
2660
  */
2640
- this.commitToolOutput = function (content, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
2661
+ this.commitToolOutput = function (toolId, content, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
2641
2662
  var _this = this;
2642
2663
  return __generator(this, function (_a) {
2643
2664
  switch (_a.label) {
2644
2665
  case 0:
2645
2666
  this.loggerService.log("sessionPublicService commitToolOutput", {
2667
+ toolId: toolId,
2646
2668
  content: content,
2647
2669
  clientId: clientId,
2648
2670
  swarmName: swarmName,
@@ -2650,7 +2672,7 @@ var SessionPublicService = /** @class */ (function () {
2650
2672
  return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
2651
2673
  return __generator(this, function (_a) {
2652
2674
  switch (_a.label) {
2653
- case 0: return [4 /*yield*/, this.sessionConnectionService.commitToolOutput(content)];
2675
+ case 0: return [4 /*yield*/, this.sessionConnectionService.commitToolOutput(toolId, content)];
2654
2676
  case 1: return [2 /*return*/, _a.sent()];
2655
2677
  }
2656
2678
  });
@@ -4151,12 +4173,13 @@ var getAgentHistory = function (clientId, agentName) { return __awaiter(void 0,
4151
4173
  * @param {AgentName} agentName - The name of the agent committing the output.
4152
4174
  * @returns {Promise<void>} - A promise that resolves when the operation is complete.
4153
4175
  */
4154
- var commitToolOutput = function (content, clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
4176
+ var commitToolOutput = function (toolId, content, clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
4155
4177
  var swarmName, currentAgentName;
4156
4178
  return __generator(this, function (_a) {
4157
4179
  switch (_a.label) {
4158
4180
  case 0:
4159
4181
  swarm.loggerService.log('function commitToolOutput', {
4182
+ toolId: toolId,
4160
4183
  content: content,
4161
4184
  clientId: clientId,
4162
4185
  agentName: agentName,
@@ -4170,13 +4193,14 @@ var commitToolOutput = function (content, clientId, agentName) { return __awaite
4170
4193
  currentAgentName = _a.sent();
4171
4194
  if (currentAgentName !== agentName) {
4172
4195
  swarm.loggerService.log('function "commitToolOutput" skipped due to the agent change', {
4196
+ toolId: toolId,
4173
4197
  currentAgentName: currentAgentName,
4174
4198
  agentName: agentName,
4175
4199
  clientId: clientId,
4176
4200
  });
4177
4201
  return [2 /*return*/];
4178
4202
  }
4179
- return [4 /*yield*/, swarm.sessionPublicService.commitToolOutput(content, clientId, swarmName)];
4203
+ return [4 /*yield*/, swarm.sessionPublicService.commitToolOutput(toolId, content, clientId, swarmName)];
4180
4204
  case 2:
4181
4205
  _a.sent();
4182
4206
  return [2 /*return*/];
package/build/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { scoped } from 'di-scoped';
2
- import { ToolRegistry, trycatch, sleep, PubsubArrayAdapter, Subject, queued, 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';
@@ -667,18 +667,19 @@ var ClientAgent = /** @class */ (function () {
667
667
  * @param {string} content - The tool output content.
668
668
  * @returns {Promise<void>}
669
669
  */
670
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
670
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
671
671
  return __generator(this, function (_a) {
672
672
  switch (_a.label) {
673
673
  case 0:
674
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput"), { content: content });
674
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput"), { content: content, toolId: toolId });
675
675
  this.params.onToolOutput &&
676
- this.params.onToolOutput(this.params.clientId, this.params.agentName, content);
676
+ this.params.onToolOutput(toolId, this.params.clientId, this.params.agentName, content);
677
677
  return [4 /*yield*/, this.params.history.push({
678
678
  role: "tool",
679
679
  agentName: this.params.agentName,
680
680
  mode: "tool",
681
681
  content: content,
682
+ tool_call_id: toolId,
682
683
  })];
683
684
  case 1:
684
685
  _a.sent();
@@ -695,11 +696,11 @@ var ClientAgent = /** @class */ (function () {
695
696
  * @returns {Promise<void>}
696
697
  */
697
698
  this.execute = queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
698
- var message, _loop_1, this_1, _a, _b, tool, state_1, e_1_1, result, validation, result1;
699
- var e_1, _c;
700
- var _d, _e, _f, _g, _h;
701
- return __generator(this, function (_j) {
702
- switch (_j.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 _b, _c, _d, _e, _f, _g;
702
+ return __generator(this, function (_h) {
703
+ switch (_h.label) {
703
704
  case 0:
704
705
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
705
706
  this.params.onExecute &&
@@ -711,120 +712,131 @@ var ClientAgent = /** @class */ (function () {
711
712
  content: incoming.trim(),
712
713
  })];
713
714
  case 1:
714
- _j.sent();
715
+ _h.sent();
715
716
  return [4 /*yield*/, this.getCompletion(mode)];
716
717
  case 2:
717
- message = _j.sent();
718
- if (!message.tool_calls) return [3 /*break*/, 11];
718
+ message = _h.sent();
719
+ if (!message.tool_calls) return [3 /*break*/, 12];
719
720
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
721
+ toolCalls = message.tool_calls.map(function (call) {
722
+ var _a, _b;
723
+ return ({
724
+ function: call.function,
725
+ id: (_a = call.id) !== null && _a !== void 0 ? _a : randomString(),
726
+ type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
727
+ });
728
+ });
729
+ return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
730
+ case 3:
731
+ _h.sent();
720
732
  _loop_1 = function (tool) {
721
733
  var targetFn, result_2, result_3;
722
- return __generator(this, function (_k) {
723
- switch (_k.label) {
734
+ return __generator(this, function (_j) {
735
+ switch (_j.label) {
724
736
  case 0:
725
- targetFn = (_d = this_1.params.tools) === null || _d === void 0 ? void 0 : _d.find(function (t) { return t.function.name === tool.function.name; });
726
- return [4 /*yield*/, this_1.params.history.push(__assign(__assign({}, message), { agentName: this_1.params.agentName }))];
727
- case 1:
728
- _k.sent();
729
- if (!!targetFn) return [3 /*break*/, 4];
737
+ targetFn = (_b = this_1.params.tools) === null || _b === void 0 ? void 0 : _b.find(function (t) { return t.function.name === tool.function.name; });
738
+ if (!!targetFn) return [3 /*break*/, 3];
730
739
  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);
731
740
  return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
732
- case 2:
733
- result_2 = _k.sent();
741
+ case 1:
742
+ result_2 = _j.sent();
734
743
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
735
744
  return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
736
- case 3:
737
- _k.sent();
745
+ case 2:
746
+ _j.sent();
738
747
  return [2 /*return*/, { value: void 0 }];
739
- case 4:
740
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onValidate) &&
741
- ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
748
+ case 3:
749
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate) &&
750
+ ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
742
751
  return [4 /*yield*/, not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
743
- case 5:
744
- if (!_k.sent()) return [3 /*break*/, 8];
752
+ case 4:
753
+ if (!_j.sent()) return [3 /*break*/, 7];
745
754
  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"));
746
755
  return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
747
- case 6:
748
- result_3 = _k.sent();
756
+ case 5:
757
+ result_3 = _j.sent();
749
758
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
750
759
  return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
751
- case 7:
752
- _k.sent();
760
+ case 6:
761
+ _j.sent();
753
762
  return [2 /*return*/, { value: void 0 }];
754
- case 8:
755
- ((_g = targetFn.callbacks) === null || _g === void 0 ? void 0 : _g.onCall) &&
756
- ((_h = targetFn.callbacks) === null || _h === void 0 ? void 0 : _h.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
763
+ case 7:
764
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onCall) &&
765
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
757
766
  /**
758
767
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
759
768
  */
760
- targetFn.call(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
769
+ targetFn.call(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
761
770
  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"));
762
771
  return [4 /*yield*/, Promise.race([
763
772
  this_1._toolCommitSubject.toPromise(),
764
773
  this_1._outputSubject.toPromise(),
765
774
  ])];
766
- case 9:
767
- _k.sent();
775
+ case 8:
776
+ _j.sent();
768
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 end"));
769
778
  return [2 /*return*/];
770
779
  }
771
780
  });
772
781
  };
773
782
  this_1 = this;
774
- _j.label = 3;
775
- case 3:
776
- _j.trys.push([3, 8, 9, 10]);
777
- _a = __values(message.tool_calls), _b = _a.next();
778
- _j.label = 4;
783
+ _h.label = 4;
779
784
  case 4:
780
- if (!!_b.done) return [3 /*break*/, 7];
781
- tool = _b.value;
782
- return [5 /*yield**/, _loop_1(tool)];
785
+ _h.trys.push([4, 9, 10, 11]);
786
+ toolCalls_1 = __values(toolCalls), toolCalls_1_1 = toolCalls_1.next();
787
+ _h.label = 5;
783
788
  case 5:
784
- state_1 = _j.sent();
789
+ if (!!toolCalls_1_1.done) return [3 /*break*/, 8];
790
+ tool = toolCalls_1_1.value;
791
+ return [5 /*yield**/, _loop_1(tool)];
792
+ case 6:
793
+ state_1 = _h.sent();
785
794
  if (typeof state_1 === "object")
786
795
  return [2 /*return*/, state_1.value];
787
- _j.label = 6;
788
- case 6:
789
- _b = _a.next();
790
- return [3 /*break*/, 4];
791
- case 7: return [3 /*break*/, 10];
792
- case 8:
793
- e_1_1 = _j.sent();
794
- e_1 = { error: e_1_1 };
795
- return [3 /*break*/, 10];
796
+ _h.label = 7;
797
+ case 7:
798
+ toolCalls_1_1 = toolCalls_1.next();
799
+ return [3 /*break*/, 5];
800
+ case 8: return [3 /*break*/, 11];
796
801
  case 9:
802
+ e_1_1 = _h.sent();
803
+ e_1 = { error: e_1_1 };
804
+ return [3 /*break*/, 11];
805
+ case 10:
797
806
  try {
798
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
807
+ if (toolCalls_1_1 && !toolCalls_1_1.done && (_a = toolCalls_1.return)) _a.call(toolCalls_1);
799
808
  }
800
809
  finally { if (e_1) throw e_1.error; }
801
810
  return [7 /*endfinally*/];
802
- case 10: return [2 /*return*/];
803
811
  case 11:
812
+ ((_g = this.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
813
+ this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
814
+ return [2 /*return*/];
815
+ case 12:
804
816
  if (!message.tool_calls) {
805
817
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
806
818
  }
807
819
  result = message.content;
808
820
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
809
- case 12:
810
- _j.sent();
821
+ case 13:
822
+ _h.sent();
811
823
  validation = null;
812
824
  return [4 /*yield*/, this.params.validate(result)];
813
- case 13:
814
- if (!(validation = _j.sent())) return [3 /*break*/, 16];
825
+ case 14:
826
+ if (!(validation = _h.sent())) return [3 /*break*/, 17];
815
827
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
816
828
  return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
817
- case 14:
818
- result1 = _j.sent();
819
- return [4 /*yield*/, this._emitOuput(mode, result1)];
820
829
  case 15:
821
- _j.sent();
822
- return [2 /*return*/];
830
+ result1 = _h.sent();
831
+ return [4 /*yield*/, this._emitOuput(mode, result1)];
823
832
  case 16:
833
+ _h.sent();
834
+ return [2 /*return*/];
835
+ case 17:
824
836
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
825
837
  return [4 /*yield*/, this._emitOuput(mode, result)];
826
- case 17:
827
- _j.sent();
838
+ case 18:
839
+ _h.sent();
828
840
  return [2 /*return*/];
829
841
  }
830
842
  });
@@ -916,17 +928,19 @@ var AgentConnectionService = /** @class */ (function () {
916
928
  /**
917
929
  * Commits tool output.
918
930
  * @param {string} content - The tool output content.
931
+ * @param {string} toolId - The `tool_call_id` for openai history
919
932
  * @returns {Promise<any>} The commit result.
920
933
  */
921
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
934
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
922
935
  return __generator(this, function (_a) {
923
936
  switch (_a.label) {
924
937
  case 0:
925
938
  this.loggerService.log("agentConnectionService commitToolOutput", {
926
939
  content: content,
940
+ toolId: toolId,
927
941
  context: this.contextService.context,
928
942
  });
929
- return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitToolOutput(content)];
943
+ return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitToolOutput(toolId, content)];
930
944
  case 1: return [2 /*return*/, _a.sent()];
931
945
  }
932
946
  });
@@ -1782,21 +1796,23 @@ var ClientSession = /** @class */ (function () {
1782
1796
  }); };
1783
1797
  /**
1784
1798
  * Commits tool output.
1799
+ * @param {string} toolId - The `tool_call_id` for openai history
1785
1800
  * @param {string} content - The content to commit.
1786
1801
  * @returns {Promise<void>}
1787
1802
  */
1788
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
1803
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
1789
1804
  var agent;
1790
1805
  return __generator(this, function (_a) {
1791
1806
  switch (_a.label) {
1792
1807
  case 0:
1793
1808
  this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " commitToolOutput"), {
1794
1809
  content: content,
1810
+ toolId: toolId,
1795
1811
  });
1796
1812
  return [4 /*yield*/, this.params.swarm.getAgent()];
1797
1813
  case 1:
1798
1814
  agent = _a.sent();
1799
- return [4 /*yield*/, agent.commitToolOutput(content)];
1815
+ return [4 /*yield*/, agent.commitToolOutput(toolId, content)];
1800
1816
  case 2: return [2 /*return*/, _a.sent()];
1801
1817
  }
1802
1818
  });
@@ -2004,18 +2020,20 @@ var SessionConnectionService = /** @class */ (function () {
2004
2020
  };
2005
2021
  /**
2006
2022
  * Commits tool output to the session.
2023
+ * @param {string} toolId - The `tool_call_id` for openai history
2007
2024
  * @param {string} content - The content to commit.
2008
2025
  * @returns {Promise<void>} A promise that resolves when the content is committed.
2009
2026
  */
2010
- this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
2027
+ this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
2011
2028
  return __generator(this, function (_a) {
2012
2029
  switch (_a.label) {
2013
2030
  case 0:
2014
2031
  this.loggerService.log("sessionConnectionService commitToolOutput", {
2015
2032
  context: this.contextService.context,
2016
2033
  content: content,
2034
+ toolId: toolId,
2017
2035
  });
2018
- return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).commitToolOutput(content)];
2036
+ return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).commitToolOutput(toolId, content)];
2019
2037
  case 1: return [2 /*return*/, _a.sent()];
2020
2038
  }
2021
2039
  });
@@ -2202,12 +2220,13 @@ var AgentPublicService = /** @class */ (function () {
2202
2220
  }); };
2203
2221
  /**
2204
2222
  * Commits tool output to the agent.
2223
+ * @param {string} toolId - The `tool_call_id` for openai history
2205
2224
  * @param {string} content - The content to commit.
2206
2225
  * @param {string} clientId - The client ID.
2207
2226
  * @param {AgentName} agentName - The name of the agent.
2208
2227
  * @returns {Promise<unknown>} The commit result.
2209
2228
  */
2210
- this.commitToolOutput = function (content, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
2229
+ this.commitToolOutput = function (toolId, content, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
2211
2230
  var _this = this;
2212
2231
  return __generator(this, function (_a) {
2213
2232
  switch (_a.label) {
@@ -2215,12 +2234,13 @@ var AgentPublicService = /** @class */ (function () {
2215
2234
  this.loggerService.log("agentPublicService commitToolOutput", {
2216
2235
  content: content,
2217
2236
  clientId: clientId,
2237
+ toolId: toolId,
2218
2238
  agentName: agentName,
2219
2239
  });
2220
2240
  return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
2221
2241
  return __generator(this, function (_a) {
2222
2242
  switch (_a.label) {
2223
- case 0: return [4 /*yield*/, this.agentConnectionService.commitToolOutput(content)];
2243
+ case 0: return [4 /*yield*/, this.agentConnectionService.commitToolOutput(toolId, content)];
2224
2244
  case 1: return [2 /*return*/, _a.sent()];
2225
2245
  }
2226
2246
  });
@@ -2630,17 +2650,19 @@ var SessionPublicService = /** @class */ (function () {
2630
2650
  };
2631
2651
  /**
2632
2652
  * Commits tool output to the session.
2653
+ * @param {string} toolId - The `tool_call_id` for openai history
2633
2654
  * @param {string} content - The content to commit.
2634
2655
  * @param {string} clientId - The client ID.
2635
2656
  * @param {SwarmName} swarmName - The swarm name.
2636
2657
  * @returns {Promise<void>}
2637
2658
  */
2638
- this.commitToolOutput = function (content, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
2659
+ this.commitToolOutput = function (toolId, content, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
2639
2660
  var _this = this;
2640
2661
  return __generator(this, function (_a) {
2641
2662
  switch (_a.label) {
2642
2663
  case 0:
2643
2664
  this.loggerService.log("sessionPublicService commitToolOutput", {
2665
+ toolId: toolId,
2644
2666
  content: content,
2645
2667
  clientId: clientId,
2646
2668
  swarmName: swarmName,
@@ -2648,7 +2670,7 @@ var SessionPublicService = /** @class */ (function () {
2648
2670
  return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
2649
2671
  return __generator(this, function (_a) {
2650
2672
  switch (_a.label) {
2651
- case 0: return [4 /*yield*/, this.sessionConnectionService.commitToolOutput(content)];
2673
+ case 0: return [4 /*yield*/, this.sessionConnectionService.commitToolOutput(toolId, content)];
2652
2674
  case 1: return [2 /*return*/, _a.sent()];
2653
2675
  }
2654
2676
  });
@@ -4149,12 +4171,13 @@ var getAgentHistory = function (clientId, agentName) { return __awaiter(void 0,
4149
4171
  * @param {AgentName} agentName - The name of the agent committing the output.
4150
4172
  * @returns {Promise<void>} - A promise that resolves when the operation is complete.
4151
4173
  */
4152
- var commitToolOutput = function (content, clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
4174
+ var commitToolOutput = function (toolId, content, clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
4153
4175
  var swarmName, currentAgentName;
4154
4176
  return __generator(this, function (_a) {
4155
4177
  switch (_a.label) {
4156
4178
  case 0:
4157
4179
  swarm.loggerService.log('function commitToolOutput', {
4180
+ toolId: toolId,
4158
4181
  content: content,
4159
4182
  clientId: clientId,
4160
4183
  agentName: agentName,
@@ -4168,13 +4191,14 @@ var commitToolOutput = function (content, clientId, agentName) { return __awaite
4168
4191
  currentAgentName = _a.sent();
4169
4192
  if (currentAgentName !== agentName) {
4170
4193
  swarm.loggerService.log('function "commitToolOutput" skipped due to the agent change', {
4194
+ toolId: toolId,
4171
4195
  currentAgentName: currentAgentName,
4172
4196
  agentName: agentName,
4173
4197
  clientId: clientId,
4174
4198
  });
4175
4199
  return [2 /*return*/];
4176
4200
  }
4177
- return [4 /*yield*/, swarm.sessionPublicService.commitToolOutput(content, clientId, swarmName)];
4201
+ return [4 /*yield*/, swarm.sessionPublicService.commitToolOutput(toolId, content, clientId, swarmName)];
4178
4202
  case 2:
4179
4203
  _a.sent();
4180
4204
  return [2 /*return*/];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.37",
3
+ "version": "1.0.39",
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
@@ -218,10 +218,11 @@ interface ISession {
218
218
  connect(connector: SendMessageFn$1): ReceiveMessageFn;
219
219
  /**
220
220
  * Commit tool output.
221
+ * @param {string} toolId - The `tool_call_id` for openai history
221
222
  * @param {string} content - The content to commit.
222
223
  * @returns {Promise<void>}
223
224
  */
224
- commitToolOutput(content: string): Promise<void>;
225
+ commitToolOutput(toolId: string, content: string): Promise<void>;
225
226
  /**
226
227
  * Commit user message without answer
227
228
  * @param {string} message - The message to commit.
@@ -257,6 +258,86 @@ type SessionMode = "session" | "makeConnection" | "complete";
257
258
  */
258
259
  type ExecutionMode = "tool" | "user";
259
260
 
261
+ /**
262
+ * Represents a tool call with a function name and arguments.
263
+ */
264
+ interface IToolCall {
265
+ /**
266
+ * The ID of the tool call.
267
+ */
268
+ id: string;
269
+ /**
270
+ * The type of the tool. Currently, only `function` is supported.
271
+ */
272
+ type: 'function';
273
+ /**
274
+ * The function that the model called.
275
+ */
276
+ function: {
277
+ /**
278
+ * The name of the function to be called.
279
+ */
280
+ name: string;
281
+ /**
282
+ * The arguments to be passed to the function.
283
+ */
284
+ arguments: {
285
+ [key: string]: any;
286
+ };
287
+ };
288
+ }
289
+ /**
290
+ * Represents a tool with a type and function details.
291
+ */
292
+ interface ITool {
293
+ /**
294
+ * The type of the tool.
295
+ */
296
+ type: string;
297
+ function: {
298
+ /**
299
+ * The name of the function.
300
+ */
301
+ name: string;
302
+ /**
303
+ * The description of the function.
304
+ */
305
+ description: string;
306
+ /**
307
+ * The parameters required by the function.
308
+ */
309
+ parameters: {
310
+ /**
311
+ * The type of the parameters.
312
+ */
313
+ type: string;
314
+ /**
315
+ * The list of required parameters.
316
+ */
317
+ required: string[];
318
+ /**
319
+ * The properties of the parameters.
320
+ */
321
+ properties: {
322
+ [key: string]: {
323
+ /**
324
+ * The type of the property.
325
+ */
326
+ type: string;
327
+ /**
328
+ * The description of the property.
329
+ */
330
+ description: string;
331
+ /**
332
+ * The possible values for the property.
333
+ */
334
+ enum?: string[];
335
+ };
336
+ };
337
+ };
338
+ };
339
+ }
340
+
260
341
  /**
261
342
  * Interface representing a model message.
262
343
  */
@@ -285,14 +366,11 @@ interface IModelMessage {
285
366
  * Optional tool calls associated with the message.
286
367
  * @type {Array<{ function: { name: string; arguments: { [key: string]: any; }; }; }>}
287
368
  */
288
- tool_calls?: {
289
- function: {
290
- name: string;
291
- arguments: {
292
- [key: string]: any;
293
- };
294
- };
295
- }[];
369
+ tool_calls?: IToolCall[];
370
+ /**
371
+ * Tool call that this message is responding to.
372
+ */
373
+ tool_call_id?: string;
296
374
  }
297
375
 
298
376
  /**
@@ -348,75 +426,6 @@ interface IHistorySchema {
348
426
  items: IPubsubArray<IModelMessage>;
349
427
  }
350
428
 
351
- /**
352
- * Represents a tool call with a function name and arguments.
353
- */
354
- interface IToolCall {
355
- function: {
356
- /**
357
- * The name of the function to be called.
358
- */
359
- name: string;
360
- /**
361
- * The arguments to be passed to the function.
362
- */
363
- arguments: {
364
- [key: string]: any;
365
- };
366
- };
367
- }
368
- /**
369
- * Represents a tool with a type and function details.
370
- */
371
- interface ITool {
372
- /**
373
- * The type of the tool.
374
- */
375
- type: string;
376
- function: {
377
- /**
378
- * The name of the function.
379
- */
380
- name: string;
381
- /**
382
- * The description of the function.
383
- */
384
- description: string;
385
- /**
386
- * The parameters required by the function.
387
- */
388
- parameters: {
389
- /**
390
- * The type of the parameters.
391
- */
392
- type: string;
393
- /**
394
- * The list of required parameters.
395
- */
396
- required: string[];
397
- /**
398
- * The properties of the parameters.
399
- */
400
- properties: {
401
- [key: string]: {
402
- /**
403
- * The type of the property.
404
- */
405
- type: string;
406
- /**
407
- * The description of the property.
408
- */
409
- description: string;
410
- /**
411
- * The possible values for the property.
412
- */
413
- enum?: string[];
414
- };
415
- };
416
- };
417
- };
418
- }
419
-
420
429
  /**
421
430
  * Interface representing a completion.
422
431
  */
@@ -518,7 +527,7 @@ interface IAgentTool<T = Record<string, unknown>> extends ITool {
518
527
  * @param params - The parameters for the tool.
519
528
  * @returns A promise that resolves when the tool call is complete.
520
529
  */
521
- call(clientId: string, agentName: AgentName, params: T): Promise<void>;
530
+ call(toolId: string, clientId: string, agentName: AgentName, params: T): Promise<void>;
522
531
  /**
523
532
  * Validates the parameters for the tool.
524
533
  * @param clientId - The ID of the client.
@@ -569,11 +578,12 @@ interface IAgentSchemaCallbacks {
569
578
  onExecute?: (clientId: string, agentName: AgentName, input: string, mode: ExecutionMode) => void;
570
579
  /**
571
580
  * Callback triggered when there is tool output.
581
+ * @param toolId - The `tool_call_id` for openai history
572
582
  * @param clientId - The ID of the client.
573
583
  * @param agentName - The name of the agent.
574
584
  * @param content - The content of the tool output.
575
585
  */
576
- onToolOutput?: (clientId: string, agentName: AgentName, content: string) => void;
586
+ onToolOutput?: (toolId: string, clientId: string, agentName: AgentName, content: string) => void;
577
587
  /**
578
588
  * Callback triggered when there is a system message.
579
589
  * @param clientId - The ID of the client.
@@ -621,6 +631,13 @@ interface IAgentSchemaCallbacks {
621
631
  * @param agentName - The name of the agent.
622
632
  */
623
633
  onDispose?: (clientId: string, agentName: AgentName) => void;
634
+ /**
635
+ * Callback triggered after all tools are called
636
+ * @param clientId - The ID of the client.
637
+ * @param agentName - The name of the agent.
638
+ * @param toolCalls - The array of tool calls
639
+ */
640
+ onAfterToolCalls?: (clientId: string, agentName: AgentName, toolCalls: IToolCall[]) => void;
624
641
  }
625
642
  /**
626
643
  * Interface representing the schema for an agent.
@@ -665,10 +682,11 @@ interface IAgent {
665
682
  waitForOutput: () => Promise<string>;
666
683
  /**
667
684
  * Commits the tool output.
685
+ * @param {string} toolId - The `tool_call_id` for openai history
668
686
  * @param content - The content of the tool output.
669
687
  * @returns A promise that resolves when the tool output is committed.
670
688
  */
671
- commitToolOutput(content: string): Promise<void>;
689
+ commitToolOutput(toolId: string, content: string): Promise<void>;
672
690
  /**
673
691
  * Commits a system message.
674
692
  * @param message - The system message to commit.
@@ -793,7 +811,7 @@ declare class ClientAgent implements IAgent {
793
811
  * @param {string} content - The tool output content.
794
812
  * @returns {Promise<void>}
795
813
  */
796
- commitToolOutput: (content: string) => Promise<void>;
814
+ commitToolOutput: (toolId: string, content: string) => Promise<void>;
797
815
  /**
798
816
  * Executes the incoming message and processes tool calls if any.
799
817
  * @param {string} incoming - The incoming message content.
@@ -840,9 +858,10 @@ declare class AgentConnectionService implements IAgent {
840
858
  /**
841
859
  * Commits tool output.
842
860
  * @param {string} content - The tool output content.
861
+ * @param {string} toolId - The `tool_call_id` for openai history
843
862
  * @returns {Promise<any>} The commit result.
844
863
  */
845
- commitToolOutput: (content: string) => Promise<void>;
864
+ commitToolOutput: (toolId: string, content: string) => Promise<void>;
846
865
  /**
847
866
  * Commits a system message.
848
867
  * @param {string} message - The system message.
@@ -1148,10 +1167,11 @@ declare class ClientSession implements ISession {
1148
1167
  execute: (message: string, mode: ExecutionMode) => Promise<string>;
1149
1168
  /**
1150
1169
  * Commits tool output.
1170
+ * @param {string} toolId - The `tool_call_id` for openai history
1151
1171
  * @param {string} content - The content to commit.
1152
1172
  * @returns {Promise<void>}
1153
1173
  */
1154
- commitToolOutput: (content: string) => Promise<void>;
1174
+ commitToolOutput: (toolId: string, content: string) => Promise<void>;
1155
1175
  /**
1156
1176
  * Commits user message without answer.
1157
1177
  * @param {string} message - The message to commit.
@@ -1218,10 +1238,11 @@ declare class SessionConnectionService implements ISession {
1218
1238
  connect: (connector: SendMessageFn$1) => ReceiveMessageFn;
1219
1239
  /**
1220
1240
  * Commits tool output to the session.
1241
+ * @param {string} toolId - The `tool_call_id` for openai history
1221
1242
  * @param {string} content - The content to commit.
1222
1243
  * @returns {Promise<void>} A promise that resolves when the content is committed.
1223
1244
  */
1224
- commitToolOutput: (content: string) => Promise<void>;
1245
+ commitToolOutput: (toolId: string, content: string) => Promise<void>;
1225
1246
  /**
1226
1247
  * Commits a system message to the session.
1227
1248
  * @param {string} message - The message to commit.
@@ -1285,12 +1306,13 @@ declare class AgentPublicService implements TAgentConnectionService {
1285
1306
  waitForOutput: (clientId: string, agentName: AgentName) => Promise<string>;
1286
1307
  /**
1287
1308
  * Commits tool output to the agent.
1309
+ * @param {string} toolId - The `tool_call_id` for openai history
1288
1310
  * @param {string} content - The content to commit.
1289
1311
  * @param {string} clientId - The client ID.
1290
1312
  * @param {AgentName} agentName - The name of the agent.
1291
1313
  * @returns {Promise<unknown>} The commit result.
1292
1314
  */
1293
- commitToolOutput: (content: string, clientId: string, agentName: AgentName) => Promise<void>;
1315
+ commitToolOutput: (toolId: string, content: string, clientId: string, agentName: AgentName) => Promise<void>;
1294
1316
  /**
1295
1317
  * Commits a system message to the agent.
1296
1318
  * @param {string} message - The message to commit.
@@ -1410,12 +1432,13 @@ declare class SessionPublicService implements TSessionConnectionService {
1410
1432
  connect: (connector: SendMessageFn$1, clientId: string, swarmName: SwarmName) => ReceiveMessageFn;
1411
1433
  /**
1412
1434
  * Commits tool output to the session.
1435
+ * @param {string} toolId - The `tool_call_id` for openai history
1413
1436
  * @param {string} content - The content to commit.
1414
1437
  * @param {string} clientId - The client ID.
1415
1438
  * @param {SwarmName} swarmName - The swarm name.
1416
1439
  * @returns {Promise<void>}
1417
1440
  */
1418
- commitToolOutput: (content: string, clientId: string, swarmName: SwarmName) => Promise<void>;
1441
+ commitToolOutput: (toolId: string, content: string, clientId: string, swarmName: SwarmName) => Promise<void>;
1419
1442
  /**
1420
1443
  * Commits a system message to the session.
1421
1444
  * @param {string} message - The message to commit.
@@ -1907,7 +1930,7 @@ declare const getAgentHistory: (clientId: string, agentName: AgentName) => Promi
1907
1930
  * @param {AgentName} agentName - The name of the agent committing the output.
1908
1931
  * @returns {Promise<void>} - A promise that resolves when the operation is complete.
1909
1932
  */
1910
- declare const commitToolOutput: (content: string, clientId: string, agentName: AgentName) => Promise<void>;
1933
+ declare const commitToolOutput: (toolId: string, content: string, clientId: string, agentName: AgentName) => Promise<void>;
1911
1934
 
1912
1935
  /**
1913
1936
  * Commits a system message to the active agent in as swarm.