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

@@ -24,6 +24,7 @@ import threading
import mcp_client
import paths
import timeouts
from builtin_tools import BUILTIN_TOOLS, run_with_timeout
# 文件工具的缓存:path -> {'mtime':..., 'spec':..., 'error':...}
@@ -32,7 +33,9 @@ _cache_lock = threading.Lock()
# 已经 import 过的模块名(重载时要先从 sys.modules 里摘掉)
_loaded_modules: dict = {}
TOOL_TIMEOUT_S = 60
# ★ 工具的默认执行超时**不在这里** —— 它和别的 15 个超时一起收在
# `timeouts.py` 里,用户可以在「设置 → 超时」里调。这里以前写死 `TOOL_TIMEOUT_S = 60`,
# 现在一律 `timeouts.get('tool')`。
BUILTIN_FILE_LABEL = '<内置>'
# --------------------------------------------------------------------------- #
@@ -61,6 +64,41 @@ def _waits_for_user(name: str, spec: dict, module) -> bool:
return bool(getattr(module, 'WAIT_FOR_USER', False))
def _with_default_timeouts(name: str, spec: dict, module, payload: dict) -> dict:
"""
把设置里的默认超时**注入**给声明了要的工具。
## 工具是这么声明的
# tools/ask_user.py
TIMEOUT_KEYS = {'timeout': 'askUser'}
意思是:"我这个 `timeout` 参数如果模型没给,就用设置里 `askUser` 那一项的值。"
## 为什么要走这条路,而不是让工具自己 import timeouts
因为工具**不一定跑在内核进程里**:安卓端是进程内 import(能读),
电脑端是 `python tool.py` 起子进程(读不到内核的模块)。而工具是要跨端复用的,
所以不能让它们依赖内核的模块。
内核在**调用前**把值塞进参数里,工具照常从自己的参数读 —— 两端行为一致,
工具本身也不用知道"设置"这回事(单独跑、自测时用自己文件里的兜底默认值)。
"""
keys = getattr(module, 'TIMEOUT_KEYS', None)
if not isinstance(keys, dict) or not keys:
return payload
out = dict(payload)
for arg_name, key in keys.items():
# 模型给了就不覆盖(模型知道自己要等多久);给 0 也当"没给",
# 因为 0 是"不限时"的合法值,但默认参数里它和"没填"是同一个意思
if out.get(arg_name) in (None, '', 0, '0'):
try:
out[arg_name] = timeouts.get(key)
except Exception: # noqa: BLE001 - 设置读不出来就用工具自己的兜底
pass
return out
# --------------------------------------------------------------------------- #
# 界面工具的开关
@@ -328,6 +366,8 @@ def run_python_tool(name: str, args: dict) -> dict:
runner = getattr(module, 'run')
payload = args if isinstance(args, dict) else {}
# ★ 设置里的默认超时注入给声明了 TIMEOUT_KEYS 的工具(ask_user / show_ui)
payload = _with_default_timeouts(name, spec, module, payload)
# ★★★ **先抓住当前线程的上下文**,因为 run_with_timeout 会在**另一个线程**里
# 跑工具,而 threading.local() 是**不跨线程继承**的 —— 不搬过去的话,
# 工作线程里 current_context() 永远是空的。
@@ -351,14 +391,18 @@ def run_python_tool(name: str, args: dict) -> dict:
# ★ 会等用户的工具(ask_user / show_ui 的 wait 模式)**不限时** ——
# 它们的耗时取决于用户什么时候点,给上限等于"用户想久了 AI 就自己编"。
# 见 _waits_for_user 的注释。
budget = 0 if _waits_for_user(name, spec, module) else TOOL_TIMEOUT_S
# ★ 预算是**设置里可调**的(设置 → 超时 → 工具执行),不是写死的 60。
if _waits_for_user(name, spec, module):
budget = 0
else:
budget = timeouts.get('tool')
outcome = run_with_timeout(_invoke, budget)
except BaseException as exc: # noqa: BLE001 - 工具抛错要当成"工具失败",不是接口 500
# 与 Node 版一致:_protocol 把异常转成 {"ok":false,"error":"ValueError: xxx"},
# 这里在进程内直接捕获,格式保持一致(前端/模型看到的东西不变)。
return {'ok': False, 'error': f'{type(exc).__name__}: {exc}'}
if outcome is None:
return {'ok': False, 'error': f'工具执行超时({TOOL_TIMEOUT_S}s)'}
return {'ok': False, 'error': f'工具执行超时({budget:g}s,可在设置里调)'}
result = outcome
# 工具可以像 _protocol 那样返回 {"ok":..,"result":..},也可以直接返回业务结果
if isinstance(result, dict) and 'ok' in result: