From af0cd9bdff3903d36829b569217cdd5fe958e5bd Mon Sep 17 00:00:00 2001 From: Evan <58194240+celestial-vault@users.noreply.github.com> Date: Thu, 20 Mar 2025 18:07:32 -0700 Subject: [PATCH] [cline.ts] Refactor out ensureTaskDirectoryExists [1/8] (#2356) * refactor out ensureTaskDirectoryExists * changeset --------- Co-authored-by: Marlon Monroy <7920256+saito-sv@users.noreply.github.com> --- .changeset/real-kings-guess.md | 5 +++++ src/core/Cline.ts | 35 +++++++++++++++++++--------------- src/core/messages-io.ts | 11 +++++++++++ 3 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 .changeset/real-kings-guess.md create mode 100644 src/core/messages-io.ts diff --git a/.changeset/real-kings-guess.md b/.changeset/real-kings-guess.md new file mode 100644 index 00000000..73e3adde --- /dev/null +++ b/.changeset/real-kings-guess.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +refactor diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 7d7040ad..5ae12d49 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -66,6 +66,7 @@ import { DEFAULT_LANGUAGE_SETTINGS, getLanguageKey, LanguageDisplay, LanguageKey import { telemetryService } from "../services/telemetry/TelemetryService" import pTimeout from "p-timeout" import { GlobalFileNames } from "../global-constants" +import { ensureTaskDirectoryExists } from "./messages-io" const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution @@ -178,18 +179,13 @@ export class Cline { // Storing task to disk for history - private async ensureTaskDirectoryExists(): Promise { - const globalStoragePath = this.providerRef.deref()?.context.globalStorageUri.fsPath - if (!globalStoragePath) { - throw new Error("Global storage uri is invalid") - } - const taskDir = path.join(globalStoragePath, "tasks", this.taskId) - await fs.mkdir(taskDir, { recursive: true }) - return taskDir - } - private async getSavedApiConversationHistory(): Promise { - const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.apiConversationHistory) + const globalStoragePath = this.providerRef.deref()?.context.globalStorageUri.fsPath + const taskId = this.taskId + const filePath = path.join( + await ensureTaskDirectoryExists(globalStoragePath, taskId), + GlobalFileNames.apiConversationHistory, + ) const fileExists = await fileExistsAtPath(filePath) if (fileExists) { return JSON.parse(await fs.readFile(filePath, "utf8")) @@ -209,7 +205,12 @@ export class Cline { private async saveApiConversationHistory() { try { - const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.apiConversationHistory) + const globalStoragePath = this.providerRef.deref()?.context.globalStorageUri.fsPath + const taskId = this.taskId + const filePath = path.join( + await ensureTaskDirectoryExists(globalStoragePath, taskId), + GlobalFileNames.apiConversationHistory, + ) await fs.writeFile(filePath, JSON.stringify(this.apiConversationHistory)) } catch (error) { // in the off chance this fails, we don't want to stop the task @@ -218,12 +219,14 @@ export class Cline { } private async getSavedClineMessages(): Promise { - const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.uiMessages) + const globalStoragePath = this.providerRef.deref()?.context.globalStorageUri.fsPath + const taskId = this.taskId + const filePath = path.join(await ensureTaskDirectoryExists(globalStoragePath, taskId), GlobalFileNames.uiMessages) if (await fileExistsAtPath(filePath)) { return JSON.parse(await fs.readFile(filePath, "utf8")) } else { // check old location - const oldPath = path.join(await this.ensureTaskDirectoryExists(), "claude_messages.json") + const oldPath = path.join(await ensureTaskDirectoryExists(globalStoragePath, taskId), "claude_messages.json") if (await fileExistsAtPath(oldPath)) { const data = JSON.parse(await fs.readFile(oldPath, "utf8")) await fs.unlink(oldPath) // remove old file @@ -249,7 +252,9 @@ export class Cline { private async saveClineMessages() { try { - const taskDir = await this.ensureTaskDirectoryExists() + const globalStoragePath = this.providerRef.deref()?.context.globalStorageUri.fsPath + const taskId = this.taskId + const taskDir = await ensureTaskDirectoryExists(globalStoragePath, taskId) const filePath = path.join(taskDir, GlobalFileNames.uiMessages) await fs.writeFile(filePath, JSON.stringify(this.clineMessages)) // combined as they are in ChatView diff --git a/src/core/messages-io.ts b/src/core/messages-io.ts new file mode 100644 index 00000000..d5923711 --- /dev/null +++ b/src/core/messages-io.ts @@ -0,0 +1,11 @@ +import fs from "fs/promises" +import path from "path" + +export async function ensureTaskDirectoryExists(globalStoragePath: string | undefined, taskId: string): Promise { + if (!globalStoragePath) { + throw new Error("Global storage uri is invalid") + } + const taskDir = path.join(globalStoragePath, "tasks", taskId) + await fs.mkdir(taskDir, { recursive: true }) + return taskDir +}