-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: enable andAgent tool usage by switching to generateText with Output.object
#927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --- | ||
| "@voltagent/core": patch | ||
| --- | ||
|
|
||
| feat: enable `andAgent` tool usage by switching to `generateText` with `Output.object` while keeping structured output | ||
|
|
||
| Example: | ||
|
|
||
| ```ts | ||
| import { Agent, createTool, createWorkflowChain } from "@voltagent/core"; | ||
| import { z } from "zod"; | ||
| import { openai } from "@ai-sdk/openai"; | ||
|
|
||
| const getWeather = createTool({ | ||
| name: "get_weather", | ||
| description: "Get weather for a city", | ||
| parameters: z.object({ city: z.string() }), | ||
| execute: async ({ city }) => ({ city, temp: 72, condition: "sunny" }), | ||
| }); | ||
|
|
||
| const agent = new Agent({ | ||
| name: "WeatherAgent", | ||
| model: openai("gpt-4o-mini"), | ||
| tools: [getWeather], | ||
| }); | ||
|
|
||
| const workflow = createWorkflowChain({ | ||
| id: "weather-flow", | ||
| input: z.object({ city: z.string() }), | ||
| }).andAgent(({ data }) => `What is the weather in ${data.city}?`, agent, { | ||
| schema: z.object({ temp: z.number(), condition: z.string() }), | ||
| }); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import type { ModelMessage } from "@ai-sdk/provider-utils"; | ||
| import type { UIMessage } from "ai"; | ||
| import { Output, type UIMessage } from "ai"; | ||
| import type { z } from "zod"; | ||
| import type { Agent, BaseGenerationOptions } from "../../agent/agent"; | ||
| import { convertUsage } from "../../utils/usage-converter"; | ||
|
|
@@ -33,8 +33,8 @@ export type AgentConfig<SCHEMA extends z.ZodTypeAny, INPUT, DATA> = BaseGenerati | |
| * ``` | ||
| * | ||
| * @param task - The task (prompt) to execute for the agent, can be a string or a function that returns a string | ||
| * @param agent - The agent to execute the task using `generateObject` | ||
| * @param config - The config for the agent (schema) `generateObject` call | ||
| * @param agent - The agent to execute the task using `generateText` | ||
| * @param config - The config for the agent (schema) `generateText` call | ||
| * @returns A workflow step that executes the agent with the task | ||
| */ | ||
| export function andAgent<INPUT, DATA, SCHEMA extends z.ZodTypeAny>( | ||
|
|
@@ -58,15 +58,18 @@ export function andAgent<INPUT, DATA, SCHEMA extends z.ZodTypeAny>( | |
| const finalTask = typeof task === "function" ? await task(context) : task; | ||
| const finalSchema = typeof schema === "function" ? await schema(context) : schema; | ||
|
|
||
| const output = Output.object({ schema: finalSchema }); | ||
|
|
||
| // Create step context and publish start event | ||
| if (!state.workflowContext) { | ||
| // No workflow context, execute without events | ||
| const result = await agent.generateObject(finalTask, finalSchema, { | ||
| const result = await agent.generateText(finalTask, { | ||
| ...restConfig, | ||
| context: restConfig.context ?? state.context, | ||
| conversationId: restConfig.conversationId ?? state.conversationId, | ||
| userId: restConfig.userId ?? state.userId, | ||
| // No parentSpan when there's no workflow context | ||
| output, | ||
| }); | ||
| // Accumulate usage if available (no workflow context) | ||
| if (result.usage && state.usage) { | ||
|
|
@@ -81,19 +84,20 @@ export function andAgent<INPUT, DATA, SCHEMA extends z.ZodTypeAny>( | |
| } | ||
| state.usage.totalTokens += convertedUsage?.totalTokens || 0; | ||
| } | ||
| return result.object; | ||
| return result.output as z.infer<SCHEMA>; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for structured output generation failures and validate result.output. The return statements cast
Also applies to: 119-119 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| // Step start event removed - now handled by OpenTelemetry spans | ||
|
|
||
| try { | ||
| const result = await agent.generateObject(finalTask, finalSchema, { | ||
| const result = await agent.generateText(finalTask, { | ||
| ...restConfig, | ||
| context: restConfig.context ?? state.context, | ||
| conversationId: restConfig.conversationId ?? state.conversationId, | ||
| userId: restConfig.userId ?? state.userId, | ||
| // Pass the current step span as parent for proper span hierarchy | ||
| parentSpan: state.workflowContext?.currentStepSpan, | ||
| output, | ||
| }); | ||
|
|
||
| // Step success event removed - now handled by OpenTelemetry spans | ||
|
|
@@ -112,7 +116,7 @@ export function andAgent<INPUT, DATA, SCHEMA extends z.ZodTypeAny>( | |
| state.usage.totalTokens += convertedUsage?.totalTokens || 0; | ||
| } | ||
|
|
||
| return result.object; | ||
| return result.output as z.infer<SCHEMA>; | ||
| } catch (error) { | ||
| // Check if this is a suspension, not an error | ||
| if ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.