fix: 平台 type 放开校验;模型唯一键改为 平台名/模型id
- type 仅决定余额适配器,未知值合法(查不了余额而已),不再报非法 - 打平视图 cfg.models:id = 内部唯一键「平台名/模型id」(池/会话/用量/前端按它认模型,调用处字段名不变), mid = 发给上游的原始模型名(callModel body.model 用它) - 同平台内模型 id 必须唯一;不同平台允许同 id(旧版直接报错的问题) - 旧 models[] 迁移时平台名自动去重,避免唯一键碰撞 - 前端:卡片/编辑器展示 mid(原始名),编辑弹窗新增时按 平台+id 查重 - concurrency 仍仅解析记录,调度保持每模型 1 会话
This commit is contained in:
+98
-16
@@ -22,7 +22,7 @@ const DEFAULTS = {
|
||||
model_hold: { idle_release_ms: 180000, active_max_ms: 1800000, sweep_ms: 10000 },
|
||||
// 日志等级:debug < info < warn < error < none,只输出 >= 阈值等级的日志;none=全部静默。
|
||||
log_level: 'info',
|
||||
models: [],
|
||||
platforms: [],
|
||||
// 登录认证:{ users: [{user, password}], secret?, ttl_ms? },密码明文存配置(自用场景)。
|
||||
// users 为空则不启用认证(可交给前置 nginx-proxy,但手机浏览器原生下载不带 Basic 头,不推荐)。
|
||||
auth: null,
|
||||
@@ -43,23 +43,99 @@ function deepMerge(base, extra) {
|
||||
return out;
|
||||
}
|
||||
|
||||
function hostnameOf(url) {
|
||||
try { return new URL(url).hostname.split('.')[0]; } catch (_) { return ''; }
|
||||
}
|
||||
|
||||
// 旧版扁平 models[](每模型自带 url/api_key)自动迁移为 platforms 层级:同 url+api_key 归为同一平台同一 key。
|
||||
function migrateLegacyModels(models) {
|
||||
const groups = [];
|
||||
const byKey = new Map();
|
||||
for (const m of models) {
|
||||
if (!m || !m.id) continue;
|
||||
const url = String(m.url || '').replace(/\/+$/, '');
|
||||
const apiKey = String(m.api_key || '');
|
||||
const gk = url + '\n' + apiKey;
|
||||
let p = byKey.get(gk);
|
||||
if (!p) {
|
||||
// 平台名 = 主机名首段;同名(如只 path 不同)自动加序号,避免「平台名/模型id」唯一键碰撞。
|
||||
let name = hostnameOf(url) || ('p' + (groups.length + 1));
|
||||
for (let n = 2; groups.some(g => g.name === name); n++) name = (hostnameOf(url) || 'p') + '-' + n;
|
||||
p = { name, url, type: 'openai', keys: [{ api_key: apiKey, concurrency: 1, models: [] }] };
|
||||
byKey.set(gk, p);
|
||||
groups.push(p);
|
||||
}
|
||||
p.keys[0].models.push({ id: m.id, display_name: m.display_name || m.id, vision: m.vision === true });
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
// 规范化并校验 platforms 层级:平台(name/url/type) → keys(api_key/concurrency) → models(id/display_name/vision)。
|
||||
// 内部唯一键 = 平台名/模型id:不同平台允许同模型 id,同平台内必须唯一(会话/模型池/用量均按该键认模型);
|
||||
// 允许键/列表为空(便于页面增删)。
|
||||
function normalizePlatforms(rawPlatforms) {
|
||||
const seen = new Set(); // 已用唯一键(平台名/模型id)
|
||||
const out = [];
|
||||
for (let pi = 0; pi < rawPlatforms.length; pi++) {
|
||||
const p = rawPlatforms[pi] || {};
|
||||
if (!p.url) throw new Error('platforms[' + pi + '] 缺少 url(OpenAI 兼容 /v1 地址)');
|
||||
const url = String(p.url).replace(/\/+$/, '');
|
||||
const name = String(p.name || '').trim() || (hostnameOf(url) || ('p' + (pi + 1)));
|
||||
// type 决定余额查询适配器(预留);openai/deepseek/custom 之外也允许,只是查不了余额。
|
||||
const type = String(p.type || 'openai').trim();
|
||||
const keys = [];
|
||||
for (let ki = 0; ki < (p.keys || []).length; ki++) {
|
||||
const k = p.keys[ki] || {};
|
||||
const concurrency = Math.max(1, parseInt(k.concurrency, 10) || 1);
|
||||
const models = [];
|
||||
for (let mi = 0; mi < (k.models || []).length; mi++) {
|
||||
const m = k.models[mi] || {};
|
||||
if (!m || !m.id) throw new Error('平台「' + name + '」key#' + (ki + 1) + ' 的 models[' + mi + '] 缺少 id');
|
||||
const uid = name + '/' + String(m.id);
|
||||
if (seen.has(uid)) throw new Error('模型重复:平台「' + name + '」下已有模型 id:' + m.id + '(不同平台可同名,同平台内必须唯一)');
|
||||
seen.add(uid);
|
||||
models.push({
|
||||
id: String(m.id),
|
||||
display_name: m.display_name ? String(m.display_name) : String(m.id),
|
||||
vision: m.vision === true, // 支持时 read_file 读图会回传图片,会话上下文会告知 AI
|
||||
});
|
||||
}
|
||||
keys.push({ api_key: String(k.api_key || ''), concurrency, models });
|
||||
}
|
||||
out.push({ name, url, type, keys });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// 校验原始配置文本(启动读文件与网页编辑共用)。configPath 仅记录来源,可为空。
|
||||
function parseAndValidate(rawText, configPath) {
|
||||
const raw = YAML.parse(rawText || '') || {};
|
||||
let raw = YAML.parse(rawText || '') || {};
|
||||
const cfg = deepMerge(DEFAULTS, raw);
|
||||
|
||||
if (!Array.isArray(cfg.models) || cfg.models.length === 0) {
|
||||
throw new Error('配置缺少 models:至少需要一个模型 {id, url, display_name}')
|
||||
// ---------- 模型配置:platforms 层级(platforms[].keys[].models[])----------
|
||||
// 旧版扁平 models[](每模型自带 url/api_key)自动迁移;cfg.models 为打平后的运行时视图,
|
||||
// 每项继承所属平台的 url/type 与 key 的 api_key/concurrency,并携带位置下标(前端 PATCH 定位用)。
|
||||
if ((!Array.isArray(raw.platforms) || !raw.platforms.length) && Array.isArray(raw.models) && raw.models.length) {
|
||||
raw = Object.assign({}, raw, { platforms: migrateLegacyModels(raw.models) });
|
||||
log.info('[config] 检测到旧配置 models[] 扁平格式,已自动迁移为 platforms 层级;建议改写 config.yml 为 platforms 格式');
|
||||
}
|
||||
// 每个模型独立配置上游:url 必填(OpenAI 兼容 /v1 地址),api_key 选填(留空则不带鉴权头)
|
||||
for (const m of cfg.models) {
|
||||
if (!m || !m.id) throw new Error('models 中每项必须有 id');
|
||||
if (!m.url) throw new Error('models.' + m.id + ' 缺少 url(每个模型独立配置上游 /v1 地址)');
|
||||
if (!m.display_name) m.display_name = m.id;
|
||||
m.url = String(m.url).replace(/\/+$/, '');
|
||||
m.api_key = m.api_key || '';
|
||||
// 模型是否支持 vision(图片内容回传);默认 false,会话上下文中会告知 AI
|
||||
m.vision = m.vision === true;
|
||||
if (!Array.isArray(raw.platforms) || !raw.platforms.length) {
|
||||
throw new Error('配置缺少 platforms:至少需要一个平台 {name, url, keys:[{api_key, models:[{id}]}]}')
|
||||
}
|
||||
cfg.platforms = normalizePlatforms(raw.platforms);
|
||||
cfg.models = [];
|
||||
cfg.platforms.forEach((p, pi) => p.keys.forEach((k, ki) => k.models.forEach((m, mi) => {
|
||||
cfg.models.push({
|
||||
// id = 内部唯一键「平台名/模型id」(池/会话/用量/前端均按它认模型,调用处无需区分);
|
||||
// mid = 发给上游的原始模型名(请求体 body.model 用它)。
|
||||
id: p.name + '/' + m.id, mid: m.id,
|
||||
display_name: m.display_name, vision: m.vision,
|
||||
url: p.url, api_key: k.api_key, platform: p.name, type: p.type, concurrency: k.concurrency,
|
||||
platform_index: pi, key_index: ki, model_index: mi,
|
||||
});
|
||||
})));
|
||||
if (!cfg.models.length) {
|
||||
throw new Error('platforms 下没有任何可用模型:keys[].models 至少需要一个 {id}')
|
||||
}
|
||||
|
||||
cfg.workspace_root = path.resolve(cfg.workspace_root);
|
||||
@@ -137,15 +213,21 @@ function readConfigText(cfg) {
|
||||
return fs.readFileSync(p, 'utf8');
|
||||
}
|
||||
|
||||
// 单项编辑:[{path:[...], value}] 或 [{path:[...], delete:true}]。
|
||||
// 用 YAML Document 写回,保留注释与排版;路径段为纯数字时按数组下标处理(如 ["models",0,"url"])。
|
||||
// 单项编辑:[{path:[...], value}] 或 [{path:[...], delete:true}] 或 [{path:[...], value, append:true}]。
|
||||
// 用 YAML Document 写回,保留注释与排版;路径段为纯数字时按数组下标处理(如 ["platforms",0,"keys",1,"models",0])。
|
||||
// append:向目标数组追加一项(新增平台/Key/模型用);目标不存在时创建新数组。
|
||||
function applyEditsToText(text, edits) {
|
||||
const doc = YAML.parseDocument(text || '');
|
||||
for (const e of edits || []) {
|
||||
if (!e || !Array.isArray(e.path) || !e.path.length) throw new Error('配置项 path 必须是非空数组');
|
||||
const docPath = e.path.map(s => (typeof s === 'string' && /^\d+$/.test(s)) ? Number(s) : s);
|
||||
if (e.delete) doc.deleteIn(docPath);
|
||||
else doc.setIn(docPath, e.value);
|
||||
else if (e.append) {
|
||||
const target = doc.getIn(docPath, true);
|
||||
if (target && typeof target.add === 'function') target.add(e.value);
|
||||
else if (target === null || target === undefined) doc.setIn(docPath, [e.value]);
|
||||
else throw new Error('配置项 ' + docPath.join('.') + ' 不是数组,无法追加');
|
||||
} else doc.setIn(docPath, e.value);
|
||||
}
|
||||
return doc.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user