Class LocalFilesystemBackend

java.lang.Object
com.alibaba.cloud.ai.graph.agent.extension.file.LocalFilesystemBackend
All Implemented Interfaces:
FilesystemBackend

public class LocalFilesystemBackend extends Object implements FilesystemBackend
Backend that reads and writes files directly from the filesystem. Files are accessed using their actual filesystem paths. Relative paths are resolved relative to the current working directory. Content is read/written as plain text, and metadata (timestamps) are derived from filesystem stats. Security and search upgrades: - Secure path resolution with root containment when in virtual_mode (sandboxed to cwd) - Prevent symlink-following on file I/O - Ripgrep-powered grep with JSON parsing, plus Java fallback with regex and optional glob include filtering, while preserving virtual path behavior
  • Constructor Details

    • LocalFilesystemBackend

      public LocalFilesystemBackend(String rootDir, boolean virtualMode, int maxFileSizeMb)
      Initialize filesystem backend.
      Parameters:
      rootDir - Optional root directory for file operations. If provided, all file paths will be resolved relative to this directory. If not provided, uses the current working directory.
      virtualMode - When true, treat incoming paths as virtual absolute paths under cwd, disallow traversal (.., ~) and ensure resolved path stays within root.
      maxFileSizeMb - Maximum file size in MB for reading operations
    • LocalFilesystemBackend

      public LocalFilesystemBackend(String rootDir)
  • Method Details

    • lsInfo

      public List<FileInfo> lsInfo(String path)
      Description copied from interface: FilesystemBackend
      List files and directories in the specified directory (non-recursive).
      Specified by:
      lsInfo in interface FilesystemBackend
      Parameters:
      path - Absolute directory path to list files from
      Returns:
      List of FileInfo objects for files and directories directly in the directory. Directories have a trailing / in their path and isDir=true.
    • read

      public String read(String filePath, int offset, int limit)
      Description copied from interface: FilesystemBackend
      Read file content with line numbers and pagination.
      Specified by:
      read in interface FilesystemBackend
      Parameters:
      filePath - Absolute or relative file path
      offset - Line offset to start reading from (0-indexed)
      limit - Maximum number of lines to read
      Returns:
      Formatted file content with line numbers, or error message
    • write

      public WriteResult write(String filePath, String content)
      Description copied from interface: FilesystemBackend
      Write content to a new file.
      Specified by:
      write in interface FilesystemBackend
      Parameters:
      filePath - Absolute or relative file path
      content - Content to write
      Returns:
      WriteResult with path or error. External storage sets filesUpdate=null.
    • edit

      public EditResult edit(String filePath, String oldString, String newString, boolean replaceAll)
      Description copied from interface: FilesystemBackend
      Edit file by replacing string occurrences.
      Specified by:
      edit in interface FilesystemBackend
      Parameters:
      filePath - Absolute or relative file path
      oldString - String to replace
      newString - Replacement string
      replaceAll - Whether to replace all occurrences
      Returns:
      EditResult with occurrences count or error. External storage sets filesUpdate=null.
    • globInfo

      public List<FileInfo> globInfo(String pattern, String path)
      Description copied from interface: FilesystemBackend
      Find files matching glob pattern.
      Specified by:
      globInfo in interface FilesystemBackend
      Parameters:
      pattern - Glob pattern (e.g., "*.py", "**​/*.ts")
      path - Base path to search from
      Returns:
      List of FileInfo objects for matching files
    • grepRaw

      public Object grepRaw(String pattern, String path, String glob)
      Description copied from interface: FilesystemBackend
      Search for pattern in files using regex.
      Specified by:
      grepRaw in interface FilesystemBackend
      Parameters:
      pattern - Regex pattern to search for
      path - Base path to search from (null for current directory)
      glob - Glob pattern to filter files (null for all files)
      Returns:
      List of GrepMatch objects or error message