@creatoria/miniapp-mcp 0.2.0 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/README.md +157 -73
  2. package/dist/capabilities/assert/handlers/assert-handlers.d.ts +98 -0
  3. package/dist/capabilities/assert/handlers/assert-handlers.d.ts.map +1 -0
  4. package/dist/capabilities/assert/handlers/assert-handlers.js +179 -0
  5. package/dist/capabilities/assert/handlers/assert-handlers.js.map +1 -0
  6. package/dist/capabilities/assert/handlers/index.d.ts +7 -0
  7. package/dist/capabilities/assert/handlers/index.d.ts.map +1 -0
  8. package/dist/capabilities/assert/handlers/index.js +7 -0
  9. package/dist/capabilities/assert/handlers/index.js.map +1 -0
  10. package/dist/capabilities/assert/index.d.ts +12 -2
  11. package/dist/capabilities/assert/index.d.ts.map +1 -1
  12. package/dist/capabilities/assert/index.js +87 -2
  13. package/dist/capabilities/assert/index.js.map +1 -1
  14. package/dist/capabilities/assert/schemas/index.d.ts +101 -0
  15. package/dist/capabilities/assert/schemas/index.d.ts.map +1 -0
  16. package/dist/capabilities/assert/schemas/index.js +62 -0
  17. package/dist/capabilities/assert/schemas/index.js.map +1 -0
  18. package/dist/capabilities/automator/handlers/close.d.ts +18 -0
  19. package/dist/capabilities/automator/handlers/close.d.ts.map +1 -0
  20. package/dist/capabilities/automator/handlers/close.js +45 -0
  21. package/dist/capabilities/automator/handlers/close.js.map +1 -0
  22. package/dist/capabilities/automator/handlers/connect.d.ts +24 -0
  23. package/dist/capabilities/automator/handlers/connect.d.ts.map +1 -0
  24. package/dist/capabilities/automator/handlers/connect.js +87 -0
  25. package/dist/capabilities/automator/handlers/connect.js.map +1 -0
  26. package/dist/capabilities/automator/handlers/disconnect.d.ts +16 -0
  27. package/dist/capabilities/automator/handlers/disconnect.d.ts.map +1 -0
  28. package/dist/capabilities/automator/handlers/disconnect.js +47 -0
  29. package/dist/capabilities/automator/handlers/disconnect.js.map +1 -0
  30. package/dist/capabilities/automator/handlers/index.d.ts +8 -0
  31. package/dist/capabilities/automator/handlers/index.d.ts.map +1 -0
  32. package/dist/capabilities/automator/handlers/index.js +8 -0
  33. package/dist/capabilities/automator/handlers/index.js.map +1 -0
  34. package/dist/capabilities/automator/handlers/launch.d.ts +30 -0
  35. package/dist/capabilities/automator/handlers/launch.d.ts.map +1 -0
  36. package/dist/capabilities/automator/handlers/launch.js +91 -0
  37. package/dist/capabilities/automator/handlers/launch.js.map +1 -0
  38. package/dist/capabilities/automator/index.d.ts +11 -2
  39. package/dist/capabilities/automator/index.d.ts.map +1 -1
  40. package/dist/capabilities/automator/index.js +51 -2
  41. package/dist/capabilities/automator/index.js.map +1 -1
  42. package/dist/capabilities/automator/schemas/index.d.ts +5 -0
  43. package/dist/capabilities/automator/schemas/index.d.ts.map +1 -1
  44. package/dist/capabilities/automator/schemas/index.js +6 -4
  45. package/dist/capabilities/automator/schemas/index.js.map +1 -1
  46. package/dist/capabilities/automator/schemas/launch.d.ts +3 -0
  47. package/dist/capabilities/automator/schemas/launch.d.ts.map +1 -1
  48. package/dist/capabilities/automator/schemas/launch.js +4 -0
  49. package/dist/capabilities/automator/schemas/launch.js.map +1 -1
  50. package/dist/capabilities/element/handlers/element-handlers.d.ts +212 -0
  51. package/dist/capabilities/element/handlers/element-handlers.d.ts.map +1 -0
  52. package/dist/capabilities/element/handlers/element-handlers.js +383 -0
  53. package/dist/capabilities/element/handlers/element-handlers.js.map +1 -0
  54. package/dist/capabilities/element/handlers/index.d.ts +7 -0
  55. package/dist/capabilities/element/handlers/index.d.ts.map +1 -0
  56. package/dist/capabilities/element/handlers/index.js +27 -0
  57. package/dist/capabilities/element/handlers/index.js.map +1 -0
  58. package/dist/capabilities/element/index.d.ts +17 -2
  59. package/dist/capabilities/element/index.d.ts.map +1 -1
  60. package/dist/capabilities/element/index.js +196 -2
  61. package/dist/capabilities/element/index.js.map +1 -1
  62. package/dist/capabilities/element/schemas/index.d.ts +325 -0
  63. package/dist/capabilities/element/schemas/index.d.ts.map +1 -0
  64. package/dist/capabilities/element/schemas/index.js +168 -0
  65. package/dist/capabilities/element/schemas/index.js.map +1 -0
  66. package/dist/capabilities/index.d.ts +23 -4
  67. package/dist/capabilities/index.d.ts.map +1 -1
  68. package/dist/capabilities/index.js +29 -3
  69. package/dist/capabilities/index.js.map +1 -1
  70. package/dist/capabilities/loader.d.ts +61 -0
  71. package/dist/capabilities/loader.d.ts.map +1 -0
  72. package/dist/capabilities/loader.js +164 -0
  73. package/dist/capabilities/loader.js.map +1 -0
  74. package/dist/capabilities/miniprogram/handlers/call-wx.d.ts +24 -0
  75. package/dist/capabilities/miniprogram/handlers/call-wx.d.ts.map +1 -0
  76. package/dist/capabilities/miniprogram/handlers/call-wx.js +42 -0
  77. package/dist/capabilities/miniprogram/handlers/call-wx.js.map +1 -0
  78. package/dist/capabilities/miniprogram/handlers/evaluate.d.ts +32 -0
  79. package/dist/capabilities/miniprogram/handlers/evaluate.d.ts.map +1 -0
  80. package/dist/capabilities/miniprogram/handlers/evaluate.js +56 -0
  81. package/dist/capabilities/miniprogram/handlers/evaluate.js.map +1 -0
  82. package/dist/capabilities/miniprogram/handlers/index.d.ts +12 -0
  83. package/dist/capabilities/miniprogram/handlers/index.d.ts.map +1 -0
  84. package/dist/capabilities/miniprogram/handlers/index.js +12 -0
  85. package/dist/capabilities/miniprogram/handlers/index.js.map +1 -0
  86. package/dist/capabilities/miniprogram/handlers/navigate.d.ts +26 -0
  87. package/dist/capabilities/miniprogram/handlers/navigate.d.ts.map +1 -0
  88. package/dist/capabilities/miniprogram/handlers/navigate.js +61 -0
  89. package/dist/capabilities/miniprogram/handlers/navigate.js.map +1 -0
  90. package/dist/capabilities/miniprogram/handlers/page-stack.d.ts +20 -0
  91. package/dist/capabilities/miniprogram/handlers/page-stack.d.ts.map +1 -0
  92. package/dist/capabilities/miniprogram/handlers/page-stack.js +42 -0
  93. package/dist/capabilities/miniprogram/handlers/page-stack.js.map +1 -0
  94. package/dist/capabilities/miniprogram/handlers/screenshot.d.ts +33 -0
  95. package/dist/capabilities/miniprogram/handlers/screenshot.d.ts.map +1 -0
  96. package/dist/capabilities/miniprogram/handlers/screenshot.js +146 -0
  97. package/dist/capabilities/miniprogram/handlers/screenshot.js.map +1 -0
  98. package/dist/capabilities/miniprogram/handlers/system-info.d.ts +17 -0
  99. package/dist/capabilities/miniprogram/handlers/system-info.d.ts.map +1 -0
  100. package/dist/capabilities/miniprogram/handlers/system-info.js +36 -0
  101. package/dist/capabilities/miniprogram/handlers/system-info.js.map +1 -0
  102. package/dist/capabilities/miniprogram/index.d.ts +12 -2
  103. package/dist/capabilities/miniprogram/index.d.ts.map +1 -1
  104. package/dist/capabilities/miniprogram/index.js +66 -2
  105. package/dist/capabilities/miniprogram/index.js.map +1 -1
  106. package/dist/capabilities/miniprogram/schemas/call-wx.d.ts +15 -0
  107. package/dist/capabilities/miniprogram/schemas/call-wx.d.ts.map +1 -0
  108. package/dist/capabilities/miniprogram/schemas/call-wx.js +11 -0
  109. package/dist/capabilities/miniprogram/schemas/call-wx.js.map +1 -0
  110. package/dist/capabilities/miniprogram/schemas/evaluate.d.ts +15 -0
  111. package/dist/capabilities/miniprogram/schemas/evaluate.d.ts.map +1 -0
  112. package/dist/capabilities/miniprogram/schemas/evaluate.js +11 -0
  113. package/dist/capabilities/miniprogram/schemas/evaluate.js.map +1 -0
  114. package/dist/capabilities/miniprogram/schemas/index.d.ts +10 -0
  115. package/dist/capabilities/miniprogram/schemas/index.d.ts.map +1 -0
  116. package/dist/capabilities/miniprogram/schemas/index.js +10 -0
  117. package/dist/capabilities/miniprogram/schemas/index.js.map +1 -0
  118. package/dist/capabilities/miniprogram/schemas/navigate.d.ts +18 -0
  119. package/dist/capabilities/miniprogram/schemas/navigate.d.ts.map +1 -0
  120. package/dist/capabilities/miniprogram/schemas/navigate.js +22 -0
  121. package/dist/capabilities/miniprogram/schemas/navigate.js.map +1 -0
  122. package/dist/capabilities/miniprogram/schemas/page-stack.d.ts +6 -0
  123. package/dist/capabilities/miniprogram/schemas/page-stack.d.ts.map +1 -0
  124. package/dist/capabilities/miniprogram/schemas/page-stack.js +6 -0
  125. package/dist/capabilities/miniprogram/schemas/page-stack.js.map +1 -0
  126. package/dist/capabilities/miniprogram/schemas/screenshot.d.ts +18 -0
  127. package/dist/capabilities/miniprogram/schemas/screenshot.d.ts.map +1 -0
  128. package/dist/capabilities/miniprogram/schemas/screenshot.js +21 -0
  129. package/dist/capabilities/miniprogram/schemas/screenshot.js.map +1 -0
  130. package/dist/capabilities/miniprogram/schemas/system-info.d.ts +6 -0
  131. package/dist/capabilities/miniprogram/schemas/system-info.d.ts.map +1 -0
  132. package/dist/capabilities/miniprogram/schemas/system-info.js +6 -0
  133. package/dist/capabilities/miniprogram/schemas/system-info.js.map +1 -0
  134. package/dist/capabilities/network/handlers/index.d.ts +7 -0
  135. package/dist/capabilities/network/handlers/index.d.ts.map +1 -0
  136. package/dist/capabilities/network/handlers/index.js +7 -0
  137. package/dist/capabilities/network/handlers/index.js.map +1 -0
  138. package/dist/capabilities/network/handlers/network-handlers.d.ts +53 -0
  139. package/dist/capabilities/network/handlers/network-handlers.d.ts.map +1 -0
  140. package/dist/capabilities/network/handlers/network-handlers.js +127 -0
  141. package/dist/capabilities/network/handlers/network-handlers.js.map +1 -0
  142. package/dist/capabilities/network/index.d.ts +12 -2
  143. package/dist/capabilities/network/index.d.ts.map +1 -1
  144. package/dist/capabilities/network/index.js +66 -2
  145. package/dist/capabilities/network/index.js.map +1 -1
  146. package/dist/capabilities/network/schemas/index.d.ts +53 -0
  147. package/dist/capabilities/network/schemas/index.d.ts.map +1 -0
  148. package/dist/capabilities/network/schemas/index.js +33 -0
  149. package/dist/capabilities/network/schemas/index.js.map +1 -0
  150. package/dist/capabilities/page/handlers/index.d.ts +7 -0
  151. package/dist/capabilities/page/handlers/index.d.ts.map +1 -0
  152. package/dist/capabilities/page/handlers/index.js +7 -0
  153. package/dist/capabilities/page/handlers/index.js.map +1 -0
  154. package/dist/capabilities/page/handlers/page-handlers.d.ts +165 -0
  155. package/dist/capabilities/page/handlers/page-handlers.d.ts.map +1 -0
  156. package/dist/capabilities/page/handlers/page-handlers.js +258 -0
  157. package/dist/capabilities/page/handlers/page-handlers.js.map +1 -0
  158. package/dist/capabilities/page/index.d.ts +12 -2
  159. package/dist/capabilities/page/index.d.ts.map +1 -1
  160. package/dist/capabilities/page/index.js +80 -2
  161. package/dist/capabilities/page/index.js.map +1 -1
  162. package/dist/capabilities/page/schemas/index.d.ts +91 -0
  163. package/dist/capabilities/page/schemas/index.d.ts.map +1 -0
  164. package/dist/capabilities/page/schemas/index.js +55 -0
  165. package/dist/capabilities/page/schemas/index.js.map +1 -0
  166. package/dist/capabilities/record/handlers/index.d.ts +7 -0
  167. package/dist/capabilities/record/handlers/index.d.ts.map +1 -0
  168. package/dist/capabilities/record/handlers/index.js +7 -0
  169. package/dist/capabilities/record/handlers/index.js.map +1 -0
  170. package/dist/capabilities/record/handlers/record-handlers.d.ts +76 -0
  171. package/dist/capabilities/record/handlers/record-handlers.d.ts.map +1 -0
  172. package/dist/capabilities/record/handlers/record-handlers.js +255 -0
  173. package/dist/capabilities/record/handlers/record-handlers.js.map +1 -0
  174. package/dist/capabilities/record/index.d.ts +12 -2
  175. package/dist/capabilities/record/index.d.ts.map +1 -1
  176. package/dist/capabilities/record/index.js +66 -2
  177. package/dist/capabilities/record/index.js.map +1 -1
  178. package/dist/capabilities/record/schemas/index.d.ts +47 -0
  179. package/dist/capabilities/record/schemas/index.d.ts.map +1 -0
  180. package/dist/capabilities/record/schemas/index.js +33 -0
  181. package/dist/capabilities/record/schemas/index.js.map +1 -0
  182. package/dist/capabilities/registry.d.ts +138 -0
  183. package/dist/capabilities/registry.d.ts.map +1 -0
  184. package/dist/capabilities/registry.js +261 -0
  185. package/dist/capabilities/registry.js.map +1 -0
  186. package/dist/capabilities/snapshot/handlers/index.d.ts +7 -0
  187. package/dist/capabilities/snapshot/handlers/index.d.ts.map +1 -0
  188. package/dist/capabilities/snapshot/handlers/index.js +7 -0
  189. package/dist/capabilities/snapshot/handlers/index.js.map +1 -0
  190. package/dist/capabilities/snapshot/handlers/snapshot-handlers.d.ts +75 -0
  191. package/dist/capabilities/snapshot/handlers/snapshot-handlers.d.ts.map +1 -0
  192. package/dist/capabilities/snapshot/handlers/snapshot-handlers.js +201 -0
  193. package/dist/capabilities/snapshot/handlers/snapshot-handlers.js.map +1 -0
  194. package/dist/capabilities/snapshot/index.d.ts +12 -2
  195. package/dist/capabilities/snapshot/index.d.ts.map +1 -1
  196. package/dist/capabilities/snapshot/index.js +45 -2
  197. package/dist/capabilities/snapshot/index.js.map +1 -1
  198. package/dist/capabilities/snapshot/schemas/index.d.ts +47 -0
  199. package/dist/capabilities/snapshot/schemas/index.d.ts.map +1 -0
  200. package/dist/capabilities/snapshot/schemas/index.js +28 -0
  201. package/dist/capabilities/snapshot/schemas/index.js.map +1 -0
  202. package/dist/config/defaults.d.ts +4 -0
  203. package/dist/config/defaults.d.ts.map +1 -1
  204. package/dist/config/defaults.js +8 -0
  205. package/dist/config/defaults.js.map +1 -1
  206. package/dist/config/loader.d.ts +3 -0
  207. package/dist/config/loader.d.ts.map +1 -1
  208. package/dist/config/loader.js +21 -0
  209. package/dist/config/loader.js.map +1 -1
  210. package/dist/runtime/concurrency/mutex.d.ts +29 -0
  211. package/dist/runtime/concurrency/mutex.d.ts.map +1 -0
  212. package/dist/runtime/concurrency/mutex.js +46 -0
  213. package/dist/runtime/concurrency/mutex.js.map +1 -0
  214. package/dist/runtime/index.d.ts +2 -1
  215. package/dist/runtime/index.d.ts.map +1 -1
  216. package/dist/runtime/index.js +2 -1
  217. package/dist/runtime/index.js.map +1 -1
  218. package/dist/runtime/logging/tool-logger.js +2 -2
  219. package/dist/runtime/logging/tool-logger.js.map +1 -1
  220. package/dist/runtime/network/probe-port.d.ts +15 -0
  221. package/dist/runtime/network/probe-port.d.ts.map +1 -0
  222. package/dist/runtime/network/probe-port.js +33 -0
  223. package/dist/runtime/network/probe-port.js.map +1 -0
  224. package/dist/runtime/retry/index.d.ts +5 -0
  225. package/dist/runtime/retry/index.d.ts.map +1 -0
  226. package/dist/runtime/retry/index.js +5 -0
  227. package/dist/runtime/retry/index.js.map +1 -0
  228. package/dist/runtime/retry/retry.d.ts +61 -0
  229. package/dist/runtime/retry/retry.d.ts.map +1 -0
  230. package/dist/runtime/retry/retry.js +90 -0
  231. package/dist/runtime/retry/retry.js.map +1 -0
  232. package/dist/runtime/timeout/timeout.d.ts +10 -0
  233. package/dist/runtime/timeout/timeout.d.ts.map +1 -1
  234. package/dist/runtime/timeout/timeout.js +17 -5
  235. package/dist/runtime/timeout/timeout.js.map +1 -1
  236. package/dist/server.d.ts.map +1 -1
  237. package/dist/server.js +7 -6
  238. package/dist/server.js.map +1 -1
  239. package/dist/tools/index.d.ts.map +1 -1
  240. package/dist/tools/index.js +7 -3
  241. package/dist/tools/index.js.map +1 -1
  242. package/dist/tools/miniprogram.d.ts +11 -1
  243. package/dist/tools/miniprogram.d.ts.map +1 -1
  244. package/dist/tools/miniprogram.js +102 -20
  245. package/dist/tools/miniprogram.js.map +1 -1
  246. package/dist/types.d.ts +5 -0
  247. package/dist/types.d.ts.map +1 -1
  248. package/docs/current/01-architecture-migration.md +337 -0
  249. package/docs/current/02-screenshot-timeout-fix.md +589 -0
  250. package/docs/current/03-connect-screenshot-timeout-diagnosis.md +168 -0
  251. package/docs/current/04-toolchain-version-baseline.md +123 -0
  252. package/docs/current/README.md +229 -0
  253. package/package.json +2 -2
@@ -0,0 +1,168 @@
1
+ # 连接 / 截图超时问题诊断与优化方案
2
+
3
+ > 版本: 1.0.0
4
+ > 日期: 2026-05-31
5
+ > 状态: 🔍 诊断完成,待修复
6
+
7
+ ## 0. 调研结论速览(TL;DR)
8
+
9
+ | 任务 | 结论 |
10
+ |------|------|
11
+ | **1. 代码是否需要更新以适配小程序 CLI** | ⚠️ 需要少量更新。SDK `miniprogram-automator@0.12.1` 是当前最新可用版本,CLI 路径/端口适配本身没问题,但 **launch 传入的 `timeout` 参数 SDK 内部根本没使用**,connect 的 `wsEndpoint` 在不传 port 时传了 `undefined` 会触发 SDK 异常路径。 |
12
+ | **2. 修复现有超时问题** | 🔴 必须修复。**根因是 `connect`/`launch` 两个 handler 完全没有超时/重试保护**,连接超时卡死的概率最高;截图虽已有保护,但 `withTimeout` 实现有「超时后底层 promise 不取消」的泄漏缺陷。 |
13
+
14
+ ---
15
+
16
+ ## 1. 问题现象
17
+
18
+ - 调用 `automator_connect` / `automator_launch` 经常卡住无响应,最终 MCP 调用超时
19
+ - 调用 `miniprogram_screenshot`(尤其 `fullPage`)偶发卡顿
20
+ - 卡死后后续工具调用全部被阻塞
21
+
22
+ ---
23
+
24
+ ## 2. 根因分析(基于 SDK 源码逐行确认)
25
+
26
+ ### 2.1 🔴 P0:connect / launch 无任何超时保护(连接卡死的主因)
27
+
28
+ **位置**:
29
+ - `src/capabilities/automator/handlers/connect.ts:47`
30
+ - `src/capabilities/automator/handlers/launch.ts:58`
31
+
32
+ 两个 handler 都是直接 `await automator.connect()/launch()`,**没有 `withTimeout` 也没有 `withRetry`**,与 screenshot/evaluate 的保护策略不一致。
33
+
34
+ #### SDK connect 内部行为(`node_modules/miniprogram-automator/out/Launcher.js` + `Connection.js`)
35
+
36
+ ```
37
+ connect(opts) → connectTool(opts) → Connection.create(wsEndpoint) → new ws(endpoint)
38
+ ```
39
+
40
+ `Connection.create` 仅 `new ws(e)` 并监听 `open`,**没有连接超时**。
41
+ 当出现以下情况时,`new ws()` 会永久挂起、既不 resolve 也不 reject:
42
+ - 端口可达(TCP 层通),但 DevTools 未开启「CLI/HTTP 调用」
43
+ - DevTools 进程假死 / 自动化端口被占用但无响应
44
+ - 防火墙吞包(SYN 通、握手后无数据)
45
+
46
+ → **这是「连接经常超时」的根因**:超时不是发生在我们这层,而是 SDK 的 WebSocket 永远不返回,MCP 层只能等到上游(MCP client)整体超时。
47
+
48
+ #### SDK launch 内部行为(已逐行核验 Launcher.js + licia/waitUntil.js)
49
+
50
+ ```
51
+ launch() → spawn(cli) → waitUntil(condition, timeout=30s, interval=1s) → checkVersion() → sleep(5000) → 返回
52
+ ```
53
+
54
+ condition 内部:`p(spawn error) || f(exit+15s 超时标志) || (d = await connectTool({wsEndpoint:'ws://127.0.0.1:'+port}))`
55
+
56
+ **关键缺陷(决定性)**:`licia/waitUntil` 的超时判断 `elapsed >= timeout` **只在 condition() 这个 Promise resolve 之后**才执行:
57
+
58
+ ```js
59
+ evalCondition().then(function(val){
60
+ var elapsed = now() - startTime
61
+ if (val) resolve(val)
62
+ else if (timeout && elapsed >= timeout) reject(...) // ← 只有 condition 返回后才走到
63
+ else setTimeout(pollCondition, interval)
64
+ }, reject)
65
+ ```
66
+
67
+ 而 condition 里 `await connectTool()` → `Connection.create()` → `new ws()`。**端口通但 DevTools 无响应时 `new ws()` 永不 resolve → condition() 永不返回 → 30s 超时判断永远执行不到 → launch 永久挂死。SDK 的 30s `timeout` 在最常见故障场景下完全失效。**
68
+
69
+ - `ILaunchOptions.timeout`(默认 `3e4`)**确实被用作 waitUntil 的总超时**(我初版误判为"未引用",此处已纠正);但因上面缺陷,它只在「CLI 进程起不来 / 端口轮询返回 false」时生效,对「ws 挂起」无效
70
+ - CLI 进程 `exit` 后再过 **15s** 才把 `f=!0` 置位(仅覆盖进程崩溃场景)
71
+ - 因此 **launch 与 connect 共享同一根因**:都死在 `Connection.create` 那个无超时的 `new ws()`
72
+
73
+ ### 2.2 🟠 P1:`withTimeout` 超时后底层 promise 不取消(资源泄漏 + 误导性日志)
74
+
75
+ **位置**:`src/runtime/timeout/timeout.ts:20-44`
76
+
77
+ ```typescript
78
+ const result = await Promise.race([promise, timeoutPromise])
79
+ ```
80
+
81
+ `Promise.race` 在超时时只是让外层 race 出结果,**底层 SDK 调用仍在继续运行**。后果:
82
+ 1. SDK `Connection` 内部用自增 `id` 累积 pending callback(`Connection.js:30,46`),超时后这些 callback 永不清理 → 内存泄漏
83
+ 2. 截图重试时,上一次「超时」的截图请求其实还在跑,与重试请求并发,**加重 DevTools 负载**,让超时更易复现
84
+ 3. `setTimeout` 定时器未 `unref()`,进程退出时可能被 timer 拖住
85
+
86
+ ### 2.3 🟡 P2:connect 的 wsEndpoint 默认值缺陷
87
+
88
+ **位置**:`src/capabilities/automator/handlers/connect.ts:47-49`
89
+
90
+ ```typescript
91
+ const miniProgram = await automator.connect({
92
+ wsEndpoint: port ? `ws://localhost:${port}` : undefined, // ← undefined
93
+ } as any)
94
+ ```
95
+
96
+ `IConnectOptions.wsEndpoint` 是**必填 string**。传 `undefined` 时 SDK 走 `new ws(undefined)`,行为未定义(多数情况立刻抛错或挂起),错误信息也不友好。应在 handler 层兜底默认端口 9420。
97
+
98
+ ### 2.4 🟡 P2:launch 未利用 connectTimeout 配置 + 缺少 connectTimeout 配置项
99
+
100
+ `SessionConfig` 有 `launchTimeout`/`screenshotTimeout`,但 **没有 `connectTimeout`**,且 launch handler 没有读取 `launchTimeout` 来做外层 race 兜底。
101
+
102
+ ---
103
+
104
+ ## 3. 修复方案
105
+
106
+ ### 3.1 P0:给 connect / launch 加超时 + 重试保护
107
+
108
+ **connect.ts**:
109
+ - `wsEndpoint` 不传 port 时兜底为 `ws://127.0.0.1:9420`(与 SDK 默认端口一致,用 127.0.0.1 避免 localhost 解析到 IPv6 ::1 导致连不上)
110
+ - 用 `withTimeout` 包裹,默认 `connectTimeout = 30s`
111
+ - 用 `withRetry` 包裹,`maxRetries: 1`,`shouldRetry: onConnectionError`
112
+
113
+ **launch.ts**:
114
+ - 用 `withTimeout` 包裹,默认 `launchTimeout = 60s`(SDK 内部轮询 + sleep(5s) 较慢,给足余量)
115
+ - 不再依赖 SDK 失效的 `timeout` 参数,改由外层 race 兜底
116
+ - 超时时给出明确指引:"请检查 DevTools 是否已打开、自动化端口是否开启"
117
+
118
+ ### 3.2 P1:修复 withTimeout(unref + 可选 onTimeout 清理)
119
+
120
+ ```typescript
121
+ export async function withTimeout<T>(
122
+ promise: Promise<T>,
123
+ timeoutMs: number,
124
+ operation: string
125
+ ): Promise<T> {
126
+ let timeoutId: NodeJS.Timeout
127
+ const timeoutPromise = new Promise<T>((_, reject) => {
128
+ timeoutId = setTimeout(() => {
129
+ reject(new Error(`${operation} timed out after ${timeoutMs}ms`))
130
+ }, timeoutMs)
131
+ // 避免 timer 阻止进程退出
132
+ if (typeof timeoutId.unref === 'function') timeoutId.unref()
133
+ })
134
+ try {
135
+ return await Promise.race([promise, timeoutPromise])
136
+ } finally {
137
+ clearTimeout(timeoutId!)
138
+ }
139
+ }
140
+ ```
141
+
142
+ > 注:底层 SDK promise 无法真正 abort(SDK 未暴露 cancel),但 `unref` + `finally clearTimeout` 已能消除 timer 泄漏。SDK pending callback 泄漏需 SDK 层支持,列为已知限制。
143
+
144
+ ### 3.3 P2:新增 connectTimeout 配置项
145
+
146
+ - `types.ts` `SessionConfig` 增加 `connectTimeout?: number`
147
+ - `config/defaults.ts` 增加默认值 30000
148
+ - `config/loader.ts` 支持 `MCP_CONNECT_TIMEOUT` 环境变量
149
+ - `timeout.ts` `DEFAULT_TIMEOUTS.connect` 已存在(30000),复用
150
+
151
+ ---
152
+
153
+ ## 4. 任务 1(适配 CLI)结论详述
154
+
155
+ `miniprogram-automator@0.12.1` 已是该包目前可用最新版本,对接的是微信开发者工具 CLI(`/Applications/wechatwebdevtools.app/Contents/MacOS/cli`),CLI 接口长期稳定,**不需要为「适配新版 CLI」做大改**。需要做的「代码更新」仅限:
156
+
157
+ 1. launch 应显式传 `timeout`(= launchTimeout),SDK 确实会用它做轮询总超时;但因 SDK ws 挂起缺陷,仍需外层 race 兜底
158
+ 2. connect `wsEndpoint` 兜底默认端口(见 3.3),并改用 `127.0.0.1` 与 SDK launch 内部一致(避免 localhost → IPv6 ::1)
159
+ 3. 在 handler 层补齐超时/重试(见 3.1),这才是 CLI 交互不稳定时的真正防护
160
+
161
+ ---
162
+
163
+ ## 5. 验证计划
164
+
165
+ - 单元测试:connect/launch handler 在底层 promise 永不 resolve 时,能在 `connectTimeout/launchTimeout` 内抛出明确错误(用 fake miniProgram + 永挂 promise)
166
+ - 单元测试:`withTimeout` 超时后 timer 被清理(无 open handle)
167
+ - 回归:`pnpm test:unit` 全绿、`pnpm typecheck` 通过
168
+ - 集成(需真机 DevTools):故意不开自动化端口,确认 connect 在 30s 内失败而非永久挂起
@@ -0,0 +1,123 @@
1
+ # 工具链版本基线与超时配置说明
2
+
3
+ > 最后更新: 2026-05-31
4
+ > 版本: 0.3.0 (规划中)
5
+
6
+ ---
7
+
8
+ ## 工具链版本基线
9
+
10
+ | 组件 | 当前版本 | 状态 | 说明 |
11
+ |------|---------|------|------|
12
+ | miniprogram-automator | 0.12.1 (2023-11-07) | 已停更(约两年半,changelog 停在 0.12.0 / 2022-09) | 自动化能力仍可用,但不会有新特性 |
13
+ | 微信开发者工具 | v2.01.2510260 (2025-10) | 活跃迭代 | 推荐使用最新稳定版 |
14
+ | CLI auto 最低工具版本 | 1.05.2111232 | 最低要求 | 低于此版本不支持 auto 命令 |
15
+ | Node.js | >= 18.0.0 | 必需 | 运行时环境 |
16
+
17
+ ---
18
+
19
+ ## CLI 命令与路径
20
+
21
+ ### 命令格式
22
+
23
+ ```bash
24
+ cli auto --project <path> --auto-port <port> [--auto-account <openid>]
25
+ ```
26
+
27
+ ### CLI 路径
28
+
29
+ **macOS:**
30
+
31
+ ```text
32
+ /Applications/wechatwebdevtools.app/Contents/MacOS/cli
33
+ ```
34
+
35
+ **Windows:**
36
+
37
+ ```text
38
+ <安装路径>/cli.bat
39
+ ```
40
+
41
+ ### 启用自动化
42
+
43
+ 在开发者工具中:**设置 → 安全 → 开启 CLI/HTTP 调用**。
44
+
45
+ 默认自动化端口:`9420`。
46
+
47
+ ---
48
+
49
+ ## 超时配置说明
50
+
51
+ ### 配置项一览
52
+
53
+ | 配置项 | SessionConfig 字段 | 默认值 | 作用范围 |
54
+ |--------|-------------------|--------|---------|
55
+ | 全局操作超时 | `timeout` | 30s | 导航、wait 等通用操作 |
56
+ | JS 执行超时 | `evaluateTimeout` | 5s | evaluate / wx 调用 |
57
+ | 启动超时 | `launchTimeout` | 60s | miniprogram_launch |
58
+ | 连接超时 | `connectTimeout` | 30s | miniprogram_connect |
59
+ | 截图超时 | `screenshotTimeout` | 10s | 普通截图;fullPage 截图基础超时也基于此值 |
60
+
61
+ ### 配置方式
62
+
63
+ #### 方式一:.mcp.json 配置文件(推荐)
64
+
65
+ 在项目根目录创建 `.mcp.json`,写入超时字段(单位:毫秒):
66
+
67
+ ```json
68
+ {
69
+ "projectPath": "/path/to/miniprogram",
70
+ "cliPath": "/Applications/wechatwebdevtools.app/Contents/MacOS/cli",
71
+ "port": 9420,
72
+ "timeout": 30000,
73
+ "evaluateTimeout": 5000,
74
+ "launchTimeout": 60000,
75
+ "connectTimeout": 30000,
76
+ "screenshotTimeout": 10000,
77
+ "capabilities": ["core", "assert", "snapshot", "record", "network"]
78
+ }
79
+ ```
80
+
81
+ #### 方式二:CLI 参数
82
+
83
+ 通过命令行参数传递项目路径和端口:
84
+
85
+ ```bash
86
+ npx -y @creatoria/miniapp-mcp --project-path /path/to/miniprogram --port 9420
87
+ ```
88
+
89
+ 超时参数可通过配置文件设置。
90
+
91
+ #### 方式三:环境变量
92
+
93
+ 已确认支持的环境变量:
94
+
95
+ ```bash
96
+ export MCP_PROJECT_PATH=/path/to/miniprogram
97
+ export MCP_PORT=9420
98
+ export MCP_CAPABILITIES=core,assert,snapshot
99
+ ```
100
+
101
+ 超时相关配置可通过 `.mcp.json` 配置文件设置。
102
+
103
+ ### 调优建议
104
+
105
+ - **网络慢 / 真机调试**:适当调大 `connectTimeout` 和 `launchTimeout`,避免连接阶段超时。
106
+ - **fullPage 截图慢**:fullPage 截图需要截取整个页面内容,耗时通常比普通截图更长,可调大 `screenshotTimeout`。
107
+ - **evaluate 执行慢**:如果小程序逻辑复杂,evaluate 可能需要更多时间,可调大 `evaluateTimeout`。
108
+
109
+ ---
110
+
111
+ ## 兼容性与影响
112
+
113
+ - `miniprogram-automator` SDK 已停更,但现有自动化功能仍然可用,不影响当前项目运行。
114
+ - 微信开发者工具处于活跃迭代中,新版本可能带来协议变化,需关注更新日志。
115
+ - CLI auto 命令要求开发者工具版本 >= 1.05.2111232,低于此版本需先升级。
116
+
117
+ ---
118
+
119
+ ## 相关文档
120
+
121
+ - [故障排查指南](../troubleshooting.md)
122
+ - [连接与截图超时诊断](./03-connect-screenshot-timeout-diagnosis.md)
123
+ - [微信小程序工具链版本调研](../../调研中/微信小程序工具链最新版本调研/README.md)
@@ -0,0 +1,229 @@
1
+ # 当前待实施改动总览
2
+
3
+ > 最后更新: 2025-12-27
4
+ > 版本: 0.3.0 (规划中)
5
+
6
+ ## 概述
7
+
8
+ 本目录包含两个待实施的改动计划:
9
+
10
+ 1. **架构迁移**:完全使用新架构(capabilities + runtime)
11
+ 2. **截图修复**:解决截图超时卡住问题
12
+
13
+ ---
14
+
15
+ ## 改动一览
16
+
17
+ | 改动 | 类型 | 优先级 | 预计工时 | 文档 |
18
+ |------|------|--------|---------|------|
19
+ | 架构迁移 | 重构 | 高 | 18-25h | [01-architecture-migration.md](./01-architecture-migration.md) |
20
+ | 截图修复 | Bug Fix | 紧急 | 8-9h | [02-screenshot-timeout-fix.md](./02-screenshot-timeout-fix.md) |
21
+
22
+ ---
23
+
24
+ ## 改动一:架构迁移
25
+
26
+ ### 背景
27
+
28
+ 项目当前处于混合架构状态:
29
+ - 运行时服务 (runtime/) 已完成迁移:100%
30
+ - 工具实现 (tools/) 完全未迁移:0%
31
+ - 能力框架 (capabilities/) 仅作为代理:50%
32
+
33
+ ### 目标
34
+
35
+ 将 5,244 行工具代码从 `src/tools/` 迁移到 `src/capabilities/`,实现:
36
+ - 模块化工具组织
37
+ - Schema 驱动的输入验证
38
+ - 动态工具加载
39
+ - 删除旧的 tools/ 和 core/ 目录
40
+
41
+ ### 核心改动
42
+
43
+ ```
44
+ 当前结构: 目标结构:
45
+ src/ src/
46
+ ├── tools/ ❌ 5,244行 ├── capabilities/ 🆕 模块化
47
+ │ ├── index.ts 1,645行 │ ├── automator/
48
+ │ ├── automator.ts │ │ ├── schemas/
49
+ │ ├── miniprogram.ts │ │ └── handlers/
50
+ │ └── ... │ ├── miniprogram/
51
+ ├── core/ ❌ 兼容层 │ │ ├── schemas/
52
+ │ └── *.ts → runtime │ │ └── handlers/
53
+ └── runtime/ ✅ 保持 │ ├── page/
54
+ │ ├── element/
55
+ │ ├── loader.ts 🆕
56
+ │ └── registry.ts 🆕
57
+ └── runtime/ ✅ 保持
58
+ ```
59
+
60
+ ### 工作量
61
+
62
+ | 阶段 | 内容 | 预计时间 |
63
+ |------|------|---------|
64
+ | Phase 1 | 基础设施(loader/registry) | 3-4h |
65
+ | Phase 2 | Automator 迁移(4工具) | 2-3h |
66
+ | Phase 3 | MiniProgram 迁移(6工具) | 2-3h |
67
+ | Phase 4 | Page 迁移(8工具) | 2-3h |
68
+ | Phase 5 | Element 迁移(23工具) | 3-4h |
69
+ | Phase 6 | Assert/Snapshot/Record/Network | 4-5h |
70
+ | Phase 7 | 清理与测试 | 2-3h |
71
+ | **总计** | - | **18-25h** |
72
+
73
+ ---
74
+
75
+ ## 改动二:截图超时修复
76
+
77
+ ### 问题
78
+
79
+ `miniprogram_screenshot` 工具调用时经常卡住:
80
+ - 无超时保护:Promise 永不 resolve/reject
81
+ - base64 返回被删除:必须走文件保存路径
82
+ - 级联影响:所有 snapshot 工具都会卡住
83
+
84
+ ### 根本原因
85
+
86
+ ```typescript
87
+ // ❌ 当前代码 - 无超时保护
88
+ const screenshotBuffer = await session.miniProgram.screenshot({
89
+ path: fullPath,
90
+ fullPage,
91
+ })
92
+
93
+ // ✅ 应该使用 withTimeout
94
+ const screenshotBuffer = await withTimeout(
95
+ session.miniProgram.screenshot({ ... }),
96
+ timeoutMs,
97
+ 'Screenshot capture'
98
+ )
99
+ ```
100
+
101
+ ### 核心改动
102
+
103
+ 1. **P0**:为 screenshot 添加 withTimeout 保护
104
+ 2. **P0**:恢复 returnBase64 参数和 base64 返回
105
+ 3. **P1**:为其他 miniprogram 操作添加超时
106
+ 4. **P1**:添加重试机制
107
+ 5. **P2**:优化 fullPage 超时配置
108
+
109
+ ### 工作量
110
+
111
+ | 任务 | 预计时间 | 优先级 |
112
+ |------|---------|--------|
113
+ | screenshot 超时保护 | 1h | P0 |
114
+ | 恢复 base64 返回 | 1h | P0 |
115
+ | 其他操作超时保护 | 2h | P1 |
116
+ | 重试机制 | 1.5h | P1 |
117
+ | 配置优化 | 1h | P2 |
118
+ | 测试 | 2h | P1 |
119
+ | **总计** | **8-9h** | - |
120
+
121
+ ---
122
+
123
+ ## 建议实施顺序
124
+
125
+ ### 方案 A:先修复后迁移(推荐)
126
+
127
+ ```
128
+ Week 1: 截图修复 (8-9h)
129
+ ├─ Day 1-2: P0 任务(超时保护 + base64)
130
+ └─ Day 3-4: P1 任务(其他超时 + 重试)
131
+
132
+ Week 2-3: 架构迁移 (18-25h)
133
+ ├─ Day 1: Phase 1(基础设施)
134
+ ├─ Day 2-3: Phase 2-4(核心工具)
135
+ ├─ Day 4-5: Phase 5-6(剩余工具)
136
+ └─ Day 6: Phase 7(清理测试)
137
+ ```
138
+
139
+ **优势**:
140
+ - 先解决紧急问题
141
+ - 迁移时可以同时应用超时保护
142
+
143
+ ### 方案 B:迁移时同步修复
144
+
145
+ ```
146
+ 直接进行架构迁移,迁移 miniprogram 工具时顺带修复超时
147
+ ```
148
+
149
+ **优势**:
150
+ - 避免重复修改同一文件
151
+ - 代码只需 review 一次
152
+
153
+ **劣势**:
154
+ - 紧急问题需要等待更长时间
155
+
156
+ ---
157
+
158
+ ## 文件变更汇总
159
+
160
+ ### 新建文件(约 40 个)
161
+
162
+ | 目录 | 文件数 | 描述 |
163
+ |------|--------|------|
164
+ | capabilities/*/schemas/ | ~20 | Zod schema 定义 |
165
+ | capabilities/*/handlers/ | ~12 | 工具处理器 |
166
+ | capabilities/ | 3 | loader/registry/index |
167
+ | runtime/retry/ | 2 | 重试机制 |
168
+ | tests/ | 3 | 新增测试 |
169
+
170
+ ### 修改文件(约 10 个)
171
+
172
+ | 文件 | 改动 |
173
+ |------|------|
174
+ | src/server.ts | 使用新的 capabilities 入口 |
175
+ | src/tools/miniprogram.ts | 添加超时保护(临时,迁移后删除) |
176
+ | src/config/defaults.ts | 新增超时配置 |
177
+ | src/types.ts | 更新类型定义 |
178
+ | tests/**/*.test.ts | 更新导入路径 |
179
+
180
+ ### 删除文件(约 17 个)
181
+
182
+ | 目录 | 文件数 | 描述 |
183
+ |------|--------|------|
184
+ | src/tools/ | 9 | 全部工具文件 |
185
+ | src/core/ | 8 | 全部兼容层文件 |
186
+
187
+ ---
188
+
189
+ ## 风险评估
190
+
191
+ | 风险 | 概率 | 影响 | 缓解措施 |
192
+ |------|------|------|---------|
193
+ | 迁移过程中引入 bug | 中 | 高 | 逐阶段提交,完整测试 |
194
+ | 超时时间设置不合理 | 低 | 中 | 配置化,允许用户调整 |
195
+ | 测试覆盖不足 | 低 | 中 | 先补充测试再迁移 |
196
+ | 文档不同步 | 低 | 低 | 同步更新 README |
197
+
198
+ ---
199
+
200
+ ## 验收标准
201
+
202
+ ### 架构迁移
203
+
204
+ - [ ] 所有 65 个工具迁移到 capabilities/
205
+ - [ ] tools/ 和 core/ 目录删除
206
+ - [ ] 所有工具有 Zod schema
207
+ - [ ] 单元测试 100% 通过
208
+ - [ ] 集成测试通过
209
+ - [ ] TypeScript 编译无错误
210
+
211
+ ### 截图修复
212
+
213
+ - [ ] 截图 10s 内返回或超时
214
+ - [ ] fullPage 截图 30s 内返回或超时
215
+ - [ ] returnBase64=true 正确返回 base64
216
+ - [ ] 所有 miniprogram 操作有超时保护
217
+ - [ ] 超时测试覆盖
218
+
219
+ ---
220
+
221
+ ## 相关文档
222
+
223
+ - [01-architecture-migration.md](./01-architecture-migration.md) - 架构迁移详细计划
224
+ - [02-screenshot-timeout-fix.md](./02-screenshot-timeout-fix.md) - 截图修复详细方案
225
+ - [03-connect-screenshot-timeout-diagnosis.md](./03-connect-screenshot-timeout-diagnosis.md) - 连接与截图超时问题诊断
226
+ - [04-toolchain-version-baseline.md](./04-toolchain-version-baseline.md) - 工具链版本基线与超时配置说明
227
+ - [../../调研中/微信小程序工具链最新版本调研/README.md](../../调研中/微信小程序工具链最新版本调研/README.md) - SDK/CLI/开发者工具版本调研
228
+ - [../directory-structure-and-code-style-best-practices.md](../directory-structure-and-code-style-best-practices.md) - 代码规范
229
+ - [../migration/](../migration/) - 历史迁移文档
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creatoria/miniapp-mcp",
3
- "version": "0.2.0",
3
+ "version": "0.2.3",
4
4
  "description": "MCP server for WeChat Mini Program automation using miniprogram-automator",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@9.0.0",
@@ -64,7 +64,7 @@
64
64
  "dependencies": {
65
65
  "@modelcontextprotocol/sdk": "^1.0.4",
66
66
  "commander": "^12.1.0",
67
- "miniprogram-automator": "^0.12.1",
67
+ "miniprogram-automator": "0.12.1",
68
68
  "zod": "^3.23.8"
69
69
  },
70
70
  "devDependencies": {