Class: Smith::Workflow::Composite::BudgetAllocator

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/smith/workflow/composite/budget_allocator.rb

Constant Summary collapse

TOKEN_DIMENSIONS =
%i[total_tokens token_limit].freeze
SUPPORTED_DIMENSIONS =
(TOKEN_DIMENSIONS + %i[total_cost]).freeze

Instance Method Summary collapse

Constructor Details

#initializeBudgetAllocator

Returns a new instance of BudgetAllocator.



22
23
24
25
26
27
28
29
30
# File 'lib/smith/workflow/composite/budget_allocator.rb', line 22

def initialize(...)
  super
  unless ledger.is_a?(Budget::Ledger)
    raise ArgumentError, "composite budget allocation requires a budget ledger"
  end

  Plan.validate_branch_count!(branch_count)
  @remaining = ledger.limits.to_h { |dimension, _| [dimension, ledger.remaining(dimension)] }.freeze
end

Instance Method Details

#call(ordinal:, caps: {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/smith/workflow/composite/budget_allocator.rb', line 32

def call(ordinal:, caps: {})
  validate_ordinal!(ordinal)

  @remaining.to_h do |dimension, amount|
    [dimension, capped_share(dimension, amount, ordinal, caps[dimension])]
  end.freeze
end