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

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

Class Method Summary collapse

Methods inherited from Tools::Base

deferred, deferred?, description, error_response, extension, handle_exception, input_schema, log, mcp_category, mcp_tier, runner, sticky, tags, text_response, tool_name, trigger_words

Class Method Details

.call(path:, offset: nil, limit: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/cli/chat/tools/read_file.rb', line 22

def self.call(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