class LittleGhost::ModelResolver
Maps an application model role to an executable Model.
resolver = LittleGhost::ModelResolver.new( profiles: { customer_support: {target: "openrouter:openai/gpt-5.6-luna"} } ) # With OPENROUTER_API_KEY set: model = resolver.resolve(:customer_support)
A role is an application-facing name such as :customer_support. Callers may also pass a provider:model-id target or an inline configuration mapping.
A dotted role such as customer_support.escalation uses the nearest defined profile, here customer_support, when it has none of its own. A profile’s inherits name is looked up the same way, then the profile’s target, settings, and request options are layered over its parent’s.
Model details come from the bundled catalog until you call refresh!. A long-lived process can pass catalog_refresh_interval to refresh every configured source when details are first needed and again in the background once the interval has passed.
Attributes
Provider configuration and model metadata catalog.
Seconds between automatic detail refreshes for one target, or nil.
Logical role used when an agent does not declare one.
Provider configuration and model metadata catalog.
Public Class Methods
# File lib/little_ghost/model_resolver.rb, line 58 def initialize(providers: nil, profiles: nil, default_model: nil, provider_registry: ProviderRegistry.new, catalog: nil, catalog_sources: [], provider_adapters: {}, credential_resolver: nil, catalog_refresh_interval: nil, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) unless providers.nil? || providers.is_a?(Providers::Configuration) raise ArgumentError, "providers must be a LittleGhost::Providers::Configuration" end valid_interval = catalog_refresh_interval.nil? || (catalog_refresh_interval.is_a?(Numeric) && catalog_refresh_interval.positive?) raise ArgumentError, "catalog_refresh_interval must be a positive number of seconds" unless valid_interval @providers = providers @connections = providers&.connections || {} @profiles = normalize_profiles(profiles || {}) @default_model = (default_model&.to_s || "default").dup.freeze @provider_registry = provider_adapters.empty? ? provider_registry : ProviderRegistry.new(adapters: provider_adapters) @credential_resolver = credential_resolver configure_default_providers unless providers configure_default_profiles unless profiles sources = catalog_sources.empty? ? built_in_catalog_sources : catalog_sources @catalog = catalog || Models::Catalog.new(sources:) @catalog_refresh_interval = catalog_refresh_interval @clock = clock @refresh_mutex = Mutex.new @refreshed_at = nil @refresh_done = nil end
Builds a resolver from explicit provider connections and profiles. With neither, conventional credentials select GPT-5.6 Luna.
catalog_refresh_interval, in seconds, makes details refresh all configured catalog sources in one background call. The first request waits up to ten seconds for that refresh, or less when its Run is cancelled or reaches its deadline, and then uses whatever details are available. Once the interval has passed, the next request starts another refresh and returns the current details without waiting. Concurrent requests share one refresh. A failed refresh keeps the last good details, emits a little_ghost.models.catalog_refresh_failed warning naming only the error class, and is retried after the next interval. Leave the interval nil to refresh only when you call refresh!. clock returns monotonic seconds and exists for tests.
Public Instance Methods
# File lib/little_ghost/model_resolver.rb, line 131 def details(target, context: nil) refresh_stale_catalog(context) catalog.details(target) end
Returns normalized metadata for target without constructing a provider. With a catalog_refresh_interval, stale details start a refresh first; see ::new for when this waits. Raises CancelledError or DeadlineExceededError when context stops while waiting.
Source
# File lib/little_ghost/model_resolver.rb, line 138 def refresh!(target: nil) result = catalog.refresh!(target:) @refresh_mutex.synchronize { @refreshed_at = @clock.call } if target.nil? result end
Refreshes configured metadata sources, retaining stale data on failure. A refresh of every target also restarts the catalog_refresh_interval.
# File lib/little_ghost/model_resolver.rb, line 95 def resolve(selection, invocation: nil, context: nil, profiles: nil, **options) base, role = normalize_selection(selection) if role base = resolved_role(role) profile_overlays = profiles || {} override_names(role).each do |profile| base = merge(base, profile_overlays[profile] || profile_overlays[profile.to_sym]) end end target = Models::Target.parse(base.fetch(:target)) provider_config = @connections.fetch(target.provider) do raise ConfigurationError, "Model target references unknown provider #{target.provider}" end provider_config = provider_config.merge(resolved_credentials(target.provider, provider_config)) if @credential_resolver details = details_for(target, base[:details], context) settings = clamp_output_tokens(base.fetch(:settings), details) adapter = provider_config.fetch("adapter") provider = @provider_registry.build( adapter:, model: target.model_id, configuration: provider_config, request: base.fetch(:request), role:, settings:, details:, invocation:, context:, **options ) Model.new(provider:, target:, settings:, details:, role:) end
Resolves a logical role, canonical target, or inline model mapping into an executable Model. Inline mappings require provider and model; remaining entries are trusted model settings. The optional profiles mapping applies only to role selections and is never read from invocation.