Class: RubyLLM::Agents::ImagePipelineResult

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

Overview

Result wrapper for image pipeline operations

Provides access to individual step results, aggregated costs, timing information, and the final processed image.

Examples:

Accessing pipeline results

result = ProductPipeline.call(prompt: "Laptop photo")
result.success?        # => true
result.final_image     # => The final processed image URL/data
result.total_cost      # => Combined cost of all steps
result.steps           # => Array of step results

Accessing specific step results

result.step(:generate)   # => ImageGenerationResult
result.step(:upscale)    # => ImageUpscaleResult
result.step(:analyze)    # => ImageAnalysisResult
result.analysis          # => Shortcut to analyzer step result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

included

Constructor Details

#initialize(step_results:, started_at:, completed_at:, tenant_id:, pipeline_class:, context:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ ImagePipelineResult

Initialize a new pipeline result

Parameters:

  • step_results (Array<Hash>)

    Array of step result hashes

  • started_at (Time)

    When pipeline started

  • completed_at (Time)

    When pipeline completed

  • tenant_id (String, nil)

    Tenant identifier

  • pipeline_class (String)

    Name of the pipeline class

  • context (Hash)

    Pipeline context

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

    Error class name if failed

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

    Error message if failed



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

def initialize(step_results:, started_at:, completed_at:, tenant_id:,
  pipeline_class:, context:, error_class: nil, error_message: nil, agent_class_name: nil)
  @step_results = step_results
  @started_at = started_at
  @completed_at = completed_at
  @tenant_id = tenant_id
  @pipeline_class = pipeline_class
  @context = context
  @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.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def completed_at
  @completed_at
end

#contextObject (readonly)

Returns the value of attribute context.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def context
  @context
end

#error_classObject (readonly)

Returns the value of attribute error_class.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def error_class
  @error_class
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def error_message
  @error_message
end

#execution_idObject

Returns the value of attribute execution_id.



28
29
30
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 28

def execution_id
  @execution_id
end

#pipeline_classObject (readonly)

Returns the value of attribute pipeline_class.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def pipeline_class
  @pipeline_class
end

#started_atObject (readonly)

Returns the value of attribute started_at.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def started_at
  @started_at
end

#step_resultsObject (readonly)

Returns the value of attribute step_results.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def step_results
  @step_results
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



26
27
28
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 26

def tenant_id
  @tenant_id
end

Class Method Details

.from_cache(data) ⇒ CachedImagePipelineResult

Restore from cache

Parameters:

  • data (Hash)

    Cached data

Returns:



361
362
363
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 361

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

Instance Method Details

#analysisImageAnalysisResult?

Get the analysis result if an analyzer step was run

Returns:



211
212
213
214
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 211

def analysis
  analyzer_step = step_results.find { |s| s[:type] == :analyzer }
  analyzer_step&.dig(:result)
end

#background_removalBackgroundRemovalResult?

Get the background removal result if a remover step was run

Returns:



243
244
245
246
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 243

def background_removal
  remover_step = step_results.find { |s| s[:type] == :remover }
  remover_step&.dig(:result)
end

#base64?Boolean

Check if final image is base64 encoded

Returns:

  • (Boolean)

    true if base64



189
190
191
192
193
194
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 189

def base64?
  image_step = step_results.reverse.find do |s|
    s[:type] != :analyzer && s[:result]&.success?
  end
  image_step&.dig(:result)&.base64? || false
end

#completed?Boolean

Check if pipeline completed (with or without errors)

Returns:

  • (Boolean)

    true if pipeline finished



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

def completed?
  !error_class || step_results.any?
end

#dataString?

Get the final image data

Returns:

  • (String, nil)

    Base64 data of final image



179
180
181
182
183
184
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 179

def data
  image_step = step_results.reverse.find do |s|
    s[:type] != :analyzer && s[:result]&.success? && s[:result].respond_to?(:data)
  end
  image_step&.dig(:result)&.data
end

#duration_msInteger

Pipeline duration in milliseconds

Returns:

  • (Integer)

    Duration in ms



253
254
255
256
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 253

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

#error?Boolean

Check if pipeline had any errors

Returns:

  • (Boolean)

    true if any step failed



78
79
80
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 78

def error?
  !success?
end

#executionRubyLLM::Agents::Execution?

Loads the associated Execution record from the database

Returns:



60
61
62
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 60

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

#failed_step_countInteger

Number of failed steps

Returns:

  • (Integer)

    Failed step count



146
147
148
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 146

def failed_step_count
  step_results.count { |s| s[:result]&.error? }
end

#final_imageString?

Get the final image from the last successful image-producing step

Returns:

  • (String, nil)

    URL or data of final image



155
156
157
158
159
160
161
162
163
164
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 155

def final_image
  # Find last successful step that produces an image (not analyzer)
  image_step = step_results.reverse.find do |s|
    s[:type] != :analyzer && s[:result]&.success?
  end
  return nil unless image_step

  result = image_step[:result]
  result.url || result.data
end

#generationImageGenerationResult?

Get the generation result if a generator step was run

Returns:



219
220
221
222
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 219

def generation
  generator_step = step_results.find { |s| s[:type] == :generator }
  generator_step&.dig(:result)
end

#partial?Boolean

Check if pipeline was partially successful

Returns:

  • (Boolean)

    true if some steps succeeded but not all



92
93
94
95
96
97
98
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 92

def partial?
  return false if error_class && step_results.empty?

  has_success = step_results.any? { |s| s[:result]&.success? }
  has_error = step_results.any? { |s| s[:result]&.error? }
  has_success && has_error
end

#primary_model_idString?

Get the primary model ID (from first step)

Returns:

  • (String, nil)

    Model ID



270
271
272
273
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 270

def primary_model_id
  first_result = step_results.first&.dig(:result)
  first_result&.model_id
end

#save(path) ⇒ void

This method returns an undefined value.

Save the final image to a file

Parameters:

  • path (String)

    File path



281
282
283
284
285
286
287
288
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 281

def save(path)
  image_step = step_results.reverse.find do |s|
    s[:type] != :analyzer && s[:result]&.success? && s[:result].respond_to?(:save)
  end
  raise "No image to save" unless image_step

  image_step[:result].save(path)
end

#save_all(directory, prefix: "step") ⇒ void

This method returns an undefined value.

Save all intermediate images

Parameters:

  • directory (String)

    Directory path

  • prefix (String) (defaults to: "step")

    Filename prefix



295
296
297
298
299
300
301
302
303
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 295

def save_all(directory, prefix: "step")
  step_results.each_with_index do |step, idx|
    next if step[:type] == :analyzer
    next unless step[:result]&.success? && step[:result].respond_to?(:save)

    filename = "#{prefix}_#{idx + 1}_#{step[:name]}.png"
    step[:result].save(File.join(directory, filename))
  end
end

#step(name) ⇒ Object? Also known as: []

Get a specific step result by name

Parameters:

  • name (Symbol)

    Step name

Returns:

  • (Object, nil)

    The step result or nil



113
114
115
116
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 113

def step(name)
  step_data = step_results.find { |s| s[:name] == name }
  step_data&.dig(:result)
end

#step_countInteger

Total number of steps in pipeline

Returns:

  • (Integer)

    Step count



132
133
134
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 132

def step_count
  step_results.size
end

#step_namesArray<Symbol>

Get step names

Returns:

  • (Array<Symbol>)

    Array of step names



123
124
125
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 123

def step_names
  step_results.map { |s| s[:name] }
end

#stepsArray<Hash>

Get all steps as array

Returns:

  • (Array<Hash>)

    Array of step result hashes



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

def steps
  step_results
end

#success?Boolean

Check if pipeline completed successfully

Returns:

  • (Boolean)

    true if all steps succeeded



69
70
71
72
73
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 69

def success?
  return false if error_class

  step_results.all? { |s| s[:result]&.success? }
end

#successful_step_countInteger

Number of successful steps

Returns:

  • (Integer)

    Successful step count



139
140
141
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 139

def successful_step_count
  step_results.count { |s| s[:result]&.success? }
end

#to_blobString?

Get the final image as binary blob

Returns:

  • (String, nil)

    Binary image data



199
200
201
202
203
204
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 199

def to_blob
  image_step = step_results.reverse.find do |s|
    s[:type] != :analyzer && s[:result]&.success? && s[:result].respond_to?(:to_blob)
  end
  image_step&.dig(:result)&.to_blob
end

#to_cacheHash

Convert to cacheable format

Returns:

  • (Hash)

    Cacheable hash



343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 343

def to_cache
  {
    step_results: step_results.map do |s|
      {
        name: s[:name],
        type: s[:type],
        cached_result: s[:result]&.respond_to?(:to_cache) ? s[:result].to_cache : nil
      }
    end,
    total_cost: total_cost,
    cached_at: Time.current.iso8601
  }
end

#to_hHash

Convert to hash

Returns:

  • (Hash)

    Hash representation



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 310

def to_h
  {
    success: success?,
    partial: partial?,
    step_count: step_count,
    successful_steps: successful_step_count,
    failed_steps: failed_step_count,
    steps: step_results.map do |s|
      {
        name: s[:name],
        type: s[:type],
        success: s[:result]&.success?,
        cost: s[:result]&.total_cost
      }
    end,
    final_image_url: url,
    total_cost: total_cost,
    duration_ms: duration_ms,
    started_at: started_at&.iso8601,
    completed_at: completed_at&.iso8601,
    tenant_id: tenant_id,
    pipeline_class: pipeline_class,
    error_class: error_class,
    error_message: error_message,
    execution_id: execution_id
  }
end

#total_costFloat

Total cost of all pipeline steps

Returns:

  • (Float)

    Combined cost



263
264
265
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 263

def total_cost
  step_results.sum { |s| s[:result]&.total_cost || 0 }
end

#transformImageTransformResult?

Get the transform result if a transformer step was run

Returns:



235
236
237
238
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 235

def transform
  transformer_step = step_results.find { |s| s[:type] == :transformer }
  transformer_step&.dig(:result)
end

#upscaleImageUpscaleResult?

Get the upscale result if an upscaler step was run

Returns:



227
228
229
230
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 227

def upscale
  upscaler_step = step_results.find { |s| s[:type] == :upscaler }
  upscaler_step&.dig(:result)
end

#urlString?

Get the final image URL

Returns:

  • (String, nil)

    URL of final image



169
170
171
172
173
174
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 169

def url
  image_step = step_results.reverse.find do |s|
    s[:type] != :analyzer && s[:result]&.success? && s[:result].respond_to?(:url)
  end
  image_step&.dig(:result)&.url
end