Class: Brute::Tools::FSRead
- Inherits:
-
RubyLLM::Tool
- Object
- RubyLLM::Tool
- Brute::Tools::FSRead
- Defined in:
- lib/brute/tools/fs_read.rb
Overview
Existing features (ref: opencode read tool):
-
Default line limit — cap reads at 2000 lines when no start_line/end_line given, instead of reading the entire file.
-
Byte cap — stop reading when cumulative output exceeds 50 KB (MAX_BYTES). Whichever limit (lines or bytes) is hit first wins.
-
Per-line truncation — truncate individual lines longer than 2000 chars with a suffix like “… (line truncated to 2000 chars)”.
-
Pagination hint — when output is truncated, append a hint: “(Showing lines 1-N of M. Use start_line=N+1 to continue.)” When reading completes, append “(End of file - total N lines)”.
-
Binary file detection — read first 4 KB sample, check for null bytes and known binary extensions (.zip, .exe, .so, .pyc, etc.). Reject with “Cannot read binary file: <path>”.
-
Directory listing — when file_path points to a directory, list entries (paginated, respecting limit) instead of raising an error.
-
File-not-found suggestions — on miss, scan the parent directory for similar names and suggest “Did you mean…?” candidates.
-
Return a plain string instead of a Hash — avoids the .to_s repr bloat when ToolCall coerces the result for the LLM message.
Constant Summary collapse
- BINARY_EXTENSIONS =
%w[.zip .exe .so .pyc .pyo .dll .dylib .bin .o .a .tar .gz .bz2 .xz .7z .rar .jar .war .class .png .jpg .jpeg .gif .bmp .ico .pdf .woff .woff2 .ttf .eot .mp3 .mp4 .avi .mov .flv .wmv .db .sqlite .sqlite3].freeze
- DEFAULT_LINE_CAP =
2000- MAX_BYTES =
Brute::Truncation::MAX_BYTES
- MAX_LINE_LENGTH =
Brute::Truncation::MAX_LINE_LENGTH
Instance Method Summary collapse
Instance Method Details
#execute(file_path:, start_line: nil, end_line: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/brute/tools/fs_read.rb', line 46 def execute(file_path:, start_line: nil, end_line: nil) path = File.(file_path) # Directory listing return list_directory(path) if File.directory?(path) # File-not-found suggestions unless File.exist?(path) suggestions = find_similar(path) msg = "File not found: #{path}" msg += ". Did you mean: #{suggestions.join(', ')}?" if suggestions.any? raise msg end raise "Not a file: #{path}" unless File.file?(path) # Binary file detection ext = File.extname(path).downcase raise "Cannot read binary file: #{path}" if BINARY_EXTENSIONS.include?(ext) sample = File.binread(path, 4096) || "" raise "Cannot read binary file: #{path}" if sample.include?("\x00") lines = File.readlines(path) total = lines.size first = start_line ? [start_line - 1, 0].max : 0 # Apply default line cap when no explicit range given default_last = end_line ? [end_line - 1, total - 1].min : [first + DEFAULT_LINE_CAP - 1, total - 1].min last = default_last selected = lines[first..last] || [] # Per-line truncation + byte cap numbered = [] bytes = 0 selected.each_with_index do |line, i| truncated_line = Brute::Truncation.truncate_line(line, max: MAX_LINE_LENGTH) numbered_line = "#{first + i + 1}\t#{truncated_line}" break if bytes + numbered_line.bytesize > MAX_BYTES numbered << numbered_line bytes += numbered_line.bytesize end actual_last = first + numbered.size - 1 content = numbered.join truncated = (actual_last < total - 1) && end_line.nil? if truncated content + "\n(Showing lines #{first + 1}-#{actual_last + 1} of #{total}. Use start_line=#{actual_last + 2} to continue.)" else content end end |
#name ⇒ Object
39 |
# File 'lib/brute/tools/fs_read.rb', line 39 def name; "read"; end |