A platform for community discussion. Free, open, simple.
FIX: handle noop stream signal for AI bot replies (#39725)
Samb19f6e5038ff
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.jsindex f5f1b4b59ad..6f33c6df089 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js@@ -70,22 +70,14 @@ function initializeAIBotReplies(api) {const streamingState = lookupStreamingState(api);const topicId = this.model.id;+ if (data?.noop) {+ return;+ }+if (data?.done) {streamingState?.markFinishedAfterRender(topicId, data?.post_id);} else {- const postId = data?.post_id;- // If the post is already rendered and has no .streaming class, this- // chunk is a stale replay of a stream that already finished. Calling- // markStarted would wrongly show the stop button until the 15-s idle- // timer fires. Clear any leftover state and bail out instead.- if (postId) {- const postEl = document.querySelector(`[data-post-id="${postId}"]`);- if (postEl && !postEl.classList.contains("streaming")) {- streamingState?.markFinished(topicId);- return;- }- }- streamingState?.markStarted(topicId, postId);+ streamingState?.markStarted(topicId, data?.post_id);}streamPostText(this.model.postStream, data);diff --git a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rbindex e16728ac408..96396817423 100644--- a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb@@ -321,20 +321,25 @@ RSpec.describe DiscourseAi::AiBot::BotController doit "streams a replacement into the existing bot reply" doretry_text = "second attempt"+ messages = nilDiscourseAi::Completions::Llm.with_prepared_responses([retry_text]) dopost "/discourse-ai/ai-bot/post/#{reply_post.id}/retry"- Jobs::CreateAiReply.new.execute(- post_id: prompt_post.id,- bot_user_id: bot_user.id,- agent_id: reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD].to_i,- reply_post_id: reply_post.id,- )+ messages =+ MessageBus.track_publish("discourse-ai/ai-bot/topic/#{reply_post.topic_id}") do+ Jobs::CreateAiReply.new.execute(+ post_id: prompt_post.id,+ bot_user_id: bot_user.id,+ agent_id: reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD].to_i,+ reply_post_id: reply_post.id,+ )+ endendexpect(response.status).to eq(200)expect(reply_post.reload.raw).to eq(retry_text)+ expect(messages.first.data).to include(post_id: reply_post.id, raw: "")expect(reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD].to_i).to eq(agent.id,)