Class: OpenAI::Realtime::Connection
- Inherits:
-
WebSocket::Connection
- Object
- WebSocket::Connection
- OpenAI::Realtime::Connection
- Includes:
- WebSocket::Protocol
- Defined in:
- lib/openai/helpers/realtime/connection.rb
Overview
A live, typed Realtime WebSocket connection.
Direct Known Subclasses
Instance Attribute Summary collapse
- #conversation ⇒ OpenAI::Realtime::ConnectionResources::Conversation readonly
- #input_audio_buffer ⇒ OpenAI::Realtime::ConnectionResources::InputAudioBuffer readonly
- #response ⇒ OpenAI::Realtime::ConnectionResources::Response readonly
- #session ⇒ OpenAI::Realtime::ConnectionResources::Session readonly
Attributes inherited from WebSocket::Connection
Instance Method Summary collapse
-
#flush_pending ⇒ Object
Explicitly send retained events in order on the current session.
-
#initialize(socket:, url:, recovery: nil) ⇒ Connection
constructor
private
A new instance of Connection.
-
#parse_event(data) ⇒ Object
Parse raw JSON as a typed server event.
-
#pending_messages ⇒ Object
Snapshot of retained events, including the current event during a flush.
-
#reconnecting? ⇒ Boolean
Whether automatic recovery is opening or preparing a replacement socket.
-
#send_event(event) ⇒ Object
Validate, encode, and send a typed client event.
-
#take_pending_messages ⇒ Object
Remove and return retained events without sending them.
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 |
# File 'lib/openai/helpers/realtime/connection.rb', line 22 def initialize(socket:, url:, recovery: nil) super(socket: socket, url: url) @recovery = recovery @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
#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.
76 77 78 79 |
# File 'lib/openai/helpers/realtime/connection.rb', line 76 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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/openai/helpers/realtime/connection.rb', line 35 def parse_event(data) parsed = JSON.parse(data, symbolize_names: true) 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) end event rescue OpenAI::Errors::RealtimeProtocolError raise rescue StandardError => e raise OpenAI::Errors::RealtimeProtocolError.new(data: data, cause: e) end |
#pending_messages ⇒ Object
Snapshot of retained events, including the current event during a flush. These may contain sensitive application data. Normal connections return [].
69 |
# File 'lib/openai/helpers/realtime/connection.rb', line 69 def = @recovery ? @recovery. : [] |
#reconnecting? ⇒ Boolean
Whether automatic recovery is opening or preparing a replacement socket.
65 |
# File 'lib/openai/helpers/realtime/connection.rb', line 65 def reconnecting? = @recovery ? @recovery.reconnecting? : false |
#send_event(event) ⇒ Object
Validate, encode, and send a typed client event.
60 61 62 |
# File 'lib/openai/helpers/realtime/connection.rb', line 60 def send_event(event) send_raw(encode_client_event(event)) end |
#take_pending_messages ⇒ Object
Remove and return retained events without sending them.
72 |
# File 'lib/openai/helpers/realtime/connection.rb', line 72 def = @recovery ? @recovery. : [] |