Class: Zephira::Tools::HttpRequest
- Defined in:
- lib/zephira/tools/http_request.rb
Instance Attribute Summary
Attributes inherited from BaseTool
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from BaseTool
announces_intent?, #arg, #error_result, #initialize, read_only?, run, #success_result, #validate
Constructor Details
This class inherits a constructor from Zephira::Tools::BaseTool
Class Method Details
.description ⇒ Object
14 15 16 |
# File 'lib/zephira/tools/http_request.rb', line 14 def description "Perform HTTP requests: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS" end |
.name ⇒ Object
10 11 12 |
# File 'lib/zephira/tools/http_request.rb', line 10 def name "http_request" end |
.parameters ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/zephira/tools/http_request.rb', line 18 def parameters { type: "object", properties: { intent: { type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')." }, method: { type: "string", enum: ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"] }, url: { type: "string", description: "Request URL" }, headers: { type: "object", description: "Request headers as key-value pairs" }, query: { type: "object", description: "Query parameters as key-value pairs" }, body: { type: ["string", "object"], description: "Request body as string or JSON object" }, timeout: { type: "number", description: "Timeout in seconds for open/read" } }, required: ["intent", "method", "url"] } end |
Instance Method Details
#run ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/zephira/tools/http_request.rb', line 56 def run http_method = arg(:method) url = arg(:url) headers = arg(:headers) || {} query = arg(:query) || {} body = arg(:body) timeout = arg(:timeout) uri = URI.parse(url) uri.query = URI.encode_www_form(query) if query.is_a?(Hash) && !query.empty? http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") http.open_timeout = timeout if timeout http.read_timeout = timeout if timeout request_class = case http_method.to_s.upcase when "GET" then Net::HTTP::Get when "POST" then Net::HTTP::Post when "PUT" then Net::HTTP::Put when "PATCH" then Net::HTTP::Patch when "DELETE" then Net::HTTP::Delete when "HEAD" then Net::HTTP::Head when "OPTIONS" then Net::HTTP::Options else return error_result(message: "Unsupported HTTP method: #{http_method}") end req = request_class.new(uri) headers.each { |key, value| req[key] = value.to_s } if body if body.is_a?(Hash) req.body = body.to_json req["Content-Type"] ||= "application/json" else req.body = body.to_s end end agent.status.verbose(" • #{http_method} #{url}") response = http.request(req) agent.status.verbose(" • Response: #{response.code}") agent.logger.info("#{http_method} #{url} -> #{response.code}") charset = response.type_params["charset"] || "UTF-8" body = response.body.to_s.dup.force_encoding(charset) body = body.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?") success_result(status: response.code.to_i, headers: response.each_header.to_h, body: body) rescue => error error_result(message: error.) end |