SSE 本轮汇总:callModel 统计 data 帧数,streamAssistant 打 info 日志

定位 'AI 回复 0 字' 时单凭 finish_reason 和 aborted 区分不出 '上游没发任何
data' 和 '上游发了帧但 content 为空'。callModel 在 forEachSSEData 回调首行
累加 sseFrames 并写进返回值(不影响其他字段),streamAssistant 拿到 result
后打印一行 info:

  [sse] <sessionId> 本轮汇总:帧 N,finish=<reason>,aborted=<bool>

判读:
- 帧=0 且 aborted=false:上游未发任何 data(流异常/上游 200 但 body 空)
- 帧>0 且 finish=stop 且 content 空:上游发了帧但 content 为空(罕见,模型主动空回)
- 帧>0 且 finish=length:上游被截断,需要重试或调大 max_tokens
- aborted=true:用户在流中途取消

llm.js 保持纯 IO 职责,只多返回一个计数字段,不引入 logger。
This commit is contained in:
2026-09-05 06:09:28 +08:00
parent 510eb2fa3e
commit 893281c96c
2 changed files with 11 additions and 2 deletions
+3 -1
View File
@@ -178,10 +178,12 @@ async function callModel(cfg, modelId, messages, tools, options) {
let finishReason = null;
let usage = null;
let aborted = false;
let sseFrames = 0; // 收到的上游 data 帧数(仅计数成功下传到 onEvent 的)
const toolCalls = new Map(); // index -> {id, name, arguments}
try {
await forEachSSEData(res, evt => {
sseFrames++;
if (opts.shouldCancel && opts.shouldCancel()) { aborted = true; controller.abort(); return; }
if (evt.usage) usage = evt.usage;
const choice = evt.choices && evt.choices[0];
@@ -216,7 +218,7 @@ async function callModel(cfg, modelId, messages, tools, options) {
}
if (opts.shouldCancel && opts.shouldCancel()) aborted = true;
return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted };
return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted, sseFrames };
}
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, encodeBlocks, decodeBlocks };