Class: Ace::LLM::Models::FallbackConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/llm/models/fallback_config.rb

Overview

FallbackConfig represents fallback configuration settings This is a model - pure data structure with validation

Constant Summary collapse

DEFAULT_ENABLED =

Default configuration values

true
DEFAULT_RETRY_COUNT =
3
DEFAULT_RETRY_DELAY =
1.0
DEFAULT_PROVIDERS =
[].freeze
DEFAULT_CHAINS =
{}.freeze
DEFAULT_MAX_TOTAL_TIMEOUT =
30.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: DEFAULT_ENABLED, retry_count: DEFAULT_RETRY_COUNT, retry_delay: DEFAULT_RETRY_DELAY, providers: DEFAULT_PROVIDERS, chains: DEFAULT_CHAINS, max_total_timeout: DEFAULT_MAX_TOTAL_TIMEOUT) ⇒ FallbackConfig

Returns a new instance of FallbackConfig.

Parameters:

  • enabled (Boolean) (defaults to: DEFAULT_ENABLED)

    Whether fallback is enabled

  • retry_count (Integer) (defaults to: DEFAULT_RETRY_COUNT)

    Number of retries before fallback

  • retry_delay (Float) (defaults to: DEFAULT_RETRY_DELAY)

    Initial retry delay in seconds

  • providers (Array<String>) (defaults to: DEFAULT_PROVIDERS)

    Default fallback provider chain

  • chains (Hash{String => Array<String>}) (defaults to: DEFAULT_CHAINS)

    Per-provider fallback chains

  • max_total_timeout (Float) (defaults to: DEFAULT_MAX_TOTAL_TIMEOUT)

    Maximum total time for all retries and fallbacks



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ace/llm/models/fallback_config.rb', line 25

def initialize(enabled: DEFAULT_ENABLED,
  retry_count: DEFAULT_RETRY_COUNT,
  retry_delay: DEFAULT_RETRY_DELAY,
  providers: DEFAULT_PROVIDERS,
  chains: DEFAULT_CHAINS,
  max_total_timeout: DEFAULT_MAX_TOTAL_TIMEOUT)
  @enabled = enabled
  @retry_count = retry_count
  @retry_delay = retry_delay
  @providers = providers.freeze
  @chains = chains
  @max_total_timeout = max_total_timeout

  validate!

  @chains = normalize_chains(@chains).freeze
end

Instance Attribute Details

#chainsObject (readonly)

Returns the value of attribute chains.



9
10
11
# File 'lib/ace/llm/models/fallback_config.rb', line 9

def chains
  @chains
end

#enabledObject (readonly)

Returns the value of attribute enabled.



9
10
11
# File 'lib/ace/llm/models/fallback_config.rb', line 9

def enabled
  @enabled
end

#max_total_timeoutObject (readonly)

Returns the value of attribute max_total_timeout.



9
10
11
# File 'lib/ace/llm/models/fallback_config.rb', line 9

def max_total_timeout
  @max_total_timeout
end

#providersObject (readonly)

Returns the value of attribute providers.



9
10
11
# File 'lib/ace/llm/models/fallback_config.rb', line 9

def providers
  @providers
end

#retry_countObject (readonly)

Returns the value of attribute retry_count.



9
10
11
# File 'lib/ace/llm/models/fallback_config.rb', line 9

def retry_count
  @retry_count
end

#retry_delayObject (readonly)

Returns the value of attribute retry_delay.



9
10
11
# File 'lib/ace/llm/models/fallback_config.rb', line 9

def retry_delay
  @retry_delay
end

Class Method Details

.fetch_key(hash, key, default) ⇒ Object

Helper to fetch key from hash supporting both symbol and string keys

Parameters:

  • hash (Hash)

    Hash to fetch from

  • key (Symbol)

    Key to fetch

  • default (Object)

    Default value if key not found

Returns:

  • (Object)

    Value from hash or default



127
128
129
# File 'lib/ace/llm/models/fallback_config.rb', line 127

def self.fetch_key(hash, key, default)
  hash.fetch(key, hash.fetch(key.to_s, default))
end

.from_hash(hash) ⇒ FallbackConfig

Create FallbackConfig from a hash (e.g., from YAML)

Parameters:

  • hash (Hash)

    Configuration hash

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ace/llm/models/fallback_config.rb', line 46

def self.from_hash(hash)
  return new unless hash

  new(
    enabled: fetch_key(hash, :enabled, DEFAULT_ENABLED),
    retry_count: fetch_key(hash, :retry_count, DEFAULT_RETRY_COUNT),
    retry_delay: fetch_key(hash, :retry_delay, DEFAULT_RETRY_DELAY),
    providers: fetch_key(hash, :providers, DEFAULT_PROVIDERS),
    chains: fetch_key(hash, :chains, DEFAULT_CHAINS),
    max_total_timeout: fetch_key(hash, :max_total_timeout, DEFAULT_MAX_TOTAL_TIMEOUT)
  )
end

Instance Method Details

#disabled?Boolean

Check if fallback is disabled

Returns:

  • (Boolean)


89
90
91
# File 'lib/ace/llm/models/fallback_config.rb', line 89

def disabled?
  !enabled?
end

#enabled?Boolean

Check if fallback is enabled

Returns:

  • (Boolean)


83
84
85
# File 'lib/ace/llm/models/fallback_config.rb', line 83

def enabled?
  @enabled == true
end

#has_providers?Boolean

Check if there are any fallback providers configured

Returns:

  • (Boolean)


95
96
97
# File 'lib/ace/llm/models/fallback_config.rb', line 95

def has_providers?
  @providers && !@providers.empty?
end

#merge(other) ⇒ FallbackConfig

Merge with another config (other takes precedence)

Parameters:

Returns:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ace/llm/models/fallback_config.rb', line 102

def merge(other)
  other_hash = other.is_a?(Hash) ? other : other.to_h

  merged_chains = @chains.dup
  if other_hash.key?(:chains)
    other_hash[:chains].each { |k, v| merged_chains[k.to_s] = v }
  end

  self.class.new(
    enabled: other_hash.fetch(:enabled, @enabled),
    retry_count: other_hash.fetch(:retry_count, @retry_count),
    retry_delay: other_hash.fetch(:retry_delay, @retry_delay),
    providers: other_hash.fetch(:providers, @providers),
    chains: merged_chains,
    max_total_timeout: other_hash.fetch(:max_total_timeout, @max_total_timeout)
  )
end

#providers_for(primary) ⇒ Array<String>

Return fallback providers for a specific primary provider Uses per-provider chain if configured, otherwise falls back to default providers

Parameters:

  • primary (String)

    Primary provider name

Returns:

  • (Array<String>)

    Ordered fallback provider list



63
64
65
66
# File 'lib/ace/llm/models/fallback_config.rb', line 63

def providers_for(primary)
  key = primary.to_s
  @chains[key] || @providers
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)

    Configuration as hash



70
71
72
73
74
75
76
77
78
79
# File 'lib/ace/llm/models/fallback_config.rb', line 70

def to_h
  {
    enabled: @enabled,
    retry_count: @retry_count,
    retry_delay: @retry_delay,
    providers: @providers.dup,
    chains: @chains.transform_values(&:dup),
    max_total_timeout: @max_total_timeout
  }
end