Class: PromptObjects::Primitives::HttpGet

Inherits:
PromptObjects::Primitive show all
Defined in:
lib/prompt_objects/primitives/http_get.rb

Overview

Primitive capability to fetch content from a URL.

Instance Attribute Summary

Attributes inherited from Capability

#state

Instance Method Summary collapse

Methods inherited from PromptObjects::Primitive

#initialize

Methods inherited from Capability

#descriptor, execution_policy, execution_policy_definition, #execution_policy_definition, #execution_policy_signature, #initialize, #resolved_execution_policy

Constructor Details

This class inherits a constructor from PromptObjects::Primitive

Instance Method Details

#descriptionObject



16
17
18
# File 'lib/prompt_objects/primitives/http_get.rb', line 16

def description
  "Fetch content from a URL via HTTP GET request"
end

#nameObject



12
13
14
# File 'lib/prompt_objects/primitives/http_get.rb', line 12

def name
  "http_get"
end

#parametersObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/prompt_objects/primitives/http_get.rb', line 20

def parameters
  {
    type: "object",
    properties: {
      url: {
        type: "string",
        description: "The URL to fetch"
      }
    },
    required: ["url"]
  }
end

#receive(message, context:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/prompt_objects/primitives/http_get.rb', line 33

def receive(message, context:)
  url = message[:url] || message["url"]

  return "Error: URL is required" if url.nil? || url.empty?

  begin
    uri = URI.parse(url)

    unless %w[http https].include?(uri.scheme)
      return "Error: Only http and https URLs are supported"
    end

    response = Net::HTTP.get_response(uri)

    case response
    when Net::HTTPSuccess
      content = response.body.to_s

      # Truncate very large responses
      if content.length > 50_000
        content = content[0, 50_000] + "\n\n... [truncated, response is #{content.length} bytes]"
      end

      content
    when Net::HTTPRedirection
      "Redirected to: #{response['location']}"
    else
      "HTTP Error: #{response.code} #{response.message}"
    end
  rescue URI::InvalidURIError
    "Error: Invalid URL format"
  rescue SocketError => e
    "Error: Could not connect - #{e.message}"
  rescue Timeout::Error
    "Error: Request timed out"
  rescue StandardError => e
    "Error fetching URL: #{e.message}"
  end
end