Skip to content

Commit 10a48a3

Browse files
authored
feat: add model enable toggle action (#40)
* feat: add model enable toggle action * feat: add model enable toggle action
1 parent ec2b35d commit 10a48a3

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

ui/src/views/components/AllModelListItem.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { useClipboard } from '@vueuse/core'
2424
import { computed, shallowRef } from 'vue'
2525
import { useRouter } from 'vue-router'
2626
import ModelEditingModal from './ModelEditingModal.vue'
27+
import ModelEnableSwitch from './ModelEnableSwitch.vue'
2728
2829
const props = defineProps<{
2930
model: AiModel
@@ -106,6 +107,7 @@ const canTest = computed(() => isEnabledTestableModel(props.model))
106107
<div class=":uno: flex flex-wrap items-center justify-end gap-2">
107108
<ModelBadgeGroup :model="model" />
108109
<VStatusDot v-if="model.metadata.deletionTimestamp" animate state="warning" text="删除中" />
110+
<ModelEnableSwitch :model="model" />
109111
</div>
110112
</template>
111113
<template #dropdownItems>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<script lang="ts" setup>
2+
import { aiCoreApiClient } from '@/api'
3+
import type { AiModel } from '@/api/generated'
4+
import { QK_MODELS } from '@/composables/use-models-fetch'
5+
import { Toast, VSwitch } from '@halo-dev/components'
6+
import { useMutation, useQueryClient } from '@tanstack/vue-query'
7+
import { computed } from 'vue'
8+
9+
const props = defineProps<{
10+
model: AiModel
11+
}>()
12+
13+
const queryClient = useQueryClient()
14+
15+
const enabled = computed(() => props.model.spec.enabled !== false)
16+
17+
const mutation = useMutation({
18+
mutationFn: async (nextEnabled: boolean) => {
19+
return await aiCoreApiClient.model.patchAiModel({
20+
name: props.model.metadata.name,
21+
jsonPatchInner: [
22+
{
23+
op: 'add',
24+
path: '/spec/enabled',
25+
value: nextEnabled,
26+
},
27+
],
28+
})
29+
},
30+
onSuccess: (_data, nextEnabled) => {
31+
Toast.success(nextEnabled ? '模型已启用' : '模型已禁用')
32+
queryClient.invalidateQueries({ queryKey: [QK_MODELS] })
33+
},
34+
onError: (error) => {
35+
Toast.error('模型状态更新失败: ' + (error as Error).message)
36+
},
37+
})
38+
39+
const isUpdating = computed(() => mutation.isPending.value)
40+
const disabled = computed(() => !!props.model.metadata.deletionTimestamp || isUpdating.value)
41+
42+
function handleToggle() {
43+
if (disabled.value) {
44+
return
45+
}
46+
mutation.mutate(!enabled.value)
47+
}
48+
</script>
49+
50+
<template>
51+
<VSwitch
52+
:model-value="enabled"
53+
:loading="isUpdating"
54+
:disabled="disabled"
55+
v-tooltip="enabled ? '禁用模型' : '启用模型'"
56+
@click.stop="handleToggle"
57+
/>
58+
</template>

ui/src/views/components/ProviderModelListItem.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useClipboard } from '@vueuse/core'
1919
import { computed, shallowRef } from 'vue'
2020
import { useRouter } from 'vue-router'
2121
import ModelEditingModal from './ModelEditingModal.vue'
22+
import ModelEnableSwitch from './ModelEnableSwitch.vue'
2223
2324
const props = defineProps<{
2425
model: AiModel
@@ -84,6 +85,7 @@ function handleDelete() {
8485
<div class=":uno: flex flex-wrap items-center justify-end gap-1.5">
8586
<ModelBadgeGroup :model="model" />
8687
<VStatusDot v-if="model.metadata.deletionTimestamp" animate state="warning" text="删除中" />
88+
<ModelEnableSwitch :model="model" />
8789
</div>
8890
</template>
8991
<template #dropdownItems>

0 commit comments

Comments
 (0)