@@ -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' ;
108106import { getWritable } from ' workflow' ;
109107import { 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' ;
145148import { 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
149153export 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
162185Unlike ` 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:
0 commit comments