model_profiles.tools_allow 语义修正:空数组 [] = 零工具(纯文本组合)

此前复用 MCP 的 normToolNames 约定(空数组视作未配置),导致 tools_allow: [] 的
profile 反而全量发工具。现只认「没写/非数组 = 不过滤」;[] 为有效配置 = 一个都不发
(callModel 对空数组不发 tools 字段)。与 MCP tools_allow 的约定有意分叉,文档同步。
This commit is contained in:
2026-09-22 12:13:18 +08:00
parent ef1e92f95c
commit 038f1d869b
4 changed files with 17 additions and 10 deletions
+5 -3
View File
@@ -46,9 +46,11 @@ platforms:
# 模型 profile(可选):预定义多套「prompt + tools 白名单」组合,platforms 里的模型用
# profile: <名字> 引用,同 id 模型跨平台复用时免重复配置。动机:默认发给模型的
# 内置工具 schema + system prompt 合计 ~20KB 字符(约 6K token),对特别小的模型会撑爆上下文。
# - tools_allow:白名单(字符串数组),只保留名单内的工具发给模型;内置名 + MCP 最终名
# (含冲突时加的 服务名__ 前缀,与 data/prompt.txt dump 一致可对)。不配 = 全量发。
# 只影响模型「看得到什么」:名单外但真实存在的工具若被调用,后端照常执行,不拦不误杀。
// - tools_allow:白名单(字符串数组),只保留名单内的工具发给模型;内置名 + MCP 最终名
// (含冲突时加的 服务名__ 前缀,与 data/prompt.txt dump 一致可对)。不配 = 全量发;
// 空数组 [] = 一个都不发(纯文本模式,给弱到不会调工具的小模型;与 MCP 的 tools_allow
// 「空名单视作未配」约定有意不同)。只影响模型「看得到什么」:名单外但真实存在的工具若被调用,
// 后端照常执行,不拦不误杀。
# - system_prompt_file:自定义 system prompt 文件,相对 server/templates/(也接受绝对路径),
# 配置加载时一次性读入常驻内存;设了就完全接管(不拼技能列表段,只保留末尾语言指令)。
# 文件缺失/为空直接校验报错。两者至少配一个;引用不存在的 profile 名同样直接报错。
+8 -4
View File
@@ -20,9 +20,10 @@ function normToolNames(v) {
const TEMPLATES_DIR = path.join(__dirname, 'templates');
// model_profiles 归一化:顶层 map {名称: {tools_allow?, system_prompt_file?}},
// 供 platforms[].models[].profile 引用(同 id 模型跨平台复用时免重复配置)。
// - tools_allow 复用 MCP 名单约定(normToolNames):只按最终发给模型的工具名匹配
// (内置名 + MCP 名,含冲突时加的 server__ 前缀);非数组/空数组 = 未配置不过滤。
// 供 platforms[].models[].profile 引用(同 id 模型跳平台复用时免重复配置)。
// - tools_allow 只按最终发给模型的工具名匹配(内置名 + MCP 名,含冲突时加的 server__ 前缀)。
// 语义与 MCP 的 tools_allow(空数组=未配)有意不同:这里空数组 [] 是有效配置 = 一个工具都不发
// (纯文本模式,给弱到不会调工具的小模型用);只有「没写/非数组」才是「不过滤、发全量」。
// - system_prompt_file:相对 templates/(也接受绝对路径),配置加载时一次性读入常驻内存,
// 运行期切换零 IO;缺失/读不到/内容为空直接报校验错——拦在保存/重启预检,不留到运行期降级。
// - 两样都没配 = 不起任何作用,报错误防误配空壳。
@@ -35,7 +36,10 @@ function normalizeModelProfiles(rawProfiles) {
if (!p || typeof p !== 'object' || Array.isArray(p)) {
throw new Error('model_profiles.' + key + ' 必须是对象 {tools_allow, system_prompt_file}');
}
const tools_allow = normToolNames(p.tools_allow);
// 不用 normToolNames:那会把空数组吐成 undefined(丢抑「[] = 零工具」语义);这里只归一字符串项。
const tools_allow = Array.isArray(p.tools_allow)
? p.tools_allow.map(x => String(x).trim()).filter(Boolean)
: undefined;
const file = String(p.system_prompt_file || '').trim();
let prompt = '';
if (file) {
+3 -2
View File
@@ -252,13 +252,14 @@ function resolveProfileName(session, run) {
}
// 本轮工具定义集:内置 + MCP 全量;profile 配了 tools_allow 则按最终名过滤
// (与发给模型的 tools 字段/prompt.txt dump 同源可对)。只影响模型「看得到什么」:
// (与发给模型的 tools 字段/prompt.txt dump 同源可对)。空数组 = 零工具(纯文本组合,
// callModel 对空数组不发 tools 字段);未配 = 全量。只影响模型「看得到什么」:
// 名单外但真实存在的工具若被调用,executor 照常执行(HANDLERS 全表分发),不拦不误杀。
function effectiveTools(profileName) {
const all = TOOL_DEFINITIONS.concat(mcp.toolDefinitions());
const prof = profileName && cfg.model_profiles && cfg.model_profiles[profileName];
const allow = prof && prof.tools_allow;
if (!allow) return all;
if (!Array.isArray(allow)) return all;
const set = new Set(allow);
return all.filter(t => t && t.function && set.has(t.function.name));
}