Class: RubyLLM::Agents::BackgroundRemovalResult
- Inherits:
-
Object
- Object
- RubyLLM::Agents::BackgroundRemovalResult
- Includes:
- Trackable
- Defined in:
- lib/ruby_llm/agents/results/background_removal_result.rb
Overview
Result wrapper for background removal operations
Provides a consistent interface for accessing the extracted foreground, segmentation mask, and metadata.
Instance Attribute Summary collapse
-
#alpha_matting ⇒ Object
readonly
Returns the value of attribute alpha_matting.
-
#completed_at ⇒ Object
readonly
Returns the value of attribute completed_at.
-
#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.
-
#foreground ⇒ Object
readonly
Returns the value of attribute foreground.
-
#mask ⇒ Object
readonly
Returns the value of attribute mask.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#output_format ⇒ Object
readonly
Returns the value of attribute output_format.
-
#refine_edges ⇒ Object
readonly
Returns the value of attribute refine_edges.
-
#remover_class ⇒ Object
readonly
Returns the value of attribute remover_class.
-
#source_image ⇒ Object
readonly
Returns the value of attribute source_image.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#tenant_id ⇒ Object
readonly
Returns the value of attribute tenant_id.
Class Method Summary collapse
Instance Method Summary collapse
- #base64? ⇒ Boolean
- #batch? ⇒ Boolean
- #blobs ⇒ Object
-
#count ⇒ Object
Count (always 1 for removal).
- #data ⇒ Object
- #datas ⇒ Object
-
#duration_ms ⇒ Object
Timing.
- #error? ⇒ Boolean
-
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database.
-
#has_alpha? ⇒ Boolean
Check if result has alpha channel (transparency).
-
#image ⇒ Object
Image access (foreground).
-
#initialize(foreground:, mask:, source_image:, model_id:, output_format:, alpha_matting:, refine_edges:, started_at:, completed_at:, tenant_id:, remover_class:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ BackgroundRemovalResult
constructor
Initialize a new result.
-
#mask? ⇒ Boolean
Mask access.
- #mask_blob ⇒ Object
- #mask_data ⇒ Object
- #mask_url ⇒ Object
- #mime_type ⇒ Object
-
#save(path) ⇒ Object
File operations.
- #save_mask(path) ⇒ Object
-
#single? ⇒ Boolean
Always single for background removal.
-
#success? ⇒ Boolean
Status helpers.
- #to_blob ⇒ Object
-
#to_cache ⇒ Object
Caching.
-
#to_h ⇒ Object
Serialization.
-
#total_cost ⇒ Object
Cost estimation.
- #url ⇒ Object
- #urls ⇒ Object
Methods included from Trackable
Constructor Details
#initialize(foreground:, mask:, source_image:, model_id:, output_format:, alpha_matting:, refine_edges:, started_at:, completed_at:, tenant_id:, remover_class:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ BackgroundRemovalResult
Initialize a new result
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 41 def initialize(foreground:, mask:, source_image:, model_id:, output_format:, alpha_matting:, refine_edges:, started_at:, completed_at:, tenant_id:, remover_class:, error_class: nil, error_message: nil, agent_class_name: nil) @foreground = foreground @mask = mask @source_image = source_image @model_id = model_id @output_format = output_format @alpha_matting = alpha_matting @refine_edges = refine_edges @started_at = started_at @completed_at = completed_at @tenant_id = tenant_id @remover_class = remover_class @error_class = error_class @error_message = @execution_id = nil # Tracking @agent_class_name = agent_class_name register_with_tracker end |
Instance Attribute Details
#alpha_matting ⇒ Object (readonly)
Returns the value of attribute alpha_matting.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def alpha_matting @alpha_matting end |
#completed_at ⇒ Object (readonly)
Returns the value of attribute completed_at.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def completed_at @completed_at end |
#error_class ⇒ Object (readonly)
Returns the value of attribute error_class.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def error_class @error_class end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def @error_message end |
#execution_id ⇒ Object
Returns the value of attribute execution_id.
24 25 26 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 24 def execution_id @execution_id end |
#foreground ⇒ Object (readonly)
Returns the value of attribute foreground.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def foreground @foreground end |
#mask ⇒ Object (readonly)
Returns the value of attribute mask.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def mask @mask end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def model_id @model_id end |
#output_format ⇒ Object (readonly)
Returns the value of attribute output_format.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def output_format @output_format end |
#refine_edges ⇒ Object (readonly)
Returns the value of attribute refine_edges.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def refine_edges @refine_edges end |
#remover_class ⇒ Object (readonly)
Returns the value of attribute remover_class.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def remover_class @remover_class end |
#source_image ⇒ Object (readonly)
Returns the value of attribute source_image.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def source_image @source_image end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def started_at @started_at end |
#tenant_id ⇒ Object (readonly)
Returns the value of attribute tenant_id.
20 21 22 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 20 def tenant_id @tenant_id end |
Class Method Details
.from_cache(data) ⇒ Object
235 236 237 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 235 def self.from_cache(data) CachedBackgroundRemovalResult.new(data) end |
Instance Method Details
#base64? ⇒ Boolean
112 113 114 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 112 def base64? foreground&.base64? || false end |
#batch? ⇒ Boolean
86 87 88 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 86 def batch? false end |
#blobs ⇒ Object
187 188 189 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 187 def blobs success? ? [to_blob].compact : [] end |
#count ⇒ Object
Count (always 1 for removal)
144 145 146 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 144 def count success? ? 1 : 0 end |
#data ⇒ Object
104 105 106 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 104 def data foreground&.data end |
#datas ⇒ Object
108 109 110 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 108 def datas success? ? [data].compact : [] end |
#duration_ms ⇒ Object
Timing
150 151 152 153 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 150 def duration_ms return 0 unless started_at && completed_at ((completed_at - started_at) * 1000).round end |
#error? ⇒ Boolean
77 78 79 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 77 def error? !success? end |
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database
67 68 69 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 67 def execution @execution ||= RubyLLM::Agents::Execution.find_by(id: execution_id) if execution_id end |
#has_alpha? ⇒ Boolean
Check if result has alpha channel (transparency)
135 136 137 138 139 140 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 135 def has_alpha? return false if error? # PNG and WebP support alpha %i[png webp].include?(output_format) end |
#image ⇒ Object
Image access (foreground)
92 93 94 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 92 def image foreground end |
#mask? ⇒ Boolean
Mask access
122 123 124 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 122 def mask? !mask.nil? end |
#mask_blob ⇒ Object
183 184 185 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 183 def mask_blob mask&.to_blob end |
#mask_data ⇒ Object
130 131 132 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 130 def mask_data mask&.data end |
#mask_url ⇒ Object
126 127 128 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 126 def mask_url mask&.url end |
#mime_type ⇒ Object
116 117 118 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 116 def mime_type foreground&.mime_type || "image/#{output_format}" end |
#save(path) ⇒ Object
File operations
169 170 171 172 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 169 def save(path) raise "No foreground image to save" unless foreground foreground.save(path) end |
#save_mask(path) ⇒ Object
174 175 176 177 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 174 def save_mask(path) raise "No mask to save" unless mask mask.save(path) end |
#single? ⇒ Boolean
Always single for background removal
82 83 84 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 82 def single? true end |
#success? ⇒ Boolean
Status helpers
73 74 75 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 73 def success? error_class.nil? && !foreground.nil? end |
#to_blob ⇒ Object
179 180 181 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 179 def to_blob foreground&.to_blob end |
#to_cache ⇒ Object
Caching
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 221 def to_cache { url: url, data: data, mask_url: mask_url, mask_data: mask_data, mime_type: mime_type, model_id: model_id, output_format: output_format, total_cost: total_cost, cached_at: Time.current.iso8601 } end |
#to_h ⇒ Object
Serialization
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 193 def to_h { success: success?, url: url, mask_url: mask_url, base64: base64?, mime_type: mime_type, has_alpha: has_alpha?, has_mask: mask?, source_image: source_image, model_id: model_id, output_format: output_format, alpha_matting: alpha_matting, refine_edges: refine_edges, total_cost: total_cost, duration_ms: duration_ms, started_at: started_at&.iso8601, completed_at: completed_at&.iso8601, tenant_id: tenant_id, remover_class: remover_class, error_class: error_class, error_message: , execution_id: execution_id } end |
#total_cost ⇒ Object
Cost estimation
157 158 159 160 161 162 163 164 165 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 157 def total_cost return 0 if error? # Background removal typically has fixed per-image cost ImageGenerator::Pricing.calculate_cost( model_id: model_id, count: 1 ) end |
#url ⇒ Object
96 97 98 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 96 def url foreground&.url end |
#urls ⇒ Object
100 101 102 |
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 100 def urls success? ? [url].compact : [] end |