agent-swarm-kit 1.0.144 → 1.0.146

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
@@ -1817,22 +1817,18 @@ var AGENT_CHANGE_SYMBOL = Symbol("agent-change");
1817
1817
  var MODEL_RESQUE_SYMBOL = Symbol("model-resque");
1818
1818
  var TOOL_ERROR_SYMBOL = Symbol("tool-error");
1819
1819
  var TOOL_STOP_SYMBOL = Symbol("tool-stop");
1820
- var TOOL_NO_OUTPUT_WARNING = 15000;
1820
+ var TOOL_NO_OUTPUT_WARNING_TIMEOUT = 15000;
1821
+ var TOOL_NO_OUTPUT_WARNING_SYMBOL = Symbol("tool-warning-timeout");
1821
1822
  var createPlaceholder = function () {
1822
1823
  return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
1823
1824
  };
1824
- var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued) { return __awaiter(void 0, void 0, void 0, function () {
1825
+ var createToolCall = function (idx, tool, toolCalls, targetFn, self) { return __awaiter(void 0, void 0, void 0, function () {
1825
1826
  var error_1;
1826
1827
  var _a, _b, _c, _d;
1827
1828
  return __generator(this, function (_e) {
1828
1829
  switch (_e.label) {
1829
1830
  case 0:
1830
- if (isResqued()) {
1831
- return [2 /*return*/];
1832
- }
1833
- _e.label = 1;
1834
- case 1:
1835
- _e.trys.push([1, 3, , 4]);
1831
+ _e.trys.push([0, 2, , 3]);
1836
1832
  return [4 /*yield*/, targetFn.call({
1837
1833
  toolId: tool.id,
1838
1834
  clientId: self.params.clientId,
@@ -1841,12 +1837,12 @@ var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued)
1841
1837
  isLast: idx === toolCalls.length - 1,
1842
1838
  toolCalls: toolCalls,
1843
1839
  })];
1844
- case 2:
1840
+ case 1:
1845
1841
  _e.sent();
1846
1842
  ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
1847
1843
  ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
1848
- return [3 /*break*/, 4];
1849
- case 3:
1844
+ return [3 /*break*/, 3];
1845
+ case 2:
1850
1846
  error_1 = _e.sent();
1851
1847
  console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(functoolsKit.getErrorMessage(error_1)), {
1852
1848
  clientId: self.params.clientId,
@@ -1858,8 +1854,8 @@ var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued)
1858
1854
  ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onCallError) &&
1859
1855
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onCallError(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments, error_1));
1860
1856
  self._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
1861
- return [3 /*break*/, 4];
1862
- case 4: return [2 /*return*/];
1857
+ return [3 /*break*/, 3];
1858
+ case 3: return [2 /*return*/];
1863
1859
  }
1864
1860
  });
1865
1861
  }); };
@@ -1938,7 +1934,7 @@ var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0
1938
1934
  });
1939
1935
  }); };
1940
1936
  var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void 0, void 0, function () {
1941
- var rawMessage, message, toolCalls_1, lastToolCallRef, isResqued_1, unResque, _loop_1, idx, state_1, result, validation, result1;
1937
+ var rawMessage, message, toolCalls_1, lastToolStatusRef, _loop_1, idx, state_1, result, validation, result1;
1942
1938
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1943
1939
  return __generator(this, function (_k) {
1944
1940
  switch (_k.label) {
@@ -1977,41 +1973,14 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1977
1973
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1978
1974
  case 4:
1979
1975
  _k.sent();
1980
- lastToolCallRef = Promise.resolve(null);
1981
- isResqued_1 = false;
1982
- {
1983
- unResque = self._modelResqueSubject.once(function () {
1984
- isResqued_1 = false;
1985
- });
1986
- /**
1987
- * Effective way of garbage collection cause the agent
1988
- * will defenitely say something or will be recreated on change
1989
- *
1990
- * On navigation:
1991
- *
1992
- * 1. Agent.dispose
1993
- * 2. Agent.createAgentRef
1994
- * 3. Swarm.setAgentRef
1995
- *
1996
- * That means the _outputSubject being marked for GC so this
1997
- * does not matter are we listening it or not
1998
- *
1999
- * @see /src/function/navigate/changeToAgent
2000
- */
2001
- self._outputSubject.once(unResque);
2002
- }
1976
+ lastToolStatusRef = Promise.resolve(null);
2003
1977
  _loop_1 = function (idx) {
2004
- var tool, targetFn, result_1, result_2, statusAwaiter, isResolved, status, result_3;
1978
+ var tool, targetFn, result_1, result_2, status, result_3;
2005
1979
  return __generator(this, function (_l) {
2006
1980
  switch (_l.label) {
2007
1981
  case 0:
2008
1982
  tool = toolCalls_1[idx];
2009
1983
  targetFn = (_a = self.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
2010
- if (isResqued_1) {
2011
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2012
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool execution canceled due to the model was resqued in the chain"), self.params.tools);
2013
- return [2 /*return*/, { value: void 0 }];
2014
- }
2015
1984
  if (!!targetFn) return [3 /*break*/, 3];
2016
1985
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2017
1986
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool function not found"), self.params.tools);
@@ -2020,7 +1989,7 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2020
1989
  result_1 = _l.sent();
2021
1990
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2022
1991
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_1));
2023
- return [4 /*yield*/, self._emitOuput(mode, result_1)];
1992
+ return [4 /*yield*/, self._emitOutput(mode, result_1)];
2024
1993
  case 2:
2025
1994
  _l.sent();
2026
1995
  return [2 /*return*/, { value: void 0 }];
@@ -2042,40 +2011,55 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2042
2011
  result_2 = _l.sent();
2043
2012
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2044
2013
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_2));
2045
- return [4 /*yield*/, self._emitOuput(mode, result_2)];
2014
+ return [4 /*yield*/, self._emitOutput(mode, result_2)];
2046
2015
  case 6:
2047
2016
  _l.sent();
2048
2017
  return [2 /*return*/, { value: void 0 }];
2049
2018
  case 7:
2050
2019
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
2051
2020
  ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
2052
- statusAwaiter = Promise.race([
2053
- self._agentChangeSubject.toPromise(),
2054
- self._toolCommitSubject.toPromise(),
2055
- self._toolErrorSubject.toPromise(),
2056
- self._toolStopSubject.toPromise(),
2057
- self._outputSubject.toPromise(),
2058
- self._modelResqueSubject.toPromise(),
2059
- ]);
2060
2021
  /**
2061
- * Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
2022
+ * Do not await directly to avoid the deadlock! The tool can send the message to other agents by emulating user messages
2062
2023
  */
2063
2024
  {
2064
- lastToolCallRef.then(function () { return createToolCall(idx, tool, toolCalls_1, targetFn, self, function () { return isResqued_1; }); });
2065
- lastToolCallRef = lastToolCallRef.then(function () { return statusAwaiter; });
2025
+ lastToolStatusRef.then(function (lastStatus) {
2026
+ if (lastStatus === MODEL_RESQUE_SYMBOL) {
2027
+ return;
2028
+ }
2029
+ if (lastStatus === AGENT_CHANGE_SYMBOL) {
2030
+ return;
2031
+ }
2032
+ if (lastStatus === TOOL_STOP_SYMBOL) {
2033
+ return;
2034
+ }
2035
+ if (lastStatus === TOOL_ERROR_SYMBOL) {
2036
+ return;
2037
+ }
2038
+ return createToolCall(idx, tool, toolCalls_1, targetFn, self);
2039
+ });
2040
+ lastToolStatusRef = lastToolStatusRef.then(function () {
2041
+ return Promise.race([
2042
+ self._agentChangeSubject.toPromise(),
2043
+ self._toolCommitSubject.toPromise(),
2044
+ self._toolErrorSubject.toPromise(),
2045
+ self._toolStopSubject.toPromise(),
2046
+ self._resqueSubject.toPromise(),
2047
+ ]);
2048
+ });
2066
2049
  }
2067
2050
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2068
2051
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
2069
- isResolved = false;
2070
- functoolsKit.sleep(TOOL_NO_OUTPUT_WARNING).then(function () {
2071
- if (!isResolved) {
2072
- console.warn("agent-swarm no tool output after ".concat(TOOL_NO_OUTPUT_WARNING, "ms clientId=").concat(self.params.clientId, " agentName=").concat(self.params.agentName, " toolId=").concat(tool.id, " functionName=").concat(tool.function.name));
2052
+ Promise.race([
2053
+ functoolsKit.sleep(TOOL_NO_OUTPUT_WARNING_TIMEOUT).then(function () { return TOOL_NO_OUTPUT_WARNING_SYMBOL; }),
2054
+ lastToolStatusRef,
2055
+ ]).then(function (result) {
2056
+ if (result === TOOL_NO_OUTPUT_WARNING_SYMBOL) {
2057
+ console.warn("agent-swarm no tool output after ".concat(TOOL_NO_OUTPUT_WARNING_TIMEOUT, "ms clientId=").concat(self.params.clientId, " agentName=").concat(self.params.agentName, " toolId=").concat(tool.id, " functionName=").concat(tool.function.name));
2073
2058
  }
2074
2059
  });
2075
- return [4 /*yield*/, statusAwaiter];
2060
+ return [4 /*yield*/, lastToolStatusRef];
2076
2061
  case 8:
2077
2062
  status = _l.sent();
2078
- isResolved = true;
2079
2063
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2080
2064
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2081
2065
  if (status === MODEL_RESQUE_SYMBOL) {
@@ -2107,7 +2091,7 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2107
2091
  result_3 = _l.sent();
2108
2092
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2109
2093
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2110
- return [4 /*yield*/, self._emitOuput(mode, result_3)];
2094
+ return [4 /*yield*/, self._emitOutput(mode, result_3)];
2111
2095
  case 10:
2112
2096
  _l.sent();
2113
2097
  return [2 /*return*/, { value: void 0 }];
@@ -2152,14 +2136,14 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2152
2136
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2153
2137
  case 13:
2154
2138
  result1 = _k.sent();
2155
- return [4 /*yield*/, self._emitOuput(mode, result1)];
2139
+ return [4 /*yield*/, self._emitOutput(mode, result1)];
2156
2140
  case 14:
2157
2141
  _k.sent();
2158
2142
  return [2 /*return*/];
2159
2143
  case 15:
2160
2144
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2161
2145
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2162
- return [4 /*yield*/, self._emitOuput(mode, result)];
2146
+ return [4 /*yield*/, self._emitOutput(mode, result)];
2163
2147
  case 16:
2164
2148
  _k.sent();
2165
2149
  return [2 /*return*/];
@@ -2179,7 +2163,7 @@ var ClientAgent = /** @class */ (function () {
2179
2163
  var _this = this;
2180
2164
  this.params = params;
2181
2165
  this._agentChangeSubject = new functoolsKit.Subject();
2182
- this._modelResqueSubject = new functoolsKit.Subject();
2166
+ this._resqueSubject = new functoolsKit.Subject();
2183
2167
  this._toolErrorSubject = new functoolsKit.Subject();
2184
2168
  this._toolStopSubject = new functoolsKit.Subject();
2185
2169
  this._toolCommitSubject = new functoolsKit.Subject();
@@ -2218,7 +2202,7 @@ var ClientAgent = /** @class */ (function () {
2218
2202
  * @returns {Promise<void>}
2219
2203
  * @private
2220
2204
  */
2221
- ClientAgent.prototype._emitOuput = function (mode, rawResult) {
2205
+ ClientAgent.prototype._emitOutput = function (mode, rawResult) {
2222
2206
  return __awaiter(this, void 0, void 0, function () {
2223
2207
  var result, validation, rawResult_1, result_4;
2224
2208
  return __generator(this, function (_a) {
@@ -2227,7 +2211,7 @@ var ClientAgent = /** @class */ (function () {
2227
2211
  case 1:
2228
2212
  result = _a.sent();
2229
2213
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2230
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"), { mode: mode, result: result, rawResult: rawResult });
2214
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOutput"), { mode: mode, result: result, rawResult: rawResult });
2231
2215
  validation = null;
2232
2216
  return [4 /*yield*/, this.params.validate(result)];
2233
2217
  case 2:
@@ -2375,14 +2359,14 @@ var ClientAgent = /** @class */ (function () {
2375
2359
  })];
2376
2360
  case 13:
2377
2361
  _a.sent();
2378
- return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2362
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2379
2363
  case 14:
2380
2364
  _a.sent();
2381
2365
  return [2 /*return*/, content];
2382
2366
  case 15: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2383
2367
  case 16:
2384
2368
  _a.sent();
2385
- return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2369
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2386
2370
  case 17:
2387
2371
  _a.sent();
2388
2372
  return [2 /*return*/, result];
package/build/index.mjs CHANGED
@@ -1815,22 +1815,18 @@ var AGENT_CHANGE_SYMBOL = Symbol("agent-change");
1815
1815
  var MODEL_RESQUE_SYMBOL = Symbol("model-resque");
1816
1816
  var TOOL_ERROR_SYMBOL = Symbol("tool-error");
1817
1817
  var TOOL_STOP_SYMBOL = Symbol("tool-stop");
1818
- var TOOL_NO_OUTPUT_WARNING = 15000;
1818
+ var TOOL_NO_OUTPUT_WARNING_TIMEOUT = 15000;
1819
+ var TOOL_NO_OUTPUT_WARNING_SYMBOL = Symbol("tool-warning-timeout");
1819
1820
  var createPlaceholder = function () {
1820
1821
  return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
1821
1822
  };
1822
- var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued) { return __awaiter(void 0, void 0, void 0, function () {
1823
+ var createToolCall = function (idx, tool, toolCalls, targetFn, self) { return __awaiter(void 0, void 0, void 0, function () {
1823
1824
  var error_1;
1824
1825
  var _a, _b, _c, _d;
1825
1826
  return __generator(this, function (_e) {
1826
1827
  switch (_e.label) {
1827
1828
  case 0:
1828
- if (isResqued()) {
1829
- return [2 /*return*/];
1830
- }
1831
- _e.label = 1;
1832
- case 1:
1833
- _e.trys.push([1, 3, , 4]);
1829
+ _e.trys.push([0, 2, , 3]);
1834
1830
  return [4 /*yield*/, targetFn.call({
1835
1831
  toolId: tool.id,
1836
1832
  clientId: self.params.clientId,
@@ -1839,12 +1835,12 @@ var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued)
1839
1835
  isLast: idx === toolCalls.length - 1,
1840
1836
  toolCalls: toolCalls,
1841
1837
  })];
1842
- case 2:
1838
+ case 1:
1843
1839
  _e.sent();
1844
1840
  ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
1845
1841
  ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
1846
- return [3 /*break*/, 4];
1847
- case 3:
1842
+ return [3 /*break*/, 3];
1843
+ case 2:
1848
1844
  error_1 = _e.sent();
1849
1845
  console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(getErrorMessage(error_1)), {
1850
1846
  clientId: self.params.clientId,
@@ -1856,8 +1852,8 @@ var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued)
1856
1852
  ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onCallError) &&
1857
1853
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onCallError(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments, error_1));
1858
1854
  self._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
1859
- return [3 /*break*/, 4];
1860
- case 4: return [2 /*return*/];
1855
+ return [3 /*break*/, 3];
1856
+ case 3: return [2 /*return*/];
1861
1857
  }
1862
1858
  });
1863
1859
  }); };
@@ -1936,7 +1932,7 @@ var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0
1936
1932
  });
1937
1933
  }); };
1938
1934
  var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void 0, void 0, function () {
1939
- var rawMessage, message, toolCalls_1, lastToolCallRef, isResqued_1, unResque, _loop_1, idx, state_1, result, validation, result1;
1935
+ var rawMessage, message, toolCalls_1, lastToolStatusRef, _loop_1, idx, state_1, result, validation, result1;
1940
1936
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1941
1937
  return __generator(this, function (_k) {
1942
1938
  switch (_k.label) {
@@ -1975,41 +1971,14 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1975
1971
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1976
1972
  case 4:
1977
1973
  _k.sent();
1978
- lastToolCallRef = Promise.resolve(null);
1979
- isResqued_1 = false;
1980
- {
1981
- unResque = self._modelResqueSubject.once(function () {
1982
- isResqued_1 = false;
1983
- });
1984
- /**
1985
- * Effective way of garbage collection cause the agent
1986
- * will defenitely say something or will be recreated on change
1987
- *
1988
- * On navigation:
1989
- *
1990
- * 1. Agent.dispose
1991
- * 2. Agent.createAgentRef
1992
- * 3. Swarm.setAgentRef
1993
- *
1994
- * That means the _outputSubject being marked for GC so this
1995
- * does not matter are we listening it or not
1996
- *
1997
- * @see /src/function/navigate/changeToAgent
1998
- */
1999
- self._outputSubject.once(unResque);
2000
- }
1974
+ lastToolStatusRef = Promise.resolve(null);
2001
1975
  _loop_1 = function (idx) {
2002
- var tool, targetFn, result_1, result_2, statusAwaiter, isResolved, status, result_3;
1976
+ var tool, targetFn, result_1, result_2, status, result_3;
2003
1977
  return __generator(this, function (_l) {
2004
1978
  switch (_l.label) {
2005
1979
  case 0:
2006
1980
  tool = toolCalls_1[idx];
2007
1981
  targetFn = (_a = self.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
2008
- if (isResqued_1) {
2009
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2010
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool execution canceled due to the model was resqued in the chain"), self.params.tools);
2011
- return [2 /*return*/, { value: void 0 }];
2012
- }
2013
1982
  if (!!targetFn) return [3 /*break*/, 3];
2014
1983
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2015
1984
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool function not found"), self.params.tools);
@@ -2018,7 +1987,7 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2018
1987
  result_1 = _l.sent();
2019
1988
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2020
1989
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_1));
2021
- return [4 /*yield*/, self._emitOuput(mode, result_1)];
1990
+ return [4 /*yield*/, self._emitOutput(mode, result_1)];
2022
1991
  case 2:
2023
1992
  _l.sent();
2024
1993
  return [2 /*return*/, { value: void 0 }];
@@ -2040,40 +2009,55 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2040
2009
  result_2 = _l.sent();
2041
2010
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2042
2011
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_2));
2043
- return [4 /*yield*/, self._emitOuput(mode, result_2)];
2012
+ return [4 /*yield*/, self._emitOutput(mode, result_2)];
2044
2013
  case 6:
2045
2014
  _l.sent();
2046
2015
  return [2 /*return*/, { value: void 0 }];
2047
2016
  case 7:
2048
2017
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
2049
2018
  ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
2050
- statusAwaiter = Promise.race([
2051
- self._agentChangeSubject.toPromise(),
2052
- self._toolCommitSubject.toPromise(),
2053
- self._toolErrorSubject.toPromise(),
2054
- self._toolStopSubject.toPromise(),
2055
- self._outputSubject.toPromise(),
2056
- self._modelResqueSubject.toPromise(),
2057
- ]);
2058
2019
  /**
2059
- * Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
2020
+ * Do not await directly to avoid the deadlock! The tool can send the message to other agents by emulating user messages
2060
2021
  */
2061
2022
  {
2062
- lastToolCallRef.then(function () { return createToolCall(idx, tool, toolCalls_1, targetFn, self, function () { return isResqued_1; }); });
2063
- lastToolCallRef = lastToolCallRef.then(function () { return statusAwaiter; });
2023
+ lastToolStatusRef.then(function (lastStatus) {
2024
+ if (lastStatus === MODEL_RESQUE_SYMBOL) {
2025
+ return;
2026
+ }
2027
+ if (lastStatus === AGENT_CHANGE_SYMBOL) {
2028
+ return;
2029
+ }
2030
+ if (lastStatus === TOOL_STOP_SYMBOL) {
2031
+ return;
2032
+ }
2033
+ if (lastStatus === TOOL_ERROR_SYMBOL) {
2034
+ return;
2035
+ }
2036
+ return createToolCall(idx, tool, toolCalls_1, targetFn, self);
2037
+ });
2038
+ lastToolStatusRef = lastToolStatusRef.then(function () {
2039
+ return Promise.race([
2040
+ self._agentChangeSubject.toPromise(),
2041
+ self._toolCommitSubject.toPromise(),
2042
+ self._toolErrorSubject.toPromise(),
2043
+ self._toolStopSubject.toPromise(),
2044
+ self._resqueSubject.toPromise(),
2045
+ ]);
2046
+ });
2064
2047
  }
2065
2048
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2066
2049
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
2067
- isResolved = false;
2068
- sleep(TOOL_NO_OUTPUT_WARNING).then(function () {
2069
- if (!isResolved) {
2070
- console.warn("agent-swarm no tool output after ".concat(TOOL_NO_OUTPUT_WARNING, "ms clientId=").concat(self.params.clientId, " agentName=").concat(self.params.agentName, " toolId=").concat(tool.id, " functionName=").concat(tool.function.name));
2050
+ Promise.race([
2051
+ sleep(TOOL_NO_OUTPUT_WARNING_TIMEOUT).then(function () { return TOOL_NO_OUTPUT_WARNING_SYMBOL; }),
2052
+ lastToolStatusRef,
2053
+ ]).then(function (result) {
2054
+ if (result === TOOL_NO_OUTPUT_WARNING_SYMBOL) {
2055
+ console.warn("agent-swarm no tool output after ".concat(TOOL_NO_OUTPUT_WARNING_TIMEOUT, "ms clientId=").concat(self.params.clientId, " agentName=").concat(self.params.agentName, " toolId=").concat(tool.id, " functionName=").concat(tool.function.name));
2071
2056
  }
2072
2057
  });
2073
- return [4 /*yield*/, statusAwaiter];
2058
+ return [4 /*yield*/, lastToolStatusRef];
2074
2059
  case 8:
2075
2060
  status = _l.sent();
2076
- isResolved = true;
2077
2061
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2078
2062
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2079
2063
  if (status === MODEL_RESQUE_SYMBOL) {
@@ -2105,7 +2089,7 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2105
2089
  result_3 = _l.sent();
2106
2090
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2107
2091
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2108
- return [4 /*yield*/, self._emitOuput(mode, result_3)];
2092
+ return [4 /*yield*/, self._emitOutput(mode, result_3)];
2109
2093
  case 10:
2110
2094
  _l.sent();
2111
2095
  return [2 /*return*/, { value: void 0 }];
@@ -2150,14 +2134,14 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2150
2134
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2151
2135
  case 13:
2152
2136
  result1 = _k.sent();
2153
- return [4 /*yield*/, self._emitOuput(mode, result1)];
2137
+ return [4 /*yield*/, self._emitOutput(mode, result1)];
2154
2138
  case 14:
2155
2139
  _k.sent();
2156
2140
  return [2 /*return*/];
2157
2141
  case 15:
2158
2142
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2159
2143
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2160
- return [4 /*yield*/, self._emitOuput(mode, result)];
2144
+ return [4 /*yield*/, self._emitOutput(mode, result)];
2161
2145
  case 16:
2162
2146
  _k.sent();
2163
2147
  return [2 /*return*/];
@@ -2177,7 +2161,7 @@ var ClientAgent = /** @class */ (function () {
2177
2161
  var _this = this;
2178
2162
  this.params = params;
2179
2163
  this._agentChangeSubject = new Subject();
2180
- this._modelResqueSubject = new Subject();
2164
+ this._resqueSubject = new Subject();
2181
2165
  this._toolErrorSubject = new Subject();
2182
2166
  this._toolStopSubject = new Subject();
2183
2167
  this._toolCommitSubject = new Subject();
@@ -2216,7 +2200,7 @@ var ClientAgent = /** @class */ (function () {
2216
2200
  * @returns {Promise<void>}
2217
2201
  * @private
2218
2202
  */
2219
- ClientAgent.prototype._emitOuput = function (mode, rawResult) {
2203
+ ClientAgent.prototype._emitOutput = function (mode, rawResult) {
2220
2204
  return __awaiter(this, void 0, void 0, function () {
2221
2205
  var result, validation, rawResult_1, result_4;
2222
2206
  return __generator(this, function (_a) {
@@ -2225,7 +2209,7 @@ var ClientAgent = /** @class */ (function () {
2225
2209
  case 1:
2226
2210
  result = _a.sent();
2227
2211
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2228
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"), { mode: mode, result: result, rawResult: rawResult });
2212
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOutput"), { mode: mode, result: result, rawResult: rawResult });
2229
2213
  validation = null;
2230
2214
  return [4 /*yield*/, this.params.validate(result)];
2231
2215
  case 2:
@@ -2373,14 +2357,14 @@ var ClientAgent = /** @class */ (function () {
2373
2357
  })];
2374
2358
  case 13:
2375
2359
  _a.sent();
2376
- return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2360
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2377
2361
  case 14:
2378
2362
  _a.sent();
2379
2363
  return [2 /*return*/, content];
2380
2364
  case 15: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2381
2365
  case 16:
2382
2366
  _a.sent();
2383
- return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2367
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2384
2368
  case 17:
2385
2369
  _a.sent();
2386
2370
  return [2 /*return*/, result];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.144",
3
+ "version": "1.0.146",
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
@@ -1817,7 +1817,7 @@ declare const TOOL_STOP_SYMBOL: unique symbol;
1817
1817
  declare class ClientAgent implements IAgent {
1818
1818
  readonly params: IAgentParams;
1819
1819
  readonly _agentChangeSubject: Subject<typeof AGENT_CHANGE_SYMBOL>;
1820
- readonly _modelResqueSubject: Subject<typeof MODEL_RESQUE_SYMBOL>;
1820
+ readonly _resqueSubject: Subject<typeof MODEL_RESQUE_SYMBOL>;
1821
1821
  readonly _toolErrorSubject: Subject<typeof TOOL_ERROR_SYMBOL>;
1822
1822
  readonly _toolStopSubject: Subject<typeof TOOL_STOP_SYMBOL>;
1823
1823
  readonly _toolCommitSubject: Subject<void>;
@@ -1833,7 +1833,7 @@ declare class ClientAgent implements IAgent {
1833
1833
  * @returns {Promise<void>}
1834
1834
  * @private
1835
1835
  */
1836
- _emitOuput(mode: ExecutionMode, rawResult: string): Promise<void>;
1836
+ _emitOutput(mode: ExecutionMode, rawResult: string): Promise<void>;
1837
1837
  /**
1838
1838
  * Resurrects the model based on the given reason.
1839
1839
  * @param {string} [reason] - The reason for resurrecting the model.