Class: Genie::ListFiles

Inherits:
RubyLLM::Tool
  • Object
show all
Includes:
SandboxedFileTool
Defined in:
lib/tools/list_files.rb

Instance Method Summary collapse

Methods included from SandboxedFileTool

#enforce_sandbox!, #within_sandbox?

Constructor Details

#initialize(base_path:, ignore_paths: []) ⇒ ListFiles

Returns a new instance of ListFiles.



11
12
13
14
15
16
# File 'lib/tools/list_files.rb', line 11

def initialize(base_path:, ignore_paths: [])
  @base_path = base_path
  @base_path.freeze

  @ignore_paths = ignore_paths
end

Instance Method Details

#execute(directory: '.', recursive: false, filter: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tools/list_files.rb', line 18

def execute(directory: '.', recursive: false, filter: nil)
  directory = File.expand_path(directory, @base_path)

  Genie.output "Listing files in directory: #{directory} (recursive: #{recursive})", color: :blue

  raise ArgumentError, "Directory not allowed: #{directory}. Must be within base path: #{@base_path}" unless directory.start_with?(@base_path)

  listing = recursive ? list_recursive(directory) : list_non_recursive(directory)

  # Apply filter if provided
  if filter && !filter.empty?
    listing = listing.select { |entry| entry.include?(filter) }
  end

  # Apply ignore paths
  listing.reject! { |entry| @ignore_paths.any? { |ignore_path| entry.start_with?(ignore_path) } }

  Genie.output listing.join("\n") + "\n", color: :green

  listing.join("\n")
rescue => e
  Genie.output "Error: #{e.message}", color: :red
  { error: e.message }
end