-
Notifications
You must be signed in to change notification settings - Fork 227
fix(ai): align toolsToModelTools with core AI SDK's prepareToolsAndToolChoice #1544
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
Open
iNishant
wants to merge
2
commits into
vercel:main
Choose a base branch
from
iNishant:fix/tools-to-model-tools-strict-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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,5 @@ | ||
| --- | ||
| "@workflow/ai": patch | ||
| --- | ||
|
|
||
| Forward `strict`, `inputExamples`, and `providerOptions` tool properties to language model providers, and add support for `type: 'provider'` 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,186 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { z } from 'zod'; | ||
| import { tool } from 'ai'; | ||
| import { toolsToModelTools } from './tools-to-model-tools.js'; | ||
|
|
||
| describe('toolsToModelTools', () => { | ||
| it('converts a basic function tool', async () => { | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0]).toMatchObject({ | ||
| type: 'function', | ||
| name: 'weather', | ||
| description: 'Get weather', | ||
| }); | ||
| expect(result[0]).toHaveProperty('inputSchema'); | ||
| expect(result[0]).not.toHaveProperty('strict'); | ||
| expect(result[0]).not.toHaveProperty('inputExamples'); | ||
| }); | ||
|
|
||
| it('forwards strict: true', async () => { | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| strict: true, | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result[0]).toMatchObject({ strict: true }); | ||
| }); | ||
|
|
||
| it('forwards strict: false', async () => { | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| strict: false, | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result[0]).toMatchObject({ strict: false }); | ||
| }); | ||
|
|
||
| it('omits strict key when not set', async () => { | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result[0]).not.toHaveProperty('strict'); | ||
| }); | ||
|
|
||
| it('forwards inputExamples', async () => { | ||
| const examples = [{ input: { location: 'Tokyo' } }]; | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| inputExamples: examples, | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result[0]).toMatchObject({ inputExamples: examples }); | ||
| }); | ||
|
|
||
| it('omits inputExamples key when not set', async () => { | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result[0]).not.toHaveProperty('inputExamples'); | ||
| }); | ||
|
|
||
| it('forwards providerOptions', async () => { | ||
| const providerOptions = { openai: { parallel_tool_calls: false } }; | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| providerOptions, | ||
| }), | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools); | ||
|
|
||
| expect(result[0]).toMatchObject({ providerOptions }); | ||
| }); | ||
|
|
||
| it('handles provider-type tools', async () => { | ||
| const tools = { | ||
| webSearch: { | ||
| type: 'provider' as const, | ||
| id: 'openai.web_search' as const, | ||
| args: { search_context_size: 'medium' }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools( | ||
| tools as any // provider tools don't have inputSchema/execute | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0]).toEqual({ | ||
| type: 'provider', | ||
| name: 'webSearch', | ||
| id: 'openai.web_search', | ||
| args: { search_context_size: 'medium' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('handles a mix of function and provider tools', async () => { | ||
| const tools = { | ||
| weather: tool({ | ||
| description: 'Get weather', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async () => 'sunny', | ||
| }), | ||
| webSearch: { | ||
| type: 'provider' as const, | ||
| id: 'openai.web_search' as const, | ||
| args: {}, | ||
| }, | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools as any); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
| expect(result.find((t) => t.name === 'weather')?.type).toBe('function'); | ||
| expect(result.find((t) => t.name === 'webSearch')?.type).toBe('provider'); | ||
| }); | ||
|
|
||
| it('handles tools with type: "dynamic" as function tools', async () => { | ||
| const tools = { | ||
| dynamic: { | ||
| type: 'dynamic' as const, | ||
| description: 'A dynamic tool', | ||
| inputSchema: z.object({ input: z.string() }), | ||
| execute: async () => 'result', | ||
| }, | ||
| }; | ||
|
|
||
| const result = await toolsToModelTools(tools as any); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0]).toMatchObject({ | ||
| type: 'function', | ||
| name: 'dynamic', | ||
| description: 'A dynamic tool', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns empty array for empty tools', async () => { | ||
| const result = await toolsToModelTools({}); | ||
| expect(result).toEqual([]); | ||
| }); | ||
| }); |
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,52 @@ | ||
| import type { LanguageModelV3FunctionTool } from '@ai-sdk/provider'; | ||
| import type { | ||
| LanguageModelV3FunctionTool, | ||
| LanguageModelV3ProviderTool, | ||
| } from '@ai-sdk/provider'; | ||
| import { asSchema, type ToolSet } from 'ai'; | ||
|
|
||
| // Mirrors the tool→LanguageModelV3FunctionTool/LanguageModelV3ProviderTool | ||
| // mapping in the core AI SDK's prepareToolsAndToolChoice | ||
| // (ai/src/prompt/prepare-tools-and-tool-choice.ts). | ||
| export async function toolsToModelTools( | ||
| tools: ToolSet | ||
| ): Promise<LanguageModelV3FunctionTool[]> { | ||
| return Promise.all( | ||
| Object.entries(tools).map(async ([name, tool]) => ({ | ||
| type: 'function' as const, | ||
| name, | ||
| description: tool.description, | ||
| inputSchema: await asSchema(tool.inputSchema).jsonSchema, | ||
| })) | ||
| ); | ||
| ): Promise<Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderTool>> { | ||
| const result: Array< | ||
| LanguageModelV3FunctionTool | LanguageModelV3ProviderTool | ||
| > = []; | ||
|
|
||
| for (const [name, tool] of Object.entries(tools)) { | ||
| const toolType = tool.type; | ||
|
|
||
| switch (toolType) { | ||
| case undefined: | ||
| case 'dynamic': | ||
| case 'function': | ||
| result.push({ | ||
| type: 'function' as const, | ||
| name, | ||
| description: tool.description, | ||
| inputSchema: await asSchema(tool.inputSchema).jsonSchema, | ||
| ...(tool.inputExamples != null | ||
| ? { inputExamples: tool.inputExamples } | ||
| : {}), | ||
| providerOptions: tool.providerOptions, | ||
| ...(tool.strict != null ? { strict: tool.strict } : {}), | ||
| }); | ||
| break; | ||
| case 'provider': | ||
| result.push({ | ||
| type: 'provider' as const, | ||
| name, | ||
| id: tool.id, | ||
| args: tool.args, | ||
| }); | ||
| break; | ||
| default: { | ||
| const exhaustiveCheck: never = toolType as never; | ||
| throw new Error(`Unsupported tool type: ${exhaustiveCheck}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
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.