rivetkit 2.3.3 → 2.3.5-rc.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.
Files changed (161) hide show
  1. package/dist/browser/client.d.ts +157 -9
  2. package/dist/browser/client.js +518 -542
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.d.ts +101 -2
  5. package/dist/browser/inspector/client.js +1245 -227
  6. package/dist/browser/inspector/client.js.map +1 -1
  7. package/dist/tsup/agent-os/index.cjs +103 -36
  8. package/dist/tsup/agent-os/index.cjs.map +1 -1
  9. package/dist/tsup/agent-os/index.d.cts +159 -11
  10. package/dist/tsup/agent-os/index.d.ts +159 -11
  11. package/dist/tsup/agent-os/index.js +103 -36
  12. package/dist/tsup/agent-os/index.js.map +1 -1
  13. package/dist/tsup/{chunk-34P567PO.cjs → chunk-4XKED5P5.cjs} +145 -176
  14. package/dist/tsup/chunk-4XKED5P5.cjs.map +1 -0
  15. package/dist/tsup/{chunk-PYO2U53V.cjs → chunk-6HQCLWAW.cjs} +384 -4
  16. package/dist/tsup/chunk-6HQCLWAW.cjs.map +1 -0
  17. package/dist/tsup/{chunk-JZ7TWV65.js → chunk-7AFZMFPQ.js} +15 -1
  18. package/dist/tsup/chunk-7AFZMFPQ.js.map +1 -0
  19. package/dist/tsup/{chunk-BLDO2FKO.js → chunk-B4IZU537.js} +382 -2
  20. package/dist/tsup/chunk-B4IZU537.js.map +1 -0
  21. package/dist/tsup/{chunk-3MHDOUD7.cjs → chunk-G3CCRCTK.cjs} +14 -9
  22. package/dist/tsup/chunk-G3CCRCTK.cjs.map +1 -0
  23. package/dist/tsup/{chunk-CXRPENO3.js → chunk-GAV4PKFT.js} +15 -46
  24. package/dist/tsup/{chunk-CXRPENO3.js.map → chunk-GAV4PKFT.js.map} +1 -1
  25. package/dist/tsup/{chunk-HXUEHHJF.js → chunk-JI6GZ2C2.js} +2 -11
  26. package/dist/tsup/{chunk-HXUEHHJF.js.map → chunk-JI6GZ2C2.js.map} +1 -1
  27. package/dist/tsup/{chunk-YUEC5SEY.cjs → chunk-M3FCXL3Q.cjs} +3 -3
  28. package/dist/tsup/{chunk-YUEC5SEY.cjs.map → chunk-M3FCXL3Q.cjs.map} +1 -1
  29. package/dist/tsup/{chunk-KEKWJECX.js → chunk-MGTSVSUC.js} +2 -2
  30. package/dist/tsup/{chunk-VE2X4KMG.cjs → chunk-MYWZCDML.cjs} +2 -11
  31. package/dist/tsup/chunk-MYWZCDML.cjs.map +1 -0
  32. package/dist/tsup/{chunk-DFA7SYSW.js → chunk-NIC3QLKL.js} +3 -3
  33. package/dist/tsup/{chunk-BZM4PMFS.js → chunk-P4OGQ4JL.js} +5 -7
  34. package/dist/tsup/{chunk-BZM4PMFS.js.map → chunk-P4OGQ4JL.js.map} +1 -1
  35. package/dist/tsup/{chunk-NT2NK3VT.js → chunk-PAG6G46L.js} +2 -2
  36. package/dist/tsup/{chunk-XIX5DOZN.js → chunk-SHBE77IT.js} +14 -9
  37. package/dist/tsup/chunk-SHBE77IT.js.map +1 -0
  38. package/dist/tsup/{chunk-TYLP3U4Y.cjs → chunk-TNIWIUNV.cjs} +9 -11
  39. package/dist/tsup/chunk-TNIWIUNV.cjs.map +1 -0
  40. package/dist/tsup/{chunk-S644FL73.cjs → chunk-TR5NLR5V.cjs} +4 -4
  41. package/dist/tsup/{chunk-S644FL73.cjs.map → chunk-TR5NLR5V.cjs.map} +1 -1
  42. package/dist/tsup/{chunk-ZA7FLHKH.cjs → chunk-UEULFXUI.cjs} +16 -2
  43. package/dist/tsup/chunk-UEULFXUI.cjs.map +1 -0
  44. package/dist/tsup/{chunk-X63ROSOG.cjs → chunk-UJ2FQY63.cjs} +7 -7
  45. package/dist/tsup/{chunk-X63ROSOG.cjs.map → chunk-UJ2FQY63.cjs.map} +1 -1
  46. package/dist/tsup/client/mod.cjs +7 -8
  47. package/dist/tsup/client/mod.cjs.map +1 -1
  48. package/dist/tsup/client/mod.d.cts +5 -4
  49. package/dist/tsup/client/mod.d.ts +5 -4
  50. package/dist/tsup/client/mod.js +6 -7
  51. package/dist/tsup/common/log.cjs +2 -2
  52. package/dist/tsup/common/log.js +1 -1
  53. package/dist/tsup/common/websocket.cjs +3 -3
  54. package/dist/tsup/common/websocket.js +2 -2
  55. package/dist/tsup/{config-D49x8NpL.d.cts → config-BxOimoGX.d.cts} +16 -0
  56. package/dist/tsup/{config-D49x8NpL.d.ts → config-BxOimoGX.d.ts} +16 -0
  57. package/dist/tsup/{config-ZkIsRFsN.d.ts → config-BzdqbLAP.d.ts} +164 -27
  58. package/dist/tsup/{config-BfBhjEbk.d.cts → config-CPPEwn_e.d.cts} +164 -27
  59. package/dist/tsup/{context-sZ9GlaBK.d.cts → context-DSA9gFXB.d.cts} +2 -2
  60. package/dist/tsup/{context-B7-84u9K.d.ts → context-wWv-K2K_.d.ts} +2 -2
  61. package/dist/tsup/db/drizzle.cjs +120 -68
  62. package/dist/tsup/db/drizzle.cjs.map +1 -1
  63. package/dist/tsup/db/drizzle.d.cts +8 -3
  64. package/dist/tsup/db/drizzle.d.ts +8 -3
  65. package/dist/tsup/db/drizzle.js +120 -68
  66. package/dist/tsup/db/drizzle.js.map +1 -1
  67. package/dist/tsup/db/mod.cjs +74 -22
  68. package/dist/tsup/db/mod.cjs.map +1 -1
  69. package/dist/tsup/db/mod.d.cts +4 -3
  70. package/dist/tsup/db/mod.d.ts +4 -3
  71. package/dist/tsup/db/mod.js +74 -22
  72. package/dist/tsup/db/mod.js.map +1 -1
  73. package/dist/tsup/dynamic/mod.cjs +2 -2
  74. package/dist/tsup/dynamic/mod.d.cts +3 -3
  75. package/dist/tsup/dynamic/mod.d.ts +3 -3
  76. package/dist/tsup/dynamic/mod.js +1 -1
  77. package/dist/tsup/inspector/mod.cjs +4 -5
  78. package/dist/tsup/inspector/mod.cjs.map +1 -1
  79. package/dist/tsup/inspector/mod.js +3 -4
  80. package/dist/tsup/inspector/mod.js.map +1 -1
  81. package/dist/tsup/inspector-tab/mod.cjs +2 -2
  82. package/dist/tsup/inspector-tab/mod.cjs.map +1 -1
  83. package/dist/tsup/inspector-tab/mod.d.cts +5 -5
  84. package/dist/tsup/inspector-tab/mod.d.ts +5 -5
  85. package/dist/tsup/inspector-tab/mod.js +1 -1
  86. package/dist/tsup/inspector-tab/mod.js.map +1 -1
  87. package/dist/tsup/mod.cjs +605 -158
  88. package/dist/tsup/mod.cjs.map +1 -1
  89. package/dist/tsup/mod.d.cts +5 -5
  90. package/dist/tsup/mod.d.ts +5 -5
  91. package/dist/tsup/mod.js +528 -81
  92. package/dist/tsup/mod.js.map +1 -1
  93. package/dist/tsup/test/mod.cjs +10 -11
  94. package/dist/tsup/test/mod.cjs.map +1 -1
  95. package/dist/tsup/test/mod.d.cts +3 -3
  96. package/dist/tsup/test/mod.d.ts +3 -3
  97. package/dist/tsup/test/mod.js +6 -7
  98. package/dist/tsup/test/mod.js.map +1 -1
  99. package/dist/tsup/utils-CV0akuDD.d.ts +162 -0
  100. package/dist/tsup/utils-DQDCphrl.d.cts +162 -0
  101. package/dist/tsup/utils.cjs +6 -2
  102. package/dist/tsup/utils.cjs.map +1 -1
  103. package/dist/tsup/utils.d.cts +3 -146
  104. package/dist/tsup/utils.d.ts +3 -146
  105. package/dist/tsup/utils.js +5 -1
  106. package/dist/tsup/workflow/mod.cjs +198 -151
  107. package/dist/tsup/workflow/mod.cjs.map +1 -1
  108. package/dist/tsup/workflow/mod.d.cts +5 -5
  109. package/dist/tsup/workflow/mod.d.ts +5 -5
  110. package/dist/tsup/workflow/mod.js +187 -140
  111. package/dist/tsup/workflow/mod.js.map +1 -1
  112. package/package.json +8 -8
  113. package/src/actor/config.test.ts +14 -0
  114. package/src/actor/config.ts +104 -16
  115. package/src/actor/definition.ts +18 -7
  116. package/src/agent-os/actor/index.ts +2 -2
  117. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
  118. package/src/common/bare/generated/inspector/v6.ts +1367 -0
  119. package/src/common/database/config.ts +24 -0
  120. package/src/common/database/mod.test.ts +148 -59
  121. package/src/common/database/mod.ts +93 -23
  122. package/src/common/database/native-database.test.ts +9 -0
  123. package/src/common/database/native-database.ts +151 -0
  124. package/src/common/database/shared.ts +15 -0
  125. package/src/common/encoding.ts +28 -21
  126. package/src/db/drizzle.test.ts +157 -0
  127. package/src/db/drizzle.ts +170 -79
  128. package/src/dynamic/isolate-runtime.ts +0 -2
  129. package/src/engine-client/driver.ts +0 -15
  130. package/src/engine-client/mod.ts +0 -41
  131. package/src/inspector/client.browser.ts +69 -8
  132. package/src/inspector-tab/mod.ts +1 -0
  133. package/src/registry/config/index.ts +1 -35
  134. package/src/registry/napi-runtime.ts +204 -16
  135. package/src/registry/native.ts +200 -30
  136. package/src/registry/runtime.ts +146 -3
  137. package/src/registry/wasm-runtime.ts +217 -6
  138. package/src/utils.ts +3 -0
  139. package/src/workflow/context.ts +5 -27
  140. package/src/workflow/driver.test.ts +62 -0
  141. package/src/workflow/driver.ts +231 -113
  142. package/src/workflow/mod.ts +3 -3
  143. package/dist/tsup/chunk-34P567PO.cjs.map +0 -1
  144. package/dist/tsup/chunk-3MHDOUD7.cjs.map +0 -1
  145. package/dist/tsup/chunk-APW73HCH.cjs +0 -382
  146. package/dist/tsup/chunk-APW73HCH.cjs.map +0 -1
  147. package/dist/tsup/chunk-BLDO2FKO.js.map +0 -1
  148. package/dist/tsup/chunk-DHXWD4DF.js +0 -382
  149. package/dist/tsup/chunk-DHXWD4DF.js.map +0 -1
  150. package/dist/tsup/chunk-JZ7TWV65.js.map +0 -1
  151. package/dist/tsup/chunk-PYO2U53V.cjs.map +0 -1
  152. package/dist/tsup/chunk-TYLP3U4Y.cjs.map +0 -1
  153. package/dist/tsup/chunk-VE2X4KMG.cjs.map +0 -1
  154. package/dist/tsup/chunk-XIX5DOZN.js.map +0 -1
  155. package/dist/tsup/chunk-ZA7FLHKH.cjs.map +0 -1
  156. package/src/actor/driver.ts +0 -141
  157. package/src/actor/instance/mod.ts +0 -2484
  158. package/src/drivers/engine/actor-driver.ts +0 -2473
  159. /package/dist/tsup/{chunk-KEKWJECX.js.map → chunk-MGTSVSUC.js.map} +0 -0
  160. /package/dist/tsup/{chunk-DFA7SYSW.js.map → chunk-NIC3QLKL.js.map} +0 -0
  161. /package/dist/tsup/{chunk-NT2NK3VT.js.map → chunk-PAG6G46L.js.map} +0 -0
@@ -1,20 +1,18 @@
1
1
  import {
2
2
  createWorkflowInspectorAdapter
3
- } from "../chunk-BZM4PMFS.js";
4
- import "../chunk-KEKWJECX.js";
3
+ } from "../chunk-P4OGQ4JL.js";
4
+ import "../chunk-MGTSVSUC.js";
5
5
  import {
6
6
  ACTOR_CONTEXT_INTERNAL_SYMBOL,
7
- RAW_STATE_SYMBOL,
8
7
  RUN_FUNCTION_CONFIG_SYMBOL
9
- } from "../chunk-XIX5DOZN.js";
8
+ } from "../chunk-SHBE77IT.js";
10
9
  import {
11
10
  makeWorkflowKey,
12
11
  workflowStoragePrefix
13
- } from "../chunk-HXUEHHJF.js";
14
- import "../chunk-DHXWD4DF.js";
12
+ } from "../chunk-JI6GZ2C2.js";
15
13
  import {
16
14
  stringifyError
17
- } from "../chunk-BLDO2FKO.js";
15
+ } from "../chunk-B4IZU537.js";
18
16
  import {
19
17
  RivetError,
20
18
  isActorAbortedError
@@ -289,32 +287,13 @@ var WorkflowContext = class _WorkflowContext {
289
287
  isEvicted() {
290
288
  return this.#inner.isEvicted();
291
289
  }
292
- // Runs a user step body inside a fresh step context, snapshotting actor
293
- // state/vars so a thrown step rolls back its mutations, and deactivating the
294
- // step context once the body settles so it cannot be used after the step.
290
+ // Runs a user step body inside a fresh step context and deactivates the step
291
+ // context once the body settles. Workflows are roll-forward only, so state and
292
+ // vars mutations made before a failure always remain visible.
295
293
  async #runStep(run) {
296
294
  const stepCtx = new WorkflowStepContext(this.#runCtx, () => this.#markGuardTriggered());
297
- let stateSnapshot = null;
298
- try {
299
- stateSnapshot = { state: this.#runCtx[RAW_STATE_SYMBOL]() };
300
- } catch (error) {
301
- this.#runCtx.log.debug({
302
- msg: "failed to get state, likely due to being stateless workflow",
303
- error
304
- });
305
- }
306
- if (stateSnapshot) {
307
- stateSnapshot.state = structuredClone(stateSnapshot.state);
308
- }
309
- const varsSnapshot = structuredClone(this.#runCtx.vars);
310
295
  try {
311
296
  return await run(stepCtx);
312
- } catch (error) {
313
- if (stateSnapshot) {
314
- this.#runCtx.state = stateSnapshot.state;
315
- }
316
- this.#runCtx.vars = varsSnapshot;
317
- throw error;
318
297
  } finally {
319
298
  stepCtx[DEACTIVATE_STEP]();
320
299
  }
@@ -334,8 +313,7 @@ var WorkflowContext = class _WorkflowContext {
334
313
  ...message.complete ? { complete: message.complete } : {}
335
314
  };
336
315
  }
337
- // Runs a step rollback compensation with an active step context. Rollbacks
338
- // intentionally mutate actor state, so their writes are not snapshotted.
316
+ // Runs a step rollback compensation with an active step context.
339
317
  async #runRollback(rollback, output) {
340
318
  const stepCtx = new WorkflowStepContext(this.#runCtx, () => this.#markGuardTriggered());
341
319
  try {
@@ -377,6 +355,8 @@ var WorkflowContext = class _WorkflowContext {
377
355
 
378
356
  // src/workflow/driver.ts
379
357
  var WORKFLOW_STORAGE_PREFIX = workflowStoragePrefix();
358
+ var WORKFLOW_SQLITE_MAX_BATCH_ROWS = 128;
359
+ var WORKFLOW_SQLITE_MAX_BATCH_BYTES = 512 * 1024;
380
360
  function stripWorkflowKey(prefixed) {
381
361
  return prefixed.slice(WORKFLOW_STORAGE_PREFIX.length);
382
362
  }
@@ -390,6 +370,159 @@ function computeUpperBound(prefix) {
390
370
  }
391
371
  return null;
392
372
  }
373
+ function normalizeSqlBlob(value) {
374
+ if (value instanceof Uint8Array) {
375
+ return value;
376
+ }
377
+ if (value instanceof ArrayBuffer) {
378
+ return new Uint8Array(value);
379
+ }
380
+ if (ArrayBuffer.isView(value)) {
381
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
382
+ }
383
+ if (Array.isArray(value)) {
384
+ const bytes = new Uint8Array(value.length);
385
+ for (const [i, item] of value.entries()) {
386
+ if (!Number.isInteger(item) || item < 0 || item > 255) {
387
+ throw new Error("workflow sqlite value was not a byte array");
388
+ }
389
+ bytes[i] = item;
390
+ }
391
+ return bytes;
392
+ }
393
+ throw new Error("workflow sqlite value was not a blob");
394
+ }
395
+ function runtimeSqlFromContext(runCtx) {
396
+ const sql = runCtx == null ? void 0 : runCtx.sql;
397
+ if (sql && typeof sql === "object" && "query" in sql && "execute" in sql && "executeBatch" in sql && "run" in sql) {
398
+ return sql;
399
+ }
400
+ return void 0;
401
+ }
402
+ var WorkflowStorage = class {
403
+ #sql;
404
+ constructor(sql) {
405
+ if (!sql) {
406
+ throw new Error(
407
+ "workflow storage requires embedded SQLite; actor KV workflow storage is no longer supported"
408
+ );
409
+ }
410
+ if (!sql.executeBatch) {
411
+ throw new Error(
412
+ "workflow storage requires a SQLite database with executeBatch support"
413
+ );
414
+ }
415
+ this.#sql = sql;
416
+ }
417
+ async get(key) {
418
+ var _a;
419
+ const prefixed = makeWorkflowKey(key);
420
+ const result = await this.#sql.query(
421
+ "SELECT value FROM _rivet_wf_kv WHERE key = ?",
422
+ [prefixed]
423
+ );
424
+ return ((_a = result.rows[0]) == null ? void 0 : _a[0]) == null ? null : normalizeSqlBlob(result.rows[0][0]);
425
+ }
426
+ async set(key, value) {
427
+ await this.batch([{ key, value }]);
428
+ }
429
+ async delete(key) {
430
+ const prefixed = makeWorkflowKey(key);
431
+ await this.#sql.run("DELETE FROM _rivet_wf_kv WHERE key = ?", [
432
+ prefixed
433
+ ]);
434
+ }
435
+ async deletePrefix(prefix) {
436
+ const start = makeWorkflowKey(prefix);
437
+ const end = computeUpperBound(start);
438
+ if (end) {
439
+ await this.#sql.run(
440
+ "DELETE FROM _rivet_wf_kv WHERE key >= ? AND key < ?",
441
+ [start, end]
442
+ );
443
+ return;
444
+ }
445
+ const entries = await this.listRaw(start);
446
+ await this.deleteRawKeys(
447
+ entries.filter(([key]) => keyStartsWith(key, start)).map(([key]) => key)
448
+ );
449
+ }
450
+ async deleteRange(start, end) {
451
+ const prefixedStart = makeWorkflowKey(start);
452
+ const prefixedEnd = makeWorkflowKey(end);
453
+ await this.#sql.run(
454
+ "DELETE FROM _rivet_wf_kv WHERE key >= ? AND key < ?",
455
+ [prefixedStart, prefixedEnd]
456
+ );
457
+ }
458
+ async list(prefix) {
459
+ const prefixed = makeWorkflowKey(prefix);
460
+ const entries = await this.listRaw(prefixed);
461
+ return entries.filter(([key]) => keyStartsWith(key, prefixed)).map(([key, value]) => ({
462
+ key: stripWorkflowKey(key),
463
+ value
464
+ }));
465
+ }
466
+ async batch(writes) {
467
+ if (writes.length === 0) return;
468
+ for (const chunk of chunkWorkflowWrites(writes)) {
469
+ await this.#sql.executeBatch(
470
+ chunk.map(({ key, value }) => ({
471
+ sql: "INSERT INTO _rivet_wf_kv (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value",
472
+ params: [makeWorkflowKey(key), value]
473
+ }))
474
+ );
475
+ }
476
+ }
477
+ async listRaw(prefix) {
478
+ const end = computeUpperBound(prefix);
479
+ const result = end ? await this.#sql.query(
480
+ "SELECT key, value FROM _rivet_wf_kv WHERE key >= ? AND key < ? ORDER BY key ASC",
481
+ [prefix, end]
482
+ ) : await this.#sql.query(
483
+ "SELECT key, value FROM _rivet_wf_kv WHERE key >= ? ORDER BY key ASC",
484
+ [prefix]
485
+ );
486
+ return result.rows.map((row) => [
487
+ normalizeSqlBlob(row[0]),
488
+ normalizeSqlBlob(row[1])
489
+ ]);
490
+ }
491
+ async deleteRawKeys(keys) {
492
+ for (let start = 0; start < keys.length; start += WORKFLOW_SQLITE_MAX_BATCH_ROWS) {
493
+ await this.#sql.executeBatch(
494
+ keys.slice(start, start + WORKFLOW_SQLITE_MAX_BATCH_ROWS).map((key) => ({
495
+ sql: "DELETE FROM _rivet_wf_kv WHERE key = ?",
496
+ params: [key]
497
+ }))
498
+ );
499
+ }
500
+ }
501
+ };
502
+ function chunkWorkflowWrites(writes) {
503
+ const chunks = [];
504
+ let chunk = [];
505
+ let chunkBytes = 0;
506
+ for (const write of writes) {
507
+ const writeBytes = WORKFLOW_STORAGE_PREFIX.length + write.key.length + write.value.length;
508
+ if (chunk.length > 0 && (chunk.length >= WORKFLOW_SQLITE_MAX_BATCH_ROWS || chunkBytes + writeBytes > WORKFLOW_SQLITE_MAX_BATCH_BYTES)) {
509
+ chunks.push(chunk);
510
+ chunk = [];
511
+ chunkBytes = 0;
512
+ }
513
+ chunk.push(write);
514
+ chunkBytes += writeBytes;
515
+ }
516
+ if (chunk.length > 0) chunks.push(chunk);
517
+ return chunks;
518
+ }
519
+ function keyStartsWith(key, prefix) {
520
+ if (key.length < prefix.length) return false;
521
+ for (let i = 0; i < prefix.length; i++) {
522
+ if (key[i] !== prefix[i]) return false;
523
+ }
524
+ return true;
525
+ }
393
526
  var ActorWorkflowMessageDriver = class {
394
527
  #actor;
395
528
  #runCtx;
@@ -442,95 +575,44 @@ var ActorWorkflowMessageDriver = class {
442
575
  }
443
576
  };
444
577
  var ActorWorkflowDriver = class {
578
+ atomicBatch = true;
445
579
  workerPollInterval = 100;
446
580
  messageDriver;
447
581
  #actor;
448
582
  #runCtx;
583
+ #storage;
449
584
  constructor(actor, runCtx) {
450
585
  this.#actor = actor;
451
586
  this.#runCtx = runCtx;
452
587
  this.messageDriver = new ActorWorkflowMessageDriver(actor, runCtx);
588
+ this.#storage = new WorkflowStorage(runtimeSqlFromContext(runCtx));
453
589
  }
454
590
  async get(key) {
455
- const [value] = await this.#runCtx.internalKeepAwake(
456
- this.#actor.driver.kvBatchGet(this.#actor.id, [
457
- makeWorkflowKey(key)
458
- ])
459
- );
460
- return value ?? null;
591
+ return await this.#runCtx.internalKeepAwake(this.#storage.get(key));
461
592
  }
462
593
  async set(key, value) {
463
- await this.#runCtx.internalKeepAwake(
464
- this.#actor.driver.kvBatchPut(this.#actor.id, [
465
- [makeWorkflowKey(key), value]
466
- ])
467
- );
594
+ await this.#runCtx.internalKeepAwake(this.#storage.set(key, value));
468
595
  }
469
596
  async delete(key) {
470
- await this.#runCtx.internalKeepAwake(
471
- this.#actor.driver.kvBatchDelete(this.#actor.id, [
472
- makeWorkflowKey(key)
473
- ])
474
- );
597
+ await this.#runCtx.internalKeepAwake(this.#storage.delete(key));
475
598
  }
476
599
  async deletePrefix(prefix) {
477
- const start = makeWorkflowKey(prefix);
478
- const end = computeUpperBound(start);
479
- if (end) {
480
- await this.#runCtx.internalKeepAwake(
481
- this.#actor.driver.kvDeleteRange(this.#actor.id, start, end)
482
- );
483
- } else {
484
- const entries = await this.#runCtx.internalKeepAwake(
485
- this.#actor.driver.kvListPrefix(this.#actor.id, start)
486
- );
487
- if (entries.length === 0) {
488
- return;
489
- }
490
- await this.#runCtx.internalKeepAwake(
491
- this.#actor.driver.kvBatchDelete(
492
- this.#actor.id,
493
- entries.map(([key]) => key)
494
- )
495
- );
496
- }
600
+ await this.#runCtx.internalKeepAwake(
601
+ this.#storage.deletePrefix(prefix)
602
+ );
497
603
  }
498
604
  async deleteRange(start, end) {
499
605
  await this.#runCtx.internalKeepAwake(
500
- this.#actor.driver.kvDeleteRange(
501
- this.#actor.id,
502
- makeWorkflowKey(start),
503
- makeWorkflowKey(end)
504
- )
606
+ this.#storage.deleteRange(start, end)
505
607
  );
506
608
  }
507
609
  async list(prefix) {
508
- const entries = await this.#runCtx.internalKeepAwake(
509
- this.#actor.driver.kvListPrefix(
510
- this.#actor.id,
511
- makeWorkflowKey(prefix)
512
- )
513
- );
514
- return entries.map(([key, value]) => ({
515
- key: stripWorkflowKey(key),
516
- value
517
- }));
610
+ return await this.#runCtx.internalKeepAwake(this.#storage.list(prefix));
518
611
  }
519
612
  async batch(writes) {
520
613
  if (writes.length === 0) return;
521
614
  await this.#runCtx.internalKeepAwake(
522
- Promise.all([
523
- this.#actor.driver.kvBatchPut(
524
- this.#actor.id,
525
- writes.map(({ key, value }) => [
526
- makeWorkflowKey(key),
527
- value
528
- ])
529
- ),
530
- this.#actor.stateManager.saveState({
531
- immediate: true
532
- })
533
- ])
615
+ this.#actor.stateManager.saveStateAndWorkflowBatch(writes)
534
616
  );
535
617
  }
536
618
  async setAlarm(_workflowId, wakeAt) {
@@ -563,69 +645,34 @@ var ActorWorkflowControlDriver = class {
563
645
  workerPollInterval = 100;
564
646
  messageDriver = new NoopWorkflowMessageDriver();
565
647
  #actor;
566
- constructor(actor) {
648
+ #storage;
649
+ constructor(actor, runCtx) {
567
650
  this.#actor = actor;
651
+ this.#storage = new WorkflowStorage(runtimeSqlFromContext(runCtx));
568
652
  }
569
653
  async get(key) {
570
- const [value] = await this.#actor.driver.kvBatchGet(this.#actor.id, [
571
- makeWorkflowKey(key)
572
- ]);
573
- return value ?? null;
654
+ return await this.#storage.get(key);
574
655
  }
575
656
  async set(key, value) {
576
- await this.#actor.driver.kvBatchPut(this.#actor.id, [
577
- [makeWorkflowKey(key), value]
578
- ]);
657
+ await this.#storage.set(key, value);
579
658
  }
580
659
  async delete(key) {
581
- await this.#actor.driver.kvBatchDelete(this.#actor.id, [
582
- makeWorkflowKey(key)
583
- ]);
660
+ await this.#storage.delete(key);
584
661
  }
585
662
  async deletePrefix(prefix) {
586
- const start = makeWorkflowKey(prefix);
587
- const end = computeUpperBound(start);
588
- if (end) {
589
- await this.#actor.driver.kvDeleteRange(this.#actor.id, start, end);
590
- return;
591
- }
592
- const entries = await this.#actor.driver.kvListPrefix(
593
- this.#actor.id,
594
- start
595
- );
596
- if (entries.length === 0) {
597
- return;
598
- }
599
- await this.#actor.driver.kvBatchDelete(
600
- this.#actor.id,
601
- entries.map(([key]) => key)
602
- );
663
+ await this.#storage.deletePrefix(prefix);
603
664
  }
604
665
  async deleteRange(start, end) {
605
- await this.#actor.driver.kvDeleteRange(
606
- this.#actor.id,
607
- makeWorkflowKey(start),
608
- makeWorkflowKey(end)
609
- );
666
+ await this.#storage.deleteRange(start, end);
610
667
  }
611
668
  async list(prefix) {
612
- const entries = await this.#actor.driver.kvListPrefix(
613
- this.#actor.id,
614
- makeWorkflowKey(prefix)
615
- );
616
- return entries.map(([key, value]) => ({
617
- key: stripWorkflowKey(key),
618
- value
619
- }));
669
+ return await this.#storage.list(prefix);
620
670
  }
621
671
  async batch(writes) {
622
672
  if (writes.length === 0) {
623
673
  return;
624
674
  }
625
- await this.#actor.driver.kvBatchPut(
626
- this.#actor.id,
627
- writes.map(({ key, value }) => [makeWorkflowKey(key), value])
628
- );
675
+ await this.#storage.batch(writes);
629
676
  }
630
677
  async setAlarm(_workflowId, wakeAt) {
631
678
  await this.#actor.driver.setAlarm(this.#actor, wakeAt);
@@ -679,7 +726,7 @@ function workflow(fn, options = {}) {
679
726
  invariant(actor, "workflow() requires an actor instance");
680
727
  const workflowInspector = getWorkflowInspector(actor.id);
681
728
  const driver = new ActorWorkflowDriver(actor, runCtx);
682
- const controlDriver = new ActorWorkflowControlDriver(actor);
729
+ const controlDriver = new ActorWorkflowControlDriver(actor, runCtx);
683
730
  workflowInspector.setReplayFromStep(async (entryId) => {
684
731
  const workflowState = await workflowInspector.adapter.getState();
685
732
  if (actor.isRunHandlerActive() || workflowState === "pending" || workflowState === "running") {