Class: Phronomy::Context::TriggerContext

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/context/trigger_context.rb

Overview

Read-only context passed to the +on_compaction_trigger+ callback.

The callback inspects the current message list and budget, then returns a truthy value to trigger compaction or a falsy value to skip it.

No mutations are allowed through this object; use CompactionContext (passed to +on_compact+) for actual modifications.

Examples:

Trigger compaction when messages exceed 80% of the input budget

on_compaction_trigger do |ctx|
  limit = ctx.budget&.available(used: 0) || Float::INFINITY
  ctx.total_tokens > limit * 0.8
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_elements:, budget:) ⇒ TriggerContext

Returns a new instance of TriggerContext.

Parameters:



31
32
33
34
35
# File 'lib/phronomy/context/trigger_context.rb', line 31

def initialize(message_elements:, budget:)
  @message_elements = message_elements.dup.freeze
  @budget = budget
  @total_tokens = message_elements.sum { |e| e[:tokens] }
end

Instance Attribute Details

#budgetPhronomy::Context::TokenBudget? (readonly)

Returns token budget for this invocation.

Returns:



24
25
26
# File 'lib/phronomy/context/trigger_context.rb', line 24

def budget
  @budget
end

#message_elementsArray<Hash> (readonly)

Returns frozen snapshot of message elements each element: { seq: Integer, message: Object, tokens: Integer, role: Symbol }.

Returns:

  • (Array<Hash>)

    frozen snapshot of message elements each element: { seq: Integer, message: Object, tokens: Integer, role: Symbol }



21
22
23
# File 'lib/phronomy/context/trigger_context.rb', line 21

def message_elements
  @message_elements
end

#total_tokensInteger (readonly)

Returns total estimated token count of all message elements.

Returns:

  • (Integer)

    total estimated token count of all message elements



27
28
29
# File 'lib/phronomy/context/trigger_context.rb', line 27

def total_tokens
  @total_tokens
end