Skip to content

Commit cb1bdd2

Browse files
committed
docs: update based on latest changes
1 parent 9f6dc8f commit cb1bdd2

File tree

2 files changed

+89
-62
lines changed

2 files changed

+89
-62
lines changed

content/docs/03-agents/07-workflow-agent.mdx

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -100,63 +100,86 @@ new WorkflowAgent({
100100

101101
### End-to-End Example
102102

103-
```ts filename="app/api/chat/workflow.ts"
104-
'use workflow';
105-
106-
import { WorkflowAgent } from '@ai-sdk/workflow';
107-
import { tool } from 'ai';
103+
```ts filename="workflow/agent-chat.ts"
104+
import { WorkflowAgent, type ModelCallStreamPart } from '@ai-sdk/workflow';
105+
import { convertToModelMessages, tool, type UIMessage } from 'ai';
108106
import { getWritable } from 'workflow';
109107
import { z } from 'zod';
110108

111-
const agent = new WorkflowAgent({
112-
model: 'anthropic/claude-sonnet-4-6',
113-
instructions: 'You are a flight booking assistant.',
114-
tools: {
115-
searchFlights: tool({
116-
description: 'Search for available flights',
117-
inputSchema: z.object({
118-
origin: z.string(),
119-
destination: z.string(),
120-
date: z.string(),
109+
export async function chat(messages: UIMessage[]) {
110+
'use workflow';
111+
112+
const modelMessages = await convertToModelMessages(messages);
113+
114+
const agent = new WorkflowAgent({
115+
model: 'anthropic/claude-sonnet-4-6',
116+
instructions: 'You are a flight booking assistant.',
117+
tools: {
118+
searchFlights: tool({
119+
description: 'Search for available flights',
120+
inputSchema: z.object({
121+
origin: z.string(),
122+
destination: z.string(),
123+
date: z.string(),
124+
}),
125+
execute: searchFlightsStep,
121126
}),
122-
execute: searchFlightsStep,
123-
}),
124-
bookFlight: tool({
125-
description: 'Book a specific flight',
126-
inputSchema: z.object({
127-
flightId: z.string(),
128-
passengerName: z.string(),
127+
bookFlight: tool({
128+
description: 'Book a specific flight',
129+
inputSchema: z.object({
130+
flightId: z.string(),
131+
passengerName: z.string(),
132+
}),
133+
execute: bookFlightStep,
129134
}),
130-
execute: bookFlightStep,
131-
}),
132-
},
133-
});
135+
},
136+
});
134137

135-
export default async function flightWorkflow(messages: any[]) {
136-
return await agent.stream({
137-
messages,
138-
writable: getWritable(),
138+
const result = await agent.stream({
139+
messages: modelMessages,
140+
writable: getWritable<ModelCallStreamPart>(),
139141
});
142+
143+
return { messages: result.messages };
140144
}
141145
```
142146

143147
```ts filename="app/api/chat/route.ts"
144-
import { createUIMessageStreamResponse } from 'ai';
145148
import { createModelCallToUIChunkTransform } from '@ai-sdk/workflow';
146-
import { start } from 'workflow';
147-
import flightWorkflow from './workflow';
149+
import { createUIMessageStreamResponse, type UIMessage } from 'ai';
150+
import { start } from 'workflow/api';
151+
import { chat } from '@/workflow/agent-chat';
148152

149153
export async function POST(request: Request) {
150-
const { messages } = await request.json();
154+
const { messages }: { messages: UIMessage[] } = await request.json();
151155

152-
const run = await start(flightWorkflow, { input: messages });
156+
const run = await start(chat, [messages]);
153157

154158
return createUIMessageStreamResponse({
155159
stream: run.readable.pipeThrough(createModelCallToUIChunkTransform()),
156160
});
157161
}
158162
```
159163

164+
### Message Conversion
165+
166+
`WorkflowAgent.stream()` expects `ModelMessage[]`, not `UIMessage[]`. When receiving messages from the client (via `useChat`), convert them first:
167+
168+
```ts
169+
import { convertToModelMessages, type UIMessage } from 'ai';
170+
171+
export async function chat(messages: UIMessage[]) {
172+
'use workflow';
173+
174+
const modelMessages = await convertToModelMessages(messages);
175+
176+
const result = await agent.stream({
177+
messages: modelMessages,
178+
// ...
179+
});
180+
}
181+
```
182+
160183
### Writable Streams
161184

162185
Unlike `ToolLoopAgent` where you consume the returned stream, `WorkflowAgent` writes raw `ModelCallStreamPart` chunks to a `writable` stream provided by the workflow runtime via `getWritable()`. At the response boundary, use `createModelCallToUIChunkTransform()` to convert these into `UIMessageChunk` objects for the client:

content/docs/07-reference/04-ai-sdk-workflow/01-workflow-agent.mdx

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,9 @@ console.log(result.steps);
607607

608608
### Agent in a Workflow with Durable Tools
609609

610-
```ts filename="app/api/chat/workflow.ts"
611-
'use workflow';
612-
613-
import { WorkflowAgent } from '@ai-sdk/workflow';
614-
import { tool } from 'ai';
610+
```ts filename="workflow/agent-chat.ts"
611+
import { WorkflowAgent, type ModelCallStreamPart } from '@ai-sdk/workflow';
612+
import { convertToModelMessages, tool, type UIMessage } from 'ai';
615613
import { getWritable } from 'workflow';
616614
import { z } from 'zod';
617615

@@ -626,39 +624,45 @@ async function searchFlightsStep(input: {
626624
return response.json();
627625
}
628626

629-
const agent = new WorkflowAgent({
630-
model: 'anthropic/claude-sonnet-4-6',
631-
instructions: 'You are a flight booking assistant.',
632-
tools: {
633-
searchFlights: tool({
634-
description: 'Search for available flights',
635-
inputSchema: z.object({
636-
origin: z.string(),
637-
destination: z.string(),
627+
export async function chat(messages: UIMessage[]) {
628+
'use workflow';
629+
630+
const modelMessages = await convertToModelMessages(messages);
631+
632+
const agent = new WorkflowAgent({
633+
model: 'anthropic/claude-sonnet-4-6',
634+
instructions: 'You are a flight booking assistant.',
635+
tools: {
636+
searchFlights: tool({
637+
description: 'Search for available flights',
638+
inputSchema: z.object({
639+
origin: z.string(),
640+
destination: z.string(),
641+
}),
642+
execute: searchFlightsStep,
638643
}),
639-
execute: searchFlightsStep,
640-
}),
641-
},
642-
});
644+
},
645+
});
643646

644-
export default async function flightWorkflow(messages: any[]) {
645-
return await agent.stream({
646-
messages,
647-
writable: getWritable(),
647+
const result = await agent.stream({
648+
messages: modelMessages,
649+
writable: getWritable<ModelCallStreamPart>(),
648650
});
651+
652+
return { messages: result.messages };
649653
}
650654
```
651655

652656
```ts filename="app/api/chat/route.ts"
653-
import { createUIMessageStreamResponse } from 'ai';
654657
import { createModelCallToUIChunkTransform } from '@ai-sdk/workflow';
655-
import { start } from 'workflow';
656-
import flightWorkflow from './workflow';
658+
import { createUIMessageStreamResponse, type UIMessage } from 'ai';
659+
import { start } from 'workflow/api';
660+
import { chat } from '@/workflow/agent-chat';
657661

658662
export async function POST(request: Request) {
659-
const { messages } = await request.json();
663+
const { messages }: { messages: UIMessage[] } = await request.json();
660664

661-
const run = await start(flightWorkflow, { input: messages });
665+
const run = await start(chat, [messages]);
662666

663667
return createUIMessageStreamResponse({
664668
stream: run.readable.pipeThrough(createModelCallToUIChunkTransform()),

0 commit comments

Comments
 (0)