fix(chat): eager pending AssistantMessage to fix Turbo subscribe race (#1657) (#1658)

* fix(chat): persist eager pending assistant message to fix subscribe race

When the LLM replies in ~1-2s the assistant message broadcast could
fire before the client's Turbo stream subscription was established,
leaving the UI stuck on the thinking indicator while the response was
already persisted.

Create the AssistantMessage as `pending` synchronously in
`Chat#ask_assistant_later`, so it is rendered server-side on the chat
show page with a "Thinking ..." inline placeholder. The worker then
finds and updates the existing row via `append_text!`, which flips the
status to `complete` and broadcasts updates against a DOM id that is
already in the page — no race possible. On error, the placeholder is
destroyed if no content streamed, otherwise demoted to `failed`.

Replaces the standalone thinking indicator partial and the
`Assistant::Broadcastable` thinking helpers, both now redundant.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(chat): bind each assistant job to its specific pending placeholder

Addressing review feedback on #1658:

1. The pending placeholder lookup based on `last pending` was racy —
   back-to-back user messages would let one job fill another job's
   placeholder. Pass the placeholder through the job arguments
   (`AssistantResponseJob.perform_later(user_message, pending)`) so
   each turn is bound to its own row.

2. In `Assistant::External#respond_to`, the configured/authorized
   guards raise before the local was bound, leaving rescue cleanup
   with `nil` and the placeholder visible forever. Bind the parameter
   first so cleanup can destroy it on the misconfigured path.

The kwarg defaults to nil so the API#retry path
(`AssistantResponseJob.perform_later(new_message)`) and the model-level
test calls continue to work — they fall back to an in-memory new
message, restoring the original test count assertions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(chat): i18n the pending assistant placeholder string

Move the hardcoded "Thinking ..." indicator into the locale file per
CLAUDE.md i18n guidelines. With i18n.fallbacks enabled, non-en locales
fall back to English until translated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Add thinking label translations

* Fix chat pending assistant expectations

* Fix external assistant pending test lookup

* Scope chat stream targets per chat

* Update message broadcast target tests

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michal Tajchert
2026-05-03 20:33:29 +02:00
committed by GitHub
parent 50936000e7
commit ccd6a53071
28 changed files with 91 additions and 92 deletions

View File

@@ -1,13 +1,11 @@
class Assistant::Base
include Assistant::Broadcastable
attr_reader :chat
def initialize(chat)
@chat = chat
end
def respond_to(message)
def respond_to(message, assistant_message: nil)
raise NotImplementedError, "#{self.class}#respond_to must be implemented"
end
end

View File

@@ -1,12 +0,0 @@
module Assistant::Broadcastable
extend ActiveSupport::Concern
private
def update_thinking(thought)
chat.broadcast_update target: "thinking-indicator", partial: "chats/thinking_indicator", locals: { chat: chat, message: thought }
end
def stop_thinking
chat.broadcast_remove target: "thinking-indicator"
end
end

View File

@@ -17,12 +17,8 @@ class Assistant::Builtin < Assistant::Base
@functions = functions
end
def respond_to(message)
assistant_message = AssistantMessage.new(
chat: chat,
content: "",
ai_model: message.ai_model
)
def respond_to(message, assistant_message: nil)
assistant_message ||= AssistantMessage.new(chat: chat, content: "", ai_model: message.ai_model)
llm_provider = get_model_provider(message.ai_model)
unless llm_provider
@@ -40,7 +36,6 @@ class Assistant::Builtin < Assistant::Base
responder.on(:output_text) do |text|
if assistant_message.content.blank?
stop_thinking
Chat.transaction do
assistant_message.append_text!(text)
chat.update_latest_response!(latest_response_id)
@@ -51,7 +46,6 @@ class Assistant::Builtin < Assistant::Base
end
responder.on(:response) do |data|
update_thinking("Analyzing your data...")
if data[:function_tool_calls].present?
assistant_message.tool_calls = data[:function_tool_calls]
latest_response_id = data[:id]
@@ -62,13 +56,13 @@ class Assistant::Builtin < Assistant::Base
responder.respond(previous_response_id: latest_response_id)
rescue => e
stop_thinking
# If we streamed any partial content before the error, the message was
# persisted with the default `complete` status. Demote it to `failed` so
# `Assistant::Responder#conversation_history` won't feed a broken turn
# back into future prompts.
if assistant_message&.persisted?
assistant_message.update_columns(status: "failed")
if assistant_message.content.blank?
assistant_message.destroy
else
# Demote partially-streamed turns to `failed` so `Responder#conversation_history` excludes them.
assistant_message.update_columns(status: "failed")
end
end
chat.add_error(e)
end

View File

@@ -33,8 +33,9 @@ class Assistant::External < Assistant::Base
end
end
def respond_to(message)
def respond_to(message, assistant_message: nil)
response_completed = false
assistant_message ||= AssistantMessage.new(chat: chat, content: "", ai_model: "external-agent")
unless self.class.configured?
raise Assistant::Error,
@@ -45,12 +46,6 @@ class Assistant::External < Assistant::Base
raise Assistant::Error, "Your account is not authorized to use the external assistant."
end
assistant_message = AssistantMessage.new(
chat: chat,
content: "",
ai_model: "external-agent"
)
client = build_client
messages = build_conversation_messages
@@ -58,17 +53,10 @@ class Assistant::External < Assistant::Base
messages: messages,
user: "sure-family-#{chat.user.family_id}"
) do |text|
if assistant_message.content.blank?
stop_thinking
assistant_message.content = text
assistant_message.save!
else
assistant_message.append_text!(text)
end
assistant_message.append_text!(text)
end
if assistant_message.new_record?
stop_thinking
if assistant_message.content.blank?
raise Assistant::Error, "External assistant returned an empty response."
end
@@ -76,12 +64,10 @@ class Assistant::External < Assistant::Base
assistant_message.update!(ai_model: model) if model.present?
rescue Assistant::Error, ActiveRecord::ActiveRecordError => e
cleanup_partial_response(assistant_message) unless response_completed
stop_thinking
chat.add_error(e)
rescue => e
Rails.logger.error("[Assistant::External] Unexpected error: #{e.class} - #{e.message}")
cleanup_partial_response(assistant_message) unless response_completed
stop_thinking
chat.add_error(Assistant::Error.new("Something went wrong with the external assistant. Check server logs for details."))
end
@@ -103,7 +89,7 @@ class Assistant::External < Assistant::Base
end
def build_conversation_messages
chat.conversation_messages.ordered.last(MAX_CONVERSATION_MESSAGES).map do |msg|
chat.conversation_messages.where(status: "complete").ordered.last(MAX_CONVERSATION_MESSAGES).map do |msg|
{ role: msg.role, content: msg.content }
end
end