feat(models): add native Anthropic Messages and observed endpoint diagnostics

This commit is contained in:
Agent Workbench Dev
2026-11-03 18:58:59 +08:00
parent b832982d46
commit 65ddee16b0
36 changed files with 751 additions and 87 deletions
@@ -0,0 +1,68 @@
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 (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' } } : {}),
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;
}
+1
View File
@@ -23,6 +23,7 @@ export function modelSnapshot(config) {
...modelWaitSettings(config),
...modelIdentitySettings(config),
...(config.protocol ? { protocol: config.protocol, streaming: config.streaming === true } : {}),
...(config.protocol === 'anthropic-messages' ? { messagesAuth: config.messagesAuth ?? 'api-key' } : {}),
...(config.nativeParameters ? { nativeParameters: structuredClone(config.nativeParameters), outputTokenField: config.outputTokenField } : {}),
...(config.modelRouting ? { routing: structuredClone(config.modelRouting) } : {}),
};
+2 -2
View File
@@ -1,9 +1,9 @@
export const MODEL_PROTOCOLS = ['chat-completions', 'responses'];
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 协议。');
if (!MODEL_PROTOCOLS.includes(value)) throw failure('请选择已接入的 Chat Completions、Responses 或 Anthropic Messages 协议。');
return value;
}
@@ -0,0 +1,5 @@
export function protocolLabel(protocol: string): string;
export function protocolRoute(config: { protocol?: string; baseUrl: string }): string;
export function modelRequestUrl(config: { protocol?: string; baseUrl: string }): string;
export function messagesAuthSettings(protocol?: string, value?: string): { messagesAuth?: string };
export function validateProtocolEndpoint(protocol: string, baseUrl: string): string;
+29
View File
@@ -0,0 +1,29 @@
import { modelProtocol } from './model-capabilities.mjs';
export const protocolLabel = protocol => ({ 'chat-completions': 'Chat Completions', responses: 'Responses', 'anthropic-messages': 'Anthropic Messages' }[protocol] ?? protocol);
export function protocolRoute(config) {
const protocol = modelProtocol(config.protocol);
if (protocol === 'responses') return '/responses';
if (protocol === 'anthropic-messages') {
validateProtocolEndpoint(protocol, config.baseUrl);
return /\/v1$/i.test(new URL(config.baseUrl).pathname.replace(/\/+$/, '')) ? '/messages' : '/v1/messages';
}
return '/chat/completions';
}
export function modelRequestUrl(config) { return config.baseUrl.replace(/\/+$/, '') + protocolRoute(config); }
export function messagesAuthSettings(protocol, value) {
if (protocol !== 'anthropic-messages') return {};
const messagesAuth = value ?? 'api-key';
if (!['api-key', 'bearer'].includes(messagesAuth)) throw Object.assign(new Error('Messages 鉴权请选择 x-api-key 或 Bearer。'), { status: 400 });
return { messagesAuth };
}
export function validateProtocolEndpoint(protocol, baseUrl) {
if (protocol === 'anthropic-messages' && /\/(?:messages|responses|chat\/completions)$/i.test(new URL(baseUrl).pathname.replace(/\/+$/, ''))) {
throw Object.assign(new Error('请填写 Messages 服务根地址或 /v1 根地址,不要填写完整的 /messages 或其它协议路径。'), { status: 400 });
}
return baseUrl;
}
+1 -1
View File
@@ -19,7 +19,7 @@ export function modelIdentityObserver(requestedModel, config = {}) {
const settings = modelIdentitySettings(config), models = new Set(); let invalid = false;
return {
observe(value) {
for (const object of [value, value?.response]) {
for (const object of [value, value?.response, ...(value?.type === 'message_start' ? [value.message] : [])]) {
if (!object || !Object.hasOwn(object, 'model')) continue;
if (!validModel(object.model) || models.size >= 16 && !models.has(object.model)) { invalid = true; continue; }
models.add(object.model);
+6 -3
View File
@@ -1,6 +1,7 @@
import { createHash } from 'node:crypto';
import { modelProtocol } from './model-capabilities.mjs';
import { nativeParameterSettings } from './native-parameters.mjs';
import { messagesBody, messagesHistory } from './anthropic-messages.mjs';
function wireBody(config, messages, tools) {
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
@@ -9,6 +10,7 @@ function wireBody(config, messages, tools) {
? [{type:'input_text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'input_image',image_url:image.dataUrl,detail:'high'}))]
: [{type:'text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'image_url',image_url:{url:image.dataUrl,detail:'high'}}))]}];
const cleanMessages = messages.map(({ providerState, ...message }) => message);
if (protocol === 'anthropic-messages') return messagesBody(config, messages, tools, native);
if (protocol === 'chat-completions') return { model: config.model, messages: cleanMessages, tools, tool_choice: 'auto', [native.outputTokenField]: config.maxOutputTokens, stream, ...native.nativeParameters,
...(stream ? { stream_options: { include_usage: true } } : {}) };
const input = messages.flatMap(message => {
@@ -26,7 +28,7 @@ function wireBody(config, messages, tools) {
export function completionBody(config, messages, tools) {
const members = config.modelRouting?.members;
if (members?.some(member => member.protocol === 'responses' || member.streaming === true || member.nativeParameters || member.outputTokenField)) {
if (members?.some(member => member.protocol && member.protocol !== 'chat-completions' || member.streaming === true || member.nativeParameters || member.outputTokenField)) {
return { schema: 1, kind: 'model-route-plan', messages, variants: members.map(member => ({ connectionId: member.id,
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, nativeParameters: member.nativeParameters ?? {}, outputTokenField: member.outputTokenField,
maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
@@ -48,6 +50,7 @@ export function actualRequest(body, member) {
export const outputReservation = body => body.max_output_tokens ?? body.max_completion_tokens ?? body.max_tokens;
export function requestHistory(body) {
if (body.system !== undefined || body.tools?.some(tool => tool.input_schema) || body.messages?.some(message => Array.isArray(message.content) && message.content.some(part => ['tool_use', 'tool_result', 'thinking', 'image'].includes(part.type)))) return messagesHistory(body);
if (body.messages) return body.messages;
const messages = [];
for (const item of body.input ?? []) {
@@ -67,7 +70,7 @@ export function requestMetrics(body) {
const variants = requestVariants(body);
const images=requestImages(body);
const imageReserve=imageTokenReserve(body);
const estimates=variants.map(value=>Math.ceil(Buffer.byteLength(JSON.stringify(value, (key,item)=>typeof item==='string'&&item.startsWith('data:image/')?'[bounded image bytes]':item),'utf8')/3)
const estimates=variants.map(value=>Math.ceil(Buffer.byteLength(JSON.stringify(value, (key,item)=>typeof item==='string'&&item.startsWith('data:image/')?'[bounded image bytes]':item?.type==='base64'&&item.media_type==='image/png'?{...item,data:'[bounded image bytes]'}:item),'utf8')/3)
+ (value.messages?.length ?? value.input?.length ?? 0)*8+(value.tools?.length??0)*16+imageTokenReserve(value));
if(images.length)return {bytes,sha256:createHash('sha256').update(serialized,'utf8').digest('hex'),estimatedInputTokens:Math.max(...estimates),
imageReservationTokens:imageReserve,imageCount:Math.max(...variants.map(value=>requestImages(value).length)),estimateMethod:'text-utf8-plus-image-reservation-v1',imageEstimate:'local-reservation-not-provider-billing'};
@@ -78,7 +81,7 @@ export function requestMetrics(body) {
}
export function requestImages(body) {
return requestVariants(body).flatMap(value=>(value.messages??value.input??[]).flatMap(message=>Array.isArray(message.content)?message.content.filter(part=>['image_url','input_image'].includes(part.type)).map(part=>part.type==='image_url'?part.image_url?.url:part.image_url):[]));
return requestVariants(body).flatMap(value=>(value.messages??value.input??[]).flatMap(message=>Array.isArray(message.content)?message.content.filter(part=>['image_url','input_image','image'].includes(part.type)).map(part=>part.type==='image_url'?part.image_url?.url:part.type==='image'?`data:${part.source?.media_type};base64,${part.source?.data}`:part.image_url):[]));
}
export function imageTokenReserve(body){
return Math.max(0,...requestVariants(body).map(value=>requestImages(value).reduce((total,url)=>{
+8 -3
View File
@@ -5,11 +5,16 @@ const verbosity = ['low', 'medium', 'high'];
export function nativeParameterSettings(protocol = 'chat-completions', parameters = {}, outputTokenField) {
if (!object(parameters) || JSON.stringify(parameters).length > 8192) fail('原生参数必须是8KB以内的JSON对象。');
const allowed = protocol === 'responses' ? ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning', 'text']
const allowed = protocol === 'anthropic-messages' ? ['temperature', 'top_p', 'top_k', 'stop_sequences', 'thinking'] : protocol === 'responses' ? ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning', 'text']
: ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning_effort', 'verbosity', 'seed'];
for (const [key, value] of Object.entries(parameters)) {
if (!allowed.includes(key)) fail('所选协议尚未接入原生参数:' + key + '。参数未保存或发送。');
if (key === 'temperature' && (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 2)) fail('temperature应为0–2之间的数字。');
if (key === 'temperature' && (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > (protocol === 'anthropic-messages' ? 1 : 2))) fail('temperature超出所选协议范围(Messages 0–1,其它 0–2)。');
if (key === 'top_k' && (!Number.isSafeInteger(value) || value < 0)) fail('Messages top_k应为非负安全整数。');
if (key === 'stop_sequences' && (!Array.isArray(value) || value.length > 32 || value.some(item => typeof item !== 'string' || !item || item.length > 1000))) fail('Messages stop_sequences应为最多32项的非空字符串数组。');
if (key === 'thinking' && (!object(value) || !['enabled', 'disabled', 'adaptive'].includes(value.type)
|| Object.keys(value).some(field => !['type', 'budget_tokens'].includes(field))
|| (value.type === 'enabled' ? !Number.isSafeInteger(value.budget_tokens) || value.budget_tokens < 1024 : value.budget_tokens !== undefined))) fail('Messages thinking格式为disabled/adaptive,或enabled加至少1024的budget_tokens。');
if (key === 'top_p' && (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 1)) fail('top_p应为0–1之间的数字。');
if (key === 'seed' && !Number.isSafeInteger(value)) fail('seed应为安全整数。');
if (key === 'parallel_tool_calls' && typeof value !== 'boolean') fail('parallel_tool_calls应为布尔值。');
@@ -19,6 +24,6 @@ export function nativeParameterSettings(protocol = 'chat-completions', parameter
if (key === 'text' && (!object(value) || Object.keys(value).length !== 1 || !verbosity.includes(value.verbosity))) fail('Responses text当前接入格式为{"verbosity":"low|medium|high"}。');
}
const field = outputTokenField ?? (protocol === 'responses' ? 'max_output_tokens' : 'max_tokens');
if (!(protocol === 'responses' ? ['max_output_tokens'] : ['max_tokens', 'max_completion_tokens']).includes(field)) fail('输出上限字段与所选协议不兼容。');
if (!(protocol === 'responses' ? ['max_output_tokens'] : protocol === 'anthropic-messages' ? ['max_tokens'] : ['max_tokens', 'max_completion_tokens']).includes(field)) fail('输出上限字段与所选协议不兼容。');
return { nativeParameters: structuredClone(parameters), outputTokenField: field };
}