Class: OpenAI::Realtime::Connection

Inherits:
WebSocket::Connection show all
Includes:
WebSocket::Protocol
Defined in:
lib/openai/helpers/realtime/connection.rb

Overview

A live, typed Realtime WebSocket connection.

Instance Attribute Summary collapse

Attributes inherited from WebSocket::Connection

#url

Instance Method Summary collapse

Methods inherited from WebSocket::Connection

#abort, #close, #closed?, #each, #receive, #receive_raw, #send_raw

Constructor Details

#initialize(socket:, url:, recovery: nil) ⇒ Connection

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 Connection.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/openai/helpers/realtime/connection.rb', line 22

def initialize(socket:, url:, recovery: nil)
  super(socket: socket, url: url)
  @recovery = recovery
  @read_mutex = Mutex.new
  @server_event_names = discriminator_values(OpenAI::Realtime::RealtimeServerEvent)
  @client_event_names = discriminator_values(OpenAI::Realtime::RealtimeClientEvent)
  @session = OpenAI::Realtime::ConnectionResources::Session.new(self)
  @response = OpenAI::Realtime::ConnectionResources::Response.new(self)
  @conversation = OpenAI::Realtime::ConnectionResources::Conversation.new(self)
  @input_audio_buffer = OpenAI::Realtime::ConnectionResources::InputAudioBuffer.new(self)
end

Instance Attribute Details

#conversation ⇒ OpenAI::Realtime::ConnectionResources::Conversation (readonly)



16
17
18
# File 'lib/openai/helpers/realtime/connection.rb', line 16

def conversation
  @conversation
end

#input_audio_buffer ⇒ OpenAI::Realtime::ConnectionResources::InputAudioBuffer (readonly)



19
20
21
# File 'lib/openai/helpers/realtime/connection.rb', line 19

def input_audio_buffer
  @input_audio_buffer
end

#response ⇒ OpenAI::Realtime::ConnectionResources::Response (readonly)



13
14
15
# File 'lib/openai/helpers/realtime/connection.rb', line 13

def response
  @response
end

#session ⇒ OpenAI::Realtime::ConnectionResources::Session (readonly)



10
11
12
# File 'lib/openai/helpers/realtime/connection.rb', line 10

def session
  @session
end

Instance Method Details

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



68
69
70
71
# File 'lib/openai/helpers/realtime/connection.rb', line 68

def enable_no_status_recovery
  @socket.enable_no_status_recovery if @socket.respond_to?(:enable_no_status_recovery)
  nil
end

#flush_pending ⇒ Object

Explicitly send retained events in order on the current session. Restore any required session state first. Does not retry uncertain failed writes.



109
110
111
112
# File 'lib/openai/helpers/realtime/connection.rb', line 109

def flush_pending
  @recovery&.flush_pending
  nil
end

#parse_event(data) ⇒ Object

Parse raw JSON as a typed server event. Valid events that are newer than this SDK remain observable as UnknownServerEvent values.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openai/helpers/realtime/connection.rb', line 36

def parse_event(data)
  parsed = parse_json(data)
  type = event_type(parsed)
  unless @server_event_names.key?(type.to_s)
    return OpenAI::Realtime::UnknownServerEvent.new(data: parsed)
  end

  state = OpenAI::Internal::Type::Converter.new_coerce_state
  event = OpenAI::Internal::Type::Converter.coerce(
    OpenAI::Realtime::RealtimeServerEvent,
    parsed,
    state: state
  )
  if (cause = coercion_error(state))
    raise OpenAI::Errors::RealtimeProtocolError.new(data: data, cause: cause), cause: nil
  end

  validate_wire_field_names!(event, parsed)
rescue OpenAI::Errors::RealtimeProtocolError
  raise
rescue SystemStackError
  raise OpenAI::Errors::RealtimeProtocolError.new(data: data), cause: nil
rescue StandardError => e
  raise OpenAI::Errors::RealtimeProtocolError.new(data: data, cause: e), cause: nil
end

#pending_messages ⇒ Object

Snapshot of retained events, including the current event during a flush. These may contain sensitive application data. Normal connections return [].



102
# File 'lib/openai/helpers/realtime/connection.rb', line 102

def pending_messages = @recovery ? @recovery.pending_messages : []

#reconnecting? ⇒ Boolean

Whether automatic recovery is opening or preparing a replacement socket.

Returns:



98
# File 'lib/openai/helpers/realtime/connection.rb', line 98

def reconnecting? = @recovery ? @recovery.reconnecting? : false

#send_event(event) ⇒ Object

Validate, encode, and send a typed client event.



63
64
65
# File 'lib/openai/helpers/realtime/connection.rb', line 63

def send_event(event)
  send_raw(encode_client_event(event))
end

#take_pending_messages ⇒ Object

Remove and return retained events without sending them.



105
# File 'lib/openai/helpers/realtime/connection.rb', line 105

def take_pending_messages = @recovery ? @recovery.take_pending_messages : []