fix issue with costs resetting once chat is completed

This commit is contained in:
Dax Raad 2025-06-29 19:43:03 -04:00
parent ca48a4f0fb
commit 248325925f

View file

@ -502,15 +502,6 @@ export namespace Session {
} }
text = undefined text = undefined
}, },
async onFinish(input) {
log.info("message finish", {
reason: input.finishReason,
})
const assistant = next.metadata!.assistant!
const usage = getUsage(model.info, input.usage, input.providerMetadata)
assistant.cost = usage.cost
await updateMessage(next)
},
onError(err) { onError(err) {
log.error("callback error", err) log.error("callback error", err)
switch (true) { switch (true) {
@ -681,7 +672,7 @@ export namespace Session {
value.usage, value.usage,
value.providerMetadata, value.providerMetadata,
) )
assistant.cost = usage.cost assistant.cost += usage.cost
await updateMessage(next) await updateMessage(next)
if (value.finishReason === "length") if (value.finishReason === "length")
throw new Message.OutputLengthError({}) throw new Message.OutputLengthError({})
@ -830,7 +821,7 @@ export namespace Session {
async onFinish(input) { async onFinish(input) {
const assistant = next.metadata!.assistant! const assistant = next.metadata!.assistant!
const usage = getUsage(model.info, input.usage, input.providerMetadata) const usage = getUsage(model.info, input.usage, input.providerMetadata)
assistant.cost = usage.cost assistant.cost += usage.cost
assistant.tokens = usage.tokens assistant.tokens = usage.tokens
next.metadata!.time.completed = Date.now() next.metadata!.time.completed = Date.now()
await updateMessage(next) await updateMessage(next)