orchajs 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +50 -3
  2. package/dist/amarsia/analytics.js +12 -0
  3. package/dist/amarsia/inference.d.ts +6 -0
  4. package/dist/amarsia/inference.js +337 -0
  5. package/dist/browser.js +5 -0
  6. package/dist/cli-agents-template.d.ts +1 -1
  7. package/dist/cli-agents-template.js +31 -7
  8. package/dist/compiler/build-project.js +57 -14
  9. package/dist/compiler/node-esm-banner.d.ts +2 -0
  10. package/dist/compiler/node-esm-banner.js +5 -0
  11. package/dist/orcha.js +5 -0
  12. package/dist/project-loader.js +4 -2
  13. package/dist/providers/types.d.ts +4 -0
  14. package/dist/react-native.js +5 -0
  15. package/dist/runtime/agent.js +103 -14
  16. package/dist/runtime/compiler-hook.d.ts +4 -0
  17. package/dist/runtime/compiler-hook.js +9 -0
  18. package/dist/runtime/create-orcha.d.ts +19 -0
  19. package/dist/runtime/create-orcha.js +109 -12
  20. package/dist/runtime/optional-features.d.ts +19 -0
  21. package/dist/runtime/optional-features.js +22 -0
  22. package/dist/runtime/platform-browser.js +16 -3
  23. package/dist/runtime/platform-node.js +16 -2
  24. package/dist/runtime/platform-react-native.js +16 -3
  25. package/dist/storage/browser-indexeddb.d.ts +3 -0
  26. package/dist/storage/browser-indexeddb.js +72 -8
  27. package/dist/storage/cloud-outbox.d.ts +30 -0
  28. package/dist/storage/cloud-outbox.js +628 -0
  29. package/dist/storage/cloud.d.ts +1 -0
  30. package/dist/storage/cloud.js +1 -0
  31. package/dist/storage/node-jsonl.d.ts +3 -0
  32. package/dist/storage/node-jsonl.js +273 -4
  33. package/dist/storage/node-sqlite.d.ts +3 -0
  34. package/dist/storage/node-sqlite.js +66 -17
  35. package/dist/storage/react-native-jsonl.d.ts +3 -0
  36. package/dist/storage/react-native-jsonl.js +223 -0
  37. package/dist/types.d.ts +21 -2
  38. package/dist-cjs/amarsia/analytics.js +12 -0
  39. package/dist-cjs/amarsia/inference.js +335 -0
  40. package/dist-cjs/browser.js +5 -0
  41. package/dist-cjs/cli-agents-template.js +31 -7
  42. package/dist-cjs/compiler/build-project.js +68 -4
  43. package/dist-cjs/compiler/node-esm-banner.js +32 -0
  44. package/dist-cjs/orcha.js +5 -0
  45. package/dist-cjs/project-loader.js +7 -5
  46. package/dist-cjs/react-native.js +5 -0
  47. package/dist-cjs/runtime/agent.js +137 -30
  48. package/dist-cjs/runtime/compiler-hook.js +11 -0
  49. package/dist-cjs/runtime/create-orcha.js +117 -12
  50. package/dist-cjs/runtime/optional-features.js +58 -0
  51. package/dist-cjs/runtime/platform-browser.js +16 -3
  52. package/dist-cjs/runtime/platform-node.js +16 -2
  53. package/dist-cjs/runtime/platform-react-native.js +18 -3
  54. package/dist-cjs/storage/browser-indexeddb.js +73 -8
  55. package/dist-cjs/storage/cloud-outbox.js +646 -0
  56. package/dist-cjs/storage/cloud.js +29 -0
  57. package/dist-cjs/storage/node-jsonl.js +246 -3
  58. package/dist-cjs/storage/node-sqlite.js +70 -19
  59. package/dist-cjs/storage/react-native-jsonl.js +207 -0
  60. package/package.json +1 -1
@@ -24,12 +24,18 @@ __export(create_orcha_exports, {
24
24
  module.exports = __toCommonJS(create_orcha_exports);
25
25
  var import_agent = require("./agent.js");
26
26
  var import_analytics = require("../amarsia/analytics.js");
27
+ var import_compiler_hook = require("./compiler-hook.js");
27
28
  var import_context = require("./context.js");
29
+ var import_optional_features = require("./optional-features.js");
28
30
  class OrchaRuntimeCore {
29
31
  #agents = /* @__PURE__ */ new Map();
30
32
  #bundle;
33
+ #inferenceEnabled = false;
34
+ #cloudStorageEnabled = false;
31
35
  #client;
32
36
  #telemetry;
37
+ #stores = /* @__PURE__ */ new Set();
38
+ #closing;
33
39
  #activeSessions = /* @__PURE__ */ new Set();
34
40
  #sessionControls = /* @__PURE__ */ new Map();
35
41
  #resolveBundle;
@@ -44,16 +50,25 @@ class OrchaRuntimeCore {
44
50
  this.#client = client;
45
51
  }
46
52
  init(configuration) {
47
- if (this.#telemetry) {
48
- void this.#telemetry.close();
49
- }
50
- for (const agent of this.#agents.values()) {
51
- if (agent instanceof import_agent.RuntimeAgent) {
52
- void agent._close();
53
- }
53
+ if (this.#bundle || this.#closing) {
54
+ throw new Error(
55
+ "Orcha is already initialized. Await orcha.close() before initializing it again."
56
+ );
54
57
  }
55
58
  const projectRoot = this.#platform.projectRoot(configuration);
56
59
  const bundle = configuration.bundle ?? this.#resolveBundle(configuration);
60
+ const inferenceEnabled = configuration.amarsia?.inference === true;
61
+ if ((0, import_compiler_hook.isRegistryCompilationActive)()) {
62
+ this.#bundle = bundle;
63
+ this.#inferenceEnabled = inferenceEnabled;
64
+ this.#cloudStorageEnabled = configuration.storage?.strategy === "cloud";
65
+ return this.#client;
66
+ }
67
+ if (inferenceEnabled && !configuration.amarsia?.key.trim()) {
68
+ throw new Error(
69
+ "Amarsia inference requires a non-empty amarsia.key."
70
+ );
71
+ }
57
72
  if (configuration.amarsia?.key && this.#platform.syncProject) {
58
73
  this.#platform.syncProject({
59
74
  configuration,
@@ -63,7 +78,7 @@ class OrchaRuntimeCore {
63
78
  }
64
79
  const runtimeConfiguration = {
65
80
  providers: Object.fromEntries(
66
- Object.entries(configuration.providers).map(([name, provider]) => [
81
+ Object.entries(configuration.providers ?? {}).map(([name, provider]) => [
67
82
  name,
68
83
  typeof provider === "string" ? { apiKey: provider } : provider
69
84
  ])
@@ -75,6 +90,14 @@ class OrchaRuntimeCore {
75
90
  mcp: configuration.mcp,
76
91
  storage: configuration.storage
77
92
  };
93
+ if (inferenceEnabled) {
94
+ for (const provider of compiledProviderNames(bundle)) {
95
+ const providers = runtimeConfiguration.providers;
96
+ providers[provider] ??= { apiKey: "" };
97
+ }
98
+ }
99
+ const inferenceGenerator = inferenceEnabled ? (0, import_optional_features.createInferenceGenerator)(configuration.amarsia) : void 0;
100
+ const generateProviderResponse = inferenceGenerator ?? this.#generateProviderResponse;
78
101
  if (bundle.actionRuntime && runtimeConfiguration.actions?.runtime !== bundle.actionRuntime) {
79
102
  throw new Error(
80
103
  `Production bundle expects local action runtime "${bundle.actionRuntime}".`
@@ -89,7 +112,9 @@ class OrchaRuntimeCore {
89
112
  this.#telemetry = telemetry;
90
113
  const createRuntimeStore = (manifest) => {
91
114
  const store = createStore(manifest);
92
- return telemetry ? (0, import_analytics.withAmarsiaTelemetry)({ store, manifest, client: telemetry }) : store;
115
+ const runtimeStore = telemetry ? (0, import_analytics.withAmarsiaTelemetry)({ store, manifest, client: telemetry }) : store;
116
+ this.#stores.add(runtimeStore);
117
+ return runtimeStore;
93
118
  };
94
119
  const agents = /* @__PURE__ */ new Map();
95
120
  for (const [name, manifest] of Object.entries(bundle.agents)) {
@@ -103,7 +128,7 @@ class OrchaRuntimeCore {
103
128
  store: createRuntimeStore(subagentManifest),
104
129
  activeSessions: this.#activeSessions,
105
130
  sessionControls: this.#sessionControls,
106
- generateProviderResponse: this.#generateProviderResponse,
131
+ generateProviderResponse,
107
132
  delegatedOnly: true,
108
133
  projectRoot,
109
134
  randomUUID: this.#platform.randomUUID
@@ -119,7 +144,7 @@ class OrchaRuntimeCore {
119
144
  store: createRuntimeStore(manifest),
120
145
  activeSessions: this.#activeSessions,
121
146
  sessionControls: this.#sessionControls,
122
- generateProviderResponse: this.#generateProviderResponse,
147
+ generateProviderResponse,
123
148
  subagents,
124
149
  projectRoot,
125
150
  randomUUID: this.#platform.randomUUID
@@ -127,19 +152,81 @@ class OrchaRuntimeCore {
127
152
  );
128
153
  }
129
154
  this.#bundle = bundle;
155
+ this.#inferenceEnabled = inferenceEnabled;
156
+ this.#cloudStorageEnabled = configuration.storage?.strategy === "cloud";
130
157
  this.#agents = agents;
131
158
  (0, import_context.setOrchaRuntimeContext)(this.#client, {
132
159
  projectRoot,
133
160
  registrations: { ...configuration.agents },
134
161
  bundle,
135
162
  configuration: runtimeConfiguration,
136
- generateProviderResponse: this.#generateProviderResponse
163
+ generateProviderResponse
137
164
  });
138
165
  return this.#client;
139
166
  }
140
167
  isInitialized() {
141
168
  return this.#bundle !== void 0;
142
169
  }
170
+ get cloudStoragePendingCount() {
171
+ let pending = 0;
172
+ for (const store of this.#stores) {
173
+ pending += store.cloudStoragePendingCount ?? 0;
174
+ }
175
+ return pending;
176
+ }
177
+ get cloudStorageLastError() {
178
+ let error;
179
+ for (const store of this.#stores) {
180
+ error = store.cloudStorageLastError ?? error;
181
+ }
182
+ return error;
183
+ }
184
+ async flushCloudStorage(options = {}) {
185
+ const normalized = typeof options === "number" ? { timeoutMs: options } : options;
186
+ await Promise.all(
187
+ [...this.#stores].map(
188
+ (store) => store.flushCloudStorage?.(normalized)
189
+ )
190
+ );
191
+ }
192
+ async close(options = {}) {
193
+ if (this.#closing) return this.#closing;
194
+ const close = (async () => {
195
+ const backupOperations = [
196
+ this.#telemetry?.close(options),
197
+ ...[...this.#stores].map(
198
+ (store) => store.closeCloudStorage?.(options)
199
+ )
200
+ ].filter(
201
+ (operation) => operation !== void 0
202
+ );
203
+ const resourceOperations = [
204
+ ...[...this.#agents.values()].flatMap(
205
+ (agent) => agent instanceof import_agent.RuntimeAgent ? [agent._close()] : []
206
+ )
207
+ ];
208
+ const [, resourceResults] = await Promise.all([
209
+ Promise.allSettled(backupOperations),
210
+ Promise.allSettled(resourceOperations)
211
+ ]);
212
+ const failure = resourceResults.find(
213
+ (result) => result.status === "rejected"
214
+ );
215
+ this.#stores.clear();
216
+ this.#agents.clear();
217
+ this.#telemetry = void 0;
218
+ this.#bundle = void 0;
219
+ this.#inferenceEnabled = false;
220
+ this.#cloudStorageEnabled = false;
221
+ if (failure) throw failure.reason;
222
+ })();
223
+ this.#closing = close;
224
+ try {
225
+ await close;
226
+ } finally {
227
+ this.#closing = void 0;
228
+ }
229
+ }
143
230
  async flushTelemetry(options) {
144
231
  await this.#telemetry?.drain(options);
145
232
  }
@@ -149,10 +236,28 @@ class OrchaRuntimeCore {
149
236
  }
150
237
  return this.#bundle;
151
238
  }
239
+ isInferenceEnabled() {
240
+ return this.#inferenceEnabled;
241
+ }
242
+ isCloudStorageEnabled() {
243
+ return this.#cloudStorageEnabled;
244
+ }
152
245
  agent(name) {
153
246
  return this.#agents.get(name);
154
247
  }
155
248
  }
249
+ function compiledProviderNames(bundle) {
250
+ const names = /* @__PURE__ */ new Set();
251
+ const visit = (manifest) => {
252
+ names.add(manifest.provider);
253
+ for (const evaluation of Object.values(manifest.evaluations ?? {})) {
254
+ if (evaluation.enabled !== false) names.add(evaluation.provider);
255
+ }
256
+ Object.values(manifest.subagents ?? {}).forEach(visit);
257
+ };
258
+ Object.values(bundle.agents).forEach(visit);
259
+ return names;
260
+ }
156
261
  function createOrcha(resolveBundle, generateProviderResponse, platform) {
157
262
  const core = new OrchaRuntimeCore(
158
263
  resolveBundle,
@@ -0,0 +1,58 @@
1
+ "use strict"; const __orchaImportMetaUrl = require("url").pathToFileURL(__filename).href;
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var optional_features_exports = {};
21
+ __export(optional_features_exports, {
22
+ createCloudSessionStore: () => createCloudSessionStore,
23
+ createInferenceGenerator: () => createInferenceGenerator,
24
+ setCloudSessionStoreFactory: () => setCloudSessionStoreFactory,
25
+ setInferenceGeneratorFactory: () => setInferenceGeneratorFactory
26
+ });
27
+ module.exports = __toCommonJS(optional_features_exports);
28
+ let inferenceGeneratorFactory;
29
+ let cloudSessionStoreFactory;
30
+ function setInferenceGeneratorFactory(factory) {
31
+ inferenceGeneratorFactory = factory;
32
+ }
33
+ function setCloudSessionStoreFactory(factory) {
34
+ cloudSessionStoreFactory = factory;
35
+ }
36
+ function createInferenceGenerator(configuration) {
37
+ if (!inferenceGeneratorFactory) {
38
+ throw new Error(
39
+ "Amarsia inference was not included in this Orcha runtime."
40
+ );
41
+ }
42
+ return inferenceGeneratorFactory(configuration);
43
+ }
44
+ function createCloudSessionStore(options) {
45
+ if (!cloudSessionStoreFactory) {
46
+ throw new Error(
47
+ "Cloud session storage was not included in this Orcha runtime."
48
+ );
49
+ }
50
+ return cloudSessionStoreFactory(options);
51
+ }
52
+ // Annotate the CommonJS export names for ESM import in node:
53
+ 0 && (module.exports = {
54
+ createCloudSessionStore,
55
+ createInferenceGenerator,
56
+ setCloudSessionStoreFactory,
57
+ setInferenceGeneratorFactory
58
+ });
@@ -23,6 +23,7 @@ __export(platform_browser_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(platform_browser_exports);
25
25
  var import_browser_indexeddb = require("../storage/browser-indexeddb.js");
26
+ var import_optional_features = require("./optional-features.js");
26
27
  var import_random_id = require("./random-id.js");
27
28
  const browserRuntimePlatform = {
28
29
  randomUUID: import_random_id.portableRandomUUID,
@@ -31,15 +32,27 @@ const browserRuntimePlatform = {
31
32
  },
32
33
  createSessionStore({ configuration, manifest }) {
33
34
  const strategy = configuration.storage?.strategy ?? "indexeddb";
34
- if (strategy !== "indexeddb") {
35
+ if (strategy !== "indexeddb" && strategy !== "cloud") {
35
36
  throw new Error(
36
- `Browser runtimes require storage strategy "indexeddb", received "${strategy}".`
37
+ `Browser runtimes require storage strategy "indexeddb" or "cloud", received "${strategy}".`
37
38
  );
38
39
  }
39
- return new import_browser_indexeddb.BrowserIndexedDbSessionStore(
40
+ const local = new import_browser_indexeddb.BrowserIndexedDbSessionStore(
40
41
  manifest.key ?? manifest.name,
41
42
  configuration.storage?.directory ?? "orchajs"
42
43
  );
44
+ if (strategy !== "cloud") {
45
+ return local;
46
+ }
47
+ if (!configuration.amarsia?.key) {
48
+ throw new Error('Storage strategy "cloud" requires amarsia.key.');
49
+ }
50
+ return (0, import_optional_features.createCloudSessionStore)({
51
+ local,
52
+ key: configuration.amarsia.key,
53
+ endpoint: configuration.amarsia.endpoint,
54
+ agentName: manifest.key ?? manifest.name
55
+ });
43
56
  }
44
57
  };
45
58
  // Annotate the CommonJS export names for ESM import in node:
@@ -26,6 +26,7 @@ var import_crypto = require("crypto");
26
26
  var import_path = require("path");
27
27
  var import_sync = require("../amarsia/sync.js");
28
28
  var import_create_store = require("../storage/create-store.js");
29
+ var import_optional_features = require("./optional-features.js");
29
30
  const nodeRuntimePlatform = {
30
31
  randomUUID: import_crypto.randomUUID,
31
32
  projectRoot(configuration) {
@@ -34,12 +35,25 @@ const nodeRuntimePlatform = {
34
35
  );
35
36
  },
36
37
  createSessionStore({ configuration, projectRoot, manifest }) {
37
- return (0, import_create_store.createSessionStore)({
38
- strategy: configuration.storage?.strategy,
38
+ const strategy = configuration.storage?.strategy;
39
+ const local = (0, import_create_store.createSessionStore)({
40
+ strategy: strategy === "cloud" ? "node-jsonl" : strategy,
39
41
  directory: configuration.storage?.directory,
40
42
  projectRoot,
41
43
  agentName: manifest.key ?? manifest.name
42
44
  });
45
+ if (strategy !== "cloud") {
46
+ return local;
47
+ }
48
+ if (!configuration.amarsia?.key) {
49
+ throw new Error('Storage strategy "cloud" requires amarsia.key.');
50
+ }
51
+ return (0, import_optional_features.createCloudSessionStore)({
52
+ local,
53
+ key: configuration.amarsia.key,
54
+ endpoint: configuration.amarsia.endpoint,
55
+ agentName: manifest.key ?? manifest.name
56
+ });
43
57
  },
44
58
  syncProject({ configuration, bundle, projectRoot }) {
45
59
  if (!configuration.amarsia?.key) {
@@ -23,6 +23,7 @@ __export(platform_react_native_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(platform_react_native_exports);
25
25
  var import_react_native_jsonl = require("../storage/react-native-jsonl.js");
26
+ var import_optional_features = require("./optional-features.js");
26
27
  var import_random_id = require("./random-id.js");
27
28
  const reactNativeRuntimePlatform = {
28
29
  randomUUID: import_random_id.portableRandomUUID,
@@ -31,12 +32,26 @@ const reactNativeRuntimePlatform = {
31
32
  },
32
33
  createSessionStore({ configuration, manifest }) {
33
34
  const strategy = configuration.storage?.strategy ?? "react-native";
34
- if (strategy !== "react-native") {
35
+ if (strategy !== "react-native" && strategy !== "cloud") {
35
36
  throw new Error(
36
- `React Native requires storage strategy "react-native", received "${strategy}".`
37
+ `React Native requires storage strategy "react-native" or "cloud", received "${strategy}".`
37
38
  );
38
39
  }
39
- return new import_react_native_jsonl.ReactNativeJsonlSessionStore(manifest.key ?? manifest.name);
40
+ const local = new import_react_native_jsonl.ReactNativeJsonlSessionStore(
41
+ manifest.key ?? manifest.name
42
+ );
43
+ if (strategy !== "cloud") {
44
+ return local;
45
+ }
46
+ if (!configuration.amarsia?.key) {
47
+ throw new Error('Storage strategy "cloud" requires amarsia.key.');
48
+ }
49
+ return (0, import_optional_features.createCloudSessionStore)({
50
+ local,
51
+ key: configuration.amarsia.key,
52
+ endpoint: configuration.amarsia.endpoint,
53
+ agentName: manifest.key ?? manifest.name
54
+ });
40
55
  }
41
56
  };
42
57
  // Annotate the CommonJS export names for ESM import in node:
@@ -22,8 +22,10 @@ __export(browser_indexeddb_exports, {
22
22
  BrowserIndexedDbSessionStore: () => BrowserIndexedDbSessionStore
23
23
  });
24
24
  module.exports = __toCommonJS(browser_indexeddb_exports);
25
- const DATABASE_VERSION = 1;
25
+ const DATABASE_VERSION = 3;
26
26
  const EVENT_STORE = "events";
27
+ const CLOUD_ACK_STORE = "cloud-acknowledgements";
28
+ const SESSION_SEQUENCE_INDEX = "session-sequence";
27
29
  class BrowserIndexedDbSessionStore {
28
30
  #databaseName;
29
31
  #namespace;
@@ -43,6 +45,24 @@ class BrowserIndexedDbSessionStore {
43
45
  await transactionComplete(transaction);
44
46
  return records.sort((left, right) => left.sequence - right.sequence).map((record) => record.event);
45
47
  }
48
+ async readAfterSequence(sessionId, afterSequence, limit) {
49
+ validateTailArguments(afterSequence, limit);
50
+ const database = await this.#open();
51
+ const transaction = database.transaction(EVENT_STORE, "readonly");
52
+ const index = transaction.objectStore(EVENT_STORE).index(SESSION_SEQUENCE_INDEX);
53
+ const records = await request(
54
+ index.getAll(
55
+ IDBKeyRange.bound(
56
+ [this.#namespace, sessionId, afterSequence],
57
+ [this.#namespace, sessionId, Number.MAX_SAFE_INTEGER],
58
+ true
59
+ ),
60
+ limit
61
+ )
62
+ );
63
+ await transactionComplete(transaction);
64
+ return records.map((record) => record.event);
65
+ }
46
66
  async append(sessionId, events) {
47
67
  if (events.length === 0) {
48
68
  return;
@@ -73,6 +93,31 @@ class BrowserIndexedDbSessionStore {
73
93
  await transactionComplete(transaction);
74
94
  return [...new Set(records.map((record) => record.sessionId))].sort();
75
95
  }
96
+ async getCloudAcknowledgement(sessionId) {
97
+ const database = await this.#open();
98
+ const transaction = database.transaction(CLOUD_ACK_STORE, "readonly");
99
+ const value = await request(
100
+ transaction.objectStore(CLOUD_ACK_STORE).get(
101
+ acknowledgementId(this.#namespace, sessionId)
102
+ )
103
+ );
104
+ await transactionComplete(transaction);
105
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : 0;
106
+ }
107
+ async setCloudAcknowledgement(sessionId, sequence) {
108
+ if (!Number.isInteger(sequence) || sequence < 0) {
109
+ throw new Error("Invalid cloud acknowledgement sequence.");
110
+ }
111
+ const database = await this.#open();
112
+ const transaction = database.transaction(CLOUD_ACK_STORE, "readwrite");
113
+ const store = transaction.objectStore(CLOUD_ACK_STORE);
114
+ const id = acknowledgementId(this.#namespace, sessionId);
115
+ const current = await request(store.get(id));
116
+ if (typeof current !== "number" || !Number.isInteger(current) || current < sequence) {
117
+ store.put(sequence, id);
118
+ }
119
+ await transactionComplete(transaction);
120
+ }
76
121
  subscribe(sessionId, listener) {
77
122
  const listeners = this.#listeners.get(sessionId) ?? /* @__PURE__ */ new Set();
78
123
  listeners.add(listener);
@@ -99,14 +144,26 @@ class BrowserIndexedDbSessionStore {
99
144
  const openRequest = indexedDB.open(this.#databaseName, DATABASE_VERSION);
100
145
  openRequest.onupgradeneeded = () => {
101
146
  const database = openRequest.result;
102
- if (database.objectStoreNames.contains(EVENT_STORE)) {
103
- return;
147
+ let eventStore;
148
+ if (!database.objectStoreNames.contains(EVENT_STORE)) {
149
+ eventStore = database.createObjectStore(EVENT_STORE, {
150
+ keyPath: "id"
151
+ });
152
+ eventStore.createIndex("session", ["namespace", "sessionId"]);
153
+ eventStore.createIndex("namespace", "namespace");
154
+ } else {
155
+ eventStore = openRequest.transaction.objectStore(EVENT_STORE);
156
+ }
157
+ if (!eventStore.indexNames.contains(SESSION_SEQUENCE_INDEX)) {
158
+ eventStore.createIndex(SESSION_SEQUENCE_INDEX, [
159
+ "namespace",
160
+ "sessionId",
161
+ "sequence"
162
+ ]);
163
+ }
164
+ if (!database.objectStoreNames.contains(CLOUD_ACK_STORE)) {
165
+ database.createObjectStore(CLOUD_ACK_STORE);
104
166
  }
105
- const store = database.createObjectStore(EVENT_STORE, {
106
- keyPath: "id"
107
- });
108
- store.createIndex("session", ["namespace", "sessionId"]);
109
- store.createIndex("namespace", "namespace");
110
167
  };
111
168
  openRequest.onsuccess = () => resolve(openRequest.result);
112
169
  openRequest.onerror = () => reject(openRequest.error);
@@ -114,6 +171,9 @@ class BrowserIndexedDbSessionStore {
114
171
  });
115
172
  }
116
173
  }
174
+ function acknowledgementId(namespace, sessionId) {
175
+ return `${namespace}\0${sessionId}`;
176
+ }
117
177
  function eventId(namespace, sessionId, sequence) {
118
178
  return `${namespace}\0${sessionId}\0${sequence.toString().padStart(12, "0")}`;
119
179
  }
@@ -130,6 +190,11 @@ function transactionComplete(transaction) {
130
190
  transaction.onabort = () => reject(transaction.error ?? new Error("IndexedDB transaction aborted."));
131
191
  });
132
192
  }
193
+ function validateTailArguments(afterSequence, limit) {
194
+ if (!Number.isInteger(afterSequence) || afterSequence < 0 || !Number.isInteger(limit) || limit <= 0) {
195
+ throw new Error("Invalid session event tail range.");
196
+ }
197
+ }
133
198
  // Annotate the CommonJS export names for ESM import in node:
134
199
  0 && (module.exports = {
135
200
  BrowserIndexedDbSessionStore