SSE 错误帧识别:data 解析成功但无 choices 时调 onBadEvent

上游 DeepSeek 代理在某些情况下会以 SSE event: error 帧返回错误响应(如
'Assistant tool call function.arguments must be valid JSON.'),data 行本身
JSON.parse 没问题但 evt.choices 缺失。旧逻辑走 if (!choice) return 沉默跳过,
结果就是 'AI 回复 0 字' 但没有任何信号告诉调用方上游其实报了错。

callModel 在 !choice 之前调 opts.onBadEvent(evt) 把整个 evt 透传给调用方,
streamAssistant 挂上 warn 级日志把错误帧 dump 出来。

调试样例(之前看到的):
  [sse] task-... 上游错误帧:{"error":{"message":"Assistant tool call
   function.arguments must be valid JSON.",...},"id":0}

llm.js 仅改 1 处(callModel 回调体内 if (!choice) 块),其余调试信号不动。
This commit is contained in:
2026-09-05 07:12:54 +08:00
parent 893281c96c
commit 0d5d74bcf6
2 changed files with 7 additions and 1 deletions
+4 -1
View File
@@ -187,7 +187,10 @@ async function callModel(cfg, modelId, messages, tools, options) {
if (opts.shouldCancel && opts.shouldCancel()) { aborted = true; controller.abort(); return; }
if (evt.usage) usage = evt.usage;
const choice = evt.choices && evt.choices[0];
if (!choice) return;
if (!choice) {
if (opts.onBadEvent) opts.onBadEvent(evt);
return;
}
if (choice.finish_reason) finishReason = choice.finish_reason;
const delta = choice.delta || {};
if (typeof delta.content === 'string' && delta.content.length) {