Class: RosettAi::AiConfig::ModelRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/ai_config/model_router.rb

Overview

Maps canonical model tiers to engine-specific model identifiers.

Supports three standard tiers (economy, standard, premium) plus custom model names. Engine manifests declare their model mappings. Use ModelRouter.with_manifest_mappings to load mappings from installed engines.

Author:

  • hugo

  • claude

Constant Summary collapse

CANONICAL_TIERS =
['economy', 'standard', 'premium'].freeze
DEFAULT_MAPPINGS =

Default model mappings per engine (used when engine manifest unavailable).

{
  'claude' => {
    'economy' => 'claude-haiku-4-5-20251001',
    'standard' => 'claude-sonnet-4-5-20250929',
    'premium' => 'claude-opus-4-6'
  },
  'ollama' => {
    'economy' => 'llama3.2:3b',
    'standard' => 'llama3.3:8b',
    'premium' => 'llama3.3:70b'
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_mappings: {}) ⇒ ModelRouter

Returns a new instance of ModelRouter.

Parameters:

  • custom_mappings (Hash) (defaults to: {})

    additional engine model mappings



44
45
46
# File 'lib/rosett_ai/ai_config/model_router.rb', line 44

def initialize(custom_mappings: {})
  @mappings = DEFAULT_MAPPINGS.merge(custom_mappings)
end

Class Method Details

.with_manifest_mappings(extra_mappings: {}) ⇒ ModelRouter

Builds a ModelRouter that merges manifest-declared model mappings from installed engines with DEFAULT_MAPPINGS.

Parameters:

  • extra_mappings (Hash) (defaults to: {})

    additional overrides on top of manifest data

Returns:



38
39
40
41
# File 'lib/rosett_ai/ai_config/model_router.rb', line 38

def self.with_manifest_mappings(extra_mappings: {})
  manifest_mappings = load_manifest_mappings
  new(custom_mappings: manifest_mappings.merge(extra_mappings))
end

Instance Method Details

#available_tiers(engine) ⇒ Array<String>

Lists all available tiers for an engine.

Parameters:

  • engine (String)

    engine identifier

Returns:

  • (Array<String>)

    available tier names



69
70
71
72
# File 'lib/rosett_ai/ai_config/model_router.rb', line 69

def available_tiers(engine)
  engine_map = @mappings.fetch(engine, {})
  engine_map.keys
end

#engine_supported?(engine) ⇒ Boolean

Returns true if engine has model mappings.

Parameters:

  • engine (String)

    engine identifier

Returns:

  • (Boolean)

    true if engine has model mappings



76
77
78
# File 'lib/rosett_ai/ai_config/model_router.rb', line 76

def engine_supported?(engine)
  @mappings.key?(engine)
end

#resolve(tier, engine:) ⇒ String

Resolves a canonical tier to an engine-specific model ID.

Parameters:

  • tier (String)

    canonical tier name or "custom:"

  • engine (String)

    engine identifier

Returns:

  • (String)

    resolved model identifier

Raises:



54
55
56
57
58
59
60
61
62
63
# File 'lib/rosett_ai/ai_config/model_router.rb', line 54

def resolve(tier, engine:)
  return extract_custom_model(tier) if tier.start_with?('custom:')

  validate_tier!(tier)
  engine_map = @mappings.fetch(engine, {})
  engine_map.fetch(tier) do
    raise RosettAi::AiConfigError,
          "No model mapping for tier '#{tier}' on engine '#{engine}'"
  end
end

#resolve_all(tier) ⇒ Hash<String, String>

Resolves a canonical tier across all known engines.

Parameters:

  • tier (String)

    canonical tier name

Returns:

  • (Hash<String, String>)

    engine name => resolved model ID



84
85
86
87
88
89
# File 'lib/rosett_ai/ai_config/model_router.rb', line 84

def resolve_all(tier)
  validate_tier!(tier)
  @mappings.each_with_object({}) do |(engine, map), result|
    result[engine] = map[tier] if map.key?(tier)
  end
end