workflow 5.0.0-beta.4 → 5.0.0-beta.40
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 +63 -20
- package/dist/api-workflow.d.ts +1 -1
- package/dist/api-workflow.d.ts.map +1 -1
- package/dist/api-workflow.js +1 -1
- package/dist/api.d.ts +5 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +12 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/internal/builtins.d.ts +17 -0
- package/dist/internal/builtins.d.ts.map +1 -1
- package/dist/internal/builtins.js +65 -1
- package/dist/internal/errors.d.ts +1 -1
- package/dist/internal/errors.d.ts.map +1 -1
- package/dist/internal/errors.js +2 -2
- package/dist/nest-builder.d.ts +2 -0
- package/dist/nest-builder.d.ts.map +1 -0
- package/dist/nest-builder.js +2 -0
- package/dist/nest-vercel-builder.d.ts +2 -0
- package/dist/nest-vercel-builder.d.ts.map +1 -0
- package/dist/nest-vercel-builder.js +2 -0
- package/dist/observability.d.ts +1 -1
- package/dist/observability.js +2 -2
- package/dist/runtime.d.ts +2 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +5 -2
- package/docs/ai/chat-session-modeling.mdx +7 -3
- package/docs/ai/defining-tools.mdx +2 -2
- package/docs/ai/index.mdx +30 -25
- package/docs/ai/message-queueing.mdx +10 -10
- package/docs/ai/resumable-streams.mdx +9 -1
- package/docs/api-reference/index.mdx +24 -0
- package/docs/api-reference/meta.json +8 -0
- package/docs/api-reference/vitest/index.mdx +28 -7
- package/docs/api-reference/workflow/create-hook.mdx +79 -0
- package/docs/api-reference/workflow/create-webhook.mdx +1 -0
- package/docs/api-reference/workflow/define-hook.mdx +26 -24
- package/docs/api-reference/workflow/fatal-error.mdx +29 -7
- package/docs/api-reference/workflow/fetch.mdx +8 -4
- package/docs/api-reference/workflow/index.mdx +3 -0
- package/docs/api-reference/workflow/set-attributes.mdx +61 -0
- package/docs/api-reference/workflow/sleep.mdx +1 -1
- package/docs/api-reference/workflow-ai/durable-agent.mdx +7 -45
- package/docs/api-reference/workflow-ai/index.mdx +2 -2
- package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +44 -1
- package/docs/api-reference/workflow-api/get-hook-by-token.mdx +9 -0
- package/docs/api-reference/workflow-api/get-run.mdx +25 -0
- package/docs/api-reference/workflow-api/index.mdx +6 -8
- package/docs/api-reference/workflow-api/resume-hook.mdx +60 -1
- package/docs/api-reference/workflow-api/resume-webhook.mdx +6 -4
- package/docs/api-reference/workflow-api/start.mdx +39 -6
- 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 +88 -0
- package/docs/api-reference/workflow-errors/meta.json +6 -0
- package/docs/api-reference/workflow-errors/precondition-failed-error.mdx +68 -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-failed-error.mdx +16 -6
- 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-globals.mdx +4 -1
- 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-next/with-workflow.mdx +56 -2
- package/docs/api-reference/workflow-nitro/index.mdx +60 -0
- package/docs/api-reference/workflow-nuxt/index.mdx +48 -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-runtime/world/analytics.mdx +138 -0
- package/docs/api-reference/{workflow-api → workflow-runtime}/world/index.mdx +10 -10
- package/docs/api-reference/workflow-runtime/world/meta.json +4 -0
- package/docs/api-reference/{workflow-api → workflow-runtime}/world/queue.mdx +8 -6
- package/docs/api-reference/{workflow-api → workflow-runtime}/world/storage.mdx +18 -5
- 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 +2 -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 +380 -0
- package/docs/changelog/eager-processing.mdx +269 -0
- package/docs/changelog/index.mdx +3 -1
- package/docs/changelog/lazy-event-creation.md +127 -0
- package/docs/changelog/meta.json +9 -1
- package/docs/changelog/resilient-resume.mdx +22 -0
- package/docs/changelog/resilient-start.mdx +31 -283
- package/docs/changelog/step-message-ownership.mdx +360 -0
- package/docs/changelog/turbo-mode.md +87 -0
- package/docs/comparisons/index.mdx +66 -0
- package/docs/comparisons/meta.json +11 -0
- package/docs/comparisons/workflow-sdk-vs-aws-agentcore.mdx +55 -0
- package/docs/comparisons/workflow-sdk-vs-aws-step-functions.mdx +111 -0
- package/docs/comparisons/workflow-sdk-vs-cloudflare-workflows.mdx +71 -0
- package/docs/comparisons/workflow-sdk-vs-inngest.mdx +102 -0
- package/docs/comparisons/workflow-sdk-vs-temporal.mdx +123 -0
- package/docs/comparisons/workflow-sdk-vs-trigger-dev.mdx +103 -0
- package/docs/configuration/build-and-diagnostics.mdx +70 -0
- package/docs/configuration/cli-and-web-ui.mdx +182 -0
- package/docs/configuration/framework-options.mdx +165 -0
- package/docs/configuration/index.mdx +32 -0
- package/docs/configuration/meta.json +12 -0
- package/docs/configuration/runtime-tuning.mdx +276 -0
- package/docs/configuration/worlds.mdx +275 -0
- package/docs/cookbook/advanced/child-workflows.mdx +203 -256
- package/docs/cookbook/advanced/meta.json +1 -1
- package/docs/cookbook/advanced/publishing-libraries.mdx +33 -24
- package/docs/cookbook/advanced/serializable-steps.mdx +12 -4
- package/docs/cookbook/advanced/upgrading-workflows.mdx +199 -0
- package/docs/cookbook/agent-patterns/agent-cancellation.mdx +34 -75
- package/docs/cookbook/agent-patterns/durable-agent.mdx +14 -142
- package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +11 -3
- package/docs/cookbook/common-patterns/batching.mdx +4 -0
- package/docs/cookbook/common-patterns/idempotency.mdx +40 -52
- package/docs/cookbook/common-patterns/rate-limiting.mdx +5 -1
- package/docs/cookbook/common-patterns/saga.mdx +6 -2
- package/docs/cookbook/common-patterns/scheduling.mdx +12 -0
- package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +4 -0
- package/docs/cookbook/common-patterns/timeouts.mdx +6 -1
- package/docs/cookbook/common-patterns/webhooks.mdx +4 -0
- package/docs/cookbook/common-patterns/workflow-composition.mdx +19 -16
- package/docs/cookbook/index.mdx +3 -3
- package/docs/cookbook/integrations/ai-sdk.mdx +64 -28
- package/docs/cookbook/integrations/chat-sdk.mdx +17 -0
- package/docs/cookbook/integrations/sandbox.mdx +17 -0
- package/docs/{deploying/index.mdx → deploying.mdx} +7 -7
- package/docs/errors/abort-signal-timeout-in-workflow.mdx +84 -0
- package/docs/errors/corrupted-event-log.mdx +5 -5
- package/docs/errors/deployment-mismatch.mdx +71 -0
- package/docs/errors/fetch-in-workflow.mdx +4 -0
- package/docs/errors/hook-conflict.mdx +60 -4
- package/docs/errors/index.mdx +1 -35
- package/docs/errors/node-js-module-in-workflow.mdx +4 -0
- package/docs/errors/replay-divergence.mdx +27 -0
- package/docs/errors/runtime-decryption-failed.mdx +77 -0
- package/docs/errors/serialization-failed.mdx +4 -0
- package/docs/errors/start-invalid-workflow-function.mdx +4 -0
- package/docs/errors/step-executed-multiple-times.mdx +23 -0
- package/docs/errors/step-not-registered.mdx +1 -1
- package/docs/errors/timeout-in-workflow.mdx +4 -0
- package/docs/errors/webhook-response-not-sent.mdx +4 -0
- package/docs/foundations/cancellation.mdx +459 -0
- package/docs/foundations/errors-and-retries.mdx +7 -3
- package/docs/foundations/hooks.mdx +29 -0
- package/docs/foundations/idempotency.mdx +243 -11
- package/docs/foundations/index.mdx +1 -23
- package/docs/foundations/meta.json +3 -1
- package/docs/foundations/serialization.mdx +77 -41
- package/docs/foundations/starting-workflows.mdx +79 -2
- package/docs/foundations/streaming.mdx +14 -23
- package/docs/foundations/versioning.mdx +263 -0
- package/docs/getting-started/astro.mdx +12 -2
- package/docs/getting-started/express.mdx +6 -2
- package/docs/getting-started/fastify.mdx +6 -2
- package/docs/getting-started/hono.mdx +6 -2
- package/docs/getting-started/index.mdx +16 -10
- package/docs/getting-started/meta.json +4 -1
- package/docs/getting-started/nestjs.mdx +77 -3
- package/docs/getting-started/next.mdx +12 -6
- package/docs/getting-started/nitro.mdx +28 -2
- package/docs/getting-started/nuxt.mdx +6 -2
- package/docs/getting-started/python.mdx +24 -18
- package/docs/getting-started/react-router/index.mdx +33 -0
- package/docs/getting-started/react-router/meta.json +5 -0
- package/docs/getting-started/react-router/v7.mdx +237 -0
- package/docs/getting-started/react-router/v8.mdx +232 -0
- package/docs/getting-started/sveltekit.mdx +12 -2
- package/docs/getting-started/tanstack-start.mdx +245 -0
- package/docs/getting-started/vite.mdx +6 -2
- package/docs/how-it-works/cancellation.mdx +287 -0
- package/docs/how-it-works/code-transform.mdx +21 -17
- package/docs/how-it-works/encryption.mdx +5 -5
- package/docs/how-it-works/event-sourcing.mdx +6 -6
- package/docs/how-it-works/framework-integrations.mdx +96 -337
- package/docs/how-it-works/meta.json +2 -1
- package/docs/internal/index.mdx +21 -0
- package/docs/internal/meta.json +10 -0
- package/docs/internal/nitro-native-build.mdx +38 -0
- package/docs/internal/nitro-web-ui.mdx +24 -0
- package/docs/internal/serializable-abort-controller.mdx +148 -0
- package/docs/meta.json +2 -2
- package/docs/observability/attributes.mdx +112 -0
- package/docs/observability/index.mdx +20 -1
- package/docs/observability/meta.json +1 -1
- package/docs/observability/tracing.mdx +124 -0
- package/docs/testing/index.mdx +2 -2
- package/package.json +17 -13
- package/docs/api-reference/workflow-api/world/meta.json +0 -4
- package/docs/api-reference/workflow-api/world/observability.mdx +0 -164
- package/docs/cookbook/advanced/distributed-abort-controller.mdx +0 -318
- package/docs/deploying/building-a-world.mdx +0 -251
- package/docs/deploying/meta.json +0 -4
- package/docs/deploying/world/local-world.mdx +0 -84
- package/docs/deploying/world/meta.json +0 -4
- package/docs/deploying/world/postgres-world.mdx +0 -222
- package/docs/deploying/world/vercel-world.mdx +0 -179
- package/docs/migration-guides/index.mdx +0 -34
- package/docs/migration-guides/meta.json +0 -9
- package/docs/migration-guides/migrating-from-aws-step-functions.mdx +0 -363
- package/docs/migration-guides/migrating-from-inngest.mdx +0 -314
- package/docs/migration-guides/migrating-from-temporal.mdx +0 -318
- package/docs/migration-guides/migrating-from-trigger-dev.mdx +0 -337
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
title: Publishing Libraries
|
|
3
3
|
description: Structure and publish npm packages that export workflow functions for consumers to use with Workflow SDK.
|
|
4
4
|
type: guide
|
|
5
|
-
summary: Learn how to build, export, and test npm packages that ship workflow and step functions — including package.json exports, re-exporting
|
|
5
|
+
summary: Learn how to build, export, and test npm packages that ship workflow and step functions — including package.json exports, re-exporting so the consumer's compiler discovers your workflows, keeping step I/O clean, and integration testing.
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
+
<CopyPrompt
|
|
9
|
+
text="Package these workflow functions as a publishable npm library. Give the package a dedicated workflows entry point (for example `exports["./workflows"]`) that ships the workflow and step source for the consumer's compiler to process. Keep every workflow and step input and output serializable, and read credentials from environment variables inside steps instead of accepting client instances. Document the consumer re-export requirement: consumers create a file in their `workflows/` directory containing `export * from "<pkg>/workflows"` so their build discovers and compiles the library's workflow and step files and replay can resolve them after cold starts. Add an integration test that runs a library workflow end to end from a consumer-style setup. Verify the build, that the library's workflows and steps show up as compiled entries, and replay safety."
|
|
10
|
+
/>
|
|
11
|
+
|
|
8
12
|
import { File, Folder, Files } from "fumadocs-ui/components/files";
|
|
9
13
|
|
|
10
14
|
<Callout>
|
|
@@ -110,31 +114,35 @@ export default defineConfig({
|
|
|
110
114
|
});
|
|
111
115
|
```
|
|
112
116
|
|
|
113
|
-
## Re-Exporting for
|
|
117
|
+
## Re-Exporting for Compiler Discovery
|
|
114
118
|
|
|
115
|
-
|
|
119
|
+
The workflow compiler only transforms files it discovers, and discovery starts from the consumer's `workflows/` directory and follows imports out from there. A library's workflow functions are not on that graph by default, so nothing compiles them and the runtime has no definition to run.
|
|
116
120
|
|
|
117
|
-
The fix is a **re-export file**. The consumer creates a file in their `workflows/` directory that re-exports the library's workflows
|
|
121
|
+
The fix is a **re-export file**. The consumer creates a file in their `workflows/` directory that re-exports the library's workflows, which pulls the library's source onto the discovery graph and gives its entry point an address the runtime can resolve.
|
|
118
122
|
|
|
119
123
|
### Consumer Setup
|
|
120
124
|
|
|
121
125
|
```typescript lineNumbers
|
|
122
126
|
// workflows/media.ts (in the consumer's project)
|
|
123
|
-
// Re-export library workflows so the
|
|
127
|
+
// Re-export library workflows so the compiler discovers and transforms them
|
|
124
128
|
export * from "@acme/media/workflows"; // [!code highlight]
|
|
125
129
|
```
|
|
126
130
|
|
|
127
|
-
This one-line file is all that's needed. The
|
|
131
|
+
This one-line file is all that's needed. The compiler follows the re-export into the package, transforms the workflow and step functions it finds, and registers them under IDs the runtime can resolve.
|
|
128
132
|
|
|
129
133
|
### Why This Is Necessary
|
|
130
134
|
|
|
131
|
-
Without re-exporting, the workflow runtime cannot match a running workflow to its function definition. When a
|
|
135
|
+
Without re-exporting, the workflow runtime cannot match a running workflow to its function definition. When a run is replayed after a cold start, the runtime looks up functions by their compiler-assigned IDs. If those functions were never compiled, the IDs don't exist and replay fails.
|
|
136
|
+
|
|
137
|
+
Point the re-export at the package's dedicated workflows entry point rather than a deep path into `dist/`. Files reachable through the package's `exports` map get an ID of the form `name/subpath@version`; a deep, non-exported file falls back to a path-based ID instead.
|
|
138
|
+
|
|
139
|
+
<Callout type="info">
|
|
140
|
+
**IDs for code you ship embed your package version.** A step in `@acme/media` version `1.4.0` gets the ID `step//@acme/media/workflows@1.4.0//transcode`, so publishing `1.5.0` renames every workflow and step the package ships.
|
|
132
141
|
|
|
133
|
-
|
|
142
|
+
That is safe on worlds with deployment pinning, such as Vercel, because runs are pinned to the deployment that started them. A run records its deployment ID and every resume targets that same deployment, so after a consumer upgrades, new runs execute the new version while runs already in flight keep replaying against the old one. Consumers do not need to drain anything before upgrading.
|
|
134
143
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
3. **Version upgrades** — re-exported IDs remain stable as long as the consumer's file doesn't change
|
|
144
|
+
The re-export file does not change any of this. An ID is derived from where the file lives, not from how it was imported, so a package file keeps its `name@version` ID whether or not a consumer re-exports it.
|
|
145
|
+
</Callout>
|
|
138
146
|
|
|
139
147
|
## Keeping Step I/O Clean
|
|
140
148
|
|
|
@@ -248,7 +256,7 @@ Some libraries want to be useful to consumers who *aren't* using Workflow SDK at
|
|
|
248
256
|
Two rules for isomorphic packages:
|
|
249
257
|
|
|
250
258
|
1. **Any runtime reference to the `workflow` package must be loaded via dynamic `import("workflow")` inside a try/catch.** A static top-level import makes the module fail to load for consumers who haven't installed workflow.
|
|
251
|
-
2. **The `"use workflow"` and `"use step"` directives are safe to keep in your library source.** When a consumer compiles your code with the Workflow SDK toolchain (via the [re-export pattern](#re-exporting-for-
|
|
259
|
+
2. **The `"use workflow"` and `"use step"` directives are safe to keep in your library source.** When a consumer compiles your code with the Workflow SDK toolchain (via the [re-export pattern](#re-exporting-for-compiler-discovery) above), the SWC plugin transforms them into durable-execution glue. When they're not compiled — plain Node, plain tests, a consumer without the runtime — they are just string expression statements and run as no-ops.
|
|
252
260
|
</Callout>
|
|
253
261
|
|
|
254
262
|
### Optional peer dependency
|
|
@@ -270,14 +278,14 @@ Declare `workflow` as an **optional** peer so consumers without the runtime aren
|
|
|
270
278
|
|
|
271
279
|
### Runtime detection
|
|
272
280
|
|
|
273
|
-
Wrap a dynamic `import("workflow")` in try/catch. If either the module isn't installed *or* `
|
|
281
|
+
Wrap a dynamic `import("workflow")` in try/catch. If either the module isn't installed *or* `getStepMetadata()` throws (call site isn't inside a workflow step), fall through to the standalone path.
|
|
274
282
|
|
|
275
283
|
```typescript lineNumbers
|
|
276
|
-
async function
|
|
284
|
+
async function getWorkflowStepId(): Promise<string | null> { // [!code highlight]
|
|
277
285
|
try {
|
|
278
286
|
const wf = await import("workflow");
|
|
279
|
-
const {
|
|
280
|
-
return
|
|
287
|
+
const { stepId } = wf.getStepMetadata();
|
|
288
|
+
return stepId;
|
|
281
289
|
} catch {
|
|
282
290
|
return null;
|
|
283
291
|
}
|
|
@@ -286,13 +294,14 @@ async function getWorkflowRunId(): Promise<string | null> { // [!code highlight]
|
|
|
286
294
|
|
|
287
295
|
### A concrete use case: replay-safe idempotency keys
|
|
288
296
|
|
|
289
|
-
A payments utility that uses the workflow
|
|
297
|
+
A payments utility that uses the current workflow step ID as a Stripe idempotency key when available, and a fresh UUID otherwise:
|
|
290
298
|
|
|
291
|
-
{/* @skip-typecheck - depends on getWorkflowRunId defined in the previous block */}
|
|
292
299
|
```typescript lineNumbers
|
|
300
|
+
declare function getWorkflowStepId(): Promise<string | null>; // @setup (defined in the previous block)
|
|
301
|
+
|
|
293
302
|
export async function processPayment(amount: number, currency: string) {
|
|
294
|
-
const
|
|
295
|
-
const idempotencyKey =
|
|
303
|
+
const stepId = await getWorkflowStepId();
|
|
304
|
+
const idempotencyKey = stepId ? `payment:${stepId}` : crypto.randomUUID(); // [!code highlight]
|
|
296
305
|
|
|
297
306
|
const res = await fetch("https://api.stripe.com/v1/charges", {
|
|
298
307
|
method: "POST",
|
|
@@ -306,7 +315,7 @@ export async function processPayment(amount: number, currency: string) {
|
|
|
306
315
|
}
|
|
307
316
|
```
|
|
308
317
|
|
|
309
|
-
When called from inside a workflow
|
|
318
|
+
When called from inside a workflow step, the utility gets a stable idempotency key for that step across retries — Stripe dedupes retries for free. When called from a plain Node.js process, it behaves like any other function and a fresh UUID is generated. For more patterns, see [Idempotency](/docs/foundations/idempotency).
|
|
310
319
|
|
|
311
320
|
### In production
|
|
312
321
|
|
|
@@ -330,7 +339,7 @@ Before publishing a workflow library:
|
|
|
330
339
|
|
|
331
340
|
## Key APIs
|
|
332
341
|
|
|
333
|
-
- [`"use workflow"`](/docs/
|
|
334
|
-
- [`"use step"`](/docs/
|
|
335
|
-
- [`start`](/docs/api-reference/workflow/start) — starts a workflow run
|
|
342
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions) — declares the orchestrator function
|
|
343
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions) — marks functions for durable execution
|
|
344
|
+
- [`start`](/docs/api-reference/workflow-api/start) — starts a workflow run
|
|
336
345
|
- [`getWorkflowMetadata`](/docs/api-reference/workflow/get-workflow-metadata) — runtime detection and run ID access
|
|
@@ -6,9 +6,13 @@ summary: Return a callback from a step to defer construction of a non-owned clas
|
|
|
6
6
|
related:
|
|
7
7
|
- /docs/foundations/serialization
|
|
8
8
|
- /docs/foundations/serialization#custom-class-serialization
|
|
9
|
-
- /docs/
|
|
9
|
+
- /docs/foundations/workflows-and-steps#step-functions
|
|
10
10
|
---
|
|
11
11
|
|
|
12
|
+
<CopyPrompt
|
|
13
|
+
text="Make this non-serializable dependency usable inside a durable workflow with the step-as-factory pattern. Instead of passing the object (AI SDK model, cloud SDK client) into the workflow, export a factory that returns an async callback marked with "use step" which constructs and returns the object at execution time, for example `export function openai(...args) { return async () => { "use step"; return openaiProvider(...args); }; }`. Pass the factory across the workflow boundary — the compiler serializes the function reference, not the instance — and invoke it inside steps where full Node.js access is available. Keep the factory's constructor arguments serializable. Verify the workflow builds, replays deterministically, and the dependency is only instantiated during step execution."
|
|
14
|
+
/>
|
|
15
|
+
|
|
12
16
|
<Callout>
|
|
13
17
|
This is an advanced guide. It dives into workflow internals and is not required reading to use workflow.
|
|
14
18
|
</Callout>
|
|
@@ -63,6 +67,10 @@ The `DurableAgent` receives a function (`() => Promise<LanguageModel>`) instead
|
|
|
63
67
|
|
|
64
68
|
## How `@workflow/ai` Uses This
|
|
65
69
|
|
|
70
|
+
<Callout type="warn">
|
|
71
|
+
`@workflow/ai`'s pre-wrapped providers and `DurableAgent` are deprecated. AI SDK's [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) resolves models from AI Gateway model strings (e.g. `"openai/gpt-4o"`), which usually removes the need for a model factory — see the [migration guide](https://ai-sdk.dev/v7/docs/agents/workflow-agent#migrating-from-durableagent). The serialization pattern on this page still applies to any non-serializable dependency you own (for example, cloud SDK clients).
|
|
72
|
+
</Callout>
|
|
73
|
+
|
|
66
74
|
The `@workflow/ai` package ships pre-wrapped providers for all major AI SDK backends. Each one follows the same pattern:
|
|
67
75
|
|
|
68
76
|
```typescript lineNumbers
|
|
@@ -141,7 +149,7 @@ async function uploadFile(
|
|
|
141
149
|
|
|
142
150
|
## Key APIs
|
|
143
151
|
|
|
144
|
-
- [`"use step"`](/docs/
|
|
145
|
-
- [`"use workflow"`](/docs/
|
|
146
|
-
- [`
|
|
152
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps#step-functions) — marks a function for extraction and serialization
|
|
153
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions) — declares the orchestrator function
|
|
154
|
+
- [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) — AI SDK's durable agent (resolves models via AI Gateway strings; replaces `DurableAgent`)
|
|
147
155
|
- [Custom class serialization](/docs/foundations/serialization#custom-class-serialization) — the companion pattern for classes you own (`WORKFLOW_SERIALIZE` / `WORKFLOW_DESERIALIZE`)
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Upgrading Workflows
|
|
3
|
+
description: Identify a clean upgrade point in a long-running workflow and spawn a fresh run on the latest deployment carrying state forward.
|
|
4
|
+
type: guide
|
|
5
|
+
summary: 'Identify a clean upgrade point and hand off to a fresh run via `start(self, [state], { deploymentId: "latest" })` — either automatically on every iteration, or on demand via a dedicated upgrade hook.'
|
|
6
|
+
related:
|
|
7
|
+
- /docs/foundations/versioning
|
|
8
|
+
- /cookbook/common-patterns/workflow-composition
|
|
9
|
+
- /docs/api-reference/workflow-api/start
|
|
10
|
+
- /docs/foundations/hooks
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
<CopyPrompt
|
|
14
|
+
text="Add a safe self-upgrade point to this long-running workflow. Identify the loop boundary where no step is mid-side-effect. Define a serializable state object that contains all progress needed to continue. At the boundary, call `start(self, [state], { deploymentId: "latest" })` or the documented replacement workflow with the carried state, then return from the old run. If upgrades should be manual, add a `defineHook()` upgrade signal and resume it from an API route with `resumeHook()` from `workflow/api`. Make the handoff idempotent so retries do not start duplicate successor runs, and verify old-to-new handoff plus duplicate prevention."
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
Workflows that block on external events for days, weeks, or months can outlive many deployments. **The key is to identify a clean upgrade point in the workflow** — a moment where it's safe to checkpoint state and start fresh — and then call [`start()`](/docs/api-reference/workflow-api/start) with `deploymentId: "latest"` to spawn a new run carrying that state forward. The current run ends; the next run begins on whatever deployment is live at that moment, so shipped fixes apply immediately without ever migrating an in-flight run.
|
|
18
|
+
|
|
19
|
+
<Callout type="info">
|
|
20
|
+
For the underlying model — why runs pin to a deployment by default, how cancel-and-rerun works, and how state crosses the version boundary — see [Versioning](/docs/foundations/versioning). This recipe focuses on event-driven workflows that need to keep advancing across deployments.
|
|
21
|
+
</Callout>
|
|
22
|
+
|
|
23
|
+
A clean upgrade point is any spot in the workflow where:
|
|
24
|
+
|
|
25
|
+
- All in-progress side effects have completed (or aren't needed by the next iteration)
|
|
26
|
+
- The relevant state can be serialized into the workflow's input arguments
|
|
27
|
+
- It's natural for the workflow to "checkpoint" — typically right after handling an external event, completing a batch, or finishing a logical phase
|
|
28
|
+
|
|
29
|
+
There are two ways to apply this:
|
|
30
|
+
|
|
31
|
+
1. **Upgrade on every iteration** ([Method 1](#method-1-upgrade-on-every-iteration)). Each run handles a single event and unconditionally hands off to a fresh run on the latest deployment before exiting. Simple — no extra triggers — but every event pays the respawn cost.
|
|
32
|
+
2. **Upgrade on demand via a dedicated hook** ([Method 2](#method-2-upgrade-on-demand-via-a-dedicated-hook)). A single long-lived run handles many events in a loop and only respawns when an `upgradeHook` fires. A separate endpoint resumes that hook from your control plane (e.g. after a deploy). More control and fewer respawns, at the cost of an explicit trigger.
|
|
33
|
+
|
|
34
|
+
### When to use each
|
|
35
|
+
|
|
36
|
+
- **Method 1** when iterations are short and frequent, the work is cheap to checkpoint, and you want shipped fixes to apply on the very next event. Long-lived "session" workflows (subscriptions, queues, FSMs) that already process events one at a time fit this naturally.
|
|
37
|
+
- **Method 2** when iterations are infrequent or expensive (you don't want to respawn on every event), or when you need to roll out a fix to a fleet of in-flight runs after a deploy by fanning out to a control-plane endpoint. Also fits when "upgrade" should be an explicit operation rather than a side effect of handling each event.
|
|
38
|
+
|
|
39
|
+
## Method 1: Upgrade on every iteration
|
|
40
|
+
|
|
41
|
+
Each run inherits state via its argument, blocks on a hook, processes the resume, then unconditionally hands off to its successor by calling `start()` directly from the workflow body with `deploymentId: "latest"`.
|
|
42
|
+
|
|
43
|
+
```typescript lineNumbers
|
|
44
|
+
import { defineHook, getWorkflowMetadata } from "workflow";
|
|
45
|
+
import { start } from "workflow/api";
|
|
46
|
+
|
|
47
|
+
declare function processItem(itemId: string): Promise<void>; // @setup
|
|
48
|
+
|
|
49
|
+
interface QueueState {
|
|
50
|
+
processed: number;
|
|
51
|
+
cursor: string | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const nextItemHook = defineHook<{ itemId: string }>();
|
|
55
|
+
|
|
56
|
+
export async function longRunningQueue(
|
|
57
|
+
state: QueueState = { processed: 0, cursor: null },
|
|
58
|
+
): Promise<void> {
|
|
59
|
+
"use workflow";
|
|
60
|
+
|
|
61
|
+
const { workflowRunId } = getWorkflowMetadata();
|
|
62
|
+
|
|
63
|
+
// Block until something fires the hook — could be hours, days, or longer.
|
|
64
|
+
// Per-run hook tokens (workflowRunId) keep concurrent chains isolated.
|
|
65
|
+
const { itemId } = await nextItemHook.create({ token: workflowRunId }); // [!code highlight]
|
|
66
|
+
|
|
67
|
+
await processItem(itemId);
|
|
68
|
+
|
|
69
|
+
// Hand off to a fresh run on the latest deployment. THIS run ends here.
|
|
70
|
+
// `deploymentId: "latest"` resolves to whichever deployment is current
|
|
71
|
+
// when this spawn lands — NOT the deployment running this code.
|
|
72
|
+
await start( // [!code highlight]
|
|
73
|
+
longRunningQueue, // [!code highlight]
|
|
74
|
+
[{ processed: state.processed + 1, cursor: itemId }], // [!code highlight]
|
|
75
|
+
{ deploymentId: "latest" }, // [!code highlight]
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Resuming the hook
|
|
81
|
+
|
|
82
|
+
Any server-side code can resume the currently-active iteration by calling `.resume()` with the run ID:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { nextItemHook } from "@/workflows/long-running-queue";
|
|
86
|
+
|
|
87
|
+
export async function POST(req: Request) {
|
|
88
|
+
const { runId, itemId } = await req.json();
|
|
89
|
+
|
|
90
|
+
await nextItemHook.resume(runId, { itemId }); // [!code highlight]
|
|
91
|
+
|
|
92
|
+
return Response.json({ success: true });
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The caller tracks the active `runId` (e.g. in a database, KV, or returned from the previous iteration) and updates it whenever the chain advances.
|
|
97
|
+
|
|
98
|
+
## Method 2: Upgrade on demand via a dedicated hook
|
|
99
|
+
|
|
100
|
+
Use a single long-running workflow that handles events in a loop. Define a second hook — `upgradeHook` — alongside the work hook, and race them. While only the work hook fires, the run keeps handling events on its current deployment. When `upgradeHook` resumes, the workflow captures current state and respawns on the latest deployment, then exits.
|
|
101
|
+
|
|
102
|
+
```typescript lineNumbers
|
|
103
|
+
import { defineHook, getWorkflowMetadata } from "workflow";
|
|
104
|
+
import { start } from "workflow/api";
|
|
105
|
+
|
|
106
|
+
declare function processItem(itemId: string): Promise<void>; // @setup
|
|
107
|
+
|
|
108
|
+
interface QueueState {
|
|
109
|
+
processed: number;
|
|
110
|
+
cursor: string | null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const nextItemHook = defineHook<{ itemId: string }>();
|
|
114
|
+
export const upgradeHook = defineHook<{ reason?: string }>(); // [!code highlight]
|
|
115
|
+
|
|
116
|
+
export async function longRunningQueue(
|
|
117
|
+
state: QueueState = { processed: 0, cursor: null },
|
|
118
|
+
): Promise<void> {
|
|
119
|
+
"use workflow";
|
|
120
|
+
|
|
121
|
+
const { workflowRunId } = getWorkflowMetadata();
|
|
122
|
+
|
|
123
|
+
while (true) {
|
|
124
|
+
// Race a normal work event against the upgrade signal.
|
|
125
|
+
const event = await Promise.race([ // [!code highlight]
|
|
126
|
+
nextItemHook
|
|
127
|
+
.create({ token: workflowRunId })
|
|
128
|
+
.then((payload) => ({ kind: "work" as const, payload })),
|
|
129
|
+
upgradeHook // [!code highlight]
|
|
130
|
+
.create({ token: workflowRunId }) // [!code highlight]
|
|
131
|
+
.then(() => ({ kind: "upgrade" as const })), // [!code highlight]
|
|
132
|
+
]);
|
|
133
|
+
|
|
134
|
+
if (event.kind === "upgrade") { // [!code highlight]
|
|
135
|
+
// Checkpoint current state and hand off to a fresh run
|
|
136
|
+
// on whatever deployment is live now. THIS run ends here.
|
|
137
|
+
await start(longRunningQueue, [state], { // [!code highlight]
|
|
138
|
+
deploymentId: "latest", // [!code highlight]
|
|
139
|
+
}); // [!code highlight]
|
|
140
|
+
return; // [!code highlight]
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
await processItem(event.payload.itemId);
|
|
144
|
+
state = {
|
|
145
|
+
processed: state.processed + 1,
|
|
146
|
+
cursor: event.payload.itemId,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Triggering the upgrade
|
|
153
|
+
|
|
154
|
+
Expose a separate endpoint that resumes `upgradeHook` for a given run. Call it from your deploy pipeline, an admin UI, or a fan-out script that iterates over every active run after shipping a fix.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { upgradeHook } from "@/workflows/long-running-queue";
|
|
158
|
+
|
|
159
|
+
export async function POST(req: Request) {
|
|
160
|
+
const { runId, reason } = await req.json();
|
|
161
|
+
|
|
162
|
+
// The workflow exits its loop, captures state, and respawns
|
|
163
|
+
// on the latest deployment.
|
|
164
|
+
await upgradeHook.resume(runId, { reason }); // [!code highlight]
|
|
165
|
+
|
|
166
|
+
return Response.json({ success: true });
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
To upgrade a fleet of runs after a deploy, list active runs (e.g. from a tracking store) and call this endpoint for each.
|
|
171
|
+
|
|
172
|
+
## How it works
|
|
173
|
+
|
|
174
|
+
1. **`deploymentId: "latest"` is the upgrade knob.** Without it, the spawn pins to the current deployment. With it, the new run resolves to whatever deployment is current when the runtime picks it up — so any shipped fix applies starting from that respawn. Both methods rely on this.
|
|
175
|
+
2. **`start()` runs directly from the workflow body.** In v5, [`start()`](/docs/api-reference/workflow-api/start) is step-backed, so it can be called from a workflow function and still records a deterministic step boundary in the event log — no manual `"use step"` wrapper is required.
|
|
176
|
+
3. **State carries through the function argument.** The accumulating context flows from run N to run N+1 as a serialized argument. No external store is required for the state itself.
|
|
177
|
+
4. **Per-run hook tokens.** Using `workflowRunId` as the hook token scopes each iteration's wait to its own run, so multiple chains can run concurrently without interfering.
|
|
178
|
+
5. **Method 1 vs Method 2 is just where the spawn happens.** In Method 1 every run spawns its successor unconditionally before exiting — there is no long-lived process to migrate. In Method 2 the spawn happens only when the upgrade hook fires; otherwise the loop keeps handling events on the same run.
|
|
179
|
+
|
|
180
|
+
## Adapting to your use case
|
|
181
|
+
|
|
182
|
+
- **Combine with a sleep.** Race the hook against `sleep()` so iterations also tick on a timer: `Promise.race([hook, sleep("1d")])` lets the workflow advance even if no external event arrives.
|
|
183
|
+
- **Stateless successors.** If the next iteration doesn't need the previous state (e.g. a pure event router), call `start(longRunningQueue, [], { deploymentId: "latest" })` and skip the argument plumbing.
|
|
184
|
+
- **Persist state externally.** If state needs to be readable from outside the workflow (dashboards, debugging, recovery), write it to a database in a step before spawning the next run.
|
|
185
|
+
- **Track the active runId externally.** Whatever resumes the hook needs to know the current run. Capture the `runId` returned by `start()` and write it to a KV/database keyed by a stable session identifier (in a step) so resumers always look up the latest one.
|
|
186
|
+
|
|
187
|
+
## Caveats
|
|
188
|
+
|
|
189
|
+
- **Backward compatibility matters.** Because the next run executes on a different deployment, the workflow's input arguments and return type must remain compatible across deployments. Adding required fields, removing fields, or changing types can cause serialization failures. See the [`deploymentId: "latest"` callout](/docs/api-reference/workflow-api/start#using-deploymentid-latest).
|
|
190
|
+
- **Workflow identity is the function name + file path.** Renaming the function or moving the file across a deployment changes the workflow ID — the next iteration will fail to resolve. Treat the workflow's name and location as stable interfaces.
|
|
191
|
+
- **There is a tiny gap between iterations.** The current run ends as soon as `start()` returns; the next run starts asynchronously. A resume that arrives in that window can fail with "hook not found." Make resumers retry, or have the API persist pending payloads and apply them once the next iteration is ready.
|
|
192
|
+
- **Method 2: track active runs externally.** Because Method 2's runs are long-lived, the set of in-flight runs only changes when one starts, completes, or upgrades. Persist run IDs (and clean them up on completion or upgrade) so a rollout script can fan out reliably. After resuming `upgradeHook`, also update the tracked run ID once the new run reports back, the same way you would in Method 1.
|
|
193
|
+
|
|
194
|
+
## Key APIs
|
|
195
|
+
|
|
196
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps) — marks the orchestrator function
|
|
197
|
+
- [`start()`](/docs/api-reference/workflow-api/start) with [`deploymentId: "latest"`](/docs/api-reference/workflow-api/start#using-deploymentid-latest) — spawn the successor on the newest deployment
|
|
198
|
+
- [`defineHook()`](/docs/api-reference/workflow/define-hook) — suspend the workflow until an external event resumes it
|
|
199
|
+
- [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata) — exposes `workflowRunId` for per-run hook tokens
|
|
@@ -1,70 +1,23 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: Agent Cancellation
|
|
3
|
-
description: Cancel a running agent from the outside —
|
|
3
|
+
description: Cancel a running agent from the outside using AbortSignal — a hook fires the abort, the agent step bails out of the model stream, and the client gets a clean stop notification.
|
|
4
4
|
type: guide
|
|
5
|
-
summary:
|
|
5
|
+
summary: Cancel a running agent cooperatively with AbortController. A stop hook fires controller.abort(), the signal propagates into the agent step to cancel the model stream, and a data-stopped part is emitted to streaming clients before the workflow returns.
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
<CopyPrompt
|
|
9
|
+
text="Add cancellation to this durable AI agent. For hard cancellation, expose a server route that receives `runId` and calls `getRun(runId).cancel()` from `workflow/api`. For graceful stop, define `stopHook` with `defineHook()` from `workflow`, create it with a stable token such as the workflow run ID, and race the agent loop against the stop hook using `Promise.race`. Use `getWritable<UIMessageChunk>()` to emit a final stopped/cancelled message before returning. Wire the UI Stop button to the route that resumes the hook or falls back to `getRun(runId).cancel()`. Verify active model/tool work stops, cleanup runs for graceful stop, and stale run IDs are handled."
|
|
10
|
+
/>
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* **Chat stop buttons** — let users cancel a long-running agent from the browser
|
|
13
|
-
* **Admin cancellation** — stop an agent from a different process or API
|
|
14
|
-
* **Timeout fallback** — combine with `sleep()` to auto-stop after a deadline
|
|
15
|
-
|
|
16
|
-
## Choosing an approach
|
|
17
|
-
|
|
18
|
-
Pick the option that matches what your endpoint needs to deliver to the caller:
|
|
19
|
-
|
|
20
|
-
* **Hard Cancellation** — terminates the run immediately with no opportunity for cleanup or client notification. A single line of code, but the workflow throws `WorkflowRunCancelledError` and any streaming clients see an abrupt connection close.
|
|
21
|
-
* **Stop Signal** — the workflow exits as soon as the hook fires, runs any pending cleanup, emits a final `data-stopped` part to the stream so the client can render cleanly, and returns a real result.
|
|
22
|
-
|
|
23
|
-
The trade-offs at a glance:
|
|
24
|
-
|
|
25
|
-
| | Hard Cancellation | Stop Signal |
|
|
26
|
-
| --- | --- | --- |
|
|
27
|
-
| Mechanism | `getRun(runId).cancel()` | Hook + `Promise.race` |
|
|
28
|
-
| Speed to terminate | Immediate | At the next `await` boundary in the workflow |
|
|
29
|
-
| Runs `finally` / cleanup | No | Yes |
|
|
30
|
-
| Final stream notification | No (abrupt close) | Yes (`data-stopped` part) |
|
|
31
|
-
| `run.returnValue` | Throws `WorkflowRunCancelledError` | Returns the workflow's result |
|
|
32
|
-
| Code complexity | One line | Hook + race + signal step |
|
|
33
|
-
| Best for | Stuck or unresponsive runs, forced termination | User-facing stop, admin cancel, timeouts |
|
|
34
|
-
|
|
35
|
-
## Hard Cancellation
|
|
36
|
-
|
|
37
|
-
Call `.cancel()` on a run to terminate it immediately:
|
|
38
|
-
|
|
39
|
-
```typescript lineNumbers
|
|
40
|
-
import { getRun } from "workflow/api";
|
|
41
|
-
|
|
42
|
-
export async function POST(
|
|
43
|
-
_request: Request,
|
|
44
|
-
{ params }: { params: Promise<{ runId: string }> }
|
|
45
|
-
) {
|
|
46
|
-
const { runId } = await params;
|
|
47
|
-
await getRun(runId).cancel(); // [!code highlight]
|
|
48
|
-
return Response.json({ success: true });
|
|
49
|
-
}
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
This is an abrupt termination — the run is stopped mid-step with no opportunity to exit cleanly:
|
|
53
|
-
|
|
54
|
-
* **No cleanup runs** — `finally` blocks, defer-style step cleanup, and any logic after the current step are all skipped
|
|
55
|
-
* **No final notification to the client** — the writable closes abruptly, so a streaming UI just sees the connection drop with no `data-stopped` part to render a clean ending
|
|
56
|
-
* **`run.returnValue` throws** — anyone awaiting the result receives [`WorkflowRunCancelledError`](/docs/api-reference/workflow-errors/workflow-run-cancelled-error) instead of a meaningful payload
|
|
57
|
-
* **Underlying step keeps running** — same caveat as the Stop Signal pattern below: the model stream or HTTP call inside the current step continues to completion in the background
|
|
58
|
-
|
|
59
|
-
Hard Cancellation is the appropriate choice when the run is stuck or unresponsive, has exceeded its expected runtime, or you don't need a clean exit. For everything else — chat stop buttons, admin "stop" actions, timeout fallbacks — you typically want the Stop Signal pattern: the agent finishes its current step, emits a final stream part so the client renders a clean ending, and returns a real result.
|
|
60
|
-
|
|
61
|
-
## Stop Signal
|
|
12
|
+
Cancel a running agent from the outside — for example, a "Stop" button in a chat UI, an admin cancellation endpoint, or a timeout fallback.
|
|
62
13
|
|
|
63
14
|
<Callout type="warn">
|
|
64
|
-
|
|
15
|
+
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 cancellation patterns here (`run.cancel()`, stop-signal hook + `Promise.race`, `AbortController`) apply to either API.
|
|
65
16
|
</Callout>
|
|
66
17
|
|
|
67
|
-
|
|
18
|
+
## Pattern
|
|
19
|
+
|
|
20
|
+
Create an `AbortController` in the workflow and race the agent (passing its signal) against a stop hook. When the hook fires, `controller.abort()` is called — the signal propagates into the agent step and cancels the underlying model stream. Before returning, a `data-stopped` part is written to the stream so any streaming clients can render a clean end state.
|
|
68
21
|
|
|
69
22
|
```typescript lineNumbers
|
|
70
23
|
import { DurableAgent } from "@workflow/ai/agent";
|
|
@@ -88,7 +41,7 @@ async function analyzeData({ topic }: { topic: string }) {
|
|
|
88
41
|
return { summary: `Analysis of ${topic}: significant developments found.`, confidence: 0.85 };
|
|
89
42
|
}
|
|
90
43
|
|
|
91
|
-
async function emitStopSignal(details: { reason?: string }) {
|
|
44
|
+
async function emitStopSignal(details: { reason?: string }) {
|
|
92
45
|
"use step";
|
|
93
46
|
const writer = getWritable<UIMessageChunk>().getWriter();
|
|
94
47
|
try {
|
|
@@ -102,7 +55,8 @@ export async function stoppableAgent(messages: ModelMessage[]) {
|
|
|
102
55
|
"use workflow";
|
|
103
56
|
|
|
104
57
|
const { workflowRunId } = getWorkflowMetadata();
|
|
105
|
-
const
|
|
58
|
+
const controller = new AbortController(); // [!code highlight]
|
|
59
|
+
const hook = stopHook.create({ token: `stop:${workflowRunId}` });
|
|
106
60
|
|
|
107
61
|
const agent = new DurableAgent({
|
|
108
62
|
model: "anthropic/claude-haiku-4.5",
|
|
@@ -121,15 +75,23 @@ export async function stoppableAgent(messages: ModelMessage[]) {
|
|
|
121
75
|
},
|
|
122
76
|
});
|
|
123
77
|
|
|
124
|
-
const result = await Promise.race([
|
|
78
|
+
const result = await Promise.race([
|
|
125
79
|
agent
|
|
126
|
-
.stream({
|
|
80
|
+
.stream({
|
|
81
|
+
messages,
|
|
82
|
+
writable: getWritable<UIMessageChunk>(),
|
|
83
|
+
abortSignal: controller.signal, // [!code highlight]
|
|
84
|
+
maxSteps: 15,
|
|
85
|
+
})
|
|
127
86
|
.then((r) => ({ type: "complete" as const, messages: r.messages })),
|
|
128
|
-
hook.then(({ reason }) =>
|
|
87
|
+
hook.then(({ reason }) => {
|
|
88
|
+
controller.abort(reason); // [!code highlight]
|
|
89
|
+
return { type: "stopped" as const, reason };
|
|
90
|
+
}),
|
|
129
91
|
]);
|
|
130
92
|
|
|
131
93
|
if (result.type === "stopped") {
|
|
132
|
-
await emitStopSignal({ reason: result.reason });
|
|
94
|
+
await emitStopSignal({ reason: result.reason });
|
|
133
95
|
}
|
|
134
96
|
|
|
135
97
|
return result;
|
|
@@ -148,7 +110,7 @@ export async function POST(
|
|
|
148
110
|
const { runId } = await params;
|
|
149
111
|
const { reason } = await request.json();
|
|
150
112
|
|
|
151
|
-
await stopHook.resume(`stop:${runId}`, {
|
|
113
|
+
await stopHook.resume(`stop:${runId}`, {
|
|
152
114
|
reason: reason || "User requested stop",
|
|
153
115
|
});
|
|
154
116
|
|
|
@@ -180,13 +142,12 @@ export function StopButton({ runId }: { runId: string }) {
|
|
|
180
142
|
|
|
181
143
|
## How it works
|
|
182
144
|
|
|
183
|
-
1.
|
|
184
|
-
2.
|
|
185
|
-
3.
|
|
186
|
-
4.
|
|
187
|
-
5.
|
|
188
|
-
|
|
189
|
-
This is the same pattern used by the [Distributed Abort Controller](/cookbook/advanced/distributed-abort-controller) — race a long-running operation against a hook signal.
|
|
145
|
+
1. An `AbortController` is created at the start of the workflow
|
|
146
|
+
2. A hook is created with token `stop:${workflowRunId}`
|
|
147
|
+
3. `Promise.race` runs the agent stream and the stop hook concurrently
|
|
148
|
+
4. The agent receives `controller.signal` — when aborted, the underlying model stream is cancelled
|
|
149
|
+
5. When the stop API resumes the hook, `controller.abort()` is called — the race resolves and the workflow exits
|
|
150
|
+
6. `emitStopSignal` writes a `data-stopped` part to the stream so the client renders a clean stop state
|
|
190
151
|
|
|
191
152
|
## Adapting this
|
|
192
153
|
|
|
@@ -194,12 +155,10 @@ This is the same pattern used by the [Distributed Abort Controller](/cookbook/ad
|
|
|
194
155
|
* **Audit logging** — include a `reason` field in the stop schema to record who stopped and why
|
|
195
156
|
* **Cross-process** — the hook token is deterministic, so any process can call `stopHook.resume()` with the run ID
|
|
196
157
|
* **Step limits** — combine with `maxSteps` on the agent to cap execution even without manual stop
|
|
197
|
-
* **Hard Cancellation as a fallback** — wire your stop endpoint to fall back to `getRun(runId).cancel()` if the hook resume errors with `not found` / `expired` (for example, the hook was already consumed). This guarantees the run is terminated even when the Stop Signal path is unavailable.
|
|
198
158
|
|
|
199
159
|
## Key APIs
|
|
200
160
|
|
|
201
161
|
* [`defineHook()`](/docs/api-reference/workflow/define-hook) — type-safe hook for the stop signal
|
|
202
162
|
* [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata) — access the run ID for deterministic hook tokens
|
|
203
|
-
* [`getWritable()`](/docs/api-reference/workflow/get-writable) — stream
|
|
204
|
-
* [`
|
|
205
|
-
* [`getRun()`](/docs/api-reference/workflow-api/get-run) — entry point for Hard Cancellation: `getRun(runId).cancel()`
|
|
163
|
+
* [`getWritable()`](/docs/api-reference/workflow/get-writable) — stream output and the stop notification to the client
|
|
164
|
+
* [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) — AI SDK's durable agent that respects the abort signal (replaces `DurableAgent`)
|