fix(provider): persist model enable toggle (#7865)

* fix(provider): persist model enable toggle

Fixes AstrBotDevs/AstrBot#7863

* fix(provider): wait for model toggle refresh
This commit is contained in:
诗浓
2026-04-28 23:55:46 +08:00
committed by GitHub
parent 962c299c2d
commit 98b05b7e89
3 changed files with 41 additions and 3 deletions

View File

@@ -89,6 +89,7 @@
:supports-tool-call="supportsToolCall"
:supports-reasoning="supportsReasoning"
:format-context-limit="formatContextLimit"
:saving-providers="savingProviderToggles"
:testing-providers="testingProviders"
:tm="tm"
@fetch-models="fetchAvailableModels"
@@ -209,6 +210,7 @@ const {
availableModels,
loadingModels,
savingSource,
savingProviderToggles,
testingProviders,
isSourceModified,
configSchema,

View File

@@ -84,12 +84,13 @@
<div class="provider-model-row__actions" @click.stop>
<v-switch
v-model="entry.provider.enable"
:model-value="entry.provider.enable"
density="compact"
inset
hide-details
color="primary"
class="provider-model-row__switch"
:disabled="isProviderSaving(entry.provider.id)"
@update:modelValue="emit('toggle-provider-enable', entry.provider, $event)"
></v-switch>
@@ -97,7 +98,7 @@
icon="mdi-connection"
size="small"
variant="text"
:disabled="!entry.provider.enable"
:disabled="!entry.provider.enable || isProviderSaving(entry.provider.id)"
:loading="isProviderTesting(entry.provider.id)"
@click.stop="emit('test-provider', entry.provider)"
></v-btn>
@@ -240,6 +241,10 @@ const props = defineProps({
type: Array,
default: () => []
},
savingProviders: {
type: Array,
default: () => []
},
tm: {
type: Function,
required: true
@@ -288,6 +293,7 @@ const capabilityIcons = (metadata) => {
}
const isProviderTesting = (providerId) => props.testingProviders.includes(providerId)
const isProviderSaving = (providerId) => props.savingProviders.includes(providerId)
</script>
<style scoped>

View File

@@ -58,6 +58,7 @@ export function useProviderSources(options: UseProviderSourcesOptions) {
const modelMetadata = ref<Record<string, any>>({})
const loadingModels = ref(false)
const savingSource = ref(false)
const savingProviderToggles = ref<string[]>([])
const testingProviders = ref<string[]>([])
const isSourceModified = ref(false)
const configSchema = ref<Record<string, any>>({})
@@ -610,6 +611,33 @@ export function useProviderSources(options: UseProviderSourcesOptions) {
}
}
async function toggleProviderEnable(provider: any, value: boolean) {
if (!provider?.id || savingProviderToggles.value.includes(provider.id)) {
return false
}
savingProviderToggles.value.push(provider.id)
try {
const nextConfig = { ...provider, enable: Boolean(value) }
const response = await axios.post('/api/config/provider/update', {
id: provider.id,
config: nextConfig
})
if (response.data.status === 'error') {
throw new Error(response.data.message)
}
provider.enable = nextConfig.enable
showMessage(response.data.message || tm('messages.success.statusUpdate'))
return true
} catch (error: any) {
showMessage(error.response?.data?.message || error.message || tm('providerSources.saveError'), 'error')
return false
} finally {
await loadConfig()
savingProviderToggles.value = savingProviderToggles.value.filter((id) => id !== provider.id)
}
}
async function testProvider(provider: any) {
testingProviders.value.push(provider.id)
try {
@@ -629,7 +657,7 @@ export function useProviderSources(options: UseProviderSourcesOptions) {
}
async function loadConfig() {
loadProviderTemplate()
await loadProviderTemplate()
}
async function loadProviderTemplate() {
@@ -670,6 +698,7 @@ export function useProviderSources(options: UseProviderSourcesOptions) {
modelMetadata,
loadingModels,
savingSource,
savingProviderToggles,
testingProviders,
isSourceModified,
configSchema,
@@ -711,6 +740,7 @@ export function useProviderSources(options: UseProviderSourcesOptions) {
addModelProvider,
deleteProvider,
modelAlreadyConfigured,
toggleProviderEnable,
testProvider,
loadConfig,
loadProviderTemplate