Class: RubyLLM::Agents::ImageVariationResult

Inherits:
Object
  • Object
show all
Includes:
Trackable
Defined in:
lib/ruby_llm/agents/results/image_variation_result.rb

Overview

Result wrapper for image variation operations

Provides a consistent interface for accessing variation images, metadata, timing, and cost information.

Examples:

Accessing variations

result = ImageVariator.call(image: "logo.png", count: 4)
result.urls      # => ["https://...", ...]
result.count     # => 4
result.success?  # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

included

Constructor Details

#initialize(images:, source_image:, model_id:, size:, variation_strength:, started_at:, completed_at:, tenant_id:, variator_class:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ ImageVariationResult

Initialize a new result

Parameters:

  • images (Array<Object>)

    Array of variation image objects

  • source_image (String)

    The original source image

  • model_id (String)

    Model used for variation

  • size (String)

    Image size

  • variation_strength (Float)

    Variation strength used

  • started_at (Time)

    When variation started

  • completed_at (Time)

    When variation completed

  • tenant_id (String, nil)

    Tenant identifier

  • variator_class (String)

    Name of the variator class

  • error_class (String, nil) (defaults to: nil)

    Error class name if failed

  • error_message (String, nil) (defaults to: nil)

    Error message if failed



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 37

def initialize(images:, source_image:, model_id:, size:, variation_strength:,
  started_at:, completed_at:, tenant_id:, variator_class:,
  error_class: nil, error_message: nil, agent_class_name: nil)
  @images = images
  @source_image = source_image
  @model_id = model_id
  @size = size
  @variation_strength = variation_strength
  @started_at = started_at
  @completed_at = completed_at
  @tenant_id = tenant_id
  @variator_class = variator_class
  @error_class = error_class
  @error_message = error_message
  @execution_id = nil

  # Tracking
  @agent_class_name = agent_class_name
  register_with_tracker
end

Instance Attribute Details

#completed_atObject (readonly)

Returns the value of attribute completed_at.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def completed_at
  @completed_at
end

#error_classObject (readonly)

Returns the value of attribute error_class.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def error_class
  @error_class
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def error_message
  @error_message
end

#execution_idObject

Returns the value of attribute execution_id.



22
23
24
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 22

def execution_id
  @execution_id
end

#imagesObject (readonly)

Returns the value of attribute images.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def images
  @images
end

#model_idObject (readonly)

Returns the value of attribute model_id.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def model_id
  @model_id
end

#sizeObject (readonly)

Returns the value of attribute size.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def size
  @size
end

#source_imageObject (readonly)

Returns the value of attribute source_image.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def source_image
  @source_image
end

#started_atObject (readonly)

Returns the value of attribute started_at.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def started_at
  @started_at
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def tenant_id
  @tenant_id
end

#variation_strengthObject (readonly)

Returns the value of attribute variation_strength.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def variation_strength
  @variation_strength
end

#variator_classObject (readonly)

Returns the value of attribute variator_class.



19
20
21
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 19

def variator_class
  @variator_class
end

Class Method Details

.from_cache(data) ⇒ Object



198
199
200
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 198

def self.from_cache(data)
  CachedImageVariationResult.new(data)
end

Instance Method Details

#base64?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 105

def base64?
  image&.base64? || false
end

#batch?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 79

def batch?
  count > 1
end

#blobsObject



156
157
158
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 156

def blobs
  images.map(&:to_blob)
end

#countObject

Count



115
116
117
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 115

def count
  images.size
end

#dataObject



97
98
99
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 97

def data
  image&.data
end

#datasObject



101
102
103
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 101

def datas
  images.map(&:data).compact
end

#duration_msObject

Timing



121
122
123
124
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 121

def duration_ms
  return 0 unless started_at && completed_at
  ((completed_at - started_at) * 1000).round
end

#error?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 71

def error?
  !success?
end

#executionRubyLLM::Agents::Execution?

Loads the associated Execution record from the database

Returns:



61
62
63
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 61

def execution
  @execution ||= RubyLLM::Agents::Execution.find_by(id: execution_id) if execution_id
end

#imageObject

Image access



85
86
87
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 85

def image
  images.first
end

#mime_typeObject



109
110
111
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 109

def mime_type
  image&.mime_type
end

#save(path) ⇒ Object

File operations



140
141
142
143
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 140

def save(path)
  raise "No image to save" unless image
  image.save(path)
end

#save_all(directory, prefix: "variation") ⇒ Object



145
146
147
148
149
150
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 145

def save_all(directory, prefix: "variation")
  images.each_with_index do |img, idx|
    filename = "#{prefix}_#{idx + 1}.png"
    img.save(File.join(directory, filename))
  end
end

#single?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 75

def single?
  count == 1
end

#success?Boolean

Status helpers

Returns:

  • (Boolean)


67
68
69
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 67

def success?
  error_class.nil? && images.any?
end

#to_blobObject



152
153
154
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 152

def to_blob
  image&.to_blob
end

#to_cacheObject

Caching



187
188
189
190
191
192
193
194
195
196
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 187

def to_cache
  {
    urls: urls,
    datas: datas,
    mime_type: mime_type,
    model_id: model_id,
    total_cost: total_cost,
    cached_at: Time.current.iso8601
  }
end

#to_hObject

Serialization



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 162

def to_h
  {
    success: success?,
    count: count,
    urls: urls,
    base64: base64?,
    mime_type: mime_type,
    source_image: source_image,
    model_id: model_id,
    size: size,
    variation_strength: variation_strength,
    total_cost: total_cost,
    duration_ms: duration_ms,
    started_at: started_at&.iso8601,
    completed_at: completed_at&.iso8601,
    tenant_id: tenant_id,
    variator_class: variator_class,
    error_class: error_class,
    error_message: error_message,
    execution_id: execution_id
  }
end

#total_costObject

Cost estimation



128
129
130
131
132
133
134
135
136
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 128

def total_cost
  return 0 if error?

  ImageGenerator::Pricing.calculate_cost(
    model_id: model_id,
    size: size,
    count: count
  )
end

#urlObject



89
90
91
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 89

def url
  image&.url
end

#urlsObject



93
94
95
# File 'lib/ruby_llm/agents/results/image_variation_result.rb', line 93

def urls
  images.map(&:url).compact
end