A platform for community discussion. Free, open, simple.
DEV: Remove AI Auto image caption feature (#33794)
Keegan George321f790f338c
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/composer-after-save-or-cancel/ai-image-caption-loader.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/composer-after-save-or-cancel/ai-image-caption-loader.gjsdeleted file mode 100644index 0e1b6e60f4d..00000000000--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/composer-after-save-or-cancel/ai-image-caption-loader.gjs+++ /dev/null@@ -1,19 +0,0 @@-import Component from "@glimmer/component";-import { service } from "@ember/service";-import loadingSpinner from "discourse/helpers/loading-spinner";-import { i18n } from "discourse-i18n";--export default class AiImageCaptionLoader extends Component {- @service imageCaptionPopup;-- <template>- {{#if this.imageCaptionPopup.showAutoCaptionLoader}}- <div class="auto-image-caption-loader">- {{loadingSpinner size="small"}}- <span>{{i18n- "discourse_ai.ai_helper.image_caption.automatic_caption_loading"- }}</span>- </div>- {{/if}}- </template>-}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjsindex c2db7a89a3c..ebfaa1a4c1a 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjs@@ -3,7 +3,7 @@ import { LinkTo } from "@ember/routing";import icon from "discourse/helpers/d-icon";import { i18n } from "discourse-i18n";-export default class AutoImageCaptionSetting extends Component {+export default class AiPreferences extends Component {static shouldRender(args, context) {return context.siteSettings.discourse_ai_enabled;}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/controllers/preferences-ai.js b/plugins/discourse-ai/assets/javascripts/discourse/controllers/preferences-ai.jsindex adab3654ca6..f13328bbdfa 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/controllers/preferences-ai.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/controllers/preferences-ai.js@@ -12,22 +12,6 @@ export default class PreferencesAiController extends Controller {get booleanSettings() {return [- {- key: "auto_image_caption",- label: "discourse_ai.ai_helper.image_caption.automatic_caption_setting",- settingName: "auto-image-caption",- checked: this.model.user_option.auto_image_caption,- isIncluded: (() => {- const aiHelperEnabledFeatures =- this.siteSettings.ai_helper_enabled_features.split("|");-- return (- this.model?.user_allowed_ai_auto_image_captions &&- aiHelperEnabledFeatures.includes("image_caption") &&- this.siteSettings.ai_helper_enabled- );- })(),- },{key: "ai_search_discoveries",label: "discourse_ai.discobot_discoveries.user_setting",diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.jsindex 9d31a86357c..d55d9049857 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.js@@ -1,14 +1,9 @@import { ajax } from "discourse/lib/ajax";-import { extractError, popupAjaxError } from "discourse/lib/ajax-error";+import { popupAjaxError } from "discourse/lib/ajax-error";import { apiInitializer } from "discourse/lib/api";-import {- getUploadMarkdown,- IMAGE_MARKDOWN_REGEX,- isImage,-} from "discourse/lib/uploads";import { i18n } from "discourse-i18n";-export default apiInitializer("1.25.0", (api) => {+export default apiInitializer((api) => {const buttonAttrs = {label: i18n("discourse_ai.ai_helper.image_caption.button_label"),icon: "discourse-sparkles",@@ -24,8 +19,6 @@ export default apiInitializer("1.25.0", (api) => {return;}- api.addSaveableUserOptionField("auto_image_caption");-api.addComposerImageWrapperButton(buttonAttrs.label,buttonAttrs.class,@@ -86,164 +79,4 @@ export default apiInitializer("1.25.0", (api) => {}});-- // Checks if image is small (≤ 0.1 MP)- function isSmallImage(width, height) {- const megapixels = (width * height) / 1000000;- return megapixels <= 0.1;- }-- function needsImprovedCaption(caption) {- return caption.length < 20 || caption.split(" ").length === 1;- }-- function getUploadUrlFromMarkdown(markdown) {- const regex = /\(upload:\/\/([^)]+)\)/;- const match = markdown.match(regex);- return match ? `upload://${match[1]}` : null;- }-- async function fetchImageCaption(imageUrl, urlType) {- try {- const response = await ajax(`/discourse-ai/ai-helper/caption_image`, {- method: "POST",- data: {- image_url: imageUrl,- image_url_type: urlType,- },- });- return response.caption;- } catch (error) {- toasts.error({- class: "ai-image-caption-error-toast",- duration: "short",- data: {- message: extractError(error),- },- });- }- }-- const autoCaptionAllowedGroups =- settings?.ai_auto_image_caption_allowed_groups- .split("|")- .map((id) => parseInt(id, 10));- const currentUserGroups = currentUser.groups.map((g) => g.id);-- if (- !currentUserGroups.some((groupId) =>- autoCaptionAllowedGroups.includes(groupId)- )- ) {- return;- }-- const toasts = api.container.lookup("service:toasts");- // Automatically caption uploaded images- api.addComposerUploadMarkdownResolver(async (upload) => {- const autoCaptionEnabled = currentUser.get(- "user_option.auto_image_caption"- );-- if (- !autoCaptionEnabled ||- !isImage(upload.url) ||- !needsImprovedCaption(upload.original_filename) ||- isSmallImage(upload.width, upload.height)- ) {- return getUploadMarkdown(upload);- }-- const caption = await fetchImageCaption(upload.url, "long_url");- if (!caption) {- return getUploadMarkdown(upload);- }- return ``;- });-- // Conditionally show dialog to auto image caption- api.composerBeforeSave(() => {- return new Promise((resolve, reject) => {- const dialog = api.container.lookup("service:dialog");- const composer = api.container.lookup("service:composer");- const localePrefix =- "discourse_ai.ai_helper.image_caption.automatic_caption_dialog";- const autoCaptionEnabled = currentUser.get(- "user_option.auto_image_caption"- );-- const imageUploads = composer.model.reply.match(IMAGE_MARKDOWN_REGEX);- const hasImageUploads = imageUploads?.length > 0;-- if (!hasImageUploads) {- resolve();- }-- const imagesToCaption = imageUploads.filter((image) => {- const caption = image- .substring(image.indexOf("[") + 1, image.indexOf("]"))- .split("|")[0];- // We don't check if the image is small to show the prompt here- // because the width/height are the thumbnail sizes so the mp count- // is incorrect. It doesn't matter because the auto caption won't- // happen anyways if its small because that uses the actual upload dimensions- return needsImprovedCaption(caption);- });-- const needsBetterCaptions = imagesToCaption?.length > 0;-- const keyValueStore = api.container.lookup("service:key-value-store");- const imageCaptionPopup = api.container.lookup(- "service:imageCaptionPopup"- );- const autoCaptionPromptKey = "ai-auto-caption-seen";- const seenAutoCaptionPrompt = keyValueStore.getItem(autoCaptionPromptKey);-- if (autoCaptionEnabled || !needsBetterCaptions || seenAutoCaptionPrompt) {- return resolve();- }-- keyValueStore.setItem(autoCaptionPromptKey, true);-- dialog.confirm({- message: i18n(`${localePrefix}.prompt`),- confirmButtonLabel: `${localePrefix}.confirm`,- cancelButtonLabel: `${localePrefix}.cancel`,- class: "ai-image-caption-prompt-dialog",-- didConfirm: async () => {- try {- currentUser.set("user_option.auto_image_caption", true);- await currentUser.save(["auto_image_caption"]);-- imagesToCaption.forEach(async (imageMarkdown) => {- const uploadUrl = getUploadUrlFromMarkdown(imageMarkdown);- imageCaptionPopup.showAutoCaptionLoader = true;- const caption = await fetchImageCaption(uploadUrl, "short_url");-- // Find and replace the caption in the reply- const regex = new RegExp(- `(!\\[)[^|]+(\\|[^\\]]+\\]\\(${uploadUrl}\\))`- );- const newReply = composer.model.reply.replace(- regex,- `$1${caption}$2`- );- composer.model.set("reply", newReply);- imageCaptionPopup.showAutoCaptionLoader = false;- resolve();- });- } catch (error) {- // Reject the promise if an error occurs- // Show an error saying unable to generate captions- reject(error);- }- },- didCancel: () => {- // Don't enable auto captions and continue with the save- resolve();- },- });- });- });});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/image-caption-popup.js b/plugins/discourse-ai/assets/javascripts/discourse/services/image-caption-popup.jsindex 48164070c24..8435131652f 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/services/image-caption-popup.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/image-caption-popup.js@@ -12,7 +12,6 @@ export default class ImageCaptionPopup extends Service {@tracked newCaption = null;@tracked loading = false;@tracked popupTrigger = null;- @tracked showAutoCaptionLoader = false;@tracked _request = null;updateCaption() {diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scssindex 56e22457193..d342aa643c0 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scss@@ -672,14 +672,6 @@}}-.auto-image-caption-loader {- margin-left: 2rem;- display: flex;- align-items: center;- gap: 0.5rem;- color: var(--primary-high);-}-// AI Helper Options List.ai-helper-options {margin: 0;diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex c713804f9e8..6166d661722 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -718,12 +718,6 @@ en:generating: "Generating caption..."credits: "Captioned by AI"save_caption: "Save"- automatic_caption_setting: "Enable auto caption"- automatic_caption_loading: "Captioning images..."- automatic_caption_dialog:- prompt: "This post contains non-captioned images. Would you like to enable automatic captions on image uploads? (This can be changed in your preferences later)"- confirm: "Enable"- cancel: "Don't ask again"no_content_error: "Add content first to perform AI actions on it"reviewables:diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 5a8696590dd..fbdf373c860 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -73,7 +73,6 @@ en:ai_helper_enabled_features: "Select the features to enable in the AI helper."post_ai_helper_allowed_groups: "User groups allowed to access AI Helper features in posts"ai_helper_image_caption_model: "Select the model to use for generating image captions"- ai_auto_image_caption_allowed_groups: "Users on these groups can toggle automatic image captioning."ai_embeddings_selected_model: "Use the selected model for generating embeddings."ai_embeddings_generate_for_pms: "Generate embeddings for personal messages."diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.ymlindex 43646ff6e7f..f4cbd3085e3 100644--- a/plugins/discourse-ai/config/settings.yml+++ b/plugins/discourse-ai/config/settings.yml@@ -154,14 +154,6 @@ discourse_ai:type: enumenum: "DiscourseAi::Configuration::LlmVisionEnumerator"hidden: true- ai_auto_image_caption_allowed_groups:- client: true- type: group_list- list_type: compact- default: "10" # 10: @trust_level_0- allow_any: false- refresh: true- area: "ai-features/ai_helper"ai_helper_model_allowed_seeded_models:default: ""hidden: truediff --git a/plugins/discourse-ai/lib/ai_helper/entry_point.rb b/plugins/discourse-ai/lib/ai_helper/entry_point.rbindex cef7e2a4965..e7b85f88763 100644--- a/plugins/discourse-ai/lib/ai_helper/entry_point.rb+++ b/plugins/discourse-ai/lib/ai_helper/entry_point.rb@@ -48,31 +48,6 @@ module DiscourseAiroot: false,)end-- plugin.add_to_serializer(:current_user, :user_allowed_ai_auto_image_captions) do- scope.user.in_any_groups?(SiteSetting.ai_auto_image_caption_allowed_groups_map)- end-- UserUpdater::OPTION_ATTR.push(:auto_image_caption)- plugin.add_to_serializer(- :user_option,- :auto_image_caption,- include_condition: -> do- 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,- ) { object.auto_image_caption }-- plugin.add_to_serializer(- :current_user_option,- :auto_image_caption,- include_condition: -> do- 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,- ) { object.auto_image_caption }endendenddiff --git a/plugins/discourse-ai/spec/lib/modules/ai_helper/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_helper/entry_point_spec.rbindex 9ffb5a5d4cd..5c525b2a27e 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_helper/entry_point_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_helper/entry_point_spec.rb@@ -38,15 +38,4 @@ describe DiscourseAi::AiHelper::EntryPoint do)endend-- 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.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))-- expect(serializer.user_option.auto_image_caption).to eq(false)- endenddiff --git a/plugins/discourse-ai/spec/models/user_option_spec.rb b/plugins/discourse-ai/spec/models/user_option_spec.rbindex ab1c966d7ae..943124bf163 100644--- a/plugins/discourse-ai/spec/models/user_option_spec.rb+++ b/plugins/discourse-ai/spec/models/user_option_spec.rb@@ -10,22 +10,9 @@ RSpec.describe UserOption dobefore doenable_current_plugin-- assign_fake_provider_to(:ai_helper_model)- assign_fake_provider_to(:ai_helper_image_caption_model)- SiteSetting.ai_helper_enabled = true- SiteSetting.ai_helper_enabled_features = "image_caption"- SiteSetting.ai_auto_image_caption_allowed_groups = "10" # tl0-SiteSetting.ai_bot_enabled = trueend- describe "#auto_image_caption" do- it "is present" do- expect(described_class.new.auto_image_caption).to eq(false)- end- end-describe "#ai_search_discoveries" dobefore doSiteSetting.ai_bot_discover_persona = ai_persona.iddiff --git a/plugins/discourse-ai/spec/system/ai_helper/ai_image_caption_spec.rb b/plugins/discourse-ai/spec/system/ai_helper/ai_image_caption_spec.rbindex 2387dc49679..2ed0ed12e27 100644--- a/plugins/discourse-ai/spec/system/ai_helper/ai_image_caption_spec.rb+++ b/plugins/discourse-ai/spec/system/ai_helper/ai_image_caption_spec.rb@@ -90,72 +90,4 @@ RSpec.describe "AI image caption", type: :system doendendend-- describe "automatic image captioning" do- context "when the user preference is disabled" do- before { user.user_option.update!(auto_image_caption: false) }-- it "should show a prompt when submitting a post with captionable images uploaded" do- visit("/latest")- page.find("#create-topic").click- attach_file("file-uploader", file_path, make_visible: true)- wait_for { composer.has_no_in_progress_uploads? }- composer.fill_title("I love using Discourse! It is my favorite forum software")- composer.create- expect(dialog).to be_open- end-- it "should not show a prompt when submitting a post with no captionable images uploaded" do- visit("/latest")- page.find("#create-topic").click- attach_file("file-uploader", captioned_image_path, make_visible: true)- wait_for { composer.has_no_in_progress_uploads? }- composer.fill_title("I love using Discourse! It is my favorite forum software")- composer.create- expect(dialog).to be_closed- end-- it "should auto caption the existing images and update the preference when dialog is accepted" do- DiscourseAi::Completions::Llm.with_prepared_responses([caption]) do- visit("/latest")- page.find("#create-topic").click- attach_file("file-uploader", file_path, make_visible: true)- wait_for { composer.has_no_in_progress_uploads? }- composer.fill_title("I love using Discourse! It is my favorite forum software")- composer.create- dialog.click_yes- wait_for(timeout: 100) { page.find("#post_1 .cooked img")["alt"] == caption_with_attrs }- expect(page.find("#post_1 .cooked img")["alt"]).to eq(caption_with_attrs)- end- end- end-- context "when a post has no uploads" do- before { user.user_option.update!(auto_image_caption: true) }-- it "should create the topic without triggering auto caption" do- title = "I love using Discourse! It is my favorite forum software"- visit("/latest")- page.find("#create-topic").click- composer.fill_title(title)- composer.fill_content(- "Culpa labore velit cupidatat commodo magna esse et minim consequat veniam dolore eiusmod. Labore eu elit in nulla ipsum elit consectetur sunt consectetur enim. Cillum ex ex velit eiusmod labore ullamco ut ad. Dolore dolor commodo nisi fugiat esse quis anim officia quis. Pariatur cillum pariatur irure cupidatat nostrud ullamco labore id aliqua ut nostrud. Eu adipisicing ut laboris.",- )- composer.create- expect(dialog).to be_closed- expect(topic_page).to have_topic_title(title)- end-- it "should create a reply to a post without triggering auto caption" do- visit("/t/-/#{topic.id}")- topic_page.click_footer_reply- composer.fill_content(- "Culpa labore velit cupidatat commodo magna esse et minim consequat veniam dolore eiusmod. Labore eu elit in nulla ipsum elit consectetur sunt consectetur enim. Cillum ex ex velit eiusmod labore ullamco ut ad. Dolore dolor commodo nisi fugiat esse quis anim officia quis. Pariatur cillum pariatur irure cupidatat nostrud ullamco labore id aliqua ut nostrud. Eu adipisicing ut laboris.",- )- composer.create- expect(dialog).to be_closed- expect(topic_page).to have_post_number(post.post_number)- end- end- endenddiff --git a/plugins/discourse-ai/spec/system/ai_user_preferences_spec.rb b/plugins/discourse-ai/spec/system/ai_user_preferences_spec.rbindex 8c74664238f..4285a5cf49d 100644--- a/plugins/discourse-ai/spec/system/ai_user_preferences_spec.rb+++ b/plugins/discourse-ai/spec/system/ai_user_preferences_spec.rb@@ -17,33 +17,6 @@ RSpec.describe "User AI preferences", type: :system dosign_in(user)end- describe "automatic image caption setting" do- context "when ai helper is disabled" do- before { SiteSetting.ai_helper_enabled = false }-- it "should not have the setting present in the user preferences page" do- user_preferences_ai_page.visit(user)- expect(user_preferences_ai_page).to have_no_ai_preference("pref-auto-image-caption")- end- end-- context "when toggling the setting from the user preferences page" do- before do- SiteSetting.ai_helper_enabled = true- SiteSetting.ai_helper_enabled_features = "image_caption"- user.user_option.update!(auto_image_caption: false)- end-- it "should update the preference to enabled" do- user_preferences_ai_page.visit(user)- user_preferences_ai_page.toggle_setting("pref-auto-image-caption")- user_preferences_ai_page.save_changes- wait_for(timeout: 5) { user.reload.user_option.auto_image_caption }- expect(user.reload.user_option.auto_image_caption).to eq(true)- end- end- end-describe "search discoveries setting" docontext "when discoveries are enabled" dobefore { SiteSetting.ai_bot_enabled = true }@@ -72,7 +45,6 @@ RSpec.describe "User AI preferences", type: :system doit "should not have any AI preferences and should show a message" douser_preferences_ai_page.visit(user)- expect(user_preferences_ai_page).to have_no_ai_preference("pref-auto-image-caption")expect(user_preferences_ai_page).to have_no_ai_preference("pref-ai-search-discoveries")expect(page).to have_content(I18n.t("js.discourse_ai.user_preferences.empty"))end