11import { mkdir , rename , rm } from "node:fs/promises"
2- import os from "node:os"
32import path from "node:path"
4- import { resolveDataPath } from "@deepagent-code/core/global-path"
53
64const repositorySnapshotFile = path . resolve ( import . meta. dir , "../test/tool/fixtures/models-api.json" )
75
@@ -18,7 +16,7 @@ export async function loadModelsData(
1816 if ( configuredFile ) {
1917 const configured = await readCatalog ( configuredFile )
2018 if ( ! configured ) throw new Error ( `Configured models.dev snapshot is invalid: ${ configuredFile } ` )
21- return { data : JSON . stringify ( configured ) , source : configuredFile }
19+ return result ( configured , configuredFile )
2220 }
2321
2422 const modelsURL = ( environment . DEEPAGENT_CODE_MODELS_URL ?. trim ( ) || "https://models.dev" ) . replace ( / \/ $ / , "" )
@@ -27,26 +25,34 @@ export async function loadModelsData(
2725 } )
2826 . then ( async ( response ) => ( response . ok ? catalog ( await response . json ( ) ) : undefined ) )
2927 . catch ( ( ) => undefined )
30- const cacheFile = options . cacheFile ?? path . join ( resolveDataPath ( ) , "cache" , "models.json" )
3128 if ( remote ) {
32- await persistCatalog ( cacheFile , remote ) . catch ( ( error ) =>
33- console . warn (
34- `Unable to update models.dev build cache: ${ error instanceof Error ? error . message : String ( error ) } ` ,
35- ) ,
36- )
37- return { data : JSON . stringify ( remote ) , source : `${ modelsURL } /api.json` }
29+ if ( options . cacheFile ) {
30+ await persistCatalog ( options . cacheFile , remote ) . catch ( ( error ) =>
31+ console . warn (
32+ `Unable to update models.dev build cache: ${ error instanceof Error ? error . message : String ( error ) } ` ,
33+ ) ,
34+ )
35+ }
36+ return result ( remote , `${ modelsURL } /api.json` )
3837 }
3938
40- const fallbacks = options . fallbackFiles ?? [
41- cacheFile ,
42- path . join ( os . homedir ( ) , ".cache" , "opencode" , "models.json" ) ,
43- repositorySnapshotFile ,
44- ]
39+ // Build inputs must not depend on the builder's DeepAgent/OpenCode runtime caches. A caller may
40+ // pass explicit fallback files for tests or controlled builds; the default is the committed copy.
41+ const fallbacks = options . fallbackFiles ?? [ repositorySnapshotFile ]
4542 const cached = ( await Promise . all ( fallbacks . map ( async ( file ) => ( { file, data : await readCatalog ( file ) } ) ) ) ) . find (
4643 ( item ) : item is { file : string ; data : Record < string , unknown > } => item . data !== undefined ,
4744 )
4845 if ( ! cached ) throw new Error ( `Unable to load a valid models.dev catalog from ${ modelsURL } or local snapshots` )
49- return { data : JSON . stringify ( cached . data ) , source : cached . file }
46+ return result ( cached . data , cached . file )
47+ }
48+
49+ function result ( data : Record < string , unknown > , source : string ) {
50+ const serialized = JSON . stringify ( data )
51+ return {
52+ data : serialized ,
53+ source,
54+ sha256 : new Bun . CryptoHasher ( "sha256" ) . update ( serialized ) . digest ( "hex" ) ,
55+ }
5056}
5157
5258function catalog ( value : unknown ) : Record < string , unknown > | undefined {
0 commit comments