Open History to see the last commit
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) and optional mantlePath come
12 * from packages/ai-bedrock/src/api-compatibility.ts.
13 * APIs are transcribed from:
14 * https://docs.aws.amazon.com/bedrock/latest/userguide/models-api-compatibility.html
15 * mantlePath is transcribed from each model card's Programmatic Access URL
16 * (default /v1). First matching seed rule wins; put more specific matches first.
17 * Seed extra `models` on a rule when ListFoundationModels does not return them.
18 * Update the compatibility config to add new providers/models before re-running.
19 *
20 * Why manual: ListFoundationModels carries modalities + inference types but no
21 * pricing, and per-account/region availability varies. The committed model-catalog
22 * is the long-term source of truth; this script regenerates it automatically.
23 */
24import {
25 BedrockClient,
26 ListFoundationModelsCommand,
27 ListInferenceProfilesCommand,
28} from '@aws-sdk/client-bedrock'
29import { writeFileSync } from 'node:fs'
30import { fileURLToPath } from 'node:url'
31import { join, dirname } from 'node:path'
32import {
33 BEDROCK_API_COMPATIBILITY,
34 lookupBedrockCompatibility,
35} from '../packages/ai-bedrock/src/api-compatibility.js'
36
37const __dirname = dirname(fileURLToPath(import.meta.url))
38const ROOT = join(__dirname, '..')
39
40interface CatalogApis {
41 converse: boolean
42 chat: boolean
43 responses: boolean
44}
45
46function emitCatalog(
47 entries: Array<{
48 id: string
49 profileId?: string
50 input: string[]
51 output: string[]
52 apis: CatalogApis
53 }>,
54): string {
55 const lines: string[] = [
56 `// GENERATED by scripts/fetch-bedrock-models.ts — do not edit by hand.`,
57 `// Refresh: AWS_REGION=us-east-1 pnpm tsx scripts/fetch-bedrock-models.ts`,
58 `export const GENERATED_BEDROCK_MODELS = [`,
59 ]
60
61 for (const entry of entries) {
62 const inputLiteral = entry.input.map((m) => `'${m}'`).join(', ')
63 const outputLiteral = entry.output.map((m) => `'${m}'`).join(', ')
64 const apis = entry.apis
65
66 const profilePart =
67 entry.profileId !== undefined ? `profileId: '${entry.profileId}', ` : ''
68
69 lines.push(
70 ` { id: '${entry.id}', ${profilePart}input: [${inputLiteral}], output: [${outputLiteral}], apis: { converse: ${apis.converse}, chat: ${apis.chat}, responses: ${apis.responses} } },`,
71 )
72 }
73
74 lines.push(`] as const`, ``)
75 return lines.join('\n')
76}
77
78async function main() {
79 const region = process.env['AWS_REGION'] ?? 'us-east-1'
80 const client = new BedrockClient({ region })
81 const compatRules = BEDROCK_API_COMPATIBILITY
82
83 // ListFoundationModels typically returns no nextToken, but loop on it anyway
84 // so we stay correct if it ever paginates.
85 const modelSummaries = []
86 let modelsToken: string | undefined
87 do {
88 const page = await client.send(
89 new ListFoundationModelsCommand({
90 byOutputModality: 'TEXT',
91 ...(modelsToken ? { nextToken: modelsToken } : {}),
92 }),
93 )
94 modelSummaries.push(...(page.modelSummaries ?? []))
95 modelsToken = page.nextToken
96 } while (modelsToken)
97
98 // ListInferenceProfiles is paginated — collect every page via nextToken.
99 const inferenceProfileSummaries = []
100 let profilesToken: string | undefined
101 do {
102 const page = await client.send(
103 new ListInferenceProfilesCommand(
104 profilesToken ? { nextToken: profilesToken } : {},
105 ),
106 )
107 inferenceProfileSummaries.push(...(page.inferenceProfileSummaries ?? []))
108 profilesToken = page.nextToken
109 } while (profilesToken)
110
111 // Build a lookup: base model id → preferred cross-region inference profile id.
112 // A cross-region profile id typically looks like "us.<baseModelId>".
113 const profileByBaseId = new Map<string, string>()
114 for (const profile of inferenceProfileSummaries) {
115 const profileId = profile.inferenceProfileId
116 if (!profileId) continue
117 // Strip the leading region prefix (e.g. "us.", "eu.", "ap.") to get the base id.
118 const baseId = profileId.replace(/^(us|eu|ap)\./, '')
119 if (!profileByBaseId.has(baseId)) {
120 profileByBaseId.set(baseId, profileId)
121 }
122 }
123
124 const textModels = modelSummaries
125 .filter((m) => (m.outputModalities ?? []).includes('TEXT'))
126 .map((m) => {
127 const id = m.modelId ?? ''
128 return {
129 id,
130 input: (m.inputModalities ?? []).map((x) => x.toLowerCase()),
131 }
132 })
133 .filter((m) => m.id.length > 0)
134
135 const entries = textModels.map((m) => {
136 const profileId = profileByBaseId.get(m.id)
137 const resolvedId = profileId ?? m.id
138 const apis = lookupBedrockCompatibility(resolvedId)
139
140 const entry: {
141 id: string
142 profileId?: string
143 input: string[]
144 output: string[]
145 apis: CatalogApis
146 } = {
147 id: resolvedId,
148 input: [...m.input],
149 output: ['text'],
150 apis,
151 }
152
153 if (profileId !== undefined) {
154 entry.profileId = profileId
155 }
156
157 return entry
158 })
159
160 const seenIds = new Set(entries.map((entry) => entry.id))
161 for (const rule of compatRules) {
162 for (const extra of rule.models ?? []) {
163 if (seenIds.has(extra.id)) continue
164 seenIds.add(extra.id)
165 entries.push({
166 id: extra.id,
167 input: [...extra.input],
168 output: ['text'],
169 apis: lookupBedrockCompatibility(extra.id),
170 })
171 }
172 }
173
174 const outPath = join(
175 ROOT,
176 'packages',
177 'ai-bedrock',
178 'src',
179 'model-catalog.generated.ts',
180 )
181 const content = emitCatalog(entries)
182 writeFileSync(outPath, content, 'utf-8')
183 console.log(`Wrote ${entries.length} models to ${outPath}`)
184}
185
186main().catch((err) => {
187 console.error(err)
188 process.exit(1)
189})