UNPKG

@mastra/core

Version:
96 lines 4.27 kB
import type { GatewayAuthResult, MastraModelGatewayInterface, ProviderConfig } from './base.js'; /** * A model entry in the gateway catalog (without use-count tracking). */ export interface GatewayModel { /** Full model ID (e.g., "anthropic/claude-sonnet-4-20250514") */ id: string; /** Provider prefix (e.g., "anthropic" or "netlify/anthropic") */ provider: string; /** Model name without provider prefix */ modelName: string; /** Whether the provider has valid authentication */ hasApiKey: boolean; /** Environment variable for the provider's API key */ apiKeyEnvVar?: string; } /** * Centralises the gateway-chain operations shared between * {@link ModelRouterLanguageModel} and the AgentController: gateway merging, model * routing, auth resolution, and provider/model listing. * * The manager owns the *gateway registry* (which gateway owns a model, auth * resolution through the chain). Per-instance concerns (explicit config, * model-instance caching, websocket/transport, doGenerate/doStream) remain in * the consumers. */ export declare class GatewayManager { readonly gateways: MastraModelGatewayInterface[]; constructor(gateways?: MastraModelGatewayInterface[]); /** * Returns the gateway prefix used for model-id parsing. * `models.dev` is a provider registry and doesn't use a prefix. */ static getPrefix(gatewayId: string): string | undefined; /** * Returns the prefix to use when parsing `routerId` for the given gateway. * A gateway may claim an unprefixed id (e.g. `handlesModel` matching a bare * `anthropic/...` id); in that case the gateway's prefix does not appear in * the id, so parse it as an unprefixed `provider/model` id. */ private static getPrefixForId; /** Find the gateway that handles a given model/router id. */ findGatewayForModel(routerId: string): MastraModelGatewayInterface; /** * Resolve the gateway and parsed provider/model components for a router id * in a single pass. Centralises gateway selection + id parsing so callers * (router constructor, doGenerate/doStream, supportedUrls) don't each * re-derive the gateway and prefix separately. */ resolveModelId(routerId: string): { gateway: MastraModelGatewayInterface; gatewayId: string; providerId: string; modelId: string; }; /** Parse a router id into its provider/model/gateway components. */ parseModelId(routerId: string): { providerId: string; modelId: string; gatewayId: string; }; /** * Resolve auth through the gateway chain: select the gateway via * {@link findGatewayForModel}, try its `resolveAuth()` (OAuth / stored * credentials), and fall back to `getApiKey()` (which, for registry / * openai-compatible providers, reads the API-key env var). * * The caller is responsible for merging any explicit headers/credentials * from per-instance config on top of the result returned here. */ resolveAuth(routerId: string): Promise<GatewayAuthResult>; /** * Convenience: whether auth is available for a model. * Returns `false` for expected missing-auth states (no matching gateway, * missing credentials/env vars) but re-throws unexpected errors (token * exchange failures, network bugs, malformed auth hooks) so they surface * instead of being silently hidden. */ hasAuth(routerId: string): Promise<boolean>; /** * Fetch and flatten providers from all gateways, deduped by provider key * (configured / earlier gateway wins). Each gateway's `fetchProviders()` * is a network call for gateways like models.dev / Netlify. */ listProviders(): Promise<Record<string, ProviderConfig & { gateway: string; }>>; /** * Build the model catalog from gateway providers, resolving auth per * provider (using the first model). Returns models without use-count. */ listAvailableModels(): Promise<GatewayModel[]>; /** Provider key used for catalog ids: prefix with the gateway id unless it's the prefix-less models.dev registry. */ private getProviderKey; } //# sourceMappingURL=gateway-manager.d.ts.map