Refactor claude_messages.json

This commit is contained in:
Saoud Rizwan
2024-10-06 03:56:04 -04:00
parent d67523596b
commit 372c4df3bf
2 changed files with 26 additions and 14 deletions

View File

@@ -151,10 +151,17 @@ export class Cline {
} }
private async getSavedClaudeMessages(): Promise<ClineMessage[]> { private async getSavedClaudeMessages(): Promise<ClineMessage[]> {
const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.claudeMessages) const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.uiMessages)
const fileExists = await fileExistsAtPath(filePath) if (await fileExistsAtPath(filePath)) {
if (fileExists) {
return JSON.parse(await fs.readFile(filePath, "utf8")) return JSON.parse(await fs.readFile(filePath, "utf8"))
} else {
// check old location
const oldPath = path.join(await this.ensureTaskDirectoryExists(), "claude_messages.json")
if (await fileExistsAtPath(oldPath)) {
const data = JSON.parse(await fs.readFile(oldPath, "utf8"))
await fs.unlink(oldPath) // remove old file
return data
}
} }
return [] return []
} }
@@ -171,7 +178,7 @@ export class Cline {
private async saveClaudeMessages() { private async saveClaudeMessages() {
try { try {
const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.claudeMessages) const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.uiMessages)
await fs.writeFile(filePath, JSON.stringify(this.claudeMessages)) await fs.writeFile(filePath, JSON.stringify(this.claudeMessages))
// combined as they are in ChatView // combined as they are in ChatView
const apiMetrics = getApiMetrics(combineApiRequests(combineCommandSequences(this.claudeMessages.slice(1)))) const apiMetrics = getApiMetrics(combineApiRequests(combineCommandSequences(this.claudeMessages.slice(1))))

View File

@@ -57,7 +57,7 @@ type GlobalStateKey =
export const GlobalFileNames = { export const GlobalFileNames = {
apiConversationHistory: "api_conversation_history.json", apiConversationHistory: "api_conversation_history.json",
claudeMessages: "claude_messages.json", uiMessages: "ui_messages.json",
openRouterModels: "openrouter_models.json", openRouterModels: "openrouter_models.json",
} }
@@ -651,7 +651,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
historyItem: HistoryItem historyItem: HistoryItem
taskDirPath: string taskDirPath: string
apiConversationHistoryFilePath: string apiConversationHistoryFilePath: string
claudeMessagesFilePath: string uiMessagesFilePath: string
apiConversationHistory: Anthropic.MessageParam[] apiConversationHistory: Anthropic.MessageParam[]
}> { }> {
const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || [] const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || []
@@ -659,7 +659,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
if (historyItem) { if (historyItem) {
const taskDirPath = path.join(this.context.globalStorageUri.fsPath, "tasks", id) const taskDirPath = path.join(this.context.globalStorageUri.fsPath, "tasks", id)
const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory) const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
const claudeMessagesFilePath = path.join(taskDirPath, GlobalFileNames.claudeMessages) const uiMessagesFilePath = path.join(taskDirPath, GlobalFileNames.uiMessages)
const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath) const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
if (fileExists) { if (fileExists) {
const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8")) const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8"))
@@ -667,12 +667,13 @@ export class ClineProvider implements vscode.WebviewViewProvider {
historyItem, historyItem,
taskDirPath, taskDirPath,
apiConversationHistoryFilePath, apiConversationHistoryFilePath,
claudeMessagesFilePath, uiMessagesFilePath,
apiConversationHistory, apiConversationHistory,
} }
} }
} }
// if we tried to get a task that doesn't exist, remove it from state // if we tried to get a task that doesn't exist, remove it from state
// FIXME: this seems to happen sometimes when the json file doesnt save to disk for some reason
await this.deleteTaskFromState(id) await this.deleteTaskFromState(id)
throw new Error("Task not found") throw new Error("Task not found")
} }
@@ -696,20 +697,24 @@ export class ClineProvider implements vscode.WebviewViewProvider {
await this.clearTask() await this.clearTask()
} }
const { taskDirPath, apiConversationHistoryFilePath, claudeMessagesFilePath } = await this.getTaskWithId(id) const { taskDirPath, apiConversationHistoryFilePath, uiMessagesFilePath } = await this.getTaskWithId(id)
await this.deleteTaskFromState(id)
// Delete the task files // Delete the task files
const apiConversationHistoryFileExists = await fileExistsAtPath(apiConversationHistoryFilePath) const apiConversationHistoryFileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
if (apiConversationHistoryFileExists) { if (apiConversationHistoryFileExists) {
await fs.unlink(apiConversationHistoryFilePath) await fs.unlink(apiConversationHistoryFilePath)
} }
const claudeMessagesFileExists = await fileExistsAtPath(claudeMessagesFilePath) const uiMessagesFileExists = await fileExistsAtPath(uiMessagesFilePath)
if (claudeMessagesFileExists) { if (uiMessagesFileExists) {
await fs.unlink(claudeMessagesFilePath) await fs.unlink(uiMessagesFilePath)
}
const legacyMessagesFilePath = path.join(taskDirPath, "claude_messages.json")
if (await fileExistsAtPath(legacyMessagesFilePath)) {
await fs.unlink(legacyMessagesFilePath)
} }
await fs.rmdir(taskDirPath) // succeeds if the dir is empty await fs.rmdir(taskDirPath) // succeeds if the dir is empty
await this.deleteTaskFromState(id)
} }
async deleteTaskFromState(id: string) { async deleteTaskFromState(id: string) {