11import type { ModelMessage } from "@ai-sdk/provider-utils" ;
2- import type { UIMessage } from "ai" ;
2+ import { Output , type UIMessage } from "ai" ;
33import type { z } from "zod" ;
44import type { Agent , BaseGenerationOptions } from "../../agent/agent" ;
55import { convertUsage } from "../../utils/usage-converter" ;
@@ -33,8 +33,8 @@ export type AgentConfig<SCHEMA extends z.ZodTypeAny, INPUT, DATA> = BaseGenerati
3333 * ```
3434 *
3535 * @param task - The task (prompt) to execute for the agent, can be a string or a function that returns a string
36- * @param agent - The agent to execute the task using `generateObject `
37- * @param config - The config for the agent (schema) `generateObject ` call
36+ * @param agent - The agent to execute the task using `generateText `
37+ * @param config - The config for the agent (schema) `generateText ` call
3838 * @returns A workflow step that executes the agent with the task
3939 */
4040export function andAgent < INPUT , DATA , SCHEMA extends z . ZodTypeAny > (
@@ -58,15 +58,18 @@ export function andAgent<INPUT, DATA, SCHEMA extends z.ZodTypeAny>(
5858 const finalTask = typeof task === "function" ? await task ( context ) : task ;
5959 const finalSchema = typeof schema === "function" ? await schema ( context ) : schema ;
6060
61+ const output = Output . object ( { schema : finalSchema } ) ;
62+
6163 // Create step context and publish start event
6264 if ( ! state . workflowContext ) {
6365 // No workflow context, execute without events
64- const result = await agent . generateObject ( finalTask , finalSchema , {
66+ const result = await agent . generateText ( finalTask , {
6567 ...restConfig ,
6668 context : restConfig . context ?? state . context ,
6769 conversationId : restConfig . conversationId ?? state . conversationId ,
6870 userId : restConfig . userId ?? state . userId ,
6971 // No parentSpan when there's no workflow context
72+ output,
7073 } ) ;
7174 // Accumulate usage if available (no workflow context)
7275 if ( result . usage && state . usage ) {
@@ -81,19 +84,20 @@ export function andAgent<INPUT, DATA, SCHEMA extends z.ZodTypeAny>(
8184 }
8285 state . usage . totalTokens += convertedUsage ?. totalTokens || 0 ;
8386 }
84- return result . object ;
87+ return result . output as z . infer < SCHEMA > ;
8588 }
8689
8790 // Step start event removed - now handled by OpenTelemetry spans
8891
8992 try {
90- const result = await agent . generateObject ( finalTask , finalSchema , {
93+ const result = await agent . generateText ( finalTask , {
9194 ...restConfig ,
9295 context : restConfig . context ?? state . context ,
9396 conversationId : restConfig . conversationId ?? state . conversationId ,
9497 userId : restConfig . userId ?? state . userId ,
9598 // Pass the current step span as parent for proper span hierarchy
9699 parentSpan : state . workflowContext ?. currentStepSpan ,
100+ output,
97101 } ) ;
98102
99103 // Step success event removed - now handled by OpenTelemetry spans
@@ -112,7 +116,7 @@ export function andAgent<INPUT, DATA, SCHEMA extends z.ZodTypeAny>(
112116 state . usage . totalTokens += convertedUsage ?. totalTokens || 0 ;
113117 }
114118
115- return result . object ;
119+ return result . output as z . infer < SCHEMA > ;
116120 } catch ( error ) {
117121 // Check if this is a suspension, not an error
118122 if (
0 commit comments