Roo-Code/src/core/task-persistence/taskMessages.ts
KJ7LNW 8455909809
fix: use safeWriteJson for all JSON file writes with race condition fix (#4733)
Co-authored-by: Eric Wheeler <roo-code@z.ewheeler.org>
Co-authored-by: Daniel Riccio <ricciodaniel98@gmail.com>
2025-06-25 16:05:02 -04:00

42 lines
1.2 KiB
TypeScript

import { safeWriteJson } from "../../utils/safeWriteJson"
import * as path from "path"
import * as fs from "fs/promises"
import type { ClineMessage } from "@roo-code/types"
import { fileExistsAtPath } from "../../utils/fs"
import { GlobalFileNames } from "../../shared/globalFileNames"
import { getTaskDirectoryPath } from "../../utils/storage"
export type ReadTaskMessagesOptions = {
taskId: string
globalStoragePath: string
}
export async function readTaskMessages({
taskId,
globalStoragePath,
}: ReadTaskMessagesOptions): Promise<ClineMessage[]> {
const taskDir = await getTaskDirectoryPath(globalStoragePath, taskId)
const filePath = path.join(taskDir, GlobalFileNames.uiMessages)
const fileExists = await fileExistsAtPath(filePath)
if (fileExists) {
return JSON.parse(await fs.readFile(filePath, "utf8"))
}
return []
}
export type SaveTaskMessagesOptions = {
messages: ClineMessage[]
taskId: string
globalStoragePath: string
}
export async function saveTaskMessages({ messages, taskId, globalStoragePath }: SaveTaskMessagesOptions) {
const taskDir = await getTaskDirectoryPath(globalStoragePath, taskId)
const filePath = path.join(taskDir, GlobalFileNames.uiMessages)
await safeWriteJson(filePath, messages)
}