-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathMain.vue
491 lines (449 loc) · 18.3 KB
/
Main.vue
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
483
484
485
486
487
488
489
490
491
<template>
<!-- 开关 -->
<el-row class="margin-bottom margin-left-2em">
<el-col :span="20" class="lightblue rounded-corner">
<span class="popup-text popup-vertical-left">插件状态</span>
</el-col>
<el-col :span="4">
<el-switch v-model="config.on" inline-prompt active-text="开" inactive-text="关" />
</el-col>
</el-row>
<!-- 占位符 -->
<div v-if="!config.on">
<el-empty description="插件处于禁用状态" />
</div>
<div v-show="config.on">
<!-- 翻译模式-->
<el-row class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<span class="popup-text popup-vertical-left">翻译模式</span>
</el-col>
<el-col :span="12">
<el-select v-model="config.display" placeholder="请选择翻译模式">
<el-option class="select-left" v-for="item in options.display" :key="item.value" :label="item.label"
:value="item.value" />
</el-select>
</el-col>
</el-row>
<!-- 译文样式选择器-->
<el-row v-show="config.display === 1" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
content="选择双语模式下译文的显示样式,提供多种美观的效果"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">译文样式<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-select v-model="config.style" placeholder="请选择译文显示样式">
<el-option-group v-for="group in styleGroups" :key="group.value" :label="group.label">
<el-option v-for="item in group.options"
:key="item.value"
:label="item.label"
:value="item.value"
:class="item.class" />
</el-option-group>
</el-select>
</el-col>
</el-row>
<!-- 翻译服务 -->
<el-row class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
content="机器翻译:快速稳定,适合日常使用;AI翻译:更自然流畅,需要配置令牌"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">翻译服务<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<b>
<el-select v-model="config.service" placeholder="请选择翻译服务">
<el-option class="select-left" v-for="item in compute.filteredServices" :key="item.value"
:label="item.label" :value="item.value" :disabled="item.disabled"
:class="{ 'select-divider': item.disabled }" />
</el-select>
</b>
</el-col>
</el-row>
<!-- 目标语言 -->
<el-row class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<span class="popup-text popup-vertical-left">目标语言</span>
</el-col>
<el-col :span="12">
<el-select v-model="config.to" placeholder="请选择目标语言">
<el-option class="select-left" v-for="item in options.to" :key="item.value" :label="item.label"
:value="item.value" />
</el-select>
</el-col>
</el-row>
<!-- 快捷键 -->
<el-row class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<span class="popup-text popup-vertical-left">快捷键</span>
</el-col>
<el-col :span="12">
<el-select v-model="config.hotkey" placeholder="请选择快捷键">
<el-option class="select-left" v-for="item in options.keys" :key="item.value" :label="item.label"
:value="item.value" :disabled="item.disabled"
:class="{ 'select-divider': item.disabled }" />
</el-select>
</el-col>
</el-row>
<!-- token -->
<el-row v-show="compute.showToken" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
content="API访问令牌仅保存在本地,用于访问翻译服务。获取方式请参考对应服务的官方文档"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">访问令牌<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-input v-model="config.token[config.service]" type="password" show-password placeholder="请输入API访问令牌" />
</el-col>
</el-row>
<!-- 使用AkSk -->
<el-row v-show="compute.showAkSk" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
content="百度文心一言API密钥对,用于访问翻译服务"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">API Key<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-input v-model="config.ak" placeholder="请输入Access Key" />
</el-col>
</el-row>
<el-row v-show="compute.showAkSk" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
content="百度文心一言API密钥对,用于访问翻译服务"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">Secret Key<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-input v-model="config.sk" type="password" placeholder="请输入Secret Key" />
</el-col>
</el-row>
<!-- Coze需显示 robot_id -->
<el-row v-show="compute.showRobotId" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
content="Coze机器人ID,可在Coze开发者文档中查看获取方式"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">机器人ID<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-input v-model="config.robot_id[config.service]" placeholder="请输入Coze机器人ID" />
</el-col>
</el-row>
<!-- 本地大模型配置 -->
<el-row v-show="compute.showCustom" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark" content="目前仅支持OpenAI格式的请求接口,如http://localhost:3000/v1/chat/completions,其中 localhost:11434 可更换为任意值。
如果使用的是 ollama,则需要使用 OLLAMA_ORIGINS=chrome-extension://* ollama serve 启动 ollama 以允许跨域访问。"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">自定义接口<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-input v-model="config.custom" placeholder="请输入自定义接口地址" />
</el-col>
</el-row>
<!-- 模型 -->
<el-row v-show="compute.showModel" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<span class="popup-text popup-vertical-left">模型</span>
</el-col>
<el-col :span="12">
<el-select v-model="config.model[config.service]" placeholder="请选择模型">
<el-option class="select-left" v-for="item in compute.model" :key="item" :label="item" :value="item" />
</el-select>
</el-col>
</el-row>
<el-row v-show="compute.showCustomModel" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
:content="config.service === 'doubao' ? '豆包的model为接入点,获取方式见官方文档:https://console.volcengine.com/ark/region:ark+cn-beijing/endpoint' : '注意:自定义模型名称需要与服务商提供的模型名称一致,否则无法使用!'"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">{{config.service === 'doubao' ? '接入点' : '自定义模型'}}<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-input v-model="config.customModel[config.service]" placeholder="例如:gemma:7b" />
</el-col>
</el-row>
<!-- 高级选项-->
<el-collapse>
<el-collapse-item title="高级选项" style="margin-left: 2em;" :disabled="compute.showMachine">
<!-- 使用代理转发 -->
<el-row v-show="compute.showProxy" class="margin-bottom">
<el-col :span="8" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark" content="使用代理可以解决网络无法访问的问题,如不熟悉代理设置请留空!" placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">代理地址<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="16">
<el-input v-model="config.proxy[config.service]" placeholder="默认不使用代理" />
</el-col>
</el-row>
<!-- 角色和模板 -->
<el-row v-show="compute.showAI" class="margin-bottom">
<el-col :span="8" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark" content="以系统身份 system 发送的对话,常用于指定 AI 要扮演的角色"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">system<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="16">
<el-input type="textarea" v-model="config.system_role[config.service]" maxlength="8192"
placeholder="system message " />
</el-col>
</el-row>
<el-row v-show="compute.showAI" class="margin-bottom">
<el-col :span="8" class="lightblue rounded-corner">
<el-tooltip class="box-item" effect="dark"
content="以用户身份 user 发送的对话,其中{{to}}表示目标语言,{{origin}}表示待翻译的文本内容,两者不可缺少。"
placement="top-start"
:show-after="500">
<span class="popup-text popup-vertical-left">user<el-icon class="icon-margin">
<ChatDotRound />
</el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="16">
<el-input type="textarea" v-model="config.user_role[config.service]" maxlength="8192"
placeholder="user message template" />
</el-col>
</el-row>
<!-- 恢复默认模板按钮 -->
<el-row v-show="compute.showAI" class="margin-bottom">
<el-col :span="24" style="text-align: right;">
<el-button type="primary" link @click="resetTemplate">
<el-icon><Refresh /></el-icon>
恢复默认模板
</el-button>
</el-col>
</el-row>
</el-collapse-item>
</el-collapse>
<!-- 主题设置 -->
<el-row class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<span class="popup-text popup-vertical-left">主题设置</span>
</el-col>
<el-col :span="12">
<el-select v-model="config.theme" placeholder="请选择主题模式">
<el-option class="select-left" v-for="item in options.theme" :key="item.value" :label="item.label"
:value="item.value" />
</el-select>
</el-col>
</el-row>
</div>
</template>
<script lang="ts" setup>
// Main 处理配置信息
import { computed, ref, watch, onUnmounted } from 'vue'
import { models, options, servicesType, defaultOption } from "../entrypoints/utils/option";
import { Config } from "@/entrypoints/utils/model";
import { storage } from '@wxt-dev/storage';
import { ChatDotRound, Refresh } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
// 初始化深色模式媒体查询
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
// 更新主题函数
function updateTheme(theme: string) {
if (theme === 'auto') {
// 自动模式下,直接使用系统主题
const isDark = darkModeMediaQuery.matches;
console.log('isDark', isDark);
document.documentElement.classList.toggle('dark', isDark);
} else {
// 手动模式下,使用选择的主题
document.documentElement.classList.toggle('dark', theme === 'dark');
}
}
// 配置信息
let config = ref(new Config());
// 从 storage 中获取本地配置
storage.getItem('local:config').then((value: any) => {
if (typeof value === 'string' && value) {
const parsedConfig = JSON.parse(value);
Object.assign(config.value, parsedConfig);
}
// 初始应用主题
updateTheme(config.value.theme || 'auto');
});
// 监听 storage 中 'local:config' 的变化
// 当其他页面修改了配置时,会触发这个监听器
// newValue 是新的配置值,oldValue 是旧的配置值
storage.watch('local:config', (newValue: any, oldValue: any) => {
// 检查 newValue 是否为非空字符串
if (typeof newValue === 'string' && newValue) {
// 将新的配置值解析为对象,并合并到当前的 config.value 中
// 这样可以保持所有页面的配置同步
Object.assign(config.value, JSON.parse(newValue));
}
});
// 监听菜单栏配置变化
// 当配置发生改变时,将新的配置序列化为 JSON 字符串并保存到 storage 中
// deep: true 表示深度监听对象内部属性的变化
watch(config, (newValue: any, oldValue: any) => {
// console.log('配置变化,修改存储', newValue, oldValue)
storage.setItem('local:config', JSON.stringify(newValue));
}, { deep: true });
// 计算属性
let compute = ref({
// 1、是否是AI服务
showAI: computed(() => servicesType.isAI(config.value.service)),
// 2、是否是机器翻译
showMachine: computed(() => servicesType.isMachine(config.value.service)),
// 3、是否显示代理
showProxy: computed(() => servicesType.isUseProxy(config.value.service)),
// 4、是否显示模型
showModel: computed(() => servicesType.isUseModel(config.value.service)),
// 5、是否显示token
showToken: computed(() => servicesType.isUseToken(config.value.service)),
// 6、是否显示 AkSk
showAkSk: computed(() => servicesType.isUseAkSk(config.value.service)),
// 7、获取模型列表
model: computed(() => models.get(config.value.service) || []),
// 8、是否需要自定义接口
showCustom: computed(() => servicesType.isCustom(config.value.service)),
// 9、是否自定义模型
showCustomModel: computed(() => servicesType.isAI(config.value.service) && config.value.model[config.value.service] === "自定义模型"),
// 10、判断是否为"双语模式",控制一些翻译服务的显示
filteredServices: computed(() => options.services.filter((service: any) =>
!([service.google].includes(service.value) && config.value.display !== 1))
),
// 11、判断是否为 coze
showRobotId: computed(() => servicesType.isCoze(config.value.service)),
})
// 监听主题变化
watch(() => config.value.theme, (newTheme) => {
updateTheme(newTheme || 'auto');
});
// 使用 onchange 监听系统主题变化
darkModeMediaQuery.onchange = (e) => {
if (config.value.theme === 'auto') {
updateTheme('auto');
}
};
// 组件卸载时清理
onUnmounted(() => {
darkModeMediaQuery.onchange = null;
});
// 计算样式分组
const styleGroups = computed(() => {
const groups = options.styles.filter(item => item.disabled);
return groups.map(group => ({
...group,
options: options.styles.filter(item => !item.disabled && item.group === group.value)
}));
});
// 恢复默认模板
const resetTemplate = () => {
ElMessageBox.confirm(
'确定要恢复默认的 system 和 user 模板吗?此操作将覆盖当前的自定义模板。',
'恢复默认模板',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => {
config.value.system_role[config.value.service] = defaultOption.system_role;
config.value.user_role[config.value.service] = defaultOption.user_role;
ElMessage({
message: '已成功恢复默认翻译模板',
type: 'success',
duration: 2000
});
}).catch(() => {
// 用户取消操作,不做任何处理
});
};
</script>
<style scoped>
.select-left {
text-align: left;
}
.select-divider {
background: #f2f6fc;
color: #409eff;
font-size: 12px;
padding: 4px 12px;
cursor: default;
font-weight: 500;
letter-spacing: 1px;
text-transform: uppercase;
border-bottom: 1px solid #e4e7ed;
margin: 4px 0;
pointer-events: none;
opacity: 0.9;
}
.icon-margin {
margin-left: 0.25em;
}
/* 添加自适应样式 */
:deep(.el-select) {
width: 100%;
}
:deep(.el-input) {
width: 100%;
}
.margin-bottom {
margin-bottom: 12px;
}
.margin-left-2em {
margin-left: 1em;
margin-right: 1em;
}
/* 设置滚动条样式 */
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-thumb {
background: #ddd;
border-radius: 3px;
}
::-webkit-scrollbar-track {
background: #f5f5f5;
border-radius: 3px;
}
</style>