Module: OpenAI::Helpers::Realtime::ClientExtension

Included in:
Client
Defined in:
lib/openai/helpers/realtime/client_extension.rb

Overview

Realtime request integration kept outside the generated client implementation.

Instance Method Summary collapse

Instance Method Details

#realtime_connection_request(path:, query:, websocket_base_url: nil, options: nil) ⇒ Hash{Symbol=>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.

Build a fully authenticated Realtime WebSocket handshake request. Realtime transports use this boundary so provider authentication and request options stay consistent with ordinary SDK requests.

Parameters:

  • path (String)
  • query (Hash{String=>String})
  • options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil) (defaults to: nil)

Returns:

  • (Hash{Symbol=>Object})


126
127
128
129
130
131
132
133
134
# File 'lib/openai/helpers/realtime/client_extension.rb', line 126

def realtime_connection_request(path:, query:, websocket_base_url: nil, options: nil)
  request, = build_realtime_connection_request(
    path: path,
    query: query,
    websocket_base_url: websocket_base_url,
    options: options
  )
  request
end

#request(req) ⇒ Object

A generated WebRTC allocation belongs to the SDK until its complete raw response can be returned. Keep this lifecycle outside generated resources.



13
14
15
16
17
18
19
20
21
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/client_extension.rb', line 13

def request(req)
  unless req.is_a?(Hash) &&
      req[:method] == :post &&
      req[:path] == "realtime/calls" &&
      req[:model] == OpenAI::HTTPClient::Response
    return super
  end

  delivered = false
  allocated_response = nil
  call_id = nil
  begin
    url, response, log_context = perform_request(req) do |received_response, request_url, response_url|
      if received_response.status == 201
        allocated_response = received_response
        if response_url.path == request_url.path &&
            realtime_call_origin(response_url).casecmp?(realtime_call_origin(request_url))
          call_id = realtime_call_id_from_location(
            received_response.headers["location"],
            request_url: request_url
          )
        end
      end
    end

    result = finish_request(log_context, response) do
      parse_response(req, url: url, response: response)
    end

    delivered = true
    result
  ensure
    unless delivered
      begin
        OpenAI::Internal::Util.close_fused!(allocated_response.body) if allocated_response
      rescue StandardError, *(defined?(::Async::Stop) ? [::Async::Stop] : [])
        nil
      ensure
        cleanup_created_realtime_call(call_id, options: req[:options]) if call_id
      end
    end
  end
end

#with_realtime_connection_request(path:, query:, websocket_base_url: nil, options: nil) ⇒ 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.

Yield an authenticated WebSocket request, refreshing a rejected workload identity token exactly once after a definitive upgrade 401. The caller marks the handshake complete as soon as the transport yields a socket, before running application code; only pre-yield failures may retry.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/openai/helpers/realtime/client_extension.rb', line 142

def with_realtime_connection_request(path:, query:, websocket_base_url: nil, options: nil)
  request, deadline = build_realtime_connection_request(
    path: path,
    query: query,
    websocket_base_url: websocket_base_url,
    options: options
  )
  handshake_completed = false
  mark_handshake_completed = -> { handshake_completed = true }
  yield(request, mark_handshake_completed)
rescue OpenAI::Errors::RealtimeConnectionError => e
  raise if handshake_completed
  raise unless e.http_status == 401 && @workload_identity_auth

  @workload_identity_auth.invalidate_token
  refreshed, = build_realtime_connection_request(
    path: path,
    query: query,
    websocket_base_url: websocket_base_url,
    options: options,
    deadline: deadline
  )
  yield(refreshed, mark_handshake_completed)
end