Class: Rubyagents::VisitWebpage
- Defined in:
- lib/rubyagents/tools/visit_webpage.rb
Instance Method Summary collapse
Methods inherited from Tool
description, inherited, input, inputs, output_type, to_prompt, to_schema, tool_name
Instance Method Details
#call(url:) ⇒ Object
14 15 16 17 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 |
# File 'lib/rubyagents/tools/visit_webpage.rb', line 14 def call(url:) uri = URI(url) uri = URI("https://#{url}") unless uri.scheme http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == "https" http.read_timeout = 15 http.open_timeout = 10 request = Net::HTTP::Get.new(uri) request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36" request["Accept"] = "text/html" response = http.request(request) # Follow redirects (one level) if response.is_a?(Net::HTTPRedirection) && response["location"] return call(url: response["location"]) end body = response.body .force_encoding("UTF-8") .encode("UTF-8", invalid: :replace, undef: :replace, replace: "") md = ReverseMarkdown.convert(body, unknown_tags: :bypass, github_flavored: true).strip md.empty? ? "No readable content found at #{url}" : md[0, 10_000] rescue => e "Error fetching #{url}: #{e.}" end |