fix: align web token usage display with TUI

Fixes #3322

Use tokens from the last assistant message instead of summing all
messages. This matches TUI behavior and shows the actual context size.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
majiayu000 2025-12-22 18:54:29 +08:00
parent a95aa037a3
commit 111fb5ee16

View file

@ -277,9 +277,6 @@ export default function Share(props: { id: string; api: string; info: Session.In
if (msg.role === "assistant") {
result.cost += msg.cost
result.tokens.input += msg.tokens.input
result.tokens.output += msg.tokens.output
result.tokens.reasoning += msg.tokens.reasoning
result.models[`${msg.providerID} ${msg.modelID}`] = [msg.providerID, msg.modelID]
@ -292,6 +289,16 @@ export default function Share(props: { id: string; api: string; info: Session.In
}
}
}
// Use tokens from the last assistant message (like TUI does)
// This represents the actual context size, not cumulative tokens
const lastAssistant = msgs.findLast((x) => x.role === "assistant" && x.tokens?.output > 0)
if (lastAssistant && lastAssistant.role === "assistant") {
result.tokens.input = lastAssistant.tokens.input
result.tokens.output = lastAssistant.tokens.output
result.tokens.reasoning = lastAssistant.tokens.reasoning
}
return result
})