@upstash/workflow 1.4.0 → 1.5.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.
- package/README.md +9 -0
- package/astro.d.mts +19 -2
- package/astro.d.ts +19 -2
- package/astro.js +670 -159
- package/astro.mjs +1 -1
- package/{chunk-DRHEHWMS.mjs → chunk-J3PXPHSB.mjs} +828 -317
- package/cloudflare.d.mts +17 -2
- package/cloudflare.d.ts +17 -2
- package/cloudflare.js +670 -159
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +374 -0
- package/docs/agents/getting-started.mdx +139 -0
- package/docs/agents/overview.mdx +40 -0
- package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
- package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
- package/docs/agents/patterns/parallelization.mdx +77 -0
- package/docs/agents/patterns/prompt-chaining.mdx +84 -0
- package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
- package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
- package/docs/basics/best-practices/deterministic-code.mdx +132 -0
- package/docs/basics/best-practices/idempotency.mdx +38 -0
- package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
- package/docs/basics/best-practices/overview.mdx +35 -0
- package/docs/basics/best-practices/promise-any.mdx +9 -0
- package/docs/basics/best-practices/return-step-results.mdx +80 -0
- package/docs/basics/best-practices/workflow-limits.mdx +62 -0
- package/docs/basics/client/cancel.mdx +137 -0
- package/docs/basics/client/dlq/callback.mdx +21 -0
- package/docs/basics/client/dlq/delete.mdx +120 -0
- package/docs/basics/client/dlq/list.mdx +96 -0
- package/docs/basics/client/dlq/restart.mdx +164 -0
- package/docs/basics/client/dlq/resume.mdx +167 -0
- package/docs/basics/client/logs.mdx +139 -0
- package/docs/basics/client/notify.mdx +79 -0
- package/docs/basics/client/trigger.mdx +139 -0
- package/docs/basics/client/waiters.mdx +31 -0
- package/docs/basics/client.mdx +40 -0
- package/docs/basics/how.mdx +65 -0
- package/docs/basics/serve/advanced.mdx +435 -0
- package/docs/basics/serve.mdx +142 -0
- package/docs/changelog.mdx +174 -0
- package/docs/examples/allInOne.mdx +377 -0
- package/docs/examples/authWebhook.mdx +567 -0
- package/docs/examples/customRetry.mdx +309 -0
- package/docs/examples/customerOnboarding.mdx +230 -0
- package/docs/examples/dynamicWorkflow.mdx +203 -0
- package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
- package/docs/examples/imageProcessing.mdx +339 -0
- package/docs/examples/paymentRetry.mdx +438 -0
- package/docs/examples/waitForEvent.mdx +201 -0
- package/docs/features/dlq/callback.mdx +42 -0
- package/docs/features/dlq/delete.mdx +29 -0
- package/docs/features/dlq/restart.mdx +33 -0
- package/docs/features/dlq/resume.mdx +37 -0
- package/docs/features/dlq.mdx +39 -0
- package/docs/features/failure-callback.mdx +102 -0
- package/docs/features/failureFunction/advanced.mdx +36 -0
- package/docs/features/failureFunction/reliability.mdx +33 -0
- package/docs/features/flow-control/monitor.mdx +22 -0
- package/docs/features/flow-control/parallelism.mdx +45 -0
- package/docs/features/flow-control/rate-period.mdx +46 -0
- package/docs/features/flow-control.mdx +164 -0
- package/docs/features/invoke/serveMany.mdx +109 -0
- package/docs/features/invoke.mdx +39 -0
- package/docs/features/notify.mdx +127 -0
- package/docs/features/parallel-steps.mdx +41 -0
- package/docs/features/retries/prevent-retries.mdx +140 -0
- package/docs/features/retries.mdx +93 -0
- package/docs/features/wait-for-event.mdx +104 -0
- package/docs/features/wait.mdx +44 -0
- package/docs/features/webhooks.mdx +116 -0
- package/docs/getstarted.mdx +200 -0
- package/docs/howto/cancel.mdx +50 -0
- package/docs/howto/changes.mdx +33 -0
- package/docs/howto/configure.mdx +42 -0
- package/docs/howto/failures.mdx +185 -0
- package/docs/howto/get-token.mdx +18 -0
- package/docs/howto/local-development/development-server.mdx +130 -0
- package/docs/howto/local-development/local-tunnel.mdx +73 -0
- package/docs/howto/middlewares.mdx +263 -0
- package/docs/howto/migrations.mdx +335 -0
- package/docs/howto/multi-region.mdx +107 -0
- package/docs/howto/parallel-runs.mdx +52 -0
- package/docs/howto/readonly-token.mdx +19 -0
- package/docs/howto/realtime/basic.mdx +315 -0
- package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
- package/docs/howto/redact-fields.mdx +61 -0
- package/docs/howto/retry-now.mdx +41 -0
- package/docs/howto/schedule.mdx +104 -0
- package/docs/howto/security.mdx +143 -0
- package/docs/howto/singleton.mdx +137 -0
- package/docs/howto/start.mdx +69 -0
- package/docs/howto/use-webhooks.mdx +256 -0
- package/docs/integrations/aisdk.mdx +316 -0
- package/docs/integrations/anthropic.mdx +51 -0
- package/docs/integrations/datadog.mdx +69 -0
- package/docs/integrations/openai.mdx +87 -0
- package/docs/integrations/prometheus.mdx +65 -0
- package/docs/integrations/resend.mdx +82 -0
- package/docs/llms-txt.mdx +4 -0
- package/docs/openapi.yaml +2944 -0
- package/docs/pricing.mdx +22 -0
- package/docs/quickstarts/astro.mdx +185 -0
- package/docs/quickstarts/cloudflare-workers.mdx +190 -0
- package/docs/quickstarts/express.mdx +190 -0
- package/docs/quickstarts/fastapi.mdx +282 -0
- package/docs/quickstarts/flask.mdx +282 -0
- package/docs/quickstarts/hono.mdx +221 -0
- package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
- package/docs/quickstarts/nextjs-flask.mdx +147 -0
- package/docs/quickstarts/nuxt.mdx +169 -0
- package/docs/quickstarts/platforms.mdx +78 -0
- package/docs/quickstarts/solidjs.mdx +161 -0
- package/docs/quickstarts/svelte.mdx +170 -0
- package/docs/quickstarts/tanstack-start.mdx +205 -0
- package/docs/quickstarts/vercel-nextjs.mdx +233 -0
- package/docs/roadmap.mdx +8 -0
- package/docs/sdk/workflow-js.mdx +4 -0
- package/docs/sdk/workflow-py.mdx +4 -0
- package/docs/steps/api.mdx +64 -0
- package/docs/steps/call.mdx +183 -0
- package/docs/steps/cancel.mdx +25 -0
- package/docs/steps/createWebhook.mdx +40 -0
- package/docs/steps/invoke.mdx +100 -0
- package/docs/steps/notify.mdx +105 -0
- package/docs/steps/run.mdx +150 -0
- package/docs/steps/sleep.mdx +97 -0
- package/docs/steps/sleepUntil.mdx +88 -0
- package/docs/steps/waitForEvent.mdx +60 -0
- package/docs/steps/waitForWebhook.mdx +78 -0
- package/docs/steps.mdx +108 -0
- package/docs/troubleshooting/general.mdx +331 -0
- package/docs/troubleshooting/vercel.mdx +82 -0
- package/express.d.mts +19 -2
- package/express.d.ts +19 -2
- package/express.js +670 -159
- package/express.mjs +1 -1
- package/h3.d.mts +19 -2
- package/h3.d.ts +19 -2
- package/h3.js +670 -159
- package/h3.mjs +1 -1
- package/hono.d.mts +17 -2
- package/hono.d.ts +17 -2
- package/hono.js +670 -159
- package/hono.mjs +1 -1
- package/index.d.mts +50 -21
- package/index.d.ts +50 -21
- package/index.js +686 -171
- package/index.mjs +17 -13
- package/nextjs.d.mts +40 -2
- package/nextjs.d.ts +40 -2
- package/nextjs.js +670 -159
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +16 -2
- package/react-router.d.ts +16 -2
- package/react-router.js +670 -159
- package/react-router.mjs +1 -1
- package/{serve-many-CG3BFvO3.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-iJF1IUXk.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +10 -1
- package/solidjs.d.ts +10 -1
- package/solidjs.js +670 -159
- package/solidjs.mjs +1 -1
- package/src/platforms/astro.ts +84 -0
- package/src/platforms/cloudflare.ts +127 -0
- package/src/platforms/express.ts +147 -0
- package/src/platforms/h3.ts +105 -0
- package/src/platforms/hono.ts +103 -0
- package/src/platforms/nextjs.ts +193 -0
- package/src/platforms/react-router.ts +92 -0
- package/src/platforms/solidjs.ts +59 -0
- package/src/platforms/svelte.ts +99 -0
- package/src/platforms/tanstack.ts +96 -0
- package/src/src/client/dlq.ts +387 -0
- package/src/src/client/filter-types.ts +166 -0
- package/src/src/client/index.ts +350 -0
- package/src/src/client/types.ts +501 -0
- package/src/src/client/utils.ts +307 -0
- package/src/src/constants.ts +82 -0
- package/src/src/context/api/anthropic.ts +58 -0
- package/src/src/context/api/base.ts +52 -0
- package/src/src/context/api/index.ts +24 -0
- package/src/src/context/api/openai.ts +125 -0
- package/src/src/context/api/resend.ts +57 -0
- package/src/src/context/auto-executor.ts +707 -0
- package/src/src/context/context.ts +681 -0
- package/src/src/context/index.ts +1 -0
- package/src/src/context/provider.ts +37 -0
- package/src/src/context/steps.ts +965 -0
- package/src/src/error.ts +203 -0
- package/src/src/index.ts +13 -0
- package/src/src/middleware/default-callbacks.ts +20 -0
- package/src/src/middleware/index.ts +2 -0
- package/src/src/middleware/logging.ts +39 -0
- package/src/src/middleware/manager.ts +148 -0
- package/src/src/middleware/middleware.ts +46 -0
- package/src/src/middleware/types.ts +72 -0
- package/src/src/qstash/headers.ts +397 -0
- package/src/src/qstash/step-config.ts +268 -0
- package/src/src/qstash/submit-steps.ts +284 -0
- package/src/src/serve/authorization.ts +118 -0
- package/src/src/serve/index.ts +475 -0
- package/src/src/serve/multi-region/handlers.ts +225 -0
- package/src/src/serve/multi-region/utils.ts +88 -0
- package/src/src/serve/options.ts +241 -0
- package/src/src/serve/serve-many.ts +117 -0
- package/src/src/serve/utils.ts +11 -0
- package/src/src/types.ts +590 -0
- package/src/src/utils.ts +141 -0
- package/src/src/workflow-parser.ts +472 -0
- package/src/src/workflow-requests.ts +575 -0
- package/svelte.d.mts +17 -2
- package/svelte.d.ts +17 -2
- package/svelte.js +670 -159
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +15 -2
- package/tanstack.d.ts +15 -2
- package/tanstack.js +670 -159
- package/tanstack.mjs +1 -1
- package/{types-CekOpKvz.d.ts → types-CrgK5P6p.d.mts} +372 -32
- package/{types-CekOpKvz.d.mts → types-CrgK5P6p.d.ts} +372 -32
package/src/src/error.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { QstashError } from "@upstash/qstash";
|
|
2
|
+
import type { Duration, FailureFunctionPayload, Step } from "./types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error raised during Workflow execution
|
|
6
|
+
*/
|
|
7
|
+
export class WorkflowError extends QstashError {
|
|
8
|
+
constructor(message: string) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "WorkflowError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Base error for workflow abort scenarios
|
|
16
|
+
* Raised when a workflow step executes successfully.
|
|
17
|
+
*/
|
|
18
|
+
export class WorkflowAbort extends Error {
|
|
19
|
+
public stepName: string;
|
|
20
|
+
public stepInfo?: Step;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param stepName name of the aborting step
|
|
24
|
+
* @param stepInfo step information
|
|
25
|
+
*/
|
|
26
|
+
constructor(stepName: string, stepInfo?: Step) {
|
|
27
|
+
super(
|
|
28
|
+
"This is an Upstash Workflow error thrown after a step executes. It is expected to be raised." +
|
|
29
|
+
" Make sure that you await for each step. Also, if you are using try/catch blocks, you should not wrap context.run/sleep/sleepUntil/call methods with try/catch." +
|
|
30
|
+
` Aborting workflow after executing step '${stepName}'.`
|
|
31
|
+
);
|
|
32
|
+
this.name = "WorkflowAbort";
|
|
33
|
+
this.stepName = stepName;
|
|
34
|
+
this.stepInfo = stepInfo;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Raised during authorization/dry-run to indicate a step was found
|
|
40
|
+
*/
|
|
41
|
+
export class WorkflowAuthError extends WorkflowAbort {
|
|
42
|
+
/**
|
|
43
|
+
* @param stepName name of the step found during authorization
|
|
44
|
+
*/
|
|
45
|
+
constructor(stepName: string) {
|
|
46
|
+
super(stepName);
|
|
47
|
+
this.name = "WorkflowAuthError";
|
|
48
|
+
this.message =
|
|
49
|
+
"This is an Upstash Workflow error thrown during authorization check." +
|
|
50
|
+
` Found step '${stepName}' during dry-run.`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Raised when user explicitly cancels the workflow via context.cancel()
|
|
56
|
+
*/
|
|
57
|
+
export class WorkflowCancelAbort extends WorkflowAbort {
|
|
58
|
+
constructor() {
|
|
59
|
+
super("cancel");
|
|
60
|
+
this.name = "WorkflowCancelAbort";
|
|
61
|
+
this.message = "Workflow has been canceled by user via context.cancel().";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Raised when the workflow is failed due to a non-retryable error
|
|
67
|
+
*
|
|
68
|
+
* @see node_modules/@upstash/workflow/docs/features/retries/prevent-retries.mdx
|
|
69
|
+
*/
|
|
70
|
+
export class WorkflowNonRetryableError extends WorkflowAbort {
|
|
71
|
+
/**
|
|
72
|
+
* @param message error message to be displayed
|
|
73
|
+
*/
|
|
74
|
+
constructor(message?: string) {
|
|
75
|
+
super("non-retryable-error");
|
|
76
|
+
this.name = "WorkflowNonRetryableError";
|
|
77
|
+
this.message = message ?? "Workflow failed with non-retryable error.";
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class WorkflowRetryAfterError extends WorkflowAbort {
|
|
82
|
+
public retryAfter: number | Duration;
|
|
83
|
+
/**
|
|
84
|
+
* @param message error message to be displayed
|
|
85
|
+
* @param retryAfter time in seconds after which the workflow should be retried
|
|
86
|
+
*/
|
|
87
|
+
constructor(message: string, retryAfter: number | Duration) {
|
|
88
|
+
super("retry-after-error");
|
|
89
|
+
this.name = "WorkflowRetryAfterError";
|
|
90
|
+
this.message = message;
|
|
91
|
+
this.retryAfter = retryAfter;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Formats an unknown error to match the FailureFunctionPayload format
|
|
97
|
+
*
|
|
98
|
+
* @param error error to format
|
|
99
|
+
* @returns formatted error payload
|
|
100
|
+
*/
|
|
101
|
+
export const formatWorkflowError = (error: unknown): FailureFunctionPayload => {
|
|
102
|
+
return error instanceof Error
|
|
103
|
+
? {
|
|
104
|
+
error: error.name,
|
|
105
|
+
message: error.message,
|
|
106
|
+
stack: error.stack,
|
|
107
|
+
}
|
|
108
|
+
: {
|
|
109
|
+
error: "Error",
|
|
110
|
+
message:
|
|
111
|
+
"An error occured while executing workflow: " +
|
|
112
|
+
`'${typeof error === "string" ? error : JSON.stringify(error)}'`,
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* An error that may carry the name of the step that was executing when it was
|
|
118
|
+
* thrown. The serve handler reads this to report the failing step name to QStash
|
|
119
|
+
* via the `Upstash-Error-Step-Name` response header (so it shows up in Workflow Logs).
|
|
120
|
+
*/
|
|
121
|
+
type ErrorWithStepName = {
|
|
122
|
+
errorStepName?: string;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Records the name of the currently executing step on an error so it can be
|
|
127
|
+
* surfaced later when building the error response. The first recorded name wins,
|
|
128
|
+
* so the step closest to where the error originated is preserved.
|
|
129
|
+
*
|
|
130
|
+
* @param error error thrown while executing a step
|
|
131
|
+
* @param stepName name of the step that was executing
|
|
132
|
+
*/
|
|
133
|
+
export const attachStepNameToError = (error: unknown, stepName: string): void => {
|
|
134
|
+
if (typeof error !== "object" || error === null) return;
|
|
135
|
+
if ("errorStepName" in error) return;
|
|
136
|
+
if (!Object.isExtensible(error)) return;
|
|
137
|
+
try {
|
|
138
|
+
(error as ErrorWithStepName).errorStepName = stepName;
|
|
139
|
+
} catch {
|
|
140
|
+
// best-effort metadata only; never mask the original error
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Reads the step name recorded by `attachStepNameToError`.
|
|
146
|
+
*
|
|
147
|
+
* @param error error to read the step name from
|
|
148
|
+
* @returns the recorded step name, or undefined if none was recorded
|
|
149
|
+
*/
|
|
150
|
+
export const getStepNameFromError = (error: unknown): string | undefined => {
|
|
151
|
+
if (typeof error === "object" && error !== null) {
|
|
152
|
+
const stepName = (error as ErrorWithStepName).errorStepName;
|
|
153
|
+
return typeof stepName === "string" ? stepName : undefined;
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Gets the constructor name of an object.
|
|
160
|
+
*
|
|
161
|
+
* @param obj object to get constructor name from
|
|
162
|
+
* @returns constructor name or null
|
|
163
|
+
*/
|
|
164
|
+
function getConstructorName(obj: unknown): string | null {
|
|
165
|
+
if (obj === null || obj === undefined) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const ctor = obj.constructor;
|
|
169
|
+
if (!ctor || ctor.name === "Object") {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return ctor.name;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Gets all constructor names in the prototype chain.
|
|
177
|
+
*
|
|
178
|
+
* @param obj object to get constructor names from
|
|
179
|
+
* @returns array of constructor names
|
|
180
|
+
*/
|
|
181
|
+
function getConstructorNames(obj: unknown): string[] {
|
|
182
|
+
const proto = Object.getPrototypeOf(obj);
|
|
183
|
+
const name = getConstructorName(proto);
|
|
184
|
+
if (name === null) {
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
return [name, ...getConstructorNames(proto)];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Checks if a value is an instance of a specific class.
|
|
192
|
+
*
|
|
193
|
+
* @param v value to check
|
|
194
|
+
* @param c class constructor to check against
|
|
195
|
+
* @returns true if v is an instance of c
|
|
196
|
+
*/
|
|
197
|
+
export function isInstanceOf<T>(
|
|
198
|
+
v: unknown,
|
|
199
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
200
|
+
ctor: new (...args: any[]) => T
|
|
201
|
+
): v is T {
|
|
202
|
+
return getConstructorNames(v).includes(ctor.name);
|
|
203
|
+
}
|
package/src/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { serve } from "./serve"; // exclude serveBase
|
|
2
|
+
export * from "./context";
|
|
3
|
+
export * from "./types";
|
|
4
|
+
export * from "./client/types";
|
|
5
|
+
export * from "./middleware";
|
|
6
|
+
export * from "./client";
|
|
7
|
+
export {
|
|
8
|
+
WorkflowError,
|
|
9
|
+
WorkflowAbort,
|
|
10
|
+
WorkflowNonRetryableError,
|
|
11
|
+
WorkflowRetryAfterError,
|
|
12
|
+
} from "./error";
|
|
13
|
+
export { WorkflowMiddleware, loggingMiddleware } from "./middleware";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MiddlewareCallbacks } from "./types";
|
|
2
|
+
|
|
3
|
+
export const onErrorWithConsole: Required<
|
|
4
|
+
MiddlewareCallbacks<unknown, unknown>
|
|
5
|
+
>["onError"] = async ({ workflowRunId, error }) => {
|
|
6
|
+
console.error(` [Upstash Workflow]: Error in workflow run ${workflowRunId}: ` + error);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const onWarningWithConsole: Required<
|
|
10
|
+
MiddlewareCallbacks<unknown, unknown>
|
|
11
|
+
>["onWarning"] = async ({ workflowRunId, warning }) => {
|
|
12
|
+
console.warn(` [Upstash Workflow]: Warning in workflow run ${workflowRunId}: ` + warning);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const onInfoWithConsole: Required<MiddlewareCallbacks<unknown, unknown>>["onInfo"] = async ({
|
|
16
|
+
workflowRunId,
|
|
17
|
+
info,
|
|
18
|
+
}) => {
|
|
19
|
+
console.info(` [Upstash Workflow]: Info in workflow run ${workflowRunId}: ` + info);
|
|
20
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { onErrorWithConsole, onInfoWithConsole, onWarningWithConsole } from "./default-callbacks";
|
|
2
|
+
import { WorkflowMiddleware } from "./middleware";
|
|
3
|
+
|
|
4
|
+
export const loggingMiddleware = new WorkflowMiddleware<unknown>({
|
|
5
|
+
name: "logging",
|
|
6
|
+
callbacks: {
|
|
7
|
+
afterExecution(params) {
|
|
8
|
+
const { context, ...rest } = params;
|
|
9
|
+
console.log(" [Upstash Workflow]: Step executed:", {
|
|
10
|
+
workflowRunId: context.workflowRunId,
|
|
11
|
+
...rest,
|
|
12
|
+
});
|
|
13
|
+
},
|
|
14
|
+
beforeExecution(params) {
|
|
15
|
+
const { context, ...rest } = params;
|
|
16
|
+
console.log(" [Upstash Workflow]: Step execution started:", {
|
|
17
|
+
workflowRunId: context.workflowRunId,
|
|
18
|
+
...rest,
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
runStarted(params) {
|
|
22
|
+
const { context, ...rest } = params;
|
|
23
|
+
console.log(" [Upstash Workflow]: Workflow run started:", {
|
|
24
|
+
workflowRunId: context.workflowRunId,
|
|
25
|
+
...rest,
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
runCompleted(params) {
|
|
29
|
+
const { context, ...rest } = params;
|
|
30
|
+
console.log(" [Upstash Workflow]: Workflow run completed:", {
|
|
31
|
+
workflowRunId: context.workflowRunId,
|
|
32
|
+
...rest,
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
onError: onErrorWithConsole,
|
|
36
|
+
onWarning: onWarningWithConsole,
|
|
37
|
+
onInfo: onInfoWithConsole,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { WorkflowContext } from "../context";
|
|
2
|
+
import { WorkflowError } from "../error";
|
|
3
|
+
import { onErrorWithConsole, onWarningWithConsole } from "./default-callbacks";
|
|
4
|
+
import { WorkflowMiddleware } from "./middleware";
|
|
5
|
+
import {
|
|
6
|
+
DebugEvent,
|
|
7
|
+
DebugEventParameters,
|
|
8
|
+
LifeCycleEvent,
|
|
9
|
+
LifeCycleEventParameters,
|
|
10
|
+
} from "./types";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* MiddlewareManager - Simplified middleware dispatcher
|
|
14
|
+
*
|
|
15
|
+
* This class manages middleware execution without requiring generics everywhere.
|
|
16
|
+
* Once created, you can export dispatch methods that handle both lifecycle and debug events.
|
|
17
|
+
*/
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
export class MiddlewareManager<TInitialPayload = any, TResult = any> {
|
|
20
|
+
private middlewares: WorkflowMiddleware<TInitialPayload, TResult>[];
|
|
21
|
+
private workflowRunId: string | undefined;
|
|
22
|
+
private context: WorkflowContext<TInitialPayload> | undefined;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param middlewares list of workflow middlewares
|
|
26
|
+
*/
|
|
27
|
+
constructor(middlewares: WorkflowMiddleware<TInitialPayload, TResult>[] = []) {
|
|
28
|
+
this.middlewares = middlewares;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Assign workflow run ID - will be passed to debug events
|
|
33
|
+
*
|
|
34
|
+
* @param workflowRunId workflow run id to assign
|
|
35
|
+
*/
|
|
36
|
+
assignWorkflowRunId(workflowRunId: string) {
|
|
37
|
+
this.workflowRunId = workflowRunId;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Assign context - required for lifecycle events
|
|
42
|
+
*
|
|
43
|
+
* also assigns workflowRunId from context
|
|
44
|
+
*
|
|
45
|
+
* @param context workflow context to assign
|
|
46
|
+
*/
|
|
47
|
+
assignContext(context: WorkflowContext<TInitialPayload>) {
|
|
48
|
+
this.context = context;
|
|
49
|
+
this.workflowRunId = context.workflowRunId;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Internal method to execute middlewares with common error handling logic
|
|
54
|
+
*
|
|
55
|
+
* @param event event name to dispatch
|
|
56
|
+
* @param params event parameters
|
|
57
|
+
*/
|
|
58
|
+
private async executeMiddlewares<K extends DebugEvent | LifeCycleEvent>(
|
|
59
|
+
event: K,
|
|
60
|
+
params: unknown
|
|
61
|
+
): Promise<void> {
|
|
62
|
+
// Initialize all middlewares first
|
|
63
|
+
await Promise.all(this.middlewares.map((m) => m.ensureInit()));
|
|
64
|
+
|
|
65
|
+
// Execute callbacks
|
|
66
|
+
await Promise.all(
|
|
67
|
+
this.middlewares.map(async (middleware) => {
|
|
68
|
+
const callback = middleware.getCallback(event);
|
|
69
|
+
if (callback) {
|
|
70
|
+
try {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
await callback(params as any);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
try {
|
|
75
|
+
const onErrorCallback = middleware.getCallback("onError") ?? onErrorWithConsole;
|
|
76
|
+
await onErrorCallback({
|
|
77
|
+
workflowRunId: this.workflowRunId,
|
|
78
|
+
error: error as Error,
|
|
79
|
+
});
|
|
80
|
+
} catch (onErrorError) {
|
|
81
|
+
console.error(
|
|
82
|
+
`Failed while executing "onError" of middleware "${middleware.name}", falling back to logging the error to console. Error: ${onErrorError}`
|
|
83
|
+
);
|
|
84
|
+
onErrorWithConsole({
|
|
85
|
+
workflowRunId: this.workflowRunId,
|
|
86
|
+
error: error as Error,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (event === "onError") {
|
|
95
|
+
onErrorWithConsole({
|
|
96
|
+
workflowRunId: this.workflowRunId,
|
|
97
|
+
...(params as DebugEventParameters["onError"]),
|
|
98
|
+
});
|
|
99
|
+
} else if (event === "onWarning") {
|
|
100
|
+
onWarningWithConsole({
|
|
101
|
+
workflowRunId: this.workflowRunId,
|
|
102
|
+
...(params as DebugEventParameters["onWarning"]),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Dispatch a debug event (onError, onWarning, onInfo)
|
|
109
|
+
*
|
|
110
|
+
* @param event debug event name
|
|
111
|
+
* @param params event parameters
|
|
112
|
+
*/
|
|
113
|
+
async dispatchDebug<K extends DebugEvent>(
|
|
114
|
+
event: K,
|
|
115
|
+
params: DebugEventParameters[K]
|
|
116
|
+
): Promise<void> {
|
|
117
|
+
const paramsWithRunId = {
|
|
118
|
+
...params,
|
|
119
|
+
workflowRunId: this.workflowRunId,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
await this.executeMiddlewares(event, paramsWithRunId);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Dispatch a lifecycle event (beforeExecution, afterExecution, runStarted, runCompleted)
|
|
127
|
+
*
|
|
128
|
+
* @param event lifecycle event name
|
|
129
|
+
* @param params event parameters
|
|
130
|
+
*/
|
|
131
|
+
async dispatchLifecycle<K extends LifeCycleEvent>(
|
|
132
|
+
event: K,
|
|
133
|
+
params: LifeCycleEventParameters<TResult>[K]
|
|
134
|
+
): Promise<void> {
|
|
135
|
+
if (!this.context) {
|
|
136
|
+
throw new WorkflowError(
|
|
137
|
+
`Something went wrong while calling middlewares. Lifecycle event "${event}" was called before assignContext.`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const paramsWithContext = {
|
|
142
|
+
...params,
|
|
143
|
+
context: this.context,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
await this.executeMiddlewares(event, paramsWithContext);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { WorkflowError } from "../error";
|
|
2
|
+
import { MiddlewareCallbacks, MiddlewareInitCallbacks, MiddlewareParameters } from "./types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @see node_modules/@upstash/workflow/docs/howto/middlewares.mdx
|
|
6
|
+
*/
|
|
7
|
+
export class WorkflowMiddleware<TInitialPayload = unknown, TResult = unknown> {
|
|
8
|
+
public readonly name: string;
|
|
9
|
+
private initCallbacks?: MiddlewareInitCallbacks<TInitialPayload, TResult>;
|
|
10
|
+
/**
|
|
11
|
+
* Callback functions
|
|
12
|
+
*
|
|
13
|
+
* Initially set to undefined, will be populated after init is called
|
|
14
|
+
*/
|
|
15
|
+
private middlewareCallbacks?: MiddlewareCallbacks<TInitialPayload, TResult> = undefined;
|
|
16
|
+
|
|
17
|
+
constructor(parameters: MiddlewareParameters<TInitialPayload, TResult>) {
|
|
18
|
+
this.name = parameters.name;
|
|
19
|
+
|
|
20
|
+
if ("init" in parameters) {
|
|
21
|
+
this.initCallbacks = parameters.init;
|
|
22
|
+
} else {
|
|
23
|
+
this.middlewareCallbacks = parameters.callbacks;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async ensureInit() {
|
|
28
|
+
if (!this.middlewareCallbacks) {
|
|
29
|
+
if (!this.initCallbacks) {
|
|
30
|
+
throw new WorkflowError(`Middleware "${this.name}" has no callbacks or init defined.`);
|
|
31
|
+
}
|
|
32
|
+
this.middlewareCallbacks = await this.initCallbacks();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Gets a callback function by name.
|
|
38
|
+
*
|
|
39
|
+
* @param callback name of the callback to retrieve
|
|
40
|
+
*/
|
|
41
|
+
getCallback<K extends keyof MiddlewareCallbacks<TInitialPayload, TResult>>(
|
|
42
|
+
callback: K
|
|
43
|
+
): MiddlewareCallbacks<TInitialPayload, TResult>[K] | undefined {
|
|
44
|
+
return this.middlewareCallbacks?.[callback];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { WorkflowContext } from "../context";
|
|
2
|
+
|
|
3
|
+
export type LifeCycleEvent = "beforeExecution" | "afterExecution" | "runStarted" | "runCompleted";
|
|
4
|
+
export type DebugEvent = "onError" | "onWarning" | "onInfo";
|
|
5
|
+
|
|
6
|
+
export type LifeCycleEventParameters<TResult> = {
|
|
7
|
+
beforeExecution: {
|
|
8
|
+
stepName: string;
|
|
9
|
+
};
|
|
10
|
+
afterExecution: {
|
|
11
|
+
stepName: string;
|
|
12
|
+
result: unknown;
|
|
13
|
+
};
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
15
|
+
runStarted: {};
|
|
16
|
+
runCompleted: {
|
|
17
|
+
result: TResult;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type DebugEventParameters = {
|
|
22
|
+
onError: { error: Error };
|
|
23
|
+
onWarning: { warning: string };
|
|
24
|
+
onInfo: { info: string };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type MiddlewareCallbacks<TInitialPayload, TResult> = Partial<
|
|
28
|
+
{
|
|
29
|
+
[K in LifeCycleEvent]: (
|
|
30
|
+
params: LifeCycleEventParameters<TResult>[K] & {
|
|
31
|
+
context: WorkflowContext<TInitialPayload>;
|
|
32
|
+
}
|
|
33
|
+
) => Promise<void> | void;
|
|
34
|
+
} & {
|
|
35
|
+
[K in DebugEvent]: (
|
|
36
|
+
params: DebugEventParameters[K] & { workflowRunId?: string }
|
|
37
|
+
) => Promise<void> | void;
|
|
38
|
+
}
|
|
39
|
+
>;
|
|
40
|
+
|
|
41
|
+
export type MiddlewareInitCallbacks<TInitialPayload, TResult> = () =>
|
|
42
|
+
| Promise<MiddlewareCallbacks<TInitialPayload, TResult>>
|
|
43
|
+
| MiddlewareCallbacks<TInitialPayload, TResult>;
|
|
44
|
+
|
|
45
|
+
export type MiddlewareCallbackConfig<TInitialPayload, TResult> =
|
|
46
|
+
| {
|
|
47
|
+
init: MiddlewareInitCallbacks<TInitialPayload, TResult>;
|
|
48
|
+
}
|
|
49
|
+
| {
|
|
50
|
+
callbacks: MiddlewareCallbacks<TInitialPayload, TResult>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type MiddlewareParameters<TInitialPayload, TResult> = {
|
|
54
|
+
name: string;
|
|
55
|
+
} & MiddlewareCallbackConfig<TInitialPayload, TResult>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Type for the dispatch debug method that can be passed to helper functions
|
|
59
|
+
* without needing to pass generics everywhere
|
|
60
|
+
*/
|
|
61
|
+
export type DispatchDebug = <K extends DebugEvent>(
|
|
62
|
+
event: K,
|
|
63
|
+
params: DebugEventParameters[K]
|
|
64
|
+
) => Promise<void>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Type for the dispatch lifecycle method
|
|
68
|
+
*/
|
|
69
|
+
export type DispatchLifecycle<TResult = unknown> = <K extends LifeCycleEvent>(
|
|
70
|
+
event: K,
|
|
71
|
+
params: LifeCycleEventParameters<TResult>[K]
|
|
72
|
+
) => Promise<void>;
|