Class: Nanobot::Agent::Tools::WriteFile

Inherits:
RubyLLM::Tool
  • Object
show all
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

Constructor Details

#initialize(allowed_dir: nil) ⇒ WriteFile

Returns a new instance of WriteFile.

Parameters:

  • allowed_dir (String, nil) (defaults to: nil) —

    workspace directory to restrict access to



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).expand_path : nil
end

Instance Method Details

#execute(path:, content:) ⇒ String

Write content to a file, creating parent directories as needed.

Parameters:

  • path (String) —

    path to the file to write

  • content (String) —

    content to write

Returns:

  • (String) —

    success message or error message



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).expand_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.message}"
  end
end