From 9820bb86fc7bff007b422d55d5de922967e2e9ff Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 21 Sep 2026 11:31:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=B1=A0=E6=A7=BD=E4=BD=8D?= =?UTF-8?q?=E6=8C=89=E7=89=A9=E7=90=86key=E6=8B=86=E5=88=86=EF=BC=9A?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0key=E4=B8=BA=E5=85=B6=E4=B8=8B=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=85=B1=E4=BA=AB=E8=B5=84=E6=BA=90=EF=BC=8C=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E9=80=89=E5=85=B7=E4=BD=93=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=EF=BC=88=E4=B8=8A=E6=AC=A1=E5=B9=B3=E5=8F=B0=E4=BC=98=E5=85=88?= =?UTF-8?q?+=E5=B9=B3=E5=8F=B0=E5=86=85=E7=BB=91=E5=AE=9A=E6=95=B0?= =?UTF-8?q?=E8=BD=AE=E8=BD=AC=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config.example.yml | 2 +- app/server/db.js | 4 +- app/server/engine.js | 128 ++++++++++++++++++++++++++--------------- app/server/llm.js | 7 ++- app/server/server.js | 57 +++++++++--------- 5 files changed, 118 insertions(+), 80 deletions(-) diff --git a/app/config.example.yml b/app/config.example.yml index 87d5998..6245549 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -4,7 +4,7 @@ # 模型池:平台(platforms) → models + keys 两层结构。url/type/models 配在平台层(同一份 models # 被该平台下所有 key 共用),api_key/concurrency 配在 keys 里(每个 key 独立账号/额度)。 # 加载时自动打平:每个 key 复制一份该平台下所有 models,生成全局 cfg.models 列表。 -# 暂按每模型 1 会话调度;concurrency 已解析入库,多会话并发后续再接调度。 +# 暂按每 key 1 会话调度(同平台模型共享该批 key,平台 key 全满则其下模型均不可选);concurrency 已解析入库,后续再接调度。 platforms: - name: proxy-a # 平台名;留空取 url 主机名首段。注意:它是模型唯一键的一部分,改名等于重建其下所有模型(历史会话回绑失效、用量统计分家) url: "https://proxy-a.example.com/v1" # OpenAI 兼容 /v1 地址,该平台下所有模型共用 diff --git a/app/server/db.js b/app/server/db.js index 1b6499b..1486d58 100644 --- a/app/server/db.js +++ b/app/server/db.js @@ -121,10 +121,10 @@ function initDb(dbPath) { // 迁移:旧库补 held_at 列(held 保持租约起始时间) const cols = db.prepare('PRAGMA table_info(sessions)').all(); if (!cols.some(c => c.name === 'held_at')) db.exec('ALTER TABLE sessions ADD COLUMN held_at INTEGER'); - // 迁移:model_slot 记会话占用的具体池槽位(「模型id#key下标」,同模型多 key 并行占用), + // 迁移:model_slot 记会话占用的具体池 key 槽位(「平台#key下标」,平台 key 为其下模型共享资源), // model_id 仍存模型 id 供展示/统计(见 engine.bindModel)。 if (!cols.some(c => c.name === 'model_slot')) db.exec('ALTER TABLE sessions ADD COLUMN model_slot TEXT'); - // 迁移:model_pool.last_used_at 记槽位最近被绑定时刻,调度按「最久未用先上」轮转(见 engine.freeModel) + // 迁移:model_pool 一行一个物理 key 槽位(id=「平台#下标」),last_used_at 支撑「最久未用先上」轮转(见 engine.freeModel) const poolCols = db.prepare('PRAGMA table_info(model_pool)').all(); if (!poolCols.some(c => c.name === 'last_used_at')) db.exec('ALTER TABLE model_pool ADD COLUMN last_used_at INTEGER'); // 迁移:last_model_id 记住会话最近用过的模型,释放后新消息优先回绑(见 engine.dispatchQueued) diff --git a/app/server/engine.js b/app/server/engine.js index 266005c..e79d88d 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -157,16 +157,15 @@ function syncModelPool() { const stmt = db.prepare( 'INSERT INTO model_pool (id, display_name, is_busy, current_session_id) VALUES (?,?,0,NULL) ON CONFLICT(id) DO UPDATE SET display_name=excluded.display_name' ); - // 池槽位按 key 拆分:cfg.models 里同模型每个 key 一条副本,槽位 id = 「模型id#key下标」, - // 一个空闲 key 一个槽位 → 同模型可被多会话并行占用(见 slotBase/freeSlotFor)。 - // enable:false 的模型不进池:分配/绑定均按池取模型,天然跳过禁用项(与其他模型配置一致,重启生效)。 - const slotId = m => m.id + '#' + m.key_index; - const enabled = cfg.models.filter(m => m.enable !== false); - for (const m of enabled) stmt.run(slotId(m), m.display_name); - // 清理配置中已删除/已禁用的模型(含旧版按模型 id 建的行,重启后自动重建为槽位行) - const ids = enabled.map(slotId); + // 池槽位按物理 key 拆分:槽位 id = 「平台#key下标」,一个 key 同时只服务一个会话(concurrency 仍仅记录未调度)。 + // 同平台的模型共享这批 key:平台 key 全部被占时,其下所有模型都不可再选。 + // enable:false 的模型不进池:平台下模型全禁用则该平台不建槽位(与其他模型配置一致,重启生效)。 + const seen = new Set(); + for (const m of cfg.models) if (m.enable !== false) seen.add(m.platform + '#' + m.key_index); + for (const slot of seen) stmt.run(slot, slot.split('#')[0]); + // 清理配置中已删除/已禁用的平台 key(含旧版按模型拆的槽位行,重启后自动重建) for (const row of db.prepare('SELECT id FROM model_pool').all()) { - if (!ids.includes(row.id)) db.prepare('DELETE FROM model_pool WHERE id = ?').run(row.id); + if (!seen.has(row.id)) db.prepare('DELETE FROM model_pool WHERE id = ?').run(row.id); } // 进程重启后:释放所有绑定、把 active 会话退回队列;held 会话直接结版(释放)。 // 禁用模型同样走这里收尾:重启后池里已无它,绑过它的会话被退回队列/结版,后续重新分配启用中的模型。 @@ -182,20 +181,45 @@ function syncModelPool() { } function freeModel() { - // 轮转:最久未用(从未用过排最前)的空闲槽先上,同模型多 key / 跨模型都自然摊平 + // 轮转:最久未用(从未用过排最前)的空闲 key 先上 → 全平台 key 自然摊平, + // 平台 key 灌满后自动轮到下一个平台(见 pickModelFor)。 return db.prepare('SELECT * FROM model_pool WHERE is_busy = 0 ORDER BY ifnull(last_used_at, 0), id').get(); } -// 槽位 id(「模型id#key下标」)→ 模型 id(「平台/模型」)。对外一律用模型 id,槽位只在池内/取 key 时用。 -function slotBase(id) { return String(id || '').split('#')[0]; } +// 模型 id「平台/模型」或池槽位 id「平台#key下标」→ 平台名。 +function platformOf(id) { + const s = String(id || ''); + const h = s.indexOf('#'); + if (h >= 0) return s.slice(0, h); + const sl = s.indexOf('/'); + return sl >= 0 ? s.slice(0, sl) : s; +} -// 找指定模型(传模型 id 或槽位 id 均可)的空闲槽位:多个空闲 key 时取最久未用的。 -// 用范围比较而非 LIKE,避免模型 id 里的 _/% 被当通配符。 -function freeSlotFor(modelId) { - const base = slotBase(modelId); - if (!base) return null; +// 指定平台的空闲 key 槽位:最久未用的先上。 +// 用范围比较而非 LIKE,避免平台名里的 _/% 被当通配符。 +function freeKeyOf(platform) { + if (!platform) return null; return db.prepare('SELECT * FROM model_pool WHERE id >= ? AND id < ? AND is_busy = 0 ORDER BY ifnull(last_used_at, 0), id LIMIT 1') - .get(base + '#', base + '#\uffff'); + .get(platform + '#', platform + '#\uffff'); +} + +// 平台下启用中的模型 id 列表(cfg.models 按 key 展开过,这里去重并保留配置顺序)。 +function platformModels(platform) { + const seen = new Set(); + const out = []; + for (const m of cfg.models) if (m.enable !== false && m.platform === platform && !seen.has(m.id)) { seen.add(m.id); out.push(m.id); } + return out; +} + +// 在 key 所属平台的模型里选一个:preferred(会话上次用过的同平台模型)优先; +// 否则取平台内当前绑定会话数最少的(打平按配置顺序),同平台多模型也轮转摊平。 +function chooseModel(platform, preferred) { + const ms = platformModels(platform); + if (!ms.length) return null; + if (preferred && ms.includes(preferred)) return preferred; + const cnt = {}; + for (const r of db.prepare('SELECT model_id, COUNT(*) AS c FROM sessions WHERE model_id IS NOT NULL GROUP BY model_id').all()) cnt[r.model_id] = r.c; + return ms.reduce((a, b) => ((cnt[b] || 0) < (cnt[a] || 0) ? b : a), ms[0]); } // 会话绑定的模型是否支持 vision(图片回传);决定 read_file 读图行为(见 tools/executor.js)。 @@ -213,17 +237,18 @@ function dequeue(sessionId) { db.prepare('DELETE FROM wait_queue WHERE session_id = ?').run(sessionId); } +// key:占用的池 key 槽位行(id=「平台#ki」);modelId:选定的模型 id(同平台模型共享 key,绑定时才决定用哪个)。 // status:绑定后的会话状态。 // - 'active'(默认):绑定即开跑(有待回应消息,紧随其后 runLoop);排队派发 dispatchQueued 用。 // - 'held':仅预留模型占位(手动选模/新建选模,尚无待回应消息), // 用户发首条消息后由 postUserMessage 的 held 分支转 active 开跑。 -function bindModel(session, model, status) { +function bindModel(session, key, modelId, status) { const st = status === 'held' ? 'held' : 'active'; - db.prepare('UPDATE model_pool SET is_busy = 1, current_session_id = ?, last_used_at = ? WHERE id = ?').run(session.id, now(), model.id); - // model_id/last_model_id 存模型 id(展示/统计/回绑语义不变),model_slot 记绑定的具体 key 槽位; - // last_model_id 不随释放清除:会话再次需要模型时优先回绑同模型的空闲 key(见 pickModelFor) + db.prepare('UPDATE model_pool SET is_busy = 1, current_session_id = ?, last_used_at = ? WHERE id = ?').run(session.id, now(), key.id); + // model_id/last_model_id 存模型 id(展示/统计用),model_slot 记占用的具体 key 槽位; + // last_model_id 不随释放清除:会话再次需要模型时优先回绑同平台(见 pickModelFor) db.prepare("UPDATE sessions SET status=?, model_id=?, model_slot=?, last_model_id=?, held_at=?, updated_at=? WHERE id=?") - .run(st, slotBase(model.id), model.id, slotBase(model.id), st === 'held' ? now() : null, now(), session.id); + .run(st, modelId, key.id, modelId, st === 'held' ? now() : null, now(), session.id); } function releaseModel(sessionId) { @@ -241,16 +266,18 @@ function bindToSession(sessionId, modelId) { if (s.status !== 'completed' && s.status !== 'cancelled') { throw Object.assign(new Error('仅已完成/已终止的会话可手动选择模型'), { status_code: 409 }); } - // 传模型 id:绑到该模型任意一个空闲 key 槽位;全部槽位被占才算「正被占用」 - const model = freeSlotFor(String(modelId || '')); - if (!model) throw Object.assign(new Error('模型不存在或正被占用'), { status_code: 409 }); + // 手动选定模型:占用其平台任意一个空闲 key(平台 key 全满才算被占);模型需在启用列表中。 + const want = String(modelId || ''); + const plat = platformOf(want); + const key = freeKeyOf(plat); + if (!key || !platformModels(plat).includes(want)) throw Object.assign(new Error('模型不存在或正被占用'), { status_code: 409 }); touch(sessionId, { cancelled_at: null, completed_at: null, held_at: null }); const pending = hasPendingUser(sessionId); - bindModel(s, model, pending ? 'active' : 'held'); + bindModel(s, key, want, pending ? 'active' : 'held'); emitStatus(sessionId); - log.info('model', sessionId + ' 手动绑定模型 ' + model.id); + log.info('model', sessionId + ' 手动绑定模型 ' + want + '(key ' + key.id + ')'); if (pending) runLoop(sessionId); - return { ok: true, model_id: slotBase(model.id) }; + return { ok: true, model_id: want }; } // 是否还有待回应的新用户消息(最后一条非 tool 消息是 user)。 @@ -263,17 +290,19 @@ function hasPendingUser(sessionId) { return false; } -// 为排队会话挑模型:上次用过的模型(last_model_id)恰好空闲则优先回绑; -// 被占用(或从未绑过/已从配置删除)则按 id 序取任意空闲模型。 +// 为排队会话挑 key+模型:上次用过的模型(last_model_id)所在平台有空闲 key 则优先回该平台; +// 否则按「最久未用」取全局任意空闲 key。具体模型由 chooseModel 在 key 平台上定(同平台偏好上次模型)。 function pickModelFor(session) { - if (session.last_model_id) { - const pref = freeSlotFor(session.last_model_id); - if (pref) return pref; - } - return freeModel(); + const want = session.last_model_id || ''; + let key = want ? freeKeyOf(platformOf(want)) : null; + if (!key) key = freeModel(); + if (!key) return null; + const plat = platformOf(key.id); + const model = chooseModel(plat, platformOf(want) === plat ? want : ''); + return model ? { key, model } : null; } -// 有空闲模型就按 FIFO 派发排队会话(每会话优先回绑上次用过的模型)。 +// 有空闲 key 就按 FIFO 派发排队会话(每会话优先回绑上次用过的模型所在平台)。 function dispatchQueued() { for (;;) { if (!freeModel()) return; @@ -282,13 +311,15 @@ function dispatchQueued() { dequeue(head.session_id); const session = getSession(head.session_id); if (!session || session.status !== 'queued') continue; - // 上面已确认有空闲模型,pickModelFor 必能回退到 freeModel; + // 上面已确认有空闲 key;若目标平台无启用模型/无空闲 key 致分配失败(理论上不会),退回队尾等下次派发。 + const got = pickModelFor(session); + if (!got) { enqueue(session.id); continue; } // 无待回应消息的会话(如新建后从未发言)只预留不占跑,绑成 held 等首条消息。 // 待开跑的压缩回合(内存 run.summarizing,见 summarizeContext)也算待处理: // 它没有对应的用户消息行,hasPendingUser 判不到,需单独纳入。 const run0 = runs.get(session.id); const pending = hasPendingUser(session.id) || !!(run0 && run0.summarizing); - bindModel(session, pickModelFor(session), pending ? 'active' : 'held'); + bindModel(session, got.key, got.model, pending ? 'active' : 'held'); emitStatus(session.id); if (pending) runLoop(session.id); } @@ -438,11 +469,11 @@ function finalize(sessionId, run) { function maybeGenerateTitle(sessionId, s) { try { if (s.title) return; - const modelId = s.model_slot || s.model_id; // 用绑定槽位精确到 key + const modelId = s.model_id; if (!modelId || titling.has(sessionId)) return; titling.add(sessionId); const messages = titleMessages(loadHistory(sessionId), cfg.agent.language); - callModel(cfg, modelId, messages, null, {}) + callModel(cfg, modelId, messages, null, { key_slot: s.model_slot }) .then(r => { const title = cleanTitle(r && r.content); if (!title) return; @@ -690,8 +721,9 @@ async function streamAssistant(sessionId, run, session, messages, tools) { const tcDbg = { starts: 0, frags: 0, chars: 0 }; try { const handleSseMeta = makeSseMetaHandler(sessionId); - // 用绑定槽位(含 key 下标)精确取到会话占用的那个 key 的配置 - const result = await callModel(cfg, session.model_slot || session.model_id, messages, tools, { + // key_slot 定位会话占用的具体 key 副本(model_id 只到「平台/模型」) + const result = await callModel(cfg, session.model_id, messages, tools, { + key_slot: session.model_slot, signal: run.abortController.signal, shouldCancel: () => run.cancelled, onText: delta => bus.emit(sessionId, 'assistant_delta', { text: delta }), @@ -811,12 +843,14 @@ function createSession(title, preferModelId, assistantType, user) { db.prepare( "INSERT INTO sessions (id, title, status, phase, model_id, assistant_type, user, created_at, updated_at) VALUES (?,?,'queued','planning',NULL,?,?,?,?)" ).run(id, title || '', assistantType || 'secretary', user || '', now(), now()); - // 指定了模型且它恰好空闲 → 直接绑定,跳过排队;否则回退到 FIFO 队列 - const prefer = preferModelId ? freeSlotFor(preferModelId) : null; + // 指定了模型且其平台恰好有空闲 key → 直接绑定,跳过排队;否则回退到 FIFO 队列 + const prefer = preferModelId && platformModels(platformOf(preferModelId)).includes(preferModelId) + ? freeKeyOf(platformOf(preferModelId)) + : null; if (prefer) { - // 新建时指定模型 = 仅预留(此时还没有任何消息):先绑成 held 占住模型, + // 新建时指定模型 = 仅预留(此时还没有任何消息):先绑成 held 占住 key, // 用户发首条消息后再转 active 开跑 - bindModel(getSession(id), prefer, 'held'); + bindModel(getSession(id), prefer, preferModelId, 'held'); emitStatus(id); } else { enqueue(id); diff --git a/app/server/llm.js b/app/server/llm.js index 4725f19..8f16089 100644 --- a/app/server/llm.js +++ b/app/server/llm.js @@ -344,10 +344,11 @@ async function callModel(cfg, modelId, messages, tools, options) { if (opts.signal.aborted) controller.abort(); else opts.signal.addEventListener('abort', () => controller.abort()); } - // modelId = 池槽位 id「平台名/模型id#key下标」(精确到会话占用的 key);兼容传纯模型 id(取第一个 key 副本)。 - // 发给上游的模型名用 mid(原始 id)。 + // modelId = 「平台名/模型id」;opts.key_slot = 池槽位「平台#key下标」(会话占用的 key),两者合定位具体 key 副本; + // 未传/未命中时退回按模型 id 取第一个副本(标题等旁路调用)。发给上游的模型名用 mid(原始 id)。 const all = cfg.models || []; - const modelCfg = all.find(m => m.id + '#' + m.key_index === modelId) || all.find(m => m.id === modelId); + const modelCfg = (opts.key_slot ? all.find(m => m.id === modelId && m.platform + '#' + m.key_index === opts.key_slot) : null) + || all.find(m => m.id === modelId); if (!modelCfg || !modelCfg.url) throw new Error('模型未在配置中定义或缺少 url: ' + modelId); const body = { diff --git a/app/server/server.js b/app/server/server.js index ff84d25..777da59 100644 --- a/app/server/server.js +++ b/app/server/server.js @@ -57,36 +57,39 @@ app.use('/api/system', require('./routes/system')); // model_pool 表只有 id/display_name/占用态,会话页「选择模型」弹窗需要按平台分组展示。 // 配置里新增但重启前未进池的模型不在此返回(池才是运行时真相)。 app.get('/api/models', (req, res) => { - // 池行是按 key 拆分的槽位(id=「模型id#下标」):按模型聚合为一行对外展示, - // is_busy = 全部槽位被占;附 slots_total/slots_busy 供前端显示剩余空闲 key。 - const raw = db.prepare('SELECT id, display_name, is_busy, current_session_id FROM model_pool ORDER BY id').all(); - const byBase = new Map(); - for (const r of raw) { - const base = r.id.split('#')[0]; - let g = byBase.get(base); - if (!g) { g = { id: base, display_name: r.display_name, is_busy: 0, slots_total: 0, slots_busy: 0, sessions: [] }; byBase.set(base, g); } - g.slots_total++; - if (r.is_busy) { g.slots_busy++; if (r.current_session_id) g.sessions.push(r.current_session_id); } - } - const rows = [...byBase.values()].map(g => Object.assign(g, { is_busy: g.slots_busy >= g.slots_total ? 1 : 0, current_session_id: g.sessions.join(',') })); - const stats = engine.modelUsageStats(); - // 配置视图:模型唯一键 → 平台/原始 id/vision;平台名 → key 数(cfg.models 含 key 副本,不能直接拿来用) - const cfgNow = engine.getConfig(); - const infoById = {}; - const keyCountByPlat = {}; - for (const p of cfgNow.platforms || []) { - keyCountByPlat[p.name] = (p.keys || []).length; - for (const m of p.models || []) { - infoById[p.name + '/' + m.id] = { platform: p.name, mid: m.id, vision: !!m.vision, type: p.type }; + // 池行是平台物理 key(id=「平台#下标」):key 为平台下模型共享资源, + // 按模型聚合返回:slots_total/slots_busy = 平台 key 总数/被占数,is_busy = 平台 key 全满(其下所有模型都不可选)。 + const keyRows = db.prepare('SELECT id, is_busy, current_session_id FROM model_pool').all(); + const totalByPlat = {}; + const busyByPlat = {}; + const sessByPlat = {}; + for (const r of keyRows) { + const plat = r.id.split('#')[0]; + totalByPlat[plat] = (totalByPlat[plat] || 0) + 1; + if (r.is_busy) { + busyByPlat[plat] = (busyByPlat[plat] || 0) + 1; + if (r.current_session_id) (sessByPlat[plat] = sessByPlat[plat] || []).push(r.current_session_id); } } + // 模型行取自生效配置(cfg 重启生效,与池同步):按模型 id 去重、保留配置顺序,禁用模型不返回。 + const cfgNow = engine.getConfig(); + const seen = new Set(); + const rows = []; + for (const m of cfgNow.models || []) { + if (m.enable === false || seen.has(m.id)) continue; + seen.add(m.id); + const total = totalByPlat[m.platform] || 0; + const busy = busyByPlat[m.platform] || 0; + rows.push({ + id: m.id, display_name: m.display_name, platform: m.platform, mid: m.mid, + vision: !!m.vision, type: m.type, key_count: total, + slots_total: total, slots_busy: busy, + is_busy: total > 0 && busy >= total ? 1 : 0, + current_session_id: (sessByPlat[m.platform] || []).join(','), + }); + } + const stats = engine.modelUsageStats(); for (const r of rows) { - const info = infoById[r.id] || {}; - r.platform = info.platform || ''; - r.mid = info.mid || ''; - r.vision = info.vision || false; - r.type = info.type || ''; - r.key_count = keyCountByPlat[r.platform] || 0; const s = stats[r.id] || {}; r.today_prompt = s.today_prompt || 0; r.today_completion = s.today_completion || 0;