Class: Nanobot::Agent::Tools::ListDir
- Inherits:
-
RubyLLM::Tool
- Object
- RubyLLM::Tool
- Nanobot::Agent::Tools::ListDir
- 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
-
#execute(path:) ⇒ String
List the contents of a directory, sorted alphabetically.
-
#initialize(allowed_dir: nil) ⇒ ListDir
constructor
A new instance of ListDir.
Constructor Details
#initialize(allowed_dir: nil) ⇒ ListDir
Returns a new instance of ListDir.
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). : nil end |
Instance Method Details
#execute(path:) ⇒ String
List the contents of a directory, sorted alphabetically.
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). # 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 |