Skip to content

fix(provider): persist model enable toggle#7865

Open
ACAne0320 wants to merge 2 commits intoAstrBotDevs:masterfrom
ACAne0320:fix/7863-provider-model-toggle
Open

fix(provider): persist model enable toggle#7865
ACAne0320 wants to merge 2 commits intoAstrBotDevs:masterfrom
ACAne0320:fix/7863-provider-model-toggle

Conversation

@ACAne0320
Copy link
Copy Markdown

@ACAne0320 ACAne0320 commented Apr 28, 2026

Fixes #7863

This PR fixes a WebUI provider model toggle issue.

When a user fetches models from an OpenAI Compatible provider source, adds a model, enables it, and immediately clicks the test button, the UI previously showed the model as enabled but did not persist the enable state or reload the provider before calling /config/provider/check_one.

As a result, the backend could not find the provider in provider_manager.inst_map and returned:

Provider with id '...' not found in provider_manager.

Modifications / 改动点

Persist model enable/disable changes through /api/config/provider/update when toggling a configured provider model.

Refresh provider config after the toggle update so the WebUI stays in sync with backend state.

Avoid local-only optimistic switching by replacing the model switch v-model with :model-value.

Disable the model switch and test button while the enable/disable update is being saved, preventing users from testing before the provider reload completes.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Manual verification path:

  1. Configure an OpenAI Compatible provider source with a valid API Key and API Base URL.
  2. Fetch models successfully.
  3. Add a fetched model to the configured model list.
  4. Enable the model from the switch.
  5. Without opening the model config dialog or clicking save there, click the test button.
  6. The provider is now persisted/reloaded before testing, so /config/provider/check_one no longer fails with not found in provider_manager.

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Ensure provider model enable toggles are persisted and synchronized with backend state before testing.

Bug Fixes:

  • Persist provider model enable/disable state via the provider update API instead of relying on local optimistic updates.
  • Reload provider configuration after toggling so the WebUI and backend remain consistent, preventing missing provider errors during tests.
  • Disable the model enable switch and test button while a toggle is being saved to avoid testing before the provider reload completes.

Enhancements:

  • Track per-provider toggle save state in the provider sources composable and expose it to components so they can reflect saving status in the UI.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. area:webui The bug / feature is about webui(dashboard) of astrbot. labels Apr 28, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Calling loadConfig() in the finally of toggleProviderEnable for every toggle may introduce noticeable latency and extra load; consider narrowing the refresh scope (e.g., updating only the changed provider in local state or batching/debouncing full reloads) instead of reloading the entire config each time.
  • The savingProviderToggles guard prevents double-submits per provider, but concurrent toggles of different providers might still interleave with a shared loadConfig() result; if that’s problematic, you may want to serialize config reloads or ensure newer responses cannot overwrite more recent local changes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Calling `loadConfig()` in the `finally` of `toggleProviderEnable` for every toggle may introduce noticeable latency and extra load; consider narrowing the refresh scope (e.g., updating only the changed provider in local state or batching/debouncing full reloads) instead of reloading the entire config each time.
- The `savingProviderToggles` guard prevents double-submits per provider, but concurrent toggles of different providers might still interleave with a shared `loadConfig()` result; if that’s problematic, you may want to serialize config reloads or ensure newer responses cannot overwrite more recent local changes.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new toggle functionality for providers, allowing users to enable or disable them via the UI with appropriate loading states. I have identified a critical race condition in the toggleProviderEnable function where the loading state is cleared before the configuration reload completes, and a secondary issue where loadConfig fails to properly await the underlying template loading. I have provided a suggestion to reorder these operations to ensure the UI remains disabled until the backend state is fully synchronized.

Comment on lines +636 to +637
savingProviderToggles.value = savingProviderToggles.value.filter((id) => id !== provider.id)
await loadConfig()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are two issues here that prevent the UI from correctly waiting for the backend to reload the provider, which is the stated goal of this PR:

  1. Race Condition: The savingProviderToggles state is cleared before the configuration is reloaded. This re-enables the UI components (like the test button) before the backend has finished processing the update and the frontend has received the fresh state.
  2. Broken Await: The loadConfig function (defined at line 659) is async but does not await the call to loadProviderTemplate(). Consequently, await loadConfig() returns immediately without waiting for the network request to complete.

To fix this, you should swap the order here and ensure the definition of loadConfig at line 659 is updated to await loadProviderTemplate().

Suggested change
savingProviderToggles.value = savingProviderToggles.value.filter((id) => id !== provider.id)
await loadConfig()
await loadConfig()
savingProviderToggles.value = savingProviderToggles.value.filter((id) => id !== provider.id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. area:webui The bug / feature is about webui(dashboard) of astrbot. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 获取模型列表后添加模型测试连接失败

1 participant