feat(models): pair reasoning controls with provider profiles and tool history

This commit is contained in:
Agent Workbench Dev
2026-11-05 00:13:53 +08:00
parent 3ab3a6e46a
commit f1242ee800
22 changed files with 546 additions and 34 deletions
+2
View File
@@ -25,6 +25,8 @@ export function modelSnapshot(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.parameterProfile ? { parameterProfile: config.parameterProfile } : {}),
...(config.modelConnectionId ? { modelConnectionId: config.modelConnectionId, modelCredentialRevision: config.modelCredentialRevision } : {}),
...(config.modelRouting ? { routing: structuredClone(config.modelRouting) } : {}),
};
}
@@ -0,0 +1,12 @@
export interface ParameterConnection {
protocol?: string; parameterProfile?: string; brand?: string; model?: string; baseUrl?: string;
nativeParameters?: Record<string, unknown>; capabilities?: Record<string, string>; capabilityDeclarations?: Record<string, string>;
}
export const reasoningEfforts: string[];
export const messagesEfforts: string[];
export function parameterProfiles(protocol: string): { id: string; label: string }[];
export function parameterProfileSettings(protocol: string, value?: string): string;
export function modelParameterProfile(connection: ParameterConnection): { id: string; label: string; source: string; efforts: string[]; thinking: boolean };
export function reasoningSetting(connection: ParameterConnection, parameters: Record<string, unknown>): { mode: string; effort: string; budget: number };
export function changeReasoningSetting(connection: ParameterConnection, parameters: Record<string, unknown>, field: string, value: string | number): Record<string, unknown>;
export function modelFeatureRows(connection: ParameterConnection): { name: string; upstream: string; local: string }[];
@@ -0,0 +1,69 @@
export const reasoningEfforts = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'];
export const messagesEfforts = ['low', 'medium', 'high', 'xhigh', 'max'];
const labels = { auto: '自动匹配', openai: 'OpenAI 兼容', anthropic: 'Claude Messages 兼容', deepseek: 'DeepSeek 兼容' };
export function parameterProfiles(protocol) {
return (protocol === 'anthropic-messages' ? ['auto', 'anthropic', 'deepseek'] : ['auto', 'openai', 'deepseek'])
.map(id => ({ id, label: labels[id] }));
}
export function parameterProfileSettings(protocol, value = 'auto') {
if (!parameterProfiles(protocol).some(profile => profile.id === value)) throw Object.assign(new Error('参数配置档与所选接口协议不兼容。'), { status: 400 });
return value;
}
// A profile describes the adapter's fields. It is not evidence that a vendor,
// proxy or particular model supports every field in that protocol.
export function modelParameterProfile(connection) {
const protocol = connection.protocol ?? 'chat-completions';
const selected = parameterProfileSettings(protocol, connection.parameterProfile);
const deepseek = connection.brand === 'DeepSeek' || /^deepseek(?:-|$)/i.test(connection.model ?? '')
|| /^https:\/\/api\.deepseek\.com(?:\/|$)/i.test(connection.baseUrl ?? '') || !!connection.nativeParameters?.thinking && protocol === 'chat-completions';
const id = selected !== 'auto' ? selected : deepseek ? 'deepseek' : protocol === 'anthropic-messages' ? 'anthropic' : 'openai';
return { id, label: labels[id], source: selected === 'auto' ? '按品牌、型号与协议匹配,可手动更改' : '用户选择的兼容格式',
efforts: id === 'deepseek' ? ['low', 'high', 'max'] : protocol === 'anthropic-messages' ? messagesEfforts : reasoningEfforts.filter(value => value !== 'none'),
thinking: protocol === 'anthropic-messages' || id === 'deepseek' && protocol === 'chat-completions' };
}
export function reasoningSetting(connection, parameters) {
const profile = modelParameterProfile(connection), protocol = connection.protocol;
const effort = protocol === 'anthropic-messages' ? parameters.output_config?.effort : protocol === 'responses' ? parameters.reasoning?.effort : parameters.reasoning_effort;
return { mode: profile.thinking ? parameters.thinking?.type ?? 'default' : effort === 'none' ? 'disabled' : effort ? 'enabled' : 'default',
effort: effort === 'none' ? '' : effort ?? '', budget: parameters.thinking?.budget_tokens ?? 1024 };
}
export function changeReasoningSetting(connection, parameters, field, value) {
const next = structuredClone(parameters), profile = modelParameterProfile(connection), protocol = connection.protocol;
const effortField = protocol === 'anthropic-messages' ? 'output_config' : protocol === 'responses' ? 'reasoning' : 'reasoning_effort';
const setEffort = effort => {
if (!effort) delete next[effortField];
else next[effortField] = effortField === 'reasoning_effort' ? effort : { effort };
};
if (field === 'effort') setEffort(value);
else if (field === 'budget') next.thinking = { type: 'enabled', budget_tokens: value };
else if (field === 'mode') {
if (value === 'default') { delete next.thinking; setEffort(''); }
else if (profile.thinking) {
next.thinking = value === 'enabled' && protocol === 'anthropic-messages'
? { type: value, budget_tokens: parameters.thinking?.budget_tokens ?? 1024 } : { type: value };
if (value === 'disabled') setEffort('');
} else {
delete next.thinking;
setEffort(value === 'disabled' ? 'none' : reasoningSetting(connection, parameters).effort || 'medium');
}
}
return next;
}
export function modelFeatureRows(connection) {
const states = { supported: '声明支持', unsupported: '声明不支持', unknown: '待确认', verified: '已验证', protocol_declared: '声明支持' };
const declared = key => states[connection.capabilities?.[key] ?? connection.capabilityDeclarations?.[key] ?? 'unknown'] ?? '待确认';
return [
{ name: '文本 / 工具调用', upstream: declared('text') + ' / ' + declared('tools'), local: '已接入,可单独检测' },
{ name: '流式输出', upstream: declared('streaming'), local: '已接入,按连接开关发送' },
{ name: '图像输入', upstream: declared('vision'), local: '已接入 PNG,可单独检测' },
{ name: '推理模式 / 强度', upstream: '依型号与兼容服务支持范围', local: '按配置档发送,受原输出预算约束' },
{ name: '语音 / 视频 / 服务端工具', upstream: '需供应商提供相应协议', local: '尚未接入' },
{ name: '结构化输出约束', upstream: '依型号与兼容服务支持范围', local: '尚未接入,参数保存时明确拒绝' },
];
}
+9 -3
View File
@@ -5,11 +5,17 @@ import { messagesBody, messagesHistory } from './anthropic-messages.mjs';
function wireBody(config, messages, tools) {
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
const native = nativeParameterSettings(protocol, config.nativeParameters ?? {}, config.outputTokenField);
const native = nativeParameterSettings(protocol, config.nativeParameters ?? {}, config.outputTokenField, config.parameterProfile);
if(config.inputImages?.length) messages=[...messages,{role:'user',content:protocol==='responses'
? [{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);
const cleanMessages = messages.map(({ providerState, reasoning_content, ...message }) => {
if (protocol === 'chat-completions' && message.role === 'assistant' && providerState?.protocol === 'chat-completions'
&& providerState.endpoint === config.baseUrl && providerState.model === config.model
&& providerState.connectionId === (config.modelConnectionId ?? null) && providerState.credentialRevision === (config.modelCredentialRevision ?? null)
&& typeof providerState.reasoningContent === 'string') return { ...message, reasoning_content: providerState.reasoningContent };
return 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 } } : {}) };
@@ -31,7 +37,7 @@ export function completionBody(config, messages, tools) {
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) })) };
modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision, maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
}
return wireBody(config, messages, tools);
}
+2 -1
View File
@@ -1,4 +1,5 @@
export function nativeParameterSettings(protocol?: string, parameters?: Record<string, unknown>, outputTokenField?: string): {
export function nativeParameterSettings(protocol?: string, parameters?: Record<string, unknown>, outputTokenField?: string, parameterProfile?: string): {
nativeParameters: Record<string, unknown>;
outputTokenField: string;
parameterProfile?: string;
};
+10 -7
View File
@@ -1,29 +1,32 @@
const fail = message => { throw Object.assign(new Error(message), { status: 400 }); };
const object = value => value && typeof value === 'object' && !Array.isArray(value);
const efforts = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'];
import { reasoningEfforts as efforts, messagesEfforts, parameterProfileSettings } from './model-parameter-profiles.mjs';
const verbosity = ['low', 'medium', 'high'];
export function nativeParameterSettings(protocol = 'chat-completions', parameters = {}, outputTokenField) {
export function nativeParameterSettings(protocol = 'chat-completions', parameters = {}, outputTokenField, parameterProfile) {
if (!object(parameters) || JSON.stringify(parameters).length > 8192) fail('原生参数必须是8KB以内的JSON对象。');
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'];
const allowed = protocol === 'anthropic-messages' ? ['temperature', 'top_p', 'top_k', 'stop_sequences', 'thinking', 'output_config'] : protocol === 'responses' ? ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning', 'text']
: ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning_effort', 'verbosity', 'seed', 'thinking'];
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 > (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)
if (key === 'thinking' && protocol === 'anthropic-messages' && (!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 === 'thinking' && protocol === 'chat-completions' && (!object(value) || Object.keys(value).length !== 1 || !['enabled', 'disabled'].includes(value.type))) fail('Chat thinking格式为{"type":"enabled"}或{"type":"disabled"},需服务支持DeepSeek兼容推理开关。');
if (key === 'output_config' && (!object(value) || Object.keys(value).length !== 1 || !messagesEfforts.includes(value.effort))) fail('Messages output_config当前接入格式为{"effort":"low|medium|high|xhigh|max"}。');
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应为布尔值。');
if (key === 'reasoning_effort' && !efforts.includes(value)) fail('reasoning_effort应为none/minimal/low/medium/high/xhigh。');
if (key === 'reasoning_effort' && !efforts.includes(value)) fail('reasoning_effort应为none/minimal/low/medium/high/xhigh/max。');
if (key === 'verbosity' && !verbosity.includes(value)) fail('verbosity应为low/medium/high。');
if (key === 'reasoning' && (!object(value) || Object.keys(value).length !== 1 || !efforts.includes(value.effort))) fail('Responses reasoning当前接入格式为{"effort":"low|medium|high等"}。');
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'] : protocol === 'anthropic-messages' ? ['max_tokens'] : ['max_tokens', 'max_completion_tokens']).includes(field)) fail('输出上限字段与所选协议不兼容。');
return { nativeParameters: structuredClone(parameters), outputTokenField: field };
return { nativeParameters: structuredClone(parameters), outputTokenField: field,
...(parameterProfile !== undefined ? { parameterProfile: parameterProfileSettings(protocol, parameterProfile) } : {}) };
}