Class: OllamaAgent::Tools::HttpPost

Inherits:
Base
  • Object
show all
Defined in:
lib/ollama_agent/tools/http_tools.rb

Overview

HTTP POST tool — for sending data to APIs

Constant Summary

Constants inherited from Base

Base::RISK_LEVELS

Instance Attribute Summary

Attributes inherited from Base

#description, #input_schema, #name, #output_schema, #requires_approval, #risk_level

Instance Method Summary collapse

Methods inherited from Base

#to_anthropic_schema, #to_ollama_schema, #to_s, tool_description, tool_name, tool_output_schema, tool_requires_approval, tool_risk, tool_schema

Constructor Details

#initialize(allowed_hosts: nil, timeout: 30, **_opts) ⇒ HttpPost

Returns a new instance of HttpPost.



173
174
175
176
177
# File 'lib/ollama_agent/tools/http_tools.rb', line 173

def initialize(allowed_hosts: nil, timeout: 30, **_opts)
  super()
  @allowed_hosts = allowed_hosts
  @timeout       = timeout
end

Instance Method Details

#call(args, context: {}) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ollama_agent/tools/http_tools.rb', line 180

def call(args, context: {})
  return "http_post is disabled in read-only mode" if context[:read_only]

  url  = args["url"].to_s.strip
  body = args["body"]

  uri = URI.parse(url)
  raise OllamaAgent::Error, "http_post: only https/http URLs" unless %w[http https].include?(uri.scheme)

  raise OllamaAgent::Error, "http_post: host #{uri.host} not on allowlist" if @allowed_hosts&.none? { |pat| HttpHostPattern.match?(pat, uri.host) }

  headers = (args["headers"] || {}).merge("Content-Type" => "application/json")

  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
                                      read_timeout: @timeout, open_timeout: 5) do |http|
    req = Net::HTTP::Post.new(uri)
    headers.each { |k, v| req[k] = v }
    req.body = JSON.generate(body)
    resp     = http.request(req)
    "HTTP #{resp.code}: #{resp.body.to_s.byteslice(0, 8192)}"
  end
rescue StandardError => e
  "Error: #{e.message}"
end