diff --git a/app/models/completion_prompt.rb b/app/models/completion_prompt.rb index 52650d9a..15979a46 100644 --- a/app/models/completion_prompt.rb +++ b/app/models/completion_prompt.rb @@ -17,6 +17,8 @@ class CompletionPrompt < ActiveRecord::Base validates :messages, length: { maximum: 20 } validate :each_message_length + after_commit { DiscourseAi::AiHelper::Assistant.clear_prompt_cache! } + def self.enabled_by_name(name) where(enabled: true).find_by(name: name) end diff --git a/lib/ai_helper/assistant.rb b/lib/ai_helper/assistant.rb index 4fa8efe4..8664709f 100644 --- a/lib/ai_helper/assistant.rb +++ b/lib/ai_helper/assistant.rb @@ -5,10 +5,14 @@ module DiscourseAi class Assistant AI_HELPER_PROMPTS_CACHE_KEY = "ai_helper_prompts" + def self.clear_prompt_cache! + Discourse.cache.delete(AI_HELPER_PROMPTS_CACHE_KEY) + end + def available_prompts Discourse .cache - .fetch(AI_HELPER_PROMPTS_CACHE_KEY) do + .fetch(AI_HELPER_PROMPTS_CACHE_KEY, expires_in: 30.minutes) do prompts = CompletionPrompt.where(enabled: true) # Hide illustrate_post if disabled diff --git a/spec/lib/modules/ai_helper/assistant_spec.rb b/spec/lib/modules/ai_helper/assistant_spec.rb index c7e59c96..08defa10 100644 --- a/spec/lib/modules/ai_helper/assistant_spec.rb +++ b/spec/lib/modules/ai_helper/assistant_spec.rb @@ -15,7 +15,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do describe("#available_prompts") do before do SiteSetting.ai_helper_illustrate_post_model = "disabled" - Discourse.cache.delete(DiscourseAi::AiHelper::Assistant::AI_HELPER_PROMPTS_CACHE_KEY) + DiscourseAi::AiHelper::Assistant.clear_prompt_cache! end it "returns all available prompts" do @@ -35,7 +35,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do context "when illustrate post model is enabled" do before do SiteSetting.ai_helper_illustrate_post_model = "stable_diffusion_xl" - Discourse.cache.delete(DiscourseAi::AiHelper::Assistant::AI_HELPER_PROMPTS_CACHE_KEY) + DiscourseAi::AiHelper::Assistant.clear_prompt_cache! end it "returns the illustrate_post prompt in the list of all prompts" do diff --git a/spec/plugin_spec.rb b/spec/plugin_spec.rb index 4cb0240d..f52d9cfa 100644 --- a/spec/plugin_spec.rb +++ b/spec/plugin_spec.rb @@ -32,6 +32,8 @@ describe Plugin::Instance do SiteSetting.composer_ai_helper_enabled = true SiteSetting.ai_helper_illustrate_post_model = "disabled" Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) + + DiscourseAi::AiHelper::Assistant.clear_prompt_cache! end let(:serializer) { CurrentUserSerializer.new(user, scope: Guardian.new(user)) }