Skip to content

Commit 0b39814

Browse files
committed
Simplify chat UI broadcasts with broadcasts_to
- switch generated message model broadcasting from explicit create/update/destroy callbacks to broadcasts_to - keep only broadcast_append_chunk for streamed chunk appends - make message role partials accept message local fallback (user/assistant/system/tool/tool_calls ||= local_assigns[:message]) so broadcasts_to rendering works with role-based partial conventions - keep create.turbo_stream focused on form reset and remove update.turbo_stream template generation - update chat UI generator specs and Rails docs to match the new broadcast + partial-local behavior
1 parent 7248b25 commit 0b39814

15 files changed

Lines changed: 26 additions & 56 deletions

docs/_advanced/rails.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ The generated chat UI follows one convention: each message partial uses the loca
180180

181181
This comes from Rails partial rendering: `render @chat.messages` calls `to_partial_path`, and Rails injects a local named after that partial.
182182

183+
For compatibility with model broadcasts (`broadcasts_to`), generated message partials also accept a `message` local as a fallback.
184+
183185
#### Tool Call and Tool Result Partials
184186

185187
Tool-specific partials are generated under `app/views/messages/tool_calls` and `app/views/messages/tool_results`:
@@ -205,8 +207,7 @@ Using fixed locals keeps the templates dumb and predictable.
205207

206208
Turbo Stream templates used by the generated chat UI:
207209

208-
- `messages/create.turbo_stream.erb` resets the message form for `MessagesController#create` and appends a rendered message when invoked from model create broadcasts.
209-
- `messages/update.turbo_stream.erb` is used by model update broadcasts to re-render the message row.
210+
- `messages/create.turbo_stream.erb` resets the message form for `MessagesController#create`.
210211

211212
#### Generator Options
212213

lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ def create_views
7777
"app/views/#{message_view_path}/tool_results/_default.html.erb"
7878
template ui_template('views/messages/create.turbo_stream.erb'),
7979
"app/views/#{message_view_path}/create.turbo_stream.erb"
80-
template ui_template('views/messages/update.turbo_stream.erb'),
81-
"app/views/#{message_view_path}/update.turbo_stream.erb"
8280
template ui_template('views/messages/_content.html.erb'), "app/views/#{message_view_path}/_content.html.erb"
8381
template ui_template('views/messages/_form.html.erb'), "app/views/#{message_view_path}/_form.html.erb"
8482

@@ -156,29 +154,9 @@ def add_broadcasting_to_message_model
156154
# e.g., for LLM::Message, the chat association might be :llm_chat
157155
chat_association = chat_table_name.singularize
158156

159-
partial_path = message_model_name.underscore.pluralize
160-
161157
broadcasting_callbacks = <<-RUBY
162158
163-
after_create_commit :broadcast_message_created
164-
after_update_commit :broadcast_message_updated
165-
after_destroy_commit :broadcast_message_removed
166-
167-
def broadcast_message_created
168-
broadcast_render_later_to "#{chat_var}_\#{#{chat_association}_id}",
169-
template: "#{partial_path}/create",
170-
locals: { #{msg_var}: self }
171-
end
172-
173-
def broadcast_message_updated
174-
broadcast_render_later_to "#{chat_var}_\#{#{chat_association}_id}",
175-
template: "#{partial_path}/update",
176-
locals: { #{msg_var}: self }
177-
end
178-
179-
def broadcast_message_removed
180-
broadcast_remove_to "#{chat_var}_\#{#{chat_association}_id}"
181-
end
159+
broadcasts_to ->(#{msg_var}) { "#{chat_var}_\#{#{msg_var}.#{chat_association}_id}" }, inserts_by: :append
182160
183161
def broadcast_append_chunk(content)
184162
broadcast_append_to "#{chat_var}_\#{#{chat_association}_id}",

lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_assistant.html.erb.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%% assistant ||= local_assigns[:message] %>
12
<div id="<%= message_variable_name %>_<%%= assistant.id %>" class="w-full sm:w-auto my-5 space-y-3 rounded-md px-3 py-2 bg-green-50">
23
<div>
34
<span class="inline-block rounded px-2 py-0.5 text-xs font-medium bg-green-100 text-green-700">

lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_system.html.erb.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%% system ||= local_assigns[:message] %>
12
<div id="<%= message_variable_name %>_<%%= system.id %>" class="w-full sm:w-auto my-5 space-y-3 rounded-md px-3 py-2 bg-gray-50 border border-gray-200">
23
<div>
34
<span class="inline-block rounded px-2 py-0.5 text-xs font-medium bg-gray-200 text-gray-700">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
<%% tool ||= local_assigns[:message] %>
12
<%%= render tool_result_partial(tool), tool: tool %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%% tool_calls ||= local_assigns[:message] %>
12
<%% tool_calls.<%= tool_call_variable_name.pluralize %>.each do |tool_call| %>
23
<%%= render tool_call_partial(tool_call), tool_calls: tool_calls, tool_call: tool_call %>
34
<%% end %>

lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_user.html.erb.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%% user ||= local_assigns[:message] %>
12
<div id="<%= message_variable_name %>_<%%= user.id %>" class="w-full sm:w-auto my-5 space-y-3 rounded-md px-3 py-2 bg-blue-50">
23
<div>
34
<span class="inline-block rounded px-2 py-0.5 text-xs font-medium bg-blue-100 text-blue-700">

lib/generators/ruby_llm/chat_ui/templates/views/messages/_assistant.html.erb.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%% assistant ||= local_assigns[:message] %>
12
<div id="<%= message_variable_name %>_<%%= assistant.id %>" class="<%= message_variable_name %>"
23
style="margin-bottom: 20px; padding: 10px; border-left: 3px solid #28a745;">
34
<div style="font-weight: bold; margin-bottom: 5px;">Assistant</div>

lib/generators/ruby_llm/chat_ui/templates/views/messages/_system.html.erb.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%% system ||= local_assigns[:message] %>
12
<div id="<%= message_variable_name %>_<%%= system.id %>" class="<%= message_variable_name %>"
23
style="margin-bottom: 20px; padding: 10px; border-left: 3px solid #6b7280; background: #f9fafb;">
34
<div style="font-weight: bold; margin-bottom: 5px; color: #374151;">System</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
<%% tool ||= local_assigns[:message] %>
12
<%%= render tool_result_partial(tool), tool: tool %>

0 commit comments

Comments
 (0)