feat: 所有超时收成一份、全部可调(设置 → 超时,18 项)
用户要求"全部超时做成可调"。改造前 15 个超时散在 10 个文件里当模块级常量, 说不清一共有几个,也答不了"哪些能调"。 内核 - 新增 android/kernel/timeouts.py:**超时的唯一一份定义**(DEFAULT + SPECS), 带每项的 名字/分组/说明/默认值/上下界;get() 一律夹取,用户填什么都不炸 - 新增 GET/POST /api/timeouts(清单 + 当前值 + 保存 + 恢复默认) - 各处改现读:tools_runtime(tool)、builtin_tools(shell/pythonRun)、 doubao_entry(termux)、providers(model)、speech(tts/stt)、 mcp_client(请求/连接/调用)、meeting(meetingNode)、custom_nodes(customNode)、 app.py 的 SSE 心跳 - ask_user / show_ui 用 TIMEOUT_KEYS 声明"哪个参数该用设置里的值", 内核调用前注入(工具的默认值退化成单独跑时的兜底) - store.py:timeouts 走**整体替换**而不是深合并 —— 深合并会让"改回默认"删不掉 安卓 - data/AppTimeouts.kt:客户端缓存(OkHttp 的 readTimeout、文件选择没法现问内核) - KernelClient:readTimeout 跟着 sseRead 走,改完重建连接池;新增 timeouts 接口 - ui/TimeoutsScreen.kt:全屏界面,按分组列出、-/+ 与输入框、恢复默认 - 设置页加「超时」卡片入口;NativeBridge 的本地接口请求 / 选文件等待接上 - Settings 模型加 TimeoutItem / TimeoutsState(字段全部来自内核) Node 端(Web/exe) - 新增 server/src/timeouts.mjs(和 Python 那份逐字对应) - config.mjs 加 timeouts(同样整体替换);speech / python-tools / mcp / providers 各处改读;index.mjs 加 /api/timeouts 验证 - 新增 scripts/verify-timeouts.py:清单完整性、边界容错(文字/空串/负数/超大值/ 带单位/None)、**扫内核有没有漏网的硬编码超时**(带白名单)、 工具侧声明、以及和 Node 端逐项对拍 - 实测真机:界面显示 18 项来自内核 → 点 + 保存 → 内核 heartbeat 15→25 → 恢复默认 → 回 15;askUser 显示"不限时" 文档:README §3.13;docs/AI界面能力.md 的"不限时要三处一起改"补上可调入口。 版本 2.0.46。
This commit is contained in:
@@ -311,6 +311,10 @@ export const DEFAULT_SETTINGS = {
|
||||
openaiBaseUrl: '', // 留空则复用主对话的 Base URL
|
||||
ttsModel: 'tts-1',
|
||||
},
|
||||
// 超时。**这里故意是空的** —— 默认值全在 timeouts.mjs 的 DEFAULT_TIMEOUTS 里,
|
||||
// 只有用户改过的项才会出现在这儿(和默认一样就删掉,见 patchTimeouts)。
|
||||
// 和 Python 端 config 的 'timeouts': {} 一致。
|
||||
timeouts: {},
|
||||
pythonBin: '',
|
||||
nickname: '我',
|
||||
doubaoId: '378269252',
|
||||
@@ -354,6 +358,12 @@ export function readSettings() {
|
||||
};
|
||||
}
|
||||
if (!Array.isArray(settings.security.allow)) settings.security.allow = [];
|
||||
// ★ timeouts 走**整体替换**(和 Python 端 store.py 一致):
|
||||
// 默认值在 timeouts.mjs 里,深合并会让"改回默认 = 删掉这一项"永远删不掉,
|
||||
// 旧值残留在配置里,用户会看到"我明明恢复默认了怎么还是 30 秒"。
|
||||
if (typeof settings.timeouts !== 'object' || settings.timeouts === null || Array.isArray(settings.timeouts)) {
|
||||
settings.timeouts = {};
|
||||
}
|
||||
|
||||
// 环境变量兜底:没填 key 时用 .env 里的
|
||||
const preset = PROVIDER_PRESETS[settings.provider];
|
||||
@@ -406,6 +416,16 @@ export function writeSettings(patch) {
|
||||
console.error(`[config] 补丁里的 ${key} 不是对象(${typeof v}),已忽略`);
|
||||
}
|
||||
}
|
||||
// ★ timeouts 是**整体替换**,不深合并 —— 和 Python 端同一套语义。
|
||||
// 调用方(/api/timeouts)传进来的就是"完整的非默认集合"。
|
||||
if ('timeouts' in clean) {
|
||||
const t = clean.timeouts;
|
||||
if (t && typeof t === 'object' && !Array.isArray(t)) next.timeouts = t;
|
||||
else {
|
||||
next.timeouts = current.timeouts || {};
|
||||
console.error(`[config] 补丁里的 timeouts 不是对象(${typeof t}),已忽略`);
|
||||
}
|
||||
}
|
||||
// 掩码值表示"不修改"
|
||||
if (clean.apiKey === '••••••••') next.apiKey = current.apiKey;
|
||||
// ★ 语音里的密钥同理:界面读到的是掩码,用户不改语音设置直接保存时掩码会跟着回来,
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
readSettings,
|
||||
writeSettings,
|
||||
} from './config.mjs';
|
||||
import { patchTimeouts, resolvedTimeouts, timeoutsCatalog } from './timeouts.mjs';
|
||||
import * as store from './store.mjs';
|
||||
import { listAllTools, runAgent, executeTool, suggestAllowlist } from './agent.mjs';
|
||||
import {
|
||||
@@ -589,6 +590,34 @@ async function handleApi(req, res, url) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---- 超时(设置 → 超时)----
|
||||
// ★ 和 Python 内核的 /api/timeouts 行为一致:清单由服务端给(界面不硬编码
|
||||
// 有哪些超时),写的时候是**整体替换**(默认值在 timeouts.mjs 里,
|
||||
// "改回默认" = 从配置里删掉这一项)。
|
||||
if (pathname === '/api/timeouts' && method === 'GET') {
|
||||
const settings = readSettings();
|
||||
sendJson(res, 200, {
|
||||
ok: true,
|
||||
items: timeoutsCatalog(settings),
|
||||
values: resolvedTimeouts(settings),
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pathname === '/api/timeouts' && (method === 'PUT' || method === 'POST')) {
|
||||
const body = await readBody(req);
|
||||
const current = readSettings();
|
||||
const nextValues = body.reset ? {} : patchTimeouts(current, body.values || body);
|
||||
writeSettings({ timeouts: nextValues });
|
||||
const after = readSettings();
|
||||
sendJson(res, 200, {
|
||||
ok: true,
|
||||
items: timeoutsCatalog(after),
|
||||
values: resolvedTimeouts(after),
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pathname === '/api/models' && method === 'GET') {
|
||||
const settings = readSettings();
|
||||
try {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* MCP(Model Context Protocol)客户端管理。
|
||||
*
|
||||
* tools/mcp.servers.json 描述要连接的服务:
|
||||
@@ -9,7 +9,8 @@
|
||||
* 对外暴露的工具名统一为 mcp__<server>__<tool>,避免和 Python 工具重名。
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import { MCP_FILE } from './config.mjs';
|
||||
import { MCP_FILE, readSettings } from './config.mjs';
|
||||
import { timeoutOf } from './timeouts.mjs';
|
||||
|
||||
const clients = new Map(); // serverName -> { client, transport, tools, error, spec }
|
||||
|
||||
@@ -149,8 +150,9 @@ export async function connect(serverName, { force = false } = {}) {
|
||||
}
|
||||
const client = new sdk.Client({ name: 'doubao-kernel', version: '0.1.0' }, { capabilities: {} });
|
||||
// 首次用 npx 拉起服务器时要下载包,可能远超 SDK 默认的 60s 超时
|
||||
await client.connect(transport, { timeout: 180000 });
|
||||
const listed = await client.listTools(undefined, { timeout: 60000 });
|
||||
// ★ 超时可调(设置 → 超时 → MCP 连接)。npx/uvx 第一次要下载,默认给到 180 秒
|
||||
await client.connect(transport, { timeout: timeoutOf(readSettings(), 'mcpConnect') * 1000 });
|
||||
const listed = await client.listTools(undefined, { timeout: timeoutOf(readSettings(), 'mcpRequest') * 1000 });
|
||||
record.client = client;
|
||||
record.transport = transport;
|
||||
record.tools = listed.tools || [];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* 提供商调用层:火山方舟 / OpenAI / DeepSeek / Kimi / 智谱 / 通义 / 硅基流动 / OpenRouter …
|
||||
* 全都是 OpenAI 兼容协议,所以这里只有一份实现。
|
||||
* 统一暴露:
|
||||
@@ -7,7 +7,8 @@
|
||||
* listModels() - 拉取模型列表(可选能力,失败不致命)
|
||||
*/
|
||||
|
||||
import { PROVIDER_PRESETS } from './config.mjs';
|
||||
import { PROVIDER_PRESETS, readSettings } from './config.mjs';
|
||||
import { timeoutOf } from './timeouts.mjs';
|
||||
|
||||
function joinUrl(baseUrl, suffix) {
|
||||
return `${String(baseUrl || '').replace(/\/+$/, '')}${suffix}`;
|
||||
@@ -86,7 +87,10 @@ function buildBody({ provider, model, messages, tools, temperature, stream, thin
|
||||
return normalizeBody({ provider, model, thinkingLevel, body });
|
||||
}
|
||||
|
||||
async function post({ baseUrl, apiKey, body, signal, timeoutMs = 180000 }) {
|
||||
async function post({ baseUrl, apiKey, body, signal, timeoutMs = null }) {
|
||||
// ★ 超时可调(设置 → 超时 → 等模型回复)。JS 的默认参数是**每次调用**求值的,
|
||||
// 所以这里现读就能立刻生效(不用重启)。
|
||||
if (!timeoutMs) timeoutMs = timeoutOf(readSettings(), 'model') * 1000;
|
||||
if (!apiKey) {
|
||||
throw new Error('缺少 API Key:请在「设置 → AI 内核」里填写,或在 server/.env 里配置');
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* Python 工具运行时。
|
||||
*
|
||||
* 工具即 tools/*.py,每个文件用 `_protocol.serve(TOOL, run)` 暴露能力:
|
||||
@@ -15,6 +15,7 @@ import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { TMP_DIR, TOOLS_DIR, readSettings } from './config.mjs';
|
||||
import { timeoutOf } from './timeouts.mjs';
|
||||
|
||||
const specCache = new Map(); // file -> { mtimeMs, spec, error }
|
||||
|
||||
@@ -205,7 +206,7 @@ async function runPythonToolWithFile(tool, py, args) {
|
||||
bin: py.bin,
|
||||
args: [...py.prefix, tool.__file, '--input', inPath],
|
||||
input: '',
|
||||
timeoutMs: 60000,
|
||||
timeoutMs: timeoutOf(readSettings(), 'tool') * 1000,
|
||||
cwd: toolsDir(),
|
||||
});
|
||||
try {
|
||||
@@ -231,7 +232,7 @@ async function runPythonToolWithFile(tool, py, args) {
|
||||
}
|
||||
return {
|
||||
ok: res.code === 0,
|
||||
error: res.timedOut ? '工具执行超时(60s)' : `未解析到 JSON 输出(退出码 ${res.code})`,
|
||||
error: res.timedOut ? `工具执行超时(${Math.round(timeoutOf(readSettings(), 'tool'))}s,可在设置里调)` : `未解析到 JSON 输出(退出码 ${res.code})`,
|
||||
stdout: res.stdout.slice(0, 2000),
|
||||
stderr: res.stderr.slice(0, 2000),
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* 语音:朗读(TTS)和语音识别(STT)。
|
||||
*
|
||||
* 对应 Python 版 android/kernel/speech.py —— 两份实现的请求体、返回字段、错误文案
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
// 全量音色表(抓官方文档生成)。常用精选在下面,界面上是"常用 + 全部可搜索"两段式。
|
||||
import { VOLC_VOICES_ALL, VOLC_V3_SPEAKERS_ALL } from './voices_doubao.mjs';
|
||||
import { readSettings } from './config.mjs';
|
||||
import { timeoutOf } from './timeouts.mjs';
|
||||
|
||||
/** V1 合成接口(appid + access_token,Bearer 后面是分号)。自测时可指到 mock */
|
||||
export const VOLC_TTS_URL = process.env.DOUBAO_VOLC_TTS_URL || 'https://openspeech.bytedance.com/api/v1/tts';
|
||||
@@ -57,8 +59,11 @@ export const VOLC_V3_TTS_URL = process.env.DOUBAO_VOLC_V3_TTS_URL
|
||||
|| 'https://openspeech.bytedance.com/api/v3/tts/unidirectional';
|
||||
export const VOLC_V3_RESOURCE_ID = 'seed-tts-2.0';
|
||||
|
||||
const TIMEOUT_TTS = 60000;
|
||||
const TIMEOUT_STT = 120000;
|
||||
// ★ ★ 超时可调(设置 → 超时 → 语音合成 / 语音识别)。**现读**,改完立刻生效,
|
||||
// 不用重启。默认值在 timeouts.mjs 里,和 Python 内核那份逐字对应。
|
||||
// 以前这里是两个模块级常量(60000 / 120000),网络慢或回复长的时候用户只能忍着。
|
||||
const ttsTimeoutMs = () => timeoutOf(readSettings(), 'tts') * 1000;
|
||||
const sttTimeoutMs = () => timeoutOf(readSettings(), 'stt') * 1000;
|
||||
|
||||
const VOLC_MIME = {
|
||||
mp3: 'audio/mpeg',
|
||||
@@ -360,7 +365,7 @@ async function synthesizeVolcV3(text, speech, speed) {
|
||||
'X-Control-Require-Usage-Tokens-Return': '*',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
}, TIMEOUT_TTS);
|
||||
}, ttsTimeoutMs());
|
||||
} catch (err) {
|
||||
throw new SpeechError(`连不上火山 V3:${err.message}`);
|
||||
}
|
||||
@@ -438,7 +443,7 @@ async function synthesizeVolc(text, speech, speed) {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer;${token}` },
|
||||
body: JSON.stringify(body),
|
||||
}, TIMEOUT_TTS);
|
||||
}, ttsTimeoutMs());
|
||||
} catch (err) {
|
||||
throw new SpeechError(`连不上:${err.message}`);
|
||||
}
|
||||
@@ -472,7 +477,7 @@ async function synthesizeOpenai(text, settings, speech, speed) {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}` },
|
||||
body: JSON.stringify({ model, input: text, voice, response_format: format, speed }),
|
||||
}, TIMEOUT_TTS);
|
||||
}, ttsTimeoutMs());
|
||||
} catch (err) {
|
||||
throw new SpeechError(`连不上:${err.message}`);
|
||||
}
|
||||
@@ -511,7 +516,7 @@ export async function transcribe(audio, filename, contentType, settings) {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
body: form,
|
||||
}, TIMEOUT_STT);
|
||||
}, sttTimeoutMs());
|
||||
} catch (err) {
|
||||
throw new SpeechError(`连不上:${err.message}`);
|
||||
}
|
||||
|
||||
210
server/src/timeouts.mjs
Normal file
210
server/src/timeouts.mjs
Normal file
@@ -0,0 +1,210 @@
|
||||
/**
|
||||
* 所有超时的定义 —— **和 Python 内核的 `android/kernel/timeouts.py` 逐字对应**。
|
||||
*
|
||||
* ## 为什么两端各写一份,而不是共享
|
||||
*
|
||||
* 因为一个是 Python、一个是 JS,没法共享代码。这是这个仓库里跨语言一致性的
|
||||
* 标准做法:**两边都写 + 一个脚本盯着**(`scripts/verify-timeouts.py` 会把
|
||||
* 这份和 Python 那份逐项对拍:key / label / 分组 / 默认值 / 上下界)。
|
||||
*
|
||||
* 对拍的不是"默认值"那么简单 —— 默认值不一致的表现最难查:
|
||||
* Web 端说 60 秒、安卓端说 30 秒,同一句"命令超时"在两个端上根本不是一回事。
|
||||
*
|
||||
* ## 用户要"全部超时做成可调"
|
||||
*
|
||||
* 所以这份同时是**界面数据的来源**(`GET /api/timeouts` 返回的就是它)。
|
||||
* 加一项超时:这里加一条 + Python 那份加一条,跑一次对拍脚本。
|
||||
*/
|
||||
|
||||
/** 默认值 —— 和 Python 的 `DEFAULT` 一一对应 */
|
||||
export const DEFAULT_TIMEOUTS = {
|
||||
// ---- 对话 ----
|
||||
model: 180,
|
||||
sseRead: 120,
|
||||
heartbeat: 15,
|
||||
kernelLocal: 10,
|
||||
// ---- 工具 ----
|
||||
tool: 60,
|
||||
shell: 30,
|
||||
pythonRun: 60,
|
||||
termux: 30,
|
||||
meetingNode: 10,
|
||||
customNode: 10,
|
||||
// ---- 界面(等用户)----
|
||||
askUser: 0,
|
||||
waitUi: 0,
|
||||
filePicker: 120,
|
||||
// ---- 语音 ----
|
||||
tts: 60,
|
||||
stt: 120,
|
||||
// ---- MCP ----
|
||||
mcpRequest: 60,
|
||||
mcpConnect: 180,
|
||||
mcpTool: 120,
|
||||
};
|
||||
|
||||
/** 每一项的说明(界面直接用) */
|
||||
export const TIMEOUT_SPECS = [
|
||||
{
|
||||
key: 'model', group: '对话', label: '等模型回复',
|
||||
default: 180, min: 10, max: 1800, unit: '秒',
|
||||
desc: '发出去到开始收到第一个字的最长等待。深度思考的模型(R1 这类)'
|
||||
+ '经常要想一分钟以上,调小了会频繁断在半路;本地小模型可以调大。',
|
||||
},
|
||||
{
|
||||
key: 'sseRead', group: '对话', label: '连接空闲上限',
|
||||
default: 120, min: 20, max: 3600, unit: '秒',
|
||||
desc: '手机上"多久没收到任何数据就断开"。内核每【心跳间隔】秒发一次心跳,'
|
||||
+ '所以这个值必须比心跳间隔大不少 —— 内核发的就是心跳间隔那项。'
|
||||
+ '弱网/代理会悄悄掐长连接,可以把两个都调小一点,断得早反而好重连。',
|
||||
},
|
||||
{
|
||||
key: 'heartbeat', group: '对话', label: '心跳间隔',
|
||||
default: 15, min: 5, max: 300, unit: '秒',
|
||||
desc: '对话过程中内核每隔这么久发一个空包,证明连接还活着。'
|
||||
+ '调大了在弱网下容易被中间设备当成死连接掐掉;调小了费电。',
|
||||
},
|
||||
{
|
||||
key: 'kernelLocal', group: '对话', label: '本地接口请求',
|
||||
default: 10, min: 3, max: 300, unit: '秒',
|
||||
desc: '界面调内核的普通接口(读会话、存设置、拉笔记)等多久。'
|
||||
+ '只有老设备 + 会话特别长时才会顶到这个值。',
|
||||
},
|
||||
{
|
||||
key: 'tool', group: '工具', label: '工具执行',
|
||||
default: 60, min: 0, max: 3600, unit: '秒', zero: '不限时',
|
||||
desc: '任意一个 Python 工具最多跑多久。这是"工具里写了死循环"的安全网。\n'
|
||||
+ '填 0 = 不限时。注意:会停下来等你的工具(AI 提问、AI 界面等待)'
|
||||
+ '本来就不受这一项限制 —— 它们的时长取决于你什么时候点。',
|
||||
},
|
||||
{
|
||||
key: 'shell', group: '工具', label: 'shell 命令',
|
||||
default: 30, min: 1, max: 3600, unit: '秒',
|
||||
desc: 'AI 调用内置 shell 工具跑一条命令的最长时间。'
|
||||
+ '装包(pkg install / pip install)这类会超过 30 秒,那时可以让 AI 自己传更长的值。',
|
||||
},
|
||||
{
|
||||
key: 'pythonRun', group: '工具', label: '跑 Python 代码',
|
||||
default: 60, min: 1, max: 3600, unit: '秒',
|
||||
desc: 'AI 写一段 Python 交给内核执行的最长时间。算得久(循环、大文件)就调大。',
|
||||
},
|
||||
{
|
||||
key: 'termux', group: '工具', label: 'Termux 命令',
|
||||
default: 30, min: 1, max: 3600, unit: '秒',
|
||||
desc: '在内置 Termux 环境里跑命令的最长时间。',
|
||||
},
|
||||
{
|
||||
key: 'meetingNode', group: '工具', label: '会议节点',
|
||||
default: 10, min: 1, max: 600, unit: '秒',
|
||||
desc: '会议里一个 Python 节点 / 自定义节点最多跑多久。'
|
||||
+ '这是防死循环的 —— 调太大会让整个会议卡住,建议别超过 60。',
|
||||
},
|
||||
{
|
||||
key: 'customNode', group: '工具', label: '自定义节点',
|
||||
default: 10, min: 1, max: 600, unit: '秒',
|
||||
desc: 'nodes/ 目录里自己写的节点,执行一次的最长时间。',
|
||||
},
|
||||
{
|
||||
key: 'askUser', group: '界面', label: 'AI 提问等多久',
|
||||
default: 0, min: 0, max: 3600, unit: '秒', zero: '不限时',
|
||||
desc: 'AI 弹选择框问你时,等多久算"你没空"。**默认 0 = 一直等**。\n'
|
||||
+ '不建议改:设成有限值后,你去忙一会儿回来,AI 已经按它自己猜的答案'
|
||||
+ '往下写了,而它并不知道自己在猜。',
|
||||
},
|
||||
{
|
||||
key: 'waitUi', group: '界面', label: 'AI 界面等多久',
|
||||
default: 0, min: 0, max: 3600, unit: '秒', zero: '不限时',
|
||||
desc: 'AI 弹的界面在 wait 模式下等你点按钮的时长。**默认 0 = 一直等**。',
|
||||
},
|
||||
{
|
||||
key: 'filePicker', group: '界面', label: '选文件等多久',
|
||||
default: 120, min: 10, max: 3600, unit: '秒',
|
||||
desc: 'AI 让你挑一个文件时,等你挑完的时长。'
|
||||
+ '去相册里翻一张老照片可能不止两分钟,可以调大。',
|
||||
},
|
||||
{
|
||||
key: 'tts', group: '语音', label: '语音合成',
|
||||
default: 60, min: 5, max: 600, unit: '秒',
|
||||
desc: '把回复转成语音的最长等待。长回复、网络慢时要更久。',
|
||||
},
|
||||
{
|
||||
key: 'stt', group: '语音', label: '语音识别',
|
||||
default: 120, min: 5, max: 600, unit: '秒',
|
||||
desc: '把录音转成文字的最长等待。录音越长越久。',
|
||||
},
|
||||
{
|
||||
key: 'mcpRequest', group: 'MCP', label: '单次请求',
|
||||
default: 60, min: 5, max: 600, unit: '秒',
|
||||
desc: '和 MCP 服务说话时,单次请求等多久。',
|
||||
},
|
||||
{
|
||||
key: 'mcpConnect', group: 'MCP', label: '连接',
|
||||
default: 180, min: 5, max: 900, unit: '秒',
|
||||
desc: '启动 MCP 服务(npx / uvx 第一次要下载)的最长等待。'
|
||||
+ '第一次装包经常要几分钟,这也是默认给到 180 秒的原因。',
|
||||
},
|
||||
{
|
||||
key: 'mcpTool', group: 'MCP', label: '调用工具',
|
||||
default: 120, min: 5, max: 900, unit: '秒',
|
||||
desc: '调用 MCP 服务里某个工具的最长等待。',
|
||||
},
|
||||
];
|
||||
|
||||
const BY_KEY = new Map(TIMEOUT_SPECS.map((s) => [s.key, s]));
|
||||
|
||||
/**
|
||||
* 把用户填的东西变成合法的秒数。
|
||||
* ★ 容错是必须的:界面上是输入框,可能填 "60秒"、空串、负数、巨大的数。
|
||||
*/
|
||||
export function coerceTimeout(key, raw) {
|
||||
const spec = BY_KEY.get(key);
|
||||
if (!spec) throw new Error(`没有这个超时项:${key}`);
|
||||
let value = spec.default;
|
||||
if (raw !== null && raw !== undefined && raw !== '') {
|
||||
const n = parseFloat(String(raw).trim().replace(/[秒sS]$/, ''));
|
||||
value = Number.isFinite(n) ? n : spec.default;
|
||||
}
|
||||
return Math.min(spec.max, Math.max(spec.min, value));
|
||||
}
|
||||
|
||||
/** 读一项(秒)。0 = 不限时 */
|
||||
export function timeoutOf(settings, key) {
|
||||
const stored = (settings && typeof settings.timeouts === 'object' && settings.timeouts) || {};
|
||||
return coerceTimeout(key, stored[key]);
|
||||
}
|
||||
|
||||
/** 当前生效的全部值 */
|
||||
export function resolvedTimeouts(settings) {
|
||||
const stored = (settings && typeof settings.timeouts === 'object' && settings.timeouts) || {};
|
||||
const out = {};
|
||||
for (const s of TIMEOUT_SPECS) out[s.key] = coerceTimeout(s.key, stored[s.key]);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 给界面的清单(说明 + 默认值 + 边界 + 现在是多少) */
|
||||
export function timeoutsCatalog(settings) {
|
||||
const stored = (settings && typeof settings.timeouts === 'object' && settings.timeouts) || {};
|
||||
return TIMEOUT_SPECS.map((s) => ({
|
||||
...s,
|
||||
value: coerceTimeout(s.key, stored[s.key]),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 算出"要存下来的那部分":只含和默认值不同的项。
|
||||
*
|
||||
* ★ 和 Python 的 `patch()` 同一套语义:改回默认 = 从设置里**删掉**这一项,
|
||||
* 这样以后调整默认值时,没动过的用户会跟着新默认走。
|
||||
*/
|
||||
export function patchTimeouts(settings, values) {
|
||||
const stored = (settings && typeof settings.timeouts === 'object' && settings.timeouts) || {};
|
||||
const out = { ...stored };
|
||||
if (!values || typeof values !== 'object') return out;
|
||||
for (const [key, raw] of Object.entries(values)) {
|
||||
if (!BY_KEY.has(key)) continue;
|
||||
const value = coerceTimeout(key, raw);
|
||||
if (Math.abs(value - BY_KEY.get(key).default) < 1e-9) delete out[key];
|
||||
else out[key] = Number.isInteger(value) ? value : value;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user