Skip to main content

Router

Struct Router 

Source
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

Source

pub fn new() -> Self

Source

pub fn register(self, key: &str, provider: Box<dyn Provider>) -> Self

Source

pub fn alias(self, from: &str, to: &str) -> Self

Source

pub fn route(self, name: &str, route: Route) -> Self

Register a named route. The name is opaque to llmshim.

Source

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.

Source

pub fn breaker(&self) -> &Arc<ProviderBreaker>

The provider-health breaker governing this router’s dispatches.

Source

pub fn route_names(&self) -> Vec<&str>

Names of the configured routes.

Source

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.

Source

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.

Source

pub fn provider_keys(&self) -> Vec<&str>

Returns the keys of all registered providers.

Source

pub fn get(&self, key: &str) -> Result<&dyn Provider>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn resolve(&self, model: &str) -> Result<(&dyn Provider, String)>

Resolve model string to (provider, model_name).

Source

pub fn resolve_owned(&self, model: &str) -> Result<(Arc<dyn Provider>, String)>

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more