fix: Make context token counter more reliable

- Only consider API requests with valid token information
- Skip messages with invalid/missing token data
- Prevent counter from resetting on action approval messages
- Ensure both tokensIn and tokensOut are valid numbers

This makes the context token counter more stable and accurate
by only updating on valid API responses with complete token data.
This commit is contained in:
MFPires
2025-01-28 00:27:17 -03:00
parent 97fe13dcb1
commit 5311e0c8ab

View File

@@ -36,10 +36,18 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics {
contextTokens: 0,
}
// Find the last api_req_started message to get the context size
const lastApiReq = [...messages]
.reverse()
.find((message) => message.type === "say" && message.say === "api_req_started" && message.text)
// Find the last api_req_started message that has valid token information
const lastApiReq = [...messages].reverse().find((message) => {
if (message.type === "say" && message.say === "api_req_started" && message.text) {
try {
const parsedData = JSON.parse(message.text)
return typeof parsedData.tokensIn === "number" && typeof parsedData.tokensOut === "number"
} catch {
return false
}
}
return false
})
messages.forEach((message) => {
if (message.type === "say" && message.say === "api_req_started" && message.text) {