0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/lib/localized_cooked_post_processor.rb
Natalie Tay 9f6fa67949
FEATURE: Automatic AI captions (#41586)
### settings

Settings required for V2 captions:
- discourse_ai_enabled = true
- ai_post_image_descriptions_enabled = true
- ai_helper_image_caption_agent must be set. It defaults to -26, the
default image caption agent

`+` optional tuning:
- ai_post_image_descriptions_per_post_limit, default 10
- ai_post_image_descriptions_backfill_hourly_rate, default 0, so
backfill is off by default
- ai_post_image_descriptions_backfill_max_age_days, default 30

These settings are required for generation/backfill:
- the selected `ai_helper_image_caption_agent` must exist and be enabled
- the agent/persona must be vision-enabled
- the resolved LLM must be vision-capable
- credits must be available
- ai_post_image_descriptions_backfill_hourly_rate > 0 for scheduled
backfill

______

### this pr 

... does _a few things_.

1. removes the old AI image captioning flow (v1)...
  - removes the composer manual captioning UI
  - stops inserting generated captions into post raw
  - removes `image_caption` from the AI helper enabled features setting

2. adds the new post image description flow (v2)...
  - backfills descriptions for existing post images
- stores descriptions in `ai_post_image_descriptions`, keyed by
`post_id`, `locale`, and `base62_sha1`
- keeps duplicate rows per post image instead of relying only on uploads
- cook-time lookup, deletion, and per-post editing stay simple even when
multiple posts reuse the same upload
- doing this also prevents the author of one post image from affecting
another post's image if they have the same image
- uses `base62_sha1` and `locale` for reuse when the same image appears
again
- exposes caption editing from the composer preview for
already-described post images
- includes descriptions in search indexing without inserting them into
raw
  - decorates cooked post images with localized `aria-description`
- shows the AI description visibly in the lightbox under the existing
image title
2026-07-15 13:24:10 +08:00

30 lines
895 B
Ruby
Vendored

# frozen_string_literal: true
class LocalizedCookedPostProcessor
include CookedProcessorMixin
def initialize(post_localization, post, opts = {})
@post_localization = post_localization
@post = post
@opts = (opts || {}).merge(locale: post_localization.locale)
@doc = Loofah.html5_fragment(@post_localization.cooked)
@cooking_options = @post.cooking_options || {}
@cooking_options[:topic_id] = @post.topic_id
@cooking_options = @cooking_options.symbolize_keys
@model = @post
@category_id = @post&.topic&.category_id
@omit_nofollow = @post.omit_nofollow?
@size_cache = {}
end
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
def html
@doc.try(:to_html)
end
end