class OpenUI::Rails::Stream::Bridge
Turns model output into an ordered sequence of {Envelope}s, coalescing small chunks up to pending_byte_limit and enforcing stream_byte_limit. Transport-agnostic: a caller broadcasts the yielded envelopes over Turbo Streams or Action Cable.
Pull sources (an Enumerable of chunks) go through {#each_envelope}; push sources (a streaming callback) use a {Writer} directly.
Attributes
Public Class Methods
# File lib/openui/rails/stream/bridge.rb, line 27 def initialize(message_id:, library_version: nil, configuration: OpenUI::Rails.configuration) @message_id = message_id @library_version = library_version @configuration = configuration end
Create a bridge with message metadata and configured byte limits.
@param message_id [String] stable id shared by all frames in the stream
@param library_version [String, nil] manifest fingerprint attached to frames
@param configuration [Configuration]
@return [Bridge]
Public Instance Methods
# File lib/openui/rails/stream/bridge.rb, line 47 def each_envelope(source) writer = writer() source = source.to_enum loop do begin chunk = source.next envelopes = writer.push(chunk) rescue StopIteration break rescue StandardError => error envelope = writer.error_for(error) yield envelope if envelope return end # Transport/consumer failures are not provider failures. Let the # host retry or report them without recursively broadcasting errors. envelopes.each { |envelope| yield envelope } return if writer.terminated? end writer.finish.each { |envelope| yield envelope } end
Yield ordered envelopes for every chunk from a pull-based source. Provider errors become a structured error envelope. Exceptions raised by the consumer block propagate to the caller. Pulling stops once the writer terminates, so an oversized response does not drain the source.
@param source [#each] yields String chunks @yieldparam envelope [Envelope]
Source
# File lib/openui/rails/stream/bridge.rb, line 36 def envelopes(source) to_enum(:each_envelope, source) end
@param source [#each] yields String chunks
@return [Enumerator<Envelope>]
Source
# File lib/openui/rails/stream/bridge.rb, line 72 def writer Writer.new( message_id: message_id, library_version: library_version, pending_byte_limit: @configuration.pending_byte_limit, stream_byte_limit: @configuration.stream_byte_limit, on_error: @configuration.on_error ) end
Build a stateful writer for push-based providers.
@return [Writer]