Class: Brute::Middleware::CompactionCheck::Compactor
- Inherits:
-
Object
- Object
- Brute::Middleware::CompactionCheck::Compactor
- Defined in:
- lib/brute/middleware/040_compaction_check.rb
Overview
Context compaction service. When the conversation grows past configurable thresholds, older messages are summarized into a condensed form and the original messages are dropped, keeping the context window manageable.
Constant Summary collapse
- DEFAULTS =
{ token_threshold: 100_000, # Compact when estimated tokens exceed this message_threshold: 200, # Compact when message count exceeds this retention_window: 6, # Minimum recent messages to always keep summary_model: nil, # Model for summarization (uses agent's model if nil) }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#compact(messages) ⇒ Object
Compact the message history by summarizing older messages.
-
#initialize(provider, **opts) ⇒ Compactor
constructor
A new instance of Compactor.
-
#should_compact?(messages, usage: nil) ⇒ Boolean
Check whether compaction should run based on current context state.
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
62 63 64 |
# File 'lib/brute/middleware/040_compaction_check.rb', line 62 def config @config end |
Instance Method Details
#compact(messages) ⇒ Object
Compact the message history by summarizing older messages.
Returns [summary_message, kept_messages] — the caller rebuilds the context from these.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/brute/middleware/040_compaction_check.rb', line 80 def compact() total = .size keep_count = [@config[:retention_window], total].min return nil if total <= keep_count = [0...(total - keep_count)] = [(total - keep_count)..] summary_text = summarize() [summary_text, ] end |
#should_compact?(messages, usage: nil) ⇒ Boolean
Check whether compaction should run based on current context state.
70 71 72 73 74 |
# File 'lib/brute/middleware/040_compaction_check.rb', line 70 def should_compact?(, usage: nil) return true if .size > @config[:message_threshold] return true if usage && (usage[:total] || 0) > @config[:token_threshold] false end |