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

fetch-bedrock-models.ts

190 lines6.0 KB

1/**
2 * Fetches the Bedrock foundation-model + inference-profile catalog and WRITES
3 * packages/ai-bedrock/src/model-catalog.generated.ts so the committed file
4 * stays fresh without manual editing.
5 *
6 * MAINTAINER-ONLY. Not run in CI. Requires AWS credentials (standard provider
7 * chain) with bedrock:List* permissions, and the AWS SDK:
8 * pnpm add -Dw @aws-sdk/client-bedrock # if not already installed
9 * AWS_REGION=us-east-1 pnpm tsx scripts/fetch-bedrock-models.ts
10 *
11 * Per-API flags (converse / chat / responses) come from the static seed file
12 * scripts/bedrock-api-compatibility.json, transcribed from:
13 * https://docs.aws.amazon.com/bedrock/latest/userguide/models-api-compatibility.html
14 * Update the JSON seed to add new providers/models before re-running the script.
15 *
16 * Why manual: ListFoundationModels carries modalities + inference types but no
17 * pricing, and per-account/region availability varies. The committed model-catalog
18 * is the long-term source of truth; this script regenerates it automatically.
19 */
20import {
21 BedrockClient,
22 ListFoundationModelsCommand,
23 ListInferenceProfilesCommand,
24} from '@aws-sdk/client-bedrock'
25import { readFileSync, writeFileSync } from 'node:fs'
26import { fileURLToPath } from 'node:url'
27import { join, dirname } from 'node:path'
28​
29const __dirname = dirname(fileURLToPath(import.meta.url))
30const ROOT = join(__dirname, '..')
31​
32interface CompatibilityRule {
33 match: string
34 converse: boolean
35 chat: boolean
36 responses: boolean
38​
39function loadCompatibilitySeed(): CompatibilityRule[] {
40 const seedPath = join(__dirname, 'bedrock-api-compatibility.json')
41 return JSON.parse(readFileSync(seedPath, 'utf-8')) as CompatibilityRule[]
43​
44function lookupApis(
45 id: string,
46 rules: CompatibilityRule[],
47): { converse: boolean; chat: boolean; responses: boolean } {
48 for (const rule of rules) {
49 if (id.includes(rule.match)) {
50 return {
51 converse: rule.converse,
52 chat: rule.chat,
53 responses: rule.responses,
54 }
55 }
56 }
57 // Default: Converse is supported by virtually all text models; chat/responses are opt-in.
58 return { converse: true, chat: false, responses: false }
60​
61function emitCatalog(
62 entries: Array<{
63 id: string
64 profileId?: string
65 input: string[]
66 output: string[]
67 apis: { converse: boolean; chat: boolean; responses: boolean }
68 }>,
69): string {
70 const lines: string[] = [
71 `// GENERATED by scripts/fetch-bedrock-models.ts — do not edit by hand.`,
72 `// Refresh: AWS_REGION=us-east-1 pnpm tsx scripts/fetch-bedrock-models.ts`,
73 `export const GENERATED_BEDROCK_MODELS = [`,
74 ]
75​
76 for (const entry of entries) {
77 const inputLiteral = entry.input.map((m) => `'${m}'`).join(', ')
78 const outputLiteral = entry.output.map((m) => `'${m}'`).join(', ')
79 const apis = entry.apis
80​
81 const profilePart =
82 entry.profileId !== undefined ? `profileId: '${entry.profileId}', ` : ''
83​
84 lines.push(
85 ` { id: '${entry.id}', ${profilePart}input: [${inputLiteral}], output: [${outputLiteral}], apis: { converse: ${apis.converse}, chat: ${apis.chat}, responses: ${apis.responses} } },`,
86 )
87 }
88​
89 lines.push(`] as const`, ``)
90 return lines.join('\n')
92​
93async function main() {
94 const region = process.env['AWS_REGION'] ?? 'us-east-1'
95 const client = new BedrockClient({ region })
96 const compatRules = loadCompatibilitySeed()
97​
98 // ListFoundationModels typically returns no nextToken, but loop on it anyway
99 // so we stay correct if it ever paginates.
100 const modelSummaries = []
101 let modelsToken: string | undefined
102 do {
103 const page = await client.send(
104 new ListFoundationModelsCommand({
105 byOutputModality: 'TEXT',
106 ...(modelsToken ? { nextToken: modelsToken } : {}),
107 }),
108 )
109 modelSummaries.push(...(page.modelSummaries ?? []))
110 modelsToken = page.nextToken
111 } while (modelsToken)
112​
113 // ListInferenceProfiles is paginated — collect every page via nextToken.
114 const inferenceProfileSummaries = []
115 let profilesToken: string | undefined
116 do {
117 const page = await client.send(
118 new ListInferenceProfilesCommand(
119 profilesToken ? { nextToken: profilesToken } : {},
120 ),
121 )
122 inferenceProfileSummaries.push(...(page.inferenceProfileSummaries ?? []))
123 profilesToken = page.nextToken
124 } while (profilesToken)
125​
126 // Build a lookup: base model id → preferred cross-region inference profile id.
127 // A cross-region profile id typically looks like "us.<baseModelId>".
128 const profileByBaseId = new Map<string, string>()
129 for (const profile of inferenceProfileSummaries) {
130 const profileId = profile.inferenceProfileId
131 if (!profileId) continue
132 // Strip the leading region prefix (e.g. "us.", "eu.", "ap.") to get the base id.
133 const baseId = profileId.replace(/^(us|eu|ap)\./, '')
134 if (!profileByBaseId.has(baseId)) {
135 profileByBaseId.set(baseId, profileId)
136 }
137 }
138​
139 const textModels = modelSummaries
140 .filter((m) => (m.outputModalities ?? []).includes('TEXT'))
141 .map((m) => {
142 const id = m.modelId ?? ''
143 return {
144 id,
145 input: (m.inputModalities ?? []).map((x) => x.toLowerCase()),
146 }
147 })
148 .filter((m) => m.id.length > 0)
149​
150 const entries = textModels.map((m) => {
151 const profileId = profileByBaseId.get(m.id)
152 const resolvedId = profileId ?? m.id
153 const apis = lookupApis(resolvedId, compatRules)
154​
155 const entry: {
156 id: string
157 profileId?: string
158 input: string[]
159 output: string[]
160 apis: { converse: boolean; chat: boolean; responses: boolean }
161 } = {
162 id: resolvedId,
163 input: m.input,
164 output: ['text'],
165 apis,
166 }
167​
168 if (profileId !== undefined) {
169 entry.profileId = profileId
170 }
171​
172 return entry
173 })
174​
175 const outPath = join(
176 ROOT,
177 'packages',
178 'ai-bedrock',
179 'src',
180 'model-catalog.generated.ts',
181 )
182 const content = emitCatalog(entries)
183 writeFileSync(outPath, content, 'utf-8')
184 console.log(`Wrote ${entries.length} models to ${outPath}`)
186​
187main().catch((err) => {
188 console.error(err)
189 process.exit(1)
190})