dsh-mobile-plugin 0.5.94 → 0.5.96

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 (34) hide show
  1. package/README.md +1 -1
  2. package/config/agent-presets/standard/skills/dsh-mobile-kb/SKILL.md +11 -3
  3. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/cloudflare.md +58 -0
  4. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/frp.md +50 -0
  5. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/ipv4.md +2 -2
  6. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/load-error-after-action.md +27 -0
  7. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/network-guide.md +55 -0
  8. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/network.md +5 -1
  9. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/plugin-not-loaded.md +22 -0
  10. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/relay-server.md +48 -0
  11. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/relay.md +9 -0
  12. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/selfhost-common.md +1 -1
  13. package/lib/cloudflared/provision.cjs +392 -0
  14. package/lib/cloudflared/runner.cjs +305 -0
  15. package/lib/cloudflared/wiring.cjs +82 -0
  16. package/lib/frpc/runner.cjs +1 -1
  17. package/lib/impl.js +11 -1
  18. package/lib/mobile/history-derive.js +34 -0
  19. package/lib/mobile/pairing.js +4 -0
  20. package/lib/mobile/question-reply.js +159 -0
  21. package/lib/mobile/stream.js +25 -2
  22. package/lib/panel/index.html +47 -27
  23. package/lib/relay/config-store.js +9 -0
  24. package/lib/relay/control.js +8 -0
  25. package/lib/relay/core.js +6 -0
  26. package/lib/relay/lan-network.cjs +23 -0
  27. package/lib/relay/pairing-payload.cjs +2 -1
  28. package/lib/remote/tunnel-helper.cjs +3 -3
  29. package/lib/routes/control-plane.js +100 -6
  30. package/lib/routes/mobile-api.js +10 -2
  31. package/lib/shared/route-channels.cjs +1 -0
  32. package/lib/shared/route-order.cjs +8 -1
  33. package/lib/tailcat/host-process.cjs +9 -3
  34. package/package.json +14 -13
@@ -0,0 +1,159 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * `ask_user_question` 的问答派生(历史与直播共用这一份)。
5
+ *
6
+ * 桌面那边由 `dsh-client-ui-user-questions` 的 `answeredQuestionDefinition` 把「问 + 答」
7
+ * 折成一条右对齐的用户气泡(`QuestionReplyView`):每道题在上、用户给的答案在下,跳过显示
8
+ * 「已跳过」。手机此前只把提问卡收掉,对话记录里看不到用户选了什么 —— 这里补上同一件事。
9
+ *
10
+ * 只认 `ask_user_question` 的调用与其结果**成对**出现:
11
+ * - 计划评审(`intent.kind === 'plan-review'`)不是普通选择题,走它自己的接管卡,这里不出气泡;
12
+ * - pending(限时提问还没答)/ 失败 / 题数对不上 / 畸形的结果都不算,交回通用工具行;
13
+ * - 题里任一题带 plan-review、或没有题,整批都不出(与桌面 `callQuestions` 同口径)。
14
+ *
15
+ * 状态按 callId 记:调用先到、结果后到时才能把题目和答案配起来。历史与直播各持一份,
16
+ * 因为两边的游标互不相干(见 `history-derive.js` 与 `stream.js`)。
17
+ */
18
+
19
+ /** 计划评审的 intent.kind,出现在任一题里就不当普通提问。 */
20
+ const PLAN_REVIEW = 'plan-review';
21
+
22
+ function isRecord(value) {
23
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
24
+ }
25
+
26
+ function parseJsonRecord(text) {
27
+ try {
28
+ const value = JSON.parse(text);
29
+ return isRecord(value) ? value : null;
30
+ } catch {
31
+ return null;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * 从调用参数解析题目;解析失败 / 无题 / 有题带 plan-review intent 时返回 null。
37
+ *
38
+ * `arguments` 既可能是 JSON 原文,也可能是已解析对象(PTC / 合成事件两种形状都见过)。
39
+ */
40
+ function questionsOf(args) {
41
+ const value = typeof args === 'string' ? parseJsonRecord(args) : (isRecord(args) ? args : null);
42
+ if (!value || !Array.isArray(value.questions) || value.questions.length === 0) return null;
43
+ const ids = new Set();
44
+ const questions = [];
45
+ for (const question of value.questions) {
46
+ if (!isRecord(question) || typeof question.id !== 'string' || typeof question.question !== 'string') return null;
47
+ if (ids.has(question.id)) return null;
48
+ if (isRecord(question.intent) && question.intent.kind === PLAN_REVIEW) return null;
49
+ ids.add(question.id);
50
+ questions.push({
51
+ id: question.id,
52
+ question: question.question,
53
+ ...(typeof question.header === 'string' && question.header ? { header: question.header } : {}),
54
+ });
55
+ }
56
+ return questions;
57
+ }
58
+
59
+ /**
60
+ * 从工具结果解析答案批次;pending / 失败 / 题数对不上 / 畸形返回 null。
61
+ *
62
+ * 结果正文是宿主塞给模型的那段 JSON:`{"answers":[{"id","selected","custom"?}]}`。
63
+ */
64
+ function answersOf(resultMessage, questions) {
65
+ if (!isRecord(resultMessage) || resultMessage.isError === true) return null;
66
+ const content = resultMessage.content;
67
+ if (!Array.isArray(content) || content.length !== 1) return null;
68
+ const block = content[0];
69
+ if (!isRecord(block) || block.type !== 'text' || typeof block.text !== 'string') return null;
70
+ const value = parseJsonRecord(block.text);
71
+ if (!value || value.pending === true || !Array.isArray(value.answers)) return null;
72
+ if (value.answers.length !== questions.length) return null;
73
+ const ids = new Set(questions.map((question) => question.id));
74
+ const answers = [];
75
+ for (const answer of value.answers) {
76
+ if (!isRecord(answer) || typeof answer.id !== 'string' || !ids.delete(answer.id)) return null;
77
+ if (!Array.isArray(answer.selected) || !answer.selected.every((label) => typeof label === 'string')) return null;
78
+ if (answer.custom !== undefined && typeof answer.custom !== 'string') return null;
79
+ answers.push({
80
+ id: answer.id,
81
+ selected: [...answer.selected],
82
+ ...(answer.custom === undefined ? {} : { custom: answer.custom }),
83
+ });
84
+ }
85
+ return answers;
86
+ }
87
+
88
+ /** 从工具结果事件取 callId(两种宿主形状:`message.source.callId` / `message.toolCallId`)。 */
89
+ function callIdOfResult(data) {
90
+ const message = data && data.message;
91
+ if (!isRecord(message)) return '';
92
+ const source = message.source;
93
+ if (isRecord(source) && source.callId != null) return String(source.callId);
94
+ return message.toolCallId != null ? String(message.toolCallId) : '';
95
+ }
96
+
97
+ /**
98
+ * 建一份问答累积器。`note(event)` 喂原始会话事件:
99
+ * - `tool/call` / `assistant/message` 里的 `ask_user_question` 调用 → 记下题目,返回 null;
100
+ * - 对应的 `tool/result` 且答案合法 → 吐出结构化行(`kind:'question-reply'`),否则 null。
101
+ *
102
+ * 返回的行形状对齐 App 的 `ChatMessage`(`text` 空串、内容全在 `questionReply` 上)。
103
+ */
104
+ function createQuestionReplyTracker() {
105
+ /** callId → 题目数组。 */
106
+ const questionsByCall = new Map();
107
+
108
+ const rememberCall = (callId, args) => {
109
+ if (!callId) return;
110
+ const questions = questionsOf(args);
111
+ if (questions) questionsByCall.set(callId, questions);
112
+ };
113
+
114
+ function note(event) {
115
+ if (!event || typeof event.type !== 'string') return null;
116
+
117
+ if (event.type === 'tool/call') {
118
+ const data = event.data;
119
+ if (!isRecord(data) || data.name !== 'ask_user_question') return null;
120
+ rememberCall(data.callId != null ? String(data.callId) : '', data.arguments);
121
+ return null;
122
+ }
123
+
124
+ if (event.type === 'assistant/message') {
125
+ const message = event.data && event.data.message;
126
+ if (!isRecord(message) || !Array.isArray(message.content)) return null;
127
+ for (const block of message.content) {
128
+ if (!isRecord(block) || block.type !== 'tool-call' || block.name !== 'ask_user_question') continue;
129
+ rememberCall(block.id != null ? String(block.id) : '', block.arguments);
130
+ }
131
+ return null;
132
+ }
133
+
134
+ if (event.type !== 'tool/result') return null;
135
+ const data = event.data;
136
+ const callId = callIdOfResult(data);
137
+ const questions = callId ? questionsByCall.get(callId) : undefined;
138
+ if (!questions) return null;
139
+ const answers = answersOf(data.message, questions);
140
+ if (!answers) return null;
141
+ questionsByCall.delete(callId);
142
+ return {
143
+ id: `question-reply-${callId}`,
144
+ kind: 'question-reply',
145
+ text: '',
146
+ questionReply: { callId, questions, answers },
147
+ seq: Number.isSafeInteger(event.seq) ? event.seq : 0,
148
+ time: typeof event.time === 'number' ? event.time : 0,
149
+ };
150
+ }
151
+
152
+ return { note };
153
+ }
154
+
155
+ module.exports = {
156
+ createQuestionReplyTracker,
157
+ questionsOf,
158
+ answersOf,
159
+ };
@@ -43,6 +43,7 @@ const { callSessions } = require('./rpc.js');
43
43
  const { unwrap } = require('./envelope.js');
44
44
  const { normalizeJobs } = require('./jobs.js');
45
45
  const { cutEvent, createToolNameIndex } = require('./wire-cut.js');
46
+ const { createQuestionReplyTracker } = require('./question-reply.js');
46
47
  const { createPresentedStats } = require('./presented-file-stats.js');
47
48
  const { noteLiveEvent, drainLiveSteps, liveStepsOf, liveStepsTurnOf, liveTurnFactsOf, drainLiveTurnFacts, seedLiveTurnFacts, claimLiveWriter, releaseLiveWriter, attachLiveSteps, broadcastLiveSteps } = require('./live-steps.js');
48
49
  const { readTurnEvents } = require('./session-changes.js');
@@ -519,6 +520,9 @@ async function handleStream(ctx, req, res) {
519
520
  const imageBudget = createTextImageBudget();
520
521
  // 过程明细的减法(wire-cut.js)与它要的工具名索引:整条流共用一份,回放与实时帧都往里吸。
521
522
  const toolNames = createToolNameIndex();
523
+ // 问答派生:`ask_user_question` 的调用与结果在减法之前先过一遍,折出「问 + 答」行推给手机
524
+ // (见 question-reply.js)。回放与实时帧共用同一份,断线续传也能把这一行补回来。
525
+ const questionReplies = createQuestionReplyTracker();
522
526
  /**
523
527
  * 跑着那一轮的过程清单一发(`session/live-steps`)。
524
528
  *
@@ -564,7 +568,7 @@ async function handleStream(ctx, req, res) {
564
568
 
565
569
  // 断线续传:回放 history 中 seq > resumeSeq 的事件(收敛状态)。
566
570
  if (resumeSeq != null && resumeSeq > 0) {
567
- const replay = await replaySince(ctx, sessionId, resumeSeq, res, ac.signal, (seq) => { lastDeliveredSeq = Math.max(lastDeliveredSeq, seq); }, streamAddress, sealFrame, imageBudget, toolNames, presentedStats, liveWriterId);
571
+ const replay = await replaySince(ctx, sessionId, resumeSeq, res, ac.signal, (seq) => { lastDeliveredSeq = Math.max(lastDeliveredSeq, seq); }, streamAddress, sealFrame, imageBudget, toolNames, presentedStats, liveWriterId, questionReplies);
568
572
  if (replay.gap && !ac.signal.aborted) {
569
573
  const value = { type: 'session/resync-required', sessionId, resumeSeq, oldestSeq: replay.oldestSeq };
570
574
  // 旧实现在这里直接 `return`:既不清 keepAlive 也不 end,漏出一条永不收尾的连接。
@@ -644,6 +648,15 @@ async function handleStream(ctx, req, res) {
644
648
  // 过程副本:推理正文与工具参数不上线路,但用户点开「正在读取文件」那一步时要
645
649
  // 就地答得出来(落盘之前宿主的历史里还没有这一份,见 live-steps.js)。
646
650
  noteLiveEvent(ctx, sessionId, event, liveWriterId);
651
+ // 问答派生要在 `cutEvent` 之前:工具参数与结果正文都不上线路,只能在电脑侧折成
652
+ // 一条 `question-reply` 行推给手机(见 question-reply.js)。
653
+ {
654
+ const replyItem = questionReplies.note(event);
655
+ if (replyItem) {
656
+ const value = { type: 'session/question-reply', sessionId, item: replyItem };
657
+ if (await pushOrdered(value, null) === 'closed') { clientGone = true; break; }
658
+ }
659
+ }
647
660
  const facts = event.type === 'turn/start' ? null : drainLiveTurnFacts(ctx, sessionId, liveWriterId);
648
661
  if (facts) {
649
662
  const value = { type: 'session/turn-facts', sessionId, ...facts };
@@ -780,7 +793,7 @@ async function handleStream(ctx, req, res) {
780
793
  * 从会话 history 回放 seq > sinceSeq 的事件(升序),逐个经附件解析后写 SSE。
781
794
  * 通过 onDelivered(seq) 回报已交付的最大 seq,供上层去重。
782
795
  */
783
- async function replaySince(ctx, sessionId, sinceSeq, res, signal, onDelivered, address, sealFrame = null, imageBudget = null, toolNames = null, presentedStats = null, liveWriterId) {
796
+ async function replaySince(ctx, sessionId, sinceSeq, res, signal, onDelivered, address, sealFrame = null, imageBudget = null, toolNames = null, presentedStats = null, liveWriterId, questionReplies = null) {
784
797
  try {
785
798
  // 子会话的重放也要带父会话(同 historyAddress 的口径),否则宿主回 agent-busy,
786
799
  // 这条断线续传静默退化成「后面缺一段」。
@@ -828,6 +841,8 @@ async function replaySince(ctx, sessionId, sinceSeq, res, signal, onDelivered, a
828
841
  // 漏掉的事件重走一遍,顺手折回去最省事(turn/start 的重置也照直播的口径生效)。
829
842
  if (typeof e.type === 'string' && e.type.startsWith('tool-workflow/')) noteWorkflowEvent(ctx, sessionId, e);
830
843
  const resolved = await resolveEventAttachments(ctx, sessionId, e, { referencesOnly: true });
844
+ // 问答派生要在减之前喂原始事件(参数与结果正文不上线路,见 question-reply.js)。
845
+ const replyItem = questionReplies ? questionReplies.note(resolved) : null;
831
846
  let event = cutEvent(resolved, toolNames);
832
847
  // 回放与直播同一副眼镜:整条不上线路的事件这里也跳过(见 wire-cut.js)。
833
848
  if (!event) continue;
@@ -844,6 +859,14 @@ async function replaySince(ctx, sessionId, sinceSeq, res, signal, onDelivered, a
844
859
  // 回放是「一次性灌几千条」,最容易把写队列撑爆:写不动就在这里等,别一口气塞完。
845
860
  if (sent.needDrain && !(await waitForDrain(res, signal))) break;
846
861
  if (Number.isFinite(event.seq)) onDelivered(event.seq);
862
+ // 这一事件折出的问答行单独补一帧:手机端把它并进消息列表(同 id 幂等去重)。
863
+ if (replyItem) {
864
+ const value = { type: 'session/question-reply', sessionId, item: replyItem };
865
+ const replySent = writeSse(res, value, { payload: value }, { burst: true, sealFrame });
866
+ if (!replySent.ok) break;
867
+ written += (replySent.bytes ?? 0);
868
+ if (replySent.needDrain && !(await waitForDrain(res, signal))) break;
869
+ }
847
870
  // 字节到顶也一样:剩下的交给分页历史,别再灌。**但必须把缺口报出去** —— 只发前缀
848
871
  // 而不说话,客户端会以为这一段就是全部,接着实时流往下走(它只按 seq 去重、不查跳跃),
849
872
  // 缺掉的那一截永远不会被补。与 `gap` 同一个出口,上层据此发 resync。