Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 5841543

Browse files
committed
FIX: validator
1 parent 5ae8064 commit 5841543

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

config/locales/server.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ en:
583583
llm:
584584
configuration:
585585
create_llm: "You need to setup an LLM before enabling this feature"
586-
disable_module_first: "You have to disable %{setting} first."
586+
disable_modules_first: "You must disable these modules first: %{settings}"
587587
set_llm_first: "Set %{setting} first"
588588
model_unreachable: "We couldn't get a response from this model. Check your settings first."
589589
invalid_seeded_model: "You can't use this model with this feature"

lib/configuration/llm_dependency_validator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ def initialize(opts = {})
1010
def valid_value?(val)
1111
return true if val == "f"
1212

13-
@llm_dependency_setting_name =
14-
DiscourseAi::Configuration::LlmValidator.new.choose_llm_setting_for(@opts[:name])
13+
@llm_dependency_setting_name = :ai_default_llm_model
1514

1615
SiteSetting.public_send(@llm_dependency_setting_name).present?
1716
end

lib/configuration/llm_validator.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ def initialize(opts = {})
99

1010
def valid_value?(val)
1111
if val == ""
12-
@parent_module_name = modules_and_choose_llm_settings.invert[@opts[:name]]
12+
if @opts[:name] == :ai_default_llm_model
13+
@parent_module_names = []
1314

14-
@parent_enabled = SiteSetting.public_send(@parent_module_name)
15-
return !@parent_enabled
15+
enabled_settings.each do |setting_name|
16+
if SiteSetting.public_send(setting_name) == true
17+
@parent_module_names << setting_name
18+
@parent_enabled = true
19+
end
20+
end
21+
22+
return !@parent_enabled
23+
end
1624
end
1725

1826
run_test(val).tap { |result| @unreachable = result }
@@ -43,11 +51,11 @@ def modules_using(llm_model)
4351
end
4452

4553
def error_message
46-
if @parent_enabled
54+
if @parent_enabled && @parent_module_names.present?
4755
return(
4856
I18n.t(
49-
"discourse_ai.llm.configuration.disable_module_first",
50-
setting: @parent_module_name,
57+
"discourse_ai.llm.configuration.disable_modules_first",
58+
settings: @parent_module_names.join(", "),
5159
)
5260
)
5361
end
@@ -57,17 +65,13 @@ def error_message
5765
I18n.t("discourse_ai.llm.configuration.model_unreachable")
5866
end
5967

60-
def choose_llm_setting_for(module_enabler_setting)
61-
modules_and_choose_llm_settings[module_enabler_setting]
62-
end
63-
64-
def modules_and_choose_llm_settings
65-
{
66-
ai_embeddings_semantic_search_enabled: :ai_default_llm_model,
67-
ai_helper_enabled: :ai_default_llm_model,
68-
ai_summarization_enabled: :ai_default_llm_model,
69-
ai_translation_enabled: :ai_default_llm_model,
70-
}
68+
def enabled_settings
69+
%i[
70+
ai_embeddings_semantic_search_enabled
71+
ai_helper_enabled
72+
ai_summarization_enabled
73+
ai_translation_enabled
74+
]
7175
end
7276
end
7377
end
Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
# frozen_string_literal: true
22

3-
RSpec.describe DiscourseAi::Configuration::LlmValidator do
3+
require "rails_helper"
4+
5+
describe DiscourseAi::Configuration::LlmValidator do
46
describe "#valid_value?" do
5-
context "when the parent module is enabled and we try to reset the selected model" do
6-
before do
7-
assign_fake_provider_to(:ai_default_llm_model)
8-
SiteSetting.ai_summarization_enabled = true
9-
end
7+
let(:validator) { described_class.new(name: :ai_default_llm_model) }
8+
9+
before do
10+
assign_fake_provider_to(:ai_default_llm_model)
11+
SiteSetting.ai_helper_enabled = false
12+
SiteSetting.ai_summarization_enabled = false
13+
SiteSetting.ai_embeddings_semantic_search_enabled = false
14+
SiteSetting.ai_translation_enabled = false
15+
end
1016

11-
it "returns false and displays an error message" do
12-
validator = described_class.new(name: :ai_summarization_model)
17+
it "returns true when no modules are enabled and value is empty string" do
18+
expect(validator.valid_value?("")).to eq(true)
19+
end
1320

14-
value = validator.valid_value?("")
21+
it "returns false when a module is enabled and value is empty string" do
22+
SiteSetting.ai_helper_enabled = true
23+
expect(validator.valid_value?("")).to eq(false)
24+
expect(validator.error_message).to include("ai_helper_enabled")
25+
end
26+
27+
it "returns false when multiple modules are enabled and value is empty string" do
28+
SiteSetting.ai_helper_enabled = true
29+
SiteSetting.ai_summarization_enabled = true
30+
expect(validator.valid_value?("")).to eq(false)
31+
expect(validator.error_message).to include("ai_helper_enabled, ai_summarization_enabled")
32+
end
1533

16-
expect(value).to eq(false)
17-
expect(validator.error_message).to include("ai_summarization_enabled")
18-
end
34+
it "returns true for non-empty values regardless of module state" do
35+
SiteSetting.ai_helper_enabled = true
36+
SiteSetting.ai_summarization_enabled = true
37+
expect(validator.valid_value?("some_model")).to eq(true)
1938
end
2039
end
2140
end

0 commit comments

Comments
 (0)