@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
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Development Server"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Upstash Workflow is built on top of Upstash QStash.
|
|
6
|
+
The QStash CLI provides a local development server that performs QStash functionality locally for development and testing purposes.
|
|
7
|
+
|
|
8
|
+
## Automatic dev server (recommended)
|
|
9
|
+
|
|
10
|
+
If you are using `@upstash/workflow`, you can just set `QSTASH_DEV=true` in your environment, and the SDK will download and connect to the dev server automatically. No tokens or signing keys required.
|
|
11
|
+
|
|
12
|
+
```bash .env
|
|
13
|
+
QSTASH_DEV=true
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
With `QSTASH_DEV=true` set, both the workflow client and the `serve()` endpoint pick up the dev server automatically. The endpoint also verifies incoming signatures against the dev server's deterministic signing keys, so signature verification works end-to-end with no extra setup.
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
// app/api/workflow/route.ts
|
|
20
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
21
|
+
|
|
22
|
+
export const { POST } = serve(async (context) => {
|
|
23
|
+
await context.run("step-1", () => console.log("running locally"));
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Client } from "@upstash/workflow";
|
|
29
|
+
|
|
30
|
+
const client = new Client({ token: process.env.QSTASH_TOKEN ?? "" });
|
|
31
|
+
|
|
32
|
+
await client.trigger({
|
|
33
|
+
url: "http://localhost:3000/api/workflow",
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For details on the dev server behavior, ports, and the `registerQStashDev()` helper for Next.js edge routes, see the [QStash Local Development docs](/qstash/howto/local-development).
|
|
38
|
+
|
|
39
|
+
## Manual setup
|
|
40
|
+
|
|
41
|
+
If you would rather start and manage the QStash dev server yourself, follow the steps below.
|
|
42
|
+
|
|
43
|
+
<Steps>
|
|
44
|
+
<Step title="Install and Start Development Server">
|
|
45
|
+
Start the development server using the QStash CLI:
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
npx @upstash/qstash-cli dev
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The QStash CLI output will look something like this:
|
|
52
|
+
|
|
53
|
+
```plaintext QStash CLI Output
|
|
54
|
+
Upstash QStash development server is runnning at
|
|
55
|
+
|
|
56
|
+
A default user has been created for you to authorize your requests.
|
|
57
|
+
QSTASH_TOKEN=eyJVc2VySUQiOiJkZWZhdWx0VXNlciIsIlBhc3N3b3JkIjoiZGVmYXVsdFBhc3N3b3JkIn0=
|
|
58
|
+
QSTASH_CURRENT_SIGNING_KEY=sig_7RvLjqfZBvP5KEUimQCE1pvpLuou
|
|
59
|
+
QSTASH_NEXT_SIGNING_KEY=sig_7W3ZNbfKWk5NWwEs3U4ixuQ7fxwE
|
|
60
|
+
|
|
61
|
+
Sample cURL request:
|
|
62
|
+
curl -X POST http://127.0.0.1:8080/v2/publish/https://example.com -H "Authorization: Bearer eyJVc2VySUQiOiJkZWZhdWx0VXNlciIsIlBhc3N3b3JkIjoiZGVmYXVsdFBhc3N3b3JkIn0="
|
|
63
|
+
|
|
64
|
+
Check out documentation for more details:
|
|
65
|
+
https://upstash.com/docs/qstash/howto/local-development
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
For detailed instructions on setting up the development server, see our [QStash Local Development Guide](/qstash/howto/local-development).
|
|
69
|
+
</Step>
|
|
70
|
+
|
|
71
|
+
<Step title="Enable Local Mode on Console">
|
|
72
|
+
Once you start the local server, you can go to the Workflow tab on Upstash Console and enable local mode, which will allow you to monitor and debug workflow runs with the local server.
|
|
73
|
+
|
|
74
|
+
<img src="/img/workflow/local-mode-workflow.png" alt="Local Workflow server flow diagram" />
|
|
75
|
+
</Step>
|
|
76
|
+
|
|
77
|
+
<Step title="Update Environment Variables">
|
|
78
|
+
Once your development server is running, update your environment variables to route QStash requests to your local server.
|
|
79
|
+
|
|
80
|
+
```env
|
|
81
|
+
QSTASH_URL="http://127.0.0.1:8080"
|
|
82
|
+
QSTASH_TOKEN="eyJVc2VySUQiOiJkZWZhdWx0VXNlciIsIlBhc3N3b3JkIjoiZGVmYXVsdFBhc3N3b3JkIn0="
|
|
83
|
+
QSTASH_CURRENT_SIGNING_KEY="sig_7RvLjqfZBvP5KEUimQCE1pvpLuou"
|
|
84
|
+
QSTASH_NEXT_SIGNING_KEY="sig_7W3ZNbfKWk5NWwEs3U4ixuQ7fxwE"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
</Step>
|
|
88
|
+
|
|
89
|
+
<Step title="Use local addresses">
|
|
90
|
+
It's all set up 🎉
|
|
91
|
+
|
|
92
|
+
Now, you can use your local address when triggering the workflow runs.
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
import { Client } from "@upstash/workflow";
|
|
97
|
+
|
|
98
|
+
const client = Client()
|
|
99
|
+
|
|
100
|
+
const { workflowRunId } = await client.trigger({
|
|
101
|
+
url: `http://localhost:3000/api/workflow`,
|
|
102
|
+
retries: 3
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
<Tip>
|
|
106
|
+
Inside the `trigger()` call, you need to provide the URL of your workflow endpoint:
|
|
107
|
+
|
|
108
|
+
- Local development → use the URL where your app is running, for example: http://localhost:3000/api/PATH
|
|
109
|
+
- Production → use the URL of your deployed app, for example: https://yourapp.com/api/PATH
|
|
110
|
+
|
|
111
|
+
To avoid hardcoding URLs, you can define a `BASE_URL` constant and set it based on the environment.
|
|
112
|
+
A common pattern is to check an environment variable that only exists in production:
|
|
113
|
+
|
|
114
|
+
```javascript
|
|
115
|
+
const BASE_URL = process.env.VERCEL_URL
|
|
116
|
+
? `https://${process.env.VERCEL_URL}`
|
|
117
|
+
: `http://localhost:3000`
|
|
118
|
+
|
|
119
|
+
const { workflowRunId } = await client.trigger({
|
|
120
|
+
url: `${BASE_URL}/api/workflow`,
|
|
121
|
+
retries: 3
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
</Tip>
|
|
125
|
+
|
|
126
|
+
</Step>
|
|
127
|
+
|
|
128
|
+
</Steps>
|
|
129
|
+
|
|
130
|
+
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Local Tunnel for Upstash Workflow Development
|
|
3
|
+
sidebarTitle: "Local Tunnel"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Upstash Workflow requires your application to be publicly accessible in production.
|
|
7
|
+
The recommended approach is running the development server we provide locally and work with local addresses.
|
|
8
|
+
An alternative is to making your application publibly accessible so that you can work with the managed Upstash Workflow servers.
|
|
9
|
+
|
|
10
|
+
The easiest way to make a local URL publically available is [ngrok](https://ngrok.com), a free tunneling service.
|
|
11
|
+
|
|
12
|
+
Create an account on [dashboard.ngrok.com/signup](https://dashboard.ngrok.com/signup) and follow the [setup instructions](https://dashboard.ngrok.com/get-started/setup) to download the ngrok CLI and connect your account. This process takes only a few minutes and is totally free.
|
|
13
|
+
|
|
14
|
+
You can connect your account like this:
|
|
15
|
+
|
|
16
|
+
<Tabs>
|
|
17
|
+
<Tab title="macOS">
|
|
18
|
+
<Frame>
|
|
19
|
+
<img src="/img/qstash-workflow/ngrok_mac_setup.png" alt="Configure an ngrok tunnel on macOS" />
|
|
20
|
+
</Frame>
|
|
21
|
+
</Tab>
|
|
22
|
+
|
|
23
|
+
<Tab title="Windows">
|
|
24
|
+
<Frame>
|
|
25
|
+
<img src="/img/qstash-workflow/ngrok_windows_setup.png" alt="Configure an ngrok tunnel on Windows" />
|
|
26
|
+
</Frame>
|
|
27
|
+
</Tab>
|
|
28
|
+
</Tabs>
|
|
29
|
+
|
|
30
|
+
Once you have installed the ngrok CLI, add your ngrok-issued auth token like this:
|
|
31
|
+
|
|
32
|
+
```bash Terminal
|
|
33
|
+
ngrok config add-authtoken <YOUR-AUTH-TOKEN>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
and replace `<YOUR-AUTH-TOKEN>` with your actual auth token.
|
|
37
|
+
|
|
38
|
+
### Start the tunnel
|
|
39
|
+
|
|
40
|
+
Make your local server available publically by running the following command:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
ngrok http <PORT>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
for example, if your Next.js server is running on port `3000`, the command is:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
ngrok http 3000
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The output will look something like this:
|
|
53
|
+
|
|
54
|
+
```plaintext
|
|
55
|
+
Session Status online
|
|
56
|
+
Account <YOUR-NAME> (Plan: Free)
|
|
57
|
+
Version 3.1.0
|
|
58
|
+
Region Europe (eu)
|
|
59
|
+
Latency -
|
|
60
|
+
Web Interface http://127.0.0.1:4040
|
|
61
|
+
Forwarding https://e02f-2a02-810d-af40-5284-b139-58cc-89df-b740.eu.ngrok.io -> http://localhost:3000
|
|
62
|
+
Connections ttl opn rt1 rt5 p50 p90
|
|
63
|
+
0 0 0.00 0.00 0.00 0.00
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The long URL in the `Forwarding` line serves the same purpose as your localhost URL, the only difference being that it is publically accessible. We need this URL to make our workflow available to QStash for local development, either as the `baseUrl` parameter or the `UPSTASH_WORKFLOW_URL` environment variable (both options provide the same functionality).
|
|
67
|
+
|
|
68
|
+
Note: The `UPSTASH_WORKFLOW_URL` environment variable is only necessary for local development. In production, the `baseUrl` parameter is automatically set and can be omitted.
|
|
69
|
+
|
|
70
|
+
<Tip>
|
|
71
|
+
Ensure that the port of your local server matches the one you're using with ngrok. For example, if your server is
|
|
72
|
+
running on port 8080, use `ngrok http 8080`.
|
|
73
|
+
</Tip>
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Middlewares"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Middlewares allow you to intercept and respond to workflow lifecycle events and debug messages. They're useful for logging, monitoring, error tracking, and custom integrations.
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
A middleware can hook into:
|
|
10
|
+
- **Lifecycle Events**: Run started, run completed, before/after step execution
|
|
11
|
+
- **Debug Events**: Errors, warnings, and info messages
|
|
12
|
+
|
|
13
|
+
## Built-in Middleware
|
|
14
|
+
|
|
15
|
+
Upstash Workflow provides a built-in logging middleware that you can use out of the box:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
19
|
+
import { loggingMiddleware } from "@upstash/workflow";
|
|
20
|
+
|
|
21
|
+
export const { POST } = serve(
|
|
22
|
+
async (context) => {
|
|
23
|
+
await context.run("step-1", () => {
|
|
24
|
+
return "Hello World";
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
middlewares: [loggingMiddleware]
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The logging middleware outputs detailed execution logs to your application's console, including:
|
|
34
|
+
- When workflow runs start and complete
|
|
35
|
+
- Before and after each step execution
|
|
36
|
+
- Error, warning, and info messages
|
|
37
|
+
|
|
38
|
+
## Creating Custom Middleware
|
|
39
|
+
|
|
40
|
+
You can create your own middleware by instantiating a `WorkflowMiddleware` class.
|
|
41
|
+
|
|
42
|
+
### Using Direct Callbacks
|
|
43
|
+
|
|
44
|
+
The simplest way to create a middleware is by providing callbacks directly:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { WorkflowMiddleware } from "@upstash/workflow";
|
|
48
|
+
|
|
49
|
+
const customMiddleware = new WorkflowMiddleware({
|
|
50
|
+
name: "custom-logger",
|
|
51
|
+
callbacks: {
|
|
52
|
+
// Lifecycle events
|
|
53
|
+
runStarted: async ({ context }) => {
|
|
54
|
+
console.log(`Workflow ${context.workflowRunId} started`);
|
|
55
|
+
},
|
|
56
|
+
beforeExecution: async ({ context, stepName }) => {
|
|
57
|
+
console.log(`Executing step: ${stepName}`);
|
|
58
|
+
},
|
|
59
|
+
afterExecution: async ({ context, stepName, result }) => {
|
|
60
|
+
console.log(`Step ${stepName} completed with result:`, result);
|
|
61
|
+
},
|
|
62
|
+
runCompleted: async ({ context, result }) => {
|
|
63
|
+
console.log(`Workflow ${context.workflowRunId} completed:`, result);
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
// Debug events
|
|
67
|
+
onError: async ({ workflowRunId, error }) => {
|
|
68
|
+
console.error(`Error in ${workflowRunId}:`, error);
|
|
69
|
+
},
|
|
70
|
+
onWarning: async ({ workflowRunId, warning }) => {
|
|
71
|
+
console.warn(`Warning in ${workflowRunId}:`, warning);
|
|
72
|
+
},
|
|
73
|
+
onInfo: async ({ workflowRunId, info }) => {
|
|
74
|
+
console.info(`Info from ${workflowRunId}:`, info);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Using Init Function
|
|
81
|
+
|
|
82
|
+
For middlewares that need to initialize resources (like database connections or external clients), use the `init` pattern:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { WorkflowMiddleware } from "@upstash/workflow";
|
|
86
|
+
|
|
87
|
+
const databaseMiddleware = new WorkflowMiddleware({
|
|
88
|
+
name: "database-logger",
|
|
89
|
+
init: async () => {
|
|
90
|
+
// Initialize your resources
|
|
91
|
+
const db = await connectToDatabase();
|
|
92
|
+
|
|
93
|
+
// Return the callbacks that use the initialized resources
|
|
94
|
+
return {
|
|
95
|
+
runStarted: async ({ context }) => {
|
|
96
|
+
await db.insert({ workflowRunId: context.workflowRunId, status: 'started' });
|
|
97
|
+
},
|
|
98
|
+
runCompleted: async ({ context, result }) => {
|
|
99
|
+
await db.update({ workflowRunId: context.workflowRunId, status: 'completed', result });
|
|
100
|
+
},
|
|
101
|
+
onError: async ({ workflowRunId, error }) => {
|
|
102
|
+
await db.insert({ workflowRunId, level: 'error', message: error.message });
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Event Types
|
|
110
|
+
|
|
111
|
+
### Lifecycle Events
|
|
112
|
+
|
|
113
|
+
<ParamField path="runStarted" type="function">
|
|
114
|
+
Called when a workflow run begins.
|
|
115
|
+
|
|
116
|
+
**Parameters:**
|
|
117
|
+
- `context`: The workflow context
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
runStarted: async ({ context }) => {
|
|
121
|
+
// Handle run start
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
</ParamField>
|
|
125
|
+
|
|
126
|
+
<ParamField path="beforeExecution" type="function">
|
|
127
|
+
Called before each step executes.
|
|
128
|
+
|
|
129
|
+
**Parameters:**
|
|
130
|
+
- `context`: The workflow context
|
|
131
|
+
- `stepName`: Name of the step about to execute
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
beforeExecution: async ({ context, stepName }) => {
|
|
135
|
+
// Handle step start
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
</ParamField>
|
|
139
|
+
|
|
140
|
+
<ParamField path="afterExecution" type="function">
|
|
141
|
+
Called after each step completes.
|
|
142
|
+
|
|
143
|
+
**Parameters:**
|
|
144
|
+
- `context`: The workflow context
|
|
145
|
+
- `stepName`: Name of the completed step
|
|
146
|
+
- `result`: The result returned by the step
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
afterExecution: async ({ context, stepName, result }) => {
|
|
150
|
+
// Handle step completion
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
</ParamField>
|
|
154
|
+
|
|
155
|
+
<ParamField path="runCompleted" type="function">
|
|
156
|
+
Called when the entire workflow run finishes.
|
|
157
|
+
|
|
158
|
+
**Parameters:**
|
|
159
|
+
- `context`: The workflow context
|
|
160
|
+
- `result`: The final result of the workflow
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
runCompleted: async ({ context, result }) => {
|
|
164
|
+
// Handle run completion
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
</ParamField>
|
|
168
|
+
|
|
169
|
+
### Debug Events
|
|
170
|
+
|
|
171
|
+
<ParamField path="onError" type="function">
|
|
172
|
+
Called when an error occurs.
|
|
173
|
+
|
|
174
|
+
**Parameters:**
|
|
175
|
+
- `workflowRunId`: The workflow run ID (optional)
|
|
176
|
+
- `error`: The error object
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
onError: async ({ workflowRunId, error }) => {
|
|
180
|
+
// Handle error
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
</ParamField>
|
|
184
|
+
|
|
185
|
+
<ParamField path="onWarning" type="function">
|
|
186
|
+
Called when a warning is logged.
|
|
187
|
+
|
|
188
|
+
**Parameters:**
|
|
189
|
+
- `workflowRunId`: The workflow run ID (optional)
|
|
190
|
+
- `warning`: The warning message
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
onWarning: async ({ workflowRunId, warning }) => {
|
|
194
|
+
// Handle warning
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
</ParamField>
|
|
198
|
+
|
|
199
|
+
<ParamField path="onInfo" type="function">
|
|
200
|
+
Called when an info message is logged.
|
|
201
|
+
|
|
202
|
+
**Parameters:**
|
|
203
|
+
- `workflowRunId`: The workflow run ID (optional)
|
|
204
|
+
- `info`: The info message
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
onInfo: async ({ workflowRunId, info }) => {
|
|
208
|
+
// Handle info
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
</ParamField>
|
|
212
|
+
|
|
213
|
+
## Examples
|
|
214
|
+
|
|
215
|
+
### Error Tracking Middleware
|
|
216
|
+
|
|
217
|
+
Send errors to an external monitoring service:
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { WorkflowMiddleware } from "@upstash/workflow";
|
|
221
|
+
|
|
222
|
+
const errorTrackingMiddleware = new WorkflowMiddleware({
|
|
223
|
+
name: "error-tracking",
|
|
224
|
+
callbacks: {
|
|
225
|
+
onError: async ({ workflowRunId, error }) => {
|
|
226
|
+
await fetch("https://your-monitoring-service.com/errors", {
|
|
227
|
+
method: "POST",
|
|
228
|
+
headers: { "Content-Type": "application/json" },
|
|
229
|
+
body: JSON.stringify({
|
|
230
|
+
workflowRunId,
|
|
231
|
+
error: error.message,
|
|
232
|
+
stack: error.stack,
|
|
233
|
+
timestamp: new Date().toISOString()
|
|
234
|
+
})
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Multiple Middlewares
|
|
242
|
+
|
|
243
|
+
You can use multiple middlewares together:
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
247
|
+
import { loggingMiddleware } from "@upstash/workflow";
|
|
248
|
+
|
|
249
|
+
export const { POST } = serve(
|
|
250
|
+
async (context) => {
|
|
251
|
+
// Your workflow logic
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
middlewares: [
|
|
255
|
+
loggingMiddleware,
|
|
256
|
+
errorTrackingMiddleware,
|
|
257
|
+
performanceMiddleware
|
|
258
|
+
]
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Middlewares are executed in the order they're provided in the array.
|