fix(messageThunk): ensure usage is only estimated when not already present in response (#5553)

* fix(messageThunk): ensure usage is only estimated when not already present in response

* refactor: clean logic
This commit is contained in:
SuYao 2025-05-01 11:04:43 +08:00 committed by GitHub
parent f9690d52ee
commit d05f6985bb

View File

@ -603,7 +603,7 @@ const fetchAndProcessAssistantResponseImpl = async (
const finalStateOnComplete = getState()
const finalAssistantMsg = finalStateOnComplete.messages.entities[assistantMsgId]
if (status === 'success' && finalAssistantMsg && response && !response?.usage) {
if (status === 'success' && finalAssistantMsg) {
const userMsgId = finalAssistantMsg.askId
const orderedMsgs = selectMessagesForTopic(finalStateOnComplete, topicId)
const userMsgIndex = orderedMsgs.findIndex((m) => m.id === userMsgId)
@ -613,8 +613,10 @@ const fetchAndProcessAssistantResponseImpl = async (
// 更新topic的name
autoRenameTopic(assistant, topicId)
const usage = await estimateMessagesUsage({ assistant, messages: finalContextWithAssistant })
response.usage = usage
if (response && !response.usage) {
const usage = await estimateMessagesUsage({ assistant, messages: finalContextWithAssistant })
response.usage = usage
}
}
if (response && response.metrics) {
if (!response.metrics.completion_tokens && response.usage) {