mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-20 12:21:13 -05:00
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:
@@ -36,10 +36,18 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics {
|
|||||||
contextTokens: 0,
|
contextTokens: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the last api_req_started message to get the context size
|
// Find the last api_req_started message that has valid token information
|
||||||
const lastApiReq = [...messages]
|
const lastApiReq = [...messages].reverse().find((message) => {
|
||||||
.reverse()
|
if (message.type === "say" && message.say === "api_req_started" && message.text) {
|
||||||
.find((message) => 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) => {
|
messages.forEach((message) => {
|
||||||
if (message.type === "say" && message.say === "api_req_started" && message.text) {
|
if (message.type === "say" && message.say === "api_req_started" && message.text) {
|
||||||
|
|||||||
Reference in New Issue
Block a user