2025-04-15 21:41:08 -07:00
import fs from 'fs' ;
import path from 'path' ;
2025-04-17 17:25:01 -04:00
import { BaseTool , ToolResult } from './tools.js' ;
2025-04-15 21:41:08 -07:00
import { SchemaValidator } from '../utils/schemaValidator.js' ;
import { makeRelative , shortenPath } from '../utils/paths.js' ;
/**
* Parameters for the LS tool
*/
export interface LSToolParams {
/**
* The absolute path to the directory to list
*/
path : string ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* List of glob patterns to ignore
*/
ignore? : string [ ] ;
}
/**
* File entry returned by LS tool
*/
export interface FileEntry {
/**
* Name of the file or directory
*/
name : string ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Absolute path to the file or directory
*/
path : string ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Whether this entry is a directory
*/
isDirectory : boolean ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Size of the file in bytes (0 for directories)
*/
size : number ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Last modified timestamp
*/
modifiedTime : Date ;
}
/**
* Result from the LS tool
*/
export interface LSToolResult extends ToolResult {
/**
* List of file entries
*/
entries : FileEntry [ ] ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* The directory that was listed
*/
listedPath : string ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Total number of entries found
*/
totalEntries : number ;
}
/**
* Implementation of the LS tool that lists directory contents
*/
export class LSTool extends BaseTool < LSToolParams , LSToolResult > {
/**
* The root directory that this tool is grounded in.
* All path operations will be restricted to this directory.
*/
private rootDirectory : string ;
/**
* Creates a new instance of the LSTool
* @param rootDirectory Root directory to ground this tool in. All operations will be restricted to this directory.
*/
constructor ( rootDirectory : string ) {
super (
'list_directory' ,
'ReadFolder' ,
'Lists the names of files and subdirectories directly within a specified directory path. Can optionally ignore entries matching provided glob patterns.' ,
{
properties : {
path : {
2025-04-17 18:06:21 -04:00
description :
'The absolute path to the directory to list (must be absolute, not relative)' ,
type : 'string' ,
2025-04-15 21:41:08 -07:00
} ,
ignore : {
description : 'List of glob patterns to ignore' ,
items : {
2025-04-17 18:06:21 -04:00
type : 'string' ,
2025-04-15 21:41:08 -07:00
} ,
2025-04-17 18:06:21 -04:00
type : 'array' ,
} ,
2025-04-15 21:41:08 -07:00
} ,
required : [ 'path' ] ,
2025-04-17 18:06:21 -04:00
type : 'object' ,
} ,
2025-04-15 21:41:08 -07:00
) ;
// Set the root directory
this . rootDirectory = path . resolve ( rootDirectory ) ;
}
/**
* Checks if a path is within the root directory
* @param pathToCheck The path to check
* @returns True if the path is within the root directory, false otherwise
*/
private isWithinRoot ( pathToCheck : string ) : boolean {
const normalizedPath = path . normalize ( pathToCheck ) ;
const normalizedRoot = path . normalize ( this . rootDirectory ) ;
// Ensure the normalizedRoot ends with a path separator for proper path comparison
2025-04-17 12:03:02 -07:00
const rootWithSep = normalizedRoot . endsWith ( path . sep )
? normalizedRoot
2025-04-15 21:41:08 -07:00
: normalizedRoot + path . sep ;
2025-04-17 18:06:21 -04:00
return (
normalizedPath === normalizedRoot ||
normalizedPath . startsWith ( rootWithSep )
) ;
2025-04-15 21:41:08 -07:00
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Validates the parameters for the tool
* @param params Parameters to validate
* @returns An error message string if invalid, null otherwise
*/
invalidParams ( params : LSToolParams ) : string | null {
2025-04-17 18:06:21 -04:00
if (
this . schema . parameters &&
! SchemaValidator . validate (
this . schema . parameters as Record < string , unknown > ,
params ,
)
) {
return 'Parameters failed schema validation.' ;
2025-04-15 21:41:08 -07:00
}
// Ensure path is absolute
if ( ! path . isAbsolute ( params . path ) ) {
return ` Path must be absolute: ${ params . path } ` ;
}
// Ensure path is within the root directory
if ( ! this . isWithinRoot ( params . path ) ) {
return ` Path must be within the root directory ( ${ this . rootDirectory } ): ${ params . path } ` ;
}
return null ;
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Checks if a filename matches any of the ignore patterns
* @param filename Filename to check
* @param patterns Array of glob patterns to check against
* @returns True if the filename should be ignored
*/
private shouldIgnore ( filename : string , patterns? : string [ ] ) : boolean {
if ( ! patterns || patterns . length === 0 ) {
return false ;
}
for ( const pattern of patterns ) {
// Convert glob pattern to RegExp
const regexPattern = pattern
. replace ( /[.+^${}()|[\]\\]/g , '\\$&' )
. replace ( /\*/g , '.*' )
. replace ( /\?/g , '.' ) ;
const regex = new RegExp ( ` ^ ${ regexPattern } $ ` ) ;
if ( regex . test ( filename ) ) {
return true ;
}
}
return false ;
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
2025-04-17 12:03:02 -07:00
* Gets a description of the file reading operation
2025-04-15 21:41:08 -07:00
* @param params Parameters for the file reading
* @returns A string describing the file being read
*/
getDescription ( params : LSToolParams ) : string {
const relativePath = makeRelative ( params . path , this . rootDirectory ) ;
return shortenPath ( relativePath ) ;
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
/**
* Executes the LS operation with the given parameters
* @param params Parameters for the LS operation
* @returns Result of the LS operation
*/
async execute ( params : LSToolParams ) : Promise < LSToolResult > {
const validationError = this . invalidParams ( params ) ;
if ( validationError ) {
return {
entries : [ ] ,
listedPath : params.path ,
totalEntries : 0 ,
llmContent : ` Error: Invalid parameters provided. Reason: ${ validationError } ` ,
2025-04-17 18:06:21 -04:00
returnDisplay : '**Error:** Failed to execute tool.' ,
2025-04-15 21:41:08 -07:00
} ;
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
try {
// Check if path exists
if ( ! fs . existsSync ( params . path ) ) {
return {
entries : [ ] ,
listedPath : params.path ,
totalEntries : 0 ,
llmContent : ` Directory does not exist: ${ params . path } ` ,
2025-04-17 18:06:21 -04:00
returnDisplay : ` Directory does not exist ` ,
2025-04-15 21:41:08 -07:00
} ;
}
// Check if path is a directory
const stats = fs . statSync ( params . path ) ;
if ( ! stats . isDirectory ( ) ) {
return {
entries : [ ] ,
listedPath : params.path ,
totalEntries : 0 ,
llmContent : ` Path is not a directory: ${ params . path } ` ,
2025-04-17 18:06:21 -04:00
returnDisplay : ` Path is not a directory ` ,
2025-04-15 21:41:08 -07:00
} ;
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
// Read directory contents
const files = fs . readdirSync ( params . path ) ;
const entries : FileEntry [ ] = [ ] ;
if ( files . length === 0 ) {
return {
entries : [ ] ,
listedPath : params.path ,
totalEntries : 0 ,
llmContent : ` Directory is empty: ${ params . path } ` ,
2025-04-17 18:06:21 -04:00
returnDisplay : ` Directory is empty. ` ,
2025-04-15 21:41:08 -07:00
} ;
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
for ( const file of files ) {
if ( this . shouldIgnore ( file , params . ignore ) ) {
continue ;
}
const fullPath = path . join ( params . path , file ) ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
try {
const stats = fs . statSync ( fullPath ) ;
const isDir = stats . isDirectory ( ) ;
entries . push ( {
name : file ,
path : fullPath ,
isDirectory : isDir ,
size : isDir ? 0 : stats.size ,
2025-04-17 18:06:21 -04:00
modifiedTime : stats.mtime ,
2025-04-15 21:41:08 -07:00
} ) ;
} catch ( error ) {
// Skip entries that can't be accessed
console . error ( ` Error accessing ${ fullPath } : ${ error } ` ) ;
}
}
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
// Sort entries (directories first, then alphabetically)
entries . sort ( ( a , b ) = > {
if ( a . isDirectory && ! b . isDirectory ) return - 1 ;
if ( ! a . isDirectory && b . isDirectory ) return 1 ;
return a . name . localeCompare ( b . name ) ;
} ) ;
2025-04-17 12:03:02 -07:00
2025-04-15 21:41:08 -07:00
// Create formatted content for display
2025-04-17 18:06:21 -04:00
const directoryContent = entries
. map ( ( entry ) = > {
const typeIndicator = entry . isDirectory ? 'd' : '-' ;
const sizeInfo = entry . isDirectory ? '' : ` ( ${ entry . size } bytes) ` ;
return ` ${ typeIndicator } ${ entry . name } ${ sizeInfo } ` ;
} )
. join ( '\n' ) ;
2025-04-15 21:41:08 -07:00
return {
entries ,
listedPath : params.path ,
totalEntries : entries.length ,
llmContent : ` Directory listing for ${ params . path } : \ n ${ directoryContent } ` ,
2025-04-17 18:06:21 -04:00
returnDisplay : ` Found ${ entries . length } item(s). ` ,
2025-04-15 21:41:08 -07:00
} ;
} catch ( error ) {
const errorMessage = ` Error listing directory: ${ error instanceof Error ? error.message : String ( error ) } ` ;
return {
entries : [ ] ,
listedPath : params.path ,
totalEntries : 0 ,
llmContent : errorMessage ,
2025-04-17 18:06:21 -04:00
returnDisplay : ` **Error:** ${ errorMessage } ` ,
2025-04-15 21:41:08 -07:00
} ;
}
}
2025-04-17 18:06:21 -04:00
}