Class: OpenAI::Realtime::Transports::AsyncWebSocket

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

Overview

Default Realtime transport backed by the optional async-websocket gem.

Defined Under Namespace

Classes: Socket

Instance Method Summary collapse

Constructor Details

#initialize(&tls_configurator) ⇒ AsyncWebSocket

Configure the native TLS context used by secure Realtime WebSockets. Peer and hostname verification and HTTP/1.1 ALPN remain SDK-owned.



138
139
140
# File 'lib/openai/helpers/realtime/transports/async_websocket.rb', line 138

def initialize(&tls_configurator)
  @tls_configurator = tls_configurator
end

Instance Method Details

#open(url:, headers:, timeout:, **endpoint_options) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/openai/helpers/realtime/transports/async_websocket.rb', line 185

def open(url:, headers:, timeout:, **endpoint_options)
  load_dependencies(url)

  # Proxy credentials belong only on the CONNECT request assembled from
  # proxy configuration. Never forward a caller-supplied value to the
  # Realtime origin handshake.
  headers = headers.reject do |name, _value|
    name.to_s.casecmp?("proxy-authorization")
  end

  # Classic WebSocket negotiation uses HTTP/1.1. Pinning ALPN also avoids an
  # HTTP/2 selection on servers that advertise both protocols.
  options = {
    alpn_protocols: ::Async::HTTP::Protocol::HTTP11.names,
    **endpoint_options
  }
  if url.scheme == "wss" || @tls_configurator
    options[:ssl_context] = build_tls_context(url)
  end

  sideband = sideband_call?(url)
  endpoint_url = url.dup
  endpoint_url.query = nil if sideband

  endpoint = ::Async::HTTP::Endpoint.parse(
    endpoint_url.to_s,
    **options
  )
  request_target = endpoint.path
  request_target = "#{request_target}?#{url.query}" if sideband
  proxy_client = nil
  if (proxy = proxy_uri(url))
    proxy_endpoint = ::Async::HTTP::Endpoint.parse(proxy_url(proxy).to_s)
    proxy_client = ::Async::HTTP::Client.open(proxy_endpoint)
    tunnel = ::Async::HTTP::Proxy.new(
      proxy_client,
      authority(url, include_default_port: true),
      trace_safe_headers(proxy_headers(proxy))
    )
    endpoint = tunnel.wrap_endpoint(endpoint)
  end

  block_error = nil
  # Keep the request timeout scoped to WebSocket negotiation. Endpoint timeouts
  # remain installed on the socket and would otherwise terminate healthy idle
  # Realtime sessions after the ordinary HTTP request timeout.
  ::Kernel.Sync() do
    client_options = sideband ? {protocol: TraceSafeProtocol.new(endpoint.protocol)} : {}
    client = ::Async::WebSocket::Client.open(endpoint, **client_options)
    connection = nil
    socket = nil
    begin
      connection = negotiate(
        client,
        endpoint,
        request_target: request_target,
        headers: headers,
        timeout: timeout
      )
      socket = Socket.new(connection, url: url)
      begin
        yield(socket)
      rescue StandardError => e
        block_error = e
        raise
      end

    ensure
      close_resources(
        connection,
        client,
        proxy_client,
        connection_aborted: socket&.aborted?,
        pending_error: $ERROR_INFO
      )
    end
  end

rescue OpenAI::Errors::RealtimeConnectionError
  raise
rescue StandardError => e
  raise if e.equal?(block_error)

  raise(
    OpenAI::Errors::RealtimeConnectionError.new(
      url: url,
      cause: e,
      http_status: handshake_status(e)
    )
  )
end