Class: RubyLLM::Agents::Pipeline::Middleware::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/agents/pipeline/middleware/base.rb

Overview

This class is abstract.

Subclass and implement #call

Base class for all middleware in the pipeline.

Middleware wraps the next handler in the chain and can:

  • Modify the context before passing it down

  • Short-circuit the chain (e.g., return cached result)

  • Handle errors from downstream

  • Modify the context after the response

Each middleware receives:

  • @app: The next handler in the chain (another middleware or the executor)

  • @agent_class: The agent class, for reading DSL configuration

Examples:

Simple pass-through middleware

class Logger < Base
  def call(context)
    puts "Before: #{context.input}"
    result = @app.call(context)
    puts "After: #{context.output}"
    result
  end
end

Short-circuiting middleware

class Cache < Base
  def call(context)
    if (cached = read_cache(context))
      context.output = cached
      context.cached = true
      return context
    end
    @app.call(context)
  end
end

Direct Known Subclasses

Budget, Cache, Instrumentation, Reliability, Tenant

Constant Summary collapse

LOG_TAG =
"[RubyLLM::Agents::Pipeline]"

Instance Method Summary collapse

Constructor Details

#initialize(app, agent_class) ⇒ Base

Returns a new instance of Base.

Parameters:

  • app (#call)

    The next handler in the chain

  • agent_class (Class)

    The agent class (for reading DSL config)



48
49
50
51
# File 'lib/ruby_llm/agents/pipeline/middleware/base.rb', line 48

def initialize(app, agent_class)
  @app = app
  @agent_class = agent_class
end

Instance Method Details

#call(context) ⇒ Context

Process the context through this middleware

Subclasses must implement this method. The typical pattern is:

  1. Do pre-processing on context

  2. Call @app.call(context) to continue the chain

  3. Do post-processing on context

  4. Return context

Parameters:

  • context (Context)

    The execution context

Returns:

  • (Context)

    The (possibly modified) context

Raises:

  • (NotImplementedError)

    If not implemented by subclass



64
65
66
# File 'lib/ruby_llm/agents/pipeline/middleware/base.rb', line 64

def call(context)
  raise NotImplementedError, "#{self.class} must implement #call"
end