@@ -18,6 +18,7 @@ process.chdir(dir)
1818import { Script } from "@opencode-ai/script"
1919import pkg from "../package.json"
2020import { walkInputs } from "./stamp-inputs"
21+ import { assertUsableCatalog , catalogDiagnosticOrigin , formatCatalogSummary } from "./models-catalog"
2122
2223// Python engine has been eliminated — all methods run natively in TypeScript.
2324// ALTIMATE_ENGINE_VERSION is no longer needed at runtime.
@@ -30,61 +31,21 @@ console.log(`Loaded CHANGELOG.md (${changelog.length} chars)`)
3031const modelsUrlOverride = process . env . OPENCODE_MODELS_URL || undefined
3132const modelsUrl = modelsUrlOverride ?? "https://models.dev"
3233
33- // The providers whose absence would make a shipped binary visibly broken.
34- const REQUIRED_CATALOG_PROVIDERS = [ "anthropic" , "openai" , "google" ]
35- // Size floor for the real models.dev catalog: live carries 200+ providers and the
36- // checked-in fixture 105, so anything under this is a fetch that went wrong rather
37- // than a real shrink. Applied only in strict mode — a private catalog pinned via
38- // OPENCODE_MODELS_URL / MODELS_DEV_API_JSON is legitimately allowed to be small.
39- const MIN_CATALOG_PROVIDERS = 50
4034const CATALOG_FETCH_TIMEOUT_MS = 60_000
4135// The hard backstop must lose the race to `AbortSignal.timeout` in every case the
4236// signal CAN handle, or it fires first and replaces the precise per-stage message
4337// ("fetch failed", "body read failed") with its own generic one. The margin is
4438// what makes it a backstop rather than the primary timeout.
4539const CATALOG_HARD_DEADLINE_MS = CATALOG_FETCH_TIMEOUT_MS + 15_000
4640
47- /** Describe the first structural problem in a provider entry, or undefined when
48- * it is well formed.
49- *
50- * The provider-level rules match the runtime's own predicate (`isCatalogEntry` in
51- * src/provider/models-catalog.ts, which `Provider.state()` screens every entry
52- * with). The per-model rules cover exactly the fields the runtime dereferences
53- * WITHOUT a guard: `Provider.fromModelsDevModel` reads `model.limit.context`
54- * directly while every neighbouring field uses `?.`/`??`, so a model record
55- * missing `limit` crashes provider initialisation on cold start instead of
56- * degrading. Catching it here turns that into a failed release build.
57- *
58- * Deliberately NOT the zod `Provider` schema, which requires an `options` record
59- * real models.dev entries do not carry and would reject valid live data — see the
60- * note on `isCatalogEntry` for that history. Verified against every model in the
61- * release fixture (4108), the committed snapshot (5299) and the live catalog
62- * (7487): zero failures. */
63- function providerEntryProblem ( id : string , entry : unknown ) : string | undefined {
64- if ( typeof entry !== "object" || entry === null || Array . isArray ( entry ) ) return `${ id } (not an object)`
65- if ( ! ( "id" in entry ) || typeof entry . id !== "string" ) return `${ id } (no string id)`
66- if ( ! ( "models" in entry ) ) return `${ id } (no models)`
67- const models = entry . models
68- if ( typeof models !== "object" || models === null || Array . isArray ( models ) ) return `${ id } (models is not a map)`
69- for ( const [ modelId , model ] of Object . entries ( models ) ) {
70- const where = `${ id } /${ modelId } `
71- if ( typeof model !== "object" || model === null || Array . isArray ( model ) ) return `${ where } (not an object)`
72- if ( ! ( "limit" in model ) ) return `${ where } (no limit)`
73- const limit = model . limit
74- if ( typeof limit !== "object" || limit === null || Array . isArray ( limit ) ) return `${ where } (limit is not an object)`
75- if ( ! ( "context" in limit ) || typeof limit . context !== "number" ) return `${ where } (limit.context is not a number)`
76- }
77- return undefined
78- }
79-
8041/** Fetch the models.dev catalog, failing loudly rather than hanging or
8142 * returning an error page.
8243 *
8344 * `fetch` resolves for 4xx/5xx, so without the `res.ok` check a load-balancer
8445 * error page flows straight into the snapshot. An HTML body would at least break
8546 * the build at parse time, but a JSON error body (`{"error": ...}`) is valid
8647 * TypeScript and would ship as a catalog with no providers in it. */
87- async function fetchModelsCatalog ( url : string ) : Promise < string > {
48+ async function fetchModelsCatalog ( url : string , diagnosticOrigin : string ) : Promise < string > {
8849 // Backstop for the case where the abort signal fires but the fetch promise never
8950 // settles, so the `catch` below is never reached. `AbortSignal.timeout` cannot
9051 // cancel a blocked `getaddrinfo()` — documented in src/provider/models.ts
@@ -99,7 +60,7 @@ async function fetchModelsCatalog(url: string): Promise<string> {
9960 // fails; it never falls through to a stale catalog.
10061 const deadline = setTimeout ( ( ) => {
10162 console . error (
102- `error: models.dev fetch from ${ url } did not settle within ${ CATALOG_HARD_DEADLINE_MS } ms ` +
63+ `error: models.dev fetch from ${ diagnosticOrigin } did not settle within ${ CATALOG_HARD_DEADLINE_MS } ms ` +
10364 `(host unreachable or unresolvable); failing the build` ,
10465 )
10566 process . exit ( 1 )
@@ -108,84 +69,34 @@ async function fetchModelsCatalog(url: string): Promise<string> {
10869 let res : Response
10970 try {
11071 res = await fetch ( url , { signal : AbortSignal . timeout ( CATALOG_FETCH_TIMEOUT_MS ) } )
111- } catch ( e ) {
112- throw new Error ( `models.dev fetch from ${ url } failed or timed out after ${ CATALOG_FETCH_TIMEOUT_MS } ms` , {
113- cause : e ,
114- } )
72+ } catch {
73+ throw new Error (
74+ `models.dev fetch from ${ diagnosticOrigin } failed or timed out after ${ CATALOG_FETCH_TIMEOUT_MS } ms` ,
75+ )
11576 }
116- if ( ! res . ok ) throw new Error ( `models.dev fetch failed: HTTP ${ res . status } ${ res . statusText } from ${ url } ` )
77+ if ( ! res . ok )
78+ throw new Error ( `models.dev fetch failed: HTTP ${ res . status } ${ res . statusText } from ${ diagnosticOrigin } ` )
11779 try {
11880 // Inside its own try: a host that sends headers promptly then stalls
11981 // mid-body aborts here, and an uncaught abort surfaces as a bare
12082 // AbortError carrying none of the context above.
12183 return await res . text ( )
122- } catch ( e ) {
123- throw new Error ( `models.dev body read from ${ url } failed or timed out after ${ CATALOG_FETCH_TIMEOUT_MS } ms` , {
124- cause : e ,
125- } )
84+ } catch {
85+ throw new Error (
86+ `models.dev body read from ${ diagnosticOrigin } failed or timed out after ${ CATALOG_FETCH_TIMEOUT_MS } ms` ,
87+ )
12688 }
12789 } finally {
12890 clearTimeout ( deadline )
12991 }
13092}
13193
132- /** Reject a catalog that parses but is not usable.
133- *
134- * Release builds embed this in every binary, so an empty, truncated or
135- * structurally broken payload has to stop the release rather than ship a CLI
136- * that offers no models.
137- *
138- * `strict` adds the checks that only make sense for the real models.dev catalog:
139- * the size floor, and the requirement that the major providers carry models rather
140- * than merely appear as keys. It is ON for every release build and for any plain
141- * default-endpoint build, and OFF only when an operator has deliberately pointed
142- * the build at a custom catalog, which is allowed to be small. The structural
143- * checks run in both modes — a key count alone is not enough, since a payload can
144- * carry 50+ keys whose values are junk and still parse. */
145- function assertUsableCatalog ( text : string , origin : string , strict : boolean ) : void {
146- let parsed : unknown
94+ async function readModelsCatalog ( file : string , diagnosticOrigin : string ) : Promise < string > {
14795 try {
148- parsed = JSON . parse ( text )
149- } catch ( e ) {
150- throw new Error ( `models.dev catalog from ${ origin } is not valid JSON` , { cause : e } )
151- }
152- if ( typeof parsed !== "object" || parsed === null || Array . isArray ( parsed ) )
153- throw new Error ( `models.dev catalog from ${ origin } is not a provider object` )
154- const catalog = new Map < string , unknown > ( Object . entries ( parsed ) )
155- if ( catalog . size === 0 ) throw new Error ( `models.dev catalog from ${ origin } is empty` )
156- const problems = [ ...catalog . entries ( ) ]
157- . map ( ( [ id , entry ] ) => providerEntryProblem ( id , entry ) )
158- . filter ( ( p ) : p is string => p !== undefined )
159- if ( problems . length > 0 )
160- throw new Error (
161- `models.dev catalog from ${ origin } has ${ problems . length } malformed provider entries: ` +
162- `${ problems . slice ( 0 , 5 ) . join ( ", " ) } ${ problems . length > 5 ? ", …" : "" } ` ,
163- )
164- const modelCount = ( id : string ) : number => {
165- const entry = catalog . get ( id )
166- if ( typeof entry !== "object" || entry === null || ! ( "models" in entry ) ) return 0
167- const models = entry . models
168- return typeof models === "object" && models !== null ? Object . keys ( models ) . length : 0
169- }
170- if ( ! strict ) {
171- console . log ( `models.dev catalog from ${ origin } : ${ catalog . size } providers (custom catalog, size floor not applied)` )
172- return
96+ return await Bun . file ( file ) . text ( )
97+ } catch {
98+ throw new Error ( `models.dev catalog read from ${ diagnosticOrigin } failed` )
17399 }
174- if ( catalog . size < MIN_CATALOG_PROVIDERS )
175- throw new Error (
176- `models.dev catalog from ${ origin } has only ${ catalog . size } providers, expected at least ${ MIN_CATALOG_PROVIDERS } ` ,
177- )
178- const missing = REQUIRED_CATALOG_PROVIDERS . filter ( ( p ) => ! catalog . has ( p ) )
179- if ( missing . length > 0 )
180- throw new Error ( `models.dev catalog from ${ origin } is missing required providers: ${ missing . join ( ", " ) } ` )
181- // Every required provider must actually carry models, not just exist as a key.
182- const empty = REQUIRED_CATALOG_PROVIDERS . filter ( ( p ) => modelCount ( p ) === 0 )
183- if ( empty . length > 0 )
184- throw new Error ( `models.dev catalog from ${ origin } has no usable models for: ${ empty . join ( ", " ) } ` )
185- console . log (
186- `models.dev catalog from ${ origin } : ${ catalog . size } providers ` +
187- `(${ REQUIRED_CATALOG_PROVIDERS . map ( ( p ) => `${ p } =${ modelCount ( p ) } ` ) . join ( ", " ) } )` ,
188- )
189100}
190101
191102// Fetch and generate models.dev snapshot. MODELS_DEV_API_JSON pins the catalog to
@@ -196,12 +107,16 @@ function assertUsableCatalog(text: string, origin: string, strict: boolean): voi
196107// and the build dies on `fetch("")` with ERR_INVALID_URL.
197108const modelsFile = process . env . MODELS_DEV_API_JSON || undefined
198109const modelsOrigin = modelsFile ?? `${ modelsUrl } /api.json`
199- const modelsData = modelsFile ? await Bun . file ( modelsFile ) . text ( ) : await fetchModelsCatalog ( modelsOrigin )
110+ const modelsDiagnosticOrigin = catalogDiagnosticOrigin ( modelsOrigin , modelsFile ? "file" : "url" )
111+ const modelsData = modelsFile
112+ ? await readModelsCatalog ( modelsFile , modelsDiagnosticOrigin )
113+ : await fetchModelsCatalog ( modelsOrigin , modelsDiagnosticOrigin )
200114// A release is held to the full floor however its catalog was sourced, so pointing
201115// a release build at a custom catalog cannot quietly skip the size and
202116// required-provider checks.
203117const strictCatalog = ! ! process . env . OPENCODE_RELEASE || ( ! modelsFile && ! modelsUrlOverride )
204- assertUsableCatalog ( modelsData , modelsOrigin , strictCatalog )
118+ const catalogSummary = assertUsableCatalog ( modelsData , modelsDiagnosticOrigin , strictCatalog )
119+ console . log ( formatCatalogSummary ( catalogSummary , modelsDiagnosticOrigin ) )
205120await Bun . write (
206121 path . join ( dir , "src/provider/models-snapshot.ts" ) ,
207122 `// Auto-generated by build.ts - do not edit\nexport const snapshot = ${ modelsData . trim ( ) } as const\n` ,
0 commit comments