Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discourse_ai:
discourse_ai_enabled:
default: true
default: false
client: true
ai_artifact_security:
client: true
Expand Down
23 changes: 23 additions & 0 deletions db/migrate/20250721192553_enable_ai_if_already_installed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

class EnableAiIfAlreadyInstalled < ActiveRecord::Migration[7.2]
def up
installed_at = DB.query_single(<<~SQL)&.first
SELECT created_at FROM schema_migration_details WHERE version='20230224165056'
SQL

if installed_at && installed_at < 1.hour.ago
# The plugin was installed before we changed it to be disabled-by-default
# Therefore, if there is no existing database value, enable the plugin
execute <<~SQL
INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
VALUES('discourse_ai_enabled', 5, 't', NOW(), NOW())
ON CONFLICT (name) DO NOTHING
SQL
end
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
2 changes: 2 additions & 0 deletions spec/configuration/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def allow_configuring_setting(&block)
DiscourseAi::Completions::Llm.with_prepared_responses(["OK"]) { block.call }
end

before { enable_current_plugin }

describe "#llm_model" do
context "when persona is not found" do
it "returns nil when persona_id is invalid" do
Expand Down
2 changes: 2 additions & 0 deletions spec/configuration/llm_enumerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Fabricate(:automation, script: "llm_report", name: "some automation", enabled: true)
end

before { enable_current_plugin }

describe "#values_for_serialization" do
it "returns an array for that can be used for serialization" do
fake_model.destroy!
Expand Down
2 changes: 2 additions & 0 deletions spec/configuration/llm_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe DiscourseAi::Configuration::LlmValidator do
before { enable_current_plugin }

describe "#valid_value?" do
context "when the parent module is enabled and we try to reset the selected model" do
before do
Expand Down
2 changes: 2 additions & 0 deletions spec/configuration/spam_detection_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
RSpec.describe DiscourseAi::Configuration::SpamDetectionValidator do
let(:validator) { described_class.new }

before { enable_current_plugin }

it "always returns true if setting the value to false" do
expect(validator.valid_value?("f")).to eq(true)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
RSpec.describe MigrateSentimentClassificationResultFormat do
let(:connection) { ActiveRecord::Base.connection }

before { connection.execute(<<~SQL) }
before do
enable_current_plugin
connection.execute(<<~SQL)
INSERT INTO classification_results (model_used, classification, created_at, updated_at) VALUES
('sentiment', '{"neutral": 65, "negative": 20, "positive": 14}', NOW(), NOW()),
('emotion', '{"sadness": 10, "surprise": 15, "fear": 5, "anger": 20, "joy": 30, "disgust": 8, "neutral": 10}', NOW(), NOW());
SQL
end

after { connection.execute("DELETE FROM classification_results") }

Expand All @@ -21,7 +24,7 @@

it "migrates sentiment classifications correctly" do
sentiment_result = connection.execute(<<~SQL).first
SELECT * FROM classification_results
SELECT * FROM classification_results
WHERE model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest';
SQL

Expand All @@ -32,7 +35,7 @@

it "migrates emotion classifications correctly" do
emotion_result = connection.execute(<<~SQL).first
SELECT * FROM classification_results
SELECT * FROM classification_results
WHERE model_used = 'j-hartmann/emotion-english-distilroberta-base';
SQL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def configured_model_id
).first
end

before { enable_current_plugin }

describe "#up" do
context "when embeddings are already configured" do
fab!(:embedding_definition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
RSpec.describe CleanUnusedEmbeddingSearchIndexes do
let(:connection) { ActiveRecord::Base.connection }

before { enable_current_plugin }

describe "#up" do
before do
# Copied from 20241008054440_create_binary_indexes_for_embeddings
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/regular/detect_translate_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:locales) { %w[en ja] }

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand Down
3 changes: 1 addition & 2 deletions spec/jobs/regular/detect_translate_topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:locales) { %w[en ja] }

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand All @@ -32,7 +32,6 @@
end

it "detects locale" do
SiteSetting.discourse_ai_enabled = true
allow(DiscourseAi::Translation::TopicLocaleDetector).to receive(:detect_locale).with(
topic,
).and_return("zh_CN")
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/regular/digest_rag_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
end

before do
enable_current_plugin

SiteSetting.ai_embeddings_selected_model = cloudflare_embedding_def.id
SiteSetting.ai_embeddings_enabled = true
SiteSetting.authorized_extensions = "txt"
Expand Down
1 change: 1 addition & 0 deletions spec/jobs/regular/fast_track_topic_gist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
fab!(:post_2) { Fabricate(:post, topic: topic_1, post_number: 2) }

before do
enable_current_plugin
assign_fake_provider_to(:ai_summarization_model)
SiteSetting.ai_summarization_enabled = true
SiteSetting.ai_summary_gists_enabled = true
Expand Down
5 changes: 4 additions & 1 deletion spec/jobs/regular/generate_inferred_concepts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
fab!(:post)
fab!(:concept) { Fabricate(:inferred_concept, name: "programming") }

before { SiteSetting.inferred_concepts_enabled = true }
before do
enable_current_plugin
SiteSetting.inferred_concepts_enabled = true
end

describe "#execute" do
it "does nothing with blank item_ids" do
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/regular/generate_rag_embeddings_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Jobs::GenerateRagEmbeddings do
before { enable_current_plugin }

describe "#execute" do
fab!(:vector_def) { Fabricate(:embedding_definition) }

Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/regular/localize_categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def localize_all_categories(*locales)
end

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/regular/localize_posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:locales) { %w[en ja de] }

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/regular/localize_topics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:locales) { %w[en ja de] }

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/regular/manage_embedding_def_search_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
RSpec.describe Jobs::ManageEmbeddingDefSearchIndex do
fab!(:embedding_definition)

before { enable_current_plugin }

describe "#execute" do
context "when there is no embedding def" do
it "does nothing" do
Expand Down
5 changes: 4 additions & 1 deletion spec/jobs/regular/stream_composer_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
RSpec.describe Jobs::StreamComposerHelper do
subject(:job) { described_class.new }

before { assign_fake_provider_to(:ai_helper_model) }
before do
enable_current_plugin
assign_fake_provider_to(:ai_helper_model)
end

describe "#execute" do
let!(:input) { "I liek to eet pie fur brakefast becuz it is delishus." }
Expand Down
1 change: 1 addition & 0 deletions spec/jobs/regular/stream_discord_reply_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
fab!(:persona) { Fabricate(:ai_persona, default_llm_id: llm_model.id) }

before do
enable_current_plugin
SiteSetting.ai_discord_search_enabled = true
SiteSetting.ai_discord_search_mode = "persona"
SiteSetting.ai_discord_search_persona = persona.id
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/regular/stream_discover_reply_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
RSpec.describe Jobs::StreamDiscoverReply do
subject(:job) { described_class.new }

before { enable_current_plugin }

describe "#execute" do
fab!(:user)
fab!(:llm_model)
Expand Down
5 changes: 4 additions & 1 deletion spec/jobs/regular/stream_post_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
RSpec.describe Jobs::StreamPostHelper do
subject(:job) { described_class.new }

before { assign_fake_provider_to(:ai_helper_model) }
before do
enable_current_plugin
assign_fake_provider_to(:ai_helper_model)
end

describe "#execute" do
fab!(:topic)
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/regular/stream_topic_ai_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
RSpec.describe Jobs::StreamTopicAiSummary do
subject(:job) { described_class.new }

before { enable_current_plugin }

describe "#execute" do
fab!(:topic) { Fabricate(:topic, highest_post_number: 2) }
fab!(:post_1) { Fabricate(:post, topic: topic, post_number: 1) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject(:job) { described_class.new }

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/scheduled/embeddings_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
fab!(:embedding_array) { Array.new(1024) { 1 } }

before do
enable_current_plugin

SiteSetting.ai_embeddings_selected_model = vector_def.id
SiteSetting.ai_embeddings_enabled = true
SiteSetting.ai_embeddings_backfill_batch_size = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
fab!(:post) { Fabricate(:post, like_count: 8, post_number: 2) }

before do
enable_current_plugin
SiteSetting.inferred_concepts_enabled = true
SiteSetting.inferred_concepts_daily_topics_limit = 20
SiteSetting.inferred_concepts_daily_posts_limit = 30
Expand Down
4 changes: 1 addition & 3 deletions spec/jobs/scheduled/post_localization_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

describe Jobs::PostLocalizationBackfill do
before do
enable_current_plugin
SiteSetting.ai_translation_backfill_hourly_rate = 100
SiteSetting.content_localization_supported_locales = "en"
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
SiteSetting.ai_translation_enabled = true
SiteSetting.discourse_ai_enabled = true
end

it "does not enqueue post translation when translator disabled" do
Expand Down Expand Up @@ -36,7 +36,6 @@
end

it "does not enqueue post translation if backfill limit is set to 0" do
SiteSetting.discourse_ai_enabled = true
SiteSetting.ai_translation_enabled = true
SiteSetting.ai_translation_backfill_hourly_rate = 0

Expand All @@ -46,7 +45,6 @@
end

it "enqueues post translation with correct limit" do
SiteSetting.discourse_ai_enabled = true
SiteSetting.ai_translation_enabled = true
SiteSetting.ai_translation_backfill_hourly_rate = 100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject(:job) { described_class.new }

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/scheduled/remove_orphaned_embeddings_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Jobs::RemoveOrphanedEmbeddings do
before { enable_current_plugin }

describe "#execute" do
fab!(:embedding_definition)
fab!(:embedding_definition_2) { Fabricate(:embedding_definition) }
Expand Down
2 changes: 2 additions & 0 deletions spec/jobs/scheduled/sentiment_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "../../support/sentiment_inference_stubs"

RSpec.describe Jobs::SentimentBackfill do
before { enable_current_plugin }

describe "#execute" do
fab!(:post)

Expand Down
1 change: 1 addition & 0 deletions spec/jobs/scheduled/summaries_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
let(:intervals) { 12 } # budget is split into intervals. Job runs every five minutes.

before do
enable_current_plugin
assign_fake_provider_to(:ai_summarization_model)
SiteSetting.ai_summarization_enabled = true
SiteSetting.ai_summary_backfill_maximum_topics_per_hour = limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject(:job) { described_class.new }

before do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
Expand Down
5 changes: 4 additions & 1 deletion spec/jobs/shared_conversation_adjust_upload_security_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
fab!(:claude_2) { Fabricate(:llm_model, name: "claude-2") }

fab!(:bot_user) do
SiteSetting.discourse_ai_enabled = true
enable_current_plugin
toggle_enabled_bots(bots: [claude_2])
SiteSetting.ai_bot_enabled = true
SiteSetting.ai_bot_allowed_groups = "10"
SiteSetting.ai_bot_public_sharing_allowed_groups = "10"
claude_2.reload.user
end

fab!(:user)
fab!(:topic) { Fabricate(:private_message_topic, user: user, recipient: bot_user) }
fab!(:post_1) { Fabricate(:post, topic: topic, user: bot_user) }
Expand All @@ -23,6 +24,8 @@ def run_job
described_class.new.execute(params)
end

before { enable_current_plugin }

context "when conversation is created" do
let(:params) { { conversation_id: conversation.id } }

Expand Down
Loading
Loading