Class: RubyLLM::Toolbox::Tools::WebFetch

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

Overview

SAFE. Fetches a URL over http/https and returns its readable text (HTML is stripped to text). Every request and redirect hop passes through UrlGuard, so it can’t be pointed at internal/loopback/metadata addresses. Output is token-budgeted.

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:, unsafe: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_llm/toolbox/tools/web_fetch.rb', line 28

def execute(url:, unsafe: false)
  bypass = permit_unsafe!(unsafe, url)
  response = guarded_get(url, guard: !bypass)

  if response.status >= 400
    return error("HTTP #{response.status} from #{response.final_url}", code: :http_error)
  end

  content_type = content_type_of(response)
  text = extract_text(response.body, content_type)

  header = "GET #{response.final_url} -> #{response.status} (#{content_type})"
  truncate("#{header}\n\n#{text}".strip)
rescue Safety::UrlGuard::Blocked => e
  error(e.message, code: :url_blocked)
rescue HttpHelpers::FetchError => e
  error("fetch failed: #{e.message}", code: :fetch_failed)
end