Class FileFunctionTools
- All Implemented Interfaces:
FunctionTools
Genai.
Tools in this installer are intended for host-integrated use where the host controls the base working directory. All path 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 FilegetRecursiveFiles(File dir, int max_count, File projectDir) Lists files recursively in a directory up to a specified maximum limit.getRecursiveFolders(File dir, int max_count, File projectDir) Implementsget_recursive_folder_list.static StringgetRelativePath(File dir, File file, boolean addSingleDot) Computes a project-relative path string.Implementslist_files_in_directory.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
Implementslist_files_in_directory.This AI functional tool lists the immediate children of a directory.
- Parameters:
dirPath- directory to list, resolved relative toprojectDirprojectDir- project root used to resolve the directory- Returns:
- project-relative paths of immediate children, or an empty list when
the path is not a directory
Expected parameters:
JsonNodeoptionally containingdir_pathFileworking directory
-
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 scanmax_count- 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:
IllegalArgumentException- if the number of discovered files exceedsmax_count
-
getRecursiveFolders
Implementsget_recursive_folder_list.This AI functional tool returns folders discovered below a directory.
- Parameters:
path- directory path relative toprojectDirmax_count- maximum number of folders allowed in the resultprojectDir- project root used to resolve the directory- Returns:
- project-relative folder paths, or a message when none are found
- Throws:
IllegalArgumentException- if the number of discovered folders exceedsmax_countExpected parameters:
JsonNodeoptionally containingdir_pathFileworking directory
-
writeFile
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
-
writeFileContent
WritescontenttofileusingcharsetName.- Parameters:
file- destination filecontent- content to writecharsetName- character set name- Throws:
IOException- if writing fails
-
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 occurs
-
readFile
Implementsread_file.This AI functional tool reads a file and returns its text content.
Expected parameters:
JsonNodecontainingfile_pathFileworking directory
- Parameters:
filePath- file to read, relative toprojectDircharsetName- character set used to decode the fileprojectDir- project root used to resolve the file- Returns:
- the file contents as text
- Throws:
IOException- if the path is not a regular file or cannot be read
-
getFile
-
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, ornulliffileis not a descendant ofdir
-
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.
-