模型池槽位按物理key拆分:平台key为其下模型共享资源,绑定时选具体模型(上次平台优先+平台内绑定数轮转)

This commit is contained in:
2026-09-21 11:31:57 +08:00
parent 3e9a87adb5
commit 9820bb86fc
5 changed files with 118 additions and 80 deletions
+30 -27
View File
@@ -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;