Class: Brute::Middleware::CompactionCheck

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

Overview

Checks context size after each LLM call and triggers compaction when thresholds are exceeded.

It should add a compaction event to the logs with the context token total listed… this way a model that supports extra context can include the compaction as well as the previous messages…

Or an LLM that doesn’t support it can just use the messages that come after the compaction

Defined Under Namespace

Classes: Compactor

Instance Method Summary collapse

Constructor Details

#initialize(app, compactor: nil, system_prompt:, **compactor_opts) ⇒ CompactionCheck

Returns a new instance of CompactionCheck.



19
20
21
22
23
24
# File 'lib/brute/middleware/040_compaction_check.rb', line 19

def initialize(app, compactor: nil, system_prompt:, **compactor_opts)
  @app = app
  @compactor = compactor
  @compactor_opts = compactor_opts
  @system_prompt = system_prompt
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
47
48
49
# File 'lib/brute/middleware/040_compaction_check.rb', line 26

def call(env)
  #@compactor ||= Compactor.new(env[:provider], **@compactor_opts)

  #messages = env[:messages]
  #usage = env[:metadata].dig(:tokens, :last_call)

  #if @compactor.should_compact?(messages, usage: usage)
  #  result = @compactor.compact(messages)
  #  if result
  #    summary_text, _recent = result
  #    env[:metadata][:compaction] = {
  #      messages_before: messages.size,
  #      timestamp: Time.now.iso8601,
  #    }
  #    # Replace the message history with the summary
  #    env[:messages] = [
  #      RubyLLM::Message.new(role: :system, content: @system_prompt),
  #      RubyLLM::Message.new(role: :user, content: "[Previous conversation summary]\n\n#{summary_text}"),
  #    ]
  #  end
  #end

  @app.call(env)
end