-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathpage.tsx
More file actions
482 lines (426 loc) · 17.8 KB
/
page.tsx
File metadata and controls
482 lines (426 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
'use client'
import { useState, useEffect } from "react";
import { useTranslations } from 'next-intl';
import { useLocalStorage } from 'react-use';
import { Store } from "@tauri-apps/plugin-store";
import { v4 } from 'uuid';
import { confirm } from '@tauri-apps/plugin-dialog';
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
} from "@/components/ui/select"
import {
Accordion,
} from "@/components/ui/accordion"
import Image from "next/image";
import { SettingType, FormItem } from "../components/setting-base";
import { AiConfig, ModelConfig, baseAiConfig } from "../config";
import useSettingStore from "@/stores/setting";
import { noteGenModelKeys } from "@/app/model-config";
import { BotMessageSquare, Copy, Eye, EyeOff, Plus, Trash2, X } from "lucide-react";
import { OpenBroswer } from "@/components/open-broswer";
import DefaultModelsSection from "./default-models";
import ModelCard from "./model-card";
import CreateConfig from "./create";
export default function AiPage() {
const t = useTranslations('settings.ai');
const {
aiModelList,
setAiModelList
} = useSettingStore()
// 过滤掉默认模型,只显示用户自定义模型
const userCustomModels = aiModelList.filter(model => !noteGenModelKeys.includes(model.key) && model.title !== 'NoteGen Limited')
const [apiKeyVisible, setApiKeyVisible] = useState<boolean>(false)
const [headerPairs, setHeaderPairs] = useState<Array<{key: string, value: string, id: string}>>([])
const [expandedModels, setExpandedModels] = useState<string[]>([])
// 使用 useLocalStorage 记录当前选择的AI配置
const [selectedAiConfig, setSelectedAiConfig] = useLocalStorage<string>('ai-config-selected', '')
// 当前选中的AI配置
const currentConfig = userCustomModels.find(model => model.key === selectedAiConfig)
const getConfigDisplayTitle = (config?: AiConfig) => {
if (!config) return t('selectConfig')
return baseAiConfig.find(item => item.baseURL === config.baseURL)?.title || config.title
}
const parseHeadersToKeyValue = (headers: Record<string, string> = {}) => {
return Object.entries(headers).map(([key, value]) => ({
key, value: String(value), id: Math.random().toString(36).substr(2, 9)
}))
}
const convertKeyValueToJson = (pairs: Array<{key: string, value: string}>) => {
const obj: Record<string, string> = {}
pairs.forEach(pair => { if (pair.key.trim()) obj[pair.key.trim()] = pair.value })
return obj
}
// 添加新模型
const addNewModel = async () => {
if (!currentConfig) return
const newModelId = v4()
const newModel: ModelConfig = {
id: newModelId,
model: '',
modelType: 'chat',
temperature: 0.7,
topP: 1.0,
enableStream: true
}
const updatedConfig = {
...currentConfig,
models: [...(currentConfig.models || []), newModel]
}
await updateAiConfig(updatedConfig)
// 自动展开新创建的模型
setExpandedModels(prev => [...prev, newModelId])
}
// 删除模型
const deleteModel = async (modelId: string) => {
if (!currentConfig) return
const confirmed = await confirm('确定要删除这个模型吗?')
if (!confirmed) return
const updatedConfig = {
...currentConfig,
models: (currentConfig.models || []).filter(m => m.id !== modelId)
}
await updateAiConfig(updatedConfig)
// 从展开列表中移除被删除的模型
setExpandedModels(prev => prev.filter(id => id !== modelId))
}
// 更新模型配置
const updateModelConfig = async (modelId: string, field: keyof ModelConfig, value: any) => {
if (!currentConfig) return
const updatedModels = (currentConfig.models || []).map(model =>
model.id === modelId ? { ...model, [field]: value } : model
)
const updatedConfig = {
...currentConfig,
models: updatedModels
}
await updateAiConfig(updatedConfig)
}
// 更新AI配置到store
const updateAiConfig = async (config: AiConfig) => {
const store = await Store.load('store.json')
const aiModelList = await store.get<AiConfig[]>('aiModelList') || []
const index = aiModelList.findIndex(item => item.key === config.key)
if (index >= 0) {
aiModelList[index] = config
await store.set('aiModelList', aiModelList)
setAiModelList(aiModelList)
}
}
// 复制当前配置
const copyConfig = async () => {
if (!currentConfig) return
const id = v4()
const newConfig: AiConfig = {
...currentConfig,
key: id,
title: `${currentConfig.title || 'Copy'} (Copy)`,
// 复制models数组
models: currentConfig.models?.map(model => ({
...model,
id: v4() // 给每个模型生成新的ID
})) || []
}
const store = await Store.load('store.json')
const aiModelList = await store.get<AiConfig[]>('aiModelList') || []
const updatedList = [...aiModelList, newConfig]
await store.set('aiModelList', updatedList)
setAiModelList(updatedList)
setSelectedAiConfig(newConfig.key)
}
// 删除当前配置
const deleteCurrentConfig = async () => {
if (!currentConfig) return
// 检查是否是NoteGen默认模型
if (noteGenModelKeys.includes(currentConfig.key)) {
return // 不能删除默认模型
}
const confirmed = await confirm(t('deleteCustomModelConfirm'))
if (!confirmed) return
const store = await Store.load('store.json')
const aiModelList = await store.get<AiConfig[]>('aiModelList') || []
const updatedList = aiModelList.filter(item => item.key !== currentConfig.key)
await store.set('aiModelList', updatedList)
setAiModelList(updatedList)
// 删除后选择下一个用户自定义模型
const remainingUserModels = updatedList.filter(model => !noteGenModelKeys.includes(model.key))
if (remainingUserModels.length > 0) {
setSelectedAiConfig(remainingUserModels[0].key)
} else {
setSelectedAiConfig('')
}
}
// 迁移旧配置到新格式
const migrateOldConfig = (config: AiConfig): AiConfig => {
// 如果已经有models数组,直接返回
if (config.models && config.models.length > 0) {
return config
}
// 如果有旧的model配置,迁移到models数组
if (config.model) {
const migratedModel: ModelConfig = {
id: v4(),
model: config.model,
modelType: config.modelType || 'chat',
temperature: config.temperature,
topP: config.topP,
voice: config.voice,
enableStream: config.enableStream
}
return {
...config,
models: [migratedModel]
}
}
return config
}
// 当选中的配置改变时,更新headers
useEffect(() => {
if (currentConfig) {
setHeaderPairs(parseHeadersToKeyValue(currentConfig.customHeaders))
} else {
setHeaderPairs([])
}
}, [currentConfig])
useEffect(() => {
async function init() {
const store = await Store.load('store.json');
const aiModelList = await store.get<AiConfig[]>('aiModelList')
if (aiModelList) {
// 迁移旧配置
const migratedList = aiModelList.map(migrateOldConfig)
// 检查是否有配置被迁移,如果有则保存
const hasChanges = migratedList.some((config, index) =>
JSON.stringify(config) !== JSON.stringify(aiModelList[index])
)
if (hasChanges) {
await store.set('aiModelList', migratedList)
setAiModelList(migratedList)
}
}
// 过滤出用户自定义模型
const userModels = aiModelList?.filter(model => !noteGenModelKeys.includes(model.key)) || []
// 如果已经有保存的选择,且该配置仍然存在,则使用它
if (selectedAiConfig && userModels.find(model => model.key === selectedAiConfig)) {
// 已经有保存的选择,不需要做任何事情
return
} else if (userModels.length > 0) {
// 如果没有保存的选择或选择的配置不存在,选择第一个
const firstUserModel = userModels[0]
setSelectedAiConfig(firstUserModel.key)
} else {
// 如果没有用户自定义模型,清空选择
setSelectedAiConfig('')
}
}
init()
}, [])
return (
<SettingType id="ai" icon={<BotMessageSquare />} title={t('title')} desc={t('desc')}>
{/* 当没有用户自定义模型时显示默认模型区域 */}
{userCustomModels.length === 0 && <DefaultModelsSection />}
<CreateConfig
hasCustomModels={userCustomModels.length > 0}
onConfigCreated={(configId) => {
setSelectedAiConfig(configId)
}}
/>
{userCustomModels.length > 0 && (
<div className="space-y-8">
{/* AI配置选择 */}
<FormItem title={t('modelConfigTitle')} desc={t('modelConfigDesc')}>
<div className="flex items-center gap-2 md:flex-row flex-col">
<Select value={selectedAiConfig} onValueChange={setSelectedAiConfig}>
<SelectTrigger className="w-full">
<div className="flex items-center gap-2">
{getConfigDisplayTitle(currentConfig)}
</div>
</SelectTrigger>
<SelectContent>
{userCustomModels.map((item) => (
<SelectItem value={item.key} key={item.key}>
{getConfigDisplayTitle(item)}
</SelectItem>
))}
</SelectContent>
</Select>
<div className="flex items-center gap-2 md:w-auto w-full">
<Button
disabled={!currentConfig}
variant="outline"
onClick={copyConfig}
>
<Copy className="h-4 w-4 mr-2" />
{t('copyConfig')}
</Button>
<Button
disabled={!currentConfig || noteGenModelKeys.includes(currentConfig?.key || '')}
variant="destructive"
onClick={deleteCurrentConfig}
>
<Trash2 className="h-4 w-4 mr-2" />
{t('deleteCustomModel')}
</Button>
</div>
</div>
</FormItem>
{/* 当前配置的基础设置 */}
{currentConfig && (
<>
{/* 供应商模板配置信息显示 */}
{baseAiConfig.find(config => config.baseURL === currentConfig.baseURL) && (
<FormItem title={t('providerInfo')}>
<div className="flex items-center gap-3 p-3 bg-muted/50 rounded-lg">
{baseAiConfig.find(config => config.baseURL === currentConfig.baseURL)?.icon && (
<Image
src={baseAiConfig.find(config => config.baseURL === currentConfig.baseURL)?.icon || ''}
alt={currentConfig.title}
width={32}
height={32}
className="w-8 h-8 rounded"
/>
)}
<div>
<div className="font-medium">{baseAiConfig.find(config => config.baseURL === currentConfig.baseURL)?.title || currentConfig.title}</div>
<div className="text-sm text-muted-foreground">{currentConfig.baseURL}</div>
</div>
</div>
</FormItem>
)}
{/* 配置名称 - 只有非供应商模板配置才显示 */}
{!baseAiConfig.find(config => config.baseURL === currentConfig.baseURL) && (
<FormItem title={t('modelTitle')} desc={t('modelTitleDesc')}>
<Input
value={currentConfig.title}
onChange={(e) => updateAiConfig({...currentConfig, title: e.target.value})}
/>
</FormItem>
)}
{/* BaseURL - 只有非供应商模板配置才显示 */}
{!baseAiConfig.find(config => config.baseURL === currentConfig.baseURL) && (
<FormItem title="BaseURL" desc={t('modelBaseUrlDesc')}>
<Input
value={currentConfig.baseURL || ''}
onChange={(e) => updateAiConfig({...currentConfig, baseURL: e.target.value})}
/>
</FormItem>
)}
{/* API Key */}
<FormItem title="API Key">
<div className="flex gap-2">
<Input
className="flex-1"
value={currentConfig.apiKey || ''}
type={apiKeyVisible ? 'text' : 'password'}
onChange={(e) => updateAiConfig({...currentConfig, apiKey: e.target.value})}
/>
<Button variant="outline" size="icon" onClick={() => setApiKeyVisible(!apiKeyVisible)}>
{apiKeyVisible ? <Eye /> : <EyeOff />}
</Button>
{baseAiConfig.find(item => item.baseURL === currentConfig.baseURL)?.apiKeyUrl && (
<OpenBroswer
type="button"
url={baseAiConfig.find(item => item.baseURL === currentConfig.baseURL)?.apiKeyUrl || ''}
title={t('apiKeyUrl')}
/>
)}
</div>
</FormItem>
{/* 自定义Headers */}
{!baseAiConfig.find(config => config.baseURL === currentConfig.baseURL) && (
<FormItem title={t('customHeaders')} desc={t('customHeadersDesc')}>
<div className="space-y-2">
{headerPairs.map((pair, index) => (
<div key={pair.id} className="flex gap-2 items-center">
<Input
placeholder={t('headerKey')}
value={pair.key}
onChange={(e) => {
const newPairs = [...headerPairs]
newPairs[index].key = e.target.value
setHeaderPairs(newPairs)
}}
onBlur={() => {
const jsonObj = convertKeyValueToJson(headerPairs)
updateAiConfig({...currentConfig, customHeaders: jsonObj})
}}
className="flex-1"
/>
<Input
placeholder={t('headerValue')}
value={pair.value}
onChange={(e) => {
const newPairs = [...headerPairs]
newPairs[index].value = e.target.value
setHeaderPairs(newPairs)
}}
onBlur={() => {
const jsonObj = convertKeyValueToJson(headerPairs)
updateAiConfig({...currentConfig, customHeaders: jsonObj})
}}
className="flex-1"
/>
<Button
variant="outline"
size="icon"
onClick={() => {
const newPairs = headerPairs.filter((_, i) => i !== index)
setHeaderPairs(newPairs)
updateAiConfig({...currentConfig, customHeaders: convertKeyValueToJson(newPairs)})
}}
>
<X className="h-4 w-4" />
</Button>
</div>
))}
<Button
variant="outline"
onClick={() => setHeaderPairs([...headerPairs, {
key: '', value: '', id: Math.random().toString(36).substr(2, 9)
}])}
className="w-full"
>
<Plus className="h-4 w-4 mr-2" />
{t('addHeader')}
</Button>
</div>
</FormItem>
)}
{/* 模型配置区域 */}
<FormItem title={t('models')}>
<div className="space-y-4">
{/* 模型卡片列表 */}
<Accordion
type="multiple"
className="space-y-2"
value={expandedModels}
onValueChange={setExpandedModels}
>
{(currentConfig.models || []).map((modelConfig) => (
<ModelCard
key={modelConfig.id}
modelConfig={modelConfig}
aiConfig={currentConfig}
onUpdate={updateModelConfig}
onDelete={deleteModel}
/>
))}
</Accordion>
{/* 添加模型按钮 */}
<Button onClick={addNewModel} className="w-full">
<Plus className="h-4 w-4 mr-2" />
{t('addModel')}
</Button>
</div>
</FormItem>
</>
)}
</div>
)}
</SettingType>
)
}