@upstash/workflow 0.1.0

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.
@@ -0,0 +1,616 @@
1
+ import { HTTPMethods, Client, Receiver } from '@upstash/qstash';
2
+
3
+ /**
4
+ * Base class outlining steps. Basically, each step kind (run/sleep/sleepUntil)
5
+ * should have two methods: getPlanStep & getResultStep.
6
+ *
7
+ * getPlanStep works the same way for all so it's implemented here.
8
+ * The different step types will implement their own getResultStep method.
9
+ */
10
+ declare abstract class BaseLazyStep<TResult = unknown> {
11
+ readonly stepName: string;
12
+ abstract readonly stepType: StepType;
13
+ constructor(stepName: string);
14
+ /**
15
+ * plan step to submit when step will run parallel with other
16
+ * steps (parallel call state `first`)
17
+ *
18
+ * @param concurrent number of steps running parallel
19
+ * @param targetStep target step id corresponding to this step
20
+ * @returns
21
+ */
22
+ abstract getPlanStep(concurrent: number, targetStep: number): Step<undefined>;
23
+ /**
24
+ * result step to submit after the step executes. Used in single step executions
25
+ * and when a plan step executes in parallel executions (parallel call state `partial`).
26
+ *
27
+ * @param concurrent
28
+ * @param stepId
29
+ */
30
+ abstract getResultStep(concurrent: number, stepId: number): Promise<Step<TResult>>;
31
+ }
32
+
33
+ declare const LOG_LEVELS: readonly ["DEBUG", "INFO", "SUBMIT", "WARN", "ERROR"];
34
+ type LogLevel = (typeof LOG_LEVELS)[number];
35
+ type ChatLogEntry = {
36
+ timestamp: number;
37
+ workflowRunId: string;
38
+ logLevel: LogLevel;
39
+ eventType: "ENDPOINT_START" | "SUBMIT_THIRD_PARTY_RESULT" | "CREATE_CONTEXT" | "SUBMIT_FIRST_INVOCATION" | "RUN_SINGLE" | "RUN_PARALLEL" | "SUBMIT_STEP" | "SUBMIT_CLEANUP" | "RESPONSE_WORKFLOW" | "RESPONSE_DEFAULT" | "ERROR";
40
+ details: unknown;
41
+ };
42
+ type WorkflowLoggerOptions = {
43
+ logLevel: LogLevel;
44
+ logOutput: "console";
45
+ };
46
+ declare class WorkflowLogger {
47
+ private logs;
48
+ private options;
49
+ private workflowRunId?;
50
+ constructor(options: WorkflowLoggerOptions);
51
+ log(level: LogLevel, eventType: ChatLogEntry["eventType"], details?: unknown): Promise<void>;
52
+ setWorkflowRunId(workflowRunId: string): void;
53
+ private writeToConsole;
54
+ private shouldLog;
55
+ static getLogger(verbose?: boolean | WorkflowLogger): WorkflowLogger | undefined;
56
+ }
57
+
58
+ declare class AutoExecutor {
59
+ private context;
60
+ private promises;
61
+ private activeLazyStepList?;
62
+ private debug?;
63
+ private readonly nonPlanStepCount;
64
+ private readonly steps;
65
+ private indexInCurrentList;
66
+ stepCount: number;
67
+ planStepCount: number;
68
+ protected executingStep: string | false;
69
+ constructor(context: WorkflowContext, steps: Step[], debug?: WorkflowLogger);
70
+ /**
71
+ * Adds the step function to the list of step functions to run in
72
+ * parallel. After adding the function, defers the execution, so
73
+ * that if there is another step function to be added, it's also
74
+ * added.
75
+ *
76
+ * After all functions are added, list of functions are executed.
77
+ * If there is a single function, it's executed by itself. If there
78
+ * are multiple, they are run in parallel.
79
+ *
80
+ * If a function is already executing (this.executingStep), this
81
+ * means that there is a nested step which is not allowed. In this
82
+ * case, addStep throws QStashWorkflowError.
83
+ *
84
+ * @param stepInfo step plan to add
85
+ * @returns result of the step function
86
+ */
87
+ addStep<TResult>(stepInfo: BaseLazyStep<TResult>): Promise<TResult>;
88
+ /**
89
+ * Wraps a step function to set this.executingStep to step name
90
+ * before running and set this.executingStep to False after execution
91
+ * ends.
92
+ *
93
+ * this.executingStep allows us to detect nested steps which are not
94
+ * allowed.
95
+ *
96
+ * @param stepName name of the step being wrapped
97
+ * @param stepFunction step function to wrap
98
+ * @returns wrapped step function
99
+ */
100
+ wrapStep<TResult = unknown>(stepName: string, stepFunction: StepFunction<TResult>): TResult | Promise<TResult>;
101
+ /**
102
+ * Executes a step:
103
+ * - If the step result is available in the steps, returns the result
104
+ * - If the result is not avaiable, runs the function
105
+ * - Sends the result to QStash
106
+ *
107
+ * @param lazyStep lazy step to execute
108
+ * @returns step result
109
+ */
110
+ protected runSingle<TResult>(lazyStep: BaseLazyStep<TResult>): Promise<TResult>;
111
+ /**
112
+ * Runs steps in parallel.
113
+ *
114
+ * @param stepName parallel step name
115
+ * @param stepFunctions list of async functions to run in parallel
116
+ * @returns results of the functions run in parallel
117
+ */
118
+ protected runParallel<TResults extends unknown[]>(parallelSteps: {
119
+ [K in keyof TResults]: BaseLazyStep<TResults[K]>;
120
+ }): Promise<TResults>;
121
+ /**
122
+ * Determines the parallel call state
123
+ *
124
+ * First filters the steps to get the steps which are after `initialStepCount` parameter.
125
+ *
126
+ * Depending on the remaining steps, decides the parallel state:
127
+ * - "first": If there are no steps
128
+ * - "last" If there are equal to or more than `2 * parallelStepCount`. We multiply by two
129
+ * because each step in a parallel execution will have 2 steps: a plan step and a result
130
+ * step.
131
+ * - "partial": If the last step is a plan step
132
+ * - "discard": If the last step is not a plan step. This means that the parallel execution
133
+ * is in progress (there are still steps to run) and one step has finished and submitted
134
+ * its result to QStash
135
+ *
136
+ * @param parallelStepCount number of steps to run in parallel
137
+ * @param initialStepCount steps after the parallel invocation
138
+ * @returns parallel call state
139
+ */
140
+ protected getParallelCallState(parallelStepCount: number, initialStepCount: number): ParallelCallState;
141
+ /**
142
+ * sends the steps to QStash as batch
143
+ *
144
+ * @param steps steps to send
145
+ */
146
+ private submitStepsToQStash;
147
+ /**
148
+ * Get the promise by executing the lazt steps list. If there is a single
149
+ * step, we call `runSingle`. Otherwise `runParallel` is called.
150
+ *
151
+ * @param lazyStepList steps list to execute
152
+ * @returns promise corresponding to the execution
153
+ */
154
+ private getExecutionPromise;
155
+ /**
156
+ * @param lazyStepList steps we executed
157
+ * @param result result of the promise from `getExecutionPromise`
158
+ * @param index index of the current step
159
+ * @returns result[index] if lazyStepList > 1, otherwise result
160
+ */
161
+ private static getResult;
162
+ private deferExecution;
163
+ }
164
+
165
+ /**
166
+ * Upstash Workflow context
167
+ *
168
+ * See the docs for fields and methods https://upstash.com/docs/qstash/workflows/basics/context
169
+ */
170
+ declare class WorkflowContext<TInitialPayload = unknown> {
171
+ protected readonly executor: AutoExecutor;
172
+ protected readonly steps: Step[];
173
+ /**
174
+ * QStash client of the workflow
175
+ *
176
+ * Can be overwritten by passing `qstashClient` parameter in `serve`:
177
+ *
178
+ * ```ts
179
+ * import { Client } from "@upstash/qstash"
180
+ *
181
+ * export const POST = serve(
182
+ * async (context) => {
183
+ * ...
184
+ * },
185
+ * {
186
+ * qstashClient: new Client({...})
187
+ * }
188
+ * )
189
+ * ```
190
+ */
191
+ readonly qstashClient: WorkflowClient;
192
+ /**
193
+ * Run id of the workflow
194
+ */
195
+ readonly workflowRunId: string;
196
+ /**
197
+ * URL of the workflow
198
+ *
199
+ * Can be overwritten by passing a `url` parameter in `serve`:
200
+ *
201
+ * ```ts
202
+ * export const POST = serve(
203
+ * async (context) => {
204
+ * ...
205
+ * },
206
+ * {
207
+ * url: "new-url-value"
208
+ * }
209
+ * )
210
+ * ```
211
+ */
212
+ readonly url: string;
213
+ /**
214
+ * URL to call in case of workflow failure with QStash failure callback
215
+ *
216
+ * https://upstash.com/docs/qstash/features/callbacks#what-is-a-failure-callback
217
+ *
218
+ * Can be overwritten by passing a `failureUrl` parameter in `serve`:
219
+ *
220
+ * ```ts
221
+ * export const POST = serve(
222
+ * async (context) => {
223
+ * ...
224
+ * },
225
+ * {
226
+ * failureUrl: "new-url-value"
227
+ * }
228
+ * )
229
+ * ```
230
+ */
231
+ readonly failureUrl?: string;
232
+ /**
233
+ * Payload of the request which started the workflow.
234
+ *
235
+ * To specify its type, you can define `serve` as follows:
236
+ *
237
+ * ```ts
238
+ * // set requestPayload type to MyPayload:
239
+ * export const POST = serve<MyPayload>(
240
+ * async (context) => {
241
+ * ...
242
+ * }
243
+ * )
244
+ * ```
245
+ *
246
+ * By default, `serve` tries to apply `JSON.parse` to the request payload.
247
+ * If your payload is encoded in a format other than JSON, you can utilize
248
+ * the `initialPayloadParser` parameter:
249
+ *
250
+ * ```ts
251
+ * export const POST = serve<MyPayload>(
252
+ * async (context) => {
253
+ * ...
254
+ * },
255
+ * {
256
+ * initialPayloadParser: (initialPayload) => {return doSomething(initialPayload)}
257
+ * }
258
+ * )
259
+ * ```
260
+ */
261
+ readonly requestPayload: TInitialPayload;
262
+ /**
263
+ * headers of the initial request
264
+ */
265
+ readonly headers: Headers;
266
+ /**
267
+ * initial payload as a raw string
268
+ */
269
+ readonly rawInitialPayload: string;
270
+ /**
271
+ * Map of environment variables and their values.
272
+ *
273
+ * Can be set using the `env` option of serve:
274
+ *
275
+ * ```ts
276
+ * export const POST = serve<MyPayload>(
277
+ * async (context) => {
278
+ * const key = context.env["API_KEY"];
279
+ * },
280
+ * {
281
+ * env: {
282
+ * "API_KEY": "*****";
283
+ * }
284
+ * }
285
+ * )
286
+ * ```
287
+ *
288
+ * Default value is set to `process.env`.
289
+ */
290
+ readonly env: Record<string, string | undefined>;
291
+ /**
292
+ * Number of retries
293
+ */
294
+ readonly retries: number;
295
+ constructor({ qstashClient, workflowRunId, headers, steps, url, failureUrl, debug, initialPayload, rawInitialPayload, env, retries, }: {
296
+ qstashClient: WorkflowClient;
297
+ workflowRunId: string;
298
+ headers: Headers;
299
+ steps: Step[];
300
+ url: string;
301
+ failureUrl?: string;
302
+ debug?: WorkflowLogger;
303
+ initialPayload: TInitialPayload;
304
+ rawInitialPayload?: string;
305
+ env?: Record<string, string | undefined>;
306
+ retries?: number;
307
+ });
308
+ /**
309
+ * Executes a workflow step
310
+ *
311
+ * ```typescript
312
+ * const result = await context.run("step 1", () => {
313
+ * return "result"
314
+ * })
315
+ * ```
316
+ *
317
+ * Can also be called in parallel and the steps will be executed
318
+ * simulatenously:
319
+ *
320
+ * ```typescript
321
+ * const [result1, result2] = await Promise.all([
322
+ * context.run("step 1", () => {
323
+ * return "result1"
324
+ * })
325
+ * context.run("step 2", async () => {
326
+ * return await fetchResults()
327
+ * })
328
+ * ])
329
+ * ```
330
+ *
331
+ * @param stepName name of the step
332
+ * @param stepFunction step function to be executed
333
+ * @returns result of the step function
334
+ */
335
+ run<TResult>(stepName: string, stepFunction: StepFunction<TResult>): Promise<TResult>;
336
+ /**
337
+ * Stops the execution for the duration provided.
338
+ *
339
+ * @param stepName
340
+ * @param duration sleep duration in seconds
341
+ * @returns undefined
342
+ */
343
+ sleep(stepName: string, duration: number): Promise<void>;
344
+ /**
345
+ * Stops the execution until the date time provided.
346
+ *
347
+ * @param stepName
348
+ * @param datetime time to sleep until. Can be provided as a number (in unix seconds),
349
+ * as a Date object or a string (passed to `new Date(datetimeString)`)
350
+ * @returns undefined
351
+ */
352
+ sleepUntil(stepName: string, datetime: Date | string | number): Promise<void>;
353
+ /**
354
+ * Makes a third party call through QStash in order to make a
355
+ * network call without consuming any runtime.
356
+ *
357
+ * ```ts
358
+ * const postResult = await context.call<string>(
359
+ * "post call step",
360
+ * `https://www.some-endpoint.com/api`,
361
+ * "POST",
362
+ * "my-payload"
363
+ * );
364
+ * ```
365
+ *
366
+ * tries to parse the result of the request as JSON. If it's
367
+ * not a JSON which can be parsed, simply returns the response
368
+ * body as it is.
369
+ *
370
+ * @param stepName
371
+ * @param url url to call
372
+ * @param method call method
373
+ * @param body call body
374
+ * @param headers call headers
375
+ * @returns call result (parsed as JSON if possible)
376
+ */
377
+ call<TResult = unknown, TBody = unknown>(stepName: string, url: string, method: HTTPMethods, body?: TBody, headers?: Record<string, string>): Promise<TResult>;
378
+ waitForEvent(stepName: string, eventId: string, timeout: string | number): Promise<WaitStepResponse>;
379
+ /**
380
+ * Adds steps to the executor. Needed so that it can be overwritten in
381
+ * DisabledWorkflowContext.
382
+ */
383
+ protected addStep<TResult = unknown>(step: BaseLazyStep<TResult>): Promise<TResult>;
384
+ }
385
+
386
+ /**
387
+ * Interface for Client with required methods
388
+ *
389
+ * Neeeded to resolve import issues
390
+ */
391
+ type WorkflowClient = {
392
+ batchJSON: InstanceType<typeof Client>["batchJSON"];
393
+ publishJSON: InstanceType<typeof Client>["publishJSON"];
394
+ http: InstanceType<typeof Client>["http"];
395
+ };
396
+ /**
397
+ * Interface for Receiver with required methods
398
+ *
399
+ * Neeeded to resolve import issues
400
+ */
401
+ type WorkflowReceiver = {
402
+ verify: InstanceType<typeof Receiver>["verify"];
403
+ };
404
+ declare const StepTypes: readonly ["Initial", "Run", "SleepFor", "SleepUntil", "Call", "Wait", "Notify"];
405
+ type StepType = (typeof StepTypes)[number];
406
+ type ThirdPartyCallFields<TBody = unknown> = {
407
+ /**
408
+ * Third party call URL. Set when context.call is used.
409
+ */
410
+ callUrl: string;
411
+ /**
412
+ * Third party call method. Set when context.call is used.
413
+ */
414
+ callMethod: HTTPMethods;
415
+ /**
416
+ * Third party call body. Set when context.call is used.
417
+ */
418
+ callBody: TBody;
419
+ /**
420
+ * Third party call headers. Set when context.call is used.
421
+ */
422
+ callHeaders: Record<string, string>;
423
+ };
424
+ type WaitFields = {
425
+ waitEventId: string;
426
+ timeout: string;
427
+ waitTimeout?: boolean;
428
+ };
429
+ type NotifyFields = {
430
+ notifyEventId?: string;
431
+ notifyBody?: string;
432
+ };
433
+ type Step<TResult = unknown, TBody = unknown> = {
434
+ /**
435
+ * index of the step
436
+ */
437
+ stepId: number;
438
+ /**
439
+ * name of the step
440
+ */
441
+ stepName: string;
442
+ /**
443
+ * type of the step (Initial/Run/SleepFor/SleepUntil/Call)
444
+ */
445
+ stepType: StepType;
446
+ /**
447
+ * step result. Set if context.run or context.call are used.
448
+ */
449
+ out?: TResult;
450
+ /**
451
+ * sleep duration in seconds. Set when context.sleep is used.
452
+ */
453
+ sleepFor?: number;
454
+ /**
455
+ * unix timestamp (in seconds) to wait until. Set when context.sleepUntil is used.
456
+ */
457
+ sleepUntil?: number;
458
+ /**
459
+ * number of steps running concurrently if the step is in a parallel run.
460
+ * Set to 1 if step is not parallel.
461
+ */
462
+ concurrent: number;
463
+ /**
464
+ * target step of a plan step. In other words, the step to assign the
465
+ * result of a plan step.
466
+ *
467
+ * undefined if the step is not a plan step (of a parallel run). Otherwise,
468
+ * set to the target step.
469
+ */
470
+ targetStep?: number;
471
+ } & (ThirdPartyCallFields<TBody> | {
472
+ [P in keyof ThirdPartyCallFields]?: never;
473
+ }) & (WaitFields | {
474
+ [P in keyof WaitFields]?: never;
475
+ }) & (NotifyFields | {
476
+ [P in keyof NotifyFields]?: never;
477
+ });
478
+ type RawStep = {
479
+ messageId: string;
480
+ body: string;
481
+ callType: "step" | "toCallback" | "fromCallback";
482
+ };
483
+ type SyncStepFunction<TResult> = () => TResult;
484
+ type AsyncStepFunction<TResult> = () => Promise<TResult>;
485
+ type StepFunction<TResult> = AsyncStepFunction<TResult> | SyncStepFunction<TResult>;
486
+ type ParallelCallState = "first" | "partial" | "discard" | "last";
487
+ type RouteFunction<TInitialPayload> = (context: WorkflowContext<TInitialPayload>) => Promise<void>;
488
+ type FinishCondition = "success" | "duplicate-step" | "fromCallback" | "auth-fail" | "failure-callback";
489
+ type WorkflowServeOptions<TResponse extends Response = Response, TInitialPayload = unknown> = {
490
+ /**
491
+ * QStash client
492
+ */
493
+ qstashClient?: WorkflowClient;
494
+ /**
495
+ * Function called to return a response after each step execution
496
+ *
497
+ * @param workflowRunId
498
+ * @returns response
499
+ */
500
+ onStepFinish?: (workflowRunId: string, finishCondition: FinishCondition) => TResponse;
501
+ /**
502
+ * Function to parse the initial payload passed by the user
503
+ */
504
+ initialPayloadParser?: (initialPayload: string) => TInitialPayload;
505
+ /**
506
+ * Url of the endpoint where the workflow is set up.
507
+ *
508
+ * If not set, url will be inferred from the request.
509
+ */
510
+ url?: string;
511
+ /**
512
+ * Verbose mode
513
+ *
514
+ * Disabled if not set. If set to true, a logger is created automatically.
515
+ *
516
+ * Alternatively, a WorkflowLogger can be passed.
517
+ */
518
+ verbose?: WorkflowLogger | true;
519
+ /**
520
+ * Receiver to verify *all* requests by checking if they come from QStash
521
+ *
522
+ * By default, a receiver is created from the env variables
523
+ * QSTASH_CURRENT_SIGNING_KEY and QSTASH_NEXT_SIGNING_KEY if they are set.
524
+ */
525
+ receiver?: WorkflowReceiver;
526
+ /**
527
+ * Url to call if QStash retries are exhausted while executing the workflow
528
+ */
529
+ failureUrl?: string;
530
+ /**
531
+ * Failure function called when QStash retries are exhausted while executing
532
+ * the workflow. Will overwrite `failureUrl` parameter with the workflow
533
+ * endpoint if passed.
534
+ *
535
+ * @param context workflow context at the moment of error
536
+ * @param failStatus error status
537
+ * @param failResponse error message
538
+ * @returns void
539
+ */
540
+ failureFunction?: (context: Omit<WorkflowContext, "run" | "sleepUntil" | "sleep" | "call">, failStatus: number, failResponse: string, failHeader: Record<string, string[]>) => Promise<void> | void;
541
+ /**
542
+ * Base Url of the workflow endpoint
543
+ *
544
+ * Can be used to set if there is a local tunnel or a proxy between
545
+ * QStash and the workflow endpoint.
546
+ *
547
+ * Will be set to the env variable UPSTASH_WORKFLOW_URL if not passed.
548
+ * If the env variable is not set, the url will be infered as usual from
549
+ * the `request.url` or the `url` parameter in `serve` options.
550
+ *
551
+ * @default undefined
552
+ */
553
+ baseUrl?: string;
554
+ /**
555
+ * Optionally, one can pass an env object mapping environment
556
+ * variables to their keys.
557
+ *
558
+ * Useful in cases like cloudflare with hono.
559
+ */
560
+ env?: Record<string, string | undefined>;
561
+ /**
562
+ * Number of retries to use in workflow requests
563
+ *
564
+ * 3 by default
565
+ */
566
+ retries?: number;
567
+ };
568
+ /**
569
+ * Payload passed as body in failureFunction
570
+ */
571
+ type FailureFunctionPayload = {
572
+ /**
573
+ * error name
574
+ */
575
+ error: string;
576
+ /**
577
+ * error message
578
+ */
579
+ message: string;
580
+ };
581
+ /**
582
+ * Makes all fields except the ones selected required
583
+ */
584
+ type RequiredExceptFields<T, K extends keyof T> = Omit<Required<T>, K> & Partial<Pick<T, K>>;
585
+ type WaitResult<TResult = unknown> = {
586
+ result: TResult;
587
+ timeout: boolean;
588
+ };
589
+ type Waiter = {
590
+ url: string;
591
+ deadline: number;
592
+ headers: Record<string, string[]>;
593
+ timeoutUrl: string;
594
+ timeoutBody: unknown;
595
+ timeoutHeaders: Record<string, string[]>;
596
+ };
597
+ type NotifyResponse = {
598
+ waiter: Waiter;
599
+ messageId: string;
600
+ deduplicated: boolean;
601
+ error: string;
602
+ };
603
+ type WaitRequest = {
604
+ url: string;
605
+ timeout: string;
606
+ timeoutBody?: string;
607
+ timeoutUrl?: string;
608
+ timeoutHeaders?: Record<string, string[]>;
609
+ step: Step;
610
+ };
611
+ type WaitStepResponse = {
612
+ timeout: boolean;
613
+ notifyBody: unknown;
614
+ };
615
+
616
+ export { type AsyncStepFunction as A, type FinishCondition as F, type LogLevel as L, type NotifyResponse as N, type ParallelCallState as P, type RouteFunction as R, type Step as S, type WorkflowServeOptions as W, WorkflowContext as a, type WorkflowClient as b, type WorkflowReceiver as c, StepTypes as d, type StepType as e, type RawStep as f, type SyncStepFunction as g, type StepFunction as h, type FailureFunctionPayload as i, type RequiredExceptFields as j, type WaitResult as k, type Waiter as l, type WaitRequest as m, type WaitStepResponse as n, type WorkflowLoggerOptions as o, WorkflowLogger as p };