Class: Ask::Agent::Middleware::Pipeline
- Inherits:
-
Object
- Object
- Ask::Agent::Middleware::Pipeline
- Defined in:
- lib/ask/agent/middleware/pipeline.rb
Overview
A composable chain of middleware that wraps an LLM provider.
Middleware are applied in order — the first middleware registered wraps the outermost layer and sees the request first.
Constant Summary collapse
- KNOWN_MIDDLEWARES =
{ retry_on_failure: "Ask::Agent::Middleware::RetryOnFailure", log_calls: "Ask::Agent::Middleware::LogCalls", default_settings: "Ask::Agent::Middleware::DefaultSettings", model_fallback: "Ask::Agent::Middleware::ModelFallback" }.freeze
Instance Method Summary collapse
-
#configured? ⇒ Boolean
Whether any middleware has been registered.
-
#each(&block) ⇒ Object
Iterate over the configured middleware entries.
-
#initialize ⇒ Pipeline
constructor
A new instance of Pipeline.
-
#invoke(provider, request) { ... } ⇒ Object
Invoke the middleware chain around a provider call.
-
#use(middleware, **options) ⇒ Object
Register a middleware in the chain.
Constructor Details
#initialize ⇒ Pipeline
Returns a new instance of Pipeline.
26 27 28 |
# File 'lib/ask/agent/middleware/pipeline.rb', line 26 def initialize @entries = [] end |
Instance Method Details
#configured? ⇒ Boolean
Returns whether any middleware has been registered.
41 42 43 |
# File 'lib/ask/agent/middleware/pipeline.rb', line 41 def configured? @entries.any? end |
#each(&block) ⇒ Object
Iterate over the configured middleware entries.
46 47 48 |
# File 'lib/ask/agent/middleware/pipeline.rb', line 46 def each(&block) @entries.each(&block) end |
#invoke(provider, request) { ... } ⇒ Object
Invoke the middleware chain around a provider call.
Builds a chain of lambdas from the innermost (actual provider) outward, then invokes it. Each middleware's Base#around_request wraps the next link.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ask/agent/middleware/pipeline.rb', line 60 def invoke(provider, request) inner = -> { yield } chain = inner @entries.reverse_each do |entry| instance = entry[:klass].new(**entry[:options]) current = chain chain = -> { instance.around_request(provider, request) { current.call } } end chain.call end |
#use(middleware, **options) ⇒ Object
Register a middleware in the chain.
34 35 36 37 38 |
# File 'lib/ask/agent/middleware/pipeline.rb', line 34 def use(middleware, **) klass = resolve(middleware) @entries << { klass: klass, options: } self end |