@@ -10,67 +10,32 @@ import useAIConfigStore from '@/stores/AIConfig'
10
10
const emit = defineEmits ([` saved ` ])
11
11
12
12
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 )
25
14
26
15
/** UI 状态 */
27
16
const loading = ref (false )
28
17
const testResult = ref (` ` )
29
18
30
19
/** 当前服务信息 */
31
20
const currentService = computed (
32
- () => serviceOptions .find (s => s .value === config . type ) || serviceOptions [0 ],
21
+ () => serviceOptions .find (s => s .value === type . value ) || serviceOptions [0 ],
33
22
)
34
23
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
-
47
24
/* -------------------------- 监听 -------------------------- */
48
25
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
+ })
59
30
60
- watch (() => config .model , () => (testResult .value = ` ` ))
31
+ // 监听模型变化,清空测试结果
32
+ watch (model , () => {
33
+ testResult .value = ` `
34
+ })
61
35
62
36
/* -------------------------- 操作 -------------------------- */
63
37
64
38
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
-
74
39
if (emitEvent ) {
75
40
testResult .value = ` ✅ 配置已保存 `
76
41
emit (` saved ` )
@@ -79,7 +44,6 @@ function saveConfig(emitEvent = true) {
79
44
80
45
function clearConfig() {
81
46
AIConfigStore .reset ()
82
- pullFromStore ()
83
47
testResult .value = ` 🗑️ 当前 AI 配置已清除 `
84
48
}
85
49
@@ -88,16 +52,16 @@ async function testConnection() {
88
52
loading .value = true
89
53
90
54
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 } `
93
57
94
58
try {
95
- const url = new URL (config . endpoint )
59
+ const url = new URL (endpoint . value )
96
60
if (! url .pathname .endsWith (` /chat/completions ` ))
97
61
url .pathname = url .pathname .replace (/ \/ ? $ / , ` /chat/completions ` )
98
62
99
63
const payload = {
100
- model: config . model ,
64
+ model: model . value ,
101
65
messages: [{ role: ` user ` , content: ` ping ` }],
102
66
temperature: 0 ,
103
67
max_tokens: 1 ,
@@ -123,7 +87,7 @@ async function testConnection() {
123
87
&& (error ?.code === ` ModelNotOpen `
124
88
|| / not activated| 未开通/ i .test (error ?.message ))
125
89
) {
126
- testResult .value = ` ⚠️ 测试成功,但当前模型未开通:${config . model } `
90
+ testResult .value = ` ⚠️ 测试成功,但当前模型未开通:${model . value } `
127
91
saveConfig (false )
128
92
return
129
93
}
@@ -150,7 +114,7 @@ async function testConnection() {
150
114
<!-- 服务类型 -->
151
115
<div >
152
116
<Label class =" mb-1 block text-sm font-medium" >服务类型</Label >
153
- <Select v-model =" config. type" >
117
+ <Select v-model =" type" >
154
118
<SelectTrigger class =" w-full" >
155
119
<SelectValue >
156
120
{{ currentService.label }}
@@ -169,20 +133,20 @@ async function testConnection() {
169
133
</div >
170
134
171
135
<!-- API 端点 -->
172
- <div v-if =" config. type !== DEFAULT_SERVICE_TYPE" >
136
+ <div v-if =" type !== DEFAULT_SERVICE_TYPE" >
173
137
<Label class =" mb-1 block text-sm font-medium" >API 端点</Label >
174
138
<Input
175
- v-model =" config. endpoint"
139
+ v-model =" endpoint"
176
140
placeholder =" 输入 API 端点 URL"
177
141
class =" focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
178
142
/>
179
143
</div >
180
144
181
145
<!-- API 密钥,仅非 default 显示 -->
182
- <div v-if =" config. type !== DEFAULT_SERVICE_TYPE" >
146
+ <div v-if =" type !== DEFAULT_SERVICE_TYPE" >
183
147
<Label class =" mb-1 block text-sm font-medium" >API 密钥</Label >
184
148
<PasswordInput
185
- v-model =" config. apiKey"
149
+ v-model =" apiKey"
186
150
placeholder =" sk-..."
187
151
class =" focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
188
152
/>
@@ -191,10 +155,10 @@ async function testConnection() {
191
155
<!-- 模型名称 -->
192
156
<div >
193
157
<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" >
195
159
<SelectTrigger class =" w-full" >
196
160
<SelectValue >
197
- {{ config. model || '请选择模型' }}
161
+ {{ model || '请选择模型' }}
198
162
</SelectValue >
199
163
</SelectTrigger >
200
164
<SelectContent >
@@ -209,7 +173,7 @@ async function testConnection() {
209
173
</Select >
210
174
<Input
211
175
v-else
212
- v-model =" config. model"
176
+ v-model =" model"
213
177
placeholder =" 输入模型名称"
214
178
class =" focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
215
179
/>
@@ -231,7 +195,7 @@ async function testConnection() {
231
195
</TooltipProvider >
232
196
</Label >
233
197
<Input
234
- v-model.number =" config. temperature"
198
+ v-model.number =" temperature"
235
199
type =" number"
236
200
step =" 0.1"
237
201
min =" 0"
@@ -245,7 +209,7 @@ async function testConnection() {
245
209
<div >
246
210
<Label class =" mb-1 block text-sm font-medium" >最大 Token 数</Label >
247
211
<Input
248
- v-model.number =" config. maxToken"
212
+ v-model.number =" maxToken"
249
213
type =" number"
250
214
min =" 1"
251
215
max =" 32768"
0 commit comments