Class: RubyLLM::Toolbox::Tools::WriteFile

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/toolbox/tools/write_file.rb

Overview

EXEC. Creates or overwrites a text file within fs_root. Missing parent directories are created (inside the jail). Path traversal and symlink escapes are rejected by PathJail.

Constant Summary collapse

MAX_BYTES =
10 * 1024 * 1024

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(path:, content:, unsafe: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/toolbox/tools/write_file.rb', line 32

def execute(path:, content:, unsafe: false)
  body = content.to_s
  return error("content too large (> #{MAX_BYTES} bytes)", code: :too_large) if body.bytesize > MAX_BYTES

  jail = path_jail(unsafe: unsafe, detail: path)
  real = jail.resolve(path)
  return error("path is a directory: #{path}", code: :is_a_directory) if File.directory?(real)

  existed = File.exist?(real)
  FileUtils.mkdir_p(File.dirname(real))
  File.write(real, body)

  verb = existed ? "Overwrote" : "Created"
  "#{verb} #{path} (#{body.bytesize} bytes)"
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
end