Class: Brute::Middleware::ReasoningNormalizer

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

Overview

Handles reasoning/thinking content across model switches.

PRE-call:

- If reasoning is enabled, injects provider-specific params into
  the env (e.g., Anthropic thinking config, OpenAI reasoning_effort).
- Tracks which model produced each message. When the model changes,
  strips reasoning_content from messages produced by the old model
  (signatures are model-specific and cryptographically tied).

POST-call:

- Records the current model on the response for future normalization.

llm.rb exposes:

- response.reasoning_content  — the thinking text
- response.reasoning_tokens   — token count
- Provider params pass-through — we can send thinking:, reasoning_effort:, etc.

Constant Summary collapse

EFFORT_LEVELS =

Effort levels that map to provider-specific params. Mirrors forgecode’s Effort enum.

{
  none: "none",
  minimal: "low",
  low: "low",
  medium: "medium",
  high: "high",
  xhigh: "high",
  max: "high",
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, model_id: nil, effort: :medium, enabled: true, budget_tokens: nil) ⇒ ReasoningNormalizer

Returns a new instance of ReasoningNormalizer.



40
41
42
43
44
45
46
47
# File 'lib/brute/middleware/reasoning_normalizer.rb', line 40

def initialize(app, model_id: nil, effort: :medium, enabled: true, budget_tokens: nil)
  super(app)
  @model_id = model_id
  @effort = effort
  @enabled = enabled
  @budget_tokens = budget_tokens
  @message_models = [] # tracks which model produced each assistant message
end

Instance Method Details

#call(env) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/brute/middleware/reasoning_normalizer.rb', line 49

def call(env)
  if @enabled
    inject_reasoning_params!(env)
  end

  response = @app.call(env)

  # POST: record which model produced this response
  if response
    @message_models << @model_id
  end

  response
end

#model_id=(new_model) ⇒ Object

Update the active model (e.g., when user switches models mid-session).



65
66
67
# File 'lib/brute/middleware/reasoning_normalizer.rb', line 65

def model_id=(new_model)
  @model_id = new_model
end