dsh-mobile-plugin 0.5.113 → 0.5.114

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.
@@ -1,5 +1,6 @@
1
1
  // Cloudflare 快速隧道的进程起停、banner 地址解析、连没连上边缘、崩溃退避重连与用户开关标记。
2
2
  // 内置保底隧道默认开:标记文件记的是「用户显式关掉」,没有这个文件就算开着。
3
+ // 进程消失以 exit 为准(stdio 管道被占时 close 可能不来),exit/close 共用幂等清理;spawn error 也清 child 并撤回地址。
3
4
  'use strict';
4
5
 
5
6
  /** 本机 cloudflared 快速隧道的进程生命周期管理。 */
@@ -270,10 +271,16 @@ function createCloudflaredRunner(options = {}) {
270
271
  stream.on('data', consume);
271
272
  }
272
273
  if (typeof proc.on === 'function') {
273
- proc.on('error', (err) => { if (child === proc) error = describeSpawnFailure(err, binary); });
274
- // close 时两路输出已收全,保留 cloudflared 自己给的退出原因。
275
- proc.on('close', (code, signal) => {
276
- if (child !== proc) return;
274
+ /** 进程没了的最终原因;close 到得晚、输出更全,用它覆盖 exit 时那句粗略的原因。 */
275
+ const setGoneError = (code, signal) => {
276
+ const lastLine = tail.trim() ? tail.trim().split('\n').pop() : '';
277
+ error = `cloudflared 已退出(code=${code}${signal ? `, ${signal}` : ''})${lastLine ? `:${lastLine}` : ''}`;
278
+ };
279
+ let gone = false;
280
+ /** exit 和 close 共用的幂等清理:谁先到谁做一次,管道被占住时 close 不来就靠 exit 兜底。 */
281
+ const onGone = (code, signal) => {
282
+ if (gone) return;
283
+ gone = true;
277
284
  clearConnectTimer();
278
285
  child = null;
279
286
  runningLocalPort = 0;
@@ -282,9 +289,21 @@ function createCloudflaredRunner(options = {}) {
282
289
  lineBuf = '';
283
290
  // 进程没了,之前上报的地址一定连不上,先撤掉;重连成功会带着新地址再报一次。
284
291
  withdrawAddress();
285
- const lastLine = tail.trim() ? tail.trim().split('\n').pop() : '';
286
- error = `cloudflared 已退出(code=${code}${signal ? `, ${signal}` : ''})${lastLine ? `:${lastLine}` : ''}`;
292
+ setGoneError(code, signal);
287
293
  scheduleRestart();
294
+ };
295
+ proc.on('error', (err) => {
296
+ if (child !== proc || gone) return;
297
+ error = describeSpawnFailure(err, binary);
298
+ // spawn 失败时 child 还挂着,不清掉 launch() 里的 if(child) return 会永久卡死。
299
+ child = null;
300
+ withdrawAddress();
301
+ scheduleRestart();
302
+ });
303
+ proc.on('exit', (code, signal) => { if (child === proc) onGone(code, signal); });
304
+ proc.on('close', (code, signal) => {
305
+ if (child === proc) onGone(code, signal);
306
+ else if (gone) setGoneError(code, signal);
288
307
  });
289
308
  }
290
309
  })();
package/lib/impl.js CHANGED
@@ -1,3 +1,4 @@
1
+ // 本次改动:网页版预览的宿主绑定地址随 ctx.webServer.host 传入,老宿主缺省时维持环回行为。
1
2
  // 兼容服务挂不上时按本代装载失败处理,交给外壳重试,不再静默当成功。
2
3
  // Cloudflare 兜底隧道:本地装配 cloudflared runner,把快速隧道地址写进独立字段 cloudflareUrl(作为新线路 kind:'cf' 上报,不再写直连格)。
3
4
  // 身份变更同时迁移三类连接登记,主动重连独立于普通配置同步。
@@ -663,7 +664,8 @@ function apply(ctx) {
663
664
  // 「把桌面端 GUI 搬进手机」这件事。
664
665
  //
665
666
  // `ownWebPort` 是同一条边界上的唯一例外:用户显式要求手机能打开网页版(2026-11-03),
666
- // 所以 `mode=webapp` 那一档只放行这一个端口(且目标必须是环回)。同样用取值器。
667
+ // 所以 `mode=webapp` 那一档只放行这一个端口(目标主机还得是环回或宿主绑定地址)。同样用取值器。
668
+ // `ownWebHost` 跟随宿主实际绑定地址:网页版管道必须拨它,否则宿主绑在局域网地址上时手机侧两发拨空。
667
669
  const preview = createPreviewService({
668
670
  authorized: (req) => {
669
671
  const outcome = authOutcome(req)
@@ -678,6 +680,7 @@ function apply(ctx) {
678
680
  return ports
679
681
  },
680
682
  ownWebPort: () => ctx.webServer.port,
683
+ ownWebHost: () => ctx.webServer.host,
681
684
  permission: permissions.createUpgradePermission({ credentials, instanceId: state.instanceId, presentedCredential }),
682
685
  onLog: (message) => ctx.logger?.info?.(`${name}: %s`, message),
683
686
  })
@@ -1,106 +1,48 @@
1
- // 用途:订阅固定分发平台的公开反馈 SSE,实时等第一条官方回复,工具与本机控制面共用同一份线程契约。
1
+ // 用途:轮询平台现有反馈读取端点,等线程里出现第一条新官方回复;超时给改用 read 的提示。
2
2
  'use strict';
3
3
 
4
- const PLATFORM_ORIGIN = 'https://app.wnluo.com';
5
- const FEEDBACK_EVENTS_PATH = '/api/sdk/feedback/events';
6
- const DEFAULT_TIMEOUT_MS = 5 * 60_000;
4
+ const { readFeedback } = require('./feedback-read.cjs');
7
5
 
8
- /** 与 feedback-read.cjs 同一口径:错误自带 status,调用方照原样带出去。 */
9
- function feedbackError(message, status) {
10
- return Object.assign(new Error(message), { status });
11
- }
12
-
13
- /** 只认 id 大于水位、作者是官方的 reply;其余帧都不算回复。 */
14
- function staffReplyOf(event, data, afterMessageId) {
15
- if (event !== 'reply' || !data) return null;
16
- let payload = null;
17
- try { payload = JSON.parse(data); } catch { return null; }
18
- if (!payload || typeof payload.id !== 'number' || payload.id <= afterMessageId || payload.author !== 'staff') return null;
19
- return {
20
- id: payload.id,
21
- author: 'staff',
22
- content: typeof payload.content === 'string' ? payload.content : '',
23
- created_at: typeof payload.created_at === 'string' ? payload.created_at : '',
24
- };
25
- }
6
+ /** 两秒一问、最多两分钟;不是长连接,到点就停,不无限等。 */
7
+ const DEFAULT_INTERVAL_MS = 2_000;
8
+ const DEFAULT_TIMEOUT_MS = 2 * 60_000;
9
+ const TIMEOUT_MESSAGE = '暂时没收到回复,请用 read 查看';
26
10
 
27
11
  /**
28
- * 挂 SSE 长连接等官方回复。只读公开事件流,不读凭据、不碰会话内容。
29
- * 选项:afterMessageId(已见过的最大 id,默认 0)、timeoutMs(默认 5 分钟)。
30
- * afterMessageId 原样作为 after 查询参数带上(默认 0 也带),让平台从这条之后回放已有回复,
31
- * 覆盖 submit 后立刻回复、watch 稍后才连上的情况。
32
- * 超时或服务端提前断流且没有回复时返回 { timedOut: true };HTTP 错误与网络错误抛出。
12
+ * 轮询现有 /api/sdk/feedback(与 read 同一端点)等第一条官方回复。
13
+ * 只读公开反馈,不读凭据、不碰会话内容。
14
+ *
15
+ * 选项:afterMessageId(已见过的最大 id,默认 0)、intervalMs(默认 2 秒)、
16
+ * timeoutMs(默认 2 分钟)。平台没有公开事件流可用,所以每轮沿用 read 的线程视图,
17
+ * 取第一条 author 为 staff 且 id 大于水位的消息;提交后立刻到达的回复也在第一轮被看到。
18
+ * 到点仍无回复返回 { timedOut: true, message };HTTP 与网络错误照状态抛出。
33
19
  */
34
20
  async function watchFeedback(threadKey, options = {}) {
35
21
  const key = typeof threadKey === 'string' ? threadKey.trim() : '';
36
- if (!key) throw feedbackError('watch 需要 threadKey', 400);
22
+ if (!key) throw Object.assign(new Error('watch 需要 threadKey'), { status: 400 });
37
23
  const opts = options && typeof options === 'object' ? options : {};
38
24
  const afterMessageId = Number.isFinite(opts.afterMessageId) ? opts.afterMessageId : 0;
39
25
  const timeoutMs = Number.isFinite(opts.timeoutMs) && opts.timeoutMs > 0 ? opts.timeoutMs : DEFAULT_TIMEOUT_MS;
40
- const controller = new AbortController();
41
- const timer = setTimeout(() => controller.abort(), timeoutMs);
42
- let reader = null;
43
- try {
44
- const response = await fetch(`${PLATFORM_ORIGIN}${FEEDBACK_EVENTS_PATH}?thread_key=${encodeURIComponent(key)}&after=${afterMessageId}`, {
45
- method: 'GET',
46
- headers: { accept: 'text/event-stream' },
47
- signal: controller.signal,
48
- });
49
- if (response.status < 200 || response.status >= 300) {
50
- let message = '';
51
- if (typeof response.json === 'function') {
52
- try {
53
- const body = await response.json();
54
- if (body && typeof body.error === 'string') message = body.error.trim();
55
- } catch { /* 非 JSON 错误体按状态码报 */ }
56
- }
57
- throw feedbackError(message || `反馈订阅失败(HTTP ${response.status})`, response.status);
58
- }
59
- if (!response.body || typeof response.body.getReader !== 'function') {
60
- throw feedbackError('反馈订阅没有返回事件流', 502);
61
- }
62
- reader = response.body.getReader();
63
- const decoder = new TextDecoder();
64
- let buffer = '';
65
- let event = '';
66
- let data = '';
67
- for (;;) {
68
- const { done, value } = await reader.read();
69
- if (done) break;
70
- buffer += decoder.decode(value, { stream: true });
71
- let index;
72
- while ((index = buffer.indexOf('\n')) >= 0) {
73
- let line = buffer.slice(0, index);
74
- buffer = buffer.slice(index + 1);
75
- if (line.endsWith('\r')) line = line.slice(0, -1);
76
- if (line === '') {
77
- const reply = staffReplyOf(event, data, afterMessageId);
78
- if (reply) return reply;
79
- event = '';
80
- data = '';
81
- continue;
82
- }
83
- if (line.startsWith(':')) continue; // 心跳注释
84
- const colon = line.indexOf(':');
85
- const field = colon === -1 ? line : line.slice(0, colon);
86
- let valueText = colon === -1 ? '' : line.slice(colon + 1);
87
- if (valueText.startsWith(' ')) valueText = valueText.slice(1);
88
- if (field === 'event') event = valueText;
89
- else if (field === 'data') data = data ? `${data}\n${valueText}` : valueText;
90
- }
91
- }
92
- const pending = staffReplyOf(event, data, afterMessageId);
93
- if (pending) return pending;
94
- return { timedOut: true };
95
- } catch (error) {
96
- if (error && (error.name === 'AbortError' || controller.signal.aborted)) return { timedOut: true };
97
- throw error;
98
- } finally {
99
- clearTimeout(timer);
100
- if (reader) {
101
- try { await reader.cancel(); } catch { /* 流已被服务端收掉 */ }
26
+ const intervalMs = Number.isFinite(opts.intervalMs) && opts.intervalMs > 0 ? opts.intervalMs : DEFAULT_INTERVAL_MS;
27
+ const deadline = Date.now() + timeoutMs;
28
+
29
+ for (;;) {
30
+ const thread = await readFeedback(key);
31
+ const messages = Array.isArray(thread?.messages) ? thread.messages : [];
32
+ const reply = messages.find((message) => message && message.author === 'staff'
33
+ && typeof message.id === 'number' && message.id > afterMessageId);
34
+ if (reply) {
35
+ return {
36
+ id: reply.id,
37
+ author: 'staff',
38
+ content: reply.content,
39
+ created_at: reply.created_at,
40
+ };
102
41
  }
42
+ const remaining = deadline - Date.now();
43
+ if (remaining <= 0) return { timedOut: true, message: TIMEOUT_MESSAGE };
44
+ await new Promise(resolve => setTimeout(resolve, Math.min(intervalMs, remaining)));
103
45
  }
104
46
  }
105
47
 
106
- module.exports = { watchFeedback };
48
+ module.exports = { DEFAULT_INTERVAL_MS, DEFAULT_TIMEOUT_MS, TIMEOUT_MESSAGE, watchFeedback };
@@ -1,4 +1,4 @@
1
- // 用途:插件反馈统一提交、读取与实时等回复口;工具和本机面板按线程查看官方回复、继续跟进。
1
+ // 用途:插件反馈统一提交、读取与轮询等回复口;工具和本机面板按线程查看官方回复、继续跟进。
2
2
  'use strict';
3
3
 
4
4
  /**
@@ -10,8 +10,8 @@
10
10
  * - **分发平台**:公开反馈接口(`/api/sdk/feedback`)。明确指定设备号时带上那台手机的
11
11
  * 身份;没指定时用这台电脑上的插件安装身份;没配手机时依然能交,受影响手机那几格留空。
12
12
  * 诊断文本作为 text/plain 附件一起发。线程 key 当场回显,可在本机面板或工具里读取
13
- * 官方回复,再带同一个 key 继续补充;submit 后也可以用 watch 挂长连接实时等官方回复,
14
- * 不要靠反复 read 轮询。线程历史由提交端保留,不写进 App 的反馈列表。
13
+ * 官方回复,再带同一个 key 继续补充;submit 后也可以用 watch 每两秒轮询现有反馈端点等官方回复,
14
+ * 最多两分钟,超时提示改用 read。线程历史由提交端保留,不写进 App 的反馈列表。
15
15
  * - **GitHub**:插件代码本身的 bug 可以直接提 PR。这条路依赖用户自己登录 GitHub,
16
16
  * 插件无法代提,所以只把仓库与前提写进说明,由 AI 自己判断走不走。
17
17
  *
@@ -68,7 +68,7 @@ function feedbackChannels() {
68
68
  {
69
69
  kind: 'distribution',
70
70
  name: 'APP 分发系统反馈',
71
- description: '提交到分发平台的公开反馈接口(与 App 里的反馈页同一个)。没配手机也能提交:指定设备号才带那台手机的身份,否则用这台电脑的插件安装身份;诊断文本作 text/plain 附件。提交后给出可复制的线程 key,可在本机面板或工具 watch 动作实时等官方回复,不要反复 read 轮询;需要看历史再 read,用 submit 带 threadKey 继续补充;不会写进 App 的反馈列表。',
71
+ description: '提交到分发平台的公开反馈接口(与 App 里的反馈页同一个)。没配手机也能提交:指定设备号才带那台手机的身份,否则用这台电脑的插件安装身份;诊断文本作 text/plain 附件。提交后给出可复制的线程 key,可在本机面板或工具用 watch 定时轮询现有反馈端点等第一条官方回复(每两秒一次,最多两分钟,超时提示改用 read);需要看历史再 read,用 submit 带 threadKey 继续补充;不会写进 App 的反馈列表。',
72
72
  endpoint: `${PLATFORM_ORIGIN}${FEEDBACK_PATH}`,
73
73
  requiresLogin: false,
74
74
  },
@@ -242,7 +242,7 @@ function registerFeedbackTool(ctx, deps) {
242
242
  ctx.tools.register(defineTool({
243
243
  name: 'dsh_feedback',
244
244
  description: 'Report a real problem with this mobile plugin or the AI-HUB app. '
245
- + 'Feedback routes: (1) the APP distribution platform — `submit` sends the content to it. You do NOT need to find the root cause: describe what you observed, what you already checked, and what is still uncertain. No paired phone is required either; without an explicitly chosen device the plugin submits under its local installation identity, with a text/plain diagnostics attachment and a thread key you can show to the user. After `submit`, call `watch` with the returned threadKey to wait in real time for the official reply; do NOT repeatedly poll `read` while waiting. Use `read` with the thread key only when you need to review the conversation history, and `submit` with the same threadKey to follow up. The local feedback dialog can show these threads; they are not written into the in-app feedback list. '
245
+ + 'Feedback routes: (1) the APP distribution platform — `submit` sends the content to it. You do NOT need to find the root cause: describe what you observed, what you already checked, and what is still uncertain. No paired phone is required either; without an explicitly chosen device the plugin submits under its local installation identity, with a text/plain diagnostics attachment and a thread key you can show to the user. After `submit`, call `watch` with the returned threadKey: it checks the existing feedback endpoint every two seconds for the first official reply, for at most two minutes, then stops with a hint to use `read`. It is polling, not a live stream. Use `read` with the thread key only when you need to review the conversation history, and `submit` with the same threadKey to follow up. The local feedback dialog can show these threads; they are not written into the in-app feedback list. '
246
246
  + '(2) GitHub — code-level bugs can go to https://github.com/WNLUO/dsh-mobile-plugin as a pull request or issue, but that needs the user to be logged into GitHub and is not submitted by this tool. '
247
247
  + 'Use `channels` to list both routes.',
248
248
  parameters: {
@@ -250,7 +250,7 @@ function registerFeedbackTool(ctx, deps) {
250
250
  type: 'string',
251
251
  required: true,
252
252
  enum: ['channels', 'read', 'submit', 'watch'],
253
- description: 'channels: list the feedback routes. read: view a thread and replies. submit: open a feedback thread or follow up using threadKey. watch: after submit, wait on the public event stream for the first official staff reply on that threadKey; do not poll read.',
253
+ description: 'channels: list the feedback routes. read: view a thread and replies. submit: open a feedback thread or follow up using threadKey. watch: after submit, poll the existing read endpoint for the first official staff reply on that threadKey (every two seconds, up to two minutes, then a hint to use read).',
254
254
  },
255
255
  content: {
256
256
  type: 'string',
@@ -315,7 +315,7 @@ function registerFeedbackTool(ctx, deps) {
315
315
  case 'watch': {
316
316
  const reply = await watchFeedback(args.threadKey, { afterMessageId: args.afterMessageId });
317
317
  return reply && reply.timedOut
318
- ? { action: 'watch', threadKey: args.threadKey, timedOut: true }
318
+ ? { action: 'watch', threadKey: args.threadKey, timedOut: true, message: reply.message }
319
319
  : { action: 'watch', threadKey: args.threadKey, reply };
320
320
  }
321
321
  default:
@@ -338,7 +338,7 @@ function renderSummary(value) {
338
338
  case 'watch':
339
339
  return value.reply
340
340
  ? `反馈 ${value.threadKey} 收到官方回复(${value.reply.created_at}):${value.reply.content}`
341
- : `反馈 ${value.threadKey} 还没有官方回复,等官方答复后再继续。`;
341
+ : `反馈 ${value.threadKey} ${value.message || '暂时没收到回复,请用 read 查看。'}`;
342
342
  default:
343
343
  return '完成。';
344
344
  }