From e3f5e86dc5d1d75d0cd45c9ece385e5198888d48 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Mon, 2 Dec 2024 19:07:05 -0300 Subject: [PATCH] FIX: AI Automation scripts were broken when using seeded models (#991) --- lib/automation.rb | 2 +- .../discourse_automation/automation_spec.rb | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 spec/lib/discourse_automation/automation_spec.rb diff --git a/lib/automation.rb b/lib/automation.rb index 7f741186..d40b2d12 100644 --- a/lib/automation.rb +++ b/lib/automation.rb @@ -22,7 +22,7 @@ module DiscourseAi values .filter do |value_h| value_h["id"] > 0 || - SiteSetting.ai_automation_allowed_seeded_models_map.includes?(value_h["id"].to_s) + SiteSetting.ai_automation_allowed_seeded_models_map.include?(value_h["id"].to_s) end .each { |value_h| value_h["id"] = "custom:#{value_h["id"]}" } diff --git a/spec/lib/discourse_automation/automation_spec.rb b/spec/lib/discourse_automation/automation_spec.rb new file mode 100644 index 00000000..3fbc0898 --- /dev/null +++ b/spec/lib/discourse_automation/automation_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe DiscourseAi::Automation do + describe "manually configured model" do + let!(:llm_model) { Fabricate(:llm_model) } + it "returns a list of available models for automation" do + models = DiscourseAi::Automation.available_models + expect(models).to be_an(Array) + expect(models.first["translated_name"]).to eq(llm_model.display_name) + end + end + + describe "no models" do + it "returns an empty list" do + models = DiscourseAi::Automation.available_models + expect(models).to be_empty + end + end + + describe "seeded models" do + let!(:llm_model) { Fabricate(:seeded_model) } + it "returns an empty list if no seeded models are allowed" do + models = DiscourseAi::Automation.available_models + expect(models).to be_empty + end + + it "returns a list of seeded models if allowed" do + SiteSetting.ai_automation_allowed_seeded_models = llm_model.id.to_s + models = DiscourseAi::Automation.available_models + expect(models.first["translated_name"]).to eq(llm_model.display_name) + end + end + + describe "mixed models" do + let!(:llm_model) { Fabricate(:llm_model) } + let!(:seeded_model) { Fabricate(:seeded_model) } + + it "returns only the manually configured model if seeded is not allowed" do + models = DiscourseAi::Automation.available_models + expect(models.length).to eq(1) + expect(models.first["translated_name"]).to eq(llm_model.display_name) + end + + it "returns a list of seeded and custom models when seeded is allowed" do + SiteSetting.ai_automation_allowed_seeded_models = seeded_model.id.to_s + models = DiscourseAi::Automation.available_models + + expect(models).to match_array( + [ + { "translated_name" => "#{llm_model.display_name}", "id" => "custom:#{llm_model.id}" }, + { + "translated_name" => "#{seeded_model.display_name}", + "id" => "custom:#{seeded_model.id}", + }, + ], + ) + end + end +end