Class: RubyLLM::Toolbox::Tools::DownloadFile

Inherits:
Base
  • Object
show all
Includes:
HttpHelpers
Defined in:
lib/ruby_llm/toolbox/tools/download_file.rb

Overview

EXEC. Downloads a URL to a file within fs_root. The request goes through UrlGuard (so it can’t be pointed at internal/loopback/metadata addresses), follows redirects safely, and is capped at config.max_fetch_bytes. The destination path is jailed to fs_root.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods included from HttpHelpers

#guarded_get, #guarded_request, #url_guard

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(url:, path:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_llm/toolbox/tools/download_file.rb', line 30

def execute(url:, path:)
  jail = Safety::PathJail.new(config.fs_root)
  real = jail.resolve(path)
  return error("path is a directory: #{path}", code: :is_a_directory) if File.directory?(real)

  response = guarded_get(url)
  if response.status >= 400
    return error("server returned HTTP #{response.status} for #{response.final_url}", code: :http_error)
  end

  body = response.body.to_s
  FileUtils.mkdir_p(File.dirname(real))
  File.binwrite(real, body)

  "Downloaded #{body.bytesize} bytes from #{response.final_url} to #{path}"
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
rescue Safety::UrlGuard::Blocked => e
  error(e.message, code: :url_blocked)
rescue HttpHelpers::FetchError => e
  error("download failed: #{e.message}", code: :fetch_failed)
end