Class: OpenAI::Realtime::ConnectionManager Private

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/helpers/realtime/connection_manager.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Internal block-scoped lifecycle manager for Realtime WebSocket connections.

Constant Summary collapse

RESERVED_TRANSPORT_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  :alpn_protocols,
  :headers,
  :hostname,
  :port,
  :protocol,
  :scheme,
  :ssl_context,
  :timeout,
  :url
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(client:, query:, websocket_base_url:, transport:, request_options:, transport_options:, connection_class: OpenAI::Realtime::Connection) ⇒ ConnectionManager

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ConnectionManager.



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
# File 'lib/openai/helpers/realtime/connection_manager.rb', line 22

def initialize(
  client:,
  query:,
  websocket_base_url:,
  transport:,
  request_options:,
  transport_options:,
  connection_class: OpenAI::Realtime::Connection
)
  @client = client
  @query = query
    .to_h
    .to_h do |key, value|
      [key.to_s.dup.freeze, value.to_s.dup.freeze]
    end
    .freeze
  @websocket_base_url = websocket_base_url&.to_s&.dup&.freeze
  @transport = transport
  @request_options = request_options
  @connection_class = connection_class
  transport_options = transport_options.dup.freeze
  reserved_options = transport_options.keys.select do |key|
    (key.is_a?(String) || key.is_a?(Symbol)) && RESERVED_TRANSPORT_OPTIONS.include?(key.to_sym)
  end

  unless reserved_options.empty?
    raise(
      ArgumentError,
      "`transport_options` cannot include #{reserved_options.map(&:inspect).join(", ")}"
    )
  end

  @transport_options = transport_options
end

Instance Method Details

#open {|connection| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Open the WebSocket and yield a typed connection for the lifetime of the block.

Yield Parameters:

Returns:

  • (Object)

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openai/helpers/realtime/connection_manager.rb', line 63

def open
  raise ArgumentError, "A block is required to open a Realtime WebSocket." unless block_given?

  transport = @transport || OpenAI::Realtime::Transports::AsyncWebSocket.new
  unless transport.respond_to?(:open)
    raise ArgumentError, "`transport` must respond to `open`"
  end

  @client
    .with_realtime_connection_request(
      path: "realtime",
      query: @query,
      websocket_base_url: @websocket_base_url,
      options: @request_options
    ) do |request, mark_handshake_completed|
      transport
        .open(
          url: request.fetch(:url),
          headers: request.fetch(:headers),
          timeout: request.fetch(:timeout),
          **@transport_options
        ) do |socket|
          mark_handshake_completed.call
          connection = @connection_class.new(socket: socket, url: request.fetch(:url))
          begin
            yield(connection)
          ensure
            pending_error = $ERROR_INFO
            begin
              if pending_error
                connection.abort unless connection.closed?
              else
                connection.close unless connection.closed?
              end

            rescue StandardError
              raise if pending_error.nil?
            end
          end
        end
    end
end