workflow 5.0.0-beta.43 → 5.0.0-beta.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/internal/builtins.d.ts +4 -4
- package/dist/internal/builtins.js +6 -6
- package/docs/ai/chat-session-modeling.mdx +23 -24
- package/docs/ai/defining-tools.mdx +5 -6
- package/docs/ai/human-in-the-loop.mdx +11 -11
- package/docs/ai/index.mdx +20 -20
- package/docs/ai/message-queueing.mdx +6 -6
- package/docs/ai/meta.json +1 -0
- package/docs/ai/resumable-streams.mdx +28 -28
- package/docs/ai/sleep-and-delays.mdx +9 -9
- package/docs/ai/streaming-updates-from-tools.mdx +4 -4
- package/docs/api-reference/vitest/index.mdx +8 -8
- package/docs/api-reference/workflow/create-hook.mdx +15 -15
- package/docs/api-reference/workflow/create-webhook.mdx +15 -15
- package/docs/api-reference/workflow/define-hook.mdx +10 -10
- package/docs/api-reference/workflow/fatal-error.mdx +2 -2
- package/docs/api-reference/workflow/fetch.mdx +7 -7
- package/docs/api-reference/workflow/get-step-metadata.mdx +2 -2
- package/docs/api-reference/workflow/get-workflow-metadata.mdx +3 -3
- package/docs/api-reference/workflow/get-writable.mdx +7 -7
- package/docs/api-reference/workflow/index.mdx +1 -1
- package/docs/api-reference/workflow/retryable-error.mdx +1 -1
- package/docs/api-reference/workflow/set-attributes.mdx +2 -2
- package/docs/api-reference/workflow/sleep.mdx +3 -3
- package/docs/api-reference/workflow-ai/durable-agent.mdx +42 -42
- package/docs/api-reference/workflow-ai/index.mdx +3 -3
- package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +28 -28
- package/docs/api-reference/workflow-api/get-hook-by-token.mdx +11 -11
- package/docs/api-reference/workflow-api/get-run.mdx +10 -10
- package/docs/api-reference/workflow-api/index.mdx +2 -4
- package/docs/api-reference/workflow-api/resume-hook.mdx +14 -14
- package/docs/api-reference/workflow-api/resume-webhook.mdx +3 -3
- package/docs/api-reference/workflow-api/start.mdx +16 -15
- package/docs/api-reference/workflow-astro/workflow.mdx +3 -3
- package/docs/api-reference/workflow-errors/entity-conflict-error.mdx +4 -4
- package/docs/api-reference/workflow-errors/hook-conflict-error.mdx +4 -4
- package/docs/api-reference/workflow-errors/hook-not-found-error.mdx +8 -8
- package/docs/api-reference/workflow-errors/index.mdx +6 -6
- package/docs/api-reference/workflow-errors/precondition-failed-error.mdx +9 -9
- package/docs/api-reference/workflow-errors/run-expired-error.mdx +2 -2
- package/docs/api-reference/workflow-errors/run-not-supported-error.mdx +4 -4
- package/docs/api-reference/workflow-errors/step-not-registered-error.mdx +5 -5
- package/docs/api-reference/workflow-errors/throttle-error.mdx +2 -2
- package/docs/api-reference/workflow-errors/too-early-error.mdx +2 -2
- package/docs/api-reference/workflow-errors/workflow-error.mdx +4 -4
- package/docs/api-reference/workflow-errors/workflow-not-registered-error.mdx +5 -6
- package/docs/api-reference/workflow-errors/workflow-run-cancelled-error.mdx +6 -6
- package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +5 -5
- package/docs/api-reference/workflow-errors/workflow-run-not-completed-error.mdx +4 -4
- package/docs/api-reference/workflow-errors/workflow-run-not-found-error.mdx +4 -4
- package/docs/api-reference/workflow-errors/workflow-runtime-error.mdx +2 -2
- package/docs/api-reference/workflow-errors/workflow-world-error.mdx +8 -8
- package/docs/api-reference/workflow-globals.mdx +12 -11
- package/docs/api-reference/workflow-nest/configure-workflow-controller.mdx +2 -2
- package/docs/api-reference/workflow-nest/nest-local-builder.mdx +4 -4
- package/docs/api-reference/workflow-nest/workflow-controller.mdx +1 -1
- package/docs/api-reference/workflow-nest/workflow-module.mdx +4 -4
- package/docs/api-reference/workflow-next/with-workflow.mdx +16 -16
- package/docs/api-reference/workflow-nitro/index.mdx +6 -6
- package/docs/api-reference/workflow-nuxt/index.mdx +4 -4
- package/docs/api-reference/workflow-observability/hydrate-data.mdx +5 -5
- package/docs/api-reference/workflow-observability/hydrate-resource-io.mdx +4 -4
- package/docs/api-reference/workflow-observability/index.mdx +6 -8
- package/docs/api-reference/workflow-observability/observability-revivers.mdx +2 -2
- package/docs/api-reference/workflow-observability/parse-class-name.mdx +3 -3
- package/docs/api-reference/workflow-observability/parse-step-name.mdx +4 -4
- package/docs/api-reference/workflow-observability/parse-workflow-name.mdx +4 -4
- package/docs/api-reference/workflow-runtime/create-world.mdx +8 -8
- package/docs/api-reference/workflow-runtime/get-world-handlers.mdx +6 -6
- package/docs/api-reference/workflow-runtime/get-world.mdx +4 -4
- package/docs/api-reference/workflow-runtime/health-check.mdx +1 -1
- package/docs/api-reference/workflow-runtime/index.mdx +2 -4
- package/docs/api-reference/workflow-runtime/set-world.mdx +15 -13
- package/docs/api-reference/workflow-runtime/workflow-entrypoint.mdx +6 -5
- package/docs/api-reference/workflow-runtime/world/analytics.mdx +9 -9
- package/docs/api-reference/workflow-runtime/world/index.mdx +5 -3
- package/docs/api-reference/workflow-runtime/world/queue.mdx +11 -11
- package/docs/api-reference/workflow-runtime/world/storage.mdx +62 -28
- package/docs/api-reference/workflow-runtime/world/streams.mdx +7 -7
- package/docs/api-reference/workflow-serde/index.mdx +1 -1
- package/docs/api-reference/workflow-serde/workflow-deserialize.mdx +2 -2
- package/docs/api-reference/workflow-serde/workflow-serialize.mdx +7 -7
- package/docs/api-reference/workflow-sveltekit/workflow-plugin.mdx +3 -3
- package/docs/api-reference/workflow-vite/workflow.mdx +5 -5
- package/docs/changelog/attributes-mvp.mdx +39 -39
- package/docs/changelog/batched-event-writes.mdx +12 -12
- package/docs/changelog/eager-processing.mdx +63 -63
- package/docs/changelog/index.mdx +3 -3
- package/docs/changelog/lazy-event-creation.md +27 -27
- package/docs/changelog/resilient-resume.mdx +5 -5
- package/docs/changelog/resilient-start.mdx +14 -14
- package/docs/changelog/step-message-ownership.mdx +47 -47
- package/docs/changelog/turbo-mode.md +20 -20
- package/docs/comparisons/index.mdx +13 -13
- package/docs/comparisons/workflow-sdk-vs-aws-agentcore.mdx +15 -15
- package/docs/comparisons/workflow-sdk-vs-aws-step-functions.mdx +12 -12
- package/docs/comparisons/workflow-sdk-vs-cloudflare-workflows.mdx +11 -11
- package/docs/comparisons/workflow-sdk-vs-inngest.mdx +19 -19
- package/docs/comparisons/workflow-sdk-vs-temporal.mdx +23 -23
- package/docs/comparisons/workflow-sdk-vs-trigger-dev.mdx +18 -17
- package/docs/configuration/build-and-diagnostics.mdx +5 -5
- package/docs/configuration/cli-and-web-ui.mdx +4 -4
- package/docs/configuration/runtime-tuning.mdx +86 -23
- package/docs/configuration/worlds.mdx +28 -14
- package/docs/cookbook/advanced/child-workflows.mdx +25 -25
- package/docs/cookbook/advanced/publishing-libraries.mdx +40 -40
- package/docs/cookbook/advanced/serializable-steps.mdx +21 -21
- package/docs/cookbook/advanced/upgrading-workflows.mdx +31 -31
- package/docs/cookbook/agent-patterns/agent-cancellation.mdx +20 -20
- package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +22 -22
- package/docs/cookbook/common-patterns/batching.mdx +14 -14
- package/docs/cookbook/common-patterns/idempotency.mdx +9 -9
- package/docs/cookbook/common-patterns/rate-limiting.mdx +3 -3
- package/docs/cookbook/common-patterns/saga.mdx +19 -19
- package/docs/cookbook/common-patterns/scheduling.mdx +23 -23
- package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +26 -26
- package/docs/cookbook/common-patterns/timeouts.mdx +23 -23
- package/docs/cookbook/common-patterns/webhooks.mdx +6 -6
- package/docs/cookbook/common-patterns/workflow-composition.mdx +19 -19
- package/docs/cookbook/index.mdx +22 -22
- package/docs/cookbook/integrations/ai-sdk.mdx +43 -41
- package/docs/cookbook/integrations/chat-sdk.mdx +34 -34
- package/docs/cookbook/integrations/sandbox.mdx +46 -46
- package/docs/deploying.mdx +15 -15
- package/docs/errors/abort-signal-timeout-in-workflow.mdx +12 -12
- package/docs/errors/corrupted-event-log.mdx +11 -11
- package/docs/errors/deployment-mismatch.mdx +14 -14
- package/docs/errors/fetch-in-workflow.mdx +8 -8
- package/docs/errors/hook-conflict.mdx +11 -11
- package/docs/errors/index.mdx +1 -1
- package/docs/errors/node-js-module-in-workflow.mdx +5 -5
- package/docs/errors/replay-divergence.mdx +2 -2
- package/docs/errors/runtime-decryption-failed.mdx +12 -12
- package/docs/errors/serialization-failed.mdx +40 -12
- package/docs/errors/start-invalid-workflow-function.mdx +5 -5
- package/docs/errors/step-executed-multiple-times.mdx +2 -2
- package/docs/errors/step-not-registered.mdx +5 -5
- package/docs/errors/timeout-in-workflow.mdx +8 -8
- package/docs/errors/webhook-invalid-respond-with-value.mdx +18 -18
- package/docs/errors/webhook-response-not-sent.mdx +16 -16
- package/docs/errors/workflow-not-registered.mdx +5 -5
- package/docs/foundations/cancellation.mdx +31 -31
- package/docs/foundations/errors-and-retries.mdx +42 -11
- package/docs/foundations/hooks.mdx +35 -35
- package/docs/foundations/idempotency.mdx +9 -9
- package/docs/foundations/serialization.mdx +21 -22
- package/docs/foundations/starting-workflows.mdx +36 -37
- package/docs/foundations/streaming.mdx +46 -41
- package/docs/foundations/versioning.mdx +3 -3
- package/docs/foundations/workflows-and-steps.mdx +9 -9
- package/docs/getting-started/astro.mdx +16 -16
- package/docs/getting-started/express.mdx +8 -8
- package/docs/getting-started/fastify.mdx +8 -8
- package/docs/getting-started/hono.mdx +8 -8
- package/docs/getting-started/nestjs.mdx +18 -17
- package/docs/getting-started/next.mdx +11 -11
- package/docs/getting-started/nitro.mdx +16 -16
- package/docs/getting-started/nuxt.mdx +8 -8
- package/docs/getting-started/python.mdx +4 -4
- package/docs/getting-started/react-router/v7.mdx +1 -1
- package/docs/getting-started/react-router/v8.mdx +1 -1
- package/docs/getting-started/sveltekit.mdx +14 -14
- package/docs/getting-started/tanstack-start.mdx +12 -12
- package/docs/getting-started/vite.mdx +8 -8
- package/docs/how-it-works/cancellation.mdx +62 -62
- package/docs/how-it-works/code-transform.mdx +66 -54
- package/docs/how-it-works/encryption.mdx +25 -21
- package/docs/how-it-works/event-sourcing.mdx +53 -35
- package/docs/how-it-works/framework-integrations.mdx +12 -12
- package/docs/how-it-works/understanding-directives.mdx +21 -21
- package/docs/internal/index.mdx +6 -6
- package/docs/internal/nitro-native-build.mdx +2 -2
- package/docs/internal/nitro-web-ui.mdx +4 -4
- package/docs/internal/serializable-abort-controller.mdx +7 -7
- package/docs/observability/attributes.mdx +3 -3
- package/docs/observability/index.mdx +14 -10
- package/docs/observability/tracing.mdx +10 -10
- package/docs/testing/index.mdx +33 -33
- package/docs/testing/server-based.mdx +10 -10
- package/docs/whats-new.mdx +185 -0
- package/package.json +12 -12
|
@@ -13,14 +13,14 @@ summary: Use defineHook with the tool call ID to suspend an agent for human appr
|
|
|
13
13
|
This recipe uses the deprecated `DurableAgent` API. For new agents, use AI SDK's [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) and follow the [migration guide](https://ai-sdk.dev/v7/docs/agents/workflow-agent#migrating-from-durableagent). The human-in-the-loop pattern here (hooks, `Promise.race`, approval gating) applies to either API.
|
|
14
14
|
</Callout>
|
|
15
15
|
|
|
16
|
-
Use this pattern when an AI agent needs human confirmation before performing a consequential action like booking, purchasing, or publishing. The workflow suspends without consuming resources until the human responds.
|
|
16
|
+
Use this pattern when an AI agent needs human confirmation before performing a consequential action like booking, purchasing, or publishing. The workflow suspends without consuming resources until the human responds through a user interface (UI) or API.
|
|
17
17
|
|
|
18
18
|
## When to use this
|
|
19
19
|
|
|
20
20
|
- Booking confirmations where users must approve before charges are made
|
|
21
21
|
- Content publishing gates where an editor must sign off
|
|
22
|
-
-
|
|
23
|
-
- Actions with side effects that
|
|
22
|
+
- Agent actions where the cost of an error justifies a human check
|
|
23
|
+
- Actions with side effects that are difficult to reverse
|
|
24
24
|
|
|
25
25
|
## Pattern
|
|
26
26
|
|
|
@@ -67,7 +67,7 @@ async function confirmBooking({ flightId, passenger }: {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
// Stream a custom data part so the client can render the approval UI.
|
|
70
|
-
// This MUST run before the hook suspends the workflow
|
|
70
|
+
// This MUST run before the hook suspends the workflow, otherwise
|
|
71
71
|
// the tool-invocation won't appear in the stream until the tool returns,
|
|
72
72
|
// and the client would have no way to show approval buttons.
|
|
73
73
|
async function emitApprovalRequest(details: {
|
|
@@ -107,7 +107,7 @@ async function emitApprovalResolved(details: {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
// No "use step"
|
|
110
|
+
// No "use step": hooks are workflow-level primitives
|
|
111
111
|
async function requestBookingApproval(
|
|
112
112
|
{ flightId, passenger, price }: {
|
|
113
113
|
flightId: string;
|
|
@@ -238,26 +238,26 @@ const approvalResult = messages
|
|
|
238
238
|
|
|
239
239
|
## How it works
|
|
240
240
|
|
|
241
|
-
1. **`defineHook()` with schema
|
|
242
|
-
2. **`toolCallId` as token
|
|
243
|
-
3. **`emitApprovalRequest` step
|
|
244
|
-
4. **No `"use step"` on the approval tool
|
|
245
|
-
5. **`Promise.race` with sleep
|
|
246
|
-
6. **`emitApprovalResolved` step
|
|
241
|
+
1. **`defineHook()` with schema**: Creates a typed hook with Zod validation. The approval payload is validated before the workflow receives it.
|
|
242
|
+
2. **`toolCallId` as token**: Uses the tool call ID as the hook token, linking the hook to the specific tool invocation.
|
|
243
|
+
3. **`emitApprovalRequest` step**: Writes a `data-approval-needed` custom data part to the stream *before* the hook suspends. Without this step, the client wouldn't see the approval controls because tool invocations don't stream until the tool returns.
|
|
244
|
+
4. **No `"use step"` on the approval tool**: Runs the tool at the workflow level because `defineHook().create()` is a workflow primitive. The tool calls step functions (`emitApprovalRequest`, `emitApprovalResolved`, and `confirmBooking`) for I/O.
|
|
245
|
+
5. **`Promise.race` with sleep**: Races the approval against a durable timeout. If nobody responds, the workflow continues with an expiration message.
|
|
246
|
+
6. **`emitApprovalResolved` step**: Writes the outcome to the stream so the client can update the card immediately without waiting for the tool-invocation result.
|
|
247
247
|
|
|
248
248
|
## Adapting to your use case
|
|
249
249
|
|
|
250
|
-
- **Change the approval schema
|
|
251
|
-
- **Multiple approval gates
|
|
252
|
-
- **Escalation
|
|
253
|
-
- **Adjust timeout
|
|
254
|
-
- **Workflow-level
|
|
250
|
+
- **Change the approval schema**: Add fields such as `reason`, `amount`, and `reviewerEmail` to match your domain.
|
|
251
|
+
- **Multiple approval gates**: Apply the pattern to any number of tools. Each tool creates its own hook with its own `toolCallId`.
|
|
252
|
+
- **Escalation**: If the first approver doesn't respond, use `sleep()` and another hook to escalate to a backup reviewer.
|
|
253
|
+
- **Adjust the timeout**: Use `"24h"` for production and shorter durations for demos.
|
|
254
|
+
- **Workflow-level versus step tools**: Tools that use `sleep()`, `defineHook()`, or other workflow primitives must not use `"use step"`. Tools with only I/O, such as API calls and database queries, should use `"use step"` for retries.
|
|
255
255
|
|
|
256
256
|
## Key APIs
|
|
257
257
|
|
|
258
|
-
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions)
|
|
259
|
-
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions)
|
|
260
|
-
- [`defineHook()`](/docs/api-reference/workflow/define-hook)
|
|
261
|
-
- [`sleep()`](/docs/api-reference/workflow/sleep)
|
|
262
|
-
- [`getWritable()`](/docs/api-reference/workflow/get-writable)
|
|
263
|
-
- [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent)
|
|
258
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions): Declares the orchestrator function.
|
|
259
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions): Declares step functions with retries.
|
|
260
|
+
- [`defineHook()`](/docs/api-reference/workflow/define-hook): Defines a type-safe hook with schema validation.
|
|
261
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep): Provides a durable timeout for approval expiration.
|
|
262
|
+
- [`getWritable()`](/docs/api-reference/workflow/get-writable): Streams custom data parts from steps.
|
|
263
|
+
- [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent): Provides AI SDK's durable agent and replaces `DurableAgent`.
|
|
@@ -13,7 +13,7 @@ Use batching when you need to process a large list of items in parallel while co
|
|
|
13
13
|
|
|
14
14
|
## When to use this
|
|
15
15
|
|
|
16
|
-
- Bulk data imports (contacts, orders, products from a CSV)
|
|
16
|
+
- Bulk data imports (contacts, orders, or products from a comma-separated values (CSV) file)
|
|
17
17
|
- Processing hundreds or thousands of items against external APIs
|
|
18
18
|
- Calling rate-limited APIs where you need to control concurrency
|
|
19
19
|
- Any fan-out where you want failure isolation between groups
|
|
@@ -21,7 +21,7 @@ Use batching when you need to process a large list of items in parallel while co
|
|
|
21
21
|
## How it works
|
|
22
22
|
|
|
23
23
|
1. Records are split into fixed-size batches.
|
|
24
|
-
2. Each batch runs in parallel
|
|
24
|
+
2. Each batch runs in parallel through `Promise.allSettled`, so failures in one record don't affect others.
|
|
25
25
|
3. A `sleep()` between batches paces requests to avoid overloading downstream services.
|
|
26
26
|
4. After all batches, a summary is returned with succeeded/failed counts.
|
|
27
27
|
|
|
@@ -45,7 +45,7 @@ export async function batchImport(records: Record[], batchSize: number) {
|
|
|
45
45
|
for (let i = 0; i < records.length; i += batchSize) {
|
|
46
46
|
const batch = records.slice(i, i + batchSize);
|
|
47
47
|
|
|
48
|
-
// Run batch in parallel
|
|
48
|
+
// Run batch in parallel: failures are isolated per record
|
|
49
49
|
const outcomes = await Promise.allSettled( // [!code highlight]
|
|
50
50
|
batch.map((record) => processRecord(record))
|
|
51
51
|
);
|
|
@@ -89,21 +89,21 @@ async function processRecord(record: Record): Promise<string> {
|
|
|
89
89
|
|
|
90
90
|
## Adapting to your use case
|
|
91
91
|
|
|
92
|
-
- Replace the `Record` type with your actual data shape
|
|
93
|
-
- Replace `processRecord()` with your
|
|
92
|
+
- Replace the `Record` type with your actual data shape, such as orders, images, or products.
|
|
93
|
+
- Replace `processRecord()` with your import logic, such as database upserts, API calls, or file processing.
|
|
94
94
|
- Tune `batchSize` and the `sleep()` duration to match your downstream rate limits.
|
|
95
|
-
- Add or remove tracking as needed
|
|
95
|
+
- Add or remove tracking as needed; the pattern works with any item type.
|
|
96
96
|
|
|
97
97
|
## Tips
|
|
98
98
|
|
|
99
|
-
- **Use `Promise.allSettled`
|
|
100
|
-
- **Tune batch size to your downstream API limits
|
|
101
|
-
- **Add pacing with `sleep()
|
|
102
|
-
- **
|
|
99
|
+
- **Use `Promise.allSettled` instead of `Promise.all`**: Use this pattern when you want to continue even if some items fail. `Promise.all` rejects on the first failure, while `allSettled` waits for everything and identifies failures.
|
|
100
|
+
- **Tune batch size to your downstream API limits**: If the API allows 10 concurrent requests, use `batchSize: 10`.
|
|
101
|
+
- **Add pacing with `sleep()`**: Add a delay between batches to respect rate limits. The sleep is durable and survives cold starts.
|
|
102
|
+
- **Treat each `processRecord` call as an independent step**: If one call fails, it retries up to three times without affecting other items in the batch.
|
|
103
103
|
|
|
104
104
|
## Key APIs
|
|
105
105
|
|
|
106
|
-
- [`"use workflow"`](/docs/foundations/workflows-and-steps)
|
|
107
|
-
- [`"use step"`](/docs/foundations/workflows-and-steps)
|
|
108
|
-
- [`sleep()`](/docs/api-reference/workflow/sleep)
|
|
109
|
-
- [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled)
|
|
106
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps): Marks the orchestrator function.
|
|
107
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps): Marks functions that run with full Node.js access.
|
|
108
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep): Adds a pacing delay between batches.
|
|
109
|
+
- [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled): Runs items in parallel and isolates failures.
|
|
@@ -6,7 +6,7 @@ summary: Use step IDs for retry-safe external calls, and use deterministic hook
|
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
<CopyPrompt
|
|
9
|
-
text="Make this workflow's side effects idempotent. For retry-safe external calls, read the deterministic step ID inside the "use step" function with `getStepMetadata()` from `workflow` and pass `stepId` as the idempotency key to the external API (for example Stripe's `Idempotency-Key` header) so step retries deduplicate. For duplicate workflow starts, derive a deterministic hook token from the domain key (for example `order:${orderId}`): in the API route, look up the active hook with `getHookByToken(token)` from `workflow/api`
|
|
9
|
+
text="Make this workflow's side effects idempotent. For retry-safe external calls, read the deterministic step ID inside the "use step" function with `getStepMetadata()` from `workflow` and pass `stepId` as the idempotency key to the external API (for example Stripe's `Idempotency-Key` header) so step retries deduplicate. For duplicate workflow starts, derive a deterministic hook token from the domain key (for example `order:${orderId}`): in the API route, look up the active hook with `getHookByToken(token)` from `workflow/api` (catching `HookNotFoundError` from `workflow/errors`) and reuse its `runId`, otherwise call `start(...)`; inside the workflow, create the hook with the same token and check `await hook.getConflict()` before duplicate-sensitive work. Verify retried steps deduplicate, duplicate starts reuse the active run, and conflicts are handled."
|
|
10
10
|
/>
|
|
11
11
|
|
|
12
12
|
Use idempotency when a retry or duplicate request should not repeat the underlying work. In Workflow, there are two common patterns: use the step ID for retry-safe external calls, and use hook tokens to coordinate duplicate workflow starts.
|
|
@@ -82,14 +82,14 @@ export async function POST(request: Request) {
|
|
|
82
82
|
}
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
-
The workflow should create the deterministic hook and check `await hook.getConflict()` before duplicate-sensitive work
|
|
85
|
+
The workflow should create the deterministic hook and check `await hook.getConflict()` before duplicate-sensitive work: awaiting `getConflict()` suspends the workflow to commit the hook registration and resolves with the conflicting run when another active run already owns the token (or `null` once the hook is registered). See [Run idempotency](/docs/foundations/idempotency#run-idempotency) for the full pattern, including how to steer an active run with `resumeHook()` and how to handle the current race between `start()` and hook registration.
|
|
86
86
|
|
|
87
87
|
## Key APIs
|
|
88
88
|
|
|
89
|
-
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions)
|
|
90
|
-
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions)
|
|
91
|
-
- [`getStepMetadata()`](/docs/api-reference/workflow/get-step-metadata)
|
|
92
|
-
- [`createHook()`](/docs/api-reference/workflow/create-hook)
|
|
93
|
-
- [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token)
|
|
94
|
-
- [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook)
|
|
95
|
-
- [`start()`](/docs/api-reference/workflow-api/start)
|
|
89
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions): Declares the orchestrator function.
|
|
90
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions): Declares step functions with full Node.js access.
|
|
91
|
+
- [`getStepMetadata()`](/docs/api-reference/workflow/get-step-metadata): Provides the deterministic `stepId` for idempotency keys.
|
|
92
|
+
- [`createHook()`](/docs/api-reference/workflow/create-hook): Creates a hook with an optional deterministic token.
|
|
93
|
+
- [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token): Finds the hook that owns a token.
|
|
94
|
+
- [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook): Resumes the active hook when the duplicate request carries data.
|
|
95
|
+
- [`start()`](/docs/api-reference/workflow-api/start): Starts a new workflow run.
|
|
@@ -67,7 +67,7 @@ async function upsertToWarehouse(contactId: string, contact: unknown) {
|
|
|
67
67
|
}
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
## Pattern:
|
|
70
|
+
## Pattern: exponential backoff
|
|
71
71
|
|
|
72
72
|
Use `getStepMetadata()` to access the current attempt number and calculate increasing delays:
|
|
73
73
|
|
|
@@ -90,7 +90,7 @@ async function callFlakeyApi(endpoint: string) {
|
|
|
90
90
|
}
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
## Pattern:
|
|
93
|
+
## Pattern: circuit breaker with sleep
|
|
94
94
|
|
|
95
95
|
When a dependency is completely down, stop hitting it for a cooldown period using `sleep()`, then probe with a single test request:
|
|
96
96
|
|
|
@@ -138,7 +138,7 @@ async function callService(requestNum: number): Promise<boolean> {
|
|
|
138
138
|
}
|
|
139
139
|
```
|
|
140
140
|
|
|
141
|
-
## Pattern:
|
|
141
|
+
## Pattern: custom max retries
|
|
142
142
|
|
|
143
143
|
Override the default retry count (3) for steps that need more or fewer attempts:
|
|
144
144
|
|
|
@@ -20,8 +20,8 @@ Use the saga pattern when a business transaction spans multiple services and you
|
|
|
20
20
|
## How it works
|
|
21
21
|
|
|
22
22
|
1. Each forward step does work and registers a compensation function.
|
|
23
|
-
2. If any step throws `FatalError`, the catch block runs compensations in reverse (LIFO) order to restore consistency.
|
|
24
|
-
3. Regular errors are retried automatically
|
|
23
|
+
2. If any step throws `FatalError`, the catch block runs compensations in reverse, or last in, first out (LIFO), order to restore consistency.
|
|
24
|
+
3. Regular errors are retried automatically, up to three times by default. Use `FatalError` only for permanent failures where retrying won't help.
|
|
25
25
|
|
|
26
26
|
## Pattern
|
|
27
27
|
|
|
@@ -53,7 +53,7 @@ export async function subscriptionUpgradeSaga(accountId: string, seats: number)
|
|
|
53
53
|
const entitlementId = await provisionSeats(accountId, seats);
|
|
54
54
|
compensations.push(() => deprovisionSeats(accountId, entitlementId)); // [!code highlight]
|
|
55
55
|
|
|
56
|
-
// No compensation
|
|
56
|
+
// No compensation: notifications are fire-and-forget
|
|
57
57
|
await sendConfirmation(accountId, invoiceId, entitlementId);
|
|
58
58
|
|
|
59
59
|
return { status: "completed" };
|
|
@@ -70,7 +70,7 @@ export async function subscriptionUpgradeSaga(accountId: string, seats: number)
|
|
|
70
70
|
|
|
71
71
|
### Step functions
|
|
72
72
|
|
|
73
|
-
Each step is a `"use step"` function with full Node.js access (fetch, fs, npm packages). Forward steps do the work and throw `FatalError` on permanent failure; compensation steps undo it and must be idempotent
|
|
73
|
+
Each step is a `"use step"` function with full Node.js access (fetch, fs, npm packages). Forward steps do the work and throw `FatalError` on permanent failure; compensation steps undo it and must be idempotent: safe to call multiple times if the workflow restarts mid-rollback.
|
|
74
74
|
|
|
75
75
|
```typescript
|
|
76
76
|
import { FatalError } from "workflow";
|
|
@@ -122,7 +122,7 @@ async function sendConfirmation(
|
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
// Compensation steps
|
|
125
|
+
// Compensation steps: must be idempotent
|
|
126
126
|
|
|
127
127
|
async function releaseSeats(accountId: string, reservationId: string): Promise<void> {
|
|
128
128
|
"use step";
|
|
@@ -151,7 +151,7 @@ async function deprovisionSeats(accountId: string, entitlementId: string): Promi
|
|
|
151
151
|
|
|
152
152
|
### Streaming step progress (optional)
|
|
153
153
|
|
|
154
|
-
Use `getWritable()` to stream progress events to a UI so users can see each step execute in real time.
|
|
154
|
+
Use `getWritable()` to stream progress events to a user interface (UI) so users can see each step execute in real time.
|
|
155
155
|
|
|
156
156
|
```typescript
|
|
157
157
|
import { FatalError } from "workflow";
|
|
@@ -204,7 +204,7 @@ export async function subscriptionUpgradeSaga(accountId: string, seats: number)
|
|
|
204
204
|
compensations.push({ name: "Deprovision Seats", execute: () => deprovisionSeats(accountId, entitlementId) });
|
|
205
205
|
await emit({ type: "step_done", step: "Provision Seats", detail: entitlementId });
|
|
206
206
|
|
|
207
|
-
// No compensation
|
|
207
|
+
// No compensation: notifications are fire-and-forget
|
|
208
208
|
await emit({ type: "step_start", step: "Send Confirmation" });
|
|
209
209
|
await sendConfirmation(accountId, invoiceId, entitlementId);
|
|
210
210
|
await emit({ type: "step_done", step: "Send Confirmation", detail: "sent" });
|
|
@@ -231,21 +231,21 @@ export async function subscriptionUpgradeSaga(accountId: string, seats: number)
|
|
|
231
231
|
## Adapting to your use case
|
|
232
232
|
|
|
233
233
|
- Replace the step functions with real API calls. Each `"use step"` function has full Node.js access.
|
|
234
|
-
- Add or remove steps as needed
|
|
235
|
-
- Make compensations idempotent
|
|
236
|
-
- The `emit()` calls and `SagaEvent` type are optional
|
|
234
|
+
- Add or remove steps as needed; the pattern scales to any number of steps.
|
|
235
|
+
- Make compensations idempotent, since they may be retried if the workflow restarts mid-rollback.
|
|
236
|
+
- The `emit()` calls and `SagaEvent` type are optional; remove them if you don't need real-time UI progress.
|
|
237
237
|
|
|
238
238
|
## Tips
|
|
239
239
|
|
|
240
|
-
- **Use `FatalError` for permanent failures
|
|
241
|
-
- **Make compensations idempotent
|
|
242
|
-
- **
|
|
243
|
-
- **Capture values in closures carefully
|
|
244
|
-
- **
|
|
240
|
+
- **Use `FatalError` for permanent failures**: Regular errors trigger automatic retries, up to three times by default. Throw `FatalError` when retrying won't help, such as for insufficient funds or invalid input.
|
|
241
|
+
- **Make compensations idempotent**: If a compensation step is retried, it should produce the same result. Check whether the resource was already released before releasing it again.
|
|
242
|
+
- **Use `"use step"` functions for compensation steps**: This makes them durable. If the workflow restarts during rollback, it resumes where it left off.
|
|
243
|
+
- **Capture values in closures carefully**: Use block-scoped variables or copy values before pushing compensations to avoid referencing stale state.
|
|
244
|
+
- **Skip compensations for notifications**: Fire-and-forget steps such as sending emails or Slack messages typically don't register a compensation.
|
|
245
245
|
|
|
246
246
|
## Key APIs
|
|
247
247
|
|
|
248
|
-
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions)
|
|
249
|
-
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions)
|
|
250
|
-
- [`FatalError`](/docs/api-reference/workflow/fatal-error)
|
|
251
|
-
- [`getWritable()`](/docs/api-reference/workflow/get-writable)
|
|
248
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions): Declares the orchestrator function.
|
|
249
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions): Declares step functions with full Node.js access.
|
|
250
|
+
- [`FatalError`](/docs/api-reference/workflow/fatal-error): Represents a non-retryable error that triggers compensation.
|
|
251
|
+
- [`getWritable()`](/docs/api-reference/workflow/get-writable): Streams data from workflows for real-time UI updates.
|
|
@@ -6,10 +6,10 @@ summary: Schedule future actions with durable sleep that survives cold starts, a
|
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
<CopyPrompt
|
|
9
|
-
text="Create an interruptible scheduled workflow. Import `defineHook` and `sleep` from `workflow`. Define a cancellation hook such as `export const cancelDrip = defineHook<{ reason?: string }>()`. In an exported workflow function with "use workflow", create the hook once, send the first message in a "use step" helper, then `await Promise.race([sleep("2d").then(() => false), cancelHook.then(() => true)])` before each delayed action. If the hook wins, return a
|
|
9
|
+
text="Create an interruptible scheduled workflow. Import `defineHook` and `sleep` from `workflow`. Define a cancellation hook such as `export const cancelDrip = defineHook<{ reason?: string }>()`. In an exported workflow function with "use workflow", create the hook once, send the first message in a "use step" helper, then `await Promise.race([sleep("2d").then(() => false), cancelHook.then(() => true)])` before each delayed action. If the hook wins, return a canceled status; if sleep wins, continue to the next step. Add an API route that calls `cancelDrip.resume(runIdOrToken, { reason })` or `resumeHook()` from `workflow/api`. Verify scheduled delivery, cancellation before sleep completes, and resume after server restart."
|
|
10
10
|
/>
|
|
11
11
|
|
|
12
|
-
Workflow's `sleep()` is durable
|
|
12
|
+
Workflow's `sleep()` is durable: it survives cold starts, restarts, and deployments. Combined with `defineHook()` and `Promise.race()`, it becomes the foundation for interruptible scheduled workflows like drip campaigns, reminders, and timed sequences.
|
|
13
13
|
|
|
14
14
|
<Callout type="info">
|
|
15
15
|
Scheduled workflows are still pinned to the deployment that started them. If you are building recurring or indefinitely running schedules that should adopt newer code over time, see [Versioning](/docs/foundations/versioning) for the explicit `deploymentId: "latest"` continuation pattern.
|
|
@@ -17,13 +17,13 @@ Scheduled workflows are still pinned to the deployment that started them. If you
|
|
|
17
17
|
|
|
18
18
|
## When to use this
|
|
19
19
|
|
|
20
|
-
- Sending emails on a schedule
|
|
21
|
-
- Waiting for a deadline
|
|
22
|
-
-
|
|
20
|
+
- Sending emails on a schedule, such as drip campaigns, onboarding sequences, or reminders
|
|
21
|
+
- Waiting for a deadline while allowing early cancellation
|
|
22
|
+
- Reliable, interruptible patterns that perform one action, wait for a specified time, and then perform another action
|
|
23
23
|
|
|
24
24
|
## Drip campaign with cancellation
|
|
25
25
|
|
|
26
|
-
A drip campaign sends emails at intervals, sleeping between each. Each sleep races against a cancellation hook
|
|
26
|
+
A drip campaign sends emails at intervals, sleeping between each message. Each sleep races against a cancellation hook. If an external event fires the hook, such as when a user converts or unsubscribes, the campaign stops immediately.
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
29
|
import { defineHook, sleep } from "workflow";
|
|
@@ -105,10 +105,10 @@ export async function POST(req: Request) {
|
|
|
105
105
|
|
|
106
106
|
## How it works
|
|
107
107
|
|
|
108
|
-
1. **Durable sleep
|
|
109
|
-
2. **Hook creation
|
|
110
|
-
3. **Race
|
|
111
|
-
4. **Fresh hooks per window
|
|
108
|
+
1. **Durable sleep**: `sleep("2d")` persists through restarts at zero compute cost. The workflow resumes when the timer fires.
|
|
109
|
+
2. **Hook creation**: `cancelDrip.create({ token })` registers a hook that resolves when any external system calls `.resume()` with the same token.
|
|
110
|
+
3. **Race**: `Promise.race([sleep(...), hook])` blocks until either the timer fires or the hook is resumed, whichever comes first.
|
|
111
|
+
4. **Fresh hooks per window**: After a sleep completes normally, the previous hook instance is consumed. A new `.create()` call registers a fresh hook for the next sleep window, reusing the same token.
|
|
112
112
|
|
|
113
113
|
<Callout type="info">
|
|
114
114
|
Deterministic hook tokens can also serve as the idempotency point for scheduled runs. If duplicate schedule starts would send duplicate campaigns or reminders, create a hook with a token derived from the campaign key near the beginning of the workflow and route retries through that hook. If two scheduled starts race, the duplicate run can detect the conflict early with `await hook.getConflict()`, which resolves with the active owner so the duplicate can defer to it. See [Idempotency](/docs/foundations/idempotency).
|
|
@@ -116,22 +116,22 @@ Deterministic hook tokens can also serve as the idempotency point for scheduled
|
|
|
116
116
|
|
|
117
117
|
## Adapting to your use case
|
|
118
118
|
|
|
119
|
-
- **Change durations
|
|
120
|
-
- **Add more steps
|
|
121
|
-
- **Snooze instead of cancel
|
|
122
|
-
- **
|
|
123
|
-
- **
|
|
119
|
+
- **Change durations**: Replace `"2d"` with any duration string (`"1h"`, `"7d"`, or `"30m"`) or a `Date` object for absolute times.
|
|
120
|
+
- **Add more steps**: The pattern scales to any number of email-then-sleep pairs.
|
|
121
|
+
- **Snooze instead of cancel**: Resolve the hook with a `snooze` payload and sleep again with `sleep(new Date(Date.now() + payload.snoozeMs))`.
|
|
122
|
+
- **Set a timeout for any operation**: Use the same `Promise.race(sleep, work)` pattern to add deadlines to slow steps.
|
|
123
|
+
- **Use production providers**: Replace the `sendEmail` step body with Resend, Postmark, or any HTTP API. The `"use step"` function has full Node.js access.
|
|
124
124
|
|
|
125
125
|
## Tips
|
|
126
126
|
|
|
127
|
-
-
|
|
128
|
-
- **
|
|
129
|
-
- **Use `sleep()` in workflow context
|
|
127
|
+
- **Pass supported values to `sleep()`**: Use duration strings (`"1d"`, `"2h"`, or `"30s"`), milliseconds, or `Date` objects for sleeping until a specific time.
|
|
128
|
+
- **Sleeping consumes no compute or memory**: A workflow waiting on `sleep("7d")` consumes no compute or memory while sleeping.
|
|
129
|
+
- **Use `sleep()` only in workflow context**: Step functions cannot call `sleep()` directly. If a step needs a delay, use `setTimeout` inside the step.
|
|
130
130
|
|
|
131
131
|
## Key APIs
|
|
132
132
|
|
|
133
|
-
- [`"use workflow"`](/docs/foundations/workflows-and-steps)
|
|
134
|
-
- [`"use step"`](/docs/foundations/workflows-and-steps)
|
|
135
|
-
- [`sleep()`](/docs/api-reference/workflow/sleep)
|
|
136
|
-
- [`defineHook()`](/docs/api-reference/workflow/define-hook)
|
|
137
|
-
- [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)
|
|
133
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps): Marks the orchestrator function.
|
|
134
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps): Marks functions that run with full Node.js access.
|
|
135
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep): Provides a durable wait that survives restarts with zero compute cost.
|
|
136
|
+
- [`defineHook()`](/docs/api-reference/workflow/define-hook): Creates a typed hook that external systems can fire.
|
|
137
|
+
- [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race): Races sleep against hooks for interruptible waits.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: Sequential & Parallel Execution
|
|
3
|
-
description: Compose steps with familiar async/await patterns
|
|
3
|
+
description: Compose steps with familiar async/await patterns, sequential await, Promise.all, and Promise.race.
|
|
4
4
|
type: guide
|
|
5
|
-
summary: Workflows are
|
|
5
|
+
summary: Workflows are plain async functions, so all the standard composition primitives (await, Promise.all, Promise.race) apply unchanged, including racing webhooks against durable sleeps.
|
|
6
6
|
related:
|
|
7
7
|
- /docs/foundations/workflows-and-steps
|
|
8
8
|
- /cookbook/common-patterns/timeouts
|
|
@@ -10,17 +10,17 @@ related:
|
|
|
10
10
|
---
|
|
11
11
|
|
|
12
12
|
<CopyPrompt
|
|
13
|
-
text="Compose these workflow steps with standard async/await patterns. In the exported "use workflow" function, chain dependent "use step" calls with sequential `await`; run independent steps concurrently by starting them without `await` and awaiting `Promise.all([...])`; and use `Promise.race([...])` to act on whichever promise settles first. These compose with durable primitives
|
|
13
|
+
text="Compose these workflow steps with standard async/await patterns. In the exported "use workflow" function, chain dependent "use step" calls with sequential `await`; run independent steps concurrently by starting them without `await` and awaiting `Promise.all([...])`; and use `Promise.race([...])` to act on whichever promise settles first. These compose with durable primitives: race a step or a webhook from `createWebhook()` against `sleep()` from `workflow` for deadlines. Keep every step input and output serializable, and remember `Promise.race` does not cancel the losing branch (it keeps running), so side-effectful losers need idempotency keys. Verify sequential ordering, parallel execution, and both race outcomes."
|
|
14
14
|
/>
|
|
15
15
|
|
|
16
|
-
Workflows are written in plain async/await
|
|
16
|
+
Workflows are written in plain async/await: there's no new control-flow API to learn. Sequential awaits chain steps that depend on each other, `Promise.all` runs independent steps in parallel, and `Promise.race` returns whichever finishes first. These compose with workflow primitives like [`sleep()`](/docs/api-reference/workflow/sleep) and [`createWebhook()`](/docs/api-reference/workflow/create-webhook) since those are also promises.
|
|
17
17
|
|
|
18
18
|
## When to use this
|
|
19
19
|
|
|
20
|
-
- **Pipelines
|
|
21
|
-
- **Independent fan-out
|
|
22
|
-
- **Race conditions
|
|
23
|
-
- **Mixing primitives
|
|
20
|
+
- **Pipelines**: Each step depends on the previous step's output (validate → process → store).
|
|
21
|
+
- **Independent fan-out**: Fetch multiple resources or perform multiple actions that don't depend on each other.
|
|
22
|
+
- **Race conditions**: Return as soon as one operation completes, such as for a timeout, first responder, or deadline.
|
|
23
|
+
- **Mixing primitives**: Run steps, sleeps, and webhooks side by side in the same control-flow expression.
|
|
24
24
|
|
|
25
25
|
## Pattern
|
|
26
26
|
|
|
@@ -68,7 +68,7 @@ export async function fetchUserData(userId: string) {
|
|
|
68
68
|
|
|
69
69
|
### Race with `Promise.race`
|
|
70
70
|
|
|
71
|
-
`Promise.race` resolves as soon as the first promise settles. Since [`sleep()`](/docs/api-reference/workflow/sleep) and [`createWebhook()`](/docs/api-reference/workflow/create-webhook) return promises, they compose naturally
|
|
71
|
+
`Promise.race` resolves as soon as the first promise settles. Since [`sleep()`](/docs/api-reference/workflow/sleep) and [`createWebhook()`](/docs/api-reference/workflow/create-webhook) return promises, they compose naturally. For example, waiting for a webhook callback with a deadline:
|
|
72
72
|
|
|
73
73
|
```typescript lineNumbers
|
|
74
74
|
import { sleep, createWebhook } from "workflow";
|
|
@@ -90,11 +90,11 @@ export async function runExternalTask(userId: string) {
|
|
|
90
90
|
}
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
For racing operations against deadlines specifically (timeouts), see the dedicated [Timeouts](/cookbook/common-patterns/timeouts) recipe
|
|
93
|
+
For racing operations against deadlines specifically (timeouts), see the dedicated [Timeouts](/cookbook/common-patterns/timeouts) recipe, which covers result discrimination, `FatalError` semantics, and the "loser keeps running" caveat.
|
|
94
94
|
|
|
95
95
|
### Combining sequential, parallel, and durable primitives
|
|
96
96
|
|
|
97
|
-
Most
|
|
97
|
+
Most workflows combine all three patterns. The following simplified version of the [birthday card generator demo](https://github.com/vercel/workflow-examples/tree/main/birthday-card-generator) uses sequential card generation, parallel invitation-response fan-out, non-blocking webhook collection, and a durable sleep until the birthday:
|
|
98
98
|
|
|
99
99
|
```typescript lineNumbers
|
|
100
100
|
import { createWebhook, sleep, type Webhook } from "workflow";
|
|
@@ -136,24 +136,24 @@ export async function birthdayWorkflow(
|
|
|
136
136
|
|
|
137
137
|
## How it works
|
|
138
138
|
|
|
139
|
-
1. **`await` is durable
|
|
140
|
-
2. **`Promise.all` runs steps concurrently
|
|
141
|
-
3. **`Promise.race` resolves
|
|
142
|
-
4. **All primitives are promises
|
|
139
|
+
1. **`await` is durable**: When the workflow awaits a step, the runtime persists the step's input, suspends the workflow, runs the step, and replays the workflow with the step's result on resume. The same applies to `sleep()` and `createWebhook()`.
|
|
140
|
+
2. **`Promise.all` runs steps concurrently**: Each promise in the array is suspended on its own, and the workflow resumes only when all have settled. If any promise rejects, the whole `Promise.all` rejects.
|
|
141
|
+
3. **`Promise.race` resolves when the first promise settles**: The losing promises keep running in the background, but the workflow discards their results.
|
|
142
|
+
4. **All primitives are promises**: `sleep("1 day")` and `createWebhook()` return promises, so they compose with `Promise.all` and `Promise.race` like steps do. This behavior enables patterns such as racing a webhook against a 24-hour deadline.
|
|
143
143
|
|
|
144
144
|
## Adapting to your use case
|
|
145
145
|
|
|
146
|
-
- **Replace `Promise.all` with `Promise.allSettled
|
|
147
|
-
- **Bound the parallelism
|
|
148
|
-
- **Add a deadline to any race
|
|
149
|
-
- **Mix steps and hooks in a race
|
|
146
|
+
- **Replace `Promise.all` with `Promise.allSettled`**: Use this option when partial failures shouldn't abort the remaining operations. You'll get an array of `{ status, value | reason }` instead of an error on the first rejection.
|
|
147
|
+
- **Bound the parallelism**: `Promise.all` over 1,000 items will fan out 1,000 concurrent steps. If your downstream APIs can't handle that, batch the array into chunks (see [Batching](/cookbook/common-patterns/batching)).
|
|
148
|
+
- **Add a deadline to any race**: Pair the operation with `sleep("30s").then(() => "timeout" as const)` and check the discriminated result. See [Timeouts](/cookbook/common-patterns/timeouts).
|
|
149
|
+
- **Mix steps and hooks in a race**: Wait for an external signal, a deadline, or a step result in the same `Promise.race`. The first promise to resolve wins.
|
|
150
150
|
|
|
151
151
|
## Key APIs
|
|
152
152
|
|
|
153
|
-
- [`"use workflow"`](/docs/foundations/workflows-and-steps)
|
|
154
|
-
- [`"use step"`](/docs/foundations/workflows-and-steps)
|
|
155
|
-
- [`sleep()`](/docs/api-reference/workflow/sleep)
|
|
156
|
-
- [`createWebhook()`](/docs/api-reference/workflow/create-webhook)
|
|
157
|
-
- [`Promise.all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)
|
|
158
|
-
- [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)
|
|
159
|
-
- [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled)
|
|
153
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps): Marks the orchestrator function.
|
|
154
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps): Marks functions with full Node.js access.
|
|
155
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep): Provides durable sleep that survives restarts.
|
|
156
|
+
- [`createWebhook()`](/docs/api-reference/workflow/create-webhook): Provides a webhook URL that the workflow can race against.
|
|
157
|
+
- [`Promise.all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all): Waits for all promises.
|
|
158
|
+
- [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race): Waits for the first promise to settle.
|
|
159
|
+
- [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled): Waits for all promises, including failures.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
title: Timeouts
|
|
3
3
|
description: Add deadlines to slow operations by racing them against a durable sleep.
|
|
4
4
|
type: guide
|
|
5
|
-
summary: Use `Promise.race` with `sleep()` to bound the time any step, hook, or webhook is allowed to take
|
|
5
|
+
summary: Use `Promise.race` with `sleep()` to bound the time any step, hook, or webhook is allowed to take, and recover gracefully when the deadline fires first.
|
|
6
6
|
related:
|
|
7
7
|
- /docs/api-reference/workflow/sleep
|
|
8
8
|
- /docs/foundations/hooks
|
|
@@ -11,17 +11,17 @@ related:
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
13
|
<CopyPrompt
|
|
14
|
-
text="Add a deadline to this slow workflow operation. Import `sleep` from `workflow`. In the exported "use workflow" function, race the operation against a durable sleep: `await Promise.race([slowStep(...), deadline("10m")])`, where `deadline` is a helper that awaits `sleep(duration)` and returns a sentinel value or throws. Branch on the winner: continue normally on success, run the fallback or compensation path on timeout. Use the same pattern to bound hooks and webhooks. `Promise.race` does not cancel the loser
|
|
14
|
+
text="Add a deadline to this slow workflow operation. Import `sleep` from `workflow`. In the exported "use workflow" function, race the operation against a durable sleep: `await Promise.race([slowStep(...), deadline("10m")])`, where `deadline` is a helper that awaits `sleep(duration)` and returns a sentinel value or throws. Branch on the winner: continue normally on success, run the fallback or compensation path on timeout. Use the same pattern to bound hooks and webhooks. `Promise.race` does not cancel the loser (the underlying step keeps running), so pass an `AbortSignal` into the step for cooperative cancellation and make non-idempotent side effects retry-safe with idempotency keys. Verify the fast path, the timeout path, and side-effect safety when the loser completes late."
|
|
15
15
|
/>
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Workflows often need to limit how long they wait for a slow step, an external webhook, or human approval. Race the operation against a durable `sleep()` with `Promise.race()`. The first operation to finish wins, while the other keeps running and its result is ignored.
|
|
18
18
|
|
|
19
19
|
## When to use this
|
|
20
20
|
|
|
21
|
-
- **Slow steps
|
|
22
|
-
- **External callbacks
|
|
23
|
-
- **Human approvals
|
|
24
|
-
- **Polling loops
|
|
21
|
+
- **Slow steps**: bound the time spent waiting on third-party APIs, model calls, or expensive computation
|
|
22
|
+
- **External callbacks**: give webhooks a deadline so the workflow doesn't hang forever waiting for an event that may never arrive
|
|
23
|
+
- **Human approvals**: auto-decline or escalate when a hook isn't resumed within a window
|
|
24
|
+
- **Polling loops**: give an outer poll-until-ready loop an overall budget
|
|
25
25
|
|
|
26
26
|
## Pattern
|
|
27
27
|
|
|
@@ -50,7 +50,7 @@ export async function processWithTimeout(data: string) {
|
|
|
50
50
|
|
|
51
51
|
### Timeout on a webhook
|
|
52
52
|
|
|
53
|
-
The same pattern works for any promise
|
|
53
|
+
The same pattern works for any promise, including hooks and webhooks. Here a webhook waits for an external service to call back, with a hard deadline of 7 days:
|
|
54
54
|
|
|
55
55
|
```typescript lineNumbers
|
|
56
56
|
import { sleep, createWebhook } from "workflow";
|
|
@@ -78,27 +78,27 @@ export async function waitForApproval(requestId: string) {
|
|
|
78
78
|
|
|
79
79
|
## How it works
|
|
80
80
|
|
|
81
|
-
1. **Durable sleep
|
|
82
|
-
2. **Race
|
|
83
|
-
3. **Discriminated result
|
|
84
|
-
4. **Throw to fail the workflow
|
|
81
|
+
1. **Durable sleep**: `sleep("30s")` persists through restarts at zero compute cost. The workflow resumes precisely when the timer fires.
|
|
82
|
+
2. **Race**: `Promise.race([work, sleep(...)])` returns the value of whichever promise resolves first. The loser keeps running in the background but its result is ignored by the workflow.
|
|
83
|
+
3. **Discriminated result**: tagging the sleep branch with a sentinel value (`"timeout" as const`, `{ timedOut: true }`) lets TypeScript narrow the result and pick the right branch.
|
|
84
|
+
4. **Throw to fail the workflow**: inside a workflow function, throwing an `Error` exits the run with that error. Use `FatalError` inside steps; throw plain errors inside workflows.
|
|
85
85
|
|
|
86
86
|
<Callout type="warn">
|
|
87
|
-
**The losing operation keeps running.** `Promise.race` doesn't cancel
|
|
87
|
+
**The losing operation keeps running.** `Promise.race` doesn't cancel: when the sleep wins, the underlying step (or model call, or HTTP request) continues to completion in the background. This is fine for idempotent reads but matters when the operation has side effects or costs money. Pass an `AbortSignal` into the step to cancel it cooperatively, and use idempotency keys for non-idempotent side effects. See the [Cancellation Guide](/docs/foundations/cancellation) and [Idempotency](/docs/foundations/idempotency) for patterns.
|
|
88
88
|
</Callout>
|
|
89
89
|
|
|
90
90
|
## Adapting to your use case
|
|
91
91
|
|
|
92
|
-
- **Different durations
|
|
93
|
-
- **Soft timeout (retry)
|
|
94
|
-
- **Soft timeout (fallback)
|
|
95
|
-
- **Combine with cancellation
|
|
96
|
-
- **Per-step deadlines
|
|
92
|
+
- **Different durations**: `sleep()` accepts duration strings (`"30s"`, `"5m"`, `"7 days"`), milliseconds, or `Date` objects for absolute deadlines.
|
|
93
|
+
- **Soft timeout (retry)**: instead of throwing, loop and retry with a fresh `Promise.race` and a backoff.
|
|
94
|
+
- **Soft timeout (fallback)**: return a default value when the timer wins instead of throwing: `if (result === "timeout") return cachedFallback`.
|
|
95
|
+
- **Combine with cancellation**: race three promises: the operation, a deadline `sleep()`, and a cancellation hook. See the [Scheduling cookbook](/cookbook/common-patterns/scheduling) for the cancellation half of this pattern.
|
|
96
|
+
- **Per-step deadlines**: wrap each step in its own `Promise.race` for independent budgets, or use a single outer race for an overall workflow deadline.
|
|
97
97
|
|
|
98
98
|
## Key APIs
|
|
99
99
|
|
|
100
|
-
- [`sleep()`](/docs/api-reference/workflow/sleep)
|
|
101
|
-
- [`createWebhook()`](/docs/api-reference/workflow/create-webhook)
|
|
102
|
-
- [`defineHook()`](/docs/api-reference/workflow/define-hook)
|
|
103
|
-
- [Idempotency](/docs/foundations/idempotency)
|
|
104
|
-
- [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)
|
|
100
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep): durable wait (survives restarts, zero compute cost)
|
|
101
|
+
- [`createWebhook()`](/docs/api-reference/workflow/create-webhook): create a webhook URL the workflow can race against
|
|
102
|
+
- [`defineHook()`](/docs/api-reference/workflow/define-hook): typed hook for in-process cancellation
|
|
103
|
+
- [Idempotency](/docs/foundations/idempotency): protect side effects that may keep running after a timeout
|
|
104
|
+
- [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race): race operations against deadlines
|