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:
l
2026-10-16 10:29:46 +08:00
parent d82c3d3176
commit c7fed95650
32 changed files with 1523 additions and 70 deletions

View File

@@ -29,10 +29,37 @@ import urllib.error
import urllib.request
import paths
import timeouts
PROTOCOL_VERSION = '2024-11-05'
CLIENT_INFO = {'name': 'doubao-kernel-py', 'version': '0.1.0'}
# --------------------------------------------------------------------------- #
# 超时
# --------------------------------------------------------------------------- #
# ★ 这三个超时**不写死在这里** —— 它们在 timeouts.py 里,用户可以在
# 「设置 → 超时 → MCP」那一组里调。
#
# ★ 为什么是函数而不是默认参数值:Python 的默认参数**只在函数定义时求值一次**,
# 写成 `def request(self, message, timeout=timeouts.get('mcpRequest'))`
# 会把第一次读到的值永久钉住 —— 用户在设置里改完发现没生效,
# 而且这种 bug 光看代码看不出来(写法看起来很合理)。
# 所以默认值一律写 None,真正取值在这里做。
def _span_timeout(value, key: str, fallback: float) -> float:
if value:
return float(value)
try:
return float(timeouts.get(key))
except Exception: # noqa: BLE001 - 设置读不出来也不能让 MCP 用不了
return fallback
def _req_timeout(value) -> float:
"""单次请求的超时"""
return _span_timeout(value, 'mcpRequest', 60.0)
_clients: dict = {} # serverName -> {'spec','client','tools','error'}
_lock = threading.RLock()
@@ -204,7 +231,8 @@ class _HttpTransport:
self.session_id = sid
return resp
def request(self, message: dict, timeout: float = 60) -> dict:
def request(self, message: dict, timeout: float = None) -> dict:
timeout = _req_timeout(timeout)
resp = self._post(message, timeout)
try:
status = getattr(resp, 'status', 200)
@@ -575,7 +603,8 @@ class _StdioTransport:
self._closed = True
self._cv.notify_all()
def request(self, message: dict, timeout: float = 60) -> dict:
def request(self, message: dict, timeout: float = None) -> dict:
timeout = _req_timeout(timeout)
with self._cv:
if self._closed:
raise McpError('MCP 服务进程已退出')
@@ -704,7 +733,8 @@ class _InprocTransport:
self._closed = True
self._cv.notify_all()
def request(self, message: dict, timeout: float = 60) -> dict:
def request(self, message: dict, timeout: float = None) -> dict:
timeout = _req_timeout(timeout)
req_id = message.get('id')
with self._cv:
if self._closed:
@@ -805,7 +835,8 @@ class McpClient:
raise McpError(f'{method} 失败:{detail}')
return response.get('result')
def connect(self, timeout: float = 180) -> 'McpClient':
def connect(self, timeout: float = None) -> 'McpClient':
timeout = _span_timeout(timeout, 'mcpConnect', 180.0)
# initialize 允许失败:inproc 的最小实现可能只认 tools/list、tools/call
self._request(
'initialize',
@@ -818,11 +849,12 @@ class McpClient:
tolerate_error=True,
)
self.transport.notify({'jsonrpc': '2.0', 'method': 'notifications/initialized', 'params': {}})
listed = self._request('tools/list', {}, 60) or {}
listed = self._request('tools/list', {}, _req_timeout(None)) or {}
self.tools = listed.get('tools') or []
return self
def call_tool(self, name: str, arguments: dict, timeout: float = 120) -> dict:
def call_tool(self, name: str, arguments: dict, timeout: float = None) -> dict:
timeout = _span_timeout(timeout, 'mcpTool', 120.0)
result = self._request('tools/call', {'name': name, 'arguments': arguments or {}}, timeout)
return result or {}