A platform for community discussion. Free, open, simple.
FIX: Keep AI error replies as whispers (#44701)
David Taylore9802082fa24
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rbindex b73a4c6484f..318595d5a5f 100644--- a/plugins/discourse-ai/lib/ai_bot/playground.rb+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb@@ -988,6 +988,7 @@ module DiscourseAiraw: failure_reply,skip_validations: true,skip_guardian: true,+ post_type: post_type,custom_fields: ai_custom_fields(authorization_user_id: authorization_user_id),)end@@ -1019,6 +1020,7 @@ module DiscourseAiraw: error_message,skip_validations: true,skip_guardian: true,+ post_type: post_type,custom_fields: ai_custom_fields(authorization_user_id: authorization_user_id),)end@@ -1037,6 +1039,7 @@ module DiscourseAiraw: failure_reply,skip_validations: true,skip_guardian: true,+ post_type: post_type,custom_fields: ai_custom_fields(authorization_user_id: authorization_user_id),)enddiff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rbindex bc3bac0f35a..aafb5fdb75f 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb@@ -2041,6 +2041,64 @@ RSpec.describe DiscourseAi::AiBot::Playground doexpect(last_post.raw).to include(expected_message)expect(last_post.user_id).to eq(bot_user.id)end++ context "when whispering in a public topic" do+ fab!(:topic_post) { Fabricate(:post, user: user) }++ def last_reply+ topic_post.topic.reload.posts.order(:post_number).last+ end++ it "whispers the credit limit error" do+ allocation =+ Fabricate(+ :llm_credit_allocation,+ llm_model: Fabricate(:seeded_model),+ daily_credits: 1000,+ daily_used: 1000,+ )+ allow(LlmCreditAllocation).to receive(:check_credits!).and_raise(+ LlmCreditAllocation::CreditLimitExceeded.new("Credit limit exceeded", allocation:),+ )++ playground.reply_to(topic_post, whisper: true)++ expect(last_reply.raw).to include(+ I18n.t(+ "discourse_ai.llm_credit_allocation.limit_exceeded_user",+ reset_time: allocation.formatted_reset_time,+ ),+ )+ expect(last_reply).to be_whisper+ end++ it "whispers the reply when the history changes while replying" do+ DiscourseAi::Completions::Llm.with_prepared_responses(["Triage done"]) do+ playground.reply_to(topic_post, whisper: true) do+ topic_post.update_columns(raw: "Edited while replying")+ end+ end++ expect(last_reply.raw).to include("Triage done", "history_changed")+ expect(last_reply).to be_whisper+ end++ it "whispers the reply when an unexpected error interrupts it" do+ allow_any_instance_of(DiscourseAi::Completions::HistorySnapshot).to receive(+ :stamp!,+ ).and_raise(StandardError, "boom")++ DiscourseAi::Completions::Llm.with_prepared_responses(["Triage done"]) do+ expect { playground.reply_to(topic_post, whisper: true) }.to raise_error(+ StandardError,+ "boom",+ )+ end++ expect(last_reply.raw).to include("Triage done", "boom")+ expect(last_reply).to be_whisper+ end+ endenddescribe "#canceling a completions" do