mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 08:00:59 +00:00
fix(ai): coerce string numbers in tool argument validation (#786)
* fix(ai): coerce string numbers in tool argument validation * fix(ai): clone tool arguments before AJV validation for type coercion
This commit is contained in:
parent
09d9107d7f
commit
923b9cb9ee
2 changed files with 8 additions and 4 deletions
|
|
@ -19,6 +19,7 @@ if (!isBrowserExtension) {
|
||||||
ajv = new Ajv({
|
ajv = new Ajv({
|
||||||
allErrors: true,
|
allErrors: true,
|
||||||
strict: false,
|
strict: false,
|
||||||
|
coerceTypes: true,
|
||||||
});
|
});
|
||||||
addFormats(ajv);
|
addFormats(ajv);
|
||||||
} catch (_e) {
|
} catch (_e) {
|
||||||
|
|
@ -46,7 +47,7 @@ export function validateToolCall(tools: Tool[], toolCall: ToolCall): any {
|
||||||
* Validates tool call arguments against the tool's TypeBox schema
|
* Validates tool call arguments against the tool's TypeBox schema
|
||||||
* @param tool The tool definition with TypeBox schema
|
* @param tool The tool definition with TypeBox schema
|
||||||
* @param toolCall The tool call from the LLM
|
* @param toolCall The tool call from the LLM
|
||||||
* @returns The validated arguments
|
* @returns The validated (and potentially coerced) arguments
|
||||||
* @throws Error with formatted message if validation fails
|
* @throws Error with formatted message if validation fails
|
||||||
*/
|
*/
|
||||||
export function validateToolArguments(tool: Tool, toolCall: ToolCall): any {
|
export function validateToolArguments(tool: Tool, toolCall: ToolCall): any {
|
||||||
|
|
@ -60,9 +61,12 @@ export function validateToolArguments(tool: Tool, toolCall: ToolCall): any {
|
||||||
// Compile the schema
|
// Compile the schema
|
||||||
const validate = ajv.compile(tool.parameters);
|
const validate = ajv.compile(tool.parameters);
|
||||||
|
|
||||||
// Validate the arguments
|
// Clone arguments so AJV can safely mutate for type coercion
|
||||||
if (validate(toolCall.arguments)) {
|
const args = structuredClone(toolCall.arguments);
|
||||||
return toolCall.arguments;
|
|
||||||
|
// Validate the arguments (AJV mutates args in-place for type coercion)
|
||||||
|
if (validate(args)) {
|
||||||
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format validation errors nicely
|
// Format validation errors nicely
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 321 B |
Loading…
Add table
Add a link
Reference in a new issue