Class: OpenAI::Responses::Connection
- Inherits:
-
WebSocket::Connection
- Object
- WebSocket::Connection
- OpenAI::Responses::Connection
- Includes:
- WebSocket::Protocol
- Defined in:
- lib/openai/helpers/responses_websocket/connection.rb
Overview
A live, typed Responses WebSocket connection.
Instance Attribute Summary collapse
Attributes inherited from WebSocket::Connection
Instance Method Summary collapse
- #abort ⇒ Object private
- #close(code: 1000, reason: "") ⇒ Object
- #closed? ⇒ Boolean
- #each ⇒ Object
- #encode_client_event(event, response_create: false) ⇒ Object private
-
#initialize(socket:, url:) ⇒ Connection
constructor
private
A new instance of Connection.
- #poisoned? ⇒ Boolean private
- #receive ⇒ Object
-
#receive_raw ⇒ Object
private
Receive the next raw WebSocket message while preserving Responses' single-owner and one-reader guarantees.
-
#send_event(event) ⇒ nil
Validate, encode, and send a Responses client event.
-
#send_raw(data) ⇒ Object
private
Send an already encoded text message while preserving Responses' single-owner and terminal-write guarantees.
- #send_response_create(event) ⇒ Object private
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.
13 14 15 16 17 18 19 20 21 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 13 def initialize(socket:, url:) super @poisoned = false @state = :open @owner_thread = Thread.current.object_id @reading = false @server_event_names = discriminator_values(OpenAI::Responses::ResponsesServerEvent) @response = OpenAI::Responses::ConnectionResources::Response.new(self) end |
Instance Attribute Details
#response ⇒ OpenAI::Responses::ConnectionResources::Response (readonly)
10 11 12 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 10 def response @response 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.
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 108 def abort assert_owner! return if socket_closed? super @state = :closed nil rescue OpenAI::Errors::ResponsesConnectionError raise rescue StandardError @state = :closed raise OpenAI::Errors::ResponsesConnectionError.new(url: @url), cause: nil end |
#close(code: 1000, reason: "") ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 92 def close(code: 1000, reason: "") assert_owner! return if socket_closed? return abort if poisoned? super @state = :closed nil rescue OpenAI::Errors::ResponsesConnectionError raise rescue StandardError @state = :closed raise OpenAI::Errors::ResponsesConnectionError.new(url: @url), cause: nil end |
#closed? ⇒ Boolean
122 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 122 def closed? = @state == :closed || super |
#each ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 23 def each return enum_for(__method__) unless block_given? with_read_lease do while (event = read_one) yield(event) end end self end |
#encode_client_event(event, response_create: false) ⇒ 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.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 174 def encode_client_event(event, response_create: false) create_type = OpenAI::Responses::ResponsesClientEvent::ResponseCreate if response_create validate_event_tree!(event, target: create_type) payload = if response_create OpenAI::Internal::Type::Converter.dump(create_type, event) elsif event.is_a?(Hash) OpenAI::Internal::Type::Unknown.dump(event, state: {can_retry: true}) else OpenAI::Internal::Type::Converter.dump(OpenAI::Responses::ResponsesClientEvent, event) end raise OpenAI::Errors::ResponsesClientEventError.new unless payload.is_a?(Hash) OpenAI::Helpers::StructuredOutput.prepare_configuration_updates!(normalize_event_keys(payload)) rescue OpenAI::Errors::ResponsesClientEventError raise rescue StandardError raise OpenAI::Errors::ResponsesClientEventError.new, cause: nil end |
#poisoned? ⇒ Boolean
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.
90 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 90 def poisoned? = @poisoned |
#receive ⇒ Object
35 36 37 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 35 def receive with_read_lease { read_one } end |
#receive_raw ⇒ 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.
Receive the next raw WebSocket message while preserving Responses' single-owner and one-reader guarantees.
43 44 45 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 43 def receive_raw with_read_lease { read_raw } end |
#send_event(event) ⇒ nil
Validate, encode, and send a Responses client event.
50 51 52 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 50 def send_event(event) send_client_event(event) end |
#send_raw(data) ⇒ 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.
Send an already encoded text message while preserving Responses' single-owner and terminal-write guarantees.
82 83 84 85 86 87 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 82 def send_raw(data) assert_owner! raise connection_error("Cannot send on a closed Responses WebSocket.") if poisoned? || closed? super end |
#send_response_create(event) ⇒ 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.
55 56 57 |
# File 'lib/openai/helpers/responses_websocket/connection.rb', line 55 def send_response_create(event) send_client_event(event, response_create: true) end |