37 lines
2.2 KiB
JavaScript
37 lines
2.2 KiB
JavaScript
export const MODEL_PROTOCOLS = ['chat-completions', 'responses', 'anthropic-messages'];
|
|
export const MODEL_CAPABILITIES = ['text', 'tools', 'streaming', 'vision', 'audio'];
|
|
const failure = message => Object.assign(new Error(message), { status: 400 });
|
|
|
|
export function modelProtocol(value = 'chat-completions') {
|
|
if (!MODEL_PROTOCOLS.includes(value)) throw failure('请选择已接入的 Chat Completions、Responses 或 Anthropic Messages 协议。');
|
|
return value;
|
|
}
|
|
|
|
export function capabilityDeclarations(input, previous = null) {
|
|
const result = { text: 'supported', tools: 'supported', streaming: 'unknown', vision: 'unknown', audio: 'unknown', ...previous };
|
|
if (input === undefined) return result;
|
|
if (!input || typeof input !== 'object' || Array.isArray(input)) throw failure('模型能力声明格式无效。');
|
|
for (const [key, value] of Object.entries(input)) {
|
|
if (!MODEL_CAPABILITIES.includes(key) || !['supported', 'unsupported', 'unknown'].includes(value)) throw failure('能力只能声明支持、不支持或未知,不能自行标为已验证。');
|
|
result[key] = value;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function capabilityView(connection, credentialRevision = 0) {
|
|
const declarations = capabilityDeclarations(undefined, connection.capabilityDeclarations);
|
|
const evidence = connection.capabilityEvidence;
|
|
const current = evidence?.connectionRevision === connection.revision && evidence?.credentialRevision === credentialRevision;
|
|
return Object.fromEntries(MODEL_CAPABILITIES.map(key => [key,
|
|
declarations[key] === 'unsupported' ? 'unsupported' : current && evidence.results?.[key] === 'verified' ? 'verified'
|
|
: declarations[key] === 'supported' ? 'protocol_declared' : 'unknown']));
|
|
}
|
|
|
|
export function capabilityRejections(connection, requirements, credentialRevision = 0) {
|
|
const view = capabilityView(connection, credentialRevision);
|
|
return requirements.filter(key => !['verified', 'protocol_declared'].includes(view[key])).map(capability => ({
|
|
connectionId: connection.id, connectionName: connection.name, capability, state: view[capability] ?? 'unknown',
|
|
reason: view[capability] === 'unsupported' ? '连接明确声明不支持所需能力' : '连接尚未声明或验证所需能力',
|
|
}));
|
|
}
|