From c049436101ff0a1ba78a582c9a5d179dd05ccc7a Mon Sep 17 00:00:00 2001 From: Vignesh Subbiah Date: Sat, 8 Feb 2025 18:44:32 +0530 Subject: [PATCH] DRYs up read model from cache logic --- src/core/webview/ClineProvider.ts | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index add82603de..fb05cfc585 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -1775,16 +1775,20 @@ export class ClineProvider implements vscode.WebviewViewProvider { // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome } - async readGlamaModels(): Promise | undefined> { - const glamaModelsFilePath = path.join(await this.ensureCacheDirectoryExists(), GlobalFileNames.glamaModels) - const fileExists = await fileExistsAtPath(glamaModelsFilePath) + private async readModelsFromCache(filename: string): Promise | undefined> { + const filePath = path.join(await this.ensureCacheDirectoryExists(), filename) + const fileExists = await fileExistsAtPath(filePath) if (fileExists) { - const fileContents = await fs.readFile(glamaModelsFilePath, "utf8") + const fileContents = await fs.readFile(filePath, "utf8") return JSON.parse(fileContents) } return undefined } + async readGlamaModels(): Promise | undefined> { + return this.readModelsFromCache(GlobalFileNames.glamaModels) + } + async refreshGlamaModels() { const glamaModelsFilePath = path.join(await this.ensureCacheDirectoryExists(), GlobalFileNames.glamaModels) @@ -1860,16 +1864,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { } async readOpenRouterModels(): Promise | undefined> { - const openRouterModelsFilePath = path.join( - await this.ensureCacheDirectoryExists(), - GlobalFileNames.openRouterModels, - ) - const fileExists = await fileExistsAtPath(openRouterModelsFilePath) - if (fileExists) { - const fileContents = await fs.readFile(openRouterModelsFilePath, "utf8") - return JSON.parse(fileContents) - } - return undefined + return this.readModelsFromCache(GlobalFileNames.openRouterModels) } async refreshOpenRouterModels() { @@ -1985,13 +1980,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { } async readUnboundModels(): Promise | undefined> { - const unboundModelsFilePath = path.join(await this.ensureCacheDirectoryExists(), GlobalFileNames.unboundModels) - const fileExists = await fileExistsAtPath(unboundModelsFilePath) - if (fileExists) { - const fileContents = await fs.readFile(unboundModelsFilePath, "utf8") - return JSON.parse(fileContents) - } - return undefined + return this.readModelsFromCache(GlobalFileNames.unboundModels) } async refreshUnboundModels() {