Files
OmniRoute/open-sse/utils/usageOnlyChunk.ts
Dizzle 91990d606b fix(sse): emit trailing usage estimate in translate streams when upstream stays silent (#12828)
Validado numa worktree combinada com a onda de streaming desta leva sobre `release/v3.8.51`: typecheck:core limpo, check-api-typecheck OK (289), check-file-size OK após rebaseline, 88/88 nos testes focados.

O #12151 cobriu só metade: passthrough emitia o chunk final de usage, translate calculava a estimativa **depois** de fechar o stream, então o número só chegava ao log do servidor e nunca ao cliente. Fechar essa metade é o que faz a feature existir de fato.

Não emitir segundo chunk quando o upstream já mandou usage real é o detalhe que impede a correção de virar contagem dobrada.
2026-09-10 10:19:02 -03:00

16 lines
504 B
TypeScript

/**
* Canonical OpenAI trailing usage-only chunk (`choices: []`) sent before `[DONE]`
* when the upstream never reported usage, so metered chat clients still see
* token counts (#12151). Shared by the passthrough and translate flushes.
*/
export function buildUsageOnlyChunk(id: unknown, model: unknown, usage: unknown) {
return {
id: id ?? `chatcmpl-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model,
choices: [],
usage,
};
}