module RubyLLM::ActiveRecord::MessageBroadcasting
Shows a chat’s messages live with Turbo when Configuration#broadcast_messages, or the model’s broadcasts: option, is on. An empty assistant reply is appended when it is created, so its chunks have a place to land, and each chunk is appended as a numbered span that the chat UI’s Stimulus controller puts back in order. A saved message replaces itself, a destroyed one is removed, and every change refreshes the chat page. Broadcasts go to the chat record’s stream:
<%= turbo_stream_from @chat %>
Public Class Methods
Source
# File lib/ruby_llm/active_record/message_broadcasting.rb, line 23 def self.available? defined?(::Turbo::Broadcastable) && defined?(::ActionCable) ? true : false end
Returns whether Turbo broadcasting can run: turbo-rails and Action Cable are loaded.
Public Instance Methods
Source
# File lib/ruby_llm/active_record/message_broadcasting.rb, line 57 def broadcast_chunk(chunk) raise RubyLLM::ConfigurationError, ChunkMethods::BROADCASTING_OFF unless broadcasting? return if chunk.content.to_s.empty? after = @last_broadcast_chunk_index @last_broadcast_chunk_index = chunk.index broadcast_append_to chat_association, target: "#{ActionView::RecordIdentifier.dom_id(self)}_stream", content: chunk_span(chunk, after) end
Appends the content of chunk, a RubyLLM::Chunk streamed into this message, to the message’s <dom_id>_stream element as <span data-seq="index">, escaped. Each span names the index of the span before it in data-after, so the page can order them. Returns nil for a chunk without content. Raises RubyLLM::ConfigurationError when the message does not broadcast.
chat.ask("Tell me a story") { |chunk| chunk.message.broadcast_chunk(chunk) }
Source
# File lib/ruby_llm/active_record/message_broadcasting.rb, line 44 def broadcasting? self.class.broadcasting? end
Returns whether this message broadcasts. See ::broadcasting?.