Class: OpenAI::Realtime::Connection
- Inherits:
-
Object
- Object
- OpenAI::Realtime::Connection
- Includes:
- Enumerable
- 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
- #url ⇒ URI::Generic readonly
Instance Method Summary collapse
-
#abort ⇒ Object
private
Abort without waiting for the WebSocket close handshake.
-
#close(code: 1000, reason: "") ⇒ Object
Close the connection.
- #closed? ⇒ Boolean
-
#each ⇒ Object
Yield server events until the remote peer closes the connection.
-
#initialize(socket:, url:) ⇒ Connection
constructor
private
A new instance of Connection.
-
#parse_event(data) ⇒ Object
Parse raw JSON as a typed server event.
-
#receive ⇒ Object
Receive and parse the next server event, or return nil after a clean close.
-
#receive_raw ⇒ Object
Receive the next raw WebSocket message.
-
#send_event(event) ⇒ Object
Validate, encode, and send a typed client event.
-
#send_raw(data) ⇒ Object
Send an already encoded text message.
Constructor Details
#initialize(socket:, url:) ⇒ 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:) @socket = socket @url = url @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 |
#url ⇒ URI::Generic (readonly)
34 35 36 |
# File 'lib/openai/helpers/realtime/connection.rb', line 34 def url @url end |
Instance Method Details
#abort ⇒ 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.
Abort without waiting for the WebSocket close handshake.
151 152 153 154 155 156 |
# File 'lib/openai/helpers/realtime/connection.rb', line 151 def abort return if closed? @socket.abort nil end |
#close(code: 1000, reason: "") ⇒ Object
Close the connection.
141 142 143 144 145 146 |
# File 'lib/openai/helpers/realtime/connection.rb', line 141 def close(code: 1000, reason: "") return if closed? @socket.close(code: code, reason: reason) nil end |
#closed? ⇒ Boolean
159 |
# File 'lib/openai/helpers/realtime/connection.rb', line 159 def closed? = @socket.closed? |
#each ⇒ Object
Yield server events until the remote peer closes the connection.
37 38 39 40 41 42 43 44 45 |
# File 'lib/openai/helpers/realtime/connection.rb', line 37 def each return enum_for(__method__) unless block_given? while (event = receive) yield(event) end self 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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/openai/helpers/realtime/connection.rb', line 63 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 |
#receive ⇒ Object
Receive and parse the next server event, or return nil after a clean close.
48 49 50 51 52 53 |
# File 'lib/openai/helpers/realtime/connection.rb', line 48 def receive data = receive_raw return nil if data.nil? parse_event(data) end |
#receive_raw ⇒ Object
Receive the next raw WebSocket message.
56 57 58 59 |
# File 'lib/openai/helpers/realtime/connection.rb', line 56 def receive_raw = @socket.read &.to_str end |
#send_event(event) ⇒ Object
Validate, encode, and send a typed client event.
88 89 90 |
# File 'lib/openai/helpers/realtime/connection.rb', line 88 def send_event(event) send_raw(encode_client_event(event)) end |
#send_raw(data) ⇒ Object
Send an already encoded text message.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/openai/helpers/realtime/connection.rb', line 119 def send_raw(data) if closed? raise( OpenAI::Errors::RealtimeConnectionError.new( url: @url, message: "Cannot send on a closed Realtime WebSocket." ) ) end text = data.dup text.force_encoding(Encoding::UTF_8) if text.encoding == Encoding::BINARY text = text.encode(Encoding::UTF_8) unless text.encoding == Encoding::UTF_8 unless text.valid_encoding? raise ArgumentError, "Realtime WebSocket text must contain valid UTF-8" end @socket.write(text) nil end |