From 86eb38322752454e3aa9484d7d4e04b5be74a540 Mon Sep 17 00:00:00 2001 From: MFPires Date: Tue, 28 Jan 2025 21:49:26 -0300 Subject: [PATCH] fix: prevent context token counter flickering during API requests Maintains the last valid context token count between API requests by: - Adding tracking of last valid context tokens - Using last known value as fallback when current request lacks token data - Only updating when new valid token counts are available This prevents the context counter from flickering to "-" during tool calls and API request transitions, improving the UI stability. --- src/shared/getApiMetrics.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/shared/getApiMetrics.ts b/src/shared/getApiMetrics.ts index 882c4aa14b9..af94462ea09 100644 --- a/src/shared/getApiMetrics.ts +++ b/src/shared/getApiMetrics.ts @@ -49,6 +49,9 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { return false }) + // Keep track of the last valid context tokens + let lastValidContextTokens = 0 + messages.forEach((message) => { if (message.type === "say" && message.say === "api_req_started" && message.text) { try { @@ -71,12 +74,15 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { result.totalCost += cost } + // Update last valid context tokens whenever we have valid input and output tokens + if (tokensIn > 0 && tokensOut > 0) { + lastValidContextTokens = tokensIn + tokensOut + } + // If this is the last api request, use its tokens for context size if (message === lastApiReq) { - // Only update context tokens if both input and output tokens are non-zero - if (tokensIn > 0 && tokensOut > 0) { - result.contextTokens = tokensIn + tokensOut - } + // Use the last valid context tokens if the current request doesn't have valid tokens + result.contextTokens = tokensIn > 0 && tokensOut > 0 ? tokensIn + tokensOut : lastValidContextTokens } } catch (error) { console.error("Error parsing JSON:", error)