workflow 5.0.0-beta.14 → 5.0.0-beta.16
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/docs/api-reference/index.mdx +24 -0
- package/docs/api-reference/meta.json +8 -0
- package/docs/api-reference/vitest/index.mdx +1 -7
- package/docs/api-reference/workflow/create-hook.mdx +38 -0
- package/docs/api-reference/workflow/create-webhook.mdx +1 -0
- package/docs/api-reference/workflow/experimental-set-attributes.mdx +3 -1
- package/docs/api-reference/workflow/fetch.mdx +5 -0
- package/docs/api-reference/workflow-api/get-hook-by-token.mdx +7 -0
- package/docs/api-reference/workflow-api/get-run.mdx +6 -0
- package/docs/api-reference/workflow-api/index.mdx +6 -8
- package/docs/api-reference/workflow-api/resume-hook.mdx +57 -0
- package/docs/api-reference/workflow-api/start.mdx +6 -1
- package/docs/api-reference/workflow-astro/index.mdx +18 -0
- package/docs/api-reference/workflow-astro/meta.json +4 -0
- package/docs/api-reference/workflow-astro/workflow.mdx +45 -0
- package/docs/api-reference/workflow-errors/hook-conflict-error.mdx +60 -0
- package/docs/api-reference/workflow-errors/index.mdx +85 -0
- package/docs/api-reference/workflow-errors/meta.json +5 -0
- package/docs/api-reference/workflow-errors/run-not-supported-error.mdx +58 -0
- package/docs/api-reference/workflow-errors/workflow-error.mdx +52 -0
- package/docs/api-reference/workflow-errors/workflow-run-not-completed-error.mdx +58 -0
- package/docs/api-reference/workflow-errors/workflow-runtime-error.mdx +58 -0
- package/docs/api-reference/workflow-nest/configure-workflow-controller.mdx +33 -0
- package/docs/api-reference/workflow-nest/index.mdx +31 -0
- package/docs/api-reference/workflow-nest/meta.json +9 -0
- package/docs/api-reference/workflow-nest/nest-local-builder.mdx +64 -0
- package/docs/api-reference/workflow-nest/workflow-controller.mdx +40 -0
- package/docs/api-reference/workflow-nest/workflow-module.mdx +74 -0
- package/docs/api-reference/workflow-nitro/index.mdx +59 -0
- package/docs/api-reference/workflow-nuxt/index.mdx +47 -0
- package/docs/api-reference/workflow-observability/hydrate-data.mdx +35 -0
- package/docs/api-reference/workflow-observability/hydrate-resource-io.mdx +62 -0
- package/docs/api-reference/workflow-observability/index.mdx +64 -0
- package/docs/api-reference/workflow-observability/meta.json +11 -0
- package/docs/api-reference/workflow-observability/observability-revivers.mdx +50 -0
- package/docs/api-reference/workflow-observability/parse-class-name.mdx +41 -0
- package/docs/api-reference/workflow-observability/parse-step-name.mdx +40 -0
- package/docs/api-reference/workflow-observability/parse-workflow-name.mdx +55 -0
- package/docs/api-reference/workflow-runtime/create-world.mdx +39 -0
- package/docs/api-reference/workflow-runtime/get-world-handlers.mdx +44 -0
- package/docs/api-reference/{workflow-api → workflow-runtime}/get-world.mdx +7 -10
- package/docs/api-reference/workflow-runtime/health-check.mdx +50 -0
- package/docs/api-reference/workflow-runtime/index.mdx +43 -0
- package/docs/api-reference/workflow-runtime/meta.json +12 -0
- package/docs/api-reference/workflow-runtime/set-world.mdx +49 -0
- package/docs/api-reference/workflow-runtime/workflow-entrypoint.mdx +42 -0
- package/docs/api-reference/{workflow-api → workflow-runtime}/world/index.mdx +5 -8
- package/docs/api-reference/workflow-runtime/world/meta.json +4 -0
- package/docs/api-reference/{workflow-api → workflow-runtime}/world/queue.mdx +2 -2
- package/docs/api-reference/{workflow-api → workflow-runtime}/world/storage.mdx +11 -4
- package/docs/api-reference/{workflow-api → workflow-runtime}/world/streams.mdx +2 -2
- package/docs/api-reference/workflow-serde/index.mdx +0 -1
- package/docs/api-reference/workflow-serde/workflow-deserialize.mdx +1 -2
- package/docs/api-reference/workflow-serde/workflow-serialize.mdx +1 -2
- package/docs/api-reference/workflow-sveltekit/index.mdx +18 -0
- package/docs/api-reference/workflow-sveltekit/meta.json +4 -0
- package/docs/api-reference/workflow-sveltekit/workflow-plugin.mdx +42 -0
- package/docs/api-reference/workflow-vite/index.mdx +18 -0
- package/docs/api-reference/workflow-vite/meta.json +4 -0
- package/docs/api-reference/workflow-vite/workflow.mdx +48 -0
- package/docs/changelog/attributes-mvp.mdx +15 -3
- package/docs/changelog/eager-processing.mdx +26 -6
- package/docs/changelog/index.mdx +1 -1
- package/docs/cookbook/advanced/child-workflows.mdx +3 -1
- package/docs/cookbook/advanced/publishing-libraries.mdx +13 -12
- package/docs/cookbook/advanced/serializable-steps.mdx +3 -3
- package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +2 -2
- package/docs/cookbook/common-patterns/idempotency.mdx +36 -52
- package/docs/cookbook/common-patterns/rate-limiting.mdx +1 -1
- package/docs/cookbook/common-patterns/saga.mdx +2 -2
- package/docs/cookbook/common-patterns/scheduling.mdx +4 -0
- package/docs/cookbook/common-patterns/timeouts.mdx +2 -1
- package/docs/cookbook/common-patterns/workflow-composition.mdx +6 -0
- package/docs/cookbook/index.mdx +1 -1
- package/docs/cookbook/integrations/ai-sdk.mdx +10 -1
- package/docs/cookbook/integrations/chat-sdk.mdx +9 -0
- package/docs/cookbook/integrations/sandbox.mdx +9 -0
- package/docs/errors/step-not-registered.mdx +1 -1
- package/docs/foundations/cancellation.mdx +1 -2
- package/docs/foundations/hooks.mdx +29 -0
- package/docs/foundations/idempotency.mdx +236 -11
- package/docs/foundations/streaming.mdx +1 -1
- package/docs/foundations/versioning.mdx +1 -1
- package/docs/how-it-works/encryption.mdx +2 -2
- package/docs/how-it-works/event-sourcing.mdx +2 -2
- package/docs/migration-guides/migrating-from-temporal.mdx +1 -1
- package/docs/observability/attributes.mdx +26 -5
- package/docs/testing/index.mdx +2 -2
- package/package.json +10 -10
- package/docs/api-reference/workflow-api/world/meta.json +0 -4
- package/docs/api-reference/workflow-api/world/observability.mdx +0 -164
|
@@ -17,9 +17,33 @@ All the functions and primitives that come with Workflow SDK by package.
|
|
|
17
17
|
<Card title="workflow/api" href="/docs/api-reference/workflow-api">
|
|
18
18
|
API reference for runtime functions from the `workflow/api` package.
|
|
19
19
|
</Card>
|
|
20
|
+
<Card title="workflow/runtime" href="/docs/api-reference/workflow-runtime">
|
|
21
|
+
Runtime functions for resolving the World instance and the low-level World SDK.
|
|
22
|
+
</Card>
|
|
23
|
+
<Card title="workflow/observability" href="/docs/api-reference/workflow-observability">
|
|
24
|
+
Utilities to hydrate step I/O, parse display names, and decrypt workflow data.
|
|
25
|
+
</Card>
|
|
20
26
|
<Card title="workflow/next" href="/docs/api-reference/workflow-next">
|
|
21
27
|
Next.js integration for Workflow SDK that automatically configures bundling and runtime support.
|
|
22
28
|
</Card>
|
|
29
|
+
<Card title="workflow/nitro" href="/docs/api-reference/workflow-nitro">
|
|
30
|
+
Nitro module for workflow bundling and runtime support.
|
|
31
|
+
</Card>
|
|
32
|
+
<Card title="workflow/nuxt" href="/docs/api-reference/workflow-nuxt">
|
|
33
|
+
Nuxt module for workflow bundling and runtime support.
|
|
34
|
+
</Card>
|
|
35
|
+
<Card title="workflow/sveltekit" href="/docs/api-reference/workflow-sveltekit">
|
|
36
|
+
SvelteKit Vite plugin for workflow bundling and runtime support.
|
|
37
|
+
</Card>
|
|
38
|
+
<Card title="workflow/astro" href="/docs/api-reference/workflow-astro">
|
|
39
|
+
Astro integration for workflow bundling and runtime support.
|
|
40
|
+
</Card>
|
|
41
|
+
<Card title="workflow/vite" href="/docs/api-reference/workflow-vite">
|
|
42
|
+
Standalone Vite plugin for workflow bundling and runtime support.
|
|
43
|
+
</Card>
|
|
44
|
+
<Card title="workflow/nest" href="/docs/api-reference/workflow-nest">
|
|
45
|
+
NestJS module for workflow bundling and runtime support.
|
|
46
|
+
</Card>
|
|
23
47
|
<Card title="workflow/errors" href="/docs/api-reference/workflow-errors">
|
|
24
48
|
Semantic error types for handling workflow storage backend failures.
|
|
25
49
|
</Card>
|
|
@@ -4,7 +4,15 @@
|
|
|
4
4
|
"workflow-globals",
|
|
5
5
|
"workflow",
|
|
6
6
|
"workflow-api",
|
|
7
|
+
"workflow-runtime",
|
|
8
|
+
"workflow-observability",
|
|
7
9
|
"workflow-next",
|
|
10
|
+
"workflow-nitro",
|
|
11
|
+
"workflow-nuxt",
|
|
12
|
+
"workflow-sveltekit",
|
|
13
|
+
"workflow-astro",
|
|
14
|
+
"workflow-vite",
|
|
15
|
+
"workflow-nest",
|
|
8
16
|
"workflow-errors",
|
|
9
17
|
"workflow-serde",
|
|
10
18
|
"workflow-ai",
|
|
@@ -11,7 +11,6 @@ The `@workflow/vitest` package provides a Vitest plugin and test helpers for run
|
|
|
11
11
|
|
|
12
12
|
Returns a Vite plugin array that handles SWC transforms, bundle building, and in-process handler registration automatically.
|
|
13
13
|
|
|
14
|
-
{/* @skip-typecheck - @workflow/vitest not available in docs-typecheck */}
|
|
15
14
|
|
|
16
15
|
```typescript
|
|
17
16
|
import { defineConfig } from "vitest/config";
|
|
@@ -24,7 +23,6 @@ export default defineConfig({
|
|
|
24
23
|
|
|
25
24
|
Pass a [`WorkflowTestOptions`](#workflowtestoptions) object when your project uses a non-standard layout — for example, a monorepo where `workflows/` does not live at the Vitest config's directory, or when the default `.workflow-data` / `.workflow-vitest` output locations need to move. The plugin forwards these paths to `buildWorkflowTests()` and `setupWorkflowTests()` through Vitest's per-project provided context, so each Vitest workspace project stays isolated.
|
|
26
25
|
|
|
27
|
-
{/* @skip-typecheck - @workflow/vitest not available in docs-typecheck */}
|
|
28
26
|
|
|
29
27
|
```typescript
|
|
30
28
|
import { defineConfig } from "vitest/config";
|
|
@@ -54,7 +52,6 @@ export default defineConfig({
|
|
|
54
52
|
|
|
55
53
|
Builds workflow and step bundles to disk. Called automatically by the `workflow()` plugin in `globalSetup`. Use directly only for [manual setup](/docs/testing#manual-setup).
|
|
56
54
|
|
|
57
|
-
{/* @skip-typecheck - @workflow/vitest not available in docs-typecheck */}
|
|
58
55
|
|
|
59
56
|
```typescript
|
|
60
57
|
import { buildWorkflowTests } from "@workflow/vitest";
|
|
@@ -72,11 +69,10 @@ export async function setup() {
|
|
|
72
69
|
|
|
73
70
|
### `setupWorkflowTests()`
|
|
74
71
|
|
|
75
|
-
Sets up an in-process workflow runtime in each test worker. Imports pre-built bundles, creates a [Local World](/
|
|
72
|
+
Sets up an in-process workflow runtime in each test worker. Imports pre-built bundles, creates a [Local World](/worlds/local) instance with direct handlers, and sets it as the global world. Clears all workflow data on each invocation for full test isolation.
|
|
76
73
|
|
|
77
74
|
Called automatically by the `workflow()` plugin in `setupFiles`. Use directly only for [manual setup](/docs/testing#manual-setup).
|
|
78
75
|
|
|
79
|
-
{/* @skip-typecheck - @workflow/vitest not available in docs-typecheck */}
|
|
80
76
|
|
|
81
77
|
```typescript
|
|
82
78
|
import { beforeAll, afterAll } from "vitest";
|
|
@@ -118,7 +114,6 @@ Tears down the workflow test world. Clears the global world and closes the Local
|
|
|
118
114
|
|
|
119
115
|
Polls the event log until the workflow has a pending `sleep()` call — one with a `wait_created` event but no corresponding `wait_completed` event. Returns the correlation ID of the pending sleep, which can be passed to [`wakeUp()`](/docs/api-reference/workflow-api/get-run) to target a specific sleep.
|
|
120
116
|
|
|
121
|
-
{/* @skip-typecheck - @workflow/vitest not available in docs-typecheck */}
|
|
122
117
|
|
|
123
118
|
```typescript
|
|
124
119
|
import { waitForSleep } from "@workflow/vitest"; // [!code highlight]
|
|
@@ -147,7 +142,6 @@ await getRun(run.runId).wakeUp({ correlationIds: [sleepId] }); // [!code highlig
|
|
|
147
142
|
|
|
148
143
|
Polls the hook list and event log until a hook matching the optional `token` filter exists that hasn't been received yet. Returns the matching hook object.
|
|
149
144
|
|
|
150
|
-
{/* @skip-typecheck - @workflow/vitest not available in docs-typecheck */}
|
|
151
145
|
|
|
152
146
|
```typescript
|
|
153
147
|
import { waitForHook } from "@workflow/vitest"; // [!code highlight]
|
|
@@ -8,6 +8,7 @@ prerequisites:
|
|
|
8
8
|
related:
|
|
9
9
|
- /docs/api-reference/workflow/define-hook
|
|
10
10
|
- /docs/api-reference/workflow/create-webhook
|
|
11
|
+
- /docs/foundations/idempotency
|
|
11
12
|
---
|
|
12
13
|
|
|
13
14
|
Creates a low-level hook primitive that can be used to resume a workflow run with arbitrary payloads.
|
|
@@ -65,6 +66,8 @@ export default Hook;`}
|
|
|
65
66
|
|
|
66
67
|
The returned `Hook` object also implements `AsyncIterable<T>`, which allows you to iterate over incoming payloads using `for await...of` syntax.
|
|
67
68
|
|
|
69
|
+
Use `hook.getConflict()` to check whether the hook token is already claimed by another active hook, without waiting for hook payload data. Calling `createHook()` on its own does not register the hook — registration is only committed when the workflow suspends. Awaiting `hook.getConflict()` suspends the workflow to commit the registration, then resolves with `null` once `hook_created` is recorded, or with the conflicting [`Run`](/docs/api-reference/workflow-api/get-run) if another active hook already owns the same token.
|
|
70
|
+
|
|
68
71
|
## Examples
|
|
69
72
|
|
|
70
73
|
### Basic Usage
|
|
@@ -112,6 +115,40 @@ export async function slackBotWorkflow(channelId: string) {
|
|
|
112
115
|
}
|
|
113
116
|
```
|
|
114
117
|
|
|
118
|
+
### Detecting Token Conflicts
|
|
119
|
+
|
|
120
|
+
Use `hook.getConflict()` when the workflow needs to claim a hook token before doing other work, but does not need a payload yet:
|
|
121
|
+
|
|
122
|
+
```typescript lineNumbers
|
|
123
|
+
import { createHook } from "workflow";
|
|
124
|
+
|
|
125
|
+
declare function chargeOrder(orderId: string): Promise<void>; // @setup
|
|
126
|
+
|
|
127
|
+
async function processOrder(orderId: string) {
|
|
128
|
+
"use workflow";
|
|
129
|
+
|
|
130
|
+
using hook = createHook({ // [!code highlight]
|
|
131
|
+
token: `order:${orderId}` // [!code highlight]
|
|
132
|
+
}); // [!code highlight]
|
|
133
|
+
|
|
134
|
+
const conflict = await hook.getConflict(); // [!code highlight]
|
|
135
|
+
if (conflict) { // [!code highlight]
|
|
136
|
+
// Another active workflow run already owns this token.
|
|
137
|
+
return { dedupedTo: conflict.runId };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
await chargeOrder(orderId);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Because `createHook()` alone does not suspend the workflow, awaiting `hook.getConflict()` is what actually suspends the run and commits the hook registration. It only waits for registration — to receive payload data from a future `resumeHook()` call, await the hook itself or iterate it with `for await...of`.
|
|
145
|
+
|
|
146
|
+
On a conflict, the resolved value is a `Run` handle for the run that currently owns the token, with durable step-backed accessors. The duplicate run can decide in code how to handle it: return or log `conflict.runId`, inspect `await conflict.status`, wait on `await conflict.returnValue`, or cancel the owner with `await conflict.cancel()` and continue in the current run. See [Run idempotency](/docs/foundations/idempotency#run-idempotency) for these strategies in context.
|
|
147
|
+
|
|
148
|
+
<Callout type="info">
|
|
149
|
+
Custom hook tokens are the recommended way to coordinate active workflow runs. Use a deterministic token from your domain, such as an order ID or conversation ID, create the hook near the beginning of the workflow, and check `await hook.getConflict()` before work that depends on owning the token. See [Run idempotency](/docs/foundations/idempotency#run-idempotency).
|
|
150
|
+
</Callout>
|
|
151
|
+
|
|
115
152
|
### Waiting for Multiple Payloads
|
|
116
153
|
|
|
117
154
|
You can also wait for multiple payloads by using the `for await...of` syntax.
|
|
@@ -195,3 +232,4 @@ This is equivalent to manually calling `dispose()` but ensures the hook is alway
|
|
|
195
232
|
- [`defineHook()`](/docs/api-reference/workflow/define-hook) - Type-safe hook helper
|
|
196
233
|
- [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook) - Resume a hook with a payload
|
|
197
234
|
- [`createWebhook()`](/docs/api-reference/workflow/create-webhook) - Higher-level HTTP webhook abstraction
|
|
235
|
+
- [Idempotency](/docs/foundations/idempotency) - Deduplicate step side effects and workflow starts
|
|
@@ -55,6 +55,7 @@ The returned `Webhook` object has:
|
|
|
55
55
|
|
|
56
56
|
- `url`: The HTTP endpoint URL that external systems can call
|
|
57
57
|
- `token`: The unique token identifying this webhook
|
|
58
|
+
- `getConflict()`: A promise that resolves with the conflicting run if another active hook already owns this token, or `null` once the webhook endpoint has been registered
|
|
58
59
|
- Implements `AsyncIterable<T>` for handling multiple requests, where `T` is `Request` (default) or `RequestWithResponse` (manual mode)
|
|
59
60
|
|
|
60
61
|
When using `createWebhook({ respondWith: 'manual' })`, the resolved request type is `RequestWithResponse`, which extends the standard `Request` interface with a `respondWith(response: Response): Promise<void>` method for sending custom responses back to the caller.
|
|
@@ -60,4 +60,6 @@ Attribute keys must be 1-256 characters, values must be strings up to 256 bytes,
|
|
|
60
60
|
|
|
61
61
|
Validation errors throw [`FatalError`](/docs/api-reference/workflow/fatal-error) and fail the run before an attribute write is attempted.
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
Calls from both workflow and step bodies append a native `attr_set` event, which the World materializes onto `run.attributes`. Workflow-originated events record a workflow writer; step-originated events record the originating step ID and attempt.
|
|
64
|
+
|
|
65
|
+
Native attributes require spec version 4 or later. Step-body storage errors throw from `experimental_setAttributes`; catch them inside the step if the write should be best-effort. Workflow-body writes are committed when the workflow suspends: transient storage errors are retried with the suspension, while a write the World rejects as invalid — such as exceeding the per-run attribute cap across multiple calls — fails the run with the validation error.
|
|
@@ -7,12 +7,17 @@ prerequisites:
|
|
|
7
7
|
- /docs/foundations/workflows-and-steps
|
|
8
8
|
related:
|
|
9
9
|
- /docs/errors/fetch-in-workflow
|
|
10
|
+
- /docs/foundations/idempotency
|
|
10
11
|
---
|
|
11
12
|
|
|
12
13
|
Makes HTTP requests from within a workflow. This is a special step function that wraps the standard `fetch` API, automatically handling serialization and providing retry semantics.
|
|
13
14
|
|
|
14
15
|
This is useful when you need to call external APIs or services from within your workflow.
|
|
15
16
|
|
|
17
|
+
<Callout type="warn">
|
|
18
|
+
Because workflow `fetch()` has retry semantics, use idempotency keys when the request mutates an external system, such as creating a charge, sending an email, or enqueueing work. See [Idempotency](/docs/foundations/idempotency).
|
|
19
|
+
</Callout>
|
|
20
|
+
|
|
16
21
|
<Callout>
|
|
17
22
|
`fetch` is a *special* type of step function provided and should be called directly inside workflow functions.
|
|
18
23
|
</Callout>
|
|
@@ -5,6 +5,8 @@ type: reference
|
|
|
5
5
|
summary: Use getHookByToken to look up a hook's metadata and associated workflow run before resuming it.
|
|
6
6
|
prerequisites:
|
|
7
7
|
- /docs/foundations/hooks
|
|
8
|
+
related:
|
|
9
|
+
- /docs/foundations/idempotency
|
|
8
10
|
---
|
|
9
11
|
|
|
10
12
|
Retrieves a hook by its unique token, returning the associated workflow run information and any metadata that was set when the hook was created. This function is useful for inspecting hook details before deciding whether to resume a workflow.
|
|
@@ -13,6 +15,10 @@ Retrieves a hook by its unique token, returning the associated workflow run info
|
|
|
13
15
|
`getHookByToken` is a runtime function that must be called from outside a workflow function.
|
|
14
16
|
</Callout>
|
|
15
17
|
|
|
18
|
+
<Callout type="info">
|
|
19
|
+
Looking up a deterministic hook token is useful in hook-based idempotency flows, but it is only an advisory check. If no hook exists yet, another request can still start the same workflow before your `start()` call registers its hook. Use the lookup to avoid obvious duplicate starts, and handle the race inside the workflow by checking `await hook.getConflict()` before duplicate-sensitive work — on a conflict it resolves with the run that owns the token, so the duplicate can route the caller to the active owner. If duplicates must be rejected before a workflow body runs, keep a durable request record until native atomic start-and-hook registration exists. See [Run idempotency](/docs/foundations/idempotency#run-idempotency).
|
|
20
|
+
</Callout>
|
|
21
|
+
|
|
16
22
|
```typescript lineNumbers
|
|
17
23
|
import { getHookByToken } from "workflow/api";
|
|
18
24
|
|
|
@@ -178,3 +184,4 @@ export async function POST(request: Request) {
|
|
|
178
184
|
- [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook) - Resume a hook with a payload.
|
|
179
185
|
- [`createHook()`](/docs/api-reference/workflow/create-hook) - Create a hook in a workflow.
|
|
180
186
|
- [`defineHook()`](/docs/api-reference/workflow/define-hook) - Type-safe hook helper.
|
|
187
|
+
- [Idempotency](/docs/foundations/idempotency) - Deduplicate step side effects and workflow starts.
|
|
@@ -5,12 +5,18 @@ type: reference
|
|
|
5
5
|
summary: Use getRun to check a workflow run's status and metadata without blocking on completion.
|
|
6
6
|
prerequisites:
|
|
7
7
|
- /docs/foundations/starting-workflows
|
|
8
|
+
related:
|
|
9
|
+
- /docs/foundations/idempotency
|
|
8
10
|
---
|
|
9
11
|
|
|
10
12
|
Retrieves the workflow run metadata and status information for a given run ID. This function provides immediate access to workflow run details without waiting for completion, making it ideal for status checking and monitoring.
|
|
11
13
|
|
|
12
14
|
Use this function when you need to check workflow status, get timing information, or access workflow metadata without blocking on workflow completion.
|
|
13
15
|
|
|
16
|
+
<Callout type="info">
|
|
17
|
+
`getRun()` retrieves a run when you already have its `runId`. It does not look up runs by a business key. For retried requests that should route to one active workflow, use a deterministic hook token and [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token). After a hook conflict, `HookConflictError.conflictingRunId` can be passed to `getRun()` to inspect, stream, or return the active owner. See [Run idempotency](/docs/foundations/idempotency#run-idempotency).
|
|
18
|
+
</Callout>
|
|
19
|
+
|
|
14
20
|
```typescript lineNumbers
|
|
15
21
|
import { getRun } from "workflow/api";
|
|
16
22
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "workflow/api"
|
|
3
|
-
description: Runtime functions to inspect runs, start workflows, and
|
|
3
|
+
description: Runtime functions to inspect runs, start workflows, and manage hooks.
|
|
4
4
|
type: overview
|
|
5
5
|
summary: Explore runtime functions for starting workflows, inspecting runs, and managing hooks.
|
|
6
6
|
---
|
|
@@ -9,7 +9,7 @@ API reference for runtime functions from the `workflow/api` package.
|
|
|
9
9
|
|
|
10
10
|
## Functions
|
|
11
11
|
|
|
12
|
-
The API package is for access and introspection of workflow data to inspect runs, start new runs,
|
|
12
|
+
The API package is for access and introspection of workflow data to inspect runs, start new runs, and manage hooks.
|
|
13
13
|
|
|
14
14
|
<Cards>
|
|
15
15
|
<Card href="/docs/api-reference/workflow-api/start" title="start()">
|
|
@@ -27,10 +27,8 @@ The API package is for access and introspection of workflow data to inspect runs
|
|
|
27
27
|
<Card href="/docs/api-reference/workflow-api/get-run" title="getRun()">
|
|
28
28
|
Get workflow run status and metadata without waiting for completion.
|
|
29
29
|
</Card>
|
|
30
|
-
<Card href="/docs/api-reference/workflow-api/get-world" title="getWorld()">
|
|
31
|
-
Async: resolve the World instance for storage, queuing, and streaming backends.
|
|
32
|
-
</Card>
|
|
33
|
-
<Card href="/docs/api-reference/workflow-api/world" title="World SDK">
|
|
34
|
-
Low-level API for inspecting runs, steps, events, hooks, streams, and queues.
|
|
35
|
-
</Card>
|
|
36
30
|
</Cards>
|
|
31
|
+
|
|
32
|
+
<Callout type="info">
|
|
33
|
+
Looking for `getWorld()` and the World SDK? They are exported from `workflow/runtime` — see the [`workflow/runtime` reference](/docs/api-reference/workflow-runtime).
|
|
34
|
+
</Callout>
|
|
@@ -7,6 +7,7 @@ prerequisites:
|
|
|
7
7
|
- /docs/foundations/hooks
|
|
8
8
|
related:
|
|
9
9
|
- /docs/api-reference/workflow-api/resume-webhook
|
|
10
|
+
- /docs/foundations/idempotency
|
|
10
11
|
---
|
|
11
12
|
|
|
12
13
|
Resumes a workflow run by sending a payload to a hook identified by its token.
|
|
@@ -155,8 +156,64 @@ export async function POST(request: Request) {
|
|
|
155
156
|
}
|
|
156
157
|
```
|
|
157
158
|
|
|
159
|
+
### Resume or Start
|
|
160
|
+
|
|
161
|
+
A common endpoint shape is "resume or start": one route that resumes the active workflow run for a business key if one exists, or starts a new run otherwise. This comes up when the workflow uses a deterministic hook token as its idempotency key — for example, one active run per order or conversation.
|
|
162
|
+
|
|
163
|
+
`resumeHook()` is the resume half of that flow. Try it first; if it throws `HookNotFoundError`, no active run owns the token yet, so start the workflow. One subtlety: `start()` returns before the new run executes and registers its hook, so you cannot resume immediately after starting. Retry the resume until the hook is registered — if you drop the payload and only start the workflow, the data from this request is lost.
|
|
164
|
+
|
|
165
|
+
```typescript lineNumbers
|
|
166
|
+
import { resumeHook, start } from "workflow/api";
|
|
167
|
+
import { HookNotFoundError } from "workflow/errors";
|
|
168
|
+
import { processOrder } from "./workflows/process-order";
|
|
169
|
+
|
|
170
|
+
type OrderRequest = { confirmed: boolean };
|
|
171
|
+
|
|
172
|
+
async function resumeWithRetry(token: string, payload: OrderRequest) {
|
|
173
|
+
for (let attempt = 0; attempt < 5; attempt++) {
|
|
174
|
+
try {
|
|
175
|
+
return await resumeHook(token, payload); // [!code highlight]
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (!HookNotFoundError.is(error)) throw error;
|
|
178
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
throw new Error("Workflow did not register its hook in time");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function POST(request: Request) {
|
|
186
|
+
const { orderId, confirmed } = await request.json();
|
|
187
|
+
const token = `order:${orderId}`;
|
|
188
|
+
const payload = { confirmed };
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
// An active run already owns this token: resume it.
|
|
192
|
+
const hook = await resumeHook(token, payload); // [!code highlight]
|
|
193
|
+
return Response.json({ runId: hook.runId, reused: true });
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (!HookNotFoundError.is(error)) throw error;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// No hook yet: start a new run, then retry the resume so this
|
|
199
|
+
// request's payload still reaches the workflow.
|
|
200
|
+
const run = await start(processOrder, [orderId]); // [!code highlight]
|
|
201
|
+
const resumed = await resumeWithRetry(token, payload);
|
|
202
|
+
|
|
203
|
+
// A concurrent request can win the race between `start()` and hook
|
|
204
|
+
// registration; the resume always reaches the actual active owner.
|
|
205
|
+
return Response.json({
|
|
206
|
+
runId: resumed.runId,
|
|
207
|
+
reused: resumed.runId !== run.runId,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
See [Run idempotency](/docs/foundations/idempotency#run-idempotency) for the full pattern, including how the workflow claims the token with `hook.getConflict()` and how concurrent starts converge on one active owner.
|
|
213
|
+
|
|
158
214
|
## Related Functions
|
|
159
215
|
|
|
160
216
|
- [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token) - Get hook details before resuming.
|
|
161
217
|
- [`createHook()`](/docs/api-reference/workflow/create-hook) - Create a hook in a workflow.
|
|
162
218
|
- [`defineHook()`](/docs/api-reference/workflow/define-hook) - Type-safe hook helper.
|
|
219
|
+
- [Idempotency](/docs/foundations/idempotency) - Deduplicate step side effects and workflow starts.
|
|
@@ -5,6 +5,8 @@ type: reference
|
|
|
5
5
|
summary: Use start to programmatically enqueue a new workflow run.
|
|
6
6
|
prerequisites:
|
|
7
7
|
- /docs/foundations/starting-workflows
|
|
8
|
+
related:
|
|
9
|
+
- /docs/foundations/idempotency
|
|
8
10
|
---
|
|
9
11
|
|
|
10
12
|
Start/enqueue a new workflow run.
|
|
@@ -54,8 +56,10 @@ Learn more about [`WorkflowReadableStreamOptions`](/docs/api-reference/workflow-
|
|
|
54
56
|
* In v5, `start()` can also be called directly from a workflow function to spawn a child run or continue work in a new run. See [Workflow Composition](/cookbook/common-patterns/workflow-composition) and [Versioning](/docs/foundations/versioning).
|
|
55
57
|
* This is different from calling workflow functions directly, which is the typical pattern in Next.js applications.
|
|
56
58
|
* The function returns immediately after enqueuing the workflow - it doesn't wait for the workflow to complete.
|
|
59
|
+
* Each call to `start()` creates a new workflow run. If retried requests must route to one active workflow, have the workflow create a deterministic hook token and use [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token) to reuse an already-registered active hook. The lookup is not atomic with `start()`, so concurrent callers can still create extra runs before the hook is registered; handle that race inside the workflow by checking `await hook.getConflict()` before duplicate-sensitive work — on a conflict it resolves with the run that owns the token, so the duplicate can return the active owner to the caller. If duplicates must be rejected before a workflow body runs, keep a durable request record until native atomic start-and-hook registration exists. See [Idempotency](/docs/foundations/idempotency#run-idempotency).
|
|
57
60
|
* All arguments must be [serializable](/docs/foundations/serialization).
|
|
58
61
|
* When `deploymentId` is provided, the argument types and return type become `unknown` since there is no guarantee the workflow function's types will be consistent across different deployments.
|
|
62
|
+
* `attributes` seeds plaintext run metadata as part of creation and requires a World implementing spec version 4 or later. Keys that start with `$` are reserved for framework and library code; framework-level callers can pass `allowReservedAttributes: true` to seed reserved keys, with the same semantics as the [`experimental_setAttributes`](/docs/api-reference/workflow/experimental-set-attributes) option of the same name.
|
|
59
63
|
|
|
60
64
|
<Callout type="info">
|
|
61
65
|
If `start()` throws `'start' received an invalid workflow function. Ensure the Workflow Development Kit is configured correctly and the function includes a 'use workflow' directive.`, the passed function was not transformed as a workflow. The two most common causes are a missing `"use workflow"` directive or missing framework integration. See [start-invalid-workflow-function](/docs/errors/start-invalid-workflow-function).
|
|
@@ -79,7 +83,8 @@ import { start } from "workflow/api";
|
|
|
79
83
|
import { myWorkflow } from "./workflows/my-workflow";
|
|
80
84
|
|
|
81
85
|
const run = await start(myWorkflow, ["arg1", "arg2"], { // [!code highlight]
|
|
82
|
-
deploymentId: "custom-deployment-id" // [!code highlight]
|
|
86
|
+
deploymentId: "custom-deployment-id", // [!code highlight]
|
|
87
|
+
attributes: { source: "checkout" } // [!code highlight]
|
|
83
88
|
}); // [!code highlight]
|
|
84
89
|
```
|
|
85
90
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "workflow/astro"
|
|
3
|
+
description: Astro integration for automatic workflow bundling and route registration.
|
|
4
|
+
type: overview
|
|
5
|
+
summary: Explore the Astro integration for automatic workflow bundling and runtime support.
|
|
6
|
+
related:
|
|
7
|
+
- /docs/getting-started/astro
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
Astro integration for Workflow SDK that transforms workflow code and builds the workflow bundles.
|
|
11
|
+
|
|
12
|
+
## Functions
|
|
13
|
+
|
|
14
|
+
<Cards>
|
|
15
|
+
<Card title="workflow()" href="/docs/api-reference/workflow-astro/workflow">
|
|
16
|
+
Astro integration that transforms workflow code (`"use step"`/`"use workflow"` directives)
|
|
17
|
+
</Card>
|
|
18
|
+
</Cards>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: workflow
|
|
3
|
+
description: Configure Astro to transform workflow directives.
|
|
4
|
+
type: reference
|
|
5
|
+
summary: Add the workflow integration to your Astro config to enable workflow directive transformation.
|
|
6
|
+
prerequisites:
|
|
7
|
+
- /docs/getting-started/astro
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
Returns an Astro integration that transforms workflow code (`"use step"`/`"use workflow"` directives) and builds the workflow bundles.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
To enable `"use step"` and `"use workflow"` directives while developing locally or deploying to production, add `workflow()` to the `integrations` array of your Astro config.
|
|
15
|
+
|
|
16
|
+
```typescript title="astro.config.mjs" lineNumbers
|
|
17
|
+
// @ts-check
|
|
18
|
+
import { defineConfig } from "astro/config";
|
|
19
|
+
import { workflow } from "workflow/astro"; // [!code highlight]
|
|
20
|
+
|
|
21
|
+
// https://astro.build/config
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
integrations: [workflow()], // [!code highlight]
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The integration registers the workflow Vite transform plugins during `astro:config:setup` and builds the workflow bundles — locally during config setup, or via the Vercel builder after `astro:build:done` when deploying to Vercel.
|
|
28
|
+
|
|
29
|
+
## API Signature
|
|
30
|
+
|
|
31
|
+
### Parameters
|
|
32
|
+
|
|
33
|
+
| Parameter | Type | Description |
|
|
34
|
+
| --- | --- | --- |
|
|
35
|
+
| `options` | `WorkflowPluginOptions` | Optional. Configures the workflow build. |
|
|
36
|
+
|
|
37
|
+
#### WorkflowPluginOptions
|
|
38
|
+
|
|
39
|
+
| Option | Type | Default | Description |
|
|
40
|
+
| --- | --- | --- | --- |
|
|
41
|
+
| `sourcemap` | `boolean \| 'inline' \| 'linked' \| 'external' \| 'both'` | `'inline'` | Controls source maps on generated workflow bundles. Accepts the same values as esbuild's `sourcemap` option. Set to `false` for smaller function bundles (useful for staying under the Vercel 250MB function size limit) at the cost of stack traces pointing at generated code. Can also be set via the `WORKFLOW_SOURCEMAP` environment variable. |
|
|
42
|
+
|
|
43
|
+
### Returns
|
|
44
|
+
|
|
45
|
+
Returns an `AstroIntegration` object to include in the `integrations` array of your Astro config.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: HookConflictError
|
|
3
|
+
description: Thrown when creating a hook with a token that is already in use by another workflow run.
|
|
4
|
+
type: reference
|
|
5
|
+
summary: Catch HookConflictError when a hook token is already claimed by another active workflow run.
|
|
6
|
+
related:
|
|
7
|
+
- /docs/api-reference/workflow/create-hook
|
|
8
|
+
- /docs/foundations/hooks
|
|
9
|
+
- /docs/errors/hook-conflict
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
`HookConflictError` is thrown when creating a hook with a token that is already in use by another active workflow run. Hook tokens must be unique across all running workflows — see the [hook-conflict](/docs/errors/hook-conflict) error guide for resolution strategies.
|
|
13
|
+
|
|
14
|
+
```typescript lineNumbers
|
|
15
|
+
import { HookConflictError } from "workflow/errors"
|
|
16
|
+
declare function startApprovalWorkflow(token: string): Promise<void>; // @setup
|
|
17
|
+
declare const token: string; // @setup
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
await startApprovalWorkflow(token);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (HookConflictError.is(error)) { // [!code highlight]
|
|
23
|
+
console.error(
|
|
24
|
+
`Token "${error.token}" already in use by run ${error.conflictingRunId}`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API Signature
|
|
31
|
+
|
|
32
|
+
### Properties
|
|
33
|
+
|
|
34
|
+
<TSDoc
|
|
35
|
+
definition={`
|
|
36
|
+
interface HookConflictError {
|
|
37
|
+
/** The hook token that conflicted. */
|
|
38
|
+
token: string;
|
|
39
|
+
/** The run ID of the workflow currently holding the token, when known. */
|
|
40
|
+
conflictingRunId?: string;
|
|
41
|
+
/** The error message. */
|
|
42
|
+
message: string;
|
|
43
|
+
}
|
|
44
|
+
export default HookConflictError;`}
|
|
45
|
+
/>
|
|
46
|
+
|
|
47
|
+
### Static Methods
|
|
48
|
+
|
|
49
|
+
#### `HookConflictError.is(value)`
|
|
50
|
+
|
|
51
|
+
Type-safe check for `HookConflictError` instances. Preferred over `instanceof` because it works across module boundaries and VM contexts.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { HookConflictError } from "workflow/errors"
|
|
55
|
+
declare const error: unknown; // @setup
|
|
56
|
+
|
|
57
|
+
if (HookConflictError.is(error)) {
|
|
58
|
+
// error is typed as HookConflictError
|
|
59
|
+
}
|
|
60
|
+
```
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "workflow/errors"
|
|
3
|
+
description: Semantic error types thrown by the Workflow SDK and its storage backends.
|
|
4
|
+
type: overview
|
|
5
|
+
summary: Explore the error classes exported from workflow/errors for handling workflow failures.
|
|
6
|
+
related:
|
|
7
|
+
- /docs/foundations/errors-and-retries
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
API reference for the error classes exported from the `workflow/errors` package.
|
|
11
|
+
|
|
12
|
+
All errors extend [`WorkflowError`](/docs/api-reference/workflow-errors/workflow-error), so you can catch any SDK error with a single `instanceof` check, or narrow to a specific class for fine-grained handling.
|
|
13
|
+
|
|
14
|
+
## Base Classes
|
|
15
|
+
|
|
16
|
+
<Cards>
|
|
17
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-error" title="WorkflowError">
|
|
18
|
+
Base class for all workflow error types.
|
|
19
|
+
</Card>
|
|
20
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-world-error" title="WorkflowWorldError">
|
|
21
|
+
Base error for failures from workflow storage backends.
|
|
22
|
+
</Card>
|
|
23
|
+
</Cards>
|
|
24
|
+
|
|
25
|
+
## Registration Errors
|
|
26
|
+
|
|
27
|
+
<Cards>
|
|
28
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-not-registered-error" title="WorkflowNotRegisteredError">
|
|
29
|
+
Thrown when a workflow function is not registered in the current deployment.
|
|
30
|
+
</Card>
|
|
31
|
+
<Card href="/docs/api-reference/workflow-errors/step-not-registered-error" title="StepNotRegisteredError">
|
|
32
|
+
Thrown when a step function is not registered in the current deployment.
|
|
33
|
+
</Card>
|
|
34
|
+
</Cards>
|
|
35
|
+
|
|
36
|
+
## Run Errors
|
|
37
|
+
|
|
38
|
+
<Cards>
|
|
39
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-run-not-found-error" title="WorkflowRunNotFoundError">
|
|
40
|
+
Thrown when operating on a workflow run that does not exist.
|
|
41
|
+
</Card>
|
|
42
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-run-failed-error" title="WorkflowRunFailedError">
|
|
43
|
+
Thrown when awaiting the return value of a failed workflow run.
|
|
44
|
+
</Card>
|
|
45
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-run-cancelled-error" title="WorkflowRunCancelledError">
|
|
46
|
+
Thrown when awaiting the return value of a cancelled workflow run.
|
|
47
|
+
</Card>
|
|
48
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-run-not-completed-error" title="WorkflowRunNotCompletedError">
|
|
49
|
+
Thrown when requesting the result of a workflow run that has not completed yet.
|
|
50
|
+
</Card>
|
|
51
|
+
<Card href="/docs/api-reference/workflow-errors/workflow-runtime-error" title="WorkflowRuntimeError">
|
|
52
|
+
Thrown when the workflow runtime encounters an execution error, such as serialization failures or timeouts.
|
|
53
|
+
</Card>
|
|
54
|
+
<Card href="/docs/api-reference/workflow-errors/run-expired-error" title="RunExpiredError">
|
|
55
|
+
Thrown when a workflow run has expired and can no longer be operated on.
|
|
56
|
+
</Card>
|
|
57
|
+
<Card href="/docs/api-reference/workflow-errors/run-not-supported-error" title="RunNotSupportedError">
|
|
58
|
+
Thrown when a workflow run requires a newer workflow spec version than the installed SDK supports.
|
|
59
|
+
</Card>
|
|
60
|
+
</Cards>
|
|
61
|
+
|
|
62
|
+
## Hook Errors
|
|
63
|
+
|
|
64
|
+
<Cards>
|
|
65
|
+
<Card href="/docs/api-reference/workflow-errors/hook-not-found-error" title="HookNotFoundError">
|
|
66
|
+
Thrown when resuming a hook that does not exist.
|
|
67
|
+
</Card>
|
|
68
|
+
<Card href="/docs/api-reference/workflow-errors/hook-conflict-error" title="HookConflictError">
|
|
69
|
+
Thrown when creating a hook with a token that is already in use by another workflow run.
|
|
70
|
+
</Card>
|
|
71
|
+
</Cards>
|
|
72
|
+
|
|
73
|
+
## Backend Errors
|
|
74
|
+
|
|
75
|
+
<Cards>
|
|
76
|
+
<Card href="/docs/api-reference/workflow-errors/throttle-error" title="ThrottleError">
|
|
77
|
+
Thrown when a request is rate-limited by the workflow backend.
|
|
78
|
+
</Card>
|
|
79
|
+
<Card href="/docs/api-reference/workflow-errors/entity-conflict-error" title="EntityConflictError">
|
|
80
|
+
Thrown when a storage operation conflicts with the current entity state.
|
|
81
|
+
</Card>
|
|
82
|
+
<Card href="/docs/api-reference/workflow-errors/too-early-error" title="TooEarlyError">
|
|
83
|
+
Thrown when a request is made before the system is ready to process it.
|
|
84
|
+
</Card>
|
|
85
|
+
</Cards>
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"title": "workflow/errors",
|
|
3
3
|
"pages": [
|
|
4
|
+
"workflow-error",
|
|
4
5
|
"hook-not-found-error",
|
|
6
|
+
"hook-conflict-error",
|
|
5
7
|
"step-not-registered-error",
|
|
6
8
|
"workflow-not-registered-error",
|
|
7
9
|
"workflow-run-not-found-error",
|
|
8
10
|
"workflow-run-failed-error",
|
|
9
11
|
"workflow-run-cancelled-error",
|
|
12
|
+
"workflow-run-not-completed-error",
|
|
13
|
+
"workflow-runtime-error",
|
|
10
14
|
"workflow-world-error",
|
|
11
15
|
"throttle-error",
|
|
12
16
|
"entity-conflict-error",
|
|
13
17
|
"run-expired-error",
|
|
18
|
+
"run-not-supported-error",
|
|
14
19
|
"too-early-error"
|
|
15
20
|
]
|
|
16
21
|
}
|