fix(models): retain upstream identity and pause strict mismatches
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user