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.
This commit is contained in:
friendfish
2026-04-26 21:03:54 +08:00
committed by GitHub
parent f6470d9e6a
commit 2ad5e2d5ce

View File

@@ -299,18 +299,10 @@ function bindWaterfallActions(page, state) {
container.querySelectorAll('.btn-set-primary-from-fb').forEach(btn => { container.querySelectorAll('.btn-set-primary-from-fb').forEach(btn => {
btn.onclick = () => { btn.onclick = () => {
const full = btn.dataset.id const full = btn.dataset.id
const oldPrimary = getCurrentPrimary(state.config)
const fallbacks = state.config.agents.defaults.model.fallbacks || []
pushUndo(state) pushUndo(state)
// 1. 设置新主模型
setPrimary(state, full) setPrimary(state, full)
// 2. 将旧主模型放入备选链(原位置或末尾)
const newFallbacks = fallbacks.filter(f => f !== full)
if (oldPrimary) newFallbacks.push(oldPrimary)
state.config.agents.defaults.model.fallbacks = newFallbacks
renderDefaultBar(page, state) renderDefaultBar(page, state)
renderProviders(page, state)
updateUndoBtn(page, state) updateUndoBtn(page, state)
autoSave(state) autoSave(state)
toast(t('models.setAsPrimarySuccess', { model: full }), 'success') toast(t('models.setAsPrimarySuccess', { model: full }), 'success')
@@ -1003,9 +995,39 @@ async function handleAction(action, btn, card, section, providerKey, provider, p
} }
} }
// 设置主模型(仅修改 state,不写入文件) // 设置主模型入口
function setPrimary(state, full) { function setPrimary(state, full) {
const oldPrimary = getCurrentPrimary(state.config)
if (oldPrimary === full) return
// 1. 设置新主模型状态
ensureDefaultModelConfig(state).primary = full ensureDefaultModelConfig(state).primary = full
// 2. 轮转备选链状态
rotateFallbackChain(state, oldPrimary, full)
}
// 处理主模型变更后,备选链的数据流转
function rotateFallbackChain(state, oldPrimary, newPrimary) {
const modelConfig = ensureDefaultModelConfig(state)
const validModels = new Set(collectAllModels(state.config).map(m => m.full))
const seen = new Set()
// 从备选链中移除新上位的主模型
const newFallbacks = (modelConfig.fallbacks || [])
.filter(f => f !== newPrimary && validModels.has(f))
.filter(f => {
if (seen.has(f)) return false
seen.add(f)
return true
})
// 将原主模型降级放入备选链
if (oldPrimary && oldPrimary !== newPrimary && validModels.has(oldPrimary) && !seen.has(oldPrimary)) {
newFallbacks.push(oldPrimary)
}
modelConfig.fallbacks = newFallbacks
} }
// 应用默认模型:primary + 其余自动成为备选 // 应用默认模型:primary + 其余自动成为备选