Files
kiss-translator/src/apis/trans.js

941 lines
20 KiB
JavaScript
Raw Normal View History

import queryString from "query-string";
import {
OPT_TRANS_GOOGLE,
2025-07-02 13:38:30 +08:00
OPT_TRANS_GOOGLE_2,
OPT_TRANS_MICROSOFT,
OPT_TRANS_DEEPL,
OPT_TRANS_DEEPLFREE,
OPT_TRANS_DEEPLX,
2024-04-12 11:31:01 +08:00
OPT_TRANS_NIUTRANS,
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
2025-06-30 21:34:37 +08:00
OPT_TRANS_VOLCENGINE,
OPT_TRANS_OPENAI,
2024-04-28 16:58:09 +08:00
OPT_TRANS_OPENAI_2,
OPT_TRANS_OPENAI_3,
2023-12-21 14:15:14 +08:00
OPT_TRANS_GEMINI,
2025-07-02 21:54:18 +08:00
OPT_TRANS_GEMINI_2,
2024-09-23 18:22:19 +08:00
OPT_TRANS_CLAUDE,
2023-10-26 11:13:50 +08:00
OPT_TRANS_CLOUDFLAREAI,
2024-04-28 21:43:20 +08:00
OPT_TRANS_OLLAMA,
OPT_TRANS_OLLAMA_2,
OPT_TRANS_OLLAMA_3,
OPT_TRANS_OPENROUTER,
OPT_TRANS_CUSTOMIZE,
2024-04-10 13:37:16 +08:00
OPT_TRANS_CUSTOMIZE_2,
OPT_TRANS_CUSTOMIZE_3,
OPT_TRANS_CUSTOMIZE_4,
OPT_TRANS_CUSTOMIZE_5,
2025-09-03 20:43:07 +08:00
OPT_TRANS_CONTEXT,
2024-04-17 17:38:54 +08:00
INPUT_PLACE_FROM,
INPUT_PLACE_TO,
INPUT_PLACE_TEXT,
INPUT_PLACE_KEY,
2024-05-12 16:25:20 +08:00
INPUT_PLACE_MODEL,
} from "../config";
import { msAuth } from "../libs/auth";
import { genDeeplFree } from "./deepl";
import { genBaidu } from "./baidu";
2024-05-12 16:10:11 +08:00
import interpreter from "../libs/interpreter";
import { parseJsonObj, extractJson } from "../libs/utils";
import { kissLog } from "../libs/log";
import { fetchData } from "../libs/fetch";
2025-09-03 20:43:07 +08:00
import { getMsgHistory } from "./history";
2023-12-22 11:35:46 +08:00
const keyMap = new Map();
2024-04-20 14:01:34 +08:00
const urlMap = new Map();
2023-12-22 11:35:46 +08:00
2024-04-20 14:01:34 +08:00
// 轮询key/url
const keyPick = (translator, key = "", cacheMap) => {
2023-12-22 11:35:46 +08:00
const keys = key
2024-01-19 16:02:53 +08:00
.split(/\n|,/)
2023-12-22 11:35:46 +08:00
.map((item) => item.trim())
.filter(Boolean);
if (keys.length === 0) {
return "";
}
2024-04-20 14:01:34 +08:00
const preIndex = cacheMap.get(translator) ?? -1;
2024-01-04 09:40:03 +08:00
const curIndex = (preIndex + 1) % keys.length;
2024-04-20 14:01:34 +08:00
cacheMap.set(translator, curIndex);
2023-12-22 11:35:46 +08:00
2024-01-04 09:40:03 +08:00
return keys[curIndex];
2023-12-22 11:35:46 +08:00
};
const genSystemPrompt = ({ systemPrompt, from, to }) =>
systemPrompt
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to);
const genUserPrompt = ({ userPrompt, from, to, texts, docInfo }) => {
const prompt = JSON.stringify({
targetLanguage: to,
title: docInfo.title,
description: docInfo.description,
segments: texts.map((text, i) => ({ id: i, text })),
});
if (userPrompt.includes(INPUT_PLACE_TEXT)) {
return userPrompt
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, prompt);
}
return prompt;
};
2025-09-03 09:40:25 +08:00
const parseAIRes = (raw) => {
let data;
try {
const jsonString = extractJson(raw);
data = JSON.parse(jsonString);
} catch (err) {
2025-09-03 09:40:25 +08:00
kissLog(err, "parseAIRes");
data = { translations: [] };
}
if (!Array.isArray(data.translations)) {
data.translations = [];
}
return data.translations.map((item) => [item.text]);
};
const genGoogle = ({ texts, from, to, url, key }) => {
2025-06-26 11:13:51 +08:00
const params = {
client: "gtx",
dt: "t",
dj: 1,
ie: "UTF-8",
sl: from,
tl: to,
q: texts.join(" "),
2025-06-26 11:13:51 +08:00
};
const input = `${url}?${queryString.stringify(params)}`;
const init = {
headers: {
2025-06-26 11:13:51 +08:00
"Content-type": "application/json",
},
};
2025-06-26 11:13:51 +08:00
if (key) {
init.headers.Authorization = `Bearer ${key}`;
}
return [input, init];
};
const genGoogle2 = ({ texts, from, to, url, key }) => {
const body = JSON.stringify([[texts, from, to], "wt_lib"]);
const init = {
method: "POST",
headers: {
"Content-Type": "application/json+protobuf",
"X-Goog-API-Key": key,
},
body,
};
return [url, init];
};
const genMicrosoft = async ({ texts, from, to }) => {
const [token] = await msAuth();
const params = {
from,
to,
"api-version": "3.0",
};
2025-09-01 18:56:48 +08:00
const input = `https://api-edge.cognitive.microsofttranslator.com/translate?${queryString.stringify(params)}`;
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${token}`,
},
method: "POST",
body: JSON.stringify(texts.map((text) => ({ Text: text }))),
};
return [input, init];
};
const genDeepl = ({ texts, from, to, url, key }) => {
const data = {
text: texts,
target_lang: to,
source_lang: from,
// split_sentences: "0",
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `DeepL-Auth-Key ${key}`,
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
const genDeeplX = ({ texts, from, to, url, key }) => {
const data = {
text: texts.join(" "),
target_lang: to,
source_lang: from,
};
const init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
if (key) {
init.headers.Authorization = `Bearer ${key}`;
}
return [url, init];
};
const genNiuTrans = ({ texts, from, to, url, key, dictNo, memoryNo }) => {
2024-04-12 11:31:01 +08:00
const data = {
from,
to,
apikey: key,
src_text: texts.join(" "),
2024-04-12 11:31:01 +08:00
dictNo,
memoryNo,
};
const init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
const genTencent = ({ texts, from, to }) => {
const data = {
header: {
2025-06-27 20:03:58 +08:00
fn: "auto_translation",
client_key:
"browser-chrome-110.0.0-Mac OS-df4bd4c5-a65d-44b2-a40f-42f34f3535f2-1677486696487",
},
2025-06-27 20:03:58 +08:00
type: "plain",
model_category: "normal",
source: {
text_list: texts,
lang: from,
},
target: {
lang: to,
},
};
2025-09-01 18:56:48 +08:00
const input = "https://transmart.qq.com/api/imt";
const init = {
headers: {
"Content-Type": "application/json",
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
referer: "https://transmart.qq.com/zh-CN/index",
},
method: "POST",
body: JSON.stringify(data),
};
2025-09-01 18:56:48 +08:00
return [input, init];
};
const genVolcengine = ({ texts, from, to }) => {
2025-06-30 21:34:37 +08:00
const data = {
source_language: from,
target_language: to,
text: texts.join(" "),
2025-06-30 21:34:37 +08:00
};
2025-09-01 18:56:48 +08:00
const input = "https://translate.volcengine.com/crx/translate/v1";
2025-06-30 21:34:37 +08:00
const init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
2025-09-01 18:56:48 +08:00
return [input, init];
2025-06-30 21:34:37 +08:00
};
const genOpenAI = ({
texts,
from,
to,
url,
key,
2024-09-25 14:03:12 +08:00
systemPrompt,
2024-11-30 00:41:29 +08:00
userPrompt,
model,
temperature,
maxTokens,
2025-08-11 16:28:23 +08:00
customHeader,
2025-08-11 16:03:30 +08:00
customBody,
docInfo,
2025-09-03 20:43:07 +08:00
hisMsgs,
}) => {
systemPrompt = genSystemPrompt({ systemPrompt, from, to });
userPrompt = genUserPrompt({ userPrompt, from, to, texts, docInfo });
customHeader = parseJsonObj(customHeader);
customBody = parseJsonObj(customBody);
2025-08-11 12:12:03 +08:00
2025-09-03 20:43:07 +08:00
const userMsg = {
role: "user",
content: userPrompt,
};
const data = {
model,
messages: [
{
role: "system",
2024-09-25 14:03:12 +08:00
content: systemPrompt,
},
2025-09-03 20:43:07 +08:00
...hisMsgs,
userMsg,
],
temperature,
max_completion_tokens: maxTokens,
2025-08-11 16:03:30 +08:00
...customBody,
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${key}`, // OpenAI
"api-key": key, // Azure OpenAI
2025-08-11 16:28:23 +08:00
...customHeader,
},
method: "POST",
body: JSON.stringify(data),
};
2025-09-03 20:43:07 +08:00
return [url, init, userMsg];
};
const genGemini = ({
texts,
from,
to,
url,
key,
systemPrompt,
userPrompt,
model,
2025-07-02 16:37:57 +08:00
temperature,
maxTokens,
2025-08-11 16:28:23 +08:00
customHeader,
2025-08-11 16:03:30 +08:00
customBody,
docInfo,
2025-09-03 20:43:07 +08:00
hisMsgs,
}) => {
2024-05-12 16:25:20 +08:00
url = url
.replaceAll(INPUT_PLACE_MODEL, model)
.replaceAll(INPUT_PLACE_KEY, key);
systemPrompt = genSystemPrompt({ systemPrompt, from, to });
userPrompt = genUserPrompt({ userPrompt, from, to, texts, docInfo });
customHeader = parseJsonObj(customHeader);
customBody = parseJsonObj(customBody);
2025-08-11 12:12:03 +08:00
2025-09-03 20:43:07 +08:00
const userMsg = { role: "user", parts: [{ text: userPrompt }] };
2023-12-21 14:15:14 +08:00
const data = {
2024-11-30 00:41:29 +08:00
system_instruction: {
parts: {
text: systemPrompt,
},
2024-11-30 00:41:29 +08:00
},
2025-09-03 20:43:07 +08:00
contents: [...hisMsgs, userMsg],
2025-07-02 16:37:57 +08:00
generationConfig: {
maxOutputTokens: maxTokens,
temperature,
// topP: 0.8,
// topK: 10,
},
2025-09-03 12:04:56 +08:00
// thinkingConfig: {
// thinkingBudget: 0,
// },
safetySettings: [
{
category: "HARM_CATEGORY_HARASSMENT",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_HATE_SPEECH",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
threshold: "BLOCK_NONE",
},
],
2025-08-11 16:03:30 +08:00
...customBody,
2023-12-21 14:15:14 +08:00
};
const init = {
headers: {
"Content-type": "application/json",
2025-08-11 16:28:23 +08:00
...customHeader,
2023-12-21 14:15:14 +08:00
},
method: "POST",
body: JSON.stringify(data),
};
2025-09-03 20:43:07 +08:00
return [url, init, userMsg];
2023-12-21 14:15:14 +08:00
};
2025-07-02 21:54:18 +08:00
const genGemini2 = ({
texts,
2025-07-02 21:54:18 +08:00
from,
to,
url,
key,
systemPrompt,
userPrompt,
model,
temperature,
2025-07-23 20:03:54 +08:00
maxTokens,
2025-08-11 16:28:23 +08:00
customHeader,
2025-08-11 16:03:30 +08:00
customBody,
docInfo,
2025-09-03 20:43:07 +08:00
hisMsgs,
2025-07-02 21:54:18 +08:00
}) => {
systemPrompt = genSystemPrompt({ systemPrompt, from, to });
userPrompt = genUserPrompt({ userPrompt, from, to, texts, docInfo });
customHeader = parseJsonObj(customHeader);
customBody = parseJsonObj(customBody);
2025-08-11 12:12:03 +08:00
2025-09-03 20:43:07 +08:00
const userMsg = {
role: "user",
content: userPrompt,
};
2025-07-02 21:54:18 +08:00
const data = {
model,
messages: [
{
role: "system",
content: systemPrompt,
},
2025-09-03 20:43:07 +08:00
...hisMsgs,
userMsg,
2025-07-02 21:54:18 +08:00
],
temperature,
2025-07-23 20:03:54 +08:00
max_tokens: maxTokens,
2025-08-11 16:03:30 +08:00
...customBody,
2025-07-02 21:54:18 +08:00
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${key}`,
2025-08-11 16:28:23 +08:00
...customHeader,
2025-07-02 21:54:18 +08:00
},
method: "POST",
body: JSON.stringify(data),
};
2025-09-03 20:43:07 +08:00
return [url, init, userMsg];
2025-07-02 21:54:18 +08:00
};
2024-09-23 18:22:19 +08:00
const genClaude = ({
texts,
2024-09-23 18:22:19 +08:00
from,
to,
url,
key,
systemPrompt,
2024-11-30 00:41:29 +08:00
userPrompt,
2024-09-23 18:22:19 +08:00
model,
temperature,
maxTokens,
2025-08-11 16:28:23 +08:00
customHeader,
2025-08-11 16:03:30 +08:00
customBody,
docInfo,
2025-09-03 20:43:07 +08:00
hisMsgs,
2024-09-23 18:22:19 +08:00
}) => {
systemPrompt = genSystemPrompt({ systemPrompt, from, to });
userPrompt = genUserPrompt({ userPrompt, from, to, texts, docInfo });
customHeader = parseJsonObj(customHeader);
customBody = parseJsonObj(customBody);
2025-08-11 12:12:03 +08:00
2025-09-03 20:43:07 +08:00
const userMsg = {
role: "user",
content: userPrompt,
};
2024-09-23 18:22:19 +08:00
const data = {
model,
system: systemPrompt,
2025-09-03 20:43:07 +08:00
messages: [...hisMsgs, userMsg],
2024-09-23 18:22:19 +08:00
temperature,
max_tokens: maxTokens,
2025-08-11 16:03:30 +08:00
...customBody,
2024-09-23 18:22:19 +08:00
};
const init = {
headers: {
"Content-type": "application/json",
"anthropic-version": "2023-06-01",
2025-08-22 08:03:10 +08:00
"anthropic-dangerous-direct-browser-access": "true",
2024-09-23 18:22:19 +08:00
"x-api-key": key,
2025-08-11 16:28:23 +08:00
...customHeader,
2024-09-23 18:22:19 +08:00
},
method: "POST",
body: JSON.stringify(data),
};
2025-09-03 20:43:07 +08:00
return [url, init, userMsg];
2024-09-23 18:22:19 +08:00
};
const genOpenRouter = ({
texts,
from,
to,
url,
key,
systemPrompt,
userPrompt,
model,
temperature,
maxTokens,
customHeader,
customBody,
docInfo,
2025-09-03 20:43:07 +08:00
hisMsgs,
}) => {
systemPrompt = genSystemPrompt({ systemPrompt, from, to });
userPrompt = genUserPrompt({ userPrompt, from, to, texts, docInfo });
customHeader = parseJsonObj(customHeader);
customBody = parseJsonObj(customBody);
2025-09-03 20:43:07 +08:00
const userMsg = {
role: "user",
content: userPrompt,
};
const data = {
model,
messages: [
{
role: "system",
content: systemPrompt,
},
2025-09-03 20:43:07 +08:00
...hisMsgs,
userMsg,
],
temperature,
max_tokens: maxTokens,
...customBody,
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${key}`,
...customHeader,
},
method: "POST",
body: JSON.stringify(data),
};
2025-09-03 20:43:07 +08:00
return [url, init, userMsg];
};
const genOllama = ({
texts,
from,
to,
think,
url,
key,
systemPrompt,
userPrompt,
model,
2025-09-03 14:02:55 +08:00
temperature,
maxTokens,
2025-08-11 16:28:23 +08:00
customHeader,
2025-08-11 16:03:30 +08:00
customBody,
docInfo,
2025-09-03 20:43:07 +08:00
hisMsgs,
}) => {
systemPrompt = genSystemPrompt({ systemPrompt, from, to });
userPrompt = genUserPrompt({ userPrompt, from, to, texts, docInfo });
customHeader = parseJsonObj(customHeader);
customBody = parseJsonObj(customBody);
2025-08-11 12:12:03 +08:00
2025-09-03 20:43:07 +08:00
const userMsg = {
role: "user",
content: userPrompt,
};
2024-04-28 21:43:20 +08:00
const data = {
model,
2025-09-03 14:02:55 +08:00
messages: [
{
role: "system",
content: systemPrompt,
},
2025-09-03 20:43:07 +08:00
...hisMsgs,
userMsg,
2025-09-03 14:02:55 +08:00
],
temperature,
max_tokens: maxTokens,
think,
2024-04-28 21:43:20 +08:00
stream: false,
2025-08-11 16:03:30 +08:00
...customBody,
2024-04-28 21:43:20 +08:00
};
const init = {
headers: {
"Content-type": "application/json",
2025-08-11 16:28:23 +08:00
...customHeader,
2024-04-28 21:43:20 +08:00
},
method: "POST",
body: JSON.stringify(data),
};
if (key) {
init.headers.Authorization = `Bearer ${key}`;
}
2025-09-03 20:43:07 +08:00
return [url, init, userMsg];
2024-04-28 21:43:20 +08:00
};
const genCloudflareAI = ({ texts, from, to, url, key }) => {
2023-10-26 11:13:50 +08:00
const data = {
text: texts.join(" "),
2023-10-26 11:13:50 +08:00
source_lang: from,
target_lang: to,
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${key}`,
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
2025-09-03 20:43:07 +08:00
const genCustom = ({
texts,
from,
to,
url,
key,
reqHook,
docInfo,
hisMsgs,
}) => {
2024-05-12 16:10:11 +08:00
if (reqHook?.trim()) {
interpreter.run(`exports.reqHook = ${reqHook}`);
2025-09-03 20:43:07 +08:00
return interpreter.exports.reqHook({
texts,
from,
to,
url,
key,
docInfo,
hisMsgs,
});
2024-05-12 16:10:11 +08:00
}
const data = { texts, from, to };
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${key}`,
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
2023-10-26 11:13:50 +08:00
/**
* 构造翻译接口请求参数
2023-10-26 11:13:50 +08:00
* @param {*}
* @returns
*/
export const genTransReq = (translator, args) => {
2023-12-22 11:35:46 +08:00
switch (translator) {
case OPT_TRANS_DEEPL:
case OPT_TRANS_OPENAI:
2024-04-28 16:58:09 +08:00
case OPT_TRANS_OPENAI_2:
case OPT_TRANS_OPENAI_3:
2023-12-22 11:35:46 +08:00
case OPT_TRANS_GEMINI:
2025-07-02 21:54:18 +08:00
case OPT_TRANS_GEMINI_2:
2024-09-23 18:22:19 +08:00
case OPT_TRANS_CLAUDE:
2023-12-22 11:35:46 +08:00
case OPT_TRANS_CLOUDFLAREAI:
2024-04-28 21:43:20 +08:00
case OPT_TRANS_OLLAMA:
case OPT_TRANS_OLLAMA_2:
case OPT_TRANS_OLLAMA_3:
case OPT_TRANS_OPENROUTER:
2024-04-12 11:31:01 +08:00
case OPT_TRANS_NIUTRANS:
case OPT_TRANS_CUSTOMIZE:
case OPT_TRANS_CUSTOMIZE_2:
case OPT_TRANS_CUSTOMIZE_3:
case OPT_TRANS_CUSTOMIZE_4:
case OPT_TRANS_CUSTOMIZE_5:
2024-04-20 14:01:34 +08:00
args.key = keyPick(translator, args.key, keyMap);
break;
case OPT_TRANS_DEEPLX:
args.url = keyPick(translator, args.url, urlMap);
2023-12-22 11:35:46 +08:00
break;
default:
}
switch (translator) {
case OPT_TRANS_GOOGLE:
return genGoogle(args);
2025-07-02 13:38:30 +08:00
case OPT_TRANS_GOOGLE_2:
return genGoogle2(args);
case OPT_TRANS_MICROSOFT:
return genMicrosoft(args);
case OPT_TRANS_DEEPL:
return genDeepl(args);
case OPT_TRANS_DEEPLFREE:
return genDeeplFree(args);
case OPT_TRANS_DEEPLX:
return genDeeplX(args);
2024-04-12 11:31:01 +08:00
case OPT_TRANS_NIUTRANS:
return genNiuTrans(args);
case OPT_TRANS_BAIDU:
return genBaidu(args);
case OPT_TRANS_TENCENT:
return genTencent(args);
2025-06-30 21:34:37 +08:00
case OPT_TRANS_VOLCENGINE:
return genVolcengine(args);
case OPT_TRANS_OPENAI:
2024-04-28 16:58:09 +08:00
case OPT_TRANS_OPENAI_2:
case OPT_TRANS_OPENAI_3:
2023-10-26 11:13:50 +08:00
return genOpenAI(args);
2023-12-21 14:15:14 +08:00
case OPT_TRANS_GEMINI:
return genGemini(args);
2025-07-02 21:54:18 +08:00
case OPT_TRANS_GEMINI_2:
return genGemini2(args);
2024-09-23 18:22:19 +08:00
case OPT_TRANS_CLAUDE:
return genClaude(args);
2023-10-26 11:13:50 +08:00
case OPT_TRANS_CLOUDFLAREAI:
return genCloudflareAI(args);
2024-04-28 21:43:20 +08:00
case OPT_TRANS_OLLAMA:
case OPT_TRANS_OLLAMA_2:
case OPT_TRANS_OLLAMA_3:
return genOllama(args);
case OPT_TRANS_OPENROUTER:
return genOpenRouter(args);
case OPT_TRANS_CUSTOMIZE:
2024-04-10 13:37:16 +08:00
case OPT_TRANS_CUSTOMIZE_2:
case OPT_TRANS_CUSTOMIZE_3:
case OPT_TRANS_CUSTOMIZE_4:
case OPT_TRANS_CUSTOMIZE_5:
return genCustom(args);
default:
throw new Error(`[trans] translator: ${translator} not support`);
}
};
/**
* 解析翻译接口返回数据
* @param {*} translator
* @param {*} res
* @param {*} param3
* @returns
*/
export const parseTransRes = (
translator,
res,
2025-09-03 20:43:07 +08:00
{ texts, from, to, resHook, thinkIgnore, history, userMsg }
) => {
2025-09-03 20:43:07 +08:00
let modelMsg = "";
switch (translator) {
case OPT_TRANS_GOOGLE:
return [[res?.sentences?.map((item) => item.trans).join(" "), res?.src]];
case OPT_TRANS_GOOGLE_2:
return res?.[0]?.map((_, i) => [res?.[0]?.[i], res?.[1]?.[i]]);
case OPT_TRANS_MICROSOFT:
return res?.map((item) => [
item.translations.map((item) => item.text).join(" "),
item.detectedLanguage.language,
]);
case OPT_TRANS_DEEPL:
return res?.translations?.map((item) => [
item.text,
item.detected_source_language,
]);
case OPT_TRANS_DEEPLFREE:
return [
[
res?.result?.texts?.map((item) => item.text).join(" "),
res?.result?.lang,
],
];
case OPT_TRANS_DEEPLX:
return [[res?.data, res?.source_lang]];
case OPT_TRANS_NIUTRANS:
const json = JSON.parse(res);
if (json.error_msg) {
throw new Error(json.error_msg);
}
return [[json.tgt_text, json.from]];
case OPT_TRANS_BAIDU:
if (res.type === 1) {
return [
[
Object.keys(JSON.parse(res.result).content[0].mean[0].cont)[0],
res.from,
],
];
} else if (res.type === 2) {
return [[res.data.map((item) => item.dst).join(" "), res.from]];
}
break;
case OPT_TRANS_TENCENT:
return res?.auto_translation?.map((text) => [text, res?.src_lang]);
case OPT_TRANS_VOLCENGINE:
return new Map([[0, [res?.translation, res?.detected_language]]]);
case OPT_TRANS_OPENAI:
case OPT_TRANS_OPENAI_2:
case OPT_TRANS_OPENAI_3:
case OPT_TRANS_GEMINI_2:
case OPT_TRANS_OPENROUTER:
2025-09-03 20:43:07 +08:00
modelMsg = res?.choices?.[0]?.message;
if (history && userMsg && modelMsg) {
history.add(userMsg, {
role: modelMsg.role,
content: modelMsg.content,
});
}
2025-09-03 09:40:25 +08:00
return parseAIRes(res?.choices?.[0]?.message?.content ?? "");
case OPT_TRANS_GEMINI:
2025-09-03 20:43:07 +08:00
modelMsg = res?.candidates?.[0]?.content;
if (history && userMsg && modelMsg) {
history.add(userMsg, modelMsg);
}
2025-09-03 09:40:25 +08:00
return parseAIRes(res?.candidates?.[0]?.content?.parts?.[0]?.text ?? "");
case OPT_TRANS_CLAUDE:
2025-09-03 20:43:07 +08:00
modelMsg = { role: res?.role, content: res?.content?.text };
if (history && userMsg && modelMsg) {
history.add(userMsg, {
role: modelMsg.role,
content: modelMsg.content,
});
}
2025-09-03 09:40:25 +08:00
return parseAIRes(res?.content?.[0]?.text ?? "");
case OPT_TRANS_CLOUDFLAREAI:
return [[res?.result?.translated_text]];
case OPT_TRANS_OLLAMA:
case OPT_TRANS_OLLAMA_2:
case OPT_TRANS_OLLAMA_3:
2025-09-03 20:43:07 +08:00
modelMsg = res?.choices?.[0]?.message;
2025-09-03 12:04:56 +08:00
const deepModels = thinkIgnore.split(",").filter((model) => model.trim());
if (deepModels.some((model) => res?.model?.startsWith(model))) {
2025-09-03 20:43:07 +08:00
modelMsg?.content.replace(/<think>[\s\S]*<\/think>/i, "");
2025-09-03 12:04:56 +08:00
}
2025-09-03 20:43:07 +08:00
if (history && userMsg && modelMsg) {
history.add(userMsg, {
role: modelMsg.role,
content: modelMsg.content,
});
}
return parseAIRes(modelMsg?.content);
case OPT_TRANS_CUSTOMIZE:
case OPT_TRANS_CUSTOMIZE_2:
case OPT_TRANS_CUSTOMIZE_3:
case OPT_TRANS_CUSTOMIZE_4:
case OPT_TRANS_CUSTOMIZE_5:
if (resHook?.trim()) {
interpreter.run(`exports.resHook = ${resHook}`);
2025-09-03 20:43:07 +08:00
if (history) {
const [translations, modelMsg] = interpreter.exports.resHook({
res,
texts,
from,
to,
});
userMsg && modelMsg && history.add(userMsg, modelMsg);
return translations;
} else {
return interpreter.exports.resHook({ res, texts, from, to });
}
} else {
return res?.map((item) => [item.text, item.src]);
}
default:
}
return [];
};
/**
* 发送翻译请求并解析
* @param {*} param0
* @returns
*/
2025-09-03 13:05:41 +08:00
export const handleTranslate = async ({
translator,
texts,
from,
to,
docInfo,
apiSetting,
usePool,
}) => {
2025-09-03 20:43:07 +08:00
let history = null;
let hisMsgs = [];
if (apiSetting.useContext && OPT_TRANS_CONTEXT.has(translator)) {
history = getMsgHistory(translator, apiSetting.contextSize);
hisMsgs = history.getAll();
}
const [input, init, userMsg] = await genTransReq(translator, {
texts,
from,
to,
docInfo,
2025-09-03 20:43:07 +08:00
hisMsgs,
...apiSetting,
});
const res = await fetchData(input, init, {
useCache: false,
usePool,
fetchInterval: apiSetting.fetchInterval,
fetchLimit: apiSetting.fetchLimit,
httpTimeout: apiSetting.httpTimeout,
});
if (!res) {
throw new Error("tranlate got empty response");
}
return parseTransRes(translator, res, {
texts,
from,
to,
2025-09-03 20:43:07 +08:00
history,
userMsg,
...apiSetting,
});
};