Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Automatic AI captions (#41586)

Natalie Tay9f6fa67949ed
diff --git a/app/assets/stylesheets/common/base/lightbox.scss b/app/assets/stylesheets/common/base/lightbox.scss
index 54f635ce480..2526afcd40f 100644
--- a/app/assets/stylesheets/common/base/lightbox.scss
+++ b/app/assets/stylesheets/common/base/lightbox.scss
@@ -185,6 +185,20 @@
transition: transform 0.4s ease;
}
+ .pswp__caption-ai-description {
+ left: 50%;
+ bottom: 0;
+ color: var(--pswp-icon-color);
+ line-height: 1.35;
+ position: relative;
+ max-width: min(800px, calc(100% - 2rem));
+ padding: 0 1em 0.75em;
+ font-size: var(--font-down-1);
+ overflow-wrap: anywhere;
+ transform: translateX(-50%);
+ transition: transform 0.4s ease;
+ }
+
.pswp__caption-details {
position: relative;
left: 50%;
@@ -206,6 +220,10 @@
transform: translate(-50%, -5px);
}
+.pswp--caption-expanded .pswp__caption-ai-description {
+ transform: translate(-50%, -5px);
+}
+
.pswp--caption-expanded .pswp__caption-details {
transform: translate(-50%, -5px);
opacity: 1;
diff --git a/app/services/search_indexer.rb b/app/services/search_indexer.rb
index 09ec2c2e967..a3e443bcb67 100644
--- a/app/services/search_indexer.rb
+++ b/app/services/search_indexer.rb
@@ -196,8 +196,19 @@ class SearchIndexer
category_name:,
topic_tags:,
cooked:,
- private_message:
+ private_message:,
+ locale: SiteSetting.default_locale
)
+ post_text = HtmlScrubber.scrub(cooked)
+ post_text =
+ DiscoursePluginRegistry.apply_modifier(
+ :post_search_index_text,
+ post_text,
+ post_id,
+ cooked,
+ locale,
+ )
+
update_index(
table: "post",
id: post_id,
@@ -208,7 +219,7 @@ class SearchIndexer
# the original string. Since there is no way to estimate the length of
# the expected tsvector, we limit the input to ~50% of the maximum
# length of a tsvector (1_048_576 bytes).
- d_weight: HtmlScrubber.scrub(cooked)[0..600_000],
+ d_weight: post_text[0..600_000],
) { |params| params["private_message"] = private_message }
end
@@ -310,6 +321,7 @@ class SearchIndexer
topic_tags: tag_names,
cooked: obj.cooked,
private_message: topic.private_message?,
+ locale: obj.locale.presence || SiteSetting.default_locale,
)
SearchIndexer.update_topics_index(topic.id, topic.title, obj.cooked) if obj.is_first_post?
@@ -335,6 +347,7 @@ class SearchIndexer
topic_tags: tag_names,
cooked: post.cooked,
private_message: obj.private_message?,
+ locale: post.locale.presence || SiteSetting.default_locale,
)
SearchIndexer.update_topics_index(obj.id, obj.title, post.cooked)
diff --git a/db/structure.sql b/db/structure.sql
index 101d3de2f85..575c2cb2720 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -844,6 +844,44 @@ CREATE SEQUENCE public.ai_moderation_settings_id_seq
ALTER SEQUENCE public.ai_moderation_settings_id_seq OWNED BY public.ai_moderation_settings.id;
+--
+-- Name: ai_post_image_descriptions; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.ai_post_image_descriptions (
+ id bigint NOT NULL,
+ post_id integer NOT NULL,
+ upload_id integer NOT NULL,
+ base62_sha1 character varying(27) NOT NULL,
+ locale character varying(20) NOT NULL,
+ description text,
+ attempts integer DEFAULT 0 NOT NULL,
+ last_attempted_at timestamp(6) without time zone,
+ last_error text,
+ created_at timestamp(6) without time zone NOT NULL,
+ updated_at timestamp(6) without time zone NOT NULL
+);
+
+
+--
+-- Name: ai_post_image_descriptions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE public.ai_post_image_descriptions_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+
+
+--
+-- Name: ai_post_image_descriptions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE public.ai_post_image_descriptions_id_seq OWNED BY public.ai_post_image_descriptions.id;
+
+
--
-- Name: ai_posts_embeddings; Type: TABLE; Schema: public; Owner: -
--
@@ -12336,6 +12374,13 @@ ALTER TABLE ONLY public.ai_mcp_servers ALTER COLUMN id SET DEFAULT nextval('publ
ALTER TABLE ONLY public.ai_moderation_settings ALTER COLUMN id SET DEFAULT nextval('public.ai_moderation_settings_id_seq'::regclass);
+--
+-- Name: ai_post_image_descriptions id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.ai_post_image_descriptions ALTER COLUMN id SET DEFAULT nextval('public.ai_post_image_descriptions_id_seq'::regclass);
+
+
--
-- Name: ai_secrets id; Type: DEFAULT; Schema: public; Owner: -
--
@@ -14550,6 +14595,14 @@ ALTER TABLE ONLY public.ai_moderation_settings
ADD CONSTRAINT ai_moderation_settings_pkey PRIMARY KEY (id);
+--
+-- Name: ai_post_image_descriptions ai_post_image_descriptions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.ai_post_image_descriptions
+ ADD CONSTRAINT ai_post_image_descriptions_pkey PRIMARY KEY (id);
+
+
--
-- Name: ai_secrets ai_secrets_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -17119,6 +17172,20 @@ CREATE INDEX idx_ai_bot_conversation_stars_user_created ON public.discourse_ai_a
CREATE UNIQUE INDEX idx_ai_bot_conversation_stars_user_topic ON public.discourse_ai_ai_bot_conversation_stars USING btree (user_id, topic_id);
+--
+-- Name: idx_ai_post_image_descriptions_lookup; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX idx_ai_post_image_descriptions_lookup ON public.ai_post_image_descriptions USING btree (post_id, locale, base62_sha1);
+
+
+--
+-- Name: idx_ai_post_image_descriptions_reuse; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX idx_ai_post_image_descriptions_reuse ON public.ai_post_image_descriptions USING btree (base62_sha1, locale);
+
+
--
-- Name: idx_bookmarks_user_polymorphic_unique; Type: INDEX; Schema: public; Owner: -
--
@@ -22584,9 +22651,11 @@ INSERT INTO "schema_migrations" (version) VALUES
('20260713180615'),
('20260708095336'),
('20260708080308'),
+('20260708051450'),
('20260707184150'),
('20260707184146'),
('20260707013407'),
+('20260706151932'),
('20260703164430'),
('20260703163425'),
('20260702102111'),
diff --git a/frontend/discourse/app/lib/lightbox.js b/frontend/discourse/app/lib/lightbox.js
index 62f7a1a7ec3..442b576c9ed 100644
--- a/frontend/discourse/app/lib/lightbox.js
+++ b/frontend/discourse/app/lib/lightbox.js
@@ -152,23 +152,33 @@ async function initLightbox(elem, additionalData = {}) {
html: "",
onInit: (caption, pswp) => {
pswp.on("change", () => {
- const { element, title, inlineSVG } = pswp.currSlide.data;
+ const { aiDescription, element, title, inlineSVG } =
+ pswp.currSlide.data;
- if (!element || !title || inlineSVG) {
+ if (!element || inlineSVG) {
+ caption.innerHTML = "";
return;
}
- const captionTitle = escapeExpression(title);
+ const captionTitle = title ? escapeExpression(title) : null;
+ const captionAiDescription = aiDescription
+ ? escapeExpression(aiDescription)
+ : null;
const captionDetails =
element.querySelector(".informations")?.textContent;
const titleEl = captionTitle
? `<div class='pswp__caption-title'>${captionTitle}</div>`
: null;
+ const aiDescriptionEl = captionAiDescription
+ ? `<div class='pswp__caption-ai-description'>${captionAiDescription}</div>`
+ : null;
const detailsEl = captionDetails
? `<div class='pswp__caption-details'>${captionDetails}</div>`
: null;
- caption.innerHTML = [titleEl, detailsEl].filter(Boolean).join("");
+ caption.innerHTML = [titleEl, aiDescriptionEl, detailsEl]
+ .filter(Boolean)
+ .join("");
});
},
});
@@ -337,6 +347,7 @@ async function initLightbox(elem, additionalData = {}) {
el.getAttribute("data-orig-src") ||
null;
data.title = el.title || imgEl?.alt || imgEl?.title || null;
+ data.aiDescription = imgEl?.getAttribute("aria-description") || null;
data.base62SHA1 =
imgEl?.getAttribute("data-base62-sha1") ||
el.getAttribute("data-base62-sha1") ||
diff --git a/frontend/discourse/tests/acceptance/lightbox-test.js b/frontend/discourse/tests/acceptance/lightbox-test.js
index db093dca7ca..9ce6e3ff39c 100644
--- a/frontend/discourse/tests/acceptance/lightbox-test.js
+++ b/frontend/discourse/tests/acceptance/lightbox-test.js
@@ -11,7 +11,7 @@ acceptance("Lightbox", function (needs) {
const topicResponse = cloneJSON(topicFixtures["/t/280/1.json"]);
topicResponse.post_stream.posts[0].cooked += `<div class="lightbox-wrapper">
<a class="lightbox" href="/images/d-logo-sketch.png" data-download-href="//discourse.local/uploads/default/ad768537789cdf4679a18161ac0b0b6f0f4ccf9e" title="<script>image</script>">
- <img src="/images/d-logo-sketch-small.png" alt="<script>image</script>" data-base62-sha1="oKwwVE8qLWFBkE5UJeCs2EwxHHg" width="690" height="387" srcset="/images/d-logo-sketch-small.png" data-small-upload="/images/d-logo-sketch-small.png">
+ <img src="/images/d-logo-sketch-small.png" alt="<script>image</script>" aria-description="AI says &lt;strong&gt;this is a logo&lt;/strong&gt;" data-base62-sha1="oKwwVE8qLWFBkE5UJeCs2EwxHHg" width="690" height="387" srcset="/images/d-logo-sketch-small.png" data-small-upload="/images/d-logo-sketch-small.png">
<div class="meta">
<svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg>
<span class="filename">image</span><span class="informations">1500×842 234 KB</span>
@@ -38,6 +38,9 @@ acceptance("Lightbox", function (needs) {
assert
.dom(".pswp__caption .pswp__caption-details")
.hasText("1500×842 234 KB");
+ assert
+ .dom(".pswp__caption .pswp__caption-ai-description")
+ .hasText("AI says <strong>this is a logo</strong>");
assert
.dom(".pswp__button--download-image")
@@ -62,6 +65,9 @@ acceptance("Lightbox", function (needs) {
assert
.dom(".pswp__caption .pswp__caption-title")
.hasHtml(/^&lt;script&gt;image&lt;\/script&gt;/);
+ assert
+ .dom(".pswp__caption .pswp__caption-ai-description")
+ .hasHtml("AI says &lt;strong&gt;this is a logo&lt;/strong&gt;");
});
});
diff --git a/lib/localized_cooked_post_processor.rb b/lib/localized_cooked_post_processor.rb
index 3be15b7d0bc..35df412f5b2 100644
--- a/lib/localized_cooked_post_processor.rb
+++ b/lib/localized_cooked_post_processor.rb
@@ -20,6 +20,7 @@ class LocalizedCookedPostProcessor
def post_process
post_process_oneboxes
post_process_images
+ DiscourseEvent.trigger(:post_process_localized_cooked, @doc, @post, @post_localization)
@post_localization.link_post_uploads(fragments: @doc)
end
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb
index fa2b6115c7c..6df48fa1832 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb
@@ -11,18 +11,7 @@ module DiscourseAi
before_action :ensure_can_request_stream_suggestions, only: :stream_suggestion
before_action :rate_limiter_performed!
- include SecureUploadEndpointHelpers
-
- RATE_LIMITS = {
- "default" => {
- amount: 6,
- interval: 3.minutes,
- },
- "caption_image" => {
- amount: 20,
- interval: 1.minute,
- },
- }.freeze
+ RATE_LIMITS = { "default" => { amount: 6, interval: 3.minutes } }.freeze
def suggest
input = get_text_param!
@@ -237,41 +226,6 @@ module DiscourseAi
status: 502
end
- def caption_image
- image_url = params[:image_url]
- image_url_type = params[:image_url_type]
-
- raise Discourse::InvalidParameters.new(:image_url) if !image_url
- raise Discourse::InvalidParameters.new(:image_url) if !image_url_type
-
- if image_url_type == "short_path"
- image = Upload.find_by(sha1: Upload.sha1_from_short_path(image_url))
- elsif image_url_type == "short_url"
- image = Upload.find_by(sha1: Upload.sha1_from_short_url(image_url))
- else
- image = upload_from_full_url(image_url)
- end
-
- raise Discourse::NotFound if image.blank?
-
- check_secure_upload_permission(image) if image.secure?
- user = current_user
- assistant = DiscourseAi::AiHelper::Assistant.new
- assistant.ensure_mode_access!(DiscourseAi::AiHelper::Assistant::IMAGE_CAPTION, user)
-
- hijack do
- caption = assistant.generate_image_caption(image, user)
- render json: {
- caption:
- "#{caption} (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})",
- },
- status: :ok
- end
- rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed, Net::HTTPBadResponse
- render_json_error I18n.t("discourse_ai.ai_helper.errors.completion_request_failed"),
- status: 502
- end
-
private
CHANNEL_ID_KEY = "discourse_ai_helper_next_channel_id"
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/post_image_descriptions_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/post_image_descriptions_controller.rb
new file mode 100644
index 00000000000..e7354bcf25f
--- /dev/null
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/post_image_descriptions_controller.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ class PostImageDescriptionsController < ::ApplicationController
+ requires_plugin PLUGIN_NAME
+ requires_login
+
+ def index
+ post = find_post
+ guardian.ensure_can_edit!(post)
+
+ render json: {
+ descriptions:
+ DiscourseAi::PostImageDescriptions.editable_descriptions(
+ post,
+ description_locale(post),
+ ),
+ }
+ end
+
+ def update
+ post = find_post
+ guardian.ensure_can_edit!(post)
+
+ description = params[:description].to_s.strip
+
+ if description.blank?
+ return(
+ render_json_error(
+ I18n.t("discourse_ai.post_image_descriptions.errors.description_blank"),
+ status: :unprocessable_entity,
+ )
+ )
+ end
+
+ if description.length > DiscourseAi::PostImageDescriptions::MAX_DESCRIPTION_LENGTH
+ return(
+ render_json_error(
+ I18n.t(
+ "discourse_ai.post_image_descriptions.errors.description_too_long",
+ count: DiscourseAi::PostImageDescriptions::MAX_DESCRIPTION_LENGTH,
+ ),
+ status: :unprocessable_entity,
+ )
+ )
+ end
+
+ image_description =
+ DiscourseAi::PostImageDescriptions.update_description(
+ post,
+ description_locale(post),
+ params[:base62_sha1],
+ description,
+ )
+
+ raise Discourse::NotFound if image_description.blank?
+
+ render json: {
+ base62_sha1: image_description.base62_sha1,
+ description: image_description.description,
+ }
+ end
+
+ private
+
+ def find_post
+ Post.find(params[:post_id])
+ end
+
+ def description_locale(post)
+ params[:locale].presence || DiscourseAi::PostImageDescriptions.original_locale(post)
+ end
+ end
+end
diff --git a/plugins/discourse-ai/app/jobs/regular/generate_post_image_descriptions.rb b/plugins/discourse-ai/app/jobs/regular/generate_post_image_descriptions.rb
new file mode 100644
index 00000000000..d9423e51cfd
--- /dev/null
+++ b/plugins/discourse-ai/app/jobs/regular/generate_post_image_descriptions.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module Jobs
+ class GeneratePostImageDescriptions < ::Jobs::Base
+ sidekiq_options queue: "low", retry: false
+
+ def execute(args)
+ post_id = args[:post_id]
+ return if post_id.blank?
+
+ post = Post.find_by(id: post_id)
+ return if post.blank?
+
+ locale = args[:locale].presence || DiscourseAi::PostImageDescriptions.original_locale(post)
+
+ DistributedMutex.synchronize(
+ "generate_post_image_descriptions_#{post.id}_#{locale}",
+ validity: 10.minutes,
+ ) do
+ DiscourseAi::PostImageDescriptions.generate_missing(
+ post,
+ locale: locale,
+ base62_sha1s: args[:base62_sha1s],
+ )
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/app/jobs/scheduled/post_image_descriptions_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/post_image_descriptions_backfill.rb
new file mode 100644
index 00000000000..9acda8dc488
--- /dev/null
+++ b/plugins/discourse-ai/app/jobs/scheduled/post_image_descriptions_backfill.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module Jobs
+ class PostImageDescriptionsBackfill < ::Jobs::Scheduled
+ every 15.minutes
+ cluster_concurrency 1
+ sidekiq_options retry: false
+
+ def execute(_args)
+ return if !DiscourseAi::PostImageDescriptions.enabled?
+ return if !DiscourseAi::PostImageDescriptions.generation_enabled?
+ return if !DiscourseAi::PostImageDescriptions.credits_available?
+
+ limit = DiscourseAi::PostImageDescriptions.backfill_limit
+ return if limit <= 0
+
+ DiscourseAi::PostImageDescriptions
+ .backfill_targets(limit:)
+ .each do |target|
+ Jobs.enqueue(
+ :generate_post_image_descriptions,
+ post_id: target[:post_id],
+ locale: target[:locale],
+ )
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/app/models/ai_post_image_description.rb b/plugins/discourse-ai/app/models/ai_post_image_description.rb
new file mode 100644
index 00000000000..74542c1f072
--- /dev/null
+++ b/plugins/discourse-ai/app/models/ai_post_image_description.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+class AiPostImageDescription < ActiveRecord::Base
+ belongs_to :post
+ belongs_to :upload
+end
+
+# == Schema Information
+#
+# Table name: ai_post_image_descriptions
+#
+# id :bigint not null, primary key
+# attempts :integer default(0), not null
+# base62_sha1 :string(27) not null
+# description :text
+# last_attempted_at :datetime
+# last_error :text
+# locale :string(20) not null
+# created_at :datetime not null
+# updated_at :datetime not null
+# post_id :integer not null
+# upload_id :integer not null
+#
+# Indexes
+#
+# idx_ai_post_image_descriptions_lookup (post_id,locale,base62_sha1) UNIQUE
+# idx_ai_post_image_descriptions_reuse (base62_sha1,locale)
+#
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-post-image-description-editor-button.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-post-image-description-editor-button.gjs
new file mode 100644
index 00000000000..242ca175bae
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-post-image-description-editor-button.gjs
@@ -0,0 +1,36 @@
+import Component from "@glimmer/component";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import DButton from "discourse/ui-kit/d-button";
+import AiPostImageDescriptionEditorModal from "./modal/ai-post-image-description-editor";
+
+export default class AiPostImageDescriptionEditorButton extends Component {
+ @service modal;
+ @service postImageDescriptionEditor;
+
+ get description() {
+ return this.postImageDescriptionEditor.descriptionFor(this.args.base62Sha1);
+ }
+
+ @action
+ openEditor() {
+ this.modal.show(AiPostImageDescriptionEditorModal, {
+ model: {
+ base62Sha1: this.args.base62Sha1,
+ description: this.description,
+ },
+ });
+ }
+
+ <template>
+ {{#if this.description}}
+ <DButton
+ @action={{this.openEditor}}
+ @ariaLabel="discourse_ai.post_image_descriptions.edit"
+ @icon="pencil"
+ @title="discourse_ai.post_image_descriptions.edit"
+ class="btn-transparent ai-post-image-description-editor__button"
+ />
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-post-image-description-editor.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-post-image-description-editor.gjs
new file mode 100644
index 00000000000..cf748b051f9
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-post-image-description-editor.gjs
@@ -0,0 +1,61 @@
+import Component from "@glimmer/component";
+import { cached } from "@glimmer/tracking";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import Form from "discourse/components/form";
+import DModal from "discourse/ui-kit/d-modal";
+import DModalCancel from "discourse/ui-kit/d-modal-cancel";
+import { i18n } from "discourse-i18n";
+
+export default class AiPostImageDescriptionEditorModal extends Component {
+ @service postImageDescriptionEditor;
+
+ @cached
+ get formData() {
+ return {
+ description: this.args.model.description,
+ };
+ }
+
+ @action
+ async save(data) {
+ try {
+ await this.postImageDescriptionEditor.save(
+ this.args.model.base62Sha1,
+ data.description
+ );
+ this.args.closeModal();
+ } catch {
+ // popupAjaxError is handled by the service.
+ }
+ }
+
+ <template>
+ <DModal
+ @closeModal={{@closeModal}}
+ @title={{i18n "discourse_ai.post_image_descriptions.title"}}
+ class="ai-post-image-description-editor-modal"
+ >
+ <Form @data={{this.formData}} @onSubmit={{this.save}} as |form|>
+ <form.Field
+ @format="full"
+ @name="description"
+ @title={{i18n "discourse_ai.post_image_descriptions.description"}}
+ @type="textarea"
+ @validation="required:trim|length:1,1000"
+ as |field|
+ >
+ <field.Control
+ @height={{120}}
+ class="ai-post-image-description-editor-modal__textarea"
+ />
+ </form.Field>
+
+ <form.Actions>
+ <DModalCancel @close={{@closeModal}} />
+ <form.Submit @label="discourse_ai.post_image_descriptions.save" />
+ </form.Actions>
+ </Form>
+ </DModal>
+ </template>
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/editor-preview/ai-image-caption-container.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/editor-preview/ai-image-caption-container.gjs
deleted file mode 100644
index dcb11bf17f8..00000000000
--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/editor-preview/ai-image-caption-container.gjs
+++ /dev/null
@@ -1,94 +0,0 @@
-import Component from "@glimmer/component";
-import { on } from "@ember/modifier";
-import { action } from "@ember/object";
-import didInsert from "@ember/render-modifiers/modifiers/did-insert";
-import didUpdate from "@ember/render-modifiers/modifiers/did-update";
-import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
-import { service } from "@ember/service";
-import DButton from "discourse/ui-kit/d-button";
-import DConditionalLoadingSpinner from "discourse/ui-kit/d-conditional-loading-spinner";
-import DTextarea from "discourse/ui-kit/d-textarea";
-import dIcon from "discourse/ui-kit/helpers/d-icon";
-import dAutoFocus from "discourse/ui-kit/modifiers/d-auto-focus";
-import { i18n } from "discourse-i18n";
-
-export default class AiImageCaptionContainer extends Component {
- @service imageCaptionPopup;
-
- @action
- updateCaption(event) {
- event.preventDefault();
- this.imageCaptionPopup.newCaption = event.target.value;
- }
-
- @action
- saveCaption() {
- this.imageCaptionPopup.updateCaption();
- this.hidePopup();
- }
-
- @action
- resizeTextarea(target) {
- const style = window.getComputedStyle(target);
-
- // scrollbars will show based on scrollHeight alone
- // so we need to consider borders too
- const borderTopWidth = parseInt(style.borderTopWidth, 10);
- const borderBottomWidth = parseInt(style.borderBottomWidth, 10);
-
- target.scrollTop = 0;
- target.style.height = `${
- target.scrollHeight + borderTopWidth + borderBottomWidth
- }px`;
- }
-
- @action
- hidePopup() {
- this.imageCaptionPopup.showPopup = false;
- if (this.imageCaptionPopup._request) {
- this.imageCaptionPopup._request.abort();
- this.imageCaptionPopup._request = null;
- this.imageCaptionPopup.toggleLoadingState(false);
- }
- }
-
- <template>
- {{#if this.imageCaptionPopup.showPopup}}
- <div
- class="composer-popup education-message ai-caption-popup"
- {{willDestroy this.hidePopup}}
- >
- <DConditionalLoadingSpinner
- @condition={{this.imageCaptionPopup.loading}}
- >
- <DTextarea
- {{didInsert this.resizeTextarea}}
- {{didUpdate this.resizeTextarea this.imageCaptionPopup.newCaption}}
- @value={{this.imageCaptionPopup.newCaption}}
- {{on "change" this.updateCaption}}
- {{dAutoFocus}}
- />
- </DConditionalLoadingSpinner>
-
- <div class="actions">
- <DButton
- class="btn-primary"
- @label="discourse_ai.ai_helper.image_caption.save_caption"
- @icon="check"
- @action={{this.saveCaption}}
- />
- <DButton
- class="btn-flat cancel-request"
- @label="cancel"
- @action={{this.hidePopup}}
- />
-
- <span class="credits">
- {{dIcon "discourse-sparkles"}}
- <span>{{i18n "discourse_ai.ai_helper.image_caption.credits"}}</span>
- </span>
- </div>
- </div>
- {{/if}}
- </template>
-}
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.js
deleted file mode 100644
index 997043cdf78..00000000000
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.js
+++ /dev/null
@@ -1,100 +0,0 @@
-import { ajax } from "discourse/lib/ajax";
-import { popupAjaxError } from "discourse/lib/ajax-error";
-import { apiInitializer } from "discourse/lib/api";
-import { i18n } from "discourse-i18n";
-import {
- isAiCreditLimitError,
- popupAiCreditLimitError,
-} from "../lib/ai-errors";
-
-function isAiCaptionableImage(uploadUrl) {
- if (!uploadUrl) {
- return false;
- }
- return /\.(png|jpe?g|gif|webp)$/i.test(uploadUrl);
-}
-
-export default apiInitializer((api) => {
- const buttonAttrs = {
- label: i18n("discourse_ai.ai_helper.image_caption.button_label"),
- icon: "discourse-sparkles",
- class: "generate-caption",
- };
- const settings = api.container.lookup("service:site-settings");
- const currentUser = api.getCurrentUser();
-
- if (
- !settings.ai_helper_enabled_features.includes("image_caption") ||
- !currentUser?.can_use_assistant
- ) {
- return;
- }
-
- api.addComposerImageWrapperButton(
- buttonAttrs.label,
- buttonAttrs.class,
- buttonAttrs.icon,
- (event) => {
- const imageCaptionPopup = api.container.lookup(
- "service:imageCaptionPopup"
- );
-
- imageCaptionPopup.popupTrigger = event.target;
-
- if (
- imageCaptionPopup.popupTrigger.classList.contains("generate-caption")
- ) {
- const buttonWrapper = event.target.closest(".button-wrapper");
- const imageIndex = parseInt(
- buttonWrapper.getAttribute("data-image-index"),
- 10
- );
- const imageSrc = event.target
- .closest(".image-wrapper")
- .querySelector("img")
- .getAttribute("src");
-
- imageCaptionPopup.toggleLoadingState(true);
-
- const site = api.container.lookup("service:site");
- if (!site.mobileView) {
- imageCaptionPopup.showPopup = !imageCaptionPopup.showPopup;
- }
-
- imageCaptionPopup._request = ajax(
- `/discourse-ai/ai-helper/caption_image`,
- {
- method: "POST",
- data: {
- image_url: imageSrc,
- image_url_type: "long_url",
- },
- }
- );
-
- imageCaptionPopup._request
- .then(({ caption }) => {
- imageCaptionPopup.imageSrc = imageSrc;
- imageCaptionPopup.imageIndex = imageIndex;
- imageCaptionPopup.newCaption = caption;
-
- if (site.mobileView) {
- // Auto-saves caption on mobile view
- imageCaptionPopup.updateCaption();
- }
- })
- .catch((error) => {
- if (isAiCreditLimitError(error)) {
- popupAiCreditLimitError(error);
- } else {
- popupAjaxError(error);
- }
- })
- .finally(() => {
- imageCaptionPopup.toggleLoadingState(false);
- });
- }
- },
- isAiCaptionableImage
- );
-});
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-post-image-description-editor.gjs b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-post-image-description-editor.gjs
new file mode 100644
index 00000000000..412c90b61ea
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-post-image-description-editor.gjs
@@ -0,0 +1,58 @@
+import { withPluginApi } from "discourse/lib/plugin-api";
+import AiPostImageDescriptionEditorButton from "../components/ai-post-image-description-editor-button";
+import {
+ ensureImageDescriptionTarget,
+ imageBase62Sha1,
+} from "../lib/post-image-description-editor";
+
+function initializeAiPostImageDescriptionEditor(api) {
+ const editor = api.container.lookup("service:post-image-description-editor");
+
+ api.decorateCookedElement(
+ (element, helper) => {
+ if (
+ !helper.renderGlimmer ||
+ !element.classList.contains("d-editor-preview")
+ ) {
+ return;
+ }
+
+ if (!editor.canEditCurrentComposer) {
+ return;
+ }
+
+ editor.ensureLoaded();
+
+ element.querySelectorAll("span.image-wrapper").forEach((imageWrapper) => {
+ const image = imageWrapper.querySelector("img");
+ const base62Sha1 = imageBase62Sha1(image);
+
+ if (!base62Sha1) {
+ return;
+ }
+
+ const target = ensureImageDescriptionTarget(imageWrapper);
+ if (!target) {
+ return;
+ }
+
+ helper.renderGlimmer(
+ target,
+ <template>
+ <AiPostImageDescriptionEditorButton @base62Sha1={{base62Sha1}} />
+ </template>
+ );
+ });
+ },
+ {
+ id: "ai-post-image-description-editor",
+ }
+ );
+}
+
+export default {
+ name: "ai-post-image-description-editor",
+ initialize() {
+ withPluginApi(initializeAiPostImageDescriptionEditor);
+ },
+};
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
index 134826546af..4812bbd6d8e 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
@@ -23,6 +23,10 @@ export const AI_FEATURE_SETTING_GROUPS = {
settings: [
"ai_helper_automatic_chat_thread_title",
"ai_helper_automatic_chat_thread_title_delay",
+ "ai_post_image_descriptions_enabled",
+ "ai_post_image_descriptions_per_post_limit",
+ "ai_post_image_descriptions_backfill_hourly_rate",
+ "ai_post_image_descriptions_backfill_max_age_days",
],
},
{
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/post-image-description-editor.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/post-image-description-editor.js
new file mode 100644
index 00000000000..090b490098e
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/post-image-description-editor.js
@@ -0,0 +1,88 @@
+const BASE62_SHA1_REGEXP = /^[0-9A-Za-z]{1,27}$/;
+const BASE62_KEYS =
+ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+const HEX_SHA1_REGEXP = /^[0-9a-f]{40}$/i;
+const ORIGINAL_UPLOAD_SHA1_REGEXP =
+ /\/original\/\dX\/(?:[./\w-]*\/)?([0-9a-f]{40})(?:[.\w-]*)?$/i;
+const UPLOAD_SHORT_URL_PREFIX = "upload://";
+
+export function imageBase62Sha1(image) {
+ const base62Sha1 = image?.dataset?.base62Sha1;
+
+ if (base62Sha1 && BASE62_SHA1_REGEXP.test(base62Sha1)) {
+ return base62Sha1;
+ }
+
+ const origSrc =
+ image?.getAttribute?.("data-orig-src") || image?.dataset?.origSrc;
+ if (origSrc?.startsWith(UPLOAD_SHORT_URL_PREFIX)) {
+ const uploadPath = origSrc.slice(UPLOAD_SHORT_URL_PREFIX.length);
+ const extensionIndex = uploadPath.indexOf(".");
+ const queryIndex = uploadPath.indexOf("?");
+ const fragmentIndex = uploadPath.indexOf("#");
+ const endIndex = [extensionIndex, queryIndex, fragmentIndex]
+ .filter((index) => index !== -1)
+ .sort((first, second) => first - second)[0];
+ const fallbackBase62Sha1 = uploadPath.slice(
+ 0,
+ endIndex ?? uploadPath.length
+ );
+
+ if (fallbackBase62Sha1 && BASE62_SHA1_REGEXP.test(fallbackBase62Sha1)) {
+ return fallbackBase62Sha1;
+ }
+ }
+
+ return base62Sha1FromUploadUrl(image);
+}
+
+function base62Sha1FromUploadUrl(image) {
+ const src = image?.getAttribute?.("src");
+ if (!src) {
+ return;
+ }
+
+ let pathname;
+ try {
+ pathname = new URL(src, window.location.origin).pathname;
+ } catch {
+ return;
+ }
+
+ const sha1 = pathname.match(ORIGINAL_UPLOAD_SHA1_REGEXP)?.[1];
+ if (!sha1 || !HEX_SHA1_REGEXP.test(sha1)) {
+ return;
+ }
+
+ return base62Encode(BigInt(`0x${sha1}`));
+}
+
+function base62Encode(number) {
+ if (number === 0n) {
+ return "0";
+ }
+
+ let encoded = "";
+ while (number > 0n) {
+ encoded = BASE62_KEYS[Number(number % 62n)] + encoded;
+ number /= 62n;
+ }
+
+ return encoded;
+}
+
+export function ensureImageDescriptionTarget(imageWrapper) {
+ const buttonWrapper = imageWrapper.querySelector(".button-wrapper");
+ if (!buttonWrapper) {
+ return;
+ }
+
+ let target = buttonWrapper.querySelector(".ai-post-image-description-editor");
+ if (!target) {
+ target = document.createElement("span");
+ target.className = "ai-post-image-description-editor";
+ buttonWrapper.appendChild(target);
+ }
+
+ return target;
+}
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.js
deleted file mode 100644
index 8435131652f..00000000000
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/image-caption-popup.js
+++ /dev/null
@@ -1,43 +0,0 @@
-import { tracked } from "@glimmer/tracking";
-import Service, { service } from "@ember/service";
-import { IMAGE_MARKDOWN_REGEX } from "discourse/lib/uploads";
-
-export default class ImageCaptionPopup extends Service {
- @service composer;
- @service appEvents;
-
- @tracked showPopup = false;
- @tracked imageIndex = null;
- @tracked imageSrc = null;
- @tracked newCaption = null;
- @tracked loading = false;
- @tracked popupTrigger = null;
- @tracked _request = null;
-
- updateCaption() {
- const matchingPlaceholder =
- this.composer.model.reply.match(IMAGE_MARKDOWN_REGEX);
-
- if (matchingPlaceholder) {
- const match = matchingPlaceholder[this.imageIndex];
- const replacement = match.replace(
- IMAGE_MARKDOWN_REGEX,
- `![${this.newCaption}|$2$3$4]($5)`
- );
-
- if (match) {
- this.appEvents.trigger("composer:replace-text", match, replacement);
- }
- }
- }
-
- toggleLoadingState(loading) {
- if (loading) {
- this.popupTrigger?.classList.add("disabled");
- return (this.loading = true);
- }
-
- this.popupTrigger?.classList.remove("disabled");
- return (this.loading = false);
- }
-}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/post-image-description-editor.js b/plugins/discourse-ai/assets/javascripts/discourse/services/post-image-description-editor.js
new file mode 100644
index 00000000000..0d471c205fa
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/post-image-description-editor.js
@@ -0,0 +1,131 @@
+import { tracked } from "@glimmer/tracking";
+import Service, { service } from "@ember/service";
+import { ajax } from "discourse/lib/ajax";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+import { EDIT } from "discourse/models/composer";
+import { i18n } from "discourse-i18n";
+
+export default class PostImageDescriptionEditor extends Service {
+ @service a11y;
+ @service composer;
+ @service siteSettings;
+ @service toasts;
+
+ @tracked descriptions = new Map();
+ @tracked loadedKey = null;
+ @tracked loadingKey = null;
+
+ get canEditCurrentComposer() {
+ return Boolean(
+ this.siteSettings.ai_post_image_descriptions_enabled &&
+ this.composer.model?.action === EDIT &&
+ this.currentPostId
+ );
+ }
+
+ get currentPostId() {
+ return this.composer.model?.post?.id;
+ }
+
+ get currentLocale() {
+ return this.composer.model?.locale;
+ }
+
+ get currentKey() {
+ if (!this.currentPostId) {
+ return;
+ }
+
+ return `${this.currentPostId}:${this.currentLocale || ""}`;
+ }
+
+ descriptionFor(base62Sha1) {
+ if (!this.canEditCurrentComposer || this.loadedKey !== this.currentKey) {
+ return;
+ }
+
+ return this.descriptions.get(base62Sha1);
+ }
+
+ async ensureLoaded() {
+ if (!this.canEditCurrentComposer) {
+ this.#reset();
+ return;
+ }
+
+ const key = this.currentKey;
+ if (this.loadedKey === key || this.loadingKey === key) {
+ return;
+ }
+
+ this.loadingKey = key;
+ this.loadedKey = null;
+ this.descriptions = new Map();
+
+ try {
+ const result = await ajax(
+ `/discourse-ai/post-image-descriptions/${this.currentPostId}`,
+ {
+ data: this.currentLocale ? { locale: this.currentLocale } : {},
+ }
+ );
+
+ if (this.currentKey === key) {
+ this.descriptions = new Map(
+ result.descriptions.map((description) => [
+ description.base62_sha1,
+ description.description,
+ ])
+ );
+ this.loadedKey = key;
+ }
+ } catch {
+ if (this.currentKey === key) {
+ this.descriptions = new Map();
+ this.loadedKey = key;
+ }
+ } finally {
+ if (this.loadingKey === key) {
+ this.loadingKey = null;
+ }
+ }
+ }
+
+ async save(base62Sha1, description) {
+ try {
+ const result = await ajax(
+ `/discourse-ai/post-image-descriptions/${this.currentPostId}/${base62Sha1}`,
+ {
+ type: "PUT",
+ data: {
+ description,
+ ...(this.currentLocale ? { locale: this.currentLocale } : {}),
+ },
+ }
+ );
+
+ const descriptions = new Map(this.descriptions);
+ descriptions.set(result.base62_sha1, result.description);
+ this.descriptions = descriptions;
+ this.loadedKey = this.currentKey;
+
+ const message = i18n("discourse_ai.post_image_descriptions.saved");
+ this.toasts.success({
+ duration: "short",
+ data: { message },
+ });
+ this.a11y.announce(message, "polite");
+
+ return result;
+ } catch (error) {
+ popupAjaxError(error);
+ throw error;
+ }
+ }
+
+ #reset() {
+ this.descriptions = new Map();
+ this.loadedKey = null;
+ this.loadingKey = null;
+ }
+}
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.scss
index 52d0b66e216..4ebc10185a0 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
@@ -468,114 +468,19 @@
}
}
-// AI Image Caption Feature:
-.image-wrapper {
- .button-wrapper {
- .generate-caption {
- background: var(--tertiary-low);
- color: var(--tertiary);
- box-shadow: var(--shadow-dropdown);
- position: absolute;
- white-space: nowrap;
- top: -2rem;
- left: 0.35rem;
- padding: 0.33em 0.75em;
- transition: all 0.25s ease;
-
- .discourse-no-touch & {
- display: none;
- }
-
- .d-icon {
- margin-right: 0.25rem;
- }
-
- &:active {
- box-shadow: none;
- }
-
- &:hover,
- &:focus {
- background: var(--tertiary-400);
- color: var(--tertiary-hover);
- cursor: pointer;
- }
-
- &.disabled {
- pointer-events: none;
- cursor: not-allowed;
- opacity: 0.7;
- }
- }
- }
-
- .discourse-no-touch & {
- &:hover {
- .button-wrapper .generate-caption {
- display: block;
- }
- }
- }
-}
-
-.ai-caption-popup {
- --ai-caption-popup-min-width: 20rem;
- width: auto;
- right: unset;
- padding: 1em;
- top: unset;
- bottom: 0;
-
- .loading-container {
- min-width: var(--ai-caption-popup-min-width);
- }
-
- textarea {
- box-sizing: border-box;
- width: 100%;
- max-width: 40dvw;
- max-height: calc(100dvh - var(--header-offset) - 10em);
- min-height: 3em;
- height: 7em;
- min-width: var(--ai-caption-popup-min-width);
+.ai-post-image-description-editor {
+ display: contents;
- @include viewport.until(md) {
- width: 100%;
- max-width: unset;
- min-width: unset;
- }
- }
-
- .actions {
- display: flex;
- align-items: center;
- gap: 0.5rem;
-
- .credits {
- font-size: var(--font-down-1);
- margin-left: auto;
- color: var(--tertiary);
-
- .desktop-view & {
- // a little extra space for extra narrow desktop view
- @media screen and (width <= 675px) {
- span {
- display: none;
- }
- }
- }
- }
- }
-
- .spinner {
- border-color: var(--tertiary-600);
- border-right-color: var(--tertiary);
+ &__button.btn-transparent {
+ color: var(--tertiary);
+ min-height: 0;
+ padding: 0;
}
}
-.ai-image-caption-prompt-dialog {
- .dialog-content {
- max-width: 555px;
+.ai-post-image-description-editor-modal {
+ &__textarea {
+ min-height: 7em;
}
}
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index a3a2ee89dc8..4d288aed491 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -232,6 +232,12 @@ en:
discourse_ai:
title: "AI"
+ post_image_descriptions:
+ edit: "Edit AI image description"
+ title: "Edit AI image description"
+ description: "Description"
+ save: "Save description"
+ saved: "AI image description saved"
discourse_workflows:
ai_agent:
agent_id: "Agent"
@@ -371,7 +377,6 @@ en:
translate: "Translate"
markdown_tables: "Generate Markdown table"
custom_prompt: "Custom prompt"
- image_caption: "Caption images"
translator: "Translator"
post_illustrator: "Post illustrator"
chat_thread_titles: "Chat thread titles"
@@ -1121,11 +1126,6 @@ en:
select: "Select"
selected: "Selected"
try_again: "Try Again"
- image_caption:
- button_label: "Caption with AI"
- generating: "Generating caption..."
- credits: "Captioned by AI"
- save_caption: "Save"
no_content_error: "Add content first to perform AI actions on it"
errors:
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 4d4df0342ad..c225729a810 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -103,6 +103,10 @@ en:
ai_helper_automatic_chat_thread_title_delay: "Delay in minutes before the AI helper automatically sets the chat thread title."
ai_helper_automatic_chat_thread_title: "Automatically set the chat thread titles based on thread contents."
ai_helper_enabled_features: "Select the features to enable in the AI helper."
+ ai_post_image_descriptions_enabled: "Enable AI image descriptions for post images. Generating missing descriptions requires a vision-capable image caption agent."
+ ai_post_image_descriptions_per_post_limit: "Maximum number of post images to describe per post."
+ ai_post_image_descriptions_backfill_hourly_rate: "Maximum number of posts per hour to backfill with AI-generated image descriptions. Set to 0 to disable backfill."
+ ai_post_image_descriptions_backfill_max_age_days: "Maximum age in days of posts to backfill with AI-generated image descriptions."
post_ai_helper_allowed_groups: "User groups allowed to access AI Helper features in posts"
ai_helper_post_illustrator_agent: "The agent to use for the AI helper's illustrate post feature. Note: you must also create an image generation tool (Admin > Plugins > Discourse AI > Tools) for this feature to work."
ai_helper_chat_thread_title_agent: "The agent to use for generating chat thread titles."
@@ -378,7 +382,13 @@ en:
illustrate_post: "Illustrate Post"
replace_dates: "Smart dates"
image_caption:
- attribution: "Captioned by AI"
+ aria_description: "%{description} (Captioned by AI)"
+ post_image_descriptions:
+ errors:
+ description_blank: "Description can't be blank."
+ description_too_long:
+ one: "Description must be %{count} character or fewer."
+ other: "Description must be %{count} characters or fewer."
share_ai:
read_more: "Read full transcript"
diff --git a/plugins/discourse-ai/config/routes.rb b/plugins/discourse-ai/config/routes.rb
index 5ca6a62563f..67d8b23ea16 100644
--- a/plugins/discourse-ai/config/routes.rb
+++ b/plugins/discourse-ai/config/routes.rb
@@ -5,13 +5,17 @@ DiscourseAi::Engine.routes.draw do
get "status" => "ai_credits#status"
end
+ scope path: "/post-image-descriptions", defaults: { format: :json } do
+ get ":post_id" => "post_image_descriptions#index"
+ put ":post_id/:base62_sha1" => "post_image_descriptions#update"
+ end
+
scope module: :ai_helper, path: "/ai-helper", defaults: { format: :json } do
post "suggest" => "assistant#suggest"
post "suggest_title" => "assistant#suggest_title"
post "suggest_category" => "assistant#suggest_category"
post "suggest_tags" => "assistant#suggest_tags"
post "stream_suggestion" => "assistant#stream_suggestion"
- post "caption_image" => "assistant#caption_image"
end
scope module: :embeddings, path: "/embeddings", defaults: { format: :json } do
diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.yml
index 9851a33b4e7..0ed406065dd 100644
--- a/plugins/discourse-ai/config/settings.yml
+++ b/plugins/discourse-ai/config/settings.yml
@@ -140,7 +140,6 @@ discourse_ai:
choices:
- "suggestions"
- "context_menu"
- - "image_caption"
area: "ai-features/ai_helper"
ai_helper_proofreader_agent:
default: "-22"
@@ -187,6 +186,23 @@ discourse_ai:
type: enum
enum: "DiscourseAi::Configuration::AgentEnumerator"
area: "ai-features/ai_helper"
+ ai_post_image_descriptions_enabled:
+ default: false
+ client: true
+ area: "ai-features/ai_helper"
+ ai_post_image_descriptions_per_post_limit:
+ default: 10
+ min: 1
+ area: "ai-features/ai_helper"
+ ai_post_image_descriptions_backfill_hourly_rate:
+ default: 0
+ min: 0
+ area: "ai-features/ai_helper"
+ ai_post_image_descriptions_backfill_max_age_days:
+ default: 30
+ min: 1
+ max: 10000
+ area: "ai-features/ai_helper"
ai_helper_chat_thread_title_agent:
default: "-35"
type: enum
diff --git a/plugins/discourse-ai/db/migrate/20260706151932_create_ai_post_image_descriptions.rb b/plugins/discourse-ai/db/migrate/20260706151932_create_ai_post_image_descriptions.rb
new file mode 100644
index 00000000000..f00a80e026e
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20260706151932_create_ai_post_image_descriptions.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class CreateAiPostImageDescriptions < ActiveRecord::Migration[8.0]
+ def change
+ create_table :ai_post_image_descriptions do |t|
+ t.integer :post_id, null: false
+ t.integer :upload_id, null: false
+ t.string :base62_sha1, null: false, limit: 27
+ t.string :locale, null: false, limit: 20
+ t.text :description
+ t.integer :attempts, null: false, default: 0
+ t.datetime :last_attempted_at
+ t.text :last_error
+ t.timestamps null: false
+ end
+
+ add_index :ai_post_image_descriptions,
+ %i[post_id locale base62_sha1],
+ unique: true,
+ name: "idx_ai_post_image_descriptions_lookup"
+ add_index :ai_post_image_descriptions,
+ %i[base62_sha1 locale],
+ name: "idx_ai_post_image_descriptions_reuse"
+ end
+end
diff --git a/plugins/discourse-ai/db/migrate/20260708051450_remove_image_caption_from_ai_helper_enabled_features.rb b/plugins/discourse-ai/db/migrate/20260708051450_remove_image_caption_from_ai_helper_enabled_features.rb
new file mode 100644
index 00000000000..e82b2cb285a
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20260708051450_remove_image_caption_from_ai_helper_enabled_features.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+class RemoveImageCaptionFromAiHelperEnabledFeatures < ActiveRecord::Migration[8.0]
+ def up
+ execute <<~SQL
+ UPDATE site_settings
+ SET value = (
+ SELECT array_to_string(
+ ARRAY(
+ SELECT feature
+ FROM unnest(string_to_array(site_settings.value, '|')) AS feature
+ WHERE feature <> 'image_caption'
+ ),
+ '|'
+ )
+ ),
+ updated_at = NOW()
+ WHERE name = 'ai_helper_enabled_features'
+ AND 'image_caption' = ANY(string_to_array(value, '|'))
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-ai/lib/ai_helper/assistant.rb b/plugins/discourse-ai/lib/ai_helper/assistant.rb
index 6f8ff1874b4..0703538e59d 100644
--- a/plugins/discourse-ai/lib/ai_helper/assistant.rb
+++ b/plugins/discourse-ai/lib/ai_helper/assistant.rb
@@ -68,6 +68,10 @@ module DiscourseAi
def custom_locale_instructions(user = nil, force_default_locale)
locale = SiteSetting.default_locale
locale = user.effective_locale if !force_default_locale && user
+ locale_instructions(locale)
+ end
+
+ def locale_instructions(locale)
locale_hash = LocaleSiteSetting.language_names[locale]
if locale != "en" && locale_hash
@@ -250,12 +254,19 @@ module DiscourseAi
end
end
- def generate_image_caption(upload, user)
- bot = build_bot(IMAGE_CAPTION, user)
+ def generate_image_caption(upload, user, locale: nil, post: nil, skip_access_check: false)
+ bot = build_bot(IMAGE_CAPTION, user, skip_access_check: skip_access_check)
force_default_locale = false
+ custom_instructions =
+ if locale.present?
+ locale_instructions(locale)
+ else
+ custom_locale_instructions(user, force_default_locale)
+ end
context =
DiscourseAi::Agents::BotContext.new(
+ post: post,
user: user,
skip_show_thinking: true,
feature_name: IMAGE_CAPTION,
@@ -265,7 +276,7 @@ module DiscourseAi
content: ["Describe this image in a single sentence.", { upload_id: upload.id }],
},
],
- custom_instructions: custom_locale_instructions(user, force_default_locale),
+ custom_instructions: custom_instructions,
)
structured_output = nil
@@ -318,8 +329,13 @@ module DiscourseAi
AiAgent.find_by(id: agent_id)
end
- def build_bot(helper_mode, user)
- ai_agent = ensure_mode_access!(helper_mode, user)
+ def build_bot(helper_mode, user, skip_access_check: false)
+ ai_agent =
+ if skip_access_check
+ ai_agent_for_mode(helper_mode)
+ else
+ ensure_mode_access!(helper_mode, user)
+ end
return if ai_agent.nil?
agent_klass = ai_agent.class_instance
diff --git a/plugins/discourse-ai/lib/configuration/feature.rb b/plugins/discourse-ai/lib/configuration/feature.rb
index 6efc2f24c8b..c38c4498989 100644
--- a/plugins/discourse-ai/lib/configuration/feature.rb
+++ b/plugins/discourse-ai/lib/configuration/feature.rb
@@ -115,12 +115,6 @@ module DiscourseAi
DiscourseAi::Configuration::Module::AI_HELPER_ID,
DiscourseAi::Configuration::Module::AI_HELPER,
),
- new(
- "image_caption",
- "ai_helper_image_caption_agent",
- DiscourseAi::Configuration::Module::AI_HELPER_ID,
- DiscourseAi::Configuration::Module::AI_HELPER,
- ),
new(
"post_illustrator",
"ai_helper_post_illustrator_agent",
diff --git a/plugins/discourse-ai/lib/configuration/llm_enumerator.rb b/plugins/discourse-ai/lib/configuration/llm_enumerator.rb
index 51cc71860b3..314178179f7 100644
--- a/plugins/discourse-ai/lib/configuration/llm_enumerator.rb
+++ b/plugins/discourse-ai/lib/configuration/llm_enumerator.rb
@@ -47,11 +47,14 @@ module DiscourseAi
end
end
- if SiteSetting.ai_helper_enabled_features.split("|").include?("image_caption")
+ if SiteSetting.ai_post_image_descriptions_enabled
image_caption_agent = AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent)
- model_id = image_caption_agent.default_llm_id || SiteSetting.ai_default_llm_model.to_i
- rval[model_id] << { type: :ai_helper_image_caption }
+ if image_caption_agent.present?
+ model_id = image_caption_agent.default_llm_id || SiteSetting.ai_default_llm_model.to_i
+
+ rval[model_id] << { type: :ai_helper_image_caption }
+ end
end
if SiteSetting.ai_summarization_enabled
diff --git a/plugins/discourse-ai/lib/post_image_descriptions.rb b/plugins/discourse-ai/lib/post_image_descriptions.rb
new file mode 100644
index 00000000000..306cae5e7a8
--- /dev/null
+++ b/plugins/discourse-ai/lib/post_image_descriptions.rb
@@ -0,0 +1,678 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module PostImageDescriptions
+ DESCRIPTION_CLASS = "ai-image-description"
+ DESCRIPTION_ID_PREFIX = "ai-image-description"
+ ARIA_DESCRIPTION_ATTRIBUTE = "aria-description"
+ LOOKUP_INDEX = "idx_ai_post_image_descriptions_lookup"
+ REUSE_INDEX = "idx_ai_post_image_descriptions_reuse"
+ MAX_ATTEMPTS = 3
+ RETRY_AFTER = 1.day
+ COOK_ENQUEUE_POST_MAX_AGE = 1.day
+ BACKFILL_RUNS_PER_HOUR = 4
+ SUPPORTED_EXTENSIONS = %w[jpg jpeg png gif webp].freeze
+ MAX_DESCRIPTION_LENGTH = 1_000
+
+ module_function
+
+ def enabled?
+ SiteSetting.discourse_ai_enabled && SiteSetting.ai_post_image_descriptions_enabled &&
+ SiteSetting.ai_helper_image_caption_agent.present?
+ end
+
+ def generation_enabled?
+ return false if !enabled?
+
+ agent = image_caption_agent
+ return false if agent.blank? || !agent.enabled?
+
+ llm_model = image_caption_llm_model(agent)
+
+ agent.class_instance&.vision_enabled && llm_model&.vision_enabled?
+ end
+
+ def process_cooked(doc, post, locale:)
+ remove_existing_description_metadata(doc)
+ return if !enabled?
+ return if !captionable_post?(post)
+
+ base62_sha1s = image_base62_sha1s(doc, post_id: post.id)
+ base62_sha1s = capped_base62_sha1s(base62_sha1s)
+
+ delete_removed_descriptions(post, locale, base62_sha1s)
+
+ return if base62_sha1s.blank?
+
+ descriptions = descriptions_for(post.id, locale, base62_sha1s)
+ exact_description_base62_sha1s = descriptions.keys
+ descriptions = descriptions_with_locale_fallback(post, locale, base62_sha1s, descriptions)
+ decorate(doc, descriptions:, locale:)
+ enqueue_missing(
+ post,
+ locale: locale,
+ base62_sha1s: base62_sha1s,
+ existing_base62_sha1s: exact_description_base62_sha1s,
+ )
+ end
+
+ def decorate(doc, descriptions:, locale:)
+ return if descriptions.blank?
+
+ image_nodes(doc).each do |img|
+ base62_sha1 = image_node_base62_sha1(img)
+ description = descriptions[base62_sha1]
+ next if description.blank?
+
+ aria_description = aria_description(description, locale)
+ img[ARIA_DESCRIPTION_ATTRIBUTE] = aria_description
+ end
+ end
+
+ def enqueue_missing(post, locale:, base62_sha1s:, existing_base62_sha1s: nil)
+ return if base62_sha1s.blank?
+ return if !generation_enabled?
+ return if post.updated_at < COOK_ENQUEUE_POST_MAX_AGE.ago
+
+ missing_base62_sha1s = pending_base62_sha1s(post.id, locale, base62_sha1s)
+ missing_base62_sha1s -= existing_base62_sha1s if existing_base62_sha1s
+ return if missing_base62_sha1s.blank?
+
+ Jobs.enqueue(
+ :generate_post_image_descriptions,
+ post_id: post.id,
+ locale: locale,
+ base62_sha1s: missing_base62_sha1s,
+ )
+ end
+
+ def generate_missing(post, locale:, base62_sha1s: nil)
+ return 0 if !generation_enabled?
+ return 0 if !captionable_post?(post)
+ return 0 if !credits_available?
+
+ current_base62_sha1s =
+ image_base62_sha1s(Nokogiri::HTML5.fragment(post.cooked), post_id: post.id)
+ current_base62_sha1s = capped_base62_sha1s(current_base62_sha1s)
+ current_base62_sha1s &= base62_sha1s if base62_sha1s.present?
+ if current_base62_sha1s.blank? && base62_sha1s.blank?
+ record_uncaptionable_backfill_candidate(post, locale)
+ end
+ return 0 if current_base62_sha1s.blank?
+
+ missing_base62_sha1s = pending_base62_sha1s(post.id, locale, current_base62_sha1s)
+ return 0 if missing_base62_sha1s.blank?
+
+ uploads_by_base62_sha1 = uploads_by_base62_sha1(missing_base62_sha1s)
+
+ generated_count =
+ reuse_descriptions(post, locale, missing_base62_sha1s, uploads_by_base62_sha1)
+ missing_base62_sha1s = pending_base62_sha1s(post.id, locale, current_base62_sha1s)
+ return 0 if generated_count == 0 && missing_base62_sha1s.blank?
+
+ assistant = DiscourseAi::AiHelper::Assistant.new if missing_base62_sha1s.present?
+
+ missing_base62_sha1s.each do |base62_sha1|
+ upload = uploads_by_base62_sha1[base62_sha1]
+ next if upload.blank?
+
+ if !captionable_upload?(upload, post)
+ record_attempt(post, upload, base62_sha1, locale, error: "upload_not_captionable")
+ next
+ end
+
+ begin
+ description =
+ assistant.generate_image_caption(
+ upload,
+ Discourse.system_user,
+ locale: locale,
+ post: post,
+ skip_access_check: true,
+ )
+
+ if description.blank?
+ record_attempt(post, upload, base62_sha1, locale, error: "blank_response")
+ else
+ record_attempt(post, upload, base62_sha1, locale, description: description)
+ generated_count += 1
+ end
+ rescue StandardError => e
+ record_attempt(post, upload, base62_sha1, locale, error: e.message)
+ end
+ end
+
+ return 0 if generated_count == 0
+
+ SearchIndexer.index(post, force: true)
+ refresh_cooked(post, locale)
+
+ generated_count
+ end
+
+ def append_to_search_text(text, post_id, cooked, locale: SiteSetting.default_locale)
+ return text if !enabled?
+ return text if cooked.blank? || !cooked.include?("data-base62-sha1")
+
+ locale = locale.presence || SiteSetting.default_locale
+
+ base62_sha1s = image_base62_sha1s(Nokogiri::HTML5.fragment(cooked), post_id: post_id)
+ base62_sha1s = capped_base62_sha1s(base62_sha1s)
+ return text if base62_sha1s.blank?
+
+ descriptions =
+ AiPostImageDescription
+ .where(post_id: post_id, locale: locale, base62_sha1: base62_sha1s)
+ .where.not(description: nil)
+ .distinct
+ .pluck(:description)
+
+ return text if descriptions.blank?
+
+ "#{text} #{descriptions.join(" ")}"
+ end
+
+ def editable_descriptions(post, locale)
+ return [] if !enabled?
+ return [] if !captionable_post?(post)
+
+ locale = locale.presence || original_locale(post)
+ base62_sha1s = current_base62_sha1s(post)
+ return [] if base62_sha1s.blank?
+
+ descriptions = descriptions_for(post.id, locale, base62_sha1s)
+
+ base62_sha1s.filter_map do |base62_sha1|
+ description = descriptions[base62_sha1]
+ next if description.blank?
+
+ { base62_sha1: base62_sha1, description: description }
+ end
+ end
+
+ def update_description(post, locale, base62_sha1, description)
+ return if !enabled?
+ return if !captionable_post?(post)
+ return if !current_base62_sha1s(post).include?(base62_sha1)
+
+ locale = locale.presence || original_locale(post)
+
+ image_description =
+ AiPostImageDescription.find_by(post_id: post.id, locale: locale, base62_sha1: base62_sha1)
+
+ return if image_description.blank? || image_description.description.blank?
+
+ image_description.update!(description: description, last_error: nil)
+ SearchIndexer.index(post, force: true)
+ refresh_cooked(post, locale)
+
+ image_description
+ end
+
+ def backfill_limit
+ hourly_rate = SiteSetting.ai_post_image_descriptions_backfill_hourly_rate.to_i
+ return 0 if hourly_rate <= 0
+
+ used_budget = DB.query_single(<<~SQL, threshold: 1.hour.ago).first.to_i
+ SELECT COUNT(*)
+ FROM (
+ SELECT DISTINCT post_id, locale
+ FROM ai_post_image_descriptions
+ WHERE created_at > :threshold OR last_attempted_at > :threshold
+ ) recent_targets
+ SQL
+ remaining_budget = hourly_rate - used_budget
+ return 0 if remaining_budget <= 0
+
+ per_run_limit = [hourly_rate / BACKFILL_RUNS_PER_HOUR, 1].max
+ [per_run_limit, remaining_budget].min
+ end
+
+ def backfill_targets(limit:)
+ return [] if limit.to_i <= 0
+
+ connection = ActiveRecord::Base.connection
+ default_locale = connection.quote(SiteSetting.default_locale)
+ retry_after = connection.quote(RETRY_AFTER.ago)
+ max_age_days = SiteSetting.ai_post_image_descriptions_backfill_max_age_days.to_i
+ original_locale_sql = "COALESCE(NULLIF(posts.locale, ''), #{default_locale})"
+ supported_extensions =
+ SUPPORTED_EXTENSIONS.map { |extension| connection.quote(extension) }.join(", ")
+ localized_targets_sql =
+ if SiteSetting.content_localization_enabled
+ <<~SQL
+ UNION ALL
+
+ SELECT posts.id AS post_id,
+ posts.image_upload_id AS upload_id,
+ post_localizations.locale AS locale
+ FROM posts
+ INNER JOIN topics ON topics.id = posts.topic_id
+ INNER JOIN uploads ON uploads.id = posts.image_upload_id
+ INNER JOIN post_localizations ON post_localizations.post_id = posts.id
+ WHERE posts.post_type = #{Post.types[:regular]}
+ AND posts.deleted_at IS NULL
+ AND topics.deleted_at IS NULL
+ AND topics.archetype <> #{connection.quote(Archetype.private_message)}
+ AND posts.created_at > #{connection.quote(max_age_days.days.ago)}
+ AND LOWER(uploads.extension) IN (#{supported_extensions})
+ AND post_localizations.locale <> #{original_locale_sql}
+ SQL
+ else
+ ""
+ end
+
+ DB
+ .query(<<~SQL, limit: limit.to_i)
+ WITH candidate_targets AS (
+ SELECT posts.id AS post_id,
+ posts.image_upload_id AS upload_id,
+ #{original_locale_sql} AS locale
+ FROM posts
+ INNER JOIN topics ON topics.id = posts.topic_id
+ INNER JOIN uploads ON uploads.id = posts.image_upload_id
+ WHERE posts.post_type = #{Post.types[:regular]}
+ AND posts.deleted_at IS NULL
+ AND topics.deleted_at IS NULL
+ AND topics.archetype <> #{connection.quote(Archetype.private_message)}
+ AND posts.created_at > #{connection.quote(max_age_days.days.ago)}
+ AND LOWER(uploads.extension) IN (#{supported_extensions})
+
+ #{localized_targets_sql}
+ )
+
+ SELECT candidate_targets.post_id,
+ candidate_targets.locale
+ FROM candidate_targets
+ LEFT JOIN ai_post_image_descriptions existing_descriptions
+ ON existing_descriptions.post_id = candidate_targets.post_id
+ AND existing_descriptions.upload_id = candidate_targets.upload_id
+ AND existing_descriptions.locale = candidate_targets.locale
+ AND (
+ existing_descriptions.description IS NOT NULL OR
+ existing_descriptions.attempts >= #{MAX_ATTEMPTS} OR
+ existing_descriptions.last_attempted_at > #{retry_after}
+ )
+ WHERE existing_descriptions.post_id IS NULL
+ OR EXISTS (
+ SELECT 1
+ FROM ai_post_image_descriptions retryable_descriptions
+ WHERE retryable_descriptions.post_id = candidate_targets.post_id
+ AND retryable_descriptions.locale = candidate_targets.locale
+ AND retryable_descriptions.description IS NULL
+ AND retryable_descriptions.attempts < #{MAX_ATTEMPTS}
+ AND (
+ retryable_descriptions.last_attempted_at IS NULL OR
+ retryable_descriptions.last_attempted_at <= #{retry_after}
+ )
+ )
+ GROUP BY candidate_targets.post_id, candidate_targets.locale
+ ORDER BY candidate_targets.post_id DESC, candidate_targets.locale
+ LIMIT :limit
+ SQL
+ .map { |target| { post_id: target.post_id, locale: target.locale } }
+ end
+
+ def original_locale(post)
+ post.locale.presence || SiteSetting.default_locale
+ end
+
+ def image_nodes(doc)
+ nodes = doc.css("img[data-base62-sha1]")
+ nodes -= doc.css(".quote img")
+ nodes -= doc.css(".onebox img, .onebox-body img")
+ nodes -= doc.css("img.avatar, img.emoji, img.site-icon, img.onebox-avatar")
+ nodes -= doc.css("img.onebox-avatar-inline")
+ nodes.select { |img| img["data-base62-sha1"].present? }
+ end
+
+ def image_base62_sha1s(doc, post_id: nil)
+ sha1s_by_base62_sha1 =
+ image_nodes(doc).each_with_object({}) do |img, result|
+ base62_sha1 = image_node_base62_sha1(img)
+ next if base62_sha1.blank?
+
+ result[base62_sha1] = sha1_from_base62_sha1(base62_sha1)
+ end
+
+ return [] if sha1s_by_base62_sha1.blank?
+
+ if post_id.present?
+ post_upload_sha1s =
+ Upload
+ .joins(:upload_references)
+ .where(sha1: sha1s_by_base62_sha1.values)
+ .where(upload_references: { target_type: "Post", target_id: post_id })
+ .pluck(:sha1)
+ .to_set
+
+ sha1s_by_base62_sha1.select! { |_, sha1| post_upload_sha1s.include?(sha1) }
+ end
+
+ sha1s_by_base62_sha1.keys
+ end
+
+ def descriptions_for(post_id, locale, base62_sha1s)
+ AiPostImageDescription
+ .where(post_id: post_id, locale: locale, base62_sha1: base62_sha1s)
+ .where.not(description: nil)
+ .pluck(:base62_sha1, :description)
+ .to_h
+ end
+
+ def descriptions_with_locale_fallback(post, locale, base62_sha1s, descriptions)
+ return descriptions if locale == original_locale(post)
+
+ missing_base62_sha1s = base62_sha1s - descriptions.keys
+ return descriptions if missing_base62_sha1s.blank?
+
+ descriptions_for(post.id, original_locale(post), missing_base62_sha1s).merge(descriptions)
+ end
+
+ def capped_base62_sha1s(base62_sha1s)
+ base62_sha1s.first(SiteSetting.ai_post_image_descriptions_per_post_limit.to_i)
+ end
+
+ def current_base62_sha1s(post)
+ return [] if post.cooked.blank?
+
+ base62_sha1s = image_base62_sha1s(Nokogiri::HTML5.fragment(post.cooked), post_id: post.id)
+ capped_base62_sha1s(base62_sha1s)
+ end
+
+ def pending_base62_sha1s(post_id, locale, base62_sha1s)
+ rows =
+ AiPostImageDescription
+ .where(post_id: post_id, locale: locale, base62_sha1: base62_sha1s)
+ .pluck(:base62_sha1, :description, :attempts, :last_attempted_at)
+ .index_by(&:first)
+
+ base62_sha1s.select do |base62_sha1|
+ row = rows[base62_sha1]
+ row.blank? || retryable_row?(row)
+ end
+ end
+
+ def retryable_row?(row)
+ _, description, attempts, last_attempted_at = row
+ return false if description.present?
+ return false if attempts.to_i >= MAX_ATTEMPTS
+
+ last_attempted_at.blank? || last_attempted_at <= RETRY_AFTER.ago
+ end
+
+ def uploads_by_base62_sha1(base62_sha1s)
+ sha1s_by_base62_sha1 =
+ base62_sha1s.each_with_object({}) do |base62_sha1, result|
+ sha1 = sha1_from_base62_sha1(base62_sha1)
+ result[base62_sha1] = sha1 if sha1.present?
+ end
+
+ uploads_by_sha1 = Upload.where(sha1: sha1s_by_base62_sha1.values).index_by(&:sha1)
+
+ sha1s_by_base62_sha1.transform_values { |sha1| uploads_by_sha1[sha1] }.compact
+ end
+
+ def reuse_descriptions(post, locale, base62_sha1s, uploads_by_base62_sha1)
+ reusable_descriptions = reusable_descriptions_for(post.id, locale, base62_sha1s)
+ return 0 if reusable_descriptions.blank?
+
+ reused_count = 0
+
+ reusable_descriptions.each do |base62_sha1, description|
+ upload = uploads_by_base62_sha1[base62_sha1]
+ next if upload.blank? || !captionable_upload?(upload, post)
+
+ record_reused_description(post, upload, base62_sha1, locale, description)
+ reused_count += 1
+ end
+
+ reused_count
+ end
+
+ def reusable_descriptions_for(post_id, locale, base62_sha1s)
+ DB
+ .query(<<~SQL, post_id: post_id, locale: locale, base62_sha1s: base62_sha1s)
+ SELECT DISTINCT ON (base62_sha1) base62_sha1, description
+ FROM ai_post_image_descriptions
+ WHERE locale = :locale
+ AND base62_sha1 IN (:base62_sha1s)
+ AND post_id <> :post_id
+ AND description IS NOT NULL
+ ORDER BY base62_sha1, updated_at DESC, id DESC
+ SQL
+ .map { |row| [row.base62_sha1, row.description] }
+ .to_h
+ end
+
+ def captionable_upload?(upload, post)
+ return false if upload.blank?
+ return false if !SUPPORTED_EXTENSIONS.include?(upload.extension.to_s.downcase)
+ return false if upload.secure? && upload.access_control_post_id != post.id
+
+ true
+ end
+
+ def captionable_post?(post)
+ post.present? && post.raw.present? && post.deleted_at.blank? && post.topic.present? &&
+ post.topic.deleted_at.blank? && post.post_type == Post.types[:regular] &&
+ !post.topic.private_message?
+ end
+
+ def record_uncaptionable_backfill_candidate(post, locale)
+ upload = Upload.find_by(id: post.image_upload_id)
+ return if upload.blank?
+
+ record_attempt(post, upload, upload.base62_sha1, locale, error: "no_post_image_nodes")
+ end
+
+ def record_attempt(post, upload, base62_sha1, locale, description: nil, error: nil)
+ now = Time.zone.now
+
+ DB.exec(
+ <<~SQL,
+ INSERT INTO ai_post_image_descriptions
+ (
+ post_id,
+ upload_id,
+ base62_sha1,
+ locale,
+ description,
+ attempts,
+ last_attempted_at,
+ last_error,
+ created_at,
+ updated_at
+ )
+ VALUES
+ (
+ :post_id,
+ :upload_id,
+ :base62_sha1,
+ :locale,
+ :description,
+ 1,
+ :now,
+ :last_error,
+ :now,
+ :now
+ )
+ ON CONFLICT (post_id, locale, base62_sha1) DO UPDATE SET
+ upload_id = EXCLUDED.upload_id,
+ description = EXCLUDED.description,
+ attempts = ai_post_image_descriptions.attempts + 1,
+ last_attempted_at = EXCLUDED.last_attempted_at,
+ last_error = EXCLUDED.last_error,
+ updated_at = EXCLUDED.updated_at
+ SQL
+ post_id: post.id,
+ upload_id: upload.id,
+ base62_sha1: base62_sha1,
+ locale: locale,
+ description: description,
+ last_error: error,
+ now: now,
+ )
+ end
+
+ def record_reused_description(post, upload, base62_sha1, locale, description)
+ now = Time.zone.now
+
+ DB.exec(
+ <<~SQL,
+ INSERT INTO ai_post_image_descriptions
+ (
+ post_id,
+ upload_id,
+ base62_sha1,
+ locale,
+ description,
+ attempts,
+ created_at,
+ updated_at
+ )
+ VALUES
+ (
+ :post_id,
+ :upload_id,
+ :base62_sha1,
+ :locale,
+ :description,
+ 0,
+ :now,
+ :now
+ )
+ ON CONFLICT (post_id, locale, base62_sha1) DO UPDATE SET
+ upload_id = EXCLUDED.upload_id,
+ description = EXCLUDED.description,
+ attempts = 0,
+ last_attempted_at = NULL,
+ last_error = NULL,
+ updated_at = EXCLUDED.updated_at
+ SQL
+ post_id: post.id,
+ upload_id: upload.id,
+ base62_sha1: base62_sha1,
+ locale: locale,
+ description: description,
+ now: now,
+ )
+ end
+
+ def image_caption_agent
+ AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent.to_i)
+ end
+
+ def image_caption_llm_model(agent = image_caption_agent)
+ return if agent.blank?
+
+ DiscourseAi::AiHelper::Assistant.find_ai_helper_model(
+ DiscourseAi::AiHelper::Assistant::IMAGE_CAPTION,
+ agent.class_instance,
+ )
+ end
+
+ def credits_available?
+ LlmCreditAllocation.credits_available?(image_caption_llm_model)
+ end
+
+ def delete_for_post(post_id)
+ AiPostImageDescription.where(post_id: post_id).delete_all
+ end
+
+ def aria_description(description, locale)
+ I18n.with_locale(locale) do
+ I18n.t("discourse_ai.ai_helper.image_caption.aria_description", description: description)
+ end
+ end
+
+ def image_node_base62_sha1(img)
+ base62_sha1 = img["data-base62-sha1"]
+ sha1 = sha1_from_base62_sha1(base62_sha1)
+ return if sha1.blank?
+ return if image_node_upload_sha1(img) != sha1
+
+ base62_sha1
+ end
+
+ def sha1_from_base62_sha1(base62_sha1)
+ return if base62_sha1.blank?
+ return if base62_sha1.length > Upload::MAX_BASE62_SHA1_LENGTH
+ return if !base62_sha1.match?(/\A[0-9a-zA-Z]+\z/)
+
+ Upload.sha1_from_base62_encoded(base62_sha1)
+ end
+
+ def image_node_upload_sha1(img)
+ src = img["src"].to_s
+
+ if src.end_with?("/images/transparent.png") && img["data-orig-src"].present?
+ src = img["data-orig-src"]
+ end
+
+ return Upload.sha1_from_short_url(src) if src.start_with?("upload://")
+
+ path =
+ begin
+ URI(UrlHelper.unencode(src))&.path
+ rescue URI::Error
+ src
+ end
+
+ return if path.blank?
+
+ OptimizedImage.extract_sha1(path) || Upload.extract_sha1(path) ||
+ Upload.sha1_from_short_path(path)
+ end
+
+ def delete_removed_descriptions(post, locale, base62_sha1s)
+ scope = AiPostImageDescription.where(post_id: post.id)
+ scope = scope.where(locale: locale) if locale != original_locale(post)
+ scope = scope.where.not(base62_sha1: base62_sha1s) if base62_sha1s.present?
+ scope.delete_all
+ end
+
+ def remove_existing_description_metadata(doc)
+ doc.css("span.#{DESCRIPTION_CLASS}").remove
+
+ doc.css("[aria-describedby]").each { |node| remove_ai_describedby(node) }
+
+ doc
+ .css("img[data-base62-sha1][#{ARIA_DESCRIPTION_ATTRIBUTE}]")
+ .each do |img|
+ lightbox = img.ancestors("a.lightbox").first
+
+ img.remove_attribute(ARIA_DESCRIPTION_ATTRIBUTE)
+ lightbox.remove_attribute(ARIA_DESCRIPTION_ATTRIBUTE) if lightbox
+ end
+ end
+
+ def remove_ai_describedby(node)
+ ids =
+ node["aria-describedby"].to_s.split.reject { |id| id.start_with?(DESCRIPTION_ID_PREFIX) }
+
+ if ids.present?
+ node["aria-describedby"] = ids.join(" ")
+ else
+ node.remove_attribute("aria-describedby")
+ end
+ end
+
+ def refresh_cooked(post, locale)
+ if locale == original_locale(post)
+ post.rebake!(skip_publish_rebaked_changes: true)
+ else
+ PostLocalization
+ .where(post_id: post.id, locale: locale)
+ .pluck(:id)
+ .each do |post_localization_id|
+ Jobs.enqueue(
+ :process_localized_cooked,
+ post_localization_id: post_localization_id,
+ recook: true,
+ )
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index 6ddf4a1c9f1..2bd6de3cb24 100644
--- a/plugins/discourse-ai/plugin.rb
+++ b/plugins/discourse-ai/plugin.rb
@@ -156,6 +156,7 @@ after_initialize do
on(:post_destroyed) do |post|
if !Post.with_deleted.exists?(post.id)
DiscourseAi::AiApiAuditLogCleaner.delete_for_post(post.id)
+ DiscourseAi::PostImageDescriptions.delete_for_post(post.id)
end
end
@@ -191,6 +192,26 @@ after_initialize do
end
end
+ on(:post_process_cooked) do |doc, post|
+ DiscourseAi::PostImageDescriptions.process_cooked(
+ doc,
+ post,
+ locale: DiscourseAi::PostImageDescriptions.original_locale(post),
+ )
+ end
+
+ on(:post_process_localized_cooked) do |doc, post, post_localization|
+ DiscourseAi::PostImageDescriptions.process_cooked(doc, post, locale: post_localization.locale)
+ end
+
+ register_modifier(:post_search_index_text) do |text, post_id, cooked, locale|
+ DiscourseAi::PostImageDescriptions.append_to_search_text(text, post_id, cooked, locale: locale)
+ end
+
+ on(:reduce_cooked) do |doc, _post|
+ DiscourseAi::PostImageDescriptions.remove_existing_description_metadata(doc)
+ end
+
add_api_key_scope(:ai, { update_agents: { actions: %w[discourse_ai/admin/ai_agents#update] } })
add_api_key_scope(
diff --git a/plugins/discourse-ai/spec/db/migrate/20260708051450_remove_image_caption_from_ai_helper_enabled_features_spec.rb b/plugins/discourse-ai/spec/db/migrate/20260708051450_remove_image_caption_from_ai_helper_enabled_features_spec.rb
new file mode 100644
index 00000000000..d0c991f9acd
--- /dev/null
+++ b/plugins/discourse-ai/spec/db/migrate/20260708051450_remove_image_caption_from_ai_helper_enabled_features_spec.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+require Rails.root.join(
+ "plugins/discourse-ai/db/migrate/20260708051450_remove_image_caption_from_ai_helper_enabled_features",
+ )
+
+describe RemoveImageCaptionFromAiHelperEnabledFeatures do
+ subject(:migration) { described_class.new }
+
+ let(:connection) { ActiveRecord::Base.connection }
+
+ around { |example| ActiveRecord::Migration.suppress_messages { example.run } }
+
+ before { delete_setting }
+
+ after { delete_setting }
+
+ def delete_setting
+ connection.execute("DELETE FROM site_settings WHERE name = 'ai_helper_enabled_features'")
+ end
+
+ def store_enabled_features(value)
+ connection.execute <<~SQL
+ INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
+ VALUES ('ai_helper_enabled_features', 20, '#{value}', NOW(), NOW())
+ SQL
+ end
+
+ def enabled_features
+ DB.query_single(
+ "SELECT value FROM site_settings WHERE name = 'ai_helper_enabled_features'",
+ ).first
+ end
+
+ it "removes the image caption feature from stored helper features", :aggregate_failures do
+ store_enabled_features("suggestions|image_caption|context_menu")
+
+ migration.up
+
+ expect(enabled_features).to eq("suggestions|context_menu")
+
+ delete_setting
+ store_enabled_features("image_caption")
+
+ migration.up
+
+ expect(enabled_features).to eq("")
+ end
+end
diff --git a/plugins/discourse-ai/spec/jobs/regular/generate_post_image_descriptions_spec.rb b/plugins/discourse-ai/spec/jobs/regular/generate_post_image_descriptions_spec.rb
new file mode 100644
index 00000000000..22293a40d0b
--- /dev/null
+++ b/plugins/discourse-ai/spec/jobs/regular/generate_post_image_descriptions_spec.rb
@@ -0,0 +1,356 @@
+# frozen_string_literal: true
+
+describe Jobs::GeneratePostImageDescriptions do
+ fab!(:upload) do
+ UploadCreator.new(
+ file_from_fixtures(
+ "100x100.jpg",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "caption-image.jpg",
+ ).create_for(Discourse.system_user.id)
+ end
+ fab!(:post) { Fabricate(:post, raw: "![searchable image|200x200](#{upload.short_url})") }
+
+ before do
+ enable_current_plugin
+ llm_model = assign_fake_provider_to(:ai_default_llm_model)
+ llm_model.update!(vision_enabled: true)
+ AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent).update!(
+ enabled: true,
+ vision_enabled: true,
+ )
+ SiteSetting.ai_post_image_descriptions_enabled = true
+ SiteSetting.ai_helper_enabled = true
+ SearchIndexer.enable
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ end
+
+ after { SearchIndexer.disable }
+
+ it "stores descriptions, reindexes, and queues a rebake", :aggregate_failures do
+ description = "A cat beside 字 on a table"
+
+ prompts = nil
+
+ expect_enqueued_with(job: :process_post, args: { post_id: post.id }) do
+ DiscourseAi::Completions::Llm.with_prepared_responses([description]) do |_, _, llm_prompts|
+ prompts = llm_prompts
+
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+ end
+
+ image_description = AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id)
+ prompt = prompts.first
+
+ expect(image_description.description).to eq(description)
+ expect(post.reload.post_search_data.raw_data).to include(description)
+ expect(prompt.post_id).to eq(post.id)
+ expect(prompt.topic_id).to eq(post.topic_id)
+ end
+
+ it "reuses a same-image same-locale description before calling the model", :aggregate_failures do
+ existing_post = Fabricate(:post, raw: "![same image|200x200](#{upload.short_url})")
+ existing_post.update_column(
+ :cooked,
+ existing_post.cook(existing_post.raw, topic_id: existing_post.topic_id),
+ )
+ existing_post.link_post_uploads
+
+ AiPostImageDescription.create!(
+ post_id: existing_post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: "A reusable description",
+ attempts: 1,
+ )
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do |canned_response|
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+
+ expect(canned_response.completions).to eq(0)
+ end
+
+ image_description = AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id)
+
+ expect(image_description.description).to eq("A reusable description")
+ expect(image_description.attempts).to eq(0)
+ end
+
+ it "replaces retryable rows with reused descriptions", :aggregate_failures do
+ existing_post = Fabricate(:post, raw: "![same image|200x200](#{upload.short_url})")
+ existing_post.update_column(
+ :cooked,
+ existing_post.cook(existing_post.raw, topic_id: existing_post.topic_id),
+ )
+ existing_post.link_post_uploads
+
+ AiPostImageDescription.create!(
+ post_id: existing_post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: "A reusable retry description",
+ attempts: 1,
+ )
+
+ AiPostImageDescription.create!(
+ post_id: post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: nil,
+ attempts: 1,
+ last_attempted_at: 2.days.ago,
+ last_error: "rate limited",
+ )
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do |canned_response|
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+
+ expect(canned_response.completions).to eq(0)
+ end
+
+ image_description = AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id)
+
+ expect(image_description.description).to eq("A reusable retry description")
+ expect(image_description.attempts).to eq(0)
+ expect(image_description.last_attempted_at).to be_nil
+ expect(image_description.last_error).to be_nil
+ end
+
+ it "ignores images that are no longer in the post" do
+ post.update_column(:cooked, "<p>No image</p>")
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+
+ expect(AiPostImageDescription.exists?(post_id: post.id)).to eq(false)
+ end
+
+ it "records uncaptionable backfill candidates" do
+ post.update_columns(cooked: "<p>No image</p>", image_upload_id: upload.id)
+
+ described_class.new.execute(post_id: post.id, locale: SiteSetting.default_locale)
+
+ image_description = AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id)
+
+ expect(image_description.description).to be_nil
+ expect(image_description.attempts).to eq(1)
+ expect(image_description.last_error).to eq("no_post_image_nodes")
+ end
+
+ it "records blank responses without retrying immediately", :aggregate_failures do
+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+
+ image_description = AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id)
+
+ expect(image_description.description).to be_nil
+ expect(image_description.attempts).to eq(1)
+ expect(image_description.last_error).to eq("blank_response")
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["second attempt"]) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+
+ image_description = AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id)
+
+ expect(image_description.description).to be_nil
+ expect(image_description.attempts).to eq(1)
+ end
+
+ it "updates retryable attempt rows", :aggregate_failures do
+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+
+ AiPostImageDescription.where(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1: upload.base62_sha1,
+ ).update_all(last_attempted_at: 2.days.ago)
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["second attempt"]) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+
+ image_description =
+ AiPostImageDescription.find_by(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1: upload.base62_sha1,
+ )
+
+ expect(image_description.description).to eq("second attempt")
+ expect(image_description.attempts).to eq(2)
+ expect(image_description.last_error).to be_nil
+ end
+
+ it "limits generated descriptions per post", :aggregate_failures do
+ second_upload =
+ UploadCreator.new(
+ file_from_fixtures(
+ "An image of discobot in action.png",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "second-image.png",
+ ).create_for(Discourse.system_user.id)
+
+ SiteSetting.ai_post_image_descriptions_per_post_limit = 1
+ post.update!(
+ raw:
+ "![first image|200x200](#{upload.short_url})\n\n![second image|200x200](#{second_upload.short_url})",
+ )
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ ["first generated description", "unused"],
+ ) do |canned_response|
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1, second_upload.base62_sha1],
+ )
+
+ expect(canned_response.completions).to eq(1)
+ end
+
+ expect(
+ AiPostImageDescription.exists?(
+ post_id: post.id,
+ base62_sha1: upload.base62_sha1,
+ description: "first generated description",
+ ),
+ ).to eq(true)
+ expect(
+ AiPostImageDescription.exists?(post_id: post.id, base62_sha1: second_upload.base62_sha1),
+ ).to eq(false)
+ end
+
+ it "stores generated descriptions before a later image fails", :aggregate_failures do
+ second_upload =
+ UploadCreator.new(
+ file_from_fixtures(
+ "An image of discobot in action.png",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "second-image.png",
+ ).create_for(Discourse.system_user.id)
+
+ post.update!(
+ raw:
+ "![first image|200x200](#{upload.short_url})\n\n![second image|200x200](#{second_upload.short_url})",
+ )
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ ["A generated first image description", StandardError.new("rate limited")],
+ ) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1, second_upload.base62_sha1],
+ )
+ end
+
+ expect(
+ AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id).description,
+ ).to eq("A generated first image description")
+ expect(
+ AiPostImageDescription.find_by(post_id: post.id, upload_id: second_upload.id).last_error,
+ ).to eq("rate limited")
+ end
+
+ it "records non-owned secure uploads without sending them", :aggregate_failures do
+ upload.update!(secure: true, access_control_post: Fabricate(:post))
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do |canned_response|
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+
+ expect(canned_response.completions).to eq(0)
+ end
+
+ image_description = AiPostImageDescription.find_by(post_id: post.id, upload_id: upload.id)
+
+ expect(image_description.description).to be_nil
+ expect(image_description.last_error).to eq("upload_not_captionable")
+ end
+
+ it "skips generation when credits are unavailable" do
+ LlmCreditAllocation.stubs(:credits_available?).returns(false)
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+
+ expect(AiPostImageDescription.exists?(post_id: post.id)).to eq(false)
+ end
+
+ it "keeps localized descriptions out of original search data", :aggregate_failures do
+ description = "A localized 字 caption"
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([description]) do
+ described_class.new.execute(
+ post_id: post.id,
+ locale: "ja",
+ base62_sha1s: [upload.base62_sha1],
+ )
+ end
+
+ expect(
+ AiPostImageDescription.exists?(post_id: post.id, locale: "ja", description: description),
+ ).to eq(true)
+ expect(post.reload.post_search_data.raw_data).not_to include(description)
+ end
+end
diff --git a/plugins/discourse-ai/spec/jobs/scheduled/post_image_descriptions_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/post_image_descriptions_backfill_spec.rb
new file mode 100644
index 00000000000..e800537b501
--- /dev/null
+++ b/plugins/discourse-ai/spec/jobs/scheduled/post_image_descriptions_backfill_spec.rb
@@ -0,0 +1,286 @@
+# frozen_string_literal: true
+
+describe Jobs::PostImageDescriptionsBackfill do
+ fab!(:upload) do
+ UploadCreator.new(
+ file_from_fixtures(
+ "100x100.jpg",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "caption-image.jpg",
+ ).create_for(Discourse.system_user.id)
+ end
+ fab!(:post) { Fabricate(:post, raw: "![backfill image|200x200](#{upload.short_url})") }
+
+ before do
+ enable_current_plugin
+ llm_model = assign_fake_provider_to(:ai_default_llm_model)
+ llm_model.update!(vision_enabled: true)
+ AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent).update!(
+ enabled: true,
+ vision_enabled: true,
+ )
+ SiteSetting.ai_post_image_descriptions_enabled = true
+ SiteSetting.ai_helper_enabled = true
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ post.update_column(:image_upload_id, upload.id)
+ end
+
+ def store_description(target_upload)
+ AiPostImageDescription.upsert_all(
+ [
+ {
+ post_id: post.id,
+ upload_id: target_upload.id,
+ base62_sha1: target_upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: "A stored description",
+ attempts: 0,
+ },
+ ],
+ unique_by: DiscourseAi::PostImageDescriptions::LOOKUP_INDEX,
+ )
+ end
+
+ def store_attempt(created_at:, target_post: nil, last_attempted_at: nil)
+ target_post ||= Fabricate(:post)
+
+ AiPostImageDescription.create!(
+ post_id: target_post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ attempts: 1,
+ last_attempted_at: last_attempted_at,
+ created_at: created_at,
+ updated_at: created_at,
+ )
+ end
+
+ it "does nothing when backfill is disabled" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 0
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "does nothing when post image descriptions are disabled" do
+ SiteSetting.ai_post_image_descriptions_enabled = false
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "enqueues posts without existing descriptions" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+
+ expect_enqueued_with(
+ job: :generate_post_image_descriptions,
+ args: {
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ },
+ ) { described_class.new.execute({}) }
+ end
+
+ it "enqueues one post when a low hourly backfill budget is unused" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 1
+
+ expect_enqueued_with(
+ job: :generate_post_image_descriptions,
+ args: {
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ },
+ ) { described_class.new.execute({}) }
+ end
+
+ it "enqueues existing localized posts when localization is enabled" do
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 8
+ Fabricate(:post_localization, post: post, locale: "ja", raw: post.raw, cooked: post.cooked)
+
+ described_class.new.execute({})
+
+ jobs =
+ Jobs::GeneratePostImageDescriptions
+ .jobs
+ .last(2)
+ .map { |job| job["args"].first.slice("post_id", "locale") }
+
+ expect(jobs).to contain_exactly(
+ { "post_id" => post.id, "locale" => SiteSetting.default_locale },
+ { "post_id" => post.id, "locale" => "ja" },
+ )
+ end
+
+ it "does not enqueue posts older than the backfill max age" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+ SiteSetting.ai_post_image_descriptions_backfill_max_age_days = 30
+ post.update_column(:created_at, 31.days.ago)
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "respects the hourly backfill budget" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 1
+ store_attempt(created_at: 10.minutes.ago)
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "counts recent retry attempts against the backfill budget" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 1
+ store_attempt(created_at: 2.days.ago, last_attempted_at: 10.minutes.ago)
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "does not repeatedly enqueue described posts with image attachments" do
+ other_upload =
+ UploadCreator.new(
+ file_from_fixtures(
+ "An image of discobot in action.png",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "other-image.png",
+ ).create_for(Discourse.system_user.id)
+
+ post.update!(
+ raw:
+ "![first image|200x200](#{upload.short_url})\n\n" \
+ "[attached image|attachment](#{other_upload.short_url})",
+ )
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ store_description(upload)
+
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "does not enqueue posts with an original locale description" do
+ store_description(upload)
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "does not immediately re-enqueue recently attempted posts" do
+ AiPostImageDescription.upsert_all(
+ [
+ {
+ post_id: post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: nil,
+ attempts: 1,
+ last_attempted_at: Time.zone.now,
+ last_error: "blank_response",
+ },
+ ],
+ unique_by: DiscourseAi::PostImageDescriptions::LOOKUP_INDEX,
+ )
+
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "re-enqueues posts with retryable image attempts" do
+ other_upload =
+ UploadCreator.new(
+ file_from_fixtures(
+ "An image of discobot in action.png",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "other-image.png",
+ ).create_for(Discourse.system_user.id)
+
+ post.update!(
+ raw:
+ "![first image|200x200](#{upload.short_url})\n\n![second image|200x200](#{other_upload.short_url})",
+ )
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ store_description(upload)
+
+ AiPostImageDescription.upsert_all(
+ [
+ {
+ post_id: post.id,
+ upload_id: other_upload.id,
+ base62_sha1: other_upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: nil,
+ attempts: 1,
+ last_attempted_at: 2.days.ago,
+ last_error: "rate limited",
+ },
+ ],
+ unique_by: DiscourseAi::PostImageDescriptions::LOOKUP_INDEX,
+ )
+
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+
+ expect_enqueued_with(
+ job: :generate_post_image_descriptions,
+ args: {
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ },
+ ) { described_class.new.execute({}) }
+ end
+
+ it "does not enqueue when credits are unavailable" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+ LlmCreditAllocation.stubs(:credits_available?).returns(false)
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+
+ it "uses post image ids for backfill candidates" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+ post.update_column(:cooked, "<p>No upload marker</p>")
+
+ expect_enqueued_with(
+ job: :generate_post_image_descriptions,
+ args: {
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ },
+ ) { described_class.new.execute({}) }
+ end
+
+ it "does not enqueue attachment-only posts" do
+ SiteSetting.ai_post_image_descriptions_backfill_hourly_rate = 4
+ post.update_column(:image_upload_id, nil)
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.new.execute({})
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/post_image_descriptions_spec.rb b/plugins/discourse-ai/spec/lib/post_image_descriptions_spec.rb
new file mode 100644
index 00000000000..aa151fcb2c7
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/post_image_descriptions_spec.rb
@@ -0,0 +1,336 @@
+# frozen_string_literal: true
+
+describe DiscourseAi::PostImageDescriptions do
+ fab!(:upload) { create_image_upload("100x100.jpg", "caption-image.jpg") }
+ fab!(:post) { Fabricate(:post, raw: "![user supplied|200x200](#{upload.short_url})") }
+
+ before do
+ enable_current_plugin
+ llm_model = assign_fake_provider_to(:ai_default_llm_model)
+ llm_model.update!(vision_enabled: true)
+ AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent).update!(
+ enabled: true,
+ vision_enabled: true,
+ )
+ SiteSetting.ai_post_image_descriptions_enabled = true
+ SiteSetting.ai_helper_enabled = true
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ end
+
+ def create_image_upload(filename, original_filename)
+ UploadCreator.new(discourse_ai_image_fixture(filename), original_filename).create_for(
+ Discourse.system_user.id,
+ )
+ end
+
+ def discourse_ai_image_fixture(filename)
+ file_from_fixtures(
+ filename,
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ )
+ end
+
+ def store_description(description, locale: SiteSetting.default_locale, target_upload: upload)
+ AiPostImageDescription.upsert_all(
+ [
+ {
+ post_id: post.id,
+ upload_id: target_upload.id,
+ base62_sha1: target_upload.base62_sha1,
+ locale: locale,
+ description: description,
+ attempts: 0,
+ },
+ ],
+ unique_by: described_class::LOOKUP_INDEX,
+ )
+ end
+
+ it "supports ActiveRecord updates with a primary key" do
+ image_description =
+ AiPostImageDescription.create!(
+ post_id: post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: "An initial description",
+ )
+
+ image_description.update!(description: "An updated description")
+
+ expect(image_description.reload.description).to eq("An updated description")
+ end
+
+ it "adds image description metadata without visible text", :aggregate_failures do
+ description = "A lighthouse beside 字 on a sign"
+ store_description(description)
+
+ processor = CookedPostProcessor.new(post)
+ processor.post_process
+ doc = Nokogiri::HTML5.fragment(processor.html)
+ image = doc.at_css("img[data-base62-sha1='#{upload.base62_sha1}']")
+ lightbox = doc.at_css("a.lightbox")
+
+ expect(post.raw).not_to include(description)
+ expect(image["alt"]).to eq("user supplied")
+ expect(lightbox["title"]).to eq("user supplied")
+ expect(doc.at_css(".meta .filename").text).to eq("user supplied")
+ expect(image["aria-description"]).to eq("#{description} (Captioned by AI)")
+ expect(lightbox["aria-description"]).to be_blank
+ expect(doc.at_css(".ai-image-description")).to be_blank
+ expect(
+ ExcerptParser.to_plain_text(ExcerptParser.get_excerpt(processor.html, 200)),
+ ).not_to include(description)
+ end
+
+ it "strips image description metadata from email HTML", :aggregate_failures do
+ description = "A generated email-only 字 description"
+ store_description(description)
+
+ processor = CookedPostProcessor.new(post)
+ processor.post_process
+ email_html = PrettyText.format_for_email(processor.html, post)
+ email_doc = Nokogiri::HTML5.fragment(email_html)
+
+ expect(email_html).not_to include(description)
+ expect(email_doc.at_css("[aria-description]")).to be_blank
+ end
+
+ it "enqueues generation for post images only" do
+ attachment = create_image_upload("An image of discobot in action.png", "attachment.png")
+
+ post.update!(
+ raw:
+ "![visible image|200x200](#{upload.short_url})\n\n" \
+ "[attached image|attachment](#{attachment.short_url})",
+ )
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+
+ expect_enqueued_with(
+ job: :generate_post_image_descriptions,
+ args: {
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ },
+ ) do
+ processor = CookedPostProcessor.new(post)
+ processor.post_process
+ end
+ end
+
+ it "does not run when post image descriptions are disabled" do
+ SiteSetting.ai_post_image_descriptions_enabled = false
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.process_cooked(Nokogiri::HTML5.fragment(post.cooked), post, locale: "en")
+ end
+ end
+
+ it "does not enqueue generation when the caption agent is disabled" do
+ AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent).update!(enabled: false)
+
+ expect(described_class.generation_enabled?).to eq(false)
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.process_cooked(
+ Nokogiri::HTML5.fragment(post.cooked),
+ post,
+ locale: SiteSetting.default_locale,
+ )
+ end
+ end
+
+ it "does not enqueue generation while cooking old posts" do
+ post.update_columns(created_at: 2.days.ago, updated_at: 2.days.ago)
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ processor = CookedPostProcessor.new(post)
+ processor.post_process
+ end
+ end
+
+ it "enqueues generation for recently edited old posts" do
+ post.update_columns(created_at: 2.days.ago, updated_at: Time.zone.now)
+
+ expect_enqueued_with(
+ job: :generate_post_image_descriptions,
+ args: {
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1s: [upload.base62_sha1],
+ },
+ ) do
+ processor = CookedPostProcessor.new(post)
+ processor.post_process
+ end
+ end
+
+ it "preserves non-AI image aria descriptions when disabled", :aggregate_failures do
+ SiteSetting.ai_post_image_descriptions_enabled = false
+ doc =
+ Nokogiri::HTML5.fragment(
+ "<a class='lightbox' aria-description='custom lightbox'>" \
+ "<img src='https://example.com/image.png' aria-description='custom image'>" \
+ "</a>",
+ )
+
+ described_class.process_cooked(doc, post, locale: SiteSetting.default_locale)
+
+ expect(doc.at_css("img")["aria-description"]).to eq("custom image")
+ expect(doc.at_css("a.lightbox")["aria-description"]).to eq("custom lightbox")
+ end
+
+ it "skips personal messages and whispers" do
+ private_topic = Fabricate(:private_message_topic, user: post.user)
+ pm_post = Fabricate(:post, topic: private_topic, raw: post.raw)
+ pm_post.update_column(:cooked, post.cooked)
+ pm_post.link_post_uploads
+
+ whisper = Fabricate(:post, raw: post.raw, post_type: Post.types[:whisper])
+ whisper.update_column(:cooked, post.cooked)
+ whisper.link_post_uploads
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.process_cooked(
+ Nokogiri::HTML5.fragment(pm_post.cooked),
+ pm_post,
+ locale: "en",
+ )
+ described_class.process_cooked(
+ Nokogiri::HTML5.fragment(whisper.cooked),
+ whisper,
+ locale: "en",
+ )
+ end
+ end
+
+ it "ignores image nodes without post upload references" do
+ unrelated_upload = create_image_upload("An image of discobot in action.png", "unrelated.png")
+
+ expect_not_enqueued_with(job: :generate_post_image_descriptions) do
+ described_class.process_cooked(
+ Nokogiri::HTML5.fragment(
+ "<img src='https://example.com/image.png' data-base62-sha1='#{unrelated_upload.base62_sha1}'>" \
+ "<img src='https://example.com/other.png' data-base62-sha1='@bad'>",
+ ),
+ post,
+ locale: SiteSetting.default_locale,
+ )
+ end
+ end
+
+ it "adds current image descriptions to search text" do
+ default_description = "A searchable 字 description"
+ japanese_description = "A Japanese 字 description"
+ store_description(default_description)
+ store_description(japanese_description, locale: "ja")
+
+ indexed_text = described_class.append_to_search_text("body", post.id, post.cooked)
+ text_without_image = described_class.append_to_search_text("body", post.id, "<p>No image</p>")
+
+ expect(indexed_text).to include(default_description)
+ expect(indexed_text).not_to include(japanese_description)
+ expect(text_without_image).to eq("body")
+ end
+
+ it "does not delete other locale descriptions during localized cooking" do
+ store_description("A default locale description")
+ store_description("A Japanese description", locale: "ja")
+ other_upload = create_image_upload("An image of discobot in action.png", "other-image.png")
+ post.update!(raw: "![other image|200x200](#{other_upload.short_url})")
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+
+ described_class.process_cooked(Nokogiri::HTML5.fragment(post.cooked), post, locale: "ja")
+
+ expect(
+ AiPostImageDescription.exists?(
+ post_id: post.id,
+ locale: SiteSetting.default_locale,
+ base62_sha1: upload.base62_sha1,
+ ),
+ ).to eq(true)
+ expect(
+ AiPostImageDescription.exists?(
+ post_id: post.id,
+ locale: "ja",
+ base62_sha1: upload.base62_sha1,
+ ),
+ ).to eq(false)
+ end
+
+ it "adds descriptions while storing localized cooked", :aggregate_failures do
+ description = "日本語の画像説明"
+ store_description(description, locale: "ja")
+ localization =
+ Fabricate(:post_localization, post: post, locale: "ja", raw: post.raw, cooked: post.cooked)
+
+ Jobs::ProcessLocalizedCooked.new.execute(post_localization_id: localization.id)
+
+ doc = Nokogiri::HTML5.fragment(localization.reload.cooked)
+ image = doc.at_css("img[data-base62-sha1='#{upload.base62_sha1}']")
+
+ expect(image["aria-description"]).to include(description)
+ expect(doc.at_css(".ai-image-description")).to be_blank
+ end
+
+ it "falls back to original descriptions while localized generation is pending",
+ :aggregate_failures do
+ description = "A default locale fallback description"
+ store_description(description)
+ localization =
+ Fabricate(:post_localization, post: post, locale: "ja", raw: post.raw, cooked: post.cooked)
+
+ expect_enqueued_with(
+ job: :generate_post_image_descriptions,
+ args: {
+ post_id: post.id,
+ locale: "ja",
+ base62_sha1s: [upload.base62_sha1],
+ },
+ ) { Jobs::ProcessLocalizedCooked.new.execute(post_localization_id: localization.id) }
+
+ doc = Nokogiri::HTML5.fragment(localization.reload.cooked)
+ image = doc.at_css("img[data-base62-sha1='#{upload.base62_sha1}']")
+
+ expect(image["aria-description"]).to include(description)
+ expect(AiPostImageDescription.exists?(post_id: post.id, locale: "ja")).to eq(false)
+ end
+
+ it "deletes descriptions after all post images are removed" do
+ store_description("A stored description")
+
+ described_class.process_cooked(Nokogiri::HTML5.fragment("<p>No image</p>"), post, locale: "en")
+
+ expect(AiPostImageDescription.exists?(post_id: post.id, base62_sha1: upload.base62_sha1)).to eq(
+ false,
+ )
+ end
+
+ it "keeps stored descriptions when the feature is disabled" do
+ store_description("A stored description")
+ SiteSetting.ai_post_image_descriptions_enabled = false
+
+ described_class.process_cooked(Nokogiri::HTML5.fragment("<p>No image</p>"), post, locale: "en")
+
+ expect(AiPostImageDescription.exists?(post_id: post.id, base62_sha1: upload.base62_sha1)).to eq(
+ true,
+ )
+ end
+
+ it "deletes descriptions only when the post is permanently destroyed", :aggregate_failures do
+ store_description("A stored description")
+ admin = Fabricate(:admin)
+
+ PostDestroyer.new(admin, post).destroy
+
+ expect(AiPostImageDescription.exists?(post_id: post.id)).to eq(true)
+
+ PostDestroyer.new(admin, post.reload, force_destroy: true).destroy
+
+ expect(AiPostImageDescription.exists?(post_id: post.id)).to eq(false)
+ end
+end
diff --git a/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb
index b0a3720a57c..21c54f42400 100644
--- a/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb
@@ -651,204 +651,10 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
end
describe "#caption_image" do
- let(:image) { plugin_file_from_fixtures("100x100.jpg") }
- let(:upload) { UploadCreator.new(image, "image.jpg").create_for(Discourse.system_user.id) }
- let(:image_url) { "#{Discourse.base_url}#{upload.url}" }
- let(:caption) { "A picture of a cat sitting on a table" }
- let(:caption_with_attrs) do
- "A picture of a cat sitting on a table (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})"
- end
- let(:bad_caption) { "A picture of a cat \nsitting on a |table|" }
-
- def request_caption(params, caption = "A picture of a cat sitting on a table")
- DiscourseAi::Completions::Llm.with_prepared_responses([caption]) do
- post "/discourse-ai/ai-helper/caption_image", params: params
-
- yield(response)
- end
- end
-
- context "when logged in as an allowed user" do
- before do
- sign_in(user)
-
- SiteSetting.composer_ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]
- end
+ it "returns not found" do
+ post "/discourse-ai/ai-helper/caption_image"
- it "returns the suggested caption for the image" do
- request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
- expect(r.status).to eq(200)
- expect(r.parsed_body["caption"]).to eq(caption_with_attrs)
- end
- end
-
- it "returns a 403 when the user cannot access the image caption agent" do
- AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent).update!(
- allowed_group_ids: [restricted_group.id],
- )
-
- request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
- expect(r.status).to eq(403)
- expect(r.parsed_body["errors"]).to be_present
- end
- end
-
- it "returns a cleaned up caption from the LLM" do
- request_caption({ image_url: image_url, image_url_type: "long_url" }, bad_caption) do |r|
- expect(r.status).to eq(200)
- expect(r.parsed_body["caption"]).to eq(caption_with_attrs)
- end
- end
-
- it "truncates the caption from the LLM" do
- request_caption({ image_url: image_url, image_url_type: "long_url" }, caption * 10) do |r|
- expect(r.status).to eq(200)
- expect(r.parsed_body["caption"].size).to be < caption.size * 10
- end
- end
-
- context "when the image_url is a short_url" do
- let(:image_url) { upload.short_url }
-
- it "returns the suggested caption for the image" do
- request_caption({ image_url: image_url, image_url_type: "short_url" }) do |r|
- expect(r.status).to eq(200)
- expect(r.parsed_body["caption"]).to eq(caption_with_attrs)
- end
- end
- end
-
- context "when the image_url is a short_path" do
- let(:image_url) { "#{Discourse.base_url}#{upload.short_path}" }
-
- it "returns the suggested caption for the image" do
- request_caption({ image_url: image_url, image_url_type: "short_path" }) do |r|
- expect(r.status).to eq(200)
- expect(r.parsed_body["caption"]).to eq(caption_with_attrs)
- end
- end
- end
-
- it "returns a 502 error when the completion call fails" do
- DiscourseAi::Completions::Llm.with_prepared_responses(
- [DiscourseAi::Completions::Endpoints::Base::CompletionFailed.new],
- ) do
- post "/discourse-ai/ai-helper/caption_image",
- params: {
- image_url: image_url,
- image_url_type: "long_url",
- }
-
- expect(response.status).to eq(502)
- end
- end
-
- it "returns a 400 error when the image_url is blank" do
- post "/discourse-ai/ai-helper/caption_image"
-
- expect(response.status).to eq(400)
- end
-
- it "returns a 404 error if no upload is found" do
- post "/discourse-ai/ai-helper/caption_image",
- params: {
- image_url: "http://blah.com/img.jpeg",
- image_url_type: "long_url",
- }
-
- expect(response.status).to eq(404)
- end
-
- context "for secure uploads" do
- fab!(:group)
- fab!(:private_category) { Fabricate(:private_category, group: group) }
- let(:image) { plugin_file_from_fixtures("100x100.jpg") }
- let(:upload) { UploadCreator.new(image, "image.jpg").create_for(Discourse.system_user.id) }
- let(:image_url) { "#{Discourse.base_url}/secure-uploads/#{upload.url}" }
-
- before do
- Jobs.run_immediately!
-
- # this is done so the after_save callbacks for site settings to make
- # UploadReference records works
- @original_provider = SiteSetting.provider
- SiteSetting.provider = SiteSettings::DbProvider.new(SiteSetting)
- enable_current_plugin
- setup_s3
- stub_s3_store
- SiteSetting.secure_uploads = true
- SiteSetting.composer_ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]
-
- Group.find(SiteSetting.composer_ai_helper_allowed_groups_map.first).add(user)
- user.reload
-
- stub_request(
- :get,
- "http://s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.#{upload.extension}",
- ).to_return(status: 200, body: "", headers: {})
- end
- after { SiteSetting.provider = @original_provider }
-
- it "returns a 403 error if the user cannot access the secure upload" do
- # hosted-site plugin edge case, it enables embeddings
- SiteSetting.ai_embeddings_enabled = false
-
- create_post(
- title: "Secure upload post",
- raw: "This is a new post <img src=\"#{upload.url}\" />",
- category: private_category,
- user: Discourse.system_user,
- )
-
- post "/discourse-ai/ai-helper/caption_image",
- params: {
- image_url: image_url,
- image_url_type: "long_url",
- }
- expect(response.status).to eq(403)
- end
-
- it "returns a 200 message and caption if user can access the secure upload" do
- group.add(user)
-
- request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
- expect(r.status).to eq(200)
- expect(r.parsed_body["caption"]).to eq(caption_with_attrs)
- end
- end
-
- context "if the input URL is for a secure upload but not on the secure-uploads path" do
- let(:image_url) { "#{Discourse.base_url}#{upload.url}" }
-
- it "creates a signed URL properly and makes the caption" do
- group.add(user)
-
- request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
- expect(r.status).to eq(200)
- expect(r.parsed_body["caption"]).to eq(caption_with_attrs)
- end
- end
- end
- end
-
- context "when performing numerous requests" do
- it "rate limits" do
- RateLimiter.enable
-
- rate_limit = described_class::RATE_LIMITS["caption_image"]
- amount = rate_limit[:amount]
-
- amount.times do
- request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
- expect(r.status).to eq(200)
- end
- end
-
- request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
- expect(r.status).to eq(429)
- end
- end
- end
+ expect(response.status).to eq(404)
end
end
diff --git a/plugins/discourse-ai/spec/requests/post_image_descriptions_controller_spec.rb b/plugins/discourse-ai/spec/requests/post_image_descriptions_controller_spec.rb
new file mode 100644
index 00000000000..564d3d0cc3c
--- /dev/null
+++ b/plugins/discourse-ai/spec/requests/post_image_descriptions_controller_spec.rb
@@ -0,0 +1,137 @@
+# frozen_string_literal: true
+
+describe DiscourseAi::PostImageDescriptionsController do
+ fab!(:admin)
+ fab!(:upload) do
+ UploadCreator.new(
+ file_from_fixtures(
+ "100x100.jpg",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "caption-image.jpg",
+ ).create_for(admin.id)
+ end
+ fab!(:post) do
+ Fabricate(:post, user: admin, raw: "![user supplied|200x200](#{upload.short_url})")
+ end
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_post_image_descriptions_enabled = true
+ SearchIndexer.enable
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ end
+
+ after { SearchIndexer.disable }
+
+ def store_description(
+ description,
+ target_post: post,
+ target_upload: upload,
+ locale: SiteSetting.default_locale
+ )
+ AiPostImageDescription.upsert_all(
+ [
+ {
+ post_id: target_post.id,
+ upload_id: target_upload.id,
+ base62_sha1: target_upload.base62_sha1,
+ locale: locale,
+ description: description,
+ attempts: 0,
+ },
+ ],
+ unique_by: DiscourseAi::PostImageDescriptions::LOOKUP_INDEX,
+ )
+ end
+
+ describe "#index" do
+ before { sign_in(admin) }
+
+ it "returns editable descriptions for current post images", :aggregate_failures do
+ description = "A generated 字 description"
+ stale_upload =
+ UploadCreator.new(
+ file_from_fixtures(
+ "1x1.jpg",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "stale-caption-image.jpg",
+ ).create_for(admin.id)
+
+ store_description(description)
+ store_description("A stale description", target_upload: stale_upload)
+
+ get "/discourse-ai/post-image-descriptions/#{post.id}.json"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["descriptions"]).to contain_exactly(
+ { "base62_sha1" => upload.base62_sha1, "description" => description },
+ )
+ end
+
+ it "requires permission to edit the post" do
+ sign_in(Fabricate(:user))
+ store_description("A generated description")
+
+ get "/discourse-ai/post-image-descriptions/#{post.id}.json"
+
+ expect(response.status).to eq(403)
+ end
+ end
+
+ describe "#update" do
+ before { sign_in(admin) }
+
+ it "updates the description and refreshes derived content", :aggregate_failures do
+ store_description("The old description")
+
+ expect_enqueued_with(job: :process_post, args: { post_id: post.id }) do
+ put "/discourse-ai/post-image-descriptions/#{post.id}/#{upload.base62_sha1}.json",
+ params: {
+ description: "An edited 字 description",
+ }
+ end
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body).to include(
+ "base62_sha1" => upload.base62_sha1,
+ "description" => "An edited 字 description",
+ )
+ expect(
+ AiPostImageDescription.find_by(
+ post_id: post.id,
+ base62_sha1: upload.base62_sha1,
+ ).description,
+ ).to eq("An edited 字 description")
+ expect(post.post_search_data.raw_data).to include("An edited 字 description")
+
+ Jobs::ProcessPost.new.execute(post_id: post.id)
+
+ expect(post.reload.cooked).to include("An edited 字 description")
+ end
+
+ it "returns not found for images without an editable row" do
+ put "/discourse-ai/post-image-descriptions/#{post.id}/#{upload.base62_sha1}.json",
+ params: {
+ description: "A new description",
+ }
+
+ expect(response.status).to eq(404)
+ end
+
+ it "validates description length" do
+ store_description("The old description")
+
+ put "/discourse-ai/post-image-descriptions/#{post.id}/#{upload.base62_sha1}.json",
+ params: {
+ description: "x" * (DiscourseAi::PostImageDescriptions::MAX_DESCRIPTION_LENGTH + 1),
+ }
+
+ expect(response.status).to eq(422)
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/requests/post_image_descriptions_rss_spec.rb b/plugins/discourse-ai/spec/requests/post_image_descriptions_rss_spec.rb
new file mode 100644
index 00000000000..47f00f667e3
--- /dev/null
+++ b/plugins/discourse-ai/spec/requests/post_image_descriptions_rss_spec.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+describe "AI post image descriptions in RSS" do
+ fab!(:upload) do
+ UploadCreator.new(
+ file_from_fixtures(
+ "100x100.jpg",
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ "caption-image.jpg",
+ ).create_for(Discourse.system_user.id)
+ end
+
+ fab!(:post) { Fabricate(:post, raw: "![user supplied|200x200](#{upload.short_url})") }
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_post_image_descriptions_enabled = true
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ end
+
+ it "omits generated descriptions from topic feeds", :aggregate_failures do
+ description = "A generated RSS-only 字 description"
+ AiPostImageDescription.upsert_all(
+ [
+ {
+ post_id: post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: SiteSetting.default_locale,
+ description: description,
+ attempts: 0,
+ },
+ ],
+ unique_by: DiscourseAi::PostImageDescriptions::LOOKUP_INDEX,
+ )
+
+ processor = CookedPostProcessor.new(post)
+ processor.post_process
+ post.update_column(:cooked, processor.html)
+
+ get "/t/#{post.topic.slug}/#{post.topic.id}.rss"
+
+ expect(response.status).to eq(200)
+ expect(response.body).not_to include(description)
+ expect(response.body).not_to include("aria-description")
+ end
+end
diff --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.rb
deleted file mode 100644
index 23eb3701d5c..00000000000
--- a/plugins/discourse-ai/spec/system/ai_helper/ai_image_caption_spec.rb
+++ /dev/null
@@ -1,92 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe "AI image caption" do
- fab!(:user) { Fabricate(:admin, refresh_auto_groups: true) }
- fab!(:non_member_group, :group)
- let(:user_preferences_ai_page) { PageObjects::Pages::UserPreferencesAi.new }
- let(:topic_page) { PageObjects::Pages::Topic.new }
- fab!(:topic)
- fab!(:post) { Fabricate(:post, topic: topic) }
- let(:composer) { PageObjects::Components::Composer.new }
- let(:popup) { PageObjects::Components::AiCaptionPopup.new }
- let(:dialog) { PageObjects::Components::Dialog.new }
- let(:file_path) { plugin_file_from_fixtures("100x100.jpg").path }
- let(:captioned_image_path) do
- plugin_file_from_fixtures("An image of discobot in action.png").path
- end
- let(:caption) do
- "The image shows a stylized speech bubble icon with a multicolored border on a black background."
- end
- let(:caption_with_attrs) do
- "#{caption} (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})"
- end
-
- before do
- enable_current_plugin
- Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user)
- assign_fake_provider_to(:ai_default_llm_model)
- SiteSetting.ai_helper_enabled = true
- SiteSetting.ai_helper_enabled_features = "image_caption"
- sign_in(user)
- end
-
- shared_examples "shows no image caption button" do
- it "should not show an image caption button" 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? }
- expect(popup).to have_no_generate_caption_button
- end
- end
-
- context "when not a member of ai helper group" do
- before { SiteSetting.composer_ai_helper_allowed_groups = non_member_group.id.to_s }
- include_examples "shows no image caption button"
- end
-
- context "when image caption feature is disabled" do
- before { SiteSetting.ai_helper_enabled_features = "" }
- include_examples "shows no image caption button"
- end
-
- context "when triggering caption with AI on desktop" do
- xit "should show an image caption in an input field" 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)
- popup.click_generate_caption
- expect(popup.has_caption_popup_value?(caption_with_attrs)).to eq(true)
- popup.save_caption
- wait_for { page.find(".image-wrapper img")["alt"] == caption_with_attrs }
- expect(page.find(".image-wrapper img")["alt"]).to eq(caption_with_attrs)
- end
- end
-
- xit "should allow you to cancel a caption request" 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)
- popup.click_generate_caption
- popup.cancel_caption
- expect(popup).to have_no_disabled_generate_button
- end
- end
- end
-
- context "when triggering caption with AI on mobile", mobile: true do
- it "should show update the image alt text with the caption" 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)
- page.find(".mobile-preview").click
- popup.click_generate_caption
- wait_for { page.find(".image-wrapper img")["alt"] == caption_with_attrs }
- expect(page.find(".image-wrapper img")["alt"]).to eq(caption_with_attrs)
- end
- end
- end
-end
diff --git a/plugins/discourse-ai/spec/system/edit_post_image_descriptions_spec.rb b/plugins/discourse-ai/spec/system/edit_post_image_descriptions_spec.rb
new file mode 100644
index 00000000000..5c0609d7a89
--- /dev/null
+++ b/plugins/discourse-ai/spec/system/edit_post_image_descriptions_spec.rb
@@ -0,0 +1,242 @@
+# frozen_string_literal: true
+
+describe "Edit AI post image descriptions" do
+ fab!(:admin)
+ fab!(:first_cat_upload) { create_image_upload("100x100.jpg", "first-cat.jpg") }
+ fab!(:second_cat_upload) do
+ create_image_upload("An image of discobot in action.png", "second-cat.png")
+ end
+ fab!(:dog_upload) { create_image_upload("1x1.jpg", "dog.jpg") }
+
+ fab!(:cat_topic) { Fabricate(:topic, title: "A topic about cats", user: admin, locale: "en") }
+ fab!(:other_topic) { Fabricate(:topic, title: "A topic about dogs", user: admin, locale: "en") }
+
+ fab!(:first_post) do
+ Fabricate(
+ :post,
+ topic: cat_topic,
+ user: admin,
+ post_number: 1,
+ locale: "en",
+ raw: "No image in this post",
+ )
+ end
+
+ fab!(:second_post) do
+ Fabricate(
+ :post,
+ topic: cat_topic,
+ user: admin,
+ post_number: 2,
+ locale: "en",
+ raw:
+ "Two cats here\n\n" \
+ "![first cat|200x200](#{first_cat_upload.short_url})\n\n" \
+ "![second cat|200x200](#{second_cat_upload.short_url})",
+ )
+ end
+
+ fab!(:third_post) do
+ Fabricate(
+ :post,
+ topic: cat_topic,
+ user: admin,
+ post_number: 3,
+ locale: "en",
+ raw: "The same second cat\n\n![second cat again|200x200](#{second_cat_upload.short_url})",
+ )
+ end
+
+ fab!(:other_post) do
+ Fabricate(
+ :post,
+ topic: other_topic,
+ user: admin,
+ locale: "en",
+ raw: "A dog post\n\n![dog|200x200](#{dog_upload.short_url})",
+ )
+ end
+
+ let(:topic_page) { PageObjects::Pages::Topic.new }
+ let(:composer) { PageObjects::Components::Composer.new }
+ let(:captions) { PageObjects::Components::AiPostImageDescriptions.new }
+
+ let(:first_cat_caption) { "An old caption for the first cat" }
+ let(:second_cat_caption) { "An old caption for the shared second cat" }
+ let(:dog_caption) { "An old caption for the dog" }
+ let(:edited_second_cat_caption) { "An edited caption for only the second post cat" }
+ let(:japanese_first_cat_caption) { "一匹目の猫の古い説明" }
+ let(:japanese_second_cat_caption) { "共有された二匹目の猫の古い説明" }
+
+ around { |example| Jobs.with_immediate_jobs { example.run } }
+
+ before do
+ enable_current_plugin
+ SiteSetting.default_locale = "en"
+ SiteSetting.ai_post_image_descriptions_enabled = true
+ SearchIndexer.enable
+
+ prepare_post(first_post)
+ prepare_post(second_post, image_upload: first_cat_upload)
+ prepare_post(third_post, image_upload: second_cat_upload)
+ prepare_post(other_post, image_upload: dog_upload)
+ end
+
+ after { SearchIndexer.disable }
+
+ it "lets the user edit one reused cat caption", :aggregate_failures do
+ SiteSetting.content_localization_enabled = false
+ seed_english_descriptions
+
+ sign_in(admin)
+ topic_page.visit_topic(cat_topic)
+
+ expect(captions).to have_post_image_count(first_post, count: 0)
+ expect(captions).to have_post_image_count(second_post, count: 2)
+ expect(captions).to have_post_image_count(third_post, count: 1)
+ expect(captions).to have_image_description(
+ second_post,
+ image: 2,
+ description: second_cat_caption,
+ )
+ expect(captions).to have_image_description(
+ third_post,
+ image: 1,
+ description: second_cat_caption,
+ )
+
+ topic_page.expand_post_actions(second_post)
+ topic_page.click_post_action_button(second_post, :edit)
+ expect(composer).to be_opened
+ expect(captions).to have_editor_button_count(count: 2)
+
+ captions.edit_preview_image_description(image: 2, description: edited_second_cat_caption)
+ process_description_cooked(post_id: second_post.id, locale: "en")
+ composer.close
+ expect(composer).to be_closed
+
+ topic_page.visit_topic(cat_topic)
+
+ expect(captions).to have_image_description(
+ second_post,
+ image: 2,
+ description: edited_second_cat_caption,
+ )
+ expect(captions).to have_image_description(
+ third_post,
+ image: 1,
+ description: second_cat_caption,
+ )
+ end
+
+ it "shows locale-specific captions and original-locale fallback", :aggregate_failures do
+ SiteSetting.allow_user_locale = true
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.content_localization_allowed_groups = Group::AUTO_GROUPS[:admins]
+ SiteSetting.content_localization_supported_locales = "en|ja"
+ admin.update!(locale: "ja")
+ admin.user_option.update!(show_original_content: false)
+ Fabricate(:topic_localization, topic: cat_topic, locale: "ja", fancy_title: "猫についての話題")
+ create_japanese_localization(second_post)
+ seed_english_descriptions
+ seed_image_description(second_post, first_cat_upload, japanese_first_cat_caption, locale: "ja")
+ seed_image_description(
+ second_post,
+ second_cat_upload,
+ japanese_second_cat_caption,
+ locale: "ja",
+ )
+ process_description_cooked(post_id: second_post.id, locale: "ja")
+
+ sign_in(admin)
+ topic_page.visit_topic(cat_topic)
+
+ expect(captions).to have_image_description(
+ second_post,
+ image: 2,
+ description: japanese_second_cat_caption,
+ )
+
+ admin.user_option.update!(show_original_content: true)
+ topic_page.visit_topic(cat_topic)
+
+ expect(captions).to have_image_description(
+ second_post,
+ image: 2,
+ description: second_cat_caption,
+ )
+ end
+
+ def create_image_upload(filename, original_filename)
+ UploadCreator.new(
+ file_from_fixtures(
+ filename,
+ "images",
+ Rails.root.join("plugins/discourse-ai/spec/fixtures").to_s,
+ ),
+ original_filename,
+ ).create_for(Discourse.system_user.id)
+ end
+
+ def prepare_post(post, image_upload: nil)
+ post.update_column(:cooked, post.cook(post.raw, topic_id: post.topic_id))
+ post.link_post_uploads
+ post.update_column(:image_upload_id, image_upload.id) if image_upload
+ end
+
+ def seed_english_descriptions
+ seed_image_description(second_post, first_cat_upload, first_cat_caption)
+ seed_image_description(second_post, second_cat_upload, second_cat_caption)
+ seed_image_description(third_post, second_cat_upload, second_cat_caption)
+ seed_image_description(other_post, dog_upload, dog_caption)
+
+ process_description_cooked(post_id: second_post.id, locale: "en")
+ process_description_cooked(post_id: third_post.id, locale: "en")
+ process_description_cooked(post_id: other_post.id, locale: "en")
+ end
+
+ def seed_image_description(post, upload, description, locale: "en")
+ AiPostImageDescription.create!(
+ post_id: post.id,
+ upload_id: upload.id,
+ base62_sha1: upload.base62_sha1,
+ locale: locale,
+ description: description,
+ attempts: 0,
+ )
+ end
+
+ def create_japanese_localization(post)
+ raw =
+ "翻訳された猫の投稿\n\n" \
+ "![一匹目の猫|200x200](#{first_cat_upload.short_url})\n\n" \
+ "![二匹目の猫|200x200](#{second_cat_upload.short_url})"
+
+ Fabricate(
+ :post_localization,
+ post: post,
+ locale: "ja",
+ raw: raw,
+ cooked: post.cook(raw, topic_id: post.topic_id),
+ )
+ end
+
+ def process_description_cooked(post_id:, locale:)
+ post = Post.find(post_id)
+ locale = locale.presence || DiscourseAi::PostImageDescriptions.original_locale(post)
+
+ if locale == DiscourseAi::PostImageDescriptions.original_locale(post)
+ Jobs::ProcessPost.new.execute(post_id: post.id, bypass_bump: true)
+ else
+ PostLocalization
+ .where(post_id: post.id, locale: locale)
+ .pluck(:id)
+ .each do |post_localization_id|
+ Jobs::ProcessLocalizedCooked.new.execute(
+ post_localization_id: post_localization_id,
+ recook: true,
+ )
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/system/page_objects/components/ai_caption_popup.rb b/plugins/discourse-ai/spec/system/page_objects/components/ai_caption_popup.rb
deleted file mode 100644
index 7f48536a403..00000000000
--- a/plugins/discourse-ai/spec/system/page_objects/components/ai_caption_popup.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-# frozen_string_literal: true
-
-module PageObjects
- module Components
- class AiCaptionPopup < PageObjects::Components::Base
- GENERATE_CAPTION_SELECTOR = ".button-wrapper .generate-caption"
- CAPTION_POPUP_SELECTOR = ".ai-caption-popup"
- CAPTION_TEXTAREA_SELECTOR = "#{CAPTION_POPUP_SELECTOR} textarea"
-
- def hover_image_wrapper
- image_wrapper = find(".d-editor-preview .image-wrapper")
- image_wrapper.hover
- end
-
- def click_generate_caption
- hover_image_wrapper
- page.find(GENERATE_CAPTION_SELECTOR, visible: false).click
- end
-
- def has_caption_popup_value?(value)
- page.find(CAPTION_TEXTAREA_SELECTOR).value == value
- end
-
- def save_caption
- hover_image_wrapper
- find("#{CAPTION_POPUP_SELECTOR} .btn-primary").click
- end
-
- def cancel_caption
- hover_image_wrapper
- find("#{CAPTION_POPUP_SELECTOR} .cancel-request").click
- end
-
- def has_no_disabled_generate_button?
- page.has_no_css?("#{GENERATE_CAPTION_SELECTOR}.disabled", visible: false)
- end
-
- def has_no_generate_caption_button?
- page.has_no_css?(GENERATE_CAPTION_SELECTOR, visible: false)
- end
- end
- end
-end
diff --git a/plugins/discourse-ai/spec/system/page_objects/components/ai_post_image_descriptions.rb b/plugins/discourse-ai/spec/system/page_objects/components/ai_post_image_descriptions.rb
new file mode 100644
index 00000000000..69e5ca72c14
--- /dev/null
+++ b/plugins/discourse-ai/spec/system/page_objects/components/ai_post_image_descriptions.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Components
+ class AiPostImageDescriptions < PageObjects::Components::Base
+ PREVIEW_EDITOR_BUTTON = ".d-editor-preview .ai-post-image-description-editor__button"
+ MODAL = ".ai-post-image-description-editor-modal"
+
+ def has_post_image_count?(post, count:)
+ post_component(post).cooked_content.has_css?("img", count: count, visible: :all)
+ end
+
+ def has_image_description?(post, image:, description:)
+ post_component(post).cooked_content.has_xpath?(
+ "(.//img)[#{image}][contains(@aria-description, #{xpath_literal(description)})]",
+ visible: :all,
+ )
+ end
+
+ def has_editor_button_count?(count:)
+ has_css?(PREVIEW_EDITOR_BUTTON, count: count)
+ end
+
+ def edit_preview_image_description(image:, description:)
+ all(PREVIEW_EDITOR_BUTTON)[image - 1].click
+ find("#{MODAL} textarea").fill_in(with: description)
+ find("#{MODAL} .btn-primary").click
+ has_no_css?(MODAL)
+ self
+ end
+
+ private
+
+ def post_component(post)
+ PageObjects::Components::Post.new(post.post_number)
+ end
+
+ def xpath_literal(value)
+ value = value.to_s
+
+ return "'#{value}'" if !value.include?("'")
+ return "\"#{value}\"" if !value.include?('"')
+
+ "concat(#{value.split("'").map { |part| "'#{part}'" }.join(%{, "\"'\"", })})"
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-post-image-description-editor-button-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-post-image-description-editor-button-test.gjs
new file mode 100644
index 00000000000..c792c52364e
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-post-image-description-editor-button-test.gjs
@@ -0,0 +1,78 @@
+import Service from "@ember/service";
+import { click, render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import AiPostImageDescriptionEditorButton from "discourse/plugins/discourse-ai/discourse/components/ai-post-image-description-editor-button";
+
+class PostImageDescriptionEditorStub extends Service {
+ openedBase62Sha1 = null;
+
+ descriptionFor(base62Sha1) {
+ if (base62Sha1 === "abc123") {
+ return "A stored description";
+ }
+ }
+}
+
+class ModalStub extends Service {
+ show(_modal, options) {
+ this.model = options.model;
+ }
+}
+
+module(
+ "Integration | Component | AiPostImageDescriptionEditorButton",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.owner.unregister("service:post-image-description-editor");
+ this.owner.unregister("service:modal");
+ this.owner.register(
+ "service:post-image-description-editor",
+ PostImageDescriptionEditorStub
+ );
+ this.owner.register("service:modal", ModalStub);
+ });
+
+ test("renders when the image has an editable description", async function (assert) {
+ await render(
+ <template>
+ <AiPostImageDescriptionEditorButton @base62Sha1="abc123" />
+ </template>
+ );
+
+ assert
+ .dom(".ai-post-image-description-editor__button")
+ .exists("the edit button is shown");
+ });
+
+ test("opens the editor modal for the image", async function (assert) {
+ await render(
+ <template>
+ <AiPostImageDescriptionEditorButton @base62Sha1="abc123" />
+ </template>
+ );
+
+ await click(".ai-post-image-description-editor__button");
+
+ const modal = this.owner.lookup("service:modal");
+ assert.deepEqual(modal.model, {
+ base62Sha1: "abc123",
+ description: "A stored description",
+ });
+ });
+
+ test("does not render without an editable description", async function (assert) {
+ await render(
+ <template>
+ <AiPostImageDescriptionEditorButton @base62Sha1="missing" />
+ </template>
+ );
+
+ assert
+ .dom(".ai-post-image-description-editor__button")
+ .doesNotExist("the edit button is hidden");
+ });
+ }
+);
diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/post-image-description-editor-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/post-image-description-editor-test.js
new file mode 100644
index 00000000000..79d79c45717
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/unit/lib/post-image-description-editor-test.js
@@ -0,0 +1,36 @@
+import { module, test } from "qunit";
+import { imageBase62Sha1 } from "discourse/plugins/discourse-ai/discourse/lib/post-image-description-editor";
+
+module("Unit | Lib | post-image-description-editor", function () {
+ test("imageBase62Sha1 reads data-base62-sha1", function (assert) {
+ const image = document.createElement("img");
+ image.dataset.base62Sha1 = "abc123XYZ";
+
+ assert.strictEqual(imageBase62Sha1(image), "abc123XYZ");
+ });
+
+ test("imageBase62Sha1 falls back to upload short URLs", function (assert) {
+ const image = document.createElement("img");
+ image.setAttribute("data-orig-src", "upload://abc123XYZ.png");
+
+ assert.strictEqual(imageBase62Sha1(image), "abc123XYZ");
+ });
+
+ test("imageBase62Sha1 falls back to resolved upload URLs", function (assert) {
+ const image = document.createElement("img");
+ image.setAttribute(
+ "src",
+ "/uploads/default/original/1X/0049585045f353bed81bb257092d58b968539ff6.jpeg"
+ );
+
+ assert.strictEqual(imageBase62Sha1(image), "2x8JyApqlboI0dAsfwbPbZ7Ho2");
+ });
+
+ test("imageBase62Sha1 ignores invalid values", function (assert) {
+ const image = document.createElement("img");
+ image.dataset.base62Sha1 = "../bad";
+ image.dataset.origSrc = "https://example.com/image.png";
+
+ assert.strictEqual(imageBase62Sha1(image), undefined);
+ });
+});
diff --git a/plugins/discourse-ai/test/javascripts/unit/services/post-image-description-editor-test.js b/plugins/discourse-ai/test/javascripts/unit/services/post-image-description-editor-test.js
new file mode 100644
index 00000000000..85027b93a83
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/unit/services/post-image-description-editor-test.js
@@ -0,0 +1,102 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import { EDIT } from "discourse/models/composer";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+
+module("Unit | Service | post-image-description-editor", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ const owner = getOwner(this);
+ const composer = owner.lookup("service:composer");
+ composer.model = {
+ action: EDIT,
+ locale: "ja",
+ post: { id: 42 },
+ };
+
+ owner.lookup("service:site-settings").ai_post_image_descriptions_enabled =
+ true;
+
+ const service = owner.lookup("service:post-image-description-editor");
+ service.descriptions = new Map();
+ service.loadedKey = null;
+ service.loadingKey = null;
+ });
+
+ test("ensureLoaded fetches descriptions for the edited post", async function (assert) {
+ pretender.get(
+ "/discourse-ai/post-image-descriptions/:post_id",
+ (request) => {
+ assert.strictEqual(request.params.post_id, "42");
+ assert.strictEqual(request.queryParams.locale, "ja");
+
+ return response({
+ descriptions: [
+ { base62_sha1: "abc123", description: "A stored description" },
+ ],
+ });
+ }
+ );
+
+ const service = getOwner(this).lookup(
+ "service:post-image-description-editor"
+ );
+
+ await service.ensureLoaded();
+
+ assert.strictEqual(
+ service.descriptionFor("abc123"),
+ "A stored description"
+ );
+ });
+
+ test("ensureLoaded skips non-edit composers", async function (assert) {
+ let requestCount = 0;
+ pretender.get("/discourse-ai/post-image-descriptions/:post_id", () => {
+ requestCount += 1;
+ return response({ descriptions: [] });
+ });
+
+ const composer = getOwner(this).lookup("service:composer");
+ composer.model.action = "reply";
+
+ const service = getOwner(this).lookup(
+ "service:post-image-description-editor"
+ );
+
+ await service.ensureLoaded();
+
+ assert.strictEqual(requestCount, 0);
+ });
+
+ test("save updates the local description", async function (assert) {
+ pretender.put(
+ "/discourse-ai/post-image-descriptions/:post_id/:base62_sha1",
+ (request) => {
+ assert.strictEqual(request.params.post_id, "42");
+ assert.strictEqual(request.params.base62_sha1, "abc123");
+
+ const params = new URLSearchParams(request.requestBody);
+ assert.strictEqual(params.get("locale"), "ja");
+
+ return response({
+ base62_sha1: "abc123",
+ description: "An edited description",
+ });
+ }
+ );
+
+ const service = getOwner(this).lookup(
+ "service:post-image-description-editor"
+ );
+
+ await service.save("abc123", "An edited description");
+
+ assert.strictEqual(
+ service.descriptionFor("abc123"),
+ "An edited description"
+ );
+ });
+});
diff --git a/spec/lib/localized_cooked_post_processor_spec.rb b/spec/lib/localized_cooked_post_processor_spec.rb
index d6f3ed3331d..db173c27432 100644
--- a/spec/lib/localized_cooked_post_processor_spec.rb
+++ b/spec/lib/localized_cooked_post_processor_spec.rb
@@ -30,6 +30,16 @@ RSpec.describe LocalizedCookedPostProcessor do
processor.post_process
end
+ it "triggers the localized cooked post-process event", :aggregate_failures do
+ events =
+ DiscourseEvent.track_events(:post_process_localized_cooked) { processor.post_process }
+
+ expect(events.size).to eq(1)
+ expect(events.first[:params].first.to_html).to include(upload1.base62_sha1)
+ expect(events.first[:params].second).to eq(post)
+ expect(events.first[:params].third).to eq(localization)
+ end
+
it "creates upload references for uploads in the localization" do
processor.post_process
diff --git a/spec/services/search_indexer_spec.rb b/spec/services/search_indexer_spec.rb
index 87ce354046d..2fc31d5ce4d 100644
--- a/spec/services/search_indexer_spec.rb
+++ b/spec/services/search_indexer_spec.rb
@@ -26,6 +26,41 @@ RSpec.describe SearchIndexer do
expect(post_search_data.search_data).to eq("'世界':2 '你好':1")
end
+ it "applies post search text modifiers with locale", :aggregate_failures do
+ cooked = "<p>hello</p>"
+ modifier_block =
+ proc do |text, modifier_post_id, modifier_cooked, locale|
+ expect(modifier_post_id).to eq(post_id)
+ expect(modifier_cooked).to eq(cooked)
+ expect(locale).to eq("ja")
+
+ "#{text} extra indexed text"
+ end
+
+ plugin_instance = Plugin::Instance.new
+ plugin_instance.register_modifier(:post_search_index_text, &modifier_block)
+
+ SearchIndexer.update_posts_index(
+ post_id: post_id,
+ topic_title: "",
+ category_name: "",
+ topic_tags: "",
+ cooked: cooked,
+ private_message: false,
+ locale: "ja",
+ )
+
+ expect(PostSearchData.find_by(post_id: post_id).raw_data).to eq("hello extra indexed text")
+ ensure
+ if plugin_instance
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin_instance,
+ :post_search_index_text,
+ &modifier_block
+ )
+ end
+ end
+
it "extract youtube title" do
html =
"<div class=\"lazy-video-container\" data-video-id=\"lmFgeFh2nlw\" data-video-title=\"Metallica Mixer Explains Missing Bass on 'And Justice for All' [Exclusive]\" data-provider-name=\"youtube\"></div>"