agent-swarm-kit 1.0.70 → 1.0.71

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.mjs CHANGED
@@ -225,6 +225,7 @@ var _a;
225
225
  var init = (_a = createActivator("agent-swarm"), _a.init), inject = _a.inject, provide = _a.provide;
226
226
 
227
227
  var baseServices$1 = {
228
+ busService: Symbol('busService'),
228
229
  loggerService: Symbol('loggerService'),
229
230
  contextService: Symbol('contextService'),
230
231
  };
@@ -951,7 +952,7 @@ var ClientAgent = /** @class */ (function () {
951
952
  validation = null;
952
953
  return [4 /*yield*/, this.params.validate(result)];
953
954
  case 2:
954
- if (!(validation = _a.sent())) return [3 /*break*/, 7];
955
+ if (!(validation = _a.sent())) return [3 /*break*/, 8];
955
956
  return [4 /*yield*/, this._resurrectModel(mode, validation)];
956
957
  case 3:
957
958
  rawResult_1 = _a.sent();
@@ -968,12 +969,44 @@ var ClientAgent = /** @class */ (function () {
968
969
  return [4 /*yield*/, this._outputSubject.next(result_1)];
969
970
  case 6:
970
971
  _a.sent();
971
- return [2 /*return*/];
972
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
973
+ type: "emit-output",
974
+ source: "agent",
975
+ input: {
976
+ mode: mode,
977
+ rawResult: rawResult_1,
978
+ },
979
+ output: {
980
+ result: result_1,
981
+ },
982
+ context: {
983
+ agentName: this.params.agentName,
984
+ }
985
+ })];
972
986
  case 7:
987
+ _a.sent();
988
+ return [2 /*return*/];
989
+ case 8:
973
990
  this.params.onOutput &&
974
991
  this.params.onOutput(this.params.clientId, this.params.agentName, result);
975
992
  return [4 /*yield*/, this._outputSubject.next(result)];
976
- case 8:
993
+ case 9:
994
+ _a.sent();
995
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
996
+ type: "emit-output",
997
+ source: "agent",
998
+ input: {
999
+ mode: mode,
1000
+ rawResult: rawResult,
1001
+ },
1002
+ output: {
1003
+ result: result,
1004
+ },
1005
+ context: {
1006
+ agentName: this.params.agentName,
1007
+ }
1008
+ })];
1009
+ case 10:
977
1010
  _a.sent();
978
1011
  return [2 /*return*/];
979
1012
  }
@@ -1105,6 +1138,19 @@ var ClientAgent = /** @class */ (function () {
1105
1138
  content: message.trim(),
1106
1139
  })];
1107
1140
  case 1:
1141
+ _a.sent();
1142
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1143
+ type: "commit-user-message",
1144
+ source: "agent",
1145
+ input: {
1146
+ message: message,
1147
+ },
1148
+ output: {},
1149
+ context: {
1150
+ agentName: this.params.agentName,
1151
+ }
1152
+ })];
1153
+ case 2:
1108
1154
  _a.sent();
1109
1155
  return [2 /*return*/];
1110
1156
  }
@@ -1128,6 +1174,17 @@ var ClientAgent = /** @class */ (function () {
1128
1174
  content: "",
1129
1175
  })];
1130
1176
  case 1:
1177
+ _a.sent();
1178
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1179
+ type: "commit-flush",
1180
+ source: "agent",
1181
+ input: {},
1182
+ output: {},
1183
+ context: {
1184
+ agentName: this.params.agentName,
1185
+ }
1186
+ })];
1187
+ case 2:
1131
1188
  _a.sent();
1132
1189
  return [2 /*return*/];
1133
1190
  }
@@ -1144,6 +1201,17 @@ var ClientAgent = /** @class */ (function () {
1144
1201
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitAgentChange"));
1145
1202
  return [4 /*yield*/, this._agentChangeSubject.next(AGENT_CHANGE_SYMBOL)];
1146
1203
  case 1:
1204
+ _a.sent();
1205
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1206
+ type: "commit-agent-change",
1207
+ source: "agent",
1208
+ input: {},
1209
+ output: {},
1210
+ context: {
1211
+ agentName: this.params.agentName,
1212
+ }
1213
+ })];
1214
+ case 2:
1147
1215
  _a.sent();
1148
1216
  return [2 /*return*/];
1149
1217
  }
@@ -1168,6 +1236,19 @@ var ClientAgent = /** @class */ (function () {
1168
1236
  content: message.trim(),
1169
1237
  })];
1170
1238
  case 1:
1239
+ _a.sent();
1240
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1241
+ type: "commit-system-message",
1242
+ source: "agent",
1243
+ input: {
1244
+ message: message,
1245
+ },
1246
+ output: {},
1247
+ context: {
1248
+ agentName: this.params.agentName,
1249
+ }
1250
+ })];
1251
+ case 2:
1171
1252
  _a.sent();
1172
1253
  return [2 /*return*/];
1173
1254
  }
@@ -1196,6 +1277,20 @@ var ClientAgent = /** @class */ (function () {
1196
1277
  _a.sent();
1197
1278
  return [4 /*yield*/, this._toolCommitSubject.next()];
1198
1279
  case 2:
1280
+ _a.sent();
1281
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1282
+ type: "commit-tool-output",
1283
+ source: "agent",
1284
+ input: {
1285
+ toolId: toolId,
1286
+ content: content,
1287
+ },
1288
+ output: {},
1289
+ context: {
1290
+ agentName: this.params.agentName,
1291
+ }
1292
+ })];
1293
+ case 3:
1199
1294
  _a.sent();
1200
1295
  return [2 /*return*/];
1201
1296
  }
@@ -1409,6 +1504,7 @@ var AgentConnectionService = /** @class */ (function () {
1409
1504
  function AgentConnectionService() {
1410
1505
  var _this = this;
1411
1506
  this.loggerService = inject(TYPES.loggerService);
1507
+ this.busService = inject(TYPES.busService);
1412
1508
  this.contextService = inject(TYPES.contextService);
1413
1509
  this.sessionValidationService = inject(TYPES.sessionValidationService);
1414
1510
  this.historyConnectionService = inject(TYPES.historyConnectionService);
@@ -1439,7 +1535,7 @@ var AgentConnectionService = /** @class */ (function () {
1439
1535
  .getStorage(clientId, storageName)
1440
1536
  .waitForInit();
1441
1537
  });
1442
- return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
1538
+ return new ClientAgent(__assign({ clientId: clientId, agentName: agentName, validate: validate, logger: _this.loggerService, bus: _this.busService, history: _this.historyConnectionService.getHistory(clientId, agentName), prompt: prompt, system: system, transform: transform, map: map, tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get), completion: completion }, callbacks));
1443
1539
  });
1444
1540
  /**
1445
1541
  * Executes an input command.
@@ -1617,6 +1713,19 @@ var ClientHistory = /** @class */ (function () {
1617
1713
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " push"), { message: message });
1618
1714
  return [4 /*yield*/, this.params.items.push(message, this.params.clientId, this.params.agentName)];
1619
1715
  case 1:
1716
+ _a.sent();
1717
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1718
+ type: "push",
1719
+ source: "history",
1720
+ input: {
1721
+ message: message
1722
+ },
1723
+ output: {},
1724
+ context: {
1725
+ agentName: this.params.agentName,
1726
+ }
1727
+ })];
1728
+ case 2:
1620
1729
  _a.sent();
1621
1730
  return [2 /*return*/];
1622
1731
  }
@@ -1850,6 +1959,7 @@ var HistoryConnectionService = /** @class */ (function () {
1850
1959
  function HistoryConnectionService() {
1851
1960
  var _this = this;
1852
1961
  this.loggerService = inject(TYPES.loggerService);
1962
+ this.busService = inject(TYPES.busService);
1853
1963
  this.contextService = inject(TYPES.contextService);
1854
1964
  this.sessionValidationService = inject(TYPES.sessionValidationService);
1855
1965
  /**
@@ -1866,6 +1976,7 @@ var HistoryConnectionService = /** @class */ (function () {
1866
1976
  return new ClientHistory({
1867
1977
  clientId: clientId,
1868
1978
  agentName: agentName,
1979
+ bus: _this.busService,
1869
1980
  items: GLOBAL_CONFIG.CC_GET_AGENT_HISTORY_ADAPTER(clientId, agentName),
1870
1981
  logger: _this.loggerService,
1871
1982
  });
@@ -1993,10 +2104,44 @@ var ClientSwarm = /** @class */ (function () {
1993
2104
  this.params = params;
1994
2105
  this._agentChangedSubject = new Subject();
1995
2106
  this._activeAgent = AGENT_NEED_FETCH;
2107
+ this._cancelOutputSubject = new Subject();
2108
+ /**
2109
+ * Cancel the await of output by emit of empty string
2110
+ * @returns {Promise<string>} - The output from the active agent.
2111
+ */
2112
+ this.cancelOutput = function () { return __awaiter(_this, void 0, void 0, function () {
2113
+ var _a, _b;
2114
+ var _c;
2115
+ return __generator(this, function (_d) {
2116
+ switch (_d.label) {
2117
+ case 0:
2118
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " cancelOutput"));
2119
+ _b = (_a = this._cancelOutputSubject).next;
2120
+ _c = {};
2121
+ return [4 /*yield*/, this.getAgentName()];
2122
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.agentName = _d.sent(),
2123
+ _c.output = "",
2124
+ _c)])];
2125
+ case 2:
2126
+ _d.sent();
2127
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2128
+ type: "cancel-output",
2129
+ source: "swarm",
2130
+ input: {},
2131
+ output: {},
2132
+ context: {
2133
+ swarmName: this.params.swarmName,
2134
+ },
2135
+ })];
2136
+ case 3:
2137
+ _d.sent();
2138
+ return [2 /*return*/];
2139
+ }
2140
+ });
2141
+ }); };
1996
2142
  /**
1997
2143
  * Waits for output from the active agent.
1998
2144
  * @returns {Promise<string>} - The output from the active agent.
1999
- * @throws {Error} - If the timeout is reached.
2000
2145
  */
2001
2146
  this.waitForOutput = queued(function () { return __awaiter(_this, void 0, void 0, function () {
2002
2147
  var _a, awaiter, resolve, getOutput, handleOutput, un, _b, agentName, output, expectAgent;
@@ -2010,18 +2155,23 @@ var ClientSwarm = /** @class */ (function () {
2010
2155
  var _this = this;
2011
2156
  return __generator(this, function (_a) {
2012
2157
  switch (_a.label) {
2013
- case 0: return [4 /*yield*/, Promise.race(this._agentList.map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
2158
+ case 0: return [4 /*yield*/, Promise.race(this._agentList
2159
+ .map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
2014
2160
  var _c;
2015
2161
  var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
2016
2162
  return __generator(this, function (_e) {
2017
2163
  switch (_e.label) {
2018
2164
  case 0:
2019
- _c = { agentName: agentName };
2165
+ _c = {
2166
+ agentName: agentName
2167
+ };
2020
2168
  return [4 /*yield*/, agent.waitForOutput()];
2021
- case 1: return [2 /*return*/, (_c.output = _e.sent(), _c)];
2169
+ case 1: return [2 /*return*/, (_c.output = _e.sent(),
2170
+ _c)];
2022
2171
  }
2023
2172
  });
2024
- }); }))];
2173
+ }); })
2174
+ .concat(this._cancelOutputSubject.toPromise()))];
2025
2175
  case 1: return [2 /*return*/, _a.sent()];
2026
2176
  }
2027
2177
  });
@@ -2044,7 +2194,21 @@ var ClientSwarm = /** @class */ (function () {
2044
2194
  return [4 /*yield*/, this.getAgentName()];
2045
2195
  case 2:
2046
2196
  expectAgent = _c.sent();
2047
- agentName !== expectAgent && this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
2197
+ agentName !== expectAgent &&
2198
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
2199
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2200
+ type: "wait-for-output",
2201
+ source: "swarm",
2202
+ input: {},
2203
+ output: {
2204
+ result: output,
2205
+ },
2206
+ context: {
2207
+ swarmName: this.params.swarmName,
2208
+ },
2209
+ })];
2210
+ case 3:
2211
+ _c.sent();
2048
2212
  return [2 /*return*/, output];
2049
2213
  }
2050
2214
  });
@@ -2065,7 +2229,20 @@ var ClientSwarm = /** @class */ (function () {
2065
2229
  case 1:
2066
2230
  _a._activeAgent = _b.sent();
2067
2231
  _b.label = 2;
2068
- case 2: return [2 /*return*/, this._activeAgent];
2232
+ case 2: return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2233
+ type: "get-agent-name",
2234
+ source: "swarm",
2235
+ input: {},
2236
+ output: {
2237
+ activeAgent: this._activeAgent,
2238
+ },
2239
+ context: {
2240
+ swarmName: this.params.swarmName,
2241
+ },
2242
+ })];
2243
+ case 3:
2244
+ _b.sent();
2245
+ return [2 /*return*/, this._activeAgent];
2069
2246
  }
2070
2247
  });
2071
2248
  }); };
@@ -2074,7 +2251,7 @@ var ClientSwarm = /** @class */ (function () {
2074
2251
  * @returns {Promise<IAgent>} - The active agent.
2075
2252
  */
2076
2253
  this.getAgent = function () { return __awaiter(_this, void 0, void 0, function () {
2077
- var agent;
2254
+ var agent, result;
2078
2255
  return __generator(this, function (_a) {
2079
2256
  switch (_a.label) {
2080
2257
  case 0:
@@ -2082,7 +2259,21 @@ var ClientSwarm = /** @class */ (function () {
2082
2259
  return [4 /*yield*/, this.getAgentName()];
2083
2260
  case 1:
2084
2261
  agent = _a.sent();
2085
- return [2 /*return*/, this.params.agentMap[agent]];
2262
+ result = this.params.agentMap[agent];
2263
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2264
+ type: "get-agent",
2265
+ source: "swarm",
2266
+ input: {
2267
+ result: result,
2268
+ },
2269
+ output: {},
2270
+ context: {
2271
+ swarmName: this.params.swarmName,
2272
+ },
2273
+ })];
2274
+ case 2:
2275
+ _a.sent();
2276
+ return [2 /*return*/, result];
2086
2277
  }
2087
2278
  });
2088
2279
  }); };
@@ -2101,8 +2292,22 @@ var ClientSwarm = /** @class */ (function () {
2101
2292
  throw new Error("agent-swarm agent ".concat(agentName, " not in the swarm"));
2102
2293
  }
2103
2294
  this.params.agentMap[agentName] = agent;
2104
- return [4 /*yield*/, this._agentChangedSubject.next([agentName, agent])];
2295
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2296
+ type: "set-agent-ref",
2297
+ source: "swarm",
2298
+ input: {
2299
+ agentName: agentName,
2300
+ agent: agent,
2301
+ },
2302
+ output: {},
2303
+ context: {
2304
+ swarmName: this.params.swarmName,
2305
+ },
2306
+ })];
2105
2307
  case 1:
2308
+ _a.sent();
2309
+ return [4 /*yield*/, this._agentChangedSubject.next([agentName, agent])];
2310
+ case 2:
2106
2311
  _a.sent();
2107
2312
  return [2 /*return*/];
2108
2313
  }
@@ -2120,6 +2325,19 @@ var ClientSwarm = /** @class */ (function () {
2120
2325
  this._activeAgent = agentName;
2121
2326
  return [4 /*yield*/, this.params.onAgentChanged(this.params.clientId, agentName, this.params.swarmName)];
2122
2327
  case 1:
2328
+ _a.sent();
2329
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2330
+ type: "set-agent-name",
2331
+ source: "swarm",
2332
+ input: {
2333
+ agentName: agentName,
2334
+ },
2335
+ output: {},
2336
+ context: {
2337
+ swarmName: this.params.swarmName,
2338
+ },
2339
+ })];
2340
+ case 2:
2123
2341
  _a.sent();
2124
2342
  return [2 /*return*/];
2125
2343
  }
@@ -2147,6 +2365,7 @@ var SwarmConnectionService = /** @class */ (function () {
2147
2365
  function SwarmConnectionService() {
2148
2366
  var _this = this;
2149
2367
  this.loggerService = inject(TYPES.loggerService);
2368
+ this.busService = inject(TYPES.busService);
2150
2369
  this.contextService = inject(TYPES.contextService);
2151
2370
  this.agentConnectionService = inject(TYPES.agentConnectionService);
2152
2371
  this.swarmSchemaService = inject(TYPES.swarmSchemaService);
@@ -2182,6 +2401,7 @@ var SwarmConnectionService = /** @class */ (function () {
2182
2401
  defaultAgent: defaultAgent,
2183
2402
  swarmName: swarmName,
2184
2403
  logger: _this.loggerService,
2404
+ bus: _this.busService,
2185
2405
  onAgentChanged: function (clientId, agentName, swarmName) {
2186
2406
  return __awaiter(this, void 0, void 0, function () {
2187
2407
  return __generator(this, function (_a) {
@@ -2201,6 +2421,22 @@ var SwarmConnectionService = /** @class */ (function () {
2201
2421
  callbacks: callbacks,
2202
2422
  });
2203
2423
  });
2424
+ /**
2425
+ * Cancel the await of output by emit of empty string
2426
+ * @returns {Promise<void>}
2427
+ */
2428
+ this.cancelOutput = function () { return __awaiter(_this, void 0, void 0, function () {
2429
+ return __generator(this, function (_a) {
2430
+ switch (_a.label) {
2431
+ case 0:
2432
+ this.loggerService.log("swarmConnectionService cancelOutput", {
2433
+ context: this.contextService.context,
2434
+ });
2435
+ return [4 /*yield*/, this.getSwarm(this.contextService.context.clientId, this.contextService.context.swarmName).cancelOutput()];
2436
+ case 1: return [2 /*return*/, _a.sent()];
2437
+ }
2438
+ });
2439
+ }); };
2204
2440
  /**
2205
2441
  * Waits for the output from the swarm.
2206
2442
  * @returns {Promise<any>} The output from the swarm.
@@ -2395,6 +2631,19 @@ var ClientSession = /** @class */ (function () {
2395
2631
  this.params.onEmit(this.params.clientId, this.params.swarmName, message);
2396
2632
  return [4 /*yield*/, this._emitSubject.next(message)];
2397
2633
  case 1:
2634
+ _a.sent();
2635
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2636
+ type: "emit",
2637
+ source: "session",
2638
+ input: {
2639
+ message: message
2640
+ },
2641
+ output: {},
2642
+ context: {
2643
+ swarmName: this.params.swarmName,
2644
+ }
2645
+ })];
2646
+ case 2:
2398
2647
  _a.sent();
2399
2648
  return [2 /*return*/];
2400
2649
  }
@@ -2425,6 +2674,22 @@ var ClientSession = /** @class */ (function () {
2425
2674
  return [4 /*yield*/, outputAwaiter];
2426
2675
  case 2:
2427
2676
  output = _a.sent();
2677
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2678
+ type: "execute",
2679
+ source: "session",
2680
+ input: {
2681
+ message: message,
2682
+ mode: mode,
2683
+ },
2684
+ output: {
2685
+ result: output,
2686
+ },
2687
+ context: {
2688
+ swarmName: this.params.swarmName,
2689
+ }
2690
+ })];
2691
+ case 3:
2692
+ _a.sent();
2428
2693
  return [2 /*return*/, output];
2429
2694
  }
2430
2695
  });
@@ -2436,7 +2701,7 @@ var ClientSession = /** @class */ (function () {
2436
2701
  * @returns {Promise<void>}
2437
2702
  */
2438
2703
  this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
2439
- var agent;
2704
+ var agent, result;
2440
2705
  return __generator(this, function (_a) {
2441
2706
  switch (_a.label) {
2442
2707
  case 0:
@@ -2448,7 +2713,23 @@ var ClientSession = /** @class */ (function () {
2448
2713
  case 1:
2449
2714
  agent = _a.sent();
2450
2715
  return [4 /*yield*/, agent.commitToolOutput(toolId, content)];
2451
- case 2: return [2 /*return*/, _a.sent()];
2716
+ case 2:
2717
+ result = _a.sent();
2718
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2719
+ type: "commit-tool-output",
2720
+ source: "session",
2721
+ input: {
2722
+ toolId: toolId,
2723
+ content: content,
2724
+ },
2725
+ output: {},
2726
+ context: {
2727
+ swarmName: this.params.swarmName,
2728
+ }
2729
+ })];
2730
+ case 3:
2731
+ _a.sent();
2732
+ return [2 /*return*/, result];
2452
2733
  }
2453
2734
  });
2454
2735
  }); };
@@ -2458,7 +2739,7 @@ var ClientSession = /** @class */ (function () {
2458
2739
  * @returns {Promise<void>}
2459
2740
  */
2460
2741
  this.commitUserMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
2461
- var agent;
2742
+ var agent, result;
2462
2743
  return __generator(this, function (_a) {
2463
2744
  switch (_a.label) {
2464
2745
  case 0:
@@ -2469,7 +2750,22 @@ var ClientSession = /** @class */ (function () {
2469
2750
  case 1:
2470
2751
  agent = _a.sent();
2471
2752
  return [4 /*yield*/, agent.commitUserMessage(message)];
2472
- case 2: return [2 /*return*/, _a.sent()];
2753
+ case 2:
2754
+ result = _a.sent();
2755
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2756
+ type: "commit-user-message",
2757
+ source: "session",
2758
+ input: {
2759
+ message: message,
2760
+ },
2761
+ output: {},
2762
+ context: {
2763
+ swarmName: this.params.swarmName,
2764
+ }
2765
+ })];
2766
+ case 3:
2767
+ _a.sent();
2768
+ return [2 /*return*/, result];
2473
2769
  }
2474
2770
  });
2475
2771
  }); };
@@ -2478,7 +2774,7 @@ var ClientSession = /** @class */ (function () {
2478
2774
  * @returns {Promise<void>}
2479
2775
  */
2480
2776
  this.commitFlush = function () { return __awaiter(_this, void 0, void 0, function () {
2481
- var agent;
2777
+ var agent, result;
2482
2778
  return __generator(this, function (_a) {
2483
2779
  switch (_a.label) {
2484
2780
  case 0:
@@ -2487,7 +2783,20 @@ var ClientSession = /** @class */ (function () {
2487
2783
  case 1:
2488
2784
  agent = _a.sent();
2489
2785
  return [4 /*yield*/, agent.commitFlush()];
2490
- case 2: return [2 /*return*/, _a.sent()];
2786
+ case 2:
2787
+ result = _a.sent();
2788
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2789
+ type: "commit-flush",
2790
+ source: "session",
2791
+ input: {},
2792
+ output: {},
2793
+ context: {
2794
+ swarmName: this.params.swarmName,
2795
+ }
2796
+ })];
2797
+ case 3:
2798
+ _a.sent();
2799
+ return [2 /*return*/, result];
2491
2800
  }
2492
2801
  });
2493
2802
  }); };
@@ -2497,7 +2806,7 @@ var ClientSession = /** @class */ (function () {
2497
2806
  * @returns {Promise<void>}
2498
2807
  */
2499
2808
  this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
2500
- var agent;
2809
+ var agent, result;
2501
2810
  return __generator(this, function (_a) {
2502
2811
  switch (_a.label) {
2503
2812
  case 0:
@@ -2508,7 +2817,22 @@ var ClientSession = /** @class */ (function () {
2508
2817
  case 1:
2509
2818
  agent = _a.sent();
2510
2819
  return [4 /*yield*/, agent.commitSystemMessage(message)];
2511
- case 2: return [2 /*return*/, _a.sent()];
2820
+ case 2:
2821
+ result = _a.sent();
2822
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2823
+ type: "commit-system-message",
2824
+ source: "session",
2825
+ input: {
2826
+ message: message
2827
+ },
2828
+ output: {},
2829
+ context: {
2830
+ swarmName: this.params.swarmName,
2831
+ }
2832
+ })];
2833
+ case 3:
2834
+ _a.sent();
2835
+ return [2 /*return*/, result];
2512
2836
  }
2513
2837
  });
2514
2838
  }); };
@@ -2539,6 +2863,15 @@ var ClientSession = /** @class */ (function () {
2539
2863
  }
2540
2864
  });
2541
2865
  }); });
2866
+ _this.params.bus.emit(_this.params.clientId, {
2867
+ type: "connect",
2868
+ source: "session",
2869
+ input: {},
2870
+ output: {},
2871
+ context: {
2872
+ swarmName: _this.params.swarmName,
2873
+ }
2874
+ });
2542
2875
  return function (incoming) { return __awaiter(_this, void 0, void 0, function () {
2543
2876
  var _a;
2544
2877
  var _b;
@@ -2590,6 +2923,7 @@ var SessionConnectionService = /** @class */ (function () {
2590
2923
  function SessionConnectionService() {
2591
2924
  var _this = this;
2592
2925
  this.loggerService = inject(TYPES.loggerService);
2926
+ this.busService = inject(TYPES.busService);
2593
2927
  this.contextService = inject(TYPES.contextService);
2594
2928
  this.swarmConnectionService = inject(TYPES.swarmConnectionService);
2595
2929
  this.swarmSchemaService = inject(TYPES.swarmSchemaService);
@@ -2604,7 +2938,7 @@ var SessionConnectionService = /** @class */ (function () {
2604
2938
  return "".concat(clientId, "-").concat(swarmName);
2605
2939
  }, function (clientId, swarmName) {
2606
2940
  var callbacks = _this.swarmSchemaService.get(swarmName).callbacks;
2607
- return new ClientSession(__assign({ clientId: clientId, logger: _this.loggerService, swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName), swarmName: swarmName }, callbacks));
2941
+ return new ClientSession(__assign({ clientId: clientId, logger: _this.loggerService, bus: _this.busService, swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName), swarmName: swarmName }, callbacks));
2608
2942
  });
2609
2943
  /**
2610
2944
  * Emits a message to the session.
@@ -3540,6 +3874,39 @@ var SwarmPublicService = /** @class */ (function () {
3540
3874
  var _this = this;
3541
3875
  this.loggerService = inject(TYPES.loggerService);
3542
3876
  this.swarmConnectionService = inject(TYPES.swarmConnectionService);
3877
+ /**
3878
+ * Cancel the await of output by emit of empty string
3879
+ * @param {string} clientId - The client ID.
3880
+ * @param {SwarmName} swarmName - The swarm name.
3881
+ * @returns {Promise<void>}
3882
+ */
3883
+ this.cancelOutput = function (clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
3884
+ var _this = this;
3885
+ return __generator(this, function (_a) {
3886
+ switch (_a.label) {
3887
+ case 0:
3888
+ this.loggerService.log("swarmPublicService cancelOutput", {
3889
+ clientId: clientId,
3890
+ swarmName: swarmName,
3891
+ });
3892
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
3893
+ return __generator(this, function (_a) {
3894
+ switch (_a.label) {
3895
+ case 0: return [4 /*yield*/, this.swarmConnectionService.cancelOutput()];
3896
+ case 1: return [2 /*return*/, _a.sent()];
3897
+ }
3898
+ });
3899
+ }); }, {
3900
+ clientId: clientId,
3901
+ swarmName: swarmName,
3902
+ agentName: "",
3903
+ storageName: "",
3904
+ stateName: "",
3905
+ })];
3906
+ case 1: return [2 /*return*/, _a.sent()];
3907
+ }
3908
+ });
3909
+ }); };
3543
3910
  /**
3544
3911
  * Waits for output from the swarm.
3545
3912
  * @param {string} clientId - The client ID.
@@ -4533,6 +4900,23 @@ var ClientStorage = /** @class */ (function () {
4533
4900
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onSearch) {
4534
4901
  (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onSearch(search, indexed, this.params.clientId, this.params.storageName);
4535
4902
  }
4903
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
4904
+ type: "take",
4905
+ source: "storage",
4906
+ input: {
4907
+ search: search,
4908
+ total: total,
4909
+ score: score,
4910
+ },
4911
+ output: {
4912
+ indexed: indexed,
4913
+ },
4914
+ context: {
4915
+ storageName: this.params.storageName,
4916
+ }
4917
+ })];
4918
+ case 3:
4919
+ _c.sent();
4536
4920
  return [2 /*return*/, indexed.take(total, score)];
4537
4921
  }
4538
4922
  });
@@ -4559,6 +4943,19 @@ var ClientStorage = /** @class */ (function () {
4559
4943
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
4560
4944
  (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
4561
4945
  }
4946
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
4947
+ type: "upsert",
4948
+ source: "storage",
4949
+ input: {
4950
+ item: item,
4951
+ },
4952
+ output: {},
4953
+ context: {
4954
+ storageName: this.params.storageName,
4955
+ }
4956
+ })];
4957
+ case 2:
4958
+ _c.sent();
4562
4959
  return [2 /*return*/];
4563
4960
  }
4564
4961
  });
@@ -4571,15 +4968,31 @@ var ClientStorage = /** @class */ (function () {
4571
4968
  this.remove = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
4572
4969
  var _a, _b;
4573
4970
  return __generator(this, function (_c) {
4574
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " remove"), {
4575
- id: itemId,
4576
- });
4577
- this._itemMap.delete(itemId);
4578
- this._createEmbedding.clear(itemId);
4579
- if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
4580
- (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
4971
+ switch (_c.label) {
4972
+ case 0:
4973
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " remove"), {
4974
+ id: itemId,
4975
+ });
4976
+ this._itemMap.delete(itemId);
4977
+ this._createEmbedding.clear(itemId);
4978
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
4979
+ (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
4980
+ }
4981
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
4982
+ type: "remove",
4983
+ source: "storage",
4984
+ input: {
4985
+ itemId: itemId,
4986
+ },
4987
+ output: {},
4988
+ context: {
4989
+ storageName: this.params.storageName,
4990
+ }
4991
+ })];
4992
+ case 1:
4993
+ _c.sent();
4994
+ return [2 /*return*/];
4581
4995
  }
4582
- return [2 /*return*/];
4583
4996
  });
4584
4997
  }); };
4585
4998
  /**
@@ -4588,10 +5001,24 @@ var ClientStorage = /** @class */ (function () {
4588
5001
  */
4589
5002
  this.clear = function () { return __awaiter(_this, void 0, void 0, function () {
4590
5003
  return __generator(this, function (_a) {
4591
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " clear"));
4592
- this._itemMap.clear();
4593
- this._createEmbedding.clear();
4594
- return [2 /*return*/];
5004
+ switch (_a.label) {
5005
+ case 0:
5006
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " clear"));
5007
+ this._itemMap.clear();
5008
+ this._createEmbedding.clear();
5009
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5010
+ type: "clear",
5011
+ source: "storage",
5012
+ input: {},
5013
+ output: {},
5014
+ context: {
5015
+ storageName: this.params.storageName,
5016
+ }
5017
+ })];
5018
+ case 1:
5019
+ _a.sent();
5020
+ return [2 /*return*/];
5021
+ }
4595
5022
  });
4596
5023
  }); };
4597
5024
  /**
@@ -4600,12 +5027,32 @@ var ClientStorage = /** @class */ (function () {
4600
5027
  * @returns {Promise<T | null>} - The item or null if not found.
4601
5028
  */
4602
5029
  this.get = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
5030
+ var result;
4603
5031
  var _a;
4604
5032
  return __generator(this, function (_b) {
4605
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " get"), {
4606
- id: itemId,
4607
- });
4608
- return [2 /*return*/, (_a = this._itemMap.get(itemId)) !== null && _a !== void 0 ? _a : null];
5033
+ switch (_b.label) {
5034
+ case 0:
5035
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " get"), {
5036
+ id: itemId,
5037
+ });
5038
+ result = (_a = this._itemMap.get(itemId)) !== null && _a !== void 0 ? _a : null;
5039
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5040
+ type: "get",
5041
+ source: "storage",
5042
+ input: {
5043
+ itemId: itemId,
5044
+ },
5045
+ output: {
5046
+ result: result,
5047
+ },
5048
+ context: {
5049
+ storageName: this.params.storageName,
5050
+ }
5051
+ })];
5052
+ case 1:
5053
+ _b.sent();
5054
+ return [2 /*return*/, result];
5055
+ }
4609
5056
  });
4610
5057
  }); };
4611
5058
  /**
@@ -4617,27 +5064,43 @@ var ClientStorage = /** @class */ (function () {
4617
5064
  var result, _a, _b, item;
4618
5065
  var e_1, _c;
4619
5066
  return __generator(this, function (_d) {
4620
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " list"));
4621
- if (!filter) {
4622
- return [2 /*return*/, __spreadArray([], __read(this._itemMap.values()), false)];
4623
- }
4624
- result = [];
4625
- try {
4626
- for (_a = __values(this._itemMap.values()), _b = _a.next(); !_b.done; _b = _a.next()) {
4627
- item = _b.value;
4628
- if (filter(item)) {
4629
- result.push(item);
5067
+ switch (_d.label) {
5068
+ case 0:
5069
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " list"));
5070
+ if (!filter) {
5071
+ return [2 /*return*/, __spreadArray([], __read(this._itemMap.values()), false)];
4630
5072
  }
4631
- }
4632
- }
4633
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4634
- finally {
4635
- try {
4636
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
4637
- }
4638
- finally { if (e_1) throw e_1.error; }
5073
+ result = [];
5074
+ try {
5075
+ for (_a = __values(this._itemMap.values()), _b = _a.next(); !_b.done; _b = _a.next()) {
5076
+ item = _b.value;
5077
+ if (filter(item)) {
5078
+ result.push(item);
5079
+ }
5080
+ }
5081
+ }
5082
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
5083
+ finally {
5084
+ try {
5085
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
5086
+ }
5087
+ finally { if (e_1) throw e_1.error; }
5088
+ }
5089
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5090
+ type: "list",
5091
+ source: "storage",
5092
+ input: {},
5093
+ output: {
5094
+ result: result,
5095
+ },
5096
+ context: {
5097
+ storageName: this.params.storageName,
5098
+ }
5099
+ })];
5100
+ case 1:
5101
+ _d.sent();
5102
+ return [2 /*return*/, result];
4639
5103
  }
4640
- return [2 /*return*/, result];
4641
5104
  });
4642
5105
  }); };
4643
5106
  /**
@@ -4672,6 +5135,7 @@ var StorageConnectionService = /** @class */ (function () {
4672
5135
  function StorageConnectionService() {
4673
5136
  var _this = this;
4674
5137
  this.loggerService = inject(TYPES.loggerService);
5138
+ this.busService = inject(TYPES.busService);
4675
5139
  this.contextService = inject(TYPES.contextService);
4676
5140
  this.storageSchemaService = inject(TYPES.storageSchemaService);
4677
5141
  this.sessionValidationService = inject(TYPES.sessionValidationService);
@@ -4691,7 +5155,7 @@ var StorageConnectionService = /** @class */ (function () {
4691
5155
  if (!shared) {
4692
5156
  throw new Error("agent-swarm storage not shared storageName=".concat(storageName));
4693
5157
  }
4694
- return new ClientStorage(__assign(__assign({ clientId: clientId, storageName: storageName, embedding: embeddingName, calculateSimilarity: calculateSimilarity, createEmbedding: createEmbedding, createIndex: createIndex, getData: getData, shared: shared, logger: _this.loggerService }, embedding), { callbacks: callbacks }));
5158
+ return new ClientStorage(__assign(__assign({ clientId: clientId, storageName: storageName, embedding: embeddingName, calculateSimilarity: calculateSimilarity, createEmbedding: createEmbedding, createIndex: createIndex, getData: getData, shared: shared, logger: _this.loggerService, bus: _this.busService }, embedding), { callbacks: callbacks }));
4695
5159
  });
4696
5160
  /**
4697
5161
  * Retrieves a storage instance based on client ID and storage name.
@@ -4709,7 +5173,7 @@ var StorageConnectionService = /** @class */ (function () {
4709
5173
  return _this.getSharedStorage(clientId, storageName);
4710
5174
  }
4711
5175
  var _c = _this.embeddingSchemaService.get(embeddingName), calculateSimilarity = _c.calculateSimilarity, createEmbedding = _c.createEmbedding, embedding = _c.callbacks;
4712
- return new ClientStorage(__assign(__assign({ clientId: clientId, storageName: storageName, embedding: embeddingName, calculateSimilarity: calculateSimilarity, createEmbedding: createEmbedding, createIndex: createIndex, getData: getData, shared: shared, logger: _this.loggerService }, embedding), { callbacks: callbacks }));
5176
+ return new ClientStorage(__assign(__assign({ clientId: clientId, storageName: storageName, embedding: embeddingName, calculateSimilarity: calculateSimilarity, createEmbedding: createEmbedding, createIndex: createIndex, getData: getData, shared: shared, logger: _this.loggerService, bus: _this.busService }, embedding), { callbacks: callbacks }));
4713
5177
  });
4714
5178
  /**
4715
5179
  * Retrieves a list of storage data based on a search query and total number of items.
@@ -5365,6 +5829,19 @@ var ClientState = /** @class */ (function () {
5365
5829
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onWrite) {
5366
5830
  this.params.callbacks.onWrite(this._state, this.params.clientId, this.params.stateName);
5367
5831
  }
5832
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5833
+ type: "set-state",
5834
+ source: "state",
5835
+ input: {},
5836
+ output: {
5837
+ state: this._state,
5838
+ },
5839
+ context: {
5840
+ stateName: this.params.stateName,
5841
+ }
5842
+ })];
5843
+ case 2:
5844
+ _b.sent();
5368
5845
  return [2 /*return*/, this._state];
5369
5846
  }
5370
5847
  });
@@ -5385,6 +5862,19 @@ var ClientState = /** @class */ (function () {
5385
5862
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onRead) {
5386
5863
  this.params.callbacks.onRead(this._state, this.params.clientId, this.params.stateName);
5387
5864
  }
5865
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5866
+ type: "get-state",
5867
+ source: "state",
5868
+ input: {},
5869
+ output: {
5870
+ state: this._state,
5871
+ },
5872
+ context: {
5873
+ stateName: this.params.stateName,
5874
+ }
5875
+ })];
5876
+ case 2:
5877
+ _b.sent();
5388
5878
  return [2 /*return*/, this._state];
5389
5879
  }
5390
5880
  });
@@ -5422,6 +5912,7 @@ var StateConnectionService = /** @class */ (function () {
5422
5912
  function StateConnectionService() {
5423
5913
  var _this = this;
5424
5914
  this.loggerService = inject(TYPES.loggerService);
5915
+ this.busService = inject(TYPES.busService);
5425
5916
  this.contextService = inject(TYPES.contextService);
5426
5917
  this.stateSchemaService = inject(TYPES.stateSchemaService);
5427
5918
  this.sessionValidationService = inject(TYPES.sessionValidationService);
@@ -5444,6 +5935,7 @@ var StateConnectionService = /** @class */ (function () {
5444
5935
  clientId: clientId,
5445
5936
  stateName: stateName,
5446
5937
  logger: _this.loggerService,
5938
+ bus: _this.busService,
5447
5939
  setState: setState
5448
5940
  ? queued(function () {
5449
5941
  var args = [];
@@ -5482,6 +5974,7 @@ var StateConnectionService = /** @class */ (function () {
5482
5974
  clientId: clientId,
5483
5975
  stateName: stateName,
5484
5976
  logger: _this.loggerService,
5977
+ bus: _this.busService,
5485
5978
  setState: setState
5486
5979
  ? queued(function () {
5487
5980
  var args = [];
@@ -5689,7 +6182,103 @@ var StatePublicService = /** @class */ (function () {
5689
6182
  return StatePublicService;
5690
6183
  }());
5691
6184
 
6185
+ var BusService = /** @class */ (function () {
6186
+ function BusService() {
6187
+ var _this = this;
6188
+ this.loggerService = inject(TYPES.loggerService);
6189
+ this._eventSourceSet = new Set();
6190
+ this.getEventSubject = memoize(function (_a) {
6191
+ var _b = __read(_a, 2), clientId = _b[0], source = _b[1];
6192
+ return "".concat(clientId, "-").concat(source);
6193
+ }, function () { return new Subject(); });
6194
+ /**
6195
+ * Subscribes to events for a specific client and source.
6196
+ * @param {string} clientId - The client ID.
6197
+ * @param {EventSource} source - The event source.
6198
+ * @param {(event: T) => void} fn - The callback function to handle the event.
6199
+ * @returns {Subscription} The subscription object.
6200
+ */
6201
+ this.subscribe = function (clientId, source, fn) {
6202
+ _this.loggerService.log("busService subscribe", {
6203
+ clientId: clientId,
6204
+ source: source,
6205
+ });
6206
+ _this._eventSourceSet.add(source);
6207
+ return _this.getEventSubject(clientId, source).subscribe(fn);
6208
+ };
6209
+ /**
6210
+ * Subscribes to a single event for a specific client and source.
6211
+ * @param {string} clientId - The client ID.
6212
+ * @param {EventSource} source - The event source.
6213
+ * @param {(event: T) => boolean} filterFn - The filter function to determine if the event should be handled.
6214
+ * @param {(event: T) => void} fn - The callback function to handle the event.
6215
+ * @returns {Subscription} The subscription object.
6216
+ */
6217
+ this.once = function (clientId, source, filterFn, fn) {
6218
+ _this.loggerService.log("busService once", {
6219
+ clientId: clientId,
6220
+ source: source,
6221
+ });
6222
+ _this._eventSourceSet.add(source);
6223
+ return _this.getEventSubject(clientId, source).filter(filterFn).once(fn);
6224
+ };
6225
+ /**
6226
+ * Emits an event for a specific client.
6227
+ * @param {string} clientId - The client ID.
6228
+ * @param {T} event - The event to emit.
6229
+ * @returns {Promise<void>} A promise that resolves when the event has been emitted.
6230
+ */
6231
+ this.emit = function (clientId, event) { return __awaiter(_this, void 0, void 0, function () {
6232
+ return __generator(this, function (_a) {
6233
+ switch (_a.label) {
6234
+ case 0:
6235
+ this.loggerService.debug("busService emit", {
6236
+ clientId: clientId,
6237
+ event: event,
6238
+ });
6239
+ if (!this.getEventSubject.has("".concat(clientId, "-").concat(event.source))) {
6240
+ return [2 /*return*/];
6241
+ }
6242
+ return [4 /*yield*/, this.getEventSubject(clientId, event.source).next(event)];
6243
+ case 1:
6244
+ _a.sent();
6245
+ return [2 /*return*/];
6246
+ }
6247
+ });
6248
+ }); };
6249
+ /**
6250
+ * Disposes of all event subscriptions for a specific client.
6251
+ * @param {string} clientId - The client ID.
6252
+ */
6253
+ this.dispose = function (clientId) {
6254
+ var e_1, _a;
6255
+ _this.loggerService.log("busService dispose", {
6256
+ clientId: clientId,
6257
+ });
6258
+ try {
6259
+ for (var _b = __values(_this._eventSourceSet), _c = _b.next(); !_c.done; _c = _b.next()) {
6260
+ var source = _c.value;
6261
+ var key = "".concat(clientId, "-").concat(source);
6262
+ if (_this.getEventSubject.has(key)) {
6263
+ _this.getEventSubject(clientId, source).unsubscribeAll();
6264
+ }
6265
+ _this.getEventSubject.clear("".concat(clientId, "-").concat(source));
6266
+ }
6267
+ }
6268
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
6269
+ finally {
6270
+ try {
6271
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6272
+ }
6273
+ finally { if (e_1) throw e_1.error; }
6274
+ }
6275
+ };
6276
+ }
6277
+ return BusService;
6278
+ }());
6279
+
5692
6280
  {
6281
+ provide(TYPES.busService, function () { return new BusService(); });
5693
6282
  provide(TYPES.loggerService, function () { return new LoggerService(); });
5694
6283
  provide(TYPES.contextService, function () { return new ContextService(); });
5695
6284
  }
@@ -5735,6 +6324,7 @@ var StatePublicService = /** @class */ (function () {
5735
6324
  }
5736
6325
 
5737
6326
  var baseServices = {
6327
+ busService: inject(TYPES.busService),
5738
6328
  loggerService: inject(TYPES.loggerService),
5739
6329
  contextService: inject(TYPES.contextService),
5740
6330
  };
@@ -6192,69 +6782,64 @@ var disposeConnection = function (clientId, swarmName) { return __awaiter(void 0
6192
6782
  });
6193
6783
  swarm.swarmValidationService.validate(swarmName, "disposeConnection");
6194
6784
  swarm.sessionValidationService.removeSession(clientId);
6785
+ swarm.busService.dispose(clientId);
6195
6786
  return [4 /*yield*/, swarm.sessionPublicService.dispose(clientId, swarmName)];
6196
6787
  case 1:
6197
6788
  _a.sent();
6198
6789
  return [4 /*yield*/, swarm.swarmPublicService.dispose(clientId, swarmName)];
6199
6790
  case 2:
6200
6791
  _a.sent();
6201
- return [4 /*yield*/, Promise.all([
6202
- swarm.swarmValidationService
6203
- .getAgentList(swarmName)
6204
- .map(function (agentName) { return __awaiter(void 0, void 0, void 0, function () {
6205
- return __generator(this, function (_a) {
6206
- switch (_a.label) {
6207
- case 0: return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
6208
- case 1:
6209
- _a.sent();
6210
- return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
6211
- case 2:
6212
- _a.sent();
6213
- return [2 /*return*/];
6214
- }
6215
- });
6216
- }); }),
6217
- ])];
6792
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
6793
+ .getAgentList(swarmName)
6794
+ .map(function (agentName) { return __awaiter(void 0, void 0, void 0, function () {
6795
+ return __generator(this, function (_a) {
6796
+ switch (_a.label) {
6797
+ case 0: return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
6798
+ case 1:
6799
+ _a.sent();
6800
+ return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
6801
+ case 2:
6802
+ _a.sent();
6803
+ return [2 /*return*/];
6804
+ }
6805
+ });
6806
+ }); }))];
6218
6807
  case 3:
6219
6808
  _a.sent();
6220
- return [4 /*yield*/, Promise.all([
6221
- swarm.swarmValidationService
6222
- .getAgentList(swarmName)
6223
- .flatMap(function (agentName) {
6224
- return swarm.agentValidationService.getStorageList(agentName);
6225
- })
6226
- .filter(function (storageName) { return !!storageName; })
6227
- .map(function (storageName) { return __awaiter(void 0, void 0, void 0, function () {
6228
- return __generator(this, function (_a) {
6229
- switch (_a.label) {
6230
- case 0: return [4 /*yield*/, swarm.storagePublicService.dispose(clientId, storageName)];
6231
- case 1:
6232
- _a.sent();
6233
- return [2 /*return*/];
6234
- }
6235
- });
6236
- }); }),
6237
- ])];
6809
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
6810
+ .getAgentList(swarmName)
6811
+ .flatMap(function (agentName) {
6812
+ return swarm.agentValidationService.getStorageList(agentName);
6813
+ })
6814
+ .filter(function (storageName) { return !!storageName; })
6815
+ .map(function (storageName) { return __awaiter(void 0, void 0, void 0, function () {
6816
+ return __generator(this, function (_a) {
6817
+ switch (_a.label) {
6818
+ case 0: return [4 /*yield*/, swarm.storagePublicService.dispose(clientId, storageName)];
6819
+ case 1:
6820
+ _a.sent();
6821
+ return [2 /*return*/];
6822
+ }
6823
+ });
6824
+ }); }))];
6238
6825
  case 4:
6239
6826
  _a.sent();
6240
- return [4 /*yield*/, Promise.all([
6241
- swarm.swarmValidationService
6242
- .getAgentList(swarmName)
6243
- .flatMap(function (agentName) {
6244
- return swarm.agentValidationService.getStateList(agentName);
6245
- })
6246
- .filter(function (stateName) { return !!stateName; })
6247
- .map(function (stateName) { return __awaiter(void 0, void 0, void 0, function () {
6248
- return __generator(this, function (_a) {
6249
- switch (_a.label) {
6250
- case 0: return [4 /*yield*/, swarm.statePublicService.dispose(clientId, stateName)];
6251
- case 1:
6252
- _a.sent();
6253
- return [2 /*return*/];
6254
- }
6255
- });
6256
- }); }),
6257
- ])];
6827
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
6828
+ .getAgentList(swarmName)
6829
+ .flatMap(function (agentName) {
6830
+ return swarm.agentValidationService.getStateList(agentName);
6831
+ })
6832
+ .filter(function (stateName) { return !!stateName; })
6833
+ .map(function (stateName) { return __awaiter(void 0, void 0, void 0, function () {
6834
+ return __generator(this, function (_a) {
6835
+ switch (_a.label) {
6836
+ case 0: return [4 /*yield*/, swarm.statePublicService.dispose(clientId, stateName)];
6837
+ case 1:
6838
+ _a.sent();
6839
+ return [2 /*return*/];
6840
+ }
6841
+ });
6842
+ }); }))];
6258
6843
  case 5:
6259
6844
  _a.sent();
6260
6845
  return [4 /*yield*/, HistoryAdapter.dispose(clientId, null)];
@@ -6922,6 +7507,35 @@ var executeForce = function (content, clientId) { return __awaiter(void 0, void
6922
7507
  });
6923
7508
  }); };
6924
7509
 
7510
+ var DISALLOWED_EVENT_SOURCE_LIST$1 = new Set([
7511
+ "agent",
7512
+ "history",
7513
+ "session",
7514
+ "state",
7515
+ "storage",
7516
+ "swarm",
7517
+ ]);
7518
+ /**
7519
+ * Listens for an event on the swarm bus service and executes a callback function when the event is received.
7520
+ *
7521
+ * @template T - The type of the data payload.
7522
+ * @param {string} clientId - The ID of the client to listen for events from.
7523
+ * @param {(data: T) => void} fn - The callback function to execute when the event is received. The data payload is passed as an argument to this function.
7524
+ * @returns {void} - Returns nothing.
7525
+ */
7526
+ var listenEvent = function (clientId, topicName, fn) {
7527
+ swarm.loggerService.log("function listenEvent", {
7528
+ clientId: clientId,
7529
+ });
7530
+ if (DISALLOWED_EVENT_SOURCE_LIST$1.has(topicName)) {
7531
+ throw new Error("agent-swarm listenEvent topic is reserved topicName=".concat(topicName));
7532
+ }
7533
+ return swarm.busService.subscribe(clientId, topicName, function (_a) {
7534
+ var payload = _a.payload;
7535
+ return fn(payload);
7536
+ });
7537
+ };
7538
+
6925
7539
  /**
6926
7540
  * Retrieves the last message sent by the user from the client's message history.
6927
7541
  *
@@ -7104,6 +7718,184 @@ var makeAutoDispose = function (clientId, swarmName, _a) {
7104
7718
  };
7105
7719
  };
7106
7720
 
7721
+ var DISALLOWED_EVENT_SOURCE_LIST = new Set([
7722
+ "agent",
7723
+ "history",
7724
+ "session",
7725
+ "state",
7726
+ "storage",
7727
+ "swarm",
7728
+ ]);
7729
+ /**
7730
+ * Emits an event to the swarm bus service.
7731
+ *
7732
+ * @template T - The type of the payload.
7733
+ * @param {string} clientId - The ID of the client emitting the event.
7734
+ * @param {T} payload - The payload of the event.
7735
+ * @returns {boolean} - Returns true if the event was successfully emitted.
7736
+ */
7737
+ var event = function (clientId, topicName, payload) {
7738
+ swarm.loggerService.log("function listenEvent", {
7739
+ clientId: clientId,
7740
+ });
7741
+ if (DISALLOWED_EVENT_SOURCE_LIST.has(topicName)) {
7742
+ throw new Error("agent-swarm event topic is reserved topicName=".concat(topicName));
7743
+ }
7744
+ return swarm.busService.emit(clientId, {
7745
+ source: topicName,
7746
+ payload: payload,
7747
+ });
7748
+ };
7749
+
7750
+ /**
7751
+ * Cancel the await of output by emit of empty string
7752
+ *
7753
+ * @param {string} clientId - The ID of the client.
7754
+ * @param {string} agentName - The name of the agent.
7755
+ * @returns {Promise<void>} - A promise that resolves when the output is canceled
7756
+ */
7757
+ var cancelOutput = function (clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
7758
+ var swarmName, currentAgentName;
7759
+ return __generator(this, function (_a) {
7760
+ switch (_a.label) {
7761
+ case 0:
7762
+ swarm.loggerService.log('function cancelOutput', {
7763
+ clientId: clientId,
7764
+ agentName: agentName,
7765
+ });
7766
+ swarm.agentValidationService.validate(agentName, "cancelOutput");
7767
+ swarm.sessionValidationService.validate(clientId, "cancelOutput");
7768
+ swarmName = swarm.sessionValidationService.getSwarm(clientId);
7769
+ swarm.swarmValidationService.validate(swarmName, "cancelOutput");
7770
+ return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
7771
+ case 1:
7772
+ currentAgentName = _a.sent();
7773
+ if (currentAgentName !== agentName) {
7774
+ swarm.loggerService.log('function "cancelOutput" skipped due to the agent change', {
7775
+ currentAgentName: currentAgentName,
7776
+ agentName: agentName,
7777
+ clientId: clientId,
7778
+ });
7779
+ return [2 /*return*/];
7780
+ }
7781
+ return [4 /*yield*/, swarm.swarmPublicService.cancelOutput(clientId, swarmName)];
7782
+ case 2:
7783
+ _a.sent();
7784
+ return [2 /*return*/];
7785
+ }
7786
+ });
7787
+ }); };
7788
+
7789
+ /**
7790
+ * Cancel the await of output by emit of empty string without checking active agent
7791
+ *
7792
+ * @param {string} clientId - The ID of the client.
7793
+ * @param {string} agentName - The name of the agent.
7794
+ * @returns {Promise<void>} - A promise that resolves when the output is canceled
7795
+ */
7796
+ var cancelOutputForce = function (clientId) { return __awaiter(void 0, void 0, void 0, function () {
7797
+ var swarmName;
7798
+ return __generator(this, function (_a) {
7799
+ switch (_a.label) {
7800
+ case 0:
7801
+ swarm.loggerService.log('function cancelOutputForce', {
7802
+ clientId: clientId,
7803
+ });
7804
+ swarm.sessionValidationService.validate(clientId, "cancelOutputForce");
7805
+ swarmName = swarm.sessionValidationService.getSwarm(clientId);
7806
+ swarm.swarmValidationService.validate(swarmName, "cancelOutputForce");
7807
+ return [4 /*yield*/, swarm.swarmPublicService.cancelOutput(clientId, swarmName)];
7808
+ case 1:
7809
+ _a.sent();
7810
+ return [2 /*return*/];
7811
+ }
7812
+ });
7813
+ }); };
7814
+
7815
+ /**
7816
+ * Hook to subscribe to agent events for a specific client.
7817
+ *
7818
+ * @param {string} clientId - The ID of the client to subscribe to events for.
7819
+ * @param {function} fn - The callback function to handle the event.
7820
+ * @returns {function} - A function to unsubscribe from the event.
7821
+ */
7822
+ var listenAgentEvent = function (clientId, fn) {
7823
+ swarm.loggerService.log("middleware listenAgentEvent", {
7824
+ clientId: clientId,
7825
+ });
7826
+ return swarm.busService.subscribe(clientId, "agent", fn);
7827
+ };
7828
+
7829
+ /**
7830
+ * Hook to subscribe to history events for a specific client.
7831
+ *
7832
+ * @param {string} clientId - The ID of the client to subscribe to.
7833
+ * @param {(event: IBaseEvent) => void} fn - The callback function to handle the event.
7834
+ * @returns {Function} - The unsubscribe function.
7835
+ */
7836
+ var listenHistoryEvent = function (clientId, fn) {
7837
+ swarm.loggerService.log("middleware listenHistoryEvent", {
7838
+ clientId: clientId,
7839
+ });
7840
+ return swarm.busService.subscribe(clientId, "history", fn);
7841
+ };
7842
+
7843
+ /**
7844
+ * Hook to subscribe to session events for a specific client.
7845
+ *
7846
+ * @param {string} clientId - The ID of the client to subscribe to session events for.
7847
+ * @param {function} fn - The callback function to handle the session events.
7848
+ * @returns {function} - The unsubscribe function to stop listening to session events.
7849
+ */
7850
+ var listenSessionEvent = function (clientId, fn) {
7851
+ swarm.loggerService.log("middleware listenSessionEvent", {
7852
+ clientId: clientId,
7853
+ });
7854
+ return swarm.busService.subscribe(clientId, "session", fn);
7855
+ };
7856
+
7857
+ /**
7858
+ * Hook to subscribe to state events for a specific client.
7859
+ *
7860
+ * @param {string} clientId - The ID of the client to subscribe to.
7861
+ * @param {function} fn - The callback function to handle the event.
7862
+ * @returns {function} - The unsubscribe function to stop listening to the events.
7863
+ */
7864
+ var listenStateEvent = function (clientId, fn) {
7865
+ swarm.loggerService.log("middleware listenStateEvent", {
7866
+ clientId: clientId,
7867
+ });
7868
+ return swarm.busService.subscribe(clientId, "state", fn);
7869
+ };
7870
+
7871
+ /**
7872
+ * Hook to subscribe to storage events for a specific client.
7873
+ *
7874
+ * @param {string} clientId - The ID of the client to subscribe to storage events for.
7875
+ * @param {function} fn - The callback function to handle the storage event.
7876
+ * @returns {function} - A function to unsubscribe from the storage events.
7877
+ */
7878
+ var listenStorageEvent = function (clientId, fn) {
7879
+ swarm.loggerService.log("middleware listenStorageEvent", {
7880
+ clientId: clientId,
7881
+ });
7882
+ return swarm.busService.subscribe(clientId, "storage", fn);
7883
+ };
7884
+
7885
+ /**
7886
+ * Hook to subscribe to swarm events for a specific client.
7887
+ *
7888
+ * @param {string} clientId - The ID of the client to subscribe to events for.
7889
+ * @param {(event: IBaseEvent) => void} fn - The callback function to handle the event.
7890
+ * @returns {Function} - A function to unsubscribe from the event.
7891
+ */
7892
+ var listenSwarmEvent = function (clientId, fn) {
7893
+ swarm.loggerService.log("middleware listenSwarmEvent", {
7894
+ clientId: clientId,
7895
+ });
7896
+ return swarm.busService.subscribe(clientId, "swarm", fn);
7897
+ };
7898
+
7107
7899
  var StorageUtils = /** @class */ (function () {
7108
7900
  function StorageUtils() {
7109
7901
  var _this = this;
@@ -7347,4 +8139,4 @@ var StateUtils = /** @class */ (function () {
7347
8139
  */
7348
8140
  var State = new StateUtils();
7349
8141
 
7350
- export { ContextService, History, HistoryAdapter, HistoryInstance, State, Storage, addAgent, addCompletion, addEmbedding, addState, addStorage, addSwarm, addTool, changeAgent, commitFlush, commitFlushForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, emit, emitForce, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getRawHistory, getSessionMode, getUserHistory, makeAutoDispose, makeConnection, session, setConfig, swarm };
8142
+ export { ContextService, History, HistoryAdapter, HistoryInstance, State, Storage, addAgent, addCompletion, addEmbedding, addState, addStorage, addSwarm, addTool, cancelOutput, cancelOutputForce, changeAgent, commitFlush, commitFlushForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, emit, emitForce, event, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getRawHistory, getSessionMode, getUserHistory, listenAgentEvent, listenEvent, listenHistoryEvent, listenSessionEvent, listenStateEvent, listenStorageEvent, listenSwarmEvent, makeAutoDispose, makeConnection, session, setConfig, swarm };