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!, #initialize, #within_sandbox?

Instance Method Details

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tools/list_files.rb', line 11

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[:name].include?(filter) }
  end

  Genie.output listing.map { |e| e[:name] }.join("\n") + "\n", color: :green

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