Class: OpenAI::Responses::SessionLane
- Inherits:
-
Object
- Object
- OpenAI::Responses::SessionLane
- Defined in:
- lib/openai/helpers/responses_websocket/session.rb
Overview
One routed consumer. Canceling its wait never cancels the socket reader.
Instance Attribute Summary collapse
-
#stream_id ⇒ Object
readonly
Returns the value of attribute stream_id.
Instance Method Summary collapse
- #close ⇒ Object
- #discard(error = nil) ⇒ Object private
- #enqueue(event, bytes, raw_command_sent = false) ⇒ Object private
- #get_final_response ⇒ Object
-
#initialize(session, stream_id, pending_steers) ⇒ SessionLane
constructor
private
A new instance of SessionLane.
- #receive ⇒ Object
- #send_event(event) ⇒ Object
- #wake ⇒ Object private
Constructor Details
#initialize(session, stream_id, pending_steers) ⇒ SessionLane
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 SessionLane.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 139 def initialize(session, stream_id, pending_steers) @session = session @stream_id = stream_id @queue = [] @bytes = 0 @closed = false @receiving = false @in_flight = false @pending_steers = pending_steers @response_id = nil @required_input = {} @changed = Async::Condition.new @accumulator = SessionAccumulator.new(session.limits.max_response_bytes) end |
Instance Attribute Details
#stream_id ⇒ Object (readonly)
Returns the value of attribute stream_id.
136 137 138 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 136 def stream_id @stream_id end |
Instance Method Details
#close ⇒ Object
350 351 352 353 354 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 350 def close @session.assert_owner! @session.detach(self) nil end |
#discard(error = 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.
372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 372 def discard(error = nil) return if @closed @closed = true @detached_error = error @queue.each { |_event, bytes| @session.consumed(bytes) } @queue.clear @bytes = 0 @accumulator = SessionAccumulator.new(@session.limits.max_response_bytes) @required_input.clear @changed.signal end |
#enqueue(event, bytes, raw_command_sent = 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.
357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 357 def enqueue(event, bytes, raw_command_sent = false) return if @closed limits = @session.limits if @queue.length >= limits.max_events_per_lane || @bytes + bytes > limits.max_bytes_per_lane raise BufferError, "The Responses lane exceeded its buffered event budget." end @queue << [event, bytes, raw_command_sent] @bytes += bytes @changed.signal true end |
#get_final_response ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 337 def get_final_response @session.assert_owner! loop do raise @accumulator.error if @accumulator.error if (response = @accumulator.final_response) @accumulator = SessionAccumulator.new(@session.limits.max_response_bytes) return response end receive end end |
#receive ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 199 def receive @session.assert_owner! raise SessionError, "This Responses lane already has a consumer." if @receiving @receiving = true begin loop do raise @detached_error if @detached_error raise SessionError, "The Responses lane is detached." if @closed unless @queue.empty? event, bytes, raw_command_sent = @queue.shift @bytes -= bytes @session.consumed(bytes) if %w[response.steer.pending response.steer.failed].include?(event.type.to_s) parent = event.steer.previous_response_id pending = @pending_steers[parent] id = event.steer.id if pending previously_pending = pending[:outcomes].delete(id) if event.type.to_s == "response.steer.pending" && !id.nil? pending[:outcomes][id] = true end unless previously_pending pending[:count] -= 1 end @pending_steers.delete(parent) if pending[:count].zero? && pending[:outcomes].empty? end end # Unregistered named streams remain available for raw inspection, # without changing the default lane's response state. if event.to_h[:stream_id] == @stream_id # Prior raw commands leave generic errors uncorrelated. Use the # receipt-time snapshot, so later commands cannot change attribution. if event.type.to_s == "error" && @stream_id.nil? && @in_flight && event.to_h[:sequence_number].nil? && raw_command_sent discard(RequestError.new(event)) @session.detach(self) return event end @accumulator.add(event, bytes) if event.type.to_s == "response.created" @in_flight = true @pending_steers.delete(@response_id) if @response_id != event.response.id @response_id = event.response.id.dup.freeze @required_input.clear elsif %w[response.completed response.failed response.incomplete error].include?(event.type.to_s) @in_flight = false if event.type.to_s == "error" @required_input.clear else @required_input = required_input_keys(@accumulator.final_response&.output || []) @response_id = event.response.id.dup.freeze end end end return event end @session.check_reader! @changed.wait end ensure @receiving = false end end |
#send_event(event) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 154 def send_event(event) @session.assert_owner! raise @detached_error if @detached_error raise SessionError, "The Responses lane is detached." if @closed data = @session.encode_client_event(event) type = data[:type] if type.to_s == "response.create" key = data.key?("stream_id") ? "stream_id" : :stream_id if data.key?(key) && data[key] != @stream_id raise ArgumentError, "The command stream_id does not match its lane." end data[key] = @stream_id unless @stream_id.nil? queued_response = @queue.any? do |queued_event, _bytes| queued_event.type.to_s == "response.created" && queued_event.to_h[:stream_id] == @stream_id end pending = @pending_steers[@response_id] if @in_flight || queued_response || (pending && pending[:count].positive? && !required_input_continuation?(data)) raise SessionError, "Consume the current response before sending another create on this lane." end @accumulator = SessionAccumulator.new(@session.limits.max_response_bytes) @in_flight = true elsif type.to_s == "response.steer" && (steer_parent = data[:previous_response_id]).is_a?(String) && !steer_parent.empty? pending = (@pending_steers[steer_parent] ||= {count: 0, outcomes: {}}) pending[:count] += 1 end @session.send_event(data) rescue OpenAI::Errors::ResponsesClientEventError @in_flight = false if type.to_s == "response.create" if type.to_s == "response.steer" && pending pending[:count] -= 1 @pending_steers.delete(steer_parent) if pending[:count].zero? && pending[:outcomes].empty? end raise end |
#wake ⇒ 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.
386 |
# File 'lib/openai/helpers/responses_websocket/session.rb', line 386 def wake = @changed.signal |