0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-ai/lib/configuration/image_caption_agent_validator.rb
Natalie Tay 86680f6b0d
FEATURE: Dedicated image caption AI feature and agent validations (#41729)
Follow up to https://github.com/discourse/discourse/pull/41586


1. **Bulk of the PR diff** -- there's a mix of usages for "description"
and "caption", we'll stick to V1 terminology and keep "AI caption"
instead of bringing in "description".

2. Move image caption out of AI helper, into its own dedicated "AI
Feature" for consolidated settings

3. Update default values for the existing image caption agent 
- vision_enabled was default off, now on
- update description

4. Validations and problem checks
- via Site Setting validation - agent / llm
  - when enabling the feature
  - or when selecting invalid agent

- inform admin when they've enabled the setting but the agent is
misconfigured via ProblemCheck
2026-07-16 11:29:39 +08:00

48 lines
1.1 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Configuration
class ImageCaptionAgentValidator
DEFAULT_AGENT_ID = "-26"
def initialize(opts = {})
@opts = opts
end
def valid_value?(val)
return true if val.to_s == DEFAULT_AGENT_ID
valid_agent_value?(val)
end
def valid_agent_value?(val)
agent = AiAgent.find_by(id: val.to_i)
return invalid(:agent_missing) if agent.blank?
agent_class = agent.class_instance
return invalid(:agent_vision_disabled) if !agent_class&.vision_enabled
llm_model =
DiscourseAi::AiHelper::Assistant.find_ai_helper_model(
DiscourseAi::AiHelper::Assistant::IMAGE_CAPTION,
agent_class,
)
return invalid(:llm_missing) if llm_model.blank?
return invalid(:llm_vision_disabled) if !llm_model.vision_enabled?
true
end
def error_message
I18n.t("discourse_ai.image_caption.configuration.#{@error_key}")
end
private
def invalid(error_key)
@error_key = error_key
false
end
end
end
end