Class: Aidp::Harness::State::ProviderState

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/harness/state/provider_state.rb

Overview

Manages provider-specific state and rate limiting

Instance Method Summary collapse

Constructor Details

#initialize(persistence) ⇒ ProviderState

Returns a new instance of ProviderState.



8
9
10
# File 'lib/aidp/harness/state/provider_state.rb', line 8

def initialize(persistence)
  @persistence = persistence
end

Instance Method Details

#next_provider_reset_timeObject



40
41
42
43
44
# File 'lib/aidp/harness/state/provider_state.rb', line 40

def next_provider_reset_time
  rate_limit_info.map do |_provider, info|
    parse_reset_time(info[:reset_time])
  end.compact.min
end

#provider_rate_limited?(provider_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/aidp/harness/state/provider_state.rb', line 32

def provider_rate_limited?(provider_name)
  info = rate_limit_info[provider_name]
  return false unless info

  reset_time = parse_reset_time(info[:reset_time])
  reset_time && Time.now < reset_time
end

#provider_stateObject



12
13
14
# File 'lib/aidp/harness/state/provider_state.rb', line 12

def provider_state
  state[:provider_state] || {}
end

#rate_limit_infoObject



22
23
24
# File 'lib/aidp/harness/state/provider_state.rb', line 22

def rate_limit_info
  state[:rate_limit_info] || {}
end

#record_token_usage(provider_name, model_name, input_tokens, output_tokens, cost = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/aidp/harness/state/provider_state.rb', line 50

def record_token_usage(provider_name, model_name, input_tokens, output_tokens, cost = nil)
  current_usage = token_usage
  key = "#{provider_name}:#{model_name}"

  current_usage[key] = update_token_usage_entry(
    current_usage[key], input_tokens, output_tokens, cost
  )

  update_state(token_usage: current_usage)
end

#token_usageObject



46
47
48
# File 'lib/aidp/harness/state/provider_state.rb', line 46

def token_usage
  state[:token_usage] || {}
end

#token_usage_summaryObject



61
62
63
64
65
66
67
68
69
# File 'lib/aidp/harness/state/provider_state.rb', line 61

def token_usage_summary
  usage = token_usage
  {
    total_tokens: calculate_total_tokens(usage),
    total_cost: calculate_total_cost(usage),
    total_requests: calculate_total_requests(usage),
    by_provider_model: usage
  }
end

#update_provider_state(provider_name, provider_data) ⇒ Object



16
17
18
19
20
# File 'lib/aidp/harness/state/provider_state.rb', line 16

def update_provider_state(provider_name, provider_data)
  current_provider_state = provider_state
  current_provider_state[provider_name] = provider_data
  update_state(provider_state: current_provider_state)
end

#update_rate_limit_info(provider_name, reset_time, error_count = 0) ⇒ Object



26
27
28
29
30
# File 'lib/aidp/harness/state/provider_state.rb', line 26

def update_rate_limit_info(provider_name, reset_time, error_count = 0)
  current_info = rate_limit_info
  current_info[provider_name] = create_rate_limit_entry(reset_time, error_count)
  update_state(rate_limit_info: current_info)
end