Class: LLM::Tool::ReadFile

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

Overview

The ReadFile class implements a tool that can read the contents of a file. It returns the content as structured lines ({lineno:, content:}), so the model can reference line numbers when requesting a narrower range.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#truncate, #truncate!

Methods inherited from LLM::Tool

NotFound, a2a, a2a?, #a2a?, abstract!, abstract?, clear_registry!, description, find_by_name!, #function, function, inherited, mcp, #mcp?, mcp?, name, #on_cancel, #on_interrupt, params, register, registry, set, subclasses, unregister

Methods included from Param

#defaults, #param, #required

Methods included from Function::Registry

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

Class Method Details

.max_bytes(bytes = UNDEFINED) ⇒ Integer

Returns (or sets) the advisory maximum number of bytes this tool returns to the model.

Parameters:

  • bytes (Integer, nil) (defaults to: UNDEFINED) —

    When given, sets the maximum

Returns:

  • (Integer)


27
28
29
30
31
32
33
# File 'lib/llm/tools/read_file.rb', line 27

def self.max_bytes(bytes = UNDEFINED)
  if bytes.equal?(UNDEFINED)
    @max_bytes || 75_000
  else
    @max_bytes = bytes
  end
end

Instance Method Details

#call(path:, start: 1, stop: -1,, max_bytes: self.class.max_bytes) ⇒ Hash

Parameters:

  • path (String)
  • start (Integer) (defaults to: 1)
  • stop (Integer) (defaults to: -1,)
  • max_bytes (Integer) (defaults to: self.class.max_bytes)

Returns:

  • (Hash)


41
42
43
44
45
# File 'lib/llm/tools/read_file.rb', line 41

def call(path:, start: 1, stop: -1, max_bytes: self.class.max_bytes)
  start, stop = swap_if_reversed(start, stop)
  content = read(path, start, stop)
  to_lines(content, start:, max_bytes:)
end