@tanstack/ai
Version:
Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.
73 lines (72 loc) • 3.19 kB
TypeScript
import { ByokProvider } from './define-provider.js';
import { ProviderId } from './providers.js';
declare const KEYED_ADAPTER: unique symbol;
/**
* An adapter that needs a provider key before it exists. Make one with
* {@link keyedAdapter}. A host finds the key and calls `create` just before a
* call.
*/
export interface KeyedAdapter<TAdapter> {
readonly [KEYED_ADAPTER]: true;
/** The provider whose key `create` needs. */
readonly provider: ByokProvider | ProviderId;
/** Builds the adapter from the key. */
readonly create: (key: string) => TAdapter;
}
/**
* Wrap an adapter factory that needs a provider key. The host finds the key
* and calls `create` just before each call, so the key is not in your code
* or in a `.env` file. Works for every adapter kind: text, image, speech,
* audio, video, and the rest.
*
* @param provider - The provider whose key `create` needs: a BYOK descriptor
* such as `openaiByok` (its `env` names are the fallback), or a provider id.
* @param create - Builds the adapter from the key.
* @throws Error when the provider id is not a valid BYOK slug.
*
* @example
* ```ts
* import { createOpenaiChat } from '@tanstack/ai-openai'
* import { openaiByok } from '@tanstack/ai-openai/byok'
*
* const adapter = keyedAdapter(openaiByok, (key) =>
* createOpenaiChat('gpt-5.5', key),
* )
* // In a server route, with `getByokKey` from `@tanstack/ai/byok/server`:
* const key = getByokKey(request, adapter.provider)
* if (key) chat({ adapter: adapter.create(key), messages })
* ```
*/
export declare function keyedAdapter<TAdapter>(provider: ByokProvider | ProviderId, create: (key: string) => TAdapter): KeyedAdapter<TAdapter>;
/**
* True when `value` came from {@link keyedAdapter}. A host uses it to decide
* if an adapter needs a key first.
*/
export declare function isKeyedAdapter<TAdapter>(value: TAdapter | KeyedAdapter<TAdapter>): value is KeyedAdapter<TAdapter>;
/** The adapter type that a {@link KeyedAdapter} builds. */
export type KeyedAdapterResult<T> = T extends KeyedAdapter<infer A> ? A : never;
/**
* Keyed adapters for several providers in one map. The map key is the
* provider id, so a key goes only to the factory of its own provider. An
* entry is a factory (the key comes only from the `x-byok-<id>` header), or a
* {@link keyedAdapter} made with a BYOK descriptor (its `env` names are the
* fallback). Pick one per request with `keyedAdapterFromRequest` from
* `@tanstack/ai/byok/server`.
*
* @throws Error when a key is not a valid provider id, or when a keyed adapter
* entry is for a different provider than its key.
*
* @example
* ```ts
* const models = keyedAdapters({
* openai: (key) => createOpenaiChat('gpt-6.1-sol', key),
* anthropic: keyedAdapter(anthropicByok, (key) =>
* createAnthropicChat('claude-sonnet-5-5', key),
* ),
* })
* ```
*/
export declare function keyedAdapters<const TMap extends Record<ProviderId, KeyedAdapter<unknown> | ((key: string) => unknown)>>(map: TMap): {
[K in keyof TMap]: TMap[K] extends KeyedAdapter<infer A> ? KeyedAdapter<A> : TMap[K] extends (key: string) => infer A ? KeyedAdapter<A> : never;
};
export {};