Skip to content

Commit b41666b

Browse files
authored
fix: ai config (#1043)
1 parent 9555b2b commit b41666b

File tree

2 files changed

+60
-143
lines changed

2 files changed

+60
-143
lines changed

apps/web/src/components/ai/chat-box/AIConfig.vue

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,32 @@ import useAIConfigStore from '@/stores/AIConfig'
1010
const emit = defineEmits([`saved`])
1111
1212
const AIConfigStore = useAIConfigStore()
13-
const { type, endpoint, model, apiKey, temperature, maxToken }
14-
= storeToRefs(AIConfigStore)
15-
16-
/** 本地草稿 */
17-
const config = reactive({
18-
type: ``,
19-
endpoint: ``,
20-
apiKey: ``,
21-
model: ``,
22-
temperature: 1,
23-
maxToken: 1024,
24-
})
13+
const { type, endpoint, model, apiKey, temperature, maxToken } = storeToRefs(AIConfigStore)
2514
2615
/** UI 状态 */
2716
const loading = ref(false)
2817
const testResult = ref(``)
2918
3019
/** 当前服务信息 */
3120
const currentService = computed(
32-
() => serviceOptions.find(s => s.value === config.type) || serviceOptions[0],
21+
() => serviceOptions.find(s => s.value === type.value) || serviceOptions[0],
3322
)
3423
35-
/* -------------------------- 同步函数 -------------------------- */
36-
37-
function pullFromStore() {
38-
config.type = type.value
39-
config.endpoint = endpoint.value
40-
config.apiKey = apiKey.value
41-
config.model = model.value
42-
config.temperature = temperature.value
43-
config.maxToken = maxToken.value
44-
}
45-
pullFromStore() // 首屏同步一次
46-
4724
/* -------------------------- 监听 -------------------------- */
4825
49-
watch(
50-
() => config.type,
51-
() => {
52-
config.endpoint = currentService.value.endpoint
53-
if (!currentService.value.models.includes(config.model)) {
54-
config.model = currentService.value.models[0] || ``
55-
}
56-
testResult.value = ``
57-
},
58-
)
26+
// 监听服务类型变化,清空测试结果
27+
watch(type, () => {
28+
testResult.value = ``
29+
})
5930
60-
watch(() => config.model, () => (testResult.value = ``))
31+
// 监听模型变化,清空测试结果
32+
watch(model, () => {
33+
testResult.value = ``
34+
})
6135
6236
/* -------------------------- 操作 -------------------------- */
6337
6438
function saveConfig(emitEvent = true) {
65-
AIConfigStore.$patch({
66-
type: config.type,
67-
endpoint: config.endpoint,
68-
model: config.model,
69-
temperature: config.temperature,
70-
maxToken: config.maxToken,
71-
})
72-
apiKey.value = config.apiKey
73-
7439
if (emitEvent) {
7540
testResult.value = `✅ 配置已保存`
7641
emit(`saved`)
@@ -79,7 +44,6 @@ function saveConfig(emitEvent = true) {
7944
8045
function clearConfig() {
8146
AIConfigStore.reset()
82-
pullFromStore()
8347
testResult.value = `🗑️ 当前 AI 配置已清除`
8448
}
8549
@@ -88,16 +52,16 @@ async function testConnection() {
8852
loading.value = true
8953
9054
const headers: Record<string, string> = { 'Content-Type': `application/json` }
91-
if (config.apiKey && config.type !== DEFAULT_SERVICE_TYPE)
92-
headers.Authorization = `Bearer ${config.apiKey}`
55+
if (apiKey.value && type.value !== DEFAULT_SERVICE_TYPE)
56+
headers.Authorization = `Bearer ${apiKey.value}`
9357
9458
try {
95-
const url = new URL(config.endpoint)
59+
const url = new URL(endpoint.value)
9660
if (!url.pathname.endsWith(`/chat/completions`))
9761
url.pathname = url.pathname.replace(/\/?$/, `/chat/completions`)
9862
9963
const payload = {
100-
model: config.model,
64+
model: model.value,
10165
messages: [{ role: `user`, content: `ping` }],
10266
temperature: 0,
10367
max_tokens: 1,
@@ -123,7 +87,7 @@ async function testConnection() {
12387
&& (error?.code === `ModelNotOpen`
12488
|| /not activated|未开通/i.test(error?.message))
12589
) {
126-
testResult.value = `⚠️ 测试成功,但当前模型未开通:${config.model}`
90+
testResult.value = `⚠️ 测试成功,但当前模型未开通:${model.value}`
12791
saveConfig(false)
12892
return
12993
}
@@ -150,7 +114,7 @@ async function testConnection() {
150114
<!-- 服务类型 -->
151115
<div>
152116
<Label class="mb-1 block text-sm font-medium">服务类型</Label>
153-
<Select v-model="config.type">
117+
<Select v-model="type">
154118
<SelectTrigger class="w-full">
155119
<SelectValue>
156120
{{ currentService.label }}
@@ -169,20 +133,20 @@ async function testConnection() {
169133
</div>
170134

171135
<!-- API 端点 -->
172-
<div v-if="config.type !== DEFAULT_SERVICE_TYPE">
136+
<div v-if="type !== DEFAULT_SERVICE_TYPE">
173137
<Label class="mb-1 block text-sm font-medium">API 端点</Label>
174138
<Input
175-
v-model="config.endpoint"
139+
v-model="endpoint"
176140
placeholder="输入 API 端点 URL"
177141
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
178142
/>
179143
</div>
180144

181145
<!-- API 密钥,仅非 default 显示 -->
182-
<div v-if="config.type !== DEFAULT_SERVICE_TYPE">
146+
<div v-if="type !== DEFAULT_SERVICE_TYPE">
183147
<Label class="mb-1 block text-sm font-medium">API 密钥</Label>
184148
<PasswordInput
185-
v-model="config.apiKey"
149+
v-model="apiKey"
186150
placeholder="sk-..."
187151
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
188152
/>
@@ -191,10 +155,10 @@ async function testConnection() {
191155
<!-- 模型名称 -->
192156
<div>
193157
<Label class="mb-1 block text-sm font-medium">模型名称</Label>
194-
<Select v-if="currentService.models.length > 0" v-model="config.model">
158+
<Select v-if="currentService.models.length > 0" v-model="model">
195159
<SelectTrigger class="w-full">
196160
<SelectValue>
197-
{{ config.model || '请选择模型' }}
161+
{{ model || '请选择模型' }}
198162
</SelectValue>
199163
</SelectTrigger>
200164
<SelectContent>
@@ -209,7 +173,7 @@ async function testConnection() {
209173
</Select>
210174
<Input
211175
v-else
212-
v-model="config.model"
176+
v-model="model"
213177
placeholder="输入模型名称"
214178
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
215179
/>
@@ -231,7 +195,7 @@ async function testConnection() {
231195
</TooltipProvider>
232196
</Label>
233197
<Input
234-
v-model.number="config.temperature"
198+
v-model.number="temperature"
235199
type="number"
236200
step="0.1"
237201
min="0"
@@ -245,7 +209,7 @@ async function testConnection() {
245209
<div>
246210
<Label class="mb-1 block text-sm font-medium">最大 Token 数</Label>
247211
<Input
248-
v-model.number="config.maxToken"
212+
v-model.number="maxToken"
249213
type="number"
250214
min="1"
251215
max="32768"

0 commit comments

Comments
 (0)