Class: Brute::Middleware::OpenRouter::Completion
- Inherits:
-
Object
- Object
- Brute::Middleware::OpenRouter::Completion
- Defined in:
- lib/brute/middleware/open_router.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, config: {}, **options) ⇒ Completion
constructor
config: keyword arguments for OpenRouter::Client.new (access_token:, request_timeout:, uri_base:, extra_headers:).
Constructor Details
#initialize(app, config: {}, **options) ⇒ Completion
config: keyword arguments for OpenRouter::Client.new (access_token:, request_timeout:, uri_base:, extra_headers:). Defaults to OpenRouter.configuration's global settings. options: keyword arguments for OpenRouter::CompletionOptions.new (model:, temperature:, tools:, ...).
12 13 14 15 16 |
# File 'lib/brute/middleware/open_router.rb', line 12 def initialize(app, config: {}, **) @app = app @config = config @options = ::OpenRouter::CompletionOptions.new(**) end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/brute/middleware/open_router.rb', line 18 def call(env) env[:hooks]&.emit(:before_llm, env) = Brute::MessageTransport::OpenRouter.dump_all(env[:messages]) ::OpenRouter::Client.new(**@config).then do |client| client.complete(, @options).then do |response| # Expose the provider's usage for downstream accounting # middleware (goal budgets, autonomous limits, compaction # thresholds, usage attribution) — additive metadata only. if response.respond_to?(:usage) && response.usage (env[:metadata] ||= {})[:last_llm_usage] = response.usage end # OpenRouter in fact only returns a single message... # https://github.com/estiens/open_router_enhanced/blob/main/lib/open_router/response.rb Brute::MessageTransport::OpenRouter.wrap_each(response) do || env[:messages] << end end end env[:hooks]&.emit(:after_llm, env) env end |