diff --git a/README.md b/README.md index 7b263ff..bdcf741 100644 --- a/README.md +++ b/README.md @@ -809,19 +809,27 @@ networks: ## 十四、配置文件示例 ```yaml -models: - - id: ds1 - display_name: "AMD DeepSeek 1" +# 模型池:平台(platforms) → Key → 模型 三层。url/type 配在平台,api_key/concurrency 配在 Key, +# 模型只声明 {id, display_name, vision}。加载时打平成全局模型列表。 +platforms: + - name: proxy-a url: "https://proxy-a.example.com/v1" - api_key: "sk-..." - - id: ds2 - display_name: "AMD DeepSeek 2" - url: "https://proxy-b.example.com/v1" - api_key: "sk-..." - - id: ds3 - display_name: "AMD DeepSeek 3" + type: openai # openai/deepseek/custom,决定余额查询适配器(预留) + keys: + - api_key: "sk-..." + concurrency: 1 # 该 Key 并发会话数,默认 1(当前仅记录,未参与调度) + models: + - id: ds1 + display_name: "AMD DeepSeek 1" + # vision: true # 选填,默认 false + - name: proxy-c url: "https://proxy-c.example.com/v1" - api_key: "sk-..." + type: custom + keys: + - api_key: "sk-..." + models: + - id: ds3 + display_name: "AMD DeepSeek 3" workspace_root: "/workspace/tasks" diff --git a/app/config.example.yml b/app/config.example.yml index 6170236..580548d 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -1,22 +1,34 @@ # 自用通用 Agent 系统配置示例 # 复制为 config.yml 后按需修改;容器内通过 AGENT_CONFIG 指定路径。 -# 模型池:每个模型独立配置上游 url 与 api_key(互不共用,可指向不同代理/账号)。 -# 同时活跃会话数上限 = 模型数量。 -models: - - id: ds1 - display_name: "AMD DeepSeek 1" - url: "https://proxy-a.example.com/v1" # 独立的 OpenAI 兼容 /v1 地址 - api_key: "sk-..." # 各模型可不同,留空则不带鉴权头 - # vision: true # 选填,默认 false;支持时 read_file 读图会压缩回传(长边≤2000px JPEG),会话上下文会告知 AI - - id: ds2 - display_name: "AMD DeepSeek 2" - url: "https://proxy-b.example.com/v1" - api_key: "sk-..." - - id: ds3 - display_name: "AMD DeepSeek 3" +# 模型池:平台(platforms) → Key → 模型 三层层级。url/type 配在平台,api_key/concurrency 配在 Key, +# 模型只声明 {id, display_name, vision}。加载时自动打平成全局模型列表。 +# 暂按每模型 1 会话调度;concurrency 已解析入库,多会话并发后续再接调度。 +platforms: + - name: proxy-a # 平台名;留空取 url 主机名首段。注意:它是模型唯一键的一部分,改名等于重建其下所有模型(历史会话回绑失效、用量统计分家) + url: "https://proxy-a.example.com/v1" # OpenAI 兼容 /v1 地址,该平台下所有模型共用 + type: openai # openai/deepseek/custom,决定余额查询适配器(预留) + keys: + - api_key: "sk-..." # 各 Key 独立(同平台不同账号/额度),留空则不带鉴权头 + concurrency: 1 # 该 Key 并发会话数,默认 1(当前仅记录,未参与调度) + models: + - id: ds1 # 上游模型名;只需同平台内唯一(不同平台可同名),内部唯一键 = 平台名/模型id,历史会话按它绑定,页面不可改 + display_name: "AMD DeepSeek 1" # 显示名,缺省同 id + # vision: true # 选填,默认 false;支持时 read_file 读图会压缩回传(长边≤2000px JPEG),会话上下文会告知 AI + - id: ds2 + display_name: "AMD DeepSeek 2" + - api_key: "sk-..." # 同平台的第二个 Key + models: + - id: ds3 + display_name: "AMD DeepSeek 3" + - name: proxy-c url: "https://proxy-c.example.com/v1" - api_key: "" + type: custom + keys: + - api_key: "" + models: + - id: ds4 + display_name: "No Auth Model" workspace_root: "./data/tasks" # db_path: "/data/.agent.db" diff --git a/app/server/config.js b/app/server/config.js index 8e1dbf3..4388711 100644 --- a/app/server/config.js +++ b/app/server/config.js @@ -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(); } diff --git a/app/server/llm.js b/app/server/llm.js index 5eed3ac..c343ccb 100644 --- a/app/server/llm.js +++ b/app/server/llm.js @@ -108,11 +108,12 @@ async function callModel(cfg, modelId, messages, tools, options) { if (opts.signal.aborted) controller.abort(); else opts.signal.addEventListener('abort', () => controller.abort()); } + // modelId = 内部唯一键「平台名/模型id」;发给上游的模型名用 mid(原始 id)。 const modelCfg = (cfg.models || []).find(m => m.id === modelId); - if (!modelCfg || !modelCfg.url) throw new Error('模型未在配置中定义或缺少独立 url: ' + modelId); + if (!modelCfg || !modelCfg.url) throw new Error('模型未在配置中定义或缺少 url: ' + modelId); const body = { - model: modelId, + model: modelCfg.mid || modelCfg.id, messages, stream: true, stream_options: { include_usage: true }, diff --git a/app/server/public/index.html b/app/server/public/index.html index bdc160d..d26e6cf 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -243,10 +243,25 @@