Class: RubyLLM::Agents::BackgroundRemovalResult

Inherits:
Object
  • Object
show all
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.

Examples:

Accessing removal result

result = BackgroundRemover.call(image: "photo.jpg")
result.url        # => "https://..." (transparent PNG)
result.has_alpha? # => true
result.mask       # => Segmentation mask (if requested)
result.success?   # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

included

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

Parameters:

  • foreground (Object)

    The extracted foreground image

  • mask (Object, nil)

    The segmentation mask (if requested)

  • source_image (String)

    The original source image

  • model_id (String)

    Model used for removal

  • output_format (Symbol)

    Output format used

  • alpha_matting (Boolean)

    Whether alpha matting was used

  • refine_edges (Boolean)

    Whether edge refinement was used

  • started_at (Time)

    When removal started

  • completed_at (Time)

    When removal completed

  • tenant_id (String, nil)

    Tenant identifier

  • remover_class (String)

    Name of the remover class

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

    Error class name if failed

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

    Error message if failed



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 = error_message
  @execution_id = nil

  # Tracking
  @agent_class_name = agent_class_name
  register_with_tracker
end

Instance Attribute Details

#alpha_mattingObject (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_atObject (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_classObject (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_messageObject (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
  @error_message
end

#execution_idObject

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

#foregroundObject (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

#maskObject (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_idObject (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_formatObject (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_edgesObject (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_classObject (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_imageObject (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_atObject (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_idObject (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

Returns:

  • (Boolean)


112
113
114
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 112

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

#batch?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 86

def batch?
  false
end

#blobsObject



187
188
189
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 187

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

#countObject

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

#dataObject



104
105
106
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 104

def data
  foreground&.data
end

#datasObject



108
109
110
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 108

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

#duration_msObject

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

Returns:

  • (Boolean)


77
78
79
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 77

def error?
  !success?
end

#executionRubyLLM::Agents::Execution?

Loads the associated Execution record from the database

Returns:



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)

Returns:

  • (Boolean)


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

#imageObject

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

Returns:

  • (Boolean)


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

def mask?
  !mask.nil?
end

#mask_blobObject



183
184
185
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 183

def mask_blob
  mask&.to_blob
end

#mask_dataObject



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

def mask_data
  mask&.data
end

#mask_urlObject



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

def mask_url
  mask&.url
end

#mime_typeObject



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

Returns:

  • (Boolean)


82
83
84
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 82

def single?
  true
end

#success?Boolean

Status helpers

Returns:

  • (Boolean)


73
74
75
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 73

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

#to_blobObject



179
180
181
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 179

def to_blob
  foreground&.to_blob
end

#to_cacheObject

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_hObject

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: error_message,
    execution_id: execution_id
  }
end

#total_costObject

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

#urlObject



96
97
98
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 96

def url
  foreground&.url
end

#urlsObject



100
101
102
# File 'lib/ruby_llm/agents/results/background_removal_result.rb', line 100

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