Class FileFunctionTools

java.lang.Object
org.machanism.machai.gw.tools.FileFunctionTools
All Implemented Interfaces:
FunctionTools

public class FileFunctionTools extends Object implements FunctionTools
Installs file-system tools into a 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 text
  • write_file – writes a file (creating parent directories as needed)
  • list_files_in_directory – lists immediate children of a directory
Author:
Viktor Tovstyi
  • Field Details

    • DEFAULT_CHARSET

      private static final String DEFAULT_CHARSET
      Default character set used when reading or writing text files.
      See Also:
  • Constructor Details

    • FileFunctionTools

      public FileFunctionTools()
  • Method Details

    • listFiles

      public List<String> listFiles(File dirPath, File projectDir)
      Implements list_files_in_directory.

      This AI functional tool lists the immediate children of a directory.

      Parameters:
      dirPath - directory to list, resolved relative to projectDir
      projectDir - 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:

      1. JsonNode optionally containing dir_path
      2. File working directory
    • getRecursiveFiles

      public Object getRecursiveFiles(File dir, int max_count, File projectDir)
      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 scan
      max_count - the maximum number of files allowed in the result; throws an error if exceeded
      projectDir - the root project directory context
      Returns:
      a List of relative file path strings, or a message string indicating no files were found
      Throws:
      IllegalArgumentException - if the number of discovered files exceeds max_count
    • getRecursiveFolders

      public Object getRecursiveFolders(File dir, int max_count, File projectDir)
      Implements get_recursive_folder_list.

      This AI functional tool returns folders discovered below a directory.

      Parameters:
      path - directory path relative to projectDir
      max_count - maximum number of folders allowed in the result
      projectDir - 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 exceeds max_count

      Expected parameters:

      1. JsonNode optionally containing dir_path
      2. File working directory
    • writeFile

      public String writeFile(File filePath, String text, String charsetName, File projectDir)
      Implements write_file.

      This AI functional tool creates or replaces a file with the supplied text.

      Parameters:
      filePath - file to create or replace, relative to projectDir
      text - content to write
      charsetName - character set used to encode the content
      projectDir - project root used to resolve the file
      Returns:
      a success message or an error message when writing fails
    • writeFileContent

      private void writeFileContent(File file, String content, String charsetName) throws IOException
      Writes content to file using charsetName.
      Parameters:
      file - destination file
      content - content to write
      charsetName - 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 writes text using the requested character set.
      Parameters:
      file - file to create
      text - content
      charsetName - character set name
      filePath - original (relative) file path used for messaging
      Returns:
      success message
      Throws:
      IOException - if an I/O error occurs
    • readFile

      public String readFile(File filePath, String charsetName, File projectDir) throws IOException
      Implements read_file.

      This AI functional tool reads a file and returns its text content.

      Expected parameters:

      1. JsonNode containing file_path
      2. File working directory
      Parameters:
      filePath - file to read, relative to projectDir
      charsetName - character set used to decode the file
      projectDir - 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

      private File getFile(File filePath, File projectDir)
    • getRelativePath

      public static String getRelativePath(File dir, File file, boolean addSingleDot)
      Computes a project-relative path string.

      The returned path always uses forward slashes (/) for consistency across platforms.

      Parameters:
      dir - base directory used to relativize the file
      file - target file or directory
      addSingleDot - whether to prefix relative path with ./
      Returns:
      relative path, . if dir equals file, or null if file is not a descendant of dir
    • applyPatchToFile

      public String applyPatchToFile(File file, String patch, String charsetName, File projectDir)
      Implements apply_patch_to_file.

      This AI functional tool applies a targeted unified or simplified search-and-replace patch to a file within the project directory.