Open History to see the last commit
1/**
2 * Fetches models from the LLM Gateway catalog and writes llmgateway.models.json.
3 *
4 * Usage:
5 * node scripts/fetch-llmgateway-models.ts
6 *
7 * modelschemas has no llmgateway provider. The catalog needs no API key.
8 * JSON output cannot smuggle executable code into the build.
9 */
10
11import { writeFile } from 'node:fs/promises'
12import { dirname, resolve } from 'node:path'
13import { fileURLToPath } from 'node:url'
14
15const __dirname = dirname(fileURLToPath(import.meta.url))
16const OUTPUT_PATH = resolve(__dirname, 'llmgateway.models.json')
17const API_URL = 'https://api.llmgateway.io/v1/models'
18
19interface ApiModel {
20 id: string
21 [key: string]: unknown
22}
23
24function isValidModel(model: unknown): model is ApiModel {
25 return (
26 typeof model === 'object' &&
27 model !== null &&
28 typeof (model as { id?: unknown }).id === 'string' &&
29 (model as { id: string }).id.length > 0
30 )
31}
32
33async function main() {
34 console.log(`Fetching models from ${API_URL}...`)
35 const response = await fetch(API_URL, {
36 headers: { Accept: 'application/json' },
37 signal: AbortSignal.timeout(30_000),
38 })
39 if (!response.ok) {
40 throw new Error(
41 `Failed to fetch LLM Gateway models: ${response.status} ${response.statusText}`,
42 )
43 }
44 const json = (await response.json()) as { data?: unknown }
45 if (!Array.isArray(json.data)) {
46 throw new Error('LLM Gateway /v1/models response is missing a data array')
47 }
48 const validModels = json.data.filter(isValidModel)
49 const skipped = json.data.length - validModels.length
50 if (skipped > 0) {
51 console.log(`Skipped ${skipped} models missing a string id`)
52 }
53 validModels.sort((a, b) => a.id.localeCompare(b.id))
54 await writeFile(OUTPUT_PATH, `${JSON.stringify(validModels, null, 2)}\n`)
55 console.log(`Wrote ${validModels.length} models to ${OUTPUT_PATH}`)
56}
57
58main().catch((error: unknown) => {
59 console.error(error)
60 process.exit(1)
61})