Skip to content

Commit 2ad5e2d

Browse files
authored
refactor(models): unify primary model rotation and sync
Refactor the model management page so every primary-model entry point goes through setPrimary(). This centralizes fallback-chain rotation, removes stale/deleted models from fallbacks, deduplicates the chain, and refreshes both the default bar and provider cards after primary changes.
1 parent f6470d9 commit 2ad5e2d

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

src/pages/models.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,10 @@ function bindWaterfallActions(page, state) {
299299
container.querySelectorAll('.btn-set-primary-from-fb').forEach(btn => {
300300
btn.onclick = () => {
301301
const full = btn.dataset.id
302-
const oldPrimary = getCurrentPrimary(state.config)
303-
const fallbacks = state.config.agents.defaults.model.fallbacks || []
304-
305302
pushUndo(state)
306-
// 1. 设置新主模型
307303
setPrimary(state, full)
308-
// 2. 将旧主模型放入备选链(原位置或末尾)
309-
const newFallbacks = fallbacks.filter(f => f !== full)
310-
if (oldPrimary) newFallbacks.push(oldPrimary)
311-
state.config.agents.defaults.model.fallbacks = newFallbacks
312-
313304
renderDefaultBar(page, state)
305+
renderProviders(page, state)
314306
updateUndoBtn(page, state)
315307
autoSave(state)
316308
toast(t('models.setAsPrimarySuccess', { model: full }), 'success')
@@ -1003,9 +995,39 @@ async function handleAction(action, btn, card, section, providerKey, provider, p
1003995
}
1004996
}
1005997

1006-
// 设置主模型(仅修改 state,不写入文件)
998+
// 设置主模型入口
1007999
function setPrimary(state, full) {
1000+
const oldPrimary = getCurrentPrimary(state.config)
1001+
if (oldPrimary === full) return
1002+
1003+
// 1. 设置新主模型状态
10081004
ensureDefaultModelConfig(state).primary = full
1005+
1006+
// 2. 轮转备选链状态
1007+
rotateFallbackChain(state, oldPrimary, full)
1008+
}
1009+
1010+
// 处理主模型变更后,备选链的数据流转
1011+
function rotateFallbackChain(state, oldPrimary, newPrimary) {
1012+
const modelConfig = ensureDefaultModelConfig(state)
1013+
const validModels = new Set(collectAllModels(state.config).map(m => m.full))
1014+
const seen = new Set()
1015+
1016+
// 从备选链中移除新上位的主模型
1017+
const newFallbacks = (modelConfig.fallbacks || [])
1018+
.filter(f => f !== newPrimary && validModels.has(f))
1019+
.filter(f => {
1020+
if (seen.has(f)) return false
1021+
seen.add(f)
1022+
return true
1023+
})
1024+
1025+
// 将原主模型降级放入备选链
1026+
if (oldPrimary && oldPrimary !== newPrimary && validModels.has(oldPrimary) && !seen.has(oldPrimary)) {
1027+
newFallbacks.push(oldPrimary)
1028+
}
1029+
1030+
modelConfig.fallbacks = newFallbacks
10091031
}
10101032

10111033
// 应用默认模型:primary + 其余自动成为备选

0 commit comments

Comments
 (0)