Explorar el Código

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.
MFPires hace 11 meses
padre
commit
86eb383227
Se han modificado 1 ficheros con 10 adiciones y 4 borrados
  1. 10 4
      src/shared/getApiMetrics.ts

+ 10 - 4
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)