dsh-mobile-plugin 0.5.47 → 0.5.49
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.
- package/lib/client.js +80 -5
- package/lib/impl.js +6 -1
- package/lib/mobile/answer-sources.js +61 -25
- package/lib/mobile/approval-memory.js +55 -2
- package/lib/mobile/approvals.js +38 -37
- package/lib/mobile/auto-review.js +18 -12
- package/lib/mobile/file-ops.js +33 -10
- package/lib/mobile/file-read.js +27 -6
- package/lib/mobile/history-derive.js +55 -56
- package/lib/mobile/live-steps.js +83 -5
- package/lib/mobile/paired-devices.js +183 -0
- package/lib/mobile/permissions.js +12 -1
- package/lib/mobile/presented-file-stats.js +168 -11
- package/lib/mobile/relay-control.js +14 -4
- package/lib/mobile/request-model.cjs +15 -0
- package/lib/mobile/session-artifacts.js +53 -12
- package/lib/mobile/session-changes.js +90 -0
- package/lib/mobile/session-file.js +24 -4
- package/lib/mobile/session-id.js +16 -0
- package/lib/mobile/session-snapshot.js +3 -2
- package/lib/mobile/session-write-body.js +99 -0
- package/lib/mobile/stats-collect.cjs +2 -3
- package/lib/mobile/stream.js +44 -6
- package/lib/mobile/turn-facts.js +256 -0
- package/lib/mobile/uploads.js +42 -28
- package/lib/mobile/wire-cut.js +2 -0
- package/lib/panel/index.html +9 -9
- package/lib/relay/lan-proxy.js +25 -2
- package/lib/routes/control-plane.js +54 -16
- package/lib/routes/mobile-api.js +37 -2
- package/lib/shared/http-util.js +44 -11
- package/lib/shell/device-ota.js +7 -2
- package/lib/update-install.cjs +58 -11
- package/lib/update-registry.cjs +22 -2
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -596,15 +596,51 @@ window.__ModuleLoader__.load({
|
|
|
596
596
|
*
|
|
597
597
|
* 内容由调用方按这一轮的现场拼好交进来;这里只管画与收,不做任何请求。
|
|
598
598
|
*/
|
|
599
|
+
/**
|
|
600
|
+
* 嵌在窗里的那一页对父窗口说的两句话:我还能不能继续轮询。
|
|
601
|
+
*
|
|
602
|
+
* **关窗必须让它真的停下来**:这一窗改成「收起不销毁」之后(见 `createDiagnosisDialog`
|
|
603
|
+
* 的 `keep`),那一页仍然挂在 DOM 上,页内那几个 3 秒轮询不会自己停 —— 用户说的
|
|
604
|
+
* 「关掉窗口就恢复正常」正是靠这一条维持的。iframe 跨源还好、跨源也被挡,所以这里按
|
|
605
|
+
* 同源 `postMessage` 通知,页面那边听不到就照常按 `document.hidden` 判。
|
|
606
|
+
*/
|
|
607
|
+
const PANEL_VISIBILITY = 'dsh-mobile-plugin:panel-visibility';
|
|
608
|
+
|
|
609
|
+
/** 通知嵌着的那一页:现在看得见 / 看不见。拿不到窗口就静默跳过。 */
|
|
610
|
+
function tellFrame(overlay, visible) {
|
|
611
|
+
try {
|
|
612
|
+
const frame = overlay && typeof overlay.querySelector === 'function'
|
|
613
|
+
? overlay.querySelector('iframe')
|
|
614
|
+
: null;
|
|
615
|
+
const win = frame && frame.contentWindow;
|
|
616
|
+
if (!win || typeof win.postMessage !== 'function') return;
|
|
617
|
+
win.postMessage({ type: PANEL_VISIBILITY, visible: Boolean(visible) }, '*');
|
|
618
|
+
} catch {
|
|
619
|
+
// 跨源(理论上不该发生:这一窗嵌的就是同源面板页)—— 页面自己按 document.hidden 判。
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
599
623
|
function createDiagnosisDialog(doc) {
|
|
600
624
|
let overlay = null;
|
|
601
625
|
/** 底下那一句说明(复制结果就地改它,不重画整窗)。 */
|
|
602
626
|
let noteEl = null;
|
|
603
627
|
let escapeListener = null;
|
|
604
628
|
const hide = () => {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
629
|
+
// **`keep` 的窗只收起、不销毁**:那一页是一份 1.4MB 的单文件产物,销毁掉下次
|
|
630
|
+
// 打开就得整份重新下载 + 重新挂载,实测每次多花七百来毫秒 —— 而用户点一下手机图标
|
|
631
|
+
// 只是想看一眼。留着它、隐藏起来,再打开是热的(实测 866ms → 123ms)。
|
|
632
|
+
// 代价是那一页还占着内存、页内轮询也还在跑,所以下面必须通知它「看不见了」,
|
|
633
|
+
// 让它自己把轮询停掉 —— 否则「关掉窗口就恢复正常」这条就不成立了。
|
|
634
|
+
if (overlay && overlay.__dshmKeep) {
|
|
635
|
+
if (typeof overlay.style === 'object') overlay.style.display = 'none';
|
|
636
|
+
tellFrame(overlay, false);
|
|
637
|
+
// 说明那一行**不清**:下次复用这一窗时还要往上写一句「已复制」之类。
|
|
638
|
+
} else if (overlay) {
|
|
639
|
+
if (typeof overlay.remove === 'function' && overlay.parentElement) overlay.remove();
|
|
640
|
+
// 不保留的那种收起就是彻底不留:引用一并清掉,下次是全新一窗(Esc / 遮罩也重新挂)。
|
|
641
|
+
overlay = null;
|
|
642
|
+
}
|
|
643
|
+
if (!(overlay && overlay.__dshmKeep)) noteEl = null;
|
|
608
644
|
if (escapeListener && typeof doc.removeEventListener === 'function') {
|
|
609
645
|
doc.removeEventListener('keydown', escapeListener);
|
|
610
646
|
}
|
|
@@ -619,6 +655,23 @@ window.__ModuleLoader__.load({
|
|
|
619
655
|
const open = (model) => {
|
|
620
656
|
if (!doc || typeof doc.createElement !== 'function' || !doc.body) return null;
|
|
621
657
|
const spec = model && typeof model === 'object' ? model : {};
|
|
658
|
+
const frameSrc = spec.frame && spec.frame.src ? String(spec.frame.src) : '';
|
|
659
|
+
// **同一页再开一次,直接把上一次那一窗亮出来**(见 `hide` 里的说明):那一页还活着,
|
|
660
|
+
// 重建等于把整份产物重新解析、React 重新挂载一遍。地址变了(换了页)才重建。
|
|
661
|
+
if (overlay && overlay.__dshmKeep && overlay.__dshmSrc === frameSrc) {
|
|
662
|
+
if (typeof overlay.style === 'object') overlay.style.display = '';
|
|
663
|
+
if (spec.title) overlay.setAttribute('aria-label', spec.title);
|
|
664
|
+
tellFrame(overlay, true);
|
|
665
|
+
// 底下那句说明仍然按这一轮重置(`noteEl` 是上一轮那个节点,还挂着)。
|
|
666
|
+
if (noteEl) noteEl.textContent = spec.note || '';
|
|
667
|
+
if (typeof doc.addEventListener === 'function' && !escapeListener) {
|
|
668
|
+
escapeListener = (event) => {
|
|
669
|
+
if (event && (event.key === 'Escape' || event.key === 'Esc')) hide();
|
|
670
|
+
};
|
|
671
|
+
doc.addEventListener('keydown', escapeListener);
|
|
672
|
+
}
|
|
673
|
+
return overlay;
|
|
674
|
+
}
|
|
622
675
|
if (!overlay) {
|
|
623
676
|
overlay = make('div', 'dshm_overlay');
|
|
624
677
|
overlay.setAttribute(DIAG_MARK, '');
|
|
@@ -637,6 +690,10 @@ window.__ModuleLoader__.load({
|
|
|
637
690
|
}
|
|
638
691
|
}
|
|
639
692
|
if (spec.title) overlay.setAttribute('aria-label', spec.title);
|
|
693
|
+
// 「这一窗收起时不销毁」的记号(见 `hide`)。只有带整页 iframe 的那一窗需要它:
|
|
694
|
+
// 那一页重建一次要重新解析一整份产物,而诊断窗那几行读数是现拼的,重建几乎不要钱。
|
|
695
|
+
overlay.__dshmKeep = Boolean(spec.keep);
|
|
696
|
+
overlay.__dshmSrc = frameSrc;
|
|
640
697
|
// 每次重画:读数与按钮都是这一轮的现场,别留上一轮的字。
|
|
641
698
|
// **`children` 是 HTMLCollection,没有 `slice()`**(假元素是数组,测试覆盖不到);
|
|
642
699
|
// 先摊成数组再边删边走。
|
|
@@ -707,11 +764,26 @@ window.__ModuleLoader__.load({
|
|
|
707
764
|
noteEl = note;
|
|
708
765
|
return overlay;
|
|
709
766
|
};
|
|
767
|
+
/**
|
|
768
|
+
* 彻底销毁这一窗(连同嵌着的那一页)。
|
|
769
|
+
*
|
|
770
|
+
* `hide` 对带整页的窗是「收起、留着复用」,但**离开这一窗去浏览器**那一支不该留:
|
|
771
|
+
* 用户已经不在这里看了,把那一页和一整份产物继续占着内存没有道理。
|
|
772
|
+
*/
|
|
773
|
+
const dispose = () => {
|
|
774
|
+
if (overlay && typeof overlay.remove === 'function' && overlay.parentElement) overlay.remove();
|
|
775
|
+
overlay = null;
|
|
776
|
+
noteEl = null;
|
|
777
|
+
if (escapeListener && typeof doc.removeEventListener === 'function') {
|
|
778
|
+
doc.removeEventListener('keydown', escapeListener);
|
|
779
|
+
}
|
|
780
|
+
escapeListener = null;
|
|
781
|
+
};
|
|
710
782
|
/** 就地更新底下那一句说明(不重画整窗)。 */
|
|
711
783
|
const note = (text) => {
|
|
712
784
|
if (noteEl) noteEl.textContent = text === undefined ? '' : String(text);
|
|
713
785
|
};
|
|
714
|
-
return { open, hide, note };
|
|
786
|
+
return { open, hide, note, dispose };
|
|
715
787
|
}
|
|
716
788
|
|
|
717
789
|
/** 复制一段文字:先走剪贴板 API,不行退回 textarea + execCommand。 */
|
|
@@ -1307,13 +1379,16 @@ window.__ModuleLoader__.load({
|
|
|
1307
1379
|
const openBrowser = (page) => {
|
|
1308
1380
|
if (typeof window === 'undefined' || typeof window.open !== 'function') return;
|
|
1309
1381
|
const opened = window.open(page || consolePage, '_blank', 'noreferrer');
|
|
1310
|
-
|
|
1382
|
+
// 开出去了就彻底销毁:用户已经不在这一窗里看了,没必要继续留着一整页。
|
|
1383
|
+
if (opened) dialog.dispose();
|
|
1311
1384
|
else dialog.note(t('consolePopupBlocked'));
|
|
1312
1385
|
};
|
|
1313
1386
|
|
|
1314
1387
|
dialog.open({
|
|
1315
1388
|
title: t('consoleTitle'),
|
|
1316
1389
|
wide: true,
|
|
1390
|
+
// 收起时不销毁(见 `hide`):再点一次图标就是热的,省掉一次整页重载。
|
|
1391
|
+
keep: true,
|
|
1317
1392
|
frame: {
|
|
1318
1393
|
src: consolePage,
|
|
1319
1394
|
title: t('consoleTitle'),
|
package/lib/impl.js
CHANGED
|
@@ -13,7 +13,7 @@ import z from '@deepseek-ai/schemastery'
|
|
|
13
13
|
const require = createRequire(import.meta.url)
|
|
14
14
|
const core = require('./relay/core.js')
|
|
15
15
|
const pairingApproval = require('./relay/pairing-policy.js')
|
|
16
|
-
const { sendJson, sendHtml } = require('./shared/http-util.js')
|
|
16
|
+
const { sendJson, sendHtml, htmlEtag } = require('./shared/http-util.js')
|
|
17
17
|
// 控制面守卫(localOnly)与移动端守卫(guarded)都住在这里;两套守卫并排放,
|
|
18
18
|
// 「控制面多挡跨站与 method、移动端只挡 token」这个不对称一眼可见。
|
|
19
19
|
const { localOnly, trustedLocalUi, readJson, createGuarded } = require('./http/routing.js')
|
|
@@ -454,6 +454,7 @@ function apply(ctx) {
|
|
|
454
454
|
sendJson,
|
|
455
455
|
// 面板页(lib/panel/index.html)整页吐给浏览器用;与控制面其它响应一样按注入传,不在这里 require,路由表才保持「只依赖注入项」。
|
|
456
456
|
sendHtml,
|
|
457
|
+
htmlEtag,
|
|
457
458
|
state,
|
|
458
459
|
sidecarToken,
|
|
459
460
|
status,
|
|
@@ -555,6 +556,10 @@ function apply(ctx) {
|
|
|
555
556
|
hostMetadata,
|
|
556
557
|
// iOS 推送令牌上报(/push-token)的登记 / 删除出口;不传时路由只回 ok。
|
|
557
558
|
pushNotify,
|
|
559
|
+
// 「这台电脑上还连着哪些手机」的两条路由(见 mobile/paired-devices.js):凭据库真源 +
|
|
560
|
+
// 吊销时收掉被踢设备在途的 SSE。
|
|
561
|
+
credentials,
|
|
562
|
+
dropDeviceStreams: (deviceId) => streamRegistry.drop(deviceId),
|
|
558
563
|
// 取值器而非快照对象:snapshot 会被 applySidecarConfig 整体替换,闭包捕获对象会让
|
|
559
564
|
// 连接信息永远停在进程启动那一刻。
|
|
560
565
|
getSidecarSnapshot: sidecarLifecycle.getSnapshot,
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
* - `read` / `read_image`:读过的本地文件,路径指出「这个结论是从哪份材料来的」;
|
|
17
17
|
* - `web_search`:搜索本身也算材料来源,查询词是它唯一的指针。
|
|
18
18
|
* `grep` / `glob` 不挑:它们是在仓库里找,挑出来只是一串正则,读者拿它回不到任何一份材料。
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* 来源地址从工具原始参数里取,完整保留路径 / 网址 / 查询词;过程清单的 72 字摘要只用于
|
|
20
|
+
* 预览,不能拿来当来源地址,否则长路径会先被截短,连文件名与复制出的地址也一起丢失。
|
|
21
21
|
*
|
|
22
22
|
* 条数:这一轮读过的材料都给出来,手机上自己折叠成前几条、展开看全。插件侧只留一条极端兜底
|
|
23
23
|
* (`MAX_SOURCES`),防的是一轮读几百份材料时把回包撑爆,正常研究场景到不了。
|
|
@@ -26,8 +26,10 @@
|
|
|
26
26
|
const { sendJson } = require('../shared/http-util');
|
|
27
27
|
const { callSessions } = require('./rpc.js');
|
|
28
28
|
const { unwrap } = require('./envelope.js');
|
|
29
|
-
const {
|
|
30
|
-
const { liveStepsOf, liveStepsTurnOf } = require('./live-steps.js');
|
|
29
|
+
const { parseArgs } = require('./step-index.js');
|
|
30
|
+
const { liveStepOf, liveStepsOf, liveStepsTurnOf } = require('./live-steps.js');
|
|
31
|
+
const { readTurnEvents } = require('./session-changes.js');
|
|
32
|
+
const { resultCallId, successfulResult } = require('./turn-facts.js');
|
|
31
33
|
|
|
32
34
|
/** 来源条数的极端兜底:一轮读到的材料照单全出,这里只防回包被几百份材料撑爆。 */
|
|
33
35
|
const MAX_SOURCES = 60;
|
|
@@ -48,6 +50,21 @@ function kindOfTool(name) {
|
|
|
48
50
|
return '';
|
|
49
51
|
}
|
|
50
52
|
|
|
53
|
+
/** 来源指针取原始参数,不经过过程清单的摘要截断。 */
|
|
54
|
+
function placesOfTool(name, rawArgs) {
|
|
55
|
+
const args = parseArgs(rawArgs);
|
|
56
|
+
if (SEARCH_TOOLS.has(name) && Array.isArray(args.queries)) {
|
|
57
|
+
return args.queries.filter((value) => typeof value === 'string' && value.trim() !== '')
|
|
58
|
+
.map((value) => value.trim());
|
|
59
|
+
}
|
|
60
|
+
const keys = FILE_TOOLS.has(name) ? ['file_path', 'path']
|
|
61
|
+
: WEB_TOOLS.has(name) ? ['url'] : ['query', 'q'];
|
|
62
|
+
for (const key of keys) {
|
|
63
|
+
if (typeof args[key] === 'string' && args[key].trim() !== '') return [args[key].trim()];
|
|
64
|
+
}
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
|
|
51
68
|
/** 网址里的主机名(取不到就回空串,调用方退回整条网址)。 */
|
|
52
69
|
function hostOf(url) {
|
|
53
70
|
const match = /^[a-z][a-z0-9+.-]*:\/\/([^/?#]+)/i.exec(String(url || ''));
|
|
@@ -64,9 +81,8 @@ function baseOf(path) {
|
|
|
64
81
|
/**
|
|
65
82
|
* 从一轮的步骤里挑出来源。
|
|
66
83
|
*
|
|
67
|
-
* @param {ReadonlyArray<{key?: string, step?: object}>} entries
|
|
68
|
-
*
|
|
69
|
-
* `state`)。
|
|
84
|
+
* @param {ReadonlyArray<{key?: string, step?: object, argsRaw?: string|object}>} entries
|
|
85
|
+
* 过程清单附宿主内的原始参数;只提取指针,不把参数放进返回值。
|
|
70
86
|
* @returns {Array<{obsId:number, kind:string, place:string, title:string, step:number, mediaIds:number[], source:string}>}
|
|
71
87
|
* 宿主侧的来源形状(App 的 `HostAnswerSource`),至多 `MAX_SOURCES` 条,按读到先后。
|
|
72
88
|
*/
|
|
@@ -83,13 +99,13 @@ function answerSourcesOf(entries) {
|
|
|
83
99
|
const name = typeof step.name === 'string' ? step.name : '';
|
|
84
100
|
const kind = kindOfTool(name);
|
|
85
101
|
if (kind === '') continue;
|
|
86
|
-
const place
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
102
|
+
for (const place of placesOfTool(name, entry.argsRaw)) {
|
|
103
|
+
const title = kind === 'web' ? (hostOf(place) || place) : kind === 'file' ? (baseOf(place) || place) : place;
|
|
104
|
+
const hit = { kind, place, title, step: index + 1, source: name };
|
|
105
|
+
const seen = hits.findIndex((item) => item.kind === kind && item.place === place);
|
|
106
|
+
if (seen >= 0) hits.splice(seen, 1);
|
|
107
|
+
hits.push(hit);
|
|
108
|
+
}
|
|
93
109
|
}
|
|
94
110
|
// 冲到兜底上限时留最近读的那几条:越近越可能是这一轮结论的出处。
|
|
95
111
|
return hits.slice(-MAX_SOURCES).map((hit, index) => ({
|
|
@@ -107,11 +123,20 @@ function answerSourcesOf(entries) {
|
|
|
107
123
|
* 历史事件里的工具调用 → 每一轮的步骤数组。
|
|
108
124
|
*
|
|
109
125
|
* 只读 assistant 消息里的 tool-call 块(参数在这一层还在,与 `history-derive` 取「做过哪些
|
|
110
|
-
*
|
|
126
|
+
* 写」同一个位置);保留原始参数供来源提取,不把截短的摘要当地址。
|
|
111
127
|
*/
|
|
112
128
|
function stepsByTurnOf(events) {
|
|
113
129
|
const turns = [];
|
|
114
130
|
const byTurn = new Map();
|
|
131
|
+
const results = new Map();
|
|
132
|
+
for (const raw of events) {
|
|
133
|
+
const event = raw && raw.event ? raw.event : raw;
|
|
134
|
+
if (event?.type !== 'tool/result') continue;
|
|
135
|
+
const id = resultCallId(event);
|
|
136
|
+
if (!id) continue;
|
|
137
|
+
const interrupted = event.data?.error?.code === 'interrupted';
|
|
138
|
+
results.set(id, successfulResult(event) ? 'ok' : interrupted ? 'stopped' : 'error');
|
|
139
|
+
}
|
|
115
140
|
const push = (turn, entry) => {
|
|
116
141
|
if (!byTurn.has(turn)) {
|
|
117
142
|
byTurn.set(turn, []);
|
|
@@ -124,6 +149,11 @@ function stepsByTurnOf(events) {
|
|
|
124
149
|
if (!event || event.type !== 'assistant/message') continue;
|
|
125
150
|
const data = event.data && typeof event.data === 'object' ? event.data : {};
|
|
126
151
|
const turn = typeof data.turn === 'number' ? data.turn : null;
|
|
152
|
+
// 已知轮的纯文字答复也证明空来源;没在窗口里的旧轮仍返回 null。
|
|
153
|
+
if (turn !== null && !byTurn.has(turn)) {
|
|
154
|
+
byTurn.set(turn, []);
|
|
155
|
+
turns.push(turn);
|
|
156
|
+
}
|
|
127
157
|
const message = data.message;
|
|
128
158
|
const content = message && Array.isArray(message.content) ? message.content : [];
|
|
129
159
|
for (const block of content) {
|
|
@@ -131,11 +161,11 @@ function stepsByTurnOf(events) {
|
|
|
131
161
|
const callId = block.id != null ? String(block.id) : `${turn}:${byTurn.get(turn) ? byTurn.get(turn).length : 0}`;
|
|
132
162
|
push(turn, {
|
|
133
163
|
key: `tool:${callId}`,
|
|
164
|
+
argsRaw: block.arguments,
|
|
134
165
|
step: {
|
|
135
166
|
kind: 'tool',
|
|
136
167
|
name: block.name,
|
|
137
|
-
|
|
138
|
-
state: 'ok',
|
|
168
|
+
state: results.get(callId) || 'ok',
|
|
139
169
|
},
|
|
140
170
|
});
|
|
141
171
|
}
|
|
@@ -190,19 +220,25 @@ async function handleAnswerSources(ctx, req, res) {
|
|
|
190
220
|
// 直播副本优先:跑着的那一轮(以及刚收口、还没重新拉历史的那一轮)只有这里有。
|
|
191
221
|
const liveTurn = liveStepsTurnOf(ctx, sessionId);
|
|
192
222
|
const liveSteps = liveStepsOf(ctx, sessionId);
|
|
193
|
-
if (liveSteps.length > 0 && (wantedTurn === null || liveTurn ===
|
|
194
|
-
|
|
223
|
+
if (!hasSubagentAddress && liveSteps.length > 0 && (wantedTurn === null || liveTurn === wantedTurn)) {
|
|
224
|
+
const entries = liveSteps.map((entry) => {
|
|
225
|
+
if (kindOfTool(entry.step.name) === '') return entry;
|
|
226
|
+
const detail = liveStepOf(ctx, sessionId, entry.step);
|
|
227
|
+
return { ...entry, argsRaw: detail && detail.argsRaw };
|
|
228
|
+
});
|
|
229
|
+
return sendJson(res, 200, { ok: true, turn: liveTurn, sources: answerSourcesOf(entries) });
|
|
195
230
|
}
|
|
196
231
|
|
|
197
232
|
const address = hasSubagentAddress ? { parentSessionId, mode } : {};
|
|
198
233
|
try {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
...address,
|
|
234
|
+
// 点名旧轮时沿现有游标定向翻到轮首;只读尾窗会让屏幕上已经能看的旧回答永远没有来源。
|
|
235
|
+
const requested = wantedTurn !== null ? await readTurnEvents(ctx, sessionId, wantedTurn, address, new AbortController().signal) : null;
|
|
236
|
+
const history = requested ? null : unwrap(await callSessions(ctx, 'history', {
|
|
237
|
+
sessionId, maxMessages: HISTORY_MAX_MESSAGES, ...address,
|
|
203
238
|
}));
|
|
204
|
-
if (error) throw error;
|
|
205
|
-
const events = value && Array.isArray(value.events) ? value.events : [];
|
|
239
|
+
if (history && history.error) throw history.error;
|
|
240
|
+
const events = requested ? requested.events : history?.value && Array.isArray(history.value.events) ? history.value.events : [];
|
|
241
|
+
if (requested?.truncated) return sendJson(res, 200, { ok: true, turn: null, sources: [] });
|
|
206
242
|
const { turns, byTurn } = stepsByTurnOf(events);
|
|
207
243
|
// 没有明确的轮号就取最近一轮(历史事件按 seq 递增,最后出现的轮号就是最近的)。
|
|
208
244
|
const turn = wantedTurn !== null && byTurn.has(wantedTurn)
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
* 一节才有得对账(理由当时只在卡上闪一下,卡一结算就没了)。这份记录只给人看,不参与任何
|
|
12
12
|
* 判定,所以只留最近若干条、超了挤掉最老的。
|
|
13
13
|
*
|
|
14
|
+
* 第三段是**自动审阅档的判定流水**(`autoReview`):这一档会替人放行,每次判定都要留凭据。
|
|
15
|
+
* 这条记录**不写会话日志** —— 宿主读盘时认不出的事件类型会让整份会话打不开,账本才是它该在
|
|
16
|
+
* 的地方(见 auto-review.js 的 `record`)。
|
|
17
|
+
*
|
|
14
18
|
* 落盘位置 `<DSH_HOME>/storages/mobile-plugin/approval-rules.json`,照移动端那几份账本的
|
|
15
19
|
* 同一套「原子写 + 读坏当空」的边界:账本读不动不能把审批拦在门外,写失败也只影响「重启后
|
|
16
20
|
* 还在不在」。`DSH_MOBILE_APPROVAL_RULES` 只给测试注入路径用。
|
|
@@ -28,6 +32,8 @@ const VERSION = 1;
|
|
|
28
32
|
const MAX_RULES = 500;
|
|
29
33
|
/** 拒绝记录上限:只是给人对账用的流水,留最近这些就够了。 */
|
|
30
34
|
const MAX_DECISIONS = 50;
|
|
35
|
+
/** 自动审阅判定上限:同上,只是流水。 */
|
|
36
|
+
const MAX_AUTO_REVIEW = 200;
|
|
31
37
|
|
|
32
38
|
function rulesFile() {
|
|
33
39
|
return process.env.DSH_MOBILE_APPROVAL_RULES
|
|
@@ -59,7 +65,25 @@ function normalizeRule(entry) {
|
|
|
59
65
|
}
|
|
60
66
|
|
|
61
67
|
function emptyStore() {
|
|
62
|
-
return { version: VERSION, updatedAt: 0, rules: [], decisions: [] };
|
|
68
|
+
return { version: VERSION, updatedAt: 0, rules: [], decisions: [], autoReview: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 自动审阅档的三种结论;其余取值当坏记录丢掉。 */
|
|
72
|
+
const AUTO_REVIEW_ACTIONS = new Set(['allow', 'reject', 'card']);
|
|
73
|
+
|
|
74
|
+
/** 一条自动审阅判定:形状不对的丢掉,读坏的文件不能把页面撑炸。 */
|
|
75
|
+
function normalizeAutoReview(entry) {
|
|
76
|
+
if (!isRecord(entry) || !AUTO_REVIEW_ACTIONS.has(entry.action)) return null;
|
|
77
|
+
return {
|
|
78
|
+
sessionId: textOf(entry.sessionId, 200) || null,
|
|
79
|
+
action: entry.action,
|
|
80
|
+
verdict: textOf(entry.verdict, 40) || null,
|
|
81
|
+
decidedBy: textOf(entry.decidedBy, 40) || null,
|
|
82
|
+
targetMode: textOf(entry.targetMode, 40) || null,
|
|
83
|
+
toolName: textOf(entry.toolName, 120) || null,
|
|
84
|
+
callId: textOf(entry.callId, 200) || null,
|
|
85
|
+
at: Number.isFinite(entry.at) ? entry.at : Date.now(),
|
|
86
|
+
};
|
|
63
87
|
}
|
|
64
88
|
|
|
65
89
|
/** 一条拒绝记录:形状不对的丢掉,读坏的文件不能把页面撑炸。 */
|
|
@@ -94,11 +118,17 @@ function readStore() {
|
|
|
94
118
|
const decision = normalizeDecision(entry);
|
|
95
119
|
if (decision) decisions.push(decision);
|
|
96
120
|
}
|
|
121
|
+
const autoReview = [];
|
|
122
|
+
for (const entry of Array.isArray(raw.autoReview) ? raw.autoReview : []) {
|
|
123
|
+
const decision = normalizeAutoReview(entry);
|
|
124
|
+
if (decision) autoReview.push(decision);
|
|
125
|
+
}
|
|
97
126
|
return {
|
|
98
127
|
version: VERSION,
|
|
99
128
|
updatedAt: Number.isFinite(raw.updatedAt) ? raw.updatedAt : 0,
|
|
100
129
|
rules,
|
|
101
130
|
decisions,
|
|
131
|
+
autoReview,
|
|
102
132
|
};
|
|
103
133
|
}
|
|
104
134
|
|
|
@@ -111,7 +141,9 @@ function saveStore(store) {
|
|
|
111
141
|
}
|
|
112
142
|
let decisions = (store.decisions || []).slice();
|
|
113
143
|
if (decisions.length > MAX_DECISIONS) decisions = decisions.slice(decisions.length - MAX_DECISIONS);
|
|
114
|
-
|
|
144
|
+
let autoReview = (store.autoReview || []).slice();
|
|
145
|
+
if (autoReview.length > MAX_AUTO_REVIEW) autoReview = autoReview.slice(autoReview.length - MAX_AUTO_REVIEW);
|
|
146
|
+
const next = { ...store, version: VERSION, updatedAt: Date.now(), rules, decisions, autoReview };
|
|
115
147
|
try {
|
|
116
148
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
117
149
|
const tmp = `${file}.${process.pid}.tmp`;
|
|
@@ -189,6 +221,25 @@ function recordDecision({ classKey, reason } = {}) {
|
|
|
189
221
|
return saveStore(store);
|
|
190
222
|
}
|
|
191
223
|
|
|
224
|
+
/**
|
|
225
|
+
* 记一条自动审阅判定。只给人和脚本对账用,不参与任何判定;没有合法 action 的不记,
|
|
226
|
+
* 落盘失败也只影响这一条留不留得下来。
|
|
227
|
+
*/
|
|
228
|
+
function recordAutoReview(entry = {}) {
|
|
229
|
+
const record = normalizeAutoReview({ ...entry, at: Date.now() });
|
|
230
|
+
if (!record) return false;
|
|
231
|
+
const store = readStore();
|
|
232
|
+
store.autoReview.push(record);
|
|
233
|
+
return saveStore(store);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** 最近的自动审阅判定(新的在前),给对账用。 */
|
|
237
|
+
function listAutoReview() {
|
|
238
|
+
return readStore().autoReview
|
|
239
|
+
.slice()
|
|
240
|
+
.sort((a, b) => b.at - a.at);
|
|
241
|
+
}
|
|
242
|
+
|
|
192
243
|
module.exports = {
|
|
193
244
|
listRules,
|
|
194
245
|
hasRule,
|
|
@@ -196,4 +247,6 @@ module.exports = {
|
|
|
196
247
|
removeRule,
|
|
197
248
|
listDecisions,
|
|
198
249
|
recordDecision,
|
|
250
|
+
listAutoReview,
|
|
251
|
+
recordAutoReview,
|
|
199
252
|
};
|
package/lib/mobile/approvals.js
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
* rpcId 找到挂起项把结论交回钩子。
|
|
11
11
|
*
|
|
12
12
|
* 钩子必须 prepend:宿主自己的转发器在桌面窗口开着时会一直等桌面回答、不调用 `next()`,
|
|
13
|
-
*
|
|
13
|
+
* 老实排在注册顺序后面等于「桌面开着时手机永远收不到」。手机在看时独占审批,最后一条
|
|
14
|
+
* 会话连接离开后将未裁决的请求交给桌面;桌面不可用时仍保留手机作答入口。
|
|
14
15
|
*
|
|
15
16
|
* 钩子上没有宿主 id,且与 `approval/asked` 的到达顺序不定,所以两个来源共用一张挂起项表:
|
|
16
17
|
* 先到的建项,后到的补字段。
|
|
@@ -32,6 +33,7 @@ const { randomUUID } = require('node:crypto');
|
|
|
32
33
|
const { sendJson, readJsonBody } = require('../shared/http-util');
|
|
33
34
|
const { cancelForwardedRequests } = require('./host-bridge/transport.js');
|
|
34
35
|
const autoReview = require('./auto-review.js');
|
|
36
|
+
const { sessionIdOf } = require('./session-id.js');
|
|
35
37
|
const facts = require('./approval-facts.js');
|
|
36
38
|
const approvalMemory = require('./approval-memory.js');
|
|
37
39
|
|
|
@@ -274,8 +276,11 @@ function attachSession(sessionId, sink) {
|
|
|
274
276
|
if (entry.bound) { try { sink(cardFrame(entry)); } catch { /* ignore */ } }
|
|
275
277
|
}
|
|
276
278
|
return () => {
|
|
277
|
-
set.delete(sink);
|
|
278
|
-
if (set.size === 0)
|
|
279
|
+
if (!set.delete(sink)) return;
|
|
280
|
+
if (set.size === 0) {
|
|
281
|
+
watchers.delete(sessionId);
|
|
282
|
+
for (const entry of openEntries(sessionId)) entry.forwardToDesktop?.();
|
|
283
|
+
}
|
|
279
284
|
};
|
|
280
285
|
}
|
|
281
286
|
|
|
@@ -343,14 +348,6 @@ function resetForTest() {
|
|
|
343
348
|
autoReviewConfig = null;
|
|
344
349
|
}
|
|
345
350
|
|
|
346
|
-
function sessionIdOf(session) {
|
|
347
|
-
if (!session || typeof session !== 'object') return null;
|
|
348
|
-
if (typeof session.id === 'string' && session.id) return session.id;
|
|
349
|
-
// 宿主自己的两处写法不一致(`dsh-api-session-controller` 用 `session.id`,
|
|
350
|
-
// `dsh-acp` 用 `session.header.id`);认不出会话 id 就等于这条审批没人能答,所以两种都认。
|
|
351
|
-
const header = session.header;
|
|
352
|
-
return header && typeof header.id === 'string' && header.id ? header.id : null;
|
|
353
|
-
}
|
|
354
351
|
|
|
355
352
|
function sessionIdOfRequest(request) {
|
|
356
353
|
const agent = request && request.agent;
|
|
@@ -721,36 +718,41 @@ function bindRequest(entry, request) {
|
|
|
721
718
|
}
|
|
722
719
|
|
|
723
720
|
/**
|
|
724
|
-
*
|
|
725
|
-
*
|
|
726
|
-
* 传了 `next` 就与下游竞速:`next()` 一调,桌面窗口那张卡照旧出现,谁先给结论谁算数,
|
|
727
|
-
* 输的那张卡由结算帧收掉;两边都没人答就一直挂着。
|
|
728
|
-
*
|
|
729
|
-
* 不传 `next`(手机正在看这个会话)就是手机独家作答:不往下游转发,桌面从源头就不弹卡。
|
|
730
|
-
* 独家作答后结论照常走 `approval/decided`,桌面看得到裁决结果。
|
|
731
|
-
*
|
|
732
|
-
* 唯一兜不住的边角是「审批发出时手机没在看、卡已转发到桌面,之后手机才连上来」:那一发
|
|
733
|
-
* 已经转发出去,手机后来答时靠 `cancelForwardedCards` 把它撤掉(见下)。
|
|
721
|
+
* 手机在看时先独占;最后一条连接离开后,将同一审批交给桌面竞速一次。
|
|
722
|
+
* 交接时桌面不可用只表示没人接手,仍等手机回来作答;不改变无人观看时的原有下游结论。
|
|
734
723
|
*/
|
|
735
724
|
function holdForPhone(entry, next) {
|
|
736
|
-
const
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
725
|
+
const handoff = isSessionWatched(entry.sessionId);
|
|
726
|
+
const downstream = new Promise((resolve, reject) => {
|
|
727
|
+
let forwarded = false;
|
|
728
|
+
entry.forwardToDesktop = () => {
|
|
729
|
+
if (forwarded || entry.settled) return;
|
|
730
|
+
forwarded = true;
|
|
731
|
+
Promise.resolve().then(() => {
|
|
732
|
+
if (!entry.settled) return next();
|
|
733
|
+
}).then((outcome) => {
|
|
734
|
+
if (entry.settled) return;
|
|
735
|
+
const value = DECISIONS.has(outcome) ? outcome : 'unavailable';
|
|
736
|
+
if (handoff && value === 'unavailable') return;
|
|
737
|
+
settle(entry, value);
|
|
738
|
+
resolve(value);
|
|
739
|
+
}, (error) => {
|
|
740
|
+
if (entry.settled || (handoff && error?.code === 'NO_PROVIDER')) return;
|
|
741
|
+
reject(error);
|
|
742
|
+
});
|
|
743
|
+
};
|
|
744
|
+
if (!handoff) entry.forwardToDesktop();
|
|
745
|
+
});
|
|
746
|
+
return Promise.race([entry.wait.then((result) => result.outcome), downstream])
|
|
747
|
+
.finally(() => { entry.forwardToDesktop = null; });
|
|
744
748
|
}
|
|
745
749
|
|
|
746
750
|
/**
|
|
747
751
|
* 收掉桌面那张审批卡:把宿主已经发给桌面窗口、还挂着的那一发转发撤掉。
|
|
748
752
|
*
|
|
749
|
-
*
|
|
750
|
-
*
|
|
751
|
-
*
|
|
752
|
-
* 的请求对象交给传输层,按它认回网关 `pendingRemoteEvents` 里那一发并撤回)。认不到(老
|
|
753
|
-
* 宿主没这道网关)就什么都不做 —— 「桌面没卡可收」本来就是对的。
|
|
753
|
+
* 手机在看时不转发;没有观众或最后一条连接离开后,桌面那一发会与手机作答竞速。手机先答
|
|
754
|
+
* 时按请求对象撤回桌面卡,与提问桥共用传输层的收尾口子。认不到(老宿主没这道网关)就
|
|
755
|
+
* 什么都不做 —— 「桌面没卡可收」本来就是对的。
|
|
754
756
|
*/
|
|
755
757
|
function cancelForwardedCards(ctx, entry) {
|
|
756
758
|
if (!entry.requestRef) return;
|
|
@@ -806,9 +808,8 @@ async function onApprovalRequest(ctx, request, next) {
|
|
|
806
808
|
reason: typeof request.reason === 'string' ? request.reason : undefined,
|
|
807
809
|
}, { unboundOnly: true });
|
|
808
810
|
bindRequest(entry, request);
|
|
809
|
-
//
|
|
810
|
-
|
|
811
|
-
return holdForPhone(entry, isSessionWatched(sessionId) ? undefined : next);
|
|
811
|
+
// 在场时手机先答;最后一条手机连接离开后由 attachSession 交接给桌面。
|
|
812
|
+
return holdForPhone(entry, next);
|
|
812
813
|
}
|
|
813
814
|
|
|
814
815
|
/**
|
|
@@ -30,6 +30,9 @@
|
|
|
30
30
|
* 不另设一份模型配置:换模型跟着会话走,不会和用户在手机上选的模型打架。
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
|
+
const approvalMemory = require('./approval-memory.js');
|
|
34
|
+
const { sessionIdOf } = require('./session-id.js');
|
|
35
|
+
|
|
33
36
|
/** 沙箱升档的两个目标(见 @deepseek-ai/dsh-sandbox 的 approveEscalation:审批只可能来自这两档)。 */
|
|
34
37
|
const TARGET_WORKSPACE = 'workspace-write';
|
|
35
38
|
const TARGET_DANGER = 'danger-full-access';
|
|
@@ -153,20 +156,23 @@ async function decide(ctx, request, session, watching) {
|
|
|
153
156
|
}
|
|
154
157
|
|
|
155
158
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
159
|
+
* 在插件自己的审批账本里留一条可查的判定记录。**旁路**:写不进也不能影响这次判定,异常一律吞掉。
|
|
160
|
+
* **不写会话日志**:宿主读盘时认不出的事件类型会让整份会话打不开(`ignorable` 只有宿主内部的
|
|
161
|
+
* plugin record 才带得上,外部插件没有这条通道),判定流水放 `approval-memory` 的 `autoReview`。
|
|
158
162
|
*/
|
|
159
163
|
function record(session, decision, request) {
|
|
160
|
-
if (!
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
164
|
+
if (!decision) return;
|
|
165
|
+
try {
|
|
166
|
+
approvalMemory.recordAutoReview({
|
|
167
|
+
sessionId: sessionIdOf(session),
|
|
168
|
+
action: decision.action,
|
|
169
|
+
verdict: decision.verdict,
|
|
170
|
+
decidedBy: decision.by,
|
|
171
|
+
targetMode: decision.targetMode || null,
|
|
172
|
+
toolName: request && typeof request.toolName === 'string' ? request.toolName : null,
|
|
173
|
+
callId: request && typeof request.callId === 'string' ? request.callId : null,
|
|
174
|
+
});
|
|
175
|
+
} catch { /* 记账是旁路,坏了不该改判定 */ }
|
|
170
176
|
}
|
|
171
177
|
|
|
172
178
|
module.exports = { targetModeOf, decide, record, TARGET_WORKSPACE, TARGET_DANGER };
|