dsh-mobile-plugin 0.5.35 → 0.5.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/lib/impl.js +0 -7
  2. package/lib/mobile/answer-sources.js +223 -0
  3. package/lib/mobile/approval-memory.js +1 -1
  4. package/lib/mobile/events.js +0 -20
  5. package/lib/mobile/extension-data.js +105 -5
  6. package/lib/mobile/extension-dev.js +42 -0
  7. package/lib/mobile/history-derive.js +54 -1
  8. package/lib/mobile/model-meta-table.cjs +1 -1
  9. package/lib/mobile/model-meta.js +1 -1
  10. package/lib/mobile/monitor.js +8 -2
  11. package/lib/mobile/notification-prefs.js +2 -2
  12. package/lib/mobile/permissions.js +8 -5
  13. package/lib/mobile/plugin-market.js +103 -4
  14. package/lib/mobile/run-timeline.js +317 -0
  15. package/lib/mobile/session-active.js +66 -0
  16. package/lib/mobile/session-stop-summary.js +122 -0
  17. package/lib/mobile/sessions.js +8 -2
  18. package/lib/mobile/stats-ledger.cjs +45 -37
  19. package/lib/mobile/stop-summary.js +292 -0
  20. package/lib/mobile/third-party-extensions.js +86 -0
  21. package/lib/mobile/tool-result-cache.js +30 -2
  22. package/lib/panel/index.html +15 -15
  23. package/lib/relay/lan-proxy.js +20 -11
  24. package/lib/relay/tunnel.js +28 -12
  25. package/lib/routes/mobile-api.js +25 -19
  26. package/lib/shared/derived-cache.js +121 -0
  27. package/lib/shared/http-util.js +55 -18
  28. package/package.json +1 -2
  29. package/config/agent-presets/supervisor/agent.cordis.yml +0 -237
  30. package/config/agent-presets/supervisor/preset.yml +0 -3
  31. package/lib/mobile/auto-review-log.js +0 -160
  32. package/lib/mobile/knowledge-base.js +0 -481
  33. package/lib/mobile/mail.js +0 -621
  34. package/lib/mobile/model-prices-table.cjs +0 -23
  35. package/lib/mobile/model-prices.cjs +0 -88
  36. package/lib/mobile/supervisor-broadcast.js +0 -503
  37. package/lib/mobile/supervisor-memory/index.js +0 -305
  38. package/lib/mobile/supervisor-runtime/index.js +0 -54
  39. package/lib/mobile/supervisor-runtime/inject.js +0 -218
  40. package/lib/mobile/supervisor-runtime/judgment.js +0 -113
  41. package/lib/mobile/supervisor-tools.js +0 -419
  42. package/lib/mobile/task-ledger.js +0 -381
@@ -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) stores.delete(sessionId);
66
- else stores.clear();
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,