MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / readApiMessages

Function readApiMessages

src/core/task-persistence/apiMessages.ts:40–107  ·  view source on GitHub ↗
({
	taskId,
	globalStoragePath,
}: {
	taskId: string
	globalStoragePath: string
})

Source from the content-addressed store, hash-verified

38}
39
40export async function readApiMessages({
41 taskId,
42 globalStoragePath,
43}: {
44 taskId: string
45 globalStoragePath: string
46}): Promise<ApiMessage[]> {
47 const taskDir = await getTaskDirectoryPath(globalStoragePath, taskId)
48 const filePath = path.join(taskDir, GlobalFileNames.apiConversationHistory)
49
50 if (await fileExistsAtPath(filePath)) {
51 const fileContent = await fs.readFile(filePath, "utf8")
52 try {
53 const parsedData = JSON.parse(fileContent)
54 if (!Array.isArray(parsedData)) {
55 console.warn(
56 `[readApiMessages] Parsed data is not an array (got ${typeof parsedData}), returning empty. TaskId: ${taskId}, Path: ${filePath}`,
57 )
58 return []
59 }
60 if (parsedData.length === 0) {
61 console.error(
62 `[Roo-Debug] readApiMessages: Found API conversation history file, but it's empty (parsed as []). TaskId: ${taskId}, Path: ${filePath}`,
63 )
64 }
65 return parsedData
66 } catch (error) {
67 console.warn(
68 `[readApiMessages] Error parsing API conversation history file, returning empty. TaskId: ${taskId}, Path: ${filePath}, Error: ${error}`,
69 )
70 return []
71 }
72 } else {
73 const oldPath = path.join(taskDir, "claude_messages.json")
74
75 if (await fileExistsAtPath(oldPath)) {
76 const fileContent = await fs.readFile(oldPath, "utf8")
77 try {
78 const parsedData = JSON.parse(fileContent)
79 if (!Array.isArray(parsedData)) {
80 console.warn(
81 `[readApiMessages] Parsed OLD data is not an array (got ${typeof parsedData}), returning empty. TaskId: ${taskId}, Path: ${oldPath}`,
82 )
83 return []
84 }
85 if (parsedData.length === 0) {
86 console.error(
87 `[Roo-Debug] readApiMessages: Found OLD API conversation history file (claude_messages.json), but it's empty (parsed as []). TaskId: ${taskId}, Path: ${oldPath}`,
88 )
89 }
90 await fs.unlink(oldPath)
91 return parsedData
92 } catch (error) {
93 console.warn(
94 `[readApiMessages] Error parsing OLD API conversation history file (claude_messages.json), returning empty. TaskId: ${taskId}, Path: ${oldPath}, Error: ${error}`,
95 )
96 // DO NOT unlink oldPath if parsing failed.
97 return []

Calls 6

getTaskDirectoryPathFunction · 0.90
fileExistsAtPathFunction · 0.90
parseMethod · 0.80
warnMethod · 0.65
errorMethod · 0.65
readFileMethod · 0.45

Tested by

no test coverage detected