Class: Nanobot::Agent::Tools::ListDir

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

Overview

Tool for listing directory contents

Constant Summary

Constants included from WorkspaceSandbox

WorkspaceSandbox::PROTECTED_FILENAMES

Instance Method Summary collapse

Constructor Details

#initialize(allowed_dir: nil) ⇒ ListDir

Returns a new instance of ListDir.

Parameters:

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

    workspace directory to restrict access to



205
206
207
208
# File 'lib/nanobot/agent/tools/filesystem.rb', line 205

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

Instance Method Details

#execute(path:) ⇒ String

List the contents of a directory, sorted alphabetically.

Parameters:

  • path (String) —

    path to the directory to list

Returns:

  • (String) —

    newline-separated entries or error message



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/nanobot/agent/tools/filesystem.rb', line 213

def execute(path:)
  dir_path = Pathname.new(path).expand_path

  # Security check
  if @allowed_dir && !within_allowed_dir?(dir_path)
    return "Error: Access denied. Path is outside allowed directory: #{@allowed_dir}"
  end

  return "Error: Directory not found: #{path}" unless dir_path.exist?
  return "Error: Path is not a directory: #{path}" unless dir_path.directory?

  begin
    entries = dir_path.children.map do |entry|
      if entry.directory?
        "#{entry.basename}/"
      else
        entry.basename.to_s
      end
    end.sort

    entries.empty? ? '(empty directory)' : entries.join("\n")
  rescue StandardError => e
    "Error listing directory: #{e.message}"
  end
end