@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,236 @@
1
+ import { WorkflowStateError } from "../errors/base.mjs";
2
+ import { WorkflowRunNotFoundError } from "../errors/run.mjs";
3
+ import { isTerminalWorkflowLifecycleEventForRun, isTerminalWorkflowRunStatus, limitWorkflowItems, nextRunAttempt } from "../utility.mjs";
4
+ import { omitUndefined } from "../collection.mjs";
5
+ import "../errors/mod.mjs";
6
+ import { supportsWorkflowConditionalWrite } from "./capabilities.mjs";
7
+ import { makeRunKey } from "../store-keys.mjs";
8
+ import { isSameWorkflowStorageValue } from "./revision.mjs";
9
+ import { toStorageValue } from "./storage-value.mjs";
10
+ import { isReadableWorkflowRunRecord, parseStoredWorkflowRunRecord, requireReadableStoreRun, requireValidRunTransition } from "./validation.mjs";
11
+ import { rollbackAppendedEventIfCurrent } from "./event-history.mjs";
12
+ import { makeWorkflowRunCursorKey, pageItems } from "../pagination.mjs";
13
+ import { compactWorkflowStoreRecord } from "./compaction.mjs";
14
+ import { matchesWorkflowRunListOptions, queryWorkflowRuns } from "./query.mjs";
15
+ //#region src/store/run.ts
16
+ async function updateRunIfCurrent(storage, namespace, store, current, next) {
17
+ const runId = current.id;
18
+ const nextRunId = next.id;
19
+ if (runId !== nextRunId) WorkflowStateError.runIdMismatch(runId, nextRunId);
20
+ requireReadableStoreRun(current, namespace);
21
+ if (next.namespace !== namespace) WorkflowStateError.runNamespaceMismatch(String(next.namespace), namespace);
22
+ if (!supportsWorkflowConditionalWrite(storage)) {
23
+ const latest = await store.getRun(runId);
24
+ if (latest === void 0 || !isSameWorkflowStorageValue(omitUndefined(latest), omitUndefined(current))) return;
25
+ requireValidRunTransition(current, next);
26
+ const compact = compactWorkflowStoreRecord(next);
27
+ await storage.set(makeRunKey(namespace, runId), toStorageValue(compact));
28
+ return compact;
29
+ }
30
+ requireValidRunTransition(current, next);
31
+ const compact = compactWorkflowStoreRecord(next);
32
+ return await storage.compareAndSet(makeRunKey(namespace, runId), toStorageValue(current), toStorageValue(compact)) ? compact : void 0;
33
+ }
34
+ async function getWorkflowRun(storage, namespace, runId) {
35
+ const run = await storage.get(makeRunKey(namespace, runId));
36
+ return isReadableWorkflowRunRecord(run, namespace) ? run : void 0;
37
+ }
38
+ async function listWorkflowRuns(storage, namespace, options) {
39
+ return limitWorkflowItems(await queryWorkflowRuns(storage, namespace, options, {
40
+ label: "Workflow run list options",
41
+ sort: true
42
+ }), options?.limit);
43
+ }
44
+ async function listWorkflowRunsPage(storage, namespace, options) {
45
+ return pageItems(await queryWorkflowRuns(storage, namespace, {
46
+ ...options,
47
+ limit: void 0
48
+ }, {
49
+ label: "Workflow run page options",
50
+ sort: true
51
+ }), options, "Workflow run page", makeWorkflowRunCursorKey);
52
+ }
53
+ async function countWorkflowRuns(storage, namespace, options) {
54
+ return (await queryWorkflowRuns(storage, namespace, options, {
55
+ label: "Workflow run list options",
56
+ sort: false
57
+ })).length;
58
+ }
59
+ async function updateWorkflowRun(storage, namespace, store, run) {
60
+ const runId = run.id;
61
+ if (run.namespace !== namespace) WorkflowStateError.runNamespaceMismatch(String(run.namespace), namespace);
62
+ const current = await store.getRun(runId);
63
+ if (current !== void 0) requireValidRunTransition(current, run);
64
+ const compact = compactWorkflowStoreRecord(run);
65
+ await storage.set(makeRunKey(namespace, runId), toStorageValue(compact));
66
+ return compact;
67
+ }
68
+ async function claimWorkflowRun(storage, namespace, store, options) {
69
+ const listOptions = claimableWorkflowRunListOptions(options);
70
+ const run = options.runId === void 0 ? await firstClaimableRun(store, options, listOptions) : await store.getRun(options.runId);
71
+ if (run === void 0 || !canClaimRun(run, options, listOptions)) return;
72
+ return await updateRunIfCurrent(storage, namespace, store, run, claimedRunPatch(run, options.workerId, options.now, options.leaseDuration));
73
+ }
74
+ async function extendWorkflowRunLease(storage, namespace, store, options) {
75
+ const run = await store.getRun(options.runId);
76
+ if (!isWorkflowRunLeaseHeldByWorker(run, options)) return;
77
+ return await updateRunIfCurrent(storage, namespace, store, run, extendedRunLeasePatch(run, options.now, options.leaseDuration));
78
+ }
79
+ async function releaseWorkflowRunLease(storage, namespace, store, options) {
80
+ const run = await store.getRun(options.runId);
81
+ if (!isWorkflowRunLeaseHeldByWorker(run, options)) return;
82
+ return await updateRunIfCurrent(storage, namespace, store, run, releaseRunLeasePatch(run, options.now));
83
+ }
84
+ function isSameReadableWorkflowRunRevision(latest, current, namespace) {
85
+ return latest !== void 0 && isReadableWorkflowRunRecord(latest, namespace) && isSameWorkflowStorageValue(omitUndefined(latest), omitUndefined(current));
86
+ }
87
+ async function isStoredWorkflowRunRevisionCurrent(storage, namespace, currentRun) {
88
+ return isSameReadableWorkflowRunRevision(await getReadableStoredWorkflowRunValue(storage, namespace, currentRun.id), currentRun, namespace);
89
+ }
90
+ async function getReadableStoredWorkflowRun(storage, namespace, runId) {
91
+ const run = parseStoredWorkflowRunRecord(await storage.get(makeRunKey(namespace, runId)));
92
+ return isReadableWorkflowRunRecord(run, namespace) ? run : void 0;
93
+ }
94
+ async function getReadableStoredWorkflowRunValue(storage, namespace, runId) {
95
+ const run = await storage.get(makeRunKey(namespace, runId));
96
+ return isReadableWorkflowRunRecord(run, namespace) ? run : void 0;
97
+ }
98
+ async function requireReadableStoredWorkflowRun(storage, namespace, runId) {
99
+ const run = await getReadableStoredWorkflowRun(storage, namespace, runId);
100
+ if (run === void 0) WorkflowRunNotFoundError.notFound(runId);
101
+ return run;
102
+ }
103
+ function shouldRollbackAppendedEventForRun(namespace, run, event) {
104
+ if (run === void 0 || !isReadableWorkflowRunRecord(run, namespace)) return true;
105
+ return isTerminalWorkflowRunStatus(run.status) && !isTerminalWorkflowLifecycleEventForRun(run, event);
106
+ }
107
+ function isWorkflowRunStaleForAppend(currentRun, event) {
108
+ return isTerminalWorkflowRunStatus(currentRun.status) && !isTerminalWorkflowLifecycleEventForRun(currentRun, event);
109
+ }
110
+ async function rollbackAppendedEventIfRunStale(storage, namespace, runId, event, events) {
111
+ if (!shouldRollbackAppendedEventForRun(namespace, await getReadableStoredWorkflowRunValue(storage, namespace, runId), event)) return false;
112
+ await rollbackAppendedEventIfCurrent(storage, namespace, runId, events);
113
+ return true;
114
+ }
115
+ function claimedRunPatch(run, workerId, now, leaseDuration) {
116
+ const timestamp = now;
117
+ const lastTransitionReason = isStaleLeaseClaim(run, now) ? "reclaimed" : "claimed";
118
+ return {
119
+ ...run,
120
+ status: "running",
121
+ attempts: nextRunAttempt(run.attempts),
122
+ idempotencyKey: run.idempotencyKey,
123
+ parentRunId: run.parentRunId,
124
+ parentStepId: run.parentStepId,
125
+ parentStepName: run.parentStepName,
126
+ parentStepAttempt: run.parentStepAttempt,
127
+ workerId,
128
+ createdAt: run.createdAt,
129
+ updatedAt: timestamp,
130
+ lastTransitionAt: timestamp,
131
+ lastTransitionReason,
132
+ startedAt: run.startedAt ?? timestamp,
133
+ leaseExpiresAt: now.add(leaseDuration),
134
+ deadlineAt: run.deadlineAt,
135
+ availableAt: void 0,
136
+ retryAt: void 0,
137
+ retryReason: void 0,
138
+ retryAttempt: void 0,
139
+ retryStepId: void 0,
140
+ retryStepName: void 0,
141
+ error: void 0,
142
+ output: void 0,
143
+ finishedAt: void 0
144
+ };
145
+ }
146
+ function extendedRunLeasePatch(run, now, leaseDuration) {
147
+ const timestamp = now;
148
+ return {
149
+ ...run,
150
+ idempotencyKey: run.idempotencyKey,
151
+ parentRunId: run.parentRunId,
152
+ parentStepId: run.parentStepId,
153
+ parentStepName: run.parentStepName,
154
+ parentStepAttempt: run.parentStepAttempt,
155
+ createdAt: run.createdAt,
156
+ updatedAt: timestamp,
157
+ lastTransitionAt: run.lastTransitionAt ?? timestamp,
158
+ deadlineAt: run.deadlineAt,
159
+ startedAt: run.startedAt ?? timestamp,
160
+ leaseExpiresAt: now.add(leaseDuration),
161
+ retryAt: void 0,
162
+ retryReason: void 0,
163
+ retryAttempt: void 0,
164
+ retryStepId: void 0,
165
+ retryStepName: void 0,
166
+ error: void 0,
167
+ output: void 0,
168
+ finishedAt: void 0
169
+ };
170
+ }
171
+ function releaseRunLeasePatch(run, now) {
172
+ const timestamp = now;
173
+ return {
174
+ ...run,
175
+ status: "waiting",
176
+ idempotencyKey: run.idempotencyKey,
177
+ parentRunId: run.parentRunId,
178
+ parentStepId: run.parentStepId,
179
+ parentStepName: run.parentStepName,
180
+ parentStepAttempt: run.parentStepAttempt,
181
+ availableAt: timestamp,
182
+ createdAt: run.createdAt,
183
+ updatedAt: timestamp,
184
+ lastTransitionAt: timestamp,
185
+ lastTransitionReason: "waiting",
186
+ deadlineAt: run.deadlineAt,
187
+ startedAt: run.startedAt ?? timestamp,
188
+ retryAt: void 0,
189
+ retryReason: void 0,
190
+ retryAttempt: void 0,
191
+ retryStepId: void 0,
192
+ retryStepName: void 0,
193
+ error: void 0,
194
+ output: void 0,
195
+ finishedAt: void 0,
196
+ workerId: void 0,
197
+ leaseExpiresAt: void 0
198
+ };
199
+ }
200
+ async function firstClaimableRun(store, options, listOptions) {
201
+ return (await store.listRuns(listOptions)).find((run) => canClaimRun(run, options, listOptions));
202
+ }
203
+ function claimableWorkflowRunListOptions(options) {
204
+ return {
205
+ ...options,
206
+ workerId: void 0,
207
+ availableAtFrom: void 0,
208
+ availableAtTo: void 0,
209
+ retryAtFrom: void 0,
210
+ retryAtTo: void 0,
211
+ leaseExpiresAtFrom: void 0,
212
+ leaseExpiresAtTo: void 0
213
+ };
214
+ }
215
+ function canClaimRun(run, options, listOptions) {
216
+ if (!matchesClaimableWorkflowRunListOptions(run, listOptions)) return false;
217
+ if (run.status === "pending" || run.status === "waiting") return run.availableAt === void 0 || Temporal.Instant.compare(run.availableAt, options.now) <= 0;
218
+ if (run.status !== "running") return false;
219
+ if (run.workerId === void 0) return true;
220
+ if (run.leaseExpiresAt === void 0) return true;
221
+ return Temporal.Instant.compare(run.leaseExpiresAt, options.now) <= 0;
222
+ }
223
+ function isWorkflowRunLeaseHeldByWorker(run, options) {
224
+ return run !== void 0 && run.status === "running" && run.workerId === options.workerId && (options.attempts === void 0 || run.attempts === options.attempts) && run.leaseExpiresAt !== void 0 && Temporal.Instant.compare(run.leaseExpiresAt, options.now) > 0;
225
+ }
226
+ function matchesClaimableWorkflowRunListOptions(run, listOptions) {
227
+ return matchesWorkflowRunListOptions(run, listOptions) && (listOptions.retryReason !== void 0 || run.retryReason === void 0);
228
+ }
229
+ function isStaleLeaseClaim(run, now) {
230
+ if (run.status !== "running") return false;
231
+ if (run.workerId === void 0) return true;
232
+ if (run.leaseExpiresAt === void 0) return true;
233
+ return Temporal.Instant.compare(run.leaseExpiresAt, now) <= 0;
234
+ }
235
+ //#endregion
236
+ export { canClaimRun, claimWorkflowRun, claimedRunPatch, countWorkflowRuns, extendWorkflowRunLease, extendedRunLeasePatch, firstClaimableRun, getReadableStoredWorkflowRun, getWorkflowRun, isSameReadableWorkflowRunRevision, isStaleLeaseClaim, isStoredWorkflowRunRevisionCurrent, isWorkflowRunStaleForAppend, listWorkflowRuns, listWorkflowRunsPage, releaseRunLeasePatch, releaseWorkflowRunLease, requireReadableStoredWorkflowRun, rollbackAppendedEventIfRunStale, shouldRollbackAppendedEventForRun, updateRunIfCurrent, updateWorkflowRun };
@@ -0,0 +1,12 @@
1
+ import { WorkflowPage } from "../types/pagination.mjs";
2
+ import { ListSchedulesOptions, ListSchedulesPageOptions, ScheduleId, ScheduleRecord } from "../types/schedule.mjs";
3
+ import { WorkflowStorage } from "../types/store.mjs";
4
+ import { StorageValue } from "@temelj/storage";
5
+ //#region src/store/schedule.d.ts
6
+ declare function createSchedule<TInput>(storage: WorkflowStorage, namespace: string, schedule: ScheduleRecord<TInput>): Promise<ScheduleRecord<TInput>>;
7
+ declare function getReadableSchedule<TInput = StorageValue>(storage: WorkflowStorage, namespace: string, scheduleId: ScheduleId): Promise<ScheduleRecord<TInput> | undefined>;
8
+ declare function updateScheduleIfCurrent<TInput>(storage: WorkflowStorage, namespace: string, current: ScheduleRecord, next: ScheduleRecord<TInput>): Promise<ScheduleRecord<TInput> | undefined>;
9
+ declare function listSchedules(storage: WorkflowStorage, namespace: string, options?: ListSchedulesOptions): Promise<readonly ScheduleRecord[]>;
10
+ declare function listSchedulesPage(storage: WorkflowStorage, namespace: string, options?: ListSchedulesPageOptions): Promise<WorkflowPage<ScheduleRecord>>;
11
+ //#endregion
12
+ export { createSchedule, getReadableSchedule, listSchedules, listSchedulesPage, updateScheduleIfCurrent };
@@ -0,0 +1,41 @@
1
+ import { limitWorkflowItems } from "../utility.mjs";
2
+ import { WorkflowScheduleAlreadyExistsError, WorkflowScheduleError } from "../errors/schedule.mjs";
3
+ import "../errors/mod.mjs";
4
+ import { makeScheduleKey } from "../store-keys.mjs";
5
+ import { toStorageValue } from "./storage-value.mjs";
6
+ import { isReadableScheduleRecord } from "./validation.mjs";
7
+ import { makeScheduleCursorKey, pageItems } from "../pagination.mjs";
8
+ import { compactWorkflowStoreRecord } from "./compaction.mjs";
9
+ import { querySchedules } from "./query.mjs";
10
+ //#region src/store/schedule.ts
11
+ async function createSchedule(storage, namespace, schedule) {
12
+ if (schedule.namespace !== namespace) WorkflowScheduleError.namespaceMismatch(schedule.namespace, namespace);
13
+ if (!isReadableScheduleRecord(schedule, namespace)) WorkflowScheduleError.recordInvalid();
14
+ const compact = compactWorkflowStoreRecord(schedule);
15
+ if (!await storage.compareAndSet(makeScheduleKey(namespace, schedule.id), void 0, toStorageValue(compact))) WorkflowScheduleAlreadyExistsError.alreadyExists(schedule.id);
16
+ return compact;
17
+ }
18
+ async function getReadableSchedule(storage, namespace, scheduleId) {
19
+ const schedule = await storage.get(makeScheduleKey(namespace, scheduleId));
20
+ return isReadableScheduleRecord(schedule, namespace) ? schedule : void 0;
21
+ }
22
+ async function updateScheduleIfCurrent(storage, namespace, current, next) {
23
+ if (current.namespace !== namespace) WorkflowScheduleError.namespaceMismatch(current.namespace, namespace);
24
+ if (next.namespace !== namespace) WorkflowScheduleError.namespaceMismatch(next.namespace, namespace);
25
+ if (current.id !== next.id) WorkflowScheduleError.idChangeRejected();
26
+ if (!isReadableScheduleRecord(current, namespace)) WorkflowScheduleError.currentRecordInvalid();
27
+ if (!isReadableScheduleRecord(next, namespace)) WorkflowScheduleError.nextRecordInvalid();
28
+ const compact = compactWorkflowStoreRecord(next);
29
+ return await storage.compareAndSet(makeScheduleKey(namespace, current.id), toStorageValue(current), toStorageValue(compact)) ? compact : void 0;
30
+ }
31
+ async function listSchedules(storage, namespace, options) {
32
+ return limitWorkflowItems(await querySchedules(storage, namespace, options, { label: "Workflow schedule list options" }), options?.limit);
33
+ }
34
+ async function listSchedulesPage(storage, namespace, options) {
35
+ return pageItems(await querySchedules(storage, namespace, {
36
+ ...options,
37
+ limit: void 0
38
+ }, { label: "Workflow schedule page options" }), options, "Workflow schedule page", makeScheduleCursorKey);
39
+ }
40
+ //#endregion
41
+ export { createSchedule, getReadableSchedule, listSchedules, listSchedulesPage, updateScheduleIfCurrent };
@@ -0,0 +1,14 @@
1
+ import { StorageValue } from "@temelj/storage";
2
+ import { z } from "zod";
3
+ //#region src/store/schema.d.ts
4
+ declare const workflowCleanupIndexKeySchema: z.ZodObject<{
5
+ key: z.ZodString;
6
+ value: z.ZodCustom<StorageValue, StorageValue>;
7
+ }, z.core.$strip>;
8
+ declare const workflowMessageIdempotencyIndexSchema: z.ZodObject<{
9
+ runId: z.ZodString;
10
+ messageId: z.ZodString;
11
+ timestamp: z.ZodCustom<Temporal.Instant, Temporal.Instant>;
12
+ }, z.core.$strip>;
13
+ //#endregion
14
+ export { workflowCleanupIndexKeySchema, workflowMessageIdempotencyIndexSchema };
@@ -0,0 +1,17 @@
1
+ import { nonBlankStringSchema, temporalInstantSchema } from "../types/common.mjs";
2
+ import { runIdSchema } from "../types/run-id.mjs";
3
+ import { persistedValueIssue } from "../types/run.mjs";
4
+ import { messageIdSchema } from "../types/message-id.mjs";
5
+ import { z } from "zod";
6
+ //#region src/store/schema.ts
7
+ const workflowCleanupIndexKeySchema = z.object({
8
+ key: nonBlankStringSchema,
9
+ value: z.custom((value) => value !== void 0 && persistedValueIssue(value, "value", /* @__PURE__ */ new WeakSet(), true) === void 0)
10
+ });
11
+ const workflowMessageIdempotencyIndexSchema = z.object({
12
+ runId: runIdSchema,
13
+ messageId: messageIdSchema,
14
+ timestamp: temporalInstantSchema
15
+ });
16
+ //#endregion
17
+ export { workflowCleanupIndexKeySchema, workflowMessageIdempotencyIndexSchema };
@@ -0,0 +1,14 @@
1
+ import { RunId } from "../types/run-id.mjs";
2
+ import { WorkflowPage } from "../types/pagination.mjs";
3
+ import { WorkflowListStepAttemptsPageOptions } from "../types/run-list.mjs";
4
+ import { WorkflowListStepAttemptsOptions, WorkflowStepAttemptRecord } from "../types/step-attempts.mjs";
5
+ import "../types/run.mjs";
6
+ import { WorkflowEventReader, WorkflowRunReaderRepository, WorkflowStorage } from "../types/store.mjs";
7
+ //#region src/store/step-attempt.d.ts
8
+ type WorkflowStepAttemptReadStore = WorkflowRunReaderRepository & WorkflowEventReader;
9
+ declare function listWorkflowStepAttempts(storage: WorkflowStorage, namespace: string, store: WorkflowStepAttemptReadStore, runId: RunId, options?: WorkflowListStepAttemptsOptions): Promise<readonly WorkflowStepAttemptRecord[]>;
10
+ declare function listWorkflowStepAttemptsPage(storage: WorkflowStorage, namespace: string, store: WorkflowStepAttemptReadStore, runId: RunId, options?: WorkflowListStepAttemptsPageOptions): Promise<WorkflowPage<WorkflowStepAttemptRecord>>;
11
+ declare function countWorkflowStepAttempts(storage: WorkflowStorage, namespace: string, store: WorkflowStepAttemptReadStore, runId: RunId, options?: WorkflowListStepAttemptsOptions): Promise<number>;
12
+ declare function repairWorkflowStepAttempts(storage: WorkflowStorage, namespace: string, store: WorkflowStepAttemptReadStore, runId: RunId): Promise<readonly WorkflowStepAttemptRecord[]>;
13
+ //#endregion
14
+ export { countWorkflowStepAttempts, listWorkflowStepAttempts, listWorkflowStepAttemptsPage, repairWorkflowStepAttempts };
@@ -0,0 +1,23 @@
1
+ import { limitWorkflowItems } from "../utility.mjs";
2
+ import { makeWorkflowStepAttemptCursorKey, pageItems } from "../pagination.mjs";
3
+ import { reconcileStepAttempts } from "./materialization.mjs";
4
+ import { getRequiredStoreRun, queryWorkflowStepAttempts } from "./query.mjs";
5
+ //#region src/store/step-attempt.ts
6
+ async function listWorkflowStepAttempts(storage, namespace, store, runId, options) {
7
+ return limitWorkflowItems(await queryWorkflowStepAttempts(storage, namespace, store, runId, options, { label: "Workflow step attempt list options" }), options?.limit);
8
+ }
9
+ async function listWorkflowStepAttemptsPage(storage, namespace, store, runId, options) {
10
+ return pageItems(await queryWorkflowStepAttempts(storage, namespace, store, runId, {
11
+ ...options,
12
+ limit: void 0
13
+ }, { label: "Workflow step attempt page options" }), options, "Workflow step attempt page", makeWorkflowStepAttemptCursorKey);
14
+ }
15
+ async function countWorkflowStepAttempts(storage, namespace, store, runId, options) {
16
+ return (await queryWorkflowStepAttempts(storage, namespace, store, runId, options, { label: "Workflow step attempt list options" })).length;
17
+ }
18
+ async function repairWorkflowStepAttempts(storage, namespace, store, runId) {
19
+ await getRequiredStoreRun(store, runId);
20
+ return await reconcileStepAttempts(storage, namespace, runId, await store.getEvents(runId));
21
+ }
22
+ //#endregion
23
+ export { countWorkflowStepAttempts, listWorkflowStepAttempts, listWorkflowStepAttemptsPage, repairWorkflowStepAttempts };
@@ -0,0 +1,5 @@
1
+ import { StorageValue } from "@temelj/storage";
2
+ //#region src/store/storage-value.d.ts
3
+ declare function toStorageValue(value: unknown): StorageValue;
4
+ //#endregion
5
+ export { toStorageValue };
@@ -0,0 +1,6 @@
1
+ //#region src/store/storage-value.ts
2
+ function toStorageValue(value) {
3
+ return value;
4
+ }
5
+ //#endregion
6
+ export { toStorageValue };
@@ -0,0 +1,35 @@
1
+ import { CleanupMarkerRecord } from "../types/cleanup.mjs";
2
+ import { ScheduleRecord } from "../types/schedule.mjs";
3
+ import { LockRecord } from "../types/lock.mjs";
4
+ import { WorkflowStepAttemptRecord } from "../types/step-attempts.mjs";
5
+ import { WorkflowRunRecord, WorkflowRunTransitionReason } from "../types/run.mjs";
6
+ import { EventRecord } from "../types/events.mjs";
7
+ import { WorkflowMessageIdempotencyIndex } from "../types/store.mjs";
8
+ import { StorageValue } from "@temelj/storage";
9
+ //#region src/store/validation.d.ts
10
+ /** Returns whether an unknown value is a readable run record in the expected namespace. */
11
+ declare function isReadableWorkflowRunRecord(run: unknown, expectedNamespace: string): run is WorkflowRunRecord;
12
+ /** Returns whether an unknown value is a run record that can participate in indexed list queries. */
13
+ declare function isListableWorkflowRunRecord(run: unknown, expectedNamespace: string): run is WorkflowRunRecord & {
14
+ readonly lastTransitionReason: WorkflowRunTransitionReason;
15
+ };
16
+ /** Parses a stored workflow run record, if one exists. */
17
+ declare function parseStoredWorkflowRunRecord(value: StorageValue | undefined): WorkflowRunRecord | undefined;
18
+ /** Returns whether an unknown value is a readable schedule record in the expected namespace. */
19
+ declare function isReadableScheduleRecord<TInput = StorageValue>(schedule: unknown, expectedNamespace: string): schedule is ScheduleRecord<TInput>;
20
+ /** Returns whether an unknown value is a readable lock record in the expected namespace. */
21
+ declare function isReadableLockRecord(lock: unknown, expectedNamespace: string): lock is LockRecord;
22
+ /** Returns whether an unknown value is a readable durable workflow event record. */
23
+ declare function isReadableEventRecord(event: unknown): event is EventRecord;
24
+ /** Parses a readable cleanup marker record, if the value has the expected shape. */
25
+ declare function readableCleanupMarkerRecord(marker: unknown): CleanupMarkerRecord | undefined;
26
+ /** Parses a stored materialized step-attempt record, if one exists. */
27
+ declare function parseStoredWorkflowStepAttemptRecord(value: StorageValue | undefined): WorkflowStepAttemptRecord | undefined;
28
+ /** Returns whether an unknown value is a readable message idempotency index record. */
29
+ declare function isReadableMessageIdempotencyIndex(value: unknown): value is WorkflowMessageIdempotencyIndex;
30
+ /** Returns whether the stored message idempotency index matches the expected logical index. */
31
+ declare function isSameMessageIdempotencyIndex(current: unknown, value: WorkflowMessageIdempotencyIndex): boolean;
32
+ declare function requireReadableStoreRun(run: WorkflowRunRecord, namespace: string): void;
33
+ declare function requireValidRunTransition(current: WorkflowRunRecord, next: WorkflowRunRecord): void;
34
+ //#endregion
35
+ export { isListableWorkflowRunRecord, isReadableEventRecord, isReadableLockRecord, isReadableMessageIdempotencyIndex, isReadableScheduleRecord, isReadableWorkflowRunRecord, isSameMessageIdempotencyIndex, parseStoredWorkflowRunRecord, parseStoredWorkflowStepAttemptRecord, readableCleanupMarkerRecord, requireReadableStoreRun, requireValidRunTransition };
@@ -0,0 +1,77 @@
1
+ import { WorkflowRunNotFoundError, WorkflowRunTransitionError } from "../errors/run.mjs";
2
+ import { workflowRunRecordSchema } from "../types/run.mjs";
3
+ import "../errors/mod.mjs";
4
+ import { workflowEventRecordSchema } from "../types/events.mjs";
5
+ import { workflowCleanupMarkerRecordSchema } from "../types/cleanup.mjs";
6
+ import { workflowLockRecordSchema } from "../types/lock.mjs";
7
+ import { workflowScheduleRecordSchema } from "../types/schedule.mjs";
8
+ import { workflowStepAttemptRecordSchema } from "../types/step-attempts.mjs";
9
+ import { workflowMessageIdempotencyIndexSchema } from "./schema.mjs";
10
+ //#region src/store/validation.ts
11
+ /** Returns whether an unknown value is a readable run record in the expected namespace. */
12
+ function isReadableWorkflowRunRecord(run, expectedNamespace) {
13
+ const parsed = workflowRunRecordSchema.safeParse(run);
14
+ return parsed.success && parsed.data.namespace === expectedNamespace;
15
+ }
16
+ /** Returns whether an unknown value is a run record that can participate in indexed list queries. */
17
+ function isListableWorkflowRunRecord(run, expectedNamespace) {
18
+ return isReadableWorkflowRunRecord(run, expectedNamespace);
19
+ }
20
+ /** Parses a stored workflow run record, if one exists. */
21
+ function parseStoredWorkflowRunRecord(value) {
22
+ return value === void 0 ? void 0 : workflowRunRecordSchema.parse(value);
23
+ }
24
+ /** Returns whether an unknown value is a readable schedule record in the expected namespace. */
25
+ function isReadableScheduleRecord(schedule, expectedNamespace) {
26
+ const parsed = workflowScheduleRecordSchema.safeParse(schedule);
27
+ return parsed.success && parsed.data.namespace === expectedNamespace;
28
+ }
29
+ /** Returns whether an unknown value is a readable lock record in the expected namespace. */
30
+ function isReadableLockRecord(lock, expectedNamespace) {
31
+ const parsed = workflowLockRecordSchema.safeParse(lock);
32
+ return parsed.success && parsed.data.namespace === expectedNamespace;
33
+ }
34
+ /** Returns whether an unknown value is a readable durable workflow event record. */
35
+ function isReadableEventRecord(event) {
36
+ return workflowEventRecordSchema.safeParse(event).success;
37
+ }
38
+ /** Parses a readable cleanup marker record, if the value has the expected shape. */
39
+ function readableCleanupMarkerRecord(marker) {
40
+ const parsed = workflowCleanupMarkerRecordSchema.safeParse(marker);
41
+ return parsed.success ? parsed.data : void 0;
42
+ }
43
+ /** Parses a stored materialized step-attempt record, if one exists. */
44
+ function parseStoredWorkflowStepAttemptRecord(value) {
45
+ return value === void 0 ? void 0 : workflowStepAttemptRecordSchema.parse(value);
46
+ }
47
+ /** Returns whether an unknown value is a readable message idempotency index record. */
48
+ function isReadableMessageIdempotencyIndex(value) {
49
+ return workflowMessageIdempotencyIndexSchema.safeParse(value).success;
50
+ }
51
+ /** Returns whether the stored message idempotency index matches the expected logical index. */
52
+ function isSameMessageIdempotencyIndex(current, value) {
53
+ const parsedCurrent = workflowMessageIdempotencyIndexSchema.safeParse(current);
54
+ return parsedCurrent.success && parsedCurrent.data.runId === value.runId && parsedCurrent.data.messageId === value.messageId && Temporal.Instant.compare(parsedCurrent.data.timestamp, value.timestamp) === 0;
55
+ }
56
+ function requireReadableStoreRun(run, namespace) {
57
+ const runId = run.id;
58
+ if (!isReadableWorkflowRunRecord(run, namespace)) WorkflowRunNotFoundError.notFound(runId);
59
+ }
60
+ function requireValidRunTransition(current, next) {
61
+ if (current.status === next.status) return;
62
+ if (canTransitionWorkflowRunStatus(current.status, next.status)) return;
63
+ WorkflowRunTransitionError.invalidTransition(current.id, current.status, next.status);
64
+ }
65
+ /** Returns whether a run status transition is allowed by the workflow state machine. */
66
+ function canTransitionWorkflowRunStatus(from, to) {
67
+ switch (from) {
68
+ case "pending": return to === "running" || to === "waiting" || to === "failed" || to === "canceled";
69
+ case "running": return to === "waiting" || to === "completed" || to === "failed" || to === "canceled";
70
+ case "waiting": return to === "running" || to === "failed" || to === "canceled";
71
+ case "failed": return to === "pending";
72
+ case "completed":
73
+ case "canceled": return false;
74
+ }
75
+ }
76
+ //#endregion
77
+ export { isListableWorkflowRunRecord, isReadableEventRecord, isReadableLockRecord, isReadableMessageIdempotencyIndex, isReadableScheduleRecord, isReadableWorkflowRunRecord, isSameMessageIdempotencyIndex, parseStoredWorkflowRunRecord, parseStoredWorkflowStepAttemptRecord, readableCleanupMarkerRecord, requireReadableStoreRun, requireValidRunTransition };
@@ -0,0 +1,49 @@
1
+ import { RunId } from "./types/run-id.mjs";
2
+ import { ScheduleId } from "./types/schedule.mjs";
3
+ import "./types/run.mjs";
4
+ import { WorkflowIdempotencyKeyOptions } from "./types/store.mjs";
5
+ //#region src/store-keys.d.ts
6
+ type WorkflowIdempotencyKey = `workflow:${string}:idempotency:${string}:${string}`;
7
+ type MessageIdempotencyKey = `workflow:${string}:message-idempotency:${string}:${string}`;
8
+ type WorkflowRunKey = `workflow:${string}:run:${string}`;
9
+ type WorkflowEventsKey = `workflow:${string}:events:${string}`;
10
+ type WorkflowStepAttemptKey = `workflow:${string}:step-attempt:${string}:${string}`;
11
+ type WorkflowScheduleKey = `workflow:${string}:schedule:${string}`;
12
+ type WorkflowLockKey = `workflow:${string}:lock:${string}`;
13
+ type WorkflowCleanupMarkerKey = `workflow:${string}:cleanup:${string}`;
14
+ type WorkflowNamespacePrefix = `workflow:${string}:`;
15
+ type WorkflowRunKeyPrefix = `workflow:${string}:run:`;
16
+ type WorkflowStepAttemptKeyPrefix = `workflow:${string}:step-attempt:${string}:`;
17
+ type WorkflowScheduleKeyPrefix = `workflow:${string}:schedule:`;
18
+ type WorkflowLockKeyPrefix = `workflow:${string}:lock:`;
19
+ type WorkflowCleanupMarkerKeyPrefix = `workflow:${string}:cleanup:`;
20
+ /** Builds a validated workflow idempotency storage key. */
21
+ declare function makeWorkflowIdempotencyKey(namespace: string, options: WorkflowIdempotencyKeyOptions): WorkflowIdempotencyKey;
22
+ /** Builds a workflow run storage key. */
23
+ declare function makeRunKey(namespace: string, runId: RunId): WorkflowRunKey;
24
+ /** Builds a workflow event-history storage key. */
25
+ declare function makeEventsKey(namespace: string, runId: RunId): WorkflowEventsKey;
26
+ /** Builds a workflow step-attempt storage key. */
27
+ declare function makeStepAttemptKey(namespace: string, runId: RunId, attemptKey: string): WorkflowStepAttemptKey;
28
+ /** Builds a workflow step-attempt storage key prefix. */
29
+ declare function makeStepAttemptKeyPrefix(namespace: string, runId: RunId): WorkflowStepAttemptKeyPrefix;
30
+ /** Builds a workflow message idempotency storage key. */
31
+ declare function makeMessageIdempotencyKey(namespace: string, runId: RunId, idempotencyKey: string): MessageIdempotencyKey;
32
+ /** Builds a workflow schedule storage key. */
33
+ declare function makeScheduleKey(namespace: string, scheduleId: ScheduleId): WorkflowScheduleKey;
34
+ /** Builds a workflow schedule storage key prefix. */
35
+ declare function makeScheduleKeyPrefix(namespace: string): WorkflowScheduleKeyPrefix;
36
+ /** Builds a workflow lock storage key. */
37
+ declare function makeLockKey(namespace: string, key: string): WorkflowLockKey;
38
+ /** Builds a workflow cleanup marker storage key. */
39
+ declare function makeCleanupMarkerKey(namespace: string, runId: RunId): WorkflowCleanupMarkerKey;
40
+ /** Builds a workflow cleanup marker storage key prefix. */
41
+ declare function makeCleanupMarkerKeyPrefix(namespace: string): WorkflowCleanupMarkerKeyPrefix;
42
+ /** Builds a workflow lock storage key prefix. */
43
+ declare function makeLockKeyPrefix(namespace: string): WorkflowLockKeyPrefix;
44
+ /** Builds a workflow idempotency storage key. */
45
+ declare function makeIdempotencyKey(namespace: string, options: WorkflowIdempotencyKeyOptions): WorkflowIdempotencyKey;
46
+ /** Builds a workflow run storage key prefix. */
47
+ declare function makeRunKeyPrefix(namespace: string): WorkflowRunKeyPrefix;
48
+ //#endregion
49
+ export { MessageIdempotencyKey, WorkflowCleanupMarkerKey, WorkflowCleanupMarkerKeyPrefix, WorkflowEventsKey, WorkflowIdempotencyKey, WorkflowLockKey, WorkflowLockKeyPrefix, WorkflowNamespacePrefix, WorkflowRunKey, WorkflowRunKeyPrefix, WorkflowScheduleKey, WorkflowScheduleKeyPrefix, WorkflowStepAttemptKey, WorkflowStepAttemptKeyPrefix, makeCleanupMarkerKey, makeCleanupMarkerKeyPrefix, makeEventsKey, makeIdempotencyKey, makeLockKey, makeLockKeyPrefix, makeMessageIdempotencyKey, makeRunKey, makeRunKeyPrefix, makeScheduleKey, makeScheduleKeyPrefix, makeStepAttemptKey, makeStepAttemptKeyPrefix, makeWorkflowIdempotencyKey };
@@ -0,0 +1,63 @@
1
+ //#region src/store-keys.ts
2
+ /** Builds a validated workflow idempotency storage key. */
3
+ function makeWorkflowIdempotencyKey(namespace, options) {
4
+ return makeIdempotencyKey(namespace, options);
5
+ }
6
+ /** Builds a workflow run storage key. */
7
+ function makeRunKey(namespace, runId) {
8
+ return `${namespacePrefix(namespace)}run:${runId}`;
9
+ }
10
+ /** Builds a workflow event-history storage key. */
11
+ function makeEventsKey(namespace, runId) {
12
+ return `${namespacePrefix(namespace)}events:${runId}`;
13
+ }
14
+ /** Builds a workflow step-attempt storage key. */
15
+ function makeStepAttemptKey(namespace, runId, attemptKey) {
16
+ return `${makeStepAttemptKeyPrefix(namespace, runId)}${encodeURIComponent(attemptKey)}`;
17
+ }
18
+ /** Builds a workflow step-attempt storage key prefix. */
19
+ function makeStepAttemptKeyPrefix(namespace, runId) {
20
+ return `${namespacePrefix(namespace)}step-attempt:${runId}:`;
21
+ }
22
+ /** Builds a workflow message idempotency storage key. */
23
+ function makeMessageIdempotencyKey(namespace, runId, idempotencyKey) {
24
+ return `${namespacePrefix(namespace)}message-idempotency:${encodeURIComponent(runId)}:${encodeURIComponent(idempotencyKey)}`;
25
+ }
26
+ /** Builds a workflow schedule storage key. */
27
+ function makeScheduleKey(namespace, scheduleId) {
28
+ return `${namespacePrefix(namespace)}schedule:${encodeURIComponent(scheduleId)}`;
29
+ }
30
+ /** Builds a workflow schedule storage key prefix. */
31
+ function makeScheduleKeyPrefix(namespace) {
32
+ return `${namespacePrefix(namespace)}schedule:`;
33
+ }
34
+ /** Builds a workflow lock storage key. */
35
+ function makeLockKey(namespace, key) {
36
+ return `${namespacePrefix(namespace)}lock:${encodeURIComponent(key)}`;
37
+ }
38
+ /** Builds a workflow cleanup marker storage key. */
39
+ function makeCleanupMarkerKey(namespace, runId) {
40
+ return `${namespacePrefix(namespace)}cleanup:${encodeURIComponent(runId)}`;
41
+ }
42
+ /** Builds a workflow cleanup marker storage key prefix. */
43
+ function makeCleanupMarkerKeyPrefix(namespace) {
44
+ return `${namespacePrefix(namespace)}cleanup:`;
45
+ }
46
+ /** Builds a workflow lock storage key prefix. */
47
+ function makeLockKeyPrefix(namespace) {
48
+ return `${namespacePrefix(namespace)}lock:`;
49
+ }
50
+ /** Builds a workflow idempotency storage key. */
51
+ function makeIdempotencyKey(namespace, options) {
52
+ const versionedName = options.workflowVersion === void 0 ? options.workflowName : `${options.workflowName}@${options.workflowVersion}`;
53
+ return `${namespacePrefix(namespace)}idempotency:${encodeURIComponent(versionedName)}:${encodeURIComponent(options.idempotencyKey)}`;
54
+ }
55
+ /** Builds a workflow run storage key prefix. */
56
+ function makeRunKeyPrefix(namespace) {
57
+ return `${namespacePrefix(namespace)}run:`;
58
+ }
59
+ function namespacePrefix(namespace) {
60
+ return `workflow:${encodeURIComponent(namespace)}:`;
61
+ }
62
+ //#endregion
63
+ export { makeCleanupMarkerKey, makeCleanupMarkerKeyPrefix, makeEventsKey, makeIdempotencyKey, makeLockKey, makeLockKeyPrefix, makeMessageIdempotencyKey, makeRunKey, makeRunKeyPrefix, makeScheduleKey, makeScheduleKeyPrefix, makeStepAttemptKey, makeStepAttemptKeyPrefix, makeWorkflowIdempotencyKey };