fix(models): retain upstream identity and pause strict mismatches

This commit is contained in:
Agent Workbench Dev
2026-11-03 11:53:07 +08:00
parent 410b347894
commit 3659f5e858
25 changed files with 427 additions and 35 deletions
+2 -1
View File
@@ -5,7 +5,7 @@ import { upstreamCapture } from './upstream-error.mjs';
// Only a fully terminated and validated stream produces a dispatchable message.
// Progress is cumulative, bounded and explicitly not a tool invocation.
export async function readModelStream(response, config, signal, onProgress, tools, onBytes, redact = String) {
export async function readModelStream(response, config, signal, onProgress, tools, onBytes, redact = String, observeIdentity) {
if (!response.headers.get('content-type')?.toLowerCase().includes('text/event-stream') || !response.body) throw protocolFailure('服务未返回所选协议的事件流,请核对流式配置。');
const reader = response.body.getReader(), decoder = new TextDecoder('utf-8', { fatal: true }), hash = createHash('sha256');
const chunks = [];
@@ -33,6 +33,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
if (raw === '[DONE]') { if (config.protocol === 'responses') throw protocolFailure('Responses缺少正式终结事件。'); done = true; return; }
let event;
try { event = JSON.parse(raw); } catch { throw protocolFailure('模型事件流包含无效JSON。'); }
observeIdentity?.(event);
if (event.usage) usage = event.usage;
const failUpstream = () => {
usage = event.response?.usage ?? usage;