-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (ai/core): support tool calls without arguments (#3073)
- Loading branch information
Showing
6 changed files
with
178 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
fix (ai/core): support tool calls without arguments |
This file contains 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 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,110 @@ | ||
import { z } from 'zod'; | ||
import { InvalidToolArgumentsError } from '../../errors/invalid-tool-arguments-error'; | ||
import { NoSuchToolError } from '../../errors/no-such-tool-error'; | ||
import { tool } from '../tool'; | ||
import { parseToolCall } from './parse-tool-call'; | ||
|
||
it('should successfully parse a valid tool call', () => { | ||
const result = parseToolCall({ | ||
toolCall: { | ||
toolCallType: 'function', | ||
toolName: 'testTool', | ||
toolCallId: '123', | ||
args: '{"param1": "test", "param2": 42}', | ||
}, | ||
tools: { | ||
testTool: tool({ | ||
parameters: z.object({ | ||
param1: z.string(), | ||
param2: z.number(), | ||
}), | ||
}), | ||
} as const, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
type: 'tool-call', | ||
toolCallId: '123', | ||
toolName: 'testTool', | ||
args: { param1: 'test', param2: 42 }, | ||
}); | ||
}); | ||
|
||
it('should successfully process empty calls for tools that have no parameters', () => { | ||
const result = parseToolCall({ | ||
toolCall: { | ||
toolCallType: 'function', | ||
toolName: 'testTool', | ||
toolCallId: '123', | ||
args: '', | ||
}, | ||
tools: { | ||
testTool: tool({ | ||
parameters: z.object({}), | ||
}), | ||
} as const, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
type: 'tool-call', | ||
toolCallId: '123', | ||
toolName: 'testTool', | ||
args: {}, | ||
}); | ||
}); | ||
|
||
it('should throw NoSuchToolError when tools is null', () => { | ||
expect(() => | ||
parseToolCall({ | ||
toolCall: { | ||
toolCallType: 'function', | ||
toolName: 'testTool', | ||
toolCallId: '123', | ||
args: '{}', | ||
}, | ||
tools: undefined, | ||
}), | ||
).toThrow(NoSuchToolError); | ||
}); | ||
|
||
it('should throw NoSuchToolError when tool is not found', () => { | ||
expect(() => | ||
parseToolCall({ | ||
toolCall: { | ||
toolCallType: 'function', | ||
toolName: 'nonExistentTool', | ||
toolCallId: '123', | ||
args: '{}', | ||
}, | ||
tools: { | ||
testTool: tool({ | ||
parameters: z.object({ | ||
param1: z.string(), | ||
param2: z.number(), | ||
}), | ||
}), | ||
} as const, | ||
}), | ||
).toThrow(NoSuchToolError); | ||
}); | ||
|
||
it('should throw InvalidToolArgumentsError when args are invalid', () => { | ||
expect(() => | ||
parseToolCall({ | ||
toolCall: { | ||
toolCallType: 'function', | ||
toolName: 'testTool', | ||
toolCallId: '123', | ||
args: '{"param1": "test"}', // Missing required param2 | ||
}, | ||
tools: { | ||
testTool: tool({ | ||
parameters: z.object({ | ||
param1: z.string(), | ||
param2: z.number(), | ||
}), | ||
}), | ||
} as const, | ||
}), | ||
).toThrow(InvalidToolArgumentsError); | ||
}); |
This file contains 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,57 @@ | ||
import { LanguageModelV1FunctionToolCall } from '@ai-sdk/provider'; | ||
import { safeParseJSON, safeValidateTypes } from '@ai-sdk/provider-utils'; | ||
import { Schema, asSchema } from '@ai-sdk/ui-utils'; | ||
import { InvalidToolArgumentsError } from '../../errors/invalid-tool-arguments-error'; | ||
import { NoSuchToolError } from '../../errors/no-such-tool-error'; | ||
import { CoreTool } from '../tool'; | ||
import { inferParameters } from '../tool/tool'; | ||
import { ToToolCall } from './tool-call'; | ||
|
||
export function parseToolCall<TOOLS extends Record<string, CoreTool>>({ | ||
toolCall, | ||
tools, | ||
}: { | ||
toolCall: LanguageModelV1FunctionToolCall; | ||
tools?: TOOLS; | ||
}): ToToolCall<TOOLS> { | ||
const toolName = toolCall.toolName as keyof TOOLS & string; | ||
|
||
if (tools == null) { | ||
throw new NoSuchToolError({ toolName: toolCall.toolName }); | ||
} | ||
|
||
const tool = tools[toolName]; | ||
|
||
if (tool == null) { | ||
throw new NoSuchToolError({ | ||
toolName: toolCall.toolName, | ||
availableTools: Object.keys(tools), | ||
}); | ||
} | ||
|
||
const schema = asSchema(tool.parameters) as Schema< | ||
inferParameters<TOOLS[keyof TOOLS]['parameters']> | ||
>; | ||
|
||
// when the tool call has no arguments, we try passing an empty object to the schema | ||
// (many LLMs generate empty strings for tool calls with no arguments) | ||
const parseResult = | ||
toolCall.args.trim() === '' | ||
? safeValidateTypes({ value: {}, schema }) | ||
: safeParseJSON({ text: toolCall.args, schema }); | ||
|
||
if (parseResult.success === false) { | ||
throw new InvalidToolArgumentsError({ | ||
toolName, | ||
toolArgs: toolCall.args, | ||
cause: parseResult.error, | ||
}); | ||
} | ||
|
||
return { | ||
type: 'tool-call', | ||
toolCallId: toolCall.toolCallId, | ||
toolName, | ||
args: parseResult.value, | ||
}; | ||
} |
This file contains 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 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