-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.js
More file actions
38 lines (32 loc) · 1.06 KB
/
Copy pathtranslate.js
File metadata and controls
38 lines (32 loc) · 1.06 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
const axios = require("axios");
const text = popclip.input.text;
const { apiKey, apiBase, model, promptZhToEn, promptEnToZh, temperature, timeout } = popclip.options;
if (!apiKey) {
return "❌ 请配置 API Key";
}
function isChinese(str) {
const chineseChars = (str.match(/[\u4e00-\u9fa5]/g) || []).length;
const totalChars = str.replace(/\s/g, "").length;
return totalChars > 0 && chineseChars / totalChars > 0.3;
}
const isZh = isChinese(text);
const promptTemplate = isZh ? promptZhToEn : promptEnToZh;
const prompt = `${promptTemplate}\n\n${text}`;
const res = await axios.post(apiBase, {
model: model,
messages: [{ role: "user", content: prompt }],
temperature: parseFloat(temperature) || 0.3
}, {
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
timeout: parseInt(timeout) || 30000
});
const result = res.data.choices[0].message.content.trim();
// 如果结果包含换行,使用 large 样式显示
if (result.includes('\n')) {
popclip.showText(result, { style: 'large' });
return null;
}
return result;