agent-swarm-kit 1.0.121 → 1.0.122

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
@@ -359,6 +359,26 @@ var METHOD_NAME_USE_HISTORY_CALLBACKS = "HistoryUtils.useHistoryCallbacks";
359
359
  var METHOD_NAME_ITERATE = "HistoryUtils.iterate";
360
360
  var METHOD_NAME_PUSH = "HistoryUtils.push";
361
361
  var METHOD_NAME_DISPOSE = "HistoryUtils.dispose";
362
+ var HISTORY_INSTANCE_WAIT_FOR_INIT_FN = function (agentName, self) { return __awaiter(void 0, void 0, void 0, function () {
363
+ var _b;
364
+ return __generator(this, function (_c) {
365
+ switch (_c.label) {
366
+ case 0:
367
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
368
+ swarm$1.loggerService.debug(INSTANCE_METHOD_NAME_WAIT_FOR_INIT, {
369
+ clientId: self.clientId,
370
+ agentName: agentName,
371
+ });
372
+ if (!self.callbacks.getData) return [3 /*break*/, 2];
373
+ _b = self;
374
+ return [4 /*yield*/, self.callbacks.getData(self.clientId, agentName)];
375
+ case 1:
376
+ _b._array = _c.sent();
377
+ _c.label = 2;
378
+ case 2: return [2 /*return*/];
379
+ }
380
+ });
381
+ }); };
362
382
  /**
363
383
  * Class representing a History Instance
364
384
  */
@@ -377,26 +397,12 @@ var HistoryInstance = /** @class */ (function () {
377
397
  * Makes the singleshot for initialization
378
398
  * @param agentName - The agent name.
379
399
  */
380
- this[_a$1] = functoolsKit.singleshot(function (agentName) { return __awaiter(_this, void 0, void 0, function () {
381
- var _b;
382
- return __generator(this, function (_c) {
383
- switch (_c.label) {
384
- case 0:
385
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
386
- swarm$1.loggerService.debug(INSTANCE_METHOD_NAME_WAIT_FOR_INIT, {
387
- clientId: this.clientId,
388
- agentName: agentName,
389
- });
390
- if (!this.callbacks.getData) return [3 /*break*/, 2];
391
- _b = this;
392
- return [4 /*yield*/, this.callbacks.getData(this.clientId, agentName)];
393
- case 1:
394
- _b._array = _c.sent();
395
- _c.label = 2;
396
- case 2: return [2 /*return*/];
397
- }
398
- });
399
- }); });
400
+ this[_a$1] = functoolsKit.singleshot(function (agentName) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_b) {
401
+ switch (_b.label) {
402
+ case 0: return [4 /*yield*/, HISTORY_INSTANCE_WAIT_FOR_INIT_FN(agentName, this)];
403
+ case 1: return [2 /*return*/, _b.sent()];
404
+ }
405
+ }); }); });
400
406
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
401
407
  swarm$1.loggerService.debug(INSTANCE_METHOD_NAME_CTOR, {
402
408
  clientId: this.clientId,
@@ -950,6 +956,14 @@ var nameToTitle = function (name) {
950
956
 
951
957
  var _a;
952
958
  var LOGGER_INSTANCE_WAIT_FOR_INIT = Symbol("wait-for-init");
959
+ var LOGGER_INSTANCE_WAIT_FOR_FN = function (self) { return __awaiter(void 0, void 0, void 0, function () {
960
+ return __generator(this, function (_b) {
961
+ if (self.callbacks.onInit) {
962
+ self.callbacks.onInit(self.clientId);
963
+ }
964
+ return [2 /*return*/];
965
+ });
966
+ }); };
953
967
  /**
954
968
  * @class LoggerInstance
955
969
  * @implements ILoggerInstance
@@ -960,14 +974,12 @@ var LoggerInstance = /** @class */ (function () {
960
974
  var _this = this;
961
975
  this.clientId = clientId;
962
976
  this.callbacks = callbacks;
963
- this[_a] = functoolsKit.singleshot(function () { return __awaiter(_this, void 0, void 0, function () {
964
- return __generator(this, function (_b) {
965
- if (this.callbacks.onInit) {
966
- this.callbacks.onInit(this.clientId);
967
- }
968
- return [2 /*return*/];
969
- });
970
- }); });
977
+ this[_a] = functoolsKit.singleshot(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_b) {
978
+ switch (_b.label) {
979
+ case 0: return [4 /*yield*/, LOGGER_INSTANCE_WAIT_FOR_FN(this)];
980
+ case 1: return [2 /*return*/, _b.sent()];
981
+ }
982
+ }); }); });
971
983
  }
972
984
  /**
973
985
  * @method waitForInit
@@ -1695,6 +1707,285 @@ var TOOL_STOP_SYMBOL = Symbol("tool-stop");
1695
1707
  var getPlaceholder = function () {
1696
1708
  return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
1697
1709
  };
1710
+ var RUN_FN = function (incoming, self) { return __awaiter(void 0, void 0, void 0, function () {
1711
+ var messages, args, rawMessage, message, result, validation;
1712
+ var _a, _b, _c;
1713
+ return __generator(this, function (_d) {
1714
+ switch (_d.label) {
1715
+ case 0:
1716
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1717
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " run begin"), { incoming: incoming });
1718
+ self.params.onRun &&
1719
+ self.params.onRun(self.params.clientId, self.params.agentName, incoming);
1720
+ return [4 /*yield*/, self.params.history.toArrayForAgent(self.params.prompt, self.params.system)];
1721
+ case 1:
1722
+ messages = _d.sent();
1723
+ messages.push({
1724
+ agentName: self.params.agentName,
1725
+ content: incoming,
1726
+ mode: "user",
1727
+ role: "assistant",
1728
+ });
1729
+ args = {
1730
+ clientId: self.params.clientId,
1731
+ agentName: self.params.agentName,
1732
+ messages: messages,
1733
+ mode: "user",
1734
+ tools: (_a = self.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
1735
+ return lodashEs.omit(t, "toolName", "docNote", "call", "validate", "callbacks");
1736
+ }),
1737
+ };
1738
+ return [4 /*yield*/, self.params.completion.getCompletion(args)];
1739
+ case 2:
1740
+ rawMessage = _d.sent();
1741
+ ((_b = self.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
1742
+ ((_c = self.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, rawMessage));
1743
+ return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1744
+ case 3:
1745
+ message = _d.sent();
1746
+ return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
1747
+ case 4:
1748
+ result = _d.sent();
1749
+ return [4 /*yield*/, self.params.bus.emit(self.params.clientId, {
1750
+ type: "run",
1751
+ source: "agent-bus",
1752
+ input: {
1753
+ message: message,
1754
+ },
1755
+ output: {
1756
+ result: result,
1757
+ },
1758
+ context: {
1759
+ agentName: self.params.agentName,
1760
+ },
1761
+ clientId: self.params.clientId,
1762
+ })];
1763
+ case 5:
1764
+ _d.sent();
1765
+ if (message.tool_calls) {
1766
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1767
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " run should not call tools"), { incoming: incoming, result: result });
1768
+ return [2 /*return*/, ""];
1769
+ }
1770
+ validation = null;
1771
+ return [4 /*yield*/, self.params.validate(result)];
1772
+ case 6:
1773
+ if ((validation = _d.sent())) {
1774
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1775
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " run validation not passed: ").concat(validation), { incoming: incoming, result: result });
1776
+ return [2 /*return*/, ""];
1777
+ }
1778
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1779
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " run end result=").concat(result));
1780
+ return [2 /*return*/, result];
1781
+ }
1782
+ });
1783
+ }); };
1784
+ var EXECUTE_FN = function (incoming, mode, self) { return __awaiter(void 0, void 0, void 0, function () {
1785
+ var rawMessage, message, toolCalls, _loop_1, idx, state_1, result, validation, result1;
1786
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1787
+ return __generator(this, function (_j) {
1788
+ switch (_j.label) {
1789
+ case 0:
1790
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1791
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
1792
+ self.params.onExecute &&
1793
+ self.params.onExecute(self.params.clientId, self.params.agentName, incoming, mode);
1794
+ return [4 /*yield*/, self.params.history.push({
1795
+ role: "user",
1796
+ mode: mode,
1797
+ agentName: self.params.agentName,
1798
+ content: incoming.trim(),
1799
+ })];
1800
+ case 1:
1801
+ _j.sent();
1802
+ return [4 /*yield*/, self.getCompletion(mode)];
1803
+ case 2:
1804
+ rawMessage = _j.sent();
1805
+ return [4 /*yield*/, self.params.map(rawMessage, self.params.clientId, self.params.agentName)];
1806
+ case 3:
1807
+ message = _j.sent();
1808
+ if (!message.tool_calls) return [3 /*break*/, 9];
1809
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1810
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " tool call begin"));
1811
+ toolCalls = message.tool_calls.map(function (call) {
1812
+ var _a, _b;
1813
+ return ({
1814
+ function: call.function,
1815
+ id: (_a = call.id) !== null && _a !== void 0 ? _a : functoolsKit.randomString(),
1816
+ type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
1817
+ });
1818
+ });
1819
+ return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1820
+ case 4:
1821
+ _j.sent();
1822
+ _loop_1 = function (idx) {
1823
+ var tool, targetFn, result_1, result_2, status, result_3;
1824
+ return __generator(this, function (_k) {
1825
+ switch (_k.label) {
1826
+ case 0:
1827
+ tool = toolCalls[idx];
1828
+ targetFn = (_a = self.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
1829
+ if (!!targetFn) return [3 /*break*/, 3];
1830
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1831
+ 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);
1832
+ return [4 /*yield*/, self._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
1833
+ case 1:
1834
+ result_1 = _k.sent();
1835
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1836
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_1));
1837
+ return [4 /*yield*/, self._emitOuput(mode, result_1)];
1838
+ case 2:
1839
+ _k.sent();
1840
+ return [2 /*return*/, { value: void 0 }];
1841
+ case 3:
1842
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
1843
+ ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate(self.params.clientId, self.params.agentName, tool.function.arguments));
1844
+ return [4 /*yield*/, functoolsKit.not(targetFn.validate({
1845
+ clientId: self.params.clientId,
1846
+ agentName: self.params.agentName,
1847
+ params: tool.function.arguments,
1848
+ toolCalls: toolCalls,
1849
+ }))];
1850
+ case 4:
1851
+ if (!_k.sent()) return [3 /*break*/, 7];
1852
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1853
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
1854
+ return [4 /*yield*/, self._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
1855
+ case 5:
1856
+ result_2 = _k.sent();
1857
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1858
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_2));
1859
+ return [4 /*yield*/, self._emitOuput(mode, result_2)];
1860
+ case 6:
1861
+ _k.sent();
1862
+ return [2 /*return*/, { value: void 0 }];
1863
+ case 7:
1864
+ ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
1865
+ ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
1866
+ /**
1867
+ * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
1868
+ */
1869
+ Promise.resolve(targetFn.call({
1870
+ toolId: tool.id,
1871
+ clientId: self.params.clientId,
1872
+ agentName: self.params.agentName,
1873
+ params: tool.function.arguments,
1874
+ isLast: idx === toolCalls.length - 1,
1875
+ toolCalls: toolCalls,
1876
+ }))
1877
+ .then(function () {
1878
+ var _a, _b;
1879
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
1880
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments));
1881
+ })
1882
+ .catch(function (error) {
1883
+ var _a, _b;
1884
+ console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(functoolsKit.getErrorMessage(error)), {
1885
+ clientId: self.params.clientId,
1886
+ agentName: self.params.agentName,
1887
+ tool_call_id: tool.id,
1888
+ arguments: tool.function.arguments,
1889
+ error: functoolsKit.errorData(error),
1890
+ });
1891
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onCallError) &&
1892
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onCallError(tool.id, self.params.clientId, self.params.agentName, tool.function.arguments, error));
1893
+ self._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
1894
+ });
1895
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1896
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
1897
+ return [4 /*yield*/, Promise.race([
1898
+ self._agentChangeSubject.toPromise(),
1899
+ self._toolCommitSubject.toPromise(),
1900
+ self._toolErrorSubject.toPromise(),
1901
+ self._toolStopSubject.toPromise(),
1902
+ self._outputSubject.toPromise(),
1903
+ ])];
1904
+ case 8:
1905
+ status = _k.sent();
1906
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1907
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
1908
+ if (status === AGENT_CHANGE_SYMBOL) {
1909
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1910
+ 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"));
1911
+ ((_f = self.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
1912
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
1913
+ return [2 /*return*/, { value: void 0 }];
1914
+ }
1915
+ if (status === TOOL_STOP_SYMBOL) {
1916
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1917
+ 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"));
1918
+ ((_g = self.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
1919
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
1920
+ return [2 /*return*/, { value: void 0 }];
1921
+ }
1922
+ if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 11];
1923
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1924
+ 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"));
1925
+ return [4 /*yield*/, self._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
1926
+ case 9:
1927
+ result_3 = _k.sent();
1928
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1929
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result_3));
1930
+ return [4 /*yield*/, self._emitOuput(mode, result_3)];
1931
+ case 10:
1932
+ _k.sent();
1933
+ return [2 /*return*/, { value: void 0 }];
1934
+ case 11: return [2 /*return*/];
1935
+ }
1936
+ });
1937
+ };
1938
+ idx = 0;
1939
+ _j.label = 5;
1940
+ case 5:
1941
+ if (!(idx !== toolCalls.length)) return [3 /*break*/, 8];
1942
+ return [5 /*yield**/, _loop_1(idx)];
1943
+ case 6:
1944
+ state_1 = _j.sent();
1945
+ if (typeof state_1 === "object")
1946
+ return [2 /*return*/, state_1.value];
1947
+ _j.label = 7;
1948
+ case 7:
1949
+ idx++;
1950
+ return [3 /*break*/, 5];
1951
+ case 8:
1952
+ ((_h = self.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
1953
+ self.params.callbacks.onAfterToolCalls(self.params.clientId, self.params.agentName, toolCalls);
1954
+ return [2 /*return*/];
1955
+ case 9:
1956
+ if (!message.tool_calls) {
1957
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1958
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute no tool calls detected"));
1959
+ }
1960
+ return [4 /*yield*/, self.params.transform(message.content, self.params.clientId, self.params.agentName)];
1961
+ case 10:
1962
+ result = _j.sent();
1963
+ return [4 /*yield*/, self.params.history.push(__assign(__assign({}, message), { agentName: self.params.agentName }))];
1964
+ case 11:
1965
+ _j.sent();
1966
+ validation = null;
1967
+ return [4 /*yield*/, self.params.validate(result)];
1968
+ case 12:
1969
+ if (!(validation = _j.sent())) return [3 /*break*/, 15];
1970
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1971
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
1972
+ return [4 /*yield*/, self._resurrectModel(mode, "Invalid model output: ".concat(result))];
1973
+ case 13:
1974
+ result1 = _j.sent();
1975
+ return [4 /*yield*/, self._emitOuput(mode, result1)];
1976
+ case 14:
1977
+ _j.sent();
1978
+ return [2 /*return*/];
1979
+ case 15:
1980
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
1981
+ self.params.logger.debug("ClientAgent agentName=".concat(self.params.agentName, " clientId=").concat(self.params.clientId, " execute end result=").concat(result));
1982
+ return [4 /*yield*/, self._emitOuput(mode, result)];
1983
+ case 16:
1984
+ _j.sent();
1985
+ return [2 /*return*/];
1986
+ }
1987
+ });
1988
+ }); };
1698
1989
  /**
1699
1990
  * Represents a client agent that interacts with the system.
1700
1991
  * @implements {IAgent}
@@ -1713,13 +2004,42 @@ var ClientAgent = /** @class */ (function () {
1713
2004
  this._toolCommitSubject = new functoolsKit.Subject();
1714
2005
  this._outputSubject = new functoolsKit.Subject();
1715
2006
  /**
1716
- * Emits the output result after validation.
1717
- * @param {string} result - The result to be emitted.
2007
+ * Executes the incoming message and processes tool calls if any.
2008
+ * @param {string} incoming - The incoming message content.
2009
+ * @returns {Promise<void>}
2010
+ */
2011
+ this.execute = functoolsKit.queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
2012
+ switch (_a.label) {
2013
+ case 0: return [4 /*yield*/, EXECUTE_FN(incoming, mode, this)];
2014
+ case 1: return [2 /*return*/, _a.sent()];
2015
+ }
2016
+ }); }); });
2017
+ /**
2018
+ * Run the completion stateless and return the output
2019
+ * @param {string} incoming - The incoming message content.
1718
2020
  * @returns {Promise<void>}
1719
- * @private
1720
2021
  */
1721
- this._emitOuput = function (mode, rawResult) { return __awaiter(_this, void 0, void 0, function () {
1722
- var result, validation, rawResult_1, result_1;
2022
+ this.run = functoolsKit.queued(function (incoming) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
2023
+ switch (_a.label) {
2024
+ case 0: return [4 /*yield*/, RUN_FN(incoming, this)];
2025
+ case 1: return [2 /*return*/, _a.sent()];
2026
+ }
2027
+ }); }); });
2028
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2029
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
2030
+ params: params,
2031
+ });
2032
+ this.params.onInit && this.params.onInit(params.clientId, params.agentName);
2033
+ }
2034
+ /**
2035
+ * Emits the output result after validation.
2036
+ * @param {string} result - The result to be emitted.
2037
+ * @returns {Promise<void>}
2038
+ * @private
2039
+ */
2040
+ ClientAgent.prototype._emitOuput = function (mode, rawResult) {
2041
+ return __awaiter(this, void 0, void 0, function () {
2042
+ var result, validation, rawResult_1, result_4;
1723
2043
  return __generator(this, function (_a) {
1724
2044
  switch (_a.label) {
1725
2045
  case 0: return [4 /*yield*/, this.params.transform(rawResult, this.params.clientId, this.params.agentName)];
@@ -1736,17 +2056,17 @@ var ClientAgent = /** @class */ (function () {
1736
2056
  rawResult_1 = _a.sent();
1737
2057
  return [4 /*yield*/, this.params.transform(rawResult_1, this.params.clientId, this.params.agentName)];
1738
2058
  case 4:
1739
- result_1 = _a.sent();
1740
- return [4 /*yield*/, this.params.validate(result_1)];
2059
+ result_4 = _a.sent();
2060
+ return [4 /*yield*/, this.params.validate(result_4)];
1741
2061
  case 5:
1742
2062
  if ((validation = _a.sent())) {
1743
2063
  throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " model ressurect failed: ").concat(validation));
1744
2064
  }
1745
2065
  this.params.onOutput &&
1746
- this.params.onOutput(this.params.clientId, this.params.agentName, result_1);
2066
+ this.params.onOutput(this.params.clientId, this.params.agentName, result_4);
1747
2067
  this.params.onAssistantMessage &&
1748
- this.params.onAssistantMessage(this.params.clientId, this.params.agentName, result_1);
1749
- return [4 /*yield*/, this._outputSubject.next(result_1)];
2068
+ this.params.onAssistantMessage(this.params.clientId, this.params.agentName, result_4);
2069
+ return [4 /*yield*/, this._outputSubject.next(result_4)];
1750
2070
  case 6:
1751
2071
  _a.sent();
1752
2072
  return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
@@ -1757,7 +2077,7 @@ var ClientAgent = /** @class */ (function () {
1757
2077
  rawResult: rawResult_1,
1758
2078
  },
1759
2079
  output: {
1760
- result: result_1,
2080
+ result: result_4,
1761
2081
  },
1762
2082
  context: {
1763
2083
  agentName: this.params.agentName,
@@ -1793,14 +2113,16 @@ var ClientAgent = /** @class */ (function () {
1793
2113
  return [2 /*return*/];
1794
2114
  }
1795
2115
  });
1796
- }); };
1797
- /**
1798
- * Resurrects the model based on the given reason.
1799
- * @param {string} [reason] - The reason for resurrecting the model.
1800
- * @returns {Promise<string>}
1801
- * @private
1802
- */
1803
- this._resurrectModel = function (mode, reason) { return __awaiter(_this, void 0, void 0, function () {
2116
+ });
2117
+ };
2118
+ /**
2119
+ * Resurrects the model based on the given reason.
2120
+ * @param {string} [reason] - The reason for resurrecting the model.
2121
+ * @returns {Promise<string>}
2122
+ * @private
2123
+ */
2124
+ ClientAgent.prototype._resurrectModel = function (mode, reason) {
2125
+ return __awaiter(this, void 0, void 0, function () {
1804
2126
  var rawMessage, message, result, validation, content;
1805
2127
  return __generator(this, function (_a) {
1806
2128
  switch (_a.label) {
@@ -1856,12 +2178,14 @@ var ClientAgent = /** @class */ (function () {
1856
2178
  return [2 /*return*/, result];
1857
2179
  }
1858
2180
  });
1859
- }); };
1860
- /**
1861
- * Waits for the output to be available.
1862
- * @returns {Promise<string>}
1863
- */
1864
- this.waitForOutput = function () { return __awaiter(_this, void 0, void 0, function () {
2181
+ });
2182
+ };
2183
+ /**
2184
+ * Waits for the output to be available.
2185
+ * @returns {Promise<string>}
2186
+ */
2187
+ ClientAgent.prototype.waitForOutput = function () {
2188
+ return __awaiter(this, void 0, void 0, function () {
1865
2189
  return __generator(this, function (_a) {
1866
2190
  switch (_a.label) {
1867
2191
  case 0:
@@ -1871,12 +2195,14 @@ var ClientAgent = /** @class */ (function () {
1871
2195
  case 1: return [2 /*return*/, _a.sent()];
1872
2196
  }
1873
2197
  });
1874
- }); };
1875
- /**
1876
- * Gets the completion message from the model.
1877
- * @returns {Promise<IModelMessage>}
1878
- */
1879
- this.getCompletion = function (mode) { return __awaiter(_this, void 0, void 0, function () {
2198
+ });
2199
+ };
2200
+ /**
2201
+ * Gets the completion message from the model.
2202
+ * @returns {Promise<IModelMessage>}
2203
+ */
2204
+ ClientAgent.prototype.getCompletion = function (mode) {
2205
+ return __awaiter(this, void 0, void 0, function () {
1880
2206
  var messages, args, output;
1881
2207
  var _a, _b, _c;
1882
2208
  return __generator(this, function (_d) {
@@ -1904,13 +2230,15 @@ var ClientAgent = /** @class */ (function () {
1904
2230
  return [2 /*return*/, output];
1905
2231
  }
1906
2232
  });
1907
- }); };
1908
- /**
1909
- * Commits a user message to the history without answer.
1910
- * @param {string} message - The message to commit.
1911
- * @returns {Promise<void>}
1912
- */
1913
- this.commitUserMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
2233
+ });
2234
+ };
2235
+ /**
2236
+ * Commits a user message to the history without answer.
2237
+ * @param {string} message - The message to commit.
2238
+ * @returns {Promise<void>}
2239
+ */
2240
+ ClientAgent.prototype.commitUserMessage = function (message) {
2241
+ return __awaiter(this, void 0, void 0, function () {
1914
2242
  return __generator(this, function (_a) {
1915
2243
  switch (_a.label) {
1916
2244
  case 0:
@@ -1943,12 +2271,14 @@ var ClientAgent = /** @class */ (function () {
1943
2271
  return [2 /*return*/];
1944
2272
  }
1945
2273
  });
1946
- }); };
1947
- /**
1948
- * Commits flush of agent history
1949
- * @returns {Promise<void>}
1950
- */
1951
- this.commitFlush = function () { return __awaiter(_this, void 0, void 0, function () {
2274
+ });
2275
+ };
2276
+ /**
2277
+ * Commits flush of agent history
2278
+ * @returns {Promise<void>}
2279
+ */
2280
+ ClientAgent.prototype.commitFlush = function () {
2281
+ return __awaiter(this, void 0, void 0, function () {
1952
2282
  return __generator(this, function (_a) {
1953
2283
  switch (_a.label) {
1954
2284
  case 0:
@@ -1979,12 +2309,14 @@ var ClientAgent = /** @class */ (function () {
1979
2309
  return [2 /*return*/];
1980
2310
  }
1981
2311
  });
1982
- }); };
1983
- /**
1984
- * Commits change of agent to prevent the next tool execution from being called.
1985
- * @returns {Promise<void>}
1986
- */
1987
- this.commitAgentChange = function () { return __awaiter(_this, void 0, void 0, function () {
2312
+ });
2313
+ };
2314
+ /**
2315
+ * Commits change of agent to prevent the next tool execution from being called.
2316
+ * @returns {Promise<void>}
2317
+ */
2318
+ ClientAgent.prototype.commitAgentChange = function () {
2319
+ return __awaiter(this, void 0, void 0, function () {
1988
2320
  return __generator(this, function (_a) {
1989
2321
  switch (_a.label) {
1990
2322
  case 0:
@@ -2008,12 +2340,14 @@ var ClientAgent = /** @class */ (function () {
2008
2340
  return [2 /*return*/];
2009
2341
  }
2010
2342
  });
2011
- }); };
2012
- /**
2013
- * Commits change of agent to prevent the next tool execution from being called.
2014
- * @returns {Promise<void>}
2015
- */
2016
- this.commitStopTools = function () { return __awaiter(_this, void 0, void 0, function () {
2343
+ });
2344
+ };
2345
+ /**
2346
+ * Commits change of agent to prevent the next tool execution from being called.
2347
+ * @returns {Promise<void>}
2348
+ */
2349
+ ClientAgent.prototype.commitStopTools = function () {
2350
+ return __awaiter(this, void 0, void 0, function () {
2017
2351
  return __generator(this, function (_a) {
2018
2352
  switch (_a.label) {
2019
2353
  case 0:
@@ -2037,13 +2371,15 @@ var ClientAgent = /** @class */ (function () {
2037
2371
  return [2 /*return*/];
2038
2372
  }
2039
2373
  });
2040
- }); };
2041
- /**
2042
- * Commits a system message to the history.
2043
- * @param {string} message - The system message to commit.
2044
- * @returns {Promise<void>}
2045
- */
2046
- this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
2374
+ });
2375
+ };
2376
+ /**
2377
+ * Commits a system message to the history.
2378
+ * @param {string} message - The system message to commit.
2379
+ * @returns {Promise<void>}
2380
+ */
2381
+ ClientAgent.prototype.commitSystemMessage = function (message) {
2382
+ return __awaiter(this, void 0, void 0, function () {
2047
2383
  return __generator(this, function (_a) {
2048
2384
  switch (_a.label) {
2049
2385
  case 0:
@@ -2076,13 +2412,15 @@ var ClientAgent = /** @class */ (function () {
2076
2412
  return [2 /*return*/];
2077
2413
  }
2078
2414
  });
2079
- }); };
2080
- /**
2081
- * Commits an assistant message to the history without execute.
2082
- * @param {string} message - The system message to commit.
2083
- * @returns {Promise<void>}
2084
- */
2085
- this.commitAssistantMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
2415
+ });
2416
+ };
2417
+ /**
2418
+ * Commits an assistant message to the history without execute.
2419
+ * @param {string} message - The system message to commit.
2420
+ * @returns {Promise<void>}
2421
+ */
2422
+ ClientAgent.prototype.commitAssistantMessage = function (message) {
2423
+ return __awaiter(this, void 0, void 0, function () {
2086
2424
  return __generator(this, function (_a) {
2087
2425
  switch (_a.label) {
2088
2426
  case 0:
@@ -2115,13 +2453,15 @@ var ClientAgent = /** @class */ (function () {
2115
2453
  return [2 /*return*/];
2116
2454
  }
2117
2455
  });
2118
- }); };
2119
- /**
2120
- * Commits the tool output to the history.
2121
- * @param {string} content - The tool output content.
2122
- * @returns {Promise<void>}
2123
- */
2124
- this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
2456
+ });
2457
+ };
2458
+ /**
2459
+ * Commits the tool output to the history.
2460
+ * @param {string} content - The tool output content.
2461
+ * @returns {Promise<void>}
2462
+ */
2463
+ ClientAgent.prototype.commitToolOutput = function (toolId, content) {
2464
+ return __awaiter(this, void 0, void 0, function () {
2125
2465
  return __generator(this, function (_a) {
2126
2466
  switch (_a.label) {
2127
2467
  case 0:
@@ -2159,303 +2499,14 @@ var ClientAgent = /** @class */ (function () {
2159
2499
  return [2 /*return*/];
2160
2500
  }
2161
2501
  });
2162
- }); };
2163
- /**
2164
- * Executes the incoming message and processes tool calls if any.
2165
- * @param {string} incoming - The incoming message content.
2166
- * @returns {Promise<void>}
2167
- */
2168
- this.execute = functoolsKit.queued(function (incoming, mode) { return __awaiter(_this, void 0, void 0, function () {
2169
- var rawMessage, message, toolCalls, _loop_1, this_1, idx, state_1, result, validation, result1;
2170
- var _this = this;
2171
- var _a, _b, _c, _d, _e, _f, _g, _h;
2172
- return __generator(this, function (_j) {
2173
- switch (_j.label) {
2174
- case 0:
2175
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2176
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
2177
- this.params.onExecute &&
2178
- this.params.onExecute(this.params.clientId, this.params.agentName, incoming, mode);
2179
- return [4 /*yield*/, this.params.history.push({
2180
- role: "user",
2181
- mode: mode,
2182
- agentName: this.params.agentName,
2183
- content: incoming.trim(),
2184
- })];
2185
- case 1:
2186
- _j.sent();
2187
- return [4 /*yield*/, this.getCompletion(mode)];
2188
- case 2:
2189
- rawMessage = _j.sent();
2190
- return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2191
- case 3:
2192
- message = _j.sent();
2193
- if (!message.tool_calls) return [3 /*break*/, 9];
2194
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2195
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
2196
- toolCalls = message.tool_calls.map(function (call) {
2197
- var _a, _b;
2198
- return ({
2199
- function: call.function,
2200
- id: (_a = call.id) !== null && _a !== void 0 ? _a : functoolsKit.randomString(),
2201
- type: (_b = call.type) !== null && _b !== void 0 ? _b : "function",
2202
- });
2203
- });
2204
- return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2205
- case 4:
2206
- _j.sent();
2207
- _loop_1 = function (idx) {
2208
- var tool, targetFn, result_2, result_3, status, result_4;
2209
- return __generator(this, function (_k) {
2210
- switch (_k.label) {
2211
- case 0:
2212
- tool = toolCalls[idx];
2213
- targetFn = (_a = this_1.params.tools) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.function.name === tool.function.name; });
2214
- if (!!targetFn) return [3 /*break*/, 3];
2215
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2216
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool function not found"), this_1.params.tools);
2217
- return [4 /*yield*/, this_1._resurrectModel(mode, "No target function for ".concat(tool.function.name))];
2218
- case 1:
2219
- result_2 = _k.sent();
2220
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2221
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
2222
- return [4 /*yield*/, this_1._emitOuput(mode, result_2)];
2223
- case 2:
2224
- _k.sent();
2225
- return [2 /*return*/, { value: void 0 }];
2226
- case 3:
2227
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onValidate) &&
2228
- ((_c = targetFn.callbacks) === null || _c === void 0 ? void 0 : _c.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
2229
- return [4 /*yield*/, functoolsKit.not(targetFn.validate({
2230
- clientId: this_1.params.clientId,
2231
- agentName: this_1.params.agentName,
2232
- params: tool.function.arguments,
2233
- toolCalls: toolCalls,
2234
- }))];
2235
- case 4:
2236
- if (!_k.sent()) return [3 /*break*/, 7];
2237
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2238
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
2239
- return [4 /*yield*/, this_1._resurrectModel(mode, "Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2240
- case 5:
2241
- result_3 = _k.sent();
2242
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2243
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
2244
- return [4 /*yield*/, this_1._emitOuput(mode, result_3)];
2245
- case 6:
2246
- _k.sent();
2247
- return [2 /*return*/, { value: void 0 }];
2248
- case 7:
2249
- ((_d = targetFn.callbacks) === null || _d === void 0 ? void 0 : _d.onBeforeCall) &&
2250
- ((_e = targetFn.callbacks) === null || _e === void 0 ? void 0 : _e.onBeforeCall(tool.id, this_1.params.clientId, this_1.params.agentName, tool.function.arguments));
2251
- /**
2252
- * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
2253
- */
2254
- Promise.resolve(targetFn.call({
2255
- toolId: tool.id,
2256
- clientId: this_1.params.clientId,
2257
- agentName: this_1.params.agentName,
2258
- params: tool.function.arguments,
2259
- isLast: idx === toolCalls.length - 1,
2260
- toolCalls: toolCalls,
2261
- }))
2262
- .then(function () {
2263
- var _a, _b;
2264
- ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
2265
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments));
2266
- })
2267
- .catch(function (error) {
2268
- var _a, _b;
2269
- console.error("agent-swarm tool call error functionName=".concat(tool.function.name, " error=").concat(functoolsKit.getErrorMessage(error)), {
2270
- clientId: _this.params.clientId,
2271
- agentName: _this.params.agentName,
2272
- tool_call_id: tool.id,
2273
- arguments: tool.function.arguments,
2274
- error: functoolsKit.errorData(error),
2275
- });
2276
- ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onCallError) &&
2277
- ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onCallError(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments, error));
2278
- _this._toolErrorSubject.next(TOOL_ERROR_SYMBOL);
2279
- });
2280
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2281
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
2282
- return [4 /*yield*/, Promise.race([
2283
- this_1._agentChangeSubject.toPromise(),
2284
- this_1._toolCommitSubject.toPromise(),
2285
- this_1._toolErrorSubject.toPromise(),
2286
- this_1._toolStopSubject.toPromise(),
2287
- this_1._outputSubject.toPromise(),
2288
- ])];
2289
- case 8:
2290
- status = _k.sent();
2291
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2292
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
2293
- if (status === AGENT_CHANGE_SYMBOL) {
2294
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2295
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the agent changed"));
2296
- ((_f = this_1.params.callbacks) === null || _f === void 0 ? void 0 : _f.onAfterToolCalls) &&
2297
- this_1.params.callbacks.onAfterToolCalls(this_1.params.clientId, this_1.params.agentName, toolCalls);
2298
- return [2 /*return*/, { value: void 0 }];
2299
- }
2300
- if (status === TOOL_STOP_SYMBOL) {
2301
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2302
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the commitStopTools call"));
2303
- ((_g = this_1.params.callbacks) === null || _g === void 0 ? void 0 : _g.onAfterToolCalls) &&
2304
- this_1.params.callbacks.onAfterToolCalls(this_1.params.clientId, this_1.params.agentName, toolCalls);
2305
- return [2 /*return*/, { value: void 0 }];
2306
- }
2307
- if (!(status === TOOL_ERROR_SYMBOL)) return [3 /*break*/, 11];
2308
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2309
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " the next tool execution stopped due to the call error"));
2310
- return [4 /*yield*/, this_1._resurrectModel(mode, "Function call failed with error: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
2311
- case 9:
2312
- result_4 = _k.sent();
2313
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2314
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_4));
2315
- return [4 /*yield*/, this_1._emitOuput(mode, result_4)];
2316
- case 10:
2317
- _k.sent();
2318
- return [2 /*return*/, { value: void 0 }];
2319
- case 11: return [2 /*return*/];
2320
- }
2321
- });
2322
- };
2323
- this_1 = this;
2324
- idx = 0;
2325
- _j.label = 5;
2326
- case 5:
2327
- if (!(idx !== toolCalls.length)) return [3 /*break*/, 8];
2328
- return [5 /*yield**/, _loop_1(idx)];
2329
- case 6:
2330
- state_1 = _j.sent();
2331
- if (typeof state_1 === "object")
2332
- return [2 /*return*/, state_1.value];
2333
- _j.label = 7;
2334
- case 7:
2335
- idx++;
2336
- return [3 /*break*/, 5];
2337
- case 8:
2338
- ((_h = this.params.callbacks) === null || _h === void 0 ? void 0 : _h.onAfterToolCalls) &&
2339
- this.params.callbacks.onAfterToolCalls(this.params.clientId, this.params.agentName, toolCalls);
2340
- return [2 /*return*/];
2341
- case 9:
2342
- if (!message.tool_calls) {
2343
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2344
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
2345
- }
2346
- return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2347
- case 10:
2348
- result = _j.sent();
2349
- return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2350
- case 11:
2351
- _j.sent();
2352
- validation = null;
2353
- return [4 /*yield*/, this.params.validate(result)];
2354
- case 12:
2355
- if (!(validation = _j.sent())) return [3 /*break*/, 15];
2356
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2357
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
2358
- return [4 /*yield*/, this._resurrectModel(mode, "Invalid model output: ".concat(result))];
2359
- case 13:
2360
- result1 = _j.sent();
2361
- return [4 /*yield*/, this._emitOuput(mode, result1)];
2362
- case 14:
2363
- _j.sent();
2364
- return [2 /*return*/];
2365
- case 15:
2366
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2367
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
2368
- return [4 /*yield*/, this._emitOuput(mode, result)];
2369
- case 16:
2370
- _j.sent();
2371
- return [2 /*return*/];
2372
- }
2373
- });
2374
- }); });
2375
- /**
2376
- * Run the completion stateless and return the output
2377
- * @param {string} incoming - The incoming message content.
2378
- * @returns {Promise<void>}
2379
- */
2380
- this.run = functoolsKit.queued(function (incoming) { return __awaiter(_this, void 0, void 0, function () {
2381
- var messages, args, rawMessage, message, result, validation;
2382
- var _a, _b, _c;
2383
- return __generator(this, function (_d) {
2384
- switch (_d.label) {
2385
- case 0:
2386
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2387
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " run begin"), { incoming: incoming });
2388
- this.params.onRun &&
2389
- this.params.onRun(this.params.clientId, this.params.agentName, incoming);
2390
- return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2391
- case 1:
2392
- messages = _d.sent();
2393
- messages.push({
2394
- agentName: this.params.agentName,
2395
- content: incoming,
2396
- mode: "user",
2397
- role: "assistant",
2398
- });
2399
- args = {
2400
- clientId: this.params.clientId,
2401
- agentName: this.params.agentName,
2402
- messages: messages,
2403
- mode: "user",
2404
- tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
2405
- return lodashEs.omit(t, "toolName", "docNote", "call", "validate", "callbacks");
2406
- }),
2407
- };
2408
- return [4 /*yield*/, this.params.completion.getCompletion(args)];
2409
- case 2:
2410
- rawMessage = _d.sent();
2411
- ((_b = this.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
2412
- ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, rawMessage));
2413
- return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2414
- case 3:
2415
- message = _d.sent();
2416
- return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2417
- case 4:
2418
- result = _d.sent();
2419
- return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2420
- type: "run",
2421
- source: "agent-bus",
2422
- input: {
2423
- message: message,
2424
- },
2425
- output: {
2426
- result: result,
2427
- },
2428
- context: {
2429
- agentName: this.params.agentName,
2430
- },
2431
- clientId: this.params.clientId,
2432
- })];
2433
- case 5:
2434
- _d.sent();
2435
- if (message.tool_calls) {
2436
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2437
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " run should not call tools"), { incoming: incoming, result: result });
2438
- return [2 /*return*/, ""];
2439
- }
2440
- validation = null;
2441
- return [4 /*yield*/, this.params.validate(result)];
2442
- case 6:
2443
- if ((validation = _d.sent())) {
2444
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2445
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " run validation not passed: ").concat(validation), { incoming: incoming, result: result });
2446
- return [2 /*return*/, ""];
2447
- }
2448
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2449
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " run end result=").concat(result));
2450
- return [2 /*return*/, result];
2451
- }
2452
- });
2453
- }); });
2454
- /**
2455
- * Should call on agent dispose
2456
- * @returns {Promise<void>}
2457
- */
2458
- this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
2502
+ });
2503
+ };
2504
+ /**
2505
+ * Should call on agent dispose
2506
+ * @returns {Promise<void>}
2507
+ */
2508
+ ClientAgent.prototype.dispose = function () {
2509
+ return __awaiter(this, void 0, void 0, function () {
2459
2510
  return __generator(this, function (_a) {
2460
2511
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2461
2512
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " dispose"));
@@ -2463,13 +2514,8 @@ var ClientAgent = /** @class */ (function () {
2463
2514
  this.params.onDispose(this.params.clientId, this.params.agentName);
2464
2515
  return [2 /*return*/];
2465
2516
  });
2466
- }); };
2467
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2468
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
2469
- params: params,
2470
- });
2471
- this.params.onInit && this.params.onInit(params.clientId, params.agentName);
2472
- }
2517
+ });
2518
+ };
2473
2519
  return ClientAgent;
2474
2520
  }());
2475
2521
 
@@ -2723,14 +2769,20 @@ var ClientHistory = /** @class */ (function () {
2723
2769
  * @param {IHistoryParams} params - The parameters for the history.
2724
2770
  */
2725
2771
  function ClientHistory(params) {
2726
- var _this = this;
2727
2772
  this.params = params;
2728
- /**
2729
- * Pushes a message to the history.
2730
- * @param {IModelMessage} message - The message to push.
2731
- * @returns {Promise<void>}
2732
- */
2733
- this.push = function (message) { return __awaiter(_this, void 0, void 0, function () {
2773
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2774
+ this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
2775
+ params: params,
2776
+ });
2777
+ this._filterCondition = GLOBAL_CONFIG.CC_AGENT_HISTORY_FILTER(this.params.agentName);
2778
+ }
2779
+ /**
2780
+ * Pushes a message to the history.
2781
+ * @param {IModelMessage} message - The message to push.
2782
+ * @returns {Promise<void>}
2783
+ */
2784
+ ClientHistory.prototype.push = function (message) {
2785
+ return __awaiter(this, void 0, void 0, function () {
2734
2786
  return __generator(this, function (_a) {
2735
2787
  switch (_a.label) {
2736
2788
  case 0:
@@ -2756,12 +2808,14 @@ var ClientHistory = /** @class */ (function () {
2756
2808
  return [2 /*return*/];
2757
2809
  }
2758
2810
  });
2759
- }); };
2760
- /**
2761
- * Converts the history to an array of raw messages.
2762
- * @returns {Promise<IModelMessage[]>} - The array of raw messages.
2763
- */
2764
- this.toArrayForRaw = function () { return __awaiter(_this, void 0, void 0, function () {
2811
+ });
2812
+ };
2813
+ /**
2814
+ * Converts the history to an array of raw messages.
2815
+ * @returns {Promise<IModelMessage[]>} - The array of raw messages.
2816
+ */
2817
+ ClientHistory.prototype.toArrayForRaw = function () {
2818
+ return __awaiter(this, void 0, void 0, function () {
2765
2819
  var result, _a, _b, _c, item, e_1_1;
2766
2820
  var _d, e_1, _e, _f;
2767
2821
  return __generator(this, function (_g) {
@@ -2806,14 +2860,16 @@ var ClientHistory = /** @class */ (function () {
2806
2860
  case 12: return [2 /*return*/, result];
2807
2861
  }
2808
2862
  });
2809
- }); };
2810
- /**
2811
- * Converts the history to an array of messages for the agent.
2812
- * @param {string} prompt - The prompt message.
2813
- * @param {string} system - The tool calling protocol
2814
- * @returns {Promise<IModelMessage[]>} - The array of messages for the agent.
2815
- */
2816
- this.toArrayForAgent = function (prompt, system) { return __awaiter(_this, void 0, void 0, function () {
2863
+ });
2864
+ };
2865
+ /**
2866
+ * Converts the history to an array of messages for the agent.
2867
+ * @param {string} prompt - The prompt message.
2868
+ * @param {string} system - The tool calling protocol
2869
+ * @returns {Promise<IModelMessage[]>} - The array of messages for the agent.
2870
+ */
2871
+ ClientHistory.prototype.toArrayForAgent = function (prompt, system) {
2872
+ return __awaiter(this, void 0, void 0, function () {
2817
2873
  var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, assistantToolOutputCallSet, assistantRawMessages, assistantToolCallSet, assistantMessages, promptMessages;
2818
2874
  var _this = this;
2819
2875
  var _d, e_2, _e, _f;
@@ -2954,12 +3010,14 @@ var ClientHistory = /** @class */ (function () {
2954
3010
  return [2 /*return*/, __spreadArray(__spreadArray(__spreadArray([], __read(promptMessages), false), __read(systemMessages), false), __read(assistantMessages), false)];
2955
3011
  }
2956
3012
  });
2957
- }); };
2958
- /**
2959
- * Should call on agent dispose
2960
- * @returns {Promise<void>}
2961
- */
2962
- this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
3013
+ });
3014
+ };
3015
+ /**
3016
+ * Should call on agent dispose
3017
+ * @returns {Promise<void>}
3018
+ */
3019
+ ClientHistory.prototype.dispose = function () {
3020
+ return __awaiter(this, void 0, void 0, function () {
2963
3021
  return __generator(this, function (_a) {
2964
3022
  switch (_a.label) {
2965
3023
  case 0:
@@ -2971,13 +3029,8 @@ var ClientHistory = /** @class */ (function () {
2971
3029
  return [2 /*return*/];
2972
3030
  }
2973
3031
  });
2974
- }); };
2975
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2976
- this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
2977
- params: params,
2978
- });
2979
- this._filterCondition = GLOBAL_CONFIG.CC_AGENT_HISTORY_FILTER(this.params.agentName);
2980
- }
3032
+ });
3033
+ };
2981
3034
  return ClientHistory;
2982
3035
  }());
2983
3036
 
@@ -3146,6 +3199,77 @@ var ToolSchemaService = /** @class */ (function () {
3146
3199
 
3147
3200
  var AGENT_NEED_FETCH = Symbol("agent-need-fetch");
3148
3201
  var STACK_NEED_FETCH = Symbol("stack-need-fetch");
3202
+ var WAIT_FOR_OUTPUT_FN = function (self) { return __awaiter(void 0, void 0, void 0, function () {
3203
+ var _a, awaiter, resolve, getOutput, handleOutput, un, _b, agentName, output, expectAgent;
3204
+ return __generator(this, function (_c) {
3205
+ switch (_c.label) {
3206
+ case 0:
3207
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3208
+ self.params.logger.debug("ClientSwarm swarmName=".concat(self.params.swarmName, " clientId=").concat(self.params.clientId, " waitForOutput"));
3209
+ _a = __read(functoolsKit.createAwaiter(), 2), awaiter = _a[0], resolve = _a[1].resolve;
3210
+ getOutput = functoolsKit.cancelable(function () { return __awaiter(void 0, void 0, void 0, function () {
3211
+ return __generator(this, function (_a) {
3212
+ switch (_a.label) {
3213
+ case 0: return [4 /*yield*/, Promise.race(self._agentList
3214
+ .map(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
3215
+ var _c;
3216
+ var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
3217
+ return __generator(this, function (_e) {
3218
+ switch (_e.label) {
3219
+ case 0:
3220
+ _c = {
3221
+ agentName: agentName
3222
+ };
3223
+ return [4 /*yield*/, agent.waitForOutput()];
3224
+ case 1: return [2 /*return*/, (_c.output = _e.sent(),
3225
+ _c)];
3226
+ }
3227
+ });
3228
+ }); })
3229
+ .concat(self._cancelOutputSubject.toPromise()))];
3230
+ case 1: return [2 /*return*/, _a.sent()];
3231
+ }
3232
+ });
3233
+ }); });
3234
+ handleOutput = function () {
3235
+ getOutput.cancel();
3236
+ getOutput().then(function (value) {
3237
+ if (value === functoolsKit.CANCELED_PROMISE_SYMBOL) {
3238
+ return;
3239
+ }
3240
+ resolve(value);
3241
+ });
3242
+ };
3243
+ un = self._agentChangedSubject.subscribe(handleOutput);
3244
+ handleOutput();
3245
+ return [4 /*yield*/, awaiter];
3246
+ case 1:
3247
+ _b = _c.sent(), agentName = _b.agentName, output = _b.output;
3248
+ un();
3249
+ return [4 /*yield*/, self.getAgentName()];
3250
+ case 2:
3251
+ expectAgent = _c.sent();
3252
+ agentName !== expectAgent &&
3253
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3254
+ self.params.logger.debug("ClientSwarm swarmName=".concat(self.params.swarmName, " clientId=").concat(self.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
3255
+ return [4 /*yield*/, self.params.bus.emit(self.params.clientId, {
3256
+ type: "wait-for-output",
3257
+ source: "swarm-bus",
3258
+ input: {},
3259
+ output: {
3260
+ result: output,
3261
+ },
3262
+ context: {
3263
+ swarmName: self.params.swarmName,
3264
+ },
3265
+ clientId: self.params.clientId,
3266
+ })];
3267
+ case 3:
3268
+ _c.sent();
3269
+ return [2 /*return*/, output];
3270
+ }
3271
+ });
3272
+ }); };
3149
3273
  /**
3150
3274
  * ClientSwarm class implements the ISwarm interface and manages agents within a swarm.
3151
3275
  */
@@ -3162,10 +3286,33 @@ var ClientSwarm = /** @class */ (function () {
3162
3286
  this._navigationStack = STACK_NEED_FETCH;
3163
3287
  this._cancelOutputSubject = new functoolsKit.Subject();
3164
3288
  /**
3165
- * Pop the navigation stack or return default agent
3166
- * @returns {Promise<string>} - The pending agent for navigation
3289
+ * Waits for output from the active agent.
3290
+ * @returns {Promise<string>} - The output from the active agent.
3167
3291
  */
3168
- this.navigationPop = function () { return __awaiter(_this, void 0, void 0, function () {
3292
+ this.waitForOutput = functoolsKit.queued(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
3293
+ switch (_a.label) {
3294
+ case 0: return [4 /*yield*/, WAIT_FOR_OUTPUT_FN(this)];
3295
+ case 1: return [2 /*return*/, _a.sent()];
3296
+ }
3297
+ }); }); });
3298
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3299
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " CTOR"), {
3300
+ params: params,
3301
+ });
3302
+ }
3303
+ Object.defineProperty(ClientSwarm.prototype, "_agentList", {
3304
+ get: function () {
3305
+ return Object.entries(this.params.agentMap);
3306
+ },
3307
+ enumerable: false,
3308
+ configurable: true
3309
+ });
3310
+ /**
3311
+ * Pop the navigation stack or return default agent
3312
+ * @returns {Promise<string>} - The pending agent for navigation
3313
+ */
3314
+ ClientSwarm.prototype.navigationPop = function () {
3315
+ return __awaiter(this, void 0, void 0, function () {
3169
3316
  var _a, prevAgent;
3170
3317
  return __generator(this, function (_b) {
3171
3318
  switch (_b.label) {
@@ -3186,12 +3333,14 @@ var ClientSwarm = /** @class */ (function () {
3186
3333
  return [2 /*return*/, prevAgent ? prevAgent : this.params.defaultAgent];
3187
3334
  }
3188
3335
  });
3189
- }); };
3190
- /**
3191
- * Cancel the await of output by emit of empty string
3192
- * @returns {Promise<string>} - The output from the active agent.
3193
- */
3194
- this.cancelOutput = function () { return __awaiter(_this, void 0, void 0, function () {
3336
+ });
3337
+ };
3338
+ /**
3339
+ * Cancel the await of output by emit of empty string
3340
+ * @returns {Promise<string>} - The output from the active agent.
3341
+ */
3342
+ ClientSwarm.prototype.cancelOutput = function () {
3343
+ return __awaiter(this, void 0, void 0, function () {
3195
3344
  var _a, _b;
3196
3345
  var _c;
3197
3346
  return __generator(this, function (_d) {
@@ -3222,89 +3371,14 @@ var ClientSwarm = /** @class */ (function () {
3222
3371
  return [2 /*return*/];
3223
3372
  }
3224
3373
  });
3225
- }); };
3226
- /**
3227
- * Waits for output from the active agent.
3228
- * @returns {Promise<string>} - The output from the active agent.
3229
- */
3230
- this.waitForOutput = functoolsKit.queued(function () { return __awaiter(_this, void 0, void 0, function () {
3231
- var _a, awaiter, resolve, getOutput, handleOutput, un, _b, agentName, output, expectAgent;
3232
- var _this = this;
3233
- return __generator(this, function (_c) {
3234
- switch (_c.label) {
3235
- case 0:
3236
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3237
- this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForOutput"));
3238
- _a = __read(functoolsKit.createAwaiter(), 2), awaiter = _a[0], resolve = _a[1].resolve;
3239
- getOutput = functoolsKit.cancelable(function () { return __awaiter(_this, void 0, void 0, function () {
3240
- var _this = this;
3241
- return __generator(this, function (_a) {
3242
- switch (_a.label) {
3243
- case 0: return [4 /*yield*/, Promise.race(this._agentList
3244
- .map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
3245
- var _c;
3246
- var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
3247
- return __generator(this, function (_e) {
3248
- switch (_e.label) {
3249
- case 0:
3250
- _c = {
3251
- agentName: agentName
3252
- };
3253
- return [4 /*yield*/, agent.waitForOutput()];
3254
- case 1: return [2 /*return*/, (_c.output = _e.sent(),
3255
- _c)];
3256
- }
3257
- });
3258
- }); })
3259
- .concat(this._cancelOutputSubject.toPromise()))];
3260
- case 1: return [2 /*return*/, _a.sent()];
3261
- }
3262
- });
3263
- }); });
3264
- handleOutput = function () {
3265
- getOutput.cancel();
3266
- getOutput().then(function (value) {
3267
- if (value === functoolsKit.CANCELED_PROMISE_SYMBOL) {
3268
- return;
3269
- }
3270
- resolve(value);
3271
- });
3272
- };
3273
- un = this._agentChangedSubject.subscribe(handleOutput);
3274
- handleOutput();
3275
- return [4 /*yield*/, awaiter];
3276
- case 1:
3277
- _b = _c.sent(), agentName = _b.agentName, output = _b.output;
3278
- un();
3279
- return [4 /*yield*/, this.getAgentName()];
3280
- case 2:
3281
- expectAgent = _c.sent();
3282
- agentName !== expectAgent &&
3283
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3284
- this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
3285
- return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
3286
- type: "wait-for-output",
3287
- source: "swarm-bus",
3288
- input: {},
3289
- output: {
3290
- result: output,
3291
- },
3292
- context: {
3293
- swarmName: this.params.swarmName,
3294
- },
3295
- clientId: this.params.clientId,
3296
- })];
3297
- case 3:
3298
- _c.sent();
3299
- return [2 /*return*/, output];
3300
- }
3301
- });
3302
- }); });
3303
- /**
3304
- * Gets the name of the active agent.
3305
- * @returns {Promise<AgentName>} - The name of the active agent.
3306
- */
3307
- this.getAgentName = function () { return __awaiter(_this, void 0, void 0, function () {
3374
+ });
3375
+ };
3376
+ /**
3377
+ * Gets the name of the active agent.
3378
+ * @returns {Promise<AgentName>} - The name of the active agent.
3379
+ */
3380
+ ClientSwarm.prototype.getAgentName = function () {
3381
+ return __awaiter(this, void 0, void 0, function () {
3308
3382
  var _a;
3309
3383
  return __generator(this, function (_b) {
3310
3384
  switch (_b.label) {
@@ -3334,12 +3408,14 @@ var ClientSwarm = /** @class */ (function () {
3334
3408
  return [2 /*return*/, this._activeAgent];
3335
3409
  }
3336
3410
  });
3337
- }); };
3338
- /**
3339
- * Gets the active agent.
3340
- * @returns {Promise<IAgent>} - The active agent.
3341
- */
3342
- this.getAgent = function () { return __awaiter(_this, void 0, void 0, function () {
3411
+ });
3412
+ };
3413
+ /**
3414
+ * Gets the active agent.
3415
+ * @returns {Promise<IAgent>} - The active agent.
3416
+ */
3417
+ ClientSwarm.prototype.getAgent = function () {
3418
+ return __awaiter(this, void 0, void 0, function () {
3343
3419
  var agent, result;
3344
3420
  return __generator(this, function (_a) {
3345
3421
  switch (_a.label) {
@@ -3367,14 +3443,16 @@ var ClientSwarm = /** @class */ (function () {
3367
3443
  return [2 /*return*/, result];
3368
3444
  }
3369
3445
  });
3370
- }); };
3371
- /**
3372
- * Sets the reference of an agent in the swarm.
3373
- * @param {AgentName} agentName - The name of the agent.
3374
- * @param {IAgent} agent - The agent instance.
3375
- * @throws {Error} - If the agent is not in the swarm.
3376
- */
3377
- this.setAgentRef = function (agentName, agent) { return __awaiter(_this, void 0, void 0, function () {
3446
+ });
3447
+ };
3448
+ /**
3449
+ * Sets the reference of an agent in the swarm.
3450
+ * @param {AgentName} agentName - The name of the agent.
3451
+ * @param {IAgent} agent - The agent instance.
3452
+ * @throws {Error} - If the agent is not in the swarm.
3453
+ */
3454
+ ClientSwarm.prototype.setAgentRef = function (agentName, agent) {
3455
+ return __awaiter(this, void 0, void 0, function () {
3378
3456
  return __generator(this, function (_a) {
3379
3457
  switch (_a.label) {
3380
3458
  case 0:
@@ -3405,12 +3483,14 @@ var ClientSwarm = /** @class */ (function () {
3405
3483
  return [2 /*return*/];
3406
3484
  }
3407
3485
  });
3408
- }); };
3409
- /**
3410
- * Sets the active agent by name.
3411
- * @param {AgentName} agentName - The name of the agent to set as active.
3412
- */
3413
- this.setAgentName = function (agentName) { return __awaiter(_this, void 0, void 0, function () {
3486
+ });
3487
+ };
3488
+ /**
3489
+ * Sets the active agent by name.
3490
+ * @param {AgentName} agentName - The name of the agent to set as active.
3491
+ */
3492
+ ClientSwarm.prototype.setAgentName = function (agentName) {
3493
+ return __awaiter(this, void 0, void 0, function () {
3414
3494
  var _a;
3415
3495
  var _b;
3416
3496
  return __generator(this, function (_c) {
@@ -3453,19 +3533,8 @@ var ClientSwarm = /** @class */ (function () {
3453
3533
  return [2 /*return*/];
3454
3534
  }
3455
3535
  });
3456
- }); };
3457
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3458
- this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " CTOR"), {
3459
- params: params,
3460
- });
3461
- }
3462
- Object.defineProperty(ClientSwarm.prototype, "_agentList", {
3463
- get: function () {
3464
- return Object.entries(this.params.agentMap);
3465
- },
3466
- enumerable: false,
3467
- configurable: true
3468
- });
3536
+ });
3537
+ };
3469
3538
  return ClientSwarm;
3470
3539
  }());
3471
3540
 
@@ -3768,15 +3837,21 @@ var ClientSession = /** @class */ (function () {
3768
3837
  * @param {ISessionParams} params - The session parameters.
3769
3838
  */
3770
3839
  function ClientSession(params) {
3771
- var _this = this;
3772
3840
  this.params = params;
3773
3841
  this._emitSubject = new functoolsKit.Subject();
3774
- /**
3775
- * Emits a message.
3776
- * @param {string} message - The message to emit.
3777
- * @returns {Promise<void>}
3778
- */
3779
- this.emit = function (message) { return __awaiter(_this, void 0, void 0, function () {
3842
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3843
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " CTOR"), {
3844
+ params: params,
3845
+ });
3846
+ this.params.onInit && this.params.onInit(params.clientId, params.swarmName);
3847
+ }
3848
+ /**
3849
+ * Emits a message.
3850
+ * @param {string} message - The message to emit.
3851
+ * @returns {Promise<void>}
3852
+ */
3853
+ ClientSession.prototype.emit = function (message) {
3854
+ return __awaiter(this, void 0, void 0, function () {
3780
3855
  return __generator(this, function (_a) {
3781
3856
  switch (_a.label) {
3782
3857
  case 0:
@@ -3806,14 +3881,16 @@ var ClientSession = /** @class */ (function () {
3806
3881
  return [2 /*return*/];
3807
3882
  }
3808
3883
  });
3809
- }); };
3810
- /**
3811
- * Executes a message and optionally emits the output.
3812
- * @param {string} message - The message to execute.
3813
- * @param {boolean} [noEmit=false] - Whether to emit the output or not.
3814
- * @returns {Promise<string>} - The output of the execution.
3815
- */
3816
- this.execute = function (message, mode) { return __awaiter(_this, void 0, void 0, function () {
3884
+ });
3885
+ };
3886
+ /**
3887
+ * Executes a message and optionally emits the output.
3888
+ * @param {string} message - The message to execute.
3889
+ * @param {boolean} [noEmit=false] - Whether to emit the output or not.
3890
+ * @returns {Promise<string>} - The output of the execution.
3891
+ */
3892
+ ClientSession.prototype.execute = function (message, mode) {
3893
+ return __awaiter(this, void 0, void 0, function () {
3817
3894
  var agent, outputAwaiter, output;
3818
3895
  return __generator(this, function (_a) {
3819
3896
  switch (_a.label) {
@@ -3853,13 +3930,15 @@ var ClientSession = /** @class */ (function () {
3853
3930
  return [2 /*return*/, output];
3854
3931
  }
3855
3932
  });
3856
- }); };
3857
- /**
3858
- * Run the completion stateless
3859
- * @param {string} message - The message to run.
3860
- * @returns {Promise<string>} - The output of the execution.
3861
- */
3862
- this.run = function (message) { return __awaiter(_this, void 0, void 0, function () {
3933
+ });
3934
+ };
3935
+ /**
3936
+ * Run the completion stateless
3937
+ * @param {string} message - The message to run.
3938
+ * @returns {Promise<string>} - The output of the execution.
3939
+ */
3940
+ ClientSession.prototype.run = function (message) {
3941
+ return __awaiter(this, void 0, void 0, function () {
3863
3942
  var agent, output;
3864
3943
  return __generator(this, function (_a) {
3865
3944
  switch (_a.label) {
@@ -3895,14 +3974,16 @@ var ClientSession = /** @class */ (function () {
3895
3974
  return [2 /*return*/, output];
3896
3975
  }
3897
3976
  });
3898
- }); };
3899
- /**
3900
- * Commits tool output.
3901
- * @param {string} toolId - The `tool_call_id` for openai history
3902
- * @param {string} content - The content to commit.
3903
- * @returns {Promise<void>}
3904
- */
3905
- this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
3977
+ });
3978
+ };
3979
+ /**
3980
+ * Commits tool output.
3981
+ * @param {string} toolId - The `tool_call_id` for openai history
3982
+ * @param {string} content - The content to commit.
3983
+ * @returns {Promise<void>}
3984
+ */
3985
+ ClientSession.prototype.commitToolOutput = function (toolId, content) {
3986
+ return __awaiter(this, void 0, void 0, function () {
3906
3987
  var agent, result;
3907
3988
  return __generator(this, function (_a) {
3908
3989
  switch (_a.label) {
@@ -3936,13 +4017,15 @@ var ClientSession = /** @class */ (function () {
3936
4017
  return [2 /*return*/, result];
3937
4018
  }
3938
4019
  });
3939
- }); };
3940
- /**
3941
- * Commits user message without answer.
3942
- * @param {string} message - The message to commit.
3943
- * @returns {Promise<void>}
3944
- */
3945
- this.commitUserMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
4020
+ });
4021
+ };
4022
+ /**
4023
+ * Commits user message without answer.
4024
+ * @param {string} message - The message to commit.
4025
+ * @returns {Promise<void>}
4026
+ */
4027
+ ClientSession.prototype.commitUserMessage = function (message) {
4028
+ return __awaiter(this, void 0, void 0, function () {
3946
4029
  var agent, result;
3947
4030
  return __generator(this, function (_a) {
3948
4031
  switch (_a.label) {
@@ -3974,12 +4057,14 @@ var ClientSession = /** @class */ (function () {
3974
4057
  return [2 /*return*/, result];
3975
4058
  }
3976
4059
  });
3977
- }); };
3978
- /**
3979
- * Commits flush of agent history
3980
- * @returns {Promise<void>}
3981
- */
3982
- this.commitFlush = function () { return __awaiter(_this, void 0, void 0, function () {
4060
+ });
4061
+ };
4062
+ /**
4063
+ * Commits flush of agent history
4064
+ * @returns {Promise<void>}
4065
+ */
4066
+ ClientSession.prototype.commitFlush = function () {
4067
+ return __awaiter(this, void 0, void 0, function () {
3983
4068
  var agent, result;
3984
4069
  return __generator(this, function (_a) {
3985
4070
  switch (_a.label) {
@@ -4007,12 +4092,14 @@ var ClientSession = /** @class */ (function () {
4007
4092
  return [2 /*return*/, result];
4008
4093
  }
4009
4094
  });
4010
- }); };
4011
- /**
4012
- * Commits stop of the nexttool execution
4013
- * @returns {Promise<void>}
4014
- */
4015
- this.commitStopTools = function () { return __awaiter(_this, void 0, void 0, function () {
4095
+ });
4096
+ };
4097
+ /**
4098
+ * Commits stop of the nexttool execution
4099
+ * @returns {Promise<void>}
4100
+ */
4101
+ ClientSession.prototype.commitStopTools = function () {
4102
+ return __awaiter(this, void 0, void 0, function () {
4016
4103
  var agent, result;
4017
4104
  return __generator(this, function (_a) {
4018
4105
  switch (_a.label) {
@@ -4040,13 +4127,15 @@ var ClientSession = /** @class */ (function () {
4040
4127
  return [2 /*return*/, result];
4041
4128
  }
4042
4129
  });
4043
- }); };
4044
- /**
4045
- * Commits a system message.
4046
- * @param {string} message - The system message to commit.
4047
- * @returns {Promise<void>}
4048
- */
4049
- this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
4130
+ });
4131
+ };
4132
+ /**
4133
+ * Commits a system message.
4134
+ * @param {string} message - The system message to commit.
4135
+ * @returns {Promise<void>}
4136
+ */
4137
+ ClientSession.prototype.commitSystemMessage = function (message) {
4138
+ return __awaiter(this, void 0, void 0, function () {
4050
4139
  var agent, result;
4051
4140
  return __generator(this, function (_a) {
4052
4141
  switch (_a.label) {
@@ -4078,13 +4167,15 @@ var ClientSession = /** @class */ (function () {
4078
4167
  return [2 /*return*/, result];
4079
4168
  }
4080
4169
  });
4081
- }); };
4082
- /**
4083
- * Commits an assistant message.
4084
- * @param {string} message - The assistant message to commit.
4085
- * @returns {Promise<void>}
4086
- */
4087
- this.commitAssistantMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
4170
+ });
4171
+ };
4172
+ /**
4173
+ * Commits an assistant message.
4174
+ * @param {string} message - The assistant message to commit.
4175
+ * @returns {Promise<void>}
4176
+ */
4177
+ ClientSession.prototype.commitAssistantMessage = function (message) {
4178
+ return __awaiter(this, void 0, void 0, function () {
4088
4179
  var agent, result;
4089
4180
  return __generator(this, function (_a) {
4090
4181
  switch (_a.label) {
@@ -4116,79 +4207,82 @@ var ClientSession = /** @class */ (function () {
4116
4207
  return [2 /*return*/, result];
4117
4208
  }
4118
4209
  });
4119
- }); };
4120
- /**
4121
- * Connects the session to a connector function.
4122
- * @param {SendMessageFn} connector - The connector function.
4123
- * @returns {ReceiveMessageFn<string>} - The function to receive messages.
4124
- */
4125
- this.connect = function (connector) {
4126
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4127
- _this.params.logger.debug("ClientSession clientId=".concat(_this.params.clientId, " connect"));
4128
- _this.params.onConnect &&
4129
- _this.params.onConnect(_this.params.clientId, _this.params.swarmName);
4130
- _this._emitSubject.subscribe(function (data) { return __awaiter(_this, void 0, void 0, function () {
4131
- var _a;
4132
- var _b;
4133
- return __generator(this, function (_c) {
4134
- switch (_c.label) {
4135
- case 0:
4136
- _a = connector;
4137
- _b = {
4138
- data: data
4139
- };
4140
- return [4 /*yield*/, this.params.swarm.getAgentName()];
4141
- case 1: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
4142
- _b.clientId = this.params.clientId,
4143
- _b)])];
4144
- case 2: return [2 /*return*/, _c.sent()];
4145
- }
4146
- });
4147
- }); });
4148
- _this.params.bus.emit(_this.params.clientId, {
4149
- type: "connect",
4150
- source: "session-bus",
4151
- input: {},
4152
- output: {},
4153
- context: {
4154
- swarmName: _this.params.swarmName,
4155
- },
4156
- clientId: _this.params.clientId,
4210
+ });
4211
+ };
4212
+ /**
4213
+ * Connects the session to a connector function.
4214
+ * @param {SendMessageFn} connector - The connector function.
4215
+ * @returns {ReceiveMessageFn<string>} - The function to receive messages.
4216
+ */
4217
+ ClientSession.prototype.connect = function (connector) {
4218
+ var _this = this;
4219
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4220
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " connect"));
4221
+ this.params.onConnect &&
4222
+ this.params.onConnect(this.params.clientId, this.params.swarmName);
4223
+ this._emitSubject.subscribe(function (data) { return __awaiter(_this, void 0, void 0, function () {
4224
+ var _a;
4225
+ var _b;
4226
+ return __generator(this, function (_c) {
4227
+ switch (_c.label) {
4228
+ case 0:
4229
+ _a = connector;
4230
+ _b = {
4231
+ data: data
4232
+ };
4233
+ return [4 /*yield*/, this.params.swarm.getAgentName()];
4234
+ case 1: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
4235
+ _b.clientId = this.params.clientId,
4236
+ _b)])];
4237
+ case 2: return [2 /*return*/, _c.sent()];
4238
+ }
4157
4239
  });
4158
- return function (incoming) { return __awaiter(_this, void 0, void 0, function () {
4159
- var data, _a;
4160
- var _b;
4161
- return __generator(this, function (_c) {
4162
- switch (_c.label) {
4163
- case 0:
4164
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4165
- this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " connect call"));
4166
- return [4 /*yield*/, this.execute(incoming.data, "user")];
4167
- case 1:
4168
- data = _c.sent();
4169
- if (!data) {
4170
- return [2 /*return*/];
4171
- }
4172
- _a = connector;
4173
- _b = {
4174
- data: data
4175
- };
4176
- return [4 /*yield*/, this.params.swarm.getAgentName()];
4177
- case 2: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
4178
- _b.clientId = incoming.clientId,
4179
- _b)])];
4180
- case 3:
4181
- _c.sent();
4182
- return [2 /*return*/, data];
4183
- }
4184
- });
4185
- }); };
4186
- };
4187
- /**
4188
- * Should call on session dispose
4189
- * @returns {Promise<void>}
4190
- */
4191
- this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
4240
+ }); });
4241
+ this.params.bus.emit(this.params.clientId, {
4242
+ type: "connect",
4243
+ source: "session-bus",
4244
+ input: {},
4245
+ output: {},
4246
+ context: {
4247
+ swarmName: this.params.swarmName,
4248
+ },
4249
+ clientId: this.params.clientId,
4250
+ });
4251
+ return function (incoming) { return __awaiter(_this, void 0, void 0, function () {
4252
+ var data, _a;
4253
+ var _b;
4254
+ return __generator(this, function (_c) {
4255
+ switch (_c.label) {
4256
+ case 0:
4257
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4258
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " connect call"));
4259
+ return [4 /*yield*/, this.execute(incoming.data, "user")];
4260
+ case 1:
4261
+ data = _c.sent();
4262
+ if (!data) {
4263
+ return [2 /*return*/];
4264
+ }
4265
+ _a = connector;
4266
+ _b = {
4267
+ data: data
4268
+ };
4269
+ return [4 /*yield*/, this.params.swarm.getAgentName()];
4270
+ case 2: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
4271
+ _b.clientId = incoming.clientId,
4272
+ _b)])];
4273
+ case 3:
4274
+ _c.sent();
4275
+ return [2 /*return*/, data];
4276
+ }
4277
+ });
4278
+ }); };
4279
+ };
4280
+ /**
4281
+ * Should call on session dispose
4282
+ * @returns {Promise<void>}
4283
+ */
4284
+ ClientSession.prototype.dispose = function () {
4285
+ return __awaiter(this, void 0, void 0, function () {
4192
4286
  return __generator(this, function (_a) {
4193
4287
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4194
4288
  this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " dispose"));
@@ -4196,13 +4290,8 @@ var ClientSession = /** @class */ (function () {
4196
4290
  this.params.onDispose(this.params.clientId, this.params.swarmName);
4197
4291
  return [2 /*return*/];
4198
4292
  });
4199
- }); };
4200
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4201
- this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " CTOR"), {
4202
- params: params,
4203
- });
4204
- this.params.onInit && this.params.onInit(params.clientId, params.swarmName);
4205
- }
4293
+ });
4294
+ };
4206
4295
  return ClientSession;
4207
4296
  }());
4208
4297
 
@@ -6585,6 +6674,30 @@ var StorageSchemaService = /** @class */ (function () {
6585
6674
  return StorageSchemaService;
6586
6675
  }());
6587
6676
 
6677
+ var WAIT_FOR_INIT_FN$1 = function (self) { return __awaiter(void 0, void 0, void 0, function () {
6678
+ var data;
6679
+ return __generator(this, function (_a) {
6680
+ switch (_a.label) {
6681
+ case 0:
6682
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
6683
+ self.params.logger.debug("ClientStorage storageName=".concat(self.params.storageName, " clientId=").concat(self.params.clientId, " shared=").concat(self.params.shared, " waitForInit"));
6684
+ if (!self.params.getData) {
6685
+ return [2 /*return*/];
6686
+ }
6687
+ return [4 /*yield*/, self.params.getData(self.params.clientId, self.params.storageName)];
6688
+ case 1:
6689
+ data = _a.sent();
6690
+ return [4 /*yield*/, Promise.all(data.map(functoolsKit.execpool(self._createEmbedding, {
6691
+ delay: 10,
6692
+ maxExec: GLOBAL_CONFIG.CC_STORAGE_SEARCH_POOL,
6693
+ })))];
6694
+ case 2:
6695
+ _a.sent();
6696
+ self._itemMap = new Map(data.map(function (item) { return [item.id, item]; }));
6697
+ return [2 /*return*/];
6698
+ }
6699
+ });
6700
+ }); };
6588
6701
  /**
6589
6702
  * ClientStorage class to manage storage operations.
6590
6703
  * @template T - The type of storage data.
@@ -6633,121 +6746,108 @@ var ClientStorage = /** @class */ (function () {
6633
6746
  * Waits for the initialization of the storage.
6634
6747
  * @returns {Promise<void>}
6635
6748
  */
6636
- this.waitForInit = functoolsKit.singleshot(function () { return __awaiter(_this, void 0, void 0, function () {
6637
- var data;
6638
- return __generator(this, function (_a) {
6639
- switch (_a.label) {
6749
+ this.waitForInit = functoolsKit.singleshot(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
6750
+ switch (_a.label) {
6751
+ case 0: return [4 /*yield*/, WAIT_FOR_INIT_FN$1(this)];
6752
+ case 1: return [2 /*return*/, _a.sent()];
6753
+ }
6754
+ }); }); });
6755
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
6756
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
6757
+ params: params,
6758
+ });
6759
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
6760
+ this.params.callbacks.onInit(this.params.clientId, this.params.storageName);
6761
+ }
6762
+ }
6763
+ /**
6764
+ * Takes a specified number of items based on the search criteria.
6765
+ * @param {string} search - The search string.
6766
+ * @param {number} total - The total number of items to take.
6767
+ * @param {number} [score=GLOBAL_CONFIG.CC_STORAGE_SEARCH_SIMILARITY] - The similarity score.
6768
+ * @returns {Promise<T[]>} - The list of items.
6769
+ */
6770
+ ClientStorage.prototype.take = function (search_1, total_1) {
6771
+ return __awaiter(this, arguments, void 0, function (search, total, score) {
6772
+ var indexed, searchEmbeddings;
6773
+ var _this = this;
6774
+ var _a, _b;
6775
+ if (score === void 0) { score = GLOBAL_CONFIG.CC_STORAGE_SEARCH_SIMILARITY; }
6776
+ return __generator(this, function (_c) {
6777
+ switch (_c.label) {
6640
6778
  case 0:
6641
6779
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
6642
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " waitForInit"));
6643
- if (!this.params.getData) {
6644
- return [2 /*return*/];
6645
- }
6646
- return [4 /*yield*/, this.params.getData(this.params.clientId, this.params.storageName)];
6780
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " take"), {
6781
+ search: search,
6782
+ total: total,
6783
+ });
6784
+ indexed = new functoolsKit.SortedArray();
6785
+ return [4 /*yield*/, this.params.createEmbedding(search)];
6647
6786
  case 1:
6648
- data = _a.sent();
6649
- return [4 /*yield*/, Promise.all(data.map(functoolsKit.execpool(this._createEmbedding, {
6787
+ searchEmbeddings = _c.sent();
6788
+ if (this.params.onCreate) {
6789
+ this.params.onCreate(search, searchEmbeddings, this.params.clientId, this.params.embedding);
6790
+ }
6791
+ return [4 /*yield*/, Promise.all(Array.from(this._itemMap.values()).map(functoolsKit.execpool(function (item) { return __awaiter(_this, void 0, void 0, function () {
6792
+ var _a, targetEmbeddings, index, score;
6793
+ return __generator(this, function (_b) {
6794
+ switch (_b.label) {
6795
+ case 0: return [4 /*yield*/, this._createEmbedding(item)];
6796
+ case 1:
6797
+ _a = __read.apply(void 0, [_b.sent(), 2]), targetEmbeddings = _a[0], index = _a[1];
6798
+ return [4 /*yield*/, this.params.calculateSimilarity(searchEmbeddings, targetEmbeddings)];
6799
+ case 2:
6800
+ score = _b.sent();
6801
+ if (this.params.onCompare) {
6802
+ this.params.onCompare(search, index, score, this.params.clientId, this.params.embedding);
6803
+ }
6804
+ indexed.push(item, score);
6805
+ return [2 /*return*/];
6806
+ }
6807
+ });
6808
+ }); }, {
6650
6809
  delay: 10,
6651
6810
  maxExec: GLOBAL_CONFIG.CC_STORAGE_SEARCH_POOL,
6652
6811
  })))];
6653
6812
  case 2:
6654
- _a.sent();
6655
- this._itemMap = new Map(data.map(function (item) { return [item.id, item]; }));
6656
- return [2 /*return*/];
6657
- }
6658
- });
6659
- }); });
6660
- /**
6661
- * Takes a specified number of items based on the search criteria.
6662
- * @param {string} search - The search string.
6663
- * @param {number} total - The total number of items to take.
6664
- * @param {number} [score=GLOBAL_CONFIG.CC_STORAGE_SEARCH_SIMILARITY] - The similarity score.
6665
- * @returns {Promise<T[]>} - The list of items.
6666
- */
6667
- this.take = function (search_1, total_1) {
6668
- var args_1 = [];
6669
- for (var _i = 2; _i < arguments.length; _i++) {
6670
- args_1[_i - 2] = arguments[_i];
6671
- }
6672
- return __awaiter(_this, __spreadArray([search_1, total_1], __read(args_1), false), void 0, function (search, total, score) {
6673
- var indexed, searchEmbeddings;
6674
- var _this = this;
6675
- var _a, _b;
6676
- if (score === void 0) { score = GLOBAL_CONFIG.CC_STORAGE_SEARCH_SIMILARITY; }
6677
- return __generator(this, function (_c) {
6678
- switch (_c.label) {
6679
- case 0:
6680
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
6681
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " take"), {
6813
+ _c.sent();
6814
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
6815
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " take indexed"), {
6816
+ indexed: indexed.getEntries(),
6817
+ });
6818
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onSearch) {
6819
+ (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onSearch(search, indexed, this.params.clientId, this.params.storageName);
6820
+ }
6821
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
6822
+ type: "take",
6823
+ source: "storage-bus",
6824
+ input: {
6682
6825
  search: search,
6683
6826
  total: total,
6684
- });
6685
- indexed = new functoolsKit.SortedArray();
6686
- return [4 /*yield*/, this.params.createEmbedding(search)];
6687
- case 1:
6688
- searchEmbeddings = _c.sent();
6689
- if (this.params.onCreate) {
6690
- this.params.onCreate(search, searchEmbeddings, this.params.clientId, this.params.embedding);
6691
- }
6692
- return [4 /*yield*/, Promise.all(Array.from(this._itemMap.values()).map(functoolsKit.execpool(function (item) { return __awaiter(_this, void 0, void 0, function () {
6693
- var _a, targetEmbeddings, index, score;
6694
- return __generator(this, function (_b) {
6695
- switch (_b.label) {
6696
- case 0: return [4 /*yield*/, this._createEmbedding(item)];
6697
- case 1:
6698
- _a = __read.apply(void 0, [_b.sent(), 2]), targetEmbeddings = _a[0], index = _a[1];
6699
- return [4 /*yield*/, this.params.calculateSimilarity(searchEmbeddings, targetEmbeddings)];
6700
- case 2:
6701
- score = _b.sent();
6702
- if (this.params.onCompare) {
6703
- this.params.onCompare(search, index, score, this.params.clientId, this.params.embedding);
6704
- }
6705
- indexed.push(item, score);
6706
- return [2 /*return*/];
6707
- }
6708
- });
6709
- }); }, {
6710
- delay: 10,
6711
- maxExec: GLOBAL_CONFIG.CC_STORAGE_SEARCH_POOL,
6712
- })))];
6713
- case 2:
6714
- _c.sent();
6715
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
6716
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " take indexed"), {
6717
- indexed: indexed.getEntries(),
6718
- });
6719
- if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onSearch) {
6720
- (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onSearch(search, indexed, this.params.clientId, this.params.storageName);
6721
- }
6722
- return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
6723
- type: "take",
6724
- source: "storage-bus",
6725
- input: {
6726
- search: search,
6727
- total: total,
6728
- score: score,
6729
- },
6730
- output: {
6731
- indexed: indexed,
6732
- },
6733
- context: {
6734
- storageName: this.params.storageName,
6735
- },
6736
- clientId: this.params.clientId,
6737
- })];
6738
- case 3:
6739
- _c.sent();
6740
- return [2 /*return*/, indexed.take(total, score)];
6741
- }
6742
- });
6827
+ score: score,
6828
+ },
6829
+ output: {
6830
+ indexed: indexed,
6831
+ },
6832
+ context: {
6833
+ storageName: this.params.storageName,
6834
+ },
6835
+ clientId: this.params.clientId,
6836
+ })];
6837
+ case 3:
6838
+ _c.sent();
6839
+ return [2 /*return*/, indexed.take(total, score)];
6840
+ }
6743
6841
  });
6744
- };
6745
- /**
6746
- * Upserts an item into the storage.
6747
- * @param {T} item - The item to upsert.
6748
- * @returns {Promise<void>}
6749
- */
6750
- this.upsert = function (item) { return __awaiter(_this, void 0, void 0, function () {
6842
+ });
6843
+ };
6844
+ /**
6845
+ * Upserts an item into the storage.
6846
+ * @param {T} item - The item to upsert.
6847
+ * @returns {Promise<void>}
6848
+ */
6849
+ ClientStorage.prototype.upsert = function (item) {
6850
+ return __awaiter(this, void 0, void 0, function () {
6751
6851
  var _a, _b;
6752
6852
  return __generator(this, function (_c) {
6753
6853
  switch (_c.label) {
@@ -6781,13 +6881,15 @@ var ClientStorage = /** @class */ (function () {
6781
6881
  return [2 /*return*/];
6782
6882
  }
6783
6883
  });
6784
- }); };
6785
- /**
6786
- * Removes an item from the storage.
6787
- * @param {IStorageData["id"]} itemId - The ID of the item to remove.
6788
- * @returns {Promise<void>}
6789
- */
6790
- this.remove = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
6884
+ });
6885
+ };
6886
+ /**
6887
+ * Removes an item from the storage.
6888
+ * @param {IStorageData["id"]} itemId - The ID of the item to remove.
6889
+ * @returns {Promise<void>}
6890
+ */
6891
+ ClientStorage.prototype.remove = function (itemId) {
6892
+ return __awaiter(this, void 0, void 0, function () {
6791
6893
  var _a, _b;
6792
6894
  return __generator(this, function (_c) {
6793
6895
  switch (_c.label) {
@@ -6818,12 +6920,14 @@ var ClientStorage = /** @class */ (function () {
6818
6920
  return [2 /*return*/];
6819
6921
  }
6820
6922
  });
6821
- }); };
6822
- /**
6823
- * Clears all items from the storage.
6824
- * @returns {Promise<void>}
6825
- */
6826
- this.clear = function () { return __awaiter(_this, void 0, void 0, function () {
6923
+ });
6924
+ };
6925
+ /**
6926
+ * Clears all items from the storage.
6927
+ * @returns {Promise<void>}
6928
+ */
6929
+ ClientStorage.prototype.clear = function () {
6930
+ return __awaiter(this, void 0, void 0, function () {
6827
6931
  return __generator(this, function (_a) {
6828
6932
  switch (_a.label) {
6829
6933
  case 0:
@@ -6846,13 +6950,15 @@ var ClientStorage = /** @class */ (function () {
6846
6950
  return [2 /*return*/];
6847
6951
  }
6848
6952
  });
6849
- }); };
6850
- /**
6851
- * Gets an item by its ID.
6852
- * @param {IStorageData["id"]} itemId - The ID of the item to get.
6853
- * @returns {Promise<T | null>} - The item or null if not found.
6854
- */
6855
- this.get = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
6953
+ });
6954
+ };
6955
+ /**
6956
+ * Gets an item by its ID.
6957
+ * @param {IStorageData["id"]} itemId - The ID of the item to get.
6958
+ * @returns {Promise<T | null>} - The item or null if not found.
6959
+ */
6960
+ ClientStorage.prototype.get = function (itemId) {
6961
+ return __awaiter(this, void 0, void 0, function () {
6856
6962
  var result;
6857
6963
  var _a;
6858
6964
  return __generator(this, function (_b) {
@@ -6882,13 +6988,15 @@ var ClientStorage = /** @class */ (function () {
6882
6988
  return [2 /*return*/, result];
6883
6989
  }
6884
6990
  });
6885
- }); };
6886
- /**
6887
- * Lists all items in the storage, optionally filtered by a predicate.
6888
- * @param {(item: T) => boolean} [filter] - The filter predicate.
6889
- * @returns {Promise<T[]>} - The list of items.
6890
- */
6891
- this.list = function (filter) { return __awaiter(_this, void 0, void 0, function () {
6991
+ });
6992
+ };
6993
+ /**
6994
+ * Lists all items in the storage, optionally filtered by a predicate.
6995
+ * @param {(item: T) => boolean} [filter] - The filter predicate.
6996
+ * @returns {Promise<T[]>} - The list of items.
6997
+ */
6998
+ ClientStorage.prototype.list = function (filter) {
6999
+ return __awaiter(this, void 0, void 0, function () {
6892
7000
  var result, _a, _b, item;
6893
7001
  var e_1, _c;
6894
7002
  return __generator(this, function (_d) {
@@ -6932,12 +7040,14 @@ var ClientStorage = /** @class */ (function () {
6932
7040
  return [2 /*return*/, result];
6933
7041
  }
6934
7042
  });
6935
- }); };
6936
- /**
6937
- * Disposes of the state.
6938
- * @returns {Promise<void>}
6939
- */
6940
- this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
7043
+ });
7044
+ };
7045
+ /**
7046
+ * Disposes of the state.
7047
+ * @returns {Promise<void>}
7048
+ */
7049
+ ClientStorage.prototype.dispose = function () {
7050
+ return __awaiter(this, void 0, void 0, function () {
6941
7051
  var _a;
6942
7052
  return __generator(this, function (_b) {
6943
7053
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
@@ -6947,15 +7057,8 @@ var ClientStorage = /** @class */ (function () {
6947
7057
  }
6948
7058
  return [2 /*return*/];
6949
7059
  });
6950
- }); };
6951
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
6952
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
6953
- params: params,
6954
- });
6955
- if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
6956
- this.params.callbacks.onInit(this.params.clientId, this.params.storageName);
6957
- }
6958
- }
7060
+ });
7061
+ };
6959
7062
  return ClientStorage;
6960
7063
  }());
6961
7064
 
@@ -7581,6 +7684,44 @@ var StateSchemaService = /** @class */ (function () {
7581
7684
  return StateSchemaService;
7582
7685
  }());
7583
7686
 
7687
+ var DISPATCH_FN = function (action, self, payload) { return __awaiter(void 0, void 0, void 0, function () {
7688
+ var _a;
7689
+ return __generator(this, function (_b) {
7690
+ switch (_b.label) {
7691
+ case 0:
7692
+ if (action === "read") {
7693
+ return [2 /*return*/, self._state];
7694
+ }
7695
+ if (!(action === "write")) return [3 /*break*/, 2];
7696
+ console.assert(payload, "agent-swarm ClientState write action undefined payload");
7697
+ _a = self;
7698
+ return [4 /*yield*/, payload(self._state)];
7699
+ case 1: return [2 /*return*/, (_a._state = _b.sent())];
7700
+ case 2: throw new Error("agent-swarm ClientState unknown action");
7701
+ }
7702
+ });
7703
+ }); };
7704
+ var WAIT_FOR_INIT_FN = function (self) { return __awaiter(void 0, void 0, void 0, function () {
7705
+ var _a;
7706
+ var _b;
7707
+ return __generator(this, function (_c) {
7708
+ switch (_c.label) {
7709
+ case 0:
7710
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
7711
+ self.params.logger.debug("ClientState stateName=".concat(self.params.stateName, " clientId=").concat(self.params.clientId, " shared=").concat(self.params.shared, " waitForInit"));
7712
+ _a = self;
7713
+ return [4 /*yield*/, self.params.getState(self.params.clientId, self.params.stateName)];
7714
+ case 1:
7715
+ _a._state = _c.sent();
7716
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
7717
+ self.params.logger.debug("ClientState stateName=".concat(self.params.stateName, " clientId=").concat(self.params.clientId, " shared=").concat(self.params.shared, " waitForInit output"), { initialState: self._state });
7718
+ if ((_b = self.params.callbacks) === null || _b === void 0 ? void 0 : _b.onLoad) {
7719
+ self.params.callbacks.onLoad(self._state, self.params.clientId, self.params.stateName);
7720
+ }
7721
+ return [2 /*return*/];
7722
+ }
7723
+ });
7724
+ }); };
7584
7725
  /**
7585
7726
  * Class representing the client state.
7586
7727
  * @template State - The type of the state data.
@@ -7596,54 +7737,37 @@ var ClientState = /** @class */ (function () {
7596
7737
  var _a;
7597
7738
  this.params = params;
7598
7739
  this._state = null;
7599
- this.dispatch = functoolsKit.queued(function (action, payload) { return __awaiter(_this, void 0, void 0, function () {
7600
- var _a;
7601
- return __generator(this, function (_b) {
7602
- switch (_b.label) {
7603
- case 0:
7604
- if (action === "read") {
7605
- return [2 /*return*/, this._state];
7606
- }
7607
- if (!(action === "write")) return [3 /*break*/, 2];
7608
- console.assert(payload, "agent-swarm ClientState write action undefined payload");
7609
- _a = this;
7610
- return [4 /*yield*/, payload(this._state)];
7611
- case 1: return [2 /*return*/, (_a._state = _b.sent())];
7612
- case 2: throw new Error("agent-swarm ClientState unknown action");
7613
- }
7614
- });
7615
- }); });
7740
+ this.dispatch = functoolsKit.queued(function (action, payload) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
7741
+ switch (_a.label) {
7742
+ case 0: return [4 /*yield*/, DISPATCH_FN(action, this, payload)];
7743
+ case 1: return [2 /*return*/, _a.sent()];
7744
+ }
7745
+ }); }); });
7616
7746
  /**
7617
7747
  * Waits for the state to initialize.
7618
7748
  * @returns {Promise<void>}
7619
7749
  */
7620
- this.waitForInit = functoolsKit.singleshot(function () { return __awaiter(_this, void 0, void 0, function () {
7621
- var _a;
7622
- var _b;
7623
- return __generator(this, function (_c) {
7624
- switch (_c.label) {
7625
- case 0:
7626
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
7627
- this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " waitForInit"));
7628
- _a = this;
7629
- return [4 /*yield*/, this.params.getState(this.params.clientId, this.params.stateName)];
7630
- case 1:
7631
- _a._state = _c.sent();
7632
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
7633
- this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " waitForInit output"), { initialState: this._state });
7634
- if ((_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onLoad) {
7635
- this.params.callbacks.onLoad(this._state, this.params.clientId, this.params.stateName);
7636
- }
7637
- return [2 /*return*/];
7638
- }
7750
+ this.waitForInit = functoolsKit.singleshot(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
7751
+ switch (_a.label) {
7752
+ case 0: return [4 /*yield*/, WAIT_FOR_INIT_FN(this)];
7753
+ case 1: return [2 /*return*/, _a.sent()];
7754
+ }
7755
+ }); }); });
7756
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
7757
+ this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
7758
+ params: params,
7639
7759
  });
7640
- }); });
7641
- /**
7642
- * Sets the state using the provided dispatch function.
7643
- * @param {DispatchFn<State>} dispatchFn - The dispatch function.
7644
- * @returns {Promise<State>}
7645
- */
7646
- this.setState = function (dispatchFn) { return __awaiter(_this, void 0, void 0, function () {
7760
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
7761
+ this.params.callbacks.onInit(this.params.clientId, this.params.stateName);
7762
+ }
7763
+ }
7764
+ /**
7765
+ * Sets the state using the provided dispatch function.
7766
+ * @param {DispatchFn<State>} dispatchFn - The dispatch function.
7767
+ * @returns {Promise<State>}
7768
+ */
7769
+ ClientState.prototype.setState = function (dispatchFn) {
7770
+ return __awaiter(this, void 0, void 0, function () {
7647
7771
  var _this = this;
7648
7772
  var _a;
7649
7773
  return __generator(this, function (_b) {
@@ -7715,12 +7839,14 @@ var ClientState = /** @class */ (function () {
7715
7839
  return [2 /*return*/, this._state];
7716
7840
  }
7717
7841
  });
7718
- }); };
7719
- /**
7720
- * Sets the to initial value
7721
- * @returns {Promise<State>}
7722
- */
7723
- this.clearState = function () { return __awaiter(_this, void 0, void 0, function () {
7842
+ });
7843
+ };
7844
+ /**
7845
+ * Sets the to initial value
7846
+ * @returns {Promise<State>}
7847
+ */
7848
+ ClientState.prototype.clearState = function () {
7849
+ return __awaiter(this, void 0, void 0, function () {
7724
7850
  var _this = this;
7725
7851
  var _a;
7726
7852
  return __generator(this, function (_b) {
@@ -7762,12 +7888,14 @@ var ClientState = /** @class */ (function () {
7762
7888
  return [2 /*return*/, this._state];
7763
7889
  }
7764
7890
  });
7765
- }); };
7766
- /**
7767
- * Gets the current state.
7768
- * @returns {Promise<State>}
7769
- */
7770
- this.getState = function () { return __awaiter(_this, void 0, void 0, function () {
7891
+ });
7892
+ };
7893
+ /**
7894
+ * Gets the current state.
7895
+ * @returns {Promise<State>}
7896
+ */
7897
+ ClientState.prototype.getState = function () {
7898
+ return __awaiter(this, void 0, void 0, function () {
7771
7899
  var _a;
7772
7900
  return __generator(this, function (_b) {
7773
7901
  switch (_b.label) {
@@ -7797,30 +7925,20 @@ var ClientState = /** @class */ (function () {
7797
7925
  return [2 /*return*/, this._state];
7798
7926
  }
7799
7927
  });
7800
- }); };
7801
- /**
7802
- * Disposes of the state.
7803
- * @returns {Promise<void>}
7804
- */
7805
- this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
7806
- var _a;
7807
- return __generator(this, function (_b) {
7808
- GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
7809
- this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " dispose"));
7810
- if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onDispose) {
7811
- this.params.callbacks.onDispose(this.params.clientId, this.params.stateName);
7812
- }
7813
- return [2 /*return*/];
7814
- });
7815
- }); };
7928
+ });
7929
+ };
7930
+ /**
7931
+ * Disposes of the state.
7932
+ * @returns {Promise<void>}
7933
+ */
7934
+ ClientState.prototype.dispose = function () {
7935
+ var _a;
7816
7936
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
7817
- this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
7818
- params: params,
7819
- });
7820
- if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
7821
- this.params.callbacks.onInit(this.params.clientId, this.params.stateName);
7937
+ this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " dispose"));
7938
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onDispose) {
7939
+ this.params.callbacks.onDispose(this.params.clientId, this.params.stateName);
7822
7940
  }
7823
- }
7941
+ };
7824
7942
  return ClientState;
7825
7943
  }());
7826
7944