67 lines
5.4 KiB
JavaScript
67 lines
5.4 KiB
JavaScript
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
|
||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
|
||
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
|
||
|
||
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;
|
||
if (calls !== undefined && (!Array.isArray(calls) || calls.length > 12)) throw protocolFailure('模型工具调用数量或格式无效。');
|
||
const ids = new Set();
|
||
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);
|
||
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;
|
||
}
|
||
|
||
export function normalizeProtocolResponse(config, value) {
|
||
if (config.protocol !== 'responses') return value;
|
||
if (value.status !== 'completed' || !Array.isArray(value.output)) throw protocolFailure('Responses未完整结束,原响应未用于执行工具。');
|
||
const calls = [], texts = [], ids = new Set();
|
||
for (const item of value.output) {
|
||
if (item.type === 'function_call') {
|
||
if (item.status && item.status !== 'completed') throw protocolFailure('Responses工具参数未完成。');
|
||
if (typeof item.call_id !== 'string' || !item.call_id || ids.has(item.call_id) || typeof item.name !== 'string' || !item.name || typeof item.arguments !== 'string') throw protocolFailure('Responses工具调用身份或参数无效。');
|
||
ids.add(item.call_id); calls.push({ id: item.call_id, type: 'function', function: { name: item.name, arguments: item.arguments } });
|
||
} else if (item.type === 'message') {
|
||
if (item.role !== 'assistant' || item.status && item.status !== 'completed' || !Array.isArray(item.content)) throw protocolFailure('Responses消息尚未完成或格式无效。');
|
||
for (const content of item.content) {
|
||
if (content.type === 'output_text' && typeof content.text === 'string') texts.push(content.text);
|
||
else if (content.type === 'refusal' && typeof content.refusal === 'string') texts.push(content.refusal);
|
||
else throw protocolFailure('Responses包含当前尚未接入的输出内容类型:' + String(content?.type ?? '未标注').slice(0, 100) + '。');
|
||
}
|
||
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型:' + String(item.type ?? '未标注').slice(0, 100) + '。');
|
||
}
|
||
const raw = value.usage, usage = raw ? { prompt_tokens: raw.input_tokens, completion_tokens: raw.output_tokens,
|
||
...(raw.total_tokens !== undefined ? { total_tokens: raw.total_tokens } : {}), raw, protocol: 'responses' } : null;
|
||
return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}),
|
||
providerState: { protocol: 'responses', endpoint: config.baseUrl, model: config.model, responseId: value.id ?? null, items: value.output } }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage };
|
||
}
|