agent-swarm-kit 1.0.147 → 1.0.148

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 +117 -105
  2. package/build/index.mjs +118 -106
  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
2104
  });
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
- });
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
  });
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
2102
  });
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
- });
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
  });
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.148",
4
4
  "description": "A TypeScript library for building orchestrated framework-agnostic multi-agent AI systems",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",