73 lines
4.3 KiB
JavaScript
73 lines
4.3 KiB
JavaScript
import { createHash } from 'node:crypto';
|
|
import { modelProtocol } from './model-capabilities.mjs';
|
|
|
|
function wireBody(config, messages, tools) {
|
|
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
|
|
const cleanMessages = messages.map(({ providerState, ...message }) => message);
|
|
if (protocol === 'chat-completions') return { model: config.model, messages: cleanMessages, tools, tool_choice: 'auto', max_tokens: config.maxOutputTokens, stream,
|
|
...(stream ? { stream_options: { include_usage: true } } : {}) };
|
|
const input = messages.flatMap(message => {
|
|
if (message.role === 'tool') return [{ type: 'function_call_output', call_id: message.tool_call_id, output: message.content }];
|
|
if (message.role === 'assistant') {
|
|
if (message.providerState?.protocol === 'responses' && message.providerState.endpoint === config.baseUrl && message.providerState.model === config.model) return message.providerState.items;
|
|
return [...(message.content ? [{ role: 'assistant', content: message.content }] : []), ...(message.tool_calls ?? []).map(call => ({
|
|
type: 'function_call', call_id: call.id, name: call.function.name, arguments: call.function.arguments }))];
|
|
}
|
|
return [{ role: message.role, content: message.content }];
|
|
});
|
|
return { model: config.model, input, tools: (tools ?? []).map(tool => ({ type: 'function', ...tool.function, strict: false })),
|
|
tool_choice: 'auto', max_output_tokens: config.maxOutputTokens, stream, store: false, include: ['reasoning.encrypted_content'] };
|
|
}
|
|
|
|
export function completionBody(config, messages, tools) {
|
|
const members = config.modelRouting?.members;
|
|
if (members?.some(member => member.protocol === 'responses' || member.streaming === true)) {
|
|
return { schema: 1, kind: 'model-route-plan', messages, variants: members.map(member => ({ connectionId: member.id,
|
|
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
|
|
}
|
|
return wireBody(config, messages, tools);
|
|
}
|
|
|
|
export function requestVariants(body) {
|
|
return body.kind === 'model-route-plan' ? body.variants.map(value => value.body) : [body];
|
|
}
|
|
|
|
export function actualRequest(body, member) {
|
|
if (body.kind !== 'model-route-plan') return { ...body, model: member.model };
|
|
const variant = body.variants.find(value => value.connectionId === member.id);
|
|
if (!variant || variant.protocol !== modelProtocol(member.protocol) || variant.body.model !== member.model) throw new Error('实际连接与已封存协议请求不一致。');
|
|
return structuredClone(variant.body);
|
|
}
|
|
|
|
export const outputReservation = body => body.max_output_tokens ?? body.max_tokens;
|
|
|
|
export function requestHistory(body) {
|
|
if (body.messages) return body.messages;
|
|
const messages = [];
|
|
for (const item of body.input ?? []) {
|
|
if (item.type === 'reasoning') continue;
|
|
if (item.type === 'function_call_output') messages.push({ role: 'tool', tool_call_id: item.call_id, content: item.output });
|
|
else if (item.type === 'function_call') {
|
|
if (!messages.at(-1)?.tool_calls) messages.push({ role: 'assistant', content: '', tool_calls: [] });
|
|
messages.at(-1).tool_calls.push({ id: item.call_id, type: 'function', function: { name: item.name, arguments: item.arguments } });
|
|
} else messages.push({ role: item.role, content: typeof item.content === 'string' ? item.content : JSON.stringify(item.content) });
|
|
}
|
|
return messages;
|
|
}
|
|
|
|
export function requestMetrics(body) {
|
|
const serialized = JSON.stringify(body);
|
|
const bytes = Buffer.byteLength(serialized, 'utf8');
|
|
const variants = requestVariants(body);
|
|
return { bytes, sha256: createHash('sha256').update(serialized, 'utf8').digest('hex'),
|
|
estimatedInputTokens: Math.max(...variants.map(value => Math.ceil(Buffer.byteLength(JSON.stringify(value), 'utf8') / 3)
|
|
+ (value.messages?.length ?? value.input?.length ?? 0) * 8 + (value.tools?.length ?? 0) * 16)),
|
|
estimateMethod: body.kind === 'model-route-plan' ? 'maximum-wire-variant-utf8-v1' : 'utf8-third-plus-framing-v1' };
|
|
}
|
|
|
|
export function assertPlannedRequest(body, plan) {
|
|
if (!plan) return;
|
|
const metrics = requestMetrics(body);
|
|
if (metrics.sha256 !== plan.requestSha256 || metrics.bytes !== plan.requestBytes) throw new Error('模型请求正文与已保存计划不一致,未发送请求。');
|
|
}
|