Class: Brute::Middleware::OpenRouter::Completion

Inherits:
Object
  • Object
show all
Defined in:
lib/brute/middleware/open_router.rb

Instance Method Summary collapse

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: {}, **options)
  @app = app
  @config = config
  @options = ::OpenRouter::CompletionOptions.new(**options)
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)

  messages = Brute::MessageTransport::OpenRouter.dump_all(env[:messages])

  ::OpenRouter::Client.new(**@config).then do |client|
    client.complete(messages, @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 |message|
        env[:messages] << message
      end
    end
  end

  env[:hooks]&.emit(:after_llm, env)
  env
end