From f72ab127612e629152cced081ba0fcb24c871682 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 12 Aug 2024 15:40:23 -0700 Subject: [PATCH] DEV: Clearly separate post/composer helper settings (#747) --- .../ai_helper/assistant_controller.rb | 10 +++++--- .../discourse/lib/show-ai-helper.js | 4 +--- config/locales/server.en.yml | 4 ++-- config/settings.yml | 18 +++++++-------- ...162837_rename_ai_helper_enabled_setting.rb | 11 +++++++++ ...rename_ai_helper_allowed_groups_setting.rb | 11 +++++++++ lib/ai_bot/entry_point.rb | 12 ---------- lib/ai_helper/entry_point.rb | 23 +++++++++++++------ lib/configuration/llm_validator.rb | 2 +- spec/jobs/regular/stream_post_helper_spec.rb | 2 +- .../lib/modules/ai_helper/entry_point_spec.rb | 6 ++--- spec/models/user_option_spec.rb | 2 +- spec/plugin_spec.rb | 2 +- .../ai_helper/assistant_controller_spec.rb | 10 ++++---- .../ai_helper/ai_composer_helper_spec.rb | 6 ++--- .../system/ai_helper/ai_image_caption_spec.rb | 2 +- spec/system/ai_helper/ai_post_helper_spec.rb | 6 ++--- .../ai_split_topic_suggestion_spec.rb | 2 +- 18 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 db/post_migrate/20240809162837_rename_ai_helper_enabled_setting.rb create mode 100644 db/post_migrate/20240809163303_rename_ai_helper_allowed_groups_setting.rb diff --git a/app/controllers/discourse_ai/ai_helper/assistant_controller.rb b/app/controllers/discourse_ai/ai_helper/assistant_controller.rb index 82bc25cd..3594affe 100644 --- a/app/controllers/discourse_ai/ai_helper/assistant_controller.rb +++ b/app/controllers/discourse_ai/ai_helper/assistant_controller.rb @@ -165,9 +165,13 @@ module DiscourseAi end def ensure_can_request_suggestions - if !current_user.in_any_groups?(SiteSetting.ai_helper_allowed_groups_map) - raise Discourse::InvalidAccess - end + allowed_groups = + ( + SiteSetting.composer_ai_helper_allowed_groups_map | + SiteSetting.post_ai_helper_allowed_groups_map + ) + + raise Discourse::InvalidAccess if !current_user.in_any_groups?(allowed_groups) end end end diff --git a/assets/javascripts/discourse/lib/show-ai-helper.js b/assets/javascripts/discourse/lib/show-ai-helper.js index d0c7563b..d068842e 100644 --- a/assets/javascripts/discourse/lib/show-ai-helper.js +++ b/assets/javascripts/discourse/lib/show-ai-helper.js @@ -20,7 +20,5 @@ export function showPostAIHelper(outletArgs, helper) { } function _helperEnabled(siteSettings) { - return ( - siteSettings.discourse_ai_enabled && siteSettings.composer_ai_helper_enabled - ); + return siteSettings.discourse_ai_enabled && siteSettings.ai_helper_enabled; } diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e06a963b..70b20326 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -46,8 +46,8 @@ en: ai_hugging_face_tei_endpoint: URL where the API is running for the Hugging Face text embeddings inference ai_hugging_face_tei_api_key: API key for Hugging Face text embeddings inference - composer_ai_helper_enabled: "Enable the Composer's AI helper." - ai_helper_allowed_groups: "Users on these groups will see the AI helper button in the composer." + ai_helper_enabled: "Enable the AI helper." + composer_ai_helper_allowed_groups: "Users on these groups will see the AI helper button in the composer." ai_helper_allowed_in_pm: "Enable the composer's AI helper in PMs." ai_helper_model: "Model to use for the AI helper." ai_helper_custom_prompts_allowed_groups: "Users on these groups will see the custom prompt option in the AI helper." diff --git a/config/settings.yml b/config/settings.yml index b4a3e6ce..42476330 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -96,10 +96,10 @@ discourse_ai: - opennsfw2 - nsfw_detector - ai_openai_gpt35_url: + ai_openai_gpt35_url: default: "https://api.openai.com/v1/chat/completions" hidden: true - ai_openai_gpt35_16k_url: + ai_openai_gpt35_16k_url: default: "https://api.openai.com/v1/chat/completions" hidden: true ai_openai_gpt4o_url: @@ -108,15 +108,15 @@ discourse_ai: ai_openai_gpt4_url: default: "https://api.openai.com/v1/chat/completions" hidden: true - ai_openai_gpt4_32k_url: + ai_openai_gpt4_32k_url: default: "https://api.openai.com/v1/chat/completions" hidden: true - ai_openai_gpt4_turbo_url: + ai_openai_gpt4_turbo_url: default: "https://api.openai.com/v1/chat/completions" hidden: true ai_openai_dall_e_3_url: "https://api.openai.com/v1/images/generations" ai_openai_embeddings_url: "https://api.openai.com/v1/embeddings" - ai_openai_organization: + ai_openai_organization: default: "" hidden: true ai_openai_api_key: @@ -206,7 +206,7 @@ discourse_ai: ai_vllm_endpoint_srv: default: "" hidden: true - ai_vllm_api_key: + ai_vllm_api_key: default: "" hidden: true ai_llava_endpoint: @@ -215,7 +215,7 @@ discourse_ai: ai_llava_endpoint_srv: default: "" hidden: true - ai_llava_api_key: + ai_llava_api_key: default: "" hidden: true ai_strict_token_counting: @@ -225,11 +225,11 @@ discourse_ai: hidden: true default: "" - composer_ai_helper_enabled: + ai_helper_enabled: default: false client: true validator: "DiscourseAi::Configuration::LlmDependencyValidator" - ai_helper_allowed_groups: + composer_ai_helper_allowed_groups: type: group_list list_type: compact default: "3|14" # 3: @staff, 14: @trust_level_4 diff --git a/db/post_migrate/20240809162837_rename_ai_helper_enabled_setting.rb b/db/post_migrate/20240809162837_rename_ai_helper_enabled_setting.rb new file mode 100644 index 00000000..59dadb96 --- /dev/null +++ b/db/post_migrate/20240809162837_rename_ai_helper_enabled_setting.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class RenameAiHelperEnabledSetting < ActiveRecord::Migration[7.1] + def up + execute "UPDATE site_settings SET name = 'ai_helper_enabled' WHERE name = 'composer_ai_helper_enabled'" + end + + def down + execute "UPDATE site_settings SET name = 'composer_ai_helper_enabled' WHERE name = 'ai_helper_enabled'" + end +end diff --git a/db/post_migrate/20240809163303_rename_ai_helper_allowed_groups_setting.rb b/db/post_migrate/20240809163303_rename_ai_helper_allowed_groups_setting.rb new file mode 100644 index 00000000..dc7ae56a --- /dev/null +++ b/db/post_migrate/20240809163303_rename_ai_helper_allowed_groups_setting.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class RenameAiHelperAllowedGroupsSetting < ActiveRecord::Migration[7.1] + def up + execute "UPDATE site_settings SET name = 'composer_ai_helper_allowed_groups' WHERE name = 'ai_helper_allowed_groups'" + end + + def down + execute "UPDATE site_settings SET name = 'ai_helper_allowed_groups' WHERE name = 'composer_ai_helper_allowed_groups'" + end +end diff --git a/lib/ai_bot/entry_point.rb b/lib/ai_bot/entry_point.rb index 79020425..8c214631 100644 --- a/lib/ai_bot/entry_point.rb +++ b/lib/ai_bot/entry_point.rb @@ -151,18 +151,6 @@ module DiscourseAi bots_map end - plugin.add_to_serializer(:current_user, :can_use_assistant) do - scope.user.in_any_groups?(SiteSetting.ai_helper_allowed_groups_map) - end - - plugin.add_to_serializer(:current_user, :can_use_assistant_in_post) do - scope.user.in_any_groups?(SiteSetting.post_ai_helper_allowed_groups_map) - end - - plugin.add_to_serializer(:current_user, :can_use_custom_prompts) do - scope.user.in_any_groups?(SiteSetting.ai_helper_custom_prompts_allowed_groups_map) - end - plugin.add_to_serializer(:current_user, :can_share_ai_bot_conversations) do scope.user.in_any_groups?(SiteSetting.ai_bot_public_sharing_allowed_groups_map) end diff --git a/lib/ai_helper/entry_point.rb b/lib/ai_helper/entry_point.rb index f6085b2a..31a854f6 100644 --- a/lib/ai_helper/entry_point.rb +++ b/lib/ai_helper/entry_point.rb @@ -10,8 +10,20 @@ module DiscourseAi additional_icons = %w[spell-check language images] additional_icons.each { |icon| plugin.register_svg_icon(icon) } + plugin.add_to_serializer(:current_user, :can_use_assistant) do + scope.user.in_any_groups?(SiteSetting.composer_ai_helper_allowed_groups_map) + end + + plugin.add_to_serializer(:current_user, :can_use_assistant_in_post) do + scope.user.in_any_groups?(SiteSetting.post_ai_helper_allowed_groups_map) + end + + plugin.add_to_serializer(:current_user, :can_use_custom_prompts) do + scope.user.in_any_groups?(SiteSetting.ai_helper_custom_prompts_allowed_groups_map) + end + plugin.on(:chat_message_created) do |message, channel, user, extra| - next unless SiteSetting.composer_ai_helper_enabled + next unless SiteSetting.ai_helper_enabled next unless SiteSetting.ai_helper_automatic_chat_thread_title next if extra[:thread].blank? next if extra[:thread].title.present? @@ -32,10 +44,7 @@ module DiscourseAi plugin.add_to_serializer( :current_user, :ai_helper_prompts, - include_condition: -> do - SiteSetting.composer_ai_helper_enabled && scope.authenticated? && - scope.user.in_any_groups?(SiteSetting.ai_helper_allowed_groups_map) - end, + include_condition: -> { SiteSetting.ai_helper_enabled && scope.authenticated? }, ) do ActiveModel::ArraySerializer.new( DiscourseAi::AiHelper::Assistant.new.available_prompts(scope.user), @@ -52,7 +61,7 @@ module DiscourseAi :user_option, :auto_image_caption, include_condition: -> do - SiteSetting.composer_ai_helper_enabled && + SiteSetting.ai_helper_enabled && SiteSetting.ai_helper_enabled_features.include?("image_caption") && scope.user.in_any_groups?(SiteSetting.ai_auto_image_caption_allowed_groups_map) end, @@ -62,7 +71,7 @@ module DiscourseAi :current_user_option, :auto_image_caption, include_condition: -> do - SiteSetting.composer_ai_helper_enabled && + SiteSetting.ai_helper_enabled && SiteSetting.ai_helper_enabled_features.include?("image_caption") && scope.user.in_any_groups?(SiteSetting.ai_auto_image_caption_allowed_groups_map) end, diff --git a/lib/configuration/llm_validator.rb b/lib/configuration/llm_validator.rb index fea0954a..10dee918 100644 --- a/lib/configuration/llm_validator.rb +++ b/lib/configuration/llm_validator.rb @@ -57,7 +57,7 @@ module DiscourseAi def modules_and_choose_llm_settings { ai_embeddings_semantic_search_enabled: :ai_embeddings_semantic_search_hyde_model, - composer_ai_helper_enabled: :ai_helper_model, + ai_helper_enabled: :ai_helper_model, ai_summarization_enabled: :ai_summarization_model, } end diff --git a/spec/jobs/regular/stream_post_helper_spec.rb b/spec/jobs/regular/stream_post_helper_spec.rb index 111220e2..9e811711 100644 --- a/spec/jobs/regular/stream_post_helper_spec.rb +++ b/spec/jobs/regular/stream_post_helper_spec.rb @@ -19,7 +19,7 @@ RSpec.describe Jobs::StreamPostHelper do before do Group.find(Group::AUTO_GROUPS[:trust_level_3]).add(user) - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true end describe "validates params" do diff --git a/spec/lib/modules/ai_helper/entry_point_spec.rb b/spec/lib/modules/ai_helper/entry_point_spec.rb index 1eb255c3..33765deb 100644 --- a/spec/lib/modules/ai_helper/entry_point_spec.rb +++ b/spec/lib/modules/ai_helper/entry_point_spec.rb @@ -8,8 +8,8 @@ describe DiscourseAi::AiHelper::EntryPoint do assign_fake_provider_to(:ai_helper_model) SiteSetting.default_locale = "en" SiteSetting.allow_user_locale = true - SiteSetting.composer_ai_helper_enabled = true - SiteSetting.ai_helper_allowed_groups = "10" # tl0 + SiteSetting.ai_helper_enabled = true + SiteSetting.composer_ai_helper_allowed_groups = "10" # tl0 DiscourseAi::AiHelper::Assistant.clear_prompt_cache! Group.refresh_automatic_groups! @@ -40,7 +40,7 @@ describe DiscourseAi::AiHelper::EntryPoint do it "will include auto_image_caption field in the user_option if image caption is enabled" do assign_fake_provider_to(:ai_helper_model) assign_fake_provider_to(:ai_helper_image_caption_model) - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true SiteSetting.ai_helper_enabled_features = "image_caption" SiteSetting.ai_auto_image_caption_allowed_groups = "10" # tl0 serializer = CurrentUserSerializer.new(english_user, scope: Guardian.new(english_user)) diff --git a/spec/models/user_option_spec.rb b/spec/models/user_option_spec.rb index dd36fef9..54c58b8f 100644 --- a/spec/models/user_option_spec.rb +++ b/spec/models/user_option_spec.rb @@ -4,7 +4,7 @@ RSpec.describe UserOption do before do assign_fake_provider_to(:ai_helper_model) assign_fake_provider_to(:ai_helper_image_caption_model) - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true SiteSetting.ai_helper_enabled_features = "image_caption" SiteSetting.ai_auto_image_caption_allowed_groups = "10" # tl0 end diff --git a/spec/plugin_spec.rb b/spec/plugin_spec.rb index 5fb3d344..fa01219e 100644 --- a/spec/plugin_spec.rb +++ b/spec/plugin_spec.rb @@ -29,7 +29,7 @@ describe Plugin::Instance do before do assign_fake_provider_to(:ai_helper_model) - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true SiteSetting.ai_helper_illustrate_post_model = "disabled" Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) diff --git a/spec/requests/ai_helper/assistant_controller_spec.rb b/spec/requests/ai_helper/assistant_controller_spec.rb index d6c00bb6..9df109fb 100644 --- a/spec/requests/ai_helper/assistant_controller_spec.rb +++ b/spec/requests/ai_helper/assistant_controller_spec.rb @@ -21,7 +21,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do before do sign_in(user) - SiteSetting.ai_helper_allowed_groups = Group::AUTO_GROUPS[:staff] + SiteSetting.composer_ai_helper_allowed_groups = Group::AUTO_GROUPS[:staff] end it "returns a 403 response" do @@ -37,7 +37,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do before do sign_in(user) user.group_ids = [Group::AUTO_GROUPS[:trust_level_1]] - SiteSetting.ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1] + SiteSetting.composer_ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1] end it "returns a 400 if the helper mode is invalid" do @@ -128,7 +128,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do before do sign_in(user) - SiteSetting.ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1] + SiteSetting.composer_ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1] end it "returns the suggested caption for the image" do @@ -208,9 +208,9 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do stub_s3_store assign_fake_provider_to(:ai_helper_image_caption_model) SiteSetting.secure_uploads = true - SiteSetting.ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1] + SiteSetting.composer_ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1] - Group.find(SiteSetting.ai_helper_allowed_groups_map.first).add(user) + Group.find(SiteSetting.composer_ai_helper_allowed_groups_map.first).add(user) user.reload stub_request( diff --git a/spec/system/ai_helper/ai_composer_helper_spec.rb b/spec/system/ai_helper/ai_composer_helper_spec.rb index 2c5d79ac..fa5695a2 100644 --- a/spec/system/ai_helper/ai_composer_helper_spec.rb +++ b/spec/system/ai_helper/ai_composer_helper_spec.rb @@ -7,7 +7,7 @@ RSpec.describe "AI Composer helper", type: :system, js: true do before do Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) assign_fake_provider_to(:ai_helper_model) - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true sign_in(user) end @@ -383,7 +383,7 @@ RSpec.describe "AI Composer helper", type: :system, js: true do context "when AI helper is disabled" do let(:mode) { CompletionPrompt::GENERATE_TITLES } - before { SiteSetting.composer_ai_helper_enabled = false } + before { SiteSetting.ai_helper_enabled = false } it "does not trigger AI context menu" do trigger_context_menu(input) @@ -400,7 +400,7 @@ RSpec.describe "AI Composer helper", type: :system, js: true do context "when user is not a member of AI helper allowed group" do let(:mode) { CompletionPrompt::GENERATE_TITLES } - before { SiteSetting.ai_helper_allowed_groups = non_member_group.id.to_s } + before { SiteSetting.composer_ai_helper_allowed_groups = non_member_group.id.to_s } it "does not trigger AI context menu" do trigger_context_menu(input) diff --git a/spec/system/ai_helper/ai_image_caption_spec.rb b/spec/system/ai_helper/ai_image_caption_spec.rb index 82dad656..821666d6 100644 --- a/spec/system/ai_helper/ai_image_caption_spec.rb +++ b/spec/system/ai_helper/ai_image_caption_spec.rb @@ -37,7 +37,7 @@ RSpec.describe "AI image caption", type: :system, js: true do end context "when not a member of ai helper group" do - before { SiteSetting.ai_helper_allowed_groups = non_member_group.id.to_s } + before { SiteSetting.composer_ai_helper_allowed_groups = non_member_group.id.to_s } include_examples "shows no image caption button" end diff --git a/spec/system/ai_helper/ai_post_helper_spec.rb b/spec/system/ai_helper/ai_post_helper_spec.rb index b70cc96d..e3ef4656 100644 --- a/spec/system/ai_helper/ai_post_helper_spec.rb +++ b/spec/system/ai_helper/ai_post_helper_spec.rb @@ -29,7 +29,7 @@ RSpec.describe "AI Post helper", type: :system, js: true do before do Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) assign_fake_provider_to(:ai_helper_model) - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true sign_in(user) end @@ -170,7 +170,7 @@ RSpec.describe "AI Post helper", type: :system, js: true do end context "when AI helper is disabled" do - before { SiteSetting.composer_ai_helper_enabled = false } + before { SiteSetting.ai_helper_enabled = false } it "does not show the Ask AI button in the post selection toolbar" do select_post_text(post) @@ -181,7 +181,7 @@ RSpec.describe "AI Post helper", type: :system, js: true do context "when user is not a member of the post AI helper allowed group" do before do - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true SiteSetting.post_ai_helper_allowed_groups = non_member_group.id.to_s end diff --git a/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb b/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb index e9ace7d3..150bf0df 100644 --- a/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb +++ b/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb @@ -39,7 +39,7 @@ RSpec.describe "AI Post helper", type: :system, js: true do before do Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) assign_fake_provider_to(:ai_helper_model) - SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled = true sign_in(user) end