workflow 5.0.0-beta.2 → 5.0.0-beta.4

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 (42) hide show
  1. package/dist/api-workflow.d.ts +1 -1
  2. package/dist/api-workflow.d.ts.map +1 -1
  3. package/dist/api-workflow.js +2 -2
  4. package/docs/cookbook/{common-patterns → advanced}/child-workflows.mdx +1 -1
  5. package/docs/cookbook/advanced/distributed-abort-controller.mdx +318 -0
  6. package/docs/cookbook/advanced/meta.json +2 -3
  7. package/docs/cookbook/advanced/publishing-libraries.mdx +83 -26
  8. package/docs/cookbook/advanced/serializable-steps.mdx +15 -3
  9. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +205 -0
  10. package/docs/cookbook/agent-patterns/durable-agent.mdx +50 -91
  11. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +148 -171
  12. package/docs/cookbook/agent-patterns/meta.json +1 -7
  13. package/docs/cookbook/common-patterns/batching.mdx +44 -118
  14. package/docs/cookbook/common-patterns/meta.json +4 -4
  15. package/docs/cookbook/common-patterns/saga.mdx +126 -31
  16. package/docs/cookbook/common-patterns/scheduling.mdx +70 -194
  17. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +155 -0
  18. package/docs/cookbook/common-patterns/timeouts.mdx +99 -0
  19. package/docs/cookbook/common-patterns/workflow-composition.mdx +118 -0
  20. package/docs/cookbook/index.mdx +13 -16
  21. package/docs/cookbook/integrations/ai-sdk.mdx +296 -140
  22. package/docs/cookbook/integrations/chat-sdk.mdx +251 -151
  23. package/docs/cookbook/integrations/sandbox.mdx +469 -81
  24. package/docs/cookbook/meta.json +1 -1
  25. package/docs/foundations/index.mdx +0 -3
  26. package/docs/foundations/meta.json +0 -1
  27. package/docs/foundations/serialization.mdx +1 -1
  28. package/docs/foundations/starting-workflows.mdx +1 -1
  29. package/docs/migration-guides/migrating-from-aws-step-functions.mdx +60 -8
  30. package/docs/migration-guides/migrating-from-inngest.mdx +38 -6
  31. package/docs/migration-guides/migrating-from-temporal.mdx +38 -4
  32. package/docs/migration-guides/migrating-from-trigger-dev.mdx +52 -11
  33. package/package.json +11 -11
  34. package/docs/cookbook/advanced/custom-serialization.mdx +0 -168
  35. package/docs/cookbook/advanced/durable-objects.mdx +0 -148
  36. package/docs/cookbook/advanced/isomorphic-packages.mdx +0 -145
  37. package/docs/cookbook/agent-patterns/stop-workflow.mdx +0 -216
  38. package/docs/cookbook/agent-patterns/tool-orchestration.mdx +0 -255
  39. package/docs/cookbook/agent-patterns/tool-streaming.mdx +0 -181
  40. package/docs/cookbook/common-patterns/content-router.mdx +0 -207
  41. package/docs/cookbook/common-patterns/fan-out.mdx +0 -208
  42. package/docs/foundations/common-patterns.mdx +0 -265
@@ -1,9 +1,9 @@
1
1
  export type { Event, StartOptions, StopSleepOptions, StopSleepResult, WorkflowReadableStreamOptions, WorkflowRun, } from '@workflow/core/runtime';
2
2
  export { Run } from '@workflow/core/runtime/run';
3
+ export { start } from '@workflow/core/runtime/start';
3
4
  export declare const getRun: () => never;
4
5
  export declare const getHookByToken: () => never;
5
6
  export declare const resumeHook: () => never;
6
7
  export declare const resumeWebhook: () => never;
7
8
  export declare const runStep: () => never;
8
- export declare const start: () => never;
9
9
  //# sourceMappingURL=api-workflow.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"api-workflow.d.ts","sourceRoot":"","sources":["../src/api-workflow.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,KAAK,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,6BAA6B,EAC7B,WAAW,GACZ,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AAQjD,eAAO,MAAM,MAAM,aAA+B,CAAC;AACnD,eAAO,MAAM,cAAc,aAAuC,CAAC;AACnE,eAAO,MAAM,UAAU,aAAmC,CAAC;AAC3D,eAAO,MAAM,aAAa,aAAsC,CAAC;AACjE,eAAO,MAAM,OAAO,aAAgC,CAAC;AACrD,eAAO,MAAM,KAAK,aAA8B,CAAC"}
1
+ {"version":3,"file":"api-workflow.d.ts","sourceRoot":"","sources":["../src/api-workflow.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,KAAK,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,6BAA6B,EAC7B,WAAW,GACZ,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAQrD,eAAO,MAAM,MAAM,aAA+B,CAAC;AACnD,eAAO,MAAM,cAAc,aAAuC,CAAC;AACnE,eAAO,MAAM,UAAU,aAAmC,CAAC;AAC3D,eAAO,MAAM,aAAa,aAAsC,CAAC;AACjE,eAAO,MAAM,OAAO,aAAgC,CAAC"}
@@ -1,4 +1,5 @@
1
1
  export { Run } from '@workflow/core/runtime/run';
2
+ export { start } from '@workflow/core/runtime/start';
2
3
  const workflowStub = (item) => {
3
4
  throw new Error(`The workflow environment doesn't allow this runtime usage of ${item}. Move this call to a step function ("use step") or call it outside the workflow context.`);
4
5
  };
@@ -7,5 +8,4 @@ export const getHookByToken = () => workflowStub('getHookByToken');
7
8
  export const resumeHook = () => workflowStub('resumeHook');
8
9
  export const resumeWebhook = () => workflowStub('resumeWebhook');
9
10
  export const runStep = () => workflowStub('runStep');
10
- export const start = () => workflowStub('start');
11
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBpLXdvcmtmbG93LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2FwaS13b3JrZmxvdy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFTQSxPQUFPLEVBQUUsR0FBRyxFQUFFLE1BQU0sNEJBQTRCLENBQUM7QUFFakQsTUFBTSxZQUFZLEdBQUcsQ0FBQyxJQUFZLEVBQUUsRUFBRTtJQ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mV3IEVycm9yKFxuICAgIGBUaGUgd29ya2Zsb3cgZW52aXJvbm1lbnQgZG9lc24ndCBhbGxvdyB0aGlzIHJ1bnRpbWUgdXNhZ2Ugb2YgJHtpdGVtfS4gTW92ZSB0aGlzIGNhbGwgdG8gYSBzdGVwIGZ1bmN0aW9uIChcInVzZSBzdGVwXCIpIG9yIGNhbGwgaXQgb3V0c2lkZSB0aGUgd29ya2Zsb3cgY29udGV4dC5gXG4gICk7XG59O1xuXG5leHBvcnQgY29uc3QgZ2V0UnVuID0gKCkgPT4gd29ya2Zsb3dTdHViKCdnZXRSdW4nKTtcbmV4cG9ydCBjb25zdCBnZXRIb29rQnlUb2tlbiA9ICgpID0+IHdvcmtmbG93U3R1YignZ2V0SG9va0J5VG9rZW4nKTtcbmV4cG9ydCBjb25zdCByZXN1bWVIb29rID0gKCkgPT4gd29ya2Zsb3dTdHViKCdyZXN1bWVIb29rJyk7XG5leHBvcnQgY29uc3QgcmVzdW1lV2ViaG9vayA9ICgpID0+IHdvcmtmbG93U3R1YigncmVzdW1lV2ViaG9vaycpO1xuZXhwb3J0IGNvbnN0IHJ1blN0ZXAgPSAoKSA9PiB3b3JrZmxvd1N0dWIoJ3J1blN0ZXAnKTtcbiJdfQ==
@@ -16,7 +16,7 @@ Child workflows are the right choice when:
16
16
  - **You want massive fan-out.** Spawning 50 or 500 children is practical because each runs on its own infrastructure.
17
17
  - **You need per-item observability.** Each child workflow has its own run ID, status, and event log for monitoring.
18
18
 
19
- For simpler cases where steps share a single event log, use [direct await composition](/docs/foundations/common-patterns#direct-await-flattening) instead.
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
20
 
21
21
  ## Basic pattern: spawn and poll
22
22
 
@@ -0,0 +1,318 @@
1
+ ---
2
+ title: Distributed Abort Controller
3
+ description: A distributed AbortController that uses durable workflows for cross-process cancellation signaling.
4
+ type: guide
5
+ summary: Build a distributed abort controller that uses workflow streams and hooks to propagate cancellation signals across process boundaries.
6
+ ---
7
+
8
+ Use this pattern when you need an `AbortController`-like interface that works across distributed systems. The controller uses a durable workflow to coordinate cancellation — calling `.abort()` on one machine triggers the `.signal` on any other machine.
9
+
10
+ ## When to use this
11
+
12
+ - **Cross-process cancellation** — Cancel a long-running operation from a different server, worker, or edge function
13
+ - **Durable cancellation** — The abort signal persists even if the process that created it crashes
14
+ - **UI stop buttons** — Let users cancel operations running on the server from the browser
15
+ - **Timeout coordination** — The built-in TTL auto-expires stale controllers
16
+
17
+ ## Pattern
18
+
19
+ The `DistributedAbortController` class encapsulates a workflow that:
20
+ 1. Accepts a user-provided unique ID (like a chat ID or task ID)
21
+ 2. Creates or reconnects to an existing workflow using that ID
22
+ 3. Waits for a hook signal OR TTL expiration
23
+ 4. Writes a cancellation message to the run's stream when triggered
24
+
25
+ ### Core Implementation
26
+
27
+ ```typescript lineNumbers
28
+ import { defineHook, getWritable, sleep } from "workflow";
29
+ import { start, getRun, getHookByToken } from "workflow/api";
30
+
31
+ // Default TTL: 24 hours
32
+ const DEFAULT_TTL_MS = 24 * 60 * 60 * 1000;
33
+ // Default grace period: 1 hour (keeps hook alive after abort for late subscribers)
34
+ const DEFAULT_GRACE_MS = 60 * 60 * 1000;
35
+
36
+ // Hook to trigger the abort signal
37
+ export const abortHook = defineHook<{ reason?: string }>();
38
+
39
+ // The abort message written to the stream
40
+ export type AbortMessage = {
41
+ type: "abort";
42
+ reason?: string;
43
+ expired?: boolean;
44
+ };
45
+
46
+ // Helper to create a consistent hook token from the user ID
47
+ function getAbortToken(id: string): string {
48
+ return `abort:${id}`;
49
+ }
50
+
51
+ // Step function that writes the abort message to the stream
52
+ async function writeAbortSignal(reason?: string, expired?: boolean) {
53
+ "use step";
54
+
55
+ const writable = getWritable<AbortMessage>();
56
+ const writer = writable.getWriter();
57
+ try {
58
+ await writer.write({ type: "abort", reason, expired });
59
+ } finally {
60
+ writer.releaseLock();
61
+ }
62
+ await writable.close();
63
+ }
64
+
65
+ // Workflow that waits for abort or TTL expiration
66
+ export async function abortControllerWorkflow(
67
+ id: string,
68
+ ttlMs: number,
69
+ graceMs: number
70
+ ) {
71
+ "use workflow";
72
+
73
+ const startTime = Date.now();
74
+ const hook = abortHook.create({ token: getAbortToken(id) });
75
+
76
+ // Race: manual abort OR TTL expiration // [!code highlight]
77
+ const result = await Promise.race([
78
+ hook.then((payload) => ({
79
+ reason: payload.reason,
80
+ expired: false,
81
+ })),
82
+ sleep(`${ttlMs}ms`).then(() => ({
83
+ reason: "Controller expired",
84
+ expired: true,
85
+ })),
86
+ ]);
87
+
88
+ await writeAbortSignal(result.reason, result.expired);
89
+
90
+ // Only sleep through grace period on TTL expiration (keeps hook alive for late subscribers). // [!code highlight]
91
+ // Manual aborts complete immediately.
92
+ if (result.expired) {
93
+ const elapsed = Date.now() - startTime;
94
+ const remainingTime = graceMs - (elapsed - ttlMs);
95
+ if (remainingTime > 0) {
96
+ await sleep(`${remainingTime}ms`); // [!code highlight]
97
+ }
98
+ }
99
+
100
+ return { aborted: true, reason: result.reason, expired: result.expired };
101
+ }
102
+
103
+ /**
104
+ * A distributed abort controller that works across process boundaries.
105
+ * Uses a semantically meaningful ID (like a chat ID or task ID) to coordinate.
106
+ */
107
+ export class DistributedAbortController {
108
+ private id: string;
109
+ readonly runId: string;
110
+
111
+ private constructor(id: string, runId: string) {
112
+ this.id = id;
113
+ this.runId = runId;
114
+ }
115
+
116
+ /**
117
+ * Creates or reconnects to a distributed abort controller.
118
+ * If a controller with this ID already exists, reconnects to it.
119
+ * Otherwise, starts a new workflow.
120
+ *
121
+ * @param id - A unique, semantically meaningful ID (e.g., "chat:123")
122
+ * @param options.ttlMs - Time-to-live in ms (default: 24 hours)
123
+ * @param options.graceMs - Grace period after abort (default: 1 hour)
124
+ */
125
+ static async create( // [!code highlight]
126
+ id: string,
127
+ options: { ttlMs?: number; graceMs?: number } = {}
128
+ ): Promise<DistributedAbortController> {
129
+ const { ttlMs = DEFAULT_TTL_MS, graceMs = DEFAULT_GRACE_MS } = options;
130
+ const token = getAbortToken(id);
131
+
132
+ // Try to find an existing run with this hook token
133
+ const existingHook = await getHookByToken(token).catch(() => null); // [!code highlight]
134
+
135
+ if (existingHook) {
136
+ // Reconnect to existing controller
137
+ return new DistributedAbortController(id, existingHook.runId);
138
+ }
139
+
140
+ // Create a new workflow
141
+ const run = await start(abortControllerWorkflow, [id, ttlMs, graceMs]); // [!code highlight]
142
+ return new DistributedAbortController(id, run.runId);
143
+ }
144
+
145
+ /**
146
+ * Triggers the abort signal.
147
+ * Idempotent: safe to call multiple times or after the workflow has completed.
148
+ */
149
+ async abort(reason?: string): Promise<void> { // [!code highlight]
150
+ try {
151
+ await abortHook.resume(getAbortToken(this.id), { reason });
152
+ } catch (error) {
153
+ const msg = error instanceof Error ? error.message.toLowerCase() : '';
154
+ if (msg.includes('not found') || msg.includes('expired')) {
155
+ return;
156
+ }
157
+ throw error;
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Returns an AbortSignal that fires when abort() is called or TTL expires.
163
+ * The signal fires with a reason indicating what triggered it.
164
+ */
165
+ get signal(): AbortSignal { // [!code highlight]
166
+ const run = getRun<{ aborted: boolean; reason?: string; expired?: boolean }>(this.runId);
167
+ const controller = new AbortController();
168
+ const readable = run.getReadable<AbortMessage>();
169
+
170
+ (async () => {
171
+ const reader = readable.getReader();
172
+ try {
173
+ while (true) {
174
+ const { done, value } = await reader.read();
175
+ if (done) break;
176
+ if (value.type === "abort") {
177
+ const reason = value.expired
178
+ ? `${value.reason} (expired)`
179
+ : value.reason;
180
+ controller.abort(reason);
181
+ break;
182
+ }
183
+ }
184
+ } catch (error) {
185
+ if (!controller.signal.aborted) {
186
+ controller.abort(
187
+ error instanceof Error ? error.message : "Stream read failed"
188
+ );
189
+ }
190
+ } finally {
191
+ reader.releaseLock();
192
+ }
193
+ })();
194
+
195
+ return controller.signal;
196
+ }
197
+ }
198
+ ```
199
+
200
+ ### Usage: Single Process
201
+
202
+ ```typescript lineNumbers
203
+ import { DistributedAbortController } from "./distributed-abort-controller";
204
+
205
+ // Create a controller with a meaningful ID
206
+ const controller = await DistributedAbortController.create("chat:user-123");
207
+
208
+ // Get the signal and use it with fetch
209
+ const signal = controller.signal;
210
+ const response = await fetch("https://api.example.com/long-operation", {
211
+ signal,
212
+ });
213
+
214
+ // Later: abort the operation
215
+ await controller.abort("User cancelled");
216
+ ```
217
+
218
+ ### Usage: Cross-Process Coordination
219
+
220
+ ```typescript lineNumbers
221
+ import { DistributedAbortController } from "./distributed-abort-controller";
222
+
223
+ // Process A: Create the controller
224
+ const controller = await DistributedAbortController.create("task:build-123");
225
+ // start long operation using controller.signal...
226
+
227
+ // Process B: Reconnect and abort (no run ID sharing needed!)
228
+ const sameController = await DistributedAbortController.create("task:build-123"); // [!code highlight]
229
+ await sameController.abort("Cancelled by admin");
230
+
231
+ // Process C: Reconnect and listen
232
+ const anotherRef = await DistributedAbortController.create("task:build-123");
233
+ anotherRef.signal.addEventListener("abort", (e) => {
234
+ console.log("Task was cancelled:", (e.target as AbortSignal).reason);
235
+ });
236
+ ```
237
+
238
+ ### Custom TTL
239
+
240
+ ```typescript lineNumbers
241
+ import { DistributedAbortController } from "./distributed-abort-controller";
242
+
243
+ // Short-lived controller for a quick operation (5 minutes)
244
+ const shortLived = await DistributedAbortController.create("quick-task", {
245
+ ttlMs: 5 * 60 * 1000,
246
+ });
247
+
248
+ // Long-lived controller for batch jobs (7 days)
249
+ const longLived = await DistributedAbortController.create("batch-job", {
250
+ ttlMs: 7 * 24 * 60 * 60 * 1000,
251
+ });
252
+
253
+ // When TTL expires, the signal fires with expired reason
254
+ shortLived.signal.addEventListener("abort", (e) => {
255
+ const reason = (e.target as AbortSignal).reason;
256
+ if (reason?.includes("expired")) {
257
+ console.log("Controller expired, cleaning up...");
258
+ }
259
+ });
260
+ ```
261
+
262
+ ### API Route for Remote Abort
263
+
264
+ ```typescript lineNumbers
265
+ import { DistributedAbortController } from "@/lib/distributed-abort-controller";
266
+
267
+ export async function POST(
268
+ request: Request,
269
+ { params }: { params: Promise<{ id: string }> }
270
+ ) {
271
+ const { id } = await params;
272
+ const { reason } = await request.json();
273
+
274
+ const controller = await DistributedAbortController.create(id);
275
+ await controller.abort(reason || "Cancelled via API");
276
+
277
+ return Response.json({ success: true });
278
+ }
279
+ ```
280
+
281
+ ### Client Cancel Button
282
+
283
+ ```tsx lineNumbers
284
+ "use client";
285
+
286
+ export function CancelButton({ taskId }: { taskId: string }) {
287
+ const handleCancel = async () => {
288
+ await fetch(`/api/abort/${taskId}`, {
289
+ method: "POST",
290
+ headers: { "Content-Type": "application/json" },
291
+ body: JSON.stringify({ reason: "User clicked cancel" }),
292
+ });
293
+ };
294
+
295
+ return (
296
+ <button type="button" onClick={handleCancel}>
297
+ Cancel Operation
298
+ </button>
299
+ );
300
+ }
301
+ ```
302
+
303
+ ## Tips
304
+
305
+ - **Use semantic IDs** — Use meaningful IDs like `chat:123` or `task:abc` instead of random UUIDs
306
+ - **Create is idempotent** — Calling `create()` with the same ID reconnects to the existing controller
307
+ - **TTL auto-cleanup** — Workflows self-terminate after TTL expires; no manual cleanup needed
308
+ - **Signal is a getter** — Each access to `.signal` creates a new listener; cache it if needed
309
+ - **One-shot** — Once aborted or expired, the workflow completes; create a new controller for new operations
310
+
311
+ ## Key APIs
312
+
313
+ - [`defineHook()`](/docs/api-reference/workflow/define-hook) — type-safe hook for the abort trigger
314
+ - [`getWritable()`](/docs/api-reference/workflow/get-writable) — write abort messages to the stream
315
+ - [`sleep()`](/docs/api-reference/workflow/sleep) — TTL timer for auto-expiration
316
+ - [`start()`](/docs/api-reference/workflow-api/start) — start the abort controller workflow
317
+ - [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token) — find existing run by hook token
318
+ - [`getRun()`](/docs/api-reference/workflow-api/get-run) — reconnect to the workflow's readable stream
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "title": "Advanced",
3
3
  "pages": [
4
+ "child-workflows",
5
+ "distributed-abort-controller",
4
6
  "serializable-steps",
5
- "durable-objects",
6
- "isomorphic-packages",
7
- "custom-serialization",
8
7
  "publishing-libraries"
9
8
  ]
10
9
  }
@@ -5,6 +5,8 @@ type: guide
5
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
6
  ---
7
7
 
8
+ import { File, Folder, Files } from "fumadocs-ui/components/files";
9
+
8
10
  <Callout>
9
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.
10
12
  </Callout>
@@ -13,23 +15,37 @@ This is an advanced guide for library authors who want to publish reusable workf
13
15
 
14
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()`.
15
17
 
16
- ```
17
- my-media-lib/
18
- ├── src/
19
- │ ├── index.ts # Package entry point
20
- │ ├── types.ts # Shared types
21
- │ ├── workflows/
22
- │ │ ├── index.ts # Re-exports all workflows
23
- │ │ ├── transcode.ts # Workflow: transcode a video
24
- │ │ └── generate-thumbnails.ts
25
- │ └── lib/
26
- │ └── api-client.ts # Internal helpers (NOT steps)
27
- ├── test-server/
28
- │ └── workflows.ts # Re-export for integration tests
29
- ├── tsup.config.ts
30
- ├── package.json
31
- └── tsconfig.json
32
- ```
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)).
33
49
 
34
50
  ### Entry Points and Exports
35
51
 
@@ -126,7 +142,7 @@ When you publish a workflow library, every step function's inputs and outputs ar
126
142
 
127
143
  ### 1. Everything Must Be Serializable
128
144
 
129
- 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/cookbook/advanced/custom-serialization). See the [serialization reference](/docs/foundations/serialization) for the full list of supported types. Do not pass or return:
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:
130
146
 
131
147
  - Functions or closures
132
148
  - `WeakRef`, `WeakMap`, or `WeakSet`
@@ -226,7 +242,18 @@ pnpm vitest run --config vitest.workflowsdk.config.ts
226
242
 
227
243
  ## Working With and Without Workflow Installed
228
244
 
229
- If your library should work both as a standalone package and inside Workflow SDK, declare `workflow` as an optional peer dependency:
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:
230
257
 
231
258
  ```json
232
259
  {
@@ -241,22 +268,52 @@ If your library should work both as a standalone package and inside Workflow SDK
241
268
  }
242
269
  ```
243
270
 
244
- Use dynamic imports and runtime detection so your library gracefully degrades when workflow is not installed:
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.
245
274
 
246
275
  ```typescript lineNumbers
247
- async function isWorkflowRuntime(): Promise<boolean> {
276
+ async function getWorkflowRunId(): Promise<string | null> { // [!code highlight]
248
277
  try {
249
278
  const wf = await import("workflow");
250
- if (typeof wf.getWorkflowMetadata !== "function") return false;
251
- wf.getWorkflowMetadata(); // [!code highlight]
252
- return true;
279
+ const { workflowRunId } = wf.getWorkflowMetadata();
280
+ return workflowRunId;
253
281
  } catch {
254
- return false;
282
+ return null;
255
283
  }
256
284
  }
257
285
  ```
258
286
 
259
- See [Isomorphic Packages](/docs/cookbook/advanced/isomorphic-packages) for the full pattern including feature detection, dynamic imports, and dual-path execution.
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.
260
317
 
261
318
  ## Checklist
262
319
 
@@ -1,17 +1,28 @@
1
1
  ---
2
2
  title: Serializable Steps
3
- description: Wrap non-serializable objects (like AI model providers) inside step functions so they can cross the workflow boundary.
3
+ description: Wrap non-serializable third-party objects (like AI model providers) inside step factory functions so they can cross the workflow boundary.
4
4
  type: guide
5
- summary: Return a callback from a step to defer provider initialization, making non-serializable AI SDK models work inside durable workflows.
5
+ summary: Return a callback from a step to defer construction of a non-owned class (AI SDK models, cloud SDK clients) until execution time, making them usable inside durable workflows.
6
+ related:
7
+ - /docs/foundations/serialization
8
+ - /docs/foundations/serialization#custom-class-serialization
9
+ - /docs/api-reference/workflow/use-step
6
10
  ---
7
11
 
8
12
  <Callout>
9
13
  This is an advanced guide. It dives into workflow internals and is not required reading to use workflow.
10
14
  </Callout>
11
15
 
16
+ ## When to use this pattern
17
+
18
+ Workflow functions run inside a sandboxed VM where every value that crosses a function boundary must be serializable. There are two ways to get a non-serializable object across that boundary, depending on whether you own the class:
19
+
20
+ - **You own the class** — implement the [`WORKFLOW_SERIALIZE` / `WORKFLOW_DESERIALIZE` protocol](/docs/foundations/serialization#custom-class-serialization). The instance becomes a first-class serializable value: you can pass it as a workflow input, return it from a step, and call `"use step"` instance methods on it directly. This is the right tool when the class is yours to modify.
21
+ - **You don't own the class** — you can't add methods to `openai("gpt-4o")` from `@ai-sdk/openai` or `new S3Client({...})` from `@aws-sdk/client-s3`. Instead, wrap construction in a `"use step"` factory function and pass the factory across the boundary. That's what this page covers.
22
+
12
23
  ## The Problem
13
24
 
14
- Workflow functions run inside a sandboxed VM where every value that crosses a function boundary must be serializable (JSON-safe). AI SDK model providers — `openai("gpt-4o")`, `anthropic("claude-sonnet-4-20250514")`, etc. — return complex objects with methods, closures, and internal state. Passing one directly into a step causes a serialization error.
25
+ AI SDK model providers — `openai("gpt-4o")`, `anthropic("claude-sonnet-4-20250514")`, etc. — return complex objects with methods, closures, and internal state. Passing one directly into a step causes a serialization error, and you can't bolt `WORKFLOW_SERIALIZE` onto a third-party class.
15
26
 
16
27
  ```typescript lineNumbers
17
28
  import { openai } from "@ai-sdk/openai";
@@ -133,3 +144,4 @@ async function uploadFile(
133
144
  - [`"use step"`](/docs/api-reference/workflow/use-step) — marks a function for extraction and serialization
134
145
  - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) — declares the orchestrator function
135
146
  - [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — accepts a model factory for durable AI agent streaming
147
+ - [Custom class serialization](/docs/foundations/serialization#custom-class-serialization) — the companion pattern for classes you own (`WORKFLOW_SERIALIZE` / `WORKFLOW_DESERIALIZE`)