Class: OpenAI::WebSocket::AsyncWebSocketTransport Private

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/helpers/websocket/async_websocket_transport.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.

Product-neutral transport backed by the optional async-websocket gem.

Defined Under Namespace

Classes: Socket

Instance Method Summary collapse

Constructor Details

#initialize(product_name:, error_class:, error_factory:, dependency_message: nil, sensitive_query_parameter: nil, &tls_configurator) ⇒ AsyncWebSocketTransport

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.

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/openai/helpers/websocket/async_websocket_transport.rb', line 145

def initialize(
  product_name:,
  error_class:,
  error_factory:,
  dependency_message: nil,
  sensitive_query_parameter: nil,
  &tls_configurator
)
  @product_name = product_name
  @error_class = error_class
  @error_factory = error_factory
  @dependency_message = dependency_message
  @sensitive_query_parameter = sensitive_query_parameter
  @tls_configurator = tls_configurator
end

Instance Method Details

#open(url:, headers:, timeout:, **endpoint_options) ⇒ 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.



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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/openai/helpers/websocket/async_websocket_transport.rb', line 211

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

  # Proxy credentials belong only on the CONNECT request assembled from
  # proxy configuration. Never forward a caller-supplied value to the
  # 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 = sensitive_query?(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
  # sessions after the ordinary HTTP request timeout.
  ::Kernel.Sync() do
    client_options = if sideband
      {
        protocol: TraceSafeProtocol.new(
          endpoint.protocol,
          sensitive_query_parameter: @sensitive_query_parameter
        )
      }
    else
      {}
    end

    client = nil
    connection = nil
    socket = nil
    begin
      client = ::Async::WebSocket::Client.open(endpoint, **client_options)
      connection = negotiate(
        client,
        endpoint,
        request_target: request_target,
        headers: headers,
        timeout: timeout,
        connect_options: connect_options
      )
      socket = build_socket(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 StandardError => e
  raise if @error_class === e
  raise if e.equal?(block_error)

  error = @error_factory.call(url: url, cause: e, http_status: handshake_status(e))
  raise error, cause: nil
end