Class: OpenAI::Responses::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/helpers/responses_websocket/session.rb

Overview

Owns one connection and one Async reader. Use only its lanes to receive.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task:, client:, limits:, request_options:, websocket_base_url:, transport:, transport_options:) ⇒ Session

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



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/openai/helpers/responses_websocket/session.rb', line 362

def initialize(task:, client:, limits:, request_options:, websocket_base_url:, transport:, transport_options:)
  @task = task
  @client = client
  @limits = limits
  @request_options = request_options
  @websocket_base_url = websocket_base_url
  @transport = transport
  @transport_options = transport_options
  @owner = Thread.current
  @lanes = {}
  @lane_ids = {}
  # Steering targets a response ID, independently of the lane sending the command.
  @pending_steers = {}
  @events = 0
  @bytes = 0
  @closed = false
  @default = lane(nil)
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



316
317
318
# File 'lib/openai/helpers/responses_websocket/session.rb', line 316

def default
  @default
end

#limitsObject (readonly)

Returns the value of attribute limits.



316
317
318
# File 'lib/openai/helpers/responses_websocket/session.rb', line 316

def limits
  @limits
end

Class Method Details

.open(client:, limits:, request_options: nil, websocket_base_url: nil, transport: nil, transport_options: {}) ⇒ Object

Raises:

  • (ArgumentError)


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/openai/helpers/responses_websocket/session.rb', line 318

def self.open(
  client:,
  limits:,
  request_options: nil,
  websocket_base_url: nil,
  transport: nil,
  transport_options: {}
)
  raise ArgumentError, "A block is required to open a Responses session." unless block_given?

  begin
    require "async"
    require "async/queue"
    require "async/condition"

  rescue LoadError
    raise OpenAI::Errors::Error, "Responses sessions require the async gem.", cause: nil
  end

  Sync do |task|
    session = new(
      task: task,
      client: client,
      limits: limits,
      request_options: request_options,
      websocket_base_url: websocket_base_url,
      transport: transport,
      transport_options: transport_options
    )
    begin
      session.start
      yield(session)
    ensure
      pending_error = $ERROR_INFO
      begin
        session.close
      rescue StandardError
        raise if pending_error.nil?
      end
    end
  end
end

Instance Method Details

#assert_owner!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.

Raises:



509
510
511
# File 'lib/openai/helpers/responses_websocket/session.rb', line 509

def assert_owner!
  raise SessionError, "Responses sessions are single-thread owned." unless Thread.current.equal?(@owner)
end

#check_reader!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.

Raises:

  • (@error)


503
504
505
506
# File 'lib/openai/helpers/responses_websocket/session.rb', line 503

def check_reader!
  raise @error if @error
  raise SessionError, "The Responses connection closed before another event arrived." if @closed || @ended
end

#closeObject



430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/openai/helpers/responses_websocket/session.rb', line 430

def close
  assert_owner!
  return if @closed

  @closed = true
  stop_reader
  @lanes.each_value(&:discard)
  @lanes.clear
  @lane_ids.clear
  @pending_steers.clear
  nil
end

#consumed(bytes) ⇒ 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.



514
515
516
517
# File 'lib/openai/helpers/responses_websocket/session.rb', line 514

def consumed(bytes)
  @events -= 1
  @bytes -= bytes
end

#detach(value) ⇒ 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.



520
521
522
523
# File 'lib/openai/helpers/responses_websocket/session.rb', line 520

def detach(value)
  @lanes.delete(value.stream_id) if @lanes[value.stream_id].equal?(value)
  value.discard
end

#encode_client_event(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.



481
482
483
484
# File 'lib/openai/helpers/responses_websocket/session.rb', line 481

def encode_client_event(event)
  assert_owner!
  @connection.encode_client_event(event)
end

#lane(stream_id) ⇒ Object

Raises:



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/openai/helpers/responses_websocket/session.rb', line 381

def lane(stream_id)
  assert_owner!
  raise SessionError, "The Responses session is closed." if @closed

  unless stream_id.nil? || (stream_id.is_a?(String) && /\A[A-Za-z0-9_.-]{1,256}\z/.match?(stream_id))
    raise ArgumentError, "Invalid Responses WebSocket stream ID."
  end

  stream_id = stream_id&.dup&.freeze
  raise ArgumentError, "The Responses lane is already registered." if @lane_ids.key?(stream_id)
  raise BufferError, "The Responses session exceeded max_lanes." if @lane_ids.length >= @limits.max_lanes

  value = SessionLane.new(self, stream_id, @pending_steers)
  @lane_ids[stream_id] = true
  @lanes[stream_id] = value
end

#reconnect(restore:, client: @client, request_options: @request_options) ⇒ Object

Replace the transport explicitly; old lane state is lost, never replayed. The restore callback registers new lanes and supplies application state.

Raises:



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/openai/helpers/responses_websocket/session.rb', line 400

def reconnect(restore:, client: @client, request_options: @request_options)
  assert_owner!
  raise SessionError, "The Responses session is closed." if @closed
  raise SessionError, "The Responses session is already reconnecting." if @reconnecting

  @reconnecting = true
  restored = false
  begin
    @lanes.each_value { |value|
      value.discard(
        StateLostError.new("The Responses connection was replaced; restore application state explicitly.")
      )
    }
    stop_reader
    @lanes.clear
    @lane_ids.clear
    @pending_steers.clear
    @default = lane(nil)
    @client = client
    @request_options = request_options
    start
    restore.call(self)
    restored = true
    self
  ensure
    close unless restored
    @reconnecting = false
  end
end

#send_event(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.



487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/openai/helpers/responses_websocket/session.rb', line 487

def send_event(event)
  assert_owner!
  check_reader!
  prior_raw_command_sent = @raw_command_sent
  @raw_command_sent = true unless event[:type].to_s == "response.create"
  @connection.send_event(event)
rescue OpenAI::Errors::ResponsesClientEventError
  @raw_command_sent = prior_raw_command_sent
  raise
rescue OpenAI::Errors::ResponsesSendError => error
  @error = error
  stop_reader
  raise
end

#startObject

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.



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/openai/helpers/responses_websocket/session.rb', line 444

def start
  @ended = false
  @error = nil
  @raw_command_sent = false
  generation = @generation = Object.new
  ready = Async::Queue.new
  opened = false
  @reader = @task.async do
    begin
      @client
        .responses
        .connect(
          request_options: @request_options,
          websocket_base_url: @websocket_base_url,
          transport: @transport,
          transport_options: @transport_options
        ) do |connection|
          @connection = connection
          opened = true
          ready.enqueue(true)
          connection.each { |event| dispatch(event) }
        end

    rescue StandardError => error
      @error = error if @generation.equal?(generation)
    ensure
      @ended = true if @generation.equal?(generation)
      ready.enqueue(false) unless opened
      @lanes.each_value(&:wake)
    end
  end

  raise @error || SessionError.new("The Responses connection did not open.") unless ready.dequeue
  nil
end