diff --git a/app/server/db.js b/app/server/db.js index 05c3536..1b6499b 100644 --- a/app/server/db.js +++ b/app/server/db.js @@ -64,7 +64,8 @@ function initDb(dbPath) { id TEXT PRIMARY KEY, display_name TEXT, is_busy INTEGER DEFAULT 0, - current_session_id TEXT + current_session_id TEXT, + last_used_at INTEGER ); CREATE TABLE IF NOT EXISTS wait_queue ( @@ -123,6 +124,9 @@ function initDb(dbPath) { // 迁移:model_slot 记会话占用的具体池槽位(「模型id#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) + 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) if (!cols.some(c => c.name === 'last_model_id')) { db.exec('ALTER TABLE sessions ADD COLUMN last_model_id TEXT'); diff --git a/app/server/engine.js b/app/server/engine.js index 9e51e71..266005c 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -182,18 +182,19 @@ function syncModelPool() { } function freeModel() { - return db.prepare('SELECT * FROM model_pool WHERE is_busy = 0 ORDER BY id').get(); + // 轮转:最久未用(从未用过排最前)的空闲槽先上,同模型多 key / 跨模型都自然摊平 + 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 时任取其一。 +// 找指定模型(传模型 id 或槽位 id 均可)的空闲槽位:多个空闲 key 时取最久未用的。 // 用范围比较而非 LIKE,避免模型 id 里的 _/% 被当通配符。 function freeSlotFor(modelId) { const base = slotBase(modelId); if (!base) return null; - return db.prepare('SELECT * FROM model_pool WHERE id >= ? AND id < ? AND is_busy = 0 ORDER BY id LIMIT 1') + 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'); } @@ -218,7 +219,7 @@ function dequeue(sessionId) { // 用户发首条消息后由 postUserMessage 的 held 分支转 active 开跑。 function bindModel(session, model, status) { const st = status === 'held' ? 'held' : 'active'; - db.prepare('UPDATE model_pool SET is_busy = 1, current_session_id = ? WHERE id = ?').run(session.id, model.id); + 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 sessions SET status=?, model_id=?, model_slot=?, last_model_id=?, held_at=?, updated_at=? WHERE id=?") diff --git a/app/server/llm.js b/app/server/llm.js index 6219f88..4725f19 100644 --- a/app/server/llm.js +++ b/app/server/llm.js @@ -274,8 +274,12 @@ async function forEachSSEData(response, onEvent, onMeta, onBadData) { function deepMergeInto(dst, src) { for (const k of Object.keys(src || {})) { const sv = src[k]; - if (sv && typeof sv === 'object' && !Array.isArray(sv) && dst[k] && typeof dst[k] === 'object' && !Array.isArray(dst[k])) { + if (sv && typeof sv === 'object' && !Array.isArray(sv) + && dst[k] && typeof dst[k] === 'object' && !Array.isArray(dst[k])) { deepMergeInto(dst[k], sv); + } else if (sv && typeof sv === 'object') { + // 切断与配置对象的引用关系 + dst[k] = Array.isArray(sv) ? sv.slice() : structuredClone(sv); } else { dst[k] = sv; }