Class: RubyLLM::Agents::ImagePipelineResult
- Inherits:
-
Object
- Object
- RubyLLM::Agents::ImagePipelineResult
- 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.
Instance Attribute Summary collapse
-
#completed_at ⇒ Object
readonly
Returns the value of attribute completed_at.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#error_class ⇒ Object
readonly
Returns the value of attribute error_class.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#execution_id ⇒ Object
Returns the value of attribute execution_id.
-
#pipeline_class ⇒ Object
readonly
Returns the value of attribute pipeline_class.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#step_results ⇒ Object
readonly
Returns the value of attribute step_results.
-
#tenant_id ⇒ Object
readonly
Returns the value of attribute tenant_id.
Class Method Summary collapse
-
.from_cache(data) ⇒ CachedImagePipelineResult
Restore from cache.
Instance Method Summary collapse
-
#analysis ⇒ ImageAnalysisResult?
Get the analysis result if an analyzer step was run.
-
#background_removal ⇒ BackgroundRemovalResult?
Get the background removal result if a remover step was run.
-
#base64? ⇒ Boolean
Check if final image is base64 encoded.
-
#completed? ⇒ Boolean
Check if pipeline completed (with or without errors).
-
#data ⇒ String?
Get the final image data.
-
#duration_ms ⇒ Integer
Pipeline duration in milliseconds.
-
#error? ⇒ Boolean
Check if pipeline had any errors.
-
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database.
-
#failed_step_count ⇒ Integer
Number of failed steps.
-
#final_image ⇒ String?
Get the final image from the last successful image-producing step.
-
#generation ⇒ ImageGenerationResult?
Get the generation result if a generator step was run.
-
#initialize(step_results:, started_at:, completed_at:, tenant_id:, pipeline_class:, context:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ ImagePipelineResult
constructor
Initialize a new pipeline result.
-
#partial? ⇒ Boolean
Check if pipeline was partially successful.
-
#primary_model_id ⇒ String?
Get the primary model ID (from first step).
-
#save(path) ⇒ void
Save the final image to a file.
-
#save_all(directory, prefix: "step") ⇒ void
Save all intermediate images.
-
#step(name) ⇒ Object?
(also: #[])
Get a specific step result by name.
-
#step_count ⇒ Integer
Total number of steps in pipeline.
-
#step_names ⇒ Array<Symbol>
Get step names.
-
#steps ⇒ Array<Hash>
Get all steps as array.
-
#success? ⇒ Boolean
Check if pipeline completed successfully.
-
#successful_step_count ⇒ Integer
Number of successful steps.
-
#to_blob ⇒ String?
Get the final image as binary blob.
-
#to_cache ⇒ Hash
Convert to cacheable format.
-
#to_h ⇒ Hash
Convert to hash.
-
#total_cost ⇒ Float
Total cost of all pipeline steps.
-
#transform ⇒ ImageTransformResult?
Get the transform result if a transformer step was run.
-
#upscale ⇒ ImageUpscaleResult?
Get the upscale result if an upscaler step was run.
-
#url ⇒ String?
Get the final image URL.
Methods included from Trackable
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
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 = @execution_id = nil # Tracking @agent_class_name = agent_class_name register_with_tracker end |
Instance Attribute Details
#completed_at ⇒ Object (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 |
#context ⇒ Object (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_class ⇒ Object (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_message ⇒ Object (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 end |
#execution_id ⇒ Object
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_class ⇒ Object (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_at ⇒ Object (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_results ⇒ Object (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_id ⇒ Object (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
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
#analysis ⇒ ImageAnalysisResult?
Get the analysis result if an analyzer step was run
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_removal ⇒ BackgroundRemovalResult?
Get the background removal result if a remover step was run
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
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)
85 86 87 |
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 85 def completed? !error_class || step_results.any? end |
#data ⇒ String?
Get the final image data
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_ms ⇒ Integer
Pipeline duration in milliseconds
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
78 79 80 |
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 78 def error? !success? end |
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database
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_count ⇒ Integer
Number of failed steps
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_image ⇒ String?
Get the final image from the last successful image-producing step
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 |
#generation ⇒ ImageGenerationResult?
Get the generation result if a generator step was run
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
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_id ⇒ String?
Get the primary model ID (from first step)
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
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
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
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_count ⇒ Integer
Total number of steps in pipeline
132 133 134 |
# File 'lib/ruby_llm/agents/results/image_pipeline_result.rb', line 132 def step_count step_results.size end |
#step_names ⇒ Array<Symbol>
Get 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 |
#steps ⇒ Array<Hash>
Get all steps as array
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
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_count ⇒ Integer
Number of successful steps
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_blob ⇒ String?
Get the final image as binary blob
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_cache ⇒ Hash
Convert to cacheable format
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_h ⇒ Hash
Convert to hash
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: , execution_id: execution_id } end |
#total_cost ⇒ Float
Total cost of all pipeline steps
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 |
#transform ⇒ ImageTransformResult?
Get the transform result if a transformer step was run
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 |
#upscale ⇒ ImageUpscaleResult?
Get the upscale result if an upscaler step was run
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 |
#url ⇒ String?
Get the final image URL
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 |