feat: custom API name
This commit is contained in:
@@ -72,7 +72,7 @@ function runtimeListener(translator) {
|
|||||||
default:
|
default:
|
||||||
return { error: `message action is unavailable: ${action}` };
|
return { error: `message action is unavailable: ${action}` };
|
||||||
}
|
}
|
||||||
return { data: translator.rule };
|
return { rule: translator.rule, setting: translator.setting };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -954,4 +954,12 @@ export const I18N = {
|
|||||||
zh: `英文词典`,
|
zh: `英文词典`,
|
||||||
en: `English Dictionary`,
|
en: `English Dictionary`,
|
||||||
},
|
},
|
||||||
|
api_name: {
|
||||||
|
zh: `接口名称`,
|
||||||
|
en: `API Name`,
|
||||||
|
},
|
||||||
|
is_disabled: {
|
||||||
|
zh: `是否禁用`,
|
||||||
|
en: `Is Disabled`,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -550,6 +550,8 @@ const defaultOpenaiApi = {
|
|||||||
maxTokens: 256,
|
maxTokens: 256,
|
||||||
fetchLimit: 1,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: "",
|
||||||
|
isDisabled: false,
|
||||||
};
|
};
|
||||||
const defaultOllamaApi = {
|
const defaultOllamaApi = {
|
||||||
url: "http://localhost:11434/api/generate",
|
url: "http://localhost:11434/api/generate",
|
||||||
@@ -561,6 +563,8 @@ const defaultOllamaApi = {
|
|||||||
thinkIgnore:`qwen3,deepseek-r1`,
|
thinkIgnore:`qwen3,deepseek-r1`,
|
||||||
fetchLimit: 1,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: "",
|
||||||
|
isDisabled: false,
|
||||||
};
|
};
|
||||||
export const DEFAULT_TRANS_APIS = {
|
export const DEFAULT_TRANS_APIS = {
|
||||||
[OPT_TRANS_GOOGLE]: {
|
[OPT_TRANS_GOOGLE]: {
|
||||||
@@ -568,44 +572,62 @@ export const DEFAULT_TRANS_APIS = {
|
|||||||
key: "",
|
key: "",
|
||||||
fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量
|
fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量
|
||||||
fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间
|
fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间
|
||||||
|
apiName: OPT_TRANS_GOOGLE,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_GOOGLE2]: {
|
[OPT_TRANS_GOOGLE2]: {
|
||||||
url: URL_GOOGLE_TRAN2,
|
url: URL_GOOGLE_TRAN2,
|
||||||
key: DEFAULT_GOOGLE_API_KEY,
|
key: DEFAULT_GOOGLE_API_KEY,
|
||||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||||
|
apiName: OPT_TRANS_GOOGLE2,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_MICROSOFT]: {
|
[OPT_TRANS_MICROSOFT]: {
|
||||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||||
|
apiName: OPT_TRANS_MICROSOFT,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_BAIDU]: {
|
[OPT_TRANS_BAIDU]: {
|
||||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||||
|
apiName: OPT_TRANS_BAIDU,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_TENCENT]: {
|
[OPT_TRANS_TENCENT]: {
|
||||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||||
|
apiName: OPT_TRANS_TENCENT,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_VOLCENGINE]: {
|
[OPT_TRANS_VOLCENGINE]: {
|
||||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||||
|
apiName: OPT_TRANS_VOLCENGINE,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[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,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: OPT_TRANS_DEEPL,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_DEEPLFREE]: {
|
[OPT_TRANS_DEEPLFREE]: {
|
||||||
fetchLimit: 1,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: OPT_TRANS_DEEPLFREE,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_DEEPLX]: {
|
[OPT_TRANS_DEEPLX]: {
|
||||||
url: "http://localhost:1188/translate",
|
url: "http://localhost:1188/translate",
|
||||||
key: "",
|
key: "",
|
||||||
fetchLimit: 1,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: OPT_TRANS_DEEPLX,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_NIUTRANS]: {
|
[OPT_TRANS_NIUTRANS]: {
|
||||||
url: "https://api.niutrans.com/NiuTransServer/translation",
|
url: "https://api.niutrans.com/NiuTransServer/translation",
|
||||||
@@ -614,6 +636,8 @@ export const DEFAULT_TRANS_APIS = {
|
|||||||
memoryNo: "",
|
memoryNo: "",
|
||||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||||
|
apiName: OPT_TRANS_NIUTRANS,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_OPENAI]: defaultOpenaiApi,
|
[OPT_TRANS_OPENAI]: defaultOpenaiApi,
|
||||||
[OPT_TRANS_OPENAI_2]: defaultOpenaiApi,
|
[OPT_TRANS_OPENAI_2]: defaultOpenaiApi,
|
||||||
@@ -626,6 +650,8 @@ export const DEFAULT_TRANS_APIS = {
|
|||||||
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
|
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
|
||||||
fetchLimit: 1,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: OPT_TRANS_GEMINI,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_CLAUDE]: {
|
[OPT_TRANS_CLAUDE]: {
|
||||||
url: "https://api.anthropic.com/v1/messages",
|
url: "https://api.anthropic.com/v1/messages",
|
||||||
@@ -637,12 +663,16 @@ export const DEFAULT_TRANS_APIS = {
|
|||||||
maxTokens: 1024,
|
maxTokens: 1024,
|
||||||
fetchLimit: 1,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: OPT_TRANS_CLAUDE,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[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,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
|
apiName: OPT_TRANS_CLOUDFLAREAI,
|
||||||
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
[OPT_TRANS_OLLAMA]: defaultOllamaApi,
|
[OPT_TRANS_OLLAMA]: defaultOllamaApi,
|
||||||
[OPT_TRANS_OLLAMA_2]: defaultOllamaApi,
|
[OPT_TRANS_OLLAMA_2]: defaultOllamaApi,
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import TextField from "@mui/material/TextField";
|
|||||||
import Button from "@mui/material/Button";
|
import Button from "@mui/material/Button";
|
||||||
import LoadingButton from "@mui/lab/LoadingButton";
|
import LoadingButton from "@mui/lab/LoadingButton";
|
||||||
import MenuItem from "@mui/material/MenuItem";
|
import MenuItem from "@mui/material/MenuItem";
|
||||||
|
import FormControlLabel from "@mui/material/FormControlLabel";
|
||||||
|
import Switch from "@mui/material/Switch";
|
||||||
import {
|
import {
|
||||||
OPT_TRANS_ALL,
|
OPT_TRANS_ALL,
|
||||||
OPT_TRANS_MICROSOFT,
|
OPT_TRANS_MICROSOFT,
|
||||||
@@ -129,6 +131,8 @@ function ApiFields({ translator }) {
|
|||||||
resHook = "",
|
resHook = "",
|
||||||
temperature = 0,
|
temperature = 0,
|
||||||
maxTokens = 256,
|
maxTokens = 256,
|
||||||
|
apiName = "",
|
||||||
|
isDisabled = false,
|
||||||
} = api;
|
} = api;
|
||||||
|
|
||||||
const handleChange = (e) => {
|
const handleChange = (e) => {
|
||||||
@@ -191,6 +195,14 @@ function ApiFields({ translator }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack spacing={3}>
|
<Stack spacing={3}>
|
||||||
|
<TextField
|
||||||
|
size="small"
|
||||||
|
label={i18n("api_name")}
|
||||||
|
name="apiName"
|
||||||
|
value={apiName}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
|
|
||||||
{!builtinTranslators.includes(translator) && (
|
{!builtinTranslators.includes(translator) && (
|
||||||
<>
|
<>
|
||||||
<TextField
|
<TextField
|
||||||
@@ -356,6 +368,20 @@ function ApiFields({ translator }) {
|
|||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<FormControlLabel
|
||||||
|
control={
|
||||||
|
<Switch
|
||||||
|
size="small"
|
||||||
|
name="isDisabled"
|
||||||
|
checked={isDisabled}
|
||||||
|
onChange={()=>{
|
||||||
|
updateApi({ isDisabled: !isDisabled });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
label={i18n("is_disabled")}
|
||||||
|
/>
|
||||||
|
|
||||||
<Stack direction="row" spacing={2}>
|
<Stack direction="row" spacing={2}>
|
||||||
<TestButton translator={translator} api={api} />
|
<TestButton translator={translator} api={api} />
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useMemo } from "react";
|
||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
import MenuItem from "@mui/material/MenuItem";
|
import MenuItem from "@mui/material/MenuItem";
|
||||||
@@ -23,6 +23,7 @@ import {
|
|||||||
OPT_LANGS_FROM,
|
OPT_LANGS_FROM,
|
||||||
OPT_LANGS_TO,
|
OPT_LANGS_TO,
|
||||||
OPT_STYLE_ALL,
|
OPT_STYLE_ALL,
|
||||||
|
DEFAULT_TRANS_APIS,
|
||||||
} from "../../config";
|
} from "../../config";
|
||||||
import { sendIframeMsg } from "../../libs/iframe";
|
import { sendIframeMsg } from "../../libs/iframe";
|
||||||
import { saveRule } from "../../libs/rules";
|
import { saveRule } from "../../libs/rules";
|
||||||
@@ -32,6 +33,7 @@ import { kissLog } from "../../libs/log";
|
|||||||
export default function Popup({ setShowPopup, translator: tran }) {
|
export default function Popup({ setShowPopup, translator: tran }) {
|
||||||
const i18n = useI18n();
|
const i18n = useI18n();
|
||||||
const [rule, setRule] = useState(tran?.rule);
|
const [rule, setRule] = useState(tran?.rule);
|
||||||
|
const [transApis, setTransApis] = useState(tran?.setting?.transApis || []);
|
||||||
const [commands, setCommands] = useState({});
|
const [commands, setCommands] = useState({});
|
||||||
|
|
||||||
const handleOpenSetting = () => {
|
const handleOpenSetting = () => {
|
||||||
@@ -46,6 +48,7 @@ export default function Popup({ setShowPopup, translator: tran }) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleTransToggle = async (e) => {
|
const handleTransToggle = async (e) => {
|
||||||
|
console.log({ tran });
|
||||||
try {
|
try {
|
||||||
setRule({ ...rule, transOpen: e.target.checked ? "true" : "false" });
|
setRule({ ...rule, transOpen: e.target.checked ? "true" : "false" });
|
||||||
|
|
||||||
@@ -106,7 +109,8 @@ export default function Popup({ setShowPopup, translator: tran }) {
|
|||||||
try {
|
try {
|
||||||
const res = await sendTabMsg(MSG_TRANS_GETRULE);
|
const res = await sendTabMsg(MSG_TRANS_GETRULE);
|
||||||
if (!res.error) {
|
if (!res.error) {
|
||||||
setRule(res.data);
|
setRule(res.rule);
|
||||||
|
setTransApis(res.setting.transApis);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
kissLog(err, "query rule");
|
kissLog(err, "query rule");
|
||||||
@@ -138,6 +142,20 @@ export default function Popup({ setShowPopup, translator: tran }) {
|
|||||||
})();
|
})();
|
||||||
}, [tran]);
|
}, [tran]);
|
||||||
|
|
||||||
|
const optApis = useMemo(
|
||||||
|
() =>
|
||||||
|
OPT_TRANS_ALL.map((key) => ({
|
||||||
|
...(transApis[key] || DEFAULT_TRANS_APIS[key]),
|
||||||
|
apiKey: key,
|
||||||
|
}))
|
||||||
|
.filter((item) => !item.isDisabled)
|
||||||
|
.map(({ apiKey, apiName }) => ({
|
||||||
|
key: apiKey,
|
||||||
|
name: apiName?.trim() || apiKey,
|
||||||
|
})),
|
||||||
|
[transApis]
|
||||||
|
);
|
||||||
|
|
||||||
if (!rule) {
|
if (!rule) {
|
||||||
return (
|
return (
|
||||||
<Box minWidth={300}>
|
<Box minWidth={300}>
|
||||||
@@ -197,9 +215,9 @@ export default function Popup({ setShowPopup, translator: tran }) {
|
|||||||
label={i18n("translate_service")}
|
label={i18n("translate_service")}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
>
|
>
|
||||||
{OPT_TRANS_ALL.map((item) => (
|
{optApis.map(({ key, name }) => (
|
||||||
<MenuItem key={item} value={item}>
|
<MenuItem key={key} value={key}>
|
||||||
{item}
|
{name}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
))}
|
))}
|
||||||
</TextField>
|
</TextField>
|
||||||
|
|||||||
@@ -18,8 +18,13 @@ import LockIcon from "@mui/icons-material/Lock";
|
|||||||
import LockOpenIcon from "@mui/icons-material/LockOpen";
|
import LockOpenIcon from "@mui/icons-material/LockOpen";
|
||||||
import CloseIcon from "@mui/icons-material/Close";
|
import CloseIcon from "@mui/icons-material/Close";
|
||||||
import { useI18n } from "../../hooks/I18n";
|
import { useI18n } from "../../hooks/I18n";
|
||||||
import { OPT_TRANS_ALL, OPT_LANGS_FROM, OPT_LANGS_TO } from "../../config";
|
import {
|
||||||
import { useState, useRef } from "react";
|
OPT_TRANS_ALL,
|
||||||
|
OPT_LANGS_FROM,
|
||||||
|
OPT_LANGS_TO,
|
||||||
|
DEFAULT_TRANS_APIS,
|
||||||
|
} from "../../config";
|
||||||
|
import { useState, useRef, useMemo } from "react";
|
||||||
import TranCont from "./TranCont";
|
import TranCont from "./TranCont";
|
||||||
import DictCont from "./DictCont";
|
import DictCont from "./DictCont";
|
||||||
import SugCont from "./SugCont";
|
import SugCont from "./SugCont";
|
||||||
@@ -119,6 +124,20 @@ function TranForm({
|
|||||||
const [toLang, setToLang] = useState(tranboxSetting.toLang);
|
const [toLang, setToLang] = useState(tranboxSetting.toLang);
|
||||||
const inputRef = useRef(null);
|
const inputRef = useRef(null);
|
||||||
|
|
||||||
|
const optApis = useMemo(
|
||||||
|
() =>
|
||||||
|
OPT_TRANS_ALL.map((key) => ({
|
||||||
|
...(transApis[key] || DEFAULT_TRANS_APIS[key]),
|
||||||
|
apiKey: key,
|
||||||
|
}))
|
||||||
|
.filter((item) => !item.isDisabled)
|
||||||
|
.map(({ apiKey, apiName }) => ({
|
||||||
|
key: apiKey,
|
||||||
|
name: apiName?.trim() || apiKey,
|
||||||
|
})),
|
||||||
|
[transApis]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack
|
<Stack
|
||||||
className="KT-transbox-container"
|
className="KT-transbox-container"
|
||||||
@@ -182,9 +201,9 @@ function TranForm({
|
|||||||
setTranslator(e.target.value);
|
setTranslator(e.target.value);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{OPT_TRANS_ALL.map((item) => (
|
{optApis.map(({ key, name }) => (
|
||||||
<MenuItem key={item} value={item}>
|
<MenuItem key={key} value={key}>
|
||||||
{item}
|
{name}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
))}
|
))}
|
||||||
</TextField>
|
</TextField>
|
||||||
|
|||||||
Reference in New Issue
Block a user