fix: account failed attempts and stop unchanged task loops in 1.20.1
This commit is contained in:
@@ -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