Class: Nanobot::Agent::Tools::WriteFile
- Inherits:
-
RubyLLM::Tool
- Object
- RubyLLM::Tool
- Nanobot::Agent::Tools::WriteFile
- Includes:
- WorkspaceSandbox
- Defined in:
- lib/nanobot/agent/tools/filesystem.rb
Overview
Tool for writing file contents
Constant Summary
Constants included from WorkspaceSandbox
Nanobot::Agent::Tools::WorkspaceSandbox::PROTECTED_FILENAMES
Instance Method Summary collapse
-
#execute(path:, content:) ⇒ String
Write content to a file, creating parent directories as needed.
-
#initialize(allowed_dir: nil) ⇒ WriteFile
constructor
A new instance of WriteFile.
Constructor Details
#initialize(allowed_dir: nil) ⇒ WriteFile
Returns a new instance of WriteFile.
110 111 112 113 |
# File 'lib/nanobot/agent/tools/filesystem.rb', line 110 def initialize(allowed_dir: nil) super() @allowed_dir = allowed_dir ? Pathname.new(allowed_dir). : nil end |
Instance Method Details
#execute(path:, content:) ⇒ String
Write content to a file, creating parent directories as needed.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/nanobot/agent/tools/filesystem.rb', line 119 def execute(path:, content:) file_path = Pathname.new(path). # Security check if @allowed_dir && !within_allowed_dir?(file_path) return "Error: Access denied. Path is outside allowed directory: #{@allowed_dir}" end if protected_path?(file_path) return "Error: Access denied. Bootstrap file #{file_path.basename} is read-only." end begin # Ensure parent directory exists file_path.dirname.mkpath unless file_path.dirname.exist? file_path.write(content) "Successfully wrote #{content.length} characters to #{path}" rescue StandardError => e "Error writing file: #{e.}" end end |