agent-swarm-kit 1.0.69 → 1.0.70
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 +102 -19
- package/build/index.mjs +102 -19
- package/package.json +1 -1
- package/types.d.ts +29 -0
package/build/index.cjs
CHANGED
|
@@ -4416,6 +4416,7 @@ var ClientStorage = /** @class */ (function () {
|
|
|
4416
4416
|
*/
|
|
4417
4417
|
function ClientStorage(params) {
|
|
4418
4418
|
var _this = this;
|
|
4419
|
+
var _a;
|
|
4419
4420
|
this.params = params;
|
|
4420
4421
|
this._itemMap = new Map();
|
|
4421
4422
|
/**
|
|
@@ -4641,9 +4642,26 @@ var ClientStorage = /** @class */ (function () {
|
|
|
4641
4642
|
return [2 /*return*/, result];
|
|
4642
4643
|
});
|
|
4643
4644
|
}); };
|
|
4645
|
+
/**
|
|
4646
|
+
* Disposes of the state.
|
|
4647
|
+
* @returns {Promise<void>}
|
|
4648
|
+
*/
|
|
4649
|
+
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
4650
|
+
var _a;
|
|
4651
|
+
return __generator(this, function (_b) {
|
|
4652
|
+
this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " dispose"));
|
|
4653
|
+
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onDispose) {
|
|
4654
|
+
this.params.callbacks.onDispose(this.params.clientId, this.params.storageName);
|
|
4655
|
+
}
|
|
4656
|
+
return [2 /*return*/];
|
|
4657
|
+
});
|
|
4658
|
+
}); };
|
|
4644
4659
|
this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
|
|
4645
4660
|
params: params,
|
|
4646
4661
|
});
|
|
4662
|
+
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
|
|
4663
|
+
this.params.callbacks.onInit(this.params.clientId, this.params.storageName);
|
|
4664
|
+
}
|
|
4647
4665
|
}
|
|
4648
4666
|
return ClientStorage;
|
|
4649
4667
|
}());
|
|
@@ -4838,18 +4856,31 @@ var StorageConnectionService = /** @class */ (function () {
|
|
|
4838
4856
|
* @returns {Promise<void>}
|
|
4839
4857
|
*/
|
|
4840
4858
|
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
4841
|
-
var key;
|
|
4859
|
+
var key, storage;
|
|
4842
4860
|
return __generator(this, function (_a) {
|
|
4843
|
-
|
|
4844
|
-
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4861
|
+
switch (_a.label) {
|
|
4862
|
+
case 0:
|
|
4863
|
+
this.loggerService.log("storageConnectionService dispose", {
|
|
4864
|
+
context: this.contextService.context,
|
|
4865
|
+
});
|
|
4866
|
+
key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.storageName);
|
|
4867
|
+
if (!this.getStorage.has(key)) {
|
|
4868
|
+
return [2 /*return*/];
|
|
4869
|
+
}
|
|
4870
|
+
if (!!this.getSharedStorage.has(this.contextService.context.storageName)) return [3 /*break*/, 3];
|
|
4871
|
+
storage = this.getSharedStorage(this.contextService.context.clientId, this.contextService.context.storageName);
|
|
4872
|
+
return [4 /*yield*/, storage.waitForInit()];
|
|
4873
|
+
case 1:
|
|
4874
|
+
_a.sent();
|
|
4875
|
+
return [4 /*yield*/, storage.dispose()];
|
|
4876
|
+
case 2:
|
|
4877
|
+
_a.sent();
|
|
4878
|
+
_a.label = 3;
|
|
4879
|
+
case 3:
|
|
4880
|
+
this.getStorage.clear(key);
|
|
4881
|
+
this.sessionValidationService.removeStorageUsage(this.contextService.context.clientId, this.contextService.context.storageName);
|
|
4882
|
+
return [2 /*return*/];
|
|
4849
4883
|
}
|
|
4850
|
-
this.getStorage.clear(key);
|
|
4851
|
-
this.sessionValidationService.removeStorageUsage(this.contextService.context.clientId, this.contextService.context.storageName);
|
|
4852
|
-
return [2 /*return*/];
|
|
4853
4884
|
});
|
|
4854
4885
|
}); };
|
|
4855
4886
|
}
|
|
@@ -5268,12 +5299,12 @@ var ClientState = /** @class */ (function () {
|
|
|
5268
5299
|
return __generator(this, function (_c) {
|
|
5269
5300
|
switch (_c.label) {
|
|
5270
5301
|
case 0:
|
|
5271
|
-
this.params.logger.debug("
|
|
5302
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " waitForInit"));
|
|
5272
5303
|
_a = this;
|
|
5273
5304
|
return [4 /*yield*/, this.params.getState(this.params.clientId, this.params.stateName)];
|
|
5274
5305
|
case 1:
|
|
5275
5306
|
_a._state = _c.sent();
|
|
5276
|
-
this.params.logger.debug("
|
|
5307
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " waitForInit output"), { initialState: this._state });
|
|
5277
5308
|
if ((_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onLoad) {
|
|
5278
5309
|
this.params.callbacks.onLoad(this._state, this.params.clientId, this.params.stateName);
|
|
5279
5310
|
}
|
|
@@ -5292,7 +5323,7 @@ var ClientState = /** @class */ (function () {
|
|
|
5292
5323
|
return __generator(this, function (_b) {
|
|
5293
5324
|
switch (_b.label) {
|
|
5294
5325
|
case 0:
|
|
5295
|
-
this.params.logger.debug("
|
|
5326
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " setState"));
|
|
5296
5327
|
return [4 /*yield*/, this.dispatch("write", function (currentState) { return __awaiter(_this, void 0, void 0, function () {
|
|
5297
5328
|
var _a, _b, middleware, e_1_1;
|
|
5298
5329
|
var e_1, _c;
|
|
@@ -5330,7 +5361,7 @@ var ClientState = /** @class */ (function () {
|
|
|
5330
5361
|
}); })];
|
|
5331
5362
|
case 1:
|
|
5332
5363
|
_b.sent();
|
|
5333
|
-
this.params.logger.debug("
|
|
5364
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " setState output"), { pendingState: this._state });
|
|
5334
5365
|
this.params.setState &&
|
|
5335
5366
|
this.params.setState(this._state, this.params.clientId, this.params.stateName);
|
|
5336
5367
|
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onWrite) {
|
|
@@ -5349,7 +5380,7 @@ var ClientState = /** @class */ (function () {
|
|
|
5349
5380
|
return __generator(this, function (_b) {
|
|
5350
5381
|
switch (_b.label) {
|
|
5351
5382
|
case 0:
|
|
5352
|
-
this.params.logger.debug("
|
|
5383
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " getState"));
|
|
5353
5384
|
return [4 /*yield*/, this.dispatch("read")];
|
|
5354
5385
|
case 1:
|
|
5355
5386
|
_b.sent();
|
|
@@ -5367,13 +5398,16 @@ var ClientState = /** @class */ (function () {
|
|
|
5367
5398
|
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
5368
5399
|
var _a;
|
|
5369
5400
|
return __generator(this, function (_b) {
|
|
5370
|
-
this.params.logger.debug("
|
|
5401
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " dispose"));
|
|
5371
5402
|
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onDispose) {
|
|
5372
5403
|
this.params.callbacks.onDispose(this.params.clientId, this.params.stateName);
|
|
5373
5404
|
}
|
|
5374
5405
|
return [2 /*return*/];
|
|
5375
5406
|
});
|
|
5376
5407
|
}); };
|
|
5408
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
|
|
5409
|
+
params: params,
|
|
5410
|
+
});
|
|
5377
5411
|
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
|
|
5378
5412
|
this.params.callbacks.onInit(this.params.clientId, this.params.stateName);
|
|
5379
5413
|
}
|
|
@@ -5393,6 +5427,44 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5393
5427
|
this.contextService = inject(TYPES.contextService);
|
|
5394
5428
|
this.stateSchemaService = inject(TYPES.stateSchemaService);
|
|
5395
5429
|
this.sessionValidationService = inject(TYPES.sessionValidationService);
|
|
5430
|
+
/**
|
|
5431
|
+
* Memoized function to get a shared state reference.
|
|
5432
|
+
* @param {string} clientId - The client ID.
|
|
5433
|
+
* @param {StateName} stateName - The state name.
|
|
5434
|
+
* @returns {ClientState} The client state.
|
|
5435
|
+
*/
|
|
5436
|
+
this.getSharedStateRef = functoolsKit.memoize(function (_a) {
|
|
5437
|
+
var _b = __read(_a, 2), stateName = _b[1];
|
|
5438
|
+
return "".concat(stateName);
|
|
5439
|
+
}, function (clientId, stateName) {
|
|
5440
|
+
_this.sessionValidationService.addStateUsage(clientId, stateName);
|
|
5441
|
+
var _a = _this.stateSchemaService.get(stateName), getState = _a.getState, setState = _a.setState, _b = _a.middlewares, middlewares = _b === void 0 ? [] : _b, shared = _a.shared, callbacks = _a.callbacks;
|
|
5442
|
+
if (!shared) {
|
|
5443
|
+
throw new Error("agent-swarm state not shared stateName=".concat(stateName));
|
|
5444
|
+
}
|
|
5445
|
+
return new ClientState({
|
|
5446
|
+
clientId: clientId,
|
|
5447
|
+
stateName: stateName,
|
|
5448
|
+
logger: _this.loggerService,
|
|
5449
|
+
setState: setState
|
|
5450
|
+
? functoolsKit.queued(function () {
|
|
5451
|
+
var args = [];
|
|
5452
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
5453
|
+
args[_i] = arguments[_i];
|
|
5454
|
+
}
|
|
5455
|
+
return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
5456
|
+
switch (_a.label) {
|
|
5457
|
+
case 0: return [4 /*yield*/, setState.apply(void 0, __spreadArray([], __read(args), false))];
|
|
5458
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
5459
|
+
}
|
|
5460
|
+
}); });
|
|
5461
|
+
})
|
|
5462
|
+
: setState,
|
|
5463
|
+
getState: getState,
|
|
5464
|
+
middlewares: middlewares,
|
|
5465
|
+
callbacks: callbacks,
|
|
5466
|
+
});
|
|
5467
|
+
});
|
|
5396
5468
|
/**
|
|
5397
5469
|
* Memoized function to get a state reference.
|
|
5398
5470
|
* @param {string} clientId - The client ID.
|
|
@@ -5404,7 +5476,10 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5404
5476
|
return "".concat(clientId, "-").concat(stateName);
|
|
5405
5477
|
}, function (clientId, stateName) {
|
|
5406
5478
|
_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;
|
|
5479
|
+
var _a = _this.stateSchemaService.get(stateName), getState = _a.getState, setState = _a.setState, _b = _a.middlewares, middlewares = _b === void 0 ? [] : _b, callbacks = _a.callbacks, _c = _a.shared, shared = _c === void 0 ? false : _c;
|
|
5480
|
+
if (shared) {
|
|
5481
|
+
return _this.getSharedStateRef(clientId, stateName);
|
|
5482
|
+
}
|
|
5408
5483
|
return new ClientState({
|
|
5409
5484
|
clientId: clientId,
|
|
5410
5485
|
stateName: stateName,
|
|
@@ -5487,6 +5562,7 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5487
5562
|
if (!this.getStateRef.has(key)) {
|
|
5488
5563
|
return [2 /*return*/];
|
|
5489
5564
|
}
|
|
5565
|
+
if (!!this.getSharedStateRef.has(this.contextService.context.stateName)) return [3 /*break*/, 3];
|
|
5490
5566
|
state = this.getStateRef(this.contextService.context.clientId, this.contextService.context.stateName);
|
|
5491
5567
|
return [4 /*yield*/, state.waitForInit()];
|
|
5492
5568
|
case 1:
|
|
@@ -5494,6 +5570,8 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5494
5570
|
return [4 /*yield*/, state.dispose()];
|
|
5495
5571
|
case 2:
|
|
5496
5572
|
_a.sent();
|
|
5573
|
+
_a.label = 3;
|
|
5574
|
+
case 3:
|
|
5497
5575
|
this.getStateRef.clear(key);
|
|
5498
5576
|
this.sessionValidationService.removeStateUsage(this.contextService.context.clientId, this.contextService.context.stateName);
|
|
5499
5577
|
return [2 /*return*/];
|
|
@@ -5768,10 +5846,15 @@ var addTool = function (toolSchema) {
|
|
|
5768
5846
|
* @returns {string} The name of the added state.
|
|
5769
5847
|
*/
|
|
5770
5848
|
var addState = function (stateSchema) {
|
|
5771
|
-
swarm.loggerService.log(
|
|
5772
|
-
stateSchema: stateSchema
|
|
5849
|
+
swarm.loggerService.log("function addState", {
|
|
5850
|
+
stateSchema: stateSchema,
|
|
5773
5851
|
});
|
|
5774
5852
|
swarm.stateSchemaService.register(stateSchema.stateName, stateSchema);
|
|
5853
|
+
if (stateSchema.shared) {
|
|
5854
|
+
swarm.stateConnectionService
|
|
5855
|
+
.getSharedStateRef("shared", stateSchema.stateName)
|
|
5856
|
+
.waitForInit();
|
|
5857
|
+
}
|
|
5775
5858
|
return stateSchema.stateName;
|
|
5776
5859
|
};
|
|
5777
5860
|
|
package/build/index.mjs
CHANGED
|
@@ -4414,6 +4414,7 @@ var ClientStorage = /** @class */ (function () {
|
|
|
4414
4414
|
*/
|
|
4415
4415
|
function ClientStorage(params) {
|
|
4416
4416
|
var _this = this;
|
|
4417
|
+
var _a;
|
|
4417
4418
|
this.params = params;
|
|
4418
4419
|
this._itemMap = new Map();
|
|
4419
4420
|
/**
|
|
@@ -4639,9 +4640,26 @@ var ClientStorage = /** @class */ (function () {
|
|
|
4639
4640
|
return [2 /*return*/, result];
|
|
4640
4641
|
});
|
|
4641
4642
|
}); };
|
|
4643
|
+
/**
|
|
4644
|
+
* Disposes of the state.
|
|
4645
|
+
* @returns {Promise<void>}
|
|
4646
|
+
*/
|
|
4647
|
+
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
4648
|
+
var _a;
|
|
4649
|
+
return __generator(this, function (_b) {
|
|
4650
|
+
this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " dispose"));
|
|
4651
|
+
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onDispose) {
|
|
4652
|
+
this.params.callbacks.onDispose(this.params.clientId, this.params.storageName);
|
|
4653
|
+
}
|
|
4654
|
+
return [2 /*return*/];
|
|
4655
|
+
});
|
|
4656
|
+
}); };
|
|
4642
4657
|
this.params.logger.debug("ClientStorage storageName=".concat(this.params.storageName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
|
|
4643
4658
|
params: params,
|
|
4644
4659
|
});
|
|
4660
|
+
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
|
|
4661
|
+
this.params.callbacks.onInit(this.params.clientId, this.params.storageName);
|
|
4662
|
+
}
|
|
4645
4663
|
}
|
|
4646
4664
|
return ClientStorage;
|
|
4647
4665
|
}());
|
|
@@ -4836,18 +4854,31 @@ var StorageConnectionService = /** @class */ (function () {
|
|
|
4836
4854
|
* @returns {Promise<void>}
|
|
4837
4855
|
*/
|
|
4838
4856
|
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
4839
|
-
var key;
|
|
4857
|
+
var key, storage;
|
|
4840
4858
|
return __generator(this, function (_a) {
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4844
|
-
|
|
4845
|
-
|
|
4846
|
-
|
|
4859
|
+
switch (_a.label) {
|
|
4860
|
+
case 0:
|
|
4861
|
+
this.loggerService.log("storageConnectionService dispose", {
|
|
4862
|
+
context: this.contextService.context,
|
|
4863
|
+
});
|
|
4864
|
+
key = "".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.storageName);
|
|
4865
|
+
if (!this.getStorage.has(key)) {
|
|
4866
|
+
return [2 /*return*/];
|
|
4867
|
+
}
|
|
4868
|
+
if (!!this.getSharedStorage.has(this.contextService.context.storageName)) return [3 /*break*/, 3];
|
|
4869
|
+
storage = this.getSharedStorage(this.contextService.context.clientId, this.contextService.context.storageName);
|
|
4870
|
+
return [4 /*yield*/, storage.waitForInit()];
|
|
4871
|
+
case 1:
|
|
4872
|
+
_a.sent();
|
|
4873
|
+
return [4 /*yield*/, storage.dispose()];
|
|
4874
|
+
case 2:
|
|
4875
|
+
_a.sent();
|
|
4876
|
+
_a.label = 3;
|
|
4877
|
+
case 3:
|
|
4878
|
+
this.getStorage.clear(key);
|
|
4879
|
+
this.sessionValidationService.removeStorageUsage(this.contextService.context.clientId, this.contextService.context.storageName);
|
|
4880
|
+
return [2 /*return*/];
|
|
4847
4881
|
}
|
|
4848
|
-
this.getStorage.clear(key);
|
|
4849
|
-
this.sessionValidationService.removeStorageUsage(this.contextService.context.clientId, this.contextService.context.storageName);
|
|
4850
|
-
return [2 /*return*/];
|
|
4851
4882
|
});
|
|
4852
4883
|
}); };
|
|
4853
4884
|
}
|
|
@@ -5266,12 +5297,12 @@ var ClientState = /** @class */ (function () {
|
|
|
5266
5297
|
return __generator(this, function (_c) {
|
|
5267
5298
|
switch (_c.label) {
|
|
5268
5299
|
case 0:
|
|
5269
|
-
this.params.logger.debug("
|
|
5300
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " waitForInit"));
|
|
5270
5301
|
_a = this;
|
|
5271
5302
|
return [4 /*yield*/, this.params.getState(this.params.clientId, this.params.stateName)];
|
|
5272
5303
|
case 1:
|
|
5273
5304
|
_a._state = _c.sent();
|
|
5274
|
-
this.params.logger.debug("
|
|
5305
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " waitForInit output"), { initialState: this._state });
|
|
5275
5306
|
if ((_b = this.params.callbacks) === null || _b === void 0 ? void 0 : _b.onLoad) {
|
|
5276
5307
|
this.params.callbacks.onLoad(this._state, this.params.clientId, this.params.stateName);
|
|
5277
5308
|
}
|
|
@@ -5290,7 +5321,7 @@ var ClientState = /** @class */ (function () {
|
|
|
5290
5321
|
return __generator(this, function (_b) {
|
|
5291
5322
|
switch (_b.label) {
|
|
5292
5323
|
case 0:
|
|
5293
|
-
this.params.logger.debug("
|
|
5324
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " setState"));
|
|
5294
5325
|
return [4 /*yield*/, this.dispatch("write", function (currentState) { return __awaiter(_this, void 0, void 0, function () {
|
|
5295
5326
|
var _a, _b, middleware, e_1_1;
|
|
5296
5327
|
var e_1, _c;
|
|
@@ -5328,7 +5359,7 @@ var ClientState = /** @class */ (function () {
|
|
|
5328
5359
|
}); })];
|
|
5329
5360
|
case 1:
|
|
5330
5361
|
_b.sent();
|
|
5331
|
-
this.params.logger.debug("
|
|
5362
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " setState output"), { pendingState: this._state });
|
|
5332
5363
|
this.params.setState &&
|
|
5333
5364
|
this.params.setState(this._state, this.params.clientId, this.params.stateName);
|
|
5334
5365
|
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onWrite) {
|
|
@@ -5347,7 +5378,7 @@ var ClientState = /** @class */ (function () {
|
|
|
5347
5378
|
return __generator(this, function (_b) {
|
|
5348
5379
|
switch (_b.label) {
|
|
5349
5380
|
case 0:
|
|
5350
|
-
this.params.logger.debug("
|
|
5381
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " getState"));
|
|
5351
5382
|
return [4 /*yield*/, this.dispatch("read")];
|
|
5352
5383
|
case 1:
|
|
5353
5384
|
_b.sent();
|
|
@@ -5365,13 +5396,16 @@ var ClientState = /** @class */ (function () {
|
|
|
5365
5396
|
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
5366
5397
|
var _a;
|
|
5367
5398
|
return __generator(this, function (_b) {
|
|
5368
|
-
this.params.logger.debug("
|
|
5399
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " dispose"));
|
|
5369
5400
|
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onDispose) {
|
|
5370
5401
|
this.params.callbacks.onDispose(this.params.clientId, this.params.stateName);
|
|
5371
5402
|
}
|
|
5372
5403
|
return [2 /*return*/];
|
|
5373
5404
|
});
|
|
5374
5405
|
}); };
|
|
5406
|
+
this.params.logger.debug("ClientState stateName=".concat(this.params.stateName, " clientId=").concat(this.params.clientId, " shared=").concat(this.params.shared, " CTOR"), {
|
|
5407
|
+
params: params,
|
|
5408
|
+
});
|
|
5375
5409
|
if ((_a = this.params.callbacks) === null || _a === void 0 ? void 0 : _a.onInit) {
|
|
5376
5410
|
this.params.callbacks.onInit(this.params.clientId, this.params.stateName);
|
|
5377
5411
|
}
|
|
@@ -5391,6 +5425,44 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5391
5425
|
this.contextService = inject(TYPES.contextService);
|
|
5392
5426
|
this.stateSchemaService = inject(TYPES.stateSchemaService);
|
|
5393
5427
|
this.sessionValidationService = inject(TYPES.sessionValidationService);
|
|
5428
|
+
/**
|
|
5429
|
+
* Memoized function to get a shared state reference.
|
|
5430
|
+
* @param {string} clientId - The client ID.
|
|
5431
|
+
* @param {StateName} stateName - The state name.
|
|
5432
|
+
* @returns {ClientState} The client state.
|
|
5433
|
+
*/
|
|
5434
|
+
this.getSharedStateRef = memoize(function (_a) {
|
|
5435
|
+
var _b = __read(_a, 2), stateName = _b[1];
|
|
5436
|
+
return "".concat(stateName);
|
|
5437
|
+
}, function (clientId, stateName) {
|
|
5438
|
+
_this.sessionValidationService.addStateUsage(clientId, stateName);
|
|
5439
|
+
var _a = _this.stateSchemaService.get(stateName), getState = _a.getState, setState = _a.setState, _b = _a.middlewares, middlewares = _b === void 0 ? [] : _b, shared = _a.shared, callbacks = _a.callbacks;
|
|
5440
|
+
if (!shared) {
|
|
5441
|
+
throw new Error("agent-swarm state not shared stateName=".concat(stateName));
|
|
5442
|
+
}
|
|
5443
|
+
return new ClientState({
|
|
5444
|
+
clientId: clientId,
|
|
5445
|
+
stateName: stateName,
|
|
5446
|
+
logger: _this.loggerService,
|
|
5447
|
+
setState: setState
|
|
5448
|
+
? queued(function () {
|
|
5449
|
+
var args = [];
|
|
5450
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
5451
|
+
args[_i] = arguments[_i];
|
|
5452
|
+
}
|
|
5453
|
+
return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
5454
|
+
switch (_a.label) {
|
|
5455
|
+
case 0: return [4 /*yield*/, setState.apply(void 0, __spreadArray([], __read(args), false))];
|
|
5456
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
5457
|
+
}
|
|
5458
|
+
}); });
|
|
5459
|
+
})
|
|
5460
|
+
: setState,
|
|
5461
|
+
getState: getState,
|
|
5462
|
+
middlewares: middlewares,
|
|
5463
|
+
callbacks: callbacks,
|
|
5464
|
+
});
|
|
5465
|
+
});
|
|
5394
5466
|
/**
|
|
5395
5467
|
* Memoized function to get a state reference.
|
|
5396
5468
|
* @param {string} clientId - The client ID.
|
|
@@ -5402,7 +5474,10 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5402
5474
|
return "".concat(clientId, "-").concat(stateName);
|
|
5403
5475
|
}, function (clientId, stateName) {
|
|
5404
5476
|
_this.sessionValidationService.addStateUsage(clientId, stateName);
|
|
5405
|
-
var _a = _this.stateSchemaService.get(stateName), getState = _a.getState, setState = _a.setState, _b = _a.middlewares, middlewares = _b === void 0 ? [] : _b, callbacks = _a.callbacks;
|
|
5477
|
+
var _a = _this.stateSchemaService.get(stateName), getState = _a.getState, setState = _a.setState, _b = _a.middlewares, middlewares = _b === void 0 ? [] : _b, callbacks = _a.callbacks, _c = _a.shared, shared = _c === void 0 ? false : _c;
|
|
5478
|
+
if (shared) {
|
|
5479
|
+
return _this.getSharedStateRef(clientId, stateName);
|
|
5480
|
+
}
|
|
5406
5481
|
return new ClientState({
|
|
5407
5482
|
clientId: clientId,
|
|
5408
5483
|
stateName: stateName,
|
|
@@ -5485,6 +5560,7 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5485
5560
|
if (!this.getStateRef.has(key)) {
|
|
5486
5561
|
return [2 /*return*/];
|
|
5487
5562
|
}
|
|
5563
|
+
if (!!this.getSharedStateRef.has(this.contextService.context.stateName)) return [3 /*break*/, 3];
|
|
5488
5564
|
state = this.getStateRef(this.contextService.context.clientId, this.contextService.context.stateName);
|
|
5489
5565
|
return [4 /*yield*/, state.waitForInit()];
|
|
5490
5566
|
case 1:
|
|
@@ -5492,6 +5568,8 @@ var StateConnectionService = /** @class */ (function () {
|
|
|
5492
5568
|
return [4 /*yield*/, state.dispose()];
|
|
5493
5569
|
case 2:
|
|
5494
5570
|
_a.sent();
|
|
5571
|
+
_a.label = 3;
|
|
5572
|
+
case 3:
|
|
5495
5573
|
this.getStateRef.clear(key);
|
|
5496
5574
|
this.sessionValidationService.removeStateUsage(this.contextService.context.clientId, this.contextService.context.stateName);
|
|
5497
5575
|
return [2 /*return*/];
|
|
@@ -5766,10 +5844,15 @@ var addTool = function (toolSchema) {
|
|
|
5766
5844
|
* @returns {string} The name of the added state.
|
|
5767
5845
|
*/
|
|
5768
5846
|
var addState = function (stateSchema) {
|
|
5769
|
-
swarm.loggerService.log(
|
|
5770
|
-
stateSchema: stateSchema
|
|
5847
|
+
swarm.loggerService.log("function addState", {
|
|
5848
|
+
stateSchema: stateSchema,
|
|
5771
5849
|
});
|
|
5772
5850
|
swarm.stateSchemaService.register(stateSchema.stateName, stateSchema);
|
|
5851
|
+
if (stateSchema.shared) {
|
|
5852
|
+
swarm.stateConnectionService
|
|
5853
|
+
.getSharedStateRef("shared", stateSchema.stateName)
|
|
5854
|
+
.waitForInit();
|
|
5855
|
+
}
|
|
5773
5856
|
return stateSchema.stateName;
|
|
5774
5857
|
};
|
|
5775
5858
|
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -842,6 +842,18 @@ interface IStorageCallbacks<T extends IStorageData = IStorageData> {
|
|
|
842
842
|
* @param storageName - The name of the storage.
|
|
843
843
|
*/
|
|
844
844
|
onSearch: (search: string, index: SortedArray<T>, clientId: string, storageName: StorageName) => void;
|
|
845
|
+
/**
|
|
846
|
+
* Callback function for init
|
|
847
|
+
* @param clientId - The client ID.
|
|
848
|
+
* @param storageName - The name of the storage.
|
|
849
|
+
*/
|
|
850
|
+
onInit: (clientId: string, storageName: StorageName) => void;
|
|
851
|
+
/**
|
|
852
|
+
* Callback function for dispose
|
|
853
|
+
* @param clientId - The client ID.
|
|
854
|
+
* @param storageName - The name of the storage.
|
|
855
|
+
*/
|
|
856
|
+
onDispose: (clientId: string, storageName: StorageName) => void;
|
|
845
857
|
}
|
|
846
858
|
/**
|
|
847
859
|
* Interface representing the parameters for storage.
|
|
@@ -973,6 +985,10 @@ interface IStateCallbacks<T extends IStateData = IStateData> {
|
|
|
973
985
|
* @template T - The type of the state data.
|
|
974
986
|
*/
|
|
975
987
|
interface IStateSchema<T extends IStateData = IStateData> {
|
|
988
|
+
/**
|
|
989
|
+
* The agents can share the state
|
|
990
|
+
*/
|
|
991
|
+
shared?: boolean;
|
|
976
992
|
/**
|
|
977
993
|
* The name of the state.
|
|
978
994
|
*/
|
|
@@ -2482,6 +2498,11 @@ declare class ClientStorage<T extends IStorageData = IStorageData> implements IS
|
|
|
2482
2498
|
* @returns {Promise<T[]>} - The list of items.
|
|
2483
2499
|
*/
|
|
2484
2500
|
list: (filter?: (item: T) => boolean) => Promise<T[]>;
|
|
2501
|
+
/**
|
|
2502
|
+
* Disposes of the state.
|
|
2503
|
+
* @returns {Promise<void>}
|
|
2504
|
+
*/
|
|
2505
|
+
dispose: () => Promise<void>;
|
|
2485
2506
|
}
|
|
2486
2507
|
|
|
2487
2508
|
/**
|
|
@@ -2704,6 +2725,13 @@ declare class StateConnectionService<T extends IStateData = IStateData> implemen
|
|
|
2704
2725
|
private readonly contextService;
|
|
2705
2726
|
private readonly stateSchemaService;
|
|
2706
2727
|
private readonly sessionValidationService;
|
|
2728
|
+
/**
|
|
2729
|
+
* Memoized function to get a shared state reference.
|
|
2730
|
+
* @param {string} clientId - The client ID.
|
|
2731
|
+
* @param {StateName} stateName - The state name.
|
|
2732
|
+
* @returns {ClientState} The client state.
|
|
2733
|
+
*/
|
|
2734
|
+
getSharedStateRef: ((clientId: string, stateName: StateName) => ClientState<any>) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientState<any>>;
|
|
2707
2735
|
/**
|
|
2708
2736
|
* Memoized function to get a state reference.
|
|
2709
2737
|
* @param {string} clientId - The client ID.
|
|
@@ -2733,6 +2761,7 @@ interface IStateConnectionService extends StateConnectionService {
|
|
|
2733
2761
|
}
|
|
2734
2762
|
type InternalKeys = keyof {
|
|
2735
2763
|
getStateRef: never;
|
|
2764
|
+
getSharedStateRef: never;
|
|
2736
2765
|
};
|
|
2737
2766
|
type TStateConnectionService = {
|
|
2738
2767
|
[key in Exclude<keyof IStateConnectionService, InternalKeys>]: unknown;
|