Class: Kward::ContextBudgetMeter
- Inherits:
-
Object
- Object
- Kward::ContextBudgetMeter
- Defined in:
- lib/kward/context_budget_meter.rb
Overview
Tracks approximate context bytes saved by tool budgeting during one process.
Defined Under Namespace
Classes: Snapshot
Instance Method Summary collapse
-
#initialize ⇒ ContextBudgetMeter
constructor
A new instance of ContextBudgetMeter.
- #record(tool_name:, original_bytes:, returned_bytes:) ⇒ Object
- #snapshot ⇒ Object
Constructor Details
#initialize ⇒ ContextBudgetMeter
Returns a new instance of ContextBudgetMeter.
7 8 9 10 11 12 13 |
# File 'lib/kward/context_budget_meter.rb', line 7 def initialize @mutex = Mutex.new @calls = 0 @original_bytes = 0 @returned_bytes = 0 @tool_breakdown = Hash.new { |hash, key| hash[key] = { calls: 0, originalBytes: 0, returnedBytes: 0, savedBytes: 0 } } end |
Instance Method Details
#record(tool_name:, original_bytes:, returned_bytes:) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/kward/context_budget_meter.rb', line 15 def record(tool_name:, original_bytes:, returned_bytes:) original_bytes = original_bytes.to_i returned_bytes = returned_bytes.to_i saved_bytes = [original_bytes - returned_bytes, 0].max @mutex.synchronize do @calls += 1 @original_bytes += original_bytes @returned_bytes += returned_bytes entry = @tool_breakdown[tool_name.to_s] entry[:calls] += 1 entry[:originalBytes] += original_bytes entry[:returnedBytes] += returned_bytes entry[:savedBytes] += saved_bytes end saved_bytes end |
#snapshot ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kward/context_budget_meter.rb', line 32 def snapshot @mutex.synchronize do Snapshot.new( calls: @calls, original_bytes: @original_bytes, returned_bytes: @returned_bytes, saved_bytes: [@original_bytes - @returned_bytes, 0].max, tool_breakdown: @tool_breakdown.transform_values(&:dup) ) end end |