Skip to content

Commit

Permalink
perf(component): ⚡ 可在设置中切换翻译提供商
Browse files Browse the repository at this point in the history
优化一些代码
  • Loading branch information
nongyehong committed Dec 30, 2024
1 parent c18ee4b commit 24b20c6
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 51 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/pr-chatbot-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
'src/**/*.ts' \
'src/**/*.tsx' \
'src-tauri/**/*.rs')
# 如果没有相关文件变更,设置一个提示信息
if [ -z "$DIFF" ]; then
echo "NO_CHANGES=true" >> $GITHUB_ENV
Expand Down Expand Up @@ -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",
Expand All @@ -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) {
Expand All @@ -138,4 +138,4 @@ jobs:
---
*这是自动生成的通知。*`
});
});
2 changes: 1 addition & 1 deletion src/components/rightBox/chatBox/ChatMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ const {
selectKey,
emojiList,
scrollTop
} = useChatMain(props.activeItem)
} = useChatMain()

Check warning on line 484 in src/components/rightBox/chatBox/ChatMain.vue

View check run for this annotation

Codecov / codecov/patch

src/components/rightBox/chatBox/ChatMain.vue#L484

Added line #L484 was not covered by tests
const { handlePopoverUpdate } = usePopover(selectKey, 'image-chat-main')
watch(
Expand Down
44 changes: 10 additions & 34 deletions src/hooks/useChatMain.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 = () => {

Check warning on line 15 in src/hooks/useChatMain.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useChatMain.ts#L15

Added line #L15 was not covered by tests
const { removeTag, openMsgSession, userUid } = useCommon()
const settingStore = useSettingStore()
const { chat } = storeToRefs(settingStore)

Check warning on line 18 in src/hooks/useChatMain.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useChatMain.ts#L17-L18

Added lines #L17 - L18 were not covered by tests
const globalStore = useGlobalStore()
const chatStore = useChatStore()
const userStore = useUserStore()?.userInfo
Expand All @@ -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[]>([
Expand All @@ -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)

Check warning on line 56 in src/hooks/useChatMain.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useChatMain.ts#L56

Added line #L56 was not covered by tests
// 将翻译结果添加到消息中
if (!item.message.body.translatedText) {
item.message.body.translatedText = {
Expand Down Expand Up @@ -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)

Check warning on line 155 in src/hooks/useChatMain.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useChatMain.ts#L154-L155

Added lines #L154 - L155 were not covered by tests
}
},
...commonMenuList.value,
Expand Down Expand Up @@ -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)

Check warning on line 301 in src/hooks/useChatMain.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useChatMain.ts#L300-L301

Added lines #L300 - L301 were not covered by tests
// 取消监听键盘事件,以免多次绑定
document.removeEventListener('keydown', handleKeyPress)
}
Expand All @@ -333,14 +311,12 @@ export const useChatMain = (activeItem?: SessionItem) => {
handleMsgClick,
handleConfirm,
handleItemType,
items,
activeBubble,
floatFooter,
historyIndex,
tips,
modalShow,
specialMenuList,
itemComputed,
optionsList,
report,
selectKey,
Expand Down
4 changes: 1 addition & 3 deletions src/services/translate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fetch } from '@tauri-apps/plugin-http'
import CryptoJS from 'crypto-js'
import { TranslateProvider } from './types.ts'

// 有道云翻译接口响应类型
interface YoudaoResponse {
Expand All @@ -24,9 +25,6 @@ interface TranslateResult {
provider: string
}

// 支持的翻译服务提供商类型
type TranslateProvider = 'youdao' | 'tencent'

// 签名计算工具对象
const signUtils = {
/**
Expand Down
4 changes: 4 additions & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ export type MsgReadUnReadCountType = {
/** 未读数 */
unReadCount: number | null
}

// 支持的翻译服务提供商类型
export type TranslateProvider = 'youdao' | 'tencent'

/* ======================================================== */
/**! 模拟信息数据的类型 */
export type MockItem = {
Expand Down
3 changes: 2 additions & 1 deletion src/stores/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const useSettingStore = defineStore(StoresEnum.SETTING, {
},
chat: {
sendKey: 'Enter',
isDouble: true
isDouble: true,
translate: 'tencent'
},
page: {
shadow: true,
Expand Down
2 changes: 2 additions & 0 deletions src/typings/stores.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ declare namespace STO {
sendKey: string
/** 是否双击打开独立会话窗口 */
isDouble: boolean
/** 翻译提供商 */
translate: 'youdao' | 'tencent'
}
/** 界面设置 */
page: {
Expand Down
16 changes: 14 additions & 2 deletions src/views/moreWindow/settings/General.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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">

Check warning on line 80 in src/views/moreWindow/settings/General.vue

View check run for this annotation

Codecov / codecov/patch

src/views/moreWindow/settings/General.vue#L80

Added line #L80 was not covered by tests
<span>双击会话列表打开独立聊天窗口</span>
<n-switch size="small" v-model:value="chat.isDouble" />
</n-flex> -->

<!-- 翻译api选项 -->
<n-flex align="center" justify="space-between">

Check warning on line 87 in src/views/moreWindow/settings/General.vue

View check run for this annotation

Codecov / codecov/patch

src/views/moreWindow/settings/General.vue#L86-L87

Added lines #L86 - L87 were not covered by tests
<span>翻译提供商</span>

<n-select
class="w-140px"
size="small"
label-field="label"
v-model:value="chat.translate"
:options="translateOptions" />

Check warning on line 95 in src/views/moreWindow/settings/General.vue

View check run for this annotation

Codecov / codecov/patch

src/views/moreWindow/settings/General.vue#L90-L95

Added lines #L90 - L95 were not covered by tests
</n-flex>
</n-flex>
</n-flex>
Expand Down Expand Up @@ -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'
Expand Down
14 changes: 13 additions & 1 deletion src/views/moreWindow/settings/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ const sendOptions = [
}
]

/** 翻译提供商的选项 */
const translateOptions = [
{
label: '腾讯云翻译',
value: 'tencent'
},
{
label: '有道云翻译',
value: 'youdao'
}
]

Check warning on line 49 in src/views/moreWindow/settings/config.ts

View check run for this annotation

Codecov / codecov/patch

src/views/moreWindow/settings/config.ts#L40-L49

Added lines #L40 - L49 were not covered by tests

/** 字体 */
const fontOptions = [
{
Expand All @@ -47,4 +59,4 @@ const fontOptions = [
value: 'AliFangYuan'
}
]
export { sideOptions, sendOptions, fontOptions }
export { sideOptions, sendOptions, fontOptions, translateOptions }

0 comments on commit 24b20c6

Please sign in to comment.