agent-swarm-kit 1.0.140 → 1.0.142

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
@@ -1546,7 +1546,7 @@ var CC_AGENT_HISTORY_FILTER = function (agentName) {
1546
1546
  };
1547
1547
  var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
1548
1548
  var CC_KEEP_MESSAGES = 15;
1549
- var CC_MAX_TOOLS = 1;
1549
+ var CC_MAX_TOOLS = 3;
1550
1550
  var CC_GET_AGENT_HISTORY_ADAPTER = function () { return HistoryAdapter; };
1551
1551
  var CC_GET_CLIENT_LOGGER_ADAPTER = function () { return LoggerAdapter; };
1552
1552
  var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
@@ -1814,12 +1814,55 @@ var AgentSchemaService = /** @class */ (function () {
1814
1814
  }());
1815
1815
 
1816
1816
  var AGENT_CHANGE_SYMBOL = Symbol("agent-change");
1817
+ var MODEL_RESQUE_SYMBOL = Symbol("model-resque");
1817
1818
  var TOOL_ERROR_SYMBOL = Symbol("tool-error");
1818
1819
  var TOOL_STOP_SYMBOL = Symbol("tool-stop");
1819
1820
  var TOOL_NO_OUTPUT_WARNING = 15000;
1820
- var getPlaceholder = function () {
1821
+ var createPlaceholder = function () {
1821
1822
  return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
1822
1823
  };
1824
+ var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued) { return __awaiter(void 0, void 0, void 0, function () {
1825
+ var error_1;
1826
+ var _a, _b, _c, _d;
1827
+ return __generator(this, function (_e) {
1828
+ switch (_e.label) {
1829
+ case 0:
1830
+ if (isResqued()) {
1831
+ return [2 /*return*/];
1832
+ }
1833
+ _e.label = 1;
1834
+ case 1:
1835
+ _e.trys.push([1, 3, , 4]);
1836
+ return [4 /*yield*/, targetFn.call({
1837
+ toolId: tool.id,
1838
+ clientId: self.params.clientId,
1839
+ agentName: self.params.agentName,
1840
+ params: tool.function.arguments,
1841
+ isLast: idx === toolCalls.length - 1,
1842
+ toolCalls: toolCalls,
1843
+ })];
1844
+ case 2:
1845
+ _e.sent();
1846
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
1847
+ ((_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:
1850
+ error_1 = _e.sent();
1851
+ console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(functoolsKit.getErrorMessage(error_1)), {
1852
+ clientId: self.params.clientId,
1853
+ agentName: self.params.agentName,
1854
+ tool_call_id: tool.id,
1855
+ arguments: tool.function.arguments,
1856
+ error: functoolsKit.errorData(error_1),
1857
+ });
1858
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onCallError) &&
1859
+ ((_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
+ self._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
1861
+ return [3 /*break*/, 4];
1862
+ case 4: return [2 /*return*/];
1863
+ }
1864
+ });
1865
+ }); };
1823
1866
  var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0, function () {
1824
1867
  var messages, args, rawMessage, message, result, validation;
1825
1868
  var _a, _b, _c;
@@ -1895,10 +1938,10 @@ var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0
1895
1938
  });
1896
1939
  }); };
1897
1940
  var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void 0, void 0, function () {
1898
- var rawMessage, message, toolCalls, _loop_1, idx, state_1, result, validation, result1;
1899
- var _a, _b, _c, _d, _e, _f, _g, _h;
1900
- return __generator(this, function (_j) {
1901
- switch (_j.label) {
1941
+ var rawMessage, message, toolCalls_1, lastToolCallRef, isResqued_1, unResque, _loop_1, idx, state_1, result, validation, result1;
1942
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1943
+ return __generator(this, function (_k) {
1944
+ switch (_k.label) {
1902
1945
  case 0:
1903
1946
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1904
1947
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
@@ -1911,48 +1954,75 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1911
1954
  content: incoming.trim(),
1912
1955
  })];
1913
1956
  case 1:
1914
- _j.sent();
1957
+ _k.sent();
1915
1958
  return [4 /*yield*/, self.getCompletion(mode)];
1916
1959
  case 2:
1917
- rawMessage = _j.sent();
1960
+ rawMessage = _k.sent();
1918
1961
  return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1919
1962
  case 3:
1920
- message = _j.sent();
1963
+ message = _k.sent();
1921
1964
  if (!message.tool_calls) return [3 /*break*/, 9];
1922
1965
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1923
1966
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " tool call begin"));
1924
- toolCalls = message.tool_calls.map(function (call) {
1967
+ toolCalls_1 = message.tool_calls
1968
+ .map(function (call) {
1925
1969
  var _a, _b;
1926
1970
  return ({
1927
1971
  function: call.function,
1928
1972
  id: (_a = call.id) !== null && _a !== void 0 ? _a : functoolsKit.randomString(),
1929
1973
  type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
1930
1974
  });
1931
- });
1975
+ })
1976
+ .slice(0, GLOBAL_CONFIG.CC_MAX_TOOLS);
1932
1977
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1933
1978
  case 4:
1934
- _j.sent();
1979
+ _k.sent();
1980
+ lastToolCallRef = Promise.resolve();
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
+ }
1935
2003
  _loop_1 = function (idx) {
1936
2004
  var tool, targetFn, result_1, result_2, isResolved, status, result_3;
1937
- return __generator(this, function (_k) {
1938
- switch (_k.label) {
2005
+ return __generator(this, function (_l) {
2006
+ switch (_l.label) {
1939
2007
  case 0:
1940
- if (idx >= GLOBAL_CONFIG.CC_MAX_TOOLS) {
1941
- return [2 /*return*/, "break"];
1942
- }
1943
- tool = toolCalls[idx];
2008
+ tool = toolCalls_1[idx];
1944
2009
  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
+ }
1945
2015
  if (!!targetFn) return [3 /*break*/, 3];
1946
2016
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1947
2017
  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);
1948
2018
  return [4 /*yield*/, self._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
1949
2019
  case 1:
1950
- result_1 = _k.sent();
2020
+ result_1 = _l.sent();
1951
2021
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1952
2022
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_1));
1953
2023
  return [4 /*yield*/, self._emitOuput(mode, result_1)];
1954
2024
  case 2:
1955
- _k.sent();
2025
+ _l.sent();
1956
2026
  return [2 /*return*/, { value: void 0 }];
1957
2027
  case 3:
1958
2028
  ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
@@ -1961,20 +2031,20 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1961
2031
  clientId: self.params.clientId,
1962
2032
  agentName: self.params.agentName,
1963
2033
  params: tool.function.arguments,
1964
- toolCalls: toolCalls,
2034
+ toolCalls: toolCalls_1,
1965
2035
  }))];
1966
2036
  case 4:
1967
- if (!_k.sent()) return [3 /*break*/, 7];
2037
+ if (!_l.sent()) return [3 /*break*/, 7];
1968
2038
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1969
2039
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
1970
2040
  return [4 /*yield*/, self._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
1971
2041
  case 5:
1972
- result_2 = _k.sent();
2042
+ result_2 = _l.sent();
1973
2043
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1974
2044
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_2));
1975
2045
  return [4 /*yield*/, self._emitOuput(mode, result_2)];
1976
2046
  case 6:
1977
- _k.sent();
2047
+ _l.sent();
1978
2048
  return [2 /*return*/, { value: void 0 }];
1979
2049
  case 7:
1980
2050
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
@@ -1982,31 +2052,8 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1982
2052
  /**
1983
2053
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
1984
2054
  */
1985
- Promise.resolve(targetFn.call({
1986
- toolId: tool.id,
1987
- clientId: self.params.clientId,
1988
- agentName: self.params.agentName,
1989
- params: tool.function.arguments,
1990
- isLast: idx === toolCalls.length - 1,
1991
- toolCalls: toolCalls,
1992
- }))
1993
- .then(function () {
1994
- var _a, _b;
1995
- ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
1996
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
1997
- })
1998
- .catch(function (error) {
1999
- var _a, _b;
2000
- console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(functoolsKit.getErrorMessage(error)), {
2001
- clientId: self.params.clientId,
2002
- agentName: self.params.agentName,
2003
- tool_call_id: tool.id,
2004
- arguments: tool.function.arguments,
2005
- error: functoolsKit.errorData(error),
2006
- });
2007
- ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onCallError) &&
2008
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onCallError(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments, error));
2009
- self._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
2055
+ lastToolCallRef = lastToolCallRef.then(function () {
2056
+ return createToolCall(idx, tool, toolCalls_1, targetFn, self, function () { return isResqued_1; });
2010
2057
  });
2011
2058
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2012
2059
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
@@ -2022,24 +2069,32 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2022
2069
  self._toolErrorSubject.toPromise(),
2023
2070
  self._toolStopSubject.toPromise(),
2024
2071
  self._outputSubject.toPromise(),
2072
+ self._modelResqueSubject.toPromise(),
2025
2073
  ])];
2026
2074
  case 8:
2027
- status = _k.sent();
2075
+ status = _l.sent();
2028
2076
  isResolved = true;
2029
2077
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2030
2078
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2079
+ if (status === MODEL_RESQUE_SYMBOL) {
2080
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2081
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the model resque"));
2082
+ ((_f = self.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
2083
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2084
+ return [2 /*return*/, { value: void 0 }];
2085
+ }
2031
2086
  if (status === AGENT_CHANGE_SYMBOL) {
2032
2087
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2033
2088
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the agent changed"));
2034
- ((_f = self.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
2035
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
2089
+ ((_g = self.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
2090
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2036
2091
  return [2 /*return*/, { value: void 0 }];
2037
2092
  }
2038
2093
  if (status === TOOL_STOP_SYMBOL) {
2039
2094
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2040
2095
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the commitStopTools call"));
2041
- ((_g = self.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
2042
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
2096
+ ((_h = self.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
2097
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2043
2098
  return [2 /*return*/, { value: void 0 }];
2044
2099
  }
2045
2100
  if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 11];
@@ -2047,35 +2102,33 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2047
2102
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the call error"));
2048
2103
  return [4 /*yield*/, self._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2049
2104
  case 9:
2050
- result_3 = _k.sent();
2105
+ result_3 = _l.sent();
2051
2106
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2052
2107
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2053
2108
  return [4 /*yield*/, self._emitOuput(mode, result_3)];
2054
2109
  case 10:
2055
- _k.sent();
2110
+ _l.sent();
2056
2111
  return [2 /*return*/, { value: void 0 }];
2057
2112
  case 11: return [2 /*return*/];
2058
2113
  }
2059
2114
  });
2060
2115
  };
2061
2116
  idx = 0;
2062
- _j.label = 5;
2117
+ _k.label = 5;
2063
2118
  case 5:
2064
- if (!(idx !== toolCalls.length)) return [3 /*break*/, 8];
2119
+ if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 8];
2065
2120
  return [5 /*yield**/, _loop_1(idx)];
2066
2121
  case 6:
2067
- state_1 = _j.sent();
2122
+ state_1 = _k.sent();
2068
2123
  if (typeof state_1 === "object")
2069
2124
  return [2 /*return*/, state_1.value];
2070
- if (state_1 === "break")
2071
- return [3 /*break*/, 8];
2072
- _j.label = 7;
2125
+ _k.label = 7;
2073
2126
  case 7:
2074
2127
  idx++;
2075
2128
  return [3 /*break*/, 5];
2076
2129
  case 8:
2077
- ((_h = self.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
2078
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
2130
+ ((_j = self.params.callbacks) === null || _j === void 0 ? void 0 : _j.onAfterToolCalls) &&
2131
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2079
2132
  return [2 /*return*/];
2080
2133
  case 9:
2081
2134
  if (!message.tool_calls) {
@@ -2084,29 +2137,29 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2084
2137
  }
2085
2138
  return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
2086
2139
  case 10:
2087
- result = _j.sent();
2140
+ result = _k.sent();
2088
2141
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
2089
2142
  case 11:
2090
- _j.sent();
2143
+ _k.sent();
2091
2144
  validation = null;
2092
2145
  return [4 /*yield*/, self.params.validate(result)];
2093
2146
  case 12:
2094
- if (!(validation = _j.sent())) return [3 /*break*/, 15];
2147
+ if (!(validation = _k.sent())) return [3 /*break*/, 15];
2095
2148
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2096
2149
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
2097
2150
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2098
2151
  case 13:
2099
- result1 = _j.sent();
2152
+ result1 = _k.sent();
2100
2153
  return [4 /*yield*/, self._emitOuput(mode, result1)];
2101
2154
  case 14:
2102
- _j.sent();
2155
+ _k.sent();
2103
2156
  return [2 /*return*/];
2104
2157
  case 15:
2105
2158
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2106
2159
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2107
2160
  return [4 /*yield*/, self._emitOuput(mode, result)];
2108
2161
  case 16:
2109
- _j.sent();
2162
+ _k.sent();
2110
2163
  return [2 /*return*/];
2111
2164
  }
2112
2165
  });
@@ -2124,6 +2177,7 @@ var ClientAgent = /** @class */ (function () {
2124
2177
  var _this = this;
2125
2178
  this.params = params;
2126
2179
  this._agentChangeSubject = new functoolsKit.Subject();
2180
+ this._modelResqueSubject = new functoolsKit.Subject();
2127
2181
  this._toolErrorSubject = new functoolsKit.Subject();
2128
2182
  this._toolStopSubject = new functoolsKit.Subject();
2129
2183
  this._toolCommitSubject = new functoolsKit.Subject();
@@ -2306,11 +2360,11 @@ var ClientAgent = /** @class */ (function () {
2306
2360
  validation = null;
2307
2361
  return [4 /*yield*/, this.params.validate(result)];
2308
2362
  case 12:
2309
- if (!(validation = _a.sent())) return [3 /*break*/, 14];
2363
+ if (!(validation = _a.sent())) return [3 /*break*/, 15];
2310
2364
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2311
2365
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2312
2366
  console.warn("agent-swarm model ressurect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2313
- content = getPlaceholder();
2367
+ content = createPlaceholder();
2314
2368
  return [4 /*yield*/, this.params.history.push({
2315
2369
  agentName: this.params.agentName,
2316
2370
  role: "assistant",
@@ -2318,10 +2372,16 @@ var ClientAgent = /** @class */ (function () {
2318
2372
  content: content,
2319
2373
  })];
2320
2374
  case 13:
2375
+ _a.sent();
2376
+ return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2377
+ case 14:
2321
2378
  _a.sent();
2322
2379
  return [2 /*return*/, content];
2323
- case 14: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2324
- case 15:
2380
+ case 15: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2381
+ case 16:
2382
+ _a.sent();
2383
+ return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2384
+ case 17:
2325
2385
  _a.sent();
2326
2386
  return [2 /*return*/, result];
2327
2387
  }
package/build/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { scoped } from 'di-scoped';
2
2
  import { createActivator } from 'di-kit';
3
- import { trycatch, singleshot, memoize, str, randomString, ToolRegistry, Subject, queued, getErrorMessage, errorData, sleep, not, isObject, createAwaiter, cancelable, CANCELED_PROMISE_SYMBOL, execpool, SortedArray, Source, schedule, rate, ttl } from 'functools-kit';
3
+ import { trycatch, singleshot, memoize, str, randomString, ToolRegistry, Subject, queued, sleep, not, getErrorMessage, errorData, isObject, createAwaiter, cancelable, CANCELED_PROMISE_SYMBOL, execpool, SortedArray, Source, schedule, rate, ttl } from 'functools-kit';
4
4
  import xml2js from 'xml2js';
5
5
  import { AsyncResource } from 'async_hooks';
6
6
  import { omit } from 'lodash-es';
@@ -1544,7 +1544,7 @@ var CC_AGENT_HISTORY_FILTER = function (agentName) {
1544
1544
  };
1545
1545
  var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
1546
1546
  var CC_KEEP_MESSAGES = 15;
1547
- var CC_MAX_TOOLS = 1;
1547
+ var CC_MAX_TOOLS = 3;
1548
1548
  var CC_GET_AGENT_HISTORY_ADAPTER = function () { return HistoryAdapter; };
1549
1549
  var CC_GET_CLIENT_LOGGER_ADAPTER = function () { return LoggerAdapter; };
1550
1550
  var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
@@ -1812,12 +1812,55 @@ var AgentSchemaService = /** @class */ (function () {
1812
1812
  }());
1813
1813
 
1814
1814
  var AGENT_CHANGE_SYMBOL = Symbol("agent-change");
1815
+ var MODEL_RESQUE_SYMBOL = Symbol("model-resque");
1815
1816
  var TOOL_ERROR_SYMBOL = Symbol("tool-error");
1816
1817
  var TOOL_STOP_SYMBOL = Symbol("tool-stop");
1817
1818
  var TOOL_NO_OUTPUT_WARNING = 15000;
1818
- var getPlaceholder = function () {
1819
+ var createPlaceholder = function () {
1819
1820
  return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
1820
1821
  };
1822
+ var createToolCall = function (idx, tool, toolCalls, targetFn, self, isResqued) { return __awaiter(void 0, void 0, void 0, function () {
1823
+ var error_1;
1824
+ var _a, _b, _c, _d;
1825
+ return __generator(this, function (_e) {
1826
+ switch (_e.label) {
1827
+ case 0:
1828
+ if (isResqued()) {
1829
+ return [2 /*return*/];
1830
+ }
1831
+ _e.label = 1;
1832
+ case 1:
1833
+ _e.trys.push([1, 3, , 4]);
1834
+ return [4 /*yield*/, targetFn.call({
1835
+ toolId: tool.id,
1836
+ clientId: self.params.clientId,
1837
+ agentName: self.params.agentName,
1838
+ params: tool.function.arguments,
1839
+ isLast: idx === toolCalls.length - 1,
1840
+ toolCalls: toolCalls,
1841
+ })];
1842
+ case 2:
1843
+ _e.sent();
1844
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
1845
+ ((_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:
1848
+ error_1 = _e.sent();
1849
+ console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(getErrorMessage(error_1)), {
1850
+ clientId: self.params.clientId,
1851
+ agentName: self.params.agentName,
1852
+ tool_call_id: tool.id,
1853
+ arguments: tool.function.arguments,
1854
+ error: errorData(error_1),
1855
+ });
1856
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onCallError) &&
1857
+ ((_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
+ self._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
1859
+ return [3 /*break*/, 4];
1860
+ case 4: return [2 /*return*/];
1861
+ }
1862
+ });
1863
+ }); };
1821
1864
  var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0, function () {
1822
1865
  var messages, args, rawMessage, message, result, validation;
1823
1866
  var _a, _b, _c;
@@ -1893,10 +1936,10 @@ var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0
1893
1936
  });
1894
1937
  }); };
1895
1938
  var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void 0, void 0, function () {
1896
- var rawMessage, message, toolCalls, _loop_1, idx, state_1, result, validation, result1;
1897
- var _a, _b, _c, _d, _e, _f, _g, _h;
1898
- return __generator(this, function (_j) {
1899
- switch (_j.label) {
1939
+ var rawMessage, message, toolCalls_1, lastToolCallRef, isResqued_1, unResque, _loop_1, idx, state_1, result, validation, result1;
1940
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1941
+ return __generator(this, function (_k) {
1942
+ switch (_k.label) {
1900
1943
  case 0:
1901
1944
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1902
1945
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
@@ -1909,48 +1952,75 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1909
1952
  content: incoming.trim(),
1910
1953
  })];
1911
1954
  case 1:
1912
- _j.sent();
1955
+ _k.sent();
1913
1956
  return [4 /*yield*/, self.getCompletion(mode)];
1914
1957
  case 2:
1915
- rawMessage = _j.sent();
1958
+ rawMessage = _k.sent();
1916
1959
  return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1917
1960
  case 3:
1918
- message = _j.sent();
1961
+ message = _k.sent();
1919
1962
  if (!message.tool_calls) return [3 /*break*/, 9];
1920
1963
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1921
1964
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " tool call begin"));
1922
- toolCalls = message.tool_calls.map(function (call) {
1965
+ toolCalls_1 = message.tool_calls
1966
+ .map(function (call) {
1923
1967
  var _a, _b;
1924
1968
  return ({
1925
1969
  function: call.function,
1926
1970
  id: (_a = call.id) !== null && _a !== void 0 ? _a : randomString(),
1927
1971
  type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
1928
1972
  });
1929
- });
1973
+ })
1974
+ .slice(0, GLOBAL_CONFIG.CC_MAX_TOOLS);
1930
1975
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1931
1976
  case 4:
1932
- _j.sent();
1977
+ _k.sent();
1978
+ lastToolCallRef = Promise.resolve();
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
+ }
1933
2001
  _loop_1 = function (idx) {
1934
2002
  var tool, targetFn, result_1, result_2, isResolved, status, result_3;
1935
- return __generator(this, function (_k) {
1936
- switch (_k.label) {
2003
+ return __generator(this, function (_l) {
2004
+ switch (_l.label) {
1937
2005
  case 0:
1938
- if (idx >= GLOBAL_CONFIG.CC_MAX_TOOLS) {
1939
- return [2 /*return*/, "break"];
1940
- }
1941
- tool = toolCalls[idx];
2006
+ tool = toolCalls_1[idx];
1942
2007
  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
+ }
1943
2013
  if (!!targetFn) return [3 /*break*/, 3];
1944
2014
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1945
2015
  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);
1946
2016
  return [4 /*yield*/, self._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
1947
2017
  case 1:
1948
- result_1 = _k.sent();
2018
+ result_1 = _l.sent();
1949
2019
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1950
2020
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_1));
1951
2021
  return [4 /*yield*/, self._emitOuput(mode, result_1)];
1952
2022
  case 2:
1953
- _k.sent();
2023
+ _l.sent();
1954
2024
  return [2 /*return*/, { value: void 0 }];
1955
2025
  case 3:
1956
2026
  ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
@@ -1959,20 +2029,20 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1959
2029
  clientId: self.params.clientId,
1960
2030
  agentName: self.params.agentName,
1961
2031
  params: tool.function.arguments,
1962
- toolCalls: toolCalls,
2032
+ toolCalls: toolCalls_1,
1963
2033
  }))];
1964
2034
  case 4:
1965
- if (!_k.sent()) return [3 /*break*/, 7];
2035
+ if (!_l.sent()) return [3 /*break*/, 7];
1966
2036
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1967
2037
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
1968
2038
  return [4 /*yield*/, self._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
1969
2039
  case 5:
1970
- result_2 = _k.sent();
2040
+ result_2 = _l.sent();
1971
2041
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1972
2042
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_2));
1973
2043
  return [4 /*yield*/, self._emitOuput(mode, result_2)];
1974
2044
  case 6:
1975
- _k.sent();
2045
+ _l.sent();
1976
2046
  return [2 /*return*/, { value: void 0 }];
1977
2047
  case 7:
1978
2048
  ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
@@ -1980,31 +2050,8 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1980
2050
  /**
1981
2051
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
1982
2052
  */
1983
- Promise.resolve(targetFn.call({
1984
- toolId: tool.id,
1985
- clientId: self.params.clientId,
1986
- agentName: self.params.agentName,
1987
- params: tool.function.arguments,
1988
- isLast: idx === toolCalls.length - 1,
1989
- toolCalls: toolCalls,
1990
- }))
1991
- .then(function () {
1992
- var _a, _b;
1993
- ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
1994
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
1995
- })
1996
- .catch(function (error) {
1997
- var _a, _b;
1998
- console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(getErrorMessage(error)), {
1999
- clientId: self.params.clientId,
2000
- agentName: self.params.agentName,
2001
- tool_call_id: tool.id,
2002
- arguments: tool.function.arguments,
2003
- error: errorData(error),
2004
- });
2005
- ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onCallError) &&
2006
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onCallError(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments, error));
2007
- self._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
2053
+ lastToolCallRef = lastToolCallRef.then(function () {
2054
+ return createToolCall(idx, tool, toolCalls_1, targetFn, self, function () { return isResqued_1; });
2008
2055
  });
2009
2056
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2010
2057
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
@@ -2020,24 +2067,32 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2020
2067
  self._toolErrorSubject.toPromise(),
2021
2068
  self._toolStopSubject.toPromise(),
2022
2069
  self._outputSubject.toPromise(),
2070
+ self._modelResqueSubject.toPromise(),
2023
2071
  ])];
2024
2072
  case 8:
2025
- status = _k.sent();
2073
+ status = _l.sent();
2026
2074
  isResolved = true;
2027
2075
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2028
2076
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2077
+ if (status === MODEL_RESQUE_SYMBOL) {
2078
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2079
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the model resque"));
2080
+ ((_f = self.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
2081
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2082
+ return [2 /*return*/, { value: void 0 }];
2083
+ }
2029
2084
  if (status === AGENT_CHANGE_SYMBOL) {
2030
2085
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2031
2086
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the agent changed"));
2032
- ((_f = self.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
2033
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
2087
+ ((_g = self.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
2088
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2034
2089
  return [2 /*return*/, { value: void 0 }];
2035
2090
  }
2036
2091
  if (status === TOOL_STOP_SYMBOL) {
2037
2092
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2038
2093
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the commitStopTools call"));
2039
- ((_g = self.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
2040
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
2094
+ ((_h = self.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
2095
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2041
2096
  return [2 /*return*/, { value: void 0 }];
2042
2097
  }
2043
2098
  if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 11];
@@ -2045,35 +2100,33 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2045
2100
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the call error"));
2046
2101
  return [4 /*yield*/, self._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2047
2102
  case 9:
2048
- result_3 = _k.sent();
2103
+ result_3 = _l.sent();
2049
2104
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2050
2105
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2051
2106
  return [4 /*yield*/, self._emitOuput(mode, result_3)];
2052
2107
  case 10:
2053
- _k.sent();
2108
+ _l.sent();
2054
2109
  return [2 /*return*/, { value: void 0 }];
2055
2110
  case 11: return [2 /*return*/];
2056
2111
  }
2057
2112
  });
2058
2113
  };
2059
2114
  idx = 0;
2060
- _j.label = 5;
2115
+ _k.label = 5;
2061
2116
  case 5:
2062
- if (!(idx !== toolCalls.length)) return [3 /*break*/, 8];
2117
+ if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 8];
2063
2118
  return [5 /*yield**/, _loop_1(idx)];
2064
2119
  case 6:
2065
- state_1 = _j.sent();
2120
+ state_1 = _k.sent();
2066
2121
  if (typeof state_1 === "object")
2067
2122
  return [2 /*return*/, state_1.value];
2068
- if (state_1 === "break")
2069
- return [3 /*break*/, 8];
2070
- _j.label = 7;
2123
+ _k.label = 7;
2071
2124
  case 7:
2072
2125
  idx++;
2073
2126
  return [3 /*break*/, 5];
2074
2127
  case 8:
2075
- ((_h = self.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
2076
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
2128
+ ((_j = self.params.callbacks) === null || _j === void 0 ? void 0 : _j.onAfterToolCalls) &&
2129
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2077
2130
  return [2 /*return*/];
2078
2131
  case 9:
2079
2132
  if (!message.tool_calls) {
@@ -2082,29 +2135,29 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2082
2135
  }
2083
2136
  return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
2084
2137
  case 10:
2085
- result = _j.sent();
2138
+ result = _k.sent();
2086
2139
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
2087
2140
  case 11:
2088
- _j.sent();
2141
+ _k.sent();
2089
2142
  validation = null;
2090
2143
  return [4 /*yield*/, self.params.validate(result)];
2091
2144
  case 12:
2092
- if (!(validation = _j.sent())) return [3 /*break*/, 15];
2145
+ if (!(validation = _k.sent())) return [3 /*break*/, 15];
2093
2146
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2094
2147
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
2095
2148
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2096
2149
  case 13:
2097
- result1 = _j.sent();
2150
+ result1 = _k.sent();
2098
2151
  return [4 /*yield*/, self._emitOuput(mode, result1)];
2099
2152
  case 14:
2100
- _j.sent();
2153
+ _k.sent();
2101
2154
  return [2 /*return*/];
2102
2155
  case 15:
2103
2156
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2104
2157
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2105
2158
  return [4 /*yield*/, self._emitOuput(mode, result)];
2106
2159
  case 16:
2107
- _j.sent();
2160
+ _k.sent();
2108
2161
  return [2 /*return*/];
2109
2162
  }
2110
2163
  });
@@ -2122,6 +2175,7 @@ var ClientAgent = /** @class */ (function () {
2122
2175
  var _this = this;
2123
2176
  this.params = params;
2124
2177
  this._agentChangeSubject = new Subject();
2178
+ this._modelResqueSubject = new Subject();
2125
2179
  this._toolErrorSubject = new Subject();
2126
2180
  this._toolStopSubject = new Subject();
2127
2181
  this._toolCommitSubject = new Subject();
@@ -2304,11 +2358,11 @@ var ClientAgent = /** @class */ (function () {
2304
2358
  validation = null;
2305
2359
  return [4 /*yield*/, this.params.validate(result)];
2306
2360
  case 12:
2307
- if (!(validation = _a.sent())) return [3 /*break*/, 14];
2361
+ if (!(validation = _a.sent())) return [3 /*break*/, 15];
2308
2362
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2309
2363
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2310
2364
  console.warn("agent-swarm model ressurect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2311
- content = getPlaceholder();
2365
+ content = createPlaceholder();
2312
2366
  return [4 /*yield*/, this.params.history.push({
2313
2367
  agentName: this.params.agentName,
2314
2368
  role: "assistant",
@@ -2316,10 +2370,16 @@ var ClientAgent = /** @class */ (function () {
2316
2370
  content: content,
2317
2371
  })];
2318
2372
  case 13:
2373
+ _a.sent();
2374
+ return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2375
+ case 14:
2319
2376
  _a.sent();
2320
2377
  return [2 /*return*/, content];
2321
- case 14: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2322
- case 15:
2378
+ case 15: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2379
+ case 16:
2380
+ _a.sent();
2381
+ return [4 /*yield*/, this._modelResqueSubject.next(MODEL_RESQUE_SYMBOL)];
2382
+ case 17:
2323
2383
  _a.sent();
2324
2384
  return [2 /*return*/, result];
2325
2385
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.140",
3
+ "version": "1.0.142",
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
@@ -1807,6 +1807,7 @@ declare class LoggerService implements ILogger {
1807
1807
  }
1808
1808
 
1809
1809
  declare const AGENT_CHANGE_SYMBOL: unique symbol;
1810
+ declare const MODEL_RESQUE_SYMBOL: unique symbol;
1810
1811
  declare const TOOL_ERROR_SYMBOL: unique symbol;
1811
1812
  declare const TOOL_STOP_SYMBOL: unique symbol;
1812
1813
  /**
@@ -1816,6 +1817,7 @@ declare const TOOL_STOP_SYMBOL: unique symbol;
1816
1817
  declare class ClientAgent implements IAgent {
1817
1818
  readonly params: IAgentParams;
1818
1819
  readonly _agentChangeSubject: Subject<typeof AGENT_CHANGE_SYMBOL>;
1820
+ readonly _modelResqueSubject: Subject<typeof MODEL_RESQUE_SYMBOL>;
1819
1821
  readonly _toolErrorSubject: Subject<typeof TOOL_ERROR_SYMBOL>;
1820
1822
  readonly _toolStopSubject: Subject<typeof TOOL_STOP_SYMBOL>;
1821
1823
  readonly _toolCommitSubject: Subject<void>;