Skip to content

fix(custom): 首个模型自动设为默认 LLM,修复加模型后登录反复弹初始化 / auto-promote first model to default LLM - #780

Open
WenhuaXia wants to merge 1 commit into
AgentFlocks:mainfrom
WenhuaXia:fix/auto-promote-first-model-to-default
Open

WenhuaXia wants to merge 1 commit into
AgentFlocks:mainfrom
WenhuaXia:fix/auto-promote-first-model-to-default

Conversation

@WenhuaXia

Copy link
Copy Markdown

问题描述 / Bug Description

从模型管理页添加模型(非 onboarding 向导)后,provider 已配置、凭据已存、模型可用,但 default_models.llm 为空。/api/onboarding/status 要求「默认模型已设置 + 凭据可用」才返回 completed=True,而 WebUI 在 completed=False 时每次登录都强制弹出初始化向导(前端 dismissed flag 被测试用例显式忽略)。

结果:用户明明加了模型并可用,每次登录还是被弹初始化。

After adding a model from the model management page (not the onboarding wizard), the provider is configured and credentials exist, but default_models.llm stays empty. /api/onboarding/status only reports completed=True when a default model is set AND credentials exist, and the WebUI force-opens the setup wizard on every login when completed=False.

根因 / Root Cause

create_model(custom_provider.py)只 upsert 模型条目,从不调 set_default_model。只有 onboarding /apply 路径会设默认模型,而该路径此前被验证 bug 挡住(见 PR #779),用户只能跳过 onboarding 后手动加模型,陷入「有模型、无默认」的中间态。

create_model only upserts the model entry and never calls set_default_model. Only the onboarding /apply path sets a default, and that path was previously blocked by the validation bug in PR #779 — users who skipped onboarding and added a model manually were left in a "model exists but no default" limbo.

修复 / Fix

create_model 新增 _maybe_promote_first_model_to_default,当且仅当三个条件同时满足时自动把新模型设为默认 LLM:

  1. 该 provider 此前没有任何模型(这是第一个模型)
  2. 全局尚无默认 LLM(default_models.llm 为空)
  3. 该 provider 已有可用凭据(secret 或 inline apiKey)

设计取舍:

  • 仅第一个模型才提升 — 加第二/第三个模型时不会覆盖用户已选的默认
  • 有默认就不提升 — 不覆盖 onboarding/其他 provider 设置的默认
  • 有凭据才提升 — 与 /api/onboarding/status 的 completed 判定完全对齐;无凭据时不设,避免出现「设了默认但跑不通、status 仍 False」的矛盾态
  • 提升逻辑 try/except 兜底,绝不阻塞模型创建本身

验证 / Verification

新增 5 个回归测试,覆盖全部条件组合:

场景 预期
首个模型 + secret 凭据 ✅ 自动设默认
首个模型 + inline apiKey(flocks.json) ✅ 自动设默认
首个模型 + 无凭据 ❌ 不设(等补 key)
第二个模型(已有默认) ❌ 不覆盖
其他 provider 已有默认 ❌ 不覆盖

测试运行:

tests/server/routes/test_custom_provider_routes.py  → 8 passed (3 existing + 5 new)
tests/server/routes/test_custom_provider_runtime.py
tests/server/routes/test_provider_optional_api_key.py
tests/server/routes/test_onboarding*.py             → 53 passed, 5 skipped, 0 regression

真实环境验证:用户 openai-compatible provider + Qwen3.8-27B 模型已配置,设默认后 /api/onboarding/status 返回 completed=True,登录不再弹初始化。

测试计划 / Test Plan

  • 5 个新回归测试(条件组合全覆盖)
  • 3 个现有 create_model 测试不受影响(无凭据场景,新逻辑静默跳过)
  • 相邻模块 53 passed / 5 skipped
  • CI

风险评估 / Risk Assessment

Low — 改动仅在 create_model 内,且三重条件门控(首模型 ∧ 无默认 ∧ 有凭据),任何一条不满足都静默跳过。提升失败被 try/except 吞掉并记 warning,不影响 201 返回。不触及 onboarding/credential/默认模型管理端点的既有语义。

关联 / Related

@WenhuaXia
WenhuaXia force-pushed the fix/auto-promote-first-model-to-default branch 2 times, most recently from 08bb40f to 720fe96 Compare October 17, 2026 06:26
…ion tests for first-model auto-default (implementation upstreamed via PR AgentFlocks#54)

上游 origin/main 的 fix(models): set first configured model as default LLM
(PR AgentFlocks#54) 已实现 _maybe_promote_first_model_to_default,且比本地原实现
多解析 legacy model 配置 + webui 创建后刷新默认模型。
本分支保留 8 个回归测试覆盖同一行为契约:
首个模型+凭据→设默认; inline key→设默认; 无凭据→不设;
第二个模型→不覆盖; 其它 provider 已有默认→不覆盖。
Upstream PR AgentFlocks#54 implements the same fix (plus legacy `model`
resolution and a WebUI refresh after create). This branch now
carries only the regression tests pinning the behavior contract.
@WenhuaXia
WenhuaXia force-pushed the fix/auto-promote-first-model-to-default branch from 720fe96 to 456bd84 Compare October 19, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant