workflow 5.0.0-beta.2 → 5.0.0-beta.20

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 (169) hide show
  1. package/dist/api-workflow.d.ts +1 -1
  2. package/dist/api-workflow.d.ts.map +1 -1
  3. package/dist/api-workflow.js +2 -2
  4. package/dist/api.d.ts +5 -1
  5. package/dist/api.d.ts.map +1 -1
  6. package/dist/api.js +14 -2
  7. package/dist/internal/builtins.d.ts +17 -0
  8. package/dist/internal/builtins.d.ts.map +1 -1
  9. package/dist/internal/builtins.js +65 -1
  10. package/dist/observability.d.ts +1 -1
  11. package/dist/observability.js +2 -2
  12. package/dist/runtime.d.ts +1 -1
  13. package/dist/runtime.d.ts.map +1 -1
  14. package/dist/runtime.js +2 -2
  15. package/docs/ai/index.mdx +27 -23
  16. package/docs/api-reference/index.mdx +24 -0
  17. package/docs/api-reference/meta.json +8 -0
  18. package/docs/api-reference/vitest/index.mdx +28 -7
  19. package/docs/api-reference/workflow/create-hook.mdx +38 -0
  20. package/docs/api-reference/workflow/create-webhook.mdx +1 -0
  21. package/docs/api-reference/workflow/experimental-set-attributes.mdx +65 -0
  22. package/docs/api-reference/workflow/fetch.mdx +5 -0
  23. package/docs/api-reference/workflow/index.mdx +3 -0
  24. package/docs/api-reference/workflow-ai/durable-agent.mdx +7 -45
  25. package/docs/api-reference/workflow-api/get-hook-by-token.mdx +7 -0
  26. package/docs/api-reference/workflow-api/get-run.mdx +6 -0
  27. package/docs/api-reference/workflow-api/index.mdx +6 -8
  28. package/docs/api-reference/workflow-api/resume-hook.mdx +57 -0
  29. package/docs/api-reference/workflow-api/start.mdx +13 -5
  30. package/docs/api-reference/workflow-astro/index.mdx +18 -0
  31. package/docs/api-reference/workflow-astro/meta.json +4 -0
  32. package/docs/api-reference/workflow-astro/workflow.mdx +45 -0
  33. package/docs/api-reference/workflow-errors/hook-conflict-error.mdx +60 -0
  34. package/docs/api-reference/workflow-errors/index.mdx +85 -0
  35. package/docs/api-reference/workflow-errors/meta.json +5 -0
  36. package/docs/api-reference/workflow-errors/run-not-supported-error.mdx +58 -0
  37. package/docs/api-reference/workflow-errors/workflow-error.mdx +52 -0
  38. package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +16 -6
  39. package/docs/api-reference/workflow-errors/workflow-run-not-completed-error.mdx +58 -0
  40. package/docs/api-reference/workflow-errors/workflow-runtime-error.mdx +58 -0
  41. package/docs/api-reference/workflow-nest/configure-workflow-controller.mdx +33 -0
  42. package/docs/api-reference/workflow-nest/index.mdx +31 -0
  43. package/docs/api-reference/workflow-nest/meta.json +9 -0
  44. package/docs/api-reference/workflow-nest/nest-local-builder.mdx +64 -0
  45. package/docs/api-reference/workflow-nest/workflow-controller.mdx +40 -0
  46. package/docs/api-reference/workflow-nest/workflow-module.mdx +74 -0
  47. package/docs/api-reference/workflow-next/with-workflow.mdx +34 -2
  48. package/docs/api-reference/workflow-nitro/index.mdx +59 -0
  49. package/docs/api-reference/workflow-nuxt/index.mdx +47 -0
  50. package/docs/api-reference/workflow-observability/hydrate-data.mdx +35 -0
  51. package/docs/api-reference/workflow-observability/hydrate-resource-io.mdx +62 -0
  52. package/docs/api-reference/workflow-observability/index.mdx +64 -0
  53. package/docs/api-reference/workflow-observability/meta.json +11 -0
  54. package/docs/api-reference/workflow-observability/observability-revivers.mdx +50 -0
  55. package/docs/api-reference/workflow-observability/parse-class-name.mdx +41 -0
  56. package/docs/api-reference/workflow-observability/parse-step-name.mdx +40 -0
  57. package/docs/api-reference/workflow-observability/parse-workflow-name.mdx +55 -0
  58. package/docs/api-reference/workflow-runtime/create-world.mdx +39 -0
  59. package/docs/api-reference/workflow-runtime/get-world-handlers.mdx +44 -0
  60. package/docs/api-reference/{workflow-api → workflow-runtime}/get-world.mdx +7 -10
  61. package/docs/api-reference/workflow-runtime/health-check.mdx +50 -0
  62. package/docs/api-reference/workflow-runtime/index.mdx +43 -0
  63. package/docs/api-reference/workflow-runtime/meta.json +12 -0
  64. package/docs/api-reference/workflow-runtime/set-world.mdx +49 -0
  65. package/docs/api-reference/workflow-runtime/workflow-entrypoint.mdx +42 -0
  66. package/docs/api-reference/{workflow-api → workflow-runtime}/world/index.mdx +5 -8
  67. package/docs/api-reference/workflow-runtime/world/meta.json +4 -0
  68. package/docs/api-reference/{workflow-api → workflow-runtime}/world/queue.mdx +2 -2
  69. package/docs/api-reference/{workflow-api → workflow-runtime}/world/storage.mdx +11 -4
  70. package/docs/api-reference/{workflow-api → workflow-runtime}/world/streams.mdx +2 -2
  71. package/docs/api-reference/workflow-serde/index.mdx +0 -1
  72. package/docs/api-reference/workflow-serde/workflow-deserialize.mdx +1 -2
  73. package/docs/api-reference/workflow-serde/workflow-serialize.mdx +1 -2
  74. package/docs/api-reference/workflow-sveltekit/index.mdx +18 -0
  75. package/docs/api-reference/workflow-sveltekit/meta.json +4 -0
  76. package/docs/api-reference/workflow-sveltekit/workflow-plugin.mdx +42 -0
  77. package/docs/api-reference/workflow-vite/index.mdx +18 -0
  78. package/docs/api-reference/workflow-vite/meta.json +4 -0
  79. package/docs/api-reference/workflow-vite/workflow.mdx +48 -0
  80. package/docs/changelog/attributes-mvp.mdx +380 -0
  81. package/docs/changelog/eager-processing.mdx +269 -0
  82. package/docs/changelog/index.mdx +2 -1
  83. package/docs/changelog/lazy-event-creation.md +127 -0
  84. package/docs/changelog/meta.json +6 -1
  85. package/docs/changelog/resilient-start.mdx +31 -283
  86. package/docs/cookbook/advanced/child-workflows.mdx +315 -0
  87. package/docs/cookbook/advanced/meta.json +2 -3
  88. package/docs/cookbook/advanced/publishing-libraries.mdx +87 -29
  89. package/docs/cookbook/advanced/serializable-steps.mdx +17 -5
  90. package/docs/cookbook/advanced/upgrading-workflows.mdx +195 -0
  91. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +156 -0
  92. package/docs/cookbook/agent-patterns/durable-agent.mdx +11 -184
  93. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +150 -173
  94. package/docs/cookbook/agent-patterns/meta.json +1 -7
  95. package/docs/cookbook/common-patterns/batching.mdx +44 -118
  96. package/docs/cookbook/common-patterns/idempotency.mdx +36 -52
  97. package/docs/cookbook/common-patterns/meta.json +4 -4
  98. package/docs/cookbook/common-patterns/rate-limiting.mdx +1 -1
  99. package/docs/cookbook/common-patterns/saga.mdx +128 -33
  100. package/docs/cookbook/common-patterns/scheduling.mdx +77 -193
  101. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +155 -0
  102. package/docs/cookbook/common-patterns/timeouts.mdx +100 -0
  103. package/docs/cookbook/common-patterns/workflow-composition.mdx +117 -0
  104. package/docs/cookbook/index.mdx +14 -17
  105. package/docs/cookbook/integrations/ai-sdk.mdx +330 -142
  106. package/docs/cookbook/integrations/chat-sdk.mdx +264 -151
  107. package/docs/cookbook/integrations/sandbox.mdx +482 -81
  108. package/docs/cookbook/meta.json +1 -1
  109. package/docs/deploying/building-a-world.mdx +1 -1
  110. package/docs/deploying/world/postgres-world.mdx +5 -3
  111. package/docs/deploying/world/vercel-world.mdx +2 -0
  112. package/docs/errors/abort-signal-timeout-in-workflow.mdx +80 -0
  113. package/docs/errors/corrupted-event-log.mdx +5 -5
  114. package/docs/errors/hook-conflict.mdx +56 -4
  115. package/docs/errors/index.mdx +9 -0
  116. package/docs/errors/replay-divergence.mdx +27 -0
  117. package/docs/errors/runtime-decryption-failed.mdx +77 -0
  118. package/docs/errors/step-executed-multiple-times.mdx +23 -0
  119. package/docs/errors/step-not-registered.mdx +1 -1
  120. package/docs/foundations/cancellation.mdx +459 -0
  121. package/docs/foundations/errors-and-retries.mdx +7 -3
  122. package/docs/foundations/hooks.mdx +29 -0
  123. package/docs/foundations/idempotency.mdx +236 -11
  124. package/docs/foundations/index.mdx +3 -3
  125. package/docs/foundations/meta.json +3 -2
  126. package/docs/foundations/serialization.mdx +78 -42
  127. package/docs/foundations/starting-workflows.mdx +6 -2
  128. package/docs/foundations/streaming.mdx +14 -23
  129. package/docs/foundations/versioning.mdx +263 -0
  130. package/docs/getting-started/astro.mdx +6 -0
  131. package/docs/getting-started/index.mdx +6 -7
  132. package/docs/getting-started/meta.json +1 -0
  133. package/docs/getting-started/nestjs.mdx +9 -0
  134. package/docs/getting-started/next.mdx +5 -3
  135. package/docs/getting-started/nitro.mdx +22 -0
  136. package/docs/getting-started/sveltekit.mdx +6 -0
  137. package/docs/getting-started/tanstack-start.mdx +241 -0
  138. package/docs/how-it-works/cancellation.mdx +287 -0
  139. package/docs/how-it-works/code-transform.mdx +2 -2
  140. package/docs/how-it-works/encryption.mdx +2 -2
  141. package/docs/how-it-works/event-sourcing.mdx +2 -2
  142. package/docs/how-it-works/meta.json +2 -1
  143. package/docs/internal/index.mdx +21 -0
  144. package/docs/internal/meta.json +10 -0
  145. package/docs/internal/nitro-native-build.mdx +38 -0
  146. package/docs/internal/nitro-web-ui.mdx +24 -0
  147. package/docs/internal/serializable-abort-controller.mdx +148 -0
  148. package/docs/migration-guides/migrating-from-aws-step-functions.mdx +63 -16
  149. package/docs/migration-guides/migrating-from-inngest.mdx +44 -22
  150. package/docs/migration-guides/migrating-from-temporal.mdx +43 -14
  151. package/docs/migration-guides/migrating-from-trigger-dev.mdx +59 -27
  152. package/docs/observability/attributes.mdx +87 -0
  153. package/docs/observability/index.mdx +25 -1
  154. package/docs/observability/meta.json +1 -1
  155. package/docs/observability/tracing.mdx +106 -0
  156. package/docs/testing/index.mdx +2 -2
  157. package/package.json +14 -13
  158. package/docs/api-reference/workflow-api/world/meta.json +0 -4
  159. package/docs/api-reference/workflow-api/world/observability.mdx +0 -164
  160. package/docs/cookbook/advanced/custom-serialization.mdx +0 -168
  161. package/docs/cookbook/advanced/durable-objects.mdx +0 -148
  162. package/docs/cookbook/advanced/isomorphic-packages.mdx +0 -145
  163. package/docs/cookbook/agent-patterns/stop-workflow.mdx +0 -216
  164. package/docs/cookbook/agent-patterns/tool-orchestration.mdx +0 -255
  165. package/docs/cookbook/agent-patterns/tool-streaming.mdx +0 -181
  166. package/docs/cookbook/common-patterns/child-workflows.mdx +0 -372
  167. package/docs/cookbook/common-patterns/content-router.mdx +0 -207
  168. package/docs/cookbook/common-patterns/fan-out.mdx +0 -208
  169. package/docs/foundations/common-patterns.mdx +0 -265
@@ -1,204 +1,392 @@
1
1
  ---
2
2
  title: AI SDK
3
- description: Use AI SDK model providers, tool calling, and streaming inside durable workflows.
3
+ description: Use AI SDK's streamText directly inside durable workflows when you need the raw AI SDK API or a per-turn durability boundary.
4
4
  type: guide
5
- summary: Turn any AI SDK model call into a retryable, observable workflow step with built-in streaming.
5
+ summary: Use streamText() inside a workflow when the durability boundary is an entire user turn, or when you need AI SDK APIs not exposed by DurableAgent. Individual tool calls and LLM calls inside a turn are not separately durable.
6
6
  related:
7
7
  - /docs/ai
8
+ - /docs/ai/chat-session-modeling
8
9
  - /docs/ai/defining-tools
9
10
  - /docs/ai/resumable-streams
10
11
  - /docs/api-reference/workflow-ai/durable-agent
11
12
  ---
12
13
 
13
- Workflow SDK integrates with [AI SDK](https://ai-sdk.dev) through the `@workflow/ai` package. This turns your LLM calls and tool executions into durable, retryable steps with built-in streaming and observability.
14
+ [AI SDK](https://ai-sdk.dev/) is Vercel's framework-agnostic TypeScript toolkit for building AI-powered apps and agents — unified provider access, streaming, tool calling, structured output, and UI hooks. Workflow SDK complements it by making the multi-turn loop durable: the conversation state, hooks, and per-turn responses survive restarts and timeouts. Note that in this pattern the durability boundary is the entire turn — individual tool calls inside a turn are **not** durable on their own (see [Pitfalls](#tools-are-not-individually-durable) below).
14
15
 
15
- ## What It Enables
16
+ For the full AI SDK reference (providers, `streamText`, `generateObject`, `useChat`, tool calling, etc.) see the [AI SDK docs](https://ai-sdk.dev/docs). This page covers the Workflow-specific integration points.
16
17
 
17
- - **Durable LLM calls** -- Model invocations become steps that survive crashes and cold starts
18
- - **Any model provider** -- Use OpenAI, Anthropic, Google, Bedrock, or any AI SDK-compatible provider through [Vercel Gateway](https://vercel.com/docs/gateway) or direct provider configuration
19
- - **Tool durability** -- Tool executions become steps with automatic retries and event logging
20
- - **Resumable streaming** -- Clients reconnect mid-stream without losing data
18
+ <Callout type="info">
19
+ For most agent use cases, prefer [`DurableAgent`](/cookbook/agent-patterns/durable-agent), which implements the same agent loop as [`streamText`](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text), manages tool calling automatically, and runs tools at workflow scope — each tool can be marked `"use step"` for per-call durability and retries, or stay at workflow level to use primitives like `sleep()` and hooks. Use this page's raw `streamText()` pattern when you want the exact AI SDK API (for example `toUIMessageStream()`, `onChunk`, or `generateText`), or when the durability boundary should be an entire user turn in one step — accepting that tool calls inside that turn are not individually durable.
20
+ </Callout>
21
21
 
22
- ## When to Use
22
+ ## When to use streamText directly
23
23
 
24
- Use this integration when your application calls an LLM and needs:
24
+ Use [`streamText()`](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text) instead of `DurableAgent` when you need:
25
25
 
26
- - Reliability for long-running agent loops (multi-step tool calling)
27
- - Automatic retry on transient model API failures
28
- - Stream resumption after disconnects
29
- - Observability into each model call and tool execution
26
+ * **The raw AI SDK API** — `streamText().toUIMessageStream()`, `onChunk`, `smoothStream`, or other options that map directly to the [`streamText`](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text) return value rather than `DurableAgent.stream()`
27
+ * **Per-turn durability** wrap the entire agent response (model + tools) in a single `"use step"` function so one user turn is the atomic retry unit; useful when you want all tool calls inside a turn to re-execute together
28
+ * **Custom multi-turn orchestration** manual hook loops, per-turn stream slicing (`sliceUntilFinish`), or other workflow patterns shown below that don't map cleanly to `DurableAgent`
30
29
 
31
- ## DurableAgent with Model Providers
30
+ `DurableAgent` already supports `stopWhen`, `prepareStep`, `onStepFinish`, structured output (`experimental_output`), per-step model switching, and [provider options](https://ai-sdk.dev/docs/ai-sdk-core/provider-options). See the [DurableAgent reference](/docs/api-reference/workflow-ai/durable-agent).
32
31
 
33
- The `DurableAgent` wraps AI SDK's streaming interface. Pass any model string supported by [Vercel Gateway](https://vercel.com/docs/gateway) or a provider-specific model ID.
32
+ ## Multi-turn pattern
34
33
 
35
- ```typescript title="workflows/research.ts" lineNumbers
36
- import { DurableAgent } from "@workflow/ai/agent";
37
- import { convertToModelMessages, type UIMessage, type UIMessageChunk } from "ai";
38
- import { getWritable } from "workflow";
39
- import z from "zod/v4";
34
+ One workflow run = one full conversation. The workflow suspends between turns on a hook and resumes when the next user message arrives. Conversation state, tool history, and intermediate computation all live inside the run.
40
35
 
41
- async function searchWeb(input: { query: string }): Promise<{ results: string[] }> {
42
- "use step";
43
- const response = await fetch(
44
- `https://api.example.com/search?q=${encodeURIComponent(input.query)}`
45
- );
46
- const data = await response.json();
47
- return { results: data.items.map((item: { title: string }) => item.title) };
36
+ <Callout type="info">
37
+ Because the conversation is one workflow run, it stays on the deployment that started it. If each turn should run on the latest deployment while preserving selected state or streams, see [Versioning](/docs/foundations/versioning) for the child-run continuation pattern.
38
+ </Callout>
39
+
40
+ <Tabs items={['Workflow', 'API Route', 'Client']}>
41
+
42
+ <Tab value="Workflow">
43
+
44
+ ```typescript title="workflows/support.ts" lineNumbers
45
+ import { streamText, stepCountIs } from "ai";
46
+ import { defineHook, getWritable, getWorkflowMetadata } from "workflow";
47
+ import type { ModelMessage, UIMessageChunk } from "ai";
48
+ import { z } from "zod";
49
+
50
+ const MAX_TURNS = 20;
51
+
52
+ export const turnHook = defineHook({ // [!code highlight]
53
+ schema: z.object({ message: z.string() }),
54
+ });
55
+
56
+ // `streamText` runs tool executes inside `runTurn` (a step), so tool calls
57
+ // are not individually durable — the entire turn retries together. See
58
+ // "Tools are not individually durable" below. Make side-effectful tools idempotent.
59
+ async function lookupOrder({ orderId }: { orderId: string }) {
60
+ const res = await fetch(`https://api.store.com/orders/${orderId}`);
61
+ return res.json();
48
62
  }
49
63
 
50
- async function summarize(input: { text: string }): Promise<{ summary: string }> {
51
- "use step";
52
- // Each step is individually retried on failure
53
- const response = await fetch("https://api.example.com/summarize", {
64
+ async function processRefund({ orderId, reason }: { orderId: string; reason: string }) {
65
+ const res = await fetch("https://api.store.com/refunds", {
54
66
  method: "POST",
55
- body: JSON.stringify({ text: input.text }),
67
+ body: JSON.stringify({ orderId, reason }),
68
+ });
69
+ return res.json();
70
+ }
71
+
72
+ const TOOLS = {
73
+ lookupOrder: {
74
+ description: "Look up an order by ID",
75
+ inputSchema: z.object({ orderId: z.string() }),
76
+ execute: lookupOrder,
77
+ },
78
+ processRefund: {
79
+ description: "Process a refund",
80
+ inputSchema: z.object({ orderId: z.string(), reason: z.string() }),
81
+ execute: processRefund,
82
+ },
83
+ };
84
+
85
+ // Per-turn step — streams one agent response to the durable writable // [!code highlight]
86
+ async function runTurn(messages: ModelMessage[]) {
87
+ "use step";
88
+
89
+ const result = streamText({
90
+ model: "anthropic/claude-haiku-4.5",
91
+ system: "You are a customer support agent.",
92
+ messages,
93
+ tools: TOOLS,
94
+ stopWhen: stepCountIs(8),
56
95
  });
57
- const data = await response.json();
58
- return { summary: data.summary };
96
+
97
+ const writable = getWritable<UIMessageChunk>();
98
+ // preventClose keeps the durable writable open so the next turn can // write to it. Each turn still emits its own start + finish chunks.
99
+ await result.toUIMessageStream().pipeTo(writable, { preventClose: true }); // [!code highlight]
100
+
101
+ const response = await result.response;
102
+ return { responseMessages: response.messages };
59
103
  }
60
104
 
61
- export async function researchAgent(messages: UIMessage[]) {
105
+ export async function supportWorkflow(initialMessages: ModelMessage[]) {
62
106
  "use workflow";
63
107
 
64
- const agent = new DurableAgent({ // [!code highlight]
65
- model: "anthropic/claude-sonnet-4-20250514",
66
- instructions: "You are a research assistant. Search the web and summarize findings.",
67
- tools: {
68
- searchWeb: {
69
- description: "Search the web for information",
70
- inputSchema: z.object({
71
- query: z.string().describe("The search query"),
72
- }),
73
- execute: searchWeb,
74
- },
75
- summarize: {
76
- description: "Summarize a block of text",
77
- inputSchema: z.object({
78
- text: z.string().describe("The text to summarize"),
79
- }),
80
- execute: summarize,
81
- },
108
+ const { workflowRunId } = getWorkflowMetadata();
109
+ // Create the hook once, outside the loop — same token = HookConflictError // [!code highlight]
110
+ const hook = turnHook.create({ token: workflowRunId }); // [!code highlight]
111
+ let allMessages = initialMessages;
112
+
113
+ for (let turn = 0; turn < MAX_TURNS; turn++) {
114
+ const { responseMessages } = await runTurn(allMessages);
115
+ allMessages = [...allMessages, ...responseMessages];
116
+
117
+ const { message } = await hook; // [!code highlight] suspend until next user message
118
+ if (message === "/done") break;
119
+
120
+ allMessages = [...allMessages, { role: "user", content: message }];
121
+ }
122
+
123
+ return { turns: MAX_TURNS };
124
+ }
125
+ ```
126
+
127
+ </Tab>
128
+
129
+ <Tab value="API Route">
130
+
131
+ One endpoint handles first turn, follow-ups, and the `/done` exit. The client sends `runId` in the body to distinguish first vs follow-up.
132
+
133
+ <Callout type="info">
134
+ The first turn calls `start()` and then returns the `runId`. If your client or platform can retry that first request before it receives and stores the `runId`, use an atomic conversation or request key before starting the workflow. See [Run idempotency](/docs/foundations/idempotency#run-idempotency).
135
+ </Callout>
136
+
137
+ ```typescript title="app/api/support/route.ts" lineNumbers
138
+ import type { UIMessage, UIMessageChunk } from "ai";
139
+ import { convertToModelMessages, createUIMessageStreamResponse } from "ai";
140
+ import { start, getRun } from "workflow/api";
141
+ import { supportWorkflow, turnHook } from "@/workflows/support";
142
+
143
+ // Pump the durable stream until this turn's `finish` chunk, then close // the HTTP response. The source reader is released (not cancelled) so the
144
+ // workflow's durable stream keeps flowing for the next turn.
145
+ function sliceUntilFinish( // [!code highlight]
146
+ source: ReadableStream<UIMessageChunk>
147
+ ): ReadableStream<UIMessageChunk> {
148
+ return new ReadableStream<UIMessageChunk>({
149
+ async start(controller) {
150
+ const reader = source.getReader();
151
+ try {
152
+ while (true) {
153
+ const { done, value } = await reader.read();
154
+ if (done) break;
155
+ controller.enqueue(value);
156
+ if (value.type === "finish") break; // [!code highlight]
157
+ }
158
+ controller.close();
159
+ } catch (e) {
160
+ controller.error(e);
161
+ } finally {
162
+ reader.releaseLock();
163
+ }
82
164
  },
83
165
  });
166
+ }
84
167
 
85
- const result = await agent.stream({ // [!code highlight]
86
- messages: await convertToModelMessages(messages),
87
- writable: getWritable<UIMessageChunk>(),
168
+ // `/done` exits the workflow without emitting chunks. Return a synthetic
169
+ // start+finish so useChat's lifecycle terminates cleanly.
170
+ function emptyTurnStream(): ReadableStream<UIMessageChunk> {
171
+ return new ReadableStream<UIMessageChunk>({
172
+ start(controller) {
173
+ controller.enqueue({ type: "start", messageId: crypto.randomUUID() });
174
+ controller.enqueue({ type: "finish" });
175
+ controller.close();
176
+ },
88
177
  });
178
+ }
179
+
180
+ export async function POST(req: Request) {
181
+ const { messages, runId }: { messages: UIMessage[]; runId?: string } =
182
+ await req.json();
183
+ const modelMessages = await convertToModelMessages(messages);
184
+
185
+ // Follow-up turn: resume hook, return stream starting AFTER the last turn // [!code highlight]
186
+ if (runId) {
187
+ try {
188
+ const run = getRun(runId);
189
+
190
+ // Snapshot tail before resuming so our slice only contains this turn // [!code highlight]
191
+ const probe = run.getReadable();
192
+ const tailIndex = await probe.getTailIndex();
193
+ await probe.cancel();
194
+
195
+ const lastUser = modelMessages.filter((m) => m.role === "user").at(-1);
196
+ const text =
197
+ typeof lastUser?.content === "string"
198
+ ? lastUser.content
199
+ : Array.isArray(lastUser?.content)
200
+ ? lastUser.content
201
+ .filter((p): p is { type: "text"; text: string } =>
202
+ "type" in p && p.type === "text"
203
+ )
204
+ .map((p) => p.text)
205
+ .join("")
206
+ : "";
207
+
208
+ await turnHook.resume(runId, { message: text }); // [!code highlight]
209
+
210
+ if (text === "/done") {
211
+ return createUIMessageStreamResponse({
212
+ stream: emptyTurnStream(),
213
+ headers: { "x-workflow-run-id": runId },
214
+ });
215
+ }
216
+
217
+ const stream = sliceUntilFinish(
218
+ run.getReadable({ startIndex: tailIndex + 1 }) // [!code highlight]
219
+ );
220
+
221
+ return createUIMessageStreamResponse({
222
+ stream,
223
+ headers: { "x-workflow-run-id": runId },
224
+ });
225
+ } catch (e: unknown) {
226
+ const msg = e instanceof Error ? e.message.toLowerCase() : "";
227
+ if (!msg.includes("not found") && !msg.includes("expired")) throw e;
228
+ // Stale runId — fall through to start fresh
229
+ }
230
+ }
231
+
232
+ // First turn: start a new workflow // [!code highlight]
233
+ const run = await start(supportWorkflow, [modelMessages]);
234
+ const stream = sliceUntilFinish(run.readable);
89
235
 
90
- return { messages: result.messages };
236
+ return createUIMessageStreamResponse({
237
+ stream,
238
+ headers: { "x-workflow-run-id": run.runId },
239
+ });
91
240
  }
92
241
  ```
93
242
 
94
- ### Using Different Providers
243
+ </Tab>
95
244
 
96
- #### Vercel Gateway (string model IDs)
245
+ <Tab value="Client">
97
246
 
98
- All string model IDs route through [Vercel Gateway](https://vercel.com/docs/gateway). Switch providers by changing the model string -- no other code changes required.
247
+ Store the `runId` in a ref and pass it in the body of every follow-up. `WorkflowChatTransport` forwards it for you.
99
248
 
100
- {/* @skip-typecheck - illustrative snippets with intentional redeclarations */}
101
- ```typescript
102
- // All string model IDs route through Vercel Gateway
103
- const agent = new DurableAgent({ model: "anthropic/claude-sonnet-4-20250514" });
104
- const agent = new DurableAgent({ model: "openai/gpt-4o" });
105
- const agent = new DurableAgent({ model: "google/gemini-2.5-pro" });
106
- const agent = new DurableAgent({ model: "bedrock/claude-haiku-4-5-20251001-v1" });
249
+ ```tsx title="components/support-chat.tsx" lineNumbers
250
+ "use client";
251
+
252
+ import { useChat } from "@ai-sdk/react";
253
+ import { WorkflowChatTransport } from "@workflow/ai";
254
+ import { useMemo, useRef, useState } from "react";
255
+
256
+ export function SupportChat() {
257
+ const [input, setInput] = useState("");
258
+ const runIdRef = useRef<string | null>(null); // [!code highlight]
259
+
260
+ const transport = useMemo(
261
+ () =>
262
+ new WorkflowChatTransport({
263
+ api: "/api/support",
264
+ prepareSendMessagesRequest: ({ messages, body }) => ({
265
+ body: { ...body, messages, runId: runIdRef.current }, // [!code highlight]
266
+ }),
267
+ onChatSendMessage: (response) => {
268
+ const id = response.headers.get("x-workflow-run-id");
269
+ if (id) runIdRef.current = id; // [!code highlight]
270
+ },
271
+ }),
272
+ []
273
+ );
274
+
275
+ const { messages, sendMessage, status } = useChat({ transport });
276
+ const busy = status === "streaming" || status === "submitted";
277
+
278
+ return (
279
+ <form
280
+ onSubmit={(e) => {
281
+ e.preventDefault();
282
+ if (busy || !input.trim()) return;
283
+ sendMessage({ text: input });
284
+ setInput("");
285
+ }}
286
+ >
287
+ {messages.map((m) => (
288
+ <div key={m.id}>{m.role}: {m.parts.map((p) => p.type === "text" ? p.text : "").join("")}</div>
289
+ ))}
290
+ <input value={input} onChange={(e) => setInput(e.target.value)} disabled={busy} />
291
+ </form>
292
+ );
293
+ }
107
294
  ```
108
295
 
109
- #### Direct Provider Access
296
+ </Tab>
110
297
 
111
- Import from a provider package to bypass Gateway and connect to the provider directly.
298
+ </Tabs>
112
299
 
113
- ```typescript
114
- import { DurableAgent } from "@workflow/ai/agent";
115
- import { openai } from "@workflow/ai/openai";
300
+ ## How it works
116
301
 
117
- const agent = new DurableAgent({ model: openai("gpt-4o") });
118
- ```
302
+ 1. **One workflow = one conversation.** The workflow loops on a hook, keeping `allMessages`, tool history, and state alive across turns.
303
+ 2. **`runTurn` is the durability boundary.** Each turn is one step. The model request and all tool calls inside it run as plain inline functions within that step. If anything throws mid-turn, the whole `runTurn` retries — individual tool calls are not separately durable. See [Pitfalls](#tools-are-not-individually-durable).
304
+ 3. **Hook is created once.** `turnHook.create({ token: workflowRunId })` outside the loop — calling it twice with the same token throws `HookConflictError`.
305
+ 4. **`preventClose: true`** on `pipeTo` keeps the durable writable open so the next turn can write to it.
306
+ 5. **`sliceUntilFinish`** in the API reads chunks until `type === "finish"`, then closes the HTTP response. The source reader is released — not cancelled — so the workflow stream keeps flowing.
307
+ 6. **`startIndex: tailIndex + 1`** gives each follow-up response only the new chunks, avoiding replay of previous turns.
308
+ 7. **`/done`** resumes the hook so the workflow exits cleanly, then returns a synthetic `start` + `finish` so `useChat` transitions out of "streaming".
119
309
 
120
- ### Provider-Specific Options
310
+ ## Pitfalls
121
311
 
122
- Pass provider options for features like reasoning or extended thinking.
312
+ Non-obvious correctness details worth knowing before adapting this pattern.
123
313
 
124
- ```typescript
125
- const agent = new DurableAgent({
126
- model: "anthropic/claude-sonnet-4-20250514",
127
- providerOptions: {
128
- anthropic: { thinking: { type: "enabled", budgetTokens: 10000 } },
129
- },
130
- // ...tools and instructions
131
- });
132
- ```
314
+ ### Tools are not individually durable
315
+
316
+ `streamText()` is invoked from inside `runTurn` (a `"use step"` function), and the AI SDK calls each tool by directly invoking its `execute` function in that same step. Even if a tool body has its own `"use step"` directive, that directive is a [no-op when called from another step](/docs/foundations/workflows-and-steps#step-functions) — the function just runs inline.
317
+
318
+ The consequences:
133
319
 
134
- ## Tool Functions with Steps
320
+ - The atomic retry unit is the entire `runTurn`, not the individual tool call.
321
+ - If `processRefund` succeeds and then the model call (or a later tool) throws, the whole turn retries, and `processRefund` will run again.
322
+ - Tool calls do not appear as separate entries in the event log or observability dashboard.
135
323
 
136
- Tool `execute` functions can optionally include steps by using the `"use step"` directive. When a tool is **not** a step, it runs inside the workflow context and can modify workflow state directly. When a tool **is** marked with `"use step"`, it becomes a durable step with:
324
+ **Mitigations:**
137
325
 
138
- - **Automatic retries** -- If a tool fails (network error, API timeout), the framework retries it
139
- - **Event logging** -- Inputs and outputs are recorded for observability and replay
140
- - **Idempotency** -- On replay after a crash, completed steps return their cached result
326
+ - Make side-effectful tool implementations idempotent — dedupe server-side on a stable key (e.g. `orderId`, an `Idempotency-Key` header, etc.).
327
+ - Or use [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent), which runs tools at workflow scope each tool can be marked `"use step"` to become its own durable, retryable step, or stay at workflow level to use primitives like `sleep()` and hooks.
141
328
 
329
+ ### Snapshot `tailIndex` *before* resuming the hook
330
+
331
+ {/* @skip-typecheck - fragment referencing variables from the surrounding multi-turn pattern */}
142
332
  ```typescript
143
- async function bookFlight(input: {
144
- origin: string;
145
- destination: string;
146
- date: string;
147
- }): Promise<{ confirmationId: string }> {
148
- "use step";
149
- // This call is retried on transient failures and its result is persisted
150
- const response = await fetch("https://api.airline.com/book", {
151
- method: "POST",
152
- headers: { "Content-Type": "application/json" },
153
- body: JSON.stringify(input),
154
- });
155
- if (!response.ok) throw new Error(`Booking failed: ${response.status}`);
156
- return response.json();
157
- }
333
+ const tailIndex = await probe.getTailIndex(); // [!code highlight] FIRST
334
+ await probe.cancel();
335
+ await turnHook.resume(runId, { message: text }); // [!code highlight] THEN
336
+ const stream = run.getReadable({ startIndex: tailIndex + 1 });
158
337
  ```
159
338
 
160
- ## Resumable Streaming
339
+ Reversing the order races the workflow: by the time you read `tailIndex`, the next turn has already written its `start` chunk, and your `startIndex + 1` skips past it.
161
340
 
162
- Use `WorkflowChatTransport` on the client to automatically reconnect to a workflow's stream if the connection drops.
341
+ ### Don't call `writable.close()` inside a workflow function
163
342
 
164
- ```typescript title="app/api/chat/route.ts" lineNumbers
165
- import { createUIMessageStreamResponse } from "ai";
166
- import { start } from "workflow/api";
167
- import { researchAgent } from "@/workflows/research";
343
+ I/O operations like closing streams must happen inside a `"use step"` function. Calling `writable.close()` directly in the workflow body throws `Not supported in workflow functions`. When the workflow returns, the runtime closes the underlying writable for you.
168
344
 
169
- export async function POST(request: Request) {
170
- const { messages } = await request.json();
171
- const run = await start(researchAgent, [messages]); // [!code highlight]
345
+ ### Don't use `TransformStream.terminate()` to slice the stream
172
346
 
173
- return createUIMessageStreamResponse({
174
- stream: run.readable, // [!code highlight]
175
- headers: { "x-workflow-run-id": run.runId },
176
- });
177
- }
178
- ```
347
+ A `TransformStream` with `controller.terminate()` on the `finish` chunk seems like the obvious fit for `sliceUntilFinish`, but throws `Invalid state: TransformStream has been terminated` when late-arriving chunks hit the transform callback. Manual pumping through a custom `ReadableStream` (as shown above) sidesteps the problem entirely.
179
348
 
180
- ```typescript title="components/chat.tsx" lineNumbers
181
- "use client";
349
+ ### Release the source reader, don't cancel it
182
350
 
183
- import { useChat } from "@ai-sdk/react";
184
- import { WorkflowChatTransport } from "@workflow/ai";
351
+ In `sliceUntilFinish`, use `reader.releaseLock()` in the `finally` block rather than `source.cancel()`. Cancelling propagates upstream and closes the durable writable, breaking the next turn. Releasing the lock just detaches our reader; the durable stream keeps flowing.
185
352
 
186
- export function Chat() {
187
- const chat = useChat({
188
- transport: new WorkflowChatTransport({ // [!code highlight]
189
- api: "/api/chat",
190
- }),
191
- });
353
+ ### Handle stale `runId` gracefully
192
354
 
193
- // Standard useChat usage -- reconnection is handled automatically
194
- return (
195
- <div>
196
- {chat.messages.map((m) => (
197
- <div key={m.id}>{m.content}</div>
198
- ))}
199
- </div>
200
- );
201
- }
202
- ```
355
+ Clients can send a `runId` from a long-gone workflow (localStorage, back button, server restart). Wrap the follow-up path in a try/catch for `not found` / `expired` and fall through to the first-turn code path to start a fresh workflow.
356
+
357
+ ### Make the first turn idempotent when needed
358
+
359
+ This example stores the `runId` after the first response. For strict one-session-per-thread behavior, use a deterministic hook token derived from the thread ID or conversation ID and route retries through the active hook. See [Idempotency](/docs/foundations/idempotency).
360
+
361
+ ## streamText vs DurableAgent
362
+
363
+ | | `streamText()` (this pattern) | `DurableAgent` |
364
+ |---|---|---|
365
+ | **Tool loop** | AI SDK handles via `stopWhen` | Handles internally (AI SDK–compatible options) |
366
+ | **LLM call durability** | Re-executes with the parent turn | Each LLM call is a durable step |
367
+ | **Tool call durability** | Not individually durable — re-executes with the parent turn | Per tool — mark `"use step"` for a durable, retryable step, or keep at workflow level for `sleep()` / hooks |
368
+ | **Stop conditions** | `stopWhen`, `prepareStep` | `stopWhen`, `prepareStep` |
369
+ | **Structured output** | `Output.object()`, `Output.array()` | `experimental_output` (`Output.object()`, `Output.text()`) |
370
+ | **Step callbacks** | `onStepFinish`, `onChunk`, etc. | `onStepFinish`, `onFinish`, `onError`, `onAbort` (`onChunk` not available) |
371
+ | **Setup** | Manual stream piping and turn slicing | Automatic |
372
+
373
+ Use `DurableAgent` for most agent use cases. Use `streamText` when you need the raw AI SDK surface or a per-turn durability boundary.
374
+
375
+ ## Key APIs
376
+
377
+ **AI SDK** ([docs](https://ai-sdk.dev/docs))
378
+
379
+ * [`streamText()`](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text) — core streaming function; `toUIMessageStream()` pipes into the durable writable
380
+ * [`tool()` / tool calling](https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling) — tools are plain async functions invoked by `streamText` inside the turn step; they are **not** individually durable in this pattern (see [Pitfalls](#tools-are-not-individually-durable))
381
+ * [`stepCountIs()` / `stopWhen`](https://ai-sdk.dev/docs/ai-sdk-core/agents#stop-conditions) — bound the agent loop inside each turn
382
+ * [`convertToModelMessages()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/convert-to-model-messages) / [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response) — UI ↔ model message conversion at the API boundary
383
+ * [`useChat()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat) — React hook that consumes the UI message stream on the client
384
+
385
+ **Workflow SDK**
203
386
 
204
- See [Resumable Streams](/docs/ai/resumable-streams) for advanced options like `startIndex` and `prepareReconnectToStreamRequest`.
387
+ * [`"use step"`](/docs/foundations/workflows-and-steps#step-functions) applied to `runTurn` to make each turn a durable, retryable unit
388
+ * [`defineHook()`](/docs/api-reference/workflow/define-hook) — suspension point for follow-up messages
389
+ * [`getWritable()`](/docs/api-reference/workflow/get-writable) — resumable stream output
390
+ * [`getRun()`](/docs/api-reference/workflow-api/get-run) — `run.getReadable({ startIndex })` for slicing per-turn streams
391
+ * [`WorkflowChatTransport`](/docs/api-reference/workflow-ai/workflow-chat-transport) — passes `runId` between turns
392
+ * [Idempotency](/docs/foundations/idempotency) — protect duplicate-sensitive first turns and side effects