perf(component): ⚡ 可在设置中切换翻译提供商 (#132)

优化一些代码
This commit is contained in:
Dawn
2024-12-30 15:07:20 +08:00
committed by GitHub
parent c18ee4b467
commit 99a8859476
9 changed files with 56 additions and 51 deletions
+9 -9
View File
@@ -27,7 +27,7 @@ jobs:
'src/**/*.ts' \
'src/**/*.tsx' \
'src-tauri/**/*.rs')
# 如果没有相关文件变更,设置一个提示信息
if [ -z "$DIFF" ]; then
echo "NO_CHANGES=true" >> $GITHUB_ENV
@@ -77,14 +77,14 @@ jobs:
with:
script: |
const OpenAI = require('openai');
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
});
const diff = process.env.DIFF;
try {
const completion = await openai.chat.completions.create({
model: "qwen-plus",
@@ -104,19 +104,19 @@ jobs:
temperature: 0.7,
max_tokens: 1000
});
const analysis = completion.choices[0].message.content;
core.setOutput('analysis', analysis);
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## PR 代码分析
body: `
PR 代码分析
${analysis}
---
*这是由通义千问 AI 自动生成的 PR 分析,仅供参考。*`
});
} catch (error) {
@@ -138,4 +138,4 @@ jobs:
---
*这是自动生成的通知。*`
});
});
+1 -1
View File
@@ -481,7 +481,7 @@ const {
selectKey,
emojiList,
scrollTop
} = useChatMain(props.activeItem)
} = useChatMain()
const { handlePopoverUpdate } = usePopover(selectKey, 'image-chat-main')
watch(
+10 -34
View File
@@ -1,6 +1,6 @@
import { useCommon } from '@/hooks/useCommon.ts'
import { MittEnum, MsgEnum, PowerEnum } from '@/enums'
import { MessageType, SessionItem } from '@/services/types.ts'
import { MessageType } from '@/services/types.ts'
import { useMitt } from '@/hooks/useMitt.ts'
import { useChatStore } from '@/stores/chat.ts'
import apis from '@/services/apis.ts'
@@ -10,9 +10,12 @@ import { useGlobalStore } from '@/stores/global.ts'
import { isDiffNow } from '@/utils/ComputedTime.ts'
import { writeText } from '@tauri-apps/plugin-clipboard-manager'
import { translateText } from '@/services/translate'
import { useSettingStore } from '@/stores/setting.ts'
export const useChatMain = (activeItem?: SessionItem) => {
export const useChatMain = () => {
const { removeTag, openMsgSession, userUid } = useCommon()
const settingStore = useSettingStore()
const { chat } = storeToRefs(settingStore)
const globalStore = useGlobalStore()
const chatStore = useChatStore()
const userStore = useUserStore()?.userInfo
@@ -37,29 +40,6 @@ export const useChatMain = (activeItem?: SessionItem) => {
const historyIndex = ref(0)
/** 当前点击的用户的key */
const selectKey = ref()
/** 计算出触发页脚后的历史消息下标 */
const itemComputed = computed(() => {
return items.value.filter((item) => item.accountId !== userUid.value).length
})
/**! 模拟信息列表 */
const items = ref(
Array.from({ length: 5 }, (_, i) => ({
value: `${i}安老师`,
key: i,
accountId: activeItem?.roomId,
avatar: activeItem?.avatar,
content: '123',
type: MsgEnum.TEXT,
reply: MsgEnum.RECALL
? {
accountName: '',
content: '',
key: ''
}
: null
}))
)
/** 通用右键菜单 */
const commonMenuList = ref<OPT.RightMenu[]>([
@@ -73,7 +53,7 @@ export const useChatMain = (activeItem?: SessionItem) => {
icon: 'translate',
click: async (item: MessageType) => {
const content = item.message.body.content
const result = await translateText(content, 'tencent')
const result = await translateText(content, chat.value.translate)
// 将翻译结果添加到消息中
if (!item.message.body.translatedText) {
item.message.body.translatedText = {
@@ -171,9 +151,8 @@ export const useChatMain = (activeItem?: SessionItem) => {
{
label: '复制',
icon: 'copy',
click: async (item: any) => {
const content = items.value[item.key].content
await handleCopy(content)
click: async (item: MessageType) => {
await handleCopy(item.message.body.content)
}
},
...commonMenuList.value,
@@ -318,9 +297,8 @@ export const useChatMain = (activeItem?: SessionItem) => {
activeBubble.value = item.message.id
// 启用键盘监听
const handleKeyPress = (e: KeyboardEvent) => {
if (e.ctrlKey && e.key === 'c') {
const content = items.value[item.message.id].content
handleCopy(content)
if ((e.ctrlKey && e.key === 'c') || (e.metaKey && e.key === 'c')) {
handleCopy(item.message.body.content)
// 取消监听键盘事件,以免多次绑定
document.removeEventListener('keydown', handleKeyPress)
}
@@ -333,14 +311,12 @@ export const useChatMain = (activeItem?: SessionItem) => {
handleMsgClick,
handleConfirm,
handleItemType,
items,
activeBubble,
floatFooter,
historyIndex,
tips,
modalShow,
specialMenuList,
itemComputed,
optionsList,
report,
selectKey,
+1 -3
View File
@@ -1,5 +1,6 @@
import { fetch } from '@tauri-apps/plugin-http'
import CryptoJS from 'crypto-js'
import { TranslateProvider } from './types.ts'
// 有道云翻译接口响应类型
interface YoudaoResponse {
@@ -24,9 +25,6 @@ interface TranslateResult {
provider: string
}
// 支持的翻译服务提供商类型
type TranslateProvider = 'youdao' | 'tencent'
// 签名计算工具对象
const signUtils = {
/**
+4
View File
@@ -440,6 +440,10 @@ export type MsgReadUnReadCountType = {
/** 未读数 */
unReadCount: number | null
}
// 支持的翻译服务提供商类型
export type TranslateProvider = 'youdao' | 'tencent'
/* ======================================================== */
/**! 模拟信息数据的类型 */
export type MockItem = {
+2 -1
View File
@@ -25,7 +25,8 @@ export const useSettingStore = defineStore(StoresEnum.SETTING, {
},
chat: {
sendKey: 'Enter',
isDouble: true
isDouble: true,
translate: 'tencent'
},
page: {
shadow: true,
+2
View File
@@ -48,6 +48,8 @@ declare namespace STO {
sendKey: string
/** 是否双击打开独立会话窗口 */
isDouble: boolean
/** 翻译提供商 */
translate: 'youdao' | 'tencent'
}
/** 界面设置 */
page: {
+14 -2
View File
@@ -77,10 +77,22 @@
<span class="w-full h-1px bg-[--line-color]"></span>
<!-- 双击打开独立会话 -->
<n-flex align="center" justify="space-between">
<!-- <n-flex align="center" justify="space-between">
<span>双击会话列表打开独立聊天窗口</span>
<n-switch size="small" v-model:value="chat.isDouble" />
</n-flex> -->
<!-- 翻译api选项 -->
<n-flex align="center" justify="space-between">
<span>翻译提供商</span>
<n-select
class="w-140px"
size="small"
label-field="label"
v-model:value="chat.translate"
:options="translateOptions" />
</n-flex>
</n-flex>
</n-flex>
@@ -135,7 +147,7 @@
import { useSettingStore } from '@/stores/setting.ts'
import { CloseBxEnum, ShowModeEnum } from '@/enums'
import { topicsList } from './model.tsx'
import { sendOptions, fontOptions } from './config.ts'
import { sendOptions, fontOptions, translateOptions } from './config.ts'
import { type } from '@tauri-apps/plugin-os'
import { NSwitch } from 'naive-ui'
import { invoke } from '@tauri-apps/api/core'
+13 -1
View File
@@ -36,6 +36,18 @@ const sendOptions = [
}
]
/** 翻译提供商的选项 */
const translateOptions = [
{
label: '腾讯云翻译',
value: 'tencent'
},
{
label: '有道云翻译',
value: 'youdao'
}
]
/** 字体 */
const fontOptions = [
{
@@ -47,4 +59,4 @@ const fontOptions = [
value: 'AliFangYuan'
}
]
export { sideOptions, sendOptions, fontOptions }
export { sideOptions, sendOptions, fontOptions, translateOptions }