Skip to content

Commit e168cbd

Browse files
committed
Throttle calls to calculate task folder size
1 parent 22e18dd commit e168cbd

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/core/Cline.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ export class Cline extends EventEmitter<ClineEvents> {
307307
}
308308

309309
private async addToApiConversationHistory(message: Anthropic.MessageParam) {
310-
const messageWithTs = { ...message, ts: Date.now() }
310+
const ts = Date.now()
311+
const messageWithTs = { ...message, ts }
311312
this.apiConversationHistory.push(messageWithTs)
312313
await this.saveApiConversationHistory()
314+
console.log(`addToApiConversationHistory: ${Date.now() - ts}ms`)
313315
}
314316

315317
async overwriteApiConversationHistory(newHistory: Anthropic.MessageParam[]) {
@@ -362,7 +364,13 @@ export class Cline extends EventEmitter<ClineEvents> {
362364
this.emit("message", { action: "updated", message: partialMessage })
363365
}
364366

367+
private taskDirSize = 0
368+
private taskDirSizeCheckedAt = 0
369+
private readonly taskDirSizeCheckInterval = 30_000
370+
365371
private async saveClineMessages() {
372+
const ts = Date.now()
373+
366374
try {
367375
const taskDir = await this.ensureTaskDirectoryExists()
368376
const filePath = path.join(taskDir, GlobalFileNames.uiMessages)
@@ -381,14 +389,15 @@ export class Cline extends EventEmitter<ClineEvents> {
381389
)
382390
]
383391

384-
let taskDirSize = 0
385-
386-
try {
387-
taskDirSize = await getFolderSize.loose(taskDir)
388-
} catch (err) {
389-
console.error(
390-
`[saveClineMessages] failed to get task directory size (${taskDir}): ${err instanceof Error ? err.message : String(err)}`,
391-
)
392+
if (Date.now() - this.taskDirSizeCheckedAt > this.taskDirSizeCheckInterval) {
393+
try {
394+
this.taskDirSize = await getFolderSize.loose(taskDir)
395+
this.taskDirSizeCheckedAt = Date.now()
396+
} catch (err) {
397+
console.error(
398+
`[saveClineMessages] failed to get task directory size (${taskDir}): ${err instanceof Error ? err.message : String(err)}`,
399+
)
400+
}
392401
}
393402

394403
await this.providerRef.deref()?.updateTaskHistory({
@@ -401,12 +410,14 @@ export class Cline extends EventEmitter<ClineEvents> {
401410
cacheWrites: tokenUsage.totalCacheWrites,
402411
cacheReads: tokenUsage.totalCacheReads,
403412
totalCost: tokenUsage.totalCost,
404-
size: taskDirSize,
413+
size: this.taskDirSize,
405414
workspace: this.cwd,
406415
})
407416
} catch (error) {
408417
console.error("Failed to save cline messages:", error)
409418
}
419+
420+
console.log(`saveClineMessages: ${Date.now() - ts}ms`)
410421
}
411422

412423
// Communicate with webview
@@ -1885,11 +1896,13 @@ export class Cline extends EventEmitter<ClineEvents> {
18851896
// now add to apiconversationhistory
18861897
// need to save assistant responses to file before proceeding to tool use since user can exit at any moment and we wouldn't be able to save the assistant's response
18871898
let didEndLoop = false
1899+
18881900
if (assistantMessage.length > 0) {
18891901
await this.addToApiConversationHistory({
18901902
role: "assistant",
18911903
content: [{ type: "text", text: assistantMessage }],
18921904
})
1905+
18931906
telemetryService.captureConversationMessage(this.taskId, "assistant")
18941907

18951908
// NOTE: this comment is here for future reference - this was a workaround for userMessageContent not getting set to true. It was due to it not recursively calling for partial blocks when didRejectTool, so it would get stuck waiting for a partial block to complete before it could continue.

0 commit comments

Comments
 (0)