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,148 +1,5 @@
1
- import { Context, Handler } from 'hono';
1
+ import 'hono';
2
2
  export { d as assertUnreachable, s as stringifyError } from './utils-CqDnC_PS.js';
3
+ export { D as DeepMutable, a as DeepReadonly, b as EXTRA_ERROR_LOG, G as GetUpgradeWebSocket, L as LongTimeoutHandle, R as Runtime, S as SinglePromiseQueue, U as UpgradeWebSocket, V as VERSION, c as arrayBuffersEqual, d as bufferToArrayBuffer, e as combineUrlPath, f as dbg, g as detectRuntime, h as getEnvUniversal, i as httpUserAgent, j as interval, k as joinSignals, l as jsonParseCompat, m as jsonStringifyCompat, p as promiseWithResolvers, s as setLongTimeout, n as sleep, t as toUint8Array } from './utils-CV0akuDD.js';
3
4
  import 'hono/utils/http-status';
4
-
5
- /**
6
- * Joins multiple abort signals into one.
7
- *
8
- * The returned signal aborts when the first input signal aborts.
9
- * Uses `AbortSignal.any(...)` when available, with a runtime fallback.
10
- *
11
- * @experimental
12
- */
13
- declare function joinSignals(...signals: Array<AbortSignal | undefined | null>): AbortSignal;
14
- /**
15
- * Returns a promise that resolves after the given number of milliseconds.
16
- *
17
- * @experimental
18
- */
19
- declare function sleep(ms: number): Promise<void>;
20
- /**
21
- * Creates a fixed-rate interval tick function that can be awaited in a loop.
22
- *
23
- * @example
24
- * const tick = interval(100);
25
- * while (!c.aborted) {
26
- * await tick();
27
- * if (c.aborted) break;
28
- * // ... game logic
29
- * }
30
- *
31
- * @experimental
32
- */
33
- declare function interval(ms: number): () => Promise<void>;
34
- declare const VERSION: string;
35
- /**
36
- * Builds the HTTP user agent used by this library.
37
- *
38
- * @experimental
39
- */
40
- declare function httpUserAgent(): string;
41
- type UpgradeWebSocket = (createEvents: (c: Context) => any) => Handler;
42
- type GetUpgradeWebSocket = () => UpgradeWebSocket;
43
- /**
44
- * Reads an environment variable from Deno or Node runtimes.
45
- *
46
- * @experimental
47
- */
48
- declare function getEnvUniversal(key: string): string | undefined;
49
- /**
50
- * Traces a debug value and returns it.
51
- *
52
- * @experimental
53
- */
54
- declare function dbg<T>(x: T): T;
55
- /**
56
- * Converts various ArrayBuffer-like types to Uint8Array.
57
- * Handles ArrayBuffer, ArrayBufferView (including typed arrays), and passes through existing Uint8Array.
58
- *
59
- * @param data - The ArrayBuffer or ArrayBufferView to convert
60
- * @returns A Uint8Array view of the data
61
- *
62
- * @experimental
63
- */
64
- declare function toUint8Array(data: ArrayBuffer | ArrayBufferView): Uint8Array;
65
- type LongTimeoutHandle = {
66
- abort: () => void;
67
- };
68
- /**
69
- * Polyfill for Promise.withResolvers().
70
- *
71
- * This is specifically for Cloudflare Workers. Their implementation of Promise.withResolvers does not work correctly.
72
- *
73
- * @experimental
74
- */
75
- declare function promiseWithResolvers<T>(onReject: (reason?: any) => void): {
76
- promise: Promise<T>;
77
- resolve: (value: T | PromiseLike<T>) => void;
78
- reject: (reason?: any) => void;
79
- };
80
- /**
81
- * Sets a timeout that supports delays larger than the JavaScript timer limit.
82
- *
83
- * @experimental
84
- */
85
- declare function setLongTimeout(listener: () => void, after: number): LongTimeoutHandle;
86
- /**
87
- * A tiny utility that coalesces/enqueues async operations so only the latest
88
- * queued task runs per cycle, while callers receive a promise that resolves
89
- * when the task for the cycle they joined has completed.
90
- */
91
- declare class SinglePromiseQueue {
92
- #private;
93
- /** The currently running promise of #drainLoop. Do not await this, instead await `pending` to await the current cycle. */
94
- runningDrainLoop?: Promise<void>;
95
- /** Queue the next operation and return a promise that resolves when it flushes. */
96
- enqueue(op: () => Promise<void>): Promise<void>;
97
- }
98
- /**
99
- * Converts a Buffer or Uint8Array into an ArrayBuffer view.
100
- *
101
- * @experimental
102
- */
103
- declare function bufferToArrayBuffer(buf: Buffer | Uint8Array): ArrayBuffer;
104
- /**
105
- * Properly combines a base URL endpoint with a path, preserving any base path in the endpoint.
106
- *
107
- * @example
108
- * combineUrlPath("http://localhost:8787/rivet", "/actors/action")
109
- * // Returns: "http://localhost:8787/rivet/actors/action"
110
- *
111
- * @example
112
- * combineUrlPath("http://localhost:8787/rivet", "/actors?type=foo", { namespace: "test" })
113
- * // Returns: "http://localhost:8787/rivet/actors?type=foo&namespace=test"
114
- *
115
- * @param endpoint The base URL endpoint that may contain a path component
116
- * @param path The path to append to the endpoint (may include query parameters)
117
- * @param queryParams Optional additional query parameters to append
118
- * @returns The properly combined URL string
119
- *
120
- * @experimental
121
- */
122
- declare function combineUrlPath(endpoint: string, path: string, queryParams?: Record<string, string | undefined>): string;
123
- /**
124
- * Compares two ArrayBuffer values by byte content.
125
- *
126
- * @experimental
127
- */
128
- declare function arrayBuffersEqual(buf1: ArrayBuffer, buf2: ArrayBuffer): boolean;
129
- declare const EXTRA_ERROR_LOG: {
130
- issues: string;
131
- support: string;
132
- version: string;
133
- };
134
- type Runtime = "deno" | "bun" | "node";
135
- /**
136
- * Detects the current JavaScript runtime from the user agent.
137
- *
138
- * @experimental
139
- */
140
- declare function detectRuntime(): Runtime;
141
- type DeepReadonly<T> = {
142
- readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K];
143
- };
144
- type DeepMutable<T> = {
145
- -readonly [K in keyof T]: T[K] extends object ? DeepMutable<T[K]> : T[K];
146
- };
147
-
148
- export { type DeepMutable, type DeepReadonly, EXTRA_ERROR_LOG, type GetUpgradeWebSocket, type LongTimeoutHandle, type Runtime, SinglePromiseQueue, type UpgradeWebSocket, VERSION, arrayBuffersEqual, bufferToArrayBuffer, combineUrlPath, dbg, detectRuntime, getEnvUniversal, httpUserAgent, interval, joinSignals, promiseWithResolvers, setLongTimeout, sleep, toUint8Array };
5
+ import 'zod/v4';
@@ -12,12 +12,14 @@ import {
12
12
  httpUserAgent,
13
13
  interval,
14
14
  joinSignals,
15
+ jsonParseCompat,
16
+ jsonStringifyCompat,
15
17
  promiseWithResolvers,
16
18
  setLongTimeout,
17
19
  sleep,
18
20
  stringifyError,
19
21
  toUint8Array
20
- } from "./chunk-BLDO2FKO.js";
22
+ } from "./chunk-B4IZU537.js";
21
23
  import "./chunk-ZZ3WBRPD.js";
22
24
  export {
23
25
  EXTRA_ERROR_LOG,
@@ -33,6 +35,8 @@ export {
33
35
  httpUserAgent,
34
36
  interval,
35
37
  joinSignals,
38
+ jsonParseCompat,
39
+ jsonStringifyCompat,
36
40
  promiseWithResolvers,
37
41
  setLongTimeout,
38
42
  sleep,
@@ -1,20 +1,18 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } var _class; var _class2;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } var _class; var _class2;
2
2
 
3
- var _chunkTYLP3U4Ycjs = require('../chunk-TYLP3U4Y.cjs');
4
- require('../chunk-S644FL73.cjs');
3
+ var _chunkTNIWIUNVcjs = require('../chunk-TNIWIUNV.cjs');
4
+ require('../chunk-TR5NLR5V.cjs');
5
5
 
6
6
 
7
7
 
8
+ var _chunkG3CCRCTKcjs = require('../chunk-G3CCRCTK.cjs');
8
9
 
9
- var _chunk3MHDOUD7cjs = require('../chunk-3MHDOUD7.cjs');
10
10
 
11
11
 
12
+ var _chunkMYWZCDMLcjs = require('../chunk-MYWZCDML.cjs');
12
13
 
13
- var _chunkVE2X4KMGcjs = require('../chunk-VE2X4KMG.cjs');
14
- require('../chunk-APW73HCH.cjs');
15
14
 
16
-
17
- var _chunkPYO2U53Vcjs = require('../chunk-PYO2U53V.cjs');
15
+ var _chunk6HQCLWAWcjs = require('../chunk-6HQCLWAW.cjs');
18
16
 
19
17
 
20
18
 
@@ -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[_chunk3MHDOUD7cjs.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 {
@@ -376,7 +354,9 @@ var WorkflowContext = class _WorkflowContext {
376
354
  };
377
355
 
378
356
  // src/workflow/driver.ts
379
- var WORKFLOW_STORAGE_PREFIX = _chunkVE2X4KMGcjs.workflowStoragePrefix.call(void 0, );
357
+ var WORKFLOW_STORAGE_PREFIX = _chunkMYWZCDMLcjs.workflowStoragePrefix.call(void 0, );
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 = _chunkMYWZCDMLcjs.makeWorkflowKey.call(void 0, 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 = _chunkMYWZCDMLcjs.makeWorkflowKey.call(void 0, key);
431
+ await this.#sql.run("DELETE FROM _rivet_wf_kv WHERE key = ?", [
432
+ prefixed
433
+ ]);
434
+ }
435
+ async deletePrefix(prefix) {
436
+ const start = _chunkMYWZCDMLcjs.makeWorkflowKey.call(void 0, 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 = _chunkMYWZCDMLcjs.makeWorkflowKey.call(void 0, start);
452
+ const prefixedEnd = _chunkMYWZCDMLcjs.makeWorkflowKey.call(void 0, 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 = _chunkMYWZCDMLcjs.makeWorkflowKey.call(void 0, 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: [_chunkMYWZCDMLcjs.makeWorkflowKey.call(void 0, 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 = class {
445
- __init() {this.workerPollInterval = 100}
578
+ __init() {this.atomicBatch = true}
579
+ __init2() {this.workerPollInterval = 100}
446
580
 
447
581
  #actor;
448
582
  #runCtx;
449
- constructor(actor, runCtx) {;_class.prototype.__init.call(this);
583
+ #storage;
584
+ constructor(actor, runCtx) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);
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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, key)
458
- ])
459
- );
460
- return _nullishCoalesce(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
- [_chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, key)
473
- ])
474
- );
597
+ await this.#runCtx.internalKeepAwake(this.#storage.delete(key));
475
598
  }
476
599
  async deletePrefix(prefix) {
477
- const start = _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, start),
503
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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) {
@@ -560,72 +642,37 @@ var NoopWorkflowMessageDriver = class {
560
642
  }
561
643
  };
562
644
  var ActorWorkflowControlDriver = (_class2 = class {
563
- __init2() {this.workerPollInterval = 100}
564
- __init3() {this.messageDriver = new NoopWorkflowMessageDriver()}
645
+ __init3() {this.workerPollInterval = 100}
646
+ __init4() {this.messageDriver = new NoopWorkflowMessageDriver()}
565
647
  #actor;
566
- constructor(actor) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);
648
+ #storage;
649
+ constructor(actor, runCtx) {;_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);
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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, key)
572
- ]);
573
- return _nullishCoalesce(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
- [_chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, key)
583
- ]);
660
+ await this.#storage.delete(key);
584
661
  }
585
662
  async deletePrefix(prefix) {
586
- const start = _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, start),
608
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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
- _chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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 }) => [_chunkVE2X4KMGcjs.makeWorkflowKey.call(void 0, 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);
@@ -669,17 +716,17 @@ function workflow(fn, options = {}) {
669
716
  function getWorkflowInspector(actorId) {
670
717
  let workflowInspector = workflowInspectors.get(actorId);
671
718
  if (!workflowInspector) {
672
- workflowInspector = _chunkTYLP3U4Ycjs.createWorkflowInspectorAdapter.call(void 0, );
719
+ workflowInspector = _chunkTNIWIUNVcjs.createWorkflowInspectorAdapter.call(void 0, );
673
720
  workflowInspectors.set(actorId, workflowInspector);
674
721
  }
675
722
  return workflowInspector;
676
723
  }
677
724
  async function run(runCtx) {
678
- const actor = runCtx[_chunk3MHDOUD7cjs.ACTOR_CONTEXT_INTERNAL_SYMBOL];
725
+ const actor = runCtx[_chunkG3CCRCTKcjs.ACTOR_CONTEXT_INTERNAL_SYMBOL];
679
726
  _invariant2.default.call(void 0, 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") {
@@ -737,20 +784,20 @@ function workflow(fn, options = {}) {
737
784
  if (shouldRethrowWorkflowError(error)) {
738
785
  runCtx.log.error({
739
786
  msg: "workflow run failed",
740
- error: _chunkPYO2U53Vcjs.stringifyError.call(void 0, error)
787
+ error: _chunk6HQCLWAWcjs.stringifyError.call(void 0, error)
741
788
  });
742
789
  throw error;
743
790
  }
744
791
  runCtx.log.warn({
745
792
  msg: "workflow failed and will sleep until woken",
746
- error: _chunkPYO2U53Vcjs.stringifyError.call(void 0, error)
793
+ error: _chunk6HQCLWAWcjs.stringifyError.call(void 0, error)
747
794
  });
748
795
  } finally {
749
796
  runCtx.abortSignal.removeEventListener("abort", onAbort);
750
797
  }
751
798
  }
752
799
  const runWithConfig = run;
753
- runWithConfig[_chunk3MHDOUD7cjs.RUN_FUNCTION_CONFIG_SYMBOL] = {
800
+ runWithConfig[_chunkG3CCRCTKcjs.RUN_FUNCTION_CONFIG_SYMBOL] = {
754
801
  icon: "diagram-project",
755
802
  // Drop the per-actor inspector when the actor is destroyed so this map
756
803
  // does not retain one inspector (and its encoded history) per actor id