Skip to content

A platform for community discussion. Free, open, simple.

FIX: keep silent AI triage silent on credit exhaustion (#38738)

Rafael dos Santos Silvaf28c24c78aa7
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 7c1229a82aa..5a39b8ce0ba 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -635,6 +635,8 @@ module DiscourseAi
reply_post
rescue LlmCreditAllocation::CreditLimitExceeded => e
+ return if silent_mode
+
reset_time = e.allocation&.formatted_reset_time || ""
locale_key = post.user.admin? ? "limit_exceeded_admin" : "limit_exceeded_user"
error_message =
diff --git a/plugins/discourse-ai/spec/lib/discourse_automation/llm_agent_triage_spec.rb b/plugins/discourse-ai/spec/lib/discourse_automation/llm_agent_triage_spec.rb
index 1d37df62d97..ab60b6057b9 100644
--- a/plugins/discourse-ai/spec/lib/discourse_automation/llm_agent_triage_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_automation/llm_agent_triage_spec.rb
@@ -162,6 +162,31 @@ describe DiscourseAi::Automation::LlmAgentTriage do
expect(last_post.post_type).to eq(Post.types[:whisper]) # This should be a whisper
end
+ it "does not post a credit limit error when silent mode is enabled" do
+ add_automation_field("silent_mode", true, type: "boolean")
+ post = Fabricate(:post, raw: "This is a test post that needs triage")
+
+ seeded_llm = Fabricate(:seeded_model)
+ allocation =
+ Fabricate(
+ :llm_credit_allocation,
+ llm_model: seeded_llm,
+ daily_credits: 1000,
+ daily_used: 1000,
+ )
+
+ exception = LlmCreditAllocation::CreditLimitExceeded.new("Credit limit exceeded", allocation:)
+ allow(LlmCreditAllocation).to receive(:check_credits!).and_raise(exception)
+
+ expect {
+ automation.running_in_background!
+ automation.trigger!({ "post" => post })
+ }.not_to raise_error
+
+ expect(post.topic.reload.posts.count).to eq(1)
+ expect(post.topic.posts.where(user_id: bot_user.id)).to be_blank
+ end
+
it "does not respond to posts made by bots" do
bot = Fabricate(:bot)
bot_post = Fabricate(:post, user: bot, raw: "This is a bot post")
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
index b25d6e56cd9..bb1e5a5edd1 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
@@ -1178,6 +1178,25 @@ RSpec.describe DiscourseAi::AiBot::Playground do
expect(last_post.user_id).to eq(bot_user.id)
end
+ it "does not send a credit limit error message in silent mode" do
+ seeded_llm = Fabricate(:seeded_model)
+ allocation =
+ Fabricate(
+ :llm_credit_allocation,
+ llm_model: seeded_llm,
+ daily_credits: 1000,
+ daily_used: 1000,
+ )
+
+ exception = LlmCreditAllocation::CreditLimitExceeded.new("Credit limit exceeded", allocation:)
+ allow(LlmCreditAllocation).to receive(:check_credits!).and_raise(exception)
+
+ expect { playground.reply_to(third_post, silent_mode: true) }.not_to raise_error
+
+ expect(pm.reload.posts.count).to eq(3)
+ expect(pm.posts.order(:post_number).last.id).to eq(third_post.id)
+ end
+
it "sends admin credit limit error message when credit limit is exceeded for admin users" do
seeded_llm = Fabricate(:seeded_model)
allocation =