workflow 5.0.0-beta.1 → 5.0.0-beta.10

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.
Files changed (113) hide show
  1. package/README.md +4 -4
  2. package/dist/api-workflow.d.ts +1 -1
  3. package/dist/api-workflow.d.ts.map +1 -1
  4. package/dist/api-workflow.js +2 -2
  5. package/dist/api.d.ts +5 -1
  6. package/dist/api.d.ts.map +1 -1
  7. package/dist/api.js +14 -2
  8. package/dist/astro.js +1 -1
  9. package/dist/index.js +1 -1
  10. package/dist/internal/builtins.d.ts +17 -0
  11. package/dist/internal/builtins.d.ts.map +1 -1
  12. package/dist/internal/builtins.js +65 -1
  13. package/dist/internal/class-serialization.js +1 -1
  14. package/dist/internal/errors.js +1 -1
  15. package/dist/nest.js +1 -1
  16. package/dist/next.cjs +1 -1
  17. package/dist/nitro.js +1 -1
  18. package/dist/nuxt.js +1 -1
  19. package/dist/observability.d.ts +1 -1
  20. package/dist/observability.js +2 -2
  21. package/dist/runtime.d.ts +1 -1
  22. package/dist/runtime.d.ts.map +1 -1
  23. package/dist/runtime.js +2 -2
  24. package/dist/stdlib.js +1 -1
  25. package/dist/sveltekit.js +1 -1
  26. package/dist/typescript-plugin.cjs +1 -1
  27. package/dist/vite.js +1 -1
  28. package/dist/workflow.js +1 -1
  29. package/docs/ai/index.mdx +6 -5
  30. package/docs/ai/resumable-streams.mdx +1 -1
  31. package/docs/api-reference/vitest/index.mdx +28 -1
  32. package/docs/api-reference/workflow/create-webhook.mdx +37 -18
  33. package/docs/api-reference/workflow/experimental-set-attributes.mdx +63 -0
  34. package/docs/api-reference/workflow/get-workflow-metadata.mdx +34 -0
  35. package/docs/api-reference/workflow/index.mdx +3 -0
  36. package/docs/api-reference/workflow-ai/durable-agent.mdx +0 -4
  37. package/docs/api-reference/workflow-ai/index.mdx +0 -5
  38. package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +0 -4
  39. package/docs/api-reference/workflow-api/start.mdx +5 -4
  40. package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +16 -6
  41. package/docs/api-reference/workflow-next/with-workflow.mdx +34 -2
  42. package/docs/changelog/attributes-mvp.mdx +368 -0
  43. package/docs/changelog/eager-processing.mdx +595 -0
  44. package/docs/changelog/index.mdx +2 -1
  45. package/docs/cookbook/advanced/child-workflows.mdx +313 -0
  46. package/docs/cookbook/advanced/meta.json +9 -0
  47. package/docs/cookbook/advanced/publishing-libraries.mdx +336 -0
  48. package/docs/cookbook/advanced/serializable-steps.mdx +147 -0
  49. package/docs/cookbook/advanced/upgrading-workflows.mdx +195 -0
  50. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +156 -0
  51. package/docs/cookbook/agent-patterns/durable-agent.mdx +154 -0
  52. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +255 -0
  53. package/docs/cookbook/agent-patterns/meta.json +4 -0
  54. package/docs/cookbook/common-patterns/batching.mdx +105 -0
  55. package/docs/cookbook/common-patterns/idempotency.mdx +107 -0
  56. package/docs/cookbook/common-patterns/meta.json +15 -0
  57. package/docs/cookbook/common-patterns/rate-limiting.mdx +228 -0
  58. package/docs/cookbook/common-patterns/saga.mdx +247 -0
  59. package/docs/cookbook/common-patterns/scheduling.mdx +129 -0
  60. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +155 -0
  61. package/docs/cookbook/common-patterns/timeouts.mdx +99 -0
  62. package/docs/cookbook/common-patterns/webhooks.mdx +185 -0
  63. package/docs/cookbook/common-patterns/workflow-composition.mdx +111 -0
  64. package/docs/cookbook/index.mdx +38 -0
  65. package/docs/cookbook/integrations/ai-sdk.mdx +383 -0
  66. package/docs/cookbook/integrations/chat-sdk.mdx +307 -0
  67. package/docs/cookbook/integrations/meta.json +4 -0
  68. package/docs/cookbook/integrations/sandbox.mdx +520 -0
  69. package/docs/cookbook/meta.json +5 -0
  70. package/docs/deploying/building-a-world.mdx +1 -1
  71. package/docs/deploying/world/local-world.mdx +1 -1
  72. package/docs/deploying/world/postgres-world.mdx +6 -4
  73. package/docs/deploying/world/vercel-world.mdx +3 -1
  74. package/docs/errors/abort-signal-timeout-in-workflow.mdx +80 -0
  75. package/docs/errors/hook-conflict.mdx +56 -4
  76. package/docs/errors/index.mdx +3 -0
  77. package/docs/errors/runtime-decryption-failed.mdx +77 -0
  78. package/docs/errors/start-invalid-workflow-function.mdx +1 -1
  79. package/docs/foundations/cancellation.mdx +460 -0
  80. package/docs/foundations/errors-and-retries.mdx +7 -3
  81. package/docs/foundations/index.mdx +3 -3
  82. package/docs/foundations/meta.json +3 -2
  83. package/docs/foundations/serialization.mdx +78 -42
  84. package/docs/foundations/starting-workflows.mdx +6 -2
  85. package/docs/foundations/versioning.mdx +263 -0
  86. package/docs/getting-started/astro.mdx +6 -0
  87. package/docs/getting-started/index.mdx +13 -7
  88. package/docs/getting-started/meta.json +3 -1
  89. package/docs/getting-started/nestjs.mdx +8 -0
  90. package/docs/getting-started/next.mdx +5 -3
  91. package/docs/getting-started/nitro.mdx +22 -0
  92. package/docs/getting-started/python.mdx +165 -0
  93. package/docs/getting-started/sveltekit.mdx +6 -0
  94. package/docs/getting-started/tanstack-start.mdx +241 -0
  95. package/docs/how-it-works/cancellation.mdx +287 -0
  96. package/docs/how-it-works/code-transform.mdx +2 -2
  97. package/docs/how-it-works/event-sourcing.mdx +2 -2
  98. package/docs/how-it-works/meta.json +2 -1
  99. package/docs/internal/index.mdx +19 -0
  100. package/docs/internal/meta.json +5 -0
  101. package/docs/internal/serializable-abort-controller.mdx +148 -0
  102. package/docs/meta.json +1 -0
  103. package/docs/migration-guides/index.mdx +34 -0
  104. package/docs/migration-guides/meta.json +9 -0
  105. package/docs/migration-guides/migrating-from-aws-step-functions.mdx +358 -0
  106. package/docs/migration-guides/migrating-from-inngest.mdx +304 -0
  107. package/docs/migration-guides/migrating-from-temporal.mdx +313 -0
  108. package/docs/migration-guides/migrating-from-trigger-dev.mdx +328 -0
  109. package/docs/observability/attributes.mdx +66 -0
  110. package/docs/observability/index.mdx +9 -1
  111. package/docs/observability/meta.json +1 -1
  112. package/package.json +14 -13
  113. package/docs/foundations/common-patterns.mdx +0 -265
@@ -12,4 +12,5 @@ Stay up to date with the latest changes to Workflow SDK.
12
12
 
13
13
  ## 2026
14
14
 
15
- - TBD
15
+ - [Eager processing of steps and incremental event replay](/docs/changelog/eager-processing) - March 2026
16
+ - [Serializable AbortController and AbortSignal](/docs/changelog/serializable-abort-controller) — March 12, 2026
@@ -0,0 +1,313 @@
1
+ ---
2
+ title: Child Workflows
3
+ description: Spawn child workflows from a parent and wait for completion via hook resume.
4
+ type: guide
5
+ summary: Orchestrate independent child workflows from a parent using start(), defineHook(), and startAndWait() — the child resumes the parent's hook when done instead of polling getRun().status.
6
+ ---
7
+
8
+ Use child workflows when a single workflow needs to orchestrate many independent units of work. Each child runs as its own workflow with a separate event log, retry boundary, and failure scope -- if one child fails, it doesn't take down the parent or siblings.
9
+
10
+ ## When to use child workflows
11
+
12
+ Child workflows are the right choice when:
13
+
14
+ - **Work units are independent.** Each child can run without knowing about the others (e.g., processing individual documents, generating separate reports).
15
+ - **You need isolated failure boundaries.** A failing child should not abort unrelated work. The parent decides how to handle failures.
16
+ - **You want massive fan-out.** Spawning 50 or 500 children is practical because each runs on its own infrastructure.
17
+ - **You need per-item observability.** Each child workflow has its own run ID, status, and event log for monitoring.
18
+
19
+ For simpler cases where steps share a single event log, use [direct await composition](/cookbook/common-patterns/workflow-composition#direct-await-flattening) instead.
20
+
21
+ ## Basic pattern: spawn and wait via hook
22
+
23
+ The recommended pattern has four parts:
24
+
25
+ 1. A **completion hook** the parent creates and awaits — zero compute while waiting
26
+ 2. A **wrapped child export** that runs the real child in try/catch/finally and resumes the parent's hook from a step in `finally`
27
+ 3. A **`start()` call** that spawns the wrapped child with the hook token (directly from the workflow in v5)
28
+ 4. A **`startAndWait()` helper** that ties the hook, spawn, and typed result together
29
+
30
+ ```typescript
31
+ import { defineHook, getWorkflowMetadata } from "workflow";
32
+ import { start } from "workflow/api";
33
+ import { z } from "zod";
34
+
35
+ declare function fetchDocument(documentId: string): Promise<string>; // @setup
36
+ declare function analyzeContent(content: string): Promise<string>; // @setup
37
+ declare function generateSummary(analysis: string): Promise<string>; // @setup
38
+
39
+ const childCompletionHook = defineHook({
40
+ schema: z.discriminatedUnion("status", [
41
+ z.object({ status: z.literal("completed"), value: z.unknown() }),
42
+ z.object({ status: z.literal("failed"), error: z.string() }),
43
+ ]),
44
+ });
45
+
46
+ function completionToken(parentRunId: string, key: string) {
47
+ return `child-completion:${parentRunId}:${key}`;
48
+ }
49
+
50
+ async function resumeParentCompletion(
51
+ token: string,
52
+ result:
53
+ | { status: "completed"; value: unknown }
54
+ | { status: "failed"; error: string }
55
+ ) {
56
+ "use step";
57
+ await childCompletionHook.resume(token, result);
58
+ }
59
+
60
+ async function withChildCompletionHook<TResult>(
61
+ runChild: () => Promise<TResult>,
62
+ completionTokenArg: string
63
+ ) {
64
+ let result:
65
+ | { status: "completed"; value: TResult }
66
+ | { status: "failed"; error: string }
67
+ | undefined;
68
+
69
+ try {
70
+ const value = await runChild();
71
+ result = { status: "completed", value };
72
+ } catch (error) {
73
+ result = {
74
+ status: "failed",
75
+ error: error instanceof Error ? error.message : String(error),
76
+ };
77
+ } finally {
78
+ if (result) {
79
+ await resumeParentCompletion(completionTokenArg, result);
80
+ }
81
+ }
82
+ }
83
+
84
+ // Child workflow -- processes a single document
85
+ export async function processDocument(documentId: string) {
86
+ "use workflow";
87
+
88
+ const content = await fetchDocument(documentId);
89
+ const analysis = await analyzeContent(content);
90
+ const summary = await generateSummary(analysis);
91
+
92
+ return { documentId, summary };
93
+ }
94
+
95
+ // Spawnable wrapper -- explicit export so `start()` can register it
96
+ export async function processDocumentWithCompletion(
97
+ documentId: string,
98
+ completionTokenArg: string
99
+ ) {
100
+ "use workflow";
101
+
102
+ await withChildCompletionHook(
103
+ () => processDocument(documentId),
104
+ completionTokenArg
105
+ );
106
+ }
107
+
108
+ async function startAndWait<TResult>(
109
+ key: string,
110
+ startChild: (completionTokenArg: string) => Promise<void>
111
+ ): Promise<TResult> {
112
+ const { workflowRunId } = getWorkflowMetadata();
113
+ const token = completionToken(workflowRunId, key);
114
+ const hook = childCompletionHook.create({ token }); // [!code highlight]
115
+
116
+ await startChild(token);
117
+
118
+ const completion = await hook; // [!code highlight]
119
+ if (completion.status === "failed") {
120
+ throw new Error(completion.error);
121
+ }
122
+ return completion.value as TResult;
123
+ }
124
+
125
+ // Parent workflow -- orchestrates document processing
126
+ export async function processDocumentBatch(documentIds: string[]) {
127
+ "use workflow";
128
+
129
+ const results = await Promise.all(
130
+ documentIds.map((documentId) =>
131
+ startAndWait<{ documentId: string; summary: string }>(documentId, (token) =>
132
+ start(processDocumentWithCompletion, [documentId, token]).then(() => undefined) // [!code highlight]
133
+ )
134
+ )
135
+ );
136
+
137
+ return { processed: results.length, results };
138
+ }
139
+ ```
140
+
141
+ ### Why hooks instead of polling?
142
+
143
+ Polling with `getRun().status` in a `sleep()` loop works, but hook resume is preferable because:
144
+
145
+ - **Zero compute while waiting** — the parent suspends on the hook instead of waking every poll interval
146
+ - **Immediate wake-up** — the parent resumes as soon as the child finishes, not on the next poll tick
147
+ - **Typed payloads** — the child sends `{ status, value | error }` directly; no separate `returnValue` fetch step
148
+ - **No worker-pool pressure** — `Run#returnValue` polling inside steps can hold worker slots while waiting for children (see [Eager Processing](/changelog/eager-processing))
149
+
150
+ When a parent calls a child workflow inline with `await` (flattened into the same run), the same wrapper and hook handshake still works — pass the token and `await processDocumentWithCompletion(...)` inside `startAndWait()` instead of calling `start()`.
151
+
152
+ ## Fan-out pattern: chunked spawning
153
+
154
+ When spawning hundreds of children, batch the `start()` calls to avoid overwhelming the system. Each child still gets its own completion hook keyed by a stable identifier (document ID, report ID, index).
155
+
156
+ ```typescript
157
+ import { start } from "workflow/api";
158
+
159
+ declare function startAndWait<TResult>(
160
+ key: string,
161
+ startChild: (completionTokenArg: string) => Promise<void>
162
+ ): Promise<TResult>; // @setup
163
+
164
+ const CHUNK_SIZE = 10;
165
+
166
+ export async function largeReportBatch(
167
+ reportConfigs: Array<{ id: string; query: string }>
168
+ ) {
169
+ "use workflow";
170
+
171
+ const results = [];
172
+ for (let i = 0; i < reportConfigs.length; i += CHUNK_SIZE) {
173
+ const chunk = reportConfigs.slice(i, i + CHUNK_SIZE);
174
+ const chunkResults = await Promise.all(
175
+ chunk.map((config) =>
176
+ startAndWait<{ reportId: string; formatted: string }>(config.id, (token) =>
177
+ start(generateReportWithCompletion, [
178
+ config.id,
179
+ config.query,
180
+ token,
181
+ ]).then(() => undefined)
182
+ )
183
+ )
184
+ );
185
+ results.push(...chunkResults);
186
+ }
187
+
188
+ return { total: results.length, results };
189
+ }
190
+
191
+ async function generateReportWithCompletion(
192
+ reportId: string,
193
+ query: string,
194
+ completionTokenArg: string
195
+ ) {
196
+ "use workflow";
197
+
198
+ await withChildCompletionHook(
199
+ () => generateReport(reportId, query),
200
+ completionTokenArg
201
+ );
202
+ }
203
+
204
+ async function generateReport(reportId: string, query: string) {
205
+ "use workflow";
206
+
207
+ const data = await queryDatabase(reportId, query);
208
+ const formatted = await formatReport(reportId, data);
209
+ return { reportId, formatted };
210
+ }
211
+
212
+ declare function queryDatabase(reportId: string, query: string): Promise<string>; // @setup
213
+ declare function formatReport(reportId: string, data: string): Promise<string>; // @setup
214
+ declare function withChildCompletionHook<TResult>(
215
+ runChild: () => Promise<TResult>,
216
+ completionTokenArg: string
217
+ ): Promise<void>; // @setup
218
+ ```
219
+
220
+ ## Error handling
221
+
222
+ ### Tolerating partial failures
223
+
224
+ Use `Promise.allSettled` with `startAndWait()` so one failing child doesn't abort siblings. The hook payload already carries `{ status: "failed", error }` — no status polling required.
225
+
226
+ ```typescript
227
+ import { start } from "workflow/api";
228
+
229
+ declare function startAndWait<TResult>(
230
+ key: string,
231
+ startChild: (completionTokenArg: string) => Promise<void>
232
+ ): Promise<TResult>; // @setup
233
+ declare function processDocumentWithCompletion(
234
+ documentId: string,
235
+ completionTokenArg: string
236
+ ): Promise<void>; // @setup
237
+
238
+ export async function processDocumentBatchTolerant(documentIds: string[]) {
239
+ "use workflow";
240
+
241
+ const settled = await Promise.allSettled(
242
+ documentIds.map((documentId) =>
243
+ startAndWait<{ documentId: string; summary: string }>(documentId, (token) =>
244
+ start(processDocumentWithCompletion, [documentId, token]).then(
245
+ () => undefined
246
+ )
247
+ )
248
+ )
249
+ );
250
+
251
+ const results = settled
252
+ .filter(
253
+ (entry): entry is PromiseFulfilledResult<{ documentId: string; summary: string }> =>
254
+ entry.status === "fulfilled"
255
+ )
256
+ .map((entry) => entry.value);
257
+
258
+ const failed = settled.filter((entry) => entry.status === "rejected").length;
259
+
260
+ return { processed: results.length, failed, results };
261
+ }
262
+ ```
263
+
264
+ ### Retrying failed children
265
+
266
+ When a child fails, spawn a replacement with a fresh hook token. Track restart counts to prevent infinite retry loops.
267
+
268
+ ```typescript
269
+ declare function startAndWait<TResult>(
270
+ key: string,
271
+ startChild: (completionTokenArg: string) => Promise<void>
272
+ ): Promise<TResult>; // @setup
273
+ declare function spawnProcessDocument(
274
+ documentId: string,
275
+ completionTokenArg: string
276
+ ): Promise<void>; // @setup
277
+
278
+ async function startAndWaitWithRetries(
279
+ documentId: string,
280
+ maxRestarts: number
281
+ ): Promise<{ documentId: string; summary: string }> {
282
+ for (let attempt = 0; attempt <= maxRestarts; attempt++) {
283
+ try {
284
+ return await startAndWait<{ documentId: string; summary: string }>(
285
+ `${documentId}:${attempt}`,
286
+ (token) => spawnProcessDocument(documentId, token)
287
+ );
288
+ } catch (error) {
289
+ if (attempt === maxRestarts) throw error;
290
+ }
291
+ }
292
+
293
+ throw new Error("unreachable");
294
+ }
295
+ ```
296
+
297
+ ## Tips
298
+
299
+ - **`defineHook().resume()` must be called from a step.** The wrapped child's `finally` block calls a step that resumes the parent hook.
300
+ - **Export wrapped children at module scope.** The SDK registers `"use workflow"` functions statically — a runtime higher-order function returned from `withChildCompletionHook()` cannot be passed to `start()`.
301
+ - **Use stable hook keys** — document ID, job ID, or index — so parallel children inside one parent run don't collide on tokens.
302
+ - **Use chunked spawning for large batches.** Starting 500 children at once can create a large burst of work. Break it into chunks of 10-50.
303
+ - **Each child has its own retry semantics.** Steps inside child workflows retry independently. The parent sees the final `{ status, value | error }` payload from the hook.
304
+ - **Use `deploymentId: "latest"`** if children should run on the most recent deployment. See [Versioning](/docs/foundations/versioning) for the full model and the [`start()` API reference](/docs/api-reference/workflow-api/start#using-deploymentid-latest) for compatibility considerations.
305
+
306
+ ## Key APIs
307
+
308
+ - [`start()`](/docs/api-reference/workflow-api/start) -- spawn a new workflow run and get its run ID
309
+ - [`defineHook()`](/docs/api-reference/workflow/define-hook) -- typed hook for parent/child completion handshakes
310
+ - [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook) -- resume a waiting parent from a step (called by the child wrapper)
311
+ - [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata) -- read the parent run ID for deterministic hook tokens
312
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps) -- marks the orchestrator function
313
+ - [`"use step"`](/docs/foundations/workflows-and-steps) -- marks functions with full Node.js access
@@ -0,0 +1,9 @@
1
+ {
2
+ "title": "Advanced",
3
+ "pages": [
4
+ "child-workflows",
5
+ "upgrading-workflows",
6
+ "serializable-steps",
7
+ "publishing-libraries"
8
+ ]
9
+ }
@@ -0,0 +1,336 @@
1
+ ---
2
+ title: Publishing Libraries
3
+ description: Structure and publish npm packages that export workflow functions for consumers to use with Workflow SDK.
4
+ type: guide
5
+ summary: Learn how to build, export, and test npm packages that ship workflow and step functions — including package.json exports, re-exporting for stable workflow IDs, keeping step I/O clean, and integration testing.
6
+ ---
7
+
8
+ import { File, Folder, Files } from "fumadocs-ui/components/files";
9
+
10
+ <Callout>
11
+ This is an advanced guide for library authors who want to publish reusable workflow functions as npm packages. It assumes familiarity with `"use workflow"`, `"use step"`, and the workflow execution model.
12
+ </Callout>
13
+
14
+ ## Package Structure
15
+
16
+ A workflow library follows a standard TypeScript package layout with a dedicated `workflows/` directory. Each workflow file exports one or more workflow functions that consumers can import and pass to `start()`.
17
+
18
+ <Files>
19
+ <Folder name="my-media-lib" defaultOpen>
20
+ <Folder name="src" defaultOpen>
21
+ <File name="index.ts" />
22
+ <File name="types.ts" />
23
+ <Folder name="workflows" defaultOpen>
24
+ <File name="index.ts" />
25
+ <File name="transcode.ts" />
26
+ <File name="generate-thumbnails.ts" />
27
+ </Folder>
28
+ <Folder name="lib" defaultOpen>
29
+ <File name="api-client.ts" />
30
+ </Folder>
31
+ </Folder>
32
+ <Folder name="test-server" defaultOpen>
33
+ <File name="workflows.ts" />
34
+ </Folder>
35
+ <File name="tsup.config.ts" />
36
+ <File name="package.json" />
37
+ <File name="tsconfig.json" />
38
+ </Folder>
39
+ </Files>
40
+
41
+ Key files:
42
+
43
+ - **`src/index.ts`** — Package entry point. Exports the public API.
44
+ - **`src/types.ts`** — Shared TypeScript types.
45
+ - **`src/workflows/index.ts`** — Re-exports every workflow so consumers can pull them in under one specifier (see [Entry Points and Exports](#entry-points-and-exports)).
46
+ - **`src/workflows/*.ts`** — One file per workflow function (e.g. `transcode.ts`, `generate-thumbnails.ts`).
47
+ - **`src/lib/`** — Internal helpers. Plain async code, *not* marked with `"use workflow"` or `"use step"`.
48
+ - **`test-server/workflows.ts`** — Re-export file used by integration tests (see [Testing Workflow Libraries](#testing-workflow-libraries)).
49
+
50
+ ### Entry Points and Exports
51
+
52
+ Use the `exports` field in `package.json` to expose separate entry points for the main API and the raw workflow functions:
53
+
54
+ ```json
55
+ {
56
+ "name": "@acme/media",
57
+ "type": "module",
58
+ "exports": {
59
+ ".": {
60
+ "types": { "import": "./dist/index.d.ts" },
61
+ "import": "./dist/index.js"
62
+ },
63
+ "./workflows": {
64
+ "types": { "import": "./dist/workflows/index.d.ts" },
65
+ "import": "./dist/workflows/index.js"
66
+ }
67
+ },
68
+ "files": ["dist"]
69
+ }
70
+ ```
71
+
72
+ The main entry point (`@acme/media`) exports types, utilities, and convenience wrappers. The `./workflows` entry point (`@acme/media/workflows`) exports the raw workflow functions that consumers need for the build system.
73
+
74
+ ### Source Files
75
+
76
+ The package entry re-exports workflows alongside any utilities:
77
+
78
+ ```typescript lineNumbers
79
+ // src/index.ts
80
+ export * from "./types";
81
+ export * as workflows from "./workflows";
82
+ ```
83
+
84
+ The workflows barrel file re-exports each workflow:
85
+
86
+ ```typescript lineNumbers
87
+ // src/workflows/index.ts
88
+ export * from "./transcode";
89
+ export * from "./generate-thumbnails";
90
+ ```
91
+
92
+ ### Build Configuration
93
+
94
+ Use a bundler like `tsup` with separate entry points for each export. Mark `workflow` as external so it's resolved from the consumer's project:
95
+
96
+ ```typescript lineNumbers
97
+ // tsup.config.ts
98
+ import { defineConfig } from "tsup";
99
+
100
+ export default defineConfig({
101
+ entry: [
102
+ "src/index.ts",
103
+ "src/workflows/index.ts",
104
+ ],
105
+ format: ["esm"],
106
+ dts: true,
107
+ sourcemap: true,
108
+ clean: true,
109
+ external: ["workflow"], // [!code highlight]
110
+ });
111
+ ```
112
+
113
+ ## Re-Exporting for Workflow ID Stability
114
+
115
+ Workflow SDK's compiler assigns each workflow function a stable ID based on its position in the source file that the build system processes. When a consumer imports a pre-built workflow from an npm package, the compiler never sees the original source — it only sees the compiled output. This means workflow IDs won't match between the library's development environment and the consumer's app.
116
+
117
+ The fix is a **re-export file**. The consumer creates a file in their `workflows/` directory that re-exports the library's workflows. The build system then processes this file and assigns stable IDs.
118
+
119
+ ### Consumer Setup
120
+
121
+ ```typescript lineNumbers
122
+ // workflows/media.ts (in the consumer's project)
123
+ // Re-export library workflows so the build system assigns stable IDs
124
+ export * from "@acme/media/workflows"; // [!code highlight]
125
+ ```
126
+
127
+ This one-line file is all that's needed. The workflow compiler transforms this file, discovers the workflow and step functions from the library, and assigns IDs that are stable across deployments.
128
+
129
+ ### Why This Is Necessary
130
+
131
+ Without re-exporting, the workflow runtime cannot match a running workflow to its function definition. When a workflow run is replayed after a cold start, the runtime looks up functions by their compiler-assigned IDs. If the IDs don't exist (because the compiler never processed the library's source), replay fails.
132
+
133
+ The re-export pattern ensures:
134
+
135
+ 1. **Stable IDs** — the compiler assigns IDs based on the consumer's source tree
136
+ 2. **Replay safety** — IDs persist across deployments and cold starts
137
+ 3. **Version upgrades** — re-exported IDs remain stable as long as the consumer's file doesn't change
138
+
139
+ ## Keeping Step I/O Clean
140
+
141
+ When you publish a workflow library, every step function's inputs and outputs are recorded in the event log. This has two implications:
142
+
143
+ ### 1. Everything Must Be Serializable
144
+
145
+ Step inputs and outputs must be serializable. The workflow runtime supports a rich set of types beyond plain JSON — including `Date`, `RegExp`, `Map`, `Set`, `BigInt`, `Uint8Array`, `URL`, `Error`, and class instances that implement [custom class serialization](/docs/foundations/serialization#custom-class-serialization). See the [serialization reference](/docs/foundations/serialization) for the full list of supported types. Do not pass or return:
146
+
147
+ - Functions or closures
148
+ - `WeakRef`, `WeakMap`, or `WeakSet`
149
+
150
+ If your library works with complex objects that don't implement custom class serialization, pass serializable configuration into steps and reconstruct the objects inside the step body.
151
+
152
+ {/* @skip-typecheck - good/bad comparison with duplicate function names */}
153
+ ```typescript lineNumbers
154
+ // Good: pass serializable config, construct inside the step
155
+ async function callExternalApi(endpoint: string, params: Record<string, string>) {
156
+ "use step";
157
+ const client = createApiClient(process.env.API_KEY!);
158
+ return await client.request(endpoint, params);
159
+ }
160
+
161
+ // Bad: pass a pre-constructed client object
162
+ async function callExternalApi(client: ApiClient, params: Record<string, string>) {
163
+ "use step";
164
+ // ApiClient is not serializable — this will fail on replay
165
+ return await client.request(params);
166
+ }
167
+ ```
168
+
169
+ See [Serializable Steps](/docs/cookbook/advanced/serializable-steps) for the step-as-factory pattern.
170
+
171
+ ### 2. Credentials
172
+
173
+ With workflow encryption enabled, credentials passed as step arguments are encrypted in the event log, so either approach is valid:
174
+
175
+ {/* @skip-typecheck - good/bad comparison with duplicate function names */}
176
+ ```typescript lineNumbers
177
+ // Option A: resolve credentials from environment inside the step
178
+ async function fetchData(query: string) {
179
+ "use step";
180
+ const client = createClient(process.env.API_KEY!);
181
+ return await client.fetch(query);
182
+ }
183
+
184
+ // Option B: pass credentials as step arguments (encrypted in the event log)
185
+ async function fetchData(apiKey: string, query: string) {
186
+ "use step";
187
+ const client = createClient(apiKey);
188
+ return await client.fetch(query);
189
+ }
190
+ ```
191
+
192
+ The choice is a matter of library API design preference. Resolving from environment variables keeps the step signature simpler, while passing credentials explicitly makes dependencies visible and can be easier to test.
193
+
194
+ ## Testing Workflow Libraries
195
+
196
+ Library authors need integration tests that exercise workflows through the full Workflow SDK runtime — not just unit tests of individual functions.
197
+
198
+ ### Test Server Pattern
199
+
200
+ Create a minimal test server that re-exports your library's workflows, just like a consumer would:
201
+
202
+ ```typescript lineNumbers
203
+ // test-server/workflows.ts
204
+ export * from "@acme/media/workflows"; // [!code highlight]
205
+ ```
206
+
207
+ This test server acts as a stand-in consumer app. Point your test runner at it to exercise the full workflow lifecycle: start, replay, and completion.
208
+
209
+ ### Vitest Configuration
210
+
211
+ Use a dedicated Vitest config for integration tests that run against the Workflow SDK runtime:
212
+
213
+ ```typescript lineNumbers
214
+ // vitest.workflowsdk.config.ts
215
+ import { defineConfig } from "vitest/config";
216
+
217
+ export default defineConfig({
218
+ test: {
219
+ include: ["tests/integration/**/*.workflowsdk.test.ts"],
220
+ testTimeout: 120_000, // Workflows may take time to complete
221
+ setupFiles: ["./tests/setup.ts"],
222
+ },
223
+ });
224
+ ```
225
+
226
+ Run these tests separately from your unit tests:
227
+
228
+ ```bash
229
+ # Unit tests (fast, no workflow runtime)
230
+ pnpm vitest run tests/unit
231
+
232
+ # Integration tests (requires workflow runtime)
233
+ pnpm vitest run --config vitest.workflowsdk.config.ts
234
+ ```
235
+
236
+ ### What to Test
237
+
238
+ - **Happy path**: workflow starts, all steps execute, and the final result is correct
239
+ - **Serialization round-trip**: inputs and outputs survive the event log
240
+ - **Replay**: kill and restart a workflow mid-execution to verify deterministic replay
241
+ - **Error handling**: verify that step failures produce the expected errors
242
+
243
+ ## Working With and Without Workflow Installed
244
+
245
+ Some libraries want to be useful to consumers who *aren't* using Workflow SDK at all — the library picks up durable behavior when a workflow runtime is present and falls back to plain async execution otherwise.
246
+
247
+ <Callout type="info">
248
+ Two rules for isomorphic packages:
249
+
250
+ 1. **Any runtime reference to the `workflow` package must be loaded via dynamic `import("workflow")` inside a try/catch.** A static top-level import makes the module fail to load for consumers who haven't installed workflow.
251
+ 2. **The `"use workflow"` and `"use step"` directives are safe to keep in your library source.** When a consumer compiles your code with the Workflow SDK toolchain (via the [re-export pattern](#re-exporting-for-workflow-id-stability) above), the SWC plugin transforms them into durable-execution glue. When they're not compiled — plain Node, plain tests, a consumer without the runtime — they are just string expression statements and run as no-ops.
252
+ </Callout>
253
+
254
+ ### Optional peer dependency
255
+
256
+ Declare `workflow` as an **optional** peer so consumers without the runtime aren't forced to install it:
257
+
258
+ ```json
259
+ {
260
+ "peerDependencies": {
261
+ "workflow": ">=4.0.0"
262
+ },
263
+ "peerDependenciesMeta": {
264
+ "workflow": {
265
+ "optional": true
266
+ }
267
+ }
268
+ }
269
+ ```
270
+
271
+ ### Runtime detection
272
+
273
+ Wrap a dynamic `import("workflow")` in try/catch. If either the module isn't installed *or* `getWorkflowMetadata()` throws (call site isn't inside a workflow run), fall through to the standalone path.
274
+
275
+ ```typescript lineNumbers
276
+ async function getWorkflowRunId(): Promise<string | null> { // [!code highlight]
277
+ try {
278
+ const wf = await import("workflow");
279
+ const { workflowRunId } = wf.getWorkflowMetadata();
280
+ return workflowRunId;
281
+ } catch {
282
+ return null;
283
+ }
284
+ }
285
+ ```
286
+
287
+ ### A concrete use case: replay-safe idempotency keys
288
+
289
+ A payments utility that uses the workflow run ID as a Stripe idempotency key when available, and a fresh UUID otherwise:
290
+
291
+ {/* @skip-typecheck - depends on getWorkflowRunId defined in the previous block */}
292
+ ```typescript lineNumbers
293
+ export async function processPayment(amount: number, currency: string) {
294
+ const runId = await getWorkflowRunId();
295
+ const idempotencyKey = runId ?? crypto.randomUUID(); // [!code highlight]
296
+
297
+ const res = await fetch("https://api.stripe.com/v1/charges", {
298
+ method: "POST",
299
+ headers: {
300
+ Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
301
+ "Idempotency-Key": idempotencyKey, // [!code highlight]
302
+ },
303
+ body: new URLSearchParams({ amount: String(amount), currency }),
304
+ });
305
+ return res.json();
306
+ }
307
+ ```
308
+
309
+ When called from inside a workflow run, the step wrapping this utility gets a stable idempotency key across replays — Stripe dedupes retries for free. When called from a plain Node.js process, it behaves like any other function and a fresh UUID is generated.
310
+
311
+ ### In production
312
+
313
+ Packages in the wild built on Workflow SDK:
314
+
315
+ - **[`@mux/ai`](https://github.com/muxinc/ai)** — Reusable video AI workflows (summaries, chapters, content moderation, translation, embeddings) exported with `"use workflow"` / `"use step"` directives. In a standard Node environment the directives are no-ops and the SDK runs as a plain async library; in a Workflow SDK environment the consumer's compiler transforms them into durable, resumable steps with automatic retries and observability. Written up in detail in [*How Mux shipped durable video workflows with their @mux/ai SDK*](https://vercel.com/blog/how-mux-shipped-durable-video-workflows-with-their-mux-ai-sdk) on the Vercel blog.
316
+ - **World ID** — Human-in-the-loop "proof of human" primitive for agent workflows. Developers drop a World ID step into any workflow to require a zero-knowledge cryptographic proof that a real, unique human authorized a specific action (deploy approvals, large payments, sensitive data access, etc.). Because it runs as a workflow step, every verification is durable, replay-safe, and viewable inside the run's execution timeline — giving you a provable audit record of which human approved what. Available on npm and announced in [*World ID for agents: Browserbase, Exa, Okta, and Vercel*](https://world.org/blog/announcements/browserbase-exa-okta-world-id-for-agentic-web) on the World blog.
317
+
318
+ ## Checklist
319
+
320
+ Before publishing a workflow library:
321
+
322
+ - [ ] `workflow` is listed as an **optional** peer dependency
323
+ - [ ] Separate `./workflows` export in `package.json` for the raw workflow functions
324
+ - [ ] `workflow` is marked as **external** in your bundler config
325
+ - [ ] Documentation tells consumers to re-export from `@your-lib/workflows`
326
+ - [ ] Credentials are either resolved from environment variables or passed explicitly (both are safe with encryption enabled)
327
+ - [ ] All step I/O uses [supported serializable types](/docs/foundations/serialization)
328
+ - [ ] Integration tests use a test server with re-exported workflows
329
+ - [ ] Both with-workflow and without-workflow code paths are tested
330
+
331
+ ## Key APIs
332
+
333
+ - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) — declares the orchestrator function
334
+ - [`"use step"`](/docs/api-reference/workflow/use-step) — marks functions for durable execution
335
+ - [`start`](/docs/api-reference/workflow/start) — starts a workflow run
336
+ - [`getWorkflowMetadata`](/docs/api-reference/workflow/get-workflow-metadata) — runtime detection and run ID access