dsh-mobile-plugin 0.5.67 → 0.5.68

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.
@@ -13,6 +13,23 @@
13
13
  > 把扩展目录放进插件包,发到 npm 或你自己的仓库 → 用户自己装这个插件 → 手机从他那台电脑
14
14
  > 把包拉下来。我们不审、不登记,也没有提 PR 这一步。
15
15
 
16
+ ## 这份文档怎么维护(维护者看,作者可跳过)
17
+
18
+ 这一节写给改这份文档的人。几条规矩定死,免得文档越写越大之后自己乱掉:
19
+
20
+ - **这里唯一是来源**。改这份,别改插件包里那份 `docs/extension-authoring.md` 与
21
+ `docs/extension-sample/` —— 那是发布前从这儿同步过去的副本,手改会被下次同步盖掉。
22
+ 改完到 `dsh-mobile-plugin/` 跑 `node scripts/sync-author-docs.mjs` 把副本跟上;发版预检
23
+ 会比对两边,对不上发不了。
24
+ - **节号只增不改、不重排**。新内容往后追加新号,已经写出去的节号钉死不动 —— 正文里几十处
25
+ 写死「见第 X 节」,重排一次引用全断。引用时顺带说清讲的是什么,别只甩一个节号。2.05 /
26
+ 2.06 排在 2.02 前面、3.5 / 3.6 与 3.1 不同层,是历次追加留下的,别回头 renumber。
27
+ - **新内容按主题归位**。能力、协议、行为的规则写进对应主题那一节;真机上撞过的坑写清现象、
28
+ 原因、判据,归到对应主题或「9.6 这些现在做不到,别按它设计」附近。App 内部实现细节不进
29
+ 这份文档。
30
+ - **反馈确认解决就补,不必再吩咐**。用户反馈、GitHub issue、自己写扩展踩到又解决的问题,
31
+ 只要确认解决无误、属于作者会撞上的约束或坑,就照上面归位补进来,不用等谁再说一遍。
32
+
16
33
  ## 0. 先给「帮你写的那个 AI」看这一段
17
34
 
18
35
  **先把说明书和样例拿到手**:它们随 `dsh-mobile-plugin` 这个 npm 包发布 —— 装上那个插件,你
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ // 2026-11-18:下载加国内镜像回退(官方不通自动换源,仍按同一份 sha256 校验),失败文案挖出 cause 真话。
4
+
3
5
  /**
4
6
  * 在用户电脑上「弄到一份能用的 frpc」:先看用户自己有没有,再看有没有缓存,都没有就按平台
5
7
  * 从官方 release 拉一份、校验、解包、落到缓存目录。
@@ -12,9 +14,12 @@
12
14
  * ① 用户指定的路径(环境变量 `DSHM_FRPC_BIN`)或系统 PATH 里已有的 frpc —— 直接用,不下载;
13
15
  * ② 以前下载过的缓存(按「版本 + 平台」存)—— 直接用;
14
16
  * ③ 都没有 —— 按当前系统与架构从官方 release 下载对应包,sha256 校验通过后再解包落盘。
17
+ * 国内常连不上 GitHub,下载这一步会依次回退几条公开加速镜像;镜像只换传输路径,
18
+ * 仍是同一份资产、同一份钉死的 sha256,校验不符当场拒掉,不因为「换源」放松验证。
15
19
  *
16
20
  * 失败一律走返回值(`{ ok: false, reason, message }`),从不抛给调用方:这条只是可选能力,
17
- * 弄不到就等于「这台机器没有 frpc」,插件其它功能一点不受影响。
21
+ * 弄不到就等于「这台机器没有 frpc」,插件其它功能一点不受影响。全源都不通时把每条源的
22
+ * 底层原因(从 undici 的 `cause` 里挖出来的真话,而不是一句 `fetch failed`)一并列出来。
18
23
  *
19
24
  * 纯 Node 实现,不依赖系统 tar/unzip:下载的是官方 .tar.gz(Linux/macOS)或 .zip(Windows),
20
25
  * 两种解包都在本文件里手写,只挑出里面那个 frpc 可执行文件,其余(frps、LICENSE…)不落盘。
@@ -29,9 +34,47 @@ const { dshHome } = require('../shared/paths.js');
29
34
  /** 钉死的 frp 版本。换版本时改这一个常量,并把下面的文件名与 sha256 一起更新。 */
30
35
  const FRP_VERSION = '0.72.0';
31
36
 
32
- /** 官方 release 的下载根;可被环境变量覆盖(以后换成自家分发服务器只改这里)。 */
37
+ /**
38
+ * 官方 release 的下载根;国内常连不上,下面挂着几条公开加速镜像,按顺序回退。
39
+ * 只换「同一份字节的传输路径」,资产与 sha256 不变——镜像给错内容照样在校验那步被拒。
40
+ * 镜像是第三方服务,会失效也会变,所以做成一张可替换的表,且永远最后才动失败文案。
41
+ */
33
42
  const DEFAULT_BASE_URL = `https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}`;
34
43
 
44
+ const MIRROR_BASE_URLS = [
45
+ `https://gh-proxy.com/https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}`,
46
+ `https://ghfast.top/https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}`,
47
+ `https://ghproxy.net/https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}`,
48
+ ];
49
+
50
+ /**
51
+ * 这一轮下载要按顺序试的根地址。
52
+ *
53
+ * 优先级:`baseUrl`(部署方/测试显式钉死,只试这一条)> 官方 + 镜像。任何一环给出的
54
+ * 字节都要过同一份 sha256,镜像进不了「少校验」的捷径。
55
+ */
56
+ function baseUrlsOf(options = {}) {
57
+ if (typeof options.baseUrl === 'string' && options.baseUrl) return [options.baseUrl];
58
+ return [DEFAULT_BASE_URL, ...MIRROR_BASE_URLS];
59
+ }
60
+
61
+ /**
62
+ * undici 把真正的 syscall 错塞在 `cause` 里,表层只有一句没信息的 `fetch failed`。
63
+ * 沿 cause 链找第一句比 "fetch failed" 更具体的话(connect ETIMEDOUT / ENOTFOUND /
64
+ * ECONNREFUSED / 证书问题…);找不到就退回表层那句,不凭空编。
65
+ */
66
+ function fetchErrorMessage(error) {
67
+ let cursor = error;
68
+ let generic = '';
69
+ for (let depth = 0; depth < 4 && cursor; depth += 1) {
70
+ const message = typeof cursor.message === 'string' ? cursor.message.trim() : '';
71
+ if (message && message.toLowerCase() !== 'fetch failed') return message;
72
+ if (message) generic = message;
73
+ cursor = cursor.cause;
74
+ }
75
+ return generic || (error && error.message ? error.message : String(error));
76
+ }
77
+
35
78
  /**
36
79
  * 支持的平台 → 资产描述。
37
80
  *
@@ -221,7 +264,7 @@ async function downloadTo({ url, asset, dir, fetchImpl, timeoutMs = DOWNLOAD_TIM
221
264
  try {
222
265
  response = await doFetch(url, controller ? { signal: controller.signal } : {});
223
266
  } catch (error) {
224
- throw codedError('download-failed', `下载 frpc 失败:${error && error.message ? error.message : error}`);
267
+ throw codedError('download-failed', `下载 frpc 失败:${fetchErrorMessage(error)}`);
225
268
  } finally {
226
269
  if (timer) clearTimeout(timer);
227
270
  }
@@ -334,16 +377,34 @@ async function ensureFrpc(options = {}) {
334
377
  }
335
378
  const root = options.cacheRoot || cacheRoot(options);
336
379
  const staging = path.join(root, `.staging-${process.pid}-${crypto.randomBytes(6).toString('hex')}`);
337
- const baseUrl = options.baseUrl || DEFAULT_BASE_URL;
338
- const url = `${baseUrl.replace(/\/+$/, '')}/${asset.file}`;
380
+ const bases = baseUrlsOf(options);
381
+ const attempts = [];
382
+ // 整轮回退共用一份时间预算(与过去单源超时相同):否则 GitHub 卡住不吃不吐时,
383
+ // 逐源各等满 120 秒会把面板晾八分钟。排在后面的镜像只拿得到「剩下的时间」。
384
+ const budgetMs = Number(options.timeoutMs) > 0 ? Number(options.timeoutMs) : DOWNLOAD_TIMEOUT_MS;
385
+ const deadline = Date.now() + budgetMs;
339
386
  try {
340
- await downloadTo({ url, asset, dir: staging, fetchImpl: options.fetchImpl, timeoutMs: options.timeoutMs });
341
- // 先剔掉可能残留的旧目录,再把暂存目录原子换上。
342
- fs.rmSync(dir, { recursive: true, force: true });
343
- fs.mkdirSync(path.dirname(dir), { recursive: true });
344
- moveDir(staging, dir);
345
- pruneOldVersions(root, version);
346
- return { ok: true, path: binaryPath, source: 'downloaded', version, platform };
387
+ for (const base of bases) {
388
+ const remaining = deadline - Date.now();
389
+ if (remaining <= 0) break;
390
+ const url = `${String(base).replace(/\/+$/, '')}/${asset.file}`;
391
+ try {
392
+ await downloadTo({ url, asset, dir: staging, fetchImpl: options.fetchImpl, timeoutMs: remaining });
393
+ // 先剔掉可能残留的旧目录,再把暂存目录原子换上。
394
+ fs.rmSync(dir, { recursive: true, force: true });
395
+ fs.mkdirSync(path.dirname(dir), { recursive: true });
396
+ moveDir(staging, dir);
397
+ pruneOldVersions(root, version);
398
+ return { ok: true, path: binaryPath, source: 'downloaded', version, platform };
399
+ } catch (error) {
400
+ const reason = error && error.message ? error.message : String(error);
401
+ attempts.push(reason.replace(/^下载 frpc 失败:/, ''));
402
+ // 校验不符不是「网络不通」:字节到了却对不上钉死的 sha256,换镜像也是同一份坏包,
403
+ // 继续试只会让人以为在尽力。当场拒掉,和过去单源的行为一致。
404
+ if (error && error.code === 'checksum') throw error;
405
+ }
406
+ }
407
+ return fail('download-failed', `下载 frpc 失败:${attempts.length} 个源都不通(${attempts.join(';')})`);
347
408
  } catch (error) {
348
409
  fs.rmSync(staging, { recursive: true, force: true });
349
410
  return fail(error && error.code ? error.code : 'download-failed', error && error.message ? error.message : String(error));
@@ -372,6 +433,9 @@ module.exports = {
372
433
  FRP_VERSION,
373
434
  ASSETS,
374
435
  DEFAULT_BASE_URL,
436
+ MIRROR_BASE_URLS,
437
+ baseUrlsOf,
438
+ fetchErrorMessage,
375
439
  platformId,
376
440
  cacheRoot,
377
441
  cacheDirOf,
@@ -80,25 +80,13 @@ function dispositionOf(name) {
80
80
  }
81
81
 
82
82
  /**
83
- * POST { path(绝对), sessionId? } → 该文件的原始字节。
83
+ * 判一条绝对路径认不认、认了盘上在不在、是不是普通文件 —— `/file/read` 取字节与
84
+ * `/file/stat` 问大小共用这**一份**放行判定。抄第二份就是给「能力面悄悄扩大」留后门:
85
+ * 能 stat 的路径必须与能下载的完全重合。
84
86
  *
85
- * @param {object} ctx 插件上下文
86
- * @param {object} req node:http IncomingMessage
87
- * @param {object} res node:http ServerResponse
87
+ * 回 `{ ok:true, target: realTarget, info }` 或 `{ ok:false, status, code, message }`。
88
88
  */
89
- async function handleFileRead(ctx, req, res) {
90
- const body = await readJsonBody(req);
91
- const declared = body && typeof body.path === 'string' ? body.path.trim() : '';
92
- const sessionId = body && typeof body.sessionId === 'string' ? body.sessionId.trim() : '';
93
- if (!declared) {
94
- return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { path }(绝对路径)' } });
95
- }
96
- if (!path.isAbsolute(declared)) {
97
- return sendJson(res, 400, {
98
- ok: false,
99
- error: { code: 'bad-request', message: 'path 必须是绝对路径' },
100
- });
101
- }
89
+ async function resolveReadableFile(ctx, declared, sessionId) {
102
90
  const target = path.resolve(declared);
103
91
 
104
92
  let roots;
@@ -106,19 +94,10 @@ async function handleFileRead(ctx, req, res) {
106
94
  roots = await allowedRoots(ctx, sessionId);
107
95
  } catch (error) {
108
96
  // 宿主那句原话回给手机:这条路上「没取到」与「你没有这个目录」必须分得开。
109
- return sendJson(res, 502, {
110
- ok: false,
111
- error: {
112
- code: (error && error.code) || 'workspace-list-failed',
113
- message: String((error && error.message) || error),
114
- },
115
- });
97
+ return { ok: false, status: 502, code: (error && error.code) || 'workspace-list-failed', message: String((error && error.message) || error) };
116
98
  }
117
99
  if (!inAllowedZone(roots, target)) {
118
- return sendJson(res, 403, {
119
- ok: false,
120
- error: { code: 'outside-allowed', message: '这个文件不在能看的范围里(只放行工作区目录和这条会话的项目目录)' },
121
- });
100
+ return { ok: false, status: 403, code: 'outside-allowed', message: '这个文件不在能看的范围里(只放行工作区目录和这条会话的项目目录)' };
122
101
  }
123
102
 
124
103
  // 字符串落点在允许区里还不够:工作区里的软链可能指到区外。与写侧(`file-ops.js` 的
@@ -126,24 +105,49 @@ async function handleFileRead(ctx, req, res) {
126
105
  const realTarget = await realpath(target).catch(() => null);
127
106
  if (!realTarget) {
128
107
  // realpath 失败 = 盘上没有了(或断了):与 stat 失败同一个 404 口子。
129
- return sendJson(res, 404, { ok: false, error: { code: 'missing', message: '文件已经不在这台电脑上了' } });
108
+ return { ok: false, status: 404, code: 'missing', message: '文件已经不在这台电脑上了' };
130
109
  }
131
110
  if (!inAllowedZone(await realRootsOf(roots), realTarget)) {
132
- return sendJson(res, 403, {
133
- ok: false,
134
- error: { code: 'outside-allowed', message: '这个文件的物理位置不在能看的范围里(可能经过了符号链接)' },
135
- });
111
+ return { ok: false, status: 403, code: 'outside-allowed', message: '这个文件的物理位置不在能看的范围里(可能经过了符号链接)' };
136
112
  }
137
113
 
138
114
  let info;
139
115
  try {
140
116
  info = await stat(realTarget);
141
117
  } catch {
142
- return sendJson(res, 404, { ok: false, error: { code: 'missing', message: '文件已经不在这台电脑上了' } });
118
+ return { ok: false, status: 404, code: 'missing', message: '文件已经不在这台电脑上了' };
143
119
  }
144
120
  if (!info.isFile()) {
145
- return sendJson(res, 400, { ok: false, error: { code: 'not-a-file', message: '这条路径不是一个文件' } });
121
+ return { ok: false, status: 400, code: 'not-a-file', message: '这条路径不是一个文件' };
122
+ }
123
+ return { ok: true, target: realTarget, info };
124
+ }
125
+
126
+ /**
127
+ * POST { path(绝对), sessionId? } → 该文件的原始字节。
128
+ *
129
+ * @param {object} ctx 插件上下文
130
+ * @param {object} req node:http IncomingMessage
131
+ * @param {object} res node:http ServerResponse
132
+ */
133
+ async function handleFileRead(ctx, req, res) {
134
+ const body = await readJsonBody(req);
135
+ const declared = body && typeof body.path === 'string' ? body.path.trim() : '';
136
+ const sessionId = body && typeof body.sessionId === 'string' ? body.sessionId.trim() : '';
137
+ if (!declared) {
138
+ return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { path }(绝对路径)' } });
139
+ }
140
+ if (!path.isAbsolute(declared)) {
141
+ return sendJson(res, 400, {
142
+ ok: false,
143
+ error: { code: 'bad-request', message: 'path 必须是绝对路径' },
144
+ });
146
145
  }
146
+ const resolved = await resolveReadableFile(ctx, declared, sessionId);
147
+ if (!resolved.ok) {
148
+ return sendJson(res, resolved.status, { ok: false, error: { code: resolved.code, message: resolved.message } });
149
+ }
150
+ const { target: realTarget, info } = resolved;
147
151
  if (info.size > SESSION_FILE_MAX_BYTES) {
148
152
  return sendJson(res, 413, {
149
153
  ok: false,
@@ -167,4 +171,29 @@ async function handleFileRead(ctx, req, res) {
167
171
  });
168
172
  }
169
173
 
170
- module.exports = { handleFileRead, mediaTypeOf };
174
+ /**
175
+ * POST { path(绝对), sessionId? } → `{ ok:true, size }`。
176
+ *
177
+ * 只回**原文件明文字节数**,不取字节。给 App 的下载兜底用:下载中途断在原生那一侧时,
178
+ * App 拿这个数核一下已经落盘的密文解出来是不是正好这么多,齐了就直接认、别从 0 重下。
179
+ * 放行面与 `/file/read` 完全同一份(`resolveReadableFile`),不比「能下载的」多认一条路径。
180
+ * 不受 512MB 上限约束:问大小不需要把文件读起来。
181
+ */
182
+ async function handleFileStat(ctx, req, res) {
183
+ const body = await readJsonBody(req);
184
+ const declared = body && typeof body.path === 'string' ? body.path.trim() : '';
185
+ const sessionId = body && typeof body.sessionId === 'string' ? body.sessionId.trim() : '';
186
+ if (!declared) {
187
+ return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { path }(绝对路径)' } });
188
+ }
189
+ if (!path.isAbsolute(declared)) {
190
+ return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: 'path 必须是绝对路径' } });
191
+ }
192
+ const resolved = await resolveReadableFile(ctx, declared, sessionId);
193
+ if (!resolved.ok) {
194
+ return sendJson(res, resolved.status, { ok: false, error: { code: resolved.code, message: resolved.message } });
195
+ }
196
+ return sendJson(res, 200, { ok: true, size: Number(resolved.info.size) || 0 });
197
+ }
198
+
199
+ module.exports = { handleFileRead, handleFileStat, mediaTypeOf };
@@ -224,6 +224,7 @@ const MAP = Object.freeze({
224
224
 
225
225
  // ── files.read:读文件 / 目录 / 工作区 / git / 技能正文 ─────────
226
226
  '/api/mobile/v1/file/read': V.FILES_READ,
227
+ '/api/mobile/v1/file/stat': V.FILES_READ,
227
228
  '/api/mobile/v1/directory/list': V.FILES_READ,
228
229
  '/api/mobile/v1/directory/roots': V.FILES_READ,
229
230
  '/api/mobile/v1/directory/listing': V.FILES_READ,
@@ -205,7 +205,12 @@ async function handleSelectModel(ctx, req, res) {
205
205
  const payload = { sessionId: body.sessionId, provider: body.provider, model: body.model };
206
206
  if (typeof body.reasoningEffort === 'string' && body.reasoningEffort) payload.reasoningEffort = body.reasoningEffort;
207
207
  const { value, error } = unwrap(await selectSessionModel(ctx, payload));
208
- if (error) return sendJson(res, 400, { ok: false, error });
208
+ if (error) {
209
+ // 老宿主没有「只切当前会话模型」的能力,或新建会话还没有可切模型:与 `/session/send`
210
+ // 同一口径——静默放过、用会话当前模型继续,别让新建会话这一步把第一条消息卡死。
211
+ if (error.code === 'host-unsupported') return sendJson(res, 200, { ok: true, applied: false });
212
+ return sendJson(res, 400, { ok: false, error });
213
+ }
209
214
  invalidateSessionModels(ctx, req, body.sessionId);
210
215
  sendJson(res, 200, { ok: true, ...(value || {}) });
211
216
  }
@@ -458,6 +458,8 @@ function registerMobileRoutes(deps) {
458
458
  // 文件浏览器按绝对路径取字节,范围是宿主已登记的工作区及当前会话的项目目录
459
459
  //(见 mobile/file-read.js 与 mobile/allowed-zone.js)。
460
460
  route('/api/mobile/v1/file/read', fileRead.handleFileRead)
461
+ // 只问大小(下载兜底用):放行面同 `/file/read`,不取字节。
462
+ route('/api/mobile/v1/file/stat', fileRead.handleFileStat)
461
463
  route('/api/mobile/v1/session/send', sessions.handleSend)
462
464
  // 「这一发到底进没进」:App 的离线发件箱在重发之前先问一句(按 clientMessageId 回查
463
465
  // 会话正文)。只读、只看最近一窗,见 mobile/session-delivery.js。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-plugin",
3
- "version": "0.5.67",
3
+ "version": "0.5.68",
4
4
  "description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
5
5
  "license": "MIT",
6
6
  "icon": "./icon.png",