@workflow/core 4.0.1-beta.9 → 4.1.0-beta.51

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 (181) hide show
  1. package/dist/builtins.js +1 -1
  2. package/dist/class-serialization.d.ts +26 -0
  3. package/dist/class-serialization.d.ts.map +1 -0
  4. package/dist/class-serialization.js +66 -0
  5. package/dist/create-hook.js +1 -1
  6. package/dist/define-hook.d.ts +40 -25
  7. package/dist/define-hook.d.ts.map +1 -1
  8. package/dist/define-hook.js +22 -27
  9. package/dist/events-consumer.d.ts.map +1 -1
  10. package/dist/events-consumer.js +5 -1
  11. package/dist/flushable-stream.d.ts +82 -0
  12. package/dist/flushable-stream.d.ts.map +1 -0
  13. package/dist/flushable-stream.js +214 -0
  14. package/dist/global.d.ts +4 -1
  15. package/dist/global.d.ts.map +1 -1
  16. package/dist/global.js +21 -9
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -2
  20. package/dist/logger.js +1 -1
  21. package/dist/observability.d.ts +60 -0
  22. package/dist/observability.d.ts.map +1 -1
  23. package/dist/observability.js +265 -32
  24. package/dist/private.d.ts +10 -1
  25. package/dist/private.d.ts.map +1 -1
  26. package/dist/private.js +6 -1
  27. package/dist/runtime/helpers.d.ts +52 -0
  28. package/dist/runtime/helpers.d.ts.map +1 -0
  29. package/dist/runtime/helpers.js +264 -0
  30. package/dist/runtime/resume-hook.d.ts +17 -12
  31. package/dist/runtime/resume-hook.d.ts.map +1 -1
  32. package/dist/runtime/resume-hook.js +79 -64
  33. package/dist/runtime/start.d.ts +14 -0
  34. package/dist/runtime/start.d.ts.map +1 -1
  35. package/dist/runtime/start.js +71 -45
  36. package/dist/runtime/step-handler.d.ts +7 -0
  37. package/dist/runtime/step-handler.d.ts.map +1 -0
  38. package/dist/runtime/step-handler.js +337 -0
  39. package/dist/runtime/suspension-handler.d.ts +25 -0
  40. package/dist/runtime/suspension-handler.d.ts.map +1 -0
  41. package/dist/runtime/suspension-handler.js +182 -0
  42. package/dist/runtime/world.d.ts.map +1 -1
  43. package/dist/runtime/world.js +20 -21
  44. package/dist/runtime.d.ts +3 -7
  45. package/dist/runtime.d.ts.map +1 -1
  46. package/dist/runtime.js +103 -411
  47. package/dist/schemas.d.ts +1 -15
  48. package/dist/schemas.d.ts.map +1 -1
  49. package/dist/schemas.js +2 -15
  50. package/dist/serialization.d.ts +112 -21
  51. package/dist/serialization.d.ts.map +1 -1
  52. package/dist/serialization.js +469 -85
  53. package/dist/sleep.d.ts +10 -0
  54. package/dist/sleep.d.ts.map +1 -1
  55. package/dist/sleep.js +1 -1
  56. package/dist/source-map.d.ts +10 -0
  57. package/dist/source-map.d.ts.map +1 -0
  58. package/dist/source-map.js +56 -0
  59. package/dist/step/context-storage.d.ts +2 -1
  60. package/dist/step/context-storage.d.ts.map +1 -1
  61. package/dist/step/context-storage.js +1 -1
  62. package/dist/step/get-closure-vars.d.ts +9 -0
  63. package/dist/step/get-closure-vars.d.ts.map +1 -0
  64. package/dist/step/get-closure-vars.js +16 -0
  65. package/dist/step/get-step-metadata.js +1 -1
  66. package/dist/step/get-workflow-metadata.js +1 -1
  67. package/dist/step/writable-stream.d.ts +10 -2
  68. package/dist/step/writable-stream.d.ts.map +1 -1
  69. package/dist/step/writable-stream.js +6 -5
  70. package/dist/step.d.ts +1 -1
  71. package/dist/step.d.ts.map +1 -1
  72. package/dist/step.js +93 -47
  73. package/dist/symbols.d.ts +6 -0
  74. package/dist/symbols.d.ts.map +1 -1
  75. package/dist/symbols.js +7 -1
  76. package/dist/telemetry/semantic-conventions.d.ts +66 -38
  77. package/dist/telemetry/semantic-conventions.d.ts.map +1 -1
  78. package/dist/telemetry/semantic-conventions.js +16 -3
  79. package/dist/telemetry.d.ts +8 -4
  80. package/dist/telemetry.d.ts.map +1 -1
  81. package/dist/telemetry.js +39 -6
  82. package/dist/types.js +1 -1
  83. package/dist/util.d.ts +5 -24
  84. package/dist/util.d.ts.map +1 -1
  85. package/dist/util.js +19 -38
  86. package/dist/version.d.ts +2 -0
  87. package/dist/version.d.ts.map +1 -0
  88. package/dist/version.js +3 -0
  89. package/dist/vm/index.js +2 -2
  90. package/dist/vm/uuid.js +1 -1
  91. package/dist/workflow/create-hook.js +1 -1
  92. package/dist/workflow/define-hook.d.ts +3 -3
  93. package/dist/workflow/define-hook.d.ts.map +1 -1
  94. package/dist/workflow/define-hook.js +1 -1
  95. package/dist/workflow/get-workflow-metadata.js +1 -1
  96. package/dist/workflow/hook.d.ts.map +1 -1
  97. package/dist/workflow/hook.js +49 -14
  98. package/dist/workflow/index.d.ts +1 -1
  99. package/dist/workflow/index.d.ts.map +1 -1
  100. package/dist/workflow/index.js +2 -2
  101. package/dist/workflow/sleep.d.ts +1 -1
  102. package/dist/workflow/sleep.d.ts.map +1 -1
  103. package/dist/workflow/sleep.js +26 -39
  104. package/dist/workflow/writable-stream.d.ts +1 -1
  105. package/dist/workflow/writable-stream.d.ts.map +1 -1
  106. package/dist/workflow/writable-stream.js +1 -1
  107. package/dist/workflow.d.ts +1 -1
  108. package/dist/workflow.d.ts.map +1 -1
  109. package/dist/workflow.js +72 -9
  110. package/docs/api-reference/create-hook.mdx +133 -0
  111. package/docs/api-reference/create-webhook.mdx +225 -0
  112. package/docs/api-reference/define-hook.mdx +206 -0
  113. package/docs/api-reference/fatal-error.mdx +37 -0
  114. package/docs/api-reference/fetch.mdx +139 -0
  115. package/docs/api-reference/get-step-metadata.mdx +76 -0
  116. package/docs/api-reference/get-workflow-metadata.mdx +44 -0
  117. package/docs/api-reference/get-writable.mdx +292 -0
  118. package/docs/api-reference/index.mdx +55 -0
  119. package/docs/api-reference/meta.json +3 -0
  120. package/docs/api-reference/retryable-error.mdx +106 -0
  121. package/docs/api-reference/sleep.mdx +59 -0
  122. package/docs/foundations/common-patterns.mdx +253 -0
  123. package/docs/foundations/errors-and-retries.mdx +190 -0
  124. package/docs/foundations/hooks.mdx +455 -0
  125. package/docs/foundations/idempotency.mdx +55 -0
  126. package/docs/foundations/index.mdx +32 -0
  127. package/docs/foundations/meta.json +14 -0
  128. package/docs/foundations/serialization.mdx +157 -0
  129. package/docs/foundations/starting-workflows.mdx +211 -0
  130. package/docs/foundations/streaming.mdx +569 -0
  131. package/docs/foundations/workflows-and-steps.mdx +197 -0
  132. package/docs/how-it-works/code-transform.mdx +334 -0
  133. package/docs/how-it-works/event-sourcing.mdx +254 -0
  134. package/docs/how-it-works/framework-integrations.mdx +437 -0
  135. package/docs/how-it-works/meta.json +10 -0
  136. package/docs/how-it-works/understanding-directives.mdx +611 -0
  137. package/package.json +31 -25
  138. package/dist/builtins.js.map +0 -1
  139. package/dist/create-hook.js.map +0 -1
  140. package/dist/define-hook.js.map +0 -1
  141. package/dist/events-consumer.js.map +0 -1
  142. package/dist/global.js.map +0 -1
  143. package/dist/index.js.map +0 -1
  144. package/dist/logger.js.map +0 -1
  145. package/dist/observability.js.map +0 -1
  146. package/dist/parse-name.d.ts +0 -25
  147. package/dist/parse-name.d.ts.map +0 -1
  148. package/dist/parse-name.js +0 -40
  149. package/dist/parse-name.js.map +0 -1
  150. package/dist/private.js.map +0 -1
  151. package/dist/runtime/resume-hook.js.map +0 -1
  152. package/dist/runtime/start.js.map +0 -1
  153. package/dist/runtime/world.js.map +0 -1
  154. package/dist/runtime.js.map +0 -1
  155. package/dist/schemas.js.map +0 -1
  156. package/dist/serialization.js.map +0 -1
  157. package/dist/sleep.js.map +0 -1
  158. package/dist/step/context-storage.js.map +0 -1
  159. package/dist/step/get-step-metadata.js.map +0 -1
  160. package/dist/step/get-workflow-metadata.js.map +0 -1
  161. package/dist/step/writable-stream.js.map +0 -1
  162. package/dist/step.js.map +0 -1
  163. package/dist/symbols.js.map +0 -1
  164. package/dist/telemetry/semantic-conventions.js.map +0 -1
  165. package/dist/telemetry.js.map +0 -1
  166. package/dist/types.js.map +0 -1
  167. package/dist/util.js.map +0 -1
  168. package/dist/vm/index.js.map +0 -1
  169. package/dist/vm/uuid.js.map +0 -1
  170. package/dist/workflow/create-hook.js.map +0 -1
  171. package/dist/workflow/define-hook.js.map +0 -1
  172. package/dist/workflow/get-workflow-metadata.js.map +0 -1
  173. package/dist/workflow/hook.js.map +0 -1
  174. package/dist/workflow/index.js.map +0 -1
  175. package/dist/workflow/sleep.js.map +0 -1
  176. package/dist/workflow/writable-stream.js.map +0 -1
  177. package/dist/workflow.js.map +0 -1
  178. package/dist/writable-stream.d.ts +0 -23
  179. package/dist/writable-stream.d.ts.map +0 -1
  180. package/dist/writable-stream.js +0 -17
  181. package/dist/writable-stream.js.map +0 -1
@@ -0,0 +1,253 @@
1
+ ---
2
+ title: Common Patterns
3
+ ---
4
+
5
+ Common distributed patterns are simple to implement in workflows and require learning no new syntax. You can just use familiar async/await patterns.
6
+
7
+ ## Sequential Execution
8
+
9
+ The simplest way to orchestrate steps is to execute them one after another, where each step can be dependent on the previous step.
10
+
11
+ ```typescript lineNumbers
12
+ declare function validateData(data: unknown): Promise<string>; // @setup
13
+ declare function processData(data: string): Promise<string>; // @setup
14
+ declare function storeData(data: string): Promise<string>; // @setup
15
+
16
+ export async function dataPipelineWorkflow(data: unknown) {
17
+ "use workflow";
18
+
19
+ const validated = await validateData(data);
20
+ const processed = await processData(validated);
21
+ const stored = await storeData(processed);
22
+
23
+ return stored;
24
+ }
25
+ ```
26
+
27
+ ## Parallel Execution
28
+
29
+ When you need to execute multiple steps in parallel, you can use `Promise.all` to run them all at the same time.
30
+
31
+ ```typescript lineNumbers
32
+ declare function fetchUser(userId: string): Promise<{ name: string }>; // @setup
33
+ declare function fetchOrders(userId: string): Promise<{ items: string[] }>; // @setup
34
+ declare function fetchPreferences(userId: string): Promise<{ theme: string }>; // @setup
35
+
36
+ export async function fetchUserData(userId: string) {
37
+ "use workflow";
38
+
39
+ const [user, orders, preferences] = await Promise.all([ // [!code highlight]
40
+ fetchUser(userId), // [!code highlight]
41
+ fetchOrders(userId), // [!code highlight]
42
+ fetchPreferences(userId) // [!code highlight]
43
+ ]); // [!code highlight]
44
+
45
+ return { user, orders, preferences };
46
+ }
47
+ ```
48
+
49
+ This not only applies to steps - since [`sleep()`](/docs/api-reference/workflow/sleep) and [`webhook`](/docs/api-reference/workflow/create-webhook) are also just promises, we can await those in parallel too.
50
+ We can also use `Promise.race` instead of `Promise.all` to stop executing promises after the first one completes.
51
+
52
+ ```typescript lineNumbers
53
+ import { sleep, createWebhook } from "workflow";
54
+ declare function executeExternalTask(webhookUrl: string): Promise<void>; // @setup
55
+
56
+ export async function runExternalTask(userId: string) {
57
+ "use workflow";
58
+
59
+ const webhook = createWebhook();
60
+ await executeExternalTask(webhook.url); // Send the webhook somewhere
61
+
62
+ // Wait for the external webhook to be hit, with a timeout of 1 day,
63
+ // whichever comes first
64
+ await Promise.race([ // [!code highlight]
65
+ webhook, // [!code highlight]
66
+ sleep("1 day"), // [!code highlight]
67
+ ]); // [!code highlight]
68
+
69
+ console.log("Done")
70
+ }
71
+ ```
72
+
73
+ ## A Full Example
74
+
75
+ Here's a simplified example taken from the [birthday card generator demo](https://github.com/vercel/workflow-examples/tree/main/birthday-card-generator), to illustrate how sequential and parallel execution can be combined.
76
+
77
+ ```typescript lineNumbers
78
+ import { createWebhook, sleep, type Webhook } from "workflow"
79
+ declare function makeCardText(prompt: string): Promise<string>; // @setup
80
+ declare function makeCardImage(text: string): Promise<string>; // @setup
81
+ declare function sendRSVPEmail(friend: string, webhook: Webhook): Promise<void>; // @setup
82
+ declare function sendBirthdayCard(text: string, image: string, rsvps: unknown[], email: string): Promise<void>; // @setup
83
+
84
+ async function birthdayWorkflow(
85
+ prompt: string,
86
+ email: string,
87
+ friends: string[],
88
+ birthday: Date
89
+ ) {
90
+ "use workflow";
91
+
92
+ // Generate a birthday card with sequential steps
93
+ const text = await makeCardText(prompt)
94
+ const image = await makeCardImage(text)
95
+
96
+ // Create webhooks for each friend who's invited to the birthday party
97
+ const webhooks = friends.map(_ => createWebhook())
98
+
99
+ // Send out all the RSVP invites in parallel steps
100
+ await Promise.all(
101
+ friends.map(
102
+ (friend, i) => sendRSVPEmail(friend, webhooks[i])
103
+ )
104
+ )
105
+
106
+ // Collect RSVPs as they are made without blocking the workflow
107
+ let rsvps = []
108
+ webhooks.map(
109
+ webhook => webhook
110
+ .then(req => req.json())
111
+ .then(( { rsvp } ) => rsvps.push(rsvp))
112
+ )
113
+
114
+ // Wait until the birthday
115
+ await sleep(birthday)
116
+
117
+ // Send birthday card with as many rsvps were collected
118
+ await sendBirthdayCard(text, image, rsvps, email)
119
+
120
+ return { text, image, status: "Sent" }
121
+ }
122
+ ```
123
+
124
+ ## Timeout Pattern
125
+
126
+ A common requirement is adding timeouts to operations that might take too long. Use `Promise.race` with `sleep()` to implement this pattern.
127
+
128
+ ```typescript lineNumbers
129
+ import { sleep } from "workflow";
130
+ declare function processData(data: string): Promise<string>; // @setup
131
+
132
+ export async function processWithTimeout(data: string) {
133
+ "use workflow";
134
+
135
+ const result = await Promise.race([ // [!code highlight]
136
+ processData(data), // [!code highlight]
137
+ sleep("30s").then(() => "timeout" as const), // [!code highlight]
138
+ ]); // [!code highlight]
139
+
140
+ if (result === "timeout") {
141
+ // In workflows, any thrown error exits the workflow (FatalError is for steps)
142
+ throw new Error("Processing timed out after 30 seconds");
143
+ }
144
+
145
+ return result;
146
+ }
147
+ ```
148
+
149
+ This pattern works with any promise-returning operation including steps, hooks, and webhooks. For example, you can add a timeout to a webhook that waits for external input:
150
+
151
+ ```typescript lineNumbers
152
+ import { sleep, createWebhook } from "workflow";
153
+ declare function sendApprovalRequest(requestId: string, webhookUrl: string): Promise<void>; // @setup
154
+
155
+ export async function waitForApproval(requestId: string) {
156
+ "use workflow";
157
+
158
+ const webhook = createWebhook<{ approved: boolean }>();
159
+ await sendApprovalRequest(requestId, webhook.url);
160
+
161
+ const result = await Promise.race([ // [!code highlight]
162
+ webhook.then((req) => req.json()), // [!code highlight]
163
+ sleep("7 days").then(() => ({ timedOut: true }) as const), // [!code highlight]
164
+ ]); // [!code highlight]
165
+
166
+ if ("timedOut" in result) {
167
+ throw new Error("Approval request expired after 7 days");
168
+ }
169
+
170
+ return result.approved;
171
+ }
172
+ ```
173
+
174
+ ## Workflow Composition
175
+
176
+ Workflows can call other workflows, enabling you to break complex processes into reusable building blocks. There are two approaches depending on your needs.
177
+
178
+ ### Direct Await (Flattening)
179
+
180
+ Call a child workflow directly using `await`. This "flattens" the child workflow into the parent - the child's steps execute inline within the parent workflow's context.
181
+
182
+ ```typescript lineNumbers
183
+ declare function sendEmail(userId: string): Promise<void>; // @setup
184
+ declare function sendPushNotification(userId: string): Promise<void>; // @setup
185
+ declare function createAccount(userId: string): Promise<void>; // @setup
186
+ declare function setupPreferences(userId: string): Promise<void>; // @setup
187
+
188
+ // Child workflow
189
+ export async function sendNotifications(userId: string) {
190
+ "use workflow";
191
+
192
+ await sendEmail(userId);
193
+ await sendPushNotification(userId);
194
+ return { notified: true };
195
+ }
196
+
197
+ // Parent workflow calls child directly
198
+ export async function onboardUser(userId: string) {
199
+ "use workflow";
200
+
201
+ await createAccount(userId);
202
+ await sendNotifications(userId); // [!code highlight]
203
+ await setupPreferences(userId);
204
+
205
+ return { userId, status: "onboarded" };
206
+ }
207
+ ```
208
+
209
+ With direct await, the parent workflow waits for the child to complete before continuing. The child's steps appear in the parent's event log as if they were called directly from the parent.
210
+
211
+ ### Background Execution via Step
212
+
213
+ To run a child workflow independently without blocking the parent, use a step that calls [`start()`](/docs/api-reference/workflow-api/start). This launches the child workflow in the background.
214
+
215
+ ```typescript lineNumbers
216
+ import { start } from "workflow/api";
217
+ declare function generateReport(reportId: string): Promise<void>; // @setup
218
+ declare function fulfillOrder(orderId: string): Promise<{ id: string }>; // @setup
219
+ declare function sendConfirmation(orderId: string): Promise<void>; // @setup
220
+
221
+ // Step that starts a workflow in the background
222
+ async function triggerReportGeneration(reportId: string) {
223
+ "use step";
224
+
225
+ const run = await start(generateReport, [reportId]); // [!code highlight]
226
+ return run.runId;
227
+ }
228
+
229
+ // Parent workflow
230
+ export async function processOrder(orderId: string) {
231
+ "use workflow";
232
+
233
+ const order = await fulfillOrder(orderId);
234
+
235
+ // Fire off report generation without waiting
236
+ const reportRunId = await triggerReportGeneration(orderId); // [!code highlight]
237
+
238
+ // Continue immediately - report generates in background
239
+ await sendConfirmation(orderId);
240
+
241
+ return { orderId, reportRunId };
242
+ }
243
+ ```
244
+
245
+ With background execution, the parent workflow continues immediately after starting the child. The child workflow runs independently with its own event log and can be monitored separately using the returned `runId`.
246
+
247
+ **Choose direct await when:**
248
+ - The parent needs the child's result before continuing
249
+ - You want a single, unified event log
250
+
251
+ **Choose background execution when:**
252
+ - The parent doesn't need to wait for the result
253
+ - You want separate workflow runs for observability
@@ -0,0 +1,190 @@
1
+ ---
2
+ title: Errors & Retrying
3
+ ---
4
+
5
+ By default, errors thrown inside steps are retried. Additionally, Workflow DevKit provides two new types of errors you can use to customize retries.
6
+
7
+ ## Default Retrying
8
+
9
+ By default, steps retry up to 3 times on arbitrary errors. You can customize the number of retries by adding a `maxRetries` property to the step function.
10
+
11
+ ```typescript lineNumbers
12
+ async function callApi(endpoint: string) {
13
+ "use step";
14
+
15
+ const response = await fetch(endpoint);
16
+
17
+ if (response.status >= 500) {
18
+ // Any uncaught error gets retried
19
+ throw new Error("Uncaught exceptions get retried!"); // [!code highlight]
20
+ }
21
+
22
+ return response.json();
23
+ }
24
+
25
+ callApi.maxRetries = 5; // Retry up to 5 times on failure (6 total attempts)
26
+ ```
27
+
28
+ Steps get enqueued immediately after a failure. Read on to see how this can be customized.
29
+
30
+ <Callout type="info">
31
+ When a retried step performs external side effects (payments, emails, API
32
+ writes), ensure those calls are <strong>idempotent</strong> to avoid duplicate
33
+ side effects. See <a href="/docs/foundations/idempotency">Idempotency</a> for
34
+ more information.
35
+ </Callout>
36
+
37
+ ## Intentional Errors
38
+
39
+ When your step needs to intentionally throw an error and skip retrying, simply throw a [`FatalError`](/docs/api-reference/workflow/fatal-error).
40
+
41
+ ```typescript lineNumbers
42
+ import { FatalError } from "workflow";
43
+
44
+ async function callApi(endpoint: string) {
45
+ "use step";
46
+
47
+ const response = await fetch(endpoint);
48
+
49
+ if (response.status >= 500) {
50
+ // Any uncaught error gets retried
51
+ throw new Error("Uncaught exceptions get retried!");
52
+ }
53
+
54
+ if (response.status === 404) {
55
+ throw new FatalError("Resource not found. Skipping retries."); // [!code highlight]
56
+ }
57
+
58
+ return response.json();
59
+ }
60
+ ```
61
+
62
+ ## Customize Retry Behavior
63
+
64
+ When you need to customize the delay on a retry, use [`RetryableError`](/docs/api-reference/workflow/retryable-error) and set the `retryAfter` property.
65
+
66
+ ```typescript lineNumbers
67
+ import { FatalError, RetryableError } from "workflow";
68
+
69
+ async function callApi(endpoint: string) {
70
+ "use step";
71
+
72
+ const response = await fetch(endpoint);
73
+
74
+ if (response.status >= 500) {
75
+ throw new Error("Uncaught exceptions get retried!");
76
+ }
77
+
78
+ if (response.status === 404) {
79
+ throw new FatalError("Resource not found. Skipping retries.");
80
+ }
81
+
82
+ if (response.status === 429) {
83
+ throw new RetryableError("Rate limited. Retrying...", { // [!code highlight]
84
+ retryAfter: "1m", // Duration string // [!code highlight]
85
+ }); // [!code highlight]
86
+ }
87
+
88
+ return response.json();
89
+ }
90
+ ```
91
+
92
+ ## Advanced Example
93
+
94
+ This final example combines everything we've learned, along with [`getStepMetadata`](/docs/api-reference/workflow/get-step-metadata).
95
+
96
+ ```typescript lineNumbers
97
+ import { FatalError, RetryableError, getStepMetadata } from "workflow";
98
+
99
+ async function callApi(endpoint: string) {
100
+ "use step";
101
+
102
+ const metadata = getStepMetadata();
103
+
104
+ const response = await fetch(endpoint);
105
+
106
+ if (response.status >= 500) {
107
+ // Exponential backoffs
108
+ throw new RetryableError("Backing off...", {
109
+ retryAfter: (metadata.attempt ** 2) * 1000, // [!code highlight]
110
+ });
111
+ }
112
+
113
+ if (response.status === 404) {
114
+ throw new FatalError("Resource not found. Skipping retries.");
115
+ }
116
+
117
+ if (response.status === 429) {
118
+ throw new RetryableError("Rate limited. Retrying...", {
119
+ retryAfter: new Date(Date.now() + 60000), // Date instance // [!code highlight]
120
+ });
121
+ }
122
+
123
+ return response.json();
124
+ }
125
+ callApi.maxRetries = 5; // Retry up to 5 times on failure (6 total attempts)
126
+ ```
127
+
128
+ <Callout type="info">
129
+ Setting <code>maxRetries = 0</code> means the step will run once but will not
130
+ be retried on failure. The default is <code>maxRetries = 3</code>, meaning the
131
+ step can run up to 4 times total (1 initial attempt + 3 retries).
132
+ </Callout>
133
+
134
+ ## Rolling Back Failed Steps
135
+
136
+ When a workflow fails partway through, it can leave the system in an inconsistent state.
137
+ A common pattern to address this is "rollbacks": for each successful step, record a corresponding rollback action that can undo it.
138
+ If a later step fails, run the rollbacks in reverse order to roll back.
139
+
140
+ Key guidelines:
141
+
142
+ - Make rollbacks steps as well, so they are durable and benefit from retries.
143
+ - Ensure rollbacks are [idempotent](/docs/foundations/idempotency); they may run more than once.
144
+ - Only enqueue a compensation after its forward step succeeds.
145
+
146
+ ```typescript lineNumbers
147
+ // Forward steps
148
+ async function reserveInventory(orderId: string) {
149
+ "use step";
150
+ // ... call inventory service to reserve ...
151
+ }
152
+
153
+ async function chargePayment(orderId: string) {
154
+ "use step";
155
+ // ... charge the customer ...
156
+ }
157
+
158
+ // Rollback steps
159
+ async function releaseInventory(orderId: string) {
160
+ "use step";
161
+ // ... undo inventory reservation ...
162
+ }
163
+
164
+ async function refundPayment(orderId: string) {
165
+ "use step";
166
+ // ... refund the charge ...
167
+ }
168
+
169
+ export async function placeOrderSaga(orderId: string) {
170
+ "use workflow";
171
+
172
+ const rollbacks: Array<() => Promise<void>> = [];
173
+
174
+ try {
175
+ await reserveInventory(orderId);
176
+ rollbacks.push(() => releaseInventory(orderId));
177
+
178
+ await chargePayment(orderId);
179
+ rollbacks.push(() => refundPayment(orderId));
180
+
181
+ // ... more steps & rollbacks ...
182
+ } catch (e) {
183
+ for (const rollback of rollbacks.reverse()) {
184
+ await rollback();
185
+ }
186
+ // Rethrow so the workflow records the failure after rollbacks
187
+ throw e;
188
+ }
189
+ }
190
+ ```