@temelj/workflow 0.0.0-alpha.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 (393) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -0
  3. package/dist/attributes.d.mts +15 -0
  4. package/dist/attributes.mjs +30 -0
  5. package/dist/client/create.d.mts +31 -0
  6. package/dist/client/create.mjs +67 -0
  7. package/dist/client/facades.d.mts +21 -0
  8. package/dist/client/facades.mjs +142 -0
  9. package/dist/client/mod.d.mts +2 -0
  10. package/dist/client/mod.mjs +2 -0
  11. package/dist/client/run-handle.d.mts +66 -0
  12. package/dist/client/run-handle.mjs +156 -0
  13. package/dist/client/worker-options.d.mts +17 -0
  14. package/dist/client/worker-options.mjs +25 -0
  15. package/dist/collection.d.mts +15 -0
  16. package/dist/collection.mjs +33 -0
  17. package/dist/compiled-function.d.mts +27 -0
  18. package/dist/compiled-function.mjs +54 -0
  19. package/dist/definition.d.mts +37 -0
  20. package/dist/definition.mjs +83 -0
  21. package/dist/engine/child-workflow.d.mts +10 -0
  22. package/dist/engine/child-workflow.mjs +333 -0
  23. package/dist/engine/command-identity.d.mts +9 -0
  24. package/dist/engine/command-identity.mjs +14 -0
  25. package/dist/engine/create.d.mts +106 -0
  26. package/dist/engine/create.mjs +402 -0
  27. package/dist/engine/deterministic.d.mts +12 -0
  28. package/dist/engine/deterministic.mjs +102 -0
  29. package/dist/engine/durable-command.d.mts +10 -0
  30. package/dist/engine/durable-command.mjs +17 -0
  31. package/dist/engine/execution-api.d.mts +29 -0
  32. package/dist/engine/execution-api.mjs +33 -0
  33. package/dist/engine/execution-service.d.mts +41 -0
  34. package/dist/engine/execution-service.mjs +288 -0
  35. package/dist/engine/hooks.d.mts +56 -0
  36. package/dist/engine/hooks.mjs +237 -0
  37. package/dist/engine/inspection-api.d.mts +34 -0
  38. package/dist/engine/inspection-api.mjs +77 -0
  39. package/dist/engine/inspection.d.mts +18 -0
  40. package/dist/engine/inspection.mjs +149 -0
  41. package/dist/engine/lifecycle.d.mts +18 -0
  42. package/dist/engine/lifecycle.mjs +38 -0
  43. package/dist/engine/lock-replay.d.mts +30 -0
  44. package/dist/engine/lock-replay.mjs +38 -0
  45. package/dist/engine/lock.d.mts +21 -0
  46. package/dist/engine/lock.mjs +197 -0
  47. package/dist/engine/message.d.mts +47 -0
  48. package/dist/engine/message.mjs +357 -0
  49. package/dist/engine/mod.d.mts +4 -0
  50. package/dist/engine/mod.mjs +2 -0
  51. package/dist/engine/observation.d.mts +35 -0
  52. package/dist/engine/observation.mjs +201 -0
  53. package/dist/engine/recovery.d.mts +26 -0
  54. package/dist/engine/recovery.mjs +92 -0
  55. package/dist/engine/replay.d.mts +41 -0
  56. package/dist/engine/replay.mjs +122 -0
  57. package/dist/engine/retry-policy.d.mts +4 -0
  58. package/dist/engine/retry-policy.mjs +4 -0
  59. package/dist/engine/retry.d.mts +23 -0
  60. package/dist/engine/retry.mjs +75 -0
  61. package/dist/engine/run-data.d.mts +9 -0
  62. package/dist/engine/run-data.mjs +74 -0
  63. package/dist/engine/run-service.d.mts +38 -0
  64. package/dist/engine/run-service.mjs +133 -0
  65. package/dist/engine/run-utils.d.mts +16 -0
  66. package/dist/engine/run-utils.mjs +83 -0
  67. package/dist/engine/schedule.d.mts +25 -0
  68. package/dist/engine/schedule.mjs +207 -0
  69. package/dist/engine/serialization.d.mts +9 -0
  70. package/dist/engine/serialization.mjs +85 -0
  71. package/dist/engine/start-api.d.mts +30 -0
  72. package/dist/engine/start-api.mjs +98 -0
  73. package/dist/engine/state.d.mts +77 -0
  74. package/dist/engine/state.mjs +464 -0
  75. package/dist/engine/step-api.d.mts +9 -0
  76. package/dist/engine/step-api.mjs +25 -0
  77. package/dist/engine/stream.d.mts +55 -0
  78. package/dist/engine/stream.mjs +333 -0
  79. package/dist/engine/summary/active-events.d.mts +34 -0
  80. package/dist/engine/summary/active-events.mjs +69 -0
  81. package/dist/engine/summary/child-workflow-runs.d.mts +24 -0
  82. package/dist/engine/summary/child-workflow-runs.mjs +138 -0
  83. package/dist/engine/summary/child-workflow-waits.d.mts +21 -0
  84. package/dist/engine/summary/child-workflow-waits.mjs +67 -0
  85. package/dist/engine/summary/recovery-child-workflow-waits.d.mts +11 -0
  86. package/dist/engine/summary/recovery-child-workflow-waits.mjs +89 -0
  87. package/dist/engine/summary/recovery-due-runs.d.mts +16 -0
  88. package/dist/engine/summary/recovery-due-runs.mjs +52 -0
  89. package/dist/engine/summary/recovery-failed-runs.d.mts +14 -0
  90. package/dist/engine/summary/recovery-failed-runs.mjs +33 -0
  91. package/dist/engine/summary/recovery-lease-runs.d.mts +16 -0
  92. package/dist/engine/summary/recovery-lease-runs.mjs +69 -0
  93. package/dist/engine/summary/recovery-locks.d.mts +18 -0
  94. package/dist/engine/summary/recovery-locks.mjs +100 -0
  95. package/dist/engine/summary/recovery-message-waits.d.mts +11 -0
  96. package/dist/engine/summary/recovery-message-waits.mjs +74 -0
  97. package/dist/engine/summary/recovery-retry-runs.d.mts +17 -0
  98. package/dist/engine/summary/recovery-retry-runs.mjs +65 -0
  99. package/dist/engine/summary/recovery-run-timestamps.d.mts +13 -0
  100. package/dist/engine/summary/recovery-run-timestamps.mjs +18 -0
  101. package/dist/engine/summary/recovery-runs.d.mts +23 -0
  102. package/dist/engine/summary/recovery-runs.mjs +54 -0
  103. package/dist/engine/summary/recovery-schedules.d.mts +10 -0
  104. package/dist/engine/summary/recovery-schedules.mjs +46 -0
  105. package/dist/engine/summary/recovery-sleep-waits.d.mts +10 -0
  106. package/dist/engine/summary/recovery-sleep-waits.mjs +53 -0
  107. package/dist/engine/summary/recovery-waits.d.mts +21 -0
  108. package/dist/engine/summary/recovery-waits.mjs +29 -0
  109. package/dist/engine/summary/run-availability.d.mts +13 -0
  110. package/dist/engine/summary/run-availability.mjs +37 -0
  111. package/dist/engine/summary/run-cleanup.d.mts +14 -0
  112. package/dist/engine/summary/run-cleanup.mjs +44 -0
  113. package/dist/engine/summary/run-failure.d.mts +14 -0
  114. package/dist/engine/summary/run-failure.mjs +62 -0
  115. package/dist/engine/summary/run-hooks.d.mts +14 -0
  116. package/dist/engine/summary/run-hooks.mjs +63 -0
  117. package/dist/engine/summary/run-lease.d.mts +11 -0
  118. package/dist/engine/summary/run-lease.mjs +64 -0
  119. package/dist/engine/summary/run-message-waits.d.mts +15 -0
  120. package/dist/engine/summary/run-message-waits.mjs +62 -0
  121. package/dist/engine/summary/run-metrics.d.mts +31 -0
  122. package/dist/engine/summary/run-metrics.mjs +93 -0
  123. package/dist/engine/summary/run-retry.d.mts +9 -0
  124. package/dist/engine/summary/run-retry.mjs +69 -0
  125. package/dist/engine/summary/run-sleeps.d.mts +15 -0
  126. package/dist/engine/summary/run-sleeps.mjs +61 -0
  127. package/dist/engine/summary/run-state.d.mts +26 -0
  128. package/dist/engine/summary/run-state.mjs +48 -0
  129. package/dist/engine/summary/run-status.d.mts +16 -0
  130. package/dist/engine/summary/run-status.mjs +35 -0
  131. package/dist/engine/summary/run-streams.d.mts +14 -0
  132. package/dist/engine/summary/run-streams.mjs +46 -0
  133. package/dist/engine/summary/run-transition.d.mts +9 -0
  134. package/dist/engine/summary/run-transition.mjs +39 -0
  135. package/dist/engine/summary/run-workflow.d.mts +9 -0
  136. package/dist/engine/summary/run-workflow.mjs +47 -0
  137. package/dist/engine/summary/step-attempt-active-child-workflows.d.mts +13 -0
  138. package/dist/engine/summary/step-attempt-active-child-workflows.mjs +55 -0
  139. package/dist/engine/summary/step-attempt-active-fields.d.mts +23 -0
  140. package/dist/engine/summary/step-attempt-active-fields.mjs +14 -0
  141. package/dist/engine/summary/step-attempt-active-message-waits.d.mts +12 -0
  142. package/dist/engine/summary/step-attempt-active-message-waits.mjs +47 -0
  143. package/dist/engine/summary/step-attempt-active-sleeps.d.mts +19 -0
  144. package/dist/engine/summary/step-attempt-active-sleeps.mjs +44 -0
  145. package/dist/engine/summary/step-attempt-buckets.d.mts +22 -0
  146. package/dist/engine/summary/step-attempt-buckets.mjs +152 -0
  147. package/dist/engine/summary/step-attempt-failure.d.mts +12 -0
  148. package/dist/engine/summary/step-attempt-failure.mjs +61 -0
  149. package/dist/engine/summary/step-attempt-lifecycle.d.mts +13 -0
  150. package/dist/engine/summary/step-attempt-lifecycle.mjs +19 -0
  151. package/dist/engine/summary/step-attempts.d.mts +11 -0
  152. package/dist/engine/summary/step-attempts.mjs +43 -0
  153. package/dist/engine/summary.d.mts +32 -0
  154. package/dist/engine/summary.mjs +37 -0
  155. package/dist/engine/task.d.mts +6 -0
  156. package/dist/engine/task.mjs +207 -0
  157. package/dist/errors/base.d.mts +99 -0
  158. package/dist/errors/base.mjs +426 -0
  159. package/dist/errors/capability.d.mts +11 -0
  160. package/dist/errors/capability.mjs +23 -0
  161. package/dist/errors/definition.d.mts +21 -0
  162. package/dist/errors/definition.mjs +51 -0
  163. package/dist/errors/details.d.mts +13 -0
  164. package/dist/errors/details.mjs +65 -0
  165. package/dist/errors/failures.d.mts +81 -0
  166. package/dist/errors/failures.mjs +168 -0
  167. package/dist/errors/hook.d.mts +14 -0
  168. package/dist/errors/hook.mjs +30 -0
  169. package/dist/errors/implementation.d.mts +11 -0
  170. package/dist/errors/implementation.mjs +25 -0
  171. package/dist/errors/lock.d.mts +28 -0
  172. package/dist/errors/lock.mjs +70 -0
  173. package/dist/errors/message.d.mts +106 -0
  174. package/dist/errors/message.mjs +122 -0
  175. package/dist/errors/mod.d.mts +17 -0
  176. package/dist/errors/mod.mjs +17 -0
  177. package/dist/errors/record-view.d.mts +11 -0
  178. package/dist/errors/record-view.mjs +24 -0
  179. package/dist/errors/records.d.mts +7 -0
  180. package/dist/errors/records.mjs +280 -0
  181. package/dist/errors/run.d.mts +116 -0
  182. package/dist/errors/run.mjs +232 -0
  183. package/dist/errors/runtime.d.mts +12 -0
  184. package/dist/errors/runtime.mjs +24 -0
  185. package/dist/errors/schedule.d.mts +27 -0
  186. package/dist/errors/schedule.mjs +65 -0
  187. package/dist/errors/step.d.mts +76 -0
  188. package/dist/errors/step.mjs +113 -0
  189. package/dist/errors/stream.d.mts +24 -0
  190. package/dist/errors/stream.mjs +64 -0
  191. package/dist/esbuild.d.mts +7 -0
  192. package/dist/esbuild.mjs +6 -0
  193. package/dist/events/descriptors.d.mts +245 -0
  194. package/dist/events/descriptors.mjs +405 -0
  195. package/dist/events/mod.d.mts +3 -0
  196. package/dist/events/mod.mjs +2 -0
  197. package/dist/history/attempt-key.d.mts +6 -0
  198. package/dist/history/attempt-key.mjs +7 -0
  199. package/dist/history/mod.d.mts +71 -0
  200. package/dist/history/mod.mjs +195 -0
  201. package/dist/history/projection.d.mts +8 -0
  202. package/dist/history/projection.mjs +259 -0
  203. package/dist/history/replay-projection.d.mts +88 -0
  204. package/dist/history/replay-projection.mjs +369 -0
  205. package/dist/history/sequencer.d.mts +16 -0
  206. package/dist/history/sequencer.mjs +78 -0
  207. package/dist/history/step-attempts.d.mts +9 -0
  208. package/dist/history/step-attempts.mjs +50 -0
  209. package/dist/history/timeline.d.mts +16 -0
  210. package/dist/history/timeline.mjs +272 -0
  211. package/dist/history/validator.d.mts +9 -0
  212. package/dist/history/validator.mjs +110 -0
  213. package/dist/inspection/mod.d.mts +5 -0
  214. package/dist/inspection/mod.mjs +3 -0
  215. package/dist/message-options.d.mts +8 -0
  216. package/dist/message-options.mjs +25 -0
  217. package/dist/mod.d.mts +52 -0
  218. package/dist/mod.mjs +26 -0
  219. package/dist/otel/support.d.mts +18 -0
  220. package/dist/otel/support.mjs +102 -0
  221. package/dist/otel.d.mts +30 -0
  222. package/dist/otel.mjs +588 -0
  223. package/dist/pagination.d.mts +25 -0
  224. package/dist/pagination.mjs +73 -0
  225. package/dist/registry.d.mts +20 -0
  226. package/dist/registry.mjs +48 -0
  227. package/dist/rolldown.d.mts +7 -0
  228. package/dist/rolldown.mjs +6 -0
  229. package/dist/rollup.d.mts +7 -0
  230. package/dist/rollup.mjs +6 -0
  231. package/dist/rspack.d.mts +7 -0
  232. package/dist/rspack.mjs +6 -0
  233. package/dist/runtime.d.mts +34 -0
  234. package/dist/runtime.mjs +194 -0
  235. package/dist/step-definition.d.mts +10 -0
  236. package/dist/step-definition.mjs +15 -0
  237. package/dist/store/append-projection.d.mts +15 -0
  238. package/dist/store/append-projection.mjs +26 -0
  239. package/dist/store/capabilities.d.mts +14 -0
  240. package/dist/store/capabilities.mjs +42 -0
  241. package/dist/store/cleanup.d.mts +18 -0
  242. package/dist/store/cleanup.mjs +338 -0
  243. package/dist/store/compaction.d.mts +5 -0
  244. package/dist/store/compaction.mjs +8 -0
  245. package/dist/store/create.d.mts +52 -0
  246. package/dist/store/create.mjs +159 -0
  247. package/dist/store/event-append-project.d.mts +15 -0
  248. package/dist/store/event-append-project.mjs +77 -0
  249. package/dist/store/event-append.d.mts +37 -0
  250. package/dist/store/event-append.mjs +80 -0
  251. package/dist/store/event-history.d.mts +22 -0
  252. package/dist/store/event-history.mjs +75 -0
  253. package/dist/store/event.d.mts +10 -0
  254. package/dist/store/event.mjs +28 -0
  255. package/dist/store/idempotency.d.mts +10 -0
  256. package/dist/store/idempotency.mjs +167 -0
  257. package/dist/store/instrumentation.d.mts +7 -0
  258. package/dist/store/instrumentation.mjs +123 -0
  259. package/dist/store/lock.d.mts +8 -0
  260. package/dist/store/lock.mjs +28 -0
  261. package/dist/store/materialization.d.mts +30 -0
  262. package/dist/store/materialization.mjs +127 -0
  263. package/dist/store/message-index.d.mts +21 -0
  264. package/dist/store/message-index.mjs +121 -0
  265. package/dist/store/mod.d.mts +5 -0
  266. package/dist/store/mod.mjs +4 -0
  267. package/dist/store/query.d.mts +24 -0
  268. package/dist/store/query.mjs +82 -0
  269. package/dist/store/revision.d.mts +4 -0
  270. package/dist/store/revision.mjs +7 -0
  271. package/dist/store/run.d.mts +49 -0
  272. package/dist/store/run.mjs +236 -0
  273. package/dist/store/schedule.d.mts +12 -0
  274. package/dist/store/schedule.mjs +41 -0
  275. package/dist/store/schema.d.mts +14 -0
  276. package/dist/store/schema.mjs +17 -0
  277. package/dist/store/step-attempt.d.mts +14 -0
  278. package/dist/store/step-attempt.mjs +23 -0
  279. package/dist/store/storage-value.d.mts +5 -0
  280. package/dist/store/storage-value.mjs +6 -0
  281. package/dist/store/validation.d.mts +35 -0
  282. package/dist/store/validation.mjs +77 -0
  283. package/dist/store-keys.d.mts +49 -0
  284. package/dist/store-keys.mjs +63 -0
  285. package/dist/telemetry.d.mts +132 -0
  286. package/dist/telemetry.mjs +172 -0
  287. package/dist/temporal.d.mts +23 -0
  288. package/dist/temporal.mjs +59 -0
  289. package/dist/timer.d.mts +12 -0
  290. package/dist/timer.mjs +28 -0
  291. package/dist/types/cleanup.d.mts +112 -0
  292. package/dist/types/cleanup.mjs +22 -0
  293. package/dist/types/client.d.mts +270 -0
  294. package/dist/types/client.mjs +1 -0
  295. package/dist/types/common.d.mts +19 -0
  296. package/dist/types/common.mjs +15 -0
  297. package/dist/types/definition.d.mts +92 -0
  298. package/dist/types/definition.mjs +1 -0
  299. package/dist/types/engine-options.d.mts +76 -0
  300. package/dist/types/engine-options.mjs +1 -0
  301. package/dist/types/engine.d.mts +309 -0
  302. package/dist/types/engine.mjs +1 -0
  303. package/dist/types/error-code.d.mts +256 -0
  304. package/dist/types/error-code.mjs +131 -0
  305. package/dist/types/error.d.mts +236 -0
  306. package/dist/types/error.mjs +48 -0
  307. package/dist/types/events.d.mts +2869 -0
  308. package/dist/types/events.mjs +283 -0
  309. package/dist/types/hook.d.mts +149 -0
  310. package/dist/types/hook.mjs +66 -0
  311. package/dist/types/lock.d.mts +63 -0
  312. package/dist/types/lock.mjs +18 -0
  313. package/dist/types/message-id.d.mts +7 -0
  314. package/dist/types/message-id.mjs +5 -0
  315. package/dist/types/message.d.mts +87 -0
  316. package/dist/types/message.mjs +1 -0
  317. package/dist/types/observation.d.mts +146 -0
  318. package/dist/types/observation.mjs +1 -0
  319. package/dist/types/pagination.d.mts +32 -0
  320. package/dist/types/pagination.mjs +13 -0
  321. package/dist/types/result.d.mts +62 -0
  322. package/dist/types/result.mjs +1 -0
  323. package/dist/types/retry.d.mts +48 -0
  324. package/dist/types/retry.mjs +1 -0
  325. package/dist/types/run-id.d.mts +7 -0
  326. package/dist/types/run-id.mjs +5 -0
  327. package/dist/types/run-list.d.mts +43 -0
  328. package/dist/types/run-list.mjs +1 -0
  329. package/dist/types/run-options.d.mts +86 -0
  330. package/dist/types/run-options.mjs +1 -0
  331. package/dist/types/run.d.mts +355 -0
  332. package/dist/types/run.mjs +207 -0
  333. package/dist/types/runtime.d.mts +143 -0
  334. package/dist/types/runtime.mjs +21 -0
  335. package/dist/types/schedule.d.mts +127 -0
  336. package/dist/types/schedule.mjs +48 -0
  337. package/dist/types/step-attempts.d.mts +326 -0
  338. package/dist/types/step-attempts.mjs +43 -0
  339. package/dist/types/step-id.d.mts +7 -0
  340. package/dist/types/step-id.mjs +5 -0
  341. package/dist/types/step.d.mts +183 -0
  342. package/dist/types/step.mjs +5 -0
  343. package/dist/types/store.d.mts +254 -0
  344. package/dist/types/store.mjs +1 -0
  345. package/dist/types/stream-id.d.mts +7 -0
  346. package/dist/types/stream-id.mjs +5 -0
  347. package/dist/types/stream.d.mts +126 -0
  348. package/dist/types/stream.mjs +1 -0
  349. package/dist/types/summary.d.mts +318 -0
  350. package/dist/types/summary.mjs +1 -0
  351. package/dist/types/telemetry.d.mts +74 -0
  352. package/dist/types/telemetry.mjs +9 -0
  353. package/dist/types/timeline.d.mts +237 -0
  354. package/dist/types/timeline.mjs +1 -0
  355. package/dist/types/vite.d.mts +65 -0
  356. package/dist/types/vite.mjs +1 -0
  357. package/dist/types/worker-id.d.mts +7 -0
  358. package/dist/types/worker-id.mjs +5 -0
  359. package/dist/types/worker.d.mts +94 -0
  360. package/dist/types/worker.mjs +1 -0
  361. package/dist/utility.d.mts +49 -0
  362. package/dist/utility.mjs +122 -0
  363. package/dist/vite/ast.d.mts +23 -0
  364. package/dist/vite/ast.mjs +73 -0
  365. package/dist/vite/error.d.mts +19 -0
  366. package/dist/vite/error.mjs +47 -0
  367. package/dist/vite/generated-name.d.mts +7 -0
  368. package/dist/vite/generated-name.mjs +9 -0
  369. package/dist/vite/manifest.d.mts +7 -0
  370. package/dist/vite/manifest.mjs +106 -0
  371. package/dist/vite/plugin.d.mts +7 -0
  372. package/dist/vite/plugin.mjs +190 -0
  373. package/dist/vite/safety.d.mts +12 -0
  374. package/dist/vite/safety.mjs +96 -0
  375. package/dist/vite/source-edit.d.mts +12 -0
  376. package/dist/vite/source-edit.mjs +44 -0
  377. package/dist/vite/source-map.d.mts +11 -0
  378. package/dist/vite/source-map.mjs +53 -0
  379. package/dist/vite/source.d.mts +7 -0
  380. package/dist/vite/source.mjs +14 -0
  381. package/dist/vite/transform.d.mts +7 -0
  382. package/dist/vite/transform.mjs +405 -0
  383. package/dist/vite.d.mts +6 -0
  384. package/dist/vite.mjs +5 -0
  385. package/dist/webhook.d.mts +16 -0
  386. package/dist/webhook.mjs +17 -0
  387. package/dist/webpack.d.mts +7 -0
  388. package/dist/webpack.mjs +6 -0
  389. package/dist/worker/error.d.mts +11 -0
  390. package/dist/worker/error.mjs +40 -0
  391. package/dist/worker.d.mts +18 -0
  392. package/dist/worker.mjs +427 -0
  393. package/package.json +144 -0
@@ -0,0 +1,73 @@
1
+ import { WorkflowOptionsError } from "./errors/base.mjs";
2
+ import "./errors/mod.mjs";
3
+ import { workflowCursorPayloadSchema } from "./types/pagination.mjs";
4
+ import { decodeBase64UrlString, encodeBase64UrlString } from "@temelj/string";
5
+ //#region src/pagination.ts
6
+ const CURSOR_PREFIX = "wf-page:";
7
+ const DEFAULT_PAGE_LIMIT = 100;
8
+ function pageItems(items, options, label, cursorKey) {
9
+ const pageOptions = options ?? {};
10
+ const limit = pageOptions.limit ?? DEFAULT_PAGE_LIMIT;
11
+ const cursor = decodeCursor(pageOptions.cursor, label);
12
+ const offset = cursor === void 0 ? 0 : items.findIndex((item, index) => compareCursorKeys(cursorKey(item, index), cursor) > 0);
13
+ const start = offset === -1 ? items.length : offset;
14
+ const pageItems = items.slice(start, start + limit);
15
+ const nextOffset = start + pageItems.length;
16
+ const hasMore = nextOffset < items.length;
17
+ const last = pageItems.at(-1);
18
+ return {
19
+ items: pageItems,
20
+ ...hasMore && last !== void 0 ? { nextCursor: encodeCursor({ key: [...cursorKey(last, nextOffset - 1)] }) } : {},
21
+ hasMore
22
+ };
23
+ }
24
+ function makeWorkflowRunCursorKey(run) {
25
+ return [run.createdAt.epochMilliseconds, run.id];
26
+ }
27
+ function makeWorkflowEventCursorKey(_event, index) {
28
+ return [index];
29
+ }
30
+ function makeWorkflowStepAttemptCursorKey(attempt) {
31
+ return [
32
+ attempt.startedAt.epochMilliseconds,
33
+ attempt.stepId,
34
+ attempt.attempt
35
+ ];
36
+ }
37
+ function makeScheduleCursorKey(schedule) {
38
+ return [
39
+ schedule.nextFireAt.epochMilliseconds,
40
+ schedule.createdAt.epochMilliseconds,
41
+ schedule.id
42
+ ];
43
+ }
44
+ function makeStreamChunkCursorKey(chunk) {
45
+ return [chunk.index];
46
+ }
47
+ function encodeCursor(payload) {
48
+ return `${CURSOR_PREFIX}${encodeBase64UrlString(JSON.stringify(payload))}`;
49
+ }
50
+ function decodeCursor(value, label) {
51
+ if (value === void 0) return;
52
+ if (!value.startsWith(CURSOR_PREFIX)) WorkflowOptionsError.cursorInvalid(label);
53
+ try {
54
+ const parsed = workflowCursorPayloadSchema.safeParse(JSON.parse(decodeBase64UrlString(value.slice(8))));
55
+ if (!parsed.success) WorkflowOptionsError.cursorInvalid(label);
56
+ return parsed.data.key;
57
+ } catch (error) {
58
+ if (!(error instanceof WorkflowOptionsError)) WorkflowOptionsError.cursorInvalid(label);
59
+ throw error;
60
+ }
61
+ }
62
+ function compareCursorKeys(left, right) {
63
+ const length = Math.max(left.length, right.length);
64
+ for (let index = 0; index < length; index++) {
65
+ const leftPart = left[index];
66
+ const rightPart = right[index];
67
+ const compared = leftPart === rightPart ? 0 : leftPart === void 0 ? -1 : rightPart === void 0 ? 1 : typeof leftPart === "number" && Number.isFinite(leftPart) && typeof rightPart === "number" && Number.isFinite(rightPart) ? leftPart - rightPart : String(leftPart).localeCompare(String(rightPart));
68
+ if (compared !== 0) return compared;
69
+ }
70
+ return 0;
71
+ }
72
+ //#endregion
73
+ export { makeScheduleCursorKey, makeStreamChunkCursorKey, makeWorkflowEventCursorKey, makeWorkflowRunCursorKey, makeWorkflowStepAttemptCursorKey, pageItems };
@@ -0,0 +1,20 @@
1
+ import { Registry as Registry$1, WorkflowDefinition, WorkflowHandler, WorkflowImplementation } from "./types/definition.mjs";
2
+ //#region src/registry.d.ts
3
+ /** In-memory workflow registry implementation. */
4
+ declare class Registry implements Registry$1 {
5
+ private readonly implementations;
6
+ /** Defines and registers a workflow implementation. */
7
+ readonly implementWorkflow: <TInput, TOutput, TRawInput = TInput>(definition: WorkflowDefinition<TInput, TOutput, TRawInput>, handler: WorkflowHandler<TInput, TOutput>) => WorkflowImplementation<TInput, TOutput, TRawInput>;
8
+ /** Registers a workflow implementation. */
9
+ readonly register: <TInput, TOutput, TRawInput = TInput>(implementation: WorkflowImplementation<TInput, TOutput, TRawInput>) => void;
10
+ /** Returns the workflow implementation registered for a definition. */
11
+ readonly get: <TInput, TOutput, TRawInput = TInput>(definition: WorkflowDefinition<TInput, TOutput, TRawInput>) => WorkflowImplementation<TInput, TOutput, TRawInput> | undefined;
12
+ /** Returns the workflow implementation registered for a name and optional version. */
13
+ readonly getByName: (name: string, version?: string) => WorkflowImplementation<unknown, unknown> | undefined;
14
+ /** Returns the last registered workflow implementation for a name. */
15
+ readonly latest: (name: string) => WorkflowImplementation<unknown, unknown> | undefined;
16
+ /** Returns all registered workflow implementations in registration order. */
17
+ readonly entries: () => readonly WorkflowImplementation<unknown, unknown>[];
18
+ }
19
+ //#endregion
20
+ export { Registry };
@@ -0,0 +1,48 @@
1
+ import { WorkflowDefinitionError } from "./errors/definition.mjs";
2
+ import "./errors/mod.mjs";
3
+ //#region src/registry.ts
4
+ /** In-memory workflow registry implementation. */
5
+ var Registry = class {
6
+ implementations = /* @__PURE__ */ new Map();
7
+ /** Defines and registers a workflow implementation. */
8
+ implementWorkflow = (definition, handler) => {
9
+ const implementation = {
10
+ kind: "implementation",
11
+ definition,
12
+ config: definition.config,
13
+ name: definition.name,
14
+ ...definition.version === void 0 ? {} : { version: definition.version },
15
+ handler
16
+ };
17
+ this.register(implementation);
18
+ return implementation;
19
+ };
20
+ /** Registers a workflow implementation. */
21
+ register = (implementation) => {
22
+ const key = makeWorkflowImplementationKey(implementation.name, implementation.version);
23
+ if (this.implementations.has(key)) WorkflowDefinitionError.implementationAlreadyRegistered(key);
24
+ this.implementations.set(key, implementation);
25
+ };
26
+ /** Returns the workflow implementation registered for a definition. */
27
+ get = (definition) => {
28
+ return this.implementations.get(makeWorkflowImplementationKey(definition.name, definition.version));
29
+ };
30
+ /** Returns the workflow implementation registered for a name and optional version. */
31
+ getByName = (name, version) => this.implementations.get(makeWorkflowImplementationKey(name, version));
32
+ /** Returns the last registered workflow implementation for a name. */
33
+ latest = (name) => {
34
+ const implementations = this.entries();
35
+ for (let index = implementations.length - 1; index >= 0; index--) {
36
+ const implementation = implementations[index];
37
+ if (implementation.name === name) return implementation;
38
+ }
39
+ };
40
+ /** Returns all registered workflow implementations in registration order. */
41
+ entries = () => [...this.implementations.values()];
42
+ };
43
+ /** Builds the internal registry key for a workflow implementation. */
44
+ function makeWorkflowImplementationKey(name, version) {
45
+ return version === void 0 ? name : `${name}@${version}`;
46
+ }
47
+ //#endregion
48
+ export { Registry };
@@ -0,0 +1,7 @@
1
+ import { WorkflowPluginOptions } from "./types/vite.mjs";
2
+ import { workflowPlugin } from "./vite/plugin.mjs";
3
+ //#region src/rolldown.d.ts
4
+ /** Creates the Rolldown plugin that compiles workflow and step directives. */
5
+ declare const workflow: (options?: WorkflowPluginOptions | undefined) => import("rolldown").Plugin<any>;
6
+ //#endregion
7
+ export { type WorkflowPluginOptions, workflow, workflowPlugin };
@@ -0,0 +1,6 @@
1
+ import { workflowPlugin } from "./vite/plugin.mjs";
2
+ //#region src/rolldown.ts
3
+ /** Creates the Rolldown plugin that compiles workflow and step directives. */
4
+ const workflow = workflowPlugin.rolldown;
5
+ //#endregion
6
+ export { workflow, workflowPlugin };
@@ -0,0 +1,7 @@
1
+ import { WorkflowPluginOptions } from "./types/vite.mjs";
2
+ import { workflowPlugin } from "./vite/plugin.mjs";
3
+ //#region src/rollup.d.ts
4
+ /** Creates the Rollup plugin that compiles workflow and step directives. */
5
+ declare const workflow: (options?: WorkflowPluginOptions | undefined) => import("rollup").Plugin<any>;
6
+ //#endregion
7
+ export { type WorkflowPluginOptions, workflow, workflowPlugin };
@@ -0,0 +1,6 @@
1
+ import { workflowPlugin } from "./vite/plugin.mjs";
2
+ //#region src/rollup.ts
3
+ /** Creates the Rollup plugin that compiles workflow and step directives. */
4
+ const workflow = workflowPlugin.rollup;
5
+ //#endregion
6
+ export { workflow, workflowPlugin };
@@ -0,0 +1,7 @@
1
+ import { WorkflowPluginOptions } from "./types/vite.mjs";
2
+ import { workflowPlugin } from "./vite/plugin.mjs";
3
+ //#region src/rspack.d.ts
4
+ /** Creates the Rspack plugin that compiles workflow and step directives. */
5
+ declare const workflow: (options?: WorkflowPluginOptions | undefined) => import("unplugin").RspackPluginInstance;
6
+ //#endregion
7
+ export { type WorkflowPluginOptions, workflow, workflowPlugin };
@@ -0,0 +1,6 @@
1
+ import { workflowPlugin } from "./vite/plugin.mjs";
2
+ //#region src/rspack.ts
3
+ /** Creates the Rspack plugin that compiles workflow and step directives. */
4
+ const workflow = workflowPlugin.rspack;
5
+ //#endregion
6
+ export { workflow, workflowPlugin };
@@ -0,0 +1,34 @@
1
+ import { RunId } from "./types/run-id.mjs";
2
+ import { WorkflowDefinition, WorkflowHandler, WorkflowImplementation } from "./types/definition.mjs";
3
+ import "./types/run.mjs";
4
+ import { WorkflowRunArguments, WorkflowStartOptions } from "./types/run-options.mjs";
5
+ import { CompiledWorkflowFunction, WorkflowClientWorkersApi, WorkflowRunHandle } from "./types/client.mjs";
6
+ import { CreateWorkflowRuntimeOptions, WorkflowRuntime as WorkflowRuntime$1, WorkflowRuntimeLocksApi, WorkflowRuntimeRunsApi, WorkflowRuntimeSchedulesApi } from "./types/runtime.mjs";
7
+ //#region src/runtime.d.ts
8
+ /** Creates a workflow runtime backed by a workflow client. */
9
+ declare function createWorkflowRuntime(options?: CreateWorkflowRuntimeOptions): WorkflowRuntime$1;
10
+ /** Workflow runtime implementation backed by a workflow client. */
11
+ declare class WorkflowRuntime implements WorkflowRuntime$1 {
12
+ #private;
13
+ readonly runs: WorkflowRuntimeRunsApi;
14
+ readonly schedules: WorkflowRuntimeSchedulesApi;
15
+ readonly locks: WorkflowRuntimeLocksApi;
16
+ readonly workers: WorkflowClientWorkersApi;
17
+ constructor(options?: CreateWorkflowRuntimeOptions);
18
+ close(): Promise<void>;
19
+ implementWorkflow<TInput, TOutput, TRawInput = TInput>(definition: WorkflowDefinition<TInput, TOutput, TRawInput>, handler: WorkflowHandler<TInput, TOutput>): WorkflowImplementation<TInput, TOutput, TRawInput>;
20
+ register<TInput, TOutput, TRawInput = TInput>(implementation: WorkflowImplementation<TInput, TOutput, TRawInput>): void;
21
+ start<TInput, TOutput, TRawInput = TInput>(workflow: WorkflowDefinition<TInput, TOutput, TRawInput>, ...args: WorkflowRunArguments<TRawInput, WorkflowStartOptions>): Promise<WorkflowRunHandle<TOutput>>;
22
+ start<TInput, TOutput, TRawInput = TInput>(workflow: WorkflowImplementation<TInput, TOutput, TRawInput>, ...args: WorkflowRunArguments<TRawInput, WorkflowStartOptions>): Promise<WorkflowRunHandle<TOutput>>;
23
+ start<TArgs extends readonly unknown[], TOutput>(workflow: CompiledWorkflowFunction<TArgs, TOutput>, ...args: TArgs): Promise<WorkflowRunHandle<Awaited<TOutput>>>;
24
+ }
25
+ /** Starts a workflow through an explicit runtime and returns a client run handle. */
26
+ declare function startWorkflowRuntimeRun<TInput, TOutput, TRawInput = TInput>(runtime: WorkflowRuntime, workflow: WorkflowImplementation<TInput, TOutput, TRawInput>, ...args: WorkflowRunArguments<TRawInput, WorkflowStartOptions>): Promise<WorkflowRunHandle<TOutput>>;
27
+ /** Starts a workflow through an explicit runtime and returns a client run handle. */
28
+ declare function startWorkflowRuntimeRun<TInput, TOutput, TRawInput = TInput>(runtime: WorkflowRuntime, workflow: WorkflowDefinition<TInput, TOutput, TRawInput>, ...args: WorkflowRunArguments<TRawInput, WorkflowStartOptions>): Promise<WorkflowRunHandle<TOutput>>;
29
+ /** Starts a workflow through an explicit runtime and returns a client run handle. */
30
+ declare function startWorkflowRuntimeRun<TArgs extends readonly unknown[], TOutput>(runtime: WorkflowRuntime, workflow: CompiledWorkflowFunction<TArgs, TOutput>, ...args: TArgs): Promise<WorkflowRunHandle<Awaited<TOutput>>>;
31
+ /** Returns a typed handle for an existing run through an explicit runtime. */
32
+ declare function getWorkflowRuntimeRun<TOutput = unknown>(runtime: WorkflowRuntime, runId: RunId): Promise<WorkflowRunHandle<TOutput>>;
33
+ //#endregion
34
+ export { WorkflowRuntime, createWorkflowRuntime, getWorkflowRuntimeRun, startWorkflowRuntimeRun };
@@ -0,0 +1,194 @@
1
+ import { WorkflowStateError } from "./errors/base.mjs";
2
+ import { WorkflowResultTimeoutError, WorkflowRunCanceledError, WorkflowRunNotFoundError } from "./errors/run.mjs";
3
+ import { durationTotal } from "./utility.mjs";
4
+ import { WorkflowDefinitionError } from "./errors/definition.mjs";
5
+ import { WorkflowRuntimeError } from "./errors/runtime.mjs";
6
+ import "./errors/mod.mjs";
7
+ import { getCompiledWorkflowDefinition } from "./compiled-function.mjs";
8
+ import { workflowErrorFromRecord } from "./errors/records.mjs";
9
+ import { durationBetween } from "./temporal.mjs";
10
+ import { nonNegativeTimerDelay, sleep } from "./timer.mjs";
11
+ import { Registry } from "./registry.mjs";
12
+ import { createWorkflowClient } from "./client/create.mjs";
13
+ import { parseWorkflowRuntime } from "./types/runtime.mjs";
14
+ //#region src/runtime.ts
15
+ const DEFAULT_RESULT_POLL_INTERVAL = Temporal.Duration.from({ milliseconds: 100 });
16
+ const WORKFLOW_RUNTIME_CLOCKS = /* @__PURE__ */ new WeakMap();
17
+ const WORKFLOW_RUNTIME_CLIENTS = /* @__PURE__ */ new WeakMap();
18
+ const WORKFLOW_RUNTIME_REGISTRIES = /* @__PURE__ */ new WeakMap();
19
+ /** Creates a workflow runtime backed by a workflow client. */
20
+ function createWorkflowRuntime(options = {}) {
21
+ return new WorkflowRuntime(options);
22
+ }
23
+ /** Workflow runtime implementation backed by a workflow client. */
24
+ var WorkflowRuntime = class {
25
+ runs;
26
+ schedules;
27
+ locks;
28
+ workers;
29
+ #client;
30
+ #registry;
31
+ constructor(options = {}) {
32
+ const now = options.now ?? Temporal.Now.instant;
33
+ this.#registry = options.registry ?? new Registry();
34
+ this.#client = createWorkflowClient({
35
+ ...options,
36
+ registry: this.#registry,
37
+ now
38
+ });
39
+ this.runs = {
40
+ getHandle: async (runId) => await getWorkflowRuntimeRun(this, runId),
41
+ list: async (options) => await this.#client.runs.list(options)
42
+ };
43
+ this.schedules = this.#client.schedules;
44
+ this.locks = this.#client.locks;
45
+ this.workers = this.#client.workers;
46
+ WORKFLOW_RUNTIME_CLOCKS.set(this, now);
47
+ WORKFLOW_RUNTIME_CLIENTS.set(this, this.#client);
48
+ WORKFLOW_RUNTIME_REGISTRIES.set(this, this.#registry);
49
+ }
50
+ async close() {
51
+ await this.#client.close();
52
+ }
53
+ implementWorkflow(definition, handler) {
54
+ return this.#registry.implementWorkflow(definition, handler);
55
+ }
56
+ register(implementation) {
57
+ this.#registry.register(implementation);
58
+ }
59
+ async start(workflow, ...args) {
60
+ return await startWorkflowRuntimeRunUnknown(this, workflow, ...args);
61
+ }
62
+ };
63
+ /** Starts a workflow through an explicit runtime and returns a client run handle. */
64
+ async function startWorkflowRuntimeRun(runtime, workflow, ...args) {
65
+ return await startWorkflowRuntimeRunUnknown(runtime, workflow, ...args);
66
+ }
67
+ async function startWorkflowRuntimeRunUnknown(runtime, workflow, ...args) {
68
+ parseWorkflowRuntime(runtime);
69
+ const client = getWorkflowRuntimeClient(runtime);
70
+ const registry = getWorkflowRuntimeRegistry(runtime);
71
+ if (typeof workflow === "function") {
72
+ const definition = getCompiledWorkflowDefinition(workflow);
73
+ if (definition === void 0) WorkflowDefinitionError.compiledMetadataMissing();
74
+ ensureWorkflowRegistered(registry, definition);
75
+ return await client.runs.start(definition.definition, args);
76
+ }
77
+ const target = workflow;
78
+ if (target.kind === "implementation") {
79
+ ensureWorkflowRegistered(registry, target);
80
+ return await client.runs.start(target.definition, args[0], args[1]);
81
+ }
82
+ return await client.runs.start(target, args[0], args[1]);
83
+ }
84
+ /** Returns a typed handle for an existing run through an explicit runtime. */
85
+ async function getWorkflowRuntimeRun(runtime, runId) {
86
+ parseWorkflowRuntime(runtime);
87
+ const run = await getWorkflowRuntimeClient(runtime).runs.get(runId);
88
+ if (run === void 0) WorkflowRunNotFoundError.notFound(runId);
89
+ return createStoredRunHandle(runtime, run);
90
+ }
91
+ function createStoredRunHandle(runtime, initialRun) {
92
+ const runId = initialRun.id;
93
+ const now = WORKFLOW_RUNTIME_CLOCKS.get(runtime) ?? Temporal.Now.instant;
94
+ const client = getWorkflowRuntimeClient(runtime);
95
+ const getRun = async () => {
96
+ const run = await client.runs.get(runId);
97
+ if (run === void 0) WorkflowRunNotFoundError.notFound(runId);
98
+ return run;
99
+ };
100
+ return {
101
+ runId,
102
+ workflowName: initialRun.workflowName,
103
+ ...initialRun.workflowVersion === void 0 ? {} : { workflowVersion: initialRun.workflowVersion },
104
+ getRun,
105
+ async status() {
106
+ return (await getRun()).status;
107
+ },
108
+ async events() {
109
+ return await client.runs.events(runId);
110
+ },
111
+ async timeline() {
112
+ return await client.runs.timeline(runId);
113
+ },
114
+ async result(options) {
115
+ const pollInterval = nonNegativeTimerDelay(options?.pollInterval ?? DEFAULT_RESULT_POLL_INTERVAL, "Workflow result pollInterval").clampedDelay;
116
+ const timeout = options?.timeout;
117
+ const deadlineAt = timeout === void 0 ? void 0 : now().add(timeout);
118
+ while (true) {
119
+ const run = await client.runs.get(runId);
120
+ if (run === void 0) WorkflowRunNotFoundError.notFound(runId);
121
+ if (run.status === "completed") return run.output;
122
+ if (run.status === "failed") {
123
+ if (run.error === void 0) WorkflowStateError.failedRunMissingError(runId);
124
+ throw workflowErrorFromRecord(run.error);
125
+ }
126
+ if (run.status === "canceled") WorkflowRunCanceledError.canceled(runId);
127
+ let sleepDelay = pollInterval;
128
+ if (timeout !== void 0 && deadlineAt !== void 0) {
129
+ const remainingDelay = durationTotal(durationBetween(now(), deadlineAt));
130
+ if (remainingDelay <= 0) WorkflowResultTimeoutError.timedOut(runId, timeout);
131
+ sleepDelay = Math.min(sleepDelay, remainingDelay);
132
+ }
133
+ await sleep(sleepDelay);
134
+ }
135
+ },
136
+ async getStreamInfo(streamIdOrName) {
137
+ return await client.streams.getInfo(runId, streamIdOrName);
138
+ },
139
+ followStream(streamIdOrName, options) {
140
+ return client.streams.follow(runId, streamIdOrName, options);
141
+ },
142
+ async waitForStream(streamIdOrName, options) {
143
+ return await client.streams.wait(runId, streamIdOrName, options);
144
+ },
145
+ async waitForStreamChunk(streamIdOrName, afterIndex, options) {
146
+ return await client.streams.waitForChunk(runId, streamIdOrName, afterIndex, options);
147
+ },
148
+ async waitForStreamTerminal(streamIdOrName, options) {
149
+ return await client.streams.waitForTerminal(runId, streamIdOrName, options);
150
+ },
151
+ async readStream(streamIdOrName, options) {
152
+ return await client.streams.read(runId, streamIdOrName, options);
153
+ },
154
+ async readStreamPage(streamIdOrName, options) {
155
+ return await client.streams.readPage(runId, streamIdOrName, options);
156
+ },
157
+ async reschedule(options) {
158
+ return await client.admin.reschedule(runId, options);
159
+ },
160
+ async releaseStaleLease(options) {
161
+ return await client.admin.releaseStaleLease(runId, options);
162
+ },
163
+ async retryFailed(options) {
164
+ return await client.admin.retryFailed(runId, options);
165
+ },
166
+ async markPermanentlyFailed(options) {
167
+ return await client.admin.markPermanentlyFailed(runId, options);
168
+ },
169
+ async setMetadata(metadata) {
170
+ return await client.admin.updateMetadata(runId, metadata);
171
+ },
172
+ async setAttributes(attributes) {
173
+ return await client.admin.setAttributes(runId, attributes);
174
+ },
175
+ async cancel() {
176
+ return await client.admin.cancel(runId);
177
+ }
178
+ };
179
+ }
180
+ function getWorkflowRuntimeClient(runtime) {
181
+ const client = WORKFLOW_RUNTIME_CLIENTS.get(runtime);
182
+ if (client === void 0) WorkflowRuntimeError.clientMissing();
183
+ return client;
184
+ }
185
+ function getWorkflowRuntimeRegistry(runtime) {
186
+ const registry = WORKFLOW_RUNTIME_REGISTRIES.get(runtime);
187
+ if (registry === void 0) WorkflowRuntimeError.registryMissing();
188
+ return registry;
189
+ }
190
+ function ensureWorkflowRegistered(registry, implementation) {
191
+ if (registry.get(implementation.definition) === void 0) registry.register(implementation);
192
+ }
193
+ //#endregion
194
+ export { WorkflowRuntime, createWorkflowRuntime, getWorkflowRuntimeRun, startWorkflowRuntimeRun };
@@ -0,0 +1,10 @@
1
+ import { WorkflowStepContext, WorkflowStepDefinition, WorkflowStepRunConfig } from "./types/step.mjs";
2
+ //#region src/step-definition.d.ts
3
+ type WorkflowStepHandler = (...args: never[]) => unknown;
4
+ type WorkflowStepHandlerInputTuple<THandler extends WorkflowStepHandler> = THandler extends ((...args: infer TArgs) => unknown) ? TArgs : never;
5
+ type WorkflowStepHandlerArgs<THandler extends WorkflowStepHandler> = WorkflowStepHandlerInputTuple<THandler> extends [...infer TArgs, WorkflowStepContext] ? TArgs : WorkflowStepHandlerInputTuple<THandler>;
6
+ type WorkflowStepHandlerOutput<THandler extends WorkflowStepHandler> = THandler extends ((...args: never[]) => infer TOutput) ? Awaited<TOutput> : never;
7
+ /** Defines a workflow step. */
8
+ declare function defineWorkflowStep<THandler extends WorkflowStepHandler>(config: WorkflowStepRunConfig, handler: THandler): WorkflowStepDefinition<WorkflowStepHandlerArgs<THandler>, WorkflowStepHandlerOutput<THandler>>;
9
+ //#endregion
10
+ export { defineWorkflowStep };
@@ -0,0 +1,15 @@
1
+ import { WorkflowDefinitionError } from "./errors/definition.mjs";
2
+ import "./errors/mod.mjs";
3
+ //#region src/step-definition.ts
4
+ /** Defines a workflow step. */
5
+ function defineWorkflowStep(config, handler) {
6
+ const commandId = config.commandId ?? config.name;
7
+ if (commandId === void 0) WorkflowDefinitionError.stepNameOrCommandIdRequired();
8
+ return {
9
+ config,
10
+ name: commandId,
11
+ handler
12
+ };
13
+ }
14
+ //#endregion
15
+ export { defineWorkflowStep };
@@ -0,0 +1,15 @@
1
+ import { RunId } from "../types/run-id.mjs";
2
+ import "../types/run.mjs";
3
+ import { EventRecord } from "../types/events.mjs";
4
+ import { WorkflowStorage } from "../types/store.mjs";
5
+ import { WorkflowEventAppendAtomicProjectionPlanner, WorkflowEventAppendSuccessOutcome } from "./event-append.mjs";
6
+ //#region src/store/append-projection.d.ts
7
+ interface WorkflowAppendProjectionPipeline {
8
+ readonly materializesAtomically: boolean;
9
+ readonly planAtomicProjections?: WorkflowEventAppendAtomicProjectionPlanner;
10
+ finalize(outcome: WorkflowEventAppendSuccessOutcome): Promise<readonly EventRecord[]>;
11
+ finalizePostCommit(outcome: WorkflowEventAppendSuccessOutcome): Promise<void>;
12
+ }
13
+ declare function createWorkflowAppendProjectionPipeline(storage: WorkflowStorage, namespace: string, runId: RunId, event: EventRecord): Promise<WorkflowAppendProjectionPipeline>;
14
+ //#endregion
15
+ export { WorkflowAppendProjectionPipeline, createWorkflowAppendProjectionPipeline };
@@ -0,0 +1,26 @@
1
+ import { supportsWorkflowAtomicBatchWrite } from "./capabilities.mjs";
2
+ import { createWorkflowStepAttemptMaterializationStoragePlan, materializeStepAttemptForAppendOutcome } from "./materialization.mjs";
3
+ import { projectMessageIdempotencyIndexForAppendOutcome } from "./message-index.mjs";
4
+ //#region src/store/append-projection.ts
5
+ async function createWorkflowAppendProjectionPipeline(storage, namespace, runId, event) {
6
+ if (!supportsWorkflowAtomicBatchWrite(storage)) return createWorkflowAppendProjectionPipelineFromStepAttemptPlan(storage, namespace, runId, event, void 0);
7
+ return createWorkflowAppendProjectionPipelineFromStepAttemptPlan(storage, namespace, runId, event, await createWorkflowStepAttemptMaterializationStoragePlan(storage, namespace, runId, event));
8
+ }
9
+ function createWorkflowAppendProjectionPipelineFromStepAttemptPlan(storage, namespace, runId, event, initialStepAttemptPlan) {
10
+ return {
11
+ materializesAtomically: initialStepAttemptPlan?.materializes === true,
12
+ planAtomicProjections: createWorkflowAppendAtomicProjectionPlanner(storage, namespace, runId, event, initialStepAttemptPlan),
13
+ async finalize(outcome) {
14
+ return await projectMessageIdempotencyIndexForAppendOutcome(storage, namespace, runId, event, outcome);
15
+ },
16
+ async finalizePostCommit(outcome) {
17
+ if (!supportsWorkflowAtomicBatchWrite(storage)) await materializeStepAttemptForAppendOutcome(storage, namespace, runId, event, outcome);
18
+ }
19
+ };
20
+ }
21
+ function createWorkflowAppendAtomicProjectionPlanner(storage, namespace, runId, event, initialStepAttemptPlan) {
22
+ if (!supportsWorkflowAtomicBatchWrite(storage)) return;
23
+ return async (attempt) => attempt === 0 && initialStepAttemptPlan !== void 0 ? initialStepAttemptPlan : await createWorkflowStepAttemptMaterializationStoragePlan(storage, namespace, runId, event);
24
+ }
25
+ //#endregion
26
+ export { createWorkflowAppendProjectionPipeline };
@@ -0,0 +1,14 @@
1
+ import { WorkflowStorage, WorkflowStoreCapabilities } from "../types/store.mjs";
2
+ //#region src/store/capabilities.d.ts
3
+ /** Detects which coordination capabilities are available on a workflow store backend. */
4
+ declare function getWorkflowStoreCapabilities(storage: WorkflowStorage): WorkflowStoreCapabilities;
5
+ /** Validates workflow store capabilities and throws when the requirement is not met. */
6
+ declare function requireWorkflowStoreCapabilities(store: {
7
+ readonly capabilities: WorkflowStoreCapabilities;
8
+ }, requirements: Partial<Record<keyof WorkflowStoreCapabilities, string>>): void;
9
+ /** Returns whether a storage backend can reject stale single-key writes. */
10
+ declare function supportsWorkflowConditionalWrite(storage: WorkflowStorage): boolean;
11
+ /** Returns whether a storage backend can atomically reject stale multi-key writes. */
12
+ declare function supportsWorkflowAtomicBatchWrite(storage: WorkflowStorage): boolean;
13
+ //#endregion
14
+ export { getWorkflowStoreCapabilities, requireWorkflowStoreCapabilities, supportsWorkflowAtomicBatchWrite, supportsWorkflowConditionalWrite };
@@ -0,0 +1,42 @@
1
+ import { WorkflowCapabilityError } from "../errors/capability.mjs";
2
+ import "../errors/mod.mjs";
3
+ //#region src/store/capabilities.ts
4
+ /** Detects which coordination capabilities are available on a workflow store backend. */
5
+ function getWorkflowStoreCapabilities(storage) {
6
+ const conditionalWrites = supportsWorkflowConditionalWrite(storage);
7
+ const conditionalBatches = supportsWorkflowAtomicBatchWrite(storage);
8
+ return {
9
+ conditionalWrites,
10
+ conditionalRunClaims: conditionalWrites,
11
+ conditionalRunUpdates: conditionalWrites,
12
+ conditionalRunLeaseExtensions: conditionalWrites,
13
+ conditionalRunLeaseReleases: conditionalWrites,
14
+ conditionalEventAppends: conditionalWrites,
15
+ conditionalStepAttemptMaterialization: conditionalWrites,
16
+ atomicRunCreation: conditionalBatches,
17
+ atomicEventAndStepAttemptMaterialization: conditionalBatches,
18
+ messageIdempotencyIndexes: true,
19
+ indexedRunQueries: false,
20
+ indexedRunCounts: false,
21
+ indexedClaimableRunQueries: false,
22
+ indexedStepAttemptQueries: false,
23
+ indexedScheduleQueries: false,
24
+ indexedLockQueries: false,
25
+ rollsBackEventsOnMaterializationFailure: true,
26
+ reconcilesStepAttemptsFromEvents: true
27
+ };
28
+ }
29
+ /** Validates workflow store capabilities and throws when the requirement is not met. */
30
+ function requireWorkflowStoreCapabilities(store, requirements) {
31
+ for (const [capability, label] of Object.entries(requirements)) if (store.capabilities[capability] !== true) WorkflowCapabilityError.unsupported(label ?? `workflow store ${capability}`);
32
+ }
33
+ /** Returns whether a storage backend can reject stale single-key writes. */
34
+ function supportsWorkflowConditionalWrite(storage) {
35
+ return storage.capabilities.compareAndSet;
36
+ }
37
+ /** Returns whether a storage backend can atomically reject stale multi-key writes. */
38
+ function supportsWorkflowAtomicBatchWrite(storage) {
39
+ return storage.capabilities.compareAndSetMany;
40
+ }
41
+ //#endregion
42
+ export { getWorkflowStoreCapabilities, requireWorkflowStoreCapabilities, supportsWorkflowAtomicBatchWrite, supportsWorkflowConditionalWrite };
@@ -0,0 +1,18 @@
1
+ import { RunId } from "../types/run-id.mjs";
2
+ import { CleanupMarkerRecord, CleanupRunsOptions, CleanupRunsResult } from "../types/cleanup.mjs";
3
+ import { WorkflowListRunsOptions } from "../types/run-list.mjs";
4
+ import { WorkflowRunRecord } from "../types/run.mjs";
5
+ import { EventRecord } from "../types/events.mjs";
6
+ import { WorkflowCleanupIndexKey, WorkflowStorage } from "../types/store.mjs";
7
+ //#region src/store/cleanup.d.ts
8
+ interface WorkflowCleanupRunLister {
9
+ listRuns(options: WorkflowListRunsOptions): Promise<readonly WorkflowRunRecord[]>;
10
+ }
11
+ declare function stepAttemptKeysForCleanup(storage: WorkflowStorage, namespace: string, runId: RunId): Promise<readonly string[]>;
12
+ declare function messageIdempotencyIndexesForCleanup(storage: WorkflowStorage, namespace: string, runId: RunId, events: readonly EventRecord[]): Promise<readonly WorkflowCleanupIndexKey[]>;
13
+ declare function idempotencyIndexesForCleanup(storage: WorkflowStorage, namespace: string, run: WorkflowRunRecord): Promise<readonly WorkflowCleanupIndexKey[]>;
14
+ declare function deleteCleanupIndexes(storage: WorkflowStorage, indexes: readonly WorkflowCleanupIndexKey[]): Promise<number>;
15
+ declare function cleanupRuns(storage: WorkflowStorage, namespace: string, runs: WorkflowCleanupRunLister, options: CleanupRunsOptions): Promise<CleanupRunsResult>;
16
+ declare function listCleanupMarkers(storage: WorkflowStorage, namespace: string): Promise<readonly CleanupMarkerRecord[]>;
17
+ //#endregion
18
+ export { cleanupRuns, deleteCleanupIndexes, idempotencyIndexesForCleanup, listCleanupMarkers, messageIdempotencyIndexesForCleanup, stepAttemptKeysForCleanup };