-
Notifications
You must be signed in to change notification settings - Fork 231
fix(ai): preserve provider tool identity across step boundaries #1663
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,7 @@ | ||
| --- | ||
| '@workflow/ai': patch | ||
| --- | ||
|
|
||
| fix(ai): preserve provider tool identity across step boundaries | ||
|
|
||
| Provider tools (e.g. `anthropic.tools.webSearch`) were being converted to plain function tools in `toolsToModelTools`, stripping `type: 'provider'`, `id`, and `args`. This caused providers like Anthropic Gateway to not recognize them as provider-executed tools. |
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,106 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { toolsToModelTools } from './tools-to-model-tools.js'; | ||
|
|
||
| describe('toolsToModelTools', () => { | ||
| it('serializes function tools with description and inputSchema', async () => { | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get the weather', | ||
| parameters: z.object({ city: z.string() }), | ||
| execute: async ({ city }) => `Weather in ${city}: sunny`, | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0]).toMatchObject({ | ||
| type: 'function', | ||
| name: 'weather', | ||
| description: 'Get the weather', | ||
| }); | ||
| expect(result[0]).toHaveProperty('inputSchema'); | ||
| expect(result[0]).not.toHaveProperty('id'); | ||
| expect(result[0]).not.toHaveProperty('args'); | ||
| }); | ||
|
|
||
| it('preserves provider tool type, id, and args', async () => { | ||
| // Simulate a provider tool (e.g. anthropic.tools.webSearch) | ||
| const providerTool = { | ||
| type: 'provider' as const, | ||
| id: 'anthropic.web_search' as const, | ||
| args: { maxUses: 5 }, | ||
| inputSchema: { type: 'object' as const, properties: {} }, | ||
| }; | ||
|
|
||
| const tools = { | ||
| webSearch: providerTool, | ||
| } as any; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0]).toEqual({ | ||
| type: 'provider', | ||
| id: 'anthropic.web_search', | ||
| name: 'webSearch', | ||
| args: { maxUses: 5 }, | ||
| }); | ||
| }); | ||
|
|
||
| it('handles mixed function and provider tools', async () => { | ||
| const providerTool = { | ||
| type: 'provider' as const, | ||
| id: 'anthropic.web_search' as const, | ||
| args: {}, | ||
| inputSchema: { type: 'object' as const, properties: {} }, | ||
| }; | ||
|
|
||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get the weather', | ||
| parameters: z.object({ city: z.string() }), | ||
| execute: async ({ city }) => `Weather in ${city}: sunny`, | ||
| }), | ||
| webSearch: providerTool, | ||
| } as any; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
|
|
||
| const functionTool = result.find((t) => t.name === 'weather'); | ||
| const provider = result.find((t) => t.name === 'webSearch'); | ||
|
|
||
| expect(functionTool).toMatchObject({ type: 'function', name: 'weather' }); | ||
| expect(provider).toEqual({ | ||
| type: 'provider', | ||
| id: 'anthropic.web_search', | ||
| name: 'webSearch', | ||
| args: {}, | ||
| }); | ||
| }); | ||
|
|
||
| it('defaults args to empty object when not provided on provider tool', async () => { | ||
| const providerTool = { | ||
| type: 'provider' as const, | ||
| id: 'anthropic.code_execution' as const, | ||
| inputSchema: { type: 'object' as const, properties: {} }, | ||
| }; | ||
|
|
||
| const tools = { | ||
| codeExec: providerTool, | ||
| } as any; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result[0]).toEqual({ | ||
| type: 'provider', | ||
| id: 'anthropic.code_execution', | ||
| name: 'codeExec', | ||
| args: {}, | ||
| }); | ||
| }); | ||
| }); |
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,15 +1,31 @@ | ||
| import type { LanguageModelV3FunctionTool } from '@ai-sdk/provider'; | ||
| import type { | ||
| LanguageModelV3FunctionTool, | ||
| LanguageModelV3ProviderTool, | ||
| } from '@ai-sdk/provider'; | ||
| import { asSchema, type ToolSet } from 'ai'; | ||
|
|
||
| export async function toolsToModelTools( | ||
| tools: ToolSet | ||
| ): Promise<LanguageModelV3FunctionTool[]> { | ||
| ): Promise<Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderTool>> { | ||
| return Promise.all( | ||
| Object.entries(tools).map(async ([name, tool]) => ({ | ||
| type: 'function' as const, | ||
| name, | ||
| description: tool.description, | ||
| inputSchema: await asSchema(tool.inputSchema).jsonSchema, | ||
| })) | ||
| Object.entries(tools).map(async ([name, tool]) => { | ||
| // Preserve provider tool identity (e.g. anthropic.tools.webSearch) | ||
| // instead of converting to a plain function tool | ||
| if ((tool as any).type === 'provider') { | ||
| return { | ||
| type: 'provider' as const, | ||
| id: (tool as any).id as `${string}.${string}`, | ||
| name, | ||
| args: (tool as any).args ?? {}, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| type: 'function' as const, | ||
| name, | ||
| description: tool.description, | ||
| inputSchema: await asSchema(tool.inputSchema).jsonSchema, | ||
| }; | ||
| }) | ||
| ); | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking:
(tool as any).type === 'provider'is the same pattern used in the upstream AI SDK fix (vercel/ai#14229). Ifailater exports a type guard or theToolSettype is widened to include provider tools natively, this could be made type-safe. Fine for now.