agent-swarm-kit 1.0.67 → 1.0.69

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
@@ -236,6 +236,7 @@ var connectionServices$1 = {
236
236
  swarmConnectionService: Symbol('swarmConnectionService'),
237
237
  sessionConnectionService: Symbol('sessionConnectionService'),
238
238
  storageConnectionService: Symbol('storageConnectionService'),
239
+ stateConnectionService: Symbol('stateConnectionService'),
239
240
  };
240
241
  var schemaServices$1 = {
241
242
  completionSchemaService: Symbol('completionSchemaService'),
@@ -244,6 +245,7 @@ var schemaServices$1 = {
244
245
  toolSchemaService: Symbol('toolSchemaService'),
245
246
  embeddingSchemaService: Symbol('embeddingSchemaService'),
246
247
  storageSchemaService: Symbol('storageSchemaService'),
248
+ stateSchemaService: Symbol('stateSchemaService'),
247
249
  };
248
250
  var publicServices$1 = {
249
251
  agentPublicService: Symbol('agentPublicService'),
@@ -251,6 +253,7 @@ var publicServices$1 = {
251
253
  sessionPublicService: Symbol('sessionPublicService'),
252
254
  swarmPublicService: Symbol('swarmPublicService'),
253
255
  storagePublicService: Symbol('storagePublicService'),
256
+ statePublicService: Symbol('statePublicService'),
254
257
  };
255
258
  var validationServices$1 = {
256
259
  agentValidationService: Symbol('agentValidationService'),
@@ -1425,7 +1428,7 @@ var AgentConnectionService = /** @class */ (function () {
1425
1428
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
1426
1429
  return "".concat(clientId, "-").concat(agentName);
1427
1430
  }, function (clientId, agentName) {
1428
- var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, _b = _a.transform, transform = _b === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_TRANSFORM : _b, _c = _a.map, map = _c === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP : _c, callbacks = _a.callbacks, storages = _a.storages, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
1431
+ var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, _b = _a.transform, transform = _b === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_TRANSFORM : _b, _c = _a.map, map = _c === void 0 ? GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP : _c, callbacks = _a.callbacks, storages = _a.storages, states = _a.states, completionName = _a.completion, _d = _a.validate, validate = _d === void 0 ? validateDefault : _d;
1429
1432
  var completion = _this.completionSchemaService.get(completionName);
1430
1433
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
1431
1434
  storages === null || storages === void 0 ? void 0 : storages.forEach(function (storageName) {
@@ -1433,6 +1436,11 @@ var AgentConnectionService = /** @class */ (function () {
1433
1436
  .getStorage(clientId, storageName)
1434
1437
  .waitForInit();
1435
1438
  });
1439
+ states === null || states === void 0 ? void 0 : states.forEach(function (storageName) {
1440
+ return _this.storageConnectionService
1441
+ .getStorage(clientId, storageName)
1442
+ .waitForInit();
1443
+ });
1436
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));
1437
1445
  });
1438
1446
  /**
@@ -2783,6 +2791,7 @@ var AgentPublicService = /** @class */ (function () {
2783
2791
  agentName: agentName,
2784
2792
  swarmName: "",
2785
2793
  storageName: "",
2794
+ stateName: "",
2786
2795
  })];
2787
2796
  case 1: return [2 /*return*/, _a.sent()];
2788
2797
  }
@@ -2818,6 +2827,7 @@ var AgentPublicService = /** @class */ (function () {
2818
2827
  agentName: agentName,
2819
2828
  swarmName: "",
2820
2829
  storageName: "",
2830
+ stateName: "",
2821
2831
  })];
2822
2832
  case 1: return [2 /*return*/, _a.sent()];
2823
2833
  }
@@ -2850,6 +2860,7 @@ var AgentPublicService = /** @class */ (function () {
2850
2860
  agentName: agentName,
2851
2861
  swarmName: "",
2852
2862
  storageName: "",
2863
+ stateName: "",
2853
2864
  })];
2854
2865
  case 1: return [2 /*return*/, _a.sent()];
2855
2866
  }
@@ -2886,6 +2897,7 @@ var AgentPublicService = /** @class */ (function () {
2886
2897
  agentName: agentName,
2887
2898
  swarmName: "",
2888
2899
  storageName: "",
2900
+ stateName: "",
2889
2901
  })];
2890
2902
  case 1: return [2 /*return*/, _a.sent()];
2891
2903
  }
@@ -2920,6 +2932,7 @@ var AgentPublicService = /** @class */ (function () {
2920
2932
  agentName: agentName,
2921
2933
  swarmName: "",
2922
2934
  storageName: "",
2935
+ stateName: "",
2923
2936
  })];
2924
2937
  case 1: return [2 /*return*/, _a.sent()];
2925
2938
  }
@@ -2954,6 +2967,7 @@ var AgentPublicService = /** @class */ (function () {
2954
2967
  agentName: agentName,
2955
2968
  swarmName: "",
2956
2969
  storageName: "",
2970
+ stateName: "",
2957
2971
  })];
2958
2972
  case 1: return [2 /*return*/, _a.sent()];
2959
2973
  }
@@ -2986,6 +3000,7 @@ var AgentPublicService = /** @class */ (function () {
2986
3000
  agentName: agentName,
2987
3001
  swarmName: "",
2988
3002
  storageName: "",
3003
+ stateName: "",
2989
3004
  })];
2990
3005
  case 1: return [2 /*return*/, _a.sent()];
2991
3006
  }
@@ -3018,6 +3033,7 @@ var AgentPublicService = /** @class */ (function () {
3018
3033
  agentName: agentName,
3019
3034
  swarmName: "",
3020
3035
  storageName: "",
3036
+ stateName: "",
3021
3037
  })];
3022
3038
  case 1: return [2 /*return*/, _a.sent()];
3023
3039
  }
@@ -3050,6 +3066,7 @@ var AgentPublicService = /** @class */ (function () {
3050
3066
  agentName: agentName,
3051
3067
  swarmName: "",
3052
3068
  storageName: "",
3069
+ stateName: "",
3053
3070
  })];
3054
3071
  case 1: return [2 /*return*/, _a.sent()];
3055
3072
  }
@@ -3096,6 +3113,7 @@ var HistoryPublicService = /** @class */ (function () {
3096
3113
  agentName: agentName,
3097
3114
  swarmName: "",
3098
3115
  storageName: "",
3116
+ stateName: "",
3099
3117
  })];
3100
3118
  case 1: return [2 /*return*/, _a.sent()];
3101
3119
  }
@@ -3130,6 +3148,7 @@ var HistoryPublicService = /** @class */ (function () {
3130
3148
  agentName: agentName,
3131
3149
  swarmName: "",
3132
3150
  storageName: "",
3151
+ stateName: "",
3133
3152
  })];
3134
3153
  case 1: return [2 /*return*/, _a.sent()];
3135
3154
  }
@@ -3162,6 +3181,7 @@ var HistoryPublicService = /** @class */ (function () {
3162
3181
  agentName: agentName,
3163
3182
  swarmName: "",
3164
3183
  storageName: "",
3184
+ stateName: "",
3165
3185
  })];
3166
3186
  case 1: return [2 /*return*/, _a.sent()];
3167
3187
  }
@@ -3194,6 +3214,7 @@ var HistoryPublicService = /** @class */ (function () {
3194
3214
  agentName: agentName,
3195
3215
  swarmName: "",
3196
3216
  storageName: "",
3217
+ stateName: "",
3197
3218
  })];
3198
3219
  case 1: return [2 /*return*/, _a.sent()];
3199
3220
  }
@@ -3240,6 +3261,7 @@ var SessionPublicService = /** @class */ (function () {
3240
3261
  swarmName: swarmName,
3241
3262
  agentName: "",
3242
3263
  storageName: "",
3264
+ stateName: "",
3243
3265
  })];
3244
3266
  case 1: return [2 /*return*/, _a.sent()];
3245
3267
  }
@@ -3275,6 +3297,7 @@ var SessionPublicService = /** @class */ (function () {
3275
3297
  swarmName: swarmName,
3276
3298
  agentName: "",
3277
3299
  storageName: "",
3300
+ stateName: "",
3278
3301
  })];
3279
3302
  case 1: return [2 /*return*/, _a.sent()];
3280
3303
  }
@@ -3309,6 +3332,7 @@ var SessionPublicService = /** @class */ (function () {
3309
3332
  swarmName: swarmName,
3310
3333
  agentName: "",
3311
3334
  storageName: "",
3335
+ stateName: "",
3312
3336
  })];
3313
3337
  case 1: return [2 /*return*/, _a.sent()];
3314
3338
  }
@@ -3322,6 +3346,7 @@ var SessionPublicService = /** @class */ (function () {
3322
3346
  swarmName: swarmName,
3323
3347
  agentName: "",
3324
3348
  storageName: "",
3349
+ stateName: "",
3325
3350
  });
3326
3351
  };
3327
3352
  }, {
@@ -3329,6 +3354,7 @@ var SessionPublicService = /** @class */ (function () {
3329
3354
  swarmName: swarmName,
3330
3355
  agentName: "",
3331
3356
  storageName: "",
3357
+ stateName: "",
3332
3358
  });
3333
3359
  };
3334
3360
  /**
@@ -3362,6 +3388,7 @@ var SessionPublicService = /** @class */ (function () {
3362
3388
  swarmName: swarmName,
3363
3389
  agentName: "",
3364
3390
  storageName: "",
3391
+ stateName: "",
3365
3392
  })];
3366
3393
  case 1: return [2 /*return*/, _a.sent()];
3367
3394
  }
@@ -3396,6 +3423,7 @@ var SessionPublicService = /** @class */ (function () {
3396
3423
  swarmName: swarmName,
3397
3424
  agentName: "",
3398
3425
  storageName: "",
3426
+ stateName: "",
3399
3427
  })];
3400
3428
  case 1: return [2 /*return*/, _a.sent()];
3401
3429
  }
@@ -3430,6 +3458,7 @@ var SessionPublicService = /** @class */ (function () {
3430
3458
  swarmName: swarmName,
3431
3459
  agentName: "",
3432
3460
  storageName: "",
3461
+ stateName: "",
3433
3462
  })];
3434
3463
  case 1: return [2 /*return*/, _a.sent()];
3435
3464
  }
@@ -3462,6 +3491,7 @@ var SessionPublicService = /** @class */ (function () {
3462
3491
  swarmName: swarmName,
3463
3492
  agentName: "",
3464
3493
  storageName: "",
3494
+ stateName: "",
3465
3495
  })];
3466
3496
  case 1: return [2 /*return*/, _a.sent()];
3467
3497
  }
@@ -3494,6 +3524,7 @@ var SessionPublicService = /** @class */ (function () {
3494
3524
  swarmName: swarmName,
3495
3525
  agentName: "",
3496
3526
  storageName: "",
3527
+ stateName: "",
3497
3528
  })];
3498
3529
  case 1: return [2 /*return*/, _a.sent()];
3499
3530
  }
@@ -3538,6 +3569,7 @@ var SwarmPublicService = /** @class */ (function () {
3538
3569
  swarmName: swarmName,
3539
3570
  agentName: "",
3540
3571
  storageName: "",
3572
+ stateName: "",
3541
3573
  })];
3542
3574
  case 1: return [2 /*return*/, _a.sent()];
3543
3575
  }
@@ -3570,6 +3602,7 @@ var SwarmPublicService = /** @class */ (function () {
3570
3602
  swarmName: swarmName,
3571
3603
  agentName: "",
3572
3604
  storageName: "",
3605
+ stateName: "",
3573
3606
  })];
3574
3607
  case 1: return [2 /*return*/, _a.sent()];
3575
3608
  }
@@ -3602,6 +3635,7 @@ var SwarmPublicService = /** @class */ (function () {
3602
3635
  swarmName: swarmName,
3603
3636
  agentName: "",
3604
3637
  storageName: "",
3638
+ stateName: "",
3605
3639
  })];
3606
3640
  case 1: return [2 /*return*/, _a.sent()];
3607
3641
  }
@@ -3638,6 +3672,7 @@ var SwarmPublicService = /** @class */ (function () {
3638
3672
  swarmName: swarmName,
3639
3673
  agentName: "",
3640
3674
  storageName: "",
3675
+ stateName: "",
3641
3676
  })];
3642
3677
  case 1: return [2 /*return*/, _a.sent()];
3643
3678
  }
@@ -3672,6 +3707,7 @@ var SwarmPublicService = /** @class */ (function () {
3672
3707
  swarmName: swarmName,
3673
3708
  agentName: "",
3674
3709
  storageName: "",
3710
+ stateName: "",
3675
3711
  })];
3676
3712
  case 1: return [2 /*return*/, _a.sent()];
3677
3713
  }
@@ -3704,6 +3740,7 @@ var SwarmPublicService = /** @class */ (function () {
3704
3740
  swarmName: swarmName,
3705
3741
  agentName: "",
3706
3742
  storageName: "",
3743
+ stateName: "",
3707
3744
  })];
3708
3745
  case 1: return [2 /*return*/, _a.sent()];
3709
3746
  }
@@ -3736,6 +3773,18 @@ var AgentValidationService = /** @class */ (function () {
3736
3773
  }
3737
3774
  return _this._agentMap.get(agentName).storages;
3738
3775
  };
3776
+ /**
3777
+ * Retrieves the states used by the agent
3778
+ * @param {agentName} agentName - The name of the swarm.
3779
+ * @returns {string[]} The list of state names.
3780
+ * @throws Will throw an error if the swarm is not found.
3781
+ */
3782
+ this.getStateList = function (agentName) {
3783
+ if (!_this._agentMap.has(agentName)) {
3784
+ throw new Error("agent-swarm agent ".concat(agentName, " not exist (getStateList)"));
3785
+ }
3786
+ return _this._agentMap.get(agentName).states;
3787
+ };
3739
3788
  /**
3740
3789
  * Adds a new agent to the validation service.
3741
3790
  * @param {AgentName} agentName - The name of the agent.
@@ -3769,6 +3818,23 @@ var AgentValidationService = /** @class */ (function () {
3769
3818
  var _a = _this._agentMap.get(agentName).storages, storages = _a === void 0 ? [] : _a;
3770
3819
  return storages.includes(storageName);
3771
3820
  });
3821
+ /**
3822
+ * Check if agent got registered state
3823
+ */
3824
+ this.hasState = functoolsKit.memoize(function (_a) {
3825
+ var _b = __read(_a, 2), agentName = _b[0], stateName = _b[1];
3826
+ return "".concat(agentName, "-").concat(stateName);
3827
+ }, function (agentName, stateName) {
3828
+ _this.loggerService.log("agentValidationService hasState", {
3829
+ agentName: agentName,
3830
+ stateName: stateName,
3831
+ });
3832
+ if (!_this._agentMap.has(agentName)) {
3833
+ throw new Error("agent-swarm agent ".concat(agentName, " not exist (hasState)"));
3834
+ }
3835
+ var _a = _this._agentMap.get(agentName).states, states = _a === void 0 ? [] : _a;
3836
+ return states.includes(stateName);
3837
+ });
3772
3838
  /**
3773
3839
  * Validates an agent by its name and source.
3774
3840
  * @param {AgentName} agentName - The name of the agent.
@@ -3862,6 +3928,7 @@ var SessionValidationService = /** @class */ (function () {
3862
3928
  this._storageSwarmMap = new Map();
3863
3929
  this._historySwarmMap = new Map();
3864
3930
  this._agentSwarmMap = new Map();
3931
+ this._stateSwarmMap = new Map();
3865
3932
  this._sessionSwarmMap = new Map();
3866
3933
  this._sessionModeMap = new Map();
3867
3934
  /**
@@ -3941,6 +4008,26 @@ var SessionValidationService = /** @class */ (function () {
3941
4008
  _this._storageSwarmMap.set(sessionId, [storageName]);
3942
4009
  }
3943
4010
  };
4011
+ /**
4012
+ * Adds a state usage to a session.
4013
+ * @param {SessionId} sessionId - The ID of the session.
4014
+ * @param {StateName} stateName - The name of the state.
4015
+ */
4016
+ this.addStateUsage = function (sessionId, stateName) {
4017
+ _this.loggerService.log("sessionValidationService addStateUsage", {
4018
+ sessionId: sessionId,
4019
+ stateName: stateName,
4020
+ });
4021
+ if (_this._stateSwarmMap.has(sessionId)) {
4022
+ var states = _this._stateSwarmMap.get(sessionId);
4023
+ if (!states.includes(stateName)) {
4024
+ states.push(stateName);
4025
+ }
4026
+ }
4027
+ else {
4028
+ _this._stateSwarmMap.set(sessionId, [stateName]);
4029
+ }
4030
+ };
3944
4031
  /**
3945
4032
  * Removes an agent usage from a session.
3946
4033
  * @param {SessionId} sessionId - The ID of the session.
@@ -3992,11 +4079,11 @@ var SessionValidationService = /** @class */ (function () {
3992
4079
  }
3993
4080
  };
3994
4081
  /**
3995
- * Removes a storage usage from a session.
3996
- * @param {SessionId} sessionId - The ID of the session.
3997
- * @param {StorageName} storageName - The name of the storage.
3998
- * @throws Will throw an error if no storages are found for the session.
3999
- */
4082
+ * Removes a storage usage from a session.
4083
+ * @param {SessionId} sessionId - The ID of the session.
4084
+ * @param {StorageName} storageName - The name of the storage.
4085
+ * @throws Will throw an error if no storages are found for the session.
4086
+ */
4000
4087
  this.removeStorageUsage = function (sessionId, storageName) {
4001
4088
  _this.loggerService.log("sessionValidationService removeStorageUsage", {
4002
4089
  sessionId: sessionId,
@@ -4016,6 +4103,31 @@ var SessionValidationService = /** @class */ (function () {
4016
4103
  throw new Error("No agents found for sessionId=".concat(sessionId));
4017
4104
  }
4018
4105
  };
4106
+ /**
4107
+ * Removes a state usage from a session.
4108
+ * @param {SessionId} sessionId - The ID of the session.
4109
+ * @param {StateName} stateName - The name of the state.
4110
+ * @throws Will throw an error if no states are found for the session.
4111
+ */
4112
+ this.removeStateUsage = function (sessionId, stateName) {
4113
+ _this.loggerService.log("sessionValidationService removeStateUsage", {
4114
+ sessionId: sessionId,
4115
+ stateName: stateName,
4116
+ });
4117
+ if (_this._stateSwarmMap.has(sessionId)) {
4118
+ var agents = _this._stateSwarmMap.get(sessionId);
4119
+ var agentIndex = agents.indexOf(stateName);
4120
+ if (agentIndex !== -1) {
4121
+ agents.splice(agentIndex, 1);
4122
+ }
4123
+ if (agents.length === 0) {
4124
+ _this._stateSwarmMap.delete(sessionId);
4125
+ }
4126
+ }
4127
+ else {
4128
+ throw new Error("No agents found for sessionId=".concat(sessionId));
4129
+ }
4130
+ };
4019
4131
  /**
4020
4132
  * Gets the mode of a session.
4021
4133
  * @param {SessionId} clientId - The ID of the client.
@@ -4560,6 +4672,9 @@ var StorageConnectionService = /** @class */ (function () {
4560
4672
  }, function (clientId, storageName) {
4561
4673
  var _a = _this.storageSchemaService.get(storageName), createIndex = _a.createIndex, getData = _a.getData, embeddingName = _a.embedding, shared = _a.shared, callbacks = _a.callbacks;
4562
4674
  var _b = _this.embeddingSchemaService.get(embeddingName), calculateSimilarity = _b.calculateSimilarity, createEmbedding = _b.createEmbedding, embedding = _b.callbacks;
4675
+ if (!shared) {
4676
+ throw new Error("agent-swarm storage not shared storageName=".concat(storageName));
4677
+ }
4563
4678
  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 }));
4564
4679
  });
4565
4680
  /**
@@ -4779,6 +4894,7 @@ var StoragePublicService = /** @class */ (function () {
4779
4894
  storageName: storageName,
4780
4895
  agentName: "",
4781
4896
  swarmName: "",
4897
+ stateName: "",
4782
4898
  })];
4783
4899
  case 1: return [2 /*return*/, _a.sent()];
4784
4900
  }
@@ -4811,6 +4927,7 @@ var StoragePublicService = /** @class */ (function () {
4811
4927
  storageName: storageName,
4812
4928
  agentName: "",
4813
4929
  swarmName: "",
4930
+ stateName: "",
4814
4931
  })];
4815
4932
  case 1: return [2 /*return*/, _a.sent()];
4816
4933
  }
@@ -4843,6 +4960,7 @@ var StoragePublicService = /** @class */ (function () {
4843
4960
  storageName: storageName,
4844
4961
  agentName: "",
4845
4962
  swarmName: "",
4963
+ stateName: "",
4846
4964
  })];
4847
4965
  case 1: return [2 /*return*/, _a.sent()];
4848
4966
  }
@@ -4875,6 +4993,7 @@ var StoragePublicService = /** @class */ (function () {
4875
4993
  storageName: storageName,
4876
4994
  agentName: "",
4877
4995
  swarmName: "",
4996
+ stateName: "",
4878
4997
  })];
4879
4998
  case 1: return [2 /*return*/, _a.sent()];
4880
4999
  }
@@ -4906,6 +5025,7 @@ var StoragePublicService = /** @class */ (function () {
4906
5025
  storageName: storageName,
4907
5026
  agentName: "",
4908
5027
  swarmName: "",
5028
+ stateName: "",
4909
5029
  })];
4910
5030
  case 1: return [2 /*return*/, _a.sent()];
4911
5031
  }
@@ -4936,6 +5056,7 @@ var StoragePublicService = /** @class */ (function () {
4936
5056
  storageName: storageName,
4937
5057
  agentName: "",
4938
5058
  swarmName: "",
5059
+ stateName: "",
4939
5060
  })];
4940
5061
  case 1: return [2 /*return*/, _a.sent()];
4941
5062
  }
@@ -4968,6 +5089,7 @@ var StoragePublicService = /** @class */ (function () {
4968
5089
  storageName: storageName,
4969
5090
  agentName: "",
4970
5091
  swarmName: "",
5092
+ stateName: "",
4971
5093
  })];
4972
5094
  case 1: return [2 /*return*/, _a.sent()];
4973
5095
  }
@@ -5074,6 +5196,423 @@ var EmbeddingValidationService = /** @class */ (function () {
5074
5196
  return EmbeddingValidationService;
5075
5197
  }());
5076
5198
 
5199
+ /**
5200
+ * Service for managing state schemas.
5201
+ */
5202
+ var StateSchemaService = /** @class */ (function () {
5203
+ function StateSchemaService() {
5204
+ var _this = this;
5205
+ this.loggerService = inject(TYPES.loggerService);
5206
+ this.registry = new functoolsKit.ToolRegistry("stateSchemaService");
5207
+ /**
5208
+ * Registers a new state schema.
5209
+ * @param {StateName} key - The key for the schema.
5210
+ * @param {IStateSchema} value - The schema to register.
5211
+ */
5212
+ this.register = function (key, value) {
5213
+ _this.loggerService.log("stateSchemaService register", { key: key });
5214
+ _this.registry = _this.registry.register(key, value);
5215
+ };
5216
+ /**
5217
+ * Retrieves a state schema by key.
5218
+ * @param {StateName} key - The key of the schema to retrieve.
5219
+ * @returns {IStateSchema} The retrieved schema.
5220
+ */
5221
+ this.get = function (key) {
5222
+ _this.loggerService.log("stateSchemaService get", { key: key });
5223
+ return _this.registry.get(key);
5224
+ };
5225
+ }
5226
+ return StateSchemaService;
5227
+ }());
5228
+
5229
+ /**
5230
+ * Class representing the client state.
5231
+ * @template State - The type of the state data.
5232
+ * @implements {IState<State>}
5233
+ */
5234
+ var ClientState = /** @class */ (function () {
5235
+ /**
5236
+ * Creates an instance of ClientState.
5237
+ * @param {IStateParams<State>} params - The state parameters.
5238
+ */
5239
+ function ClientState(params) {
5240
+ var _this = this;
5241
+ var _a;
5242
+ this.params = params;
5243
+ this._state = null;
5244
+ this.dispatch = functoolsKit.queued(function (action, payload) { return __awaiter(_this, void 0, void 0, function () {
5245
+ var _a;
5246
+ return __generator(this, function (_b) {
5247
+ switch (_b.label) {
5248
+ case 0:
5249
+ if (action === "read") {
5250
+ return [2 /*return*/, this._state];
5251
+ }
5252
+ if (!(action === "write")) return [3 /*break*/, 2];
5253
+ console.assert(payload, "agent-swarm ClientState write action undefined payload");
5254
+ _a = this;
5255
+ return [4 /*yield*/, payload(this._state)];
5256
+ case 1: return [2 /*return*/, _a._state = _b.sent()];
5257
+ case 2: throw new Error("agent-swarm ClientState unknown action");
5258
+ }
5259
+ });
5260
+ }); });
5261
+ /**
5262
+ * Waits for the state to initialize.
5263
+ * @returns {Promise<void>}
5264
+ */
5265
+ this.waitForInit = functoolsKit.singleshot(function () { return __awaiter(_this, void 0, void 0, function () {
5266
+ var _a;
5267
+ var _b;
5268
+ return __generator(this, function (_c) {
5269
+ switch (_c.label) {
5270
+ case 0:
5271
+ this.params.logger.debug("ClientStorage stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " waitForInit"));
5272
+ _a = this;
5273
+ return [4 /*yield*/, this.params.getState(this.params.clientId, this.params.stateName)];
5274
+ case 1:
5275
+ _a._state = _c.sent();
5276
+ this.params.logger.debug("ClientStorage stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " waitForInit output"), { initialState: this._state });
5277
+ if ((_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onLoad) {
5278
+ this.params.callbacks.onLoad(this._state, this.params.clientId, this.params.stateName);
5279
+ }
5280
+ return [2 /*return*/];
5281
+ }
5282
+ });
5283
+ }); });
5284
+ /**
5285
+ * Sets the state using the provided dispatch function.
5286
+ * @param {DispatchFn<State>} dispatchFn - The dispatch function.
5287
+ * @returns {Promise<State>}
5288
+ */
5289
+ this.setState = function (dispatchFn) { return __awaiter(_this, void 0, void 0, function () {
5290
+ var _this = this;
5291
+ var _a;
5292
+ return __generator(this, function (_b) {
5293
+ switch (_b.label) {
5294
+ case 0:
5295
+ this.params.logger.debug("ClientStorage stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " setState"));
5296
+ return [4 /*yield*/, this.dispatch("write", function (currentState) { return __awaiter(_this, void 0, void 0, function () {
5297
+ var _a, _b, middleware, e_1_1;
5298
+ var e_1, _c;
5299
+ return __generator(this, function (_d) {
5300
+ switch (_d.label) {
5301
+ case 0:
5302
+ _d.trys.push([0, 5, 6, 7]);
5303
+ _a = __values(this.params.middlewares), _b = _a.next();
5304
+ _d.label = 1;
5305
+ case 1:
5306
+ if (!!_b.done) return [3 /*break*/, 4];
5307
+ middleware = _b.value;
5308
+ return [4 /*yield*/, middleware(currentState, this.params.clientId, this.params.stateName)];
5309
+ case 2:
5310
+ currentState = _d.sent();
5311
+ _d.label = 3;
5312
+ case 3:
5313
+ _b = _a.next();
5314
+ return [3 /*break*/, 1];
5315
+ case 4: return [3 /*break*/, 7];
5316
+ case 5:
5317
+ e_1_1 = _d.sent();
5318
+ e_1 = { error: e_1_1 };
5319
+ return [3 /*break*/, 7];
5320
+ case 6:
5321
+ try {
5322
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
5323
+ }
5324
+ finally { if (e_1) throw e_1.error; }
5325
+ return [7 /*endfinally*/];
5326
+ case 7: return [4 /*yield*/, dispatchFn(currentState)];
5327
+ case 8: return [2 /*return*/, _d.sent()];
5328
+ }
5329
+ });
5330
+ }); })];
5331
+ case 1:
5332
+ _b.sent();
5333
+ this.params.logger.debug("ClientStorage stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " setState output"), { pendingState: this._state });
5334
+ this.params.setState &&
5335
+ this.params.setState(this._state, this.params.clientId, this.params.stateName);
5336
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onWrite) {
5337
+ this.params.callbacks.onWrite(this._state, this.params.clientId, this.params.stateName);
5338
+ }
5339
+ return [2 /*return*/, this._state];
5340
+ }
5341
+ });
5342
+ }); };
5343
+ /**
5344
+ * Gets the current state.
5345
+ * @returns {Promise<State>}
5346
+ */
5347
+ this.getState = function () { return __awaiter(_this, void 0, void 0, function () {
5348
+ var _a;
5349
+ return __generator(this, function (_b) {
5350
+ switch (_b.label) {
5351
+ case 0:
5352
+ this.params.logger.debug("ClientStorage stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " getState"));
5353
+ return [4 /*yield*/, this.dispatch("read")];
5354
+ case 1:
5355
+ _b.sent();
5356
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onRead) {
5357
+ this.params.callbacks.onRead(this._state, this.params.clientId, this.params.stateName);
5358
+ }
5359
+ return [2 /*return*/, this._state];
5360
+ }
5361
+ });
5362
+ }); };
5363
+ /**
5364
+ * Disposes of the state.
5365
+ * @returns {Promise<void>}
5366
+ */
5367
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
5368
+ var _a;
5369
+ return __generator(this, function (_b) {
5370
+ this.params.logger.debug("ClientStorage stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " dispose"));
5371
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onDispose) {
5372
+ this.params.callbacks.onDispose(this.params.clientId, this.params.stateName);
5373
+ }
5374
+ return [2 /*return*/];
5375
+ });
5376
+ }); };
5377
+ if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
5378
+ this.params.callbacks.onInit(this.params.clientId, this.params.stateName);
5379
+ }
5380
+ }
5381
+ return ClientState;
5382
+ }());
5383
+
5384
+ /**
5385
+ * Service for managing state connections.
5386
+ * @template T - The type of state data.
5387
+ * @implements {IState<T>}
5388
+ */
5389
+ var StateConnectionService = /** @class */ (function () {
5390
+ function StateConnectionService() {
5391
+ var _this = this;
5392
+ this.loggerService = inject(TYPES.loggerService);
5393
+ this.contextService = inject(TYPES.contextService);
5394
+ this.stateSchemaService = inject(TYPES.stateSchemaService);
5395
+ this.sessionValidationService = inject(TYPES.sessionValidationService);
5396
+ /**
5397
+ * Memoized function to get a state reference.
5398
+ * @param {string} clientId - The client ID.
5399
+ * @param {StateName} stateName - The state name.
5400
+ * @returns {ClientState} The client state.
5401
+ */
5402
+ this.getStateRef = functoolsKit.memoize(function (_a) {
5403
+ var _b = __read(_a, 2), clientId = _b[0], stateName = _b[1];
5404
+ return "".concat(clientId, "-").concat(stateName);
5405
+ }, function (clientId, stateName) {
5406
+ _this.sessionValidationService.addStateUsage(clientId, stateName);
5407
+ var _a = _this.stateSchemaService.get(stateName), getState = _a.getState, setState = _a.setState, _b = _a.middlewares, middlewares = _b === void 0 ? [] : _b, callbacks = _a.callbacks;
5408
+ return new ClientState({
5409
+ clientId: clientId,
5410
+ stateName: stateName,
5411
+ logger: _this.loggerService,
5412
+ setState: setState
5413
+ ? functoolsKit.queued(function () {
5414
+ var args = [];
5415
+ for (var _i = 0; _i < arguments.length; _i++) {
5416
+ args[_i] = arguments[_i];
5417
+ }
5418
+ return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
5419
+ switch (_a.label) {
5420
+ case 0: return [4 /*yield*/, setState.apply(void 0, __spreadArray([], __read(args), false))];
5421
+ case 1: return [2 /*return*/, _a.sent()];
5422
+ }
5423
+ }); });
5424
+ })
5425
+ : setState,
5426
+ getState: getState,
5427
+ middlewares: middlewares,
5428
+ callbacks: callbacks,
5429
+ });
5430
+ });
5431
+ /**
5432
+ * Sets the state.
5433
+ * @param {function(T): Promise<T>} dispatchFn - The function to dispatch the new state.
5434
+ * @returns {Promise<T>} The new state.
5435
+ */
5436
+ this.setState = function (dispatchFn) { return __awaiter(_this, void 0, void 0, function () {
5437
+ var state;
5438
+ return __generator(this, function (_a) {
5439
+ switch (_a.label) {
5440
+ case 0:
5441
+ this.loggerService.log("stateConnectionService setState", {
5442
+ context: this.contextService.context,
5443
+ });
5444
+ state = this.getStateRef(this.contextService.context.clientId, this.contextService.context.stateName);
5445
+ return [4 /*yield*/, state.waitForInit()];
5446
+ case 1:
5447
+ _a.sent();
5448
+ return [4 /*yield*/, state.setState(dispatchFn)];
5449
+ case 2: return [2 /*return*/, _a.sent()];
5450
+ }
5451
+ });
5452
+ }); };
5453
+ /**
5454
+ * Gets the state.
5455
+ * @returns {Promise<T>} The current state.
5456
+ */
5457
+ this.getState = function () { return __awaiter(_this, void 0, void 0, function () {
5458
+ var state;
5459
+ return __generator(this, function (_a) {
5460
+ switch (_a.label) {
5461
+ case 0:
5462
+ this.loggerService.log("stateConnectionService getState", {
5463
+ context: this.contextService.context,
5464
+ });
5465
+ state = this.getStateRef(this.contextService.context.clientId, this.contextService.context.stateName);
5466
+ return [4 /*yield*/, state.waitForInit()];
5467
+ case 1:
5468
+ _a.sent();
5469
+ return [4 /*yield*/, state.getState()];
5470
+ case 2: return [2 /*return*/, _a.sent()];
5471
+ }
5472
+ });
5473
+ }); };
5474
+ /**
5475
+ * Disposes the state connection.
5476
+ * @returns {Promise<void>}
5477
+ */
5478
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
5479
+ var key, state;
5480
+ return __generator(this, function (_a) {
5481
+ switch (_a.label) {
5482
+ case 0:
5483
+ this.loggerService.log("stateConnectionService dispose", {
5484
+ context: this.contextService.context,
5485
+ });
5486
+ key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.stateName);
5487
+ if (!this.getStateRef.has(key)) {
5488
+ return [2 /*return*/];
5489
+ }
5490
+ state = this.getStateRef(this.contextService.context.clientId, this.contextService.context.stateName);
5491
+ return [4 /*yield*/, state.waitForInit()];
5492
+ case 1:
5493
+ _a.sent();
5494
+ return [4 /*yield*/, state.dispose()];
5495
+ case 2:
5496
+ _a.sent();
5497
+ this.getStateRef.clear(key);
5498
+ this.sessionValidationService.removeStateUsage(this.contextService.context.clientId, this.contextService.context.stateName);
5499
+ return [2 /*return*/];
5500
+ }
5501
+ });
5502
+ }); };
5503
+ }
5504
+ return StateConnectionService;
5505
+ }());
5506
+
5507
+ var StatePublicService = /** @class */ (function () {
5508
+ function StatePublicService() {
5509
+ var _this = this;
5510
+ this.loggerService = inject(TYPES.loggerService);
5511
+ this.stateConnectionService = inject(TYPES.stateConnectionService);
5512
+ /**
5513
+ * Sets the state using the provided dispatch function.
5514
+ * @param {function(T): Promise<T>} dispatchFn - The function to dispatch the state change.
5515
+ * @param {string} clientId - The client ID.
5516
+ * @param {StateName} stateName - The name of the state.
5517
+ * @returns {Promise<T>} - The updated state.
5518
+ */
5519
+ this.setState = function (dispatchFn, clientId, stateName) { return __awaiter(_this, void 0, void 0, function () {
5520
+ var _this = this;
5521
+ return __generator(this, function (_a) {
5522
+ switch (_a.label) {
5523
+ case 0:
5524
+ this.loggerService.log("statePublicService setState", {
5525
+ clientId: clientId,
5526
+ stateName: stateName,
5527
+ });
5528
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
5529
+ return __generator(this, function (_a) {
5530
+ switch (_a.label) {
5531
+ case 0: return [4 /*yield*/, this.stateConnectionService.setState(dispatchFn)];
5532
+ case 1: return [2 /*return*/, _a.sent()];
5533
+ }
5534
+ });
5535
+ }); }, {
5536
+ clientId: clientId,
5537
+ stateName: stateName,
5538
+ agentName: "",
5539
+ swarmName: "",
5540
+ storageName: "",
5541
+ })];
5542
+ case 1: return [2 /*return*/, _a.sent()];
5543
+ }
5544
+ });
5545
+ }); };
5546
+ /**
5547
+ * Gets the current state.
5548
+ * @param {string} clientId - The client ID.
5549
+ * @param {StateName} stateName - The name of the state.
5550
+ * @returns {Promise<T>} - The current state.
5551
+ */
5552
+ this.getState = function (clientId, stateName) { return __awaiter(_this, void 0, void 0, function () {
5553
+ var _this = this;
5554
+ return __generator(this, function (_a) {
5555
+ switch (_a.label) {
5556
+ case 0:
5557
+ this.loggerService.log("statePublicService getState", {
5558
+ clientId: clientId,
5559
+ stateName: stateName,
5560
+ });
5561
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
5562
+ return __generator(this, function (_a) {
5563
+ switch (_a.label) {
5564
+ case 0: return [4 /*yield*/, this.stateConnectionService.getState()];
5565
+ case 1: return [2 /*return*/, _a.sent()];
5566
+ }
5567
+ });
5568
+ }); }, {
5569
+ clientId: clientId,
5570
+ stateName: stateName,
5571
+ agentName: "",
5572
+ swarmName: "",
5573
+ storageName: "",
5574
+ })];
5575
+ case 1: return [2 /*return*/, _a.sent()];
5576
+ }
5577
+ });
5578
+ }); };
5579
+ /**
5580
+ * Disposes the state.
5581
+ * @param {string} clientId - The client ID.
5582
+ * @param {StateName} stateName - The name of the state.
5583
+ * @returns {Promise<void>} - A promise that resolves when the state is disposed.
5584
+ */
5585
+ this.dispose = function (clientId, stateName) { return __awaiter(_this, void 0, void 0, function () {
5586
+ var _this = this;
5587
+ return __generator(this, function (_a) {
5588
+ switch (_a.label) {
5589
+ case 0:
5590
+ this.loggerService.log("statePublicService dispose", {
5591
+ clientId: clientId,
5592
+ stateName: stateName,
5593
+ });
5594
+ return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
5595
+ return __generator(this, function (_a) {
5596
+ switch (_a.label) {
5597
+ case 0: return [4 /*yield*/, this.stateConnectionService.dispose()];
5598
+ case 1: return [2 /*return*/, _a.sent()];
5599
+ }
5600
+ });
5601
+ }); }, {
5602
+ clientId: clientId,
5603
+ stateName: stateName,
5604
+ agentName: "",
5605
+ swarmName: "",
5606
+ storageName: "",
5607
+ })];
5608
+ case 1: return [2 /*return*/, _a.sent()];
5609
+ }
5610
+ });
5611
+ }); };
5612
+ }
5613
+ return StatePublicService;
5614
+ }());
5615
+
5077
5616
  {
5078
5617
  provide(TYPES.loggerService, function () { return new LoggerService(); });
5079
5618
  provide(TYPES.contextService, function () { return new ContextService(); });
@@ -5084,6 +5623,7 @@ var EmbeddingValidationService = /** @class */ (function () {
5084
5623
  provide(TYPES.swarmConnectionService, function () { return new SwarmConnectionService(); });
5085
5624
  provide(TYPES.sessionConnectionService, function () { return new SessionConnectionService(); });
5086
5625
  provide(TYPES.storageConnectionService, function () { return new StorageConnectionService(); });
5626
+ provide(TYPES.stateConnectionService, function () { return new StateConnectionService(); });
5087
5627
  }
5088
5628
  {
5089
5629
  provide(TYPES.agentSchemaService, function () { return new AgentSchemaService(); });
@@ -5092,6 +5632,7 @@ var EmbeddingValidationService = /** @class */ (function () {
5092
5632
  provide(TYPES.completionSchemaService, function () { return new CompletionSchemaService(); });
5093
5633
  provide(TYPES.embeddingSchemaService, function () { return new EmbeddingSchemaService(); });
5094
5634
  provide(TYPES.storageSchemaService, function () { return new StorageSchemaService(); });
5635
+ provide(TYPES.stateSchemaService, function () { return new StateSchemaService(); });
5095
5636
  }
5096
5637
  {
5097
5638
  provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
@@ -5099,6 +5640,7 @@ var EmbeddingValidationService = /** @class */ (function () {
5099
5640
  provide(TYPES.sessionPublicService, function () { return new SessionPublicService(); });
5100
5641
  provide(TYPES.swarmPublicService, function () { return new SwarmPublicService(); });
5101
5642
  provide(TYPES.storagePublicService, function () { return new StoragePublicService(); });
5643
+ provide(TYPES.statePublicService, function () { return new StatePublicService(); });
5102
5644
  }
5103
5645
  {
5104
5646
  provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
@@ -5126,6 +5668,7 @@ var connectionServices = {
5126
5668
  swarmConnectionService: inject(TYPES.swarmConnectionService),
5127
5669
  sessionConnectionService: inject(TYPES.sessionConnectionService),
5128
5670
  storageConnectionService: inject(TYPES.storageConnectionService),
5671
+ stateConnectionService: inject(TYPES.stateConnectionService),
5129
5672
  };
5130
5673
  var schemaServices = {
5131
5674
  agentSchemaService: inject(TYPES.agentSchemaService),
@@ -5134,6 +5677,7 @@ var schemaServices = {
5134
5677
  completionSchemaService: inject(TYPES.completionSchemaService),
5135
5678
  embeddingSchemaService: inject(TYPES.embeddingSchemaService),
5136
5679
  storageSchemaService: inject(TYPES.storageSchemaService),
5680
+ stateSchemaService: inject(TYPES.stateSchemaService),
5137
5681
  };
5138
5682
  var publicServices = {
5139
5683
  agentPublicService: inject(TYPES.agentPublicService),
@@ -5141,6 +5685,7 @@ var publicServices = {
5141
5685
  sessionPublicService: inject(TYPES.sessionPublicService),
5142
5686
  swarmPublicService: inject(TYPES.swarmPublicService),
5143
5687
  storagePublicService: inject(TYPES.storagePublicService),
5688
+ statePublicService: inject(TYPES.statePublicService),
5144
5689
  };
5145
5690
  var validationServices = {
5146
5691
  agentValidationService: inject(TYPES.agentValidationService),
@@ -5216,6 +5761,20 @@ var addTool = function (toolSchema) {
5216
5761
  return toolSchema.toolName;
5217
5762
  };
5218
5763
 
5764
+ /**
5765
+ * Adds a new state to the state registry. The swarm takes only those states which was registered
5766
+ *
5767
+ * @param {IStateSchema} stateSchema - The schema of the state to be added.
5768
+ * @returns {string} The name of the added state.
5769
+ */
5770
+ var addState = function (stateSchema) {
5771
+ swarm.loggerService.log('function addState', {
5772
+ stateSchema: stateSchema
5773
+ });
5774
+ swarm.stateSchemaService.register(stateSchema.stateName, stateSchema);
5775
+ return stateSchema.stateName;
5776
+ };
5777
+
5219
5778
  /**
5220
5779
  * Adds a new embedding to the embedding registry. The swarm takes only those embeddings which was registered
5221
5780
  *
@@ -5238,11 +5797,16 @@ var addEmbedding = function (embeddingSchema) {
5238
5797
  * @returns {string} The name of the added storage.
5239
5798
  */
5240
5799
  var addStorage = function (storageSchema) {
5241
- swarm.loggerService.log('function addStorage', {
5242
- storageSchema: storageSchema
5800
+ swarm.loggerService.log("function addStorage", {
5801
+ storageSchema: storageSchema,
5243
5802
  });
5244
5803
  swarm.storageValidationService.addStorage(storageSchema.storageName, storageSchema);
5245
5804
  swarm.storageSchemaService.register(storageSchema.storageName, storageSchema);
5805
+ if (storageSchema.shared) {
5806
+ swarm.storageConnectionService
5807
+ .getSharedStorage("shared", storageSchema.storageName)
5808
+ .waitForInit();
5809
+ }
5246
5810
  return storageSchema.storageName;
5247
5811
  };
5248
5812
 
@@ -5578,6 +6142,7 @@ var disposeConnection = function (clientId, swarmName) { return __awaiter(void 0
5578
6142
  .flatMap(function (agentName) {
5579
6143
  return swarm.agentValidationService.getStorageList(agentName);
5580
6144
  })
6145
+ .filter(function (storageName) { return !!storageName; })
5581
6146
  .map(function (storageName) { return __awaiter(void 0, void 0, void 0, function () {
5582
6147
  return __generator(this, function (_a) {
5583
6148
  switch (_a.label) {
@@ -5591,8 +6156,28 @@ var disposeConnection = function (clientId, swarmName) { return __awaiter(void 0
5591
6156
  ])];
5592
6157
  case 4:
5593
6158
  _a.sent();
5594
- return [4 /*yield*/, HistoryAdapter.dispose(clientId, null)];
6159
+ return [4 /*yield*/, Promise.all([
6160
+ swarm.swarmValidationService
6161
+ .getAgentList(swarmName)
6162
+ .flatMap(function (agentName) {
6163
+ return swarm.agentValidationService.getStateList(agentName);
6164
+ })
6165
+ .filter(function (stateName) { return !!stateName; })
6166
+ .map(function (stateName) { return __awaiter(void 0, void 0, void 0, function () {
6167
+ return __generator(this, function (_a) {
6168
+ switch (_a.label) {
6169
+ case 0: return [4 /*yield*/, swarm.statePublicService.dispose(clientId, stateName)];
6170
+ case 1:
6171
+ _a.sent();
6172
+ return [2 /*return*/];
6173
+ }
6174
+ });
6175
+ }); }),
6176
+ ])];
5595
6177
  case 5:
6178
+ _a.sent();
6179
+ return [4 /*yield*/, HistoryAdapter.dispose(clientId, null)];
6180
+ case 6:
5596
6181
  _a.sent();
5597
6182
  return [2 /*return*/];
5598
6183
  }
@@ -6606,14 +7191,91 @@ var StorageUtils = /** @class */ (function () {
6606
7191
  }());
6607
7192
  var Storage = new StorageUtils();
6608
7193
 
7194
+ /**
7195
+ * Utility class for managing state in the agent swarm.
7196
+ * @implements {TState}
7197
+ */
7198
+ var StateUtils = /** @class */ (function () {
7199
+ function StateUtils() {
7200
+ var _this = this;
7201
+ /**
7202
+ * Retrieves the state for a given client and state name.
7203
+ * @template T
7204
+ * @param {Object} payload - The payload containing client and state information.
7205
+ * @param {string} payload.clientId - The client ID.
7206
+ * @param {AgentName} payload.agentName - The agent name.
7207
+ * @param {StateName} payload.stateName - The state name.
7208
+ * @returns {Promise<T>} The state data.
7209
+ * @throws Will throw an error if the state is not registered in the agent.
7210
+ */
7211
+ this.getState = function (payload) { return __awaiter(_this, void 0, void 0, function () {
7212
+ return __generator(this, function (_a) {
7213
+ switch (_a.label) {
7214
+ case 0:
7215
+ swarm.loggerService.log("StateUtils getState", {
7216
+ clientId: payload.clientId,
7217
+ stateName: payload.stateName,
7218
+ });
7219
+ if (!swarm.agentValidationService.hasState(payload.agentName, payload.stateName)) {
7220
+ throw new Error("agent-swarm StateUtils ".concat(payload.stateName, " not registered in ").concat(payload.agentName, " (getState)"));
7221
+ }
7222
+ return [4 /*yield*/, swarm.statePublicService.getState(payload.clientId, payload.stateName)];
7223
+ case 1: return [2 /*return*/, _a.sent()];
7224
+ }
7225
+ });
7226
+ }); };
7227
+ /**
7228
+ * Sets the state for a given client and state name.
7229
+ * @template T
7230
+ * @param {T | ((prevState: T) => Promise<T>)} dispatchFn - The new state or a function that returns the new state.
7231
+ * @param {Object} payload - The payload containing client and state information.
7232
+ * @param {string} payload.clientId - The client ID.
7233
+ * @param {AgentName} payload.agentName - The agent name.
7234
+ * @param {StateName} payload.stateName - The state name.
7235
+ * @returns {Promise<void>}
7236
+ * @throws Will throw an error if the state is not registered in the agent.
7237
+ */
7238
+ this.setState = function (dispatchFn, payload) { return __awaiter(_this, void 0, void 0, function () {
7239
+ var _this = this;
7240
+ return __generator(this, function (_a) {
7241
+ switch (_a.label) {
7242
+ case 0:
7243
+ swarm.loggerService.log("StateUtils setState", {
7244
+ clientId: payload.clientId,
7245
+ stateName: payload.stateName,
7246
+ });
7247
+ if (!swarm.agentValidationService.hasState(payload.agentName, payload.stateName)) {
7248
+ throw new Error("agent-swarm StateUtils ".concat(payload.stateName, " not registered in ").concat(payload.agentName, " (setState)"));
7249
+ }
7250
+ if (!(typeof dispatchFn === "function")) return [3 /*break*/, 2];
7251
+ return [4 /*yield*/, swarm.statePublicService.setState(dispatchFn, payload.clientId, payload.stateName)];
7252
+ case 1: return [2 /*return*/, _a.sent()];
7253
+ case 2: return [4 /*yield*/, swarm.statePublicService.setState(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
7254
+ return [2 /*return*/, dispatchFn];
7255
+ }); }); }, payload.clientId, payload.stateName)];
7256
+ case 3: return [2 /*return*/, _a.sent()];
7257
+ }
7258
+ });
7259
+ }); };
7260
+ }
7261
+ return StateUtils;
7262
+ }());
7263
+ /**
7264
+ * Instance of StateUtils for managing state.
7265
+ * @type {StateUtils}
7266
+ */
7267
+ var State = new StateUtils();
7268
+
6609
7269
  exports.ContextService = ContextService;
6610
7270
  exports.History = History;
7271
+ exports.HistoryAdapter = HistoryAdapter;
7272
+ exports.HistoryInstance = HistoryInstance;
7273
+ exports.State = State;
6611
7274
  exports.Storage = Storage;
6612
- exports._HistoryAdapter = HistoryAdapter;
6613
- exports._HistoryInstance = HistoryInstance;
6614
7275
  exports.addAgent = addAgent;
6615
7276
  exports.addCompletion = addCompletion;
6616
7277
  exports.addEmbedding = addEmbedding;
7278
+ exports.addState = addState;
6617
7279
  exports.addStorage = addStorage;
6618
7280
  exports.addSwarm = addSwarm;
6619
7281
  exports.addTool = addTool;