Class: Nexo::Tools::WriteFile

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

Overview

Writes content to a file in the agent's sandbox. Authorizes :write first; a denial is returned as { error: ... } (and nothing is written).

Instance Method Summary collapse

Constructor Details

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

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



15
16
17
18
19
20
# File 'lib/nexo/tools/write_file.rb', line 15

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

Instance Method Details

#execute(path:, content:) ⇒ Object

Authorizes :write, applies the read-before-write + stale guard, then writes content to path and returns { ok: true, path: } — or { error: ... } on a denial or a guard violation (nothing is written).



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nexo/tools/write_file.rb', line 25

def execute(path:, content:)
  @permissions.authorize!(:write, path)
  if (guard_error = clobber_guard(path))
    return {error: guard_error}
  end

  @sandbox.write(path, content)
  {ok: true, path: path}
rescue Permissions::Denied => e
  {error: e.message}
end