pub struct Router {
pub aliases: HashMap<String, String>,
/* private fields */
}Expand description
Registry of configured providers.
Fields§
§aliases: HashMap<String, String>Implementations§
Source§impl Router
impl Router
pub fn new() -> Self
pub fn register(self, key: &str, provider: Box<dyn Provider>) -> Self
pub fn alias(self, from: &str, to: &str) -> Self
Sourcepub fn route(self, name: &str, route: Route) -> Self
pub fn route(self, name: &str, route: Route) -> Self
Register a named route. The name is opaque to llmshim.
Sourcepub fn with_breaker(self, breaker: Arc<ProviderBreaker>) -> Self
pub fn with_breaker(self, breaker: Arc<ProviderBreaker>) -> Self
Replace the provider-health breaker — how the proxy attaches a fleet-wide (Redis-coordinated) one to a router built from the env.
Sourcepub fn breaker(&self) -> &Arc<ProviderBreaker> ⓘ
pub fn breaker(&self) -> &Arc<ProviderBreaker> ⓘ
The provider-health breaker governing this router’s dispatches.
Sourcepub fn route_names(&self) -> Vec<&str>
pub fn route_names(&self) -> Vec<&str>
Names of the configured routes.
Sourcepub fn route_target(&self, model: &str) -> Result<Option<&Route>>
pub fn route_target(&self, model: &str) -> Result<Option<&Route>>
The model a route/<name> string resolves to, or None when the string
is not a named route.
An unknown name is an error. Falling back to a default model would send traffic somewhere the caller never asked for, which is exactly the failure a named route exists to prevent.
Sourcepub fn expand_route<'a>(&self, request: &'a Value) -> Result<Cow<'a, Value>>
pub fn expand_route<'a>(&self, request: &'a Value) -> Result<Cow<'a, Value>>
Expand a request addressed to route/<name> into its model plus the
route’s settings. Settings the request already carries are left alone —
a route is a default, not an override — so a caller can pick the route
and still raise reasoning_effort for one call.
Requests that name no route are returned borrowed and untouched.
Sourcepub fn provider_keys(&self) -> Vec<&str>
pub fn provider_keys(&self) -> Vec<&str>
Returns the keys of all registered providers.
pub fn get(&self, key: &str) -> Result<&dyn Provider>
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Build a router from provider env vars and a saved ChatGPT login, and schedule one background refresh of the model catalog.
This is the daemon’s constructor. The proxy starts once and serves for
days, so a single fetch at startup is what keeps its prices and
capabilities current for the rest of its life. A process that starts
many times a day, or must run air-gapped, wants
Router::from_env_without_catalog_refresh instead and decides for
itself when — or whether — to call
Router::refresh_catalog_in_background.
Sourcepub fn from_env_without_catalog_refresh() -> Self
pub fn from_env_without_catalog_refresh() -> Self
Router::from_env minus the catalog refresh: building a router makes
no network call.
The catalog is still there — the vendored snapshot, whatever an earlier
refresh cached on disk, and the local override files — so resolve and
pricing work exactly as they do after from_env; the data is simply
never newer than the disk. For a short-lived embedder that is the right
default: a CLI that phones home for a price list before the user has
typed anything is doing something nobody asked for, and a machine with
no route out should not have to discover LLMSHIM_CATALOG_OFFLINE to
stop it. The fetch becomes something the caller asks for.
Sourcepub fn from_credentials_without_catalog_refresh(
stored: &dyn StoredCredentials,
) -> Self
pub fn from_credentials_without_catalog_refresh( stored: &dyn StoredCredentials, ) -> Self
Router::from_env_without_catalog_refresh with somewhere to fall back to for a
provider the environment has no key for: an embedder’s own saved login
(crate::credentials).
The environment still wins, so this only ever registers a provider that would otherwise
have been absent. stored is asked by provider name, never by variable name, so a
caller holds no provider-specific knowledge — which is the whole reason the seam is here
rather than in the caller.
Sourcepub fn refresh_catalog_in_background(
&self,
) -> Option<JoinHandle<RefreshOutcome>>
pub fn refresh_catalog_in_background( &self, ) -> Option<JoinHandle<RefreshOutcome>>
Refresh the shared model catalog from the network, detached from every request: only startup’s local snapshot is synchronous, and no catalog HTTP fetch is ever awaited by a model request.
The refresh rides the caller’s Tokio runtime and never creates one, so
None means nothing was scheduled: there is no runtime on this thread,
LLMSHIM_CATALOG_OFFLINE=1 is set, or the local catalog configuration
is invalid. The catalog is process-wide — it is what every router in
the process resolves against — so refreshing through one router
refreshes it for all of them.
Sourcepub fn resolve(&self, model: &str) -> Result<(&dyn Provider, String)>
pub fn resolve(&self, model: &str) -> Result<(&dyn Provider, String)>
Resolve model string to (provider, model_name).