区分 usage 帧与错误帧:choices=[] 含 usage 时不再误报 onBadEvent

OpenAI 兼容协议在 stream_options.include_usage=true 时,最后一帧只发
usage 不带 choices(choices: [])。callModel 之前把这种帧当成错误帧调
onBadEvent,导致日志里 '[sse] 上游错误帧' 后面跟的是 token 统计 json,
误导排查方向。

判断条件改为 'choices=[] 但含 usage → 静默 return';只有 '无 usage 也无
choices' 的帧(如之前的 event: error 帧)才走 onBadEvent。

usage 提取移到 if (evt.usage) usage = evt.usage; 在 if (!choice) 守卫之前
已有的赋值语句,新逻辑在 !choice 块内复用该结果。
This commit is contained in:
2026-09-05 07:25:20 +08:00
parent a3499429ec
commit fcb69112ac
+3 -1
View File
@@ -188,7 +188,9 @@ async function callModel(cfg, modelId, messages, tools, options) {
if (evt.usage) usage = evt.usage;
const choice = evt.choices && evt.choices[0];
if (!choice) {
if (opts.onBadEvent) opts.onBadEvent(evt);
// choices=[] 但含 usage:OpenAI 兼容协议最后一帧只发 token 统计,正常情况,
// 不算错误帧。其它无 usage 也无 choices 的(如 event: error 帧)才走 onBadEvent。
if (!evt.usage && opts.onBadEvent) opts.onBadEvent(evt);
return;
}
if (choice.finish_reason) finishReason = choice.finish_reason;