@@ -265,15 +265,19 @@ async function main() {
265265 console . log ( "Fetching callable model availability..." ) ;
266266 const availableIds = await fetchAvailabilityIds ( ) ;
267267 console . log ( ` Callable models: ${ availableIds . length } ` ) ;
268- const { retained, unavailable } = filterCatalogByAvailability ( candidates , availableIds ) ;
269- console . log ( ` Retained ${ retained . length } , excluded ${ unavailable . length } unavailable` ) ;
268+ // Enrichment mutates entries in place, so filter the extracted candidates
269+ // first and run the floor gate before any cost work or artifact write.
270+ const prefiltered = filterCatalogByAvailability ( candidates , availableIds ) ;
271+ console . log (
272+ ` Retained ${ prefiltered . retained . length } , excluded ${ prefiltered . unavailable . length } unavailable` ,
273+ ) ;
270274 const last = lastSuccessfulModelCount ( priorManifest ) ;
271- if ( ! meetsModelCountFloor ( retained . length , last ) ) {
275+ if ( ! meetsModelCountFloor ( prefiltered . retained . length , last ) ) {
272276 throw new Error (
273- `filtered model count ${ retained . length } below floor (lastSuccessful=${ last } ); leaving generated artifacts unchanged` ,
277+ `filtered model count ${ prefiltered . retained . length } below floor (lastSuccessful=${ last } ); leaving generated artifacts unchanged` ,
274278 ) ;
275279 }
276- const entries = retained ;
280+ const entries = prefiltered . retained ;
277281
278282 const cliIds = bundleSource ? cliCostIds ( bundleSource ) : new Set < string > ( ) ;
279283 const docIds = new Set < string > ( ) ;
@@ -307,48 +311,32 @@ async function main() {
307311 const modalityFilled = applyModelsDevModalities ( entries , modelsDevRows ) ;
308312 console . log ( ` Applied models.dev modalities to ${ modalityFilled } models` ) ;
309313
310- console . log ( `\nWriting ${ MODELS_JSON } with ${ entries . length } models from ${ sourceLabel } ...` ) ;
311- writeFileSync ( MODELS_JSON , JSON . stringify ( entries , null , 2 ) + "\n" , "utf-8" ) ;
312- writeFileSync ( VERSION_PATH , `${ version } \n` , "utf-8" ) ;
313-
314+ // Entries are already filtered above; buildSyncArtifacts re-validates the
315+ // floor and returns every generated payload before the first write.
314316 const pluginVersion = ( JSON . parse ( readFileSync ( PACKAGE_JSON , "utf-8" ) ) as { version : string } )
315317 . version ;
316- const unmatchedIds = entries
317- . filter (
318- ( e ) =>
319- ! cliIds . has ( e . id ) && ! docIds . has ( e . id ) && ! freeIds . has ( e . id ) && ! thirdPartyIds . has ( e . id ) ,
320- )
321- . map ( ( e ) => e . id ) ;
322- const costSources = countCostSources ( {
323- modelIds : entries . map ( ( e ) => e . id ) ,
318+ const artifacts = buildSyncArtifacts ( {
319+ candidates : entries ,
320+ version,
321+ sourceLabel,
322+ pluginVersion,
323+ availableIds,
324+ priorManifest,
324325 cliIds,
325- officialDocIds : docIds ,
326+ docIds,
326327 thirdPartyIds,
327328 freeIds,
329+ generatedAt : new Date ( ) . toISOString ( ) ,
328330 } ) ;
329- writeManifest (
330- MANIFEST_PATH ,
331- buildManifest ( {
332- pluginVersion,
333- commandCodeVersion : version ,
334- commandCodeTarball : commandCodeTarballUrl ( version ) ,
335- modelCount : entries . length ,
336- reasoningModelCount : entries . filter ( ( e ) => e . reasoning ) . length ,
337- modelCatalogOk : true ,
338- costSources,
339- review : withUnavailableIds (
340- {
341- thirdParty : [ ...thirdPartyIds ] ,
342- free : [ ...freeIds ] ,
343- unmatched : unmatchedIds ,
344- } ,
345- unavailable ,
346- ) ,
347- generatedAt : new Date ( ) . toISOString ( ) ,
348- } ) ,
331+
332+ console . log (
333+ `\nWriting ${ MODELS_JSON } with ${ artifacts . models . length } models from ${ sourceLabel } ...` ,
349334 ) ;
335+ writeFileSync ( MODELS_JSON , JSON . stringify ( artifacts . models , null , 2 ) + "\n" , "utf-8" ) ;
336+ writeFileSync ( VERSION_PATH , `${ artifacts . version } \n` , "utf-8" ) ;
337+ writeManifest ( MANIFEST_PATH , artifacts . manifest ) ;
350338
351- const modelsObj = generateOpencodeModels ( entries ) ;
339+ const modelsObj = generateOpencodeModels ( artifacts . models ) ;
352340
353341 if ( shouldUpdateGlobal ) {
354342 console . log ( "Updating global config..." ) ;
@@ -373,4 +361,9 @@ async function main() {
373361 console . log ( "\nDone." ) ;
374362}
375363
376- main ( ) ;
364+ if ( import . meta. main ) {
365+ main ( ) . catch ( ( err ) => {
366+ console . error ( err instanceof Error ? err . message : err ) ;
367+ process . exit ( 1 ) ;
368+ } ) ;
369+ }
0 commit comments