Class: Legion::CLI::Chat::Tools::ReadFile

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

Instance Method Summary collapse

Instance Method Details

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/cli/chat/tools/read_file.rb', line 16

def execute(path:, offset: nil, limit: nil)
  expanded = File.expand_path(path)
  return "Error: file not found: #{path}" unless File.exist?(expanded)
  return "Error: path is a directory: #{path}" if File.directory?(expanded)

  lines = File.readlines(expanded, encoding: 'utf-8')
  start_line = [(offset || 1) - 1, 0].max
  count = limit || lines.length
  selected = lines[start_line, count] || []

  numbered = selected.each_with_index.map do |line, i|
    "#{(start_line + i + 1).to_s.rjust(5)} | #{line}"
  end

  "#{expanded} (#{lines.length} lines total)\n#{numbered.join}"
rescue StandardError => e
  Legion::Logging.warn("ReadFile#execute failed for #{path}: #{e.message}") if defined?(Legion::Logging)
  "Error reading #{path}: #{e.message}"
end