dsh-mobile-plugin 0.5.33 → 0.5.35

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 (142) hide show
  1. package/README.md +17 -2
  2. package/dsh-mobile-credentials/lib/credentials.js +49 -2
  3. package/extensions/README.md +25 -21
  4. package/host.js +16 -1
  5. package/lib/git/pr.js +375 -0
  6. package/lib/git/read.js +1 -0
  7. package/lib/host-capabilities.cjs +67 -0
  8. package/lib/http/routing.js +25 -0
  9. package/lib/impl.js +34 -26
  10. package/lib/mirror/history-blob.cjs +184 -0
  11. package/lib/mirror/pusher.cjs +276 -1
  12. package/lib/mobile/approval-facts.js +439 -0
  13. package/lib/mobile/approval-memory.js +199 -0
  14. package/lib/mobile/approvals.js +596 -30
  15. package/lib/mobile/auto-review-log.js +160 -0
  16. package/lib/mobile/auto-review.js +172 -0
  17. package/lib/mobile/credential-upgrade.js +14 -0
  18. package/lib/mobile/device-permissions.js +86 -0
  19. package/lib/mobile/directory-browse.js +23 -8
  20. package/lib/mobile/events.js +9 -1
  21. package/lib/mobile/extension-catalog.js +106 -44
  22. package/lib/mobile/extension-data.js +6 -0
  23. package/lib/mobile/file-ops.js +89 -76
  24. package/lib/mobile/git.js +108 -2
  25. package/lib/mobile/live-steps.js +107 -4
  26. package/lib/mobile/mail.js +621 -0
  27. package/lib/mobile/monitor.js +6 -0
  28. package/lib/mobile/notification-prefs.js +205 -0
  29. package/lib/mobile/pairing.js +15 -2
  30. package/lib/mobile/permissions.js +433 -0
  31. package/lib/mobile/project-write-queue.js +115 -0
  32. package/lib/mobile/project-writes.js +61 -0
  33. package/lib/mobile/push-notify.js +293 -13
  34. package/lib/mobile/questions.js +20 -3
  35. package/lib/mobile/recent-failures.js +48 -0
  36. package/lib/mobile/relay-control.js +5 -0
  37. package/lib/mobile/session-cache.js +50 -0
  38. package/lib/mobile/session-last-turn.js +24 -22
  39. package/lib/mobile/session-new.js +205 -0
  40. package/lib/mobile/session-send.js +0 -1
  41. package/lib/mobile/sessions.js +12 -84
  42. package/lib/mobile/skill-write.js +549 -0
  43. package/lib/mobile/stats-collect.cjs +33 -3
  44. package/lib/mobile/stats-ledger.cjs +39 -2
  45. package/lib/mobile/stream.js +24 -3
  46. package/lib/mobile/supervisor-broadcast.js +58 -11
  47. package/lib/mobile/supervisor-tools.js +1 -1
  48. package/lib/mobile/third-party-extensions.js +158 -0
  49. package/lib/mobile/uploads.js +30 -14
  50. package/lib/mobile/worktree-isolation.js +479 -0
  51. package/lib/mobile/worktree-lifecycle.js +79 -0
  52. package/lib/p2p/wiring.cjs +41 -16
  53. package/lib/panel/index.html +15 -15
  54. package/lib/read-receipt.cjs +89 -5
  55. package/lib/relay/core.js +25 -1
  56. package/lib/relay/lan-proxy.js +6 -1
  57. package/lib/relay/preview.js +21 -1
  58. package/lib/relay/protocol.js +1 -0
  59. package/lib/relay/status.js +6 -0
  60. package/lib/relay/tunnel-forwarder.cjs +178 -0
  61. package/lib/routes/control-plane.js +35 -1
  62. package/lib/routes/mobile-api.js +105 -52
  63. package/lib/shared/via-header.js +15 -2
  64. package/lib/sidecar-facade-worker.cjs +15 -2
  65. package/lib/sidecar-facade.cjs +5 -0
  66. package/lib/tailcat/host-process.cjs +1 -1
  67. package/lib/tailcat/ingress.cjs +2 -2
  68. package/lib/tailcat/wiring.cjs +34 -13
  69. package/package.json +11 -3
  70. package/assets/pets/README.md +0 -9
  71. package/assets/pets/deepy-whale/attention.gif +0 -0
  72. package/assets/pets/deepy-whale/done.gif +0 -0
  73. package/assets/pets/deepy-whale/error.gif +0 -0
  74. package/assets/pets/deepy-whale/idle.gif +0 -0
  75. package/assets/pets/deepy-whale/pack.json +0 -54
  76. package/assets/pets/deepy-whale/sleeping.gif +0 -0
  77. package/assets/pets/deepy-whale/working-3.gif +0 -0
  78. package/assets/pets/deepy-whale/working.gif +0 -0
  79. package/knowledge/apps/alipay.md +0 -30
  80. package/knowledge/apps/android-settings.md +0 -30
  81. package/knowledge/apps/baijiahao.md +0 -30
  82. package/knowledge/apps/bilibili.md +0 -31
  83. package/knowledge/apps/boss-zhipin.md +0 -32
  84. package/knowledge/apps/dedao.md +0 -33
  85. package/knowledge/apps/douyin.md +0 -32
  86. package/knowledge/apps/feishu.md +0 -35
  87. package/knowledge/apps/gmail.md +0 -33
  88. package/knowledge/apps/jike.md +0 -31
  89. package/knowledge/apps/kuaishou.md +0 -34
  90. package/knowledge/apps/linkedin.md +0 -33
  91. package/knowledge/apps/qqmail.md +0 -30
  92. package/knowledge/apps/taobao.md +0 -33
  93. package/knowledge/apps/telegram.md +0 -27
  94. package/knowledge/apps/toutiao.md +0 -32
  95. package/knowledge/apps/uber-eats.md +0 -26
  96. package/knowledge/apps/uber.md +0 -27
  97. package/knowledge/apps/wechat.md +0 -33
  98. package/knowledge/apps/weibo.md +0 -31
  99. package/knowledge/apps/whatsapp.md +0 -27
  100. package/knowledge/apps/x.md +0 -32
  101. package/knowledge/apps/xiaohongshu.md +0 -30
  102. package/knowledge/apps/zhihu.md +0 -31
  103. package/knowledge/sites/bilibili.com/SITE.md +0 -50
  104. package/knowledge/sites/bilibili.com/references/view-fields.md +0 -17
  105. package/knowledge/sites/github.com/SITE.md +0 -51
  106. package/knowledge/sites/github.com/references/repo-fields.md +0 -13
  107. package/knowledge/sites/x.com/SITE.md +0 -47
  108. package/knowledge/sites/xiaohongshu.com/SITE.md +0 -48
  109. package/knowledge/sites/xiaohongshu.com/references/note-fields.md +0 -18
  110. package/knowledge/sites/youtube.com/SITE.md +0 -49
  111. package/knowledge/sites/youtube.com/references/innertube-fields.md +0 -14
  112. package/knowledge/sites/zhihu.com/SITE.md +0 -47
  113. package/knowledge/sites/zhihu.com/references/api-fields.md +0 -21
  114. package/lib/mobile/pet-tools.js +0 -261
  115. package/lib/mobile/pets.js +0 -755
  116. package/lib/mobile/phone/access.js +0 -85
  117. package/lib/mobile/phone/binding.js +0 -247
  118. package/lib/mobile/phone/confirm.js +0 -189
  119. package/lib/mobile/phone/evidence.js +0 -228
  120. package/lib/mobile/phone/gate.js +0 -290
  121. package/lib/mobile/phone/grounding.js +0 -314
  122. package/lib/mobile/phone/guard.js +0 -465
  123. package/lib/mobile/phone/index.js +0 -83
  124. package/lib/mobile/phone/irreversible.js +0 -226
  125. package/lib/mobile/phone/knowledge-tools.js +0 -665
  126. package/lib/mobile/phone/learn.js +0 -329
  127. package/lib/mobile/phone/pacing.js +0 -173
  128. package/lib/mobile/phone/redact.js +0 -237
  129. package/lib/mobile/phone/risk.js +0 -95
  130. package/lib/mobile/phone/route-hint.js +0 -164
  131. package/lib/mobile/phone/routes-memory.js +0 -133
  132. package/lib/mobile/phone/routes.js +0 -279
  133. package/lib/mobile/phone/service.js +0 -500
  134. package/lib/mobile/phone/site-commands.js +0 -271
  135. package/lib/mobile/phone/step-summary.js +0 -201
  136. package/lib/mobile/phone/stop-summary.js +0 -235
  137. package/lib/mobile/phone/tool-groups.js +0 -125
  138. package/lib/mobile/phone/tools.js +0 -2282
  139. package/lib/mobile/phone/untrusted.js +0 -65
  140. package/lib/mobile/phone-knowledge.js +0 -317
  141. package/lib/mobile/wechat-tools.js +0 -257
  142. package/lib/mobile/wechat.js +0 -514
package/README.md CHANGED
@@ -18,7 +18,9 @@
18
18
 
19
19
  <p align="center">电脑上开着的 DeepSeek Harness,手机上接着用。</p>
20
20
 
21
- **AI-HUB 的手机端插件。** 装在电脑这边,手机 App 就通过一条中转隧道或局域网配对连进你的 DeepSeek Harness —— 看会话、接着聊、管理文件和工作区,进度实时同步。它也可以脱离桌面外壳单独跑在服务器上,当作无头宿主用。
21
+ **AI-HUB 的手机端插件。** 装在电脑这边,手机 App 就通过一条中转隧道或局域网配对连进你的 DeepSeek Harness —— 看会话、接着聊、管理文件和工作区,进度实时同步。
22
+
23
+ 它和绝大多数 DSH 插件不一样的一点:**不需要桌面外壳,也不需要 Electron**。同一份包可以单独跑在没有图形界面的服务器上,当作无头宿主用 —— 手机连的是一台根本没有屏幕的机器。
22
24
 
23
25
  ![手机 App 经中转或局域网连到你电脑上的 DeepSeek Harness](https://raw.githubusercontent.com/WNLUO/dsh-mobile-plugin/main/assets/connection-light.svg)
24
26
 
@@ -60,6 +62,8 @@
60
62
 
61
63
  在 DSH 的 **设置 → 插件** 里搜索并安装 `dsh-mobile-plugin`,然后打开 **设置 → 移动端 → 打开配置页面** 做配对。插件市场负责检查和安装更新,通常更新后不用重启 DSH。
62
64
 
65
+ 装完以后,**入口在 DSH 的「设置 → 移动端」**:点「打开配置页面」,那里有配对二维码、当前监听地址和中转状态。手机上的所有功能都从这一页开始。
66
+
63
67
  ### 无头 Host(Linux)
64
68
 
65
69
  需要 Node.js、pnpm,以及可用的 DSH 插件宿主依赖。仓库里的 `host.js` 就是独立入口:
@@ -122,11 +126,22 @@ DeepSeek Harness 跑在电脑上,但我不是一直坐在电脑前。出门、
122
126
 
123
127
  **各家 AI 助手的手机 App**:那些跑在厂商的服务器上,会话、文件和额度都在别人那儿。这里连的是你自己电脑上那个 DSH,模型是你配的,文件在你硬盘上。
124
128
 
129
+ ## 已知限制
130
+
131
+ 写在这里的是我们确实还没做到的,不是待办清单的美化版。
132
+
133
+ - **iOS 没有真正的后台提醒**。系统不允许 App 常驻后台,任务完成通知只在 Android 上生效;iOS 上没有配置离线推送时,进后台就收不到任何提醒。
134
+ - **中转能看到明文**。链路是 WSS / HTTPS 加密的,但没有端到端加密,与 frp / ngrok 同一个模型。会话正文在中转上不落盘,但中转进程本身能解密。
135
+ - **中转默认不做隧道鉴权**:能连上中转端口的人就能接走隧道。要在公网上用,请开中转控制台的限制模式,或者只把端口暴露给可信网络。
136
+ - **手机上不能卸载插件**。可以装、可以升级第三方插件,但没有卸载入口 —— 卸载要等电脑下次启动 DSH 才真的生效,在手机上按一颗不会立刻生效的按钮只会让人误会。要卸就去电脑上卸。
137
+
125
138
  ## 交流与反馈
126
139
 
127
140
  ![企业微信群二维码](https://raw.githubusercontent.com/WNLUO/dsh-mobile-plugin/main/assets/wechat-group.png)
128
141
 
129
- 扫上面的码进企业微信群。不方便的话发邮件到 <admin@wnluo.com> 或 <wnluo2@gmail.com>。用着顺手就给仓库点个 star,这是最实在的支持。
142
+ 扫上面的码进企业微信群。**这个码更新于 2026-11-09,如果扫不出来或者提示已过期**,说明二维码已经换了,发邮件到 <admin@wnluo.com> 索取新的,或者直接提 issue。
143
+
144
+ 不方便的话发邮件到 <admin@wnluo.com> 或 <wnluo2@gmail.com>。用着顺手就给仓库点个 star,这是最实在的支持。
130
145
 
131
146
  隐私政策见 [PRIVACY.md](PRIVACY.md),常见问题见 [SUPPORT.md](SUPPORT.md)。
132
147
 
@@ -200,13 +200,51 @@ function issue(instanceId, device, hostToken, ttlMs = DEFAULT_TTL_MS, extra = {}
200
200
  const keyId = typeof extra?.keyId === 'string' ? extra.keyId : '';
201
201
  if (deviceKey) record.deviceKey = deviceKey;
202
202
  if (keyId) record.keyId = keyId;
203
+ // 按设备发放的权限子集(`lib/mobile/permissions.js` 是唯一真源)。
204
+ // **只在真给了数组时才写这一格**:不写 = 全权,这是老记录与现网设备的兼容命门,
205
+ // 绝不能在这里补一个默认数组把「没设置」变成「设置成某个子集」。
206
+ if (Array.isArray(extra?.permissions)) record.permissions = extra.permissions.slice();
203
207
  prune(now);
204
208
  records.push(record);
205
209
  rebuildIndex();
206
210
  save();
207
211
  // 刚写过盘,所以接下来一分钟内的 `touch` 不必再写一次(节流基准跟着这次写走)。
208
212
  lastTouchFlushAt = now;
209
- return { credential, deviceId, expiresAt, ...(keyId ? { keyId } : {}) };
213
+ return {
214
+ credential,
215
+ deviceId,
216
+ expiresAt,
217
+ ...(keyId ? { keyId } : {}),
218
+ ...(Array.isArray(record.permissions) ? { permissions: record.permissions.slice() } : {}),
219
+ };
220
+ }
221
+
222
+ /**
223
+ * 改一台设备名下所有活记录的权限集合(电脑侧授予 / 手机侧自助降档都走它)。
224
+ *
225
+ * 只改 `permissions` 这一格:凭据串本身不动(库按整串哈希查,改一个字节手机上那枚就对不上,
226
+ * 只能重新签发、也就是重新扫码)。同一台手机名下可能有好几条活记录(重配、升级留下的),
227
+ * 一起改,否则「这台是只读」只对最新那条生效、旧的那枚照样全权。
228
+ *
229
+ * @returns {number} 改了几条
230
+ */
231
+ function setDevicePermissions(instanceId, deviceId, permissions) {
232
+ load();
233
+ if (readOnly) {
234
+ const error = new Error('凭据库的只读副本不能改权限;旁路子进程请经 IPC 交给主进程');
235
+ error.code = 'READ_ONLY';
236
+ throw error;
237
+ }
238
+ if (!Array.isArray(permissions)) return 0;
239
+ const wanted = permissions.slice();
240
+ let count = 0;
241
+ for (const record of records) {
242
+ if (record.instanceId !== instanceId || record.deviceId !== deviceId || !isLive(record)) continue;
243
+ record.permissions = wanted.slice();
244
+ count += 1;
245
+ }
246
+ if (count) save();
247
+ return count;
210
248
  }
211
249
 
212
250
  /**
@@ -414,6 +452,7 @@ function listDevices(instanceId) {
414
452
  legacy: false,
415
453
  encrypted: false,
416
454
  expiresAt: 0,
455
+ permissions: null,
417
456
  };
418
457
  rows.set(key, row);
419
458
  }
@@ -435,6 +474,13 @@ function listDevices(instanceId) {
435
474
  if (!record.deviceKey) row.legacy = true;
436
475
  // 带设备密钥的记录才走逐请求签名;只带 6 位码配对的那个版本永远是明文。
437
476
  if (record.deviceKey) row.encrypted = true;
477
+ // 权限子集取最新一条**真设过**的记录(老记录没这一格,不覆盖);一条都没设过就保持
478
+ // null = 全权,面板据此显示「全权」。`tags` 不像机型号那样按时间比,是因为它只有
479
+ // 「设过 / 没设过」两种:降档会写全部活记录(见 setDevicePermissions)。
480
+ if (Array.isArray(record.permissions) && createdAt >= (row.permissionsAt || 0)) {
481
+ row.permissions = record.permissions.slice();
482
+ row.permissionsAt = createdAt;
483
+ }
438
484
  }
439
485
  // 撞名才编号:分组用的正是面板拿来当标题的那一格 —— **机型优先,没有机型才用名字**。
440
486
  // 两台都叫「iPhone 15 Pro」时「· 1 / · 2」是人认得出的;挂着机器号后四位则不是。
@@ -452,7 +498,7 @@ function listDevices(instanceId) {
452
498
  }
453
499
  const out = [];
454
500
  for (const row of rows.values()) {
455
- const { modelAt, nameAt, platformAt, osVersionAt, appVersionAt, lastSeenFrom, ...rest } = row;
501
+ const { modelAt, nameAt, platformAt, osVersionAt, appVersionAt, lastSeenFrom, permissionsAt, ...rest } = row;
456
502
  out.push(rest);
457
503
  }
458
504
  // 最近连着的排前面;从没连过的按绑定时间倒序垫后。
@@ -507,6 +553,7 @@ module.exports = {
507
553
  isReadOnly,
508
554
  listActive,
509
555
  listDevices,
556
+ setDevicePermissions,
510
557
  activeDigests,
511
558
  revoke,
512
559
  revokeDevice,
@@ -1,9 +1,12 @@
1
- # 已审核的第三方扩展
1
+ # 随插件发布的官方扩展
2
2
 
3
- 这个目录随 `dsh-mobile-plugin` 的 npm 包一起发布。手机上「随插件发布」那一档扩展从这里读:
4
- 宿主只认 `catalog.json` 里登记的 slug,目录里有但没登记的一律不下发。
3
+ 这个目录随 `dsh-mobile-plugin` 的 npm 包一起发布,装的是**我们自己发的官方扩展**(Office 预览、
4
+ magpie 这类)。宿主只认 `catalog.json` 里登记的 slug,目录里有但没登记的一律不下发。
5
5
 
6
- ## 提交流程
6
+ 第三方扩展不经过这里。作者在自己的插件包里声明 `dshMobileExtensions` 就能让他的手机拉到,
7
+ 不用提 PR、不用我们审 —— 见 `docs/extension-authoring.md` 第 8 节。这一份只讲官方这一档。
8
+
9
+ ## 加一个官方扩展
7
10
 
8
11
  1. 在 `extensions/<slug>/` 放扩展包:`manifest.json` 与 `index.html` 等,形状与 App 侧现有
9
12
  扩展包一致(自包含、相对路径引用,不许外链)。目录名就是 slug,只认字母、数字、点、
@@ -12,7 +15,7 @@
12
15
  时会直接跳过,别等装到手机上才发现打不开。
13
16
  2. 在 `extensions/catalog.json` 的 `packages` 里加一条,填 `slug`(必须与目录名一致)与
14
17
  `description` / `author` / `releaseNotes`。
15
- 3. 提 PR 前在本地跑一遍自验:
18
+ 3. 发版前在本地跑一遍自验:
16
19
 
17
20
  ```sh
18
21
  cd dsh-mobile-plugin
@@ -20,22 +23,20 @@
20
23
  npm pack --dry-run # 看扩展目录是否真进了 npm 包
21
24
  ```
22
25
 
23
- 想按发版那套全量口径验(含「登记的扩展都带入口页」),跑 `node scripts/preflight-release.mjs`;
26
+ 按发版那套全量口径验(含「登记的扩展都带入口页」)跑 `node scripts/preflight-release.mjs`;
24
27
  空清单时这几条都是绿的,第一个包进来才真正验得到。
25
- 4. 提 PR,一次只放一个包。合并即随下一次插件发版上架;从 `packages` 里删掉那一条即下架。
28
+ 4. 随下一次插件发版上架;从 `packages` 里删掉那一条即下架。
26
29
 
27
30
  这个目录是随包发布的只读内容,宿主不会创建它,也不会写它。
28
31
 
29
- ## 审核口径
32
+ ## 判据(机械判定,不解释原因)
33
+
34
+ 官方扩展是我们自己发的,不审作者人品,只判「能不能装」——这几条判不过的包不会出现在手机上:
30
35
 
31
- 登记即视为已审核,所以审的是包本身,不是清单里的那几行字:
36
+ - 缺 `manifest.json`、或缺入口页 `index.html`;
37
+ - 整包不超过 20 MB、不超过 400 个文件,单文件不超过 8 MB。
32
38
 
33
- - 入口页第一屏能不能打开,页面有没有模仿系统界面骗用户填东西;
34
- - 包里会不会往外发请求。页面从 `file://` 加载,CSP 挡外链,资源必须都在包里;
35
- - `manifest.json` 声明的能力与实际行为对不对得上;
36
- - 右栏入口的图标与名字像不像 App 自带的功能(叫「设置」「账户」这类直接打回);
37
- - 体积:整包不超过 20 MB、不超过 200 个文件,单文件不超过 8 MB。缺 `manifest.json`、
38
- 缺 `index.html`、或体积超了,宿主都会静默跳过,包根本不会出现在手机上。
39
+ (第三方插件自带的那一档用同一套判据,见 `lib/mobile/third-party-extensions.js`。)
39
40
 
40
41
  ## 字段含义
41
42
 
@@ -43,7 +44,7 @@
43
44
 
44
45
  - `slug`(必填):包目录名,也是手机侧的身份。重复只认第一条,不合规的连读都不读。
45
46
  - `description` / `author` / `releaseNotes`(可选):只收非空字符串,缺了或不是字符串一律
46
- 回 `null`。`author` 写显示名加一个能联系上的方式,下架时找得到人。
47
+ 回 `null`。
47
48
  - **不要在这里写 `name`、`version`、`size`、`sha256`、`files`、`capabilities`**:文件清单与哈希
48
49
  由宿主扫目录现算,名字、版本、能力从包自己的 `manifest.json` 读,填了也不认。
49
50
 
@@ -55,10 +56,13 @@
55
56
 
56
57
  `GET /api/mobile/v1/extension/catalog/file?slug=&path=` 按清单里的文件逐个字节下发
57
58
  (base64 + sha256)。`path` 只能是包目录内的相对路径,绝对路径、`..`、resolve 之后越出包目录
58
- 的一律拒绝。没登记进 `catalog.json` 的 slug 取不到任何文件。
59
+ 的一律拒绝。这个 slug 既不在本插件的 `catalog.json` 里、也不是哪个插件声明过的,取不到任何文件。
60
+
61
+ ## 与另两条来源的区别
59
62
 
60
- ## 与本地开发包的区别
63
+ 手机扩展页里一共三档来源,别混:
61
64
 
62
- 作者边写边看用 `$DSH_HOME/extension-dev`(作者电脑上的目录,走 `/extension/dev/*`),那条路
63
- 绕过审核,只能手动导入、界面标「本地」。这里是随插件 npm 包发布的正式来源,要上架就得走
64
- 上面的 PR 与审核。
65
+ - **官方**:本目录,随 `dsh-mobile-plugin` 发版,登记在 `catalog.json`;
66
+ - **第三方插件**:作者自己的插件里声明 `dshMobileExtensions`,用户自己装插件、自己承担;
67
+ - **本地开发包**:作者电脑上的 `$DSH_HOME/extension-dev`(走 `/extension/dev/*`),边写边看,
68
+ 只能手动导入、界面标「本地」。
package/host.js CHANGED
@@ -18,6 +18,7 @@ import { fileURLToPath } from 'node:url'
18
18
  import { provideCmdline } from '@deepseek-ai/dsh-cmdline'
19
19
  import { installProcessGuard } from './lib/host-guard.cjs'
20
20
  import { DIRECTORY_PICKER_ROW, TOOL_ROWS, pinInsertedRows, syncPresetDir } from './lib/host-compat.cjs'
21
+ import { setCapabilityReport } from './lib/host-capabilities.cjs'
21
22
  import { dropBareNameSelfMounts } from './lib/host-patches.cjs'
22
23
  import {
23
24
  auditStartupEntries as assertEntriesActivated,
@@ -139,10 +140,19 @@ try {
139
140
  }
140
141
 
141
142
  const pinnedRows = new Map()
143
+ // 这一轮「少了什么」的台账:解析不到同代那份的行、入口本身没了的行都记进来,
144
+ // 启动后由 `/api/mobile/v1/status` 报给手机(见 lib/host-capabilities.cjs)。
145
+ // 记下来的理由只有一个:这一层坏掉的样子是**静默降级** —— 宿主照样起来、手机照样连上,
146
+ // 用户进了会话才发现「模型说没有这个工具」。不记,就没有任何地方会说出这句话。
147
+ const missingCapabilities = []
142
148
  for (const { id, name } of [...TOOL_ROWS, DIRECTORY_PICKER_ROW]) {
143
149
  const hit = resolveGeneration('运行时', runtimeRequire, name)
144
150
  ?? resolveGeneration('本包自带', bundledRequire, name)
145
- if (hit) pinnedRows.set(id, hit)
151
+ if (hit) {
152
+ pinnedRows.set(id, hit)
153
+ } else {
154
+ missingCapabilities.push({ id, name, reason: 'no-compatible-generation' })
155
+ }
146
156
  process.stderr.write(
147
157
  hit
148
158
  ? `${BIN_NAME}: ${name} → 用${hit.scope}的 ${hit.version}\n`
@@ -159,10 +169,15 @@ try {
159
169
  runtimeRequire?.resolve(listAgentsEntry)
160
170
  } catch {
161
171
  listAgentsRowGone = true
172
+ missingCapabilities.push({ id: 'tool-subagent-list-agents', name: listAgentsEntry, reason: 'entry-removed' })
162
173
  process.stderr.write(`${BIN_NAME}: 这一代运行时没有 ${listAgentsEntry}(该工具已并在主包里),预设副本里那一行不再挂\n`)
163
174
  }
164
175
  const dropPresetIds = listAgentsRowGone ? ['tool-subagent-list-agents'] : []
165
176
 
177
+ // 对齐结论在这里落账,早于 boot():装载期一旦抛错(那一行真把宿主带走了),
178
+ // 也已经记下了「本来是哪一行的问题」,否则日志里只剩一个与版本无关的栈。
179
+ setCapabilityReport({ runtimeVersion, missing: missingCapabilities })
180
+
166
181
  // 预设里的两行默认按裸名写:Loader 从它自己的位置解析,命中哪一份不由我们决定。
167
182
  // 解析到了同代的那份就改写一份副本给它,路径写死成那一份的入口。
168
183
  const presetRoots = {}
package/lib/git/pr.js ADDED
@@ -0,0 +1,375 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * GitHub PR 一层:列本分支的 PR、看检查、标可评审、合并(只 squash / merge)、读评论。
5
+ *
6
+ * 这一层是 `lib/mobile/git.js` 那组封闭动词的下半截 —— GitHub 的部分全在这里,handler 只做
7
+ * 归属校验与参数校验。**不接受**调用方给的 URL / host / method / GraphQL:这里的每条路都写死
8
+ * 成 `https://api.github.com` 下的固定 REST 子路径,参数只有 PR 号与合并方式两个值。
9
+ *
10
+ * 凭据沿用宿主环境里已有的 `GITHUB_TOKEN` / `GH_TOKEN`(与 `read.js` 的 GitHub 元信息同一条路),
11
+ * 不在 App 里输 token、不落任何明文、不读凭据文件。
12
+ *
13
+ * 错误一律是带 `code` 的 Error:404 → `PR_NOT_FOUND`、401/403 → `GH_AUTH`、429 / 限流头见底 →
14
+ * `GH_RATE_LIMITED`、网络与 5xx → `GH_UNAVAILABLE`。合并被 GitHub 拒时尽量分成
15
+ * `CHECKS_FAILING`(检查没过)与 `MERGE_NOT_ALLOWED`(其余不可合并)。
16
+ */
17
+
18
+ const { githubTargetOf, remotesOf } = require('./read');
19
+ const read = require('./read');
20
+
21
+ /** 固定基址。调用方给不了,也不该给。 */
22
+ const GITHUB_API = 'https://api.github.com';
23
+ /** 比 read.js 的元信息读宽一点:check-runs 与合并都可能多等一会儿。 */
24
+ const GITHUB_TIMEOUT_MS = 8000;
25
+ /** 列表一页的条数。PR 列表按当前分支过滤,一般就一两条。 */
26
+ const PULLS_PER_PAGE = 30;
27
+ const CHECKS_PER_PAGE = 100;
28
+
29
+ /** 合法的 PR 状态过滤;其余一律按 open。 */
30
+ const PR_STATES = new Set(['open', 'closed', 'merged', 'all']);
31
+ /** 合并方式只有两种:squash 与 merge。rebase 与自定义提交信息一律不收。 */
32
+ const MERGE_METHODS = new Set(['squash', 'merge']);
33
+
34
+ /** 列表里不额外拉 checks 时用的空汇总:字段必须齐全(前端按它画角标)。 */
35
+ function emptyChecks() {
36
+ return { total: 0, passing: 0, failing: 0, pending: 0, status: 'none' };
37
+ }
38
+
39
+ function ghError(code, message) {
40
+ const error = new Error(message);
41
+ error.name = 'GitHubRequestError';
42
+ error.code = code;
43
+ return error;
44
+ }
45
+
46
+ /** 宿主环境里的 GitHub 凭据。只读环境变量,与 read.js 完全同一条路。 */
47
+ function githubToken() {
48
+ return (process.env.GITHUB_TOKEN || process.env.GH_TOKEN || '').trim();
49
+ }
50
+
51
+ function normalizeState(value) {
52
+ const raw = typeof value === 'string' ? value.trim().toLowerCase() : '';
53
+ return PR_STATES.has(raw) ? raw : 'open';
54
+ }
55
+
56
+ /**
57
+ * 一条 check-run 的结论归一。
58
+ *
59
+ * GitHub 的 `conclusion` 在「还没跑完」时是 null,这时要看 `status`:completed 以外的都不算
60
+ * 出结果,一律按 pending —— 把「还在跑」画成失败会让人白跑一趟。
61
+ */
62
+ function normalizeConclusion(run) {
63
+ const conclusion = run && typeof run.conclusion === 'string' ? run.conclusion.trim().toLowerCase() : '';
64
+ if (!conclusion) {
65
+ const status = run && typeof run.status === 'string' ? run.status.trim().toLowerCase() : '';
66
+ if (status && status !== 'completed') return 'pending';
67
+ return 'pending';
68
+ }
69
+ switch (conclusion) {
70
+ case 'success':
71
+ return 'success';
72
+ case 'failure':
73
+ case 'action_required':
74
+ return 'failure';
75
+ case 'neutral':
76
+ return 'neutral';
77
+ case 'skipped':
78
+ return 'skipped';
79
+ case 'cancelled':
80
+ return 'cancelled';
81
+ case 'timed_out':
82
+ return 'timed_out';
83
+ case 'stale':
84
+ return 'pending';
85
+ default:
86
+ return 'pending';
87
+ }
88
+ }
89
+
90
+ /**
91
+ * check-runs 数组 → 汇总。中性 / 跳过的算「过了」(它们没拦),pending 单独一档。
92
+ *
93
+ * 一条都没有时 `status: 'none'`:那是「没配 CI」,不是「全过了」。
94
+ */
95
+ function summarizeChecks(checkRuns) {
96
+ const list = Array.isArray(checkRuns) ? checkRuns : [];
97
+ const summary = emptyChecks();
98
+ for (const run of list) {
99
+ const conclusion = normalizeConclusion(run);
100
+ summary.total += 1;
101
+ if (conclusion === 'pending') {
102
+ summary.pending += 1;
103
+ } else if (conclusion === 'failure' || conclusion === 'cancelled' || conclusion === 'timed_out') {
104
+ summary.failing += 1;
105
+ } else {
106
+ summary.passing += 1;
107
+ }
108
+ }
109
+ if (summary.total === 0) return summary;
110
+ if (summary.failing > 0) summary.status = 'failing';
111
+ else if (summary.pending > 0) summary.status = 'pending';
112
+ else summary.status = 'passing';
113
+ return summary;
114
+ }
115
+
116
+ /**
117
+ * 打一次 GitHub REST。
118
+ *
119
+ * 只接固定子路径(以 `/` 开头、拼在 `https://api.github.com` 后面),不接完整 URL、不接 host、
120
+ * 不接任意 method(GET / POST / PUT 三档够这一层用了)。
121
+ *
122
+ * 正常返回 `{ status, ok, json }`;非 2xx 按契约抛出带 `code` 的 Error,并带上 `status` 与
123
+ * GitHub 的 `message`(合并那条要靠它区分「检查没过」与「其余不可合并」)。
124
+ */
125
+ async function ghRequest(target, path, options = {}) {
126
+ const fetchImpl = options.fetchImpl || globalThis.fetch;
127
+ if (typeof fetchImpl !== 'function') throw ghError('GH_UNAVAILABLE', '这个运行时没有 fetch,读不到 GitHub');
128
+ if (typeof path !== 'string' || !path.startsWith('/')) throw ghError('BAD_REQUEST', 'GitHub 路径必须是以 / 开头的固定子路径');
129
+ const method = options.method === 'POST' || options.method === 'PUT' ? options.method : 'GET';
130
+ const token = githubToken();
131
+ const headers = {
132
+ accept: 'application/vnd.github+json',
133
+ 'user-agent': 'dsh-mobile-plugin',
134
+ ...(token ? { authorization: `Bearer ${token}` } : {}),
135
+ ...(options.body !== undefined ? { 'content-type': 'application/json' } : {}),
136
+ };
137
+ const controller = new AbortController();
138
+ const timer = setTimeout(() => controller.abort(), GITHUB_TIMEOUT_MS);
139
+ let response;
140
+ try {
141
+ response = await fetchImpl(`${GITHUB_API}${path}`, {
142
+ method,
143
+ headers,
144
+ ...(options.body !== undefined ? { body: JSON.stringify(options.body) } : {}),
145
+ signal: controller.signal,
146
+ });
147
+ } catch {
148
+ throw ghError('GH_UNAVAILABLE', '连不上 GitHub,检查一下网络或代理');
149
+ } finally {
150
+ clearTimeout(timer);
151
+ }
152
+
153
+ let json = null;
154
+ try {
155
+ json = await response.json();
156
+ } catch {
157
+ json = null;
158
+ }
159
+
160
+ const status = Number(response.status) || 0;
161
+ if (response.ok) return { status, ok: true, json };
162
+
163
+ const remaining = response.headers && typeof response.headers.get === 'function'
164
+ ? response.headers.get('x-ratelimit-remaining')
165
+ : null;
166
+ const reason = json && typeof json.message === 'string' ? json.message : '';
167
+ const error = (() => {
168
+ if (status === 401) return ghError('GH_AUTH', '这台电脑上的 GitHub 凭据不可用');
169
+ if (status === 403) {
170
+ return String(remaining).trim() === '0'
171
+ ? ghError('GH_RATE_LIMITED', 'GitHub 限流了,过一会儿再试')
172
+ : ghError('GH_AUTH', '这个 GitHub 凭据没有权限做这件事');
173
+ }
174
+ if (status === 404) return ghError('PR_NOT_FOUND', reason || 'GitHub 上找不到这条 PR');
175
+ if (status === 429) return ghError('GH_RATE_LIMITED', 'GitHub 限流了,过一会儿再试');
176
+ if (status >= 500) return ghError('GH_UNAVAILABLE', 'GitHub 暂时不可用,稍后再试');
177
+ if (status >= 400) return ghError('BAD_REQUEST', reason || 'GitHub 拒绝了这次请求');
178
+ return ghError('GH_UNAVAILABLE', 'GitHub 没有回包,稍后再试');
179
+ })();
180
+ error.status = status;
181
+ error.reason = reason;
182
+ throw error;
183
+ }
184
+
185
+ /** 项目里的第一个 GitHub 远端 → `{ owner, repo }`;没有就 null。 */
186
+ async function githubRepoOf(root) {
187
+ const remotes = await remotesOf(root);
188
+ const hit = remotes.find((item) => githubTargetOf(item.url));
189
+ if (!hit) return null;
190
+ const target = githubTargetOf(hit.url);
191
+ if (!target) return null;
192
+ return { owner: target.owner, repo: target.repo };
193
+ }
194
+
195
+ /** 上面四个「必须有 GitHub 仓库」的动作共用:没有就按「没有 PR」回,不是 500。 */
196
+ async function requireGithubRepo(root) {
197
+ const repo = await githubRepoOf(root);
198
+ if (!repo) throw ghError('NO_PR', '这个项目没有关联 GitHub 远端,看不到 PR');
199
+ return repo;
200
+ }
201
+
202
+ function repoPath(repo) {
203
+ return `/repos/${encodeURIComponent(repo.owner)}/${encodeURIComponent(repo.repo)}`;
204
+ }
205
+
206
+ /** GitHub 的 PR 对象 → 下发的形状(字段名对齐 App 的 `GitPullRequest`)。 */
207
+ function toPullRequest(item) {
208
+ const state = item && item.merged_at ? 'merged' : (item && item.state === 'open' ? 'open' : 'closed');
209
+ return {
210
+ number: Number(item.number) || 0,
211
+ title: typeof item.title === 'string' ? item.title : '',
212
+ headBranch: item.head && typeof item.head.ref === 'string' ? item.head.ref : '',
213
+ baseBranch: item.base && typeof item.base.ref === 'string' ? item.base.ref : '',
214
+ state,
215
+ draft: Boolean(item.draft),
216
+ // GitHub 的 mergeable 是异步算的:null = 还没算出来,不能当成 false。
217
+ mergeable: typeof item.mergeable === 'boolean' ? item.mergeable : null,
218
+ // 列表里不为每条 PR 再拉一次 check-runs(那是 N 次请求);要看检查结果点进去走 checksOf。
219
+ checks: emptyChecks(),
220
+ author: item.user && typeof item.user.login === 'string' ? item.user.login : '',
221
+ updatedAt: typeof item.updated_at === 'string' ? item.updated_at : '',
222
+ url: typeof item.html_url === 'string' ? item.html_url : '',
223
+ };
224
+ }
225
+
226
+ /**
227
+ * 当前分支的 PR 列表。
228
+ *
229
+ * 不收分支名:一律用仓库当前分支(`read.repoOf` 的 branch)。detached 时没有「当前分支」,
230
+ * 回 `NO_PR` 而不是拿 HEAD 去猜。`state: 'merged'` 不是 GitHub 的合法取值,映射成
231
+ * `closed` 之后在本地按 `merged_at` 过滤。
232
+ */
233
+ async function pullsOf(root, options = {}) {
234
+ const fetchImpl = options.fetchImpl || globalThis.fetch;
235
+ const state = normalizeState(options.state);
236
+ const repo = await githubRepoOf(root);
237
+ if (!repo) return { prs: [], notGithub: true };
238
+ const summary = await read.repoOf(root);
239
+ const branch = summary && typeof summary.branch === 'string' ? summary.branch.trim() : '';
240
+ if (!branch) throw ghError('NO_PR', '现在不在任何分支上,回电脑上切回分支再看 PR');
241
+ const search = new URLSearchParams({
242
+ head: `${repo.owner}:${branch}`,
243
+ state: state === 'merged' ? 'closed' : state,
244
+ per_page: String(PULLS_PER_PAGE),
245
+ });
246
+ const response = await ghRequest(repo, `${repoPath(repo)}/pulls?${search.toString()}`, { fetchImpl });
247
+ const list = Array.isArray(response.json) ? response.json : [];
248
+ const prs = list
249
+ .filter((item) => item && (state !== 'merged' || item.merged_at))
250
+ .map(toPullRequest);
251
+ return { prs, notGithub: false };
252
+ }
253
+
254
+ /** 一条 PR 的 check-runs:先拿 head.sha,再拉那个提交的检查。 */
255
+ async function checksOf(root, number, options = {}) {
256
+ const fetchImpl = options.fetchImpl || globalThis.fetch;
257
+ const repo = await requireGithubRepo(root);
258
+ const pull = await ghRequest(repo, `${repoPath(repo)}/pulls/${number}`, { fetchImpl });
259
+ const sha = pull.json && pull.json.head && typeof pull.json.head.sha === 'string' ? pull.json.head.sha : '';
260
+ if (!sha) return [];
261
+ const runs = await ghRequest(
262
+ repo,
263
+ `${repoPath(repo)}/commits/${encodeURIComponent(sha)}/check-runs?per_page=${CHECKS_PER_PAGE}`,
264
+ { fetchImpl },
265
+ );
266
+ const list = runs.json && Array.isArray(runs.json.check_runs) ? runs.json.check_runs : [];
267
+ return list.map((run) => ({
268
+ name: run && typeof run.name === 'string' ? run.name : '',
269
+ conclusion: normalizeConclusion(run),
270
+ detailsUrl: run && typeof run.details_url === 'string'
271
+ ? run.details_url
272
+ : (run && typeof run.html_url === 'string' ? run.html_url : null),
273
+ }));
274
+ }
275
+
276
+ /** 把草稿 PR 标成可评审。 */
277
+ async function markReady(root, number, options = {}) {
278
+ const fetchImpl = options.fetchImpl || globalThis.fetch;
279
+ const repo = await requireGithubRepo(root);
280
+ const response = await ghRequest(repo, `${repoPath(repo)}/pulls/${number}/ready_for_review`, {
281
+ method: 'POST',
282
+ fetchImpl,
283
+ });
284
+ const body = response.json || {};
285
+ return {
286
+ number: Number(body.number) || number,
287
+ draft: Boolean(body.draft),
288
+ state: typeof body.state === 'string' ? body.state : 'open',
289
+ };
290
+ }
291
+
292
+ /** GitHub 拒合并时的措辞分类:检查没过与其余不可合并要分开说。 */
293
+ function mergeRejection(error) {
294
+ const reason = `${error && error.reason ? error.reason : ''} ${error && error.message ? error.message : ''}`;
295
+ if (/check|status/i.test(reason)) {
296
+ return ghError('CHECKS_FAILING', '这条 PR 的检查还没过,先在 GitHub 上把检查跑绿');
297
+ }
298
+ return ghError('MERGE_NOT_ALLOWED', 'GitHub 不让合这条 PR(可能有冲突或分支保护),回电脑上看一下');
299
+ }
300
+
301
+ /**
302
+ * 合并一条 PR。只认 `squash` / `merge`,不收提交信息、不收 rebase。
303
+ *
304
+ * 先读一次 PR:已经合并过的直接回 `ALREADY_MERGED`(二次点击是常态,报成失败没有意义)。
305
+ * **不预判 checks** —— 那是 GitHub 的事,它拒了我们再按措辞分成 `CHECKS_FAILING` / `MERGE_NOT_ALLOWED`。
306
+ */
307
+ async function mergeOf(root, number, method, options = {}) {
308
+ const fetchImpl = options.fetchImpl || globalThis.fetch;
309
+ if (!MERGE_METHODS.has(method)) throw ghError('BAD_REQUEST', '合并方式只支持 squash 或 merge');
310
+ const repo = await requireGithubRepo(root);
311
+ const before = await ghRequest(repo, `${repoPath(repo)}/pulls/${number}`, { fetchImpl });
312
+ const current = before.json || {};
313
+ if (current.merged_at || current.state === 'merged') {
314
+ throw ghError('ALREADY_MERGED', `#${number} 已经合并过了`);
315
+ }
316
+ let response;
317
+ try {
318
+ response = await ghRequest(repo, `${repoPath(repo)}/pulls/${number}/merge`, {
319
+ method: 'PUT',
320
+ body: { merge_method: method },
321
+ fetchImpl,
322
+ });
323
+ } catch (error) {
324
+ if (error && (error.status === 405 || error.status === 409)) {
325
+ // 被拒之后回读一次:有可能是另一处刚合完(二次点击 / 两个人同时按)。那就按幂等提示回。
326
+ const after = await ghRequest(repo, `${repoPath(repo)}/pulls/${number}`, { fetchImpl });
327
+ const latest = after.json || {};
328
+ if (latest.merged_at || latest.state === 'merged') {
329
+ throw ghError('ALREADY_MERGED', `#${number} 已经合并过了`);
330
+ }
331
+ throw mergeRejection(error);
332
+ }
333
+ throw error;
334
+ }
335
+ const body = response.json || {};
336
+ if (!body.merged) throw mergeRejection({ reason: body.message || '', message: '' });
337
+ return { merged: true, sha: typeof body.sha === 'string' ? body.sha : '' };
338
+ }
339
+
340
+ /** 一条 PR 的评论(走 issues/{number}/comments:PR 的评论就是这个端点)。 */
341
+ async function commentsOf(root, number, options = {}) {
342
+ const fetchImpl = options.fetchImpl || globalThis.fetch;
343
+ const repo = await requireGithubRepo(root);
344
+ const response = await ghRequest(
345
+ repo,
346
+ `${repoPath(repo)}/issues/${number}/comments?per_page=${CHECKS_PER_PAGE}`,
347
+ { fetchImpl },
348
+ );
349
+ const list = Array.isArray(response.json) ? response.json : [];
350
+ return list.map((item) => ({
351
+ author: item && item.user && typeof item.user.login === 'string' ? item.user.login : '',
352
+ body: item && typeof item.body === 'string' ? item.body : '',
353
+ createdAt: item && typeof item.created_at === 'string' ? item.created_at : '',
354
+ url: item && typeof item.html_url === 'string' ? item.html_url : '',
355
+ }));
356
+ }
357
+
358
+ module.exports = {
359
+ GITHUB_API,
360
+ GITHUB_TIMEOUT_MS,
361
+ MERGE_METHODS,
362
+ PR_STATES,
363
+ githubToken,
364
+ normalizeConclusion,
365
+ normalizeState,
366
+ summarizeChecks,
367
+ githubRepoOf,
368
+ pullsOf,
369
+ checksOf,
370
+ markReady,
371
+ mergeOf,
372
+ commentsOf,
373
+ // 内部函数,导出只给测试用:对外仍然只接固定子路径。
374
+ ghRequest,
375
+ };
package/lib/git/read.js CHANGED
@@ -612,4 +612,5 @@ module.exports = {
612
612
  branchesOf,
613
613
  overviewOf,
614
614
  githubTargetOf,
615
+ remotesOf,
615
616
  };