Class: ImageMetadataSuggestionsWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/image_metadata_suggestions_worker.rb

Overview

Generates AI metadata suggestions for an existing library image and stores
them in the +ai_metadata_suggestions+ JSONB column for user review.

After completion the job redirects to the suggestions review page
(+images#ai_metadata_suggestions+) where the user can selectively apply
suggestions to the image's fields.

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(image_id, options = {}) ⇒ Object

Parameters:

  • image_id (Integer)

    ID of the Image to suggest metadata for

  • options (Hash) (defaults to: {})

    job options

Options Hash (options):

  • account_id (Integer)

    Account that requested the suggestions (usage logging)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/workers/image_metadata_suggestions_worker.rb', line 19

def perform(image_id, options = {})
   = options[:account_id]&.to_i.presence

  total 3
  at 1, 'Loading image...'

  image = Image.find_by(id: image_id)
  unless image
    store error_message: "Image #{image_id} not found"
    return
  end

  store redirect_to: "/#{I18n.locale || 'en-US'}/images/#{image.slug}/ai_metadata_suggestions"

  at 2, 'Generating metadata suggestions...'
  result = ImageMetadataSuggester.call(image)

  unless result.success?
    store error_message: "Metadata suggestion failed: #{result.error}"
    return
  end

  image.update!(
    ai_metadata_suggestions: {
      'title'            => result.title,
      'meta_title'       => result.meta_title,
      'meta_description' => result.meta_description,
      'tags'             => result.tags,
      'notes'            => result.notes,
      'model_used'       => result.model_id,
      'generated_at'     => Time.current.iso8601
    }
  )

  provider = LlmModel.find_by(model_id: result.model_id.to_s)&.provider || 'unknown'
  AiUsageLog.log!(
    provider:      provider,
    model_id:      result.model_id.to_s,
    feature:       'image_metadata_suggestion',
    input_tokens:  result.input_tokens,
    output_tokens: result.output_tokens,
    subject:       image,
    account_id:    
  )

  at 3, 'Suggestions ready!'
  store info_message: 'AI metadata suggestions are ready for review.'
  Rails.logger.info "[ImageMetadataSuggestionsWorker] Generated suggestions for Image #{image_id}"
rescue StandardError => e
  store error_message: "Error generating suggestions: #{e.message}"
  Rails.logger.error "[ImageMetadataSuggestionsWorker] Error for #{image_id}: #{e.message}"
  ErrorReporting.error(e) if defined?(ErrorReporting)
  raise
end