Class: RubyLLM::Agents::ImageUpscaleResult

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

Overview

Result wrapper for image upscaling operations

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

Examples:

Accessing upscaled image

result = ImageUpscaler.call(image: "low_res.jpg", scale: 4)
result.url          # => "https://..."
result.scale        # => 4
result.output_size  # => "4096x4096"
result.success?     # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

included

Constructor Details

#initialize(image:, source_image:, model_id:, scale:, output_size:, face_enhance:, started_at:, completed_at:, tenant_id:, upscaler_class:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ ImageUpscaleResult

Initialize a new result

Parameters:

  • image (Object)

    The upscaled image object

  • source_image (String)

    The original source image

  • model_id (String)

    Model used for upscaling

  • scale (Integer)

    Upscale factor used

  • output_size (String)

    Output image dimensions

  • face_enhance (Boolean)

    Whether face enhancement was used

  • started_at (Time)

    When upscaling started

  • completed_at (Time)

    When upscaling completed

  • tenant_id (String, nil)

    Tenant identifier

  • upscaler_class (String)

    Name of the upscaler class

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

    Error class name if failed

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

    Error message if failed



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

def initialize(image:, source_image:, model_id:, scale:, output_size:, face_enhance:,
  started_at:, completed_at:, tenant_id:, upscaler_class:,
  error_class: nil, error_message: nil, agent_class_name: nil)
  @image = image
  @source_image = source_image
  @model_id = model_id
  @scale = scale
  @output_size = output_size
  @face_enhance = face_enhance
  @started_at = started_at
  @completed_at = completed_at
  @tenant_id = tenant_id
  @upscaler_class = upscaler_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.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def completed_at
  @completed_at
end

#error_classObject (readonly)

Returns the value of attribute error_class.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def error_class
  @error_class
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def error_message
  @error_message
end

#execution_idObject

Returns the value of attribute execution_id.



23
24
25
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 23

def execution_id
  @execution_id
end

#face_enhanceObject (readonly)

Returns the value of attribute face_enhance.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def face_enhance
  @face_enhance
end

#imageObject (readonly)

Returns the value of attribute image.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def image
  @image
end

#model_idObject (readonly)

Returns the value of attribute model_id.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def model_id
  @model_id
end

#output_sizeObject (readonly)

Returns the value of attribute output_size.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def output_size
  @output_size
end

#scaleObject (readonly)

Returns the value of attribute scale.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def scale
  @scale
end

#source_imageObject (readonly)

Returns the value of attribute source_image.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def source_image
  @source_image
end

#started_atObject (readonly)

Returns the value of attribute started_at.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def started_at
  @started_at
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def tenant_id
  @tenant_id
end

#upscaler_classObject (readonly)

Returns the value of attribute upscaler_class.



20
21
22
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 20

def upscaler_class
  @upscaler_class
end

Class Method Details

.from_cache(data) ⇒ Object



209
210
211
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 209

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

Instance Method Details

#base64?Boolean

Returns:

  • (Boolean)


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

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

#batch?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 83

def batch?
  false
end

#blobsObject



165
166
167
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 165

def blobs
  success? ? [to_blob].compact : []
end

#countObject

Count (always 1 for upscaling)



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

def count
  success? ? 1 : 0
end

#dataObject



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

def data
  image&.data
end

#datasObject



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

def datas
  success? ? [data].compact : []
end

#duration_msObject

Timing



137
138
139
140
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 137

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

#error?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 74

def error?
  !success?
end

#executionRubyLLM::Agents::Execution?

Loads the associated Execution record from the database

Returns:



64
65
66
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 64

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

#mime_typeObject



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

def mime_type
  image&.mime_type
end

#output_heightObject



130
131
132
133
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 130

def output_height
  return nil unless output_size
  output_size.split("x").last.to_i
end

#output_widthObject



125
126
127
128
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 125

def output_width
  return nil unless output_size
  output_size.split("x").first.to_i
end

#save(path) ⇒ Object

File operations



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

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

#single?Boolean

Always single image for upscaling

Returns:

  • (Boolean)


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

def single?
  true
end

#sizeObject

Size helpers



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

def size
  output_size
end

#success?Boolean

Status helpers

Returns:

  • (Boolean)


70
71
72
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 70

def success?
  error_class.nil? && !image.nil?
end

#to_blobObject



161
162
163
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 161

def to_blob
  image&.to_blob
end

#to_cacheObject

Caching



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 196

def to_cache
  {
    url: url,
    data: data,
    mime_type: mime_type,
    model_id: model_id,
    scale: scale,
    output_size: output_size,
    total_cost: total_cost,
    cached_at: Time.current.iso8601
  }
end

#to_hObject

Serialization



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 171

def to_h
  {
    success: success?,
    url: url,
    base64: base64?,
    mime_type: mime_type,
    source_image: source_image,
    model_id: model_id,
    scale: scale,
    output_size: output_size,
    face_enhance: face_enhance,
    total_cost: total_cost,
    duration_ms: duration_ms,
    started_at: started_at&.iso8601,
    completed_at: completed_at&.iso8601,
    tenant_id: tenant_id,
    upscaler_class: upscaler_class,
    error_class: error_class,
    error_message: error_message,
    execution_id: execution_id
  }
end

#total_costObject

Cost estimation



144
145
146
147
148
149
150
151
152
# File 'lib/ruby_llm/agents/results/image_upscale_result.rb', line 144

def total_cost
  return 0 if error?

  # Upscaling typically has fixed per-image cost
  ImageGenerator::Pricing.calculate_cost(
    model_id: model_id,
    count: 1
  )
end

#urlObject

Image access



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

def url
  image&.url
end

#urlsObject



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

def urls
  success? ? [url].compact : []
end