Mojentic.LLM.Tools.FilesystemGateway (Mojentic v1.2.0)

Copy Markdown View Source

A gateway for interacting with the filesystem within a sandboxed base path.

This module provides safe filesystem operations that are restricted to a specific base directory, preventing path traversal attacks.

Summary

Functions

Finds files matching a glob pattern.

Finds files containing text matching a regex pattern.

Finds all lines in a file matching a regex pattern.

Lists all files recursively in a directory.

Lists files in a directory (non-recursive).

Creates a new FilesystemGateway with the specified base path.

Reads the content of a file.

Resolves a path relative to the base path and ensures it stays within the sandbox.

Writes content to a file.

Types

t()

@type t() :: %Mojentic.LLM.Tools.FilesystemGateway{base_path: String.t()}

Functions

find_files_by_glob(fs, path, pattern)

@spec find_files_by_glob(t(), String.t(), String.t()) ::
  {:ok, [String.t()]} | {:error, String.t()}

Finds files matching a glob pattern.

find_files_containing(fs, path, pattern)

@spec find_files_containing(t(), String.t(), String.t()) ::
  {:ok, [String.t()]} | {:error, String.t()}

Finds files containing text matching a regex pattern.

find_lines_matching(fs, path, file_name, pattern)

@spec find_lines_matching(t(), String.t(), String.t(), String.t()) ::
  {:ok, [map()]} | {:error, String.t()}

Finds all lines in a file matching a regex pattern.

list_all_files(fs, path)

@spec list_all_files(t(), String.t()) :: {:ok, [String.t()]} | {:error, String.t()}

Lists all files recursively in a directory.

ls(fs, path)

@spec ls(t(), String.t()) :: {:ok, [String.t()]} | {:error, String.t()}

Lists files in a directory (non-recursive).

new(base_path)

@spec new(String.t()) :: {:ok, t()} | {:error, String.t()}

Creates a new FilesystemGateway with the specified base path.

read(fs, path, file_name)

@spec read(t(), String.t(), String.t()) :: {:ok, String.t()} | {:error, String.t()}

Reads the content of a file.

resolve_path(filesystem_gateway, path)

@spec resolve_path(t(), String.t()) :: {:ok, String.t()} | {:error, String.t()}

Resolves a path relative to the base path and ensures it stays within the sandbox.

write(fs, path, file_name, content)

@spec write(t(), String.t(), String.t(), String.t()) :: :ok | {:error, String.t()}

Writes content to a file.