@microsoft/agents-hosting 1.9.0-beta.3.gb2d23c1c44 → 1.9.1
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/README.md +182 -4
- package/dist/package.json +3 -3
- package/dist/src/app/agentApplication.d.ts +4 -3
- package/dist/src/app/agentApplication.js +24 -21
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/agentApplicationBuilder.d.ts +9 -2
- package/dist/src/app/agentApplicationBuilder.js +9 -0
- package/dist/src/app/agentApplicationBuilder.js.map +1 -1
- package/dist/src/app/agentApplicationOptions.d.ts +16 -4
- package/dist/src/app/auth/authorization.d.ts +54 -0
- package/dist/src/app/auth/authorization.js +63 -2
- package/dist/src/app/auth/authorization.js.map +1 -1
- package/dist/src/app/auth/authorizationManager.d.ts +0 -8
- package/dist/src/app/auth/authorizationManager.js +104 -178
- package/dist/src/app/auth/authorizationManager.js.map +1 -1
- package/dist/src/app/auth/handlerStorage.d.ts +5 -3
- package/dist/src/app/auth/handlerStorage.js +36 -10
- package/dist/src/app/auth/handlerStorage.js.map +1 -1
- package/dist/src/app/auth/types.d.ts +2 -2
- package/dist/src/app/index.d.ts +1 -0
- package/dist/src/app/proactive/proactive.js +10 -6
- package/dist/src/app/proactive/proactive.js.map +1 -1
- package/dist/src/app/proactive/proactiveOptions.d.ts +2 -2
- package/dist/src/app/rateLimit.d.ts +315 -0
- package/dist/src/app/rateLimit.js +333 -0
- package/dist/src/app/rateLimit.js.map +1 -0
- package/dist/src/app/turnState.d.ts +6 -3
- package/dist/src/app/turnState.js +43 -4
- package/dist/src/app/turnState.js.map +1 -1
- package/dist/src/auth/authConfiguration.d.ts +10 -3
- package/dist/src/auth/authConfiguration.js +292 -259
- package/dist/src/auth/authConfiguration.js.map +1 -1
- package/dist/src/auth/delegatedTokenCredential.d.ts +29 -0
- package/dist/src/auth/delegatedTokenCredential.js +49 -0
- package/dist/src/auth/delegatedTokenCredential.js.map +1 -0
- package/dist/src/auth/msal/msalTokenCredential.js +1 -1
- package/dist/src/auth/msal/msalTokenCredential.js.map +1 -1
- package/dist/src/auth/settings.d.ts +8 -70
- package/dist/src/auth/settings.js +5 -67
- package/dist/src/auth/settings.js.map +1 -1
- package/dist/src/cloudAdapter.d.ts +5 -0
- package/dist/src/cloudAdapter.js +13 -103
- package/dist/src/cloudAdapter.js.map +1 -1
- package/dist/src/configuration/botFrameworkEnvironmentCompatibility.d.ts +23 -0
- package/dist/src/configuration/botFrameworkEnvironmentCompatibility.js +183 -0
- package/dist/src/configuration/botFrameworkEnvironmentCompatibility.js.map +1 -0
- package/dist/src/configuration/configuration.d.ts +83 -0
- package/dist/src/configuration/configuration.js +836 -0
- package/dist/src/configuration/configuration.js.map +1 -0
- package/dist/src/configuration/configurationSource.d.ts +57 -0
- package/dist/src/configuration/configurationSource.js +7 -0
- package/dist/src/configuration/configurationSource.js.map +1 -0
- package/dist/src/configuration/environmentConfiguration.d.ts +14 -0
- package/dist/src/configuration/environmentConfiguration.js +271 -0
- package/dist/src/configuration/environmentConfiguration.js.map +1 -0
- package/dist/src/configuration/index.d.ts +7 -0
- package/dist/src/configuration/index.js +14 -0
- package/dist/src/configuration/index.js.map +1 -0
- package/dist/src/configuration/jsonFileConfigurationSource.d.ts +15 -0
- package/dist/src/configuration/jsonFileConfigurationSource.js +58 -0
- package/dist/src/configuration/jsonFileConfigurationSource.js.map +1 -0
- package/dist/src/createCloudAdapter.d.ts +30 -5
- package/dist/src/createCloudAdapter.js +17 -10
- package/dist/src/createCloudAdapter.js.map +1 -1
- package/dist/src/errorHelper.js +124 -5
- package/dist/src/errorHelper.js.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/observability/traces.d.ts +6 -4
- package/dist/src/observability/traces.js +10 -9
- package/dist/src/observability/traces.js.map +1 -1
- package/dist/src/outboundHostValidator.d.ts +4 -1
- package/dist/src/outboundHostValidator.js +25 -48
- package/dist/src/outboundHostValidator.js.map +1 -1
- package/dist/src/state/agentState.d.ts +8 -3
- package/dist/src/state/agentState.js +26 -8
- package/dist/src/state/agentState.js.map +1 -1
- package/dist/src/state/conversationState.d.ts +2 -2
- package/dist/src/state/conversationState.js.map +1 -1
- package/dist/src/state/userState.d.ts +2 -2
- package/dist/src/state/userState.js.map +1 -1
- package/dist/src/statusCodes.d.ts +5 -0
- package/dist/src/statusCodes.js +5 -0
- package/dist/src/statusCodes.js.map +1 -1
- package/dist/src/storage/fileStorage.d.ts +83 -68
- package/dist/src/storage/fileStorage.js +279 -105
- package/dist/src/storage/fileStorage.js.map +1 -1
- package/dist/src/storage/index.d.ts +1 -0
- package/dist/src/storage/index.js +1 -0
- package/dist/src/storage/index.js.map +1 -1
- package/dist/src/storage/memoryStorage.d.ts +85 -60
- package/dist/src/storage/memoryStorage.js +244 -87
- package/dist/src/storage/memoryStorage.js.map +1 -1
- package/dist/src/storage/storage.d.ts +100 -2
- package/dist/src/storage/storage.js +25 -0
- package/dist/src/storage/storage.js.map +1 -1
- package/dist/src/storage/storageCompatibility.d.ts +17 -0
- package/dist/src/storage/storageCompatibility.js +157 -0
- package/dist/src/storage/storageCompatibility.js.map +1 -0
- package/dist/src/storage/storageExpiry.d.ts +6 -0
- package/dist/src/storage/storageExpiry.js +20 -0
- package/dist/src/storage/storageExpiry.js.map +1 -0
- package/dist/src/utils/env.d.ts +43 -0
- package/dist/src/utils/env.js +42 -6
- package/dist/src/utils/env.js.map +1 -1
- package/dist/src/utils.d.ts +1 -0
- package/dist/src/utils.js +10 -1
- package/dist/src/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/app/agentApplication.ts +27 -14
- package/src/app/agentApplicationBuilder.ts +13 -2
- package/src/app/agentApplicationOptions.ts +18 -4
- package/src/app/auth/authorization.ts +67 -2
- package/src/app/auth/authorizationManager.ts +126 -204
- package/src/app/auth/handlerStorage.ts +51 -7
- package/src/app/auth/types.ts +2 -2
- package/src/app/index.ts +7 -0
- package/src/app/proactive/proactive.ts +19 -9
- package/src/app/proactive/proactiveOptions.ts +2 -2
- package/src/app/rateLimit.ts +659 -0
- package/src/app/turnState.ts +50 -7
- package/src/auth/authConfiguration.ts +415 -260
- package/src/auth/delegatedTokenCredential.ts +49 -0
- package/src/auth/msal/msalTokenCredential.ts +1 -1
- package/src/auth/settings.ts +5 -82
- package/src/cloudAdapter.ts +38 -113
- package/src/configuration/botFrameworkEnvironmentCompatibility.ts +245 -0
- package/src/configuration/configuration.ts +1076 -0
- package/src/configuration/configurationSource.ts +76 -0
- package/src/configuration/environmentConfiguration.ts +377 -0
- package/src/configuration/index.ts +19 -0
- package/src/configuration/jsonFileConfigurationSource.ts +74 -0
- package/src/createCloudAdapter.ts +52 -9
- package/src/errorHelper.ts +151 -5
- package/src/index.ts +1 -0
- package/src/observability/traces.ts +8 -7
- package/src/outboundHostValidator.ts +37 -44
- package/src/state/agentState.ts +39 -13
- package/src/state/conversationState.ts +2 -2
- package/src/state/userState.ts +2 -2
- package/src/statusCodes.ts +6 -0
- package/src/storage/fileStorage.ts +299 -106
- package/src/storage/index.ts +1 -0
- package/src/storage/memoryStorage.ts +268 -85
- package/src/storage/storage.ts +114 -2
- package/src/storage/storageCompatibility.ts +185 -0
- package/src/storage/storageExpiry.ts +21 -0
- package/src/utils/env.ts +63 -0
- package/src/utils.ts +10 -0
|
@@ -7,7 +7,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
exports.OutboundHostValidator = void 0;
|
|
8
8
|
exports.loadOutboundHostValidatorOptionsFromEnv = loadOutboundHostValidatorOptionsFromEnv;
|
|
9
9
|
exports.createOutboundHostValidator = createOutboundHostValidator;
|
|
10
|
-
const
|
|
10
|
+
const configuration_1 = require("./configuration/configuration");
|
|
11
|
+
const environmentConfiguration_1 = require("./configuration/environmentConfiguration");
|
|
12
|
+
const utils_1 = require("./utils");
|
|
11
13
|
/** Hosts used by Microsoft channel callbacks and hosted attachments. */
|
|
12
14
|
const DEFAULT_MICROSOFT_HOSTS = Object.freeze([
|
|
13
15
|
'botframework.com',
|
|
@@ -19,8 +21,6 @@ const DEFAULT_MICROSOFT_HOSTS = Object.freeze([
|
|
|
19
21
|
'svc.ms',
|
|
20
22
|
'blob.core.windows.net'
|
|
21
23
|
]);
|
|
22
|
-
const OUTBOUND_HOST_VALIDATOR_ENV_PREFIX = 'OutboundHostValidator__';
|
|
23
|
-
const OUTBOUND_HOST_VALIDATOR_ENV_PREFIX_UPPER = OUTBOUND_HOST_VALIDATOR_ENV_PREFIX.toUpperCase();
|
|
24
24
|
/**
|
|
25
25
|
* Shared allowlist policy for server-side outbound requests.
|
|
26
26
|
*
|
|
@@ -30,13 +30,16 @@ const OUTBOUND_HOST_VALIDATOR_ENV_PREFIX_UPPER = OUTBOUND_HOST_VALIDATOR_ENV_PRE
|
|
|
30
30
|
class OutboundHostValidator {
|
|
31
31
|
constructor(options = {}) {
|
|
32
32
|
var _a, _b, _c;
|
|
33
|
-
|
|
33
|
+
const resolved = options.configurationContext
|
|
34
|
+
? resolveOutboundHostValidatorOptions(options)
|
|
35
|
+
: options;
|
|
36
|
+
this.enabled = (_a = resolved.enabled) !== null && _a !== void 0 ? _a : false;
|
|
34
37
|
const suffixes = new Set();
|
|
35
|
-
if ((_b =
|
|
38
|
+
if ((_b = resolved.includeDefaultMicrosoftHosts) !== null && _b !== void 0 ? _b : true) {
|
|
36
39
|
for (const host of DEFAULT_MICROSOFT_HOSTS)
|
|
37
40
|
suffixes.add(host);
|
|
38
41
|
}
|
|
39
|
-
for (const host of (_c =
|
|
42
|
+
for (const host of (_c = resolved.hosts) !== null && _c !== void 0 ? _c : []) {
|
|
40
43
|
const normalized = normalizeConfiguredHost(host);
|
|
41
44
|
if (normalized)
|
|
42
45
|
suffixes.add(normalized);
|
|
@@ -65,50 +68,24 @@ exports.OutboundHostValidator = OutboundHostValidator;
|
|
|
65
68
|
* indexed values such as `Hosts__0`, `Hosts__1`, and so on.
|
|
66
69
|
*/
|
|
67
70
|
function loadOutboundHostValidatorOptionsFromEnv() {
|
|
68
|
-
|
|
69
|
-
const indexedHosts = new Map();
|
|
70
|
-
const hosts = [];
|
|
71
|
-
for (const [envKey, rawValue] of Object.entries(process.env)) {
|
|
72
|
-
const upperKey = envKey.toUpperCase();
|
|
73
|
-
if (!upperKey.startsWith(OUTBOUND_HOST_VALIDATOR_ENV_PREFIX_UPPER))
|
|
74
|
-
continue;
|
|
75
|
-
const property = envKey.substring(OUTBOUND_HOST_VALIDATOR_ENV_PREFIX.length);
|
|
76
|
-
const upperProperty = property.toUpperCase();
|
|
77
|
-
if (upperProperty === 'ENABLED') {
|
|
78
|
-
const value = (0, env_1.parseBooleanEnv)(rawValue);
|
|
79
|
-
if (value !== undefined)
|
|
80
|
-
result.enabled = value;
|
|
81
|
-
}
|
|
82
|
-
else if (upperProperty === 'INCLUDEDEFAULTMICROSOFTHOSTS') {
|
|
83
|
-
const value = (0, env_1.parseBooleanEnv)(rawValue);
|
|
84
|
-
if (value !== undefined)
|
|
85
|
-
result.includeDefaultMicrosoftHosts = value;
|
|
86
|
-
}
|
|
87
|
-
else if (upperProperty === 'HOSTS' && rawValue) {
|
|
88
|
-
hosts.push(...rawValue.split(',').map(host => host.trim()).filter(Boolean));
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
const match = /^HOSTS__(\d+)$/i.exec(property);
|
|
92
|
-
if (match && (rawValue === null || rawValue === void 0 ? void 0 : rawValue.trim()))
|
|
93
|
-
indexedHosts.set(Number(match[1]), rawValue.trim());
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
for (const [, host] of [...indexedHosts.entries()].sort(([left], [right]) => left - right)) {
|
|
97
|
-
hosts.push(host);
|
|
98
|
-
}
|
|
99
|
-
if (hosts.length > 0)
|
|
100
|
-
result.hosts = hosts;
|
|
101
|
-
return result;
|
|
71
|
+
return { ...(0, environmentConfiguration_1.loadModernEnvironmentConfiguration)().outboundHostValidator };
|
|
102
72
|
}
|
|
103
|
-
/** Creates
|
|
104
|
-
function createOutboundHostValidator(options) {
|
|
105
|
-
|
|
73
|
+
/** Creates a validator using the shared configuration precedence. */
|
|
74
|
+
function createOutboundHostValidator(options = {}) {
|
|
75
|
+
return new OutboundHostValidator(resolveOutboundHostValidatorOptions(options));
|
|
76
|
+
}
|
|
77
|
+
function resolveOutboundHostValidatorOptions(options) {
|
|
78
|
+
const external = (0, configuration_1.getConfigurationSnapshot)(options.configurationContext);
|
|
106
79
|
const fromEnv = loadOutboundHostValidatorOptionsFromEnv();
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
80
|
+
const direct = {};
|
|
81
|
+
if (options.enabled !== undefined)
|
|
82
|
+
direct.enabled = options.enabled;
|
|
83
|
+
if (options.includeDefaultMicrosoftHosts !== undefined) {
|
|
84
|
+
direct.includeDefaultMicrosoftHosts = options.includeDefaultMicrosoftHosts;
|
|
85
|
+
}
|
|
86
|
+
if (options.hosts !== undefined)
|
|
87
|
+
direct.hosts = options.hosts;
|
|
88
|
+
return (0, utils_1.mergeDefined)(external.fallback.outboundHostValidator, fromEnv, external.overrideEnvironment.outboundHostValidator, direct, external.enforce.outboundHostValidator);
|
|
112
89
|
}
|
|
113
90
|
function getUrlHost(input) {
|
|
114
91
|
if (!input)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"outboundHostValidator.js","sourceRoot":"","sources":["../../src/outboundHostValidator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;AAEH,
|
|
1
|
+
{"version":3,"file":"outboundHostValidator.js","sourceRoot":"","sources":["../../src/outboundHostValidator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;AAEH,iEAGsC;AACtC,uFAA6F;AAC7F,mCAAsC;AAEtC,wEAAwE;AACxE,MAAM,uBAAuB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5C,kBAAkB;IAClB,yBAAyB;IACzB,qBAAqB;IACrB,oBAAoB;IACpB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AO,SAAS,CAAA;IAClB,CAAC;AACH,CAAC;AAED,SAAS,uBAAuB,CAAE,KAAa;IAC7C,IAAI,SAAS,GAAG,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,IAAI,EAAE,CAAA;IAC7B,IAAI,CAAC,SAAS;QAAE,OAAO,SAAS,CAAA;IAChC,IAAI,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAE9D,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAA;QACnC,IAAI,QAAQ,CAAC,QAAQ;YAAE,OAAO,iBAAiB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;IACpE,CAAC;IAAC,MAAM,CAAC;QACP,yDAAyD;IAC3D,CAAC;IAED,IAAI,CAAC;QACH,OAAO,iBAAiB,CAAC,IAAI,GAAG,CAAC,UAAU,SAAS,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAA;IACnE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAA;IAClB,CAAC;AACH,CAAC;AAED,SAAS,iBAAiB,CAAE,IAAY;IACtC,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;IAC/D,OAAO,UAAU,IAAI,SAAS,CAAA;AAChC,CAAC"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import { StorageKeyFactory, StorageProvider } from '../storage/storage';
|
|
6
6
|
import { TurnContext } from '../turnContext';
|
|
7
7
|
import { AgentStatePropertyAccessor } from './agentStatePropertyAccesor';
|
|
8
8
|
/**
|
|
@@ -22,6 +22,10 @@ export interface CachedAgentState {
|
|
|
22
22
|
* Hash of the state used to detect changes
|
|
23
23
|
*/
|
|
24
24
|
hash: string;
|
|
25
|
+
/** Version returned when the state was loaded, used to reject stale V2 writes. */
|
|
26
|
+
version?: string;
|
|
27
|
+
/** Whether the state was not found when it was loaded. */
|
|
28
|
+
isNew?: boolean;
|
|
25
29
|
}
|
|
26
30
|
/**
|
|
27
31
|
* Represents a custom key for storing state in a specific location.
|
|
@@ -54,16 +58,17 @@ export interface CustomKey {
|
|
|
54
58
|
* for typed access to state properties.
|
|
55
59
|
*/
|
|
56
60
|
export declare class AgentState {
|
|
57
|
-
protected storage:
|
|
61
|
+
protected storage: StorageProvider;
|
|
58
62
|
protected storageKey: StorageKeyFactory;
|
|
59
63
|
private readonly stateKey;
|
|
64
|
+
private get internalStorage();
|
|
60
65
|
/**
|
|
61
66
|
* Creates a new instance of AgentState.
|
|
62
67
|
*
|
|
63
68
|
* @param storage The storage provider used to persist state between turns
|
|
64
69
|
* @param storageKey A factory function that generates keys for storing state data
|
|
65
70
|
*/
|
|
66
|
-
constructor(storage:
|
|
71
|
+
constructor(storage: StorageProvider, storageKey: StorageKeyFactory);
|
|
67
72
|
/**
|
|
68
73
|
* Creates a property accessor for the specified property.
|
|
69
74
|
*
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.AgentState = void 0;
|
|
8
|
+
const storage_1 = require("../storage/storage");
|
|
9
|
+
const storageCompatibility_1 = require("../storage/storageCompatibility");
|
|
8
10
|
const node_crypto_1 = require("node:crypto");
|
|
9
11
|
const agentStatePropertyAccesor_1 = require("./agentStatePropertyAccesor");
|
|
10
12
|
const agents_telemetry_1 = require("@microsoft/agents-telemetry");
|
|
@@ -19,6 +21,9 @@ const logger = (0, agents_telemetry_1.debug)('agents:state');
|
|
|
19
21
|
* for typed access to state properties.
|
|
20
22
|
*/
|
|
21
23
|
class AgentState {
|
|
24
|
+
get internalStorage() {
|
|
25
|
+
return (0, storageCompatibility_1.asStorageV2)(this.storage);
|
|
26
|
+
}
|
|
22
27
|
/**
|
|
23
28
|
* Creates a new instance of AgentState.
|
|
24
29
|
*
|
|
@@ -84,14 +89,21 @@ class AgentState {
|
|
|
84
89
|
* If state is already cached in the turn context and force is not set, the cached version will be used.
|
|
85
90
|
*/
|
|
86
91
|
async load(context, force = false, customKey) {
|
|
92
|
+
var _a, _b;
|
|
93
|
+
var _c;
|
|
87
94
|
const cached = context.turnState.get(this.stateKey);
|
|
88
95
|
if (force || !cached || !cached.state) {
|
|
89
96
|
const key = await this.getStorageOrCustomKey(customKey, context);
|
|
90
97
|
logger.info(`Reading storage with key ${key}`);
|
|
91
|
-
const
|
|
92
|
-
const state =
|
|
98
|
+
const storedItems = await this.internalStorage.read([key]);
|
|
99
|
+
const state = (_c = (0, storageCompatibility_1.getStorageReadValue)(storedItems, key)) !== null && _c !== void 0 ? _c : {};
|
|
93
100
|
const hash = this.calculateChangeHash(state);
|
|
94
|
-
context.turnState.set(this.stateKey, {
|
|
101
|
+
context.turnState.set(this.stateKey, {
|
|
102
|
+
state,
|
|
103
|
+
hash,
|
|
104
|
+
version: (_a = storedItems[key]) === null || _a === void 0 ? void 0 : _a.version,
|
|
105
|
+
isNew: ((_b = storedItems[key]) === null || _b === void 0 ? void 0 : _b.status) === storage_1.StorageOperationStatus.NotFound,
|
|
106
|
+
});
|
|
95
107
|
return state;
|
|
96
108
|
}
|
|
97
109
|
return cached.state;
|
|
@@ -113,12 +125,17 @@ class AgentState {
|
|
|
113
125
|
if (!cached) {
|
|
114
126
|
cached = { state: {}, hash: '' };
|
|
115
127
|
}
|
|
116
|
-
cached.state.eTag = '*';
|
|
117
|
-
const changes = {};
|
|
118
128
|
const key = await this.getStorageOrCustomKey(customKey, context);
|
|
119
|
-
changes[key] = cached.state;
|
|
120
129
|
logger.info(`Writing storage with key ${key}`);
|
|
121
|
-
await this.
|
|
130
|
+
const results = await this.internalStorage.write({ [key]: cached.state }, cached.version !== undefined
|
|
131
|
+
? { expectedVersion: cached.version }
|
|
132
|
+
: cached.isNew && (0, storageCompatibility_1.isStorageV2)(this.storage)
|
|
133
|
+
? { mode: storage_1.StorageWriteMode.CreateOnly }
|
|
134
|
+
: undefined);
|
|
135
|
+
(0, storageCompatibility_1.assertAgentStateWriteSucceeded)(results, key, this.constructor.name);
|
|
136
|
+
const result = results[key];
|
|
137
|
+
cached.version = result.version;
|
|
138
|
+
cached.isNew = false;
|
|
122
139
|
cached.hash = this.calculateChangeHash(cached.state);
|
|
123
140
|
context.turnState.set(this.stateKey, cached);
|
|
124
141
|
}
|
|
@@ -172,7 +189,8 @@ class AgentState {
|
|
|
172
189
|
}
|
|
173
190
|
const key = await this.getStorageOrCustomKey(customKey, context);
|
|
174
191
|
logger.info(`Deleting storage with key ${key}`);
|
|
175
|
-
await this.
|
|
192
|
+
const results = await this.internalStorage.delete([key]);
|
|
193
|
+
(0, storageCompatibility_1.assertStorageDeleteSucceeded)(results, [key]);
|
|
176
194
|
}
|
|
177
195
|
/**
|
|
178
196
|
* Gets the state from the turn context without loading it from storage.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentState.js","sourceRoot":"","sources":["../../../src/state/agentState.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;
|
|
1
|
+
{"version":3,"file":"agentState.js","sourceRoot":"","sources":["../../../src/state/agentState.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,gDAAuI;AACvI,0EAMwC;AAExC,6CAAwC;AACxC,2EAAwE;AACxE,kEAAmD;AAEnD,MAAM,MAAM,GAAG,IAAA,wBAAK,EAAC,cAAc,CAAC,CAAA;AA6CpC;;;;;;;;GAQG;AACH;IAGE,IAAY,eAAe;QACzB,OAAO,IAAA,kCAAW,EAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC;IAED;;;;;OAKG;IACH,YAAuB,OAAwB,EAAY,UAA6B;QAAjE,YAAO,GAAP,OAAO,CAAiB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AAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;IAC9C,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAE,OAAoB;QAC9B,MAAM,MAAM,GAAqB,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAErE,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IAClG,CAAC;CAoCF"}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
import { AgentState } from './agentState';
|
|
6
|
-
import {
|
|
6
|
+
import { StorageProvider } from '../storage/storage';
|
|
7
7
|
/**
|
|
8
8
|
* Manages the state of a conversation.
|
|
9
9
|
*/
|
|
@@ -13,6 +13,6 @@ export declare class ConversationState extends AgentState {
|
|
|
13
13
|
* Creates a new instance of ConversationState.
|
|
14
14
|
* @param storage The storage provider.
|
|
15
15
|
*/
|
|
16
|
-
constructor(storage:
|
|
16
|
+
constructor(storage: StorageProvider, namespace?: string);
|
|
17
17
|
private getStorageKey;
|
|
18
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversationState.js","sourceRoot":"","sources":["../../../src/state/conversationState.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,6CAAyC;AAGzC,gEAAsE;AACtE,gDAAuC;AAEvC;;GAEG;AACH,uBAA+B,SAAQ,uBAAU;IAC/C;;;OAGG;IACH,YAAa,
|
|
1
|
+
{"version":3,"file":"conversationState.js","sourceRoot":"","sources":["../../../src/state/conversationState.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,6CAAyC;AAGzC,gEAAsE;AACtE,gDAAuC;AAEvC;;GAEG;AACH,uBAA+B,SAAQ,uBAAU;IAC/C;;;OAGG;IACH,YAAa,OAAwB,EAAmB,SAAS,GAAW,EAAE;QAC5E,KAAK,CAAC,OAAO,EAAE,CAAC,OAAoB,EAAE,EAAE;YACtC,MAAM,GAAG,GAAW,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YAE/C,OAAO,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,IAAI,KAAK,CAAC,0EAA0E,CAAC,CAAA;QACrG,CAAC,CAAC,CAAA;yBALoD,SAAS;IAMjE,CAAC;IAEO,aAAa,CAAE,OAAoB;QACzC,MAAM,QAAQ,GAAa,OAAO,CAAC,QAAQ,CAAA;QAC3C,MAAM,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAA;QACpC,MAAM,cAAc,GAAG,QAAQ,IAAI,CAAC,QAAQ,CAAC,YAAY,IAAI,IAAI,CAAC,IAAI,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAA;QAErI,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,MAAM,iCAAe,CAAC,iBAAiB,CAAC,KAAK,EAAE,oBAAM,CAAC,wBAAwB,CAAC,CAAA;QACjF,CAAC;QAED,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,iCAAe,CAAC,iBAAiB,CAAC,KAAK,EAAE,oBAAM,CAAC,6BAA6B,CAAC,CAAA;QACtF,CAAC;QAED,OAAO,GAAG,SAAS,kBAAkB,cAAc,IAAI,IAAI,CAAC,SAAS,EAAE,CAAA;IACzE,CAAC;CACF"}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
import { AgentState } from './agentState';
|
|
6
|
-
import {
|
|
6
|
+
import { StorageProvider } from '../storage/storage';
|
|
7
7
|
/**
|
|
8
8
|
* Manages the state of a user.
|
|
9
9
|
*/
|
|
@@ -13,6 +13,6 @@ export declare class UserState extends AgentState {
|
|
|
13
13
|
* Creates a new instance of UserState.
|
|
14
14
|
* @param storage The storage provider.
|
|
15
15
|
*/
|
|
16
|
-
constructor(storage:
|
|
16
|
+
constructor(storage: StorageProvider, namespace?: string);
|
|
17
17
|
private getStorageKey;
|
|
18
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"userState.js","sourceRoot":"","sources":["../../../src/state/userState.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,6CAAyC;AAGzC,gEAAsE;AACtE,gDAAuC;AAEvC;;GAEG;AACH,eAAuB,SAAQ,uBAAU;IACvC;;;QAGI;IACJ,YAAa,
|
|
1
|
+
{"version":3,"file":"userState.js","sourceRoot":"","sources":["../../../src/state/userState.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,6CAAyC;AAGzC,gEAAsE;AACtE,gDAAuC;AAEvC;;GAEG;AACH,eAAuB,SAAQ,uBAAU;IACvC;;;QAGI;IACJ,YAAa,OAAwB,EAAmB,SAAS,GAAW,EAAE;QAC5E,KAAK,CAAC,OAAO,EAAE,CAAC,OAAoB,EAAE,EAAE;YACtC,MAAM,GAAG,GAAW,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YAE/C,OAAO,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAA;QAC7F,CAAC,CAAC,CAAA;yBALoD,SAAS;IAMjE,CAAC;IAEO,aAAa,CAAE,OAAoB;QACzC,MAAM,QAAQ,GAAa,OAAO,CAAC,QAAQ,CAAA;QAE3C,MAAM,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAA;QACpC,MAAM,MAAM,GAAG,QAAQ,IAAI,CAAC,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAA;QAErG,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,MAAM,iCAAe,CAAC,iBAAiB,CAAC,KAAK,EAAE,oBAAM,CAAC,wBAAwB,CAAC,CAAA;QACjF,CAAC;QAED,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,iCAAe,CAAC,iBAAiB,CAAC,KAAK,EAAE,oBAAM,CAAC,qBAAqB,CAAC,CAAA;QAC9E,CAAC;QAED,OAAO,GAAG,SAAS,UAAU,MAAM,IAAI,IAAI,CAAC,SAAS,EAAE,CAAA;IACzD,CAAC;CACF"}
|
|
@@ -67,6 +67,11 @@ export declare enum StatusCodes {
|
|
|
67
67
|
* or different communication method to fulfill the request.
|
|
68
68
|
*/
|
|
69
69
|
UPGRADE_REQUIRED = 426,
|
|
70
|
+
/**
|
|
71
|
+
* The user has sent too many requests in a given amount of time.
|
|
72
|
+
* Used when rate limiting or throttling prevents a turn from being processed.
|
|
73
|
+
*/
|
|
74
|
+
TOO_MANY_REQUESTS = 429,
|
|
70
75
|
/**
|
|
71
76
|
* The server encountered an unexpected condition that prevented it from fulfilling the request.
|
|
72
77
|
* Generic error message when an unexpected agent hosting error occurs
|
package/dist/src/statusCodes.js
CHANGED
|
@@ -71,6 +71,11 @@ var StatusCodes;
|
|
|
71
71
|
* or different communication method to fulfill the request.
|
|
72
72
|
*/
|
|
73
73
|
StatusCodes[StatusCodes["UPGRADE_REQUIRED"] = 426] = "UPGRADE_REQUIRED";
|
|
74
|
+
/**
|
|
75
|
+
* The user has sent too many requests in a given amount of time.
|
|
76
|
+
* Used when rate limiting or throttling prevents a turn from being processed.
|
|
77
|
+
*/
|
|
78
|
+
StatusCodes[StatusCodes["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
|
|
74
79
|
/**
|
|
75
80
|
* The server encountered an unexpected condition that prevented it from fulfilling the request.
|
|
76
81
|
* Generic error message when an unexpected agent hosting error occurs
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"statusCodes.js","sourceRoot":"","sources":["../../src/statusCodes.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;;;GAMG;AACH,IAAY,
|
|
1
|
+
{"version":3,"file":"statusCodes.js","sourceRoot":"","sources":["../../src/statusCodes.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;;;GAMG;AACH,IAAY,WA8FX;AA9FD,WAAY,WAAW;IACrB;;;OAGG;IACH,2CAAQ,CAAA;IAER;;;OAGG;IACH,qDAAa,CAAA;IAEb;;;OAGG;IACH,uEAAsB,CAAA;IAEtB;;;;OAIG;IACH,6DAAiB,CAAA;IAEjB;;;;OAIG;IACH,+DAAkB,CAAA;IAElB;;;;OAIG;IACH,yDAAe,CAAA;IAEf;;;;OAIG;IACH,2EAAwB,CAAA;IAExB;;;;OAIG;IACH,uDAAc,CAAA;IAEd;;;;OAIG;IACH,6EAAyB,CAAA;IAEzB;;;;OAIG;IACH,uEAAsB,CAAA;IAEtB;;;OAGG;IACH,yEAAuB,CAAA;IAEvB;;;;OAIG;IACH,iFAA2B,CAAA;IAE3B;;;;OAIG;IACH,qEAAqB,CAAA;IAErB;;;;OAIG;IACH,6DAAiB,CAAA;AACnB,CAAC,EA9FW,WAAW,aAAX,WAAW,GAAX,WAAW,QA8FtB"}
|
|
@@ -2,109 +2,124 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import { Storage, StoreItem } from './storage';
|
|
5
|
+
import { StorageDeleteOptions, StorageDeleteResults, StorageReadResults, StorageWriteOptions, StorageWriteResults, Storage, StorageV2, StoreItem } from './storage';
|
|
6
|
+
declare class FileStorageInternals {
|
|
7
|
+
private readonly statePath;
|
|
8
|
+
private readonly versionsPath;
|
|
9
|
+
private readonly expirationsPath;
|
|
10
|
+
readonly state: Record<string, unknown>;
|
|
11
|
+
readonly versions: Record<string, string>;
|
|
12
|
+
readonly expirations: Record<string, number>;
|
|
13
|
+
constructor(folder: string);
|
|
14
|
+
flush(): void;
|
|
15
|
+
getOrCreateVersion(key: string): string;
|
|
16
|
+
isExpired(key: string): boolean;
|
|
17
|
+
remove(key: string): void;
|
|
18
|
+
}
|
|
6
19
|
/**
|
|
7
20
|
* A file-based storage implementation that persists data to the local filesystem.
|
|
8
21
|
*
|
|
9
22
|
* @remarks
|
|
10
|
-
* FileStorage stores all data in a single JSON file named
|
|
23
|
+
* FileStorage stores all data in a single JSON file named `state.json` within a specified folder.
|
|
11
24
|
* This implementation is suitable for development scenarios, local testing, and single-instance
|
|
12
25
|
* deployments where shared state across multiple instances is not required.
|
|
13
26
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* wrapped in Promise interfaces
|
|
27
|
+
* Values remain a key-value JSON object in `state.json`. V2 keeps generated storage versions in
|
|
28
|
+
* `state.versions.json` so a value's own `eTag` property is not changed. All operations use
|
|
29
|
+
* synchronous file I/O wrapped in Promise interfaces. Use {@link FileStorageV2} for the
|
|
30
|
+
* structured StorageV2 contract.
|
|
31
|
+
* V2 supports create-only, replace, and expected-version conditions.
|
|
32
|
+
*
|
|
33
|
+
* The inherited constructor creates the folder and `state.json` when needed, then loads values
|
|
34
|
+
* and V2 version metadata into memory. It creates `state.versions.json` on the first V2 write.
|
|
17
35
|
*
|
|
18
36
|
* ### Warning
|
|
19
37
|
* This implementation does not provide:
|
|
20
38
|
* - Thread safety for concurrent access
|
|
21
|
-
* - Optimistic concurrency control (eTag support)
|
|
22
39
|
* - Atomic operations across multiple keys
|
|
23
40
|
* - Scale for large datasets
|
|
24
41
|
*
|
|
25
|
-
* For production scenarios requiring these features,
|
|
26
|
-
* database-backed storage implementations instead.
|
|
42
|
+
* For production scenarios requiring these features, use a database-backed storage implementation.
|
|
27
43
|
*
|
|
28
44
|
* @example
|
|
29
45
|
* ```typescript
|
|
30
|
-
* const
|
|
46
|
+
* const legacyStorage = new FileStorage('./data')
|
|
47
|
+
* const storageV2 = new FileStorageV2('./data-v2')
|
|
31
48
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* 'conversation456': { turn: 5, context: 'discussing weather' }
|
|
36
|
-
* });
|
|
49
|
+
* await storageV2.write({
|
|
50
|
+
* user123: { name: 'John', lastSeen: new Date().toISOString() }
|
|
51
|
+
* })
|
|
37
52
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* console.log(data.user123); // { name: 'John', lastSeen: '...' }
|
|
53
|
+
* const result = await storageV2.read(['user123'])
|
|
54
|
+
* console.log(result.user123.value)
|
|
41
55
|
*
|
|
42
|
-
*
|
|
43
|
-
* await storage.delete(['conversation456']);
|
|
56
|
+
* await storageV2.delete(['user123'])
|
|
44
57
|
* ```
|
|
45
|
-
*
|
|
46
58
|
*/
|
|
47
|
-
export declare class FileStorage implements Storage {
|
|
48
|
-
private _folder;
|
|
49
|
-
private _stateFile;
|
|
59
|
+
export declare class FileStorage extends FileStorageInternals implements Storage {
|
|
50
60
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* @param folder The absolute or relative path to the folder where the state.json file will be stored
|
|
54
|
-
* @throws May throw filesystem errors if the folder cannot be created or accessed
|
|
55
|
-
*
|
|
56
|
-
* @remarks
|
|
57
|
-
* The constructor performs the following initialization steps:
|
|
58
|
-
* 1. Creates the target folder if it doesn't exist (including parent directories)
|
|
59
|
-
* 2. Creates an empty state.json file if it doesn't exist
|
|
60
|
-
* 3. Loads existing data from state.json into memory for fast access
|
|
61
|
+
* Reads legacy items from `state.json`.
|
|
61
62
|
*
|
|
63
|
+
* @param keys The keys to read; must not be empty.
|
|
64
|
+
* @returns Existing items keyed by storage key. Missing keys are omitted.
|
|
62
65
|
*/
|
|
63
|
-
|
|
66
|
+
read(keys: string[]): Promise<StoreItem>;
|
|
64
67
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* @param keys Array of keys to read from storage
|
|
68
|
-
* @returns Promise resolving to an object containing the requested items (keys that don't exist are omitted)
|
|
69
|
-
* @throws ReferenceError if keys array is empty or undefined
|
|
70
|
-
*
|
|
71
|
-
* @remarks
|
|
72
|
-
* This method reads from the in-memory cache that was loaded during construction,
|
|
73
|
-
* making it very fast but potentially returning stale data if the file was
|
|
74
|
-
* modified by external processes.
|
|
68
|
+
* Writes legacy items to `state.json`.
|
|
75
69
|
*
|
|
70
|
+
* @param changes The items to write, keyed by storage key.
|
|
71
|
+
* @throws If `changes` is invalid or the file cannot be written.
|
|
76
72
|
*/
|
|
77
|
-
|
|
73
|
+
write(changes: StoreItem): Promise<void>;
|
|
78
74
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* @param changes Object containing key-value pairs to write to storage
|
|
82
|
-
* @returns Promise that resolves when the write operation completes
|
|
83
|
-
*
|
|
84
|
-
* @remarks
|
|
85
|
-
* This method updates both the in-memory cache and writes the entire state
|
|
86
|
-
* to the state.json file. The file is written with pretty-printing (2-space indentation)
|
|
87
|
-
* for better readability during development and debugging.
|
|
88
|
-
*
|
|
89
|
-
* > [!NOTE]
|
|
90
|
-
* > This implementation does not support eTag-based optimistic concurrency control.
|
|
91
|
-
* > Any eTag values in the changes object are ignored.
|
|
75
|
+
* Deletes legacy items from `state.json`.
|
|
92
76
|
*
|
|
77
|
+
* @param keys The keys to delete; must not be empty. Missing keys are ignored.
|
|
93
78
|
*/
|
|
94
|
-
|
|
79
|
+
delete(keys: string[]): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A file-backed provider for the structured {@link StorageV2} contract.
|
|
83
|
+
*
|
|
84
|
+
* This is the V2 counterpart to {@link FileStorage}. Values are stored in `state.json`, while
|
|
85
|
+
* generated storage versions are stored separately in `state.versions.json`; an `eTag` property
|
|
86
|
+
* in an application value is therefore preserved. Reads and mutations return a result for every
|
|
87
|
+
* requested key and support create-only, replace, and expected-version conditions.
|
|
88
|
+
*
|
|
89
|
+
* Like {@link FileStorage}, this provider is for development, local testing, and single-instance
|
|
90
|
+
* deployments. It does not provide cross-process concurrency or multi-key atomicity.
|
|
91
|
+
*/
|
|
92
|
+
export declare class FileStorageV2 extends StorageV2 {
|
|
93
|
+
private readonly internals;
|
|
95
94
|
/**
|
|
96
|
-
*
|
|
95
|
+
* Creates a V2 file storage provider.
|
|
97
96
|
*
|
|
98
|
-
* @param
|
|
99
|
-
* @
|
|
97
|
+
* @param folder The absolute or relative folder where `state.json` is stored.
|
|
98
|
+
* @throws May throw filesystem errors if the folder or state file cannot be created or read.
|
|
99
|
+
*/
|
|
100
|
+
constructor(folder: string);
|
|
101
|
+
/**
|
|
102
|
+
* Reads items and their stored versions from the local state files.
|
|
100
103
|
*
|
|
101
|
-
* @
|
|
104
|
+
* @param keys The keys to read. Empty batches are valid.
|
|
105
|
+
* @returns A result for every key, with `notFound` for missing items. Returned values are cloned.
|
|
106
|
+
*/
|
|
107
|
+
read<T extends object = Record<string, unknown>>(keys: string[]): Promise<StorageReadResults<T>>;
|
|
108
|
+
/**
|
|
109
|
+
* Writes cloned items and generated versions to the local state files.
|
|
102
110
|
*
|
|
103
|
-
* @
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
|
|
111
|
+
* @param changes The values to write, keyed by storage key.
|
|
112
|
+
* @param options Create-only, replace, or expected-version conditions.
|
|
113
|
+
* @returns A result for every supplied key, including concurrency-condition outcomes.
|
|
114
|
+
*/
|
|
115
|
+
write<T extends object = Record<string, unknown>>(changes: Record<string, T>, options?: StorageWriteOptions): Promise<StorageWriteResults>;
|
|
116
|
+
/**
|
|
117
|
+
* Deletes items and their versions from the local state files.
|
|
107
118
|
*
|
|
119
|
+
* @param keys The keys to delete. Empty batches are valid.
|
|
120
|
+
* @param options An optional expected storage version.
|
|
121
|
+
* @returns A result for every supplied key.
|
|
108
122
|
*/
|
|
109
|
-
delete(keys: string[]): Promise<
|
|
123
|
+
delete(keys: string[], options?: StorageDeleteOptions): Promise<StorageDeleteResults>;
|
|
110
124
|
}
|
|
125
|
+
export {};
|