@tanstack/workflow-runtime 0.0.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 (47) hide show
  1. package/README.md +22 -0
  2. package/dist/define-runtime.cjs +50 -0
  3. package/dist/define-runtime.cjs.map +1 -0
  4. package/dist/define-runtime.d.cts +16 -0
  5. package/dist/define-runtime.d.ts +16 -0
  6. package/dist/define-runtime.js +48 -0
  7. package/dist/define-runtime.js.map +1 -0
  8. package/dist/in-memory-store.cjs +457 -0
  9. package/dist/in-memory-store.cjs.map +1 -0
  10. package/dist/in-memory-store.d.cts +8 -0
  11. package/dist/in-memory-store.d.ts +8 -0
  12. package/dist/in-memory-store.js +457 -0
  13. package/dist/in-memory-store.js.map +1 -0
  14. package/dist/index.cjs +14 -0
  15. package/dist/index.d.cts +7 -0
  16. package/dist/index.d.ts +7 -0
  17. package/dist/index.js +7 -0
  18. package/dist/run-store-adapter.cjs +30 -0
  19. package/dist/run-store-adapter.cjs.map +1 -0
  20. package/dist/run-store-adapter.d.cts +7 -0
  21. package/dist/run-store-adapter.d.ts +7 -0
  22. package/dist/run-store-adapter.js +29 -0
  23. package/dist/run-store-adapter.js.map +1 -0
  24. package/dist/runtime-driver.cjs +334 -0
  25. package/dist/runtime-driver.cjs.map +1 -0
  26. package/dist/runtime-driver.d.cts +12 -0
  27. package/dist/runtime-driver.d.ts +12 -0
  28. package/dist/runtime-driver.js +334 -0
  29. package/dist/runtime-driver.js.map +1 -0
  30. package/dist/schedule-materializer.cjs +156 -0
  31. package/dist/schedule-materializer.cjs.map +1 -0
  32. package/dist/schedule-materializer.d.cts +28 -0
  33. package/dist/schedule-materializer.d.ts +28 -0
  34. package/dist/schedule-materializer.js +155 -0
  35. package/dist/schedule-materializer.js.map +1 -0
  36. package/dist/types.cjs +0 -0
  37. package/dist/types.d.cts +375 -0
  38. package/dist/types.d.ts +375 -0
  39. package/dist/types.js +1 -0
  40. package/package.json +60 -0
  41. package/src/define-runtime.ts +46 -0
  42. package/src/in-memory-store.ts +607 -0
  43. package/src/index.ts +74 -0
  44. package/src/run-store-adapter.ts +49 -0
  45. package/src/runtime-driver.ts +536 -0
  46. package/src/schedule-materializer.ts +272 -0
  47. package/src/types.ts +462 -0
package/src/types.ts ADDED
@@ -0,0 +1,462 @@
1
+ import type {
2
+ AnyWorkflowDefinition,
3
+ ApprovalResult,
4
+ DeleteReason,
5
+ RunState,
6
+ RunStatus,
7
+ RunStore,
8
+ SerializedError,
9
+ SignalDelivery,
10
+ WorkflowEvent,
11
+ } from '@tanstack/workflow-core'
12
+
13
+ export type WorkflowId = string
14
+ export type WorkflowVersion = string
15
+ export type RunId = string
16
+ export type ScheduleId = string
17
+ export type ScheduleBucketId = string
18
+ export type LeaseOwner = string
19
+
20
+ export type WorkflowExecutionStatus = RunStatus | 'queued'
21
+
22
+ export interface WorkflowLease {
23
+ owner: LeaseOwner
24
+ expiresAt: number
25
+ }
26
+
27
+ export interface WorkflowExecution {
28
+ runId: RunId
29
+ workflowId: WorkflowId
30
+ workflowVersion?: WorkflowVersion
31
+ status: WorkflowExecutionStatus
32
+ input: unknown
33
+ output?: unknown
34
+ error?: SerializedError
35
+ waitingFor?: RunState['waitingFor']
36
+ pendingApproval?: RunState['pendingApproval']
37
+ wakeAt?: number
38
+ lease?: WorkflowLease
39
+ createdAt: number
40
+ updatedAt: number
41
+ }
42
+
43
+ export interface StoredWorkflowEvent {
44
+ runId: RunId
45
+ eventIndex: number
46
+ eventType: WorkflowEvent['type']
47
+ stepId?: string
48
+ event: WorkflowEvent
49
+ createdAt: number
50
+ }
51
+
52
+ export interface LoadedExecution {
53
+ run: WorkflowExecution
54
+ events: ReadonlyArray<StoredWorkflowEvent>
55
+ }
56
+
57
+ export interface CreateRunArgs {
58
+ runId: RunId
59
+ workflowId: WorkflowId
60
+ workflowVersion?: WorkflowVersion
61
+ input: unknown
62
+ now: number
63
+ }
64
+
65
+ export type CreateRunResult =
66
+ | { kind: 'created'; run: WorkflowExecution }
67
+ | { kind: 'existing'; run: WorkflowExecution }
68
+
69
+ export interface ReadEventsArgs {
70
+ runId: RunId
71
+ fromIndex?: number
72
+ }
73
+
74
+ export interface AppendEventsArgs {
75
+ runId: RunId
76
+ expectedNextIndex: number
77
+ events: ReadonlyArray<WorkflowEvent>
78
+ }
79
+
80
+ export interface AppendEventsResult {
81
+ nextIndex: number
82
+ }
83
+
84
+ export interface ClaimRunArgs {
85
+ runId: RunId
86
+ leaseOwner: LeaseOwner
87
+ leaseMs: number
88
+ now: number
89
+ }
90
+
91
+ export type ClaimRunResult =
92
+ | { kind: 'claimed'; run: WorkflowExecution }
93
+ | { kind: 'not-found' }
94
+ | { kind: 'not-claimable'; run: WorkflowExecution }
95
+
96
+ export interface HeartbeatRunLeaseArgs {
97
+ runId: RunId
98
+ leaseOwner: LeaseOwner
99
+ leaseMs: number
100
+ now: number
101
+ }
102
+
103
+ export interface ReleaseRunLeaseArgs {
104
+ runId: RunId
105
+ leaseOwner: LeaseOwner
106
+ }
107
+
108
+ export interface MarkRunPausedArgs {
109
+ runId: RunId
110
+ waitingFor?: RunState['waitingFor']
111
+ pendingApproval?: RunState['pendingApproval']
112
+ wakeAt?: number
113
+ now: number
114
+ }
115
+
116
+ export interface MarkRunFinishedArgs {
117
+ runId: RunId
118
+ output: unknown
119
+ now: number
120
+ }
121
+
122
+ export interface MarkRunErroredArgs {
123
+ runId: RunId
124
+ error: SerializedError
125
+ code: string
126
+ now: number
127
+ }
128
+
129
+ export interface ScheduleTimerArgs {
130
+ runId: RunId
131
+ workflowId: WorkflowId
132
+ workflowVersion?: WorkflowVersion
133
+ wakeAt: number
134
+ signalId: string
135
+ now: number
136
+ }
137
+
138
+ export interface ClaimDueTimersArgs {
139
+ now: number
140
+ limit: number
141
+ leaseOwner: LeaseOwner
142
+ leaseMs: number
143
+ }
144
+
145
+ export interface TimerWakeup {
146
+ runId: RunId
147
+ workflowId: WorkflowId
148
+ workflowVersion?: WorkflowVersion
149
+ wakeAt: number
150
+ signalId: string
151
+ }
152
+
153
+ export interface DeliverSignalArgs<TPayload = unknown> {
154
+ runId: RunId
155
+ delivery: SignalDelivery<TPayload>
156
+ now: number
157
+ }
158
+
159
+ export type DeliverSignalResult =
160
+ | { kind: 'delivered'; run: WorkflowExecution }
161
+ | { kind: 'duplicate'; run: WorkflowExecution }
162
+ | { kind: 'not-waiting'; run: WorkflowExecution }
163
+ | { kind: 'not-found' }
164
+
165
+ export interface DeliverApprovalArgs {
166
+ runId: RunId
167
+ approval: ApprovalResult
168
+ now: number
169
+ }
170
+
171
+ export type DeliverApprovalResult =
172
+ | { kind: 'delivered'; run: WorkflowExecution }
173
+ | { kind: 'duplicate'; run: WorkflowExecution }
174
+ | { kind: 'not-waiting'; run: WorkflowExecution }
175
+ | { kind: 'not-found' }
176
+
177
+ export type WorkflowOverlapPolicy =
178
+ | 'skip'
179
+ | 'allow'
180
+ | 'buffer-one'
181
+ | 'cancel-previous'
182
+ | 'terminate-previous'
183
+
184
+ export type WorkflowScheduleSpec =
185
+ | {
186
+ kind: 'cron'
187
+ expression: string
188
+ timezone?: string
189
+ }
190
+ | {
191
+ kind: 'interval'
192
+ everyMs: number
193
+ timezone?: string
194
+ }
195
+
196
+ export interface WorkflowScheduleDefinition {
197
+ id?: ScheduleId
198
+ schedule: WorkflowScheduleSpec
199
+ overlapPolicy?: WorkflowOverlapPolicy
200
+ input?: unknown | (() => unknown | Promise<unknown>)
201
+ enabled?: boolean
202
+ }
203
+
204
+ export interface UpsertScheduleArgs {
205
+ scheduleId: ScheduleId
206
+ workflowId: WorkflowId
207
+ workflowVersion?: WorkflowVersion
208
+ schedule: WorkflowScheduleSpec
209
+ overlapPolicy: WorkflowOverlapPolicy
210
+ input?: unknown
211
+ nextFireAt?: number
212
+ enabled: boolean
213
+ now: number
214
+ }
215
+
216
+ export interface ClaimDueScheduleBucketsArgs {
217
+ now: number
218
+ limit: number
219
+ leaseOwner: LeaseOwner
220
+ leaseMs: number
221
+ }
222
+
223
+ export interface ScheduleBucket {
224
+ scheduleId: ScheduleId
225
+ bucketId: ScheduleBucketId
226
+ workflowId: WorkflowId
227
+ workflowVersion?: WorkflowVersion
228
+ runId: RunId
229
+ fireAt: number
230
+ input: unknown
231
+ overlapPolicy: WorkflowOverlapPolicy
232
+ }
233
+
234
+ export interface MarkScheduleBucketStartedArgs {
235
+ scheduleId: ScheduleId
236
+ bucketId: ScheduleBucketId
237
+ runId: RunId
238
+ now: number
239
+ }
240
+
241
+ export interface ClaimStaleRunsArgs {
242
+ now: number
243
+ limit: number
244
+ leaseOwner: LeaseOwner
245
+ leaseMs: number
246
+ }
247
+
248
+ export interface RunClaim {
249
+ run: WorkflowExecution
250
+ lease: WorkflowLease
251
+ }
252
+
253
+ export interface ListRunsArgs {
254
+ workflowId?: WorkflowId
255
+ status?: WorkflowExecutionStatus
256
+ limit: number
257
+ cursor?: string
258
+ }
259
+
260
+ export interface RunSummary {
261
+ runId: RunId
262
+ workflowId: WorkflowId
263
+ workflowVersion?: WorkflowVersion
264
+ status: WorkflowExecutionStatus
265
+ waitingFor?: RunState['waitingFor']
266
+ pendingApproval?: RunState['pendingApproval']
267
+ wakeAt?: number
268
+ createdAt: number
269
+ updatedAt: number
270
+ }
271
+
272
+ export interface RunTimeline {
273
+ run: WorkflowExecution
274
+ events: ReadonlyArray<StoredWorkflowEvent>
275
+ }
276
+
277
+ export interface SaveRunStateArgs {
278
+ state: RunState
279
+ }
280
+
281
+ export interface WorkflowRunStoreAdapterStore {
282
+ loadRunState: (runId: RunId) => Promise<RunState | undefined>
283
+ saveRunState: (args: SaveRunStateArgs) => Promise<void>
284
+ deleteRun: (runId: RunId, reason: DeleteReason) => Promise<void>
285
+ appendEvents: (args: AppendEventsArgs) => Promise<AppendEventsResult>
286
+ readEvents: (
287
+ args: ReadEventsArgs,
288
+ ) => Promise<ReadonlyArray<StoredWorkflowEvent>>
289
+ subscribeEvents?: (
290
+ runId: RunId,
291
+ fromIndex: number,
292
+ onEvent: (event: WorkflowEvent, index: number) => void,
293
+ ) => () => void
294
+ }
295
+
296
+ export type WorkflowRunStoreAdapter = RunStore
297
+
298
+ export interface WorkflowExecutionStore extends WorkflowRunStoreAdapterStore {
299
+ createRun: (args: CreateRunArgs) => Promise<CreateRunResult>
300
+ loadRun: (runId: RunId) => Promise<WorkflowExecution | undefined>
301
+ loadExecution: (runId: RunId) => Promise<LoadedExecution | undefined>
302
+
303
+ claimRun: (args: ClaimRunArgs) => Promise<ClaimRunResult>
304
+ heartbeatRunLease: (args: HeartbeatRunLeaseArgs) => Promise<void>
305
+ releaseRunLease: (args: ReleaseRunLeaseArgs) => Promise<void>
306
+ markRunPaused: (args: MarkRunPausedArgs) => Promise<void>
307
+ markRunFinished: (args: MarkRunFinishedArgs) => Promise<void>
308
+ markRunErrored: (args: MarkRunErroredArgs) => Promise<void>
309
+
310
+ scheduleTimer: (args: ScheduleTimerArgs) => Promise<void>
311
+ claimDueTimers: (
312
+ args: ClaimDueTimersArgs,
313
+ ) => Promise<ReadonlyArray<TimerWakeup>>
314
+ deliverSignal: <TPayload = unknown>(
315
+ args: DeliverSignalArgs<TPayload>,
316
+ ) => Promise<DeliverSignalResult>
317
+ deliverApproval: (args: DeliverApprovalArgs) => Promise<DeliverApprovalResult>
318
+
319
+ upsertSchedule: (args: UpsertScheduleArgs) => Promise<void>
320
+ claimDueScheduleBuckets: (
321
+ args: ClaimDueScheduleBucketsArgs,
322
+ ) => Promise<ReadonlyArray<ScheduleBucket>>
323
+ markScheduleBucketStarted: (
324
+ args: MarkScheduleBucketStartedArgs,
325
+ ) => Promise<void>
326
+
327
+ claimStaleRuns: (args: ClaimStaleRunsArgs) => Promise<ReadonlyArray<RunClaim>>
328
+ listRuns: (args: ListRunsArgs) => Promise<ReadonlyArray<RunSummary>>
329
+ getRunTimeline: (runId: RunId) => Promise<RunTimeline | undefined>
330
+ }
331
+
332
+ export type WorkflowLoaderResult<
333
+ TWorkflow extends AnyWorkflowDefinition = AnyWorkflowDefinition,
334
+ > = TWorkflow | { default: TWorkflow } | { workflow: TWorkflow }
335
+
336
+ export type WorkflowLoader<
337
+ TWorkflow extends AnyWorkflowDefinition = AnyWorkflowDefinition,
338
+ > = () => Promise<WorkflowLoaderResult<TWorkflow>>
339
+
340
+ export interface WorkflowRegistration<
341
+ TWorkflow extends AnyWorkflowDefinition = AnyWorkflowDefinition,
342
+ > {
343
+ load: WorkflowLoader<TWorkflow>
344
+ version?: WorkflowVersion
345
+ previousVersions?: Record<WorkflowVersion, WorkflowLoader>
346
+ schedules?: ReadonlyArray<WorkflowScheduleDefinition>
347
+ }
348
+
349
+ export type WorkflowRegistrationMap = Record<string, WorkflowRegistration>
350
+
351
+ export interface WorkflowRuntimeConfig<
352
+ TWorkflows extends WorkflowRegistrationMap = WorkflowRegistrationMap,
353
+ > {
354
+ workflows: TWorkflows
355
+ store: WorkflowExecutionStore
356
+ defaultLeaseMs?: number
357
+ }
358
+
359
+ export interface WorkflowRuntimeDefinition<
360
+ TWorkflows extends WorkflowRegistrationMap = WorkflowRegistrationMap,
361
+ > extends WorkflowRuntimeConfig<TWorkflows> {
362
+ __kind: 'workflow-runtime'
363
+ startRun: (
364
+ args: WorkflowRuntimeStartRunArgs,
365
+ ) => Promise<WorkflowRuntimeRunResult>
366
+ deliverSignal: <TPayload = unknown>(
367
+ args: WorkflowRuntimeDeliverSignalArgs<TPayload>,
368
+ ) => Promise<WorkflowRuntimeRunResult>
369
+ deliverApproval: (
370
+ args: WorkflowRuntimeDeliverApprovalArgs,
371
+ ) => Promise<WorkflowRuntimeRunResult>
372
+ sweep: (
373
+ args?: WorkflowRuntimeSweepArgs,
374
+ ) => Promise<WorkflowRuntimeSweepResult>
375
+ }
376
+
377
+ export interface WorkflowRuntimeStartRunArgs {
378
+ workflowId: WorkflowId
379
+ runId: RunId
380
+ input: unknown
381
+ now?: number
382
+ leaseOwner?: LeaseOwner
383
+ leaseMs?: number
384
+ threadId?: string
385
+ includeEvents?: boolean
386
+ maxEvents?: number
387
+ }
388
+
389
+ export interface WorkflowRuntimeDeliverSignalArgs<TPayload = unknown> {
390
+ runId: RunId
391
+ signalId: string
392
+ name: string
393
+ payload: TPayload
394
+ now?: number
395
+ leaseOwner?: LeaseOwner
396
+ leaseMs?: number
397
+ threadId?: string
398
+ includeEvents?: boolean
399
+ maxEvents?: number
400
+ }
401
+
402
+ export interface WorkflowRuntimeDeliverApprovalArgs {
403
+ runId: RunId
404
+ approval: ApprovalResult
405
+ now?: number
406
+ leaseOwner?: LeaseOwner
407
+ leaseMs?: number
408
+ threadId?: string
409
+ includeEvents?: boolean
410
+ maxEvents?: number
411
+ }
412
+
413
+ export type WorkflowRuntimeRunResultKind =
414
+ | 'completed'
415
+ | 'paused'
416
+ | 'errored'
417
+ | 'running'
418
+ | 'not-found'
419
+ | 'not-claimable'
420
+ | 'not-waiting'
421
+ | 'duplicate'
422
+
423
+ export interface WorkflowRuntimeRunResult {
424
+ kind: WorkflowRuntimeRunResultKind
425
+ runId: RunId
426
+ workflowId?: WorkflowId
427
+ run?: WorkflowExecution
428
+ events: ReadonlyArray<WorkflowEvent>
429
+ eventCount: number
430
+ eventsTruncated?: boolean
431
+ }
432
+
433
+ export interface WorkflowRuntimeSweepArgs {
434
+ now?: number
435
+ limit?: number
436
+ maxScheduledRuns?: number
437
+ maxTimers?: number
438
+ maxDurationMs?: number
439
+ leaseOwner?: LeaseOwner
440
+ leaseMs?: number
441
+ includeEvents?: boolean
442
+ maxEvents?: number
443
+ }
444
+
445
+ export interface WorkflowRuntimeSweepResult {
446
+ scheduled: ReadonlyArray<WorkflowRuntimeRunResult>
447
+ timers: ReadonlyArray<WorkflowRuntimeRunResult>
448
+ summary: WorkflowRuntimeSweepSummary
449
+ deadlineReached: boolean
450
+ remainingMayExist: boolean
451
+ }
452
+
453
+ export type WorkflowRuntimeRunKindCounts = Partial<
454
+ Record<WorkflowRuntimeRunResultKind, number>
455
+ >
456
+
457
+ export interface WorkflowRuntimeSweepSummary {
458
+ scheduled: WorkflowRuntimeRunKindCounts
459
+ timers: WorkflowRuntimeRunKindCounts
460
+ eventCount: number
461
+ returnedEventCount: number
462
+ }