Class: RailsAgents::Skills::Portable::WebFetch

Inherits:
Tool
  • Object
show all
Defined in:
lib/rails_agents/skills/portable/web_fetch.rb

Instance Method Summary collapse

Methods inherited from Tool

definition, description, inherited, param, tool_name

Instance Method Details

#call(url:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rails_agents/skills/portable/web_fetch.rb', line 12

def call(url:)
  response = Faraday.get(url, nil, {"User-Agent" => "RailsAgents/1.0"})
  return {error: "Fetch failed", url: url, status: response.status} unless response.success?

  text = response.body.gsub(/<script.*?>.*?<\/script>/m, "")
    .gsub(/<style.*?>.*?<\/style>/m, "")
    .gsub(/<[^>]+>/, " ")
    .gsub(/\s+/, " ")
    .strip

  {url: url, content: text[0, 8_000]}
rescue StandardError => e
  {error: e.message, url: url}
end