Class: OpenAI::WebSocket::ConnectionManager Private

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

Shared block-scoped WebSocket opening and cleanup.

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:, transport:, transport_options:, default_transport:, connection_class:, request:, block_error_message:, abort_after_block:, transport_error_factory: nil, transport_error_message: "transport must respond to open", reserved_options_error_message: nil, connection_options: {}) ⇒ 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
# File 'lib/openai/helpers/websocket/connection_manager.rb', line 22

def initialize(
  client:,
  transport:,
  transport_options:,
  default_transport:,
  connection_class:,
  request:,
  block_error_message:,
  abort_after_block:,
  transport_error_factory: nil,
  transport_error_message: "transport must respond to open",
  reserved_options_error_message: nil,
  connection_options: {}
)
  @client = client
  @transport = transport
  @default_transport = default_transport
  @connection_class = connection_class
  @connection_options = connection_options.dup.freeze
  @request = request
  @block_error_message = block_error_message
  @abort_after_block = abort_after_block
  @transport_error_factory = transport_error_factory
  @transport_error_message = transport_error_message
  @reserved_options_error_message = reserved_options_error_message
  @transport_options = validated_transport_options(transport_options)
end

Instance Method Details

#open ⇒ 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.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/openai/helpers/websocket/connection_manager.rb', line 51

def open
  raise ArgumentError, @block_error_message unless block_given?

  transport = @transport || @default_transport.call
  raise ArgumentError, @transport_error_message unless transport.respond_to?(:open)

  requester = @client.requester
  unless @transport || (requester.instance_of?(OpenAI::NetHTTPClient) && requester.default_configuration?)
    raise(
      ArgumentError,
      "This HTTP transport's connection policy cannot be used by the default WebSocket " \
        "transport. Pass a compatible WebSocket transport with its own connection settings."
    )
  end

  @request.call do |request, mark_handshake_completed|
    block_error = nil
    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), **@connection_options)
        begin
          yield(connection)
        ensure
          block_error = $ERROR_INFO
          cleanup(connection)
        end
      end

  rescue StandardError => error
    raise if @transport_error_factory.nil?
    # Preserve the native chain, not an SDK accessor's explicit diagnostics.
    raise block_error, cause: Exception.instance_method(:cause).bind_call(block_error) if block_error
    raise @transport_error_factory.call(url: request.fetch(:url), error: error), cause: nil
  end
end