Class: Nanobot::Agent::Tools::ReadFile

Inherits:
RubyLLM::Tool
  • Object
show all
Includes:
WorkspaceSandbox
Defined in:
lib/nanobot/agent/tools/filesystem.rb

Overview

Tool for reading file contents

Constant Summary

Constants included from WorkspaceSandbox

WorkspaceSandbox::PROTECTED_FILENAMES

Instance Method Summary collapse

Constructor Details

#initialize(allowed_dir: nil) ⇒ ReadFile

Returns a new instance of ReadFile.

Parameters:

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

    workspace directory to restrict access to



74
75
76
77
# File 'lib/nanobot/agent/tools/filesystem.rb', line 74

def initialize(allowed_dir: nil)
  super()
  @allowed_dir = allowed_dir ? Pathname.new(allowed_dir).expand_path : nil
end

Instance Method Details

#execute(path:) ⇒ String

Read file contents from the given path.

Parameters:

  • path (String) —

    path to the file to read

Returns:

  • (String) —

    file contents or error message



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nanobot/agent/tools/filesystem.rb', line 82

def execute(path:)
  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

  return "Error: File not found: #{path}" unless file_path.exist?
  return "Error: Path is not a file: #{path}" unless file_path.file?

  begin
    file_path.read
  rescue StandardError => e
    "Error reading file: #{e.message}"
  end
end