FIX: Users with empty locales would error out during prompt localization (#584)

This commit is contained in:
Rafael dos Santos Silva 2024-04-22 13:55:10 -03:00 committed by GitHub
parent 5ab86923ff
commit 595cde0fd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View File

@ -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(

View File

@ -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