Pure refactor: Consolidate isWithinRoot() function calling. (#4163)

This commit is contained in:
Tommaso Sciortino
2025-07-14 22:55:49 -07:00
committed by GitHub
parent e584241141
commit fefa7ecbea
13 changed files with 96 additions and 179 deletions

View File

@@ -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 (