dsh-mobile-plugin 0.5.34 → 0.5.36
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/impl.js +0 -11
- package/lib/mobile/answer-sources.js +223 -0
- package/lib/mobile/approval-facts.js +0 -13
- package/lib/mobile/approval-memory.js +1 -1
- package/lib/mobile/events.js +0 -20
- package/lib/mobile/extension-data.js +105 -5
- package/lib/mobile/history-derive.js +54 -1
- package/lib/mobile/notification-prefs.js +2 -2
- package/lib/mobile/permissions.js +13 -24
- package/lib/mobile/plugin-market.js +103 -4
- package/lib/mobile/run-timeline.js +317 -0
- package/lib/mobile/session-send.js +0 -1
- package/lib/mobile/session-stop-summary.js +122 -0
- package/lib/mobile/stats-ledger.cjs +45 -0
- package/lib/mobile/stop-summary.js +146 -0
- package/lib/mobile/third-party-extensions.js +86 -0
- package/lib/mobile/tool-result-cache.js +30 -2
- package/lib/panel/index.html +15 -15
- package/lib/relay/lan-proxy.js +20 -11
- package/lib/relay/tunnel.js +28 -12
- package/lib/routes/mobile-api.js +26 -58
- package/lib/shared/derived-cache.js +121 -0
- package/lib/shared/http-util.js +55 -18
- package/package.json +1 -3
- package/assets/pets/README.md +0 -9
- package/assets/pets/deepy-whale/attention.gif +0 -0
- package/assets/pets/deepy-whale/done.gif +0 -0
- package/assets/pets/deepy-whale/error.gif +0 -0
- package/assets/pets/deepy-whale/idle.gif +0 -0
- package/assets/pets/deepy-whale/pack.json +0 -54
- package/assets/pets/deepy-whale/sleeping.gif +0 -0
- package/assets/pets/deepy-whale/working-3.gif +0 -0
- package/assets/pets/deepy-whale/working.gif +0 -0
- package/config/agent-presets/supervisor/agent.cordis.yml +0 -237
- package/config/agent-presets/supervisor/preset.yml +0 -3
- package/knowledge/apps/alipay.md +0 -30
- package/knowledge/apps/android-settings.md +0 -30
- package/knowledge/apps/baijiahao.md +0 -30
- package/knowledge/apps/bilibili.md +0 -31
- package/knowledge/apps/boss-zhipin.md +0 -32
- package/knowledge/apps/dedao.md +0 -33
- package/knowledge/apps/douyin.md +0 -32
- package/knowledge/apps/feishu.md +0 -35
- package/knowledge/apps/gmail.md +0 -33
- package/knowledge/apps/jike.md +0 -31
- package/knowledge/apps/kuaishou.md +0 -34
- package/knowledge/apps/linkedin.md +0 -33
- package/knowledge/apps/qqmail.md +0 -30
- package/knowledge/apps/taobao.md +0 -33
- package/knowledge/apps/telegram.md +0 -27
- package/knowledge/apps/toutiao.md +0 -32
- package/knowledge/apps/uber-eats.md +0 -26
- package/knowledge/apps/uber.md +0 -27
- package/knowledge/apps/wechat.md +0 -33
- package/knowledge/apps/weibo.md +0 -31
- package/knowledge/apps/whatsapp.md +0 -27
- package/knowledge/apps/x.md +0 -32
- package/knowledge/apps/xiaohongshu.md +0 -30
- package/knowledge/apps/zhihu.md +0 -31
- package/knowledge/sites/bilibili.com/SITE.md +0 -50
- package/knowledge/sites/bilibili.com/references/view-fields.md +0 -17
- package/knowledge/sites/github.com/SITE.md +0 -51
- package/knowledge/sites/github.com/references/repo-fields.md +0 -13
- package/knowledge/sites/x.com/SITE.md +0 -47
- package/knowledge/sites/xiaohongshu.com/SITE.md +0 -48
- package/knowledge/sites/xiaohongshu.com/references/note-fields.md +0 -18
- package/knowledge/sites/youtube.com/SITE.md +0 -49
- package/knowledge/sites/youtube.com/references/innertube-fields.md +0 -14
- package/knowledge/sites/zhihu.com/SITE.md +0 -47
- package/knowledge/sites/zhihu.com/references/api-fields.md +0 -21
- package/lib/mobile/auto-review-log.js +0 -160
- package/lib/mobile/knowledge-base.js +0 -481
- package/lib/mobile/mail.js +0 -621
- package/lib/mobile/pet-tools.js +0 -261
- package/lib/mobile/pets.js +0 -755
- package/lib/mobile/phone/access.js +0 -140
- package/lib/mobile/phone/binding.js +0 -247
- package/lib/mobile/phone/confirm.js +0 -190
- package/lib/mobile/phone/evidence.js +0 -228
- package/lib/mobile/phone/gate.js +0 -290
- package/lib/mobile/phone/grounding.js +0 -314
- package/lib/mobile/phone/guard.js +0 -465
- package/lib/mobile/phone/guarded.js +0 -86
- package/lib/mobile/phone/index.js +0 -114
- package/lib/mobile/phone/irreversible.js +0 -226
- package/lib/mobile/phone/knowledge-tools.js +0 -665
- package/lib/mobile/phone/learn.js +0 -329
- package/lib/mobile/phone/pacing.js +0 -173
- package/lib/mobile/phone/redact.js +0 -237
- package/lib/mobile/phone/risk.js +0 -95
- package/lib/mobile/phone/route-extract.js +0 -460
- package/lib/mobile/phone/route-hint.js +0 -164
- package/lib/mobile/phone/routes-memory.js +0 -133
- package/lib/mobile/phone/routes.js +0 -287
- package/lib/mobile/phone/service.js +0 -520
- package/lib/mobile/phone/site-commands.js +0 -271
- package/lib/mobile/phone/skill-recall.js +0 -246
- package/lib/mobile/phone/skill-routes.js +0 -106
- package/lib/mobile/phone/step-summary.js +0 -201
- package/lib/mobile/phone/stop-summary.js +0 -235
- package/lib/mobile/phone/tool-groups.js +0 -125
- package/lib/mobile/phone/tools.js +0 -2590
- package/lib/mobile/phone/untrusted.js +0 -65
- package/lib/mobile/phone-knowledge.js +0 -317
- package/lib/mobile/route-store.js +0 -266
- package/lib/mobile/supervisor-broadcast.js +0 -503
- package/lib/mobile/supervisor-memory/index.js +0 -305
- package/lib/mobile/supervisor-runtime/index.js +0 -54
- package/lib/mobile/supervisor-runtime/inject.js +0 -218
- package/lib/mobile/supervisor-runtime/judgment.js +0 -113
- package/lib/mobile/supervisor-tools.js +0 -419
- package/lib/mobile/task-ledger.js +0 -381
package/lib/impl.js
CHANGED
|
@@ -47,7 +47,6 @@ const { installApprovalBridge, setPushNotifier: setApprovalPushNotifier, setAuto
|
|
|
47
47
|
const { installQuestionBridge, setPushNotifier: setQuestionPushNotifier } = require('./mobile/questions.js')
|
|
48
48
|
const { createPushNotifier } = require('./mobile/push-notify.js')
|
|
49
49
|
const { installNarration } = require('./mobile/narration.js') // 过程播报:默认关,开关真值每步现读
|
|
50
|
-
const { mountSupervisorRuntime } = require('./mobile/supervisor-runtime/index.js') // 总管运行期:有活/没活判断 + 每次开口带任务账。
|
|
51
50
|
// 「手机上最近来过没有」:守卫记、控制面状态读。单独一个模块,装配层只转交。
|
|
52
51
|
const activity = require('./mobile/activity.cjs')
|
|
53
52
|
const { installSessionCleanup } = require('./session-cleanup.cjs')
|
|
@@ -56,10 +55,6 @@ const { installSessionCleanup } = require('./session-cleanup.cjs')
|
|
|
56
55
|
const statsScheduler = require('./mobile/stats-scheduler.cjs')
|
|
57
56
|
// 扩展卡的宿主侧:`extension_present` 工具与卡状态账本(见 mobile/extensions.js)。
|
|
58
57
|
const { installExtensions } = require('./mobile/extensions.js')
|
|
59
|
-
const { mountPhoneControl } = require('./mobile/phone/index.js') // 「让 AI 操作手机」,见 mobile/phone/
|
|
60
|
-
const { installPetTools } = require('./mobile/pet-tools.js') // 悬浮宠物的两个工具(看 / 写),见 mobile/pet-tools.js。
|
|
61
|
-
const { mountSupervisorTools } = require('./mobile/supervisor-tools.js') // 总管那组读写工具(列会话 / 看进度 / 读正文 / 发消息 / 开新会话 / 中断)。
|
|
62
|
-
const { mount: mountSupervisorMemory } = require('./mobile/supervisor-memory/index.js') // 总管的两本账(任务账 + 知识库)。
|
|
63
58
|
const { activeGuard } = require('./host-guard.cjs')
|
|
64
59
|
// 桌面端侧栏展开态的快照存储。桌面把这份状态只记在浏览器 localStorage 里,宿主必须
|
|
65
60
|
// 有一个落盘处:客户端半边(lib/client.js)上报 → 这里 → 移动端读取。
|
|
@@ -401,13 +396,8 @@ function apply(ctx) {
|
|
|
401
396
|
}
|
|
402
397
|
return undefined
|
|
403
398
|
}, `${name}: extension tool`)
|
|
404
|
-
mount('宠物工具', () => installPetTools(ctx)) // 缺 tools 注入的 profile 只少这两个,包目录与路由照常可用。
|
|
405
|
-
mount('总管工具', () => mountSupervisorTools(ctx)) // 同宠物工具:缺 tools 注入只少这六条,其余照常。
|
|
406
|
-
mount('总管两本账', () => ctx.effect(() => mountSupervisorMemory(ctx).dispose, `${name}: supervisor memory`)) // 任务账 + 知识库:跟着会话事件自动记,实现都在 mobile/supervisor-memory/。
|
|
407
399
|
mount('统计采集', () => ctx.effect(() => statsScheduler.start(ctx), `${name}: stats collect`))
|
|
408
400
|
mount('过程播报', () => ctx.effect(() => installNarration(ctx), `${name}: process narration`))
|
|
409
|
-
mount('总管运行期', () => ctx.effect(() => mountSupervisorRuntime(ctx), `${name}: supervisor runtime`)) // 只在总管那条会话上注入「有活/没活 + 任务账」。
|
|
410
|
-
mount('总管播报', () => ctx.effect(() => require('./mobile/supervisor-broadcast.js').installDeviceBroadcast(ctx), `${name}: supervisor broadcast`)) // 攒批合并成一句,出口是设备事件流的 supervisor/broadcast 帧。
|
|
411
401
|
|
|
412
402
|
const status = createStatusView({
|
|
413
403
|
state,
|
|
@@ -548,7 +538,6 @@ function apply(ctx) {
|
|
|
548
538
|
// 一样必须包在 ctx.effect 里,否则插件重载时重复注册会直接抛错。
|
|
549
539
|
registerUpgradeRoute: (route) => mountRoute(route, true),
|
|
550
540
|
preview,
|
|
551
|
-
phoneService: mountPhoneControl(ctx, { authorized, credentials, instanceId: state.instanceId, name }),
|
|
552
541
|
// 网页版登录令牌那条:handler 在这里造好、按依赖交给路由表,理由与 preview 一样——
|
|
553
542
|
// 它要读的是本进程的 `ctx.webServer` 与 `ctx.get('connection')`。
|
|
554
543
|
hostWeb: hostWeb.handleHostWeb,
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 回答下面的「来源」:这一轮读过哪些网页 / 文件 / 查询词。
|
|
5
|
+
*
|
|
6
|
+
* **来源由代码挑,不由模型写**(照 Mika `AnswerSources` 的做法):模型自己列参考链接时,
|
|
7
|
+
* 写了什么就是什么,对不上的也没人核;而这一轮真正读过什么,事件流里本来就有 —— 挑的时候
|
|
8
|
+
* 只认工具调用,模型说了不算。
|
|
9
|
+
*
|
|
10
|
+
* **只给指针,不带正文**:挑出来的是「哪一页 / 哪一个文件 / 哪一个查询词」,网页正文、
|
|
11
|
+
* 文件内容、工具参数一概不进来。这与「推理正文、工具参数、结果正文不下发」(`wire-cut.js`)
|
|
12
|
+
* 那条边界同向:卡上那一行要的是「值不值得点开」,不是把材料再搬一遍。
|
|
13
|
+
*
|
|
14
|
+
* 信息量口径(为什么挑这三种工具,别的都不挑):
|
|
15
|
+
* - `web_fetch`:回答里引用的外部事实的来源,网址本身就是信息;
|
|
16
|
+
* - `read` / `read_image`:读过的本地文件,路径指出「这个结论是从哪份材料来的」;
|
|
17
|
+
* - `web_search`:搜索本身也算材料来源,查询词是它唯一的指针。
|
|
18
|
+
* `grep` / `glob` 不挑:它们是在仓库里找,挑出来只是一串正则,读者拿它回不到任何一份材料。
|
|
19
|
+
* 工具名与摘要一律走 `step-index.js` 的 `toolStepSummary`,不在这里另拼一套(同一行摘要在
|
|
20
|
+
* 过程清单里长什么样,来源卡里就长什么样)。
|
|
21
|
+
*
|
|
22
|
+
* 上限 6 条(旧证据账本 `MAX_SOURCES` 的口径):再多就不是「这次读过哪些」而是第二份过程清单。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { sendJson } = require('../shared/http-util');
|
|
26
|
+
const { callSessions } = require('./rpc.js');
|
|
27
|
+
const { unwrap } = require('./envelope.js');
|
|
28
|
+
const { toolStepSummary } = require('./step-index.js');
|
|
29
|
+
const { liveStepsOf, liveStepsTurnOf } = require('./live-steps.js');
|
|
30
|
+
|
|
31
|
+
/** 至多列这么多处来源(沿用旧账本的 `MAX_SOURCES`)。 */
|
|
32
|
+
const MAX_SOURCES = 6;
|
|
33
|
+
|
|
34
|
+
/** 回拉历史的条数上限:与 `/session/step` 同一档,够盖住一轮里的全部工具调用。 */
|
|
35
|
+
const HISTORY_MAX_MESSAGES = 200;
|
|
36
|
+
|
|
37
|
+
/** 挑哪些工具。 */
|
|
38
|
+
const WEB_TOOLS = new Set(['web_fetch']);
|
|
39
|
+
const SEARCH_TOOLS = new Set(['web_search']);
|
|
40
|
+
const FILE_TOOLS = new Set(['read', 'read_image']);
|
|
41
|
+
|
|
42
|
+
/** 这一类工具 → 来源卡上的 `kind`;认不出的返回空串。 */
|
|
43
|
+
function kindOfTool(name) {
|
|
44
|
+
if (WEB_TOOLS.has(name)) return 'web';
|
|
45
|
+
if (SEARCH_TOOLS.has(name)) return 'search';
|
|
46
|
+
if (FILE_TOOLS.has(name)) return 'file';
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 网址里的主机名(取不到就回空串,调用方退回整条网址)。 */
|
|
51
|
+
function hostOf(url) {
|
|
52
|
+
const match = /^[a-z][a-z0-9+.-]*:\/\/([^/?#]+)/i.exec(String(url || ''));
|
|
53
|
+
if (!match) return '';
|
|
54
|
+
return match[1].replace(/^\[|\]$/g, '').toLowerCase();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 路径的最后一段(`/a/b/c.md` → `c.md`),取不到回空串。 */
|
|
58
|
+
function baseOf(path) {
|
|
59
|
+
const parts = String(path || '').split(/[/\\]+/).filter(Boolean);
|
|
60
|
+
return parts.length > 0 ? parts[parts.length - 1] : '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 从一轮的步骤里挑出来源。
|
|
65
|
+
*
|
|
66
|
+
* @param {ReadonlyArray<{key?: string, step?: object}>} entries 过程清单(与 `liveStepsOf` /
|
|
67
|
+
* `turnProcess.steps` 同一个形状:`{ key, step }`,`step` 带 `kind` / `name` / `summary` /
|
|
68
|
+
* `state`)。
|
|
69
|
+
* @returns {Array<{obsId:number, kind:string, place:string, title:string, step:number, mediaIds:number[], source:string}>}
|
|
70
|
+
* 宿主侧的来源形状(App 的 `HostAnswerSource`),至多 `MAX_SOURCES` 条,按读到先后。
|
|
71
|
+
*/
|
|
72
|
+
function answerSourcesOf(entries) {
|
|
73
|
+
if (!Array.isArray(entries)) return [];
|
|
74
|
+
/** 按「种类 + 地点」去重,同一个文件读两次只占一条。 */
|
|
75
|
+
const hits = [];
|
|
76
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
77
|
+
const entry = entries[index];
|
|
78
|
+
const step = entry && typeof entry === 'object' ? entry.step : null;
|
|
79
|
+
if (!step || step.kind !== 'tool') continue;
|
|
80
|
+
// 失败的那一次不算「读过」:抓取报错 / 读文件被拒没留下材料。
|
|
81
|
+
if (step.state === 'error' || step.state === 'stopped') continue;
|
|
82
|
+
const name = typeof step.name === 'string' ? step.name : '';
|
|
83
|
+
const kind = kindOfTool(name);
|
|
84
|
+
if (kind === '') continue;
|
|
85
|
+
const place = typeof step.summary === 'string' ? step.summary.trim() : '';
|
|
86
|
+
if (place === '') continue;
|
|
87
|
+
const title = kind === 'web' ? (hostOf(place) || place) : kind === 'file' ? (baseOf(place) || place) : place;
|
|
88
|
+
const hit = { kind, place, title, step: index + 1, source: name };
|
|
89
|
+
const seen = hits.findIndex((item) => item.kind === kind && item.place === place);
|
|
90
|
+
if (seen >= 0) hits.splice(seen, 1);
|
|
91
|
+
hits.push(hit);
|
|
92
|
+
}
|
|
93
|
+
// 只留最近读的几条(旧账本 `hits.slice(-MAX_SOURCES)` 的口径)。
|
|
94
|
+
return hits.slice(-MAX_SOURCES).map((hit, index) => ({
|
|
95
|
+
obsId: index + 1,
|
|
96
|
+
kind: hit.kind,
|
|
97
|
+
place: hit.place,
|
|
98
|
+
title: hit.title,
|
|
99
|
+
step: hit.step,
|
|
100
|
+
mediaIds: [],
|
|
101
|
+
source: hit.source,
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 历史事件里的工具调用 → 每一轮的步骤数组。
|
|
107
|
+
*
|
|
108
|
+
* 只读 assistant 消息里的 tool-call 块(参数在这一层还在,与 `history-derive` 取「做过哪些
|
|
109
|
+
* 写」同一个位置);摘要走 `toolStepSummary`。
|
|
110
|
+
*/
|
|
111
|
+
function stepsByTurnOf(events) {
|
|
112
|
+
const turns = [];
|
|
113
|
+
const byTurn = new Map();
|
|
114
|
+
const push = (turn, entry) => {
|
|
115
|
+
if (!byTurn.has(turn)) {
|
|
116
|
+
byTurn.set(turn, []);
|
|
117
|
+
turns.push(turn);
|
|
118
|
+
}
|
|
119
|
+
byTurn.get(turn).push(entry);
|
|
120
|
+
};
|
|
121
|
+
for (const raw of events) {
|
|
122
|
+
const event = raw && raw.event ? raw.event : raw;
|
|
123
|
+
if (!event || event.type !== 'assistant/message') continue;
|
|
124
|
+
const data = event.data && typeof event.data === 'object' ? event.data : {};
|
|
125
|
+
const turn = typeof data.turn === 'number' ? data.turn : null;
|
|
126
|
+
const message = data.message;
|
|
127
|
+
const content = message && Array.isArray(message.content) ? message.content : [];
|
|
128
|
+
for (const block of content) {
|
|
129
|
+
if (!block || block.type !== 'tool-call' || typeof block.name !== 'string') continue;
|
|
130
|
+
const callId = block.id != null ? String(block.id) : `${turn}:${byTurn.get(turn) ? byTurn.get(turn).length : 0}`;
|
|
131
|
+
push(turn, {
|
|
132
|
+
key: `tool:${callId}`,
|
|
133
|
+
step: {
|
|
134
|
+
kind: 'tool',
|
|
135
|
+
name: block.name,
|
|
136
|
+
summary: toolStepSummary(block.name, block.arguments),
|
|
137
|
+
state: 'ok',
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return { turns, byTurn };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 宿主错误 → 状态码(与 `/session/step` 同一套判据)。 */
|
|
146
|
+
function historyStatus(error) {
|
|
147
|
+
switch (error && error.code) {
|
|
148
|
+
case 'bad-request': return 400;
|
|
149
|
+
case 'session-not-found':
|
|
150
|
+
case 'subagent-not-found': return 404;
|
|
151
|
+
case 'subagent-unauthorized': return 403;
|
|
152
|
+
case 'host-unavailable': return 503;
|
|
153
|
+
default: return 502;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function intParam(url, name) {
|
|
158
|
+
const raw = url.searchParams.get(name);
|
|
159
|
+
if (raw === null || raw === '') return null;
|
|
160
|
+
if (!/^\d+$/.test(raw)) return null;
|
|
161
|
+
const value = Number(raw);
|
|
162
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* `GET /api/mobile/v1/session/answer-sources?sessionId=…&turn=…`
|
|
167
|
+
*
|
|
168
|
+
* 不带 `turn` 就是「最近一轮」:先在直播副本里找(跑着的那一轮还没落盘,历史里没有),
|
|
169
|
+
* 没有再去问历史。带 `turn` 就只要那一轮的问法 —— 历史里一轮的工具调用都在。
|
|
170
|
+
*
|
|
171
|
+
* 空结果是正常结果(这一轮只说话没读东西):回 `{ok:true, turn, sources: []}`,
|
|
172
|
+
* 让 App 那边整块不画。
|
|
173
|
+
*/
|
|
174
|
+
async function handleAnswerSources(ctx, req, res) {
|
|
175
|
+
const url = new URL(req.url, 'http://x');
|
|
176
|
+
const sessionId = url.searchParams.get('sessionId');
|
|
177
|
+
if (!sessionId) return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '缺少 sessionId' } });
|
|
178
|
+
const parentSessionId = url.searchParams.get('parentSessionId');
|
|
179
|
+
const mode = url.searchParams.get('mode');
|
|
180
|
+
const hasSubagentAddress = parentSessionId !== null || mode !== null;
|
|
181
|
+
if (hasSubagentAddress && (
|
|
182
|
+
typeof parentSessionId !== 'string' || parentSessionId.trim() === ''
|
|
183
|
+
|| (mode !== 'one-shot' && mode !== 'continuable')
|
|
184
|
+
)) {
|
|
185
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '子代理会话需要 parentSessionId 和有效 mode' } });
|
|
186
|
+
}
|
|
187
|
+
const wantedTurn = intParam(url, 'turn');
|
|
188
|
+
|
|
189
|
+
// 直播副本优先:跑着的那一轮(以及刚收口、还没重新拉历史的那一轮)只有这里有。
|
|
190
|
+
const liveTurn = liveStepsTurnOf(ctx, sessionId);
|
|
191
|
+
const liveSteps = liveStepsOf(ctx, sessionId);
|
|
192
|
+
if (liveSteps.length > 0 && (wantedTurn === null || liveTurn === null || liveTurn === wantedTurn)) {
|
|
193
|
+
return sendJson(res, 200, { ok: true, turn: liveTurn, sources: answerSourcesOf(liveSteps) });
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const address = hasSubagentAddress ? { parentSessionId, mode } : {};
|
|
197
|
+
try {
|
|
198
|
+
const { value, error } = unwrap(await callSessions(ctx, 'history', {
|
|
199
|
+
sessionId,
|
|
200
|
+
maxMessages: HISTORY_MAX_MESSAGES,
|
|
201
|
+
...address,
|
|
202
|
+
}));
|
|
203
|
+
if (error) throw error;
|
|
204
|
+
const events = value && Array.isArray(value.events) ? value.events : [];
|
|
205
|
+
const { turns, byTurn } = stepsByTurnOf(events);
|
|
206
|
+
// 没有明确的轮号就取最近一轮(历史事件按 seq 递增,最后出现的轮号就是最近的)。
|
|
207
|
+
const turn = wantedTurn !== null && byTurn.has(wantedTurn)
|
|
208
|
+
? wantedTurn
|
|
209
|
+
: (wantedTurn !== null ? null : (turns.length > 0 ? turns[turns.length - 1] : null));
|
|
210
|
+
const entries = turn === null ? [] : byTurn.get(turn) || [];
|
|
211
|
+
return sendJson(res, 200, { ok: true, turn, sources: answerSourcesOf(entries) });
|
|
212
|
+
} catch (error) {
|
|
213
|
+
const code = error && typeof error.code === 'string' ? error.code : 'host-call-failed';
|
|
214
|
+
const message = error && typeof error.message === 'string' && error.message ? error.message : '取出这一轮的来源失败';
|
|
215
|
+
return sendJson(res, historyStatus({ code }), { ok: false, error: { code, message } });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = {
|
|
220
|
+
MAX_SOURCES,
|
|
221
|
+
answerSourcesOf,
|
|
222
|
+
handleAnswerSources,
|
|
223
|
+
};
|
|
@@ -282,17 +282,6 @@ function callToPiece(toolName, args) {
|
|
|
282
282
|
return { clause: short ? `在网上搜 "${short}"` : '在网上搜', highlight: false, classKey: 'tool:web_search' };
|
|
283
283
|
}
|
|
284
284
|
|
|
285
|
-
if (name === 'phone_open_app') {
|
|
286
|
-
const pkg = isRecord(args) && typeof args.packageName === 'string' ? args.packageName : '';
|
|
287
|
-
return { clause: pkg ? `打开 ${pkg}` : '打开应用', highlight: false, classKey: pkg ? `phone_open_app:${pkg}` : 'tool:phone_open_app' };
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (name === 'phone_open_link') {
|
|
291
|
-
const uri = isRecord(args) && typeof args.uri === 'string' ? args.uri : '';
|
|
292
|
-
const host = hostOf(uri);
|
|
293
|
-
return { clause: host ? `连 ${host}` : '打开链接', highlight: false, classKey: host ? `phone_open_link:${host}` : 'tool:phone_open_link' };
|
|
294
|
-
}
|
|
295
|
-
|
|
296
285
|
// 未知工具
|
|
297
286
|
return { clause: name ? `执行 ${name}` : '', highlight: false, classKey: name ? `tool:${name}` : 'tool:unknown' };
|
|
298
287
|
}
|
|
@@ -327,8 +316,6 @@ function labelOf(classKey) {
|
|
|
327
316
|
if (tool === 'write' || tool === 'edit') return `改文件 · ${parts[1] || '未知目录'}`;
|
|
328
317
|
if (tool === 'read') return `读文件 · ${parts[1] || '未知目录'}`;
|
|
329
318
|
if (tool === 'web_fetch') return `联网 · ${parts[1] || '网页'}`;
|
|
330
|
-
if (tool === 'phone_open_app') return `打开应用 · ${parts[1] || ''}`;
|
|
331
|
-
if (tool === 'phone_open_link') return `打开链接 · ${parts[1] || ''}`;
|
|
332
319
|
if (tool === 'tool') return `工具 · ${parts[1] || '未知'}`;
|
|
333
320
|
return tool ? String(classKey) : '';
|
|
334
321
|
}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* 一节才有得对账(理由当时只在卡上闪一下,卡一结算就没了)。这份记录只给人看,不参与任何
|
|
12
12
|
* 判定,所以只留最近若干条、超了挤掉最老的。
|
|
13
13
|
*
|
|
14
|
-
* 落盘位置 `<DSH_HOME>/storages/mobile-plugin/approval-rules.json
|
|
14
|
+
* 落盘位置 `<DSH_HOME>/storages/mobile-plugin/approval-rules.json`,照移动端那几份账本的
|
|
15
15
|
* 同一套「原子写 + 读坏当空」的边界:账本读不动不能把审批拦在门外,写失败也只影响「重启后
|
|
16
16
|
* 还在不在」。`DSH_MOBILE_APPROVAL_RULES` 只给测试注入路径用。
|
|
17
17
|
*/
|
package/lib/mobile/events.js
CHANGED
|
@@ -16,7 +16,6 @@ const { createFrameSealer } = require('./stream.js');
|
|
|
16
16
|
const { listSignal: sharedListSignal } = require('../shared/list-signal.js');
|
|
17
17
|
const { sessionSignal: sharedSessionSignal } = require('../shared/session-signal.js');
|
|
18
18
|
const { desktopViewSignal: sharedDesktopViewSignal } = require('../shared/desktop-view-signal.js');
|
|
19
|
-
const { broadcastSignal: sharedBroadcastSignal } = require('./supervisor-broadcast.js');
|
|
20
19
|
const { writeSse: sharedWriteSse, stableEventId: sharedEventId, waitForDrain } = require('../shared/sse-write.js');
|
|
21
20
|
|
|
22
21
|
const DEDUP_WINDOW = 2048;
|
|
@@ -225,24 +224,6 @@ async function handleDeviceStream(ctx, req, res) {
|
|
|
225
224
|
if (ac.signal.aborted) return;
|
|
226
225
|
if (!writeFrame({ type: 'device/desktop-view-changed', updatedAt: event.updatedAt, at: event.at })) ac.abort();
|
|
227
226
|
});
|
|
228
|
-
/**
|
|
229
|
-
* 自产的「总管要开口了」帧:插件把几条会话的动静合成一句话,手机在通话里念出来。
|
|
230
|
-
*
|
|
231
|
-
* 帧上 `text` 是留在屏幕上那份摘要、`speech` 才是嘴上念的稿子 —— 手机拿到什么念什么,
|
|
232
|
-
* 不再自己洗一遍(口径只留插件这一份)。
|
|
233
|
-
*
|
|
234
|
-
* 与心跳、清单信号一样是直订 + 收尾注销。**不补发**:说话的时机是「此刻」,断流期间
|
|
235
|
-
* 漏掉的那一句没有补的价值(补回去只会让它在一分钟后突然开口)。
|
|
236
|
-
*/
|
|
237
|
-
const unsubscribeBroadcast = sharedBroadcastSignal.onBroadcast((frame) => {
|
|
238
|
-
if (ac.signal.aborted) return;
|
|
239
|
-
if (!writeFrame({
|
|
240
|
-
type: 'supervisor/broadcast',
|
|
241
|
-
text: frame.text,
|
|
242
|
-
speech: frame.speech,
|
|
243
|
-
at: frame.at,
|
|
244
|
-
})) ac.abort();
|
|
245
|
-
});
|
|
246
227
|
/**
|
|
247
228
|
* 连上就先补一帧「当前状态」。
|
|
248
229
|
*
|
|
@@ -335,7 +316,6 @@ async function handleDeviceStream(ctx, req, res) {
|
|
|
335
316
|
unsubscribeSignal();
|
|
336
317
|
unsubscribeActivity();
|
|
337
318
|
unsubscribeDesktopView();
|
|
338
|
-
unsubscribeBroadcast();
|
|
339
319
|
releaseSignal();
|
|
340
320
|
try {
|
|
341
321
|
if (!res.writableEnded) {
|
|
@@ -7,11 +7,15 @@
|
|
|
7
7
|
* 这台电脑上的服务——跨源、还要凭据。声明了 `hostData` 的扩展把命中的请求(例如 `/api/`)
|
|
8
8
|
* 交给 App,App 转到这里,由宿主**以自己人身份**去问目标服务,再把结果原样递回去。
|
|
9
9
|
*
|
|
10
|
-
* **这是通用通道,不是给 magpie
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* **这是通用通道,不是给 magpie 开的专口子**:服务登记分两处 —— 随本插件走的静态表
|
|
11
|
+
* `SERVICES`(magpie 这种「我们自己的后台」),以及**第三方插件在自己 `package.json` 里
|
|
12
|
+
* 声明的 `dshMobileDataServices`**(见 `third-party-extensions.js`)。两处同形:每个服务
|
|
13
|
+
* 自己负责「怎么找到它、怎么带凭据」,以及「允许访问哪些路径前缀」(`allow`)——
|
|
13
14
|
* 一个服务只该被够到它自己的那部分路径,别拿它当跳板去够同端口的别处。
|
|
14
15
|
*
|
|
16
|
+
* 静态表优先查:第三方不可能用一个同名的声明把内置服务顶掉。第三方声明的服务目标固定是
|
|
17
|
+
* 宿主自己那个 web 端口(插件接口就注册在同一进程里),凭据用 `launchToken` 换浏览器会话。
|
|
18
|
+
*
|
|
15
19
|
* **边界仍然收着**:只有配好对的手机(这条路由在 token 守卫之后)能调;只能到登记过的服务;
|
|
16
20
|
* 路径必须落在该服务的 `allow` 前缀内;不暴露 shell、系统设置、任意文件读写。请求体与响应体
|
|
17
21
|
* 都有限长,超了如实回错,不硬吃进内存。
|
|
@@ -24,6 +28,8 @@ const http = require('node:http');
|
|
|
24
28
|
const { sendJson, readJsonBody } = require('../shared/http-util');
|
|
25
29
|
const { ensureWeb } = require('./magpie-web.js');
|
|
26
30
|
const { hasPermission } = require('./permissions.js');
|
|
31
|
+
const { hostPortOf, launchTokenOf } = require('./host-web.js');
|
|
32
|
+
const { discoverDataServices } = require('./third-party-extensions.js');
|
|
27
33
|
|
|
28
34
|
/** 请求体的上限:扩展页发来的是「一条请求」,几个 KB 的 JSON 足够,2MB 已经很宽松。 */
|
|
29
35
|
const MAX_REQUEST_BYTES = 2 * 1024 * 1024;
|
|
@@ -174,6 +180,99 @@ function failure(res, status, code, message) {
|
|
|
174
180
|
});
|
|
175
181
|
}
|
|
176
182
|
|
|
183
|
+
/**
|
|
184
|
+
* 第三方插件声明的数据服务:把它变成一条和静态表同形的 spec。
|
|
185
|
+
*
|
|
186
|
+
* 目标固定是**宿主自己那个 web 端口** —— 第三方插件的接口就注册在同一个进程、同一个
|
|
187
|
+
* `webServer` 上,没有第二个端口要探。凭据也只有一条路:用 `launchToken` 换一张浏览器
|
|
188
|
+
* 会话 cookie(`/api/*` 由连接服务的 `admit()` 认会话,环回地址不作数)。
|
|
189
|
+
*
|
|
190
|
+
* 这样 `allow` 之外的一律 403、没声明的服务一律 404,边界与 magpie 那条完全一致。
|
|
191
|
+
*/
|
|
192
|
+
function hostServiceSpec(ctx, allow) {
|
|
193
|
+
return {
|
|
194
|
+
allow,
|
|
195
|
+
async resolve() {
|
|
196
|
+
const port = hostPortOf(ctx);
|
|
197
|
+
if (port === null) {
|
|
198
|
+
const error = new Error('service-unavailable');
|
|
199
|
+
error.reason = 'no-port';
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
return { port, key: launchTokenOf(ctx) || null };
|
|
203
|
+
},
|
|
204
|
+
async cookie(port, key) {
|
|
205
|
+
return key ? browserCookie(port, key) : '';
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** 服务登记表查一次:静态表里的(magpie)优先,其次是插件声明的。 */
|
|
211
|
+
function specOf(ctx, service) {
|
|
212
|
+
if (SERVICES[service]) return SERVICES[service];
|
|
213
|
+
const pluginsRoot = profileNodeModulesOf(ctx);
|
|
214
|
+
if (!pluginsRoot) return null;
|
|
215
|
+
const declared = discoverDataServices(pluginsRoot).find((entry) => entry.service === service);
|
|
216
|
+
return declared ? hostServiceSpec(ctx, declared.allow) : null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** profile 的 `node_modules`:与路由表那侧同一个来源(运行时快照,取不到就当没有)。 */
|
|
220
|
+
function profileNodeModulesOf(ctx) {
|
|
221
|
+
try {
|
|
222
|
+
const runtime = typeof ctx?.get === 'function' ? ctx.get('dshMobilePluginRuntime') : null;
|
|
223
|
+
const status = runtime && typeof runtime.status === 'function' ? runtime.status() : null;
|
|
224
|
+
return typeof status?.profileNodeModules === 'string' ? status.profileNodeModules : null;
|
|
225
|
+
} catch {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 用 launch token 换一张浏览器会话 cookie(`GET /connect?token=…` 的 set-cookie)。
|
|
232
|
+
*
|
|
233
|
+
* 与 magpie 那条同一个机制,只是目标换成宿主自己的端口。缓存按端口 + token:同一进程里
|
|
234
|
+
* token 不变,每次重新握手纯属浪费。拿不到就回空串,接口会按 401 如实回,比静默吞掉好查。
|
|
235
|
+
*/
|
|
236
|
+
const hostCookieCache = new Map();
|
|
237
|
+
const HOST_COOKIE_TTL_MS = 5 * 60 * 1000;
|
|
238
|
+
|
|
239
|
+
function browserCookie(port, token) {
|
|
240
|
+
const cacheKey = `${port}:${token}`;
|
|
241
|
+
const hit = hostCookieCache.get(cacheKey);
|
|
242
|
+
if (hit && Date.now() - hit.at < HOST_COOKIE_TTL_MS) return Promise.resolve(hit.cookie);
|
|
243
|
+
return new Promise((resolve) => {
|
|
244
|
+
let done = false;
|
|
245
|
+
const finish = (cookie) => {
|
|
246
|
+
if (done) return;
|
|
247
|
+
done = true;
|
|
248
|
+
if (cookie) hostCookieCache.set(cacheKey, { cookie, at: Date.now() });
|
|
249
|
+
resolve(cookie);
|
|
250
|
+
};
|
|
251
|
+
let req;
|
|
252
|
+
try {
|
|
253
|
+
req = http.request({
|
|
254
|
+
host: '127.0.0.1',
|
|
255
|
+
port,
|
|
256
|
+
path: `/connect?token=${encodeURIComponent(token)}`,
|
|
257
|
+
method: 'GET',
|
|
258
|
+
headers: { accept: 'text/html, */*' },
|
|
259
|
+
timeout: 5000,
|
|
260
|
+
}, (res) => {
|
|
261
|
+
const raw = res.headers['set-cookie'];
|
|
262
|
+
res.resume();
|
|
263
|
+
if (!raw || !raw.length) return finish('');
|
|
264
|
+
// 只取 `name=value` 那一段,Path / HttpOnly 这些属性不进请求头。
|
|
265
|
+
finish(String(raw[0]).split(';')[0]);
|
|
266
|
+
});
|
|
267
|
+
} catch {
|
|
268
|
+
return finish('');
|
|
269
|
+
}
|
|
270
|
+
req.on('timeout', () => { try { req.destroy(); } catch { /* 已断 */ } finish(''); });
|
|
271
|
+
req.on('error', () => finish(''));
|
|
272
|
+
req.end();
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
177
276
|
/**
|
|
178
277
|
* POST /api/mobile/v1/extension/data(token)
|
|
179
278
|
* body: { service, path, method?, body?, contentType? }
|
|
@@ -181,13 +280,13 @@ function failure(res, status, code, message) {
|
|
|
181
280
|
* 只有登记过的服务、落在它 `allow` 前缀内的路径才放行。目标服务的状态码 / 头 / 正文原样带回,
|
|
182
281
|
* 让扩展页像对待一个普通 Response 一样对待它(4xx / 5xx 也照原样,页面自己判断)。
|
|
183
282
|
*/
|
|
184
|
-
async function handleExtensionData(
|
|
283
|
+
async function handleExtensionData(ctx, req, res) {
|
|
185
284
|
const body = await readJsonBody(req, MAX_REQUEST_BYTES);
|
|
186
285
|
if (!body || typeof body.service !== 'string' || typeof body.path !== 'string') {
|
|
187
286
|
return failure(res, 400, 'bad-request', '需要 { service, path }');
|
|
188
287
|
}
|
|
189
288
|
const service = body.service.trim();
|
|
190
|
-
const spec =
|
|
289
|
+
const spec = specOf(ctx, service);
|
|
191
290
|
if (!spec) return failure(res, 404, 'service-unknown', `没有登记这个服务:${service}`);
|
|
192
291
|
|
|
193
292
|
const method = typeof body.method === 'string' ? body.method.trim().toUpperCase() : 'GET';
|
|
@@ -224,6 +323,7 @@ async function handleExtensionData(_ctx, req, res) {
|
|
|
224
323
|
|
|
225
324
|
module.exports = {
|
|
226
325
|
handleExtensionData,
|
|
326
|
+
specOf,
|
|
227
327
|
SERVICES,
|
|
228
328
|
MAX_REQUEST_BYTES,
|
|
229
329
|
MAX_RESPONSE_BYTES,
|
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
const { cutAssistantBlock, cutToolResultEvent, createToolNameIndex } = require('./wire-cut.js');
|
|
15
|
-
const { rememberToolResults, toolResultsFor } = require('./tool-result-cache.js');
|
|
15
|
+
const { rememberToolResults, toolResultsFor, toolResultsVersionFor } = require('./tool-result-cache.js');
|
|
16
|
+
const crypto = require('node:crypto');
|
|
17
|
+
const { derivedCache } = require('../shared/derived-cache.js');
|
|
16
18
|
|
|
17
19
|
/** 每一轮出过的图(工具结果里的图片块),轮号 → 图。`lookback` 也扫:它才装得下更早那一页。 */
|
|
18
20
|
function collectTurnImages(entries, lookbackEntries) {
|
|
@@ -365,6 +367,36 @@ function presentedPathsOf(events) {
|
|
|
365
367
|
return paths;
|
|
366
368
|
}
|
|
367
369
|
|
|
370
|
+
/**
|
|
371
|
+
* 一段事件的指纹:seq + type 的顺序序列,外加这一段之外的两格(lookback、工具结果版本)。
|
|
372
|
+
*
|
|
373
|
+
* 判据的边界写在 `deriveMessages` 的注释里:会话日志只追加,同一 seq 的事件不会换内容;
|
|
374
|
+
* 会变的只有条数与窗口。`@param {unknown[]} parts` 里每一项要么是事件数组、要么是个标量。
|
|
375
|
+
*/
|
|
376
|
+
function eventFingerprint(events, lookbackEvents, toolResultsVersion) {
|
|
377
|
+
const hash = crypto.createHash('sha1');
|
|
378
|
+
// 一条事件只喂一次 `update`:`hash.update` 自身的固定开销在这个量级上比哈希本身还大
|
|
379
|
+
// (实测 3600 条事件拆成四次 update 要 3.3ms,拼成一串只要 1.3ms)。
|
|
380
|
+
const feed = (list) => {
|
|
381
|
+
if (!Array.isArray(list)) {
|
|
382
|
+
hash.update('|');
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
hash.update(`#${list.length}|`);
|
|
386
|
+
for (const entry of list) {
|
|
387
|
+
const e = entry && entry.event ? entry.event : entry;
|
|
388
|
+
if (!e || typeof e !== 'object') { hash.update('|'); continue; }
|
|
389
|
+
hash.update(`${Number.isSafeInteger(e.seq) ? e.seq : ''}:${typeof e.type === 'string' ? e.type : ''}|`);
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
// lookback 在指纹里只需要「有没有、多长」:它只用来补更早的工具结果,内容变化一定伴随
|
|
393
|
+
// 那个工具结果表的版本号变化,逐条扫一遍这一段不划算。
|
|
394
|
+
hash.update(Array.isArray(lookbackEvents) && lookbackEvents.length ? `l${lookbackEvents.length}` : 'l0');
|
|
395
|
+
hash.update(`|v${toolResultsVersion || 0}`);
|
|
396
|
+
feed(events);
|
|
397
|
+
return hash.digest('hex').slice(0, 16);
|
|
398
|
+
}
|
|
399
|
+
|
|
368
400
|
/**
|
|
369
401
|
* 从 history 事件折叠出消息与「结构化行」(wire v1 扩展,向后兼容):
|
|
370
402
|
* - 普通消息:{ id, seq, time, role, text, blocks?, images?, clientMessageId? };
|
|
@@ -377,6 +409,24 @@ function presentedPathsOf(events) {
|
|
|
377
409
|
* 结构化项与消息按 seq 升序混排;旧载荷字段缺失时跳过对应项,绝不抛错。
|
|
378
410
|
*/
|
|
379
411
|
function deriveMessages(events, sessionId, lookbackEvents) {
|
|
412
|
+
// 同一份事件折过一次就不再折:手机 15 秒轮询一次、镜像后台 500ms tick 一次,绝大多数
|
|
413
|
+
// 轮次窗口里一条新事件都没有(见 shared/derived-cache.js)。
|
|
414
|
+
//
|
|
415
|
+
// 指纹取**每条事件的 seq + type**,不取整份内容的哈希:会话日志是只追加的,同一个 seq
|
|
416
|
+
// 上那条事件的类型与在窗口里的位置不会变,变的只有「多出来几条」和「窗口从头挪了」。
|
|
417
|
+
// 这两件事 seq/type 序列都看得出来,而整份内容哈希在这个量级上比折一遍还贵(实测长会话
|
|
418
|
+
// 400 回合:整份 sha1 要 13ms、折一遍 9ms,按 seq/type 只要 1.3ms —— 拿它当判据等于白省)。
|
|
419
|
+
//
|
|
420
|
+
// 唯一从外面溜进来的输入是「进程里记下的工具结果」那张表(倒着翻页时用来补更早的图),
|
|
421
|
+
// 它自己的版本号也进指纹:这一页没变、但更晚的一页记下了结果,图就该跟着补上。
|
|
422
|
+
// 没有 sessionId 就不进缓存(分桶靠它,没有桶名的调用方多半是一次性计算)。
|
|
423
|
+
const cacheKey = sessionId
|
|
424
|
+
? eventFingerprint(events, lookbackEvents, toolResultsVersionFor(sessionId))
|
|
425
|
+
: '';
|
|
426
|
+
if (cacheKey) {
|
|
427
|
+
const hit = derivedCache.get(sessionId, cacheKey);
|
|
428
|
+
if (hit !== undefined) return hit;
|
|
429
|
+
}
|
|
380
430
|
const out = [];
|
|
381
431
|
// 预扫描 tool/result 事件(同批次事件内,结果可能在调用块之前/之后出现)。
|
|
382
432
|
// `lookbackEvents` 是这一页**再往前**的一小段:倒着分页时工具结果常常落在比消息更早的位置
|
|
@@ -1003,6 +1053,9 @@ function deriveMessages(events, sessionId, lookbackEvents) {
|
|
|
1003
1053
|
}
|
|
1004
1054
|
// 严格按事件 seq 升序(与消息混排;compaction 行升级/替换后位置以最终事件为准)。
|
|
1005
1055
|
out.sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0));
|
|
1056
|
+
// 存进缓存的是**已经排好序的最终结果**:调用方拿到的是同一份引用,拿去 filter / map 都好,
|
|
1057
|
+
// 但不要再就地改(要改就先拷一份)。见 shared/derived-cache.js 的说明。
|
|
1058
|
+
if (cacheKey) derivedCache.set(sessionId, cacheKey, out);
|
|
1006
1059
|
return out;
|
|
1007
1060
|
}
|
|
1008
1061
|
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* - **读坏不 404**:文件缺失、读一半、结构不对、缺键,一律逐键并到默认表,绝不 404。
|
|
19
19
|
* 这张表的缺省就是「能用」,把它读成故障等于让通知系统凭空瘫掉。
|
|
20
20
|
* - **写整表替换**:PUT 收的是完整 `events`,逐键 clamp 到三档枚举、未知键丢弃,再原子
|
|
21
|
-
* 落盘(照 `
|
|
21
|
+
* 落盘(照 `approval-memory.js` 的 mkdir + 临时文件 + rename + 0o600)。写不进去如实回失败。
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
const fs = require('node:fs');
|
|
@@ -122,7 +122,7 @@ function readEvents(deviceId) {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
/**
|
|
125
|
-
* 原子写整份文件(照
|
|
125
|
+
* 原子写整份文件(照 approval-memory:mkdir + 临时文件 + rename + 0o600)。
|
|
126
126
|
* 写失败安静返回 false,让调用方如实回 500,不抛。
|
|
127
127
|
*/
|
|
128
128
|
function saveStore(store) {
|