Class: Nexo::Tools::ReadFile

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

Overview

Reads a file from the agent's sandbox. Authorizes :read before touching the sandbox; a denial or a missing file is returned as { error: ... } so the model can adapt instead of the loop crashing.

Constant Summary collapse

MAX_BYTES =

Returned content is capped to this many bytes (head), matching Tools::Fetch, so a single huge file can't blow a small context window. The read-before-write guard is unaffected — it records the file's mtime, not its content.

200_000

Instance Method Summary collapse

Constructor Details

#initialize(sandbox:, permissions:, tracker: nil) ⇒ ReadFile

tracker: is an optional ReadTracker shared with Tools::WriteFile for the read-before-write + stale guard. Default nil ⇒ nothing is recorded, preserving direct-construction behavior.



24
25
26
27
28
29
# File 'lib/nexo/tools/read_file.rb', line 24

def initialize(sandbox:, permissions:, tracker: nil)
  @sandbox = sandbox
  @permissions = permissions
  @tracker = tracker
  super()
end

Instance Method Details

#execute(path:) ⇒ Object

Authorizes :read, reads path from the sandbox, records (path, mtime) on the tracker (if any), and returns the content — or { error: ... } on a denied read or a missing file.



34
35
36
37
38
39
40
41
# File 'lib/nexo/tools/read_file.rb', line 34

def execute(path:)
  @permissions.authorize!(:read, path)
  content = @sandbox.read(path)
  @tracker&.record(path, @sandbox.mtime(path))
  cap(content)
rescue Permissions::Denied, Errno::ENOENT => e
  {error: e.message}
end