fix: account failed attempts and stop unchanged task loops in 1.20.1
This commit is contained in:
@@ -309,6 +309,13 @@ export class ModelHub {
|
||||
}
|
||||
this.attempts.push(attempt); this.saveAttempts();
|
||||
const progress = event => {
|
||||
if (event.consumption) {
|
||||
attempt.usage = event.consumption.usage; attempt.usageStatus = event.consumption.usageStatus;
|
||||
onProgress?.({ consumption: { ...event.consumption, wireAttemptId: event.consumption.attemptId, attemptId: attempt.id,
|
||||
connectionId: member.id, connectionName: member.name }, connectionId: member.id, model: member.model, modelAttemptId: attempt.id });
|
||||
this.saveAttempts();
|
||||
return;
|
||||
}
|
||||
if (event.streamCapture !== undefined) {
|
||||
attempt.stream = event.stream;
|
||||
if (plan?.sessionId) {
|
||||
@@ -326,7 +333,8 @@ export class ModelHub {
|
||||
onProgress?.({ ...event, attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, connectionName: member.name, protocol: modelProtocol(member.protocol), model: member.model, modelAttemptId: attempt.id });
|
||||
};
|
||||
const result = await execute({ ...config, baseUrl: member.baseUrl, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true }, credential?.key ?? '', actualBody, progress);
|
||||
attempt.status = 'completed'; attempt.usage = result.usage ?? null; attempt.usageStatus = result.usage ? 'reported' : 'unknown';
|
||||
attempt.status = 'completed'; attempt.usage = result.usage ?? attempt.usage; attempt.usageStatus = attempt.usage ? 'reported' : 'unknown';
|
||||
if (result.compatibility?.length) attempt.compatibility = result.compatibility;
|
||||
this.health.set(this.identity(member), { state: 'responded', lastSuccessAt: stamp(), cooldownUntil: 0, failures: 0 });
|
||||
return result;
|
||||
} catch (cause) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { protocolFailure, normalizeProtocolResponse, validateModelResponse } from './protocols.mjs';
|
||||
import { protocolFailure, normalizeProtocolResponse, validateModelResponse, reportedUsage } from './protocols.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) {
|
||||
export async function readModelStream(response, config, signal, onProgress, tools) {
|
||||
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 = [];
|
||||
@@ -53,7 +53,10 @@ export async function readModelStream(response, config, signal, onProgress) {
|
||||
} else if (event.type === 'response.completed') {
|
||||
if (!event.response || responseId && event.response.id !== responseId) throw protocolFailure('Responses终结身份与创建身份不一致。');
|
||||
terminal = event.response;
|
||||
} else if (['response.failed', 'response.incomplete', 'response.cancelled'].includes(event.type)) throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
|
||||
} else if (['response.failed', 'response.incomplete', 'response.cancelled'].includes(event.type)) {
|
||||
usage = event.response?.usage ?? usage;
|
||||
throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
|
||||
}
|
||||
} else {
|
||||
if (!Array.isArray(event.choices)) throw protocolFailure('Chat流缺少choices数组。');
|
||||
if (event.usage !== undefined && event.usage !== null) usage = event.usage;
|
||||
@@ -125,13 +128,13 @@ export async function readModelStream(response, config, signal, onProgress) {
|
||||
if (!done || !['stop', 'tool_calls'].includes(finish)) throw protocolFailure('Chat流未完整终结或达到输出上限,未执行工具。', 'stream_interrupted');
|
||||
value = { choices: [{ message: { role: 'assistant', content: text, ...(calls.size ? { tool_calls: [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([, item]) => item) } : {}) }, finish_reason: finish }], usage };
|
||||
}
|
||||
validateModelResponse(config.protocol === 'responses' ? normalizeProtocolResponse(config, value) : value);
|
||||
validateModelResponse(config.protocol === 'responses' ? normalizeProtocolResponse(config, value) : value, tools);
|
||||
validated = true; progress(true);
|
||||
return { value, evidence: { bytes, frames, sha256: hash.copy().digest('hex'), terminal: true, protocol: config.protocol ?? 'chat-completions' } };
|
||||
} finally {
|
||||
if (progressTimer) clearTimeout(progressTimer);
|
||||
try { await reader.cancel(); } catch {} reader.releaseLock();
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? usage }), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done, protocol: config.protocol ?? 'chat-completions' } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,26 @@ export const protocolFailure = (message, kind = 'protocol') => new ModelRequestE
|
||||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
|
||||
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
|
||||
|
||||
export function validateModelResponse(value) {
|
||||
export function reportedUsage(config, value) {
|
||||
const raw = value?.usage;
|
||||
if (!raw || typeof raw !== 'object') return null;
|
||||
const input = config.protocol === 'responses' ? raw.input_tokens : raw.prompt_tokens;
|
||||
const output = config.protocol === 'responses' ? raw.output_tokens : raw.completion_tokens;
|
||||
const valid = number => Number.isSafeInteger(number) && number >= 0;
|
||||
if (!valid(raw.total_tokens) && !(valid(input) && valid(output))) return null;
|
||||
return { ...(valid(input) ? { prompt_tokens: input } : {}), ...(valid(output) ? { completion_tokens: output } : {}),
|
||||
total_tokens: valid(raw.total_tokens) ? raw.total_tokens : input + output };
|
||||
}
|
||||
|
||||
function acceptsEmptyObject(tools, name) {
|
||||
const tool = tools?.find(tool => (tool.function?.name ?? tool.name) === name);
|
||||
const schema = tool?.function?.parameters ?? tool?.parameters;
|
||||
// Restrict adaptation to schemas whose empty-object validity is evident.
|
||||
return schema?.type === 'object' && !schema.required?.length && Object.keys(schema).every(key =>
|
||||
['type', 'properties', 'required', 'additionalProperties', 'description', 'title', '$schema'].includes(key));
|
||||
}
|
||||
|
||||
export function validateModelResponse(value, tools) {
|
||||
const choice = value.choices?.[0], message = choice?.message;
|
||||
if (!message || choice.finish_reason === 'length' || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或没有完整结束,未执行工具。');
|
||||
const calls = message.tool_calls;
|
||||
@@ -12,8 +31,12 @@ export function validateModelResponse(value) {
|
||||
for (const call of calls ?? []) {
|
||||
if (!call.id || typeof call.id !== 'string' || ids.has(call.id) || call.type !== 'function' || typeof call.function?.name !== 'string' || !call.function.name || typeof call.function.arguments !== 'string') throw protocolFailure('模型工具身份或参数无效。');
|
||||
ids.add(call.id);
|
||||
let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('模型工具参数不是完整JSON,未执行工具。'); }
|
||||
if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('模型工具参数必须为完整对象。');
|
||||
if (call.function.arguments.trim() === '' && acceptsEmptyObject(tools, call.function.name)) {
|
||||
(value.compatibility ??= []).push({ kind: 'empty_object_arguments', callId: call.id, tool: call.function.name, originalArguments: call.function.arguments, arguments: '{}' });
|
||||
call.function.arguments = '{}';
|
||||
}
|
||||
let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('工具 ' + call.function.name + '(' + call.id + ')参数不是完整JSON,未执行这组工具。'); }
|
||||
if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('工具 ' + call.function.name + '(' + call.id + ')参数必须为完整对象,未执行这组工具。');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user