Class: Genie::ReadFile

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

Instance Method Summary collapse

Methods included from SandboxedFileTool

#enforce_sandbox!, #initialize, #within_sandbox?

Instance Method Details

#execute(filepath:, include_line_numbers: false) ⇒ Object



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

def execute(filepath:, include_line_numbers: false)
  filepath = File.expand_path(filepath)

  Genie.output "Reading file: #{filepath}", color: :blue

  enforce_sandbox!(filepath)

  lines = File.readlines(filepath)
  contents = if include_line_numbers
    width = lines.size.to_s.length
    lines.each_with_index.map do |line, index|
      number = (index + 1).to_s.rjust(width)
      "#{number}: #{line}"
    end.join
  else
    lines.join
  end

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

  { error: e.message }
end