Improve Function Call argument validation and typing (#2881)

Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
This commit is contained in:
Tommaso Sciortino
2025-07-07 23:48:44 -07:00
committed by GitHub
parent 137ffec3f6
commit 4dab31f1c8
22 changed files with 239 additions and 246 deletions

View File

@@ -8,6 +8,7 @@ import path from 'path';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { makeRelative, shortenPath } from '../utils/paths.js';
import { BaseTool, ToolResult } from './tools.js';
import { Type } from '@google/genai';
import {
isWithinRoot,
processSingleFileContent,
@@ -58,37 +59,33 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> {
absolute_path: {
description:
"The absolute path to the file to read (e.g., '/home/user/project/file.txt'). Relative paths are not supported. You must provide an absolute path.",
type: 'string',
type: Type.STRING,
pattern: '^/',
},
offset: {
description:
"Optional: For text files, the 0-based line number to start reading from. Requires 'limit' to be set. Use for paginating through large files.",
type: 'number',
type: Type.NUMBER,
},
limit: {
description:
"Optional: For text files, maximum number of lines to read. Use with 'offset' to paginate through large files. If omitted, reads the entire file (if feasible, up to a default limit).",
type: 'number',
type: Type.NUMBER,
},
},
required: ['absolute_path'],
type: 'object',
type: Type.OBJECT,
},
);
this.rootDirectory = path.resolve(rootDirectory);
}
validateToolParams(params: ReadFileToolParams): string | null {
if (
this.schema.parameters &&
!SchemaValidator.validate(
this.schema.parameters as Record<string, unknown>,
params,
)
) {
return 'Parameters failed schema validation.';
const errors = SchemaValidator.validate(this.schema.parameters, params);
if (errors) {
return errors;
}
const filePath = params.absolute_path;
if (!path.isAbsolute(filePath)) {
return `File path must be absolute, but was relative: ${filePath}. You must provide an absolute path.`;