Class: LlmCostTracker::Pricing::Mode

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/pricing/mode.rb

Constant Summary collapse

STANDARD_MODE_VALUES =
%i[auto default standard standard_only].freeze
COMPOUND_MODIFIERS =
%i[data_residency].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modifiers) ⇒ Mode

Returns a new instance of Mode.



68
69
70
71
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 68

def initialize(modifiers)
  @modifiers = Array(modifiers).map(&:to_sym).uniq.sort
  freeze
end

Instance Attribute Details

#modifiersObject (readonly)

Returns the value of attribute modifiers.



9
10
11
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 9

def modifiers
  @modifiers
end

Class Method Details

.merge(provider_mode, request_mode) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 20

def self.merge(provider_mode, request_mode)
  return normalize(request_mode) if provider_mode.to_s.strip.empty?

  provider_tokens = tokenize(provider_mode) - STANDARD_MODE_VALUES
  request_host_tokens = tokenize(request_mode || "") & COMPOUND_MODIFIERS
  combined = provider_tokens | request_host_tokens
  return nil if combined.empty?

  normalize(combined.join("_"))
end

.normalize(value) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 11

def self.normalize(value)
  return nil if value.nil?

  symbol = normalize_string(value.to_s)
  return nil unless symbol

  STANDARD_MODE_VALUES.include?(symbol) ? nil : symbol
end

.parse(value) ⇒ Object



39
40
41
42
43
44
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 39

def self.parse(value)
  return value if value.is_a?(self)
  return new([]) if value.nil?

  new(tokenize(value.to_s))
end

.tokenize(value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 46

def self.tokenize(value)
  remaining = value.to_s.downcase.tr("-", "_")
  tokens = []
  loop do
    break if remaining.empty?

    compound = COMPOUND_MODIFIERS.find do |token|
      name = token.name
      remaining == name || remaining.start_with?("#{name}_")
    end
    if compound
      tokens << compound
      remaining = remaining.delete_prefix(compound.name).delete_prefix("_")
    else
      first, _, rest = remaining.partition("_")
      tokens << first.to_sym unless first.empty?
      remaining = rest
    end
  end
  tokens
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



96
97
98
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 96

def ==(other)
  other.is_a?(self.class) && modifiers == other.modifiers
end

#canonicalObject Also known as: to_s



81
82
83
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 81

def canonical
  modifiers.join("_")
end

#empty?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 73

def empty?
  modifiers.empty?
end

#hashObject



101
102
103
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 101

def hash
  modifiers.hash
end

#include?(modifier) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 77

def include?(modifier)
  modifiers.include?(modifier.to_sym)
end

#permutationsObject



90
91
92
93
94
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 90

def permutations
  return [canonical] if modifiers.size <= 1

  modifiers.permutation.map { |permutation| permutation.join("_") }.uniq
end

#to_symObject



86
87
88
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 86

def to_sym
  empty? ? nil : canonical.to_sym
end