diff options
| author | Tommaso Sciortino <[email protected]> | 2025-07-14 22:55:49 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-15 05:55:49 +0000 |
| commit | fefa7ecbea2ab985e48679ceb3010a174777a188 (patch) | |
| tree | 94777677e2e7e0c72e8f75941d8594b898e67192 /packages/core/src/tools/ls.ts | |
| parent | e584241141b74ab503a527fad158d60eb06c38fe (diff) | |
Pure refactor: Consolidate isWithinRoot() function calling. (#4163)
Diffstat (limited to 'packages/core/src/tools/ls.ts')
| -rw-r--r-- | packages/core/src/tools/ls.ts | 42 |
1 files changed, 9 insertions, 33 deletions
diff --git a/packages/core/src/tools/ls.ts b/packages/core/src/tools/ls.ts index df09cc50..9fb60072 100644 --- a/packages/core/src/tools/ls.ts +++ b/packages/core/src/tools/ls.ts @@ -11,6 +11,7 @@ import { Type } from '@google/genai'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { makeRelative, shortenPath } from '../utils/paths.js'; import { Config } from '../config/config.js'; +import { isWithinRoot } from '../utils/fileUtils.js'; /** * Parameters for the LS tool @@ -68,14 +69,7 @@ export interface FileEntry { export class LSTool extends BaseTool<LSToolParams, ToolResult> { static readonly Name = 'list_directory'; - /** - * Creates a new instance of the LSLogic - * @param rootDirectory Root directory to ground this tool in. All operations will be restricted to this directory. - */ - constructor( - private rootDirectory: string, - private config: Config, - ) { + constructor(private config: Config) { super( LSTool.Name, 'ReadFolder', @@ -104,27 +98,6 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> { type: Type.OBJECT, }, ); - - // Set the root directory - this.rootDirectory = path.resolve(rootDirectory); - } - - /** - * Checks if a path is within the root directory - * @param dirpath The path to check - * @returns True if the path is within the root directory, false otherwise - */ - private isWithinRoot(dirpath: string): boolean { - const normalizedPath = path.normalize(dirpath); - const normalizedRoot = path.normalize(this.rootDirectory); - // Ensure the normalizedRoot ends with a path separator for proper path comparison - const rootWithSep = normalizedRoot.endsWith(path.sep) - ? normalizedRoot - : normalizedRoot + path.sep; - return ( - normalizedPath === normalizedRoot || - normalizedPath.startsWith(rootWithSep) - ); } /** @@ -140,8 +113,8 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> { if (!path.isAbsolute(params.path)) { return `Path must be absolute: ${params.path}`; } - if (!this.isWithinRoot(params.path)) { - return `Path must be within the root directory (${this.rootDirectory}): ${params.path}`; + if (!isWithinRoot(params.path, this.config.getTargetDir())) { + return `Path must be within the root directory (${this.config.getTargetDir()}): ${params.path}`; } return null; } @@ -176,7 +149,7 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> { * @returns A string describing the file being read */ getDescription(params: LSToolParams): string { - const relativePath = makeRelative(params.path, this.rootDirectory); + const relativePath = makeRelative(params.path, this.config.getTargetDir()); return shortenPath(relativePath); } @@ -248,7 +221,10 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> { } const fullPath = path.join(params.path, file); - const relativePath = path.relative(this.rootDirectory, fullPath); + const relativePath = path.relative( + this.config.getTargetDir(), + fullPath, + ); // Check if this file should be git-ignored (only in git repositories) if ( |
