Class: LLM::Tool::ReadFile

Inherits:
LLM::Tool show all
Defined in:
lib/llm/tools/read_file.rb

Overview

The LLM::Tool::ReadFile class implements a tool that can read the contents of a file. The tool accepts two optional offsets: a start line, and a stop line. Without either the entire file contents are read into memory.

Instance Method Summary collapse

Methods inherited from LLM::Tool

a2a, a2a?, #a2a?, clear_registry!, description, find_by_name!, function, #function, inherited, mcp, mcp?, #mcp?, name, #on_cancel, #on_interrupt, params, register, registry, skill?, #skill?, unregister

Methods included from Param

#param, #required

Methods included from Function::Registry

#clear_registry!, extended, #find_by_name, #lock, #register, #registry, #registry_key, #tool_name, #unregister

Instance Method Details

#call(path:, start: 1, stop: -1)) ⇒ Hash

Parameters:

  • path (String)
  • start (Integer) (defaults to: 1)
  • stop (Integer) (defaults to: -1))

Returns:

  • (Hash)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/llm/tools/read_file.rb', line 24

def call(path:, start: 1, stop: -1)
  content, cursor = nil, 1
  File.open(path, "r") do |f|
    while cursor < start
      f.gets
      cursor += 1
    end
    if stop == -1
      content = f.read
    else
      content = start.upto(stop).map { f.gets }.join
    end
  end
  {ok: true, content:}
end