rivetkit 2.3.0-rc.8 → 2.3.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 (221) hide show
  1. package/dist/browser/client.d.ts +481 -74
  2. package/dist/browser/client.js +174 -148
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +47 -18
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/actor/errors.cjs +2 -2
  7. package/dist/tsup/actor/errors.d.cts +1 -1
  8. package/dist/tsup/actor/errors.d.ts +1 -1
  9. package/dist/tsup/actor/errors.js +1 -1
  10. package/dist/tsup/agent-os/index.cjs +2160 -2086
  11. package/dist/tsup/agent-os/index.cjs.map +1 -1
  12. package/dist/tsup/agent-os/index.d.cts +479 -73
  13. package/dist/tsup/agent-os/index.d.ts +479 -73
  14. package/dist/tsup/agent-os/index.js +2160 -2086
  15. package/dist/tsup/agent-os/index.js.map +1 -1
  16. package/dist/tsup/{chunk-KY3CERZR.js → chunk-2OTRTA3J.js} +7 -21
  17. package/dist/tsup/chunk-2OTRTA3J.js.map +1 -0
  18. package/dist/tsup/{chunk-HGW6PBWR.cjs → chunk-3677IIOV.cjs} +11 -25
  19. package/dist/tsup/chunk-3677IIOV.cjs.map +1 -0
  20. package/dist/tsup/{chunk-OT7FF6GB.cjs → chunk-47HHIEXH.cjs} +24 -9
  21. package/dist/tsup/chunk-47HHIEXH.cjs.map +1 -0
  22. package/dist/tsup/{chunk-EMFKMVJR.js → chunk-4JDSFJS5.js} +69 -58
  23. package/dist/tsup/chunk-4JDSFJS5.js.map +1 -0
  24. package/dist/tsup/{chunk-7HLFSAJP.cjs → chunk-7QKCIVAY.cjs} +225 -214
  25. package/dist/tsup/chunk-7QKCIVAY.cjs.map +1 -0
  26. package/dist/tsup/{chunk-AWTPTUQ7.cjs → chunk-B6VUNZUD.cjs} +10 -10
  27. package/dist/tsup/{chunk-AWTPTUQ7.cjs.map → chunk-B6VUNZUD.cjs.map} +1 -1
  28. package/dist/tsup/{chunk-D3T3ZBSY.js → chunk-BEI24WTI.js} +2 -2
  29. package/dist/tsup/{chunk-TMLOKTRB.js → chunk-BRP62GZC.js} +1 -1
  30. package/dist/tsup/chunk-BRP62GZC.js.map +1 -0
  31. package/dist/tsup/{chunk-D5G75T7J.js → chunk-DPIMKYNB.js} +61 -2
  32. package/dist/tsup/chunk-DPIMKYNB.js.map +1 -0
  33. package/dist/tsup/{chunk-BATTOVHF.cjs → chunk-DXXJPH55.cjs} +40 -13
  34. package/dist/tsup/chunk-DXXJPH55.cjs.map +1 -0
  35. package/dist/tsup/{chunk-3YY5S6TV.js → chunk-HXUEHHJF.js} +2 -2
  36. package/dist/tsup/chunk-HXUEHHJF.js.map +1 -0
  37. package/dist/tsup/{chunk-4BPKKZJO.cjs → chunk-I4LN3FNT.cjs} +10 -10
  38. package/dist/tsup/chunk-I4LN3FNT.cjs.map +1 -0
  39. package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JZ7TWV65.js} +1 -1
  40. package/dist/tsup/chunk-JZ7TWV65.js.map +1 -0
  41. package/dist/tsup/{chunk-63WNTDRC.cjs → chunk-KORQB2IR.cjs} +1 -1
  42. package/dist/tsup/{chunk-63WNTDRC.cjs.map → chunk-KORQB2IR.cjs.map} +1 -1
  43. package/dist/tsup/{chunk-6TQSSJ4F.cjs → chunk-LVTBW2RE.cjs} +3 -3
  44. package/dist/tsup/{chunk-6TQSSJ4F.cjs.map → chunk-LVTBW2RE.cjs.map} +1 -1
  45. package/dist/tsup/{chunk-4JU3IPG2.js → chunk-MEHBWPLJ.js} +6 -6
  46. package/dist/tsup/chunk-MEHBWPLJ.js.map +1 -0
  47. package/dist/tsup/{chunk-SRNOPUC6.cjs → chunk-NIY3RSPX.cjs} +62 -3
  48. package/dist/tsup/chunk-NIY3RSPX.cjs.map +1 -0
  49. package/dist/tsup/{chunk-UZXQEGVJ.js → chunk-P2GNQ4RN.js} +4 -4
  50. package/dist/tsup/{chunk-UZXQEGVJ.js.map → chunk-P2GNQ4RN.js.map} +1 -1
  51. package/dist/tsup/{chunk-VUGENVIK.js → chunk-UMZVD6DQ.js} +22 -7
  52. package/dist/tsup/chunk-UMZVD6DQ.js.map +1 -0
  53. package/dist/tsup/{chunk-LD5YASJU.cjs → chunk-VE2X4KMG.cjs} +2 -2
  54. package/dist/tsup/{chunk-LD5YASJU.cjs.map → chunk-VE2X4KMG.cjs.map} +1 -1
  55. package/dist/tsup/{chunk-GBG63SUG.js → chunk-VTTFNQQI.js} +32 -5
  56. package/dist/tsup/chunk-VTTFNQQI.js.map +1 -0
  57. package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-ZA7FLHKH.cjs} +1 -1
  58. package/dist/tsup/chunk-ZA7FLHKH.cjs.map +1 -0
  59. package/dist/tsup/client/mod.cjs +9 -9
  60. package/dist/tsup/client/mod.d.cts +5 -5
  61. package/dist/tsup/client/mod.d.ts +5 -5
  62. package/dist/tsup/client/mod.js +8 -8
  63. package/dist/tsup/common/log.cjs +3 -3
  64. package/dist/tsup/common/log.js +2 -2
  65. package/dist/tsup/common/websocket.cjs +4 -4
  66. package/dist/tsup/common/websocket.js +3 -3
  67. package/dist/tsup/{config-Ak1lv4gF.d.ts → config-BxWAw3iH.d.ts} +512 -27
  68. package/dist/tsup/{config-DU_xj4qZ.d.cts → config-CZQQ-mso.d.cts} +512 -27
  69. package/dist/tsup/{config-CxjGYf4K.d.ts → config-D49x8NpL.d.cts} +1 -2
  70. package/dist/tsup/{config-CxjGYf4K.d.cts → config-D49x8NpL.d.ts} +1 -2
  71. package/dist/tsup/{context-DAAp4Lpg.d.ts → context-Bw7xq8w3.d.cts} +8 -8
  72. package/dist/tsup/{context-Dt_L55q8.d.cts → context-D8QA76sV.d.ts} +8 -8
  73. package/dist/tsup/db/drizzle.cjs +3 -3
  74. package/dist/tsup/db/drizzle.d.cts +1 -1
  75. package/dist/tsup/db/drizzle.d.ts +1 -1
  76. package/dist/tsup/db/drizzle.js +1 -1
  77. package/dist/tsup/db/mod.cjs +2 -2
  78. package/dist/tsup/db/mod.d.cts +2 -2
  79. package/dist/tsup/db/mod.d.ts +2 -2
  80. package/dist/tsup/db/mod.js +1 -1
  81. package/dist/tsup/dynamic/mod.cjs +24 -0
  82. package/dist/tsup/dynamic/mod.cjs.map +1 -0
  83. package/dist/tsup/dynamic/mod.d.cts +37 -0
  84. package/dist/tsup/dynamic/mod.d.ts +37 -0
  85. package/dist/tsup/dynamic/mod.js +24 -0
  86. package/dist/tsup/dynamic/mod.js.map +1 -0
  87. package/dist/tsup/inspector/mod.cjs +6 -6
  88. package/dist/tsup/inspector/mod.js +5 -5
  89. package/dist/tsup/inspector-tab/mod.cjs +173 -0
  90. package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
  91. package/dist/tsup/inspector-tab/mod.d.cts +250 -0
  92. package/dist/tsup/inspector-tab/mod.d.ts +250 -0
  93. package/dist/tsup/inspector-tab/mod.js +173 -0
  94. package/dist/tsup/inspector-tab/mod.js.map +1 -0
  95. package/dist/tsup/mod.cjs +615 -348
  96. package/dist/tsup/mod.cjs.map +1 -1
  97. package/dist/tsup/mod.d.cts +5 -5
  98. package/dist/tsup/mod.d.ts +5 -5
  99. package/dist/tsup/mod.js +511 -244
  100. package/dist/tsup/mod.js.map +1 -1
  101. package/dist/tsup/test/mod.cjs +21 -18
  102. package/dist/tsup/test/mod.cjs.map +1 -1
  103. package/dist/tsup/test/mod.d.cts +4 -4
  104. package/dist/tsup/test/mod.d.ts +4 -4
  105. package/dist/tsup/test/mod.js +18 -15
  106. package/dist/tsup/test/mod.js.map +1 -1
  107. package/dist/tsup/{utils-DVekpm4I.d.cts → utils-DQosb24I.d.cts} +1 -1
  108. package/dist/tsup/{utils-DVekpm4I.d.ts → utils-DQosb24I.d.ts} +1 -1
  109. package/dist/tsup/utils.cjs +3 -3
  110. package/dist/tsup/utils.d.cts +1 -1
  111. package/dist/tsup/utils.d.ts +1 -1
  112. package/dist/tsup/utils.js +2 -2
  113. package/dist/tsup/workflow/mod.cjs +279 -279
  114. package/dist/tsup/workflow/mod.cjs.map +1 -1
  115. package/dist/tsup/workflow/mod.d.cts +6 -6
  116. package/dist/tsup/workflow/mod.d.ts +6 -6
  117. package/dist/tsup/workflow/mod.js +380 -380
  118. package/dist/tsup/workflow/mod.js.map +1 -1
  119. package/package.json +29 -9
  120. package/src/actor/config.ts +156 -51
  121. package/src/actor/contexts/index.ts +7 -2
  122. package/src/actor/definition.ts +17 -19
  123. package/src/actor/driver.ts +3 -3
  124. package/src/actor/errors.ts +8 -2
  125. package/src/actor/instance/mod.ts +26 -34
  126. package/src/actor/keys.ts +1 -1
  127. package/src/actor/mod.ts +22 -20
  128. package/src/actor/schema.ts +2 -2
  129. package/src/agent-os/actor/index.ts +38 -18
  130. package/src/agent-os/actor/preview.ts +1 -2
  131. package/src/agent-os/config.ts +1 -1
  132. package/src/agent-os/fs/database-vfs.ts +1 -1
  133. package/src/agent-os/index.ts +16 -15
  134. package/src/client/actor-common.ts +87 -54
  135. package/src/client/actor-conn.ts +11 -11
  136. package/src/client/actor-handle.ts +69 -52
  137. package/src/client/actor-query.ts +5 -5
  138. package/src/client/errors.ts +1 -1
  139. package/src/client/lifecycle-errors.ts +2 -4
  140. package/src/client/query.ts +1 -1
  141. package/src/client/queue.ts +8 -4
  142. package/src/client/raw-utils.ts +8 -6
  143. package/src/client/resolve-gateway-target.ts +1 -1
  144. package/src/client/utils.ts +2 -6
  145. package/src/common/actor-websocket.ts +3 -1
  146. package/src/common/bare/actor-persist/v1.ts +205 -163
  147. package/src/common/bare/actor-persist/v2.ts +265 -213
  148. package/src/common/bare/actor-persist/v3.ts +176 -172
  149. package/src/common/bare/actor-persist/v4.ts +254 -253
  150. package/src/common/bare/transport/v1.ts +659 -543
  151. package/src/common/client-protocol-versioned.ts +66 -64
  152. package/src/common/database/config.ts +2 -8
  153. package/src/common/database/native-database.ts +1 -1
  154. package/src/common/database/shared.ts +1 -0
  155. package/src/common/encoding.ts +13 -17
  156. package/src/common/engine.ts +28 -1
  157. package/src/common/eventsource.ts +1 -1
  158. package/src/common/inline-websocket-adapter.ts +3 -2
  159. package/src/common/router.ts +13 -17
  160. package/src/common/utils.ts +1 -2
  161. package/src/common/websocket-interface.ts +1 -1
  162. package/src/db/mod.ts +1 -1
  163. package/src/devtools-loader/index.ts +4 -7
  164. package/src/devtools-loader/serve-devtools.ts +26 -0
  165. package/src/drivers/engine/actor-driver.ts +48 -46
  166. package/src/dynamic/instance.ts +32 -0
  167. package/src/dynamic/internal.ts +50 -0
  168. package/src/dynamic/isolate-runtime.ts +66 -0
  169. package/src/dynamic/mod.ts +32 -0
  170. package/src/engine-client/actor-http-client.ts +3 -3
  171. package/src/engine-client/actor-websocket-client.ts +5 -5
  172. package/src/engine-client/api-endpoints.ts +51 -2
  173. package/src/engine-client/api-utils.ts +2 -2
  174. package/src/engine-client/driver.ts +1 -1
  175. package/src/engine-client/mod.ts +5 -3
  176. package/src/engine-client/ws-proxy.ts +9 -4
  177. package/src/inspector/client.browser.ts +5 -11
  178. package/src/inspector/mod.ts +1 -3
  179. package/src/inspector-tab/mod.ts +315 -0
  180. package/src/registry/config/envoy.ts +1 -2
  181. package/src/registry/config/index.ts +40 -16
  182. package/src/registry/index.ts +226 -83
  183. package/src/registry/napi-runtime.ts +46 -12
  184. package/src/registry/native-validation.ts +10 -12
  185. package/src/registry/native.ts +307 -164
  186. package/src/registry/process-metrics.ts +90 -23
  187. package/src/registry/runtime.ts +53 -6
  188. package/src/registry/wasm-runtime.ts +30 -3
  189. package/src/serde.ts +1 -1
  190. package/src/serverless/configure.ts +18 -7
  191. package/src/test/mod.ts +11 -8
  192. package/src/utils/endpoint-parser.ts +1 -1
  193. package/src/utils/env-vars.ts +6 -0
  194. package/src/utils/router.ts +1 -1
  195. package/src/utils/serve.ts +4 -5
  196. package/src/utils.ts +1 -2
  197. package/src/workflow/context.ts +30 -29
  198. package/src/workflow/driver.ts +4 -6
  199. package/src/workflow/inspector.ts +2 -2
  200. package/src/workflow/mod.ts +15 -17
  201. package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
  202. package/dist/tsup/chunk-3YY5S6TV.js.map +0 -1
  203. package/dist/tsup/chunk-4BPKKZJO.cjs.map +0 -1
  204. package/dist/tsup/chunk-4JU3IPG2.js.map +0 -1
  205. package/dist/tsup/chunk-7HLFSAJP.cjs.map +0 -1
  206. package/dist/tsup/chunk-BATTOVHF.cjs.map +0 -1
  207. package/dist/tsup/chunk-D5G75T7J.js.map +0 -1
  208. package/dist/tsup/chunk-EMFKMVJR.js.map +0 -1
  209. package/dist/tsup/chunk-GBG63SUG.js.map +0 -1
  210. package/dist/tsup/chunk-HGW6PBWR.cjs.map +0 -1
  211. package/dist/tsup/chunk-KY3CERZR.js.map +0 -1
  212. package/dist/tsup/chunk-OT7FF6GB.cjs.map +0 -1
  213. package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
  214. package/dist/tsup/chunk-SRNOPUC6.cjs.map +0 -1
  215. package/dist/tsup/chunk-TMLOKTRB.js.map +0 -1
  216. package/dist/tsup/chunk-VUGENVIK.js.map +0 -1
  217. package/dist/tsup/process-metrics-NW754INA.js +0 -118
  218. package/dist/tsup/process-metrics-NW754INA.js.map +0 -1
  219. package/dist/tsup/process-metrics-TYAGKCEJ.cjs +0 -118
  220. package/dist/tsup/process-metrics-TYAGKCEJ.cjs.map +0 -1
  221. /package/dist/tsup/{chunk-D3T3ZBSY.js.map → chunk-BEI24WTI.js.map} +0 -0
@@ -1,23 +1,23 @@
1
1
  import {
2
2
  createWorkflowInspectorAdapter
3
- } from "../chunk-UZXQEGVJ.js";
3
+ } from "../chunk-P2GNQ4RN.js";
4
+ import "../chunk-UMZVD6DQ.js";
4
5
  import {
5
6
  ACTOR_CONTEXT_INTERNAL_SYMBOL,
6
7
  RAW_STATE_SYMBOL,
7
8
  RUN_FUNCTION_CONFIG_SYMBOL
8
- } from "../chunk-D5G75T7J.js";
9
- import "../chunk-VUGENVIK.js";
9
+ } from "../chunk-DPIMKYNB.js";
10
10
  import {
11
11
  makeWorkflowKey,
12
12
  workflowStoragePrefix
13
- } from "../chunk-3YY5S6TV.js";
14
- import "../chunk-KY3CERZR.js";
13
+ } from "../chunk-HXUEHHJF.js";
14
+ import "../chunk-2OTRTA3J.js";
15
15
  import {
16
16
  stringifyError
17
- } from "../chunk-GBG63SUG.js";
17
+ } from "../chunk-VTTFNQQI.js";
18
18
  import {
19
19
  RivetError
20
- } from "../chunk-TMLOKTRB.js";
20
+ } from "../chunk-BRP62GZC.js";
21
21
 
22
22
  // src/workflow/mod.ts
23
23
  import {
@@ -26,403 +26,140 @@ import {
26
26
  HistoryDivergedError,
27
27
  JoinError,
28
28
  RaceError,
29
- replayWorkflowFromStep,
30
29
  RollbackCheckpointError,
31
30
  RollbackError,
31
+ replayWorkflowFromStep,
32
32
  runWorkflow,
33
33
  StepExhaustedError
34
34
  } from "@rivetkit/workflow-engine";
35
35
  import invariant from "invariant";
36
36
 
37
- // src/workflow/driver.ts
38
- var WORKFLOW_STORAGE_PREFIX = workflowStoragePrefix();
39
- function stripWorkflowKey(prefixed) {
40
- return prefixed.slice(WORKFLOW_STORAGE_PREFIX.length);
41
- }
42
- function computeUpperBound(prefix) {
43
- const upperBound = prefix.slice();
44
- for (let i = upperBound.length - 1; i >= 0; i--) {
45
- if (upperBound[i] !== 255) {
46
- upperBound[i]++;
47
- return upperBound.slice(0, i + 1);
48
- }
49
- }
50
- return null;
51
- }
52
- var ActorWorkflowMessageDriver = class {
53
- #actor;
37
+ // src/workflow/constants.ts
38
+ var WORKFLOW_GUARD_KV_KEY = "__rivet_actor_workflow_guard_triggered";
39
+
40
+ // src/workflow/context.ts
41
+ var ActorWorkflowContext = class _ActorWorkflowContext {
42
+ #inner;
54
43
  #runCtx;
55
- constructor(actor, runCtx) {
56
- this.#actor = actor;
44
+ #actorAccessDepth = 0;
45
+ #allowActorAccess = false;
46
+ #guardViolation = false;
47
+ constructor(inner, runCtx) {
48
+ this.#inner = inner;
57
49
  this.#runCtx = runCtx;
58
50
  }
59
- async addMessage(message) {
60
- await this.#runCtx.internalKeepAwake(
61
- this.#actor.queueManager.enqueue(message.name, message.data)
62
- );
51
+ get workflowId() {
52
+ return this.#inner.workflowId;
63
53
  }
64
- async receiveMessages(opts) {
65
- const messages = await this.#runCtx.internalKeepAwake(
66
- this.#actor.queueManager.receive(
67
- opts.names && opts.names.length > 0 ? [...opts.names] : void 0,
68
- opts.count,
69
- 0,
70
- void 0,
71
- opts.completable
72
- )
73
- );
74
- return messages.map((message) => ({
75
- id: message.id.toString(),
76
- name: message.name,
77
- data: message.body,
78
- sentAt: message.createdAt,
79
- ...opts.completable ? {
80
- complete: async (response) => {
81
- await this.#runCtx.internalKeepAwake(
82
- this.#actor.queueManager.completeMessage(
83
- message,
84
- response
85
- )
86
- );
87
- }
88
- } : {}
89
- }));
54
+ get abortSignal() {
55
+ return this.#inner.abortSignal;
90
56
  }
91
- async completeMessage(messageId, response) {
92
- let parsedId;
93
- try {
94
- parsedId = BigInt(messageId);
95
- } catch {
96
- return;
57
+ get queue() {
58
+ const self = this;
59
+ async function next(name, opts) {
60
+ const message = await self.#inner.queue.next(name, opts);
61
+ return self.#toActorQueueMessage(message);
97
62
  }
98
- await this.#runCtx.internalKeepAwake(
99
- this.#actor.queueManager.completeMessageById(parsedId, response)
100
- );
101
- }
102
- };
103
- var ActorWorkflowDriver = class {
104
- workerPollInterval = 100;
105
- messageDriver;
106
- #actor;
107
- #runCtx;
108
- constructor(actor, runCtx) {
109
- this.#actor = actor;
110
- this.#runCtx = runCtx;
111
- this.messageDriver = new ActorWorkflowMessageDriver(actor, runCtx);
112
- }
113
- async get(key) {
114
- const [value] = await this.#runCtx.internalKeepAwake(
115
- this.#actor.driver.kvBatchGet(this.#actor.id, [
116
- makeWorkflowKey(key)
117
- ])
118
- );
119
- return value ?? null;
120
- }
121
- async set(key, value) {
122
- await this.#runCtx.internalKeepAwake(
123
- this.#actor.driver.kvBatchPut(this.#actor.id, [
124
- [makeWorkflowKey(key), value]
125
- ])
126
- );
127
- }
128
- async delete(key) {
129
- await this.#runCtx.internalKeepAwake(
130
- this.#actor.driver.kvBatchDelete(this.#actor.id, [
131
- makeWorkflowKey(key)
132
- ])
133
- );
63
+ async function nextBatch(name, opts) {
64
+ const messages = await self.#inner.queue.nextBatch(name, opts);
65
+ return messages.map(
66
+ (message) => self.#toActorQueueMessage(message)
67
+ );
68
+ }
69
+ async function send(name, body) {
70
+ self.#ensureActorAccess("queue.send");
71
+ await self.#runCtx.queue.send(name, body);
72
+ }
73
+ return {
74
+ next,
75
+ nextBatch,
76
+ send
77
+ };
134
78
  }
135
- async deletePrefix(prefix) {
136
- const start = makeWorkflowKey(prefix);
137
- const end = computeUpperBound(start);
138
- if (end) {
139
- await this.#runCtx.internalKeepAwake(
140
- this.#actor.driver.kvDeleteRange(this.#actor.id, start, end)
79
+ async step(nameOrConfig, run) {
80
+ if (typeof nameOrConfig === "string") {
81
+ if (!run) {
82
+ throw new Error("Step run function missing");
83
+ }
84
+ return await this.#wrapActive(
85
+ () => this.#inner.step(
86
+ nameOrConfig,
87
+ () => this.#withActorAccessAndStateRollback(run)
88
+ )
141
89
  );
142
- } else {
143
- const entries = await this.#runCtx.internalKeepAwake(
144
- this.#actor.driver.kvListPrefix(this.#actor.id, start)
90
+ }
91
+ const stepConfig = nameOrConfig;
92
+ const config = {
93
+ ...stepConfig,
94
+ run: () => this.#withActorAccessAndStateRollback(stepConfig.run)
95
+ };
96
+ return await this.#wrapActive(() => this.#inner.step(config));
97
+ }
98
+ async tryStep(nameOrConfig, run) {
99
+ if (typeof nameOrConfig === "string") {
100
+ if (!run) {
101
+ throw new Error("Step run function missing");
102
+ }
103
+ return await this.#wrapActive(
104
+ () => this.#inner.tryStep(
105
+ nameOrConfig,
106
+ () => this.#withActorAccessAndStateRollback(run)
107
+ )
145
108
  );
146
- if (entries.length === 0) {
147
- return;
109
+ }
110
+ const stepConfig = nameOrConfig;
111
+ const config = {
112
+ ...stepConfig,
113
+ run: () => this.#withActorAccessAndStateRollback(stepConfig.run)
114
+ };
115
+ return await this.#wrapActive(() => this.#inner.tryStep(config));
116
+ }
117
+ async try(nameOrConfig, run) {
118
+ if (typeof nameOrConfig === "string") {
119
+ if (!run) {
120
+ throw new Error("Try run function missing");
148
121
  }
149
- await this.#runCtx.internalKeepAwake(
150
- this.#actor.driver.kvBatchDelete(
151
- this.#actor.id,
152
- entries.map(([key]) => key)
122
+ return await this.#wrapActive(
123
+ () => this.#inner.try(
124
+ nameOrConfig,
125
+ async (ctx) => run(this.#createChildContext(ctx))
153
126
  )
154
127
  );
155
128
  }
129
+ const tryConfig = nameOrConfig;
130
+ const config = {
131
+ ...tryConfig,
132
+ run: async (ctx) => tryConfig.run(this.#createChildContext(ctx))
133
+ };
134
+ return await this.#wrapActive(() => this.#inner.try(config));
156
135
  }
157
- async deleteRange(start, end) {
158
- await this.#runCtx.internalKeepAwake(
159
- this.#actor.driver.kvDeleteRange(
160
- this.#actor.id,
161
- makeWorkflowKey(start),
162
- makeWorkflowKey(end)
163
- )
164
- );
136
+ async loop(nameOrConfig, run) {
137
+ if (typeof nameOrConfig === "string") {
138
+ if (!run) {
139
+ throw new Error("Loop run function missing");
140
+ }
141
+ return await this.#wrapActive(
142
+ () => this.#inner.loop(
143
+ nameOrConfig,
144
+ async (ctx) => run(this.#createChildContext(ctx))
145
+ )
146
+ );
147
+ }
148
+ const wrapped = {
149
+ ...nameOrConfig,
150
+ run: async (ctx, state) => nameOrConfig.run(this.#createChildContext(ctx), state)
151
+ };
152
+ return await this.#wrapActive(() => this.#inner.loop(wrapped));
165
153
  }
166
- async list(prefix) {
167
- const entries = await this.#runCtx.internalKeepAwake(
168
- this.#actor.driver.kvListPrefix(
169
- this.#actor.id,
170
- makeWorkflowKey(prefix)
171
- )
172
- );
173
- return entries.map(([key, value]) => ({
174
- key: stripWorkflowKey(key),
175
- value
176
- }));
154
+ sleep(name, durationMs) {
155
+ return this.#inner.sleep(name, durationMs);
177
156
  }
178
- async batch(writes) {
179
- if (writes.length === 0) return;
180
- await this.#runCtx.internalKeepAwake(
181
- Promise.all([
182
- this.#actor.driver.kvBatchPut(
183
- this.#actor.id,
184
- writes.map(({ key, value }) => [
185
- makeWorkflowKey(key),
186
- value
187
- ])
188
- ),
189
- this.#actor.stateManager.saveState({
190
- immediate: true
191
- })
192
- ])
193
- );
157
+ sleepUntil(name, timestampMs) {
158
+ return this.#inner.sleepUntil(name, timestampMs);
194
159
  }
195
- async setAlarm(_workflowId, wakeAt) {
196
- await this.#runCtx.internalKeepAwake(
197
- this.#actor.driver.setAlarm(this.#actor, wakeAt)
198
- );
199
- }
200
- async clearAlarm(_workflowId) {
201
- return;
202
- }
203
- waitForMessages(messageNames, abortSignal) {
204
- return this.#actor.queueManager.waitForNames(
205
- messageNames.length > 0 ? messageNames : void 0,
206
- abortSignal
207
- );
208
- }
209
- };
210
- var NoopWorkflowMessageDriver = class {
211
- async addMessage(_message) {
212
- throw new Error("Workflow control driver does not support messages");
213
- }
214
- async receiveMessages(_opts) {
215
- throw new Error("Workflow control driver does not support messages");
216
- }
217
- async completeMessage(_messageId, _response) {
218
- throw new Error("Workflow control driver does not support messages");
219
- }
220
- };
221
- var ActorWorkflowControlDriver = class {
222
- workerPollInterval = 100;
223
- messageDriver = new NoopWorkflowMessageDriver();
224
- #actor;
225
- constructor(actor) {
226
- this.#actor = actor;
227
- }
228
- async get(key) {
229
- const [value] = await this.#actor.driver.kvBatchGet(this.#actor.id, [
230
- makeWorkflowKey(key)
231
- ]);
232
- return value ?? null;
233
- }
234
- async set(key, value) {
235
- await this.#actor.driver.kvBatchPut(this.#actor.id, [
236
- [makeWorkflowKey(key), value]
237
- ]);
238
- }
239
- async delete(key) {
240
- await this.#actor.driver.kvBatchDelete(this.#actor.id, [
241
- makeWorkflowKey(key)
242
- ]);
243
- }
244
- async deletePrefix(prefix) {
245
- const start = makeWorkflowKey(prefix);
246
- const end = computeUpperBound(start);
247
- if (end) {
248
- await this.#actor.driver.kvDeleteRange(this.#actor.id, start, end);
249
- return;
250
- }
251
- const entries = await this.#actor.driver.kvListPrefix(
252
- this.#actor.id,
253
- start
254
- );
255
- if (entries.length === 0) {
256
- return;
257
- }
258
- await this.#actor.driver.kvBatchDelete(
259
- this.#actor.id,
260
- entries.map(([key]) => key)
261
- );
262
- }
263
- async deleteRange(start, end) {
264
- await this.#actor.driver.kvDeleteRange(
265
- this.#actor.id,
266
- makeWorkflowKey(start),
267
- makeWorkflowKey(end)
268
- );
269
- }
270
- async list(prefix) {
271
- const entries = await this.#actor.driver.kvListPrefix(
272
- this.#actor.id,
273
- makeWorkflowKey(prefix)
274
- );
275
- return entries.map(([key, value]) => ({
276
- key: stripWorkflowKey(key),
277
- value
278
- }));
279
- }
280
- async batch(writes) {
281
- if (writes.length === 0) {
282
- return;
283
- }
284
- await this.#actor.driver.kvBatchPut(
285
- this.#actor.id,
286
- writes.map(({ key, value }) => [makeWorkflowKey(key), value])
287
- );
288
- }
289
- async setAlarm(_workflowId, wakeAt) {
290
- await this.#actor.driver.setAlarm(this.#actor, wakeAt);
291
- }
292
- async clearAlarm(_workflowId) {
293
- return;
294
- }
295
- waitForMessages(_messageNames, _abortSignal) {
296
- throw new Error("Workflow control driver does not support messages");
297
- }
298
- };
299
-
300
- // src/workflow/constants.ts
301
- var WORKFLOW_GUARD_KV_KEY = "__rivet_actor_workflow_guard_triggered";
302
-
303
- // src/workflow/context.ts
304
- var ActorWorkflowContext = class _ActorWorkflowContext {
305
- #inner;
306
- #runCtx;
307
- #actorAccessDepth = 0;
308
- #allowActorAccess = false;
309
- #guardViolation = false;
310
- constructor(inner, runCtx) {
311
- this.#inner = inner;
312
- this.#runCtx = runCtx;
313
- }
314
- get workflowId() {
315
- return this.#inner.workflowId;
316
- }
317
- get abortSignal() {
318
- return this.#inner.abortSignal;
319
- }
320
- get queue() {
321
- const self = this;
322
- async function next(name, opts) {
323
- const message = await self.#inner.queue.next(name, opts);
324
- return self.#toActorQueueMessage(message);
325
- }
326
- async function nextBatch(name, opts) {
327
- const messages = await self.#inner.queue.nextBatch(name, opts);
328
- return messages.map(
329
- (message) => self.#toActorQueueMessage(message)
330
- );
331
- }
332
- async function send(name, body) {
333
- self.#ensureActorAccess("queue.send");
334
- await self.#runCtx.queue.send(name, body);
335
- }
336
- return {
337
- next,
338
- nextBatch,
339
- send
340
- };
341
- }
342
- async step(nameOrConfig, run) {
343
- if (typeof nameOrConfig === "string") {
344
- if (!run) {
345
- throw new Error("Step run function missing");
346
- }
347
- return await this.#wrapActive(
348
- () => this.#inner.step(
349
- nameOrConfig,
350
- () => this.#withActorAccessAndStateRollback(run)
351
- )
352
- );
353
- }
354
- const stepConfig = nameOrConfig;
355
- const config = {
356
- ...stepConfig,
357
- run: () => this.#withActorAccessAndStateRollback(stepConfig.run)
358
- };
359
- return await this.#wrapActive(() => this.#inner.step(config));
360
- }
361
- async tryStep(nameOrConfig, run) {
362
- if (typeof nameOrConfig === "string") {
363
- if (!run) {
364
- throw new Error("Step run function missing");
365
- }
366
- return await this.#wrapActive(
367
- () => this.#inner.tryStep(
368
- nameOrConfig,
369
- () => this.#withActorAccessAndStateRollback(run)
370
- )
371
- );
372
- }
373
- const stepConfig = nameOrConfig;
374
- const config = {
375
- ...stepConfig,
376
- run: () => this.#withActorAccessAndStateRollback(stepConfig.run)
377
- };
378
- return await this.#wrapActive(() => this.#inner.tryStep(config));
379
- }
380
- async try(nameOrConfig, run) {
381
- if (typeof nameOrConfig === "string") {
382
- if (!run) {
383
- throw new Error("Try run function missing");
384
- }
385
- return await this.#wrapActive(
386
- () => this.#inner.try(
387
- nameOrConfig,
388
- async (ctx) => run(this.#createChildContext(ctx))
389
- )
390
- );
391
- }
392
- const tryConfig = nameOrConfig;
393
- const config = {
394
- ...tryConfig,
395
- run: async (ctx) => tryConfig.run(this.#createChildContext(ctx))
396
- };
397
- return await this.#wrapActive(() => this.#inner.try(config));
398
- }
399
- async loop(nameOrConfig, run) {
400
- if (typeof nameOrConfig === "string") {
401
- if (!run) {
402
- throw new Error("Loop run function missing");
403
- }
404
- return await this.#wrapActive(
405
- () => this.#inner.loop(
406
- nameOrConfig,
407
- async (ctx) => run(this.#createChildContext(ctx))
408
- )
409
- );
410
- }
411
- const wrapped = {
412
- ...nameOrConfig,
413
- run: async (ctx, state) => nameOrConfig.run(this.#createChildContext(ctx), state)
414
- };
415
- return await this.#wrapActive(() => this.#inner.loop(wrapped));
416
- }
417
- sleep(name, durationMs) {
418
- return this.#inner.sleep(name, durationMs);
419
- }
420
- sleepUntil(name, timestampMs) {
421
- return this.#inner.sleepUntil(name, timestampMs);
422
- }
423
- destroy() {
424
- this.#ensureActorAccess("destroy");
425
- this.#runCtx.destroy();
160
+ destroy() {
161
+ this.#ensureActorAccess("destroy");
162
+ this.#runCtx.destroy();
426
163
  }
427
164
  async rollbackCheckpoint(name) {
428
165
  await this.#wrapActive(() => this.#inner.rollbackCheckpoint(name));
@@ -608,6 +345,269 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
608
345
  }
609
346
  };
610
347
 
348
+ // src/workflow/driver.ts
349
+ var WORKFLOW_STORAGE_PREFIX = workflowStoragePrefix();
350
+ function stripWorkflowKey(prefixed) {
351
+ return prefixed.slice(WORKFLOW_STORAGE_PREFIX.length);
352
+ }
353
+ function computeUpperBound(prefix) {
354
+ const upperBound = prefix.slice();
355
+ for (let i = upperBound.length - 1; i >= 0; i--) {
356
+ if (upperBound[i] !== 255) {
357
+ upperBound[i]++;
358
+ return upperBound.slice(0, i + 1);
359
+ }
360
+ }
361
+ return null;
362
+ }
363
+ var ActorWorkflowMessageDriver = class {
364
+ #actor;
365
+ #runCtx;
366
+ constructor(actor, runCtx) {
367
+ this.#actor = actor;
368
+ this.#runCtx = runCtx;
369
+ }
370
+ async addMessage(message) {
371
+ await this.#runCtx.internalKeepAwake(
372
+ this.#actor.queueManager.enqueue(message.name, message.data)
373
+ );
374
+ }
375
+ async receiveMessages(opts) {
376
+ const messages = await this.#runCtx.internalKeepAwake(
377
+ this.#actor.queueManager.receive(
378
+ opts.names && opts.names.length > 0 ? [...opts.names] : void 0,
379
+ opts.count,
380
+ 0,
381
+ void 0,
382
+ opts.completable
383
+ )
384
+ );
385
+ return messages.map((message) => ({
386
+ id: message.id.toString(),
387
+ name: message.name,
388
+ data: message.body,
389
+ sentAt: message.createdAt,
390
+ ...opts.completable ? {
391
+ complete: async (response) => {
392
+ await this.#runCtx.internalKeepAwake(
393
+ this.#actor.queueManager.completeMessage(
394
+ message,
395
+ response
396
+ )
397
+ );
398
+ }
399
+ } : {}
400
+ }));
401
+ }
402
+ async completeMessage(messageId, response) {
403
+ let parsedId;
404
+ try {
405
+ parsedId = BigInt(messageId);
406
+ } catch {
407
+ return;
408
+ }
409
+ await this.#runCtx.internalKeepAwake(
410
+ this.#actor.queueManager.completeMessageById(parsedId, response)
411
+ );
412
+ }
413
+ };
414
+ var ActorWorkflowDriver = class {
415
+ workerPollInterval = 100;
416
+ messageDriver;
417
+ #actor;
418
+ #runCtx;
419
+ constructor(actor, runCtx) {
420
+ this.#actor = actor;
421
+ this.#runCtx = runCtx;
422
+ this.messageDriver = new ActorWorkflowMessageDriver(actor, runCtx);
423
+ }
424
+ async get(key) {
425
+ const [value] = await this.#runCtx.internalKeepAwake(
426
+ this.#actor.driver.kvBatchGet(this.#actor.id, [
427
+ makeWorkflowKey(key)
428
+ ])
429
+ );
430
+ return value ?? null;
431
+ }
432
+ async set(key, value) {
433
+ await this.#runCtx.internalKeepAwake(
434
+ this.#actor.driver.kvBatchPut(this.#actor.id, [
435
+ [makeWorkflowKey(key), value]
436
+ ])
437
+ );
438
+ }
439
+ async delete(key) {
440
+ await this.#runCtx.internalKeepAwake(
441
+ this.#actor.driver.kvBatchDelete(this.#actor.id, [
442
+ makeWorkflowKey(key)
443
+ ])
444
+ );
445
+ }
446
+ async deletePrefix(prefix) {
447
+ const start = makeWorkflowKey(prefix);
448
+ const end = computeUpperBound(start);
449
+ if (end) {
450
+ await this.#runCtx.internalKeepAwake(
451
+ this.#actor.driver.kvDeleteRange(this.#actor.id, start, end)
452
+ );
453
+ } else {
454
+ const entries = await this.#runCtx.internalKeepAwake(
455
+ this.#actor.driver.kvListPrefix(this.#actor.id, start)
456
+ );
457
+ if (entries.length === 0) {
458
+ return;
459
+ }
460
+ await this.#runCtx.internalKeepAwake(
461
+ this.#actor.driver.kvBatchDelete(
462
+ this.#actor.id,
463
+ entries.map(([key]) => key)
464
+ )
465
+ );
466
+ }
467
+ }
468
+ async deleteRange(start, end) {
469
+ await this.#runCtx.internalKeepAwake(
470
+ this.#actor.driver.kvDeleteRange(
471
+ this.#actor.id,
472
+ makeWorkflowKey(start),
473
+ makeWorkflowKey(end)
474
+ )
475
+ );
476
+ }
477
+ async list(prefix) {
478
+ const entries = await this.#runCtx.internalKeepAwake(
479
+ this.#actor.driver.kvListPrefix(
480
+ this.#actor.id,
481
+ makeWorkflowKey(prefix)
482
+ )
483
+ );
484
+ return entries.map(([key, value]) => ({
485
+ key: stripWorkflowKey(key),
486
+ value
487
+ }));
488
+ }
489
+ async batch(writes) {
490
+ if (writes.length === 0) return;
491
+ await this.#runCtx.internalKeepAwake(
492
+ Promise.all([
493
+ this.#actor.driver.kvBatchPut(
494
+ this.#actor.id,
495
+ writes.map(({ key, value }) => [
496
+ makeWorkflowKey(key),
497
+ value
498
+ ])
499
+ ),
500
+ this.#actor.stateManager.saveState({
501
+ immediate: true
502
+ })
503
+ ])
504
+ );
505
+ }
506
+ async setAlarm(_workflowId, wakeAt) {
507
+ await this.#runCtx.internalKeepAwake(
508
+ this.#actor.driver.setAlarm(this.#actor, wakeAt)
509
+ );
510
+ }
511
+ async clearAlarm(_workflowId) {
512
+ return;
513
+ }
514
+ waitForMessages(messageNames, abortSignal) {
515
+ return this.#actor.queueManager.waitForNames(
516
+ messageNames.length > 0 ? messageNames : void 0,
517
+ abortSignal
518
+ );
519
+ }
520
+ };
521
+ var NoopWorkflowMessageDriver = class {
522
+ async addMessage(_message) {
523
+ throw new Error("Workflow control driver does not support messages");
524
+ }
525
+ async receiveMessages(_opts) {
526
+ throw new Error("Workflow control driver does not support messages");
527
+ }
528
+ async completeMessage(_messageId, _response) {
529
+ throw new Error("Workflow control driver does not support messages");
530
+ }
531
+ };
532
+ var ActorWorkflowControlDriver = class {
533
+ workerPollInterval = 100;
534
+ messageDriver = new NoopWorkflowMessageDriver();
535
+ #actor;
536
+ constructor(actor) {
537
+ this.#actor = actor;
538
+ }
539
+ async get(key) {
540
+ const [value] = await this.#actor.driver.kvBatchGet(this.#actor.id, [
541
+ makeWorkflowKey(key)
542
+ ]);
543
+ return value ?? null;
544
+ }
545
+ async set(key, value) {
546
+ await this.#actor.driver.kvBatchPut(this.#actor.id, [
547
+ [makeWorkflowKey(key), value]
548
+ ]);
549
+ }
550
+ async delete(key) {
551
+ await this.#actor.driver.kvBatchDelete(this.#actor.id, [
552
+ makeWorkflowKey(key)
553
+ ]);
554
+ }
555
+ async deletePrefix(prefix) {
556
+ const start = makeWorkflowKey(prefix);
557
+ const end = computeUpperBound(start);
558
+ if (end) {
559
+ await this.#actor.driver.kvDeleteRange(this.#actor.id, start, end);
560
+ return;
561
+ }
562
+ const entries = await this.#actor.driver.kvListPrefix(
563
+ this.#actor.id,
564
+ start
565
+ );
566
+ if (entries.length === 0) {
567
+ return;
568
+ }
569
+ await this.#actor.driver.kvBatchDelete(
570
+ this.#actor.id,
571
+ entries.map(([key]) => key)
572
+ );
573
+ }
574
+ async deleteRange(start, end) {
575
+ await this.#actor.driver.kvDeleteRange(
576
+ this.#actor.id,
577
+ makeWorkflowKey(start),
578
+ makeWorkflowKey(end)
579
+ );
580
+ }
581
+ async list(prefix) {
582
+ const entries = await this.#actor.driver.kvListPrefix(
583
+ this.#actor.id,
584
+ makeWorkflowKey(prefix)
585
+ );
586
+ return entries.map(([key, value]) => ({
587
+ key: stripWorkflowKey(key),
588
+ value
589
+ }));
590
+ }
591
+ async batch(writes) {
592
+ if (writes.length === 0) {
593
+ return;
594
+ }
595
+ await this.#actor.driver.kvBatchPut(
596
+ this.#actor.id,
597
+ writes.map(({ key, value }) => [makeWorkflowKey(key), value])
598
+ );
599
+ }
600
+ async setAlarm(_workflowId, wakeAt) {
601
+ await this.#actor.driver.setAlarm(this.#actor, wakeAt);
602
+ }
603
+ async clearAlarm(_workflowId) {
604
+ return;
605
+ }
606
+ waitForMessages(_messageNames, _abortSignal) {
607
+ throw new Error("Workflow control driver does not support messages");
608
+ }
609
+ };
610
+
611
611
  // src/workflow/mod.ts
612
612
  import { Loop } from "@rivetkit/workflow-engine";
613
613
  function shouldRethrowWorkflowError(error) {