agent-swarm-kit 1.0.70 → 1.0.72

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
@@ -227,6 +227,7 @@ var _a;
227
227
  var init = (_a = diKit.createActivator("agent-swarm"), _a.init), inject = _a.inject, provide = _a.provide;
228
228
 
229
229
  var baseServices$1 = {
230
+ busService: Symbol('busService'),
230
231
  loggerService: Symbol('loggerService'),
231
232
  contextService: Symbol('contextService'),
232
233
  };
@@ -953,7 +954,7 @@ var ClientAgent = /** @class */ (function () {
953
954
  validation = null;
954
955
  return [4 /*yield*/, this.params.validate(result)];
955
956
  case 2:
956
- if (!(validation = _a.sent())) return [3 /*break*/, 7];
957
+ if (!(validation = _a.sent())) return [3 /*break*/, 8];
957
958
  return [4 /*yield*/, this._resurrectModel(mode, validation)];
958
959
  case 3:
959
960
  rawResult_1 = _a.sent();
@@ -970,12 +971,44 @@ var ClientAgent = /** @class */ (function () {
970
971
  return [4 /*yield*/, this._outputSubject.next(result_1)];
971
972
  case 6:
972
973
  _a.sent();
973
- return [2 /*return*/];
974
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
975
+ type: "emit-output",
976
+ source: "agent",
977
+ input: {
978
+ mode: mode,
979
+ rawResult: rawResult_1,
980
+ },
981
+ output: {
982
+ result: result_1,
983
+ },
984
+ context: {
985
+ agentName: this.params.agentName,
986
+ }
987
+ })];
974
988
  case 7:
989
+ _a.sent();
990
+ return [2 /*return*/];
991
+ case 8:
975
992
  this.params.onOutput &&
976
993
  this.params.onOutput(this.params.clientId, this.params.agentName, result);
977
994
  return [4 /*yield*/, this._outputSubject.next(result)];
978
- case 8:
995
+ case 9:
996
+ _a.sent();
997
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
998
+ type: "emit-output",
999
+ source: "agent",
1000
+ input: {
1001
+ mode: mode,
1002
+ rawResult: rawResult,
1003
+ },
1004
+ output: {
1005
+ result: result,
1006
+ },
1007
+ context: {
1008
+ agentName: this.params.agentName,
1009
+ }
1010
+ })];
1011
+ case 10:
979
1012
  _a.sent();
980
1013
  return [2 /*return*/];
981
1014
  }
@@ -1107,6 +1140,19 @@ var ClientAgent = /** @class */ (function () {
1107
1140
  content: message.trim(),
1108
1141
  })];
1109
1142
  case 1:
1143
+ _a.sent();
1144
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1145
+ type: "commit-user-message",
1146
+ source: "agent",
1147
+ input: {
1148
+ message: message,
1149
+ },
1150
+ output: {},
1151
+ context: {
1152
+ agentName: this.params.agentName,
1153
+ }
1154
+ })];
1155
+ case 2:
1110
1156
  _a.sent();
1111
1157
  return [2 /*return*/];
1112
1158
  }
@@ -1130,6 +1176,17 @@ var ClientAgent = /** @class */ (function () {
1130
1176
  content: "",
1131
1177
  })];
1132
1178
  case 1:
1179
+ _a.sent();
1180
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1181
+ type: "commit-flush",
1182
+ source: "agent",
1183
+ input: {},
1184
+ output: {},
1185
+ context: {
1186
+ agentName: this.params.agentName,
1187
+ }
1188
+ })];
1189
+ case 2:
1133
1190
  _a.sent();
1134
1191
  return [2 /*return*/];
1135
1192
  }
@@ -1146,6 +1203,17 @@ var ClientAgent = /** @class */ (function () {
1146
1203
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitAgentChange"));
1147
1204
  return [4 /*yield*/, this._agentChangeSubject.next(AGENT_CHANGE_SYMBOL)];
1148
1205
  case 1:
1206
+ _a.sent();
1207
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1208
+ type: "commit-agent-change",
1209
+ source: "agent",
1210
+ input: {},
1211
+ output: {},
1212
+ context: {
1213
+ agentName: this.params.agentName,
1214
+ }
1215
+ })];
1216
+ case 2:
1149
1217
  _a.sent();
1150
1218
  return [2 /*return*/];
1151
1219
  }
@@ -1170,6 +1238,19 @@ var ClientAgent = /** @class */ (function () {
1170
1238
  content: message.trim(),
1171
1239
  })];
1172
1240
  case 1:
1241
+ _a.sent();
1242
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1243
+ type: "commit-system-message",
1244
+ source: "agent",
1245
+ input: {
1246
+ message: message,
1247
+ },
1248
+ output: {},
1249
+ context: {
1250
+ agentName: this.params.agentName,
1251
+ }
1252
+ })];
1253
+ case 2:
1173
1254
  _a.sent();
1174
1255
  return [2 /*return*/];
1175
1256
  }
@@ -1198,6 +1279,20 @@ var ClientAgent = /** @class */ (function () {
1198
1279
  _a.sent();
1199
1280
  return [4 /*yield*/, this._toolCommitSubject.next()];
1200
1281
  case 2:
1282
+ _a.sent();
1283
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1284
+ type: "commit-tool-output",
1285
+ source: "agent",
1286
+ input: {
1287
+ toolId: toolId,
1288
+ content: content,
1289
+ },
1290
+ output: {},
1291
+ context: {
1292
+ agentName: this.params.agentName,
1293
+ }
1294
+ })];
1295
+ case 3:
1201
1296
  _a.sent();
1202
1297
  return [2 /*return*/];
1203
1298
  }
@@ -1411,6 +1506,7 @@ var AgentConnectionService = /** @class */ (function () {
1411
1506
  function AgentConnectionService() {
1412
1507
  var _this = this;
1413
1508
  this.loggerService = inject(TYPES.loggerService);
1509
+ this.busService = inject(TYPES.busService);
1414
1510
  this.contextService = inject(TYPES.contextService);
1415
1511
  this.sessionValidationService = inject(TYPES.sessionValidationService);
1416
1512
  this.historyConnectionService = inject(TYPES.historyConnectionService);
@@ -1441,7 +1537,7 @@ var AgentConnectionService = /** @class */ (function () {
1441
1537
  .getStorage(clientId, storageName)
1442
1538
  .waitForInit();
1443
1539
  });
1444
- 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));
1540
+ 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));
1445
1541
  });
1446
1542
  /**
1447
1543
  * Executes an input command.
@@ -1619,6 +1715,19 @@ var ClientHistory = /** @class */ (function () {
1619
1715
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " push"), { message: message });
1620
1716
  return [4 /*yield*/, this.params.items.push(message, this.params.clientId, this.params.agentName)];
1621
1717
  case 1:
1718
+ _a.sent();
1719
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
1720
+ type: "push",
1721
+ source: "history",
1722
+ input: {
1723
+ message: message
1724
+ },
1725
+ output: {},
1726
+ context: {
1727
+ agentName: this.params.agentName,
1728
+ }
1729
+ })];
1730
+ case 2:
1622
1731
  _a.sent();
1623
1732
  return [2 /*return*/];
1624
1733
  }
@@ -1852,6 +1961,7 @@ var HistoryConnectionService = /** @class */ (function () {
1852
1961
  function HistoryConnectionService() {
1853
1962
  var _this = this;
1854
1963
  this.loggerService = inject(TYPES.loggerService);
1964
+ this.busService = inject(TYPES.busService);
1855
1965
  this.contextService = inject(TYPES.contextService);
1856
1966
  this.sessionValidationService = inject(TYPES.sessionValidationService);
1857
1967
  /**
@@ -1868,6 +1978,7 @@ var HistoryConnectionService = /** @class */ (function () {
1868
1978
  return new ClientHistory({
1869
1979
  clientId: clientId,
1870
1980
  agentName: agentName,
1981
+ bus: _this.busService,
1871
1982
  items: GLOBAL_CONFIG.CC_GET_AGENT_HISTORY_ADAPTER(clientId, agentName),
1872
1983
  logger: _this.loggerService,
1873
1984
  });
@@ -1995,10 +2106,44 @@ var ClientSwarm = /** @class */ (function () {
1995
2106
  this.params = params;
1996
2107
  this._agentChangedSubject = new functoolsKit.Subject();
1997
2108
  this._activeAgent = AGENT_NEED_FETCH;
2109
+ this._cancelOutputSubject = new functoolsKit.Subject();
2110
+ /**
2111
+ * Cancel the await of output by emit of empty string
2112
+ * @returns {Promise<string>} - The output from the active agent.
2113
+ */
2114
+ this.cancelOutput = function () { return __awaiter(_this, void 0, void 0, function () {
2115
+ var _a, _b;
2116
+ var _c;
2117
+ return __generator(this, function (_d) {
2118
+ switch (_d.label) {
2119
+ case 0:
2120
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " cancelOutput"));
2121
+ _b = (_a = this._cancelOutputSubject).next;
2122
+ _c = {};
2123
+ return [4 /*yield*/, this.getAgentName()];
2124
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.agentName = _d.sent(),
2125
+ _c.output = "",
2126
+ _c)])];
2127
+ case 2:
2128
+ _d.sent();
2129
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2130
+ type: "cancel-output",
2131
+ source: "swarm",
2132
+ input: {},
2133
+ output: {},
2134
+ context: {
2135
+ swarmName: this.params.swarmName,
2136
+ },
2137
+ })];
2138
+ case 3:
2139
+ _d.sent();
2140
+ return [2 /*return*/];
2141
+ }
2142
+ });
2143
+ }); };
1998
2144
  /**
1999
2145
  * Waits for output from the active agent.
2000
2146
  * @returns {Promise<string>} - The output from the active agent.
2001
- * @throws {Error} - If the timeout is reached.
2002
2147
  */
2003
2148
  this.waitForOutput = functoolsKit.queued(function () { return __awaiter(_this, void 0, void 0, function () {
2004
2149
  var _a, awaiter, resolve, getOutput, handleOutput, un, _b, agentName, output, expectAgent;
@@ -2012,18 +2157,23 @@ var ClientSwarm = /** @class */ (function () {
2012
2157
  var _this = this;
2013
2158
  return __generator(this, function (_a) {
2014
2159
  switch (_a.label) {
2015
- case 0: return [4 /*yield*/, Promise.race(this._agentList.map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
2160
+ case 0: return [4 /*yield*/, Promise.race(this._agentList
2161
+ .map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
2016
2162
  var _c;
2017
2163
  var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
2018
2164
  return __generator(this, function (_e) {
2019
2165
  switch (_e.label) {
2020
2166
  case 0:
2021
- _c = { agentName: agentName };
2167
+ _c = {
2168
+ agentName: agentName
2169
+ };
2022
2170
  return [4 /*yield*/, agent.waitForOutput()];
2023
- case 1: return [2 /*return*/, (_c.output = _e.sent(), _c)];
2171
+ case 1: return [2 /*return*/, (_c.output = _e.sent(),
2172
+ _c)];
2024
2173
  }
2025
2174
  });
2026
- }); }))];
2175
+ }); })
2176
+ .concat(this._cancelOutputSubject.toPromise()))];
2027
2177
  case 1: return [2 /*return*/, _a.sent()];
2028
2178
  }
2029
2179
  });
@@ -2046,7 +2196,21 @@ var ClientSwarm = /** @class */ (function () {
2046
2196
  return [4 /*yield*/, this.getAgentName()];
2047
2197
  case 2:
2048
2198
  expectAgent = _c.sent();
2049
- agentName !== expectAgent && this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
2199
+ agentName !== expectAgent &&
2200
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForAgent agent miss"), { agentName: agentName, expectAgent: expectAgent });
2201
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2202
+ type: "wait-for-output",
2203
+ source: "swarm",
2204
+ input: {},
2205
+ output: {
2206
+ result: output,
2207
+ },
2208
+ context: {
2209
+ swarmName: this.params.swarmName,
2210
+ },
2211
+ })];
2212
+ case 3:
2213
+ _c.sent();
2050
2214
  return [2 /*return*/, output];
2051
2215
  }
2052
2216
  });
@@ -2067,7 +2231,20 @@ var ClientSwarm = /** @class */ (function () {
2067
2231
  case 1:
2068
2232
  _a._activeAgent = _b.sent();
2069
2233
  _b.label = 2;
2070
- case 2: return [2 /*return*/, this._activeAgent];
2234
+ case 2: return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2235
+ type: "get-agent-name",
2236
+ source: "swarm",
2237
+ input: {},
2238
+ output: {
2239
+ activeAgent: this._activeAgent,
2240
+ },
2241
+ context: {
2242
+ swarmName: this.params.swarmName,
2243
+ },
2244
+ })];
2245
+ case 3:
2246
+ _b.sent();
2247
+ return [2 /*return*/, this._activeAgent];
2071
2248
  }
2072
2249
  });
2073
2250
  }); };
@@ -2076,7 +2253,7 @@ var ClientSwarm = /** @class */ (function () {
2076
2253
  * @returns {Promise<IAgent>} - The active agent.
2077
2254
  */
2078
2255
  this.getAgent = function () { return __awaiter(_this, void 0, void 0, function () {
2079
- var agent;
2256
+ var agent, result;
2080
2257
  return __generator(this, function (_a) {
2081
2258
  switch (_a.label) {
2082
2259
  case 0:
@@ -2084,7 +2261,21 @@ var ClientSwarm = /** @class */ (function () {
2084
2261
  return [4 /*yield*/, this.getAgentName()];
2085
2262
  case 1:
2086
2263
  agent = _a.sent();
2087
- return [2 /*return*/, this.params.agentMap[agent]];
2264
+ result = this.params.agentMap[agent];
2265
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2266
+ type: "get-agent",
2267
+ source: "swarm",
2268
+ input: {
2269
+ result: result,
2270
+ },
2271
+ output: {},
2272
+ context: {
2273
+ swarmName: this.params.swarmName,
2274
+ },
2275
+ })];
2276
+ case 2:
2277
+ _a.sent();
2278
+ return [2 /*return*/, result];
2088
2279
  }
2089
2280
  });
2090
2281
  }); };
@@ -2103,8 +2294,22 @@ var ClientSwarm = /** @class */ (function () {
2103
2294
  throw new Error("agent-swarm agent ".concat(agentName, " not in the swarm"));
2104
2295
  }
2105
2296
  this.params.agentMap[agentName] = agent;
2106
- return [4 /*yield*/, this._agentChangedSubject.next([agentName, agent])];
2297
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2298
+ type: "set-agent-ref",
2299
+ source: "swarm",
2300
+ input: {
2301
+ agentName: agentName,
2302
+ agent: agent,
2303
+ },
2304
+ output: {},
2305
+ context: {
2306
+ swarmName: this.params.swarmName,
2307
+ },
2308
+ })];
2107
2309
  case 1:
2310
+ _a.sent();
2311
+ return [4 /*yield*/, this._agentChangedSubject.next([agentName, agent])];
2312
+ case 2:
2108
2313
  _a.sent();
2109
2314
  return [2 /*return*/];
2110
2315
  }
@@ -2122,6 +2327,19 @@ var ClientSwarm = /** @class */ (function () {
2122
2327
  this._activeAgent = agentName;
2123
2328
  return [4 /*yield*/, this.params.onAgentChanged(this.params.clientId, agentName, this.params.swarmName)];
2124
2329
  case 1:
2330
+ _a.sent();
2331
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2332
+ type: "set-agent-name",
2333
+ source: "swarm",
2334
+ input: {
2335
+ agentName: agentName,
2336
+ },
2337
+ output: {},
2338
+ context: {
2339
+ swarmName: this.params.swarmName,
2340
+ },
2341
+ })];
2342
+ case 2:
2125
2343
  _a.sent();
2126
2344
  return [2 /*return*/];
2127
2345
  }
@@ -2149,6 +2367,7 @@ var SwarmConnectionService = /** @class */ (function () {
2149
2367
  function SwarmConnectionService() {
2150
2368
  var _this = this;
2151
2369
  this.loggerService = inject(TYPES.loggerService);
2370
+ this.busService = inject(TYPES.busService);
2152
2371
  this.contextService = inject(TYPES.contextService);
2153
2372
  this.agentConnectionService = inject(TYPES.agentConnectionService);
2154
2373
  this.swarmSchemaService = inject(TYPES.swarmSchemaService);
@@ -2184,6 +2403,7 @@ var SwarmConnectionService = /** @class */ (function () {
2184
2403
  defaultAgent: defaultAgent,
2185
2404
  swarmName: swarmName,
2186
2405
  logger: _this.loggerService,
2406
+ bus: _this.busService,
2187
2407
  onAgentChanged: function (clientId, agentName, swarmName) {
2188
2408
  return __awaiter(this, void 0, void 0, function () {
2189
2409
  return __generator(this, function (_a) {
@@ -2203,6 +2423,22 @@ var SwarmConnectionService = /** @class */ (function () {
2203
2423
  callbacks: callbacks,
2204
2424
  });
2205
2425
  });
2426
+ /**
2427
+ * Cancel the await of output by emit of empty string
2428
+ * @returns {Promise<void>}
2429
+ */
2430
+ this.cancelOutput = function () { return __awaiter(_this, void 0, void 0, function () {
2431
+ return __generator(this, function (_a) {
2432
+ switch (_a.label) {
2433
+ case 0:
2434
+ this.loggerService.log("swarmConnectionService cancelOutput", {
2435
+ context: this.contextService.context,
2436
+ });
2437
+ return [4 /*yield*/, this.getSwarm(this.contextService.context.clientId, this.contextService.context.swarmName).cancelOutput()];
2438
+ case 1: return [2 /*return*/, _a.sent()];
2439
+ }
2440
+ });
2441
+ }); };
2206
2442
  /**
2207
2443
  * Waits for the output from the swarm.
2208
2444
  * @returns {Promise<any>} The output from the swarm.
@@ -2397,6 +2633,19 @@ var ClientSession = /** @class */ (function () {
2397
2633
  this.params.onEmit(this.params.clientId, this.params.swarmName, message);
2398
2634
  return [4 /*yield*/, this._emitSubject.next(message)];
2399
2635
  case 1:
2636
+ _a.sent();
2637
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2638
+ type: "emit",
2639
+ source: "session",
2640
+ input: {
2641
+ message: message
2642
+ },
2643
+ output: {},
2644
+ context: {
2645
+ swarmName: this.params.swarmName,
2646
+ }
2647
+ })];
2648
+ case 2:
2400
2649
  _a.sent();
2401
2650
  return [2 /*return*/];
2402
2651
  }
@@ -2427,6 +2676,22 @@ var ClientSession = /** @class */ (function () {
2427
2676
  return [4 /*yield*/, outputAwaiter];
2428
2677
  case 2:
2429
2678
  output = _a.sent();
2679
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2680
+ type: "execute",
2681
+ source: "session",
2682
+ input: {
2683
+ message: message,
2684
+ mode: mode,
2685
+ },
2686
+ output: {
2687
+ result: output,
2688
+ },
2689
+ context: {
2690
+ swarmName: this.params.swarmName,
2691
+ }
2692
+ })];
2693
+ case 3:
2694
+ _a.sent();
2430
2695
  return [2 /*return*/, output];
2431
2696
  }
2432
2697
  });
@@ -2438,7 +2703,7 @@ var ClientSession = /** @class */ (function () {
2438
2703
  * @returns {Promise<void>}
2439
2704
  */
2440
2705
  this.commitToolOutput = function (toolId, content) { return __awaiter(_this, void 0, void 0, function () {
2441
- var agent;
2706
+ var agent, result;
2442
2707
  return __generator(this, function (_a) {
2443
2708
  switch (_a.label) {
2444
2709
  case 0:
@@ -2450,7 +2715,23 @@ var ClientSession = /** @class */ (function () {
2450
2715
  case 1:
2451
2716
  agent = _a.sent();
2452
2717
  return [4 /*yield*/, agent.commitToolOutput(toolId, content)];
2453
- case 2: return [2 /*return*/, _a.sent()];
2718
+ case 2:
2719
+ result = _a.sent();
2720
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2721
+ type: "commit-tool-output",
2722
+ source: "session",
2723
+ input: {
2724
+ toolId: toolId,
2725
+ content: content,
2726
+ },
2727
+ output: {},
2728
+ context: {
2729
+ swarmName: this.params.swarmName,
2730
+ }
2731
+ })];
2732
+ case 3:
2733
+ _a.sent();
2734
+ return [2 /*return*/, result];
2454
2735
  }
2455
2736
  });
2456
2737
  }); };
@@ -2460,7 +2741,7 @@ var ClientSession = /** @class */ (function () {
2460
2741
  * @returns {Promise<void>}
2461
2742
  */
2462
2743
  this.commitUserMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
2463
- var agent;
2744
+ var agent, result;
2464
2745
  return __generator(this, function (_a) {
2465
2746
  switch (_a.label) {
2466
2747
  case 0:
@@ -2471,7 +2752,22 @@ var ClientSession = /** @class */ (function () {
2471
2752
  case 1:
2472
2753
  agent = _a.sent();
2473
2754
  return [4 /*yield*/, agent.commitUserMessage(message)];
2474
- case 2: return [2 /*return*/, _a.sent()];
2755
+ case 2:
2756
+ result = _a.sent();
2757
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2758
+ type: "commit-user-message",
2759
+ source: "session",
2760
+ input: {
2761
+ message: message,
2762
+ },
2763
+ output: {},
2764
+ context: {
2765
+ swarmName: this.params.swarmName,
2766
+ }
2767
+ })];
2768
+ case 3:
2769
+ _a.sent();
2770
+ return [2 /*return*/, result];
2475
2771
  }
2476
2772
  });
2477
2773
  }); };
@@ -2480,7 +2776,7 @@ var ClientSession = /** @class */ (function () {
2480
2776
  * @returns {Promise<void>}
2481
2777
  */
2482
2778
  this.commitFlush = function () { return __awaiter(_this, void 0, void 0, function () {
2483
- var agent;
2779
+ var agent, result;
2484
2780
  return __generator(this, function (_a) {
2485
2781
  switch (_a.label) {
2486
2782
  case 0:
@@ -2489,7 +2785,20 @@ var ClientSession = /** @class */ (function () {
2489
2785
  case 1:
2490
2786
  agent = _a.sent();
2491
2787
  return [4 /*yield*/, agent.commitFlush()];
2492
- case 2: return [2 /*return*/, _a.sent()];
2788
+ case 2:
2789
+ result = _a.sent();
2790
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2791
+ type: "commit-flush",
2792
+ source: "session",
2793
+ input: {},
2794
+ output: {},
2795
+ context: {
2796
+ swarmName: this.params.swarmName,
2797
+ }
2798
+ })];
2799
+ case 3:
2800
+ _a.sent();
2801
+ return [2 /*return*/, result];
2493
2802
  }
2494
2803
  });
2495
2804
  }); };
@@ -2499,7 +2808,7 @@ var ClientSession = /** @class */ (function () {
2499
2808
  * @returns {Promise<void>}
2500
2809
  */
2501
2810
  this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
2502
- var agent;
2811
+ var agent, result;
2503
2812
  return __generator(this, function (_a) {
2504
2813
  switch (_a.label) {
2505
2814
  case 0:
@@ -2510,7 +2819,22 @@ var ClientSession = /** @class */ (function () {
2510
2819
  case 1:
2511
2820
  agent = _a.sent();
2512
2821
  return [4 /*yield*/, agent.commitSystemMessage(message)];
2513
- case 2: return [2 /*return*/, _a.sent()];
2822
+ case 2:
2823
+ result = _a.sent();
2824
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
2825
+ type: "commit-system-message",
2826
+ source: "session",
2827
+ input: {
2828
+ message: message
2829
+ },
2830
+ output: {},
2831
+ context: {
2832
+ swarmName: this.params.swarmName,
2833
+ }
2834
+ })];
2835
+ case 3:
2836
+ _a.sent();
2837
+ return [2 /*return*/, result];
2514
2838
  }
2515
2839
  });
2516
2840
  }); };
@@ -2541,6 +2865,15 @@ var ClientSession = /** @class */ (function () {
2541
2865
  }
2542
2866
  });
2543
2867
  }); });
2868
+ _this.params.bus.emit(_this.params.clientId, {
2869
+ type: "connect",
2870
+ source: "session",
2871
+ input: {},
2872
+ output: {},
2873
+ context: {
2874
+ swarmName: _this.params.swarmName,
2875
+ }
2876
+ });
2544
2877
  return function (incoming) { return __awaiter(_this, void 0, void 0, function () {
2545
2878
  var _a;
2546
2879
  var _b;
@@ -2592,6 +2925,7 @@ var SessionConnectionService = /** @class */ (function () {
2592
2925
  function SessionConnectionService() {
2593
2926
  var _this = this;
2594
2927
  this.loggerService = inject(TYPES.loggerService);
2928
+ this.busService = inject(TYPES.busService);
2595
2929
  this.contextService = inject(TYPES.contextService);
2596
2930
  this.swarmConnectionService = inject(TYPES.swarmConnectionService);
2597
2931
  this.swarmSchemaService = inject(TYPES.swarmSchemaService);
@@ -2606,7 +2940,7 @@ var SessionConnectionService = /** @class */ (function () {
2606
2940
  return "".concat(clientId, "-").concat(swarmName);
2607
2941
  }, function (clientId, swarmName) {
2608
2942
  var callbacks = _this.swarmSchemaService.get(swarmName).callbacks;
2609
- return new ClientSession(__assign({ clientId: clientId, logger: _this.loggerService, swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName), swarmName: swarmName }, callbacks));
2943
+ return new ClientSession(__assign({ clientId: clientId, logger: _this.loggerService, bus: _this.busService, swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName), swarmName: swarmName }, callbacks));
2610
2944
  });
2611
2945
  /**
2612
2946
  * Emits a message to the session.
@@ -3542,6 +3876,39 @@ var SwarmPublicService = /** @class */ (function () {
3542
3876
  var _this = this;
3543
3877
  this.loggerService = inject(TYPES.loggerService);
3544
3878
  this.swarmConnectionService = inject(TYPES.swarmConnectionService);
3879
+ /**
3880
+ * Cancel the await of output by emit of empty string
3881
+ * @param {string} clientId - The client ID.
3882
+ * @param {SwarmName} swarmName - The swarm name.
3883
+ * @returns {Promise<void>}
3884
+ */
3885
+ this.cancelOutput = function (clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
3886
+ var _this = this;
3887
+ return __generator(this, function (_a) {
3888
+ switch (_a.label) {
3889
+ case 0:
3890
+ this.loggerService.log("swarmPublicService cancelOutput", {
3891
+ clientId: clientId,
3892
+ swarmName: swarmName,
3893
+ });
3894
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
3895
+ return __generator(this, function (_a) {
3896
+ switch (_a.label) {
3897
+ case 0: return [4 /*yield*/, this.swarmConnectionService.cancelOutput()];
3898
+ case 1: return [2 /*return*/, _a.sent()];
3899
+ }
3900
+ });
3901
+ }); }, {
3902
+ clientId: clientId,
3903
+ swarmName: swarmName,
3904
+ agentName: "",
3905
+ storageName: "",
3906
+ stateName: "",
3907
+ })];
3908
+ case 1: return [2 /*return*/, _a.sent()];
3909
+ }
3910
+ });
3911
+ }); };
3545
3912
  /**
3546
3913
  * Waits for output from the swarm.
3547
3914
  * @param {string} clientId - The client ID.
@@ -4535,6 +4902,23 @@ var ClientStorage = /** @class */ (function () {
4535
4902
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onSearch) {
4536
4903
  (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onSearch(search, indexed, this.params.clientId, this.params.storageName);
4537
4904
  }
4905
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
4906
+ type: "take",
4907
+ source: "storage",
4908
+ input: {
4909
+ search: search,
4910
+ total: total,
4911
+ score: score,
4912
+ },
4913
+ output: {
4914
+ indexed: indexed,
4915
+ },
4916
+ context: {
4917
+ storageName: this.params.storageName,
4918
+ }
4919
+ })];
4920
+ case 3:
4921
+ _c.sent();
4538
4922
  return [2 /*return*/, indexed.take(total, score)];
4539
4923
  }
4540
4924
  });
@@ -4561,6 +4945,19 @@ var ClientStorage = /** @class */ (function () {
4561
4945
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
4562
4946
  (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
4563
4947
  }
4948
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
4949
+ type: "upsert",
4950
+ source: "storage",
4951
+ input: {
4952
+ item: item,
4953
+ },
4954
+ output: {},
4955
+ context: {
4956
+ storageName: this.params.storageName,
4957
+ }
4958
+ })];
4959
+ case 2:
4960
+ _c.sent();
4564
4961
  return [2 /*return*/];
4565
4962
  }
4566
4963
  });
@@ -4573,15 +4970,31 @@ var ClientStorage = /** @class */ (function () {
4573
4970
  this.remove = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
4574
4971
  var _a, _b;
4575
4972
  return __generator(this, function (_c) {
4576
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " remove"), {
4577
- id: itemId,
4578
- });
4579
- this._itemMap.delete(itemId);
4580
- this._createEmbedding.clear(itemId);
4581
- if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
4582
- (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
4973
+ switch (_c.label) {
4974
+ case 0:
4975
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " remove"), {
4976
+ id: itemId,
4977
+ });
4978
+ this._itemMap.delete(itemId);
4979
+ this._createEmbedding.clear(itemId);
4980
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
4981
+ (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
4982
+ }
4983
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
4984
+ type: "remove",
4985
+ source: "storage",
4986
+ input: {
4987
+ itemId: itemId,
4988
+ },
4989
+ output: {},
4990
+ context: {
4991
+ storageName: this.params.storageName,
4992
+ }
4993
+ })];
4994
+ case 1:
4995
+ _c.sent();
4996
+ return [2 /*return*/];
4583
4997
  }
4584
- return [2 /*return*/];
4585
4998
  });
4586
4999
  }); };
4587
5000
  /**
@@ -4590,10 +5003,24 @@ var ClientStorage = /** @class */ (function () {
4590
5003
  */
4591
5004
  this.clear = function () { return __awaiter(_this, void 0, void 0, function () {
4592
5005
  return __generator(this, function (_a) {
4593
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " clear"));
4594
- this._itemMap.clear();
4595
- this._createEmbedding.clear();
4596
- return [2 /*return*/];
5006
+ switch (_a.label) {
5007
+ case 0:
5008
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " clear"));
5009
+ this._itemMap.clear();
5010
+ this._createEmbedding.clear();
5011
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5012
+ type: "clear",
5013
+ source: "storage",
5014
+ input: {},
5015
+ output: {},
5016
+ context: {
5017
+ storageName: this.params.storageName,
5018
+ }
5019
+ })];
5020
+ case 1:
5021
+ _a.sent();
5022
+ return [2 /*return*/];
5023
+ }
4597
5024
  });
4598
5025
  }); };
4599
5026
  /**
@@ -4602,12 +5029,32 @@ var ClientStorage = /** @class */ (function () {
4602
5029
  * @returns {Promise<T | null>} - The item or null if not found.
4603
5030
  */
4604
5031
  this.get = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
5032
+ var result;
4605
5033
  var _a;
4606
5034
  return __generator(this, function (_b) {
4607
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " get"), {
4608
- id: itemId,
4609
- });
4610
- return [2 /*return*/, (_a = this._itemMap.get(itemId)) !== null && _a !== void 0 ? _a : null];
5035
+ switch (_b.label) {
5036
+ case 0:
5037
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " get"), {
5038
+ id: itemId,
5039
+ });
5040
+ result = (_a = this._itemMap.get(itemId)) !== null && _a !== void 0 ? _a : null;
5041
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5042
+ type: "get",
5043
+ source: "storage",
5044
+ input: {
5045
+ itemId: itemId,
5046
+ },
5047
+ output: {
5048
+ result: result,
5049
+ },
5050
+ context: {
5051
+ storageName: this.params.storageName,
5052
+ }
5053
+ })];
5054
+ case 1:
5055
+ _b.sent();
5056
+ return [2 /*return*/, result];
5057
+ }
4611
5058
  });
4612
5059
  }); };
4613
5060
  /**
@@ -4619,27 +5066,43 @@ var ClientStorage = /** @class */ (function () {
4619
5066
  var result, _a, _b, item;
4620
5067
  var e_1, _c;
4621
5068
  return __generator(this, function (_d) {
4622
- this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " list"));
4623
- if (!filter) {
4624
- return [2 /*return*/, __spreadArray([], __read(this._itemMap.values()), false)];
4625
- }
4626
- result = [];
4627
- try {
4628
- for (_a = __values(this._itemMap.values()), _b = _a.next(); !_b.done; _b = _a.next()) {
4629
- item = _b.value;
4630
- if (filter(item)) {
4631
- result.push(item);
5069
+ switch (_d.label) {
5070
+ case 0:
5071
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " list"));
5072
+ if (!filter) {
5073
+ return [2 /*return*/, __spreadArray([], __read(this._itemMap.values()), false)];
4632
5074
  }
4633
- }
4634
- }
4635
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4636
- finally {
4637
- try {
4638
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
4639
- }
4640
- finally { if (e_1) throw e_1.error; }
5075
+ result = [];
5076
+ try {
5077
+ for (_a = __values(this._itemMap.values()), _b = _a.next(); !_b.done; _b = _a.next()) {
5078
+ item = _b.value;
5079
+ if (filter(item)) {
5080
+ result.push(item);
5081
+ }
5082
+ }
5083
+ }
5084
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
5085
+ finally {
5086
+ try {
5087
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
5088
+ }
5089
+ finally { if (e_1) throw e_1.error; }
5090
+ }
5091
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5092
+ type: "list",
5093
+ source: "storage",
5094
+ input: {},
5095
+ output: {
5096
+ result: result,
5097
+ },
5098
+ context: {
5099
+ storageName: this.params.storageName,
5100
+ }
5101
+ })];
5102
+ case 1:
5103
+ _d.sent();
5104
+ return [2 /*return*/, result];
4641
5105
  }
4642
- return [2 /*return*/, result];
4643
5106
  });
4644
5107
  }); };
4645
5108
  /**
@@ -4674,6 +5137,7 @@ var StorageConnectionService = /** @class */ (function () {
4674
5137
  function StorageConnectionService() {
4675
5138
  var _this = this;
4676
5139
  this.loggerService = inject(TYPES.loggerService);
5140
+ this.busService = inject(TYPES.busService);
4677
5141
  this.contextService = inject(TYPES.contextService);
4678
5142
  this.storageSchemaService = inject(TYPES.storageSchemaService);
4679
5143
  this.sessionValidationService = inject(TYPES.sessionValidationService);
@@ -4693,7 +5157,7 @@ var StorageConnectionService = /** @class */ (function () {
4693
5157
  if (!shared) {
4694
5158
  throw new Error("agent-swarm storage not shared storageName=".concat(storageName));
4695
5159
  }
4696
- 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 }));
5160
+ 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 }));
4697
5161
  });
4698
5162
  /**
4699
5163
  * Retrieves a storage instance based on client ID and storage name.
@@ -4711,7 +5175,7 @@ var StorageConnectionService = /** @class */ (function () {
4711
5175
  return _this.getSharedStorage(clientId, storageName);
4712
5176
  }
4713
5177
  var _c = _this.embeddingSchemaService.get(embeddingName), calculateSimilarity = _c.calculateSimilarity, createEmbedding = _c.createEmbedding, embedding = _c.callbacks;
4714
- 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 }));
5178
+ 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 }));
4715
5179
  });
4716
5180
  /**
4717
5181
  * Retrieves a list of storage data based on a search query and total number of items.
@@ -5367,6 +5831,19 @@ var ClientState = /** @class */ (function () {
5367
5831
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onWrite) {
5368
5832
  this.params.callbacks.onWrite(this._state, this.params.clientId, this.params.stateName);
5369
5833
  }
5834
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5835
+ type: "set-state",
5836
+ source: "state",
5837
+ input: {},
5838
+ output: {
5839
+ state: this._state,
5840
+ },
5841
+ context: {
5842
+ stateName: this.params.stateName,
5843
+ }
5844
+ })];
5845
+ case 2:
5846
+ _b.sent();
5370
5847
  return [2 /*return*/, this._state];
5371
5848
  }
5372
5849
  });
@@ -5387,6 +5864,19 @@ var ClientState = /** @class */ (function () {
5387
5864
  if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onRead) {
5388
5865
  this.params.callbacks.onRead(this._state, this.params.clientId, this.params.stateName);
5389
5866
  }
5867
+ return [4 /*yield*/, this.params.bus.emit(this.params.clientId, {
5868
+ type: "get-state",
5869
+ source: "state",
5870
+ input: {},
5871
+ output: {
5872
+ state: this._state,
5873
+ },
5874
+ context: {
5875
+ stateName: this.params.stateName,
5876
+ }
5877
+ })];
5878
+ case 2:
5879
+ _b.sent();
5390
5880
  return [2 /*return*/, this._state];
5391
5881
  }
5392
5882
  });
@@ -5424,6 +5914,7 @@ var StateConnectionService = /** @class */ (function () {
5424
5914
  function StateConnectionService() {
5425
5915
  var _this = this;
5426
5916
  this.loggerService = inject(TYPES.loggerService);
5917
+ this.busService = inject(TYPES.busService);
5427
5918
  this.contextService = inject(TYPES.contextService);
5428
5919
  this.stateSchemaService = inject(TYPES.stateSchemaService);
5429
5920
  this.sessionValidationService = inject(TYPES.sessionValidationService);
@@ -5446,6 +5937,7 @@ var StateConnectionService = /** @class */ (function () {
5446
5937
  clientId: clientId,
5447
5938
  stateName: stateName,
5448
5939
  logger: _this.loggerService,
5940
+ bus: _this.busService,
5449
5941
  setState: setState
5450
5942
  ? functoolsKit.queued(function () {
5451
5943
  var args = [];
@@ -5484,6 +5976,7 @@ var StateConnectionService = /** @class */ (function () {
5484
5976
  clientId: clientId,
5485
5977
  stateName: stateName,
5486
5978
  logger: _this.loggerService,
5979
+ bus: _this.busService,
5487
5980
  setState: setState
5488
5981
  ? functoolsKit.queued(function () {
5489
5982
  var args = [];
@@ -5691,7 +6184,103 @@ var StatePublicService = /** @class */ (function () {
5691
6184
  return StatePublicService;
5692
6185
  }());
5693
6186
 
6187
+ var BusService = /** @class */ (function () {
6188
+ function BusService() {
6189
+ var _this = this;
6190
+ this.loggerService = inject(TYPES.loggerService);
6191
+ this._eventSourceSet = new Set();
6192
+ this.getEventSubject = functoolsKit.memoize(function (_a) {
6193
+ var _b = __read(_a, 2), clientId = _b[0], source = _b[1];
6194
+ return "".concat(clientId, "-").concat(source);
6195
+ }, function () { return new functoolsKit.Subject(); });
6196
+ /**
6197
+ * Subscribes to events for a specific client and source.
6198
+ * @param {string} clientId - The client ID.
6199
+ * @param {EventSource} source - The event source.
6200
+ * @param {(event: T) => void} fn - The callback function to handle the event.
6201
+ * @returns {Subscription} The subscription object.
6202
+ */
6203
+ this.subscribe = function (clientId, source, fn) {
6204
+ _this.loggerService.log("busService subscribe", {
6205
+ clientId: clientId,
6206
+ source: source,
6207
+ });
6208
+ _this._eventSourceSet.add(source);
6209
+ return _this.getEventSubject(clientId, source).subscribe(fn);
6210
+ };
6211
+ /**
6212
+ * Subscribes to a single event for a specific client and source.
6213
+ * @param {string} clientId - The client ID.
6214
+ * @param {EventSource} source - The event source.
6215
+ * @param {(event: T) => boolean} filterFn - The filter function to determine if the event should be handled.
6216
+ * @param {(event: T) => void} fn - The callback function to handle the event.
6217
+ * @returns {Subscription} The subscription object.
6218
+ */
6219
+ this.once = function (clientId, source, filterFn, fn) {
6220
+ _this.loggerService.log("busService once", {
6221
+ clientId: clientId,
6222
+ source: source,
6223
+ });
6224
+ _this._eventSourceSet.add(source);
6225
+ return _this.getEventSubject(clientId, source).filter(filterFn).once(fn);
6226
+ };
6227
+ /**
6228
+ * Emits an event for a specific client.
6229
+ * @param {string} clientId - The client ID.
6230
+ * @param {T} event - The event to emit.
6231
+ * @returns {Promise<void>} A promise that resolves when the event has been emitted.
6232
+ */
6233
+ this.emit = function (clientId, event) { return __awaiter(_this, void 0, void 0, function () {
6234
+ return __generator(this, function (_a) {
6235
+ switch (_a.label) {
6236
+ case 0:
6237
+ this.loggerService.debug("busService emit", {
6238
+ clientId: clientId,
6239
+ event: event,
6240
+ });
6241
+ if (!this.getEventSubject.has("".concat(clientId, "-").concat(event.source))) {
6242
+ return [2 /*return*/];
6243
+ }
6244
+ return [4 /*yield*/, this.getEventSubject(clientId, event.source).next(event)];
6245
+ case 1:
6246
+ _a.sent();
6247
+ return [2 /*return*/];
6248
+ }
6249
+ });
6250
+ }); };
6251
+ /**
6252
+ * Disposes of all event subscriptions for a specific client.
6253
+ * @param {string} clientId - The client ID.
6254
+ */
6255
+ this.dispose = function (clientId) {
6256
+ var e_1, _a;
6257
+ _this.loggerService.log("busService dispose", {
6258
+ clientId: clientId,
6259
+ });
6260
+ try {
6261
+ for (var _b = __values(_this._eventSourceSet), _c = _b.next(); !_c.done; _c = _b.next()) {
6262
+ var source = _c.value;
6263
+ var key = "".concat(clientId, "-").concat(source);
6264
+ if (_this.getEventSubject.has(key)) {
6265
+ _this.getEventSubject(clientId, source).unsubscribeAll();
6266
+ }
6267
+ _this.getEventSubject.clear("".concat(clientId, "-").concat(source));
6268
+ }
6269
+ }
6270
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
6271
+ finally {
6272
+ try {
6273
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6274
+ }
6275
+ finally { if (e_1) throw e_1.error; }
6276
+ }
6277
+ };
6278
+ }
6279
+ return BusService;
6280
+ }());
6281
+
5694
6282
  {
6283
+ provide(TYPES.busService, function () { return new BusService(); });
5695
6284
  provide(TYPES.loggerService, function () { return new LoggerService(); });
5696
6285
  provide(TYPES.contextService, function () { return new ContextService(); });
5697
6286
  }
@@ -5737,6 +6326,7 @@ var StatePublicService = /** @class */ (function () {
5737
6326
  }
5738
6327
 
5739
6328
  var baseServices = {
6329
+ busService: inject(TYPES.busService),
5740
6330
  loggerService: inject(TYPES.loggerService),
5741
6331
  contextService: inject(TYPES.contextService),
5742
6332
  };
@@ -6194,69 +6784,64 @@ var disposeConnection = function (clientId, swarmName) { return __awaiter(void 0
6194
6784
  });
6195
6785
  swarm.swarmValidationService.validate(swarmName, "disposeConnection");
6196
6786
  swarm.sessionValidationService.removeSession(clientId);
6787
+ swarm.busService.dispose(clientId);
6197
6788
  return [4 /*yield*/, swarm.sessionPublicService.dispose(clientId, swarmName)];
6198
6789
  case 1:
6199
6790
  _a.sent();
6200
6791
  return [4 /*yield*/, swarm.swarmPublicService.dispose(clientId, swarmName)];
6201
6792
  case 2:
6202
6793
  _a.sent();
6203
- return [4 /*yield*/, Promise.all([
6204
- swarm.swarmValidationService
6205
- .getAgentList(swarmName)
6206
- .map(function (agentName) { return __awaiter(void 0, void 0, void 0, function () {
6207
- return __generator(this, function (_a) {
6208
- switch (_a.label) {
6209
- case 0: return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
6210
- case 1:
6211
- _a.sent();
6212
- return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
6213
- case 2:
6214
- _a.sent();
6215
- return [2 /*return*/];
6216
- }
6217
- });
6218
- }); }),
6219
- ])];
6794
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
6795
+ .getAgentList(swarmName)
6796
+ .map(function (agentName) { return __awaiter(void 0, void 0, void 0, function () {
6797
+ return __generator(this, function (_a) {
6798
+ switch (_a.label) {
6799
+ case 0: return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
6800
+ case 1:
6801
+ _a.sent();
6802
+ return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
6803
+ case 2:
6804
+ _a.sent();
6805
+ return [2 /*return*/];
6806
+ }
6807
+ });
6808
+ }); }))];
6220
6809
  case 3:
6221
6810
  _a.sent();
6222
- return [4 /*yield*/, Promise.all([
6223
- swarm.swarmValidationService
6224
- .getAgentList(swarmName)
6225
- .flatMap(function (agentName) {
6226
- return swarm.agentValidationService.getStorageList(agentName);
6227
- })
6228
- .filter(function (storageName) { return !!storageName; })
6229
- .map(function (storageName) { return __awaiter(void 0, void 0, void 0, function () {
6230
- return __generator(this, function (_a) {
6231
- switch (_a.label) {
6232
- case 0: return [4 /*yield*/, swarm.storagePublicService.dispose(clientId, storageName)];
6233
- case 1:
6234
- _a.sent();
6235
- return [2 /*return*/];
6236
- }
6237
- });
6238
- }); }),
6239
- ])];
6811
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
6812
+ .getAgentList(swarmName)
6813
+ .flatMap(function (agentName) {
6814
+ return swarm.agentValidationService.getStorageList(agentName);
6815
+ })
6816
+ .filter(function (storageName) { return !!storageName; })
6817
+ .map(function (storageName) { return __awaiter(void 0, void 0, void 0, function () {
6818
+ return __generator(this, function (_a) {
6819
+ switch (_a.label) {
6820
+ case 0: return [4 /*yield*/, swarm.storagePublicService.dispose(clientId, storageName)];
6821
+ case 1:
6822
+ _a.sent();
6823
+ return [2 /*return*/];
6824
+ }
6825
+ });
6826
+ }); }))];
6240
6827
  case 4:
6241
6828
  _a.sent();
6242
- return [4 /*yield*/, Promise.all([
6243
- swarm.swarmValidationService
6244
- .getAgentList(swarmName)
6245
- .flatMap(function (agentName) {
6246
- return swarm.agentValidationService.getStateList(agentName);
6247
- })
6248
- .filter(function (stateName) { return !!stateName; })
6249
- .map(function (stateName) { return __awaiter(void 0, void 0, void 0, function () {
6250
- return __generator(this, function (_a) {
6251
- switch (_a.label) {
6252
- case 0: return [4 /*yield*/, swarm.statePublicService.dispose(clientId, stateName)];
6253
- case 1:
6254
- _a.sent();
6255
- return [2 /*return*/];
6256
- }
6257
- });
6258
- }); }),
6259
- ])];
6829
+ return [4 /*yield*/, Promise.all(swarm.swarmValidationService
6830
+ .getAgentList(swarmName)
6831
+ .flatMap(function (agentName) {
6832
+ return swarm.agentValidationService.getStateList(agentName);
6833
+ })
6834
+ .filter(function (stateName) { return !!stateName; })
6835
+ .map(function (stateName) { return __awaiter(void 0, void 0, void 0, function () {
6836
+ return __generator(this, function (_a) {
6837
+ switch (_a.label) {
6838
+ case 0: return [4 /*yield*/, swarm.statePublicService.dispose(clientId, stateName)];
6839
+ case 1:
6840
+ _a.sent();
6841
+ return [2 /*return*/];
6842
+ }
6843
+ });
6844
+ }); }))];
6260
6845
  case 5:
6261
6846
  _a.sent();
6262
6847
  return [4 /*yield*/, HistoryAdapter.dispose(clientId, null)];
@@ -6924,6 +7509,35 @@ var executeForce = function (content, clientId) { return __awaiter(void 0, void
6924
7509
  });
6925
7510
  }); };
6926
7511
 
7512
+ var DISALLOWED_EVENT_SOURCE_LIST$1 = new Set([
7513
+ "agent",
7514
+ "history",
7515
+ "session",
7516
+ "state",
7517
+ "storage",
7518
+ "swarm",
7519
+ ]);
7520
+ /**
7521
+ * Listens for an event on the swarm bus service and executes a callback function when the event is received.
7522
+ *
7523
+ * @template T - The type of the data payload.
7524
+ * @param {string} clientId - The ID of the client to listen for events from.
7525
+ * @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.
7526
+ * @returns {void} - Returns nothing.
7527
+ */
7528
+ var listenEvent = function (clientId, topicName, fn) {
7529
+ swarm.loggerService.log("function listenEvent", {
7530
+ clientId: clientId,
7531
+ });
7532
+ if (DISALLOWED_EVENT_SOURCE_LIST$1.has(topicName)) {
7533
+ throw new Error("agent-swarm listenEvent topic is reserved topicName=".concat(topicName));
7534
+ }
7535
+ return swarm.busService.subscribe(clientId, topicName, function (_a) {
7536
+ var payload = _a.payload;
7537
+ return fn(payload);
7538
+ });
7539
+ };
7540
+
6927
7541
  /**
6928
7542
  * Retrieves the last message sent by the user from the client's message history.
6929
7543
  *
@@ -7106,6 +7720,184 @@ var makeAutoDispose = function (clientId, swarmName, _a) {
7106
7720
  };
7107
7721
  };
7108
7722
 
7723
+ var DISALLOWED_EVENT_SOURCE_LIST = new Set([
7724
+ "agent",
7725
+ "history",
7726
+ "session",
7727
+ "state",
7728
+ "storage",
7729
+ "swarm",
7730
+ ]);
7731
+ /**
7732
+ * Emits an event to the swarm bus service.
7733
+ *
7734
+ * @template T - The type of the payload.
7735
+ * @param {string} clientId - The ID of the client emitting the event.
7736
+ * @param {T} payload - The payload of the event.
7737
+ * @returns {boolean} - Returns true if the event was successfully emitted.
7738
+ */
7739
+ var event = function (clientId, topicName, payload) {
7740
+ swarm.loggerService.log("function listenEvent", {
7741
+ clientId: clientId,
7742
+ });
7743
+ if (DISALLOWED_EVENT_SOURCE_LIST.has(topicName)) {
7744
+ throw new Error("agent-swarm event topic is reserved topicName=".concat(topicName));
7745
+ }
7746
+ return swarm.busService.emit(clientId, {
7747
+ source: topicName,
7748
+ payload: payload,
7749
+ });
7750
+ };
7751
+
7752
+ /**
7753
+ * Cancel the await of output by emit of empty string
7754
+ *
7755
+ * @param {string} clientId - The ID of the client.
7756
+ * @param {string} agentName - The name of the agent.
7757
+ * @returns {Promise<void>} - A promise that resolves when the output is canceled
7758
+ */
7759
+ var cancelOutput = function (clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
7760
+ var swarmName, currentAgentName;
7761
+ return __generator(this, function (_a) {
7762
+ switch (_a.label) {
7763
+ case 0:
7764
+ swarm.loggerService.log('function cancelOutput', {
7765
+ clientId: clientId,
7766
+ agentName: agentName,
7767
+ });
7768
+ swarm.agentValidationService.validate(agentName, "cancelOutput");
7769
+ swarm.sessionValidationService.validate(clientId, "cancelOutput");
7770
+ swarmName = swarm.sessionValidationService.getSwarm(clientId);
7771
+ swarm.swarmValidationService.validate(swarmName, "cancelOutput");
7772
+ return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
7773
+ case 1:
7774
+ currentAgentName = _a.sent();
7775
+ if (currentAgentName !== agentName) {
7776
+ swarm.loggerService.log('function "cancelOutput" skipped due to the agent change', {
7777
+ currentAgentName: currentAgentName,
7778
+ agentName: agentName,
7779
+ clientId: clientId,
7780
+ });
7781
+ return [2 /*return*/];
7782
+ }
7783
+ return [4 /*yield*/, swarm.swarmPublicService.cancelOutput(clientId, swarmName)];
7784
+ case 2:
7785
+ _a.sent();
7786
+ return [2 /*return*/];
7787
+ }
7788
+ });
7789
+ }); };
7790
+
7791
+ /**
7792
+ * Cancel the await of output by emit of empty string without checking active agent
7793
+ *
7794
+ * @param {string} clientId - The ID of the client.
7795
+ * @param {string} agentName - The name of the agent.
7796
+ * @returns {Promise<void>} - A promise that resolves when the output is canceled
7797
+ */
7798
+ var cancelOutputForce = function (clientId) { return __awaiter(void 0, void 0, void 0, function () {
7799
+ var swarmName;
7800
+ return __generator(this, function (_a) {
7801
+ switch (_a.label) {
7802
+ case 0:
7803
+ swarm.loggerService.log('function cancelOutputForce', {
7804
+ clientId: clientId,
7805
+ });
7806
+ swarm.sessionValidationService.validate(clientId, "cancelOutputForce");
7807
+ swarmName = swarm.sessionValidationService.getSwarm(clientId);
7808
+ swarm.swarmValidationService.validate(swarmName, "cancelOutputForce");
7809
+ return [4 /*yield*/, swarm.swarmPublicService.cancelOutput(clientId, swarmName)];
7810
+ case 1:
7811
+ _a.sent();
7812
+ return [2 /*return*/];
7813
+ }
7814
+ });
7815
+ }); };
7816
+
7817
+ /**
7818
+ * Hook to subscribe to agent events for a specific client.
7819
+ *
7820
+ * @param {string} clientId - The ID of the client to subscribe to events for.
7821
+ * @param {function} fn - The callback function to handle the event.
7822
+ * @returns {function} - A function to unsubscribe from the event.
7823
+ */
7824
+ var listenAgentEvent = function (clientId, fn) {
7825
+ swarm.loggerService.log("middleware listenAgentEvent", {
7826
+ clientId: clientId,
7827
+ });
7828
+ return swarm.busService.subscribe(clientId, "agent", fn);
7829
+ };
7830
+
7831
+ /**
7832
+ * Hook to subscribe to history events for a specific client.
7833
+ *
7834
+ * @param {string} clientId - The ID of the client to subscribe to.
7835
+ * @param {(event: IBusEvent) => void} fn - The callback function to handle the event.
7836
+ * @returns {Function} - The unsubscribe function.
7837
+ */
7838
+ var listenHistoryEvent = function (clientId, fn) {
7839
+ swarm.loggerService.log("middleware listenHistoryEvent", {
7840
+ clientId: clientId,
7841
+ });
7842
+ return swarm.busService.subscribe(clientId, "history", fn);
7843
+ };
7844
+
7845
+ /**
7846
+ * Hook to subscribe to session events for a specific client.
7847
+ *
7848
+ * @param {string} clientId - The ID of the client to subscribe to session events for.
7849
+ * @param {function} fn - The callback function to handle the session events.
7850
+ * @returns {function} - The unsubscribe function to stop listening to session events.
7851
+ */
7852
+ var listenSessionEvent = function (clientId, fn) {
7853
+ swarm.loggerService.log("middleware listenSessionEvent", {
7854
+ clientId: clientId,
7855
+ });
7856
+ return swarm.busService.subscribe(clientId, "session", fn);
7857
+ };
7858
+
7859
+ /**
7860
+ * Hook to subscribe to state events for a specific client.
7861
+ *
7862
+ * @param {string} clientId - The ID of the client to subscribe to.
7863
+ * @param {function} fn - The callback function to handle the event.
7864
+ * @returns {function} - The unsubscribe function to stop listening to the events.
7865
+ */
7866
+ var listenStateEvent = function (clientId, fn) {
7867
+ swarm.loggerService.log("middleware listenStateEvent", {
7868
+ clientId: clientId,
7869
+ });
7870
+ return swarm.busService.subscribe(clientId, "state", fn);
7871
+ };
7872
+
7873
+ /**
7874
+ * Hook to subscribe to storage events for a specific client.
7875
+ *
7876
+ * @param {string} clientId - The ID of the client to subscribe to storage events for.
7877
+ * @param {function} fn - The callback function to handle the storage event.
7878
+ * @returns {function} - A function to unsubscribe from the storage events.
7879
+ */
7880
+ var listenStorageEvent = function (clientId, fn) {
7881
+ swarm.loggerService.log("middleware listenStorageEvent", {
7882
+ clientId: clientId,
7883
+ });
7884
+ return swarm.busService.subscribe(clientId, "storage", fn);
7885
+ };
7886
+
7887
+ /**
7888
+ * Hook to subscribe to swarm events for a specific client.
7889
+ *
7890
+ * @param {string} clientId - The ID of the client to subscribe to events for.
7891
+ * @param {(event: IBusEvent) => void} fn - The callback function to handle the event.
7892
+ * @returns {Function} - A function to unsubscribe from the event.
7893
+ */
7894
+ var listenSwarmEvent = function (clientId, fn) {
7895
+ swarm.loggerService.log("middleware listenSwarmEvent", {
7896
+ clientId: clientId,
7897
+ });
7898
+ return swarm.busService.subscribe(clientId, "swarm", fn);
7899
+ };
7900
+
7109
7901
  var StorageUtils = /** @class */ (function () {
7110
7902
  function StorageUtils() {
7111
7903
  var _this = this;
@@ -7362,6 +8154,8 @@ exports.addState = addState;
7362
8154
  exports.addStorage = addStorage;
7363
8155
  exports.addSwarm = addSwarm;
7364
8156
  exports.addTool = addTool;
8157
+ exports.cancelOutput = cancelOutput;
8158
+ exports.cancelOutputForce = cancelOutputForce;
7365
8159
  exports.changeAgent = changeAgent;
7366
8160
  exports.commitFlush = commitFlush;
7367
8161
  exports.commitFlushForce = commitFlushForce;
@@ -7375,6 +8169,7 @@ exports.complete = complete;
7375
8169
  exports.disposeConnection = disposeConnection;
7376
8170
  exports.emit = emit;
7377
8171
  exports.emitForce = emitForce;
8172
+ exports.event = event;
7378
8173
  exports.execute = execute;
7379
8174
  exports.executeForce = executeForce;
7380
8175
  exports.getAgentHistory = getAgentHistory;
@@ -7386,6 +8181,13 @@ exports.getLastUserMessage = getLastUserMessage;
7386
8181
  exports.getRawHistory = getRawHistory;
7387
8182
  exports.getSessionMode = getSessionMode;
7388
8183
  exports.getUserHistory = getUserHistory;
8184
+ exports.listenAgentEvent = listenAgentEvent;
8185
+ exports.listenEvent = listenEvent;
8186
+ exports.listenHistoryEvent = listenHistoryEvent;
8187
+ exports.listenSessionEvent = listenSessionEvent;
8188
+ exports.listenStateEvent = listenStateEvent;
8189
+ exports.listenStorageEvent = listenStorageEvent;
8190
+ exports.listenSwarmEvent = listenSwarmEvent;
7389
8191
  exports.makeAutoDispose = makeAutoDispose;
7390
8192
  exports.makeConnection = makeConnection;
7391
8193
  exports.session = session;