@mastra/core
Version:
115 lines • 5.5 kB
TypeScript
import type { LanguageModelV2, LanguageModelV2CallOptions } from '../../_types/@ai-sdk_provider@2.0.5/dist/index.js';
import type { StreamTransport } from '../../stream/types.js';
import type { MastraModelGatewayInterface } from './gateways/base.js';
import type { OpenAITransport } from './provider-options.js';
import type { ModelRouterModelId } from './provider-registry.js';
import type { MastraLanguageModelV2, OpenAICompatibleConfig } from './shared.types.js';
export { defaultGateways, gateways } from './gateways/defaults.js';
type StreamResult = Awaited<ReturnType<LanguageModelV2['doStream']>>;
export declare class ModelRouterLanguageModel implements MastraLanguageModelV2 {
#private;
readonly specificationVersion: 'v2';
readonly defaultObjectGenerationMode: 'json';
readonly supportsStructuredOutputs = true;
readonly supportsImageUrls = true;
/**
* Supported URL patterns by media type for the provider.
* This is a lazy promise that resolves the underlying model's supportedUrls.
* Models like Mistral define which URL patterns they support (e.g., application/pdf for https URLs).
*
* @see https://github.com/mastra-ai/mastra/issues/12152
*/
readonly supportedUrls: PromiseLike<Record<string, RegExp[]>>;
readonly modelId: string;
readonly provider: string;
readonly gatewayId: string;
/** Full `provider/model` router ID, when the ID alone resolves this model's route. */
id?: string;
private config;
private gateway;
private _supportedUrlsPromise;
private readonly instanceGatewayCache;
private readonly hasCustomGateways;
constructor(config: ModelRouterModelId | OpenAICompatibleConfig, customGateways?: MastraModelGatewayInterface[]);
/**
* Lazily resolves the underlying model's supportedUrls.
* This is cached to avoid multiple model resolutions.
* @internal
*/
private _resolveSupportedUrls;
/**
* Fetches supportedUrls from the underlying model.
* @internal
*/
private _fetchSupportedUrls;
/**
* Resolves the provider id of the model this router calls, e.g. `groq.chat` or `openai.responses`.
* `provider` only names the gateway's provider (`groq`, `openai`), which doesn't tell a Chat
* Completions endpoint from a Responses one. Returns `undefined` when the model can't be resolved
* (e.g. a missing API key); the model call reports that error itself.
*/
resolveApiProviderId(): Promise<string | undefined>;
/**
* Whether the Mastra gateway serves Observational Memory for this model's resolved credential.
*
* Gateway models do not all get memory from the gateway: only a Gateway project key routes
* through the memory-aware gateway, while organization keys (`msk-gateway-…`, `mskv2_…`) are
* served by a gateway without the memory API. Callers that would otherwise skip local memory
* processing for gateway models (the Observational Memory processor) use this to tell the two
* apart.
*
* Returns `false` for a model that isn't on the Mastra gateway. A credential that cannot be
* resolved is thrown rather than reported as local memory: the model call resolves its
* credential through this same path, so answering "local" when the lookup failed is how the
* gateway and the local store both end up saving the same conversation. That includes a missing
* credential (`MASTRA_GATEWAY_NO_API_KEY`, an unset environment variable) — the gateway may
* serve memory as soon as the credential resolves, so the ownership check stops the run instead
* of guessing.
*/
isGatewayMemoryManaged(): Promise<boolean>;
/** @internal */
_getStreamTransport(): StreamTransport | undefined;
/**
* Custom serialization for tracing/observability spans.
* Excludes `config` (holds apiKey, headers, url) and `gateway`
* (may hold proxy credentials or cached tokens) so they cannot leak
* into telemetry backends.
*/
serializeForSpan(): {
specificationVersion: 'v2';
modelId: string;
provider: string;
gatewayId: string;
};
private getGatewayCache;
private shouldUseInstanceGatewayCache;
private setStreamTransportHandle;
private resolveAuth;
private setStreamTransportFromCache;
private stripUnsupportedSamplingParams;
doGenerate(options: LanguageModelV2CallOptions): Promise<StreamResult>;
doStream(options: LanguageModelV2CallOptions): Promise<StreamResult>;
static computeModelCacheKey({ gatewayId, modelId, providerId, url, apiKey, headersKey, resolvedTransport, websocketKey, authScopeKey, api, }: {
gatewayId: string;
modelId: string;
providerId: string;
url: string;
apiKey: string;
headersKey: string;
resolvedTransport: OpenAITransport;
websocketKey: string;
authScopeKey: string;
api: 'chat' | 'responses';
}): string;
private resolveLanguageModel;
private resolveOpenAIWebSocketModel;
private static _clearCachesForTests;
private static gatewayCaches;
}
/**
* Returns the provider id of the API a model sends requests to, e.g. `openai.chat` or
* `anthropic.messages`. Router models resolve the model they will call; other models report
* their own `provider`.
*/
export declare function resolveApiProviderId(model: unknown): Promise<string | undefined>;
//# sourceMappingURL=router.d.ts.map