Class: Phronomy::Context::TrimContext
- Inherits:
-
Object
- Object
- Phronomy::Context::TrimContext
- Defined in:
- lib/phronomy/context/trim_context.rb
Overview
Context object passed to the +on_trim+ callback registered on an agent class.
The callback receives a TrimContext and may call #remove to drop specific messages from the conversation before the LLM is called. Changes affect only the current invocation; the underlying memory store is not modified.
Message elements are identified by a +:seq+ integer that is assigned sequentially (0-based) when messages are loaded from memory each turn.
Instance Attribute Summary collapse
-
#budget ⇒ Phronomy::Context::TokenBudget?
readonly
Token budget for this invocation.
-
#total_tokens ⇒ Integer
readonly
Total estimated token count of all current message elements.
Instance Method Summary collapse
-
#initialize(message_elements:, budget:) ⇒ TrimContext
constructor
private
A new instance of TrimContext.
-
#message_elements ⇒ Array<Hash>
private
Returns a snapshot of the current message elements (defensive copy).
-
#messages ⇒ Array
private
Convenience: returns the plain message objects (without element metadata).
-
#remove(seqs) ⇒ self
private
Remove messages identified by seq numbers.
Constructor Details
#initialize(message_elements:, budget:) ⇒ TrimContext
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of TrimContext.
32 33 34 35 36 |
# File 'lib/phronomy/context/trim_context.rb', line 32 def initialize(message_elements:, budget:) @message_elements = .dup @budget = budget recalculate! end |
Instance Attribute Details
#budget ⇒ Phronomy::Context::TokenBudget? (readonly)
Returns token budget for this invocation.
23 24 25 |
# File 'lib/phronomy/context/trim_context.rb', line 23 def budget @budget end |
#total_tokens ⇒ Integer (readonly)
Returns total estimated token count of all current message elements.
26 27 28 |
# File 'lib/phronomy/context/trim_context.rb', line 26 def total_tokens @total_tokens end |
Instance Method Details
#message_elements ⇒ Array<Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a snapshot of the current message elements (defensive copy). Each element is a Hash with +:seq+, +:message+, +:tokens+, and +:role+.
43 44 45 |
# File 'lib/phronomy/context/trim_context.rb', line 43 def @message_elements.dup end |
#messages ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convenience: returns the plain message objects (without element metadata).
64 65 66 |
# File 'lib/phronomy/context/trim_context.rb', line 64 def @message_elements.map { |e| e[:message] } end |
#remove(seqs) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Remove messages identified by seq numbers. Calling this multiple times accumulates removals.
53 54 55 56 57 58 |
# File 'lib/phronomy/context/trim_context.rb', line 53 def remove(seqs) seqs_set = Array(seqs).to_set @message_elements.reject! { |e| seqs_set.include?(e[:seq]) } recalculate! self end |