70 lines
4.9 KiB
JavaScript
70 lines
4.9 KiB
JavaScript
import { isPassthrough } from './model-capacity.mjs';
|
|
const object = value => value && typeof value === 'object' && !Array.isArray(value);
|
|
const fail = message => { throw new Error('Messages 请求未发送:' + message); };
|
|
|
|
function contentBlocks(content) {
|
|
if (typeof content === 'string') return content ? [{ type: 'text', text: content }] : [];
|
|
if (!Array.isArray(content)) fail('消息内容必须为文本或已接入的内容块。');
|
|
return content.map(part => {
|
|
if (['text', 'input_text', 'output_text'].includes(part.type) && typeof part.text === 'string') return { type: 'text', text: part.text };
|
|
const url = part.type === 'image_url' ? part.image_url?.url : part.type === 'input_image' ? part.image_url : null;
|
|
if (typeof url === 'string' && /^data:image\/png;base64,[A-Za-z0-9+/=]+$/.test(url)) return { type: 'image', source: { type: 'base64', media_type: 'image/png', data: url.slice(22) } };
|
|
fail('未接入的输入内容块 ' + String(part?.type ?? '未知') + '。');
|
|
});
|
|
}
|
|
|
|
export function messagesBody(config, messages, tools, native) {
|
|
if (!isPassthrough(config) && native.nativeParameters.thinking?.type === 'enabled' && native.nativeParameters.thinking.budget_tokens >= config.maxOutputTokens) fail('thinking.budget_tokens 必须小于本次 max_tokens 输出预算。');
|
|
const system = [], wire = [];
|
|
const append = (role, content) => {
|
|
if (!content.length) return;
|
|
if (wire.at(-1)?.role === role) wire.at(-1).content.push(...content);
|
|
else wire.push({ role, content });
|
|
};
|
|
for (const message of messages) {
|
|
if (message.role === 'system') { system.push(...contentBlocks(message.content)); continue; }
|
|
if (message.role === 'tool') {
|
|
if (typeof message.tool_call_id !== 'string' || !message.tool_call_id || typeof message.content !== 'string') fail('工具结果缺少原调用身份或文本。');
|
|
append('user', [{ type: 'tool_result', tool_use_id: message.tool_call_id, content: message.content }]);
|
|
} else if (message.role === 'user') append('user', contentBlocks(message.content));
|
|
else if (message.role === 'assistant') {
|
|
const state = message.providerState;
|
|
if (state?.protocol === 'anthropic-messages' && state.endpoint === config.baseUrl && state.model === config.model) {
|
|
if (!Array.isArray(state.content)) fail('原生助手历史格式无效。');
|
|
append('assistant', structuredClone(state.content));
|
|
continue;
|
|
}
|
|
const blocks = contentBlocks(message.content ?? '');
|
|
for (const call of message.tool_calls ?? []) {
|
|
let input; try { input = JSON.parse(call.function?.arguments); } catch { fail('工具历史参数不是完整 JSON。'); }
|
|
if (!object(input) || typeof call.id !== 'string' || !call.id || typeof call.function?.name !== 'string' || !call.function.name) fail('工具历史身份或参数无效。');
|
|
blocks.push({ type: 'tool_use', id: call.id, name: call.function.name, input });
|
|
}
|
|
append('assistant', blocks);
|
|
} else fail('未接入的消息角色 ' + String(message.role) + '。');
|
|
}
|
|
if (system.some(block => block.type !== 'text')) fail('系统消息只支持文本。');
|
|
const definitions = (tools ?? []).map(tool => {
|
|
if (tool.type !== 'function' || !tool.function?.name || !object(tool.function.parameters)) fail('工具定义格式无效。');
|
|
return { name: tool.function.name, ...(tool.function.description ? { description: tool.function.description } : {}), input_schema: tool.function.parameters };
|
|
});
|
|
return { model: config.model, ...(system.length ? { system } : {}), messages: wire,
|
|
...(definitions.length ? { tools: definitions, tool_choice: { type: 'auto' } } : {}),
|
|
...(!isPassthrough(config) ? { max_tokens: config.maxOutputTokens } : {}), stream: config.streaming === true, ...native.nativeParameters };
|
|
}
|
|
|
|
export function messagesHistory(body) {
|
|
const history = [];
|
|
if (typeof body.system === 'string') history.push({ role: 'system', content: body.system });
|
|
else for (const part of body.system ?? []) history.push({ role: 'system', content: part.text });
|
|
for (const message of body.messages) {
|
|
if (!Array.isArray(message.content)) { history.push(message); continue; }
|
|
const blocks = message.content, text = blocks.filter(block => block.type === 'text').map(block => block.text).join('');
|
|
const calls = blocks.filter(block => block.type === 'tool_use').map(block => ({ id: block.id, type: 'function', function: { name: block.name, arguments: JSON.stringify(block.input) } }));
|
|
const results = blocks.filter(block => block.type === 'tool_result');
|
|
for (const result of results) history.push({ role: 'tool', tool_call_id: result.tool_use_id, content: result.content });
|
|
if (message.role === 'assistant' || text || blocks.some(block => block.type === 'image')) history.push({ role: message.role, content: text, ...(calls.length ? { tool_calls: calls } : {}) });
|
|
}
|
|
return history;
|
|
}
|