rivetkit 2.0.42 → 2.1.0-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 (322) hide show
  1. package/dist/{tsup/config-CLnylLYY.d.ts → browser/client.d.ts} +2127 -1910
  2. package/dist/browser/client.js +5182 -0
  3. package/dist/browser/client.js.map +1 -0
  4. package/dist/browser/inspector/client.d.ts +130 -0
  5. package/dist/browser/inspector/client.js +2854 -0
  6. package/dist/browser/inspector/client.js.map +1 -0
  7. package/dist/browser/v3-DnYObHH3.d.ts +279 -0
  8. package/dist/schemas/actor-inspector/v2.ts +796 -0
  9. package/dist/schemas/actor-inspector/v3.ts +899 -0
  10. package/dist/schemas/actor-persist/v4.ts +406 -0
  11. package/dist/schemas/client-protocol/v3.ts +554 -0
  12. package/dist/schemas/persist/v1.ts +781 -0
  13. package/dist/schemas/transport/v1.ts +697 -0
  14. package/dist/tsup/actor/errors.cjs +27 -3
  15. package/dist/tsup/actor/errors.cjs.map +1 -1
  16. package/dist/tsup/actor/errors.d.cts +37 -1
  17. package/dist/tsup/actor/errors.d.ts +37 -1
  18. package/dist/tsup/actor/errors.js +26 -1
  19. package/dist/tsup/{actor-router-consts-DzI2szci.d.cts → actor-router-consts-D29T1Z-K.d.cts} +1 -1
  20. package/dist/tsup/{actor-router-consts-DzI2szci.d.ts → actor-router-consts-D29T1Z-K.d.ts} +1 -1
  21. package/dist/tsup/chunk-424PT5DM.js +23 -0
  22. package/dist/tsup/chunk-424PT5DM.js.map +1 -0
  23. package/dist/tsup/{chunk-JDAD2YFA.js → chunk-5ESWDTHJ.js} +148 -273
  24. package/dist/tsup/chunk-5ESWDTHJ.js.map +1 -0
  25. package/dist/tsup/{chunk-FJ3KTN4V.js → chunk-6LIBPELE.js} +119 -11
  26. package/dist/tsup/chunk-6LIBPELE.js.map +1 -0
  27. package/dist/tsup/chunk-6LJAZ5R4.cjs +96 -0
  28. package/dist/tsup/chunk-6LJAZ5R4.cjs.map +1 -0
  29. package/dist/tsup/{chunk-LFVF5SCU.js → chunk-7HTNH26M.js} +126 -1
  30. package/dist/tsup/chunk-7HTNH26M.js.map +1 -0
  31. package/dist/tsup/chunk-7K4CYDGD.js +630 -0
  32. package/dist/tsup/chunk-7K4CYDGD.js.map +1 -0
  33. package/dist/tsup/{chunk-XXGJCOL6.js → chunk-A6YIZWTK.js} +2 -2
  34. package/dist/tsup/chunk-AIYEYMX5.cjs +630 -0
  35. package/dist/tsup/chunk-AIYEYMX5.cjs.map +1 -0
  36. package/dist/tsup/{chunk-Q6W7RJJP.js → chunk-DIGBC2VI.js} +211 -2316
  37. package/dist/tsup/chunk-DIGBC2VI.js.map +1 -0
  38. package/dist/tsup/{chunk-RZW2DNND.cjs → chunk-F6JYU5IK.cjs} +1957 -1039
  39. package/dist/tsup/chunk-F6JYU5IK.cjs.map +1 -0
  40. package/dist/tsup/chunk-HAZL2EPK.cjs +534 -0
  41. package/dist/tsup/chunk-HAZL2EPK.cjs.map +1 -0
  42. package/dist/tsup/chunk-HDQ2JUQT.cjs +23 -0
  43. package/dist/tsup/chunk-HDQ2JUQT.cjs.map +1 -0
  44. package/dist/tsup/chunk-HIDX4C5Y.cjs +1036 -0
  45. package/dist/tsup/chunk-HIDX4C5Y.cjs.map +1 -0
  46. package/dist/tsup/chunk-IVG73YCW.js +534 -0
  47. package/dist/tsup/chunk-IVG73YCW.js.map +1 -0
  48. package/dist/tsup/chunk-KJSYAUOM.js +96 -0
  49. package/dist/tsup/chunk-KJSYAUOM.js.map +1 -0
  50. package/dist/tsup/{chunk-2XQS746M.cjs → chunk-L47L3ZWJ.cjs} +127 -2
  51. package/dist/tsup/chunk-L47L3ZWJ.cjs.map +1 -0
  52. package/dist/tsup/{chunk-H4TB4X25.cjs → chunk-LW6KLR7A.cjs} +126 -18
  53. package/dist/tsup/chunk-LW6KLR7A.cjs.map +1 -0
  54. package/dist/tsup/chunk-LXUQ667X.js +2006 -0
  55. package/dist/tsup/chunk-LXUQ667X.js.map +1 -0
  56. package/dist/tsup/{chunk-GMAVRZSF.js → chunk-M2T62AZQ.js} +1790 -872
  57. package/dist/tsup/chunk-M2T62AZQ.js.map +1 -0
  58. package/dist/tsup/chunk-MZ37VV3P.js +5974 -0
  59. package/dist/tsup/chunk-MZ37VV3P.js.map +1 -0
  60. package/dist/tsup/chunk-N4KRDJ56.js +72 -0
  61. package/dist/tsup/chunk-N4KRDJ56.js.map +1 -0
  62. package/dist/tsup/chunk-NIYZDWMW.cjs +2006 -0
  63. package/dist/tsup/chunk-NIYZDWMW.cjs.map +1 -0
  64. package/dist/tsup/chunk-OMEPCQK2.js +649 -0
  65. package/dist/tsup/chunk-OMEPCQK2.js.map +1 -0
  66. package/dist/tsup/chunk-SR3KQE7Q.cjs +72 -0
  67. package/dist/tsup/chunk-SR3KQE7Q.cjs.map +1 -0
  68. package/dist/tsup/chunk-SSEP6DHP.cjs +2657 -0
  69. package/dist/tsup/chunk-SSEP6DHP.cjs.map +1 -0
  70. package/dist/tsup/chunk-T5YCUGVS.js +1036 -0
  71. package/dist/tsup/chunk-T5YCUGVS.js.map +1 -0
  72. package/dist/tsup/{chunk-EJVBH5VF.cjs → chunk-TPGXWFQT.cjs} +3 -3
  73. package/dist/tsup/{chunk-EJVBH5VF.cjs.map → chunk-TPGXWFQT.cjs.map} +1 -1
  74. package/dist/tsup/{chunk-X35U3YNX.cjs → chunk-TYLXNCA5.cjs} +214 -339
  75. package/dist/tsup/chunk-TYLXNCA5.cjs.map +1 -0
  76. package/dist/tsup/chunk-VKVNIQRQ.js +257 -0
  77. package/dist/tsup/chunk-VKVNIQRQ.js.map +1 -0
  78. package/dist/tsup/chunk-XWBAQO5H.cjs +649 -0
  79. package/dist/tsup/chunk-XWBAQO5H.cjs.map +1 -0
  80. package/dist/tsup/chunk-YQ4LDVD6.cjs +5974 -0
  81. package/dist/tsup/chunk-YQ4LDVD6.cjs.map +1 -0
  82. package/dist/tsup/chunk-ZFY5J2EP.cjs +257 -0
  83. package/dist/tsup/chunk-ZFY5J2EP.cjs.map +1 -0
  84. package/dist/tsup/client/mod.cjs +9 -10
  85. package/dist/tsup/client/mod.cjs.map +1 -1
  86. package/dist/tsup/client/mod.d.cts +11 -5
  87. package/dist/tsup/client/mod.d.ts +11 -5
  88. package/dist/tsup/client/mod.js +8 -8
  89. package/dist/tsup/common/log.cjs +4 -4
  90. package/dist/tsup/common/log.d.cts +2 -2
  91. package/dist/tsup/common/log.d.ts +2 -2
  92. package/dist/tsup/common/log.js +3 -2
  93. package/dist/tsup/common/websocket.cjs +5 -5
  94. package/dist/tsup/common/websocket.js +4 -3
  95. package/dist/tsup/config-BFqid9Gr.d.ts +2574 -0
  96. package/dist/tsup/config-BiNoIHRs.d.cts +80 -0
  97. package/dist/tsup/config-BiNoIHRs.d.ts +80 -0
  98. package/dist/tsup/{config-CZB2-W8x.d.cts → config-CAZphOS1.d.cts} +681 -355
  99. package/dist/tsup/db/drizzle/mod.cjs +49 -0
  100. package/dist/tsup/db/drizzle/mod.cjs.map +1 -0
  101. package/dist/tsup/db/drizzle/mod.d.cts +17 -0
  102. package/dist/tsup/db/drizzle/mod.d.ts +17 -0
  103. package/dist/tsup/db/drizzle/mod.js +49 -0
  104. package/dist/tsup/db/drizzle/mod.js.map +1 -0
  105. package/dist/tsup/db/mod.cjs +9 -0
  106. package/dist/tsup/db/mod.cjs.map +1 -0
  107. package/dist/tsup/db/mod.d.cts +9 -0
  108. package/dist/tsup/db/mod.d.ts +9 -0
  109. package/dist/tsup/db/mod.js +9 -0
  110. package/dist/tsup/db/mod.js.map +1 -0
  111. package/dist/tsup/{driver-D0QX9M11.d.ts → driver-Bxv62E2p.d.ts} +2 -2
  112. package/dist/tsup/{driver-q-zqG7fc.d.cts → driver-DYXwJR5D.d.cts} +2 -2
  113. package/dist/tsup/driver-helpers/mod.cjs +12 -6
  114. package/dist/tsup/driver-helpers/mod.cjs.map +1 -1
  115. package/dist/tsup/driver-helpers/mod.d.cts +12 -5
  116. package/dist/tsup/driver-helpers/mod.d.ts +12 -5
  117. package/dist/tsup/driver-helpers/mod.js +12 -5
  118. package/dist/tsup/driver-test-suite/mod.cjs +1370 -116
  119. package/dist/tsup/driver-test-suite/mod.cjs.map +1 -1
  120. package/dist/tsup/driver-test-suite/mod.d.cts +10 -4
  121. package/dist/tsup/driver-test-suite/mod.d.ts +10 -4
  122. package/dist/tsup/driver-test-suite/mod.js +2093 -838
  123. package/dist/tsup/driver-test-suite/mod.js.map +1 -1
  124. package/dist/tsup/inspector/mod.cjs +29 -3
  125. package/dist/tsup/inspector/mod.cjs.map +1 -1
  126. package/dist/tsup/inspector/mod.d.cts +124 -3
  127. package/dist/tsup/inspector/mod.d.ts +124 -3
  128. package/dist/tsup/inspector/mod.js +72 -45
  129. package/dist/tsup/keys-CydblqMh.d.cts +13 -0
  130. package/dist/tsup/keys-CydblqMh.d.ts +13 -0
  131. package/dist/tsup/mod.cjs +16 -10
  132. package/dist/tsup/mod.cjs.map +1 -1
  133. package/dist/tsup/mod.d.cts +26 -14
  134. package/dist/tsup/mod.d.ts +26 -14
  135. package/dist/tsup/mod.js +20 -13
  136. package/dist/tsup/serve-test-suite/mod.cjs +1165 -83
  137. package/dist/tsup/serve-test-suite/mod.cjs.map +1 -1
  138. package/dist/tsup/serve-test-suite/mod.js +1114 -29
  139. package/dist/tsup/serve-test-suite/mod.js.map +1 -1
  140. package/dist/tsup/test/mod.cjs +84 -11
  141. package/dist/tsup/test/mod.cjs.map +1 -1
  142. package/dist/tsup/test/mod.d.cts +10 -5
  143. package/dist/tsup/test/mod.d.ts +10 -5
  144. package/dist/tsup/test/mod.js +85 -11
  145. package/dist/tsup/test/mod.js.map +1 -1
  146. package/dist/tsup/utils.cjs +10 -4
  147. package/dist/tsup/utils.cjs.map +1 -1
  148. package/dist/tsup/utils.d.cts +72 -2
  149. package/dist/tsup/utils.d.ts +72 -2
  150. package/dist/tsup/utils.js +9 -2
  151. package/dist/tsup/v3-DnYObHH3.d.cts +279 -0
  152. package/dist/tsup/v3-DnYObHH3.d.ts +279 -0
  153. package/dist/tsup/workflow/mod.cjs +16 -0
  154. package/dist/tsup/workflow/mod.cjs.map +1 -0
  155. package/dist/tsup/workflow/mod.d.cts +83 -0
  156. package/dist/tsup/workflow/mod.d.ts +83 -0
  157. package/dist/tsup/workflow/mod.js +16 -0
  158. package/dist/tsup/workflow/mod.js.map +1 -0
  159. package/package.json +62 -5
  160. package/src/actor/config.ts +478 -68
  161. package/src/actor/conn/mod.ts +68 -16
  162. package/src/actor/conn/state-manager.ts +2 -2
  163. package/src/actor/contexts/action.ts +20 -12
  164. package/src/actor/contexts/base/actor.ts +137 -7
  165. package/src/actor/contexts/base/conn-init.ts +27 -7
  166. package/src/actor/contexts/base/conn.ts +27 -18
  167. package/src/actor/contexts/before-action-response.ts +9 -2
  168. package/src/actor/contexts/before-connect.ts +7 -2
  169. package/src/actor/contexts/connect.ts +9 -2
  170. package/src/actor/contexts/create-conn-state.ts +7 -2
  171. package/src/actor/contexts/create-vars.ts +16 -3
  172. package/src/actor/contexts/create.ts +16 -3
  173. package/src/actor/contexts/destroy.ts +9 -3
  174. package/src/actor/contexts/disconnect.ts +10 -4
  175. package/src/actor/contexts/index.ts +4 -3
  176. package/src/actor/contexts/request.ts +23 -6
  177. package/src/actor/contexts/run.ts +47 -0
  178. package/src/actor/contexts/sleep.ts +9 -3
  179. package/src/actor/contexts/state-change.ts +9 -3
  180. package/src/actor/contexts/wake.ts +9 -3
  181. package/src/actor/contexts/websocket.ts +23 -6
  182. package/src/actor/database.ts +8 -18
  183. package/src/actor/definition.ts +20 -6
  184. package/src/actor/driver.ts +32 -3
  185. package/src/actor/errors.ts +127 -0
  186. package/src/actor/instance/connection-manager.ts +183 -80
  187. package/src/actor/instance/event-manager.ts +26 -15
  188. package/src/actor/instance/keys.ts +117 -0
  189. package/src/actor/instance/mod.ts +784 -174
  190. package/src/actor/instance/queue-manager.ts +603 -0
  191. package/src/actor/instance/queue.ts +287 -0
  192. package/src/actor/instance/schedule-manager.ts +49 -7
  193. package/src/actor/instance/state-manager.ts +35 -11
  194. package/src/actor/instance/traces-driver.ts +128 -0
  195. package/src/actor/mod.ts +26 -2
  196. package/src/actor/protocol/old.ts +28 -13
  197. package/src/actor/protocol/serde.ts +1 -1
  198. package/src/actor/router-endpoints.ts +177 -21
  199. package/src/actor/router-websocket-endpoints.ts +18 -29
  200. package/src/actor/router.ts +177 -0
  201. package/src/actor/schema.ts +291 -0
  202. package/src/actor/utils.ts +40 -0
  203. package/src/client/actor-common.ts +1 -1
  204. package/src/client/actor-conn.ts +100 -33
  205. package/src/client/actor-handle.ts +61 -33
  206. package/src/client/client.ts +2 -4
  207. package/src/client/config.ts +1 -1
  208. package/src/client/mod.browser.ts +2 -0
  209. package/src/client/mod.ts +1 -4
  210. package/src/client/queue.ts +146 -0
  211. package/src/client/utils.ts +1 -1
  212. package/src/common/log.ts +1 -1
  213. package/src/common/utils.ts +3 -3
  214. package/src/db/config.ts +100 -0
  215. package/src/db/drizzle/mod.ts +226 -0
  216. package/src/db/drizzle/sqlite-core.ts +22 -0
  217. package/src/db/mod.ts +125 -0
  218. package/src/db/shared.ts +92 -0
  219. package/src/db/sqlite-vfs.ts +12 -0
  220. package/src/driver-helpers/mod.ts +1 -0
  221. package/src/driver-test-suite/mod.ts +69 -43
  222. package/src/driver-test-suite/tests/access-control.ts +218 -0
  223. package/src/driver-test-suite/tests/actor-db-raw.ts +73 -0
  224. package/src/driver-test-suite/tests/actor-db.ts +394 -0
  225. package/src/driver-test-suite/tests/actor-inspector.ts +259 -358
  226. package/src/driver-test-suite/tests/actor-kv.ts +41 -20
  227. package/src/driver-test-suite/tests/actor-queue.ts +324 -0
  228. package/src/driver-test-suite/tests/actor-run.ts +181 -0
  229. package/src/driver-test-suite/tests/actor-schedule.ts +5 -2
  230. package/src/driver-test-suite/tests/actor-sleep.ts +3 -3
  231. package/src/driver-test-suite/tests/actor-stateless.ts +70 -0
  232. package/src/driver-test-suite/tests/actor-workflow.ts +108 -0
  233. package/src/driver-test-suite/tests/manager-driver.ts +11 -0
  234. package/src/driver-test-suite/tests/raw-http-request-properties.ts +1 -1
  235. package/src/driver-test-suite/tests/raw-websocket.ts +12 -12
  236. package/src/drivers/default.ts +7 -2
  237. package/src/drivers/engine/actor-driver.ts +45 -37
  238. package/src/drivers/engine/config.ts +1 -1
  239. package/src/drivers/file-system/actor.ts +20 -2
  240. package/src/drivers/file-system/global-state.ts +569 -258
  241. package/src/drivers/file-system/kv-limits.ts +70 -0
  242. package/src/drivers/file-system/manager.ts +22 -6
  243. package/src/drivers/file-system/mod.ts +39 -16
  244. package/src/drivers/file-system/sqlite-runtime.ts +210 -0
  245. package/src/inspector/actor-inspector.ts +224 -102
  246. package/src/inspector/config.ts +1 -1
  247. package/src/inspector/handler.ts +102 -20
  248. package/src/inspector/mod.browser.ts +8 -0
  249. package/src/inspector/mod.ts +2 -0
  250. package/src/inspector/serve-ui.ts +40 -0
  251. package/src/inspector/transport.ts +18 -0
  252. package/src/inspector/utils.ts +5 -39
  253. package/src/manager/gateway.ts +1 -1
  254. package/src/manager/protocol/mod.ts +1 -1
  255. package/src/manager/protocol/query.ts +1 -1
  256. package/src/manager/router-schema.ts +1 -1
  257. package/src/manager/router.ts +38 -12
  258. package/src/manager-api/actors.ts +1 -1
  259. package/src/manager-api/common.ts +1 -1
  260. package/src/registry/config/driver.ts +1 -1
  261. package/src/registry/config/index.ts +212 -43
  262. package/src/registry/config/legacy-runner.ts +1 -1
  263. package/src/registry/config/runner.ts +1 -1
  264. package/src/registry/config/serverless.ts +1 -1
  265. package/src/registry/index.ts +7 -5
  266. package/src/remote-manager-driver/api-utils.ts +1 -1
  267. package/src/schemas/actor-inspector/mod.ts +1 -1
  268. package/src/schemas/actor-inspector/versioned.ts +195 -8
  269. package/src/schemas/actor-persist/versioned.ts +87 -7
  270. package/src/schemas/client-protocol/mod.ts +1 -1
  271. package/src/schemas/client-protocol/versioned.ts +127 -11
  272. package/src/schemas/client-protocol-zod/mod.ts +16 -1
  273. package/src/schemas/persist/mod.ts +1 -0
  274. package/src/schemas/transport/mod.ts +1 -0
  275. package/src/serde.ts +1 -1
  276. package/src/serve-test-suite/mod.ts +10 -9
  277. package/src/test/mod.ts +15 -56
  278. package/src/utils/endpoint-parser.test.ts +1 -1
  279. package/src/utils/endpoint-parser.ts +1 -1
  280. package/src/utils/env-vars.ts +12 -1
  281. package/src/utils/node.ts +15 -2
  282. package/src/utils.test.ts +34 -0
  283. package/src/utils.ts +140 -6
  284. package/src/workflow/constants.ts +2 -0
  285. package/src/workflow/context.ts +532 -0
  286. package/src/workflow/driver.ts +191 -0
  287. package/src/workflow/inspector.ts +268 -0
  288. package/src/workflow/mod.ts +122 -0
  289. package/dist/tsup/chunk-2IJTYN6K.cjs +0 -278
  290. package/dist/tsup/chunk-2IJTYN6K.cjs.map +0 -1
  291. package/dist/tsup/chunk-2XQS746M.cjs.map +0 -1
  292. package/dist/tsup/chunk-3VP5CSHV.cjs +0 -114
  293. package/dist/tsup/chunk-3VP5CSHV.cjs.map +0 -1
  294. package/dist/tsup/chunk-AQFSQMBG.js +0 -114
  295. package/dist/tsup/chunk-AQFSQMBG.js.map +0 -1
  296. package/dist/tsup/chunk-E6ZE2YEA.js +0 -664
  297. package/dist/tsup/chunk-E6ZE2YEA.js.map +0 -1
  298. package/dist/tsup/chunk-FJ3KTN4V.js.map +0 -1
  299. package/dist/tsup/chunk-GBENOENJ.cjs +0 -8
  300. package/dist/tsup/chunk-GBENOENJ.cjs.map +0 -1
  301. package/dist/tsup/chunk-GD7UXGOE.cjs +0 -4762
  302. package/dist/tsup/chunk-GD7UXGOE.cjs.map +0 -1
  303. package/dist/tsup/chunk-GMAVRZSF.js.map +0 -1
  304. package/dist/tsup/chunk-H4TB4X25.cjs.map +0 -1
  305. package/dist/tsup/chunk-JDAD2YFA.js.map +0 -1
  306. package/dist/tsup/chunk-KCOVZOPS.js +0 -1946
  307. package/dist/tsup/chunk-KCOVZOPS.js.map +0 -1
  308. package/dist/tsup/chunk-KDFWJKMJ.cjs +0 -664
  309. package/dist/tsup/chunk-KDFWJKMJ.cjs.map +0 -1
  310. package/dist/tsup/chunk-LFVF5SCU.js.map +0 -1
  311. package/dist/tsup/chunk-Q6W7RJJP.js.map +0 -1
  312. package/dist/tsup/chunk-RUW5CZ5Z.cjs +0 -1949
  313. package/dist/tsup/chunk-RUW5CZ5Z.cjs.map +0 -1
  314. package/dist/tsup/chunk-RZW2DNND.cjs.map +0 -1
  315. package/dist/tsup/chunk-TCOEBUUE.js +0 -278
  316. package/dist/tsup/chunk-TCOEBUUE.js.map +0 -1
  317. package/dist/tsup/chunk-X35U3YNX.cjs.map +0 -1
  318. package/dist/tsup/keys-Chhy4ylv.d.cts +0 -8
  319. package/dist/tsup/keys-Chhy4ylv.d.ts +0 -8
  320. package/dist/tsup/v1-Gq4avTK3.d.cts +0 -240
  321. package/dist/tsup/v1-Gq4avTK3.d.ts +0 -240
  322. /package/dist/tsup/{chunk-XXGJCOL6.js.map → chunk-A6YIZWTK.js.map} +0 -0
@@ -0,0 +1,532 @@
1
+ import type { RunContext } from "@/actor/contexts/run";
2
+ import type { Client } from "@/client/client";
3
+ import type { Registry } from "@/registry";
4
+ import type { AnyDatabaseProvider, InferDatabaseClient } from "@/actor/database";
5
+ import type {
6
+ QueueFilterName,
7
+ QueueNextOptions,
8
+ QueueResultMessageForName,
9
+ } from "@/actor/instance/queue";
10
+ import type {
11
+ EventSchemaConfig,
12
+ InferEventArgs,
13
+ InferSchemaMap,
14
+ QueueSchemaConfig,
15
+ } from "@/actor/schema";
16
+ import type { WorkflowContextInterface } from "@rivetkit/workflow-engine";
17
+ import type {
18
+ BranchConfig,
19
+ BranchOutput,
20
+ EntryKindType,
21
+ LoopConfig,
22
+ LoopResult,
23
+ StepConfig,
24
+ WorkflowQueueMessage,
25
+ } from "@rivetkit/workflow-engine";
26
+ import { WORKFLOW_GUARD_KV_KEY } from "./constants";
27
+
28
+ type WorkflowActorQueueNextOptions<
29
+ TName extends string,
30
+ TCompletable extends boolean,
31
+ > = Omit<QueueNextOptions<TName, TCompletable>, "signal">;
32
+
33
+ type WorkflowActorQueueNextOptionsFallback<TCompletable extends boolean> = Omit<
34
+ QueueNextOptions<string, TCompletable>,
35
+ "signal"
36
+ >;
37
+
38
+ type ActorWorkflowLoopConfig<
39
+ S,
40
+ T,
41
+ TState,
42
+ TConnParams,
43
+ TConnState,
44
+ TVars,
45
+ TInput,
46
+ TDatabase extends AnyDatabaseProvider,
47
+ TEvents extends EventSchemaConfig,
48
+ TQueues extends QueueSchemaConfig,
49
+ > = Omit<LoopConfig<S, T>, "run"> & {
50
+ run: (
51
+ ctx: ActorWorkflowContext<
52
+ TState,
53
+ TConnParams,
54
+ TConnState,
55
+ TVars,
56
+ TInput,
57
+ TDatabase,
58
+ TEvents,
59
+ TQueues
60
+ >,
61
+ state: S,
62
+ ) => Promise<LoopResult<S, T>>;
63
+ };
64
+
65
+ type ActorWorkflowBranchConfig<
66
+ TOutput,
67
+ TState,
68
+ TConnParams,
69
+ TConnState,
70
+ TVars,
71
+ TInput,
72
+ TDatabase extends AnyDatabaseProvider,
73
+ TEvents extends EventSchemaConfig,
74
+ TQueues extends QueueSchemaConfig,
75
+ > = {
76
+ run: (
77
+ ctx: ActorWorkflowContext<
78
+ TState,
79
+ TConnParams,
80
+ TConnState,
81
+ TVars,
82
+ TInput,
83
+ TDatabase,
84
+ TEvents,
85
+ TQueues
86
+ >,
87
+ ) => Promise<TOutput>;
88
+ };
89
+
90
+ export class ActorWorkflowContext<
91
+ TState,
92
+ TConnParams,
93
+ TConnState,
94
+ TVars,
95
+ TInput,
96
+ TDatabase extends AnyDatabaseProvider,
97
+ TEvents extends EventSchemaConfig = Record<never, never>,
98
+ TQueues extends QueueSchemaConfig = Record<never, never>,
99
+ > implements WorkflowContextInterface
100
+ {
101
+ #inner: WorkflowContextInterface;
102
+ #runCtx: RunContext<
103
+ TState,
104
+ TConnParams,
105
+ TConnState,
106
+ TVars,
107
+ TInput,
108
+ TDatabase,
109
+ TEvents,
110
+ TQueues
111
+ >;
112
+ #actorAccessDepth = 0;
113
+ #allowActorAccess = false;
114
+ #guardViolation = false;
115
+
116
+ constructor(
117
+ inner: WorkflowContextInterface,
118
+ runCtx: RunContext<
119
+ TState,
120
+ TConnParams,
121
+ TConnState,
122
+ TVars,
123
+ TInput,
124
+ TDatabase,
125
+ TEvents,
126
+ TQueues
127
+ >,
128
+ ) {
129
+ this.#inner = inner;
130
+ this.#runCtx = runCtx;
131
+ }
132
+
133
+ get workflowId(): string {
134
+ return this.#inner.workflowId;
135
+ }
136
+
137
+ get abortSignal(): AbortSignal {
138
+ return this.#inner.abortSignal;
139
+ }
140
+
141
+ get queue() {
142
+ const self = this;
143
+ function next<
144
+ const TName extends QueueFilterName<TQueues>,
145
+ const TCompletable extends boolean = false,
146
+ >(
147
+ name: string,
148
+ opts?: WorkflowActorQueueNextOptions<TName, TCompletable>,
149
+ ): Promise<Array<QueueResultMessageForName<TQueues, TName, TCompletable>>>;
150
+ function next<const TCompletable extends boolean = false>(
151
+ name: string,
152
+ opts?: WorkflowActorQueueNextOptionsFallback<TCompletable>,
153
+ ): Promise<
154
+ Array<
155
+ QueueResultMessageForName<
156
+ TQueues,
157
+ QueueFilterName<TQueues>,
158
+ TCompletable
159
+ >
160
+ >
161
+ >;
162
+ async function next(
163
+ name: string,
164
+ opts?: WorkflowActorQueueNextOptions<string, boolean>,
165
+ ): Promise<Array<WorkflowQueueMessage<unknown>>> {
166
+ const messages = await self.#inner.queue.next(name, opts);
167
+ return messages.map((message) => self.#toActorQueueMessage(message));
168
+ }
169
+
170
+ function send<K extends keyof TQueues & string>(
171
+ name: K,
172
+ body: InferSchemaMap<TQueues>[K],
173
+ ): Promise<void>;
174
+ function send(
175
+ name: keyof TQueues extends never ? string : never,
176
+ body: unknown,
177
+ ): Promise<void>;
178
+ async function send(name: string, body: unknown): Promise<void> {
179
+ await self.#runCtx.queue.send(name as never, body as never);
180
+ }
181
+
182
+ return {
183
+ next,
184
+ send,
185
+ };
186
+ }
187
+
188
+ async step<T>(
189
+ nameOrConfig: string | Parameters<WorkflowContextInterface["step"]>[0],
190
+ run?: () => Promise<T>,
191
+ ): Promise<T> {
192
+ if (typeof nameOrConfig === "string") {
193
+ if (!run) {
194
+ throw new Error("Step run function missing");
195
+ }
196
+ return await this.#wrapActive(() =>
197
+ this.#inner.step(nameOrConfig, () => this.#withActorAccess(run)),
198
+ );
199
+ }
200
+ const stepConfig = nameOrConfig as StepConfig<T>;
201
+ const config: StepConfig<T> = {
202
+ ...stepConfig,
203
+ run: () => this.#withActorAccess(stepConfig.run),
204
+ };
205
+ return await this.#wrapActive(() => this.#inner.step(config));
206
+ }
207
+
208
+ async loop<T>(
209
+ name: string,
210
+ run: (
211
+ ctx: ActorWorkflowContext<
212
+ TState,
213
+ TConnParams,
214
+ TConnState,
215
+ TVars,
216
+ TInput,
217
+ TDatabase,
218
+ TEvents,
219
+ TQueues
220
+ >,
221
+ ) => Promise<LoopResult<undefined, T>>,
222
+ ): Promise<T>;
223
+ async loop<T>(
224
+ name: string,
225
+ run: (
226
+ ctx: WorkflowContextInterface,
227
+ ) => Promise<LoopResult<undefined, T>>,
228
+ ): Promise<T>;
229
+ async loop<S, T>(
230
+ config: ActorWorkflowLoopConfig<
231
+ S,
232
+ T,
233
+ TState,
234
+ TConnParams,
235
+ TConnState,
236
+ TVars,
237
+ TInput,
238
+ TDatabase,
239
+ TEvents,
240
+ TQueues
241
+ >,
242
+ ): Promise<T>;
243
+ async loop<S, T>(config: LoopConfig<S, T>): Promise<T>;
244
+ async loop(
245
+ nameOrConfig:
246
+ | string
247
+ | LoopConfig<unknown, unknown>
248
+ | ActorWorkflowLoopConfig<
249
+ unknown,
250
+ unknown,
251
+ TState,
252
+ TConnParams,
253
+ TConnState,
254
+ TVars,
255
+ TInput,
256
+ TDatabase,
257
+ TEvents,
258
+ TQueues
259
+ >,
260
+ run?: (
261
+ ctx: ActorWorkflowContext<
262
+ TState,
263
+ TConnParams,
264
+ TConnState,
265
+ TVars,
266
+ TInput,
267
+ TDatabase,
268
+ TEvents,
269
+ TQueues
270
+ >,
271
+ ) => Promise<LoopResult<undefined, unknown>>,
272
+ ): Promise<unknown> {
273
+ if (typeof nameOrConfig === "string") {
274
+ if (!run) {
275
+ throw new Error("Loop run function missing");
276
+ }
277
+ return await this.#wrapActive(() =>
278
+ this.#inner.loop(nameOrConfig, async (ctx) =>
279
+ run(this.#createChildContext(ctx)),
280
+ ),
281
+ );
282
+ }
283
+ const wrapped: LoopConfig<unknown, unknown> = {
284
+ ...nameOrConfig,
285
+ run: async (ctx, state) =>
286
+ nameOrConfig.run(this.#createChildContext(ctx), state),
287
+ };
288
+ return await this.#wrapActive(() => this.#inner.loop(wrapped));
289
+ }
290
+
291
+ sleep(name: string, durationMs: number): Promise<void> {
292
+ return this.#inner.sleep(name, durationMs);
293
+ }
294
+
295
+ sleepUntil(name: string, timestampMs: number): Promise<void> {
296
+ return this.#inner.sleepUntil(name, timestampMs);
297
+ }
298
+
299
+ async rollbackCheckpoint(name: string): Promise<void> {
300
+ await this.#wrapActive(() => this.#inner.rollbackCheckpoint(name));
301
+ }
302
+
303
+ async join<
304
+ T extends Record<
305
+ string,
306
+ ActorWorkflowBranchConfig<
307
+ unknown,
308
+ TState,
309
+ TConnParams,
310
+ TConnState,
311
+ TVars,
312
+ TInput,
313
+ TDatabase,
314
+ TEvents,
315
+ TQueues
316
+ >
317
+ >,
318
+ >(
319
+ name: string,
320
+ branches: T,
321
+ ): Promise<{ [K in keyof T]: Awaited<ReturnType<T[K]["run"]>> }>;
322
+ async join<T extends Record<string, BranchConfig<unknown>>>(
323
+ name: string,
324
+ branches: T,
325
+ ): Promise<{ [K in keyof T]: BranchOutput<T[K]> }>;
326
+ async join(name: string, branches: Record<string, BranchConfig<unknown>>) {
327
+ const wrappedBranches = Object.fromEntries(
328
+ Object.entries(branches).map(([key, branch]) => [
329
+ key,
330
+ {
331
+ run: async (ctx: WorkflowContextInterface) =>
332
+ branch.run(this.#createChildContext(ctx)),
333
+ },
334
+ ]),
335
+ ) as Record<string, BranchConfig<unknown>>;
336
+ return await this.#wrapActive(() =>
337
+ this.#inner.join(name, wrappedBranches),
338
+ );
339
+ }
340
+
341
+ async race<T>(
342
+ name: string,
343
+ branches: Array<{
344
+ name: string;
345
+ run: (
346
+ ctx: ActorWorkflowContext<
347
+ TState,
348
+ TConnParams,
349
+ TConnState,
350
+ TVars,
351
+ TInput,
352
+ TDatabase,
353
+ TEvents,
354
+ TQueues
355
+ >,
356
+ ) => Promise<T>;
357
+ }>,
358
+ ): Promise<{ winner: string; value: T }>;
359
+ async race<T>(
360
+ name: string,
361
+ branches: Array<{
362
+ name: string;
363
+ run: (ctx: WorkflowContextInterface) => Promise<T>;
364
+ }>,
365
+ ): Promise<{ winner: string; value: T }> {
366
+ const wrappedBranches = branches.map((branch) => ({
367
+ name: branch.name,
368
+ run: (ctx: WorkflowContextInterface) =>
369
+ branch.run(this.#createChildContext(ctx)),
370
+ }));
371
+ return (await this.#wrapActive(() =>
372
+ this.#inner.race(name, wrappedBranches),
373
+ )) as { winner: string; value: T };
374
+ }
375
+
376
+ async removed(name: string, originalType: EntryKindType): Promise<void> {
377
+ await this.#wrapActive(() => this.#inner.removed(name, originalType));
378
+ }
379
+
380
+ isEvicted(): boolean {
381
+ return this.#inner.isEvicted();
382
+ }
383
+
384
+ get state(): TState extends never ? never : TState {
385
+ this.#ensureActorAccess("state");
386
+ return this.#runCtx.state as TState extends never ? never : TState;
387
+ }
388
+
389
+ get vars(): TVars extends never ? never : TVars {
390
+ this.#ensureActorAccess("vars");
391
+ return this.#runCtx.vars as TVars extends never ? never : TVars;
392
+ }
393
+
394
+ client<R extends Registry<any>>(): Client<R> {
395
+ this.#ensureActorAccess("client");
396
+ return this.#runCtx.client<R>();
397
+ }
398
+
399
+ get db(): TDatabase extends never ? never : InferDatabaseClient<TDatabase> {
400
+ this.#ensureActorAccess("db");
401
+ return this.#runCtx.db as TDatabase extends never
402
+ ? never
403
+ : InferDatabaseClient<TDatabase>;
404
+ }
405
+
406
+ get log() {
407
+ return this.#runCtx.log;
408
+ }
409
+
410
+ keepAwake<T>(promise: Promise<T>): Promise<T> {
411
+ return this.#runCtx.keepAwake(promise);
412
+ }
413
+
414
+ waitUntil(promise: Promise<void>): void {
415
+ this.#runCtx.waitUntil(promise);
416
+ }
417
+
418
+ get actorId(): string {
419
+ return this.#runCtx.actorId;
420
+ }
421
+
422
+ broadcast<K extends keyof TEvents & string>(
423
+ name: K,
424
+ ...args: InferEventArgs<InferSchemaMap<TEvents>[K]>
425
+ ): void;
426
+ broadcast(
427
+ name: keyof TEvents extends never ? string : never,
428
+ ...args: Array<unknown>
429
+ ): void;
430
+ broadcast(name: string, ...args: Array<unknown>): void {
431
+ this.#runCtx.broadcast(
432
+ name as never,
433
+ ...((args as unknown[]) as never[]),
434
+ );
435
+ }
436
+
437
+ #toActorQueueMessage<T>(
438
+ message: WorkflowQueueMessage<T>,
439
+ ): WorkflowQueueMessage<T> & { id: bigint } {
440
+ let id: bigint;
441
+ try {
442
+ id = BigInt(message.id);
443
+ } catch {
444
+ throw new Error(`Invalid queue message id "${message.id}"`);
445
+ }
446
+ return {
447
+ id,
448
+ name: message.name,
449
+ body: message.body,
450
+ createdAt: message.createdAt,
451
+ ...(message.complete ? { complete: message.complete } : {}),
452
+ };
453
+ }
454
+
455
+ async #wrapActive<T>(run: () => Promise<T>): Promise<T> {
456
+ return await this.#runCtx.keepAwake(run());
457
+ }
458
+
459
+ async #withActorAccess<T>(run: () => Promise<T>): Promise<T> {
460
+ this.#actorAccessDepth++;
461
+ if (this.#actorAccessDepth === 1) {
462
+ this.#allowActorAccess = true;
463
+ }
464
+ try {
465
+ return await run();
466
+ } finally {
467
+ this.#actorAccessDepth--;
468
+ if (this.#actorAccessDepth === 0) {
469
+ this.#allowActorAccess = false;
470
+ }
471
+ }
472
+ }
473
+
474
+ #ensureActorAccess(feature: string): void {
475
+ if (!this.#allowActorAccess) {
476
+ this.#guardViolation = true;
477
+ this.#markGuardTriggered();
478
+ throw new Error(
479
+ `${feature} is only available inside workflow steps`,
480
+ );
481
+ }
482
+ }
483
+
484
+ consumeGuardViolation(): boolean {
485
+ const violated = this.#guardViolation;
486
+ this.#guardViolation = false;
487
+ return violated;
488
+ }
489
+
490
+ #markGuardTriggered(): void {
491
+ try {
492
+ const state = this.#runCtx.state as Record<string, unknown>;
493
+ if (
494
+ state &&
495
+ typeof state === "object" &&
496
+ "guardTriggered" in state
497
+ ) {
498
+ (state as Record<string, unknown>).guardTriggered = true;
499
+ }
500
+ } catch {
501
+ // Ignore if state is unavailable
502
+ }
503
+
504
+ this.#runCtx.waitUntil(
505
+ (async () => {
506
+ try {
507
+ await this.#runCtx.kv.put(WORKFLOW_GUARD_KV_KEY, "true");
508
+ } catch (error) {
509
+ this.#runCtx.log.error({
510
+ msg: "failed to persist workflow guard flag",
511
+ error,
512
+ });
513
+ }
514
+ })(),
515
+ );
516
+ }
517
+
518
+ #createChildContext(
519
+ ctx: WorkflowContextInterface,
520
+ ): ActorWorkflowContext<
521
+ TState,
522
+ TConnParams,
523
+ TConnState,
524
+ TVars,
525
+ TInput,
526
+ TDatabase,
527
+ TEvents,
528
+ TQueues
529
+ > {
530
+ return new ActorWorkflowContext(ctx, this.#runCtx);
531
+ }
532
+ }
@@ -0,0 +1,191 @@
1
+ import type { RunContext } from "@/actor/contexts/run";
2
+ import type { AnyActorInstance } from "@/actor/instance/mod";
3
+ import { makeWorkflowKey, workflowStoragePrefix } from "@/actor/instance/keys";
4
+ import type {
5
+ EngineDriver,
6
+ KVEntry,
7
+ KVWrite,
8
+ Message,
9
+ WorkflowMessageDriver,
10
+ } from "@rivetkit/workflow-engine";
11
+
12
+ const WORKFLOW_STORAGE_PREFIX = workflowStoragePrefix();
13
+
14
+ function stripWorkflowKey(prefixed: Uint8Array): Uint8Array {
15
+ return prefixed.slice(WORKFLOW_STORAGE_PREFIX.length);
16
+ }
17
+
18
+ class ActorWorkflowMessageDriver implements WorkflowMessageDriver {
19
+ #actor: AnyActorInstance;
20
+ #runCtx: RunContext<any, any, any, any, any, any, any, any>;
21
+
22
+ constructor(
23
+ actor: AnyActorInstance,
24
+ runCtx: RunContext<any, any, any, any, any, any, any, any>,
25
+ ) {
26
+ this.#actor = actor;
27
+ this.#runCtx = runCtx;
28
+ }
29
+
30
+ async addMessage(message: Message): Promise<void> {
31
+ await this.#runCtx.keepAwake(
32
+ this.#actor.queueManager.enqueue(message.name, message.data),
33
+ );
34
+ }
35
+
36
+ async receiveMessages(opts: {
37
+ names?: readonly string[];
38
+ count: number;
39
+ completable: boolean;
40
+ }): Promise<Message[]> {
41
+ const messages = await this.#runCtx.keepAwake(
42
+ this.#actor.queueManager.receive(
43
+ opts.names && opts.names.length > 0 ? [...opts.names] : undefined,
44
+ opts.count,
45
+ 0,
46
+ undefined,
47
+ opts.completable,
48
+ ),
49
+ );
50
+ return messages.map((message) => ({
51
+ id: message.id.toString(),
52
+ name: message.name,
53
+ data: message.body,
54
+ sentAt: message.createdAt,
55
+ ...(opts.completable
56
+ ? {
57
+ complete: async (response?: unknown) => {
58
+ await this.#runCtx.keepAwake(
59
+ this.#actor.queueManager.completeMessage(
60
+ message,
61
+ response,
62
+ ),
63
+ );
64
+ },
65
+ }
66
+ : {}),
67
+ }));
68
+ }
69
+
70
+ async completeMessage(messageId: string, response?: unknown): Promise<void> {
71
+ let parsedId: bigint;
72
+ try {
73
+ parsedId = BigInt(messageId);
74
+ } catch {
75
+ return;
76
+ }
77
+
78
+ await this.#runCtx.keepAwake(
79
+ this.#actor.queueManager.completeMessageById(parsedId, response),
80
+ );
81
+ }
82
+ }
83
+
84
+ export class ActorWorkflowDriver implements EngineDriver {
85
+ readonly workerPollInterval = 100;
86
+ readonly messageDriver: WorkflowMessageDriver;
87
+ #actor: AnyActorInstance;
88
+ #runCtx: RunContext<any, any, any, any, any, any, any, any>;
89
+
90
+ constructor(
91
+ actor: AnyActorInstance,
92
+ runCtx: RunContext<any, any, any, any, any, any, any, any>,
93
+ ) {
94
+ this.#actor = actor;
95
+ this.#runCtx = runCtx;
96
+ this.messageDriver = new ActorWorkflowMessageDriver(actor, runCtx);
97
+ }
98
+
99
+ async get(key: Uint8Array): Promise<Uint8Array | null> {
100
+ const [value] = await this.#runCtx.keepAwake(
101
+ this.#actor.driver.kvBatchGet(this.#actor.id, [
102
+ makeWorkflowKey(key),
103
+ ]),
104
+ );
105
+ return value ?? null;
106
+ }
107
+
108
+ async set(key: Uint8Array, value: Uint8Array): Promise<void> {
109
+ await this.#runCtx.keepAwake(
110
+ this.#actor.driver.kvBatchPut(this.#actor.id, [
111
+ [makeWorkflowKey(key), value],
112
+ ]),
113
+ );
114
+ }
115
+
116
+ async delete(key: Uint8Array): Promise<void> {
117
+ await this.#runCtx.keepAwake(
118
+ this.#actor.driver.kvBatchDelete(this.#actor.id, [
119
+ makeWorkflowKey(key),
120
+ ]),
121
+ );
122
+ }
123
+
124
+ async deletePrefix(prefix: Uint8Array): Promise<void> {
125
+ const entries = await this.#runCtx.keepAwake(
126
+ this.#actor.driver.kvListPrefix(
127
+ this.#actor.id,
128
+ makeWorkflowKey(prefix),
129
+ ),
130
+ );
131
+ if (entries.length === 0) {
132
+ return;
133
+ }
134
+ await this.#runCtx.keepAwake(
135
+ this.#actor.driver.kvBatchDelete(
136
+ this.#actor.id,
137
+ entries.map(([key]) => key),
138
+ ),
139
+ );
140
+ }
141
+
142
+ async list(prefix: Uint8Array): Promise<KVEntry[]> {
143
+ const entries = await this.#runCtx.keepAwake(
144
+ this.#actor.driver.kvListPrefix(
145
+ this.#actor.id,
146
+ makeWorkflowKey(prefix),
147
+ ),
148
+ );
149
+ return entries.map(([key, value]) => ({
150
+ key: stripWorkflowKey(key),
151
+ value,
152
+ }));
153
+ }
154
+
155
+ async batch(writes: KVWrite[]): Promise<void> {
156
+ if (writes.length === 0) return;
157
+
158
+ // Flush actor state together with workflow state to ensure atomicity.
159
+ // If the server crashes after workflow flush, actor state must also be persisted.
160
+ await this.#runCtx.keepAwake(
161
+ Promise.all([
162
+ this.#actor.driver.kvBatchPut(
163
+ this.#actor.id,
164
+ writes.map(({ key, value }) => [makeWorkflowKey(key), value]),
165
+ ),
166
+ this.#actor.stateManager.saveState({ immediate: true }),
167
+ ]),
168
+ );
169
+ }
170
+
171
+ async setAlarm(_workflowId: string, wakeAt: number): Promise<void> {
172
+ await this.#runCtx.keepAwake(
173
+ this.#actor.driver.setAlarm(this.#actor, wakeAt),
174
+ );
175
+ }
176
+
177
+ async clearAlarm(_workflowId: string): Promise<void> {
178
+ // No dedicated clear alarm support in actor drivers.
179
+ return;
180
+ }
181
+
182
+ waitForMessages(
183
+ messageNames: string[],
184
+ abortSignal: AbortSignal,
185
+ ): Promise<void> {
186
+ return this.#actor.queueManager.waitForNames(
187
+ messageNames.length > 0 ? messageNames : undefined,
188
+ abortSignal,
189
+ );
190
+ }
191
+ }