Skip to content

Commit e8a6ac5

Browse files
committed
fix: combine redundant conditional branches in drainStreamInBackgroundToFindAllUsage
- Merged if (usageFound) and else if blocks that both called captureUsageData with identical parameters into a single condition - Eliminates code duplication and improves maintainability
1 parent 986ee81 commit e8a6ac5

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/core/task/Task.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,14 +1509,21 @@ export class Task extends EventEmitter<ClineEvents> {
15091509
}
15101510

15111511
try {
1512+
// Continue processing the original stream from where the main loop left off
15121513
let usageFound = false
15131514
let chunkCount = 0
1515+
1516+
// Use the same iterator that the main loop was using
15141517
while (!item.done) {
15151518
// Check for timeout
15161519
if (Date.now() - startTime > timeoutMs) {
15171520
console.warn(
15181521
`[Background Usage Collection] Timed out after ${timeoutMs}ms for model: ${modelId}, processed ${chunkCount} chunks`,
15191522
)
1523+
// Clean up the iterator before breaking
1524+
if (iterator.return) {
1525+
await iterator.return(undefined)
1526+
}
15201527
break
15211528
}
15221529

@@ -1534,24 +1541,14 @@ export class Task extends EventEmitter<ClineEvents> {
15341541
}
15351542
}
15361543

1537-
if (usageFound) {
1538-
await captureUsageData(
1539-
{
1540-
input: bgInputTokens,
1541-
output: bgOutputTokens,
1542-
cacheWrite: bgCacheWriteTokens,
1543-
cacheRead: bgCacheReadTokens,
1544-
total: bgTotalCost,
1545-
},
1546-
lastApiReqIndex,
1547-
)
1548-
} else if (
1544+
if (
1545+
usageFound ||
15491546
bgInputTokens > 0 ||
15501547
bgOutputTokens > 0 ||
15511548
bgCacheWriteTokens > 0 ||
15521549
bgCacheReadTokens > 0
15531550
) {
1554-
// We have some usage data even if we didn't find a usage chunk
1551+
// We have usage data either from a usage chunk or accumulated tokens
15551552
await captureUsageData(
15561553
{
15571554
input: bgInputTokens,

0 commit comments

Comments
 (0)