Module: RubyLLM::Agents::ImagePipeline::DSL

Defined in:
lib/ruby_llm/agents/image/pipeline/dsl.rb

Overview

DSL for defining image pipeline steps and configuration

Provides methods for configuring pipeline steps, callbacks, caching, and error handling behavior.

Examples:

Defining pipeline steps

class MyPipeline < ImagePipeline
  step :generate, generator: LogoGenerator
  step :upscale, upscaler: PhotoUpscaler, scale: 4
  step :analyze, analyzer: ProductAnalyzer

  description "Complete product image pipeline"
  stop_on_error true
end

Instance Method Summary collapse

Instance Method Details

#after_pipeline(method_name = nil) { ... } ⇒ void

This method returns an undefined value.

Add a callback to run after the pipeline

Examples:

after_pipeline :add_watermark
after_pipeline { |result| notify_completion(result) }

Parameters:

  • method_name (Symbol) (defaults to: nil)

    Method to call

Yields:

  • Block to execute



93
94
95
96
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 93

def after_pipeline(method_name = nil, &block)
  @callbacks ||= {before: [], after: []}
  @callbacks[:after] << (block || method_name)
end

#before_pipeline(method_name = nil) { ... } ⇒ void

This method returns an undefined value.

Add a callback to run before the pipeline

Examples:

before_pipeline :validate_inputs
before_pipeline { |ctx| ctx[:started_at] = Time.current }

Parameters:

  • method_name (Symbol) (defaults to: nil)

    Method to call

Yields:

  • Block to execute



78
79
80
81
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 78

def before_pipeline(method_name = nil, &block)
  @callbacks ||= {before: [], after: []}
  @callbacks[:before] << (block || method_name)
end

#cache_enabled?Boolean

Check if caching is enabled

Returns:

  • (Boolean)

    true if caching is enabled



134
135
136
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 134

def cache_enabled?
  !cache_ttl.nil?
end

#cache_for(ttl) ⇒ Object

Enable caching with the given TTL

Parameters:

  • ttl (ActiveSupport::Duration, Integer)

    Cache duration



120
121
122
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 120

def cache_for(ttl)
  @cache_ttl = ttl
end

#cache_ttlActiveSupport::Duration, ...

Get the cache TTL

Returns:

  • (ActiveSupport::Duration, Integer, nil)

    The cache TTL



127
128
129
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 127

def cache_ttl
  @cache_ttl || inherited_or_default(:cache_ttl, nil)
end

#callbacksHash

Get callbacks

Returns:

  • (Hash)

    Hash with :before and :after arrays



101
102
103
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 101

def callbacks
  @callbacks ||= {before: [], after: []}
end

#description(value = nil) ⇒ String?

Set or get the description

Parameters:

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

    Description

Returns:

  • (String, nil)

    The description



109
110
111
112
113
114
115
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 109

def description(value = nil)
  if value
    @description = value
  else
    @description || inherited_or_default(:description, nil)
  end
end

#step(name, **config) ⇒ void

This method returns an undefined value.

Define a pipeline step

Examples:

Different step types

step :generate, generator: MyGenerator
step :upscale, upscaler: MyUpscaler, scale: 2
step :transform, transformer: StyleTransformer, strength: 0.7
step :analyze, analyzer: ContentAnalyzer
step :remove_bg, remover: BackgroundRemover

Conditional steps

step :upscale, upscaler: PhotoUpscaler, if: ->(ctx) { ctx[:high_quality] }
step :remove_bg, remover: BackgroundRemover, unless: ->(ctx) { ctx[:keep_background] }

Parameters:

  • name (Symbol)

    Step name (must be unique)

  • config (Hash)

    Step configuration

Options Hash (**config):

  • :generator (Class)

    ImageGenerator class for generation steps

  • :variator (Class)

    ImageVariator class for variation steps

  • :editor (Class)

    ImageEditor class for editing steps

  • :transformer (Class)

    ImageTransformer class for transformation steps

  • :upscaler (Class)

    ImageUpscaler class for upscaling steps

  • :analyzer (Class)

    ImageAnalyzer class for analysis steps

  • :remover (Class)

    BackgroundRemover class for background removal steps

  • :if (Proc)

    Conditional proc that receives context and returns boolean

  • :unless (Proc)

    Conditional proc that receives context and returns boolean



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 48

def step(name, **config)
  @steps ||= []

  # Validate step configuration
  validate_step_config!(name, config)

  @steps << {
    name: name,
    config: config,
    type: determine_step_type(config)
  }
end

#stepsArray<Hash>

Get all defined steps

Returns:

  • (Array<Hash>)

    Array of step definitions



64
65
66
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 64

def steps
  @steps ||= []
end

#stop_on_error(value = nil) ⇒ Boolean Also known as: stop_on_error?

Set whether to stop on error (default: true)

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    Whether to stop on first error

Returns:

  • (Boolean)

    Current setting



142
143
144
145
146
147
148
149
# File 'lib/ruby_llm/agents/image/pipeline/dsl.rb', line 142

def stop_on_error(value = nil)
  if value.nil?
    return @stop_on_error if defined?(@stop_on_error) && !@stop_on_error.nil?
    inherited_or_default(:stop_on_error, true)
  else
    @stop_on_error = value
  end
end