class Pipeline
Generic middleware machinery. Builds a chain of middleware around
a terminal app, exposes call(env) to invoke it.
Subclasses (Agent, Tool) override call to translate their public
arguments into an env hash, then delegate to super.
class MyPipeline < Brute::Pipeline
def call(input)
env = input: input, output: nil
super(env)
env[:output]
end
end
Nested
Definitions
def use(klass, *args, **kwargs, &block)
Register a middleware class.
The class must implement initialize(app, *args, **kwargs) and call(env).
Implementation
def use(klass, *args, **kwargs, &block)
tap { @middlewares << [klass, args, kwargs, block] }
end
def run(app)
Set the terminal app (innermost handler). Accepts an instance (anything responding to #call(env)) or a class.
Implementation
def run(app)
tap { @app = app }
end
def call(env)
Invoke the chain. Subclasses typically override this to shape env and extract a return value.
Implementation
def call(env)
build.call(env)
end
def build
Build the chain without calling it. Useful for inspection or caching.
Implementation
def build
raise "Stack has no terminal app — call `run` first" unless @app
@middlewares.reverse.inject(@app) do |inner, (klass, args, kwargs, block)|
if block
klass.new(inner, *args, **kwargs, &block)
else
klass.new(inner, *args, **kwargs)
end
end
end