Merge pull request #235 from unclemcz/dev

feat:基本设置增加请求超时参数&ollama接口配置增加<think>块忽略参数
This commit is contained in:
Gabe
2025-05-09 14:09:18 +08:00
committed by GitHub
6 changed files with 53 additions and 5 deletions

View File

@@ -39,6 +39,7 @@ import {
import { sha256 } from "../libs/utils"; import { sha256 } from "../libs/utils";
import interpreter from "../libs/interpreter"; import interpreter from "../libs/interpreter";
import { msAuth } from "../libs/auth"; import { msAuth } from "../libs/auth";
import {getSettingWithDefault } from "../libs/storage";
/** /**
* 同步数据 * 同步数据
@@ -332,7 +333,9 @@ export const apiTranslate = async ({
case OPT_TRANS_OLLAMA: case OPT_TRANS_OLLAMA:
case OPT_TRANS_OLLAMA_2: case OPT_TRANS_OLLAMA_2:
case OPT_TRANS_OLLAMA_3: case OPT_TRANS_OLLAMA_3:
if (res?.model.startsWith('deepseek-r1')) { let deepModels = (await getSettingWithDefault()).transApis[translator]?.thinkIgnore || '';
deepModels = deepModels.split(',').filter(model => model.trim() !== '');
if (deepModels.some(model => res?.model?.startsWith(model))) {
trText = res?.response.replace(/<think>[\s\S]*<\/think>/i, ''); trText = res?.response.replace(/<think>[\s\S]*<\/think>/i, '');
}else{ }else{
trText = res?.response; trText = res?.response;

View File

@@ -186,6 +186,10 @@ export const I18N = {
zh: `最大并发请求数量 (1-100)`, zh: `最大并发请求数量 (1-100)`,
en: `Maximum Number Of Concurrent Requests (1-100)`, en: `Maximum Number Of Concurrent Requests (1-100)`,
}, },
think_ignore: {
zh: `忽略以下模型的<think>输出,逗号(,)分割`,
en: `Ignore the <think> block for the following models, comma (,) separated`,
},
fetch_interval: { fetch_interval: {
zh: `每次请求间隔时间 (0-5000ms)`, zh: `每次请求间隔时间 (0-5000ms)`,
en: `Time Between Requests (0-5000ms)`, en: `Time Between Requests (0-5000ms)`,
@@ -194,6 +198,10 @@ export const I18N = {
zh: `重新翻译间隔时间 (100-5000ms)`, zh: `重新翻译间隔时间 (100-5000ms)`,
en: `Retranslation Interval (100-5000ms)`, en: `Retranslation Interval (100-5000ms)`,
}, },
http_timeout: {
zh: `请求超时时间 (5000-30000ms)`,
en: `Request Timeout Time (5000-30000ms)`,
},
min_translate_length: { min_translate_length: {
zh: `最小翻译字符数 (1-100)`, zh: `最小翻译字符数 (1-100)`,
en: `Minimum number Of Translated Characters (1-100)`, en: `Minimum number Of Translated Characters (1-100)`,

View File

@@ -543,6 +543,7 @@ const defaultOllamaApi = {
model: "llama3.1", model: "llama3.1",
systemPrompt: `You are a professional, authentic machine translation engine.`, systemPrompt: `You are a professional, authentic machine translation engine.`,
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:`,
thinkIgnore:`qwen3,deepseek-r1`,
fetchLimit: 1, fetchLimit: 1,
fetchInterval: 500, fetchInterval: 500,
}; };
@@ -643,6 +644,7 @@ export const DEFAULT_SHORTCUTS = {
export const TRANS_MIN_LENGTH = 5; // 最短翻译长度 export const TRANS_MIN_LENGTH = 5; // 最短翻译长度
export const TRANS_MAX_LENGTH = 5000; // 最长翻译长度 export const TRANS_MAX_LENGTH = 5000; // 最长翻译长度
export const TRANS_NEWLINE_LENGTH = 20; // 换行字符数 export const TRANS_NEWLINE_LENGTH = 20; // 换行字符数
export const HTTP_TIMEOUT = 5000; // 调用超时时间
export const DEFAULT_BLACKLIST = [ export const DEFAULT_BLACKLIST = [
"https://fishjar.github.io/kiss-translator/options.html", "https://fishjar.github.io/kiss-translator/options.html",
"https://translate.google.com", "https://translate.google.com",
@@ -660,6 +662,7 @@ export const DEFAULT_SETTING = {
minLength: TRANS_MIN_LENGTH, minLength: TRANS_MIN_LENGTH,
maxLength: TRANS_MAX_LENGTH, maxLength: TRANS_MAX_LENGTH,
newlineLength: TRANS_NEWLINE_LENGTH, newlineLength: TRANS_NEWLINE_LENGTH,
httpTimeout: HTTP_TIMEOUT,
clearCache: false, // 是否在浏览器下次启动时清除缓存 clearCache: false, // 是否在浏览器下次启动时清除缓存
injectRules: true, // 是否注入订阅规则 injectRules: true, // 是否注入订阅规则
// injectWebfix: true, // 是否注入修复补丁(作废) // injectWebfix: true, // 是否注入修复补丁(作废)

View File

@@ -1,19 +1,23 @@
import { isExt, isGm } from "./client"; import { isExt, isGm } from "./client";
import { sendBgMsg } from "./msg"; import { sendBgMsg } from "./msg";
import { taskPool } from "./pool"; import { taskPool } from "./pool";
import { storage,getSettingWithDefault } from "./storage";
import { import {
MSG_FETCH, MSG_FETCH,
MSG_GET_HTTPCACHE, MSG_GET_HTTPCACHE,
CACHE_NAME, CACHE_NAME,
DEFAULT_FETCH_INTERVAL, DEFAULT_FETCH_INTERVAL,
DEFAULT_FETCH_LIMIT, DEFAULT_FETCH_LIMIT,
HTTP_TIMEOUT,
} from "../config"; } from "../config";
import { isBg } from "./browser"; import { isBg } from "./browser";
import { genTransReq } from "../apis/trans"; import { genTransReq } from "../apis/trans";
import { kissLog } from "./log"; import { kissLog } from "./log";
import { blobToBase64 } from "./utils"; import { blobToBase64 } from "./utils";
const TIMEOUT = 5000; let TIMEOUT = HTTP_TIMEOUT;
/** /**
* 构造缓存 request * 构造缓存 request
@@ -39,7 +43,8 @@ const newCacheReq = async (input, init) => {
* @param {*} init * @param {*} init
* @returns * @returns
*/ */
export const fetchGM = async (input, { method = "GET", headers, body } = {}) => export const fetchGM = async (input, { method = "GET", headers, body } = {}) =>{
TIMEOUT = (await getSettingWithDefault()).httpTimeout;
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
GM.xmlHttpRequest({ GM.xmlHttpRequest({
method, method,
@@ -66,7 +71,7 @@ export const fetchGM = async (input, { method = "GET", headers, body } = {}) =>
onerror: reject, onerror: reject,
}); });
}); });
}
/** /**
* 发起请求 * 发起请求
* @param {*} param0 * @param {*} param0
@@ -108,6 +113,10 @@ export const fetchPatcher = async (input, init, transOpts, apiSetting) => {
} }
} }
//const kiss_setting = await storage.get("KISS-Translator_setting");
//TIMEOUT = JSON.parse(kiss_setting)?.httpTimeout || TIMEOUT;
TIMEOUT = (await getSettingWithDefault()).httpTimeout;
console.log("TIMEOUT", TIMEOUT);
if (AbortSignal?.timeout && !init.signal) { if (AbortSignal?.timeout && !init.signal) {
Object.assign(init, { signal: AbortSignal.timeout(TIMEOUT) }); Object.assign(init, { signal: AbortSignal.timeout(TIMEOUT) });
} }

View File

@@ -117,6 +117,7 @@ function ApiFields({ translator }) {
model = "", model = "",
systemPrompt = "", systemPrompt = "",
userPrompt = "", userPrompt = "",
thinkIgnore = "",
fetchLimit = DEFAULT_FETCH_LIMIT, fetchLimit = DEFAULT_FETCH_LIMIT,
fetchInterval = DEFAULT_FETCH_INTERVAL, fetchInterval = DEFAULT_FETCH_INTERVAL,
dictNo = "", dictNo = "",
@@ -246,6 +247,18 @@ function ApiFields({ translator }) {
</> </>
)} )}
{(translator.startsWith(OPT_TRANS_OLLAMA)) && (
<>
<TextField
size="small"
label={i18n("think_ignore")}
name="thinkIgnore"
value={thinkIgnore}
onChange={handleChange}
/>
</>
)}
{(translator.startsWith(OPT_TRANS_OPENAI) || {(translator.startsWith(OPT_TRANS_OPENAI) ||
translator === OPT_TRANS_CLAUDE) && ( translator === OPT_TRANS_CLAUDE) && (
<> <>

View File

@@ -27,6 +27,7 @@ import {
DEFAULT_CSPLIST, DEFAULT_CSPLIST,
MSG_CONTEXT_MENUS, MSG_CONTEXT_MENUS,
MSG_UPDATE_CSP, MSG_UPDATE_CSP,
HTTP_TIMEOUT,
} from "../../config"; } from "../../config";
import { useShortcut } from "../../hooks/Shortcut"; import { useShortcut } from "../../hooks/Shortcut";
import ShortcutInput from "./ShortcutInput"; import ShortcutInput from "./ShortcutInput";
@@ -71,6 +72,9 @@ export default function Settings() {
case "newlineLength": case "newlineLength":
value = limitNumber(value, 1, 1000); value = limitNumber(value, 1, 1000);
break; break;
case "httpTimeout":
value = limitNumber(value, 5000, 30000);
break;
case "touchTranslate": case "touchTranslate":
value = limitNumber(value, 0, 4); value = limitNumber(value, 0, 4);
break; break;
@@ -110,6 +114,7 @@ export default function Settings() {
maxLength, maxLength,
clearCache, clearCache,
newlineLength = TRANS_NEWLINE_LENGTH, newlineLength = TRANS_NEWLINE_LENGTH,
httpTimeout = HTTP_TIMEOUT,
contextMenuType = 1, contextMenuType = 1,
touchTranslate = 2, touchTranslate = 2,
blacklist = DEFAULT_BLACKLIST.join(",\n"), blacklist = DEFAULT_BLACKLIST.join(",\n"),
@@ -188,7 +193,14 @@ export default function Settings() {
defaultValue={transInterval} defaultValue={transInterval}
onChange={handleChange} onChange={handleChange}
/> />
<TextField
size="small"
label={i18n("http_timeout")}
type="number"
name="httpTimeout"
defaultValue={httpTimeout}
onChange={handleChange}
/>
<FormControl size="small"> <FormControl size="small">
<InputLabel>{i18n("touch_translate_shortcut")}</InputLabel> <InputLabel>{i18n("touch_translate_shortcut")}</InputLabel>
<Select <Select