Class: RubyLLM::Agents::ImageEditResult

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

Overview

Result wrapper for image editing operations

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

Examples:

Accessing edited image

result = ImageEditor.call(
  image: "photo.png",
  mask: "mask.png",
  prompt: "Replace background"
)
result.url       # => "https://..."
result.success?  # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

included

Constructor Details

#initialize(images:, source_image:, mask:, prompt:, model_id:, size:, started_at:, completed_at:, tenant_id:, editor_class:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ ImageEditResult

Initialize a new result

Parameters:

  • images (Array<Object>)

    Array of edited image objects

  • source_image (String)

    The original source image

  • mask (String)

    The mask image used

  • prompt (String)

    The edit prompt

  • model_id (String)

    Model used for editing

  • size (String)

    Image size

  • started_at (Time)

    When editing started

  • completed_at (Time)

    When editing completed

  • tenant_id (String, nil)

    Tenant identifier

  • editor_class (String)

    Name of the editor 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
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 41

def initialize(images:, source_image:, mask:, prompt:, model_id:, size:,
  started_at:, completed_at:, tenant_id:, editor_class:,
  error_class: nil, error_message: nil, agent_class_name: nil)
  @images = images
  @source_image = source_image
  @mask = mask
  @prompt = prompt
  @model_id = model_id
  @size = size
  @started_at = started_at
  @completed_at = completed_at
  @tenant_id = tenant_id
  @editor_class = editor_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.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def completed_at
  @completed_at
end

#editor_classObject (readonly)

Returns the value of attribute editor_class.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def editor_class
  @editor_class
end

#error_classObject (readonly)

Returns the value of attribute error_class.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def error_class
  @error_class
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def error_message
  @error_message
end

#execution_idObject

Returns the value of attribute execution_id.



25
26
27
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 25

def execution_id
  @execution_id
end

#imagesObject (readonly)

Returns the value of attribute images.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def images
  @images
end

#maskObject (readonly)

Returns the value of attribute mask.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def mask
  @mask
end

#model_idObject (readonly)

Returns the value of attribute model_id.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def model_id
  @model_id
end

#promptObject (readonly)

Returns the value of attribute prompt.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def prompt
  @prompt
end

#sizeObject (readonly)

Returns the value of attribute size.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def size
  @size
end

#source_imageObject (readonly)

Returns the value of attribute source_image.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def source_image
  @source_image
end

#started_atObject (readonly)

Returns the value of attribute started_at.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def started_at
  @started_at
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



22
23
24
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 22

def tenant_id
  @tenant_id
end

Class Method Details

.from_cache(data) ⇒ Object



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

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

Instance Method Details

#base64?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 110

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

#batch?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 84

def batch?
  count > 1
end

#blobsObject



169
170
171
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 169

def blobs
  images.map(&:to_blob)
end

#countObject

Count



124
125
126
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 124

def count
  images.size
end

#dataObject



102
103
104
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 102

def data
  image&.data
end

#datasObject



106
107
108
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 106

def datas
  images.map(&:data).compact
end

#duration_msObject

Timing



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

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

#error?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 76

def error?
  !success?
end

#executionRubyLLM::Agents::Execution?

Loads the associated Execution record from the database

Returns:



66
67
68
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 66

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

#imageObject

Image access



90
91
92
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 90

def image
  images.first
end

#input_tokensObject



147
148
149
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 147

def input_tokens
  (prompt.length / 4.0).ceil
end

#mime_typeObject



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

def mime_type
  image&.mime_type
end

#revised_promptObject



118
119
120
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 118

def revised_prompt
  image&.revised_prompt
end

#save(path) ⇒ Object

File operations



153
154
155
156
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 153

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

#save_all(directory, prefix: "edited") ⇒ Object



158
159
160
161
162
163
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 158

def save_all(directory, prefix: "edited")
  images.each_with_index do |img, idx|
    filename = "#{prefix}_#{idx + 1}.png"
    img.save(File.join(directory, filename))
  end
end

#single?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 80

def single?
  count == 1
end

#success?Boolean

Status helpers

Returns:

  • (Boolean)


72
73
74
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 72

def success?
  error_class.nil? && images.any?
end

#to_blobObject



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

def to_blob
  image&.to_blob
end

#to_cacheObject

Caching



200
201
202
203
204
205
206
207
208
209
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 200

def to_cache
  {
    urls: urls,
    datas: datas,
    mime_type: mime_type,
    model_id: model_id,
    total_cost: total_cost,
    cached_at: Time.current.iso8601
  }
end

#to_hObject

Serialization



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 175

def to_h
  {
    success: success?,
    count: count,
    urls: urls,
    base64: base64?,
    mime_type: mime_type,
    source_image: source_image,
    prompt: prompt,
    model_id: model_id,
    size: size,
    total_cost: total_cost,
    duration_ms: duration_ms,
    started_at: started_at&.iso8601,
    completed_at: completed_at&.iso8601,
    tenant_id: tenant_id,
    editor_class: editor_class,
    error_class: error_class,
    error_message: error_message,
    execution_id: execution_id
  }
end

#total_costObject

Cost estimation



137
138
139
140
141
142
143
144
145
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 137

def total_cost
  return 0 if error?

  ImageGenerator::Pricing.calculate_cost(
    model_id: model_id,
    size: size,
    count: count
  )
end

#urlObject



94
95
96
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 94

def url
  image&.url
end

#urlsObject



98
99
100
# File 'lib/ruby_llm/agents/results/image_edit_result.rb', line 98

def urls
  images.map(&:url).compact
end