Module: LlmCostTracker::Billing::Components

Defined in:
lib/llm_cost_tracker/billing/components.rb

Defined Under Namespace

Classes: Component

Constant Summary collapse

REQUIRED_FIELDS =
%i[key kind direction modality cache_state unit category].freeze
DEFINITIONS_PATH =
File.expand_path("components.yml", __dir__)
REGISTRY =
load_registry
BY_KEY =
REGISTRY.to_h { |component| [component.key, component] }.freeze
TOKEN_PRICED =
REGISTRY.select { |component| component.token_key && component.cost_key }.freeze

Class Method Summary collapse

Class Method Details

.build(attributes) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/llm_cost_tracker/billing/components.rb', line 31

def self.build(attributes)
  missing = REQUIRED_FIELDS - attributes.keys
  raise Error, "components.yml entry missing #{missing.join(', ')}: #{attributes.inspect}" if missing.any?

  Component.new(
    key: attributes.fetch(:key).to_sym,
    kind: attributes.fetch(:kind).to_sym,
    direction: attributes.fetch(:direction).to_sym,
    modality: attributes.fetch(:modality).to_sym,
    cache_state: attributes.fetch(:cache_state).to_sym,
    unit: attributes.fetch(:unit).to_sym,
    category: attributes.fetch(:category).to_sym,
    token_key: attributes[:token_key]&.to_sym,
    cost_key: attributes[:cost_key]&.to_sym
  )
end

.load_registryObject



25
26
27
28
29
# File 'lib/llm_cost_tracker/billing/components.rb', line 25

def self.load_registry
  Psych.safe_load_file(DEFINITIONS_PATH, permitted_classes: [], symbolize_names: true)
       .map { |attributes| build(attributes) }
       .freeze
end