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.
Files changed (227) hide show
  1. package/README.md +63 -20
  2. package/dist/api-workflow.d.ts +1 -1
  3. package/dist/api-workflow.d.ts.map +1 -1
  4. package/dist/api-workflow.js +1 -1
  5. package/dist/api.d.ts +5 -1
  6. package/dist/api.d.ts.map +1 -1
  7. package/dist/api.js +12 -2
  8. package/dist/index.d.ts +1 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +6 -1
  11. package/dist/internal/builtins.d.ts +17 -0
  12. package/dist/internal/builtins.d.ts.map +1 -1
  13. package/dist/internal/builtins.js +65 -1
  14. package/dist/internal/errors.d.ts +1 -1
  15. package/dist/internal/errors.d.ts.map +1 -1
  16. package/dist/internal/errors.js +2 -2
  17. package/dist/nest-builder.d.ts +2 -0
  18. package/dist/nest-builder.d.ts.map +1 -0
  19. package/dist/nest-builder.js +2 -0
  20. package/dist/nest-vercel-builder.d.ts +2 -0
  21. package/dist/nest-vercel-builder.d.ts.map +1 -0
  22. package/dist/nest-vercel-builder.js +2 -0
  23. package/dist/observability.d.ts +1 -1
  24. package/dist/observability.js +2 -2
  25. package/dist/runtime.d.ts +2 -1
  26. package/dist/runtime.d.ts.map +1 -1
  27. package/dist/runtime.js +5 -2
  28. package/docs/ai/chat-session-modeling.mdx +7 -3
  29. package/docs/ai/defining-tools.mdx +2 -2
  30. package/docs/ai/index.mdx +30 -25
  31. package/docs/ai/message-queueing.mdx +10 -10
  32. package/docs/ai/resumable-streams.mdx +9 -1
  33. package/docs/api-reference/index.mdx +24 -0
  34. package/docs/api-reference/meta.json +8 -0
  35. package/docs/api-reference/vitest/index.mdx +28 -7
  36. package/docs/api-reference/workflow/create-hook.mdx +79 -0
  37. package/docs/api-reference/workflow/create-webhook.mdx +1 -0
  38. package/docs/api-reference/workflow/define-hook.mdx +26 -24
  39. package/docs/api-reference/workflow/fatal-error.mdx +29 -7
  40. package/docs/api-reference/workflow/fetch.mdx +8 -4
  41. package/docs/api-reference/workflow/index.mdx +3 -0
  42. package/docs/api-reference/workflow/set-attributes.mdx +61 -0
  43. package/docs/api-reference/workflow/sleep.mdx +1 -1
  44. package/docs/api-reference/workflow-ai/durable-agent.mdx +7 -45
  45. package/docs/api-reference/workflow-ai/index.mdx +2 -2
  46. package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +44 -1
  47. package/docs/api-reference/workflow-api/get-hook-by-token.mdx +9 -0
  48. package/docs/api-reference/workflow-api/get-run.mdx +25 -0
  49. package/docs/api-reference/workflow-api/index.mdx +6 -8
  50. package/docs/api-reference/workflow-api/resume-hook.mdx +60 -1
  51. package/docs/api-reference/workflow-api/resume-webhook.mdx +6 -4
  52. package/docs/api-reference/workflow-api/start.mdx +39 -6
  53. package/docs/api-reference/workflow-astro/index.mdx +18 -0
  54. package/docs/api-reference/workflow-astro/meta.json +4 -0
  55. package/docs/api-reference/workflow-astro/workflow.mdx +45 -0
  56. package/docs/api-reference/workflow-errors/hook-conflict-error.mdx +60 -0
  57. package/docs/api-reference/workflow-errors/index.mdx +88 -0
  58. package/docs/api-reference/workflow-errors/meta.json +6 -0
  59. package/docs/api-reference/workflow-errors/precondition-failed-error.mdx +68 -0
  60. package/docs/api-reference/workflow-errors/run-not-supported-error.mdx +58 -0
  61. package/docs/api-reference/workflow-errors/workflow-error.mdx +52 -0
  62. package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +16 -6
  63. package/docs/api-reference/workflow-errors/workflow-run-not-completed-error.mdx +58 -0
  64. package/docs/api-reference/workflow-errors/workflow-runtime-error.mdx +58 -0
  65. package/docs/api-reference/workflow-globals.mdx +4 -1
  66. package/docs/api-reference/workflow-nest/configure-workflow-controller.mdx +33 -0
  67. package/docs/api-reference/workflow-nest/index.mdx +31 -0
  68. package/docs/api-reference/workflow-nest/meta.json +9 -0
  69. package/docs/api-reference/workflow-nest/nest-local-builder.mdx +64 -0
  70. package/docs/api-reference/workflow-nest/workflow-controller.mdx +40 -0
  71. package/docs/api-reference/workflow-nest/workflow-module.mdx +74 -0
  72. package/docs/api-reference/workflow-next/with-workflow.mdx +56 -2
  73. package/docs/api-reference/workflow-nitro/index.mdx +60 -0
  74. package/docs/api-reference/workflow-nuxt/index.mdx +48 -0
  75. package/docs/api-reference/workflow-observability/hydrate-data.mdx +35 -0
  76. package/docs/api-reference/workflow-observability/hydrate-resource-io.mdx +62 -0
  77. package/docs/api-reference/workflow-observability/index.mdx +64 -0
  78. package/docs/api-reference/workflow-observability/meta.json +11 -0
  79. package/docs/api-reference/workflow-observability/observability-revivers.mdx +50 -0
  80. package/docs/api-reference/workflow-observability/parse-class-name.mdx +41 -0
  81. package/docs/api-reference/workflow-observability/parse-step-name.mdx +40 -0
  82. package/docs/api-reference/workflow-observability/parse-workflow-name.mdx +55 -0
  83. package/docs/api-reference/workflow-runtime/create-world.mdx +39 -0
  84. package/docs/api-reference/workflow-runtime/get-world-handlers.mdx +44 -0
  85. package/docs/api-reference/{workflow-api → workflow-runtime}/get-world.mdx +7 -10
  86. package/docs/api-reference/workflow-runtime/health-check.mdx +50 -0
  87. package/docs/api-reference/workflow-runtime/index.mdx +43 -0
  88. package/docs/api-reference/workflow-runtime/meta.json +12 -0
  89. package/docs/api-reference/workflow-runtime/set-world.mdx +49 -0
  90. package/docs/api-reference/workflow-runtime/workflow-entrypoint.mdx +42 -0
  91. package/docs/api-reference/workflow-runtime/world/analytics.mdx +138 -0
  92. package/docs/api-reference/{workflow-api → workflow-runtime}/world/index.mdx +10 -10
  93. package/docs/api-reference/workflow-runtime/world/meta.json +4 -0
  94. package/docs/api-reference/{workflow-api → workflow-runtime}/world/queue.mdx +8 -6
  95. package/docs/api-reference/{workflow-api → workflow-runtime}/world/storage.mdx +18 -5
  96. package/docs/api-reference/{workflow-api → workflow-runtime}/world/streams.mdx +2 -2
  97. package/docs/api-reference/workflow-serde/index.mdx +0 -1
  98. package/docs/api-reference/workflow-serde/workflow-deserialize.mdx +1 -2
  99. package/docs/api-reference/workflow-serde/workflow-serialize.mdx +2 -2
  100. package/docs/api-reference/workflow-sveltekit/index.mdx +18 -0
  101. package/docs/api-reference/workflow-sveltekit/meta.json +4 -0
  102. package/docs/api-reference/workflow-sveltekit/workflow-plugin.mdx +42 -0
  103. package/docs/api-reference/workflow-vite/index.mdx +18 -0
  104. package/docs/api-reference/workflow-vite/meta.json +4 -0
  105. package/docs/api-reference/workflow-vite/workflow.mdx +48 -0
  106. package/docs/changelog/attributes-mvp.mdx +380 -0
  107. package/docs/changelog/eager-processing.mdx +269 -0
  108. package/docs/changelog/index.mdx +3 -1
  109. package/docs/changelog/lazy-event-creation.md +127 -0
  110. package/docs/changelog/meta.json +9 -1
  111. package/docs/changelog/resilient-resume.mdx +22 -0
  112. package/docs/changelog/resilient-start.mdx +31 -283
  113. package/docs/changelog/step-message-ownership.mdx +360 -0
  114. package/docs/changelog/turbo-mode.md +87 -0
  115. package/docs/comparisons/index.mdx +66 -0
  116. package/docs/comparisons/meta.json +11 -0
  117. package/docs/comparisons/workflow-sdk-vs-aws-agentcore.mdx +55 -0
  118. package/docs/comparisons/workflow-sdk-vs-aws-step-functions.mdx +111 -0
  119. package/docs/comparisons/workflow-sdk-vs-cloudflare-workflows.mdx +71 -0
  120. package/docs/comparisons/workflow-sdk-vs-inngest.mdx +102 -0
  121. package/docs/comparisons/workflow-sdk-vs-temporal.mdx +123 -0
  122. package/docs/comparisons/workflow-sdk-vs-trigger-dev.mdx +103 -0
  123. package/docs/configuration/build-and-diagnostics.mdx +70 -0
  124. package/docs/configuration/cli-and-web-ui.mdx +182 -0
  125. package/docs/configuration/framework-options.mdx +165 -0
  126. package/docs/configuration/index.mdx +32 -0
  127. package/docs/configuration/meta.json +12 -0
  128. package/docs/configuration/runtime-tuning.mdx +276 -0
  129. package/docs/configuration/worlds.mdx +275 -0
  130. package/docs/cookbook/advanced/child-workflows.mdx +203 -256
  131. package/docs/cookbook/advanced/meta.json +1 -1
  132. package/docs/cookbook/advanced/publishing-libraries.mdx +33 -24
  133. package/docs/cookbook/advanced/serializable-steps.mdx +12 -4
  134. package/docs/cookbook/advanced/upgrading-workflows.mdx +199 -0
  135. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +34 -75
  136. package/docs/cookbook/agent-patterns/durable-agent.mdx +14 -142
  137. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +11 -3
  138. package/docs/cookbook/common-patterns/batching.mdx +4 -0
  139. package/docs/cookbook/common-patterns/idempotency.mdx +40 -52
  140. package/docs/cookbook/common-patterns/rate-limiting.mdx +5 -1
  141. package/docs/cookbook/common-patterns/saga.mdx +6 -2
  142. package/docs/cookbook/common-patterns/scheduling.mdx +12 -0
  143. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +4 -0
  144. package/docs/cookbook/common-patterns/timeouts.mdx +6 -1
  145. package/docs/cookbook/common-patterns/webhooks.mdx +4 -0
  146. package/docs/cookbook/common-patterns/workflow-composition.mdx +19 -16
  147. package/docs/cookbook/index.mdx +3 -3
  148. package/docs/cookbook/integrations/ai-sdk.mdx +64 -28
  149. package/docs/cookbook/integrations/chat-sdk.mdx +17 -0
  150. package/docs/cookbook/integrations/sandbox.mdx +17 -0
  151. package/docs/{deploying/index.mdx → deploying.mdx} +7 -7
  152. package/docs/errors/abort-signal-timeout-in-workflow.mdx +84 -0
  153. package/docs/errors/corrupted-event-log.mdx +5 -5
  154. package/docs/errors/deployment-mismatch.mdx +71 -0
  155. package/docs/errors/fetch-in-workflow.mdx +4 -0
  156. package/docs/errors/hook-conflict.mdx +60 -4
  157. package/docs/errors/index.mdx +1 -35
  158. package/docs/errors/node-js-module-in-workflow.mdx +4 -0
  159. package/docs/errors/replay-divergence.mdx +27 -0
  160. package/docs/errors/runtime-decryption-failed.mdx +77 -0
  161. package/docs/errors/serialization-failed.mdx +4 -0
  162. package/docs/errors/start-invalid-workflow-function.mdx +4 -0
  163. package/docs/errors/step-executed-multiple-times.mdx +23 -0
  164. package/docs/errors/step-not-registered.mdx +1 -1
  165. package/docs/errors/timeout-in-workflow.mdx +4 -0
  166. package/docs/errors/webhook-response-not-sent.mdx +4 -0
  167. package/docs/foundations/cancellation.mdx +459 -0
  168. package/docs/foundations/errors-and-retries.mdx +7 -3
  169. package/docs/foundations/hooks.mdx +29 -0
  170. package/docs/foundations/idempotency.mdx +243 -11
  171. package/docs/foundations/index.mdx +1 -23
  172. package/docs/foundations/meta.json +3 -1
  173. package/docs/foundations/serialization.mdx +77 -41
  174. package/docs/foundations/starting-workflows.mdx +79 -2
  175. package/docs/foundations/streaming.mdx +14 -23
  176. package/docs/foundations/versioning.mdx +263 -0
  177. package/docs/getting-started/astro.mdx +12 -2
  178. package/docs/getting-started/express.mdx +6 -2
  179. package/docs/getting-started/fastify.mdx +6 -2
  180. package/docs/getting-started/hono.mdx +6 -2
  181. package/docs/getting-started/index.mdx +16 -10
  182. package/docs/getting-started/meta.json +4 -1
  183. package/docs/getting-started/nestjs.mdx +77 -3
  184. package/docs/getting-started/next.mdx +12 -6
  185. package/docs/getting-started/nitro.mdx +28 -2
  186. package/docs/getting-started/nuxt.mdx +6 -2
  187. package/docs/getting-started/python.mdx +24 -18
  188. package/docs/getting-started/react-router/index.mdx +33 -0
  189. package/docs/getting-started/react-router/meta.json +5 -0
  190. package/docs/getting-started/react-router/v7.mdx +237 -0
  191. package/docs/getting-started/react-router/v8.mdx +232 -0
  192. package/docs/getting-started/sveltekit.mdx +12 -2
  193. package/docs/getting-started/tanstack-start.mdx +245 -0
  194. package/docs/getting-started/vite.mdx +6 -2
  195. package/docs/how-it-works/cancellation.mdx +287 -0
  196. package/docs/how-it-works/code-transform.mdx +21 -17
  197. package/docs/how-it-works/encryption.mdx +5 -5
  198. package/docs/how-it-works/event-sourcing.mdx +6 -6
  199. package/docs/how-it-works/framework-integrations.mdx +96 -337
  200. package/docs/how-it-works/meta.json +2 -1
  201. package/docs/internal/index.mdx +21 -0
  202. package/docs/internal/meta.json +10 -0
  203. package/docs/internal/nitro-native-build.mdx +38 -0
  204. package/docs/internal/nitro-web-ui.mdx +24 -0
  205. package/docs/internal/serializable-abort-controller.mdx +148 -0
  206. package/docs/meta.json +2 -2
  207. package/docs/observability/attributes.mdx +112 -0
  208. package/docs/observability/index.mdx +20 -1
  209. package/docs/observability/meta.json +1 -1
  210. package/docs/observability/tracing.mdx +124 -0
  211. package/docs/testing/index.mdx +2 -2
  212. package/package.json +17 -13
  213. package/docs/api-reference/workflow-api/world/meta.json +0 -4
  214. package/docs/api-reference/workflow-api/world/observability.mdx +0 -164
  215. package/docs/cookbook/advanced/distributed-abort-controller.mdx +0 -318
  216. package/docs/deploying/building-a-world.mdx +0 -251
  217. package/docs/deploying/meta.json +0 -4
  218. package/docs/deploying/world/local-world.mdx +0 -84
  219. package/docs/deploying/world/meta.json +0 -4
  220. package/docs/deploying/world/postgres-world.mdx +0 -222
  221. package/docs/deploying/world/vercel-world.mdx +0 -179
  222. package/docs/migration-guides/index.mdx +0 -34
  223. package/docs/migration-guides/meta.json +0 -9
  224. package/docs/migration-guides/migrating-from-aws-step-functions.mdx +0 -363
  225. package/docs/migration-guides/migrating-from-inngest.mdx +0 -314
  226. package/docs/migration-guides/migrating-from-temporal.mdx +0 -318
  227. 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 for stable workflow IDs, keeping step I/O clean, and integration testing.
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[&quot;./workflows&quot;]`) 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 &quot;<pkg>/workflows&quot;` 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 Workflow ID Stability
117
+ ## Re-Exporting for Compiler Discovery
114
118
 
115
- Workflow SDK's compiler assigns each workflow function a stable ID based on its position in the source file that the build system processes. When a consumer imports a pre-built workflow from an npm package, the compiler never sees the original source it only sees the compiled output. This means workflow IDs won't match between the library's development environment and the consumer's app.
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. The build system then processes this file and assigns stable IDs.
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 build system assigns stable IDs
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 workflow compiler transforms this file, discovers the workflow and step functions from the library, and assigns IDs that are stable across deployments.
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 workflow run is replayed after a cold start, the runtime looks up functions by their compiler-assigned IDs. If the IDs don't exist (because the compiler never processed the library's source), replay fails.
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
- The re-export pattern ensures:
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
- 1. **Stable IDs** the compiler assigns IDs based on the consumer's source tree
136
- 2. **Replay safety** — IDs persist across deployments and cold starts
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-workflow-id-stability) above), the SWC plugin transforms them into durable-execution glue. When they're not compiled — plain Node, plain tests, a consumer without the runtime — they are just string expression statements and run as no-ops.
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* `getWorkflowMetadata()` throws (call site isn't inside a workflow run), fall through to the standalone path.
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 getWorkflowRunId(): Promise<string | null> { // [!code highlight]
284
+ async function getWorkflowStepId(): Promise<string | null> { // [!code highlight]
277
285
  try {
278
286
  const wf = await import("workflow");
279
- const { workflowRunId } = wf.getWorkflowMetadata();
280
- return workflowRunId;
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 run ID as a Stripe idempotency key when available, and a fresh UUID otherwise:
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 runId = await getWorkflowRunId();
295
- const idempotencyKey = runId ?? crypto.randomUUID(); // [!code highlight]
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 run, the step wrapping this utility gets a stable idempotency key across replays — Stripe dedupes retries for free. When called from a plain Node.js process, it behaves like any other function and a fresh UUID is generated.
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/api-reference/workflow/use-workflow) — declares the orchestrator function
334
- - [`"use step"`](/docs/api-reference/workflow/use-step) — marks functions for durable execution
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/api-reference/workflow/use-step
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 &quot;use step&quot; which constructs and returns the object at execution time, for example `export function openai(...args) { return async () => { &quot;use step&quot;; 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/api-reference/workflow/use-step) — marks a function for extraction and serialization
145
- - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) — declares the orchestrator function
146
- - [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — accepts a model factory for durable AI agent streaming
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: &quot;latest&quot; })` 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 — either immediately via run.cancel() or gracefully via a stop signal hook.
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: Two patterns for cancelling a running agent Hard Cancellation via getRun(runId).cancel() for forced termination, or Stop Signal via a hook + Promise.race for a clean exit with cleanup and final stream notification.
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
- Cancel a running agent from the outside — for example, a "Stop" button in a chat UI, an admin cancellation endpoint, or a timeout fallback. Two patterns are available depending on whether you need the agent to exit cleanly or just need the run to stop: **Hard Cancellation** via `getRun(runId).cancel()` for immediate forced termination, or **Stop Signal** via a hook + `Promise.race` for a graceful exit that runs cleanup and notifies streaming clients before returning.
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
- ## When to use this
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
- **Limitation:** This pattern does not cancel the underlying model stream. The agent step writing to the writable continues running in the background until it completes — tokens generated after the stop signal are still produced (and billed by your model provider). What this pattern *does* is exit the workflow function as soon as the hook fires and emit a `data-stopped` part so the client can stop rendering. For hard cross-process cancellation that signals the inner step to bail out, see [Distributed Abort Controller](/cookbook/advanced/distributed-abort-controller).
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
- ### Example
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 }) { // [!code highlight]
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 hook = stopHook.create({ token: `stop:${workflowRunId}` }); // [!code highlight]
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([ // [!code highlight]
78
+ const result = await Promise.race([
125
79
  agent
126
- .stream({ messages, writable: getWritable<UIMessageChunk>(), maxSteps: 15 })
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 }) => ({ type: "stopped" as const, reason })), // [!code highlight]
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 }); // [!code highlight]
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}`, { // [!code highlight]
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. A hook is created with token `stop:${workflowRunId}` when the workflow starts
184
- 2. `Promise.race` runs the agent stream and the stop hook concurrently
185
- 3. When the stop API resumes the hook, the race resolves immediately — the workflow exits
186
- 4. Before returning, `emitStopSignal` writes a `data-stopped` part to the stream so the client knows the agent was stopped (not just disconnected)
187
- 5. The client detects `data-stopped` and updates the UI accordingly
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 a stop notification to the client
204
- * [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — the agent that gets raced against the stop hook
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`)