feat: move fetch setting to apis

This commit is contained in:
Gabe Yuan
2024-03-21 15:07:50 +08:00
parent ac921cd5a0
commit 0eeb9c2fbf
4 changed files with 98 additions and 37 deletions

View File

@@ -402,34 +402,64 @@ export const DEFAULT_TRANS_APIS = {
[OPT_TRANS_GOOGLE]: { [OPT_TRANS_GOOGLE]: {
url: "https://translate.googleapis.com/translate_a/single", url: "https://translate.googleapis.com/translate_a/single",
key: "", key: "",
fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量
fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间
},
[OPT_TRANS_MICROSOFT]: {
fetchLimit: DEFAULT_FETCH_LIMIT,
fetchInterval: DEFAULT_FETCH_INTERVAL,
},
[OPT_TRANS_BAIDU]: {
fetchLimit: DEFAULT_FETCH_LIMIT,
fetchInterval: DEFAULT_FETCH_INTERVAL,
},
[OPT_TRANS_TENCENT]: {
fetchLimit: DEFAULT_FETCH_LIMIT,
fetchInterval: DEFAULT_FETCH_INTERVAL,
}, },
[OPT_TRANS_DEEPL]: { [OPT_TRANS_DEEPL]: {
url: "https://api-free.deepl.com/v2/translate", url: "https://api-free.deepl.com/v2/translate",
key: "", key: "",
fetchLimit: 1,
fetchInterval: 500,
},
[OPT_TRANS_DEEPLFREE]: {
fetchLimit: 1,
fetchInterval: 500,
}, },
[OPT_TRANS_DEEPLX]: { [OPT_TRANS_DEEPLX]: {
url: "http://localhost:1188/translate", url: "http://localhost:1188/translate",
key: "", key: "",
fetchLimit: 1,
fetchInterval: 500,
}, },
[OPT_TRANS_OPENAI]: { [OPT_TRANS_OPENAI]: {
url: "https://api.openai.com/v1/chat/completions", url: "https://api.openai.com/v1/chat/completions",
key: "", key: "",
model: "gpt-4", model: "gpt-4",
prompt: `You will be provided with a sentence in ${PROMPT_PLACE_FROM}, and your task is to translate it into ${PROMPT_PLACE_TO}.`, prompt: `You will be provided with a sentence in ${PROMPT_PLACE_FROM}, and your task is to translate it into ${PROMPT_PLACE_TO}.`,
fetchLimit: 1,
fetchInterval: 500,
}, },
[OPT_TRANS_GEMINI]: { [OPT_TRANS_GEMINI]: {
url: "https://generativelanguage.googleapis.com/v1/models", url: "https://generativelanguage.googleapis.com/v1/models",
key: "", key: "",
model: "gemini-pro", model: "gemini-pro",
prompt: `Translate the following text from ${PROMPT_PLACE_FROM} to ${PROMPT_PLACE_TO}:\n\n${PROMPT_PLACE_TEXT}`, prompt: `Translate the following text from ${PROMPT_PLACE_FROM} to ${PROMPT_PLACE_TO}:\n\n${PROMPT_PLACE_TEXT}`,
fetchLimit: 1,
fetchInterval: 500,
}, },
[OPT_TRANS_CLOUDFLAREAI]: { [OPT_TRANS_CLOUDFLAREAI]: {
url: "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/m2m100-1.2b", url: "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/m2m100-1.2b",
key: "", key: "",
fetchLimit: 1,
fetchInterval: 500,
}, },
[OPT_TRANS_CUSTOMIZE]: { [OPT_TRANS_CUSTOMIZE]: {
url: "", url: "",
key: "", key: "",
fetchLimit: DEFAULT_FETCH_LIMIT,
fetchInterval: DEFAULT_FETCH_INTERVAL,
}, },
}; };
@@ -459,8 +489,8 @@ export const DEFAULT_BLACKLIST = [
export const DEFAULT_SETTING = { export const DEFAULT_SETTING = {
darkMode: false, // 深色模式 darkMode: false, // 深色模式
uiLang: "en", // 界面语言 uiLang: "en", // 界面语言
fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量 // fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量(移至transApis作废)
fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间 // fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间(移至transApis作废)
minLength: TRANS_MIN_LENGTH, minLength: TRANS_MIN_LENGTH,
maxLength: TRANS_MAX_LENGTH, maxLength: TRANS_MAX_LENGTH,
newlineLength: TRANS_NEWLINE_LENGTH, newlineLength: TRANS_NEWLINE_LENGTH,

View File

@@ -13,6 +13,8 @@ import {
OPT_TIMING_PAGEOPEN, OPT_TIMING_PAGEOPEN,
OPT_TIMING_MOUSEOVER, OPT_TIMING_MOUSEOVER,
DEFAULT_TRANS_APIS, DEFAULT_TRANS_APIS,
DEFAULT_FETCH_LIMIT,
DEFAULT_FETCH_INTERVAL,
} from "../config"; } from "../config";
import Content from "../views/Content"; import Content from "../views/Content";
import { updateFetchPool, clearFetchPool } from "./fetch"; import { updateFetchPool, clearFetchPool } from "./fetch";
@@ -104,9 +106,19 @@ export class Translator {
}; };
}; };
constructor(rule, setting) { _updatePool(translator) {
const { fetchInterval, fetchLimit } = setting; if (!translator) {
return;
}
const {
fetchInterval = DEFAULT_FETCH_INTERVAL,
fetchLimit = DEFAULT_FETCH_LIMIT,
} = this._setting.transApis[translator] || {};
updateFetchPool(fetchInterval, fetchLimit); updateFetchPool(fetchInterval, fetchLimit);
}
constructor(rule, setting) {
this._overrideAttachShadow(); this._overrideAttachShadow();
this._setting = setting; this._setting = setting;
@@ -120,6 +132,8 @@ export class Translator {
.map((item) => item.split(",").map((item) => item.trim())) .map((item) => item.split(",").map((item) => item.trim()))
.filter(([term]) => Boolean(term)); .filter(([term]) => Boolean(term));
this._updatePool(rule.translator);
if (rule.transOpen === "true") { if (rule.transOpen === "true") {
this._register(); this._register();
} }
@@ -156,6 +170,7 @@ export class Translator {
updateRule = (obj) => { updateRule = (obj) => {
this.rule = { ...this.rule, ...obj }; this.rule = { ...this.rule, ...obj };
this._updatePool(obj.translator);
}; };
toggle = () => { toggle = () => {

View File

@@ -14,6 +14,8 @@ import {
OPT_TRANS_CLOUDFLAREAI, OPT_TRANS_CLOUDFLAREAI,
OPT_TRANS_CUSTOMIZE, OPT_TRANS_CUSTOMIZE,
URL_KISS_PROXY, URL_KISS_PROXY,
DEFAULT_FETCH_LIMIT,
DEFAULT_FETCH_INTERVAL,
} from "../../config"; } from "../../config";
import { useState } from "react"; import { useState } from "react";
import { useI18n } from "../../hooks/I18n"; import { useI18n } from "../../hooks/I18n";
@@ -28,6 +30,7 @@ import { useApi } from "../../hooks/Api";
import { apiTranslate } from "../../apis"; import { apiTranslate } from "../../apis";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
import Link from "@mui/material/Link"; import Link from "@mui/material/Link";
import { limitNumber } from "../../libs/utils";
function TestButton({ translator, api }) { function TestButton({ translator, api }) {
const i18n = useI18n(); const i18n = useI18n();
@@ -88,10 +91,26 @@ function TestButton({ translator, api }) {
function ApiFields({ translator }) { function ApiFields({ translator }) {
const i18n = useI18n(); const i18n = useI18n();
const { api, updateApi, resetApi } = useApi(translator); const { api, updateApi, resetApi } = useApi(translator);
const { url = "", key = "", model = "", prompt = "" } = api; const {
url = "",
key = "",
model = "",
prompt = "",
fetchLimit = DEFAULT_FETCH_LIMIT,
fetchInterval = DEFAULT_FETCH_INTERVAL,
} = api;
const handleChange = (e) => { const handleChange = (e) => {
const { name, value } = e.target; let { name, value } = e.target;
switch (name) {
case "fetchLimit":
value = limitNumber(value, 1, 100);
break;
case "fetchInterval":
value = limitNumber(value, 0, 5000);
break;
default:
}
updateApi({ updateApi({
[name]: value, [name]: value,
}); });
@@ -137,6 +156,7 @@ function ApiFields({ translator }) {
/> />
</> </>
)} )}
{(translator === OPT_TRANS_OPENAI || translator === OPT_TRANS_GEMINI) && ( {(translator === OPT_TRANS_OPENAI || translator === OPT_TRANS_GEMINI) && (
<> <>
<TextField <TextField
@@ -157,19 +177,35 @@ function ApiFields({ translator }) {
</> </>
)} )}
<TextField
size="small"
label={i18n("fetch_limit")}
type="number"
name="fetchLimit"
value={fetchLimit}
onChange={handleChange}
/>
<TextField
size="small"
label={i18n("fetch_interval")}
type="number"
name="fetchInterval"
value={fetchInterval}
onChange={handleChange}
/>
<Stack direction="row" spacing={2}> <Stack direction="row" spacing={2}>
<TestButton translator={translator} api={api} /> <TestButton translator={translator} api={api} />
{!buildinTranslators.includes(translator) && ( <Button
<Button size="small"
size="small" variant="outlined"
variant="outlined" onClick={() => {
onClick={() => { resetApi();
resetApi(); }}
}} >
> {i18n("restore_default")}
{i18n("restore_default")} </Button>
</Button>
)}
</Stack> </Stack>
{translator === OPT_TRANS_CUSTOMIZE && ( {translator === OPT_TRANS_CUSTOMIZE && (

View File

@@ -89,8 +89,6 @@ export default function Settings() {
const { const {
uiLang, uiLang,
fetchLimit,
fetchInterval,
minLength, minLength,
maxLength, maxLength,
clearCache, clearCache,
@@ -121,24 +119,6 @@ export default function Settings() {
</Select> </Select>
</FormControl> </FormControl>
<TextField
size="small"
label={i18n("fetch_limit")}
type="number"
name="fetchLimit"
defaultValue={fetchLimit}
onChange={handleChange}
/>
<TextField
size="small"
label={i18n("fetch_interval")}
type="number"
name="fetchInterval"
defaultValue={fetchInterval}
onChange={handleChange}
/>
<TextField <TextField
size="small" size="small"
label={i18n("translate_interval")} label={i18n("translate_interval")}