Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

model-meta.ts

122 lines4.9 KB

1import { GENERATED_BEDROCK_MODELS } from './model-catalog.generated.js'
2import type { BedrockTextProviderOptions } from './text/text-provider-options'
3import type { BedrockConverseProviderOptions } from './converse/provider-options'
4import type {
5 BedrockCohereEmbeddingProviderOptions,
6 BedrockEmbeddingProviderOptions,
7 BedrockTitanImageEmbeddingProviderOptions,
8 BedrockTitanTextEmbeddingProviderOptions,
9} from './embedding/embedding-provider-options'
10​
11type Entry = (typeof GENERATED_BEDROCK_MODELS)[number]
12​
13/**
14 * Type-level per-API filter over the generated catalog. Because the catalog is
15 * `as const`, `Extract` preserves literal `id` unions (no widening to `string`).
16 */
17type IdsWhere<TApi extends 'converse' | 'chat' | 'responses'> = Extract<
18 Entry,
19 { apis: Record<TApi, true> }
20>['id']
21​
22export type BedrockConverseModels = IdsWhere<'converse'>
23export type BedrockChatModels = IdsWhere<'chat'>
24export type BedrockResponsesModels = IdsWhere<'responses'>
25​
26/** Runtime catalogs. Cast-free narrowing via a type predicate (the ai-bedrock pattern). */
27export const BEDROCK_CONVERSE_MODELS: ReadonlyArray<BedrockConverseModels> =
28 GENERATED_BEDROCK_MODELS.filter(
29 (m): m is Extract<Entry, { apis: { converse: true } }> => m.apis.converse,
30 ).map((m) => m.id)
31​
32export const BEDROCK_CHAT_MODELS: ReadonlyArray<BedrockChatModels> =
33 GENERATED_BEDROCK_MODELS.filter(
34 (m): m is Extract<Entry, { apis: { chat: true } }> => m.apis.chat,
35 ).map((m) => m.id)
36​
37export const BEDROCK_RESPONSES_MODELS: ReadonlyArray<BedrockResponsesModels> =
38 GENERATED_BEDROCK_MODELS.filter(
39 (m): m is Extract<Entry, { apis: { responses: true } }> => m.apis.responses,
40 ).map((m) => m.id)
41​
42/** Per-model input modalities (drives type-safe multimodal content). Covers ALL models. */
43export type BedrockModelInputModalitiesByName = {
44 [E in Entry as E['id']]: E['input']
46​
47/** Provider options per model. Same options for every model; keyed over the full catalog. */
48export type BedrockChatModelProviderOptionsByName = {
49 [E in Entry as E['id']]: BedrockTextProviderOptions
51​
52/** Converse provider options per model (narrower than the Chat Completions set). */
53export type BedrockConverseModelProviderOptionsByName = {
54 [E in Entry as E['id']]: BedrockConverseProviderOptions
56​
57/** No provider-specific tools — empty tuple makes cross-provider ProviderTool a compile error. */
58export type BedrockChatModelToolCapabilitiesByName = {
59 [E in Entry as E['id']]: readonly []
61​
62export type ResolveProviderOptions<TModel extends string> =
63 TModel extends keyof BedrockChatModelProviderOptionsByName
64 ? BedrockChatModelProviderOptionsByName[TModel]
65 : BedrockTextProviderOptions
66​
67export type ResolveConverseProviderOptions<TModel extends string> =
68 TModel extends keyof BedrockConverseModelProviderOptionsByName
69 ? BedrockConverseModelProviderOptionsByName[TModel]
70 : BedrockConverseProviderOptions
71​
72export type ResolveInputModalities<TModel extends string> =
73 TModel extends keyof BedrockModelInputModalitiesByName
74 ? BedrockModelInputModalitiesByName[TModel]
75 : readonly ['text']
76​
77// ============================================================================
78// Embedding models
79// ============================================================================
80​
81/**
82 * Embedding models reachable through Bedrock's `InvokeModel` API. These are
83 * not part of the generated Converse catalog (embedding models have no
84 * conversational surface), so they're maintained by hand here.
85 */
86export const BEDROCK_EMBEDDING_MODELS = [
87 'amazon.titan-embed-text-v2:0',
88 'amazon.titan-embed-image-v1',
89 'cohere.embed-english-v3',
90 'cohere.embed-multilingual-v3',
91] as const
92​
93export type BedrockEmbeddingModel = (typeof BEDROCK_EMBEDDING_MODELS)[number]
94​
95/**
96 * Type-only map from embedding model name to its provider options type.
97 * The Cohere models make `modelOptions` REQUIRED at the `embed()` call site
98 * because `inputType` is a required field.
99 */
100export type BedrockEmbeddingModelProviderOptionsByName = {
101 'amazon.titan-embed-text-v2:0': BedrockTitanTextEmbeddingProviderOptions
102 'amazon.titan-embed-image-v1': BedrockTitanImageEmbeddingProviderOptions
103 'cohere.embed-english-v3': BedrockCohereEmbeddingProviderOptions
104 'cohere.embed-multilingual-v3': BedrockCohereEmbeddingProviderOptions
106​
107/**
108 * Per-model input modalities for embedding models. Titan Multimodal accepts
109 * text and/or images (including fused text+image items embedded into one
110 * vector); the rest are text-only, so image inputs fail at compile time.
111 */
112export type BedrockEmbeddingModelInputModalitiesByName = {
113 'amazon.titan-embed-text-v2:0': readonly ['text']
114 'amazon.titan-embed-image-v1': readonly ['text', 'image']
115 'cohere.embed-english-v3': readonly ['text']
116 'cohere.embed-multilingual-v3': readonly ['text']
118​
119export type ResolveEmbeddingProviderOptions<TModel extends string> =
120 TModel extends keyof BedrockEmbeddingModelProviderOptionsByName
121 ? BedrockEmbeddingModelProviderOptionsByName[TModel]
122 : BedrockEmbeddingProviderOptions