agent-swarm-kit 1.0.49 → 1.0.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/build/index.cjs +1247 -131
  2. package/build/index.mjs +1246 -133
  3. package/package.json +2 -2
  4. package/types.d.ts +387 -13
package/build/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { scoped } from 'di-scoped';
2
- import { ToolRegistry, trycatch, sleep, PubsubArrayAdapter, Subject, queued, not, randomString, memoize, createAwaiter, cancelable, CANCELED_PROMISE_SYMBOL, schedule, ttl, singleshot, Source } from 'functools-kit';
2
+ import { ToolRegistry, trycatch, sleep, PubsubArrayAdapter, Subject, queued, not, randomString, memoize, createAwaiter, cancelable, CANCELED_PROMISE_SYMBOL, singleshot, execpool, SortedArray, schedule, ttl, Source } from 'functools-kit';
3
3
  import { createActivator } from 'di-kit';
4
4
  import { omit } from 'lodash-es';
5
5
  import xml2js from 'xml2js';
@@ -216,18 +216,22 @@ var connectionServices$1 = {
216
216
  historyConnectionService: Symbol('historyConnectionService'),
217
217
  swarmConnectionService: Symbol('swarmConnectionService'),
218
218
  sessionConnectionService: Symbol('sessionConnectionService'),
219
+ storageConnectionService: Symbol('storageConnectionService'),
219
220
  };
220
- var specServices$1 = {
221
+ var schemaServices$1 = {
221
222
  completionSchemaService: Symbol('completionSchemaService'),
222
223
  agentSchemaService: Symbol('agentSchemaService'),
223
224
  swarmSchemaService: Symbol('swarmSchemaService'),
224
225
  toolSchemaService: Symbol('toolSchemaService'),
226
+ embeddingSchemaService: Symbol('embeddingSchemaService'),
227
+ storageSchemaService: Symbol('storageSchemaService'),
225
228
  };
226
229
  var publicServices$1 = {
227
230
  agentPublicService: Symbol('agentPublicService'),
228
231
  historyPublicService: Symbol('historyPublicService'),
229
232
  sessionPublicService: Symbol('sessionPublicService'),
230
233
  swarmPublicService: Symbol('swarmPublicService'),
234
+ storagePublicService: Symbol('storagePublicService'),
231
235
  };
232
236
  var validationServices$1 = {
233
237
  agentValidationService: Symbol('agentValidationService'),
@@ -235,8 +239,10 @@ var validationServices$1 = {
235
239
  sessionValidationService: Symbol('sessionValidationService'),
236
240
  swarmValidationService: Symbol('swarmValidationService'),
237
241
  completionValidationService: Symbol('completionValidationService'),
242
+ embeddingValidationService: Symbol('embeddingValidationService'),
243
+ storageValidationService: Symbol('storageValidationService'),
238
244
  };
239
- var TYPES = __assign(__assign(__assign(__assign(__assign({}, baseServices$1), specServices$1), connectionServices$1), publicServices$1), validationServices$1);
245
+ var TYPES = __assign(__assign(__assign(__assign(__assign({}, baseServices$1), schemaServices$1), connectionServices$1), publicServices$1), validationServices$1);
240
246
 
241
247
  /**
242
248
  * Service for managing agent schemas.
@@ -428,6 +434,8 @@ var CC_GET_AGENT_HISTORY = function () { return new PubsubArrayAdapter(); };
428
434
  var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
429
435
  var CC_AGENT_SEPARATE_HISTORY = false;
430
436
  var CC_AGENT_SYSTEM_PROMPT = undefined;
437
+ var CC_STORAGE_SEARCH_SIMILARITY = 0.65;
438
+ var CC_STORAGE_SEARCH_POOL = 5;
431
439
  var GLOBAL_CONFIG = {
432
440
  CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
433
441
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
@@ -443,6 +451,8 @@ var GLOBAL_CONFIG = {
443
451
  CC_AGENT_SEPARATE_HISTORY: CC_AGENT_SEPARATE_HISTORY,
444
452
  CC_AGENT_DISALLOWED_TAGS: CC_AGENT_DISALLOWED_TAGS,
445
453
  CC_AGENT_DISALLOWED_SYMBOLS: CC_AGENT_DISALLOWED_SYMBOLS,
454
+ CC_STORAGE_SEARCH_SIMILARITY: CC_STORAGE_SEARCH_SIMILARITY,
455
+ CC_STORAGE_SEARCH_POOL: CC_STORAGE_SEARCH_POOL,
446
456
  };
447
457
  var setConfig = function (config) {
448
458
  Object.assign(GLOBAL_CONFIG, config);
@@ -463,6 +473,7 @@ var ClientAgent = /** @class */ (function () {
463
473
  function ClientAgent(params) {
464
474
  var _this = this;
465
475
  this.params = params;
476
+ this._toolErrorSubject = new Subject();
466
477
  this._toolCommitSubject = new Subject();
467
478
  this._outputSubject = new Subject();
468
479
  /**
@@ -808,14 +819,22 @@ var ClientAgent = /** @class */ (function () {
808
819
  params: tool.function.arguments,
809
820
  isLast: idx === toolCalls.length - 1,
810
821
  toolCalls: toolCalls,
811
- })).then(function () {
822
+ }))
823
+ .then(function () {
812
824
  var _a, _b;
813
825
  ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onAfterCall) &&
814
826
  ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onAfterCall(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments));
827
+ })
828
+ .catch(function (error) {
829
+ var _a, _b;
830
+ ((_a = targetFn.callbacks) === null || _a === void 0 ? void 0 : _a.onCallError) &&
831
+ ((_b = targetFn.callbacks) === null || _b === void 0 ? void 0 : _b.onCallError(tool.id, _this.params.clientId, _this.params.agentName, tool.function.arguments, error));
832
+ _this._toolErrorSubject.next();
815
833
  });
816
834
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
817
835
  return [4 /*yield*/, Promise.race([
818
836
  this_1._toolCommitSubject.toPromise(),
837
+ this_1._toolErrorSubject.toPromise(),
819
838
  this_1._outputSubject.toPromise(),
820
839
  ])];
821
840
  case 8:
@@ -1035,16 +1054,21 @@ var AgentConnectionService = /** @class */ (function () {
1035
1054
  * @returns {Promise<void>} The dispose result.
1036
1055
  */
1037
1056
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
1057
+ var key;
1038
1058
  return __generator(this, function (_a) {
1039
1059
  switch (_a.label) {
1040
1060
  case 0:
1041
1061
  this.loggerService.log("agentConnectionService dispose", {
1042
1062
  context: this.contextService.context,
1043
1063
  });
1064
+ key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName);
1065
+ if (!this.getAgent.has(key)) {
1066
+ return [2 /*return*/];
1067
+ }
1044
1068
  return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).dispose()];
1045
1069
  case 1:
1046
1070
  _a.sent();
1047
- this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
1071
+ this.getAgent.clear(key);
1048
1072
  this.sessionValidationService.removeAgentUsage(this.contextService.context.clientId, this.contextService.context.agentName);
1049
1073
  return [2 /*return*/];
1050
1074
  }
@@ -1139,7 +1163,7 @@ var ClientHistory = /** @class */ (function () {
1139
1163
  * @returns {Promise<IModelMessage[]>} - The array of messages for the agent.
1140
1164
  */
1141
1165
  this.toArrayForAgent = function (prompt, system) { return __awaiter(_this, void 0, void 0, function () {
1142
- var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, assistantToolCallSet, assistantMessages, promptMessages;
1166
+ var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, assistantToolOutputCallSet, assistantRawMessages, assistantToolCallSet, assistantMessages, promptMessages;
1143
1167
  var _this = this;
1144
1168
  var _d, e_2, _e, _f;
1145
1169
  var _g;
@@ -1206,22 +1230,41 @@ var ClientHistory = /** @class */ (function () {
1206
1230
  commonMessages = commonMessagesRaw
1207
1231
  .filter(this._filterCondition)
1208
1232
  .slice(-GLOBAL_CONFIG.CC_KEEP_MESSAGES);
1209
- assistantToolCallSet = new Set(commonMessages.map(function (_a) {
1233
+ assistantToolOutputCallSet = new Set(commonMessages.map(function (_a) {
1210
1234
  var tool_call_id = _a.tool_call_id;
1211
1235
  return tool_call_id;
1212
1236
  }));
1213
- assistantMessages = commonMessages
1237
+ assistantRawMessages = commonMessages
1214
1238
  .map(function (_a) {
1215
1239
  var tool_calls = _a.tool_calls, message = __rest(_a, ["tool_calls"]);
1216
1240
  return (__assign(__assign({}, message), { tool_calls: tool_calls === null || tool_calls === void 0 ? void 0 : tool_calls.filter(function (_a) {
1217
1241
  var id = _a.id;
1218
- return assistantToolCallSet.has(id);
1242
+ return assistantToolOutputCallSet.has(id);
1219
1243
  }) }));
1220
1244
  })
1221
1245
  .filter(function (_a) {
1222
1246
  var content = _a.content, tool_calls = _a.tool_calls;
1223
1247
  return !!content || !!(tool_calls === null || tool_calls === void 0 ? void 0 : tool_calls.length);
1224
1248
  });
1249
+ assistantToolCallSet = new Set(assistantRawMessages
1250
+ .filter(function (_a) {
1251
+ var role = _a.role;
1252
+ return role === "tool";
1253
+ })
1254
+ .flatMap(function (_a) {
1255
+ var tool_calls = _a.tool_calls;
1256
+ return tool_calls === null || tool_calls === void 0 ? void 0 : tool_calls.map(function (_a) {
1257
+ var id = _a.id;
1258
+ return id;
1259
+ });
1260
+ }));
1261
+ assistantMessages = assistantRawMessages.filter(function (_a) {
1262
+ var role = _a.role, tool_call_id = _a.tool_call_id;
1263
+ if (role === "tool") {
1264
+ return assistantToolCallSet.has(tool_call_id);
1265
+ }
1266
+ return true;
1267
+ });
1225
1268
  promptMessages = [];
1226
1269
  {
1227
1270
  promptMessages.push({
@@ -1354,12 +1397,17 @@ var HistoryConnectionService = /** @class */ (function () {
1354
1397
  * @returns {Promise<void>} A promise that resolves when the service is disposed.
1355
1398
  */
1356
1399
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
1400
+ var key;
1357
1401
  return __generator(this, function (_a) {
1358
1402
  switch (_a.label) {
1359
1403
  case 0:
1360
1404
  this.loggerService.log("historyConnectionService dispose", {
1361
1405
  context: this.contextService.context,
1362
1406
  });
1407
+ key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName);
1408
+ if (!this.getHistory.has(key)) {
1409
+ return [2 /*return*/];
1410
+ }
1363
1411
  if (!GLOBAL_CONFIG.CC_AGENT_SEPARATE_HISTORY) return [3 /*break*/, 2];
1364
1412
  return [4 /*yield*/, this.getItems(this.contextService.context.clientId, this.contextService.context.agentName).clear()];
1365
1413
  case 1:
@@ -1367,7 +1415,7 @@ var HistoryConnectionService = /** @class */ (function () {
1367
1415
  this.getItems.clear(this.contextService.context.clientId);
1368
1416
  _a.label = 2;
1369
1417
  case 2:
1370
- this.getHistory.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
1418
+ this.getHistory.clear(key);
1371
1419
  this.sessionValidationService.removeHistoryUsage(this.contextService.context.clientId, this.contextService.context.agentName);
1372
1420
  return [2 /*return*/];
1373
1421
  }
@@ -1626,6 +1674,7 @@ var SwarmConnectionService = /** @class */ (function () {
1626
1674
  });
1627
1675
  });
1628
1676
  },
1677
+ callbacks: callbacks,
1629
1678
  });
1630
1679
  });
1631
1680
  /**
@@ -1717,11 +1766,16 @@ var SwarmConnectionService = /** @class */ (function () {
1717
1766
  * @returns {Promise<void>}
1718
1767
  */
1719
1768
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
1769
+ var key;
1720
1770
  return __generator(this, function (_a) {
1721
1771
  this.loggerService.log("swarmConnectionService dispose", {
1722
1772
  context: this.contextService.context,
1723
1773
  });
1724
- this.getSwarm.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
1774
+ key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName);
1775
+ if (!this.getSwarm.has(key)) {
1776
+ return [2 /*return*/];
1777
+ }
1778
+ this.getSwarm.clear(key);
1725
1779
  return [2 /*return*/];
1726
1780
  });
1727
1781
  }); };
@@ -1769,7 +1823,7 @@ var CompletionSchemaService = /** @class */ (function () {
1769
1823
  this.registry = new ToolRegistry("completionSchemaService");
1770
1824
  /**
1771
1825
  * Registers a new completion schema.
1772
- * @param {string} key - The key for the schema.
1826
+ * @param {CompletionName} key - The key for the schema.
1773
1827
  * @param {ICompletionSchema} value - The schema to register.
1774
1828
  */
1775
1829
  this.register = function (key, value) {
@@ -1778,7 +1832,7 @@ var CompletionSchemaService = /** @class */ (function () {
1778
1832
  };
1779
1833
  /**
1780
1834
  * Retrieves a completion schema by key.
1781
- * @param {string} key - The key of the schema to retrieve.
1835
+ * @param {CompletionName} key - The key of the schema to retrieve.
1782
1836
  * @returns {ICompletionSchema} The retrieved schema.
1783
1837
  */
1784
1838
  this.get = function (key) {
@@ -2153,16 +2207,21 @@ var SessionConnectionService = /** @class */ (function () {
2153
2207
  * @returns {Promise<void>} A promise that resolves when the service is disposed.
2154
2208
  */
2155
2209
  this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
2210
+ var key;
2156
2211
  return __generator(this, function (_a) {
2157
2212
  switch (_a.label) {
2158
2213
  case 0:
2159
2214
  this.loggerService.log("sessionConnectionService dispose", {
2160
2215
  context: this.contextService.context,
2161
2216
  });
2217
+ key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName);
2218
+ if (!this.getSession.has(key)) {
2219
+ return [2 /*return*/];
2220
+ }
2162
2221
  return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).dispose()];
2163
2222
  case 1:
2164
2223
  _a.sent();
2165
- this.getSession.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
2224
+ this.getSession.clear(key);
2166
2225
  return [2 /*return*/];
2167
2226
  }
2168
2227
  });
@@ -2205,6 +2264,7 @@ var AgentPublicService = /** @class */ (function () {
2205
2264
  clientId: clientId,
2206
2265
  agentName: agentName,
2207
2266
  swarmName: "",
2267
+ storageName: "",
2208
2268
  })];
2209
2269
  case 1: return [2 /*return*/, _a.sent()];
2210
2270
  }
@@ -2239,6 +2299,7 @@ var AgentPublicService = /** @class */ (function () {
2239
2299
  clientId: clientId,
2240
2300
  agentName: agentName,
2241
2301
  swarmName: "",
2302
+ storageName: "",
2242
2303
  })];
2243
2304
  case 1: return [2 /*return*/, _a.sent()];
2244
2305
  }
@@ -2270,6 +2331,7 @@ var AgentPublicService = /** @class */ (function () {
2270
2331
  clientId: clientId,
2271
2332
  agentName: agentName,
2272
2333
  swarmName: "",
2334
+ storageName: "",
2273
2335
  })];
2274
2336
  case 1: return [2 /*return*/, _a.sent()];
2275
2337
  }
@@ -2305,6 +2367,7 @@ var AgentPublicService = /** @class */ (function () {
2305
2367
  clientId: clientId,
2306
2368
  agentName: agentName,
2307
2369
  swarmName: "",
2370
+ storageName: "",
2308
2371
  })];
2309
2372
  case 1: return [2 /*return*/, _a.sent()];
2310
2373
  }
@@ -2338,6 +2401,7 @@ var AgentPublicService = /** @class */ (function () {
2338
2401
  clientId: clientId,
2339
2402
  agentName: agentName,
2340
2403
  swarmName: "",
2404
+ storageName: "",
2341
2405
  })];
2342
2406
  case 1: return [2 /*return*/, _a.sent()];
2343
2407
  }
@@ -2371,6 +2435,7 @@ var AgentPublicService = /** @class */ (function () {
2371
2435
  clientId: clientId,
2372
2436
  agentName: agentName,
2373
2437
  swarmName: "",
2438
+ storageName: "",
2374
2439
  })];
2375
2440
  case 1: return [2 /*return*/, _a.sent()];
2376
2441
  }
@@ -2402,6 +2467,7 @@ var AgentPublicService = /** @class */ (function () {
2402
2467
  clientId: clientId,
2403
2468
  agentName: agentName,
2404
2469
  swarmName: "",
2470
+ storageName: "",
2405
2471
  })];
2406
2472
  case 1: return [2 /*return*/, _a.sent()];
2407
2473
  }
@@ -2433,6 +2499,7 @@ var AgentPublicService = /** @class */ (function () {
2433
2499
  clientId: clientId,
2434
2500
  agentName: agentName,
2435
2501
  swarmName: "",
2502
+ storageName: "",
2436
2503
  })];
2437
2504
  case 1: return [2 /*return*/, _a.sent()];
2438
2505
  }
@@ -2478,6 +2545,7 @@ var HistoryPublicService = /** @class */ (function () {
2478
2545
  clientId: clientId,
2479
2546
  agentName: agentName,
2480
2547
  swarmName: "",
2548
+ storageName: "",
2481
2549
  })];
2482
2550
  case 1: return [2 /*return*/, _a.sent()];
2483
2551
  }
@@ -2511,6 +2579,7 @@ var HistoryPublicService = /** @class */ (function () {
2511
2579
  clientId: clientId,
2512
2580
  agentName: agentName,
2513
2581
  swarmName: "",
2582
+ storageName: "",
2514
2583
  })];
2515
2584
  case 1: return [2 /*return*/, _a.sent()];
2516
2585
  }
@@ -2542,6 +2611,7 @@ var HistoryPublicService = /** @class */ (function () {
2542
2611
  clientId: clientId,
2543
2612
  agentName: agentName,
2544
2613
  swarmName: "",
2614
+ storageName: "",
2545
2615
  })];
2546
2616
  case 1: return [2 /*return*/, _a.sent()];
2547
2617
  }
@@ -2573,6 +2643,7 @@ var HistoryPublicService = /** @class */ (function () {
2573
2643
  clientId: clientId,
2574
2644
  agentName: agentName,
2575
2645
  swarmName: "",
2646
+ storageName: "",
2576
2647
  })];
2577
2648
  case 1: return [2 /*return*/, _a.sent()];
2578
2649
  }
@@ -2618,6 +2689,7 @@ var SessionPublicService = /** @class */ (function () {
2618
2689
  clientId: clientId,
2619
2690
  swarmName: swarmName,
2620
2691
  agentName: "",
2692
+ storageName: "",
2621
2693
  })];
2622
2694
  case 1: return [2 /*return*/, _a.sent()];
2623
2695
  }
@@ -2652,6 +2724,7 @@ var SessionPublicService = /** @class */ (function () {
2652
2724
  clientId: clientId,
2653
2725
  swarmName: swarmName,
2654
2726
  agentName: "",
2727
+ storageName: "",
2655
2728
  })];
2656
2729
  case 1: return [2 /*return*/, _a.sent()];
2657
2730
  }
@@ -2685,6 +2758,7 @@ var SessionPublicService = /** @class */ (function () {
2685
2758
  clientId: clientId,
2686
2759
  swarmName: swarmName,
2687
2760
  agentName: "",
2761
+ storageName: "",
2688
2762
  })];
2689
2763
  case 1: return [2 /*return*/, _a.sent()];
2690
2764
  }
@@ -2697,12 +2771,14 @@ var SessionPublicService = /** @class */ (function () {
2697
2771
  clientId: clientId,
2698
2772
  swarmName: swarmName,
2699
2773
  agentName: "",
2774
+ storageName: "",
2700
2775
  });
2701
2776
  };
2702
2777
  }, {
2703
2778
  clientId: clientId,
2704
2779
  swarmName: swarmName,
2705
2780
  agentName: "",
2781
+ storageName: "",
2706
2782
  });
2707
2783
  };
2708
2784
  /**
@@ -2735,6 +2811,7 @@ var SessionPublicService = /** @class */ (function () {
2735
2811
  clientId: clientId,
2736
2812
  swarmName: swarmName,
2737
2813
  agentName: "",
2814
+ storageName: "",
2738
2815
  })];
2739
2816
  case 1: return [2 /*return*/, _a.sent()];
2740
2817
  }
@@ -2768,6 +2845,7 @@ var SessionPublicService = /** @class */ (function () {
2768
2845
  clientId: clientId,
2769
2846
  swarmName: swarmName,
2770
2847
  agentName: "",
2848
+ storageName: "",
2771
2849
  })];
2772
2850
  case 1: return [2 /*return*/, _a.sent()];
2773
2851
  }
@@ -2801,6 +2879,7 @@ var SessionPublicService = /** @class */ (function () {
2801
2879
  clientId: clientId,
2802
2880
  swarmName: swarmName,
2803
2881
  agentName: "",
2882
+ storageName: "",
2804
2883
  })];
2805
2884
  case 1: return [2 /*return*/, _a.sent()];
2806
2885
  }
@@ -2832,6 +2911,7 @@ var SessionPublicService = /** @class */ (function () {
2832
2911
  clientId: clientId,
2833
2912
  swarmName: swarmName,
2834
2913
  agentName: "",
2914
+ storageName: "",
2835
2915
  })];
2836
2916
  case 1: return [2 /*return*/, _a.sent()];
2837
2917
  }
@@ -2863,6 +2943,7 @@ var SessionPublicService = /** @class */ (function () {
2863
2943
  clientId: clientId,
2864
2944
  swarmName: swarmName,
2865
2945
  agentName: "",
2946
+ storageName: "",
2866
2947
  })];
2867
2948
  case 1: return [2 /*return*/, _a.sent()];
2868
2949
  }
@@ -2906,6 +2987,7 @@ var SwarmPublicService = /** @class */ (function () {
2906
2987
  clientId: clientId,
2907
2988
  swarmName: swarmName,
2908
2989
  agentName: "",
2990
+ storageName: "",
2909
2991
  })];
2910
2992
  case 1: return [2 /*return*/, _a.sent()];
2911
2993
  }
@@ -2937,6 +3019,7 @@ var SwarmPublicService = /** @class */ (function () {
2937
3019
  clientId: clientId,
2938
3020
  swarmName: swarmName,
2939
3021
  agentName: "",
3022
+ storageName: "",
2940
3023
  })];
2941
3024
  case 1: return [2 /*return*/, _a.sent()];
2942
3025
  }
@@ -2968,6 +3051,7 @@ var SwarmPublicService = /** @class */ (function () {
2968
3051
  clientId: clientId,
2969
3052
  swarmName: swarmName,
2970
3053
  agentName: "",
3054
+ storageName: "",
2971
3055
  })];
2972
3056
  case 1: return [2 /*return*/, _a.sent()];
2973
3057
  }
@@ -3003,6 +3087,7 @@ var SwarmPublicService = /** @class */ (function () {
3003
3087
  clientId: clientId,
3004
3088
  swarmName: swarmName,
3005
3089
  agentName: "",
3090
+ storageName: "",
3006
3091
  })];
3007
3092
  case 1: return [2 /*return*/, _a.sent()];
3008
3093
  }
@@ -3036,6 +3121,7 @@ var SwarmPublicService = /** @class */ (function () {
3036
3121
  clientId: clientId,
3037
3122
  swarmName: swarmName,
3038
3123
  agentName: "",
3124
+ storageName: "",
3039
3125
  })];
3040
3126
  case 1: return [2 /*return*/, _a.sent()];
3041
3127
  }
@@ -3067,6 +3153,7 @@ var SwarmPublicService = /** @class */ (function () {
3067
3153
  clientId: clientId,
3068
3154
  swarmName: swarmName,
3069
3155
  agentName: "",
3156
+ storageName: "",
3070
3157
  })];
3071
3158
  case 1: return [2 /*return*/, _a.sent()];
3072
3159
  }
@@ -3085,7 +3172,20 @@ var AgentValidationService = /** @class */ (function () {
3085
3172
  this.loggerService = inject(TYPES.loggerService);
3086
3173
  this.toolValidationService = inject(TYPES.toolValidationService);
3087
3174
  this.completionValidationService = inject(TYPES.completionValidationService);
3175
+ this.storageValidationService = inject(TYPES.storageValidationService);
3088
3176
  this._agentMap = new Map();
3177
+ /**
3178
+ * Retrieves the storages used by the agent
3179
+ * @param {agentName} agentName - The name of the swarm.
3180
+ * @returns {string[]} The list of storage names.
3181
+ * @throws Will throw an error if the swarm is not found.
3182
+ */
3183
+ this.getStorageList = function (agentName) {
3184
+ if (!_this._agentMap.has(agentName)) {
3185
+ throw new Error("agent-swarm agent ".concat(agentName, " not exist (getStorageList)"));
3186
+ }
3187
+ return _this._agentMap.get(agentName).storages;
3188
+ };
3089
3189
  /**
3090
3190
  * Adds a new agent to the validation service.
3091
3191
  * @param {AgentName} agentName - The name of the agent.
@@ -3102,6 +3202,23 @@ var AgentValidationService = /** @class */ (function () {
3102
3202
  }
3103
3203
  _this._agentMap.set(agentName, agentSchema);
3104
3204
  };
3205
+ /**
3206
+ * Check if agent got registered storage
3207
+ */
3208
+ this.hasStorage = memoize(function (_a) {
3209
+ var _b = __read(_a, 2), agentName = _b[0], storageName = _b[1];
3210
+ return "".concat(agentName, "-").concat(storageName);
3211
+ }, function (agentName, storageName) {
3212
+ _this.loggerService.log("agentValidationService hasStorage", {
3213
+ agentName: agentName,
3214
+ storageName: storageName,
3215
+ });
3216
+ if (!_this._agentMap.has(agentName)) {
3217
+ throw new Error("agent-swarm agent ".concat(agentName, " not exist (hasStorage)"));
3218
+ }
3219
+ var _a = _this._agentMap.get(agentName).storages, storages = _a === void 0 ? [] : _a;
3220
+ return storages.includes(storageName);
3221
+ });
3105
3222
  /**
3106
3223
  * Validates an agent by its name and source.
3107
3224
  * @param {AgentName} agentName - The name of the agent.
@@ -3112,7 +3229,7 @@ var AgentValidationService = /** @class */ (function () {
3112
3229
  var _b = __read(_a, 1), agentName = _b[0];
3113
3230
  return agentName;
3114
3231
  }, function (agentName, source) {
3115
- var _a;
3232
+ var _a, _b;
3116
3233
  _this.loggerService.log("agentValidationService validate", {
3117
3234
  agentName: agentName,
3118
3235
  source: source,
@@ -3123,8 +3240,17 @@ var AgentValidationService = /** @class */ (function () {
3123
3240
  }
3124
3241
  _this.completionValidationService.validate(agent.completion, source);
3125
3242
  (_a = agent.tools) === null || _a === void 0 ? void 0 : _a.forEach(function (toolName) {
3243
+ if (typeof toolName !== "string") {
3244
+ throw new Error("agent-swarm agent ".concat(agentName, " tool list is invalid (toolName=").concat(String(toolName), ") source=").concat(source));
3245
+ }
3126
3246
  _this.toolValidationService.validate(toolName, source);
3127
3247
  });
3248
+ (_b = agent.storages) === null || _b === void 0 ? void 0 : _b.forEach(function (storageName) {
3249
+ if (typeof storageName !== "string") {
3250
+ throw new Error("agent-swarm agent ".concat(agentName, " storage list is invalid (storageName=").concat(String(storageName), ") source=").concat(source));
3251
+ }
3252
+ _this.storageValidationService.validate(storageName, source);
3253
+ });
3128
3254
  });
3129
3255
  }
3130
3256
  return AgentValidationService;
@@ -3506,136 +3632,937 @@ var ToolValidationService = /** @class */ (function () {
3506
3632
  return ToolValidationService;
3507
3633
  }());
3508
3634
 
3509
- {
3510
- provide(TYPES.loggerService, function () { return new LoggerService(); });
3511
- provide(TYPES.contextService, function () { return new ContextService(); });
3512
- }
3513
- {
3514
- provide(TYPES.agentConnectionService, function () { return new AgentConnectionService(); });
3515
- provide(TYPES.historyConnectionService, function () { return new HistoryConnectionService(); });
3516
- provide(TYPES.swarmConnectionService, function () { return new SwarmConnectionService(); });
3517
- provide(TYPES.sessionConnectionService, function () { return new SessionConnectionService(); });
3518
- }
3519
- {
3520
- provide(TYPES.agentSchemaService, function () { return new AgentSchemaService(); });
3521
- provide(TYPES.toolSchemaService, function () { return new ToolSchemaService(); });
3522
- provide(TYPES.swarmSchemaService, function () { return new SwarmSchemaService(); });
3523
- provide(TYPES.completionSchemaService, function () { return new CompletionSchemaService(); });
3524
- }
3525
- {
3526
- provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
3527
- provide(TYPES.historyPublicService, function () { return new HistoryPublicService(); });
3528
- provide(TYPES.sessionPublicService, function () { return new SessionPublicService(); });
3529
- provide(TYPES.swarmPublicService, function () { return new SwarmPublicService(); });
3530
- }
3531
- {
3532
- provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
3533
- provide(TYPES.historyPublicService, function () { return new HistoryPublicService(); });
3534
- provide(TYPES.sessionPublicService, function () { return new SessionPublicService(); });
3535
- provide(TYPES.swarmPublicService, function () { return new SwarmPublicService(); });
3536
- }
3537
- {
3538
- provide(TYPES.agentValidationService, function () { return new AgentValidationService(); });
3539
- provide(TYPES.completionValidationService, function () { return new CompletionValidationService(); });
3540
- provide(TYPES.sessionValidationService, function () { return new SessionValidationService(); });
3541
- provide(TYPES.swarmValidationService, function () { return new SwarmValidationService(); });
3542
- provide(TYPES.toolValidationService, function () { return new ToolValidationService(); });
3543
- }
3544
-
3545
- var baseServices = {
3546
- loggerService: inject(TYPES.loggerService),
3547
- contextService: inject(TYPES.contextService),
3548
- };
3549
- var connectionServices = {
3550
- agentConnectionService: inject(TYPES.agentConnectionService),
3551
- historyConnectionService: inject(TYPES.historyConnectionService),
3552
- swarmConnectionService: inject(TYPES.swarmConnectionService),
3553
- sessionConnectionService: inject(TYPES.sessionConnectionService),
3554
- };
3555
- var specServices = {
3556
- agentSchemaService: inject(TYPES.agentSchemaService),
3557
- toolSchemaService: inject(TYPES.toolSchemaService),
3558
- swarmSchemaService: inject(TYPES.swarmSchemaService),
3559
- completionSchemaService: inject(TYPES.completionSchemaService),
3560
- };
3561
- var publicServices = {
3562
- agentPublicService: inject(TYPES.agentPublicService),
3563
- historyPublicService: inject(TYPES.historyPublicService),
3564
- sessionPublicService: inject(TYPES.sessionPublicService),
3565
- swarmPublicService: inject(TYPES.swarmPublicService),
3566
- };
3567
- var validationServices = {
3568
- agentValidationService: inject(TYPES.agentValidationService),
3569
- toolValidationService: inject(TYPES.toolValidationService),
3570
- sessionValidationService: inject(TYPES.sessionValidationService),
3571
- swarmValidationService: inject(TYPES.swarmValidationService),
3572
- completionValidationService: inject(TYPES.completionValidationService),
3573
- };
3574
- var swarm = __assign(__assign(__assign(__assign(__assign({}, baseServices), connectionServices), specServices), publicServices), validationServices);
3575
- init();
3576
-
3577
3635
  /**
3578
- * Adds a new agent to the agent registry. The swarm takes only those agents which was registered
3579
- *
3580
- * @param {IAgentSchema} agentSchema - The schema of the agent to be added.
3581
- * @returns {string} The name of the added agent.
3636
+ * Service for managing embedding schemas.
3582
3637
  */
3583
- var addAgent = function (agentSchema) {
3584
- swarm.loggerService.log('function addAgent', {
3585
- agentSchema: agentSchema
3586
- });
3587
- swarm.agentValidationService.addAgent(agentSchema.agentName, agentSchema);
3588
- swarm.agentSchemaService.register(agentSchema.agentName, agentSchema);
3589
- return agentSchema.agentName;
3590
- };
3638
+ var EmbeddingSchemaService = /** @class */ (function () {
3639
+ function EmbeddingSchemaService() {
3640
+ var _this = this;
3641
+ this.loggerService = inject(TYPES.loggerService);
3642
+ this.registry = new ToolRegistry("embeddingSchemaService");
3643
+ /**
3644
+ * Registers a embedding with the given key and value.
3645
+ * @param {EmbeddingName} key - The name of the embedding.
3646
+ * @param {IAgentTool} value - The embedding to register.
3647
+ */
3648
+ this.register = function (key, value) {
3649
+ _this.loggerService.log('embeddingSchemaService register');
3650
+ _this.registry = _this.registry.register(key, value);
3651
+ };
3652
+ /**
3653
+ * Retrieves a embedding by its key.
3654
+ * @param {EmbeddingName} key - The name of the embedding.
3655
+ * @returns {IAgentTool} The embedding associated with the given key.
3656
+ */
3657
+ this.get = function (key) {
3658
+ _this.loggerService.log('embeddingSchemaService get', { key: key });
3659
+ return _this.registry.get(key);
3660
+ };
3661
+ }
3662
+ return EmbeddingSchemaService;
3663
+ }());
3591
3664
 
3592
3665
  /**
3593
- * Adds a completion engine for agents. Agents could use different models and
3594
- * framewords for completion like: mock, gpt4all, ollama, openai
3595
- *
3596
- * @param {ICompletionSchema} completionSchema - The completion schema to be added.
3597
- * @returns {string} The name of the completion that was added.
3666
+ * Service for managing storage schemas.
3598
3667
  */
3599
- var addCompletion = function (completionSchema) {
3600
- swarm.loggerService.log('function addCompletion', {
3601
- completionSchema: completionSchema,
3602
- });
3603
- swarm.completionValidationService.addCompletion(completionSchema.completionName);
3604
- swarm.completionSchemaService.register(completionSchema.completionName, completionSchema);
3605
- return completionSchema.completionName;
3606
- };
3668
+ var StorageSchemaService = /** @class */ (function () {
3669
+ function StorageSchemaService() {
3670
+ var _this = this;
3671
+ this.loggerService = inject(TYPES.loggerService);
3672
+ this.registry = new ToolRegistry("storageSchemaService");
3673
+ /**
3674
+ * Registers a new storage schema.
3675
+ * @param {StorageName} key - The key for the schema.
3676
+ * @param {IStorageSchema} value - The schema to register.
3677
+ */
3678
+ this.register = function (key, value) {
3679
+ _this.loggerService.log("storageSchemaService register", { key: key });
3680
+ _this.registry = _this.registry.register(key, value);
3681
+ };
3682
+ /**
3683
+ * Retrieves a storage schema by key.
3684
+ * @param {StorageName} key - The key of the schema to retrieve.
3685
+ * @returns {IStorageSchema} The retrieved schema.
3686
+ */
3687
+ this.get = function (key) {
3688
+ _this.loggerService.log("storageSchemaService get", { key: key });
3689
+ return _this.registry.get(key);
3690
+ };
3691
+ }
3692
+ return StorageSchemaService;
3693
+ }());
3607
3694
 
3608
- /**
3609
- * Adds a new swarm to the system. The swarm is a root for starting client session
3610
- *
3611
- * @param {ISwarmSchema} swarmSchema - The schema of the swarm to be added.
3612
- * @returns {string} The name of the added swarm.
3613
- */
3614
- var addSwarm = function (swarmSchema) {
3615
- swarm.loggerService.log('function addSwarm', {
3616
- swarmSchema: swarmSchema,
3617
- });
3618
- swarm.swarmValidationService.addSwarm(swarmSchema.swarmName, swarmSchema);
3619
- swarm.swarmSchemaService.register(swarmSchema.swarmName, swarmSchema);
3620
- return swarmSchema.swarmName;
3621
- };
3695
+ var ClientStorage = /** @class */ (function () {
3696
+ function ClientStorage(params) {
3697
+ var _this = this;
3698
+ this.params = params;
3699
+ this._itemMap = new Map();
3700
+ this._createEmbedding = memoize(function (_a) {
3701
+ var _b = __read(_a, 1), id = _b[0].id;
3702
+ return id;
3703
+ }, function (item) { return __awaiter(_this, void 0, void 0, function () {
3704
+ var index, embeddings;
3705
+ return __generator(this, function (_a) {
3706
+ switch (_a.label) {
3707
+ case 0:
3708
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " _createEmbedding"), {
3709
+ id: item.id,
3710
+ });
3711
+ return [4 /*yield*/, this.params.createIndex(item)];
3712
+ case 1:
3713
+ index = _a.sent();
3714
+ return [4 /*yield*/, this.params.createEmbedding(index)];
3715
+ case 2:
3716
+ embeddings = _a.sent();
3717
+ if (this.params.onCreate) {
3718
+ this.params.onCreate(index, embeddings, this.params.clientId, this.params.embedding);
3719
+ }
3720
+ return [2 /*return*/, [embeddings, index]];
3721
+ }
3722
+ });
3723
+ }); });
3724
+ this.waitForInit = singleshot(function () { return __awaiter(_this, void 0, void 0, function () {
3725
+ var data;
3726
+ return __generator(this, function (_a) {
3727
+ switch (_a.label) {
3728
+ case 0:
3729
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " waitForInit"));
3730
+ if (!this.params.getData) {
3731
+ return [2 /*return*/];
3732
+ }
3733
+ return [4 /*yield*/, this.params.getData(this.params.clientId, this.params.storageName)];
3734
+ case 1:
3735
+ data = _a.sent();
3736
+ return [4 /*yield*/, Promise.all(data.map(execpool(this._createEmbedding, {
3737
+ delay: 10,
3738
+ maxExec: GLOBAL_CONFIG.CC_STORAGE_SEARCH_POOL,
3739
+ })))];
3740
+ case 2:
3741
+ _a.sent();
3742
+ this._itemMap = new Map(data.map(function (item) { return [item.id, item]; }));
3743
+ return [2 /*return*/];
3744
+ }
3745
+ });
3746
+ }); });
3747
+ this.take = function (search, total) { return __awaiter(_this, void 0, void 0, function () {
3748
+ var indexed, searchEmbeddings, filtered;
3749
+ var _this = this;
3750
+ return __generator(this, function (_a) {
3751
+ switch (_a.label) {
3752
+ case 0:
3753
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " take"), {
3754
+ search: search,
3755
+ total: total,
3756
+ });
3757
+ indexed = new SortedArray();
3758
+ return [4 /*yield*/, this.params.createEmbedding(search)];
3759
+ case 1:
3760
+ searchEmbeddings = _a.sent();
3761
+ if (this.params.onCreate) {
3762
+ this.params.onCreate(search, searchEmbeddings, this.params.clientId, this.params.embedding);
3763
+ }
3764
+ return [4 /*yield*/, Promise.all(Array.from(this._itemMap.values()).map(execpool(function (item) { return __awaiter(_this, void 0, void 0, function () {
3765
+ var _a, targetEmbeddings, index, score;
3766
+ return __generator(this, function (_b) {
3767
+ switch (_b.label) {
3768
+ case 0: return [4 /*yield*/, this._createEmbedding(item)];
3769
+ case 1:
3770
+ _a = __read.apply(void 0, [_b.sent(), 2]), targetEmbeddings = _a[0], index = _a[1];
3771
+ return [4 /*yield*/, this.params.calculateSimilarity(searchEmbeddings, targetEmbeddings)];
3772
+ case 2:
3773
+ score = _b.sent();
3774
+ if (this.params.onCompare) {
3775
+ this.params.onCompare(search, index, score, this.params.clientId, this.params.embedding);
3776
+ }
3777
+ indexed.push({ id: item.id }, score);
3778
+ return [2 /*return*/];
3779
+ }
3780
+ });
3781
+ }); }, {
3782
+ delay: 10,
3783
+ maxExec: GLOBAL_CONFIG.CC_STORAGE_SEARCH_POOL,
3784
+ })))];
3785
+ case 2:
3786
+ _a.sent();
3787
+ filtered = indexed.take(total, GLOBAL_CONFIG.CC_STORAGE_SEARCH_SIMILARITY);
3788
+ return [2 /*return*/, filtered.map(function (_a) {
3789
+ var id = _a.id;
3790
+ return _this._itemMap.get(id);
3791
+ })];
3792
+ }
3793
+ });
3794
+ }); };
3795
+ this.upsert = function (item) { return __awaiter(_this, void 0, void 0, function () {
3796
+ var _a, _b;
3797
+ return __generator(this, function (_c) {
3798
+ switch (_c.label) {
3799
+ case 0:
3800
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " upsert"), {
3801
+ item: item,
3802
+ });
3803
+ this._itemMap.set(item.id, item);
3804
+ this._createEmbedding.clear(item.id);
3805
+ return [4 /*yield*/, this._createEmbedding(item)];
3806
+ case 1:
3807
+ _c.sent();
3808
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
3809
+ (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
3810
+ }
3811
+ return [2 /*return*/];
3812
+ }
3813
+ });
3814
+ }); };
3815
+ this.remove = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
3816
+ var _a, _b;
3817
+ return __generator(this, function (_c) {
3818
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " remove"), {
3819
+ id: itemId,
3820
+ });
3821
+ this._itemMap.delete(itemId);
3822
+ this._createEmbedding.clear(itemId);
3823
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onUpdate) {
3824
+ (_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onUpdate(__spreadArray([], __read(this._itemMap.values()), false), this.params.clientId, this.params.storageName);
3825
+ }
3826
+ return [2 /*return*/];
3827
+ });
3828
+ }); };
3829
+ this.clear = function () { return __awaiter(_this, void 0, void 0, function () {
3830
+ return __generator(this, function (_a) {
3831
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " clear"));
3832
+ this._itemMap.clear();
3833
+ this._createEmbedding.clear();
3834
+ return [2 /*return*/];
3835
+ });
3836
+ }); };
3837
+ this.get = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
3838
+ var _a;
3839
+ return __generator(this, function (_b) {
3840
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " get"), {
3841
+ id: itemId,
3842
+ });
3843
+ return [2 /*return*/, (_a = this._itemMap.get(itemId)) !== null && _a !== void 0 ? _a : null];
3844
+ });
3845
+ }); };
3846
+ this.list = function (filter) { return __awaiter(_this, void 0, void 0, function () {
3847
+ var result, _a, _b, item;
3848
+ var e_1, _c;
3849
+ return __generator(this, function (_d) {
3850
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " list"));
3851
+ if (!filter) {
3852
+ return [2 /*return*/, __spreadArray([], __read(this._itemMap.values()), false)];
3853
+ }
3854
+ result = [];
3855
+ try {
3856
+ for (_a = __values(this._itemMap.values()), _b = _a.next(); !_b.done; _b = _a.next()) {
3857
+ item = _b.value;
3858
+ if (filter(item)) {
3859
+ result.push(item);
3860
+ }
3861
+ }
3862
+ }
3863
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3864
+ finally {
3865
+ try {
3866
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
3867
+ }
3868
+ finally { if (e_1) throw e_1.error; }
3869
+ }
3870
+ return [2 /*return*/, result];
3871
+ });
3872
+ }); };
3873
+ this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " CTOR"), {
3874
+ params: params,
3875
+ });
3876
+ }
3877
+ return ClientStorage;
3878
+ }());
3622
3879
 
3623
3880
  /**
3624
- * Adds a new tool for agents in a swarm. Tool should be registered in `addAgent`
3625
- * declaration
3626
- *
3627
- * @param {IAgentTool} toolSchema - The schema of the tool to be added.
3628
- * @returns {string} The name of the tool that was added.
3881
+ * Service for managing storage connections.
3882
+ * @implements {IStorage}
3629
3883
  */
3630
- var addTool = function (toolSchema) {
3631
- swarm.loggerService.log('function addTool', {
3632
- toolSchema: toolSchema,
3633
- });
3634
- swarm.toolValidationService.addTool(toolSchema.toolName, toolSchema);
3884
+ var StorageConnectionService = /** @class */ (function () {
3885
+ function StorageConnectionService() {
3886
+ var _this = this;
3887
+ this.loggerService = inject(TYPES.loggerService);
3888
+ this.contextService = inject(TYPES.contextService);
3889
+ this.storageSchemaService = inject(TYPES.storageSchemaService);
3890
+ this.embeddingSchemaService = inject(TYPES.embeddingSchemaService);
3891
+ /**
3892
+ * Retrieves a storage instance based on client ID and storage name.
3893
+ * @param {string} clientId - The client ID.
3894
+ * @param {string} storageName - The storage name.
3895
+ * @returns {ClientStorage} The client storage instance.
3896
+ */
3897
+ this.getStorage = memoize(function (_a) {
3898
+ var _b = __read(_a, 2), clientId = _b[0], storageName = _b[1];
3899
+ return "".concat(clientId, "-").concat(storageName);
3900
+ }, function (clientId, storageName) {
3901
+ var _a = _this.storageSchemaService.get(storageName), createIndex = _a.createIndex, getData = _a.getData, embeddingName = _a.embedding, callbacks = _a.callbacks;
3902
+ var _b = _this.embeddingSchemaService.get(embeddingName), calculateSimilarity = _b.calculateSimilarity, createEmbedding = _b.createEmbedding, embedding = _b.callbacks;
3903
+ return new ClientStorage(__assign(__assign({ clientId: clientId, storageName: storageName, embedding: embeddingName, calculateSimilarity: calculateSimilarity, createEmbedding: createEmbedding, createIndex: createIndex, getData: getData, logger: _this.loggerService }, embedding), { callbacks: callbacks }));
3904
+ });
3905
+ /**
3906
+ * Retrieves a list of storage data based on a search query and total number of items.
3907
+ * @param {string} search - The search query.
3908
+ * @param {number} total - The total number of items to retrieve.
3909
+ * @returns {Promise<IStorageData[]>} The list of storage data.
3910
+ */
3911
+ this.take = function (search, total) { return __awaiter(_this, void 0, void 0, function () {
3912
+ var storage;
3913
+ return __generator(this, function (_a) {
3914
+ switch (_a.label) {
3915
+ case 0:
3916
+ this.loggerService.log("storageConnectionService take", {
3917
+ context: this.contextService.context,
3918
+ search: search,
3919
+ total: total,
3920
+ });
3921
+ storage = this.getStorage(this.contextService.context.clientId, this.contextService.context.storageName);
3922
+ return [4 /*yield*/, storage.waitForInit()];
3923
+ case 1:
3924
+ _a.sent();
3925
+ return [4 /*yield*/, storage.take(search, total)];
3926
+ case 2: return [2 /*return*/, _a.sent()];
3927
+ }
3928
+ });
3929
+ }); };
3930
+ /**
3931
+ * Upserts an item in the storage.
3932
+ * @param {IStorageData} item - The item to upsert.
3933
+ * @returns {Promise<void>}
3934
+ */
3935
+ this.upsert = function (item) { return __awaiter(_this, void 0, void 0, function () {
3936
+ var storage;
3937
+ return __generator(this, function (_a) {
3938
+ switch (_a.label) {
3939
+ case 0:
3940
+ this.loggerService.log("storageConnectionService upsert", {
3941
+ context: this.contextService.context,
3942
+ item: item,
3943
+ });
3944
+ storage = this.getStorage(this.contextService.context.clientId, this.contextService.context.storageName);
3945
+ return [4 /*yield*/, storage.waitForInit()];
3946
+ case 1:
3947
+ _a.sent();
3948
+ return [4 /*yield*/, storage.upsert(item)];
3949
+ case 2: return [2 /*return*/, _a.sent()];
3950
+ }
3951
+ });
3952
+ }); };
3953
+ /**
3954
+ * Removes an item from the storage.
3955
+ * @param {IStorageData["id"]} itemId - The ID of the item to remove.
3956
+ * @returns {Promise<void>}
3957
+ */
3958
+ this.remove = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
3959
+ var storage;
3960
+ return __generator(this, function (_a) {
3961
+ switch (_a.label) {
3962
+ case 0:
3963
+ this.loggerService.log("storageConnectionService remove", {
3964
+ context: this.contextService.context,
3965
+ itemId: itemId,
3966
+ });
3967
+ storage = this.getStorage(this.contextService.context.clientId, this.contextService.context.storageName);
3968
+ return [4 /*yield*/, storage.waitForInit()];
3969
+ case 1:
3970
+ _a.sent();
3971
+ return [4 /*yield*/, storage.remove(itemId)];
3972
+ case 2: return [2 /*return*/, _a.sent()];
3973
+ }
3974
+ });
3975
+ }); };
3976
+ /**
3977
+ * Retrieves an item from the storage by its ID.
3978
+ * @param {IStorageData["id"]} itemId - The ID of the item to retrieve.
3979
+ * @returns {Promise<IStorageData>} The retrieved item.
3980
+ */
3981
+ this.get = function (itemId) { return __awaiter(_this, void 0, void 0, function () {
3982
+ var storage;
3983
+ return __generator(this, function (_a) {
3984
+ switch (_a.label) {
3985
+ case 0:
3986
+ this.loggerService.log("storageConnectionService get", {
3987
+ context: this.contextService.context,
3988
+ itemId: itemId,
3989
+ });
3990
+ storage = this.getStorage(this.contextService.context.clientId, this.contextService.context.storageName);
3991
+ return [4 /*yield*/, storage.waitForInit()];
3992
+ case 1:
3993
+ _a.sent();
3994
+ return [4 /*yield*/, storage.get(itemId)];
3995
+ case 2: return [2 /*return*/, _a.sent()];
3996
+ }
3997
+ });
3998
+ }); };
3999
+ /**
4000
+ * Retrieves a list of items from the storage, optionally filtered by a predicate function.
4001
+ * @param {function(IStorageData): boolean} [filter] - The optional filter function.
4002
+ * @returns {Promise<IStorageData[]>} The list of items.
4003
+ */
4004
+ this.list = function (filter) { return __awaiter(_this, void 0, void 0, function () {
4005
+ var storage;
4006
+ return __generator(this, function (_a) {
4007
+ switch (_a.label) {
4008
+ case 0:
4009
+ this.loggerService.log("storageConnectionService list", {
4010
+ context: this.contextService.context,
4011
+ });
4012
+ storage = this.getStorage(this.contextService.context.clientId, this.contextService.context.storageName);
4013
+ return [4 /*yield*/, storage.waitForInit()];
4014
+ case 1:
4015
+ _a.sent();
4016
+ return [4 /*yield*/, storage.list(filter)];
4017
+ case 2: return [2 /*return*/, _a.sent()];
4018
+ }
4019
+ });
4020
+ }); };
4021
+ /**
4022
+ * Clears all items from the storage.
4023
+ * @returns {Promise<void>}
4024
+ */
4025
+ this.clear = function () { return __awaiter(_this, void 0, void 0, function () {
4026
+ var storage;
4027
+ return __generator(this, function (_a) {
4028
+ switch (_a.label) {
4029
+ case 0:
4030
+ this.loggerService.log("storageConnectionService clear", {
4031
+ context: this.contextService.context,
4032
+ });
4033
+ storage = this.getStorage(this.contextService.context.clientId, this.contextService.context.storageName);
4034
+ return [4 /*yield*/, storage.waitForInit()];
4035
+ case 1:
4036
+ _a.sent();
4037
+ return [4 /*yield*/, storage.clear()];
4038
+ case 2: return [2 /*return*/, _a.sent()];
4039
+ }
4040
+ });
4041
+ }); };
4042
+ /**
4043
+ * Disposes of the storage connection.
4044
+ * @returns {Promise<void>}
4045
+ */
4046
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
4047
+ var key;
4048
+ return __generator(this, function (_a) {
4049
+ this.loggerService.log("storageConnectionService dispose", {
4050
+ context: this.contextService.context,
4051
+ });
4052
+ key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.storageName);
4053
+ if (!this.getStorage.has(key)) {
4054
+ return [2 /*return*/];
4055
+ }
4056
+ this.getStorage.clear(key);
4057
+ return [2 /*return*/];
4058
+ });
4059
+ }); };
4060
+ }
4061
+ return StorageConnectionService;
4062
+ }());
4063
+
4064
+ /**
4065
+ * Service for managing public storage interactions.
4066
+ */
4067
+ var StoragePublicService = /** @class */ (function () {
4068
+ function StoragePublicService() {
4069
+ var _this = this;
4070
+ this.loggerService = inject(TYPES.loggerService);
4071
+ this.storageConnectionService = inject(TYPES.storageConnectionService);
4072
+ /**
4073
+ * Retrieves a list of storage data based on a search query and total number of items.
4074
+ * @param {string} search - The search query.
4075
+ * @param {number} total - The total number of items to retrieve.
4076
+ * @returns {Promise<IStorageData[]>} The list of storage data.
4077
+ */
4078
+ this.take = function (search, total, clientId, storageName) { return __awaiter(_this, void 0, void 0, function () {
4079
+ var _this = this;
4080
+ return __generator(this, function (_a) {
4081
+ switch (_a.label) {
4082
+ case 0:
4083
+ this.loggerService.log("storagePublicService take", {
4084
+ search: search,
4085
+ total: total,
4086
+ clientId: clientId,
4087
+ storageName: storageName,
4088
+ });
4089
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
4090
+ return __generator(this, function (_a) {
4091
+ switch (_a.label) {
4092
+ case 0: return [4 /*yield*/, this.storageConnectionService.take(search, total)];
4093
+ case 1: return [2 /*return*/, _a.sent()];
4094
+ }
4095
+ });
4096
+ }); }, {
4097
+ clientId: clientId,
4098
+ storageName: storageName,
4099
+ agentName: "",
4100
+ swarmName: "",
4101
+ })];
4102
+ case 1: return [2 /*return*/, _a.sent()];
4103
+ }
4104
+ });
4105
+ }); };
4106
+ /**
4107
+ * Upserts an item in the storage.
4108
+ * @param {IStorageData} item - The item to upsert.
4109
+ * @returns {Promise<void>}
4110
+ */
4111
+ this.upsert = function (item, clientId, storageName) { return __awaiter(_this, void 0, void 0, function () {
4112
+ var _this = this;
4113
+ return __generator(this, function (_a) {
4114
+ switch (_a.label) {
4115
+ case 0:
4116
+ this.loggerService.log("storagePublicService upsert", {
4117
+ item: item,
4118
+ clientId: clientId,
4119
+ storageName: storageName,
4120
+ });
4121
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
4122
+ return __generator(this, function (_a) {
4123
+ switch (_a.label) {
4124
+ case 0: return [4 /*yield*/, this.storageConnectionService.upsert(item)];
4125
+ case 1: return [2 /*return*/, _a.sent()];
4126
+ }
4127
+ });
4128
+ }); }, {
4129
+ clientId: clientId,
4130
+ storageName: storageName,
4131
+ agentName: "",
4132
+ swarmName: "",
4133
+ })];
4134
+ case 1: return [2 /*return*/, _a.sent()];
4135
+ }
4136
+ });
4137
+ }); };
4138
+ /**
4139
+ * Removes an item from the storage.
4140
+ * @param {IStorageData["id"]} itemId - The ID of the item to remove.
4141
+ * @returns {Promise<void>}
4142
+ */
4143
+ this.remove = function (itemId, clientId, storageName) { return __awaiter(_this, void 0, void 0, function () {
4144
+ var _this = this;
4145
+ return __generator(this, function (_a) {
4146
+ switch (_a.label) {
4147
+ case 0:
4148
+ this.loggerService.log("storagePublicService remove", {
4149
+ itemId: itemId,
4150
+ clientId: clientId,
4151
+ storageName: storageName,
4152
+ });
4153
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
4154
+ return __generator(this, function (_a) {
4155
+ switch (_a.label) {
4156
+ case 0: return [4 /*yield*/, this.storageConnectionService.remove(itemId)];
4157
+ case 1: return [2 /*return*/, _a.sent()];
4158
+ }
4159
+ });
4160
+ }); }, {
4161
+ clientId: clientId,
4162
+ storageName: storageName,
4163
+ agentName: "",
4164
+ swarmName: "",
4165
+ })];
4166
+ case 1: return [2 /*return*/, _a.sent()];
4167
+ }
4168
+ });
4169
+ }); };
4170
+ /**
4171
+ * Retrieves an item from the storage by its ID.
4172
+ * @param {IStorageData["id"]} itemId - The ID of the item to retrieve.
4173
+ * @returns {Promise<IStorageData>} The retrieved item.
4174
+ */
4175
+ this.get = function (itemId, clientId, storageName) { return __awaiter(_this, void 0, void 0, function () {
4176
+ var _this = this;
4177
+ return __generator(this, function (_a) {
4178
+ switch (_a.label) {
4179
+ case 0:
4180
+ this.loggerService.log("storagePublicService get", {
4181
+ itemId: itemId,
4182
+ clientId: clientId,
4183
+ storageName: storageName,
4184
+ });
4185
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
4186
+ return __generator(this, function (_a) {
4187
+ switch (_a.label) {
4188
+ case 0: return [4 /*yield*/, this.storageConnectionService.get(itemId)];
4189
+ case 1: return [2 /*return*/, _a.sent()];
4190
+ }
4191
+ });
4192
+ }); }, {
4193
+ clientId: clientId,
4194
+ storageName: storageName,
4195
+ agentName: "",
4196
+ swarmName: "",
4197
+ })];
4198
+ case 1: return [2 /*return*/, _a.sent()];
4199
+ }
4200
+ });
4201
+ }); };
4202
+ /**
4203
+ * Retrieves a list of items from the storage, optionally filtered by a predicate function.
4204
+ * @param {function(IStorageData): boolean} [filter] - The optional filter function.
4205
+ * @returns {Promise<IStorageData[]>} The list of items.
4206
+ */
4207
+ this.list = function (clientId, storageName, filter) { return __awaiter(_this, void 0, void 0, function () {
4208
+ var _this = this;
4209
+ return __generator(this, function (_a) {
4210
+ switch (_a.label) {
4211
+ case 0:
4212
+ this.loggerService.log("storagePublicService list", {
4213
+ clientId: clientId,
4214
+ storageName: storageName,
4215
+ });
4216
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
4217
+ return __generator(this, function (_a) {
4218
+ switch (_a.label) {
4219
+ case 0: return [4 /*yield*/, this.storageConnectionService.list(filter)];
4220
+ case 1: return [2 /*return*/, _a.sent()];
4221
+ }
4222
+ });
4223
+ }); }, {
4224
+ clientId: clientId,
4225
+ storageName: storageName,
4226
+ agentName: "",
4227
+ swarmName: "",
4228
+ })];
4229
+ case 1: return [2 /*return*/, _a.sent()];
4230
+ }
4231
+ });
4232
+ }); };
4233
+ /**
4234
+ * Clears all items from the storage.
4235
+ * @returns {Promise<void>}
4236
+ */
4237
+ this.clear = function (clientId, storageName) { return __awaiter(_this, void 0, void 0, function () {
4238
+ var _this = this;
4239
+ return __generator(this, function (_a) {
4240
+ switch (_a.label) {
4241
+ case 0:
4242
+ this.loggerService.log("storagePublicService clear", {
4243
+ clientId: clientId,
4244
+ storageName: storageName,
4245
+ });
4246
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
4247
+ return __generator(this, function (_a) {
4248
+ switch (_a.label) {
4249
+ case 0: return [4 /*yield*/, this.storageConnectionService.clear()];
4250
+ case 1: return [2 /*return*/, _a.sent()];
4251
+ }
4252
+ });
4253
+ }); }, {
4254
+ clientId: clientId,
4255
+ storageName: storageName,
4256
+ agentName: "",
4257
+ swarmName: "",
4258
+ })];
4259
+ case 1: return [2 /*return*/, _a.sent()];
4260
+ }
4261
+ });
4262
+ }); };
4263
+ /**
4264
+ * Disposes of the storage.
4265
+ * @param {string} clientId - The client ID.
4266
+ * @param {StorageName} storageName - The storage name.
4267
+ * @returns {Promise<void>}
4268
+ */
4269
+ this.dispose = function (clientId, storageName) { return __awaiter(_this, void 0, void 0, function () {
4270
+ var _this = this;
4271
+ return __generator(this, function (_a) {
4272
+ switch (_a.label) {
4273
+ case 0:
4274
+ this.loggerService.log("storagePublicService dispose", {
4275
+ clientId: clientId,
4276
+ storageName: storageName,
4277
+ });
4278
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
4279
+ return __generator(this, function (_a) {
4280
+ switch (_a.label) {
4281
+ case 0: return [4 /*yield*/, this.storageConnectionService.dispose()];
4282
+ case 1: return [2 /*return*/, _a.sent()];
4283
+ }
4284
+ });
4285
+ }); }, {
4286
+ clientId: clientId,
4287
+ storageName: storageName,
4288
+ agentName: "",
4289
+ swarmName: "",
4290
+ })];
4291
+ case 1: return [2 /*return*/, _a.sent()];
4292
+ }
4293
+ });
4294
+ }); };
4295
+ }
4296
+ return StoragePublicService;
4297
+ }());
4298
+
4299
+ /**
4300
+ * Service for validating storages within the storage swarm.
4301
+ */
4302
+ var StorageValidationService = /** @class */ (function () {
4303
+ function StorageValidationService() {
4304
+ var _this = this;
4305
+ this.loggerService = inject(TYPES.loggerService);
4306
+ this.embeddingValidationService = inject(TYPES.embeddingValidationService);
4307
+ this._storageMap = new Map();
4308
+ /**
4309
+ * Adds a new storage to the validation service.
4310
+ * @param {StorageName} storageName - The name of the storage.
4311
+ * @param {IStorageSchema} storageSchema - The schema of the storage.
4312
+ * @throws {Error} If the storage already exists.
4313
+ */
4314
+ this.addStorage = function (storageName, storageSchema) {
4315
+ _this.loggerService.log("storageValidationService addStorage", {
4316
+ storageName: storageName,
4317
+ storageSchema: storageSchema,
4318
+ });
4319
+ if (_this._storageMap.has(storageName)) {
4320
+ throw new Error("storage-swarm storage ".concat(storageName, " already exist"));
4321
+ }
4322
+ _this._storageMap.set(storageName, storageSchema);
4323
+ };
4324
+ /**
4325
+ * Validates an storage by its name and source.
4326
+ * @param {StorageName} storageName - The name of the storage.
4327
+ * @param {string} source - The source of the validation request.
4328
+ * @throws {Error} If the storage is not found.
4329
+ */
4330
+ this.validate = memoize(function (_a) {
4331
+ var _b = __read(_a, 1), storageName = _b[0];
4332
+ return storageName;
4333
+ }, function (storageName, source) {
4334
+ _this.loggerService.log("storageValidationService validate", {
4335
+ storageName: storageName,
4336
+ source: source,
4337
+ });
4338
+ var storage = _this._storageMap.get(storageName);
4339
+ if (!storage) {
4340
+ throw new Error("storage-swarm storage ".concat(storageName, " not found source=").concat(source));
4341
+ }
4342
+ _this.embeddingValidationService.validate(storage.embedding, source);
4343
+ });
4344
+ }
4345
+ return StorageValidationService;
4346
+ }());
4347
+
4348
+ /**
4349
+ * Service for validating embeddings within the agent-swarm.
4350
+ */
4351
+ var EmbeddingValidationService = /** @class */ (function () {
4352
+ function EmbeddingValidationService() {
4353
+ var _this = this;
4354
+ this.loggerService = inject(TYPES.loggerService);
4355
+ this._embeddingMap = new Map();
4356
+ /**
4357
+ * Adds a new embedding to the validation service.
4358
+ * @param {EmbeddingName} embeddingName - The name of the embedding to add.
4359
+ * @param {IAgentEmbedding} embeddingSchema - The schema of the embedding to add.
4360
+ * @throws Will throw an error if the embedding already exists.
4361
+ */
4362
+ this.addEmbedding = function (embeddingName, embeddingSchema) {
4363
+ _this.loggerService.log("embeddingValidationService addEmbedding", {
4364
+ embeddingName: embeddingName,
4365
+ embeddingSchema: embeddingSchema,
4366
+ });
4367
+ if (_this._embeddingMap.has(embeddingName)) {
4368
+ throw new Error("agent-swarm embedding ".concat(embeddingName, " already exist"));
4369
+ }
4370
+ _this._embeddingMap.set(embeddingName, embeddingSchema);
4371
+ };
4372
+ /**
4373
+ * Validates if a embedding exists in the validation service.
4374
+ * @param {EmbeddingName} embeddingName - The name of the embedding to validate.
4375
+ * @param {string} source - The source of the validation request.
4376
+ * @throws Will throw an error if the embedding is not found.
4377
+ */
4378
+ this.validate = memoize(function (_a) {
4379
+ var _b = __read(_a, 1), embeddingName = _b[0];
4380
+ return embeddingName;
4381
+ }, function (embeddingName, source) {
4382
+ _this.loggerService.log("embeddingValidationService validate", {
4383
+ embeddingName: embeddingName,
4384
+ source: source,
4385
+ });
4386
+ if (!_this._embeddingMap.has(embeddingName)) {
4387
+ throw new Error("agent-swarm embedding ".concat(embeddingName, " not found source=").concat(source));
4388
+ }
4389
+ });
4390
+ }
4391
+ return EmbeddingValidationService;
4392
+ }());
4393
+
4394
+ {
4395
+ provide(TYPES.loggerService, function () { return new LoggerService(); });
4396
+ provide(TYPES.contextService, function () { return new ContextService(); });
4397
+ }
4398
+ {
4399
+ provide(TYPES.agentConnectionService, function () { return new AgentConnectionService(); });
4400
+ provide(TYPES.historyConnectionService, function () { return new HistoryConnectionService(); });
4401
+ provide(TYPES.swarmConnectionService, function () { return new SwarmConnectionService(); });
4402
+ provide(TYPES.sessionConnectionService, function () { return new SessionConnectionService(); });
4403
+ provide(TYPES.storageConnectionService, function () { return new StorageConnectionService(); });
4404
+ }
4405
+ {
4406
+ provide(TYPES.agentSchemaService, function () { return new AgentSchemaService(); });
4407
+ provide(TYPES.toolSchemaService, function () { return new ToolSchemaService(); });
4408
+ provide(TYPES.swarmSchemaService, function () { return new SwarmSchemaService(); });
4409
+ provide(TYPES.completionSchemaService, function () { return new CompletionSchemaService(); });
4410
+ provide(TYPES.embeddingSchemaService, function () { return new EmbeddingSchemaService(); });
4411
+ provide(TYPES.storageSchemaService, function () { return new StorageSchemaService(); });
4412
+ }
4413
+ {
4414
+ provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
4415
+ provide(TYPES.historyPublicService, function () { return new HistoryPublicService(); });
4416
+ provide(TYPES.sessionPublicService, function () { return new SessionPublicService(); });
4417
+ provide(TYPES.swarmPublicService, function () { return new SwarmPublicService(); });
4418
+ provide(TYPES.storagePublicService, function () { return new StoragePublicService(); });
4419
+ }
4420
+ {
4421
+ provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
4422
+ provide(TYPES.historyPublicService, function () { return new HistoryPublicService(); });
4423
+ provide(TYPES.sessionPublicService, function () { return new SessionPublicService(); });
4424
+ provide(TYPES.swarmPublicService, function () { return new SwarmPublicService(); });
4425
+ }
4426
+ {
4427
+ provide(TYPES.agentValidationService, function () { return new AgentValidationService(); });
4428
+ provide(TYPES.completionValidationService, function () { return new CompletionValidationService(); });
4429
+ provide(TYPES.sessionValidationService, function () { return new SessionValidationService(); });
4430
+ provide(TYPES.swarmValidationService, function () { return new SwarmValidationService(); });
4431
+ provide(TYPES.toolValidationService, function () { return new ToolValidationService(); });
4432
+ provide(TYPES.storageValidationService, function () { return new StorageValidationService(); });
4433
+ provide(TYPES.embeddingValidationService, function () { return new EmbeddingValidationService(); });
4434
+ }
4435
+
4436
+ var baseServices = {
4437
+ loggerService: inject(TYPES.loggerService),
4438
+ contextService: inject(TYPES.contextService),
4439
+ };
4440
+ var connectionServices = {
4441
+ agentConnectionService: inject(TYPES.agentConnectionService),
4442
+ historyConnectionService: inject(TYPES.historyConnectionService),
4443
+ swarmConnectionService: inject(TYPES.swarmConnectionService),
4444
+ sessionConnectionService: inject(TYPES.sessionConnectionService),
4445
+ storageConnectionService: inject(TYPES.storageConnectionService),
4446
+ };
4447
+ var schemaServices = {
4448
+ agentSchemaService: inject(TYPES.agentSchemaService),
4449
+ toolSchemaService: inject(TYPES.toolSchemaService),
4450
+ swarmSchemaService: inject(TYPES.swarmSchemaService),
4451
+ completionSchemaService: inject(TYPES.completionSchemaService),
4452
+ embeddingSchemaService: inject(TYPES.embeddingSchemaService),
4453
+ storageSchemaService: inject(TYPES.storageSchemaService),
4454
+ };
4455
+ var publicServices = {
4456
+ agentPublicService: inject(TYPES.agentPublicService),
4457
+ historyPublicService: inject(TYPES.historyPublicService),
4458
+ sessionPublicService: inject(TYPES.sessionPublicService),
4459
+ swarmPublicService: inject(TYPES.swarmPublicService),
4460
+ storagePublicService: inject(TYPES.storagePublicService),
4461
+ };
4462
+ var validationServices = {
4463
+ agentValidationService: inject(TYPES.agentValidationService),
4464
+ toolValidationService: inject(TYPES.toolValidationService),
4465
+ sessionValidationService: inject(TYPES.sessionValidationService),
4466
+ swarmValidationService: inject(TYPES.swarmValidationService),
4467
+ completionValidationService: inject(TYPES.completionValidationService),
4468
+ storageValidationService: inject(TYPES.storageValidationService),
4469
+ embeddingValidationService: inject(TYPES.embeddingValidationService),
4470
+ };
4471
+ var swarm = __assign(__assign(__assign(__assign(__assign({}, baseServices), connectionServices), schemaServices), publicServices), validationServices);
4472
+ init();
4473
+
4474
+ /**
4475
+ * Adds a new agent to the agent registry. The swarm takes only those agents which was registered
4476
+ *
4477
+ * @param {IAgentSchema} agentSchema - The schema of the agent to be added.
4478
+ * @returns {string} The name of the added agent.
4479
+ */
4480
+ var addAgent = function (agentSchema) {
4481
+ swarm.loggerService.log('function addAgent', {
4482
+ agentSchema: agentSchema
4483
+ });
4484
+ swarm.agentValidationService.addAgent(agentSchema.agentName, agentSchema);
4485
+ swarm.agentSchemaService.register(agentSchema.agentName, agentSchema);
4486
+ return agentSchema.agentName;
4487
+ };
4488
+
4489
+ /**
4490
+ * Adds a completion engine for agents. Agents could use different models and
4491
+ * framewords for completion like: mock, gpt4all, ollama, openai
4492
+ *
4493
+ * @param {ICompletionSchema} completionSchema - The completion schema to be added.
4494
+ * @returns {string} The name of the completion that was added.
4495
+ */
4496
+ var addCompletion = function (completionSchema) {
4497
+ swarm.loggerService.log('function addCompletion', {
4498
+ completionSchema: completionSchema,
4499
+ });
4500
+ swarm.completionValidationService.addCompletion(completionSchema.completionName);
4501
+ swarm.completionSchemaService.register(completionSchema.completionName, completionSchema);
4502
+ return completionSchema.completionName;
4503
+ };
4504
+
4505
+ /**
4506
+ * Adds a new swarm to the system. The swarm is a root for starting client session
4507
+ *
4508
+ * @param {ISwarmSchema} swarmSchema - The schema of the swarm to be added.
4509
+ * @returns {string} The name of the added swarm.
4510
+ */
4511
+ var addSwarm = function (swarmSchema) {
4512
+ swarm.loggerService.log('function addSwarm', {
4513
+ swarmSchema: swarmSchema,
4514
+ });
4515
+ swarm.swarmValidationService.addSwarm(swarmSchema.swarmName, swarmSchema);
4516
+ swarm.swarmSchemaService.register(swarmSchema.swarmName, swarmSchema);
4517
+ return swarmSchema.swarmName;
4518
+ };
4519
+
4520
+ /**
4521
+ * Adds a new tool for agents in a swarm. Tool should be registered in `addAgent`
4522
+ * declaration
4523
+ *
4524
+ * @param {IAgentTool} toolSchema - The schema of the tool to be added.
4525
+ * @returns {string} The name of the tool that was added.
4526
+ */
4527
+ var addTool = function (toolSchema) {
4528
+ swarm.loggerService.log('function addTool', {
4529
+ toolSchema: toolSchema,
4530
+ });
4531
+ swarm.toolValidationService.addTool(toolSchema.toolName, toolSchema);
3635
4532
  swarm.toolSchemaService.register(toolSchema.toolName, toolSchema);
3636
4533
  return toolSchema.toolName;
3637
4534
  };
3638
4535
 
4536
+ /**
4537
+ * Adds a new embedding to the embedding registry. The swarm takes only those embeddings which was registered
4538
+ *
4539
+ * @param {IEmbeddingSchema} embeddingSchema - The schema of the embedding to be added.
4540
+ * @returns {string} The name of the added embedding.
4541
+ */
4542
+ var addEmbedding = function (embeddingSchema) {
4543
+ swarm.loggerService.log('function addEmbedding', {
4544
+ embeddingSchema: embeddingSchema
4545
+ });
4546
+ swarm.embeddingValidationService.addEmbedding(embeddingSchema.embeddingName, embeddingSchema);
4547
+ swarm.embeddingSchemaService.register(embeddingSchema.embeddingName, embeddingSchema);
4548
+ return embeddingSchema.embeddingName;
4549
+ };
4550
+
4551
+ /**
4552
+ * Adds a new storage to the storage registry. The swarm takes only those storages which was registered
4553
+ *
4554
+ * @param {IStorageSchema} storageSchema - The schema of the storage to be added.
4555
+ * @returns {string} The name of the added storage.
4556
+ */
4557
+ var addStorage = function (storageSchema) {
4558
+ swarm.loggerService.log('function addStorage', {
4559
+ storageSchema: storageSchema
4560
+ });
4561
+ swarm.storageValidationService.addStorage(storageSchema.storageName, storageSchema);
4562
+ swarm.storageSchemaService.register(storageSchema.storageName, storageSchema);
4563
+ return storageSchema.storageName;
4564
+ };
4565
+
3639
4566
  /**
3640
4567
  * Commits a user message to the active agent history in as swarm without answer.
3641
4568
  *
@@ -3947,6 +4874,25 @@ var disposeConnection = function (clientId, swarmName) { return __awaiter(void 0
3947
4874
  }); }),
3948
4875
  ])];
3949
4876
  case 3:
4877
+ _a.sent();
4878
+ return [4 /*yield*/, Promise.all([
4879
+ swarm.swarmValidationService
4880
+ .getAgentList(swarmName)
4881
+ .flatMap(function (agentName) {
4882
+ return swarm.agentValidationService.getStorageList(agentName);
4883
+ })
4884
+ .map(function (storageName) { return __awaiter(void 0, void 0, void 0, function () {
4885
+ return __generator(this, function (_a) {
4886
+ switch (_a.label) {
4887
+ case 0: return [4 /*yield*/, swarm.storagePublicService.dispose(clientId, storageName)];
4888
+ case 1:
4889
+ _a.sent();
4890
+ return [2 /*return*/];
4891
+ }
4892
+ });
4893
+ }); }),
4894
+ ])];
4895
+ case 4:
3950
4896
  _a.sent();
3951
4897
  return [2 /*return*/];
3952
4898
  }
@@ -4792,4 +5738,171 @@ var makeAutoDispose = function (clientId, swarmName, _a) {
4792
5738
  };
4793
5739
  };
4794
5740
 
4795
- export { ContextService, addAgent, addCompletion, 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 };
5741
+ var StorageUtils = /** @class */ (function () {
5742
+ function StorageUtils() {
5743
+ var _this = this;
5744
+ /**
5745
+ * Takes items from the storage.
5746
+ * @param {string} search - The search query.
5747
+ * @param {number} total - The total number of items to take.
5748
+ * @param {string} clientId - The client ID.
5749
+ * @param {AgentName} agentName - The agent name.
5750
+ * @param {StorageName} storageName - The storage name.
5751
+ * @returns {Promise<T[]>} - A promise that resolves to an array of items.
5752
+ * @template T
5753
+ */
5754
+ this.take = function (search, total, clientId, agentName, storageName) { return __awaiter(_this, void 0, void 0, function () {
5755
+ return __generator(this, function (_a) {
5756
+ switch (_a.label) {
5757
+ case 0:
5758
+ swarm.loggerService.log("StorageStatic take", {
5759
+ search: search,
5760
+ total: total,
5761
+ clientId: clientId,
5762
+ storageName: storageName,
5763
+ });
5764
+ swarm.storageValidationService.validate(storageName, "StorageStatic");
5765
+ if (!swarm.agentValidationService.hasStorage(agentName, storageName)) {
5766
+ throw new Error("agent-swarm StorageUtils ".concat(storageName, " not registered in ").concat(agentName, " (take)"));
5767
+ }
5768
+ return [4 /*yield*/, swarm.storagePublicService.take(search, total, clientId, storageName)];
5769
+ case 1: return [2 /*return*/, (_a.sent())];
5770
+ }
5771
+ });
5772
+ }); };
5773
+ /**
5774
+ * Upserts an item in the storage.
5775
+ * @param {T} item - The item to upsert.
5776
+ * @param {string} clientId - The client ID.
5777
+ * @param {AgentName} agentName - The agent name.
5778
+ * @param {StorageName} storageName - The storage name.
5779
+ * @returns {Promise<void>} - A promise that resolves when the operation is complete.
5780
+ * @template T
5781
+ */
5782
+ this.upsert = function (item, clientId, agentName, storageName) { return __awaiter(_this, void 0, void 0, function () {
5783
+ return __generator(this, function (_a) {
5784
+ switch (_a.label) {
5785
+ case 0:
5786
+ swarm.loggerService.log("StorageStatic upsert", {
5787
+ item: item,
5788
+ clientId: clientId,
5789
+ storageName: storageName,
5790
+ });
5791
+ swarm.storageValidationService.validate(storageName, "StorageStatic");
5792
+ if (!swarm.agentValidationService.hasStorage(agentName, storageName)) {
5793
+ throw new Error("agent-swarm StorageUtils ".concat(storageName, " not registered in ").concat(agentName, " (upsert)"));
5794
+ }
5795
+ return [4 /*yield*/, swarm.storagePublicService.upsert(item, clientId, storageName)];
5796
+ case 1: return [2 /*return*/, _a.sent()];
5797
+ }
5798
+ });
5799
+ }); };
5800
+ /**
5801
+ * Removes an item from the storage.
5802
+ * @param {IStorageData["id"]} itemId - The ID of the item to remove.
5803
+ * @param {string} clientId - The client ID.
5804
+ * @param {AgentName} agentName - The agent name.
5805
+ * @param {StorageName} storageName - The storage name.
5806
+ * @returns {Promise<void>} - A promise that resolves when the operation is complete.
5807
+ */
5808
+ this.remove = function (itemId, clientId, agentName, storageName) { return __awaiter(_this, void 0, void 0, function () {
5809
+ return __generator(this, function (_a) {
5810
+ switch (_a.label) {
5811
+ case 0:
5812
+ swarm.loggerService.log("StorageStatic remove", {
5813
+ itemId: itemId,
5814
+ clientId: clientId,
5815
+ storageName: storageName,
5816
+ });
5817
+ swarm.storageValidationService.validate(storageName, "StorageStatic");
5818
+ if (!swarm.agentValidationService.hasStorage(agentName, storageName)) {
5819
+ throw new Error("agent-swarm StorageUtils ".concat(storageName, " not registered in ").concat(agentName, " (remove)"));
5820
+ }
5821
+ return [4 /*yield*/, swarm.storagePublicService.remove(itemId, clientId, storageName)];
5822
+ case 1: return [2 /*return*/, _a.sent()];
5823
+ }
5824
+ });
5825
+ }); };
5826
+ /**
5827
+ * Gets an item from the storage.
5828
+ * @param {IStorageData["id"]} itemId - The ID of the item to get.
5829
+ * @param {string} clientId - The client ID.
5830
+ * @param {AgentName} agentName - The agent name.
5831
+ * @param {StorageName} storageName - The storage name.
5832
+ * @returns {Promise<T | null>} - A promise that resolves to the item or null if not found.
5833
+ * @template T
5834
+ */
5835
+ this.get = function (itemId, clientId, agentName, storageName) { return __awaiter(_this, void 0, void 0, function () {
5836
+ return __generator(this, function (_a) {
5837
+ switch (_a.label) {
5838
+ case 0:
5839
+ swarm.loggerService.log("StorageStatic get", {
5840
+ itemId: itemId,
5841
+ clientId: clientId,
5842
+ storageName: storageName,
5843
+ });
5844
+ swarm.storageValidationService.validate(storageName, "StorageStatic");
5845
+ if (!swarm.agentValidationService.hasStorage(agentName, storageName)) {
5846
+ throw new Error("agent-swarm StorageUtils ".concat(storageName, " not registered in ").concat(agentName, " (get)"));
5847
+ }
5848
+ return [4 /*yield*/, swarm.storagePublicService.get(itemId, clientId, storageName)];
5849
+ case 1: return [2 /*return*/, (_a.sent())];
5850
+ }
5851
+ });
5852
+ }); };
5853
+ /**
5854
+ * Lists items from the storage.
5855
+ * @param {string} clientId - The client ID.
5856
+ * @param {AgentName} agentName - The agent name.
5857
+ * @param {StorageName} storageName - The storage name.
5858
+ * @param {(item: T) => boolean} [filter] - Optional filter function.
5859
+ * @returns {Promise<T[]>} - A promise that resolves to an array of items.
5860
+ * @template T
5861
+ */
5862
+ this.list = function (clientId, agentName, storageName, filter) { return __awaiter(_this, void 0, void 0, function () {
5863
+ return __generator(this, function (_a) {
5864
+ switch (_a.label) {
5865
+ case 0:
5866
+ swarm.loggerService.log("StorageStatic list", {
5867
+ clientId: clientId,
5868
+ storageName: storageName,
5869
+ });
5870
+ swarm.storageValidationService.validate(storageName, "StorageStatic");
5871
+ if (!swarm.agentValidationService.hasStorage(agentName, storageName)) {
5872
+ throw new Error("agent-swarm StorageUtils ".concat(storageName, " not registered in ").concat(agentName, " (list)"));
5873
+ }
5874
+ return [4 /*yield*/, swarm.storagePublicService.list(clientId, storageName, filter)];
5875
+ case 1: return [2 /*return*/, (_a.sent())];
5876
+ }
5877
+ });
5878
+ }); };
5879
+ /**
5880
+ * Clears the storage.
5881
+ * @param {string} clientId - The client ID.
5882
+ * @param {AgentName} agentName - The agent name.
5883
+ * @param {StorageName} storageName - The storage name.
5884
+ * @returns {Promise<void>} - A promise that resolves when the operation is complete.
5885
+ */
5886
+ this.clear = function (clientId, agentName, storageName) { return __awaiter(_this, void 0, void 0, function () {
5887
+ return __generator(this, function (_a) {
5888
+ switch (_a.label) {
5889
+ case 0:
5890
+ swarm.loggerService.log("StorageStatic clear", {
5891
+ clientId: clientId,
5892
+ storageName: storageName,
5893
+ });
5894
+ swarm.storageValidationService.validate(storageName, "StorageStatic");
5895
+ if (!swarm.agentValidationService.hasStorage(agentName, storageName)) {
5896
+ throw new Error("agent-swarm StorageUtils ".concat(storageName, " not registered in ").concat(agentName, " (clear)"));
5897
+ }
5898
+ return [4 /*yield*/, swarm.storagePublicService.clear(clientId, storageName)];
5899
+ case 1: return [2 /*return*/, _a.sent()];
5900
+ }
5901
+ });
5902
+ }); };
5903
+ }
5904
+ return StorageUtils;
5905
+ }());
5906
+ var Storage = new StorageUtils();
5907
+
5908
+ export { ContextService, Storage, addAgent, addCompletion, addEmbedding, 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 };