Files
sure/app/models/message.rb
Guillem Arias Fauste 5937195df0 fix(chat): clear assistant bubble on destroy so the chat doesn't hang on Thinking (#2315)
* fix(chat): clear the assistant message bubble when a turn is destroyed

When an assistant turn fails before any text streams (e.g. a provider
auth/model/network error on the first call), Assistant::Builtin#respond_to
destroys the still-pending message. Message only broadcast on create and
update, never on destroy, so the rendered 'Thinking…' bubble was never
removed — the chat appeared stuck thinking forever even though the job
had already errored (and appended an error via chat#add_error below it).

Add after_destroy_commit broadcast_remove_to so a destroyed message is
removed from the page.

* refactor(chat): trim destroy-broadcast comment to one line

Project convention asks for comments only when the why is non-obvious; the
behaviour is already covered by the commit/PR description. Per review feedback.

---------

Co-authored-by: Guillem Arias <guillem.arias@col.vueling.com>
2026-06-14 22:09:02 +02:00

25 lines
680 B
Ruby

class Message < ApplicationRecord
belongs_to :chat
has_many :tool_calls, dependent: :destroy
enum :status, {
pending: "pending",
complete: "complete",
failed: "failed"
}
validates :content, presence: true, unless: :pending?
after_create_commit -> { broadcast_append_to chat, target: chat.messages_target }, if: :broadcast?
after_update_commit -> { broadcast_update_to chat }, if: :broadcast?
# Clears the "Thinking…" bubble if the provider errors before any text streams.
after_destroy_commit -> { broadcast_remove_to chat }, if: :broadcast?
scope :ordered, -> { order(created_at: :asc) }
private
def broadcast?
true
end
end