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
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 停止交代的出口:`GET /api/mobile/v1/session/stop-summary?sessionId=…[&turn=N]`。
|
|
5
|
+
*
|
|
6
|
+
* 用户点了停止之后,手机上那一轮不该只剩「已停止」三个字。这条路由把**这一轮跑过的步骤**
|
|
7
|
+
* 折成一句句看得懂的话(做了什么 / 看到了什么 / 没做完什么 / 对外发出几次),交给 App 的
|
|
8
|
+
* `StopSummaryCard` 画在被停止的那一轮末尾。
|
|
9
|
+
*
|
|
10
|
+
* 两个数据源,先内存后历史:还在跑(或刚停下)的那一轮在宿主的直播副本里
|
|
11
|
+
*(`live-steps.js`),历史里还没有;已经落定的轮按 `turnProcess.steps` 从会话历史折出来。
|
|
12
|
+
* 两处都是 `{ key, step }`,折叠逻辑只有一份(`stop-summary.js`,纯函数)。
|
|
13
|
+
*
|
|
14
|
+
* **按轮次答**(`?turn=N`,重新打开一条老会话时 App 补那一发用):
|
|
15
|
+
* - 不带 `turn`:最近一轮,先直播副本后历史(向后兼容,刚按停止那一刻走的就是这条);
|
|
16
|
+
* - 带 `turn`:只要那一轮。直播副本只有在它自己就是那一轮时才算数,否则一律回历史;
|
|
17
|
+
* - 那一轮在窗口里找不到:回**空交代**(不是报错),App 那边不出卡。
|
|
18
|
+
*
|
|
19
|
+
* 判据(为什么不能按消息上的 turn 号找那一轮):历史派生的普通消息(user / assistant / wake)
|
|
20
|
+
* **不带 turn 号** —— `history-derive.js` 的 `pushMessage` 收到 `data.turn` 后只拿它归并
|
|
21
|
+
* 产出清单与交付声明,从没写回 `item.turn`;全篇只有压缩行带 `item.turn`(`:754/:764/:786`),
|
|
22
|
+
* `:335` 那个 `turn` 在重试行上、`:660` 在读压缩行的轮号,都不是开轮消息。而
|
|
23
|
+
* `turnProcess.steps` 恰恰挂在**开轮那条消息**上(`openerByTurn`,`:923-932`)。
|
|
24
|
+
* 所以改用两个确实存在的锚点定位:这一轮收口那条结构行的 id 是 `turn-<轮号>`(`:916` 的
|
|
25
|
+
* `turn-aborted`,`turn-error` / `max-tokens` 同形)、带这一轮的收口 `seq`;开轮消息的
|
|
26
|
+
* `turnProcess.startSeq` 就是它自己的 `seq`(`:930`)。取「`startSeq` 最大、且小于那条
|
|
27
|
+
* 收口行的 `seq`」的 `turnProcess`,就是这一轮的过程。
|
|
28
|
+
*
|
|
29
|
+
* 只读、只算展示结构:不碰 shell、不读任意文件,也没有写操作。取不到就回空交代,让 App
|
|
30
|
+
* 不出那张卡,不报错。
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
const { sendJson } = require('../shared/http-util');
|
|
34
|
+
const { unwrap } = require('./envelope.js');
|
|
35
|
+
const { callSessions } = require('./rpc.js');
|
|
36
|
+
const { liveStepsOf, liveStepsTurnOf } = require('./live-steps.js');
|
|
37
|
+
const { deriveMessages } = require('./history-derive.js');
|
|
38
|
+
const { summarize } = require('./stop-summary.js');
|
|
39
|
+
|
|
40
|
+
/** 回落到历史时取多少条事件:够覆盖最近一轮的步骤与开场消息即可。 */
|
|
41
|
+
const HISTORY_WINDOW = 200;
|
|
42
|
+
|
|
43
|
+
/** 历史里最近一轮的过程清单(`turnProcess.steps` 挂在开轮那条消息上)。 */
|
|
44
|
+
function lastTurnSteps(messages) {
|
|
45
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
46
|
+
const item = messages[index];
|
|
47
|
+
const steps = item && item.turnProcess && Array.isArray(item.turnProcess.steps)
|
|
48
|
+
? item.turnProcess.steps
|
|
49
|
+
: null;
|
|
50
|
+
if (steps && steps.length > 0) return steps;
|
|
51
|
+
}
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 历史里**指定那一轮**的过程清单(判据见文件头)。
|
|
57
|
+
*
|
|
58
|
+
* 锚点是这一轮收口那条结构行(`turn-aborted` / `turn-error` / `max-tokens`,id 都是
|
|
59
|
+
* `turn-<轮号>`,带这一轮收口的 `seq`):它不在窗口里就说明这一轮已经翻出去了,回空。
|
|
60
|
+
* 有了它的 `seq`,再挑 `startSeq` 最大且更早的那份 `turnProcess` —— 那就是开启这一轮的消息。
|
|
61
|
+
*/
|
|
62
|
+
function turnSteps(messages, turn) {
|
|
63
|
+
const endId = `turn-${turn}`;
|
|
64
|
+
let endSeq = null;
|
|
65
|
+
for (const item of messages) {
|
|
66
|
+
if (!item || item.id !== endId) continue;
|
|
67
|
+
if (typeof item.seq !== 'number') continue;
|
|
68
|
+
endSeq = item.seq;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
if (endSeq === null) return [];
|
|
72
|
+
let best = null;
|
|
73
|
+
for (const item of messages) {
|
|
74
|
+
const process = item && item.turnProcess;
|
|
75
|
+
if (!process || !Array.isArray(process.steps) || process.steps.length === 0) continue;
|
|
76
|
+
const startSeq = typeof process.startSeq === 'number' ? process.startSeq : item.seq;
|
|
77
|
+
if (typeof startSeq !== 'number' || startSeq >= endSeq) continue;
|
|
78
|
+
if (best === null || startSeq > best.startSeq) best = { startSeq, steps: process.steps };
|
|
79
|
+
}
|
|
80
|
+
return best === null ? [] : best.steps;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 非负整数查询参数(与 `/session/step`、`/session/run-timeline` 同一套口径)。 */
|
|
84
|
+
function intParam(url, name) {
|
|
85
|
+
const raw = url.searchParams.get(name);
|
|
86
|
+
if (raw === null || raw === '') return null;
|
|
87
|
+
if (!/^\d+$/.test(raw)) return null;
|
|
88
|
+
const value = Number(raw);
|
|
89
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** GET /api/mobile/v1/session/stop-summary?sessionId=…[&turn=N](token) */
|
|
93
|
+
async function handleStopSummary(ctx, req, res) {
|
|
94
|
+
const url = new URL(req.url, 'http://x');
|
|
95
|
+
const sessionId = url.searchParams.get('sessionId');
|
|
96
|
+
if (!sessionId) {
|
|
97
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '缺少 sessionId' } });
|
|
98
|
+
}
|
|
99
|
+
const turn = intParam(url, 'turn');
|
|
100
|
+
const live = liveStepsOf(ctx, sessionId);
|
|
101
|
+
if (live.length > 0) {
|
|
102
|
+
// 直播副本只代表**当前这一轮**:点名了别的轮就老老实实走历史。
|
|
103
|
+
const liveTurn = liveStepsTurnOf(ctx, sessionId);
|
|
104
|
+
if (turn === null || liveTurn === null || liveTurn === turn) {
|
|
105
|
+
return sendJson(res, 200, { ok: true, summary: summarize(live), source: 'live' });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
let steps = [];
|
|
109
|
+
try {
|
|
110
|
+
const { value, error } = unwrap(await callSessions(ctx, 'history', { sessionId, maxMessages: HISTORY_WINDOW }));
|
|
111
|
+
if (!error) {
|
|
112
|
+
const events = (value && Array.isArray(value.events)) ? value.events : [];
|
|
113
|
+
const messages = deriveMessages(events, sessionId);
|
|
114
|
+
steps = turn === null ? lastTurnSteps(messages) : turnSteps(messages, turn);
|
|
115
|
+
}
|
|
116
|
+
} catch {
|
|
117
|
+
// 读不到历史就当没有可交代的东西:卡不出,不把它变成一次报错。
|
|
118
|
+
}
|
|
119
|
+
return sendJson(res, 200, { ok: true, summary: summarize(steps), source: 'history' });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
module.exports = { handleStopSummary, HISTORY_WINDOW, lastTurnSteps, turnSteps };
|
|
@@ -150,6 +150,21 @@ function stateFile() {
|
|
|
150
150
|
/** 内存缓存;null = 还没读过盘。 */
|
|
151
151
|
let cached = null;
|
|
152
152
|
|
|
153
|
+
/**
|
|
154
|
+
* 账本内容的版本号:**每一处改账本的地方都抬它**(`saveLedger` 与下面三个写函数)。
|
|
155
|
+
*
|
|
156
|
+
* `summarize` 把它和范围、当天日期一起当缓存键(见 `summarize` 的注释):账本一动,版本
|
|
157
|
+
* 就变,上那份 projections 自然失效。用自增序号而不是内容哈希:账本能到四千条记录、三年
|
|
158
|
+
* 按天账,整份哈希每次都比 `summarize` 本身还贵(2026-11-10 实测:4000 条 × 62 天,
|
|
159
|
+
* 折一次 13ms,整份 sha1 要 20ms),而这里要回答的只是「还是不是同一本账」。
|
|
160
|
+
*/
|
|
161
|
+
let ledgerVersion = 0;
|
|
162
|
+
|
|
163
|
+
/** 记一次改动(三张表任何一处动了都要抬,见上面那段)。 */
|
|
164
|
+
function markLedgerChanged() {
|
|
165
|
+
ledgerVersion += 1;
|
|
166
|
+
}
|
|
167
|
+
|
|
153
168
|
function num(value) {
|
|
154
169
|
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : 0;
|
|
155
170
|
}
|
|
@@ -367,6 +382,7 @@ function readLedger() {
|
|
|
367
382
|
/** 原子写:先写临时文件再 rename,写一半断电也不会留下半份 JSON。 */
|
|
368
383
|
function saveLedger(ledger) {
|
|
369
384
|
cached = ledger;
|
|
385
|
+
markLedgerChanged();
|
|
370
386
|
ledger.updatedAt = Date.now();
|
|
371
387
|
const file = stateFile();
|
|
372
388
|
try {
|
|
@@ -382,6 +398,7 @@ function saveLedger(ledger) {
|
|
|
382
398
|
/** 测试与「换个 DSH_HOME 重读」用。 */
|
|
383
399
|
function forgetLedger() {
|
|
384
400
|
cached = null;
|
|
401
|
+
markLedgerChanged();
|
|
385
402
|
}
|
|
386
403
|
|
|
387
404
|
/**
|
|
@@ -398,6 +415,7 @@ function forgetLedger() {
|
|
|
398
415
|
* 被当成首采、把整段历史重记一遍 —— 所以档位要抬到几年也用不完,别拿它当展示上限。
|
|
399
416
|
*/
|
|
400
417
|
function trim(ledger) {
|
|
418
|
+
markLedgerChanged();
|
|
401
419
|
const staleBefore = addDays(dayKeyOf(Date.now()), -MAX_DAILY_DAYS);
|
|
402
420
|
for (const record of Object.values(ledger.sessions)) {
|
|
403
421
|
if (record.daily) {
|
|
@@ -513,6 +531,7 @@ function emptySession(at) {
|
|
|
513
531
|
* `daily` —— 凭一个「最后活动日」给它编一天,模型卡在窄范围里就会比整页 KPI 还大。
|
|
514
532
|
*/
|
|
515
533
|
function applyModelUsage(ledger, entry) {
|
|
534
|
+
markLedgerChanged();
|
|
516
535
|
const selection = sanitizeModelSelection(entry && entry.model);
|
|
517
536
|
if (!selection) return ledger;
|
|
518
537
|
const key = modelKeyOf(selection.provider, selection.model);
|
|
@@ -551,6 +570,7 @@ function applyModelUsage(ledger, entry) {
|
|
|
551
570
|
* 时就只补累计:那一档范围里卡片同样是空的,两边一致。
|
|
552
571
|
*/
|
|
553
572
|
function applyModelBackfill(ledger, entry) {
|
|
573
|
+
markLedgerChanged();
|
|
554
574
|
const selection = sanitizeModelSelection(entry && entry.model);
|
|
555
575
|
if (!selection) return ledger;
|
|
556
576
|
const key = modelKeyOf(selection.provider, selection.model);
|
|
@@ -587,6 +607,7 @@ function applyModelBackfill(ledger, entry) {
|
|
|
587
607
|
* 增量也记进模型桶(无日期那笔同样按无日期记,见 `applyModelUsage`)。
|
|
588
608
|
*/
|
|
589
609
|
function applyDelta(ledger, entry) {
|
|
610
|
+
markLedgerChanged();
|
|
590
611
|
const sessionId = entry.sessionId;
|
|
591
612
|
const delta = sanitizeUsage(entry.delta);
|
|
592
613
|
const at = num(entry.at) || Date.now();
|
|
@@ -639,6 +660,7 @@ function applyDelta(ledger, entry) {
|
|
|
639
660
|
* 把「翻不到的历史差额」补进 `history`。**不做叠加**,是这一步的全部目的。
|
|
640
661
|
*/
|
|
641
662
|
function resetSessionUsage(ledger, sessionId) {
|
|
663
|
+
markLedgerChanged();
|
|
642
664
|
const record = ledger.sessions[sessionId];
|
|
643
665
|
if (!record) return null;
|
|
644
666
|
const daily = record.daily && typeof record.daily === 'object' ? record.daily : {};
|
|
@@ -717,6 +739,28 @@ function usageInRange(record, startDay, endDay) {
|
|
|
717
739
|
*/
|
|
718
740
|
function summarize(ledger, range, now) {
|
|
719
741
|
const today = dayKeyOf(now);
|
|
742
|
+
// 同一本账、同一个范围、同一天再折一次就是在白算:统计页 1.5 秒轮询一次、App 切范围时
|
|
743
|
+
// 也只换一个词,而账本大多时候一秒都没动。判据三格一起看 —— 账本**对象身份**、改动版本
|
|
744
|
+
// (见 `ledgerVersion`)、范围 + 当天日期(跨零点要换一份逐日序列)。
|
|
745
|
+
//
|
|
746
|
+
// 只留最近一份:页面一次只看一个范围,留一份就够,也省掉一张要自己淘汰的表。
|
|
747
|
+
if (summaryCache
|
|
748
|
+
&& summaryCache.ledger === ledger
|
|
749
|
+
&& summaryCache.version === ledgerVersion
|
|
750
|
+
&& summaryCache.range === range
|
|
751
|
+
&& summaryCache.today === today) {
|
|
752
|
+
return summaryCache.result;
|
|
753
|
+
}
|
|
754
|
+
const result = summarizeUncached(ledger, range, now, today);
|
|
755
|
+
summaryCache = { ledger, version: ledgerVersion, range, today, result };
|
|
756
|
+
return result;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** `summarize` 的缓存槽(只留最近一次;见上面那段注释)。 */
|
|
760
|
+
let summaryCache = null;
|
|
761
|
+
|
|
762
|
+
/** 真正折一份出来(`summarize` 在缓存没命中时调它)。 */
|
|
763
|
+
function summarizeUncached(ledger, range, now, today) {
|
|
720
764
|
const startDay = startDayOf(range, now);
|
|
721
765
|
const endDay = endDayOf(range, now);
|
|
722
766
|
const allDays = Object.keys(ledger.days).sort();
|
|
@@ -937,6 +981,7 @@ module.exports = {
|
|
|
937
981
|
emptySession,
|
|
938
982
|
summarize,
|
|
939
983
|
trim,
|
|
984
|
+
markLedgerChanged,
|
|
940
985
|
dayKeyOf,
|
|
941
986
|
addDays,
|
|
942
987
|
startDayOf,
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 用户按停止后补在对话里的那份「本轮交代」:从**一次运行的步骤流**折成 App 侧
|
|
5
|
+
* `StopSummaryView` 那个形状(做了什么 / 看到了什么 / 没做完的部分 / 对外发出几次)。
|
|
6
|
+
*
|
|
7
|
+
* 为什么是纯函数、不调模型:停止那一刻要立刻给用户一句看得懂的话,不该再等一次模型往返。
|
|
8
|
+
* 输入复用直播清单与历史清单同一份形状(`live-steps.js` 的 `liveStepsOf` 与
|
|
9
|
+
* `history-derive.js` 的 `turnProcess.steps` 都是 `{ key, step }`,`step` 带
|
|
10
|
+
* `kind` / `name` / `summary` / `state`),所以两条数据源都能直接喂进来。
|
|
11
|
+
*
|
|
12
|
+
* 信息量口径:只陈述**能从那一步本身读到的东西**。状态只认步骤自己的 `state`
|
|
13
|
+
* (`ok` / `error` / `stopped`,缺省即「还在跑」),读不到就不写那一句 —— 宁可少写一行,
|
|
14
|
+
* 也不要把「已发出」写在一次其实失败了的动作上。「对外发出几次」同理:只认步骤上显式的
|
|
15
|
+
* `outward` / `irreversible` 标记,不按工具名或文案猜。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** 交代里每一段的条数上限:行再多也看不完,白占篇幅。 */
|
|
19
|
+
const MAX_DONE = 12;
|
|
20
|
+
const MAX_SEEN = 8;
|
|
21
|
+
const MAX_UNFINISHED = 12;
|
|
22
|
+
|
|
23
|
+
/** 单条摘要的字数上限。 */
|
|
24
|
+
const CLIP = 80;
|
|
25
|
+
|
|
26
|
+
/** 读类工具:这一步是「看到了什么」,不是「做了什么改动」。 */
|
|
27
|
+
const READ_TOOLS = new Set([
|
|
28
|
+
'read', 'read_image', 'web_fetch', 'web_search', 'grep', 'glob',
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
/** 思考步不是「做的事」,但可以是「最后想到哪 / 写了一半的字」。 */
|
|
32
|
+
function isThink(step) {
|
|
33
|
+
return step && step.kind === 'think';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 把一个清单条目摊成裸 step:`{ key, step }` 去掉壳,裸 step 原样返回。 */
|
|
37
|
+
function normalizeEntry(entry) {
|
|
38
|
+
if (!entry || typeof entry !== 'object') return {};
|
|
39
|
+
return entry.step && typeof entry.step === 'object'
|
|
40
|
+
? { key: entry.key, ...entry.step }
|
|
41
|
+
: entry;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 摊平步骤数组:认 `{ steps: [...] }` 与裸数组两种形状。 */
|
|
45
|
+
function toStepList(run) {
|
|
46
|
+
const list = Array.isArray(run)
|
|
47
|
+
? run
|
|
48
|
+
: (run && typeof run === 'object' && Array.isArray(run.steps) ? run.steps : null);
|
|
49
|
+
return list ? list.map(normalizeEntry) : [];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function clip(text, max = CLIP) {
|
|
53
|
+
const value = String(text == null ? '' : text).replace(/\s+/g, ' ').trim();
|
|
54
|
+
if (value.length <= max) return value;
|
|
55
|
+
return `${value.slice(0, max - 1)}…`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 这一步行给人看的标签:优先现成摘要,没有就从参数里挑一个最像「这一步是干嘛的」字段。 */
|
|
59
|
+
function labelOf(step) {
|
|
60
|
+
if (!step || typeof step !== 'object') return '';
|
|
61
|
+
if (typeof step.summary === 'string' && step.summary.trim() !== '') return clip(step.summary);
|
|
62
|
+
const args = step.args && typeof step.args === 'object' ? step.args : {};
|
|
63
|
+
const candidates = ['target', 'text', 'description', 'query', 'command', 'file_path', 'path', 'ref', 'url'];
|
|
64
|
+
for (const key of candidates) {
|
|
65
|
+
const value = args[key];
|
|
66
|
+
if (typeof value === 'string' && value.trim() !== '') return clip(value);
|
|
67
|
+
}
|
|
68
|
+
return clip(step.name || step.kind || '');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 一步的状态归成三种:成功 / 没用成 / 还在跑(读不到就是还在跑)。 */
|
|
72
|
+
function stateOf(step) {
|
|
73
|
+
const state = step && typeof step.state === 'string' ? step.state : '';
|
|
74
|
+
if (state === 'ok') return 'ok';
|
|
75
|
+
if (state === 'error') return 'error';
|
|
76
|
+
if (state === 'stopped') return 'stopped';
|
|
77
|
+
return 'running';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 这一步有没有显式标成「对外发出」(发送 / 提交 / 下单这类收不回来的动作)。 */
|
|
81
|
+
function outwardOf(step) {
|
|
82
|
+
if (!step || typeof step !== 'object') return false;
|
|
83
|
+
return step.outward === true || step.irreversible === true
|
|
84
|
+
|| (typeof step.outward === 'string' && step.outward !== '');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 从一次运行生成交代。
|
|
89
|
+
*
|
|
90
|
+
* @param {object|Array} run 步骤流(`{ steps: [{key, step}] }` 或裸数组)。
|
|
91
|
+
* @returns {{did: string[], saw: string[], outboundCount?: number, unfinished?: string[], closing?: string}}
|
|
92
|
+
*/
|
|
93
|
+
function summarize(run) {
|
|
94
|
+
const steps = toStepList(run);
|
|
95
|
+
const did = [];
|
|
96
|
+
const seen = [];
|
|
97
|
+
const unfinished = [];
|
|
98
|
+
let outboundCount = 0;
|
|
99
|
+
let closing = '';
|
|
100
|
+
|
|
101
|
+
for (const step of steps) {
|
|
102
|
+
if (!step || typeof step !== 'object') continue;
|
|
103
|
+
const label = labelOf(step);
|
|
104
|
+
const state = stateOf(step);
|
|
105
|
+
|
|
106
|
+
if (isThink(step)) {
|
|
107
|
+
// 思考步不进「做了什么」;只记最后一段有内容的思考。
|
|
108
|
+
if (label) closing = label;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (!step.name && !label) continue;
|
|
112
|
+
|
|
113
|
+
if (state !== 'ok') {
|
|
114
|
+
unfinished.push(label || String(step.name || '一步动作'));
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (READ_TOOLS.has(step.name)) {
|
|
119
|
+
if (label) seen.push(label);
|
|
120
|
+
} else if (label) {
|
|
121
|
+
did.push(label);
|
|
122
|
+
}
|
|
123
|
+
if (outwardOf(step)) outboundCount += 1;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const view = {
|
|
127
|
+
did: did.slice(-MAX_DONE),
|
|
128
|
+
saw: seen.slice(-MAX_SEEN),
|
|
129
|
+
};
|
|
130
|
+
if (outboundCount > 0) view.outboundCount = outboundCount;
|
|
131
|
+
if (unfinished.length > 0) view.unfinished = unfinished.slice(0, MAX_UNFINISHED);
|
|
132
|
+
if (closing) view.closing = closing;
|
|
133
|
+
return view;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
module.exports = {
|
|
137
|
+
summarize,
|
|
138
|
+
// 给守卫与接线方直接用的纯函数:
|
|
139
|
+
toStepList,
|
|
140
|
+
labelOf,
|
|
141
|
+
stateOf,
|
|
142
|
+
outwardOf,
|
|
143
|
+
MAX_DONE,
|
|
144
|
+
MAX_SEEN,
|
|
145
|
+
MAX_UNFINISHED,
|
|
146
|
+
};
|
|
@@ -23,6 +23,18 @@ const path = require('node:path');
|
|
|
23
23
|
|
|
24
24
|
/** 插件 `package.json` 里声明「我带了给手机用的扩展」的那一格。 */
|
|
25
25
|
const DECL_FIELD = 'dshMobileExtensions';
|
|
26
|
+
/**
|
|
27
|
+
* 插件 `package.json` 里声明「我给手机扩展开哪个数据接口」的那一格。
|
|
28
|
+
*
|
|
29
|
+
* 形状是一个数组,每项 `{ service, allow }`:
|
|
30
|
+
* - `service`:扩展包 `manifest.json` 的 `hostData.service` 要写的那个名字;
|
|
31
|
+
* - `allow`:允许被够到的路径前缀(以 `/` 开头)。
|
|
32
|
+
*
|
|
33
|
+
* 没有这一格的插件,它的扩展页只能拿基础两项能力,「读宿主数据」那一项即使声明了能力
|
|
34
|
+
* 也找不到服务、一律 404。安全边界不变:目标固定是宿主自己那个 web 端口,不指向任何
|
|
35
|
+
* 外部地址,路径还必须落在 `allow` 里。
|
|
36
|
+
*/
|
|
37
|
+
const DATA_SERVICES_FIELD = 'dshMobileDataServices';
|
|
26
38
|
/** `true` 时读这个目录名。 */
|
|
27
39
|
const DEFAULT_DIR = 'extensions';
|
|
28
40
|
/** 目录名与 slug 的字符口径,与包内清单那一档一致。 */
|
|
@@ -47,6 +59,50 @@ function declaredDir(manifest) {
|
|
|
47
59
|
return text.split(path.sep).join('/');
|
|
48
60
|
}
|
|
49
61
|
|
|
62
|
+
/** 服务名的字符口径:与扩展包 `hostData.service` 那一侧同一套(`host-data.ts`)。 */
|
|
63
|
+
const SERVICE_RE = /^[A-Za-z0-9._-]{1,64}$/;
|
|
64
|
+
/** 一条声明最多带这么多路径前缀:一个网页界面够用了,几十条已经离谱。 */
|
|
65
|
+
const MAX_ALLOW = 32;
|
|
66
|
+
/** 单条前缀长度上限。 */
|
|
67
|
+
const MAX_ALLOW_PREFIX = 256;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 读一个插件 `package.json` 里的数据服务声明。
|
|
71
|
+
*
|
|
72
|
+
* 只认最窄的那种写法:数组,每项一个对象带 `service` 与非空的 `allow` 字符串数组。
|
|
73
|
+
* 任何一项不合规就**只丢那一项**,不因此把整份声明判废 —— 一个手滑的前缀不该让同包
|
|
74
|
+
* 另一个本来能用的服务也一起消失。`service` 撞名的以后扫到的为准由调用方决定(见
|
|
75
|
+
* `discoverDataServices` 的注释:先扫到的优先)。
|
|
76
|
+
*
|
|
77
|
+
* 返回 `[{ service, allow }]`;没声明 / 不是数组一律空数组。
|
|
78
|
+
*/
|
|
79
|
+
function parseDataServices(manifest) {
|
|
80
|
+
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) return [];
|
|
81
|
+
const raw = manifest[DATA_SERVICES_FIELD];
|
|
82
|
+
if (!Array.isArray(raw)) return [];
|
|
83
|
+
const out = [];
|
|
84
|
+
for (const item of raw) {
|
|
85
|
+
if (!item || typeof item !== 'object' || Array.isArray(item)) continue;
|
|
86
|
+
const service = typeof item.service === 'string' ? item.service.trim() : '';
|
|
87
|
+
if (!SERVICE_RE.test(service)) continue;
|
|
88
|
+
if (!Array.isArray(item.allow)) continue;
|
|
89
|
+
const allow = [];
|
|
90
|
+
for (const prefix of item.allow) {
|
|
91
|
+
if (typeof prefix !== 'string') continue;
|
|
92
|
+
const text = prefix.trim();
|
|
93
|
+
if (!text || text.length > MAX_ALLOW_PREFIX) continue;
|
|
94
|
+
// 只认本源绝对路径前缀:整条 URL 或 `//host` 之类的写法会把它变成对外站的跳板。
|
|
95
|
+
if (!text.startsWith('/') || text.startsWith('//')) continue;
|
|
96
|
+
if (allow.includes(text)) continue;
|
|
97
|
+
allow.push(text);
|
|
98
|
+
if (allow.length >= MAX_ALLOW) break;
|
|
99
|
+
}
|
|
100
|
+
if (allow.length === 0) continue;
|
|
101
|
+
out.push({ service, allow });
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
50
106
|
/** 包名 → slug 前缀:`@scope/name` 变成 `scope.name`,其余字符削成 `_`。 */
|
|
51
107
|
function ownerSlugOf(name) {
|
|
52
108
|
const raw = String(name || '').replace(/^@/, '').replace(/[\\/]+/g, '.');
|
|
@@ -146,13 +202,43 @@ function directoryOf(pluginsRoot, slug) {
|
|
|
146
202
|
return hit ? hit.directory : null;
|
|
147
203
|
}
|
|
148
204
|
|
|
205
|
+
/**
|
|
206
|
+
* 扫一遍 profile,列出所有插件声明过的**数据服务**。
|
|
207
|
+
*
|
|
208
|
+
* 返回 `[{ service, allow, packageName }]`。同名服务先扫到的优先 —— 与 slug 去重同一套
|
|
209
|
+
* 口径(`installedPackageDirs` 的顺序是稳定的:先顶层包、再按名字),避免两个插件抢同一个
|
|
210
|
+
* 名字时结果随文件系统顺序漂。静态登记表里的服务(magpie)在调用方那一层先查,所以第三方
|
|
211
|
+
* 也不可能把内置那几个名字顶掉。
|
|
212
|
+
*/
|
|
213
|
+
function discoverDataServices(pluginsRoot) {
|
|
214
|
+
if (typeof pluginsRoot !== 'string' || !pluginsRoot) return [];
|
|
215
|
+
const found = [];
|
|
216
|
+
const seen = new Set();
|
|
217
|
+
// 按包名排一次再扫:`readdirSync` 的顺序不是承诺,同名服务「先扫到的优先」得先有个确定的顺序,
|
|
218
|
+
// 否则重启一次谁赢都可能变。
|
|
219
|
+
const dirs = installedPackageDirs(pluginsRoot).sort((a, b) => a.name.localeCompare(b.name));
|
|
220
|
+
for (const { name, directory } of dirs) {
|
|
221
|
+
const manifest = manifestOf(directory);
|
|
222
|
+
if (!manifest) continue;
|
|
223
|
+
for (const declared of parseDataServices(manifest)) {
|
|
224
|
+
if (seen.has(declared.service)) continue;
|
|
225
|
+
seen.add(declared.service);
|
|
226
|
+
found.push({ service: declared.service, allow: declared.allow, packageName: name });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return found;
|
|
230
|
+
}
|
|
231
|
+
|
|
149
232
|
module.exports = {
|
|
150
233
|
DECL_FIELD,
|
|
151
234
|
DEFAULT_DIR,
|
|
235
|
+
DATA_SERVICES_FIELD,
|
|
152
236
|
SLUG_PATTERN,
|
|
153
237
|
declaredDir,
|
|
238
|
+
parseDataServices,
|
|
154
239
|
ownerSlugOf,
|
|
155
240
|
slugOf,
|
|
156
241
|
discoverPackages,
|
|
157
242
|
directoryOf,
|
|
243
|
+
discoverDataServices,
|
|
158
244
|
};
|
|
@@ -14,6 +14,10 @@
|
|
|
14
14
|
*
|
|
15
15
|
* 边界:只活在插件进程里、只按会话分桶、每桶按插入序淘汰 —— 进程重启就没了,那时手机重新
|
|
16
16
|
* 进对话本来也要重拉。
|
|
17
|
+
*
|
|
18
|
+
* 还有一份**改动版本**(`toolResultsVersionFor`):派生缓存拿它当指纹的一格,因为这张表
|
|
19
|
+
* 是 `deriveMessages` 的隐藏输入 —— 这一页自己没变、更晚的一页记下了结果,上一页的图
|
|
20
|
+
* 也该跟着补上。
|
|
17
21
|
*/
|
|
18
22
|
|
|
19
23
|
/** 每条会话记住多少个调用:一次翻页窗口最多几百个,2000 足够覆盖一次通读。 */
|
|
@@ -24,11 +28,22 @@ const MAX_SESSIONS = 8;
|
|
|
24
28
|
/** sessionId → Map(callId → 结果)。Map 的插入序就是淘汰序。 */
|
|
25
29
|
const stores = new Map();
|
|
26
30
|
|
|
31
|
+
/**
|
|
32
|
+
* sessionId → 这张桶被改过几次。
|
|
33
|
+
*
|
|
34
|
+
* 这张表是**派生输入的一部分**(`deriveMessages` 会把记下的结果并进当前这一页),而它的
|
|
35
|
+
* 变化不一定伴随事件变化:这一页自己没变、更晚的一页把结果记了下来,上一页折出来的图就
|
|
36
|
+
* 该跟着补上。派生缓存拿它当指纹的一格,才有办法把「内容一样但输入变了」那一档分开。
|
|
37
|
+
* 只在真改了桶的时候 +1,读操作不动它。
|
|
38
|
+
*/
|
|
39
|
+
const storeVersions = new Map();
|
|
40
|
+
|
|
27
41
|
/**
|
|
28
42
|
* 记住这一页看到的结果(已存在的不覆盖:同一次调用的结果不会变,先记下的就是它)。
|
|
29
43
|
*/
|
|
30
44
|
function rememberToolResults(sessionId, results) {
|
|
31
45
|
if (!sessionId || !results || results.size === 0) return;
|
|
46
|
+
let added = false;
|
|
32
47
|
let bucket = stores.get(sessionId);
|
|
33
48
|
if (!bucket) {
|
|
34
49
|
bucket = new Map();
|
|
@@ -47,12 +62,19 @@ function rememberToolResults(sessionId, results) {
|
|
|
47
62
|
for (const [callId, result] of results) {
|
|
48
63
|
if (bucket.has(callId)) continue;
|
|
49
64
|
bucket.set(callId, result);
|
|
65
|
+
added = true;
|
|
50
66
|
if (bucket.size > MAX_CALLS_PER_SESSION) {
|
|
51
67
|
const oldest = bucket.keys().next();
|
|
52
68
|
if (oldest.done) break;
|
|
53
69
|
bucket.delete(oldest.value);
|
|
54
70
|
}
|
|
55
71
|
}
|
|
72
|
+
if (added) storeVersions.set(sessionId, (storeVersions.get(sessionId) || 0) + 1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 这条会话的结果表被改过几次(见 `storeVersions`)。没有记录就是 0。 */
|
|
76
|
+
function toolResultsVersionFor(sessionId) {
|
|
77
|
+
return (sessionId && storeVersions.get(sessionId)) || 0;
|
|
56
78
|
}
|
|
57
79
|
|
|
58
80
|
/** 这条会话已经记下的结果(没有就是 empty Map)。调用方不要改它。 */
|
|
@@ -62,13 +84,19 @@ function toolResultsFor(sessionId) {
|
|
|
62
84
|
|
|
63
85
|
/** 测试用:清掉一条(或全部)会话的记录。 */
|
|
64
86
|
function clearToolResults(sessionId) {
|
|
65
|
-
if (sessionId)
|
|
66
|
-
|
|
87
|
+
if (sessionId) {
|
|
88
|
+
stores.delete(sessionId);
|
|
89
|
+
storeVersions.set(sessionId, (storeVersions.get(sessionId) || 0) + 1);
|
|
90
|
+
} else {
|
|
91
|
+
stores.clear();
|
|
92
|
+
storeVersions.clear();
|
|
93
|
+
}
|
|
67
94
|
}
|
|
68
95
|
|
|
69
96
|
module.exports = {
|
|
70
97
|
rememberToolResults,
|
|
71
98
|
toolResultsFor,
|
|
99
|
+
toolResultsVersionFor,
|
|
72
100
|
clearToolResults,
|
|
73
101
|
MAX_CALLS_PER_SESSION,
|
|
74
102
|
MAX_SESSIONS,
|