Skip to content

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

FEATURE: Allow detecting content locales with a decision model (#45410)

Rafael dos Santos Silva7716aced9742
diff --git a/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rb
index c1e3251ea8b..57d6c28c344 100644
--- a/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rb
+++ b/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rb
@@ -9,10 +9,9 @@ module Jobs
def execute(args)
return if !DiscourseAi::Translation.backfill_enabled?(target: Category)
- llm_model = find_llm_model
- return if llm_model.blank?
+ return if !DiscourseAi::Translation.locale_detector_configured?
- unless LlmCreditAllocation.credits_available?(llm_model)
+ unless DiscourseAi::Translation.credits_available_for_locale_detection?
Rails.logger.info(
"Categories locale detection backfill skipped: insufficient credits. Will resume when credits reset.",
)
@@ -36,14 +35,5 @@ module Jobs
DiscourseAi::Translation::VerboseLogger.log("Detected #{categories.size} category locales")
end
-
- private
-
- def find_llm_model
- agent_klass = AiAgent.find_by_id_from_cache(SiteSetting.ai_translation_locale_detector_agent)
- return nil if agent_klass.blank?
-
- DiscourseAi::Translation::BaseTranslator.preferred_llm_model(agent_klass)
- end
end
end
diff --git a/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb
index 6f3dc0eb63e..1ba18b32b09 100644
--- a/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb
+++ b/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb
@@ -9,10 +9,9 @@ module Jobs
def execute(args)
return if !DiscourseAi::Translation.backfill_enabled?(target: Post)
- llm_model = find_llm_model
- return if llm_model.blank?
+ return if !DiscourseAi::Translation.locale_detector_configured?
- unless LlmCreditAllocation.credits_available?(llm_model)
+ unless DiscourseAi::Translation.credits_available_for_locale_detection?
Rails.logger.info(
"Posts locale detection backfill skipped: insufficient credits. Will resume when credits reset.",
)
@@ -44,14 +43,5 @@ module Jobs
DiscourseAi::Translation::VerboseLogger.log("Detected #{posts.size} post locales")
end
-
- private
-
- def find_llm_model
- agent_klass = AiAgent.find_by_id_from_cache(SiteSetting.ai_translation_locale_detector_agent)
- return nil if agent_klass.blank?
-
- DiscourseAi::Translation::BaseTranslator.preferred_llm_model(agent_klass)
- end
end
end
diff --git a/plugins/discourse-ai/app/jobs/scheduled/sidebar_sections_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/sidebar_sections_locale_detection_backfill.rb
index e9f5a119964..f9929575db4 100644
--- a/plugins/discourse-ai/app/jobs/scheduled/sidebar_sections_locale_detection_backfill.rb
+++ b/plugins/discourse-ai/app/jobs/scheduled/sidebar_sections_locale_detection_backfill.rb
@@ -10,10 +10,9 @@ module Jobs
return if !SiteSetting.content_localization_enabled
return if !DiscourseAi::Translation.backfill_enabled?(target: SidebarSection)
- llm_model = find_llm_model
- return if llm_model.blank?
+ return if !DiscourseAi::Translation.locale_detector_configured?
- unless LlmCreditAllocation.credits_available?(llm_model)
+ unless DiscourseAi::Translation.credits_available_for_locale_detection?
Rails.logger.info(
"Sidebar section locale detection backfill skipped: insufficient credits. Will resume when credits reset.",
)
@@ -41,14 +40,5 @@ module Jobs
"Detected #{sidebar_sections.size} sidebar section locales",
)
end
-
- private
-
- def find_llm_model
- agent_klass = AiAgent.find_by_id_from_cache(SiteSetting.ai_translation_locale_detector_agent)
- return nil if agent_klass.blank?
-
- DiscourseAi::Translation::BaseTranslator.preferred_llm_model(agent_klass)
- end
end
end
diff --git a/plugins/discourse-ai/app/jobs/scheduled/tags_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/tags_locale_detection_backfill.rb
index 22d917b50d5..4a7a766ac45 100644
--- a/plugins/discourse-ai/app/jobs/scheduled/tags_locale_detection_backfill.rb
+++ b/plugins/discourse-ai/app/jobs/scheduled/tags_locale_detection_backfill.rb
@@ -9,10 +9,9 @@ module Jobs
def execute(args)
return if !DiscourseAi::Translation.backfill_enabled?(target: Tag)
- llm_model = find_llm_model
- return if llm_model.blank?
+ return if !DiscourseAi::Translation.locale_detector_configured?
- unless LlmCreditAllocation.credits_available?(llm_model)
+ unless DiscourseAi::Translation.credits_available_for_locale_detection?
Rails.logger.info(
"Tags locale detection backfill skipped: insufficient credits. Will resume when credits reset.",
)
@@ -37,14 +36,5 @@ module Jobs
DiscourseAi::Translation::VerboseLogger.log("Detected #{tags.size} tag locales")
end
-
- private
-
- def find_llm_model
- agent_klass = AiAgent.find_by_id_from_cache(SiteSetting.ai_translation_locale_detector_agent)
- return nil if agent_klass.blank?
-
- DiscourseAi::Translation::BaseTranslator.preferred_llm_model(agent_klass)
- end
end
end
diff --git a/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rb
index 2048b5d7ab9..c879d5aa867 100644
--- a/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rb
+++ b/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rb
@@ -9,10 +9,9 @@ module Jobs
def execute(args)
return if !DiscourseAi::Translation.backfill_enabled?(target: Topic)
- llm_model = find_llm_model
- return if llm_model.blank?
+ return if !DiscourseAi::Translation.locale_detector_configured?
- unless LlmCreditAllocation.credits_available?(llm_model)
+ unless DiscourseAi::Translation.credits_available_for_locale_detection?
Rails.logger.info(
"Topics locale detection backfill skipped: insufficient credits. Will resume when credits reset.",
)
@@ -44,14 +43,5 @@ module Jobs
DiscourseAi::Translation::VerboseLogger.log("Detected #{topics.size} topic locales")
end
-
- private
-
- def find_llm_model
- agent_klass = AiAgent.find_by_id_from_cache(SiteSetting.ai_translation_locale_detector_agent)
- return nil if agent_klass.blank?
-
- DiscourseAi::Translation::BaseTranslator.preferred_llm_model(agent_klass)
- end
end
end
diff --git a/plugins/discourse-ai/app/models/decision_model_credit_allocation.rb b/plugins/discourse-ai/app/models/decision_model_credit_allocation.rb
index 7e9b0304228..9a5568f8102 100644
--- a/plugins/discourse-ai/app/models/decision_model_credit_allocation.rb
+++ b/plugins/discourse-ai/app/models/decision_model_credit_allocation.rb
@@ -13,12 +13,18 @@ class DecisionModelCreditAllocation < ActiveRecord::Base
less_than_or_equal_to: 100,
}
- def self.check_credits!(decision_model, feature_name)
- return if !decision_model&.credit_system_enabled?
- return if DecisionModelFeatureCreditCost.credit_cost_for(decision_model, feature_name).zero?
+ def self.credits_available?(decision_model, feature_name)
+ return true if !decision_model&.credit_system_enabled?
+ if DecisionModelFeatureCreditCost.credit_cost_for(decision_model, feature_name).zero?
+ return true
+ end
- allocation = decision_model.decision_model_credit_allocation
- if allocation.hard_limit_reached?
+ !decision_model.decision_model_credit_allocation.hard_limit_reached?
+ end
+
+ def self.check_credits!(decision_model, feature_name)
+ if !credits_available?(decision_model, feature_name)
+ allocation = decision_model.decision_model_credit_allocation
raise DiscourseAi::Decisions::CreditLimitExceeded.new(
I18n.t(
"discourse_ai.decision_models.credit_limit_exceeded",
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
index 64767bbe4bf..8ddeda0e13f 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
@@ -238,6 +238,7 @@ export const AI_FEATURE_SETTING_GROUPS = {
titleKey: "discourse_ai.features.translation.setting_groups.agents",
settings: [
"ai_translation_locale_detector_agent",
+ "ai_translation_locale_detector_decision_model",
"ai_translation_post_raw_translator_agent",
"ai_translation_topic_title_translator_agent",
"ai_translation_short_text_translator_agent",
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 73c6c8a5874..69021e720d1 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -201,6 +201,7 @@ en:
ai_discord_allowed_guilds: "Discord guilds (servers) where the bot is allowed to search"
ai_translation_enabled: "Enable the AI translation feature"
+ ai_translation_locale_detector_decision_model: "Decision model that detects the language of content before it is translated. Content in a language it cannot place confidently is left to the locale detector agent. Leave blank to detect every language with the agent."
ai_translation_category_scope: "Choose which categories AI translation can use. Include and exclude options use {{setting:ai_translation_categories}}. Personal messages are controlled by {{setting:ai_translation_personal_messages}}."
ai_translation_categories: "Categories used when the AI translation category scope is set to <strong>include</strong> or <strong>exclude</strong>."
ai_translation_personal_messages: "Controls which personal messages are translated. 'none' disables PM translation. 'group' translates only group PMs. 'all' translates all PMs."
diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.yml
index 07121a98b72..6d94bdc1ee8 100644
--- a/plugins/discourse-ai/config/settings.yml
+++ b/plugins/discourse-ai/config/settings.yml
@@ -699,6 +699,11 @@ discourse_ai:
type: enum
enum: "DiscourseAi::Configuration::AgentEnumerator"
area: "ai-features/translation"
+ ai_translation_locale_detector_decision_model:
+ default: ""
+ type: enum
+ enum: "DiscourseAi::Configuration::DecisionModelEnumerator"
+ area: "ai-features/translation"
ai_translation_post_raw_translator_agent:
default: "-28"
type: enum
diff --git a/plugins/discourse-ai/lib/translation.rb b/plugins/discourse-ai/lib/translation.rb
index f5c06269c04..b2df0f3f150 100644
--- a/plugins/discourse-ai/lib/translation.rb
+++ b/plugins/discourse-ai/lib/translation.rb
@@ -204,23 +204,30 @@ module DiscourseAi
models.all? { |model| LlmCreditAllocation.credits_available?(model) }
end
+ def self.locale_detector_configured?
+ DecisionLocaleDetector.decision_model.present? ||
+ llm_model_for_agent(SiteSetting.ai_translation_locale_detector_agent).present?
+ end
+
+ # The detector agent only fills in for a decision model, so the decision model's credits
+ # are what detection depends on.
+ def self.credits_available_for_locale_detection?
+ if (decision_model = DecisionLocaleDetector.decision_model)
+ DecisionModelCreditAllocation.credits_available?(
+ decision_model,
+ DecisionLocaleDetector::FEATURE_NAME,
+ )
+ else
+ credits_available_for_agent_ids?([SiteSetting.ai_translation_locale_detector_agent])
+ end
+ end
+
def self.credits_available_for_post_detection?
- credits_available_for_agent_ids?(
- [
- SiteSetting.ai_translation_locale_detector_agent,
- SiteSetting.ai_translation_post_raw_translator_agent,
- ],
- )
+ credits_available_for_locale_detection? && credits_available_for_post_localization?
end
def self.credits_available_for_topic_detection?
- credits_available_for_agent_ids?(
- [
- SiteSetting.ai_translation_locale_detector_agent,
- SiteSetting.ai_translation_topic_title_translator_agent,
- SiteSetting.ai_translation_post_raw_translator_agent,
- ],
- )
+ credits_available_for_locale_detection? && credits_available_for_topic_localization?
end
def self.credits_available_for_post_localization?
diff --git a/plugins/discourse-ai/lib/translation/decision_locale_detector.rb b/plugins/discourse-ai/lib/translation/decision_locale_detector.rb
new file mode 100644
index 00000000000..f6ba5866fbe
--- /dev/null
+++ b/plugins/discourse-ai/lib/translation/decision_locale_detector.rb
@@ -0,0 +1,139 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Translation
+ # Asks a decision model which language a text is written in. Localization works on base
+ # languages, so each choice covers every regional variant of its language.
+ class DecisionLocaleDetector
+ FEATURE_NAME = "locale_detection"
+ QUESTION_NAME = "language"
+ # below this the detector agent decides instead
+ CONFIDENT_PROBABILITY = 0.9
+ OTHER_CHOICE = "other: any language not listed above"
+ INSTRUCTIONS = <<~TEXT.squish
+ Which language did the author write this text in? Judge by the author's own words and
+ ignore quoted passages, error messages, code, URLs, file paths, usernames and emoji.
+ Each choice covers every regional variant of its language. Choose other when the
+ language is not listed.
+ TEXT
+
+ # base language => [name, the variants its choice covers]
+ COMMON_LANGUAGES = {
+ "en" => ["English", "American, British, Australian and any other English variant"],
+ "zh" => [
+ "Chinese",
+ "Simplified, Traditional, Mandarin, Cantonese and any other Chinese variant",
+ ],
+ "es" => ["Spanish", "Castilian, Latin American and any other Spanish variant"],
+ "pt" => [
+ "Portuguese",
+ "European Portuguese, Brazilian Portuguese and any other Portuguese variant",
+ ],
+ "fr" => [
+ "French",
+ "French from France, Canada, Belgium, Switzerland, Africa and any other French variant",
+ ],
+ "de" => [
+ "German",
+ "German from Germany, Austria, Switzerland and any other German variant",
+ ],
+ "ja" => ["Japanese", "Japanese in any script or variant"],
+ "ru" => ["Russian", "Russian in any country or variant"],
+ "ko" => ["Korean", "Korean from South or North Korea and any other Korean variant"],
+ "it" => ["Italian", "Italian from Italy, Switzerland and any other Italian variant"],
+ "ar" => ["Arabic", "Modern Standard Arabic and any regional Arabic dialect"],
+ "tr" => ["Turkish", "Turkish from Turkey, Cyprus and any other Turkish variant"],
+ "pl" => ["Polish", "Polish in any variant"],
+ "nl" => [
+ "Dutch",
+ "Dutch from the Netherlands, Flemish from Belgium and any other Dutch variant",
+ ],
+ "id" => ["Indonesian", "Bahasa Indonesia in any variant"],
+ "vi" => ["Vietnamese", "Vietnamese in any variant"],
+ "hi" => ["Hindi", "Hindi in Devanagari script and any other Hindi variant"],
+ "fa" => [
+ "Persian",
+ "Farsi from Iran, Dari from Afghanistan, Tajik Persian and any other Persian variant",
+ ],
+ "uk" => ["Ukrainian", "Ukrainian in any variant"],
+ "th" => ["Thai", "Thai in any variant"],
+ "he" => ["Hebrew", "Modern Hebrew in any variant"],
+ "sv" => ["Swedish", "Swedish from Sweden, Finland and any other Swedish variant"],
+ "cs" => ["Czech", "Czech in any variant"],
+ "ro" => ["Romanian", "Romanian from Romania, Moldova and any other Romanian variant"],
+ "el" => ["Greek", "Modern Greek from Greece, Cyprus and any other Greek variant"],
+ "hu" => ["Hungarian", "Hungarian in any variant"],
+ "da" => ["Danish", "Danish in any variant"],
+ "fi" => ["Finnish", "Finnish in any variant"],
+ "nb" => ["Norwegian", "Bokmål, Nynorsk and any other Norwegian variant"],
+ }.freeze
+
+ def self.decision_model
+ id = SiteSetting.ai_translation_locale_detector_decision_model
+ DecisionModel.find_by(id:) if id.present?
+ end
+
+ def initialize(decision_model)
+ @decision_model = decision_model
+ end
+
+ # @return [String, nil] the detected locale; nil when the language is not listed or the
+ # model is unsure, so the detector agent should decide
+ # @raise [DiscourseAi::Decisions::Error]
+ def detect(text)
+ bases_by_choice = choices
+ result =
+ @decision_model.decide(
+ text[0, LanguageDetector::DETECTION_CHAR_LIMIT],
+ questions: [
+ {
+ name: QUESTION_NAME,
+ type: "choice",
+ instructions: INSTRUCTIONS,
+ choices: [*bases_by_choice.keys, OTHER_CHOICE],
+ },
+ ],
+ feature_name: FEATURE_NAME,
+ user: Discourse.system_user,
+ )
+
+ answer = result[QUESTION_NAME]
+ return if answer.refusal?
+ return if answer.probabilities.fetch(answer.choice) < CONFIDENT_PROBABILITY
+
+ base = bases_by_choice[answer.choice]
+ locale_for(base) if base
+ end
+
+ private
+
+ def site_locales
+ [
+ SiteSetting.default_locale,
+ *SiteSetting.content_localization_supported_locales.split("|"),
+ ].compact_blank.uniq
+ end
+
+ # Sites may use languages beyond the common ones, and those must be detectable too.
+ def choices
+ languages = COMMON_LANGUAGES.dup
+ site_locales.each do |locale|
+ base = locale.split("_").first
+ next if languages.key?(base)
+
+ names = LocaleSiteSetting.language_names
+ name = (names[base] || names[locale])&.dig("name")
+ languages[base] = [name, "#{name} in any variant"] if name.present?
+ end
+
+ languages.to_h { |base, (name, variants)| ["#{name} (#{base}): #{variants}", base] }
+ end
+
+ # Prefers the variant the site is configured with, e.g. pt_BR over pt.
+ def locale_for(base)
+ site_locales.find { |locale| LocaleNormalizer.is_same?(locale, base) } ||
+ LocaleNormalizer.normalize_to_i18n(base)
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/translation/language_detector.rb b/plugins/discourse-ai/lib/translation/language_detector.rb
index 9e51d7ca94b..659cbe58880 100644
--- a/plugins/discourse-ai/lib/translation/language_detector.rb
+++ b/plugins/discourse-ai/lib/translation/language_detector.rb
@@ -17,6 +17,25 @@ module DiscourseAi
def detect
return nil if !SiteSetting.ai_translation_enabled
return nil if @text.blank?
+
+ if (decision_model = DecisionLocaleDetector.decision_model)
+ begin
+ locale = DecisionLocaleDetector.new(decision_model).detect(@text)
+ return locale if locale
+ rescue DiscourseAi::Decisions::CreditLimitExceeded
+ # falling back would move the spend to the LLM; detection resumes when credits reset
+ return nil
+ rescue DiscourseAi::Decisions::Error => e
+ VerboseLogger.log("Decision model locale detection failed: #{e.message}")
+ end
+ end
+
+ detect_with_agent
+ end
+
+ private
+
+ def detect_with_agent
if (
ai_agent =
AiAgent.find_by_id_from_cache(SiteSetting.ai_translation_locale_detector_agent)
diff --git a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
index aa122d54ba8..59db301e457 100644
--- a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
+++ b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
@@ -60,6 +60,24 @@ describe Jobs::PostsLocaleDetectionBackfill do
job.execute({})
end
+ it "detects with a decision model until its credits are used up" do
+ decision_model = Fabricate(:decision_model, id: -1)
+ SiteSetting.ai_translation_locale_detector_decision_model = decision_model.id
+ allocation = Fabricate(:decision_model_credit_allocation, decision_model:)
+
+ DiscourseAi::Decisions.with_canned_answers(
+ language: DiscourseAi::Translation::DecisionLocaleDetector::OTHER_CHOICE,
+ ) do |canned|
+ DiscourseAi::Completions::Llm.with_prepared_responses(["en"]) { job.execute({}) }
+ expect(post.reload.locale).to eq("en")
+
+ post.update!(locale: nil)
+ DecisionModelCreditDailyUsage.increment_usage!(decision_model, allocation.daily_credits)
+ job.execute({})
+ expect([post.reload.locale, canned.calls.size]).to eq([nil, 1])
+ end
+ end
+
it "skips bot posts" do
post.update!(user: Discourse.system_user)
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).never
diff --git a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb
index 23c223dde7e..81eccd47ceb 100644
--- a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb
@@ -87,4 +87,69 @@ describe DiscourseAi::Translation::LanguageDetector do
expect(AiAgent).not_to have_received(:find_by)
end
end
+
+ describe ".detect with a decision model" do
+ fab!(:decision_model) { Fabricate(:decision_model, id: -1) }
+
+ let(:other_choice) { DiscourseAi::Translation::DecisionLocaleDetector::OTHER_CHOICE }
+
+ before do
+ SiteSetting.ai_translation_locale_detector_decision_model = decision_model.id
+ SiteSetting.content_localization_supported_locales = "pt_BR|ja"
+ end
+
+ def choice(base)
+ name, variants =
+ DiscourseAi::Translation::DecisionLocaleDetector::COMMON_LANGUAGES.fetch(base)
+ "#{name} (#{base}): #{variants}"
+ end
+
+ it "returns the site's variant of a language the model is confident about" do
+ DiscourseAi::Decisions.with_canned_answers(language: { choice("pt") => 0.95 }) do |canned|
+ expect(described_class.new("Dúvida sobre pagamento").detect).to eq("pt_BR")
+ expect(canned.calls.first[:feature_name]).to eq("locale_detection")
+ end
+ end
+
+ it "offers the site's languages beyond the common ones and truncates long text" do
+ SiteSetting.content_localization_supported_locales = "pt_BR|bs_BA"
+ long_text = "a" * 2000
+
+ DiscourseAi::Decisions.with_canned_answers(
+ language: "Bosnian (bs): Bosnian in any variant",
+ ) do |canned|
+ expect(described_class.new(long_text).detect).to eq("bs_BA")
+ expect(canned.calls.first[:state].length).to eq(
+ DiscourseAi::Translation::LanguageDetector::DETECTION_CHAR_LIMIT,
+ )
+ end
+ end
+
+ it "lets the agent decide when the language is unlisted, unsure or refused" do
+ [other_choice, { choice("nl") => 0.86, other_choice => 0.14 }, :refusal].each do |answer|
+ DiscourseAi::Decisions.with_canned_answers(language: answer) do
+ DiscourseAi::Completions::Llm.with_prepared_responses(["af"]) do
+ expect(described_class.new("Goeie môre").detect).to eq("af")
+ end
+ end
+ end
+ end
+
+ it "lets the agent decide when the decision model request fails" do
+ stub_request(:post, decision_model.url).to_return(status: 400, body: "{}")
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["es"]) do
+ expect(described_class.new("Hola").detect).to eq("es")
+ end
+ end
+
+ it "detects nothing when the decision model has used up its credits" do
+ Fabricate(:decision_model_credit_allocation, decision_model:, daily_used: 10_000)
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["es"]) do |_, _, prompts|
+ expect(described_class.new("Hola").detect).to be_nil
+ expect(prompts).to be_empty
+ end
+ end
+ end
end
diff --git a/plugins/discourse-ai/spec/models/decision_model_credit_allocation_spec.rb b/plugins/discourse-ai/spec/models/decision_model_credit_allocation_spec.rb
index 42b757a8850..5eb8f575cc3 100644
--- a/plugins/discourse-ai/spec/models/decision_model_credit_allocation_spec.rb
+++ b/plugins/discourse-ai/spec/models/decision_model_credit_allocation_spec.rb
@@ -52,6 +52,25 @@ RSpec.describe DecisionModelCreditAllocation do
end
end
+ describe ".credits_available?" do
+ it "is false only once a charged feature has used up the daily allocation" do
+ Fabricate(
+ :decision_model_credit_allocation,
+ decision_model:,
+ daily_credits: 1000,
+ daily_used: 1000,
+ )
+ DecisionModelFeatureCreditCost.create!(
+ decision_model:,
+ feature_name: "free_feature",
+ credits_per_token: 0,
+ )
+
+ expect(described_class.credits_available?(decision_model, "workflow")).to eq(false)
+ expect(described_class.credits_available?(decision_model, "free_feature")).to eq(true)
+ end
+ end
+
describe ".deduct_credits!" do
fab!(:allocation) do
Fabricate(:decision_model_credit_allocation, decision_model:, daily_credits: 1000)