Class: Utcp::Providers::SseProvider

Inherits:
BaseProvider show all
Defined in:
lib/utcp/providers/sse_provider.rb

Overview

Execution provider for Server-Sent Events (SSE)

Instance Attribute Summary

Attributes inherited from BaseProvider

#auth, #name, #type

Instance Method Summary collapse

Constructor Details

#initialize(name:, auth: nil) ⇒ SseProvider

Returns a new instance of SseProvider.



12
13
14
# File 'lib/utcp/providers/sse_provider.rb', line 12

def initialize(name:, auth: nil)
  super(name: name, provider_type: "sse", auth: auth)
end

Instance Method Details

#call_tool(tool, arguments = {}, &block) ⇒ Object

Expects tool.provider to have: { “url”: “…”, “http_method”: “GET” }



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
54
55
56
57
58
# File 'lib/utcp/providers/sse_provider.rb', line 22

def call_tool(tool, arguments = {}, &block)
  p = tool.provider
  url = Utils::Subst.apply(p["url"])
  uri = URI(url)
  # add args as query
  args = Utils::Subst.apply(arguments || {})
  q = URI.decode_www_form(uri.query || "") + args.to_a
  uri.query = URI.encode_www_form(q)

  req = Net::HTTP::Get.new(uri)
  headers = { "Accept" => "text/event-stream" }
  @auth&.apply_query(uri) if @auth&.respond_to?(:apply_query)
  @auth&.apply_headers(headers)

  headers.each { |k, v| req[k] = v }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https")
  begin
    buffer = +""
    http.request(req) do |res|
      res.read_body do |chunk|
        buffer << chunk
        while (line = buffer.slice!(/.*\n/))
          line = line.strip
          next if line.empty? || line.start_with?(":")
          if line.start_with?("data:")
            data = line.sub(/^data:\s?/, "")
            yield data if block_given?
          end
        end
      end
    end
    nil
  ensure
    http.finish if http.active?
  end
end

#discover_tools!Object

manual discovery not supported here

Raises:



17
18
19
# File 'lib/utcp/providers/sse_provider.rb', line 17

def discover_tools!
  raise ProviderError, "SSE is an execution provider only"
end