Skip to content

Commit

Permalink
feat(service): ✨ 新增翻译服务api (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
nongyehong authored Dec 28, 2024
1 parent a1641e9 commit fadbf2b
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 57 deletions.
6 changes: 6 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ VITE_APP_TITLE="HuLa—IM"
VITE_APP_NAME="HuLa"
# gitee token
VITE_GITEE_TOKEN="0312a213a6b6882beb96f487e75661a6"
# 有道云翻译API
VITE_YOUDAO_APP_KEY=
VITE_YOUDAO_APP_SECRET=
# 腾讯翻译API
VITE_TENCENT_API_KEY=
VITE_TENCENT_SECRET_ID=
66 changes: 58 additions & 8 deletions .github/workflows/pr-chatbot-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ jobs:
id: diff
run: |
git fetch origin ${{ github.event.pull_request.base.sha }}
DIFF=$(git diff ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
echo "DIFF<<EOF" >> $GITHUB_ENV
echo "$DIFF" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
# 只获取特定文件类型的变更
DIFF=$(git diff ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- \
'src/**/*.vue' \
'src/**/*.ts' \
'src/**/*.tsx' \
'src-tauri/**/*.rs')
# 如果没有相关文件变更,设置一个提示信息
if [ -z "$DIFF" ]; then
echo "NO_CHANGES=true" >> $GITHUB_ENV
echo "DIFF=没有检测到相关文件的变更。" >> $GITHUB_ENV
else
echo "NO_CHANGES=false" >> $GITHUB_ENV
echo "DIFF<<EOF" >> $GITHUB_ENV
echo "$DIFF" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
fi
- name: Setup Node.js
uses: actions/setup-node@v4
Expand All @@ -35,15 +48,29 @@ jobs:
uses: pnpm/action-setup@v2
with:
version: 9
run_install: false

- name: Install OpenAI SDK
- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: |
pnpm config set registry https://registry.npmmirror.com/
pnpm install
pnpm add openai
- name: Analyze PR
id: analyze
if: env.NO_CHANGES != 'true'
uses: actions/github-script@v7
env:
DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }}
Expand All @@ -63,7 +90,13 @@ jobs:
model: "qwen-plus",
messages: [{
role: "system",
content: "你是一个代码审查助手,请用中文分析以下代码变更。"
content: "你是一个代码审查助手。请用中文分析以下代码变更,重点关注:\n" +
"1. 代码逻辑的改动\n" +
"2. 潜在的问题或优化空间\n" +
"3. TypeScript 类型定义的准确性\n" +
"4. Vue 组件的性能影响\n" +
"5. Rust 代码的安全性和性能\n" +
"请用中文简明扼要地总结。"
}, {
role: "user",
content: `请分析以下代码变更并总结主要改动:\n\n${diff}`
Expand All @@ -88,4 +121,21 @@ jobs:
});
} catch (error) {
core.setFailed(`分析失败: ${error.message}`);
}
}
- name: Skip Analysis Comment
if: env.NO_CHANGES == 'true'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## PR 代码分析
本次变更不包含需要分析的代码文件(src 目录下的 .vue/.ts/.tsx 文件或 src-tauri 目录下的 .rs 文件)。
---
*这是自动生成的通知。*`
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@
"@tauri-apps/plugin-sql": "^2.0.1",
"@tauri-apps/plugin-updater": "~2",
"colorthief": "^2.6.0",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.11",
"dompurify": "^3.2.3",
"grapheme-splitter": "^1.0.4",
"hula": "file:",
"hula-emojis": "^1.2.1",
"lodash-es": "^4.17.21",
"mitt": "^3.0.1",
Expand All @@ -85,6 +85,7 @@
"@release-it/conventional-changelog": "8.0.2",
"@rollup/plugin-terser": "^0.4.4",
"@tauri-apps/cli": "2.0.4",
"@types/crypto-js": "^4.2.2",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.14.14",
"@typescript-eslint/eslint-plugin": "7.1.0",
Expand Down
61 changes: 16 additions & 45 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/icon.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/components/rightBox/chatBox/ChatMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ const handleScroll = () => {
const scrollHeight = container.scrollHeight
// 获取容器的可视区域高度
const clientHeight = container.clientHeight
// ��算距离底部的距离
// 计算距离底部的距离
const distanceFromBottom = scrollHeight - scrollTop.value - clientHeight
// 存储 requestAnimationFrame 的返回值
Expand Down Expand Up @@ -737,7 +737,7 @@ const canReEdit = computed(() => (msgId: number) => {
const message = chatStore.getMessage(msgId)
if (!recalledMsg || !message) return false
// 只需要判断是否是当前用户的消��,时间检查已经在 getRecalledMessage 中处理
// 只需要判断是否是当前用户的消息,时间检查已经在 getRecalledMessage 中处理
return message.fromUser.uid === userUid.value
})
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/useChatMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useUserStore } from '@/stores/user'
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'

export const useChatMain = (activeItem?: SessionItem) => {
const { removeTag, openMsgSession, userUid } = useCommon()
Expand Down Expand Up @@ -67,6 +68,16 @@ export const useChatMain = (activeItem?: SessionItem) => {
icon: 'share',
click: () => {}
},
{
label: '翻译',
icon: 'translate',
click: async (item: MessageType) => {
const content = item.message.body.content
const translatedText = await translateText(content, 'tencent')
console.log('原文:', content)
console.log('译文:', translatedText)
}
},
{ label: '收藏', icon: 'collection-files' },
{
label: '回复',
Expand Down
Loading

0 comments on commit fadbf2b

Please sign in to comment.