Class: Brute::Middleware::CompactionCheck
- Defined in:
- lib/brute/middleware/compaction_check.rb
Overview
Checks context size after each LLM call and triggers compaction when thresholds are exceeded.
Runs POST-call: inspects message count and token usage from the response. If compaction is needed, summarizes older messages and rebuilds the context with the summary + recent messages.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, compactor:, system_prompt:, tools:, stream: nil) ⇒ CompactionCheck
constructor
A new instance of CompactionCheck.
Constructor Details
#initialize(app, compactor:, system_prompt:, tools:, stream: nil) ⇒ CompactionCheck
Returns a new instance of CompactionCheck.
18 19 20 21 22 23 24 |
# File 'lib/brute/middleware/compaction_check.rb', line 18 def initialize(app, compactor:, system_prompt:, tools:, stream: nil) super(app) @compactor = compactor @system_prompt = system_prompt @tools = tools @stream = stream end |
Instance Method Details
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/brute/middleware/compaction_check.rb', line 26 def call(env) response = @app.call(env) ctx = env[:context] = ctx..to_a.compact usage = ctx.usage rescue nil if @compactor.should_compact?(, usage: usage) result = @compactor.compact() if result summary_text, _recent = result rebuild_context!(env, summary_text) env[:metadata][:compaction] = { messages_before: .size, timestamp: Time.now.iso8601, } end end response end |