Class LocalFiles

java.lang.Object
uno.anahata.ai.tools.spi.LocalFiles

public class LocalFiles extends Object
A comprehensive, context-aware file operations tool provider.

This is the primary tool for all file I/O. it uses FileInfo POJOs and timestamps to implement safe, version-aware file modifications and stateful resource tracking.

Author:
AI
  • Constructor Details

    • LocalFiles

      public LocalFiles()
  • Method Details

    • addToUserPrompt

      public static MultiPartResponse addToUserPrompt(List<String> absolutePath) throws IOException
      Adds the contents of specified files as blobs to the next user feedback message.
      Parameters:
      absolutePath - A list of absolute file paths.
      Returns:
      A MultiPartResponse containing the file paths.
      Throws:
      IOException - if an I/O error occurs.
    • readBinaryFile

      public static byte[] readBinaryFile(String path) throws IOException
      Reads a binary file and returns its raw bytes.
      Parameters:
      path - The absolute path to the file.
      Returns:
      The file content as a byte array.
      Throws:
      IOException - if the file is not found or is a directory.
    • readFile

      public static FileInfo readFile(String path) throws IOException
      Reads a text file and returns a FileInfo object.

      This method includes a "Redundant Read Check" to prevent reloading files that are already valid in the context.

      Parameters:
      path - The absolute path to the file.
      Returns:
      A FileInfo object containing content and metadata.
      Throws:
      IOException - if the file is not found or is a directory.
    • writeFile

      public static FileInfo writeFile(String path, String content, long lastModified) throws IOException
      Writes content to an existing file, using optimistic locking.
      Parameters:
      path - The absolute path to the file.
      content - The new content to write.
      lastModified - The expected last modified timestamp for safety.
      Returns:
      An updated FileInfo object.
      Throws:
      IOException - if a modification conflict occurs or the file is missing.
    • createFile

      public static FileInfo createFile(String path, String content) throws IOException
      Creates a new file with the given content.
      Parameters:
      path - The absolute path to the file.
      content - The initial content.
      Returns:
      A FileInfo object for the new file.
      Throws:
      IOException - if the path already exists.
    • appendToFile

      public static FileInfo appendToFile(String path, String content) throws IOException
      Appends content to the end of a file.
      Parameters:
      path - The absolute path to the file.
      content - The content to append.
      Returns:
      An updated FileInfo object.
      Throws:
      IOException - if an I/O error occurs.
    • deleteFile

      public static String deleteFile(String path) throws IOException
      Deletes a file and prunes it from the context.
      Parameters:
      path - The absolute path to the file.
      Returns:
      A success message.
      Throws:
      IOException - if the file is not found.
    • moveFile

      public static String moveFile(String sourcePath, String targetPath) throws IOException
      Moves or renames a file.
      Parameters:
      sourcePath - The current path.
      targetPath - The destination path.
      Returns:
      A success message.
      Throws:
      IOException - if the source is missing or target exists.
    • copyFile

      public static String copyFile(String sourcePath, String destinationPath) throws IOException
      Copies a file.
      Parameters:
      sourcePath - The source path.
      destinationPath - The destination path.
      Returns:
      A success message.
      Throws:
      IOException - if the source is missing or target exists.
    • createDirectories

      public static String createDirectories(String path) throws IOException
      Creates a directory structure.
      Parameters:
      path - The absolute path to create.
      Returns:
      A success message.
      Throws:
      IOException - if an I/O error occurs.
    • fileExists

      public static boolean fileExists(String path)
      Checks if a file or directory exists.
      Parameters:
      path - The absolute path to check.
      Returns:
      true if it exists.
    • listDirectory

      public static List<String> listDirectory(String path) throws IOException
      Lists the contents of a directory.
      Parameters:
      path - The absolute path to list.
      Returns:
      A list of strings describing the directory entries.
      Throws:
      IOException - if the path is not a directory.