[cline.ts] Refactor out ensureTaskDirectoryExists [1/8] (#2356)
* refactor out ensureTaskDirectoryExists * changeset --------- Co-authored-by: Marlon Monroy <7920256+saito-sv@users.noreply.github.com>
This commit is contained in:
1 parent
647a964853
commit
af0cd9bdff
3 files changed
+36
-15
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"claude-dev": patch
|
||||
---
|
||||
|
||||
refactor
|
||||
+20
-15
@@ -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<string> {
|
||||
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<Anthropic.MessageParam[]> {
|
||||
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<ClineMessage[]> {
|
||||
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
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
|
||||
export async function ensureTaskDirectoryExists(globalStoragePath: string | undefined, taskId: string): Promise<string> {
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user