diff --git a/lib/ai_helper/assistant.rb b/lib/ai_helper/assistant.rb index 98807428..c12671f8 100644 --- a/lib/ai_helper/assistant.rb +++ b/lib/ai_helper/assistant.rb @@ -61,8 +61,7 @@ module DiscourseAi if prompt.messages[0][:content].include?("%LANGUAGE%") locale = SiteSetting.default_locale - locale = user.locale || SiteSetting.default_locale if SiteSetting.allow_user_locale && - user + locale = user.locale if SiteSetting.allow_user_locale && user&.locale.present? locale_hash = LocaleSiteSetting.language_names[locale] prompt.messages[0][:content] = prompt.messages[0][:content].gsub( diff --git a/spec/lib/modules/ai_helper/assistant_spec.rb b/spec/lib/modules/ai_helper/assistant_spec.rb index 9c582ec0..a618f7c8 100644 --- a/spec/lib/modules/ai_helper/assistant_spec.rb +++ b/spec/lib/modules/ai_helper/assistant_spec.rb @@ -2,6 +2,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do fab!(:user) + fab!(:empty_locale_user) { Fabricate(:user, locale: "") } let(:prompt) { CompletionPrompt.find_by(id: mode) } before { SiteSetting.ai_helper_model = "fake:fake" } @@ -72,6 +73,8 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do end describe("#localize_prompt!") do + before { SiteSetting.allow_user_locale = true } + it "is able to perform %LANGUAGE% replacements" do prompt = CompletionPrompt.new(messages: { insts: "This is a %LANGUAGE% test" }).messages_with_input( @@ -82,6 +85,17 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do expect(prompt.messages[0][:content].strip).to eq("This is a English (US) test") end + + it "handles users with empty string locales" do + prompt = + CompletionPrompt.new(messages: { insts: "This is a %LANGUAGE% test" }).messages_with_input( + "test", + ) + + subject.localize_prompt!(prompt, empty_locale_user) + + expect(prompt.messages[0][:content].strip).to eq("This is a English (US) test") + end end describe "#generate_and_send_prompt" do