@@ -16,20 +16,6 @@ import { getLiteLLMModels } from "./litellm"
1616import { GetModelsOptions } from "../../../shared/api"
1717const memoryCache = new NodeCache ( { stdTTL : 5 * 60 , checkperiod : 5 * 60 } )
1818
19- async function writeModels ( router : RouterName , data : ModelRecord ) {
20- const filename = `${ router } _models.json`
21- const cacheDir = await getCacheDirectoryPath ( ContextProxy . instance . globalStorageUri . fsPath )
22- await fs . writeFile ( path . join ( cacheDir , filename ) , JSON . stringify ( data ) )
23- }
24-
25- async function readModels ( router : RouterName ) : Promise < ModelRecord | undefined > {
26- const filename = `${ router } _models.json`
27- const cacheDir = await getCacheDirectoryPath ( ContextProxy . instance . globalStorageUri . fsPath )
28- const filePath = path . join ( cacheDir , filename )
29- const exists = await fileExistsAtPath ( filePath )
30- return exists ? JSON . parse ( await fs . readFile ( filePath , "utf8" ) ) : undefined
31- }
32-
3319/**
3420 * Get models from the cache or fetch them from the provider and cache them.
3521 * There are two caches:
@@ -43,15 +29,18 @@ async function readModels(router: RouterName): Promise<ModelRecord | undefined>
4329 */
4430export const getModels = async ( options : GetModelsOptions ) : Promise < ModelRecord > => {
4531 const { provider } = options
46- let models = memoryCache . get < ModelRecord > ( provider )
32+
33+ const cacheKey = JSON . stringify ( options )
34+ let models = memoryCache . get < ModelRecord > ( cacheKey )
35+
4736 if ( models ) {
4837 return models
4938 }
5039
5140 try {
5241 switch ( provider ) {
5342 case "openrouter" :
54- models = await getOpenRouterModels ( )
43+ models = await getOpenRouterModels ( options . baseUrl , options . apiKey )
5544 break
5645 case "requesty" :
5746 // Requesty models endpoint requires an API key for per-user custom policies
@@ -76,17 +65,8 @@ export const getModels = async (options: GetModelsOptions): Promise<ModelRecord>
7665 }
7766
7867 // Cache the fetched models (even if empty, to signify a successful fetch with no models)
79- memoryCache . set ( provider , models )
80- await writeModels ( provider , models ) . catch ( ( err ) =>
81- console . error ( `[getModels] Error writing ${ provider } models to file cache:` , err ) ,
82- )
68+ memoryCache . set ( cacheKey , models )
8369
84- try {
85- models = await readModels ( provider )
86- // console.log(`[getModels] read ${router} models from file cache`)
87- } catch ( error ) {
88- console . error ( `[getModels] error reading ${ provider } models from file cache` , error )
89- }
9070 return models || { }
9171 } catch ( error ) {
9272 // Log the error and re-throw it so the caller can handle it (e.g., show a UI message).
0 commit comments