workflow 5.0.0-beta.4 → 5.0.0-beta.6
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/api.d.ts +5 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +14 -2
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +2 -2
- package/docs/ai/index.mdx +6 -5
- package/docs/api-reference/vitest/index.mdx +28 -1
- package/docs/api-reference/workflow-api/start.mdx +5 -4
- package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +16 -6
- package/docs/api-reference/workflow-next/with-workflow.mdx +32 -0
- package/docs/changelog/eager-processing.mdx +595 -0
- package/docs/changelog/index.mdx +2 -1
- package/docs/cookbook/advanced/child-workflows.mdx +13 -24
- package/docs/cookbook/advanced/meta.json +1 -6
- package/docs/cookbook/agent-patterns/agent-cancellation.mdx +29 -78
- package/docs/cookbook/agent-patterns/durable-agent.mdx +4 -0
- package/docs/cookbook/common-patterns/scheduling.mdx +4 -0
- package/docs/cookbook/common-patterns/timeouts.mdx +1 -1
- package/docs/cookbook/common-patterns/workflow-composition.mdx +7 -14
- package/docs/cookbook/index.mdx +0 -1
- package/docs/cookbook/integrations/ai-sdk.mdx +4 -0
- package/docs/cookbook/integrations/chat-sdk.mdx +4 -0
- package/docs/cookbook/integrations/sandbox.mdx +4 -0
- package/docs/deploying/building-a-world.mdx +1 -1
- package/docs/deploying/world/postgres-world.mdx +5 -3
- package/docs/deploying/world/vercel-world.mdx +2 -0
- package/docs/errors/abort-signal-timeout-in-workflow.mdx +80 -0
- package/docs/errors/hook-conflict.mdx +56 -4
- package/docs/foundations/cancellation.mdx +460 -0
- package/docs/foundations/errors-and-retries.mdx +7 -3
- package/docs/foundations/index.mdx +3 -0
- package/docs/foundations/meta.json +3 -1
- package/docs/foundations/serialization.mdx +77 -41
- package/docs/foundations/starting-workflows.mdx +5 -1
- package/docs/foundations/versioning.mdx +263 -0
- package/docs/getting-started/astro.mdx +6 -0
- package/docs/getting-started/index.mdx +6 -7
- package/docs/getting-started/meta.json +1 -0
- package/docs/getting-started/nestjs.mdx +8 -0
- package/docs/getting-started/next.mdx +5 -3
- package/docs/getting-started/nitro.mdx +22 -0
- package/docs/getting-started/sveltekit.mdx +6 -0
- package/docs/getting-started/tanstack-start.mdx +241 -0
- package/docs/how-it-works/cancellation.mdx +287 -0
- package/docs/how-it-works/code-transform.mdx +2 -2
- package/docs/how-it-works/event-sourcing.mdx +2 -2
- package/docs/how-it-works/meta.json +2 -1
- package/docs/internal/index.mdx +19 -0
- package/docs/internal/meta.json +5 -0
- package/docs/internal/serializable-abort-controller.mdx +148 -0
- package/docs/migration-guides/migrating-from-aws-step-functions.mdx +7 -12
- package/docs/migration-guides/migrating-from-inngest.mdx +7 -17
- package/docs/migration-guides/migrating-from-temporal.mdx +5 -10
- package/docs/migration-guides/migrating-from-trigger-dev.mdx +8 -17
- package/package.json +13 -12
- package/docs/cookbook/advanced/distributed-abort-controller.mdx +0 -318
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Versioning
|
|
3
|
+
description: Understand how workflow runs are pinned to deployments, how to recover runs after a fix, and how to opt in to newer code explicitly.
|
|
4
|
+
type: guide
|
|
5
|
+
summary: Keep in-flight runs stable by default, then choose explicit upgrade boundaries when you need them.
|
|
6
|
+
prerequisites:
|
|
7
|
+
- /docs/foundations/starting-workflows
|
|
8
|
+
related:
|
|
9
|
+
- /docs/api-reference/workflow-api/start
|
|
10
|
+
- /docs/foundations/cancellation
|
|
11
|
+
- /cookbook/common-patterns/workflow-composition
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
Workflow runs are pinned to the deployment that starts them. When a run begins, Workflow SDK records the deployment for that run and continues executing the run on that same copy of your code.
|
|
15
|
+
|
|
16
|
+
That default is intentional. Durable workflows can pause for minutes, days, or months. If the code underneath a paused run changed every time you deployed, an in-flight run could resume into a different function body, different step names, or different input types than the ones it started with. That can make type safety fragile and can break long-running work in hard-to-debug ways.
|
|
17
|
+
|
|
18
|
+
With Workflow SDK, you can keep shipping. New runs use new deployments, while existing runs keep the version they already understand.
|
|
19
|
+
|
|
20
|
+
## Default behavior
|
|
21
|
+
|
|
22
|
+
Start a workflow normally:
|
|
23
|
+
|
|
24
|
+
```typescript title="app/api/orders/route.ts" lineNumbers
|
|
25
|
+
import { start } from "workflow/api";
|
|
26
|
+
import { fulfillOrder } from "@/workflows/fulfill-order";
|
|
27
|
+
|
|
28
|
+
export async function POST(request: Request) {
|
|
29
|
+
const { orderId } = await request.json();
|
|
30
|
+
|
|
31
|
+
const run = await start(fulfillOrder, [orderId]); // [!code highlight]
|
|
32
|
+
|
|
33
|
+
return Response.json({ runId: run.runId });
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The run is tied to the deployment that handled this request. If you deploy a new version while the workflow is [sleeping](/docs/api-reference/workflow/sleep), [waiting on a hook](/docs/foundations/hooks), [retrying a step](/docs/foundations/errors-and-retries), or processing later queue messages, that existing run still resumes on the original deployment.
|
|
38
|
+
|
|
39
|
+
```typescript title="workflows/fulfill-order.ts" lineNumbers
|
|
40
|
+
import { sleep } from "workflow";
|
|
41
|
+
|
|
42
|
+
export async function fulfillOrder(orderId: string) {
|
|
43
|
+
"use workflow";
|
|
44
|
+
|
|
45
|
+
await reserveInventory(orderId);
|
|
46
|
+
await sleep("2d");
|
|
47
|
+
await chargeCustomer(orderId);
|
|
48
|
+
await shipOrder(orderId);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function reserveInventory(orderId: string) {
|
|
52
|
+
"use step";
|
|
53
|
+
// ...
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function chargeCustomer(orderId: string) {
|
|
57
|
+
"use step";
|
|
58
|
+
// ...
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function shipOrder(orderId: string) {
|
|
62
|
+
"use step";
|
|
63
|
+
// ...
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If you deploy a change to `chargeCustomer()` while a run is in the two-day sleep, the existing run does not suddenly resume into the new implementation. It continues on the deployment it started on. The next order starts on the latest deployment and uses the new code from the beginning.
|
|
68
|
+
|
|
69
|
+
## Fixing in-flight runs
|
|
70
|
+
|
|
71
|
+
Sometimes you deploy because the old code had a bug. The safest fix is usually explicit:
|
|
72
|
+
|
|
73
|
+
1. Deploy the fixed code.
|
|
74
|
+
2. Find the affected runs in [observability](/docs/observability) or with the CLI.
|
|
75
|
+
3. Cancel the old runs if they are still running.
|
|
76
|
+
4. Rerun them on the latest deployment with the same inputs.
|
|
77
|
+
|
|
78
|
+
This keeps the version boundary visible. The old run ends as cancelled or failed, and the replacement run starts fresh on the fixed deployment. This is a good fit for one-off, ad-hoc upgrades where you explicitly opt in to moving affected runs onto a new version.
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
# Inspect affected runs and copy the exact workflowName value.
|
|
82
|
+
npx workflow inspect runs \
|
|
83
|
+
--backend vercel \
|
|
84
|
+
--status running
|
|
85
|
+
|
|
86
|
+
# Cancel one run.
|
|
87
|
+
npx workflow cancel <run-id> \
|
|
88
|
+
--backend vercel
|
|
89
|
+
|
|
90
|
+
# Or bulk-cancel matching running runs.
|
|
91
|
+
npx workflow cancel \
|
|
92
|
+
--status running \
|
|
93
|
+
--workflowName "workflow//./workflows/fulfill-order//fulfillOrder" \
|
|
94
|
+
--backend vercel
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The `--workflowName` filter expects the generated workflow ID, not only the exported function's short name. Use the `workflowName` value from `workflow inspect runs`, and use [`parseWorkflowName()`](/docs/api-reference/workflow-api/world/observability) when you need display-friendly names.
|
|
98
|
+
|
|
99
|
+
In the [observability UI](/docs/observability), use **Rerun on latest** to enqueue the workflow again with the same inputs against the latest deployment.
|
|
100
|
+
|
|
101
|
+
If you are writing your own recovery route, call `start()` with the same arguments and `deploymentId: "latest"`:
|
|
102
|
+
|
|
103
|
+
```typescript title="app/api/orders/rerun/route.ts" lineNumbers
|
|
104
|
+
import { start } from "workflow/api";
|
|
105
|
+
import { fulfillOrder } from "@/workflows/fulfill-order";
|
|
106
|
+
|
|
107
|
+
export async function POST(request: Request) {
|
|
108
|
+
const { orderId } = await request.json();
|
|
109
|
+
|
|
110
|
+
const run = await start(fulfillOrder, [orderId], {
|
|
111
|
+
deploymentId: "latest", // [!code highlight]
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return Response.json({ runId: run.runId });
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
<Callout type="warn">
|
|
119
|
+
`deploymentId: "latest"` is currently a Vercel-specific feature. Other Worlds may implement this option differently to match their own deployment runtimes, and the World spec may rename it from `deploymentId` to `version` in a future SDK version. On Vercel, `"latest"` resolves to the most recent deployment matching your current environment. Because the caller and target deployment can be different, keep the [workflow function name and file path](/docs/errors/workflow-not-registered), arguments, and return value backward-compatible across the deployments you plan to bridge.
|
|
120
|
+
</Callout>
|
|
121
|
+
|
|
122
|
+
## Self upgrading workflows
|
|
123
|
+
|
|
124
|
+
Some workflows are expected to run for a very long time. Scheduled loops, recurring jobs, agents, and chat sessions often should not stay on one deployment forever.
|
|
125
|
+
|
|
126
|
+
Model those as a sequence of runs. Each run does a bounded piece of work, then starts the next run on the latest deployment and exits. This is similar to `continueAsNew` in other durable execution systems, but in Workflow SDK it is just [explicit recursion through `start()`](/cookbook/common-patterns/workflow-composition).
|
|
127
|
+
|
|
128
|
+
```typescript title="workflows/daily-digest.ts" lineNumbers
|
|
129
|
+
import { sleep } from "workflow";
|
|
130
|
+
import { start } from "workflow/api";
|
|
131
|
+
|
|
132
|
+
type DigestState = {
|
|
133
|
+
userId: string;
|
|
134
|
+
lastSentAt?: string;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export async function dailyDigest(state: DigestState) {
|
|
138
|
+
"use workflow";
|
|
139
|
+
|
|
140
|
+
const sentAt = await sendDigest(state.userId);
|
|
141
|
+
await sleep("1d");
|
|
142
|
+
|
|
143
|
+
const run = await start(
|
|
144
|
+
dailyDigest,
|
|
145
|
+
[{ ...state, lastSentAt: sentAt }],
|
|
146
|
+
{
|
|
147
|
+
deploymentId: "latest", // [!code highlight]
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return { continuedAs: run.runId };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function sendDigest(userId: string) {
|
|
155
|
+
"use step";
|
|
156
|
+
// ...
|
|
157
|
+
return new Date().toISOString();
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
This pattern gives every run a clear lifecycle:
|
|
162
|
+
|
|
163
|
+
- The current run stays on its original deployment.
|
|
164
|
+
- The next run starts on the latest deployment.
|
|
165
|
+
- The [serialized `state`](/docs/foundations/serialization) is the migration boundary between versions.
|
|
166
|
+
- Observability can link parent and child runs when a workflow starts another run.
|
|
167
|
+
|
|
168
|
+
## Carrying context forward
|
|
169
|
+
|
|
170
|
+
Anything that is [serializable by Workflow SDK](/docs/foundations/serialization) can be passed from one run to the next as an argument. That includes plain state objects, `ReadableStream`, `WritableStream`, `AbortSignal`, and other supported serialized values.
|
|
171
|
+
|
|
172
|
+
For example, a long export can register its [output stream](/docs/foundations/streaming) once, write progress from each run, and pass the same stream plus updated state into the next run:
|
|
173
|
+
|
|
174
|
+
```typescript title="workflows/export-report.ts" lineNumbers
|
|
175
|
+
import { getWritable } from "workflow";
|
|
176
|
+
import { start } from "workflow/api";
|
|
177
|
+
|
|
178
|
+
type ExportState = {
|
|
179
|
+
exportId: string;
|
|
180
|
+
page: number;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export async function exportReport(
|
|
184
|
+
state: ExportState,
|
|
185
|
+
progress?: WritableStream<string>
|
|
186
|
+
) {
|
|
187
|
+
"use workflow";
|
|
188
|
+
|
|
189
|
+
// Register the stream once. Continuation runs receive this same stream
|
|
190
|
+
// as an argument and keep writing to it.
|
|
191
|
+
const stream =
|
|
192
|
+
progress !== undefined ? progress : getWritable<string>();
|
|
193
|
+
|
|
194
|
+
const hasMore = await exportPage(state, stream);
|
|
195
|
+
|
|
196
|
+
if (!hasMore) {
|
|
197
|
+
await writeProgress(stream, { type: "done", totalPages: state.page });
|
|
198
|
+
return { totalPages: state.page };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const run = await start(exportReport, [
|
|
202
|
+
{ ...state, page: state.page + 1 },
|
|
203
|
+
stream,
|
|
204
|
+
], {
|
|
205
|
+
deploymentId: "latest", // [!code highlight]
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
return { continuedAs: run.runId };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function exportPage(
|
|
212
|
+
state: ExportState,
|
|
213
|
+
stream: WritableStream<string>
|
|
214
|
+
) {
|
|
215
|
+
"use step";
|
|
216
|
+
|
|
217
|
+
// Do work for this version boundary.
|
|
218
|
+
const hasMore = state.page < 10;
|
|
219
|
+
const writer = stream.getWriter();
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
await writer.write(
|
|
223
|
+
JSON.stringify({ type: "page", page: state.page }) + "\n"
|
|
224
|
+
);
|
|
225
|
+
return hasMore;
|
|
226
|
+
} finally {
|
|
227
|
+
writer.releaseLock();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function writeProgress(
|
|
232
|
+
stream: WritableStream<string>,
|
|
233
|
+
event: { type: "done"; totalPages: number }
|
|
234
|
+
) {
|
|
235
|
+
"use step";
|
|
236
|
+
|
|
237
|
+
const writer = stream.getWriter();
|
|
238
|
+
try {
|
|
239
|
+
await writer.write(JSON.stringify(event) + "\n");
|
|
240
|
+
} finally {
|
|
241
|
+
writer.releaseLock();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
```typescript title="app/api/export/route.ts" lineNumbers
|
|
247
|
+
import { start } from "workflow/api";
|
|
248
|
+
import { exportReport } from "@/workflows/export-report";
|
|
249
|
+
|
|
250
|
+
export async function POST(request: Request) {
|
|
251
|
+
const { exportId } = await request.json();
|
|
252
|
+
|
|
253
|
+
const run = await start(exportReport, [{ exportId, page: 1 }]);
|
|
254
|
+
|
|
255
|
+
// Linked continuation runs keep writing to the stream registered by
|
|
256
|
+
// the parent run, because that stream is passed forward as an argument.
|
|
257
|
+
return new Response(run.readable, {
|
|
258
|
+
headers: { "Content-Type": "application/jsonl" },
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Each run still has one clear version boundary: the current run stays on its original deployment, the next run starts on the latest deployment, and only the explicit state and stream handle are carried forward.
|
|
@@ -51,6 +51,12 @@ export default defineConfig({
|
|
|
51
51
|
});
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
`workflow()` accepts an options object:
|
|
55
|
+
|
|
56
|
+
| Option | Type | Default | Description |
|
|
57
|
+
| --- | --- | --- | --- |
|
|
58
|
+
| `sourcemap` | `boolean \| 'inline' \| 'linked' \| 'external' \| 'both'` | `'inline'` | Controls source maps on generated workflow bundles. Accepts the same values as esbuild's `sourcemap` option. Set to `false` for smaller function bundles (useful for staying under the Vercel 250MB function size limit) at the cost of stack traces pointing at generated code. Can also be set via the `WORKFLOW_SOURCEMAP` environment variable. |
|
|
59
|
+
|
|
54
60
|
<Accordion type="single" collapsible>
|
|
55
61
|
<AccordionItem value="typescript-intellisense" className="[&_h3]:my-0">
|
|
56
62
|
<AccordionTrigger className="text-sm">
|
|
@@ -63,6 +63,12 @@ import { Next, Nitro, SvelteKit, Nuxt, Hono, Bun, AstroDark, AstroLight, TanStac
|
|
|
63
63
|
<span className="font-medium">SvelteKit</span>
|
|
64
64
|
</div>
|
|
65
65
|
</Card>
|
|
66
|
+
<Card href="/docs/getting-started/tanstack-start" >
|
|
67
|
+
<div className="flex flex-col items-center justify-center gap-2">
|
|
68
|
+
<TanStack className="size-16 dark:invert" />
|
|
69
|
+
<span className="font-medium">TanStack Start</span>
|
|
70
|
+
</div>
|
|
71
|
+
</Card>
|
|
66
72
|
<Card href="/docs/getting-started/python">
|
|
67
73
|
<div className="flex flex-col items-center justify-center gap-2">
|
|
68
74
|
<Python className="size-16" />
|
|
@@ -77,11 +83,4 @@ import { Next, Nitro, SvelteKit, Nuxt, Hono, Bun, AstroDark, AstroLight, TanStac
|
|
|
77
83
|
<Badge variant="secondary">Coming soon</Badge>
|
|
78
84
|
</div>
|
|
79
85
|
</Card>
|
|
80
|
-
<Card className="opacity-50">
|
|
81
|
-
<div className="flex flex-col items-center justify-center gap-2">
|
|
82
|
-
<TanStack className="size-16 dark:invert grayscale" />
|
|
83
|
-
<span className="font-medium">TanStack Start</span>
|
|
84
|
-
<Badge variant="secondary">Coming soon</Badge>
|
|
85
|
-
</div>
|
|
86
|
-
</Card>
|
|
87
86
|
</Cards>
|
|
@@ -386,6 +386,14 @@ WorkflowModule.forRoot({
|
|
|
386
386
|
// Only used when moduleType is 'commonjs'
|
|
387
387
|
// Should match the outDir in your tsconfig.json
|
|
388
388
|
distDir: 'dist',
|
|
389
|
+
|
|
390
|
+
// Source maps on generated workflow bundles (default: 'inline').
|
|
391
|
+
// Accepts the same values as esbuild's sourcemap option: true, false,
|
|
392
|
+
// 'inline', 'linked', 'external', 'both'. Set to false for smaller
|
|
393
|
+
// function bundles (useful for staying under the Vercel 250MB function
|
|
394
|
+
// size limit) at the cost of stack traces pointing at generated code.
|
|
395
|
+
// Can also be set via the WORKFLOW_SOURCEMAP environment variable.
|
|
396
|
+
sourcemap: 'inline',
|
|
389
397
|
});
|
|
390
398
|
```
|
|
391
399
|
|
|
@@ -75,9 +75,9 @@ To enable helpful hints in your IDE, setup the workflow plugin in `tsconfig.json
|
|
|
75
75
|
</Accordion>
|
|
76
76
|
|
|
77
77
|
<Accordion type="single" collapsible>
|
|
78
|
-
<AccordionItem value="
|
|
78
|
+
<AccordionItem value="configure-proxy-handler" className="[&_h3]:my-0">
|
|
79
79
|
<AccordionTrigger className="text-sm">
|
|
80
|
-
|
|
80
|
+
<h3 id="configure-proxy-handler">Configure Proxy Handler (if applicable)</h3>
|
|
81
81
|
</AccordionTrigger>
|
|
82
82
|
<AccordionContent className="[&_p]:my-2">
|
|
83
83
|
|
|
@@ -85,7 +85,9 @@ If your Next.js app has a [proxy handler](https://nextjs.org/docs/app/api-refere
|
|
|
85
85
|
(formerly known as "middleware"), you'll need to update the matcher pattern to exclude Workflow's
|
|
86
86
|
internal paths to prevent the proxy handler from running on them.
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
If you see `[local world] Queue operation failed` with `Cannot perform ArrayBuffer.prototype.slice on a detached ArrayBuffer`, your proxy matcher is still intercepting Workflow's internal `POST /.well-known/workflow/v1/flow` request. This is especially easy to miss in Next.js 16, where `proxy.ts` replaced `middleware.ts`.
|
|
89
|
+
|
|
90
|
+
Add `.well-known/workflow/*` to your matcher exclusion list:
|
|
89
91
|
|
|
90
92
|
```typescript title="proxy.ts" lineNumbers
|
|
91
93
|
import { NextResponse } from "next/server";
|
|
@@ -46,6 +46,28 @@ export default defineConfig({
|
|
|
46
46
|
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
### Module options
|
|
50
|
+
|
|
51
|
+
The `workflow/nitro` module reads its options from `workflow` on your Nitro config.
|
|
52
|
+
|
|
53
|
+
```typescript title="nitro.config.ts" lineNumbers
|
|
54
|
+
import { defineConfig } from "nitro";
|
|
55
|
+
|
|
56
|
+
export default defineConfig({
|
|
57
|
+
modules: ["workflow/nitro"],
|
|
58
|
+
workflow: {
|
|
59
|
+
runtime: "nodejs22.x",
|
|
60
|
+
sourcemap: "inline",
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
| Option | Type | Default | Description |
|
|
66
|
+
| --- | --- | --- | --- |
|
|
67
|
+
| `dirs` | `string[]` | — | Directories to scan for workflows and steps. By default, `workflows/` is scanned from the project root and all layer source directories. |
|
|
68
|
+
| `runtime` | `string` | `'nodejs22.x'` | Node.js runtime version for Vercel Functions (e.g. `'nodejs22.x'`, `'nodejs24.x'`). |
|
|
69
|
+
| `sourcemap` | `boolean \| 'inline' \| 'linked' \| 'external' \| 'both'` | `'inline'` | Controls source maps on generated workflow bundles. Accepts the same values as esbuild's `sourcemap` option. Set to `false` for smaller function bundles (useful for staying under the Vercel 250MB function size limit) at the cost of stack traces pointing at generated code. Can also be set via the `WORKFLOW_SOURCEMAP` environment variable. |
|
|
70
|
+
|
|
49
71
|
<Accordion type="single" collapsible>
|
|
50
72
|
<AccordionItem value="typescript-intellisense" className="[&_h3]:my-0">
|
|
51
73
|
<AccordionTrigger className="[&_p]:my-0 text-lg [&_p]:text-foreground">
|
|
@@ -46,6 +46,12 @@ export default defineConfig({
|
|
|
46
46
|
});
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
`workflowPlugin()` accepts an options object:
|
|
50
|
+
|
|
51
|
+
| Option | Type | Default | Description |
|
|
52
|
+
| --- | --- | --- | --- |
|
|
53
|
+
| `sourcemap` | `boolean \| 'inline' \| 'linked' \| 'external' \| 'both'` | `'inline'` | Controls source maps on generated workflow bundles. Accepts the same values as esbuild's `sourcemap` option. Set to `false` for smaller function bundles (useful for staying under the Vercel 250MB function size limit) at the cost of stack traces pointing at generated code. Can also be set via the `WORKFLOW_SOURCEMAP` environment variable. |
|
|
54
|
+
|
|
49
55
|
<Accordion type="single" collapsible>
|
|
50
56
|
<AccordionItem value="typescript-intellisense" className="[&_h3]:my-0">
|
|
51
57
|
<AccordionTrigger className="text-sm">
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TanStack Start
|
|
3
|
+
description: Set up your first durable workflow in a TanStack Start application.
|
|
4
|
+
type: guide
|
|
5
|
+
summary: Set up Workflow SDK in a TanStack Start app.
|
|
6
|
+
prerequisites:
|
|
7
|
+
- /docs/getting-started
|
|
8
|
+
related:
|
|
9
|
+
- /docs/foundations/workflows-and-steps
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
This guide will walk through setting up your first workflow in a TanStack Start app. Along the way, you'll learn more about the concepts that are fundamental to using the Workflow SDK in your own projects.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
<Steps>
|
|
17
|
+
|
|
18
|
+
<Step>
|
|
19
|
+
## Create Your TanStack Start Project
|
|
20
|
+
|
|
21
|
+
Start by creating a new TanStack Start project:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx @tanstack/cli create my-workflow-app
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Enter the newly made directory:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
cd my-workflow-app
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Install `workflow`
|
|
34
|
+
|
|
35
|
+
```package-install
|
|
36
|
+
npm i workflow
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Configure TanStack Start
|
|
40
|
+
|
|
41
|
+
TanStack Start runs on Vite, so the Workflow SDK is wired in via the same `workflow/vite` plugin. Add `workflow()` to the existing `plugins` array in your Vite config — list it first so the `"use workflow"` and `"use step"` transforms run before any other plugin processes the file.
|
|
42
|
+
|
|
43
|
+
```typescript title="vite.config.ts" lineNumbers
|
|
44
|
+
import { defineConfig } from "vite";
|
|
45
|
+
import { workflow } from "workflow/vite";
|
|
46
|
+
// ...
|
|
47
|
+
|
|
48
|
+
export default defineConfig({
|
|
49
|
+
plugins: [
|
|
50
|
+
workflow(), // [!code highlight]
|
|
51
|
+
// ...the existing tanstackStart(), nitro(), and any other plugins
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
<Accordion type="single" collapsible>
|
|
57
|
+
<AccordionItem value="typescript-intellisense" className="[&_h3]:my-0">
|
|
58
|
+
<AccordionTrigger className="text-sm">
|
|
59
|
+
### Setup IntelliSense for TypeScript (Optional)
|
|
60
|
+
</AccordionTrigger>
|
|
61
|
+
<AccordionContent className="[&_p]:my-2">
|
|
62
|
+
|
|
63
|
+
To enable helpful hints in your IDE, setup the workflow plugin in `tsconfig.json`:
|
|
64
|
+
|
|
65
|
+
```json title="tsconfig.json" lineNumbers
|
|
66
|
+
{
|
|
67
|
+
"compilerOptions": {
|
|
68
|
+
// ... rest of your TypeScript config
|
|
69
|
+
"plugins": [
|
|
70
|
+
{
|
|
71
|
+
"name": "workflow" // [!code highlight]
|
|
72
|
+
}
|
|
73
|
+
]
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
</AccordionContent>
|
|
79
|
+
</AccordionItem>
|
|
80
|
+
</Accordion>
|
|
81
|
+
|
|
82
|
+
</Step>
|
|
83
|
+
|
|
84
|
+
<Step>
|
|
85
|
+
|
|
86
|
+
## Create Your First Workflow
|
|
87
|
+
|
|
88
|
+
Create a new file for our first workflow:
|
|
89
|
+
|
|
90
|
+
```typescript title="src/workflows/user-signup.ts" lineNumbers
|
|
91
|
+
import { sleep } from "workflow";
|
|
92
|
+
|
|
93
|
+
export async function handleUserSignup(email: string) {
|
|
94
|
+
"use workflow"; // [!code highlight]
|
|
95
|
+
|
|
96
|
+
const user = await createUser(email);
|
|
97
|
+
await sendWelcomeEmail(user);
|
|
98
|
+
|
|
99
|
+
await sleep("5s"); // Pause for 5s - doesn't consume any resources
|
|
100
|
+
await sendOnboardingEmail(user);
|
|
101
|
+
|
|
102
|
+
return { userId: user.id, status: "onboarded" };
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
We'll fill in those functions next, but let's take a look at this code:
|
|
107
|
+
|
|
108
|
+
* We define a **workflow** function with the directive `"use workflow"`. Think of the workflow function as the _orchestrator_ of individual **steps**.
|
|
109
|
+
* The Workflow SDK's `sleep` function allows us to suspend execution of the workflow without using up any resources. A sleep can be a few seconds, hours, days, or even months long.
|
|
110
|
+
|
|
111
|
+
## Create Your Workflow Steps
|
|
112
|
+
|
|
113
|
+
Let's now define those missing functions.
|
|
114
|
+
|
|
115
|
+
```typescript title="src/workflows/user-signup.ts" lineNumbers
|
|
116
|
+
import { FatalError } from "workflow"
|
|
117
|
+
|
|
118
|
+
// Our workflow function defined earlier
|
|
119
|
+
|
|
120
|
+
async function createUser(email: string) {
|
|
121
|
+
"use step"; // [!code highlight]
|
|
122
|
+
|
|
123
|
+
console.log(`Creating user with email: ${email}`);
|
|
124
|
+
|
|
125
|
+
// Full Node.js access - database calls, APIs, etc.
|
|
126
|
+
return { id: crypto.randomUUID(), email };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function sendWelcomeEmail(user: { id: string; email: string; }) {
|
|
130
|
+
"use step"; // [!code highlight]
|
|
131
|
+
|
|
132
|
+
console.log(`Sending welcome email to user: ${user.id}`);
|
|
133
|
+
|
|
134
|
+
if (Math.random() < 0.3) {
|
|
135
|
+
// By default, steps will be retried for unhandled errors
|
|
136
|
+
throw new Error("Retryable!");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function sendOnboardingEmail(user: { id: string; email: string}) {
|
|
141
|
+
"use step"; // [!code highlight]
|
|
142
|
+
|
|
143
|
+
if (!user.email.includes("@")) {
|
|
144
|
+
// To skip retrying, throw a FatalError instead
|
|
145
|
+
throw new FatalError("Invalid Email");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
console.log(`Sending onboarding email to user: ${user.id}`);
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Taking a look at this code:
|
|
153
|
+
|
|
154
|
+
* Business logic lives inside **steps**. When a step is invoked inside a **workflow**, it gets enqueued to run on a separate request while the workflow is suspended, just like `sleep`.
|
|
155
|
+
* If a step throws an error, like in `sendWelcomeEmail`, the step will automatically be retried until it succeeds (or hits the step's max retry count).
|
|
156
|
+
* Steps can throw a `FatalError` if an error is intentional and should not be retried.
|
|
157
|
+
|
|
158
|
+
<Callout>
|
|
159
|
+
We'll dive deeper into workflows, steps, and other ways to suspend or handle events in [Foundations](/docs/foundations).
|
|
160
|
+
</Callout>
|
|
161
|
+
|
|
162
|
+
</Step>
|
|
163
|
+
|
|
164
|
+
<Step>
|
|
165
|
+
|
|
166
|
+
## Create Your Route Handler
|
|
167
|
+
|
|
168
|
+
To invoke your new workflow, add a server handler at `src/routes/api/signup.ts`:
|
|
169
|
+
|
|
170
|
+
```typescript title="src/routes/api/signup.ts"
|
|
171
|
+
import { createFileRoute } from "@tanstack/react-router";
|
|
172
|
+
import { json } from "@tanstack/react-start";
|
|
173
|
+
import { start } from "workflow/api";
|
|
174
|
+
import { handleUserSignup } from "../../workflows/user-signup";
|
|
175
|
+
|
|
176
|
+
export const Route = createFileRoute("/api/signup")({
|
|
177
|
+
server: {
|
|
178
|
+
handlers: {
|
|
179
|
+
POST: async ({ request }) => {
|
|
180
|
+
const { email } = await request.json();
|
|
181
|
+
// Executes asynchronously and doesn't block your app
|
|
182
|
+
await start(handleUserSignup, [email]);
|
|
183
|
+
return json({ message: "User signup workflow started" });
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
This route handler creates a `POST` request endpoint at `/api/signup` that will trigger your workflow.
|
|
191
|
+
|
|
192
|
+
<Callout>
|
|
193
|
+
Workflows can be triggered from API routes or any server-side code.
|
|
194
|
+
</Callout>
|
|
195
|
+
|
|
196
|
+
</Step>
|
|
197
|
+
|
|
198
|
+
</Steps>
|
|
199
|
+
|
|
200
|
+
## Run in development
|
|
201
|
+
|
|
202
|
+
To start your development server, run the following command in your terminal in the TanStack Start root directory:
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
npm run dev
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Once your development server is running, you can trigger your workflow by running this command in the terminal:
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
curl -X POST --json '{"email":"hello@example.com"}' http://localhost:3000/api/signup
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Check the dev server logs to see your workflow execute as well as the steps that are being processed.
|
|
215
|
+
|
|
216
|
+
Additionally, you can use the [Workflow SDK CLI or Web UI](/docs/observability) to inspect your workflow runs and steps in detail.
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
# Open the observability Web UI on http://localhost:3456
|
|
220
|
+
npx workflow web
|
|
221
|
+
# or if you prefer a terminal interface, use the CLI inspect command
|
|
222
|
+
npx workflow inspect runs
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+

|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## Deploying to production
|
|
230
|
+
|
|
231
|
+
Workflow SDK apps currently work best when deployed to [Vercel](https://vercel.com/home) and needs no special configuration.
|
|
232
|
+
|
|
233
|
+
<FluidComputeCallout />
|
|
234
|
+
|
|
235
|
+
Check the [Deploying](/docs/deploying) section to learn how your workflows can be deployed elsewhere.
|
|
236
|
+
|
|
237
|
+
## Next Steps
|
|
238
|
+
|
|
239
|
+
* Learn more about the [Foundations](/docs/foundations).
|
|
240
|
+
* Check [Errors](/docs/errors) if you encounter issues.
|
|
241
|
+
* Explore the [API Reference](/docs/api-reference).
|