Class FileFunctionTools
- All Implemented Interfaces:
FunctionTools
ProcessProvider.
Tools in this installer are intended for host-integrated use where the host controls the base working directory. All paths provided to these tools are interpreted relative to the working directory supplied by the provider/runtime.
Installed tools
read_file– reads a file as textwrite_file– writes a file (creating parent directories as needed)list_files_in_directory– lists immediate children of a directory
- Author:
- Viktor Tovstyi
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringDefault character set used when reading or writing text files. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapplyPatchToFile(File file, String patch, String charsetName, File projectDir) Implementsapply_patch_to_file.private voidcollectFilesRecursive(File currentDir, File projectDir, List<String> filePaths, int maxCount) Recursively collects files belowcurrentDir, enforcing the result limit as files are discovered.private voidRecursively collects directories and files belowcurrentDir.(package private) FileResolves a requested path beneath the canonical project root.getRecursiveFiles(File path, int maxCount, File projectDir) Lists files recursively in a directory up to a specified maximum limit.getRecursiveFolders(File dir, int maxCount, File projectDir) Implementsget_recursive_folder_list.static StringgetRelativePath(File dir, File file, boolean addSingleDot) Computes a project-relative path string.Lists the contents of a specified directory within a project, grouping the results into separate lists for directories and files using their relative paths.readFile(File filePath, String charsetName, int maxFileSize, File projectDir, Configurator configurator) Implementsread_file.Implementswrite_file.private voidwriteFileContent(File file, String content, String charsetName) WritescontenttofileusingcharsetName.private StringwriteNewFile(File file, String text, String charsetName, File filePath) Creates the file (and parent directories as needed) and writestextusing the requested character set.
-
Field Details
-
DEFAULT_CHARSET
Default character set used when reading or writing text files.- See Also:
-
-
Constructor Details
-
FileFunctionTools
public FileFunctionTools()
-
-
Method Details
-
listFiles
Lists the contents of a specified directory within a project, grouping the results into separate lists for directories and files using their relative paths.This method resolves the target directory using
dirPathandprojectDir, verifies that it is a valid directory, and iterates through its direct children. Each child is classified as either a directory or a file and its relative path is added to the corresponding list in the returned map.- Parameters:
dirPath- the path to the target directory to list contents of. Defaults to"."(current directory).projectDir- the root project directory used to compute relative paths for the listed files and folders.- Returns:
- a
Mapcontaining two key-value pairs: If the specified path is not a directory or is empty, the respective lists will be empty. - Throws:
IOException- if the directory cannot be accessedIllegalArgumentException- if either path isnull, cannot be canonicalized, or the requested path is outsideprojectDir
-
collectRecursive
private void collectRecursive(File currentDir, File projectDir, List<String> directories, List<String> files) Recursively collects directories and files belowcurrentDir.- Parameters:
currentDir- directory whose children are being inspectedprojectDir- root used to compute project-relative pathsdirectories- destination list for discovered directory pathsfiles- destination list for discovered file paths
-
getRecursiveFiles
Lists files recursively in a directory up to a specified maximum limit.This AI functional tool returns the files discovered below a directory.
- Parameters:
path- the relative or absolute path of the directory to scanmaxCount- the maximum number of files allowed in the result; throws an error if exceededprojectDir- the root project directory context- Returns:
- a
Listof relative file path strings, or a message string indicating no files were found - Throws:
IOException- if the directory cannot be accessedIllegalArgumentException- if the number of discovered files exceedsmaxCount, or if the requested path is invalid or outsideprojectDir
-
collectFilesRecursive
private void collectFilesRecursive(File currentDir, File projectDir, List<String> filePaths, int maxCount) Recursively collects files belowcurrentDir, enforcing the result limit as files are discovered.- Parameters:
currentDir- directory whose children are being inspectedprojectDir- root used to compute project-relative pathsfilePaths- destination list for discovered file pathsmaxCount- maximum number of files permitted- Throws:
IllegalArgumentException- if the number of discovered files exceedsmaxCount
-
getRecursiveFolders
Implementsget_recursive_folder_list.This AI functional tool recursively discovers and returns only the folder structure (directories) within a specified path. It does not return files or contents stored inside those folders.
- Parameters:
dir- directory path relative toprojectDirto start scanning frommaxCount- maximum number of folders allowed in the resultprojectDir- project root used to resolve the directory- Returns:
- project-relative folder paths as a list, or a message when none are found
- Throws:
IOException- if the directory cannot be traversedIllegalArgumentException- if the number of discovered folders exceedsmaxCount, or if the requested path is invalid or outsideprojectDir
-
writeFile
public String writeFile(File filePath, String text, String charsetName, File projectDir) throws IOException Implementswrite_file.This AI functional tool creates or replaces a file with the supplied text.
- Parameters:
filePath- file to create or replace, relative toprojectDirtext- content to writecharsetName- character set used to encode the contentprojectDir- project root used to resolve the file- Returns:
- a success message or an error message when writing fails
- Throws:
IOException- if the file cannot be created or writtenIllegalArgumentException- if the requested path is invalid or outsideprojectDir
-
writeFileContent
WritescontenttofileusingcharsetName.- Parameters:
file- destination filecontent- content to writecharsetName- character set name- Throws:
IOException- if writing failsIllegalArgumentException- ifcharsetNamedoes not identify a supported character set
-
writeNewFile
private String writeNewFile(File file, String text, String charsetName, File filePath) throws IOException Creates the file (and parent directories as needed) and writestextusing the requested character set.- Parameters:
file- file to createtext- contentcharsetName- character set namefilePath- original (relative) file path used for messaging- Returns:
- success message
- Throws:
IOException- if an I/O error occursIllegalArgumentException- ifcharsetNamedoes not identify a supported character set
-
readFile
public String readFile(File filePath, String charsetName, int maxFileSize, File projectDir, Configurator configurator) throws IOException Implementsread_file.This AI functional tool reads a file and returns its text content.
- Parameters:
filePath- file to read, relative toprojectDircharsetName- the name of the requested charset used to decode the filemaxFileSize- the maximum allowed character length of the file contentprojectDir- project root used to resolve the fileconfigurator- configuration used to substitute URL and header values- Returns:
- the complete file contents as text
- Throws:
IOException- if the file does not exist, is a directory, or cannot be readIllegalArgumentException- if the file content length exceeds the allowedmaxFileSizelimit, the requested path is invalid or outsideprojectDir, orcharsetNamedoes not identify a supported character set
-
getFile
Resolves a requested path beneath the canonical project root.- Parameters:
filePath- requested file or directoryprojectDir- project root- Returns:
- canonical file located under the project root
- Throws:
IOException- if the file cannot be resolvedIllegalArgumentException- if a path is invalid or escapes the root
-
getRelativePath
Computes a project-relative path string.The returned path always uses forward slashes (
/) for consistency across platforms.- Parameters:
dir- base directory used to relativize thefilefile- target file or directoryaddSingleDot- whether to prefix relative path with./- Returns:
- relative path,
.ifdirequalsfile, ornullwhen either argument isnullor the paths cannot be relativized (for example, because they use different roots)
-
applyPatchToFile
Implementsapply_patch_to_file.This AI functional tool applies a targeted unified or simplified search-and-replace patch to a file within the project directory.
- Parameters:
file- path of the file to patch, relative toprojectDirpatch- patch content in a supported formatcharsetName- character set used to read and write the fileprojectDir- project root used to resolve the file- Returns:
- a success message, or a failure message containing the underlying error detail
-