Class: AgentC::Tools::ReadFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace_dir: nil) ⇒ ReadFile

Returns a new instance of ReadFile.

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/agent_c/tools/read_file.rb', line 35

def initialize(workspace_dir: nil, **)
  raise ArgumentError, "workspace_dir is required" unless workspace_dir
  @workspace_dir = workspace_dir
end

Instance Attribute Details

#workspace_dirObject (readonly)

Returns the value of attribute workspace_dir.



34
35
36
# File 'lib/agent_c/tools/read_file.rb', line 34

def workspace_dir
  @workspace_dir
end

Instance Method Details

#execute(path:, line_range_start: 0, line_range_end: nil, **params) ⇒ Object



41
42
43
44
45
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
# File 'lib/agent_c/tools/read_file.rb', line 41

def execute(path:, line_range_start: 0, line_range_end: nil, **params)
  if params.any?
    return "The following params were passed but are not allowed: #{params.keys.join(",")}"
  end

  unless Paths.allowed?(workspace_dir, path)
    return "Path: #{path} not acceptable. Must be a child of directory: #{workspace_dir}."
  end

  workspace_path = Paths.relative_to_dir(workspace_dir, path)

  unless File.exist?(workspace_path)
    return "File not found"
  end

  all_lines = File.read(workspace_path).split("\n")
  lines = all_lines[line_range_start..line_range_end]

  total_lines = lines.length
  max_lines = 300

  # Determine the actual line range being returned (1-indexed for display)
  actual_start = line_range_start + 1
  actual_end = line_range_end ? [line_range_end + 1, all_lines.length].min : all_lines.length

  if total_lines > max_lines
    limited_content = format_with_line_numbers(lines[0...max_lines], actual_start)
    limited_end = actual_start + max_lines - 1
    "Returning lines #{actual_start}-#{limited_end}, out of #{total_lines} total lines:\n\n#{limited_content}"
  elsif line_range_start > 0 || line_range_end
    # Line range was specified
    formatted_content = format_with_line_numbers(lines, actual_start)
    "Returning lines #{actual_start}-#{actual_end}, out of #{all_lines.length} total lines:\n\n#{formatted_content}"
  else
    format_with_line_numbers(lines, 1)
  end
rescue => e
  "File read error: #{e.class}:#{e.message}"
end