Class: RobotLab::To::Tools::Read

Inherits:
FileTool
  • Object
show all
Defined in:
lib/robot_lab/to/tools/read.rb

Overview

Read a UTF-8 text file, optionally a line range. Output is capped so a small local model's context can't be blown by one oversized read (mirrors little-coder's read-guard intent).

Constant Summary collapse

MAX_LINES =
2000

Instance Method Summary collapse

Methods inherited from FileTool

#name, name_segment, short_name

Instance Method Details

#execute(path:, offset: nil, limit: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/robot_lab/to/tools/read.rb', line 21

def execute(path:, offset: nil, limit: nil, **)
  resolved = File.expand_path(path, Dir.pwd)
  return "Error: file not found: #{path}" unless File.file?(resolved)

  lines = File.readlines(resolved)
  slice = select_lines(lines, offset, limit)
  render(slice, lines.size, offset)
rescue SystemCallError => e
  "Error reading #{path}: #{e.message}"
end

#select_lines(lines, offset, limit) ⇒ Array<String>

Returns the chosen lines, capped at MAX_LINES.

Returns:

  • (Array<String>)

    the chosen lines, capped at MAX_LINES



33
34
35
36
37
# File 'lib/robot_lab/to/tools/read.rb', line 33

def select_lines(lines, offset, limit)
  start = offset ? [offset.to_i - 1, 0].max : 0
  count = limit ? limit.to_i : MAX_LINES
  lines[start, [count, MAX_LINES].min] || []
end