Class: RubyLLM::Toolbox::Tools::HttpRequest

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

Overview

A general HTTP client, UrlGuard-protected. Read methods (GET/HEAD) work by default; mutating methods (POST/PUT/PATCH/DELETE) require enable_exec_tools, so the safe default can’t change remote state. Returns status, key headers, and the (token-budgeted) body.

Constant Summary collapse

READ_METHODS =
%w[GET HEAD].freeze
MUTATING_METHODS =
%w[POST PUT PATCH DELETE].freeze
SHOWN_HEADERS =
%w[content-type content-length location etag last-modified].freeze

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:, method: "GET", headers: nil, body: nil, unsafe: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_llm/toolbox/tools/http_request.rb', line 41

def execute(url:, method: "GET", headers: nil, body: nil, unsafe: false)
  verb = method.to_s.strip.upcase
  verb = "GET" if verb.empty?
  return error("unsupported method: #{verb}", code: :bad_method) unless (READ_METHODS + MUTATING_METHODS).include?(verb)
  if MUTATING_METHODS.include?(verb) && !config.enable_exec_tools
    return error("#{verb} requires enable_exec_tools = true (mutating request)", code: :exec_disabled)
  end

  bypass = permit_unsafe!(unsafe, url)
  response = guarded_request(verb, url, headers: headers, body: body, guard: !bypass)
  return error("HTTP #{response.status} from #{response.final_url}", code: :http_error) if response.status >= 400

  truncate(format_response(verb, response))
rescue Safety::UrlGuard::Blocked => e
  error(e.message, code: :url_blocked)
rescue HttpHelpers::FetchError => e
  error("request failed: #{e.message}", code: :request_failed)
end