rivetkit 2.3.0 → 2.3.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.
- package/dist/browser/client.d.ts +19 -6
- package/dist/browser/client.js +4 -4
- package/dist/browser/client.js.map +1 -1
- package/dist/browser/inspector/client.js +4 -4
- package/dist/browser/inspector/client.js.map +1 -1
- package/dist/tsup/actor/errors.cjs +4 -2
- package/dist/tsup/actor/errors.cjs.map +1 -1
- package/dist/tsup/actor/errors.d.cts +1 -1
- package/dist/tsup/actor/errors.d.ts +1 -1
- package/dist/tsup/actor/errors.js +3 -1
- package/dist/tsup/agent-os/index.cjs +4 -4
- package/dist/tsup/agent-os/index.cjs.map +1 -1
- package/dist/tsup/agent-os/index.d.cts +18 -5
- package/dist/tsup/agent-os/index.d.ts +18 -5
- package/dist/tsup/agent-os/index.js +4 -4
- package/dist/tsup/agent-os/index.js.map +1 -1
- package/dist/tsup/{chunk-NIY3RSPX.cjs → chunk-3MHDOUD7.cjs} +11 -2
- package/dist/tsup/chunk-3MHDOUD7.cjs.map +1 -0
- package/dist/tsup/{chunk-KORQB2IR.cjs → chunk-4FC7TVS6.cjs} +6 -2
- package/dist/tsup/chunk-4FC7TVS6.cjs.map +1 -0
- package/dist/tsup/{chunk-47HHIEXH.cjs → chunk-4UUEB43Y.cjs} +4 -4
- package/dist/tsup/{chunk-47HHIEXH.cjs.map → chunk-4UUEB43Y.cjs.map} +1 -1
- package/dist/tsup/{chunk-7QKCIVAY.cjs → chunk-5IWLUJ6W.cjs} +167 -167
- package/dist/tsup/{chunk-7QKCIVAY.cjs.map → chunk-5IWLUJ6W.cjs.map} +1 -1
- package/dist/tsup/{chunk-P2GNQ4RN.js → chunk-H6VVZMWN.js} +4 -4
- package/dist/tsup/{chunk-MEHBWPLJ.js → chunk-I35VSLEM.js} +3 -3
- package/dist/tsup/{chunk-BEI24WTI.js → chunk-JBUZRPY5.js} +2 -2
- package/dist/tsup/{chunk-B6VUNZUD.cjs → chunk-JLJJZYCJ.cjs} +10 -10
- package/dist/tsup/{chunk-B6VUNZUD.cjs.map → chunk-JLJJZYCJ.cjs.map} +1 -1
- package/dist/tsup/{chunk-VTTFNQQI.js → chunk-L2X3YFER.js} +31 -6
- package/dist/tsup/chunk-L2X3YFER.js.map +1 -0
- package/dist/tsup/{chunk-DXXJPH55.cjs → chunk-MNHKOS6L.cjs} +39 -14
- package/dist/tsup/chunk-MNHKOS6L.cjs.map +1 -0
- package/dist/tsup/{chunk-UMZVD6DQ.js → chunk-NERUIBOT.js} +2 -2
- package/dist/tsup/{chunk-I4LN3FNT.cjs → chunk-OST76LRW.cjs} +7 -7
- package/dist/tsup/{chunk-I4LN3FNT.cjs.map → chunk-OST76LRW.cjs.map} +1 -1
- package/dist/tsup/{chunk-LVTBW2RE.cjs → chunk-OZBCXBVP.cjs} +3 -3
- package/dist/tsup/{chunk-LVTBW2RE.cjs.map → chunk-OZBCXBVP.cjs.map} +1 -1
- package/dist/tsup/{chunk-4JDSFJS5.js → chunk-PT6OIW5E.js} +5 -5
- package/dist/tsup/{chunk-2OTRTA3J.js → chunk-R6KPN5EW.js} +2 -2
- package/dist/tsup/{chunk-3677IIOV.cjs → chunk-V5KMAMX3.cjs} +5 -5
- package/dist/tsup/{chunk-3677IIOV.cjs.map → chunk-V5KMAMX3.cjs.map} +1 -1
- package/dist/tsup/{chunk-DPIMKYNB.js → chunk-XIX5DOZN.js} +10 -1
- package/dist/tsup/chunk-XIX5DOZN.js.map +1 -0
- package/dist/tsup/{chunk-BRP62GZC.js → chunk-ZZ3WBRPD.js} +5 -1
- package/dist/tsup/chunk-ZZ3WBRPD.js.map +1 -0
- package/dist/tsup/client/mod.cjs +7 -7
- package/dist/tsup/client/mod.d.cts +3 -3
- package/dist/tsup/client/mod.d.ts +3 -3
- package/dist/tsup/client/mod.js +6 -6
- package/dist/tsup/common/log.cjs +3 -3
- package/dist/tsup/common/log.js +2 -2
- package/dist/tsup/common/websocket.cjs +4 -4
- package/dist/tsup/common/websocket.js +3 -3
- package/dist/tsup/{config-CZQQ-mso.d.cts → config-CzvopP5m.d.cts} +22 -7
- package/dist/tsup/{config-BxWAw3iH.d.ts → config-DZuT7tcp.d.ts} +22 -7
- package/dist/tsup/context-CyAdY-aA.d.ts +128 -0
- package/dist/tsup/context-sNB28g0N.d.cts +128 -0
- package/dist/tsup/dynamic/mod.cjs +2 -2
- package/dist/tsup/dynamic/mod.d.cts +2 -2
- package/dist/tsup/dynamic/mod.d.ts +2 -2
- package/dist/tsup/dynamic/mod.js +1 -1
- package/dist/tsup/inspector/mod.cjs +6 -6
- package/dist/tsup/inspector/mod.js +5 -5
- package/dist/tsup/inspector-tab/mod.cjs +2 -2
- package/dist/tsup/inspector-tab/mod.cjs.map +1 -1
- package/dist/tsup/inspector-tab/mod.d.cts +3 -3
- package/dist/tsup/inspector-tab/mod.d.ts +3 -3
- package/dist/tsup/inspector-tab/mod.js +1 -1
- package/dist/tsup/inspector-tab/mod.js.map +1 -1
- package/dist/tsup/mod.cjs +292 -276
- package/dist/tsup/mod.cjs.map +1 -1
- package/dist/tsup/mod.d.cts +4 -4
- package/dist/tsup/mod.d.ts +4 -4
- package/dist/tsup/mod.js +202 -186
- package/dist/tsup/mod.js.map +1 -1
- package/dist/tsup/test/mod.cjs +10 -10
- package/dist/tsup/test/mod.d.cts +2 -2
- package/dist/tsup/test/mod.d.ts +2 -2
- package/dist/tsup/test/mod.js +6 -6
- package/dist/tsup/{utils-DQosb24I.d.ts → utils-CqDnC_PS.d.cts} +2 -1
- package/dist/tsup/{utils-DQosb24I.d.cts → utils-CqDnC_PS.d.ts} +2 -1
- package/dist/tsup/utils.cjs +3 -3
- package/dist/tsup/utils.d.cts +1 -1
- package/dist/tsup/utils.d.ts +1 -1
- package/dist/tsup/utils.js +2 -2
- package/dist/tsup/workflow/mod.cjs +184 -148
- package/dist/tsup/workflow/mod.cjs.map +1 -1
- package/dist/tsup/workflow/mod.d.cts +6 -6
- package/dist/tsup/workflow/mod.d.ts +6 -6
- package/dist/tsup/workflow/mod.js +177 -141
- package/dist/tsup/workflow/mod.js.map +1 -1
- package/package.json +11 -11
- package/src/actor/config.ts +14 -0
- package/src/actor/errors.ts +11 -0
- package/src/inspector-tab/mod.ts +1 -1
- package/src/registry/index.ts +74 -18
- package/src/registry/napi-runtime.ts +16 -0
- package/src/registry/native.ts +79 -30
- package/src/registry/runtime.ts +11 -0
- package/src/registry/wasm-runtime.ts +11 -0
- package/src/utils/env-vars.ts +31 -0
- package/src/workflow/context.ts +675 -244
- package/src/workflow/driver.ts +20 -7
- package/src/workflow/mod.ts +25 -9
- package/dist/tsup/chunk-BRP62GZC.js.map +0 -1
- package/dist/tsup/chunk-DPIMKYNB.js.map +0 -1
- package/dist/tsup/chunk-DXXJPH55.cjs.map +0 -1
- package/dist/tsup/chunk-KORQB2IR.cjs.map +0 -1
- package/dist/tsup/chunk-NIY3RSPX.cjs.map +0 -1
- package/dist/tsup/chunk-VTTFNQQI.js.map +0 -1
- package/dist/tsup/context-Bw7xq8w3.d.cts +0 -92
- package/dist/tsup/context-D8QA76sV.d.ts +0 -92
- package/src/utils/serve.ts +0 -216
- /package/dist/tsup/{chunk-P2GNQ4RN.js.map → chunk-H6VVZMWN.js.map} +0 -0
- /package/dist/tsup/{chunk-MEHBWPLJ.js.map → chunk-I35VSLEM.js.map} +0 -0
- /package/dist/tsup/{chunk-BEI24WTI.js.map → chunk-JBUZRPY5.js.map} +0 -0
- /package/dist/tsup/{chunk-UMZVD6DQ.js.map → chunk-NERUIBOT.js.map} +0 -0
- /package/dist/tsup/{chunk-4JDSFJS5.js.map → chunk-PT6OIW5E.js.map} +0 -0
- /package/dist/tsup/{chunk-2OTRTA3J.js.map → chunk-R6KPN5EW.js.map} +0 -0
package/src/workflow/context.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
|
|
3
1
|
import type {
|
|
4
2
|
BranchConfig,
|
|
5
3
|
BranchOutput,
|
|
@@ -57,7 +55,177 @@ type WorkflowActorQueueNextBatchOptions<
|
|
|
57
55
|
type WorkflowActorQueueNextBatchOptionsFallback<TCompletable extends boolean> =
|
|
58
56
|
Omit<QueueNextBatchOptions<string, TCompletable>, "signal">;
|
|
59
57
|
|
|
60
|
-
|
|
58
|
+
// Step run callbacks receive a WorkflowStepContext, which is the only place
|
|
59
|
+
// actor data (state/db/vars/client) may be touched.
|
|
60
|
+
type WorkflowStepRun<
|
|
61
|
+
T,
|
|
62
|
+
TState,
|
|
63
|
+
TConnParams,
|
|
64
|
+
TConnState,
|
|
65
|
+
TVars,
|
|
66
|
+
TInput,
|
|
67
|
+
TDatabase extends AnyDatabaseProvider,
|
|
68
|
+
TEvents extends EventSchemaConfig,
|
|
69
|
+
TQueues extends QueueSchemaConfig,
|
|
70
|
+
> = (
|
|
71
|
+
step: WorkflowStepContext<
|
|
72
|
+
TState,
|
|
73
|
+
TConnParams,
|
|
74
|
+
TConnState,
|
|
75
|
+
TVars,
|
|
76
|
+
TInput,
|
|
77
|
+
TDatabase,
|
|
78
|
+
TEvents,
|
|
79
|
+
TQueues
|
|
80
|
+
>,
|
|
81
|
+
) => Promise<T>;
|
|
82
|
+
|
|
83
|
+
// Step rollback callbacks compensate a committed step, so they also run with a
|
|
84
|
+
// WorkflowStepContext to mutate actor data.
|
|
85
|
+
type WorkflowStepRollback<
|
|
86
|
+
T,
|
|
87
|
+
TState,
|
|
88
|
+
TConnParams,
|
|
89
|
+
TConnState,
|
|
90
|
+
TVars,
|
|
91
|
+
TInput,
|
|
92
|
+
TDatabase extends AnyDatabaseProvider,
|
|
93
|
+
TEvents extends EventSchemaConfig,
|
|
94
|
+
TQueues extends QueueSchemaConfig,
|
|
95
|
+
> = (
|
|
96
|
+
step: WorkflowStepContext<
|
|
97
|
+
TState,
|
|
98
|
+
TConnParams,
|
|
99
|
+
TConnState,
|
|
100
|
+
TVars,
|
|
101
|
+
TInput,
|
|
102
|
+
TDatabase,
|
|
103
|
+
TEvents,
|
|
104
|
+
TQueues
|
|
105
|
+
>,
|
|
106
|
+
output: T,
|
|
107
|
+
) => Promise<void>;
|
|
108
|
+
|
|
109
|
+
// Orchestration callbacks (try/loop/race/join) receive a WorkflowContext,
|
|
110
|
+
// because inside them you sequence further steps rather than touch actor data.
|
|
111
|
+
type WorkflowContextRun<
|
|
112
|
+
T,
|
|
113
|
+
TState,
|
|
114
|
+
TConnParams,
|
|
115
|
+
TConnState,
|
|
116
|
+
TVars,
|
|
117
|
+
TInput,
|
|
118
|
+
TDatabase extends AnyDatabaseProvider,
|
|
119
|
+
TEvents extends EventSchemaConfig,
|
|
120
|
+
TQueues extends QueueSchemaConfig,
|
|
121
|
+
> = (
|
|
122
|
+
ctx: WorkflowContext<
|
|
123
|
+
TState,
|
|
124
|
+
TConnParams,
|
|
125
|
+
TConnState,
|
|
126
|
+
TVars,
|
|
127
|
+
TInput,
|
|
128
|
+
TDatabase,
|
|
129
|
+
TEvents,
|
|
130
|
+
TQueues
|
|
131
|
+
>,
|
|
132
|
+
) => Promise<T>;
|
|
133
|
+
|
|
134
|
+
export type WorkflowStepConfig<
|
|
135
|
+
T,
|
|
136
|
+
TState,
|
|
137
|
+
TConnParams,
|
|
138
|
+
TConnState,
|
|
139
|
+
TVars,
|
|
140
|
+
TInput,
|
|
141
|
+
TDatabase extends AnyDatabaseProvider,
|
|
142
|
+
TEvents extends EventSchemaConfig,
|
|
143
|
+
TQueues extends QueueSchemaConfig,
|
|
144
|
+
> = Omit<StepConfig<T>, "run" | "rollback"> & {
|
|
145
|
+
run: WorkflowStepRun<
|
|
146
|
+
T,
|
|
147
|
+
TState,
|
|
148
|
+
TConnParams,
|
|
149
|
+
TConnState,
|
|
150
|
+
TVars,
|
|
151
|
+
TInput,
|
|
152
|
+
TDatabase,
|
|
153
|
+
TEvents,
|
|
154
|
+
TQueues
|
|
155
|
+
>;
|
|
156
|
+
rollback?: WorkflowStepRollback<
|
|
157
|
+
T,
|
|
158
|
+
TState,
|
|
159
|
+
TConnParams,
|
|
160
|
+
TConnState,
|
|
161
|
+
TVars,
|
|
162
|
+
TInput,
|
|
163
|
+
TDatabase,
|
|
164
|
+
TEvents,
|
|
165
|
+
TQueues
|
|
166
|
+
>;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export type WorkflowTryStepConfig<
|
|
170
|
+
T,
|
|
171
|
+
TState,
|
|
172
|
+
TConnParams,
|
|
173
|
+
TConnState,
|
|
174
|
+
TVars,
|
|
175
|
+
TInput,
|
|
176
|
+
TDatabase extends AnyDatabaseProvider,
|
|
177
|
+
TEvents extends EventSchemaConfig,
|
|
178
|
+
TQueues extends QueueSchemaConfig,
|
|
179
|
+
> = Omit<TryStepConfig<T>, "run" | "rollback"> & {
|
|
180
|
+
run: WorkflowStepRun<
|
|
181
|
+
T,
|
|
182
|
+
TState,
|
|
183
|
+
TConnParams,
|
|
184
|
+
TConnState,
|
|
185
|
+
TVars,
|
|
186
|
+
TInput,
|
|
187
|
+
TDatabase,
|
|
188
|
+
TEvents,
|
|
189
|
+
TQueues
|
|
190
|
+
>;
|
|
191
|
+
rollback?: WorkflowStepRollback<
|
|
192
|
+
T,
|
|
193
|
+
TState,
|
|
194
|
+
TConnParams,
|
|
195
|
+
TConnState,
|
|
196
|
+
TVars,
|
|
197
|
+
TInput,
|
|
198
|
+
TDatabase,
|
|
199
|
+
TEvents,
|
|
200
|
+
TQueues
|
|
201
|
+
>;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export type WorkflowTryConfig<
|
|
205
|
+
T,
|
|
206
|
+
TState,
|
|
207
|
+
TConnParams,
|
|
208
|
+
TConnState,
|
|
209
|
+
TVars,
|
|
210
|
+
TInput,
|
|
211
|
+
TDatabase extends AnyDatabaseProvider,
|
|
212
|
+
TEvents extends EventSchemaConfig,
|
|
213
|
+
TQueues extends QueueSchemaConfig,
|
|
214
|
+
> = Omit<TryBlockConfig<T>, "run"> & {
|
|
215
|
+
run: WorkflowContextRun<
|
|
216
|
+
T,
|
|
217
|
+
TState,
|
|
218
|
+
TConnParams,
|
|
219
|
+
TConnState,
|
|
220
|
+
TVars,
|
|
221
|
+
TInput,
|
|
222
|
+
TDatabase,
|
|
223
|
+
TEvents,
|
|
224
|
+
TQueues
|
|
225
|
+
>;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
export type WorkflowLoopConfig<
|
|
61
229
|
S,
|
|
62
230
|
T,
|
|
63
231
|
TState,
|
|
@@ -70,7 +238,7 @@ type ActorWorkflowLoopConfig<
|
|
|
70
238
|
TQueues extends QueueSchemaConfig,
|
|
71
239
|
> = Omit<LoopConfig<S, T>, "run"> & {
|
|
72
240
|
run: (
|
|
73
|
-
ctx:
|
|
241
|
+
ctx: WorkflowContext<
|
|
74
242
|
TState,
|
|
75
243
|
TConnParams,
|
|
76
244
|
TConnState,
|
|
@@ -86,7 +254,7 @@ type ActorWorkflowLoopConfig<
|
|
|
86
254
|
>;
|
|
87
255
|
};
|
|
88
256
|
|
|
89
|
-
type
|
|
257
|
+
export type WorkflowBranchConfig<
|
|
90
258
|
TOutput,
|
|
91
259
|
TState,
|
|
92
260
|
TConnParams,
|
|
@@ -97,8 +265,54 @@ type ActorWorkflowBranchConfig<
|
|
|
97
265
|
TEvents extends EventSchemaConfig,
|
|
98
266
|
TQueues extends QueueSchemaConfig,
|
|
99
267
|
> = {
|
|
100
|
-
run:
|
|
101
|
-
|
|
268
|
+
run: WorkflowContextRun<
|
|
269
|
+
TOutput,
|
|
270
|
+
TState,
|
|
271
|
+
TConnParams,
|
|
272
|
+
TConnState,
|
|
273
|
+
TVars,
|
|
274
|
+
TInput,
|
|
275
|
+
TDatabase,
|
|
276
|
+
TEvents,
|
|
277
|
+
TQueues
|
|
278
|
+
>;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// Marks a step context inactive once its step has finished. Module-private so it
|
|
282
|
+
// never appears on the public surface.
|
|
283
|
+
const DEACTIVATE_STEP = Symbol("workflow.step.deactivate");
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The context handed to a workflow step (`step` / `tryStep` callbacks). This is
|
|
287
|
+
* the only scope where actor data (state, vars, db, client) and side effects
|
|
288
|
+
* (broadcast, queue.send) are reachable. It is valid only while its step is
|
|
289
|
+
* executing; using it after the step resolves throws.
|
|
290
|
+
*/
|
|
291
|
+
export class WorkflowStepContext<
|
|
292
|
+
TState,
|
|
293
|
+
TConnParams,
|
|
294
|
+
TConnState,
|
|
295
|
+
TVars,
|
|
296
|
+
TInput,
|
|
297
|
+
TDatabase extends AnyDatabaseProvider,
|
|
298
|
+
TEvents extends EventSchemaConfig = Record<never, never>,
|
|
299
|
+
TQueues extends QueueSchemaConfig = Record<never, never>,
|
|
300
|
+
> {
|
|
301
|
+
#runCtx: RunContext<
|
|
302
|
+
TState,
|
|
303
|
+
TConnParams,
|
|
304
|
+
TConnState,
|
|
305
|
+
TVars,
|
|
306
|
+
TInput,
|
|
307
|
+
TDatabase,
|
|
308
|
+
TEvents,
|
|
309
|
+
TQueues
|
|
310
|
+
>;
|
|
311
|
+
#active = true;
|
|
312
|
+
#onGuardViolation: () => void;
|
|
313
|
+
|
|
314
|
+
constructor(
|
|
315
|
+
runCtx: RunContext<
|
|
102
316
|
TState,
|
|
103
317
|
TConnParams,
|
|
104
318
|
TConnState,
|
|
@@ -108,10 +322,133 @@ type ActorWorkflowBranchConfig<
|
|
|
108
322
|
TEvents,
|
|
109
323
|
TQueues
|
|
110
324
|
>,
|
|
111
|
-
|
|
112
|
-
|
|
325
|
+
onGuardViolation: () => void,
|
|
326
|
+
) {
|
|
327
|
+
this.#runCtx = runCtx;
|
|
328
|
+
this.#onGuardViolation = onGuardViolation;
|
|
329
|
+
}
|
|
113
330
|
|
|
114
|
-
|
|
331
|
+
[DEACTIVATE_STEP](): void {
|
|
332
|
+
this.#active = false;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
#ensureActive(feature: string): void {
|
|
336
|
+
if (!this.#active) {
|
|
337
|
+
this.#onGuardViolation();
|
|
338
|
+
throw new Error(
|
|
339
|
+
`${feature} is only available inside workflow steps`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
get actorId(): string {
|
|
345
|
+
return this.#runCtx.actorId;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
get name(): string {
|
|
349
|
+
return this.#runCtx.name;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
get key(): string[] {
|
|
353
|
+
return this.#runCtx.key;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
get log() {
|
|
357
|
+
return this.#runCtx.log;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
get abortSignal(): AbortSignal {
|
|
361
|
+
return this.#runCtx.abortSignal;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
get state(): TState extends never ? never : TState {
|
|
365
|
+
this.#ensureActive("state");
|
|
366
|
+
return this.#runCtx.state as TState extends never ? never : TState;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
get vars(): TVars extends never ? never : TVars {
|
|
370
|
+
this.#ensureActive("vars");
|
|
371
|
+
return this.#runCtx.vars as TVars extends never ? never : TVars;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
get db(): TDatabase extends never ? never : InferDatabaseClient<TDatabase> {
|
|
375
|
+
this.#ensureActive("db");
|
|
376
|
+
return this.#runCtx.db as TDatabase extends never
|
|
377
|
+
? never
|
|
378
|
+
: InferDatabaseClient<TDatabase>;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
client<R extends Registry<any> = Registry<any>>(): Client<R> {
|
|
382
|
+
this.#ensureActive("client");
|
|
383
|
+
return this.#runCtx.client<R>();
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
broadcast<K extends keyof TEvents & string>(
|
|
387
|
+
name: K,
|
|
388
|
+
...args: InferEventArgs<InferSchemaMap<TEvents>[K]>
|
|
389
|
+
): void;
|
|
390
|
+
broadcast(
|
|
391
|
+
name: keyof TEvents extends never ? string : never,
|
|
392
|
+
...args: Array<unknown>
|
|
393
|
+
): void;
|
|
394
|
+
broadcast(name: string, ...args: Array<unknown>): void {
|
|
395
|
+
this.#ensureActive("broadcast");
|
|
396
|
+
this.#runCtx.broadcast(
|
|
397
|
+
name as never,
|
|
398
|
+
...(args as unknown[] as never[]),
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
get queue() {
|
|
403
|
+
const self = this;
|
|
404
|
+
function send<K extends keyof TQueues & string>(
|
|
405
|
+
name: K,
|
|
406
|
+
body: InferSchemaMap<TQueues>[K],
|
|
407
|
+
): Promise<void>;
|
|
408
|
+
function send(
|
|
409
|
+
name: keyof TQueues extends never ? string : never,
|
|
410
|
+
body: unknown,
|
|
411
|
+
): Promise<void>;
|
|
412
|
+
async function send(name: string, body: unknown): Promise<void> {
|
|
413
|
+
self.#ensureActive("queue.send");
|
|
414
|
+
await self.#runCtx.queue.send(name as never, body as never);
|
|
415
|
+
}
|
|
416
|
+
return { send };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Holds the actor awake for the duration of the provided promise. The actor
|
|
421
|
+
* cannot idle-sleep or finalize the sleep grace period until the promise
|
|
422
|
+
* settles.
|
|
423
|
+
*/
|
|
424
|
+
keepAwake<T>(promise: Promise<T>): Promise<T> {
|
|
425
|
+
this.#ensureActive("keepAwake");
|
|
426
|
+
return this.#runCtx.keepAwake(promise);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Registers a promise that the sleep grace period will wait on. Use this for
|
|
431
|
+
* best-effort flush/cleanup work that may complete inside the grace window.
|
|
432
|
+
*/
|
|
433
|
+
waitUntil(promise: Promise<void>): void {
|
|
434
|
+
this.#ensureActive("waitUntil");
|
|
435
|
+
this.#runCtx.waitUntil(promise);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
destroy(): void {
|
|
439
|
+
this.#ensureActive("destroy");
|
|
440
|
+
this.#runCtx.destroy();
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* The context handed to the workflow function and to orchestration callbacks
|
|
446
|
+
* (`try` / `loop` / `race` / `join`). It exposes the deterministic, replayable
|
|
447
|
+
* workflow primitives (step, sleep, queue waits, control flow). It deliberately
|
|
448
|
+
* does NOT expose actor data; reach `state`, `db`, `vars`, `client`, and
|
|
449
|
+
* `broadcast` through the step context passed to `step` / `tryStep`.
|
|
450
|
+
*/
|
|
451
|
+
export class WorkflowContext<
|
|
115
452
|
TState,
|
|
116
453
|
TConnParams,
|
|
117
454
|
TConnState,
|
|
@@ -120,8 +457,7 @@ export class ActorWorkflowContext<
|
|
|
120
457
|
TDatabase extends AnyDatabaseProvider,
|
|
121
458
|
TEvents extends EventSchemaConfig = Record<never, never>,
|
|
122
459
|
TQueues extends QueueSchemaConfig = Record<never, never>,
|
|
123
|
-
>
|
|
124
|
-
{
|
|
460
|
+
> {
|
|
125
461
|
#inner: WorkflowContextInterface;
|
|
126
462
|
#runCtx: RunContext<
|
|
127
463
|
TState,
|
|
@@ -133,9 +469,6 @@ export class ActorWorkflowContext<
|
|
|
133
469
|
TEvents,
|
|
134
470
|
TQueues
|
|
135
471
|
>;
|
|
136
|
-
#actorAccessDepth = 0;
|
|
137
|
-
#allowActorAccess = false;
|
|
138
|
-
#guardViolation = false;
|
|
139
472
|
|
|
140
473
|
constructor(
|
|
141
474
|
inner: WorkflowContextInterface,
|
|
@@ -162,6 +495,22 @@ export class ActorWorkflowContext<
|
|
|
162
495
|
return this.#inner.abortSignal;
|
|
163
496
|
}
|
|
164
497
|
|
|
498
|
+
get actorId(): string {
|
|
499
|
+
return this.#runCtx.actorId;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
get name(): string {
|
|
503
|
+
return this.#runCtx.name;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
get key(): string[] {
|
|
507
|
+
return this.#runCtx.key;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
get log() {
|
|
511
|
+
return this.#runCtx.log;
|
|
512
|
+
}
|
|
513
|
+
|
|
165
514
|
get queue() {
|
|
166
515
|
const self = this;
|
|
167
516
|
function next<
|
|
@@ -181,10 +530,9 @@ export class ActorWorkflowContext<
|
|
|
181
530
|
TCompletable
|
|
182
531
|
>
|
|
183
532
|
>;
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
): Promise<WorkflowQueueMessage<unknown>> {
|
|
533
|
+
// The implementation signature stays broad so the schema-typed public
|
|
534
|
+
// overloads above remain compatible with it.
|
|
535
|
+
async function next(name: string, opts?: any): Promise<any> {
|
|
188
536
|
const message = await self.#inner.queue.next(name, opts);
|
|
189
537
|
return self.#toActorQueueMessage(message);
|
|
190
538
|
}
|
|
@@ -210,106 +558,232 @@ export class ActorWorkflowContext<
|
|
|
210
558
|
>
|
|
211
559
|
>
|
|
212
560
|
>;
|
|
213
|
-
async function nextBatch(
|
|
214
|
-
name: string,
|
|
215
|
-
opts?: WorkflowActorQueueNextBatchOptions<string, boolean>,
|
|
216
|
-
): Promise<Array<WorkflowQueueMessage<unknown>>> {
|
|
561
|
+
async function nextBatch(name: string, opts?: any): Promise<any> {
|
|
217
562
|
const messages = await self.#inner.queue.nextBatch(name, opts);
|
|
218
563
|
return messages.map((message) =>
|
|
219
564
|
self.#toActorQueueMessage(message),
|
|
220
565
|
);
|
|
221
566
|
}
|
|
222
567
|
|
|
223
|
-
function send<K extends keyof TQueues & string>(
|
|
224
|
-
name: K,
|
|
225
|
-
body: InferSchemaMap<TQueues>[K],
|
|
226
|
-
): Promise<void>;
|
|
227
|
-
function send(
|
|
228
|
-
name: keyof TQueues extends never ? string : never,
|
|
229
|
-
body: unknown,
|
|
230
|
-
): Promise<void>;
|
|
231
|
-
async function send(name: string, body: unknown): Promise<void> {
|
|
232
|
-
self.#ensureActorAccess("queue.send");
|
|
233
|
-
await self.#runCtx.queue.send(name as never, body as never);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
568
|
return {
|
|
237
569
|
next,
|
|
238
570
|
nextBatch,
|
|
239
|
-
send,
|
|
240
571
|
};
|
|
241
572
|
}
|
|
242
573
|
|
|
574
|
+
step<T>(
|
|
575
|
+
name: string,
|
|
576
|
+
run: WorkflowStepRun<
|
|
577
|
+
T,
|
|
578
|
+
TState,
|
|
579
|
+
TConnParams,
|
|
580
|
+
TConnState,
|
|
581
|
+
TVars,
|
|
582
|
+
TInput,
|
|
583
|
+
TDatabase,
|
|
584
|
+
TEvents,
|
|
585
|
+
TQueues
|
|
586
|
+
>,
|
|
587
|
+
): Promise<T>;
|
|
588
|
+
step<T>(
|
|
589
|
+
config: WorkflowStepConfig<
|
|
590
|
+
T,
|
|
591
|
+
TState,
|
|
592
|
+
TConnParams,
|
|
593
|
+
TConnState,
|
|
594
|
+
TVars,
|
|
595
|
+
TInput,
|
|
596
|
+
TDatabase,
|
|
597
|
+
TEvents,
|
|
598
|
+
TQueues
|
|
599
|
+
>,
|
|
600
|
+
): Promise<T>;
|
|
243
601
|
async step<T>(
|
|
244
|
-
nameOrConfig:
|
|
245
|
-
|
|
602
|
+
nameOrConfig:
|
|
603
|
+
| string
|
|
604
|
+
| WorkflowStepConfig<
|
|
605
|
+
T,
|
|
606
|
+
TState,
|
|
607
|
+
TConnParams,
|
|
608
|
+
TConnState,
|
|
609
|
+
TVars,
|
|
610
|
+
TInput,
|
|
611
|
+
TDatabase,
|
|
612
|
+
TEvents,
|
|
613
|
+
TQueues
|
|
614
|
+
>,
|
|
615
|
+
run?: WorkflowStepRun<
|
|
616
|
+
T,
|
|
617
|
+
TState,
|
|
618
|
+
TConnParams,
|
|
619
|
+
TConnState,
|
|
620
|
+
TVars,
|
|
621
|
+
TInput,
|
|
622
|
+
TDatabase,
|
|
623
|
+
TEvents,
|
|
624
|
+
TQueues
|
|
625
|
+
>,
|
|
246
626
|
): Promise<T> {
|
|
247
627
|
if (typeof nameOrConfig === "string") {
|
|
248
628
|
if (!run) {
|
|
249
629
|
throw new Error("Step run function missing");
|
|
250
630
|
}
|
|
631
|
+
const stepRun = run;
|
|
251
632
|
return await this.#wrapActive(() =>
|
|
252
|
-
this.#inner.step(nameOrConfig, () =>
|
|
253
|
-
this.#withActorAccessAndStateRollback(run),
|
|
254
|
-
),
|
|
633
|
+
this.#inner.step(nameOrConfig, () => this.#runStep(stepRun)),
|
|
255
634
|
);
|
|
256
635
|
}
|
|
257
|
-
const stepConfig = nameOrConfig
|
|
636
|
+
const stepConfig = nameOrConfig;
|
|
637
|
+
const rollback = stepConfig.rollback;
|
|
258
638
|
const config: StepConfig<T> = {
|
|
259
639
|
...stepConfig,
|
|
260
|
-
run: () => this.#
|
|
640
|
+
run: () => this.#runStep(stepConfig.run),
|
|
641
|
+
rollback: rollback
|
|
642
|
+
? (_ctx, output) => this.#runRollback(rollback, output)
|
|
643
|
+
: undefined,
|
|
261
644
|
};
|
|
262
645
|
return await this.#wrapActive(() => this.#inner.step(config));
|
|
263
646
|
}
|
|
264
647
|
|
|
648
|
+
tryStep<T>(
|
|
649
|
+
name: string,
|
|
650
|
+
run: WorkflowStepRun<
|
|
651
|
+
T,
|
|
652
|
+
TState,
|
|
653
|
+
TConnParams,
|
|
654
|
+
TConnState,
|
|
655
|
+
TVars,
|
|
656
|
+
TInput,
|
|
657
|
+
TDatabase,
|
|
658
|
+
TEvents,
|
|
659
|
+
TQueues
|
|
660
|
+
>,
|
|
661
|
+
): Promise<TryStepResult<T>>;
|
|
662
|
+
tryStep<T>(
|
|
663
|
+
config: WorkflowTryStepConfig<
|
|
664
|
+
T,
|
|
665
|
+
TState,
|
|
666
|
+
TConnParams,
|
|
667
|
+
TConnState,
|
|
668
|
+
TVars,
|
|
669
|
+
TInput,
|
|
670
|
+
TDatabase,
|
|
671
|
+
TEvents,
|
|
672
|
+
TQueues
|
|
673
|
+
>,
|
|
674
|
+
): Promise<TryStepResult<T>>;
|
|
265
675
|
async tryStep<T>(
|
|
266
|
-
nameOrConfig:
|
|
267
|
-
|
|
676
|
+
nameOrConfig:
|
|
677
|
+
| string
|
|
678
|
+
| WorkflowTryStepConfig<
|
|
679
|
+
T,
|
|
680
|
+
TState,
|
|
681
|
+
TConnParams,
|
|
682
|
+
TConnState,
|
|
683
|
+
TVars,
|
|
684
|
+
TInput,
|
|
685
|
+
TDatabase,
|
|
686
|
+
TEvents,
|
|
687
|
+
TQueues
|
|
688
|
+
>,
|
|
689
|
+
run?: WorkflowStepRun<
|
|
690
|
+
T,
|
|
691
|
+
TState,
|
|
692
|
+
TConnParams,
|
|
693
|
+
TConnState,
|
|
694
|
+
TVars,
|
|
695
|
+
TInput,
|
|
696
|
+
TDatabase,
|
|
697
|
+
TEvents,
|
|
698
|
+
TQueues
|
|
699
|
+
>,
|
|
268
700
|
): Promise<TryStepResult<T>> {
|
|
269
701
|
if (typeof nameOrConfig === "string") {
|
|
270
702
|
if (!run) {
|
|
271
703
|
throw new Error("Step run function missing");
|
|
272
704
|
}
|
|
705
|
+
const stepRun = run;
|
|
273
706
|
return await this.#wrapActive(() =>
|
|
274
|
-
this.#inner.tryStep(nameOrConfig, () =>
|
|
275
|
-
this.#withActorAccessAndStateRollback(run),
|
|
276
|
-
),
|
|
707
|
+
this.#inner.tryStep(nameOrConfig, () => this.#runStep(stepRun)),
|
|
277
708
|
);
|
|
278
709
|
}
|
|
279
|
-
const stepConfig = nameOrConfig
|
|
710
|
+
const stepConfig = nameOrConfig;
|
|
711
|
+
const rollback = stepConfig.rollback;
|
|
280
712
|
const config: TryStepConfig<T> = {
|
|
281
713
|
...stepConfig,
|
|
282
|
-
run: () => this.#
|
|
714
|
+
run: () => this.#runStep(stepConfig.run),
|
|
715
|
+
rollback: rollback
|
|
716
|
+
? (_ctx, output) => this.#runRollback(rollback, output)
|
|
717
|
+
: undefined,
|
|
283
718
|
};
|
|
284
719
|
return await this.#wrapActive(() => this.#inner.tryStep(config));
|
|
285
720
|
}
|
|
286
721
|
|
|
722
|
+
try<T>(
|
|
723
|
+
name: string,
|
|
724
|
+
run: WorkflowContextRun<
|
|
725
|
+
T,
|
|
726
|
+
TState,
|
|
727
|
+
TConnParams,
|
|
728
|
+
TConnState,
|
|
729
|
+
TVars,
|
|
730
|
+
TInput,
|
|
731
|
+
TDatabase,
|
|
732
|
+
TEvents,
|
|
733
|
+
TQueues
|
|
734
|
+
>,
|
|
735
|
+
): Promise<TryBlockResult<T>>;
|
|
736
|
+
try<T>(
|
|
737
|
+
config: WorkflowTryConfig<
|
|
738
|
+
T,
|
|
739
|
+
TState,
|
|
740
|
+
TConnParams,
|
|
741
|
+
TConnState,
|
|
742
|
+
TVars,
|
|
743
|
+
TInput,
|
|
744
|
+
TDatabase,
|
|
745
|
+
TEvents,
|
|
746
|
+
TQueues
|
|
747
|
+
>,
|
|
748
|
+
): Promise<TryBlockResult<T>>;
|
|
287
749
|
async try<T>(
|
|
288
|
-
nameOrConfig:
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
750
|
+
nameOrConfig:
|
|
751
|
+
| string
|
|
752
|
+
| WorkflowTryConfig<
|
|
753
|
+
T,
|
|
754
|
+
TState,
|
|
755
|
+
TConnParams,
|
|
756
|
+
TConnState,
|
|
757
|
+
TVars,
|
|
758
|
+
TInput,
|
|
759
|
+
TDatabase,
|
|
760
|
+
TEvents,
|
|
761
|
+
TQueues
|
|
762
|
+
>,
|
|
763
|
+
run?: WorkflowContextRun<
|
|
764
|
+
T,
|
|
765
|
+
TState,
|
|
766
|
+
TConnParams,
|
|
767
|
+
TConnState,
|
|
768
|
+
TVars,
|
|
769
|
+
TInput,
|
|
770
|
+
TDatabase,
|
|
771
|
+
TEvents,
|
|
772
|
+
TQueues
|
|
773
|
+
>,
|
|
301
774
|
): Promise<TryBlockResult<T>> {
|
|
302
775
|
if (typeof nameOrConfig === "string") {
|
|
303
776
|
if (!run) {
|
|
304
777
|
throw new Error("Try run function missing");
|
|
305
778
|
}
|
|
779
|
+
const tryRun = run;
|
|
306
780
|
return await this.#wrapActive(() =>
|
|
307
781
|
this.#inner.try(nameOrConfig, async (ctx) =>
|
|
308
|
-
|
|
782
|
+
tryRun(this.#createChildContext(ctx)),
|
|
309
783
|
),
|
|
310
784
|
);
|
|
311
785
|
}
|
|
312
|
-
const tryConfig = nameOrConfig
|
|
786
|
+
const tryConfig = nameOrConfig;
|
|
313
787
|
const config: TryBlockConfig<T> = {
|
|
314
788
|
...tryConfig,
|
|
315
789
|
run: async (ctx) => tryConfig.run(this.#createChildContext(ctx)),
|
|
@@ -317,10 +791,10 @@ export class ActorWorkflowContext<
|
|
|
317
791
|
return await this.#wrapActive(() => this.#inner.try(config));
|
|
318
792
|
}
|
|
319
793
|
|
|
320
|
-
|
|
794
|
+
loop<T>(
|
|
321
795
|
name: string,
|
|
322
796
|
run: (
|
|
323
|
-
ctx:
|
|
797
|
+
ctx: WorkflowContext<
|
|
324
798
|
TState,
|
|
325
799
|
TConnParams,
|
|
326
800
|
TConnState,
|
|
@@ -332,14 +806,8 @@ export class ActorWorkflowContext<
|
|
|
332
806
|
>,
|
|
333
807
|
) => Promise<LoopResult<undefined, T> | undefined | void>,
|
|
334
808
|
): Promise<T>;
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
run: (
|
|
338
|
-
ctx: WorkflowContextInterface,
|
|
339
|
-
) => Promise<LoopResult<undefined, T> | undefined | void>,
|
|
340
|
-
): Promise<T>;
|
|
341
|
-
async loop<S, T>(
|
|
342
|
-
config: ActorWorkflowLoopConfig<
|
|
809
|
+
loop<S, T>(
|
|
810
|
+
config: WorkflowLoopConfig<
|
|
343
811
|
S,
|
|
344
812
|
T,
|
|
345
813
|
TState,
|
|
@@ -352,12 +820,10 @@ export class ActorWorkflowContext<
|
|
|
352
820
|
TQueues
|
|
353
821
|
>,
|
|
354
822
|
): Promise<T>;
|
|
355
|
-
async loop<S, T>(config: LoopConfig<S, T>): Promise<T>;
|
|
356
823
|
async loop(
|
|
357
824
|
nameOrConfig:
|
|
358
825
|
| string
|
|
359
|
-
|
|
|
360
|
-
| ActorWorkflowLoopConfig<
|
|
826
|
+
| WorkflowLoopConfig<
|
|
361
827
|
any,
|
|
362
828
|
any,
|
|
363
829
|
TState,
|
|
@@ -370,7 +836,7 @@ export class ActorWorkflowContext<
|
|
|
370
836
|
TQueues
|
|
371
837
|
>,
|
|
372
838
|
run?: (
|
|
373
|
-
ctx:
|
|
839
|
+
ctx: WorkflowContext<
|
|
374
840
|
TState,
|
|
375
841
|
TConnParams,
|
|
376
842
|
TConnState,
|
|
@@ -386,16 +852,26 @@ export class ActorWorkflowContext<
|
|
|
386
852
|
if (!run) {
|
|
387
853
|
throw new Error("Loop run function missing");
|
|
388
854
|
}
|
|
855
|
+
const loopRun = run;
|
|
389
856
|
return await this.#wrapActive(() =>
|
|
390
|
-
this.#inner.loop(
|
|
391
|
-
|
|
857
|
+
this.#inner.loop(
|
|
858
|
+
nameOrConfig,
|
|
859
|
+
// A void return (no explicit Loop result) is undefined at
|
|
860
|
+
// runtime, which the engine treats as continue.
|
|
861
|
+
async (
|
|
862
|
+
ctx,
|
|
863
|
+
): Promise<LoopResult<undefined, any> | undefined> =>
|
|
864
|
+
(await loopRun(this.#createChildContext(ctx))) ??
|
|
865
|
+
undefined,
|
|
392
866
|
),
|
|
393
867
|
);
|
|
394
868
|
}
|
|
869
|
+
const loopConfig = nameOrConfig;
|
|
395
870
|
const wrapped: LoopConfig<any, any> = {
|
|
396
|
-
...
|
|
397
|
-
run: async (ctx, state) =>
|
|
398
|
-
|
|
871
|
+
...loopConfig,
|
|
872
|
+
run: (async (ctx, state) =>
|
|
873
|
+
(await loopConfig.run(this.#createChildContext(ctx), state)) ??
|
|
874
|
+
undefined) as LoopConfig<any, any>["run"],
|
|
399
875
|
};
|
|
400
876
|
return await this.#wrapActive(() => this.#inner.loop(wrapped));
|
|
401
877
|
}
|
|
@@ -408,11 +884,6 @@ export class ActorWorkflowContext<
|
|
|
408
884
|
return this.#inner.sleepUntil(name, timestampMs);
|
|
409
885
|
}
|
|
410
886
|
|
|
411
|
-
destroy(): void {
|
|
412
|
-
this.#ensureActorAccess("destroy");
|
|
413
|
-
this.#runCtx.destroy();
|
|
414
|
-
}
|
|
415
|
-
|
|
416
887
|
async rollbackCheckpoint(name: string): Promise<void> {
|
|
417
888
|
await this.#wrapActive(() => this.#inner.rollbackCheckpoint(name));
|
|
418
889
|
}
|
|
@@ -420,7 +891,7 @@ export class ActorWorkflowContext<
|
|
|
420
891
|
async join<
|
|
421
892
|
T extends Record<
|
|
422
893
|
string,
|
|
423
|
-
|
|
894
|
+
WorkflowBranchConfig<
|
|
424
895
|
unknown,
|
|
425
896
|
TState,
|
|
426
897
|
TConnParams,
|
|
@@ -440,7 +911,10 @@ export class ActorWorkflowContext<
|
|
|
440
911
|
name: string,
|
|
441
912
|
branches: T,
|
|
442
913
|
): Promise<{ [K in keyof T]: BranchOutput<T[K]> }>;
|
|
443
|
-
async join(
|
|
914
|
+
async join(
|
|
915
|
+
name: string,
|
|
916
|
+
branches: Record<string, { run: (ctx: any) => Promise<unknown> }>,
|
|
917
|
+
) {
|
|
444
918
|
const wrappedBranches = Object.fromEntries(
|
|
445
919
|
Object.entries(branches).map(([key, branch]) => [
|
|
446
920
|
key,
|
|
@@ -459,25 +933,17 @@ export class ActorWorkflowContext<
|
|
|
459
933
|
name: string,
|
|
460
934
|
branches: Array<{
|
|
461
935
|
name: string;
|
|
462
|
-
run:
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
) => Promise<T>;
|
|
474
|
-
}>,
|
|
475
|
-
): Promise<{ winner: string; value: T }>;
|
|
476
|
-
async race<T>(
|
|
477
|
-
name: string,
|
|
478
|
-
branches: Array<{
|
|
479
|
-
name: string;
|
|
480
|
-
run: (ctx: WorkflowContextInterface) => Promise<T>;
|
|
936
|
+
run: WorkflowContextRun<
|
|
937
|
+
T,
|
|
938
|
+
TState,
|
|
939
|
+
TConnParams,
|
|
940
|
+
TConnState,
|
|
941
|
+
TVars,
|
|
942
|
+
TInput,
|
|
943
|
+
TDatabase,
|
|
944
|
+
TEvents,
|
|
945
|
+
TQueues
|
|
946
|
+
>;
|
|
481
947
|
}>,
|
|
482
948
|
): Promise<{ winner: string; value: T }> {
|
|
483
949
|
const wrappedBranches = branches.map((branch) => ({
|
|
@@ -498,124 +964,33 @@ export class ActorWorkflowContext<
|
|
|
498
964
|
return this.#inner.isEvicted();
|
|
499
965
|
}
|
|
500
966
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
get db(): TDatabase extends never ? never : InferDatabaseClient<TDatabase> {
|
|
517
|
-
this.#ensureActorAccess("db");
|
|
518
|
-
return this.#runCtx.db as TDatabase extends never
|
|
519
|
-
? never
|
|
520
|
-
: InferDatabaseClient<TDatabase>;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
get log() {
|
|
524
|
-
return this.#runCtx.log;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
/** @deprecated No-op. Use `keepAwake(promise)` or `waitUntil(promise)` instead. */
|
|
528
|
-
setPreventSleep(_prevent: boolean): void {
|
|
529
|
-
this.#ensureActorAccess("setPreventSleep");
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
/** @deprecated No-op. Always returns `false`. */
|
|
533
|
-
get preventSleep(): boolean {
|
|
534
|
-
this.#ensureActorAccess("preventSleep");
|
|
535
|
-
return false;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
/**
|
|
539
|
-
* Holds the actor awake for the duration of the provided promise. The
|
|
540
|
-
* actor cannot idle-sleep or finalize the sleep grace period until the
|
|
541
|
-
* promise settles.
|
|
542
|
-
*/
|
|
543
|
-
keepAwake<T>(promise: Promise<T>): Promise<T> {
|
|
544
|
-
this.#ensureActorAccess("keepAwake");
|
|
545
|
-
return this.#runCtx.keepAwake(promise);
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
/**
|
|
549
|
-
* Registers a promise that the sleep grace period will wait on. Use this
|
|
550
|
-
* for best-effort flush/cleanup work that may complete inside the grace
|
|
551
|
-
* window. For work the actor must stay running through, prefer
|
|
552
|
-
* `c.keepAwake(promise)` which also blocks idle sleep.
|
|
553
|
-
*/
|
|
554
|
-
waitUntil(promise: Promise<void>): void {
|
|
555
|
-
this.#ensureActorAccess("waitUntil");
|
|
556
|
-
this.#runCtx.waitUntil(promise);
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
get actorId(): string {
|
|
560
|
-
return this.#runCtx.actorId;
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
broadcast<K extends keyof TEvents & string>(
|
|
564
|
-
name: K,
|
|
565
|
-
...args: InferEventArgs<InferSchemaMap<TEvents>[K]>
|
|
566
|
-
): void;
|
|
567
|
-
broadcast(
|
|
568
|
-
name: keyof TEvents extends never ? string : never,
|
|
569
|
-
...args: Array<unknown>
|
|
570
|
-
): void;
|
|
571
|
-
broadcast(name: string, ...args: Array<unknown>): void {
|
|
572
|
-
this.#ensureActorAccess("broadcast");
|
|
573
|
-
this.#runCtx.broadcast(
|
|
574
|
-
name as never,
|
|
575
|
-
...(args as unknown[] as never[]),
|
|
576
|
-
);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
#toActorQueueMessage<T>(
|
|
580
|
-
message: WorkflowQueueMessage<T>,
|
|
581
|
-
): WorkflowQueueMessage<T> & { id: bigint } {
|
|
582
|
-
let id: bigint;
|
|
583
|
-
try {
|
|
584
|
-
id = BigInt(message.id);
|
|
585
|
-
} catch {
|
|
586
|
-
throw new Error(`Invalid queue message id "${message.id}"`);
|
|
587
|
-
}
|
|
588
|
-
return {
|
|
589
|
-
id,
|
|
590
|
-
name: message.name,
|
|
591
|
-
body: message.body,
|
|
592
|
-
createdAt: message.createdAt,
|
|
593
|
-
...(message.complete ? { complete: message.complete } : {}),
|
|
594
|
-
};
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
async #wrapActive<T>(run: () => Promise<T>): Promise<T> {
|
|
598
|
-
return await this.#runCtx.internalKeepAwake(run);
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
async #withActorAccess<T>(run: () => Promise<T>): Promise<T> {
|
|
602
|
-
this.#actorAccessDepth++;
|
|
603
|
-
if (this.#actorAccessDepth === 1) {
|
|
604
|
-
this.#allowActorAccess = true;
|
|
605
|
-
}
|
|
606
|
-
try {
|
|
607
|
-
return await run();
|
|
608
|
-
} finally {
|
|
609
|
-
this.#actorAccessDepth--;
|
|
610
|
-
if (this.#actorAccessDepth === 0) {
|
|
611
|
-
this.#allowActorAccess = false;
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
async #withActorAccessAndStateRollback<T>(
|
|
617
|
-
run: () => Promise<T>,
|
|
967
|
+
// Runs a user step body inside a fresh step context, snapshotting actor
|
|
968
|
+
// state/vars so a thrown step rolls back its mutations, and deactivating the
|
|
969
|
+
// step context once the body settles so it cannot be used after the step.
|
|
970
|
+
async #runStep<T>(
|
|
971
|
+
run: WorkflowStepRun<
|
|
972
|
+
T,
|
|
973
|
+
TState,
|
|
974
|
+
TConnParams,
|
|
975
|
+
TConnState,
|
|
976
|
+
TVars,
|
|
977
|
+
TInput,
|
|
978
|
+
TDatabase,
|
|
979
|
+
TEvents,
|
|
980
|
+
TQueues
|
|
981
|
+
>,
|
|
618
982
|
): Promise<T> {
|
|
983
|
+
const stepCtx = new WorkflowStepContext<
|
|
984
|
+
TState,
|
|
985
|
+
TConnParams,
|
|
986
|
+
TConnState,
|
|
987
|
+
TVars,
|
|
988
|
+
TInput,
|
|
989
|
+
TDatabase,
|
|
990
|
+
TEvents,
|
|
991
|
+
TQueues
|
|
992
|
+
>(this.#runCtx, () => this.#markGuardTriggered());
|
|
993
|
+
|
|
619
994
|
let stateSnapshot: { state: TState } | null = null;
|
|
620
995
|
try {
|
|
621
996
|
stateSnapshot = { state: this.#runCtx[RAW_STATE_SYMBOL]() };
|
|
@@ -629,33 +1004,77 @@ export class ActorWorkflowContext<
|
|
|
629
1004
|
stateSnapshot.state = structuredClone(stateSnapshot.state);
|
|
630
1005
|
}
|
|
631
1006
|
const varsSnapshot = structuredClone(this.#runCtx.vars);
|
|
1007
|
+
|
|
632
1008
|
try {
|
|
633
|
-
return await
|
|
1009
|
+
return await run(stepCtx);
|
|
634
1010
|
} catch (error) {
|
|
635
1011
|
if (stateSnapshot) {
|
|
636
1012
|
this.#runCtx.state = stateSnapshot.state;
|
|
637
1013
|
}
|
|
638
1014
|
this.#runCtx.vars = varsSnapshot;
|
|
639
1015
|
throw error;
|
|
1016
|
+
} finally {
|
|
1017
|
+
stepCtx[DEACTIVATE_STEP]();
|
|
640
1018
|
}
|
|
641
1019
|
}
|
|
642
1020
|
|
|
643
|
-
#
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
1021
|
+
#toActorQueueMessage<T>(
|
|
1022
|
+
message: WorkflowQueueMessage<T>,
|
|
1023
|
+
): WorkflowQueueMessage<T> & { id: bigint } {
|
|
1024
|
+
let id: bigint;
|
|
1025
|
+
try {
|
|
1026
|
+
id = BigInt(message.id);
|
|
1027
|
+
} catch {
|
|
1028
|
+
throw new Error(`Invalid queue message id "${message.id}"`);
|
|
1029
|
+
}
|
|
1030
|
+
return {
|
|
1031
|
+
id,
|
|
1032
|
+
name: message.name,
|
|
1033
|
+
body: message.body,
|
|
1034
|
+
createdAt: message.createdAt,
|
|
1035
|
+
...(message.complete ? { complete: message.complete } : {}),
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// Runs a step rollback compensation with an active step context. Rollbacks
|
|
1040
|
+
// intentionally mutate actor state, so their writes are not snapshotted.
|
|
1041
|
+
async #runRollback<T>(
|
|
1042
|
+
rollback: WorkflowStepRollback<
|
|
1043
|
+
T,
|
|
1044
|
+
TState,
|
|
1045
|
+
TConnParams,
|
|
1046
|
+
TConnState,
|
|
1047
|
+
TVars,
|
|
1048
|
+
TInput,
|
|
1049
|
+
TDatabase,
|
|
1050
|
+
TEvents,
|
|
1051
|
+
TQueues
|
|
1052
|
+
>,
|
|
1053
|
+
output: T,
|
|
1054
|
+
): Promise<void> {
|
|
1055
|
+
const stepCtx = new WorkflowStepContext<
|
|
1056
|
+
TState,
|
|
1057
|
+
TConnParams,
|
|
1058
|
+
TConnState,
|
|
1059
|
+
TVars,
|
|
1060
|
+
TInput,
|
|
1061
|
+
TDatabase,
|
|
1062
|
+
TEvents,
|
|
1063
|
+
TQueues
|
|
1064
|
+
>(this.#runCtx, () => this.#markGuardTriggered());
|
|
1065
|
+
try {
|
|
1066
|
+
await rollback(stepCtx, output);
|
|
1067
|
+
} finally {
|
|
1068
|
+
stepCtx[DEACTIVATE_STEP]();
|
|
650
1069
|
}
|
|
651
1070
|
}
|
|
652
1071
|
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
this.#guardViolation = false;
|
|
656
|
-
return violated;
|
|
1072
|
+
async #wrapActive<T>(run: () => Promise<T>): Promise<T> {
|
|
1073
|
+
return await this.#runCtx.internalKeepAwake(run);
|
|
657
1074
|
}
|
|
658
1075
|
|
|
1076
|
+
// Records that a step context was used outside its step. Mirrors the value
|
|
1077
|
+
// onto actor state and a KV flag so callers can observe the violation.
|
|
659
1078
|
#markGuardTriggered(): void {
|
|
660
1079
|
try {
|
|
661
1080
|
const state = this.#runCtx.state as Record<string, unknown>;
|
|
@@ -686,7 +1105,7 @@ export class ActorWorkflowContext<
|
|
|
686
1105
|
|
|
687
1106
|
#createChildContext(
|
|
688
1107
|
ctx: WorkflowContextInterface,
|
|
689
|
-
):
|
|
1108
|
+
): WorkflowContext<
|
|
690
1109
|
TState,
|
|
691
1110
|
TConnParams,
|
|
692
1111
|
TConnState,
|
|
@@ -696,7 +1115,7 @@ export class ActorWorkflowContext<
|
|
|
696
1115
|
TEvents,
|
|
697
1116
|
TQueues
|
|
698
1117
|
> {
|
|
699
|
-
return new
|
|
1118
|
+
return new WorkflowContext(ctx, this.#runCtx);
|
|
700
1119
|
}
|
|
701
1120
|
}
|
|
702
1121
|
|
|
@@ -712,7 +1131,7 @@ export type WorkflowContextOf<AD extends AnyActorDefinition> =
|
|
|
712
1131
|
infer Q extends QueueSchemaConfig,
|
|
713
1132
|
any
|
|
714
1133
|
>
|
|
715
|
-
?
|
|
1134
|
+
? WorkflowContext<S, CP, CS, V, I, DB, E, Q>
|
|
716
1135
|
: never;
|
|
717
1136
|
|
|
718
1137
|
export type WorkflowLoopContextOf<AD extends AnyActorDefinition> =
|
|
@@ -722,4 +1141,16 @@ export type WorkflowBranchContextOf<AD extends AnyActorDefinition> =
|
|
|
722
1141
|
WorkflowContextOf<AD>;
|
|
723
1142
|
|
|
724
1143
|
export type WorkflowStepContextOf<AD extends AnyActorDefinition> =
|
|
725
|
-
|
|
1144
|
+
AD extends BaseActorDefinition<
|
|
1145
|
+
infer S,
|
|
1146
|
+
infer CP,
|
|
1147
|
+
infer CS,
|
|
1148
|
+
infer V,
|
|
1149
|
+
infer I,
|
|
1150
|
+
infer DB extends AnyDatabaseProvider,
|
|
1151
|
+
infer E extends EventSchemaConfig,
|
|
1152
|
+
infer Q extends QueueSchemaConfig,
|
|
1153
|
+
any
|
|
1154
|
+
>
|
|
1155
|
+
? WorkflowStepContext<S, CP, CS, V, I, DB, E, Q>
|
|
1156
|
+
: never;
|