Skip to content

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

DEV: migrate `enabled_chat_bot` column to a setting (#36707)

Keegan Georgede3904e9fc96
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb
index de1fcc2653d..02d29dbeb66 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb
@@ -130,8 +130,7 @@ module DiscourseAi
}
# Clean up companion users
- llm_model.enabled_chat_bot = false
- llm_model.toggle_companion_user
+ llm_model.cleanup_companion_user
if llm_model.destroy
log_llm_model_deletion(model_details)
@@ -211,7 +210,6 @@ module DiscourseAi
:max_prompt_tokens,
:max_output_tokens,
:api_key,
- :enabled_chat_bot,
:vision_enabled,
:input_cost,
:cached_input_cost,
@@ -260,8 +258,6 @@ module DiscourseAi
},
max_output_tokens: {
},
- enabled_chat_bot: {
- },
vision_enabled: {
},
api_key: {
diff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rb
index e1229bb8c6a..e786d90503d 100644
--- a/plugins/discourse-ai/app/models/llm_model.rb
+++ b/plugins/discourse-ai/app/models/llm_model.rb
@@ -1,6 +1,10 @@
# frozen_string_literal: true
class LlmModel < ActiveRecord::Base
+ # TODO: Remove this line after 20251212144720_populate_ai_bot_enabled_llms_setting migration
+ # has been promoted to pre-deploy
+ self.ignored_columns = %w[enabled_chat_bot]
+
FIRST_BOT_USER_ID = -1200
BEDROCK_PROVIDER_NAME = "aws_bedrock"
DEFAULT_ALLOWED_ATTACHMENT_TYPES = [].freeze
@@ -38,6 +42,14 @@ class LlmModel < ActiveRecord::Base
where(id: model_ids)
end
+ def self.enabled_chat_bot_ids
+ SiteSetting.ai_bot_enabled_llms.split("|").map(&:to_i).reject(&:zero?)
+ end
+
+ def enabled_chat_bot?
+ self.class.enabled_chat_bot_ids.include?(id)
+ end
+
def self.provider_params
{
aws_bedrock: {
@@ -156,7 +168,7 @@ class LlmModel < ActiveRecord::Base
def toggle_companion_user
return if name == "fake" && Rails.env.production?
- enable_check = SiteSetting.ai_bot_enabled && enabled_chat_bot
+ enable_check = SiteSetting.ai_bot_enabled && enabled_chat_bot?
if enable_check
if !user
@@ -182,16 +194,22 @@ class LlmModel < ActiveRecord::Base
user.active = true
user.save!(validate: false)
end
- elsif user
- # will include deleted
- has_posts = DB.query_single("SELECT 1 FROM posts WHERE user_id = #{user.id} LIMIT 1").present?
+ else
+ cleanup_companion_user
+ end
+ end
- if has_posts
- user.update!(active: false) if user.active
- else
- user.destroy!
- self.update!(user: nil)
- end
+ def cleanup_companion_user
+ return unless user
+
+ # will include deleted
+ has_posts = DB.query_single("SELECT 1 FROM posts WHERE user_id = #{user.id} LIMIT 1").present?
+
+ if has_posts
+ user.update!(active: false) if user.active
+ else
+ user.destroy!
+ self.update!(user: nil)
end
end
@@ -287,7 +305,6 @@ end
# cache_write_cost :float default(0.0)
# cached_input_cost :float
# display_name :string
-# enabled_chat_bot :boolean default(FALSE), not null
# input_cost :float
# max_output_tokens :integer
# max_prompt_tokens :integer not null
diff --git a/plugins/discourse-ai/app/serializers/llm_model_serializer.rb b/plugins/discourse-ai/app/serializers/llm_model_serializer.rb
index e61cc8bd102..1d959fad393 100644
--- a/plugins/discourse-ai/app/serializers/llm_model_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/llm_model_serializer.rb
@@ -15,7 +15,6 @@ class LlmModelSerializer < ApplicationSerializer
:tokenizer,
:api_key,
:url,
- :enabled_chat_bot,
:provider_params,
:vision_enabled,
:input_cost,
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.js
index e57ef069b2e..ca2ac7e3359 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.js
@@ -13,7 +13,6 @@ export default class AiLlm extends RestModel {
"max_output_tokens",
"url",
"api_key",
- "enabled_chat_bot",
"provider_params",
"vision_enabled",
"input_cost",
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
index 78492ac611d..c55b7da4eb4 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
@@ -78,7 +78,6 @@ export default class AiLlmEditorForm extends Component {
display_name: model.display_name,
name: model.name,
provider: model.provider,
- enabled_chat_bot: model.enabled_chat_bot,
vision_enabled: model.vision_enabled,
input_cost: model.input_cost,
output_cost: model.output_cost,
@@ -511,16 +510,6 @@ export default class AiLlmEditorForm extends Component {
<field.Hint />
</form.Field>
- <form.Field
- @name="enabled_chat_bot"
- @title={{i18n "discourse_ai.llms.enabled_chat_bot"}}
- @tooltip={{i18n "discourse_ai.llms.hints.enabled_chat_bot"}}
- @format="large"
- as |field|
- >
- <field.Checkbox />
- </form.Field>
-
{{#if @model.user}}
<form.Container @title={{i18n "discourse_ai.llms.ai_bot_user"}}>
<a
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 5b6a364648e..7b8261ee8dc 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -645,7 +645,6 @@ en:
max_output_tokens: "Max output tokens"
url: "URL of the service hosting the model"
api_key: "API Key of the service hosting the model"
- enabled_chat_bot: "Allow AI bot selector"
vision_enabled: "Vision enabled"
allowed_attachment_types: "Allowed attachments"
attachment_types_placeholder: "Select or type extensions (e.g., pdf, txt, docx)"
@@ -750,7 +749,6 @@ en:
name: "We include this in the API call to specify which model we'll use"
vision_enabled: "If enabled, the AI will attempt to understand images. It depends on the model being used supporting vision. Supported by latest models from Anthropic, Google, and OpenAI."
allowed_attachment_types: "Choose which attachment types are forwarded to the model. Leave blank to disallow attachments. Images are controlled via vision."
- enabled_chat_bot: "If enabled, users can select this model when creating PMs with the AI bot"
cost_input: "The input cost per 1M tokens for this model"
cost_cached_input: "The cached input cost per 1M tokens for this model (when reading from prompt cache)"
cost_cache_write: "The cache write cost per 1M tokens for this model (when writing to prompt cache)"
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index f784b0cba60..66d4ff31b75 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -124,6 +124,7 @@ en:
ai_summary_backfill_maximum_topics_per_hour: "Number of topic summaries to backfill per hour."
ai_bot_enabled: "Enable the AI Bot module."
+ ai_bot_enabled_llms: "LLMs available to select for AI bot conversations"
ai_bot_enable_chat_warning: "Display a warning when PM chat is initiated. Can be overriden by editing the translation string: discourse_ai.ai_bot.pm_warning"
ai_bot_allowed_groups: "When the GPT Bot has access to the PM, it will reply to members of these groups."
ai_bot_debugging_allowed_groups: "Allow these groups to see a debug button on posts which displays the raw AI request and response"
diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.yml
index 8ffde34c253..7227045e35c 100644
--- a/plugins/discourse-ai/config/settings.yml
+++ b/plugins/discourse-ai/config/settings.yml
@@ -365,6 +365,15 @@ discourse_ai:
default: false
client: true
area: "ai-features/bot"
+ ai_bot_enabled_llms:
+ default: ""
+ client: true
+ type: list
+ list_type: compact
+ allow_any: false
+ enum: "DiscourseAi::Configuration::LlmEnumerator"
+ refresh: true
+ area: "ai-features/bot"
ai_bot_enable_chat_warning:
default: false
client: true
diff --git a/plugins/discourse-ai/db/migrate/20251212144720_populate_ai_bot_enabled_llms_setting.rb b/plugins/discourse-ai/db/migrate/20251212144720_populate_ai_bot_enabled_llms_setting.rb
new file mode 100644
index 00000000000..e12f283f817
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20251212144720_populate_ai_bot_enabled_llms_setting.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class PopulateAiBotEnabledLlmsSetting < ActiveRecord::Migration[7.2]
+ def up
+ enabled_ids =
+ DB.query_single("SELECT id FROM llm_models WHERE enabled_chat_bot = true ORDER BY id")
+
+ return if enabled_ids.empty?
+
+ setting_value = enabled_ids.join("|")
+
+ DB.exec(<<~SQL, setting: "ai_bot_enabled_llms", value: setting_value)
+ INSERT INTO site_settings (name, value, data_type, created_at, updated_at)
+ VALUES (:setting, :value, 8, NOW(), NOW())
+ ON CONFLICT (name) DO UPDATE SET value = :value, updated_at = NOW()
+ SQL
+ end
+
+ def down
+ DB.exec("DELETE FROM site_settings WHERE name = 'ai_bot_enabled_llms'")
+ end
+end
diff --git a/plugins/discourse-ai/lib/ai_bot/entry_point.rb b/plugins/discourse-ai/lib/ai_bot/entry_point.rb
index c1ecd3848be..08b2dac879a 100644
--- a/plugins/discourse-ai/lib/ai_bot/entry_point.rb
+++ b/plugins/discourse-ai/lib/ai_bot/entry_point.rb
@@ -9,7 +9,7 @@ module DiscourseAi
AiPersona
.persona_users
.map { |persona| persona[:user_id] }
- .concat(LlmModel.where(enabled_chat_bot: true).pluck(:user_id))
+ .concat(LlmModel.where(id: LlmModel.enabled_chat_bot_ids).pluck(:user_id).compact)
end
def self.find_participant_in(participant_ids)
@@ -31,11 +31,14 @@ module DiscourseAi
end
def self.enabled_user_ids_and_models_map
- DB.query_hash(<<~SQL)
+ enabled_ids = LlmModel.enabled_chat_bot_ids
+ return [] if enabled_ids.empty?
+
+ DB.query_hash(<<~SQL, ids: enabled_ids)
SELECT users.username AS username, users.id AS id, llms.id AS llm_model_id, llms.name AS model_name, llms.display_name AS display_name
FROM llm_models llms
INNER JOIN users ON llms.user_id = users.id
- WHERE llms.enabled_chat_bot
+ WHERE llms.id IN (:ids)
SQL
end
@@ -109,7 +112,8 @@ module DiscourseAi
end
plugin.on(:site_setting_changed) do |name, _old_value, _new_value|
- if name == :ai_bot_enabled || name == :discourse_ai_enabled
+ if name == :ai_bot_enabled || name == :discourse_ai_enabled ||
+ name == :ai_bot_enabled_llms
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
end
end
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 66b4c81e9e8..0e4178ee09b 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -110,7 +110,7 @@ module DiscourseAi
all_llm_users =
LlmModel
- .where(enabled_chat_bot: true)
+ .where(id: LlmModel.enabled_chat_bot_ids)
.joins(:user)
.pluck("users.id", "users.username_lower")
diff --git a/plugins/discourse-ai/lib/configuration/feature.rb b/plugins/discourse-ai/lib/configuration/feature.rb
index 996b44c94a5..6c12e8d37d2 100644
--- a/plugins/discourse-ai/lib/configuration/feature.rb
+++ b/plugins/discourse-ai/lib/configuration/feature.rb
@@ -178,8 +178,9 @@ module DiscourseAi
def lookup_bot_llms
persona_llms = AiPersona.where(id: lookup_bot_persona_ids).pluck(:default_llm_id)
+ enabled_chat_bot_llm_ids = LlmModel.enabled_chat_bot_ids
- LlmModel.where(id: persona_llms).where(enabled_chat_bot: true).to_a
+ LlmModel.where(id: (persona_llms + enabled_chat_bot_llm_ids).uniq).to_a
end
def translation_features
diff --git a/plugins/discourse-ai/lib/configuration/llm_enumerator.rb b/plugins/discourse-ai/lib/configuration/llm_enumerator.rb
index 735c94ece28..1d20294ea95 100644
--- a/plugins/discourse-ai/lib/configuration/llm_enumerator.rb
+++ b/plugins/discourse-ai/lib/configuration/llm_enumerator.rb
@@ -9,10 +9,7 @@ module DiscourseAi
rval = Hash.new { |h, k| h[k] = [] }
if SiteSetting.ai_bot_enabled
- LlmModel
- .where("enabled_chat_bot = ?", true)
- .pluck(:id)
- .each { |llm_id| rval[llm_id] << { type: :ai_bot } }
+ LlmModel.enabled_chat_bot_ids.each { |llm_id| rval[llm_id] << { type: :ai_bot } }
end
# this is unconditional, so it is clear that we always signal configuration
@@ -101,6 +98,8 @@ module DiscourseAi
# returns an array of hashes (id: , name:, vision_enabled:)
def self.values_for_serialization
+ return [] unless table_exists?
+
DB.query_hash(<<~SQL).map(&:symbolize_keys)
SELECT id, display_name AS name, vision_enabled
FROM llm_models
@@ -108,11 +107,20 @@ module DiscourseAi
end
def self.values
+ return [] unless table_exists?
+
DB.query_hash(<<~SQL).map(&:symbolize_keys)
SELECT display_name AS name, id AS value
FROM llm_models
SQL
end
+
+ def self.table_exists?
+ DB.exec("SELECT 1 FROM llm_models LIMIT 0")
+ true
+ rescue StandardError
+ false
+ end
end
end
end
diff --git a/plugins/discourse-ai/spec/configuration/feature_spec.rb b/plugins/discourse-ai/spec/configuration/feature_spec.rb
index eeacd009c8d..e1a7facde46 100644
--- a/plugins/discourse-ai/spec/configuration/feature_spec.rb
+++ b/plugins/discourse-ai/spec/configuration/feature_spec.rb
@@ -112,8 +112,11 @@ RSpec.describe DiscourseAi::Configuration::Feature do
end
describe ".bot_features" do
- fab!(:bot_llm) { Fabricate(:llm_model, enabled_chat_bot: true) }
- fab!(:non_bot_llm) { Fabricate(:llm_model, enabled_chat_bot: false) }
+ fab!(:bot_llm, :llm_model)
+ fab!(:non_bot_llm, :llm_model)
+
+ before { SiteSetting.ai_bot_enabled_llms = bot_llm.id.to_s }
+
fab!(:chat_persona) do
Fabricate(
:ai_persona,
@@ -161,7 +164,11 @@ RSpec.describe DiscourseAi::Configuration::Feature do
expect(bot_feature.module_name).to eq(DiscourseAi::Configuration::Module::BOT)
end
- it "returns only LLMs with enabled_chat_bot" do
+ it "returns only LLMs enabled in ai_bot_enabled_llms setting" do
+ # Disable all other personas to ensure only test personas are active
+ expected_persona_ids = [chat_persona.id, dm_persona.id, topic_persona.id, pm_persona.id]
+ AiPersona.where.not(id: expected_persona_ids).update_all(enabled: false)
+
expect(bot_feature.llm_models).to contain_exactly(bot_llm)
expect(bot_feature.llm_models).not_to include(non_bot_llm)
end
diff --git a/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb
index b2b76ae6b20..5d8bec763f8 100644
--- a/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb
+++ b/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb
@@ -131,5 +131,4 @@ Fabricator(:seeded_model, from: :llm_model) do
api_key "DSC"
tokenizer "DiscourseAi::Tokenizer::OpenAiTokenizer"
url "https://cdck.test/"
- enabled_chat_bot true
end
diff --git a/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb b/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb
index e91d1189539..7bbd78950cd 100644
--- a/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb
@@ -6,7 +6,7 @@ describe DiscourseAi::Automation::LlmPersonaTriage do
fab!(:user)
fab!(:bot_user, :user)
- fab!(:llm_model) { Fabricate(:anthropic_model, name: "claude-3-opus", enabled_chat_bot: true) }
+ fab!(:llm_model) { Fabricate(:anthropic_model, name: "claude-3-opus") }
fab!(:ai_persona) do
persona =
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 cc5d71fb915..48a3db0f323 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
@@ -774,8 +774,8 @@ RSpec.describe DiscourseAi::AiBot::Playground do
post.topic.custom_fields["ai_persona_id"] = persona.id.to_s
post.topic.save_custom_fields
- llm2 = Fabricate(:llm_model, enabled_chat_bot: true)
-
+ llm2 = Fabricate(:llm_model)
+ SiteSetting.ai_bot_enabled_llms = llm2.id.to_s
llm2.toggle_companion_user
DiscourseAi::Completions::Llm.with_prepared_responses(["Hi from bot two"], llm: llm2) do
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/site_setting_extension_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/site_setting_extension_spec.rb
index 4b3a05b0899..f1bb90cbae1 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/site_setting_extension_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/site_setting_extension_spec.rb
@@ -13,9 +13,7 @@ describe DiscourseAi::AiBot::SiteSettingsExtension do
it "correctly creates/deletes bot accounts as needed" do
SiteSetting.ai_bot_enabled = true
- gpt_4.update!(enabled_chat_bot: true)
- claude_2.update!(enabled_chat_bot: false)
- gpt_35_turbo.update!(enabled_chat_bot: false)
+ SiteSetting.ai_bot_enabled_llms = gpt_4.id.to_s
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
@@ -23,9 +21,7 @@ describe DiscourseAi::AiBot::SiteSettingsExtension do
expect(user_exists?("gpt-3.5-turbo")).to eq(false)
expect(user_exists?("claude-2")).to eq(false)
- gpt_4.update!(enabled_chat_bot: false)
- claude_2.update!(enabled_chat_bot: false)
- gpt_35_turbo.update!(enabled_chat_bot: true)
+ SiteSetting.ai_bot_enabled_llms = gpt_35_turbo.id.to_s
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
@@ -33,9 +29,7 @@ describe DiscourseAi::AiBot::SiteSettingsExtension do
expect(user_exists?("gpt-3.5-turbo")).to eq(true)
expect(user_exists?("claude-2")).to eq(false)
- gpt_4.update!(enabled_chat_bot: false)
- claude_2.update!(enabled_chat_bot: true)
- gpt_35_turbo.update!(enabled_chat_bot: true)
+ SiteSetting.ai_bot_enabled_llms = "#{claude_2.id}|#{gpt_35_turbo.id}"
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
@@ -53,7 +47,7 @@ describe DiscourseAi::AiBot::SiteSettingsExtension do
it "leaves accounts around if they have any posts" do
SiteSetting.ai_bot_enabled = true
- gpt_4.update!(enabled_chat_bot: true)
+ SiteSetting.ai_bot_enabled_llms = gpt_4.id.to_s
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
user = DiscourseAi::AiBot::EntryPoint.find_user_from_model("gpt-4")
diff --git a/plugins/discourse-ai/spec/plugin_helper.rb b/plugins/discourse-ai/spec/plugin_helper.rb
index 8ccff6faf1b..177ebfa1863 100644
--- a/plugins/discourse-ai/spec/plugin_helper.rb
+++ b/plugins/discourse-ai/spec/plugin_helper.rb
@@ -2,11 +2,8 @@
module DiscourseAi::ChatBotHelper
def toggle_enabled_bots(bots: [])
- models = LlmModel.all
- models = models.where.not(id: bots.map(&:id)) if bots.present?
- models.update_all(enabled_chat_bot: false)
-
- bots.each { |b| b.update!(enabled_chat_bot: true) }
+ SiteSetting.ai_bot_enabled = true if bots.any?
+ SiteSetting.ai_bot_enabled_llms = bots.map(&:id).join("|")
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
end
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb
index b78f04bc77d..1ce1a022b2a 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb
@@ -10,7 +10,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do
end
describe "GET #index" do
- fab!(:llm_model) { Fabricate(:llm_model, enabled_chat_bot: true) }
+ fab!(:llm_model)
fab!(:llm_model2, :llm_model)
fab!(:ai_persona) do
Fabricate(
@@ -50,6 +50,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do
it "lists enabled features on appropriate LLMs" do
SiteSetting.ai_bot_enabled = true
+ SiteSetting.ai_bot_enabled_llms = llm_model.id.to_s
fake_model = assign_fake_provider_to(:ai_default_llm_model)
# setting the setting calls the model
@@ -169,15 +170,15 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do
expect(history.subject).to eq(valid_attrs[:display_name]) # Verify subject is set to display_name
end
- it "creates a companion user" do
- post "/admin/plugins/discourse-ai/ai-llms.json",
- params: {
- ai_llm: valid_attrs.merge(enabled_chat_bot: true),
- }
+ it "creates a companion user when LLM is in ai_bot_enabled_llms setting" do
+ post "/admin/plugins/discourse-ai/ai-llms.json", params: { ai_llm: valid_attrs }
created_model = LlmModel.last
- expect(created_model.user_id).to be_present
+ SiteSetting.ai_bot_enabled_llms = created_model.id.to_s
+ created_model.toggle_companion_user
+
+ expect(created_model.reload.user_id).to be_present
end
it "stores provider-specific config params" do
@@ -391,22 +392,27 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do
expect(response.status).to eq(404)
end
- it "creates a companion user" do
+ it "creates a companion user when LLM is added to ai_bot_enabled_llms setting" do
+ SiteSetting.ai_bot_enabled_llms = llm_model.id.to_s
+
put "/admin/plugins/discourse-ai/ai-llms/#{llm_model.id}.json",
params: {
- ai_llm: update_attrs.merge(enabled_chat_bot: true),
+ ai_llm: update_attrs,
}
expect(llm_model.reload.user_id).to be_present
end
- it "removes the companion user when desabling the chat bot option" do
- llm_model.update!(enabled_chat_bot: true)
+ it "removes the companion user when LLM is removed from ai_bot_enabled_llms setting" do
+ SiteSetting.ai_bot_enabled_llms = llm_model.id.to_s
llm_model.toggle_companion_user
+ expect(llm_model.reload.user_id).to be_present
+
+ SiteSetting.ai_bot_enabled_llms = ""
put "/admin/plugins/discourse-ai/ai-llms/#{llm_model.id}.json",
params: {
- ai_llm: update_attrs.merge(enabled_chat_bot: false),
+ ai_llm: update_attrs,
}
expect(llm_model.reload.user_id).to be_nil
@@ -576,7 +582,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do
end
it "cleans up companion users before deleting the model" do
- llm_model.update!(enabled_chat_bot: true)
+ SiteSetting.ai_bot_enabled_llms = llm_model.id.to_s
llm_model.toggle_companion_user
companion_user = llm_model.user
diff --git a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb
index 9148ab85f18..17e376467e2 100644
--- a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb
@@ -108,7 +108,7 @@ RSpec.describe DiscourseAi::AiBot::BotController do
describe "#retry_response" do
fab!(:bot_user, :user)
- let!(:llm_model) { Fabricate(:llm_model, user: bot_user, enabled_chat_bot: true) }
+ let!(:llm_model) { Fabricate(:llm_model, user: bot_user) }
let!(:ai_persona) do
Fabricate(
:ai_persona,
@@ -182,14 +182,7 @@ RSpec.describe DiscourseAi::AiBot::BotController do
end
it "allows retrying if LLM model has a negative id (seeded)" do
- seeded_llm_model =
- Fabricate(
- :llm_model,
- id: -9999,
- user: bot_user,
- enabled_chat_bot: true,
- name: "second-model",
- )
+ seeded_llm_model = Fabricate(:llm_model, id: -9999, user: bot_user, name: "second-model")
bot = DiscourseAi::Personas::Bot.as(bot_user, persona: persona, model: seeded_llm_model)
DiscourseAi::Completions::Llm.with_prepared_responses(["first try"], llm: seeded_llm_model) do
@@ -218,8 +211,7 @@ RSpec.describe DiscourseAi::AiBot::BotController do
it "uses the original LLM model when retrying even if persona default changed" do
second_bot_user = Fabricate(:user)
- second_llm_model =
- Fabricate(:llm_model, user: second_bot_user, enabled_chat_bot: true, name: "second-model")
+ second_llm_model = Fabricate(:llm_model, user: second_bot_user, name: "second-model")
pm_topic.topic_allowed_users.find_or_create_by!(user: second_bot_user)
diff --git a/plugins/discourse-ai/spec/system/ai_bot/homepage_spec.rb b/plugins/discourse-ai/spec/system/ai_bot/homepage_spec.rb
index 36b1957f2c4..3ce7eedfd8b 100644
--- a/plugins/discourse-ai/spec/system/ai_bot/homepage_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_bot/homepage_spec.rb
@@ -101,7 +101,7 @@ RSpec.describe "AI Bot - Homepage", type: :system do
pm.custom_fields[DiscourseAi::AiBot::TOPIC_AI_BOT_PM_FIELD] = "t"
pm.save!
- SiteSetting.ai_bot_enabled = true
+ toggle_enabled_bots(bots: [claude_2, claude_2_dup])
SiteSetting.navigation_menu = "sidebar"
Jobs.run_immediately!
SiteSetting.ai_bot_allowed_groups = "#{Group::AUTO_GROUPS[:trust_level_0]}"
@@ -110,8 +110,9 @@ RSpec.describe "AI Bot - Homepage", type: :system do
context "when mobile", mobile: true do
it "allows navigating from AI conversation to regular topic, and loads new post stream" do
- regular_topic = Fabricate(:topic)
- regular_post = Fabricate(:post, topic: regular_topic)
+ category = Fabricate(:category)
+ regular_topic = Fabricate(:topic, category: category, user: user)
+ regular_post = Fabricate(:post, topic: regular_topic, user: user)
post_url = Topic.relative_url(regular_topic.id, regular_topic.slug)
post_with_link =
diff --git a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
index 35ffdbb3115..ad68d702cbe 100644
--- a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
+++ b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
@@ -17,7 +17,6 @@ RSpec.describe "Managing LLM configurations", type: :system do
find("[data-llm-id='anthropic-claude-opus-4-5'] button").click()
form.field("api_key").fill_in("abcd")
- form.field("enabled_chat_bot").toggle
form.submit
expect(page).to have_current_path(%r{/admin/plugins/discourse-ai/ai-llms/\d+/edit})
@@ -35,7 +34,6 @@ RSpec.describe "Managing LLM configurations", type: :system do
expect(llm.max_prompt_tokens.to_i).to eq(model_preset[:tokens])
expect(llm.provider).to eq("anthropic")
expect(llm.display_name).to eq(model_preset[:display_name])
- expect(llm.user_id).not_to be_nil
end
it "manually configures an LLM" do
@@ -58,7 +56,6 @@ RSpec.describe "Managing LLM configurations", type: :system do
form.field("tokenizer").select("DiscourseAi::Tokenizer::Llama3Tokenizer")
form.field("max_output_tokens").fill_in(2000)
form.field("vision_enabled").toggle
- form.field("enabled_chat_bot").toggle
form.submit
expect(page).to have_current_path(%r{/admin/plugins/discourse-ai/ai-llms/\d+/edit})
@@ -84,7 +81,6 @@ RSpec.describe "Managing LLM configurations", type: :system do
expect(llm.provider).to eq("vllm")
expect(llm.max_output_tokens.to_i).to eq(2001)
expect(llm.vision_enabled).to eq(true)
- expect(llm.user_id).not_to be_nil
expect(LlmModel.count).to eq(llm_count + 1)
end