Class: RubyLLM::Toolbox::Tools::ReadFile

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/toolbox/tools/read_file.rb

Overview

SAFE reference tool. Read-only, confined to fs_root, output budgeted. Loaded and usable by default.

Constant Summary collapse

MAX_BYTES =

refuse to slurp anything enormous

5 * 1024 * 1024

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(path:, start_line: nil, end_line: nil, tail: nil, unsafe: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm/toolbox/tools/read_file.rb', line 36

def execute(path:, start_line: nil, end_line: nil, tail: nil, unsafe: false)
  jail = path_jail(unsafe: unsafe, detail: path)
  real = jail.resolve(path)

  return error("not a file: #{path}", code: :not_a_file) unless File.file?(real)
  if File.size(real) > MAX_BYTES
    return error("file too large (> #{MAX_BYTES} bytes)", code: :too_large)
  end

  lines = File.readlines(real)
  selected = if tail && tail.to_i.positive?
               lines.last(tail.to_i)
             else
               slice_lines(lines, start_line, end_line)
             end
  return error("no lines in the given range", code: :empty_range) if selected.nil?

  truncate(selected.join.scrub)
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
end