diff --git a/src/api/providers/fetchers/modelCache.ts b/src/api/providers/fetchers/modelCache.ts index 12d636bc46..03c690f1d2 100644 --- a/src/api/providers/fetchers/modelCache.ts +++ b/src/api/providers/fetchers/modelCache.ts @@ -16,20 +16,6 @@ import { getLiteLLMModels } from "./litellm" import { GetModelsOptions } from "../../../shared/api" const memoryCache = new NodeCache({ stdTTL: 5 * 60, checkperiod: 5 * 60 }) -async function writeModels(router: RouterName, data: ModelRecord) { - const filename = `${router}_models.json` - const cacheDir = await getCacheDirectoryPath(ContextProxy.instance.globalStorageUri.fsPath) - await fs.writeFile(path.join(cacheDir, filename), JSON.stringify(data)) -} - -async function readModels(router: RouterName): Promise { - const filename = `${router}_models.json` - const cacheDir = await getCacheDirectoryPath(ContextProxy.instance.globalStorageUri.fsPath) - const filePath = path.join(cacheDir, filename) - const exists = await fileExistsAtPath(filePath) - return exists ? JSON.parse(await fs.readFile(filePath, "utf8")) : undefined -} - /** * Get models from the cache or fetch them from the provider and cache them. * There are two caches: @@ -43,7 +29,10 @@ async function readModels(router: RouterName): Promise */ export const getModels = async (options: GetModelsOptions): Promise => { const { provider } = options - let models = memoryCache.get(provider) + + const cacheKey = JSON.stringify(options) + let models = memoryCache.get(cacheKey) + if (models) { return models } @@ -51,7 +40,7 @@ export const getModels = async (options: GetModelsOptions): Promise try { switch (provider) { case "openrouter": - models = await getOpenRouterModels() + models = await getOpenRouterModels(options.baseUrl, options.apiKey) break case "requesty": // Requesty models endpoint requires an API key for per-user custom policies @@ -76,17 +65,8 @@ export const getModels = async (options: GetModelsOptions): Promise } // Cache the fetched models (even if empty, to signify a successful fetch with no models) - memoryCache.set(provider, models) - await writeModels(provider, models).catch((err) => - console.error(`[getModels] Error writing ${provider} models to file cache:`, err), - ) + memoryCache.set(cacheKey, models) - try { - models = await readModels(provider) - // console.log(`[getModels] read ${router} models from file cache`) - } catch (error) { - console.error(`[getModels] error reading ${provider} models from file cache`, error) - } return models || {} } catch (error) { // Log the error and re-throw it so the caller can handle it (e.g., show a UI message). diff --git a/src/api/providers/fetchers/openrouter.ts b/src/api/providers/fetchers/openrouter.ts index a98484ba0e..86d70291b0 100644 --- a/src/api/providers/fetchers/openrouter.ts +++ b/src/api/providers/fetchers/openrouter.ts @@ -93,12 +93,14 @@ type OpenRouterModelEndpointsResponse = z.infer> { +export async function getOpenRouterModels(baseUrl?: string, apiKey?: string): Promise> { const models: Record = {} - const baseURL = options?.openRouterBaseUrl || "https://openrouter.ai/api/v1" + const baseURL = baseUrl || "https://openrouter.ai/api/v1" try { - const response = await axios.get(`${baseURL}/models`) + const response = await axios.get(`${baseURL}/models`, { + headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : undefined, + }) const result = openRouterModelsResponseSchema.safeParse(response.data) const data = result.success ? result.data.data : response.data.data diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index aa182c3de4..8093ab0deb 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -752,7 +752,7 @@ export class ClineProvider - +