feat(models): execute Responses and validated streams with capability routing

This commit is contained in:
Agent Workbench Dev
2026-10-31 22:27:53 +08:00
parent 578cf88eca
commit eeb11d6a76
24 changed files with 856 additions and 52 deletions
+43
View File
@@ -0,0 +1,43 @@
import { ModelRequestError } from '../../server/diagnostics.mjs';
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', stage: 'model_request',
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
export function validateModelResponse(value) {
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);
let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('模型工具参数不是完整JSON,未执行工具。'); }
if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('模型工具参数必须为完整对象。');
}
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包含当前尚未接入的输出内容类型。');
}
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型。');
}
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 };
}