Style improvements to ls tool (#14)

This commit is contained in:
Jaana Dogan 2025-04-18 10:57:20 -07:00 committed by GitHub
parent f6a4a5c44d
commit 93fd6a9160
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 56 deletions

View File

@ -114,11 +114,11 @@ export class LSTool extends BaseTool<LSToolParams, LSToolResult> {
/** /**
* Checks if a path is within the root directory * Checks if a path is within the root directory
* @param pathToCheck The path to check * @param dirpath The path to check
* @returns True if the path is within the root directory, false otherwise * @returns True if the path is within the root directory, false otherwise
*/ */
private isWithinRoot(pathToCheck: string): boolean { private isWithinRoot(dirpath: string): boolean {
const normalizedPath = path.normalize(pathToCheck); const normalizedPath = path.normalize(dirpath);
const normalizedRoot = path.normalize(this.rootDirectory); const normalizedRoot = path.normalize(this.rootDirectory);
// Ensure the normalizedRoot ends with a path separator for proper path comparison // Ensure the normalizedRoot ends with a path separator for proper path comparison
const rootWithSep = normalizedRoot.endsWith(path.sep) const rootWithSep = normalizedRoot.endsWith(path.sep)
@ -136,21 +136,13 @@ export class LSTool extends BaseTool<LSToolParams, LSToolResult> {
* @returns An error message string if invalid, null otherwise * @returns An error message string if invalid, null otherwise
*/ */
invalidParams(params: LSToolParams): string | null { invalidParams(params: LSToolParams): string | null {
if ( if (this.schema.parameters &&
this.schema.parameters && !SchemaValidator.validate(this.schema.parameters as Record<string, unknown>, params)) {
!SchemaValidator.validate(
this.schema.parameters as Record<string, unknown>,
params,
)
) {
return 'Parameters failed schema validation.'; return 'Parameters failed schema validation.';
} }
// Ensure path is absolute
if (!path.isAbsolute(params.path)) { if (!path.isAbsolute(params.path)) {
return `Path must be absolute: ${params.path}`; return `Path must be absolute: ${params.path}`;
} }
// Ensure path is within the root directory
if (!this.isWithinRoot(params.path)) { if (!this.isWithinRoot(params.path)) {
return `Path must be within the root directory (${this.rootDirectory}): ${params.path}`; return `Path must be within the root directory (${this.rootDirectory}): ${params.path}`;
} }
@ -191,6 +183,16 @@ export class LSTool extends BaseTool<LSToolParams, LSToolResult> {
return shortenPath(relativePath); return shortenPath(relativePath);
} }
private errorResult(params: LSToolParams, llmContent: string, returnDisplay: string): LSToolResult {
return {
entries: [],
listedPath: params.path,
totalEntries: 0,
llmContent: llmContent,
returnDisplay: `**Error:** ${returnDisplay}`,
};
}
/** /**
* Executes the LS operation with the given parameters * Executes the LS operation with the given parameters
* @param params Parameters for the LS operation * @param params Parameters for the LS operation
@ -199,57 +201,42 @@ export class LSTool extends BaseTool<LSToolParams, LSToolResult> {
async execute(params: LSToolParams): Promise<LSToolResult> { async execute(params: LSToolParams): Promise<LSToolResult> {
const validationError = this.invalidParams(params); const validationError = this.invalidParams(params);
if (validationError) { if (validationError) {
return { return this.errorResult(
entries: [], params,
listedPath: params.path, `Error: Invalid parameters provided. Reason: ${validationError}`,
totalEntries: 0, `Failed to execute tool.`);
llmContent: `Error: Invalid parameters provided. Reason: ${validationError}`,
returnDisplay: '**Error:** Failed to execute tool.',
};
} }
try { try {
// Check if path exists
if (!fs.existsSync(params.path)) {
return {
entries: [],
listedPath: params.path,
totalEntries: 0,
llmContent: `Directory does not exist: ${params.path}`,
returnDisplay: `Directory does not exist`,
};
}
// Check if path is a directory
const stats = fs.statSync(params.path); const stats = fs.statSync(params.path);
if (!stats) {
return this.errorResult(
params,
`Directory does not exist: ${params.path}`,
`Directory does not exist.`);
}
if (!stats.isDirectory()) { if (!stats.isDirectory()) {
return { return this.errorResult(
entries: [], params,
listedPath: params.path, `Path is not a directory: ${params.path}`,
totalEntries: 0, `Path is not a directory.`);
llmContent: `Path is not a directory: ${params.path}`,
returnDisplay: `Path is not a directory`,
};
} }
// Read directory contents
const files = fs.readdirSync(params.path); const files = fs.readdirSync(params.path);
const entries: FileEntry[] = []; const entries: FileEntry[] = [];
if (files.length === 0) { if (files.length === 0) {
return { return this.errorResult(
entries: [], params,
listedPath: params.path, `Directory is empty: ${params.path}`,
totalEntries: 0, `Directory is empty.`);
llmContent: `Directory is empty: ${params.path}`,
returnDisplay: `Directory is empty.`,
};
} }
for (const file of files) { for (const file of files) {
if (this.shouldIgnore(file, params.ignore)) { if (this.shouldIgnore(file, params.ignore)) {
continue; continue;
} }
const fullPath = path.join(params.path, file);
const fullPath = path.join(params.path, file);
try { try {
const stats = fs.statSync(fullPath); const stats = fs.statSync(fullPath);
const isDir = stats.isDirectory(); const isDir = stats.isDirectory();
@ -261,7 +248,6 @@ export class LSTool extends BaseTool<LSToolParams, LSToolResult> {
modifiedTime: stats.mtime, modifiedTime: stats.mtime,
}); });
} catch (error) { } catch (error) {
// Skip entries that can't be accessed
console.error(`Error accessing ${fullPath}: ${error}`); console.error(`Error accessing ${fullPath}: ${error}`);
} }
} }
@ -290,14 +276,10 @@ export class LSTool extends BaseTool<LSToolParams, LSToolResult> {
returnDisplay: `Found ${entries.length} item(s).`, returnDisplay: `Found ${entries.length} item(s).`,
}; };
} catch (error) { } catch (error) {
const errorMessage = `Error listing directory: ${error instanceof Error ? error.message : String(error)}`; return this.errorResult(
return { params,
entries: [], `Error listing directory: ${error instanceof Error ? error.message : String(error)}`,
listedPath: params.path, 'Failed to list directory.');
totalEntries: 0,
llmContent: errorMessage,
returnDisplay: `**Error:** ${errorMessage}`,
};
} }
} }
} }