agent-swarm-kit 1.0.147 → 1.0.149

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.
Files changed (3) hide show
  1. package/build/index.cjs +168 -130
  2. package/build/index.mjs +169 -131
  3. package/package.json +1 -1
package/build/index.cjs CHANGED
@@ -1934,10 +1934,10 @@ var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0
1934
1934
  });
1935
1935
  }); };
1936
1936
  var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void 0, void 0, function () {
1937
- var rawMessage, message, toolCalls_1, lastToolStatusRef, _loop_1, idx, state_1, result, validation, result1;
1938
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1939
- return __generator(this, function (_k) {
1940
- switch (_k.label) {
1937
+ var rawMessage, message, toolCalls_1, lastToolStatusRef, _a, runAwaiter_1, run, _loop_1, idx, state_1, result, validation, result1;
1938
+ var _b, _c, _d, _e, _f;
1939
+ return __generator(this, function (_g) {
1940
+ switch (_g.label) {
1941
1941
  case 0:
1942
1942
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1943
1943
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
@@ -1950,13 +1950,13 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1950
1950
  content: incoming.trim(),
1951
1951
  })];
1952
1952
  case 1:
1953
- _k.sent();
1953
+ _g.sent();
1954
1954
  return [4 /*yield*/, self.getCompletion(mode)];
1955
1955
  case 2:
1956
- rawMessage = _k.sent();
1956
+ rawMessage = _g.sent();
1957
1957
  return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1958
1958
  case 3:
1959
- message = _k.sent();
1959
+ message = _g.sent();
1960
1960
  if (!message.tool_calls) return [3 /*break*/, 9];
1961
1961
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1962
1962
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " tool call begin"));
@@ -1972,30 +1972,32 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1972
1972
  .slice(0, GLOBAL_CONFIG.CC_MAX_TOOLS);
1973
1973
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1974
1974
  case 4:
1975
- _k.sent();
1975
+ _g.sent();
1976
1976
  lastToolStatusRef = Promise.resolve(null);
1977
+ _a = __read(functoolsKit.createAwaiter(), 2), runAwaiter_1 = _a[0], run = _a[1].resolve;
1977
1978
  _loop_1 = function (idx) {
1978
- var tool, targetFn, result_1, result_2, status, result_3;
1979
- return __generator(this, function (_l) {
1980
- switch (_l.label) {
1979
+ var tool, targetFn, result_1, result_2;
1980
+ return __generator(this, function (_h) {
1981
+ switch (_h.label) {
1981
1982
  case 0:
1982
1983
  tool = toolCalls_1[idx];
1983
- targetFn = (_a = self.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
1984
+ targetFn = (_b = self.params.tools) === null || _b === void 0 ? void 0 : _b.find(function (t) { return t.function.name === tool.function.name; });
1984
1985
  if (!!targetFn) return [3 /*break*/, 3];
1985
1986
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1986
1987
  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);
1987
1988
  return [4 /*yield*/, self._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
1988
1989
  case 1:
1989
- result_1 = _l.sent();
1990
+ result_1 = _h.sent();
1990
1991
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1991
1992
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_1));
1992
1993
  return [4 /*yield*/, self._emitOutput(mode, result_1)];
1993
1994
  case 2:
1994
- _l.sent();
1995
+ _h.sent();
1996
+ run(false);
1995
1997
  return [2 /*return*/, { value: void 0 }];
1996
1998
  case 3:
1997
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
1998
- ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate(self.params.clientId, self.params.agentName, tool.function.arguments));
1999
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate) &&
2000
+ ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onValidate(self.params.clientId, self.params.agentName, tool.function.arguments));
1999
2001
  return [4 /*yield*/, functoolsKit.not(targetFn.validate({
2000
2002
  clientId: self.params.clientId,
2001
2003
  agentName: self.params.agentName,
@@ -2003,118 +2005,128 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2003
2005
  toolCalls: toolCalls_1,
2004
2006
  }))];
2005
2007
  case 4:
2006
- if (!_l.sent()) return [3 /*break*/, 7];
2008
+ if (!_h.sent()) return [3 /*break*/, 7];
2007
2009
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2008
2010
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
2009
2011
  return [4 /*yield*/, self._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2010
2012
  case 5:
2011
- result_2 = _l.sent();
2013
+ result_2 = _h.sent();
2012
2014
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2013
2015
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_2));
2014
2016
  return [4 /*yield*/, self._emitOutput(mode, result_2)];
2015
2017
  case 6:
2016
- _l.sent();
2018
+ _h.sent();
2019
+ run(false);
2017
2020
  return [2 /*return*/, { value: void 0 }];
2018
2021
  case 7:
2019
- ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
2020
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
2022
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall) &&
2023
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onBeforeCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
2021
2024
  /**
2022
2025
  * Do not await directly to avoid the deadlock! The tool can send the message to other agents by emulating user messages
2023
2026
  */
2024
- {
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;
2027
+ lastToolStatusRef = lastToolStatusRef.then(function (lastStatus) { return __awaiter(void 0, void 0, void 0, function () {
2028
+ var statusAwaiter, status, result_3;
2029
+ var _a, _b, _c;
2030
+ return __generator(this, function (_d) {
2031
+ switch (_d.label) {
2032
+ case 0: return [4 /*yield*/, functoolsKit.not(runAwaiter_1)];
2033
+ case 1:
2034
+ if (_d.sent()) {
2035
+ return [2 /*return*/];
2036
+ }
2037
+ if (lastStatus === MODEL_RESQUE_SYMBOL) {
2038
+ return [2 /*return*/, lastStatus];
2039
+ }
2040
+ if (lastStatus === AGENT_CHANGE_SYMBOL) {
2041
+ return [2 /*return*/, lastStatus];
2042
+ }
2043
+ if (lastStatus === TOOL_STOP_SYMBOL) {
2044
+ return [2 /*return*/, lastStatus];
2045
+ }
2046
+ if (lastStatus === TOOL_ERROR_SYMBOL) {
2047
+ return [2 /*return*/, lastStatus];
2048
+ }
2049
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2050
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
2051
+ statusAwaiter = Promise.race([
2052
+ self._agentChangeSubject.toPromise(),
2053
+ self._toolCommitSubject.toPromise(),
2054
+ self._toolErrorSubject.toPromise(),
2055
+ self._toolStopSubject.toPromise(),
2056
+ self._resqueSubject.toPromise(),
2057
+ ]);
2058
+ Promise.race([
2059
+ functoolsKit.sleep(TOOL_NO_OUTPUT_WARNING_TIMEOUT).then(function () { return TOOL_NO_OUTPUT_WARNING_SYMBOL; }),
2060
+ statusAwaiter,
2061
+ ]).then(function (result) {
2062
+ if (result === TOOL_NO_OUTPUT_WARNING_SYMBOL) {
2063
+ 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));
2064
+ }
2065
+ });
2066
+ createToolCall(idx, tool, toolCalls_1, targetFn, self);
2067
+ return [4 /*yield*/, statusAwaiter];
2068
+ case 2:
2069
+ status = _d.sent();
2070
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2071
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2072
+ if (status === MODEL_RESQUE_SYMBOL) {
2073
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2074
+ 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"));
2075
+ ((_a = self.params.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterToolCalls) &&
2076
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2077
+ }
2078
+ if (status === AGENT_CHANGE_SYMBOL) {
2079
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2080
+ 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"));
2081
+ ((_b = self.params.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterToolCalls) &&
2082
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2083
+ }
2084
+ if (status === TOOL_STOP_SYMBOL) {
2085
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
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 commitStopTools call"));
2087
+ ((_c = self.params.callbacks) === null || _c === void 0 ? void 0 : _c.onAfterToolCalls) &&
2088
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2089
+ }
2090
+ if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 5];
2091
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2092
+ 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"));
2093
+ return [4 /*yield*/, self._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2094
+ case 3:
2095
+ result_3 = _d.sent();
2096
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2097
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2098
+ return [4 /*yield*/, self._emitOutput(mode, result_3)];
2099
+ case 4:
2100
+ _d.sent();
2101
+ _d.label = 5;
2102
+ case 5: return [2 /*return*/, status];
2037
2103
  }
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
2104
  });
2049
- }
2050
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2051
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
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));
2058
- }
2059
- });
2060
- return [4 /*yield*/, lastToolStatusRef];
2061
- case 8:
2062
- status = _l.sent();
2063
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2064
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2065
- if (status === MODEL_RESQUE_SYMBOL) {
2066
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2067
- 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"));
2068
- ((_f = self.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
2069
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2070
- return [2 /*return*/, { value: void 0 }];
2071
- }
2072
- if (status === AGENT_CHANGE_SYMBOL) {
2073
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2074
- 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"));
2075
- ((_g = self.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
2076
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2077
- return [2 /*return*/, { value: void 0 }];
2078
- }
2079
- if (status === TOOL_STOP_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 commitStopTools call"));
2082
- ((_h = self.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
2083
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2084
- return [2 /*return*/, { value: void 0 }];
2085
- }
2086
- if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 11];
2087
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
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 call error"));
2089
- return [4 /*yield*/, self._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2090
- case 9:
2091
- result_3 = _l.sent();
2092
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2093
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2094
- return [4 /*yield*/, self._emitOutput(mode, result_3)];
2095
- case 10:
2096
- _l.sent();
2097
- return [2 /*return*/, { value: void 0 }];
2098
- case 11: return [2 /*return*/];
2105
+ }); });
2106
+ return [2 /*return*/];
2099
2107
  }
2100
2108
  });
2101
2109
  };
2102
2110
  idx = 0;
2103
- _k.label = 5;
2111
+ _g.label = 5;
2104
2112
  case 5:
2105
2113
  if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 8];
2106
2114
  return [5 /*yield**/, _loop_1(idx)];
2107
2115
  case 6:
2108
- state_1 = _k.sent();
2116
+ state_1 = _g.sent();
2109
2117
  if (typeof state_1 === "object")
2110
2118
  return [2 /*return*/, state_1.value];
2111
- _k.label = 7;
2119
+ _g.label = 7;
2112
2120
  case 7:
2113
2121
  idx++;
2114
2122
  return [3 /*break*/, 5];
2115
2123
  case 8:
2116
- ((_j = self.params.callbacks) === null || _j === void 0 ? void 0 : _j.onAfterToolCalls) &&
2117
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2124
+ lastToolStatusRef.finally(function () {
2125
+ var _a;
2126
+ ((_a = self.params.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterToolCalls) &&
2127
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2128
+ });
2129
+ run(true);
2118
2130
  return [2 /*return*/];
2119
2131
  case 9:
2120
2132
  if (!message.tool_calls) {
@@ -2123,29 +2135,29 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2123
2135
  }
2124
2136
  return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
2125
2137
  case 10:
2126
- result = _k.sent();
2138
+ result = _g.sent();
2127
2139
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
2128
2140
  case 11:
2129
- _k.sent();
2141
+ _g.sent();
2130
2142
  validation = null;
2131
2143
  return [4 /*yield*/, self.params.validate(result)];
2132
2144
  case 12:
2133
- if (!(validation = _k.sent())) return [3 /*break*/, 15];
2145
+ if (!(validation = _g.sent())) return [3 /*break*/, 15];
2134
2146
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2135
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 });
2136
2148
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2137
2149
  case 13:
2138
- result1 = _k.sent();
2150
+ result1 = _g.sent();
2139
2151
  return [4 /*yield*/, self._emitOutput(mode, result1)];
2140
2152
  case 14:
2141
- _k.sent();
2153
+ _g.sent();
2142
2154
  return [2 /*return*/];
2143
2155
  case 15:
2144
2156
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2145
2157
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2146
2158
  return [4 /*yield*/, self._emitOutput(mode, result)];
2147
2159
  case 16:
2148
- _k.sent();
2160
+ _g.sent();
2149
2161
  return [2 /*return*/];
2150
2162
  }
2151
2163
  });
@@ -2288,10 +2300,11 @@ var ClientAgent = /** @class */ (function () {
2288
2300
  */
2289
2301
  ClientAgent.prototype._resurrectModel = function (mode_1) {
2290
2302
  return __awaiter(this, arguments, void 0, function (mode, reason) {
2291
- var rawMessage, message, result, validation, content;
2303
+ var rawMessage, content, message, result, validation, content;
2304
+ var _a;
2292
2305
  if (reason === void 0) { reason = "unknown"; }
2293
- return __generator(this, function (_a) {
2294
- switch (_a.label) {
2306
+ return __generator(this, function (_b) {
2307
+ switch (_b.label) {
2295
2308
  case 0:
2296
2309
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2297
2310
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
@@ -2306,7 +2319,7 @@ var ClientAgent = /** @class */ (function () {
2306
2319
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2307
2320
  })];
2308
2321
  case 1:
2309
- _a.sent();
2322
+ _b.sent();
2310
2323
  return [3 /*break*/, 8];
2311
2324
  case 2:
2312
2325
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush")) return [3 /*break*/, 5];
@@ -2317,7 +2330,7 @@ var ClientAgent = /** @class */ (function () {
2317
2330
  content: reason || "Unknown error",
2318
2331
  })];
2319
2332
  case 3:
2320
- _a.sent();
2333
+ _b.sent();
2321
2334
  return [4 /*yield*/, this.params.history.push({
2322
2335
  role: "user",
2323
2336
  mode: "tool",
@@ -2325,28 +2338,53 @@ var ClientAgent = /** @class */ (function () {
2325
2338
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_FLUSH_PROMPT,
2326
2339
  })];
2327
2340
  case 4:
2328
- _a.sent();
2341
+ _b.sent();
2329
2342
  return [3 /*break*/, 8];
2330
2343
  case 5:
2331
2344
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 7];
2332
2345
  return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2333
2346
  case 6:
2334
- _a.sent();
2347
+ _b.sent();
2335
2348
  return [3 /*break*/, 8];
2336
2349
  case 7: throw new Error("agent-swarm _resurrectModel invalid strategy value=".concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " agentName=").concat(this.params.agentName, " clientId=").concat(this.params.clientId));
2337
2350
  case 8: return [4 /*yield*/, this.getCompletion(mode)];
2338
2351
  case 9:
2339
- rawMessage = _a.sent();
2340
- return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2352
+ rawMessage = _b.sent();
2353
+ if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 13];
2354
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2355
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel failed due to tool_calls"));
2356
+ console.warn("agent-swarm model ressurect did not solved the problem (tool_calls) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2357
+ content = createPlaceholder();
2358
+ return [4 /*yield*/, this.params.history.push({
2359
+ role: "resque",
2360
+ mode: "tool",
2361
+ agentName: this.params.agentName,
2362
+ content: reason || "Unknown error",
2363
+ })];
2341
2364
  case 10:
2342
- message = _a.sent();
2343
- return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2365
+ _b.sent();
2366
+ return [4 /*yield*/, this.params.history.push({
2367
+ agentName: this.params.agentName,
2368
+ role: "assistant",
2369
+ mode: "tool",
2370
+ content: content,
2371
+ })];
2344
2372
  case 11:
2345
- result = _a.sent();
2373
+ _b.sent();
2374
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2375
+ case 12:
2376
+ _b.sent();
2377
+ return [2 /*return*/, content];
2378
+ case 13: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2379
+ case 14:
2380
+ message = _b.sent();
2381
+ return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2382
+ case 15:
2383
+ result = _b.sent();
2346
2384
  validation = null;
2347
2385
  return [4 /*yield*/, this.params.validate(result)];
2348
- case 12:
2349
- if (!(validation = _a.sent())) return [3 /*break*/, 16];
2386
+ case 16:
2387
+ if (!(validation = _b.sent())) return [3 /*break*/, 20];
2350
2388
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2351
2389
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2352
2390
  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));
@@ -2357,26 +2395,26 @@ var ClientAgent = /** @class */ (function () {
2357
2395
  agentName: this.params.agentName,
2358
2396
  content: reason || "Unknown error",
2359
2397
  })];
2360
- case 13:
2361
- _a.sent();
2398
+ case 17:
2399
+ _b.sent();
2362
2400
  return [4 /*yield*/, this.params.history.push({
2363
2401
  agentName: this.params.agentName,
2364
2402
  role: "assistant",
2365
2403
  mode: "tool",
2366
2404
  content: content,
2367
2405
  })];
2368
- case 14:
2369
- _a.sent();
2406
+ case 18:
2407
+ _b.sent();
2370
2408
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2371
- case 15:
2372
- _a.sent();
2409
+ case 19:
2410
+ _b.sent();
2373
2411
  return [2 /*return*/, content];
2374
- case 16: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2375
- case 17:
2376
- _a.sent();
2412
+ case 20: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2413
+ case 21:
2414
+ _b.sent();
2377
2415
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2378
- case 18:
2379
- _a.sent();
2416
+ case 22:
2417
+ _b.sent();
2380
2418
  return [2 /*return*/, result];
2381
2419
  }
2382
2420
  });
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, sleep, not, getErrorMessage, errorData, 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, createAwaiter, sleep, not, getErrorMessage, errorData, isObject, 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';
@@ -1932,10 +1932,10 @@ var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0
1932
1932
  });
1933
1933
  }); };
1934
1934
  var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void 0, void 0, function () {
1935
- var rawMessage, message, toolCalls_1, lastToolStatusRef, _loop_1, idx, state_1, result, validation, result1;
1936
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1937
- return __generator(this, function (_k) {
1938
- switch (_k.label) {
1935
+ var rawMessage, message, toolCalls_1, lastToolStatusRef, _a, runAwaiter_1, run, _loop_1, idx, state_1, result, validation, result1;
1936
+ var _b, _c, _d, _e, _f;
1937
+ return __generator(this, function (_g) {
1938
+ switch (_g.label) {
1939
1939
  case 0:
1940
1940
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1941
1941
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
@@ -1948,13 +1948,13 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1948
1948
  content: incoming.trim(),
1949
1949
  })];
1950
1950
  case 1:
1951
- _k.sent();
1951
+ _g.sent();
1952
1952
  return [4 /*yield*/, self.getCompletion(mode)];
1953
1953
  case 2:
1954
- rawMessage = _k.sent();
1954
+ rawMessage = _g.sent();
1955
1955
  return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1956
1956
  case 3:
1957
- message = _k.sent();
1957
+ message = _g.sent();
1958
1958
  if (!message.tool_calls) return [3 /*break*/, 9];
1959
1959
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1960
1960
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " tool call begin"));
@@ -1970,30 +1970,32 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
1970
1970
  .slice(0, GLOBAL_CONFIG.CC_MAX_TOOLS);
1971
1971
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1972
1972
  case 4:
1973
- _k.sent();
1973
+ _g.sent();
1974
1974
  lastToolStatusRef = Promise.resolve(null);
1975
+ _a = __read(createAwaiter(), 2), runAwaiter_1 = _a[0], run = _a[1].resolve;
1975
1976
  _loop_1 = function (idx) {
1976
- var tool, targetFn, result_1, result_2, status, result_3;
1977
- return __generator(this, function (_l) {
1978
- switch (_l.label) {
1977
+ var tool, targetFn, result_1, result_2;
1978
+ return __generator(this, function (_h) {
1979
+ switch (_h.label) {
1979
1980
  case 0:
1980
1981
  tool = toolCalls_1[idx];
1981
- targetFn = (_a = self.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
1982
+ targetFn = (_b = self.params.tools) === null || _b === void 0 ? void 0 : _b.find(function (t) { return t.function.name === tool.function.name; });
1982
1983
  if (!!targetFn) return [3 /*break*/, 3];
1983
1984
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1984
1985
  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);
1985
1986
  return [4 /*yield*/, self._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
1986
1987
  case 1:
1987
- result_1 = _l.sent();
1988
+ result_1 = _h.sent();
1988
1989
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1989
1990
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_1));
1990
1991
  return [4 /*yield*/, self._emitOutput(mode, result_1)];
1991
1992
  case 2:
1992
- _l.sent();
1993
+ _h.sent();
1994
+ run(false);
1993
1995
  return [2 /*return*/, { value: void 0 }];
1994
1996
  case 3:
1995
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
1996
- ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate(self.params.clientId, self.params.agentName, tool.function.arguments));
1997
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate) &&
1998
+ ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onValidate(self.params.clientId, self.params.agentName, tool.function.arguments));
1997
1999
  return [4 /*yield*/, not(targetFn.validate({
1998
2000
  clientId: self.params.clientId,
1999
2001
  agentName: self.params.agentName,
@@ -2001,118 +2003,128 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2001
2003
  toolCalls: toolCalls_1,
2002
2004
  }))];
2003
2005
  case 4:
2004
- if (!_l.sent()) return [3 /*break*/, 7];
2006
+ if (!_h.sent()) return [3 /*break*/, 7];
2005
2007
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2006
2008
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
2007
2009
  return [4 /*yield*/, self._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2008
2010
  case 5:
2009
- result_2 = _l.sent();
2011
+ result_2 = _h.sent();
2010
2012
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2011
2013
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_2));
2012
2014
  return [4 /*yield*/, self._emitOutput(mode, result_2)];
2013
2015
  case 6:
2014
- _l.sent();
2016
+ _h.sent();
2017
+ run(false);
2015
2018
  return [2 /*return*/, { value: void 0 }];
2016
2019
  case 7:
2017
- ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
2018
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
2020
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall) &&
2021
+ ((_f = targetFn.callbacks) === null || _f === void 0 ? void 0 : _f.onBeforeCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
2019
2022
  /**
2020
2023
  * Do not await directly to avoid the deadlock! The tool can send the message to other agents by emulating user messages
2021
2024
  */
2022
- {
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;
2025
+ lastToolStatusRef = lastToolStatusRef.then(function (lastStatus) { return __awaiter(void 0, void 0, void 0, function () {
2026
+ var statusAwaiter, status, result_3;
2027
+ var _a, _b, _c;
2028
+ return __generator(this, function (_d) {
2029
+ switch (_d.label) {
2030
+ case 0: return [4 /*yield*/, not(runAwaiter_1)];
2031
+ case 1:
2032
+ if (_d.sent()) {
2033
+ return [2 /*return*/];
2034
+ }
2035
+ if (lastStatus === MODEL_RESQUE_SYMBOL) {
2036
+ return [2 /*return*/, lastStatus];
2037
+ }
2038
+ if (lastStatus === AGENT_CHANGE_SYMBOL) {
2039
+ return [2 /*return*/, lastStatus];
2040
+ }
2041
+ if (lastStatus === TOOL_STOP_SYMBOL) {
2042
+ return [2 /*return*/, lastStatus];
2043
+ }
2044
+ if (lastStatus === TOOL_ERROR_SYMBOL) {
2045
+ return [2 /*return*/, lastStatus];
2046
+ }
2047
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2048
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
2049
+ statusAwaiter = Promise.race([
2050
+ self._agentChangeSubject.toPromise(),
2051
+ self._toolCommitSubject.toPromise(),
2052
+ self._toolErrorSubject.toPromise(),
2053
+ self._toolStopSubject.toPromise(),
2054
+ self._resqueSubject.toPromise(),
2055
+ ]);
2056
+ Promise.race([
2057
+ sleep(TOOL_NO_OUTPUT_WARNING_TIMEOUT).then(function () { return TOOL_NO_OUTPUT_WARNING_SYMBOL; }),
2058
+ statusAwaiter,
2059
+ ]).then(function (result) {
2060
+ if (result === TOOL_NO_OUTPUT_WARNING_SYMBOL) {
2061
+ 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));
2062
+ }
2063
+ });
2064
+ createToolCall(idx, tool, toolCalls_1, targetFn, self);
2065
+ return [4 /*yield*/, statusAwaiter];
2066
+ case 2:
2067
+ status = _d.sent();
2068
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2069
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2070
+ if (status === MODEL_RESQUE_SYMBOL) {
2071
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2072
+ 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"));
2073
+ ((_a = self.params.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterToolCalls) &&
2074
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2075
+ }
2076
+ if (status === AGENT_CHANGE_SYMBOL) {
2077
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2078
+ 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"));
2079
+ ((_b = self.params.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterToolCalls) &&
2080
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2081
+ }
2082
+ if (status === TOOL_STOP_SYMBOL) {
2083
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2084
+ 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"));
2085
+ ((_c = self.params.callbacks) === null || _c === void 0 ? void 0 : _c.onAfterToolCalls) &&
2086
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2087
+ }
2088
+ if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 5];
2089
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2090
+ 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"));
2091
+ return [4 /*yield*/, self._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2092
+ case 3:
2093
+ result_3 = _d.sent();
2094
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2095
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2096
+ return [4 /*yield*/, self._emitOutput(mode, result_3)];
2097
+ case 4:
2098
+ _d.sent();
2099
+ _d.label = 5;
2100
+ case 5: return [2 /*return*/, status];
2035
2101
  }
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
2102
  });
2047
- }
2048
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2049
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
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));
2056
- }
2057
- });
2058
- return [4 /*yield*/, lastToolStatusRef];
2059
- case 8:
2060
- status = _l.sent();
2061
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2062
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2063
- if (status === MODEL_RESQUE_SYMBOL) {
2064
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2065
- 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"));
2066
- ((_f = self.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
2067
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2068
- return [2 /*return*/, { value: void 0 }];
2069
- }
2070
- if (status === AGENT_CHANGE_SYMBOL) {
2071
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2072
- 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"));
2073
- ((_g = self.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
2074
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2075
- return [2 /*return*/, { value: void 0 }];
2076
- }
2077
- if (status === TOOL_STOP_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 commitStopTools call"));
2080
- ((_h = self.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
2081
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2082
- return [2 /*return*/, { value: void 0 }];
2083
- }
2084
- if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 11];
2085
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
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 call error"));
2087
- return [4 /*yield*/, self._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2088
- case 9:
2089
- result_3 = _l.sent();
2090
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2091
- self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
2092
- return [4 /*yield*/, self._emitOutput(mode, result_3)];
2093
- case 10:
2094
- _l.sent();
2095
- return [2 /*return*/, { value: void 0 }];
2096
- case 11: return [2 /*return*/];
2103
+ }); });
2104
+ return [2 /*return*/];
2097
2105
  }
2098
2106
  });
2099
2107
  };
2100
2108
  idx = 0;
2101
- _k.label = 5;
2109
+ _g.label = 5;
2102
2110
  case 5:
2103
2111
  if (!(idx !== toolCalls_1.length)) return [3 /*break*/, 8];
2104
2112
  return [5 /*yield**/, _loop_1(idx)];
2105
2113
  case 6:
2106
- state_1 = _k.sent();
2114
+ state_1 = _g.sent();
2107
2115
  if (typeof state_1 === "object")
2108
2116
  return [2 /*return*/, state_1.value];
2109
- _k.label = 7;
2117
+ _g.label = 7;
2110
2118
  case 7:
2111
2119
  idx++;
2112
2120
  return [3 /*break*/, 5];
2113
2121
  case 8:
2114
- ((_j = self.params.callbacks) === null || _j === void 0 ? void 0 : _j.onAfterToolCalls) &&
2115
- self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2122
+ lastToolStatusRef.finally(function () {
2123
+ var _a;
2124
+ ((_a = self.params.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterToolCalls) &&
2125
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls_1);
2126
+ });
2127
+ run(true);
2116
2128
  return [2 /*return*/];
2117
2129
  case 9:
2118
2130
  if (!message.tool_calls) {
@@ -2121,29 +2133,29 @@ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void
2121
2133
  }
2122
2134
  return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
2123
2135
  case 10:
2124
- result = _k.sent();
2136
+ result = _g.sent();
2125
2137
  return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
2126
2138
  case 11:
2127
- _k.sent();
2139
+ _g.sent();
2128
2140
  validation = null;
2129
2141
  return [4 /*yield*/, self.params.validate(result)];
2130
2142
  case 12:
2131
- if (!(validation = _k.sent())) return [3 /*break*/, 15];
2143
+ if (!(validation = _g.sent())) return [3 /*break*/, 15];
2132
2144
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2133
2145
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
2134
2146
  return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
2135
2147
  case 13:
2136
- result1 = _k.sent();
2148
+ result1 = _g.sent();
2137
2149
  return [4 /*yield*/, self._emitOutput(mode, result1)];
2138
2150
  case 14:
2139
- _k.sent();
2151
+ _g.sent();
2140
2152
  return [2 /*return*/];
2141
2153
  case 15:
2142
2154
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2143
2155
  self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
2144
2156
  return [4 /*yield*/, self._emitOutput(mode, result)];
2145
2157
  case 16:
2146
- _k.sent();
2158
+ _g.sent();
2147
2159
  return [2 /*return*/];
2148
2160
  }
2149
2161
  });
@@ -2286,10 +2298,11 @@ var ClientAgent = /** @class */ (function () {
2286
2298
  */
2287
2299
  ClientAgent.prototype._resurrectModel = function (mode_1) {
2288
2300
  return __awaiter(this, arguments, void 0, function (mode, reason) {
2289
- var rawMessage, message, result, validation, content;
2301
+ var rawMessage, content, message, result, validation, content;
2302
+ var _a;
2290
2303
  if (reason === void 0) { reason = "unknown"; }
2291
- return __generator(this, function (_a) {
2292
- switch (_a.label) {
2304
+ return __generator(this, function (_b) {
2305
+ switch (_b.label) {
2293
2306
  case 0:
2294
2307
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2295
2308
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
@@ -2304,7 +2317,7 @@ var ClientAgent = /** @class */ (function () {
2304
2317
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2305
2318
  })];
2306
2319
  case 1:
2307
- _a.sent();
2320
+ _b.sent();
2308
2321
  return [3 /*break*/, 8];
2309
2322
  case 2:
2310
2323
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush")) return [3 /*break*/, 5];
@@ -2315,7 +2328,7 @@ var ClientAgent = /** @class */ (function () {
2315
2328
  content: reason || "Unknown error",
2316
2329
  })];
2317
2330
  case 3:
2318
- _a.sent();
2331
+ _b.sent();
2319
2332
  return [4 /*yield*/, this.params.history.push({
2320
2333
  role: "user",
2321
2334
  mode: "tool",
@@ -2323,28 +2336,53 @@ var ClientAgent = /** @class */ (function () {
2323
2336
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_FLUSH_PROMPT,
2324
2337
  })];
2325
2338
  case 4:
2326
- _a.sent();
2339
+ _b.sent();
2327
2340
  return [3 /*break*/, 8];
2328
2341
  case 5:
2329
2342
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 7];
2330
2343
  return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2331
2344
  case 6:
2332
- _a.sent();
2345
+ _b.sent();
2333
2346
  return [3 /*break*/, 8];
2334
2347
  case 7: throw new Error("agent-swarm _resurrectModel invalid strategy value=".concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " agentName=").concat(this.params.agentName, " clientId=").concat(this.params.clientId));
2335
2348
  case 8: return [4 /*yield*/, this.getCompletion(mode)];
2336
2349
  case 9:
2337
- rawMessage = _a.sent();
2338
- return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2350
+ rawMessage = _b.sent();
2351
+ if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 13];
2352
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2353
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel failed due to tool_calls"));
2354
+ console.warn("agent-swarm model ressurect did not solved the problem (tool_calls) for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
2355
+ content = createPlaceholder();
2356
+ return [4 /*yield*/, this.params.history.push({
2357
+ role: "resque",
2358
+ mode: "tool",
2359
+ agentName: this.params.agentName,
2360
+ content: reason || "Unknown error",
2361
+ })];
2339
2362
  case 10:
2340
- message = _a.sent();
2341
- return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2363
+ _b.sent();
2364
+ return [4 /*yield*/, this.params.history.push({
2365
+ agentName: this.params.agentName,
2366
+ role: "assistant",
2367
+ mode: "tool",
2368
+ content: content,
2369
+ })];
2342
2370
  case 11:
2343
- result = _a.sent();
2371
+ _b.sent();
2372
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2373
+ case 12:
2374
+ _b.sent();
2375
+ return [2 /*return*/, content];
2376
+ case 13: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2377
+ case 14:
2378
+ message = _b.sent();
2379
+ return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2380
+ case 15:
2381
+ result = _b.sent();
2344
2382
  validation = null;
2345
2383
  return [4 /*yield*/, this.params.validate(result)];
2346
- case 12:
2347
- if (!(validation = _a.sent())) return [3 /*break*/, 16];
2384
+ case 16:
2385
+ if (!(validation = _b.sent())) return [3 /*break*/, 20];
2348
2386
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2349
2387
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2350
2388
  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));
@@ -2355,26 +2393,26 @@ var ClientAgent = /** @class */ (function () {
2355
2393
  agentName: this.params.agentName,
2356
2394
  content: reason || "Unknown error",
2357
2395
  })];
2358
- case 13:
2359
- _a.sent();
2396
+ case 17:
2397
+ _b.sent();
2360
2398
  return [4 /*yield*/, this.params.history.push({
2361
2399
  agentName: this.params.agentName,
2362
2400
  role: "assistant",
2363
2401
  mode: "tool",
2364
2402
  content: content,
2365
2403
  })];
2366
- case 14:
2367
- _a.sent();
2404
+ case 18:
2405
+ _b.sent();
2368
2406
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2369
- case 15:
2370
- _a.sent();
2407
+ case 19:
2408
+ _b.sent();
2371
2409
  return [2 /*return*/, content];
2372
- case 16: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2373
- case 17:
2374
- _a.sent();
2410
+ case 20: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2411
+ case 21:
2412
+ _b.sent();
2375
2413
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2376
- case 18:
2377
- _a.sent();
2414
+ case 22:
2415
+ _b.sent();
2378
2416
  return [2 /*return*/, result];
2379
2417
  }
2380
2418
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.147",
3
+ "version": "1.0.149",
4
4
  "description": "A TypeScript library for building orchestrated framework-agnostic multi-agent AI systems",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",