Class: OpenAI::Realtime::TranslationConnection

Inherits:
WebSocket::TypedConnection show all
Defined in:
lib/openai/helpers/realtime/translation.rb

Overview

Opening does not send any events. session.close flushes remaining output; read through session.closed before leaving the block to consume it.

Instance Attribute Summary

Attributes inherited from WebSocket::TypedConnection

#url

Attributes inherited from WebSocket::Connection

#url

Instance Method Summary collapse

Methods inherited from WebSocket::TypedConnection

#close, #closed?, #each, #parse_event, #receive, #send_event

Methods included from WebSocket::GuardedConnection

#abort, #close, #closed?

Methods inherited from WebSocket::Connection

#abort, #close, #closed?, #each, #receive, #receive_raw, #send_raw

Constructor Details

#initialize(socket:, url:) ⇒ TranslationConnection

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/openai/helpers/realtime/translation.rb', line 43

def initialize(socket:, url:)
  super(
    socket: socket,
    url: url,
    client_event: OpenAI::Realtime::RealtimeTranslationClientEvent,
    server_event: OpenAI::Realtime::RealtimeTranslationServerEvent
  )
  @poisoned = false
  @closed = false
  @write_mutex = Mutex.new
  @read_state_mutex = Mutex.new
  @active_reads = 0
end

Instance Method Details

#finish(timeout:) {|OpenAI::Realtime::RealtimeTranslationServerEvent| ... } ⇒ OpenAI::Realtime::RealtimeTranslationSessionClosedEvent

Request a final flush and yield every trailing event, including session.closed, to the existing receive owner. Call after the final admitted audio send returns. No second reader or reconnect is started. The block is required so trailing audio, text and errors cannot be lost.

Parameters:

  • timeout (Float) —

    maximum seconds for the close write and drain

Yields:

  • (OpenAI::Realtime::RealtimeTranslationServerEvent)

Returns:

  • (OpenAI::Realtime::RealtimeTranslationSessionClosedEvent)

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/openai/helpers/realtime/translation.rb', line 65

def finish(timeout:)
  raise ArgumentError, "A block is required to consume trailing Translation events." unless block_given?
  seconds = Float(timeout)
  unless seconds.finite? && seconds.positive?
    raise ArgumentError, "Translation finish timeout must be finite and positive."
  end

  require "timeout"

  @read_state_mutex.synchronize do
    if @finish_owner
      raise(
        connection_error("Translation finish must run on the existing receive owner, between reads."),
        cause: nil
      )
    end

    # SDK .cause accessors can expose diagnostics hidden from native rendering.
    raise @finish_error, cause: Exception.instance_method(:cause).bind_call(@finish_error) if @finish_error
    return @finish_terminal if @finish_terminal
    if @active_reads.positive? || @finish_requested
      raise(
        connection_error("Translation finish must run on the existing receive owner, between reads."),
        cause: nil
      )
    end

    @finish_owner = Fiber.current
  end

  begin
    Timeout.timeout(seconds, FinishTimeout) do
      write_text(encode_client_event(type: "session.close"), finish: true)
      while (event = receive)
        terminal = event.is_a?(OpenAI::Realtime::RealtimeTranslationSessionClosedEvent)
        @read_state_mutex.synchronize { @finish_terminal = event } if terminal
        yield(event)
        return event if terminal
      end

      raise connection_error("Translation socket closed before session.closed."), cause: nil
    end

  rescue FinishTimeout
    error = Timeout::Error.new("Timed out finishing the Translation session.")
    @read_state_mutex.synchronize { @finish_error = error }
    raise error, cause: nil
  rescue StandardError => error
    @read_state_mutex.synchronize { @finish_error = error }
    raise
  ensure
    @read_state_mutex.synchronize { @finish_owner = nil }
  end
end