Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/pages/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function collectAllModels(config) {
if (id) result.push({ provider: pk, modelId: id, full: `${pk}/${id}` })
}
}
return resul
return result
}

function getApiTypeLabel(apiType) {
Expand Down Expand Up @@ -272,18 +272,10 @@ function bindWaterfallActions(page, state) {
container.querySelectorAll('.btn-set-primary-from-fb').forEach(btn => {
btn.onclick = () => {
const full = btn.dataset.id
const oldPrimary = getCurrentPrimary(state.config)
const fallbacks = state.config.agents.defaults.model.fallbacks || []

pushUndo(state)
// 1. 设置新主模型
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)
renderProviders(page, state)
updateUndoBtn(page, state)
autoSave(state)
toast(t('models.setAsPrimarySuccess', { model: full }), 'success')
Expand Down Expand Up @@ -976,9 +968,39 @@ async function handleAction(action, btn, card, section, providerKey, provider, p
}
}

// 设置主模型(仅修改 state,不写入文件)
// 设置主模型入口
function setPrimary(state, full) {
const oldPrimary = getCurrentPrimary(state.config)
if (oldPrimary === full) return

// 1. 设置新主模型状态
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 + 其余自动成为备选
Expand Down
Loading