diff --git a/src/api/providers/fetchers/modelCache.ts b/src/api/providers/fetchers/modelCache.ts index 5956187e41..fef700268d 100644 --- a/src/api/providers/fetchers/modelCache.ts +++ b/src/api/providers/fetchers/modelCache.ts @@ -2,6 +2,7 @@ import * as path from "path" import fs from "fs/promises" import NodeCache from "node-cache" +import { safeWriteJson } from "../../../utils/safeWriteJson" import { ContextProxy } from "../../../core/config/ContextProxy" import { getCacheDirectoryPath } from "../../../utils/storage" @@ -22,7 +23,7 @@ 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)) + await safeWriteJson(path.join(cacheDir, filename), data) } async function readModels(router: RouterName): Promise { diff --git a/src/api/providers/fetchers/modelEndpointCache.ts b/src/api/providers/fetchers/modelEndpointCache.ts index c69e7c82a3..256ae84048 100644 --- a/src/api/providers/fetchers/modelEndpointCache.ts +++ b/src/api/providers/fetchers/modelEndpointCache.ts @@ -2,6 +2,7 @@ import * as path from "path" import fs from "fs/promises" import NodeCache from "node-cache" +import { safeWriteJson } from "../../../utils/safeWriteJson" import sanitize from "sanitize-filename" import { ContextProxy } from "../../../core/config/ContextProxy" @@ -18,7 +19,7 @@ const getCacheKey = (router: RouterName, modelId: string) => sanitize(`${router} async function writeModelEndpoints(key: string, data: ModelRecord) { const filename = `${key}_endpoints.json` const cacheDir = await getCacheDirectoryPath(ContextProxy.instance.globalStorageUri.fsPath) - await fs.writeFile(path.join(cacheDir, filename), JSON.stringify(data, null, 2)) + await safeWriteJson(path.join(cacheDir, filename), data) } async function readModelEndpoints(key: string): Promise { diff --git a/src/services/code-index/cache-manager.ts b/src/services/code-index/cache-manager.ts index f66f933a0b..146db4cd2a 100644 --- a/src/services/code-index/cache-manager.ts +++ b/src/services/code-index/cache-manager.ts @@ -2,6 +2,7 @@ import * as vscode from "vscode" import { createHash } from "crypto" import { ICacheManager } from "./interfaces/cache" import debounce from "lodash.debounce" +import { safeWriteJson } from "../../utils/safeWriteJson" /** * Manages the cache for code indexing @@ -46,7 +47,7 @@ export class CacheManager implements ICacheManager { */ private async _performSave(): Promise { try { - await vscode.workspace.fs.writeFile(this.cachePath, Buffer.from(JSON.stringify(this.fileHashes, null, 2))) + await safeWriteJson(this.cachePath.fsPath, this.fileHashes) } catch (error) { console.error("Failed to save cache:", error) } @@ -57,7 +58,7 @@ export class CacheManager implements ICacheManager { */ async clearCacheFile(): Promise { try { - await vscode.workspace.fs.writeFile(this.cachePath, Buffer.from("{}")) + await safeWriteJson(this.cachePath.fsPath, {}) this.fileHashes = {} } catch (error) { console.error("Failed to clear cache file:", error, this.cachePath)