dsh-mobile-plugin 0.5.41 → 0.5.45
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/config/agent-presets/standard/agent.cordis.yml +14 -0
- package/dsh-mobile-credentials/lib/credentials.js +193 -66
- package/lib/client.js +1 -94
- package/lib/desktop-half-stats.cjs +2 -12
- package/lib/host-compat.cjs +2 -0
- package/lib/http/routing.js +2 -2
- package/lib/mirror/pusher.cjs +52 -2
- package/lib/mobile/answer-sources.js +5 -4
- package/lib/mobile/events.js +2 -24
- package/lib/mobile/file-read.js +12 -36
- package/lib/mobile/file-stat.js +31 -0
- package/lib/mobile/history-derive.js +142 -5
- package/lib/mobile/host-bridge/endpoints.js +8 -0
- package/lib/mobile/permissions.js +5 -1
- package/lib/mobile/presented-file-stats.js +244 -0
- package/lib/mobile/run-timeline.js +5 -2
- package/lib/mobile/session-artifacts.js +494 -0
- package/lib/mobile/session-cache.js +49 -1
- package/lib/mobile/session-file.js +232 -100
- package/lib/mobile/session-snapshot.js +19 -4
- package/lib/mobile/session-team.js +59 -0
- package/lib/mobile/stats-collect.cjs +43 -13
- package/lib/mobile/stats.js +45 -10
- package/lib/mobile/stream.js +24 -3
- package/lib/mobile/todo-phases.js +113 -0
- package/lib/mobile/wire-cut.js +27 -1
- package/lib/panel/index.html +8 -8
- package/lib/routes/control-plane.js +21 -23
- package/lib/routes/mobile-api.js +17 -7
- package/lib/shared/e2ee-chunks.js +23 -3
- package/package.json +1 -1
- package/lib/desktop-view.cjs +0 -124
- package/lib/shared/desktop-view-signal.js +0 -66
|
@@ -12,7 +12,16 @@
|
|
|
12
12
|
* 2. 相对路径按会话工作区解析:cwd 取自会话列表摘要;没有 cwd 的会话只能走 present 白名单。
|
|
13
13
|
* 3. 流式回写,不在插件进程里缓冲:几十 MB 的录屏不该整包躺在内存里。
|
|
14
14
|
*
|
|
15
|
+
* 请求体可以带一个可选的 `offset`(明文偏移,必须是 `CHUNK` 的整数倍):边下边播按段取字节,
|
|
16
|
+
* 契约见 `docs/streaming-media.md` §1。响应那一半(明文的整份 / 206 一段、协议 v2 的分块信封
|
|
17
|
+
* 与起始块序号)都在 `sendFileBytes` 里,`/session/file` 与 `/file/read` 共用这一份。
|
|
18
|
+
*
|
|
15
19
|
* 两种失败分开:两条口子都不认就 403(`outside-workspace`),认了但盘上没了回 404(`missing`)。
|
|
20
|
+
*
|
|
21
|
+
* 放行判定与「按块加密写出去」都在这一个文件里,对外只导出**一份**:产出清单的 stat 与打包
|
|
22
|
+
*(`session-artifacts.js`)用同一个 `createSessionFileScope`,加密流用同一个
|
|
23
|
+
*`writeEncryptedStream`。手机上能 stat / 打包的路径必须与本来就能下载的完全重合,抄第二份
|
|
24
|
+
* 判定就是给「能力面悄悄扩大」留后门。
|
|
16
25
|
*/
|
|
17
26
|
|
|
18
27
|
const fs = require('node:fs');
|
|
@@ -20,8 +29,7 @@ const { stat } = require('node:fs/promises');
|
|
|
20
29
|
const path = require('node:path');
|
|
21
30
|
const { pipeline } = require('node:stream/promises');
|
|
22
31
|
const { sendJson, readJsonBody } = require('../shared/http-util');
|
|
23
|
-
const {
|
|
24
|
-
const { FILE_CHUNK_BYTES, writeChunk } = require('../shared/e2ee-chunks.js');
|
|
32
|
+
const { FILE_CHUNK_BYTES, E2EE_CHUNKS_TYPE, pipeEncrypted } = require('../shared/e2ee-chunks.js');
|
|
25
33
|
const { unwrap } = require('./envelope.js');
|
|
26
34
|
const { callSessions } = require('./rpc.js');
|
|
27
35
|
const { cwdOf } = require('./session-cwd.js');
|
|
@@ -108,114 +116,230 @@ function isInsideWorkspace(cwd, target) {
|
|
|
108
116
|
}
|
|
109
117
|
|
|
110
118
|
/**
|
|
111
|
-
*
|
|
119
|
+
* 一条会话的放行面:工作区目录(相对路径的解析基准)+ 历史窗口内的声明集合。
|
|
112
120
|
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
121
|
+
* 抽成「作用域」而不是一个布尔函数,是因为 stat 与打包都要对**一长串**路径做同一个判定:
|
|
122
|
+
* 每条都重新读一遍历史等于把 200 次判定变成 200 次宿主 RPC。这里两个值都按需取、只取一次。
|
|
123
|
+
*
|
|
124
|
+
* 这是 `/session/file`、产出清单的 stat 与打包三条路**唯一**的放行判定 —— 手机上能 stat 的
|
|
125
|
+
* 路径必须与本来就能下载的完全重合,抄第二份就是给「能力面悄悄扩大」留后门。
|
|
116
126
|
*/
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
return
|
|
123
|
-
}
|
|
127
|
+
function createSessionFileScope(ctx, sessionId) {
|
|
128
|
+
let cwdPromise = null;
|
|
129
|
+
let declaredPromise = null;
|
|
130
|
+
const cwd = () => {
|
|
131
|
+
if (!cwdPromise) cwdPromise = cwdOf(ctx, sessionId);
|
|
132
|
+
return cwdPromise;
|
|
133
|
+
};
|
|
134
|
+
// 只有「工作区那一侧不认」时才读历史:正文里点的链接不该为了一次判定去拉整窗历史。
|
|
135
|
+
const declared = () => {
|
|
136
|
+
if (!declaredPromise) declaredPromise = declaredPathsOf(ctx, sessionId);
|
|
137
|
+
return declaredPromise;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 判一条路径认不认。
|
|
142
|
+
*
|
|
143
|
+
* 回 `{ ok:true, target }` 或 `{ ok:false, status, code, message }`;历史读不出来时**抛**
|
|
144
|
+
* (调用方决定怎么报:`/session/file` 回 502,清单那两条把这一条当 denied 跳过)。
|
|
145
|
+
*
|
|
146
|
+
* @param {string} requested 调用方给的原始字符串
|
|
147
|
+
*/
|
|
148
|
+
async function resolve(requested) {
|
|
149
|
+
// 先把落点算出来:工作区那一侧判的是**解析后的真实路径**,不是输入字符串。
|
|
150
|
+
const root = await cwd();
|
|
151
|
+
let target = path.isAbsolute(requested) ? path.resolve(requested) : '';
|
|
152
|
+
if (!target && root) target = path.resolve(root, requested);
|
|
124
153
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
let target = path.isAbsolute(declared) ? path.resolve(declared) : '';
|
|
128
|
-
if (!target && cwd) target = path.resolve(cwd, declared);
|
|
154
|
+
// 工作区里的文件直接放行,一次历史都不读 —— 这是正文里点的链接走的那条路。
|
|
155
|
+
if (target && isInsideWorkspace(root, target)) return { ok: true, target };
|
|
129
156
|
|
|
130
|
-
// 工作区里的文件直接放行,一次历史都不读 —— 这是正文里点的链接走的那条路。
|
|
131
|
-
if (!target || !isInsideWorkspace(cwd, target)) {
|
|
132
157
|
// 工作区之外只剩交付声明这一条口子(绝对路径、或 cwd 变了的老会话)。
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
} catch (error) {
|
|
137
|
-
return sendJson(res, 502, {
|
|
138
|
-
ok: false,
|
|
139
|
-
error: { code: 'history-failed', message: String((error && error.message) || error) },
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
if (!declaredPaths.has(declared)) {
|
|
143
|
-
return sendJson(res, 403, {
|
|
158
|
+
const declaredSet = await declared();
|
|
159
|
+
if (!declaredSet.has(requested)) {
|
|
160
|
+
return {
|
|
144
161
|
ok: false,
|
|
145
|
-
|
|
146
|
-
|
|
162
|
+
status: 403,
|
|
163
|
+
code: 'outside-workspace',
|
|
164
|
+
message: '这个路径不在本会话的工作区里,手机上打不开',
|
|
165
|
+
};
|
|
147
166
|
}
|
|
148
167
|
if (!target) {
|
|
149
|
-
return
|
|
168
|
+
return {
|
|
150
169
|
ok: false,
|
|
151
|
-
|
|
152
|
-
|
|
170
|
+
status: 409,
|
|
171
|
+
code: 'no-cwd',
|
|
172
|
+
message: '这条会话没有工作区目录,没法解析相对路径',
|
|
173
|
+
};
|
|
153
174
|
}
|
|
175
|
+
return { ok: true, target };
|
|
154
176
|
}
|
|
155
177
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
178
|
+
return { cwd, resolve };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 两条取文件路由共用的响应实现:明文 / 分块信封、整份 / 从 `offset` 起读都在这里分岔。
|
|
183
|
+
*
|
|
184
|
+
* **为什么只有这一份**:`/session/file` 与 `/file/read` 给同一个文件必须给出同一组头、同一
|
|
185
|
+
* 种块形状、同一个起始序号口径。抄第二份漂了不会有报错,只会换来「手机上文件打不开」。
|
|
186
|
+
*
|
|
187
|
+
* `offset` 的口径见 `docs/streaming-media.md` §1:缺省 0、必须是 `CHUNK` 的整数倍;越界回
|
|
188
|
+
* `bad-range`(明文按 HTTP 416,加密档 400);`offset === size` 合法,回一个空的尾块。
|
|
189
|
+
*
|
|
190
|
+
* @param {object} req node:http IncomingMessage
|
|
191
|
+
* @param {object} res node:http ServerResponse
|
|
192
|
+
* @param {object} file
|
|
193
|
+
* @param {string} file.target 盘上的文件路径
|
|
194
|
+
* @param {number} file.size 明文总长
|
|
195
|
+
* @param {object} file.header 公共头(content-type / content-disposition / cache-control)
|
|
196
|
+
* @param {unknown} file.rawOffset 请求体里的 `offset` 原值;没给就是缺省
|
|
197
|
+
*/
|
|
198
|
+
async function sendFileBytes(req, res, { target, size, header, rawOffset }) {
|
|
199
|
+
const hasOffset = rawOffset !== undefined && rawOffset !== null;
|
|
200
|
+
const offset = hasOffset ? rawOffset : 0;
|
|
201
|
+
// 对齐是客户端的责任,这一层只如实拒收:不对齐的段起点在块边界上落不下来,硬读会让
|
|
202
|
+
// 第 n 块的 AAD 序号与字节位置对不上。
|
|
203
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset % FILE_CHUNK_BYTES !== 0) {
|
|
204
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-offset', message: '偏移没有对齐' } });
|
|
164
205
|
}
|
|
165
|
-
|
|
166
|
-
|
|
206
|
+
|
|
207
|
+
const opened = req && req.__dshE2ee;
|
|
208
|
+
const encrypted = Boolean(opened && opened.keys && opened.req);
|
|
209
|
+
// 越界:加密档按契约回 400,明文档按 HTTP 的规矩回 416,体是同一份 JSON。
|
|
210
|
+
if (offset > size) {
|
|
211
|
+
return sendJson(res, encrypted ? 400 : 416, {
|
|
167
212
|
ok: false,
|
|
168
|
-
error: { code: '
|
|
213
|
+
error: { code: 'bad-range', message: '偏移超过了文件大小' },
|
|
169
214
|
});
|
|
170
215
|
}
|
|
171
216
|
|
|
172
|
-
|
|
173
|
-
const header = {
|
|
174
|
-
'content-type': mediaTypeOf(target),
|
|
175
|
-
'content-disposition': dispositionOf(name),
|
|
176
|
-
'cache-control': 'no-store',
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
// 协议 v2:分块信封(§8.3(6))。
|
|
180
|
-
//
|
|
181
|
-
// 套一次性 AEAD 意味着宿主得先把整份读进内存 —— 单文件上限 512MB,那条路不能走。
|
|
182
|
-
// 所以改成固定分块流式写:宿主仍然边读边发,内存占用还是一块的大小;App 那边按长度字段
|
|
183
|
-
// 切块、逐块解即可,不需要流式解密栈。
|
|
184
|
-
const opened = req && req.__dshE2ee;
|
|
185
|
-
if (opened && opened.keys && opened.req) {
|
|
217
|
+
if (encrypted) {
|
|
186
218
|
res.writeHead(200, {
|
|
187
219
|
...header,
|
|
188
|
-
'content-type':
|
|
220
|
+
'content-type': E2EE_CHUNKS_TYPE,
|
|
189
221
|
// **给长度,不发分块**(2026-10-30):加密后这份响应有多长是算得出来的(见
|
|
190
222
|
// `encryptedLengthOf`),而不给长度的代价很具体 —— 客户端只能「流到哪儿算哪儿」地判
|
|
191
223
|
// 收没收完,Android 那个原生下载器更是按「实收字节数 == content-length」判,
|
|
192
224
|
// 分块响应在它那儿一律判成 `Download interrupted.`,手机上就是「点开文件说网络断了」。
|
|
193
225
|
// 长度顺带还是这份分块信封自己的完整性凭据:被截断的字节数一定对不上。
|
|
194
|
-
'content-length': String(encryptedLengthOf(
|
|
226
|
+
'content-length': String(encryptedLengthOf(size - offset)),
|
|
195
227
|
// 原文件的字节数:中转有时会把 `content-length` 吞掉(改成分块),而客户端要靠它
|
|
196
228
|
// 判「这份到底收全没有」—— 分块信封自己的认证判不出「正好在块边界被截断」那种。
|
|
197
|
-
'x-dsh-file-size': String(
|
|
229
|
+
'x-dsh-file-size': String(size),
|
|
198
230
|
'x-dsh-proto': '2',
|
|
231
|
+
// 这一段是从明文的哪个字节开始的:客户端靠它对 `seqStart`,也是「老插件不认识 offset」
|
|
232
|
+
// 的探测点(缺了这个头就退回整份下完再开)。
|
|
233
|
+
'x-dsh-range-start': String(offset),
|
|
199
234
|
// 真实类型单独给:App 解密之后要拿它决定怎么预览。
|
|
200
235
|
'x-dsh-content-type': header['content-type'],
|
|
201
236
|
});
|
|
202
|
-
await writeEncryptedFile(req, res, target, opened);
|
|
237
|
+
await writeEncryptedFile(req, res, target, opened, offset);
|
|
203
238
|
return;
|
|
204
239
|
}
|
|
205
240
|
|
|
206
|
-
|
|
207
|
-
if (
|
|
241
|
+
// 明文:不带 `offset` 时与今天完全一致(200、全量);带了就是 206 + content-range。
|
|
242
|
+
if (!hasOffset) {
|
|
243
|
+
res.writeHead(200, { ...header, 'content-length': String(size) });
|
|
244
|
+
if (size === 0) return res.end();
|
|
245
|
+
await pipePlain(target, res, 0, size);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
208
248
|
|
|
209
|
-
|
|
249
|
+
// `offset === size`:已经到尾巴了,回 206 + 空体加 `bytes */size`,客户端据此确认
|
|
250
|
+
// 「到这里就是头」—— 这不是 416,416 留给越界。
|
|
251
|
+
if (offset === size) {
|
|
252
|
+
res.writeHead(206, {
|
|
253
|
+
...header,
|
|
254
|
+
'content-range': `bytes */${size}`,
|
|
255
|
+
'content-length': '0',
|
|
256
|
+
});
|
|
257
|
+
return res.end();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
res.writeHead(206, {
|
|
261
|
+
...header,
|
|
262
|
+
'content-range': `bytes ${offset}-${size - 1}/${size}`,
|
|
263
|
+
'content-length': String(size - offset),
|
|
264
|
+
});
|
|
265
|
+
await pipePlain(target, res, offset, size - offset);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 明文档的字节流:从 `offset` 起读 `length` 字节写进 `res`。
|
|
270
|
+
*
|
|
271
|
+
* 只吃长度确定的区间,失败时只能断开(头已经发出去了,改不了状态码)。
|
|
272
|
+
*/
|
|
273
|
+
async function pipePlain(target, res, offset, length) {
|
|
274
|
+
const stream = fs.createReadStream(target, { start: offset, end: offset + length - 1 });
|
|
210
275
|
res.on('close', () => stream.destroy());
|
|
211
276
|
try {
|
|
212
277
|
await pipeline(stream, res);
|
|
213
278
|
} catch {
|
|
214
|
-
//
|
|
279
|
+
// 已经写出去了一部分,只能断开让对端按截断处理(与会话日志导出一致)。
|
|
215
280
|
try { res.destroy(); } catch { /* 连接已关闭 */ }
|
|
216
281
|
}
|
|
217
282
|
}
|
|
218
283
|
|
|
284
|
+
/**
|
|
285
|
+
* POST { sessionId, path, offset? } → 该文件的原始字节。
|
|
286
|
+
*
|
|
287
|
+
* @param {object} ctx 插件上下文
|
|
288
|
+
* @param {object} req node:http IncomingMessage
|
|
289
|
+
* @param {object} res node:http ServerResponse
|
|
290
|
+
*/
|
|
291
|
+
async function handleSessionFile(ctx, req, res) {
|
|
292
|
+
const body = await readJsonBody(req);
|
|
293
|
+
const sessionId = body && typeof body.sessionId === 'string' ? body.sessionId.trim() : '';
|
|
294
|
+
const declared = body && typeof body.path === 'string' ? body.path : '';
|
|
295
|
+
if (!sessionId || !declared) {
|
|
296
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { sessionId, path }' } });
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const scope = createSessionFileScope(ctx, sessionId);
|
|
300
|
+
let decided;
|
|
301
|
+
try {
|
|
302
|
+
decided = await scope.resolve(declared);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
return sendJson(res, 502, {
|
|
305
|
+
ok: false,
|
|
306
|
+
error: { code: 'history-failed', message: String((error && error.message) || error) },
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
if (!decided.ok) {
|
|
310
|
+
return sendJson(res, decided.status, { ok: false, error: { code: decided.code, message: decided.message } });
|
|
311
|
+
}
|
|
312
|
+
const target = decided.target;
|
|
313
|
+
|
|
314
|
+
let info;
|
|
315
|
+
try {
|
|
316
|
+
info = await stat(target);
|
|
317
|
+
} catch {
|
|
318
|
+
return sendJson(res, 404, { ok: false, error: { code: 'missing', message: '文件已经不在这台电脑上了' } });
|
|
319
|
+
}
|
|
320
|
+
if (!info.isFile()) {
|
|
321
|
+
return sendJson(res, 400, { ok: false, error: { code: 'not-a-file', message: '这条路径不是一个文件' } });
|
|
322
|
+
}
|
|
323
|
+
if (info.size > SESSION_FILE_MAX_BYTES) {
|
|
324
|
+
return sendJson(res, 413, {
|
|
325
|
+
ok: false,
|
|
326
|
+
error: { code: 'too-large', message: '这个文件太大,手机这边不支持整份取回' },
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const name = path.basename(target);
|
|
331
|
+
await sendFileBytes(req, res, {
|
|
332
|
+
target,
|
|
333
|
+
size: info.size,
|
|
334
|
+
header: {
|
|
335
|
+
'content-type': mediaTypeOf(target),
|
|
336
|
+
'content-disposition': dispositionOf(name),
|
|
337
|
+
'cache-control': 'no-store',
|
|
338
|
+
},
|
|
339
|
+
rawOffset: body && body.offset,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
219
343
|
/**
|
|
220
344
|
* 按块加密写出去(§8.3(6))。
|
|
221
345
|
*
|
|
@@ -224,39 +348,39 @@ async function handleSessionFile(ctx, req, res) {
|
|
|
224
348
|
*
|
|
225
349
|
* AAD 绑这条请求的路径、请求 nonce 与块序号 —— 块序号让中转不能把别的文件(或这一份的
|
|
226
350
|
* 别的块)的字节搬进来,也不能重排或截断而让 App 察觉不到。
|
|
351
|
+
*
|
|
352
|
+
* 只吃一个 Readable:文件那条路给它一个读文件流,产出清单的打包给它一个读临时 zip 的流。
|
|
353
|
+
* **压缩之后才知道有多长**,所以那条路不能直接对着 `res` 写 —— 得先落到临时文件、
|
|
354
|
+
* 拿到确定大小、再把临时文件当 source 喂进来。
|
|
355
|
+
*
|
|
356
|
+
* @param {object} req node:http IncomingMessage(`__dshE2ee` 已由守卫挂好)
|
|
357
|
+
* @param {object} res node:http ServerResponse(调用方已经 `writeHead(200)`)
|
|
358
|
+
* @param {import('node:stream').Readable} source 明文字节流
|
|
359
|
+
* @param {object} [opened] 信封材料;缺省取 `req.__dshE2ee`(真机上守卫挂的那一份)。
|
|
360
|
+
* 长度不在这里收:`content-length` 是调用点按 `encryptedLengthOf(size)` 算的,这一层只负责
|
|
361
|
+
* 把块写出去 —— 再收一份大小只会多一个「谁才算数」的问题。
|
|
362
|
+
* @param {number} [startOffset] 本次第一块的明文偏移(从文件中间取一段时用);块序号由它推出
|
|
363
|
+
* `startOffset / FILE_CHUNK_BYTES + 1`,与契约 §1 的 AAD 口径一致。
|
|
227
364
|
*/
|
|
228
|
-
async function
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
seq += 1;
|
|
248
|
-
writeChunk(res, opened.keys.down, block, e2ee.fileAad(path0, requestNonce, seq));
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
// 尾块(可能不足一块);空文件也给一块,否则 App 拿不到「这是个空文件」的确认。
|
|
252
|
-
if (!closed) {
|
|
253
|
-
seq += 1;
|
|
254
|
-
writeChunk(res, opened.keys.down, pending, e2ee.fileAad(path0, requestNonce, seq));
|
|
255
|
-
}
|
|
256
|
-
if (!closed) res.end();
|
|
257
|
-
} catch {
|
|
258
|
-
try { res.destroy(); } catch { /* 连接已关闭 */ }
|
|
259
|
-
}
|
|
365
|
+
async function writeEncryptedStream(req, res, source, opened = req && req.__dshE2ee, startOffset = 0) {
|
|
366
|
+
await pipeEncrypted(req, res, source, { opened, startOffset });
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 一个文件按块加密写出去:`writeEncryptedStream` 的薄封装。
|
|
371
|
+
*
|
|
372
|
+
* @param {object} req node:http IncomingMessage
|
|
373
|
+
* @param {object} res node:http ServerResponse
|
|
374
|
+
* @param {string} target 盘上的文件路径
|
|
375
|
+
* @param {object} opened `req.__dshE2ee`
|
|
376
|
+
* @param {number} [startOffset] 从明文的这个偏移起读(块序号跟着推,见 `writeEncryptedStream`)
|
|
377
|
+
*/
|
|
378
|
+
async function writeEncryptedFile(req, res, target, opened, startOffset = 0) {
|
|
379
|
+
const stream = fs.createReadStream(target, {
|
|
380
|
+
start: startOffset,
|
|
381
|
+
highWaterMark: FILE_CHUNK_BYTES,
|
|
382
|
+
});
|
|
383
|
+
await writeEncryptedStream(req, res, stream, opened, startOffset);
|
|
260
384
|
}
|
|
261
385
|
|
|
262
386
|
/** 一块除明文字节之外还带多少:`[4 字节长度][12 字节 nonce][16 字节 tag]`。 */
|
|
@@ -276,8 +400,16 @@ function encryptedLengthOf(size) {
|
|
|
276
400
|
|
|
277
401
|
module.exports = {
|
|
278
402
|
handleSessionFile,
|
|
403
|
+
// 两条取文件路由共用的响应实现(明文 / 分块信封、整份 / 带 offset 的一段都走它)。
|
|
404
|
+
sendFileBytes,
|
|
279
405
|
SESSION_FILE_MAX_BYTES,
|
|
280
406
|
FILE_CHUNK_BYTES,
|
|
281
407
|
writeEncryptedFile,
|
|
408
|
+
writeEncryptedStream,
|
|
282
409
|
encryptedLengthOf,
|
|
410
|
+
// 放行判定的唯一实现:`/session/file`、产出清单 stat 与打包都用这一份作用域。
|
|
411
|
+
createSessionFileScope,
|
|
412
|
+
isInsideWorkspace,
|
|
413
|
+
// 下载文件名的口径(RFC 5987 的 `filename*`):打包那条沿用同一份,别另写一个。
|
|
414
|
+
dispositionOf,
|
|
283
415
|
};
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
* 上游历史接口不认 afterSeq(只认 beforeSeq/maxMessages),所以增量轮询在插件内用较大的
|
|
12
12
|
* 窗口(Math.max(limit, 200))拉最近若干事件后再按 seq 过滤;缺口超过窗口时会漏,此时
|
|
13
13
|
* 用 resyncRequired 让客户端回落到权威快照,而不是安静地少消息。
|
|
14
|
+
*
|
|
15
|
+
* 折叠出来的消息在这里顺手补上交付文件的 size / mtime(见 presented-file-stats.js):
|
|
16
|
+
* 轮尾卡的第二行要写「1.2 MB · 今天 14:32」,而电脑这边本来就知道文件在哪。这一步**只量**、
|
|
17
|
+
* 且有总预算与并发上限 —— `/session/messages` 是开屏与轮询的主路径,量不到就不带。
|
|
14
18
|
*/
|
|
15
19
|
|
|
16
20
|
const { sendJson } = require('../shared/http-util');
|
|
@@ -20,6 +24,7 @@ const { deriveMessages, thinEntryMessages, toolResultPatchesFor } = require('./h
|
|
|
20
24
|
const { resolveEventAttachments } = require('./attachments.js');
|
|
21
25
|
const { createTextImageBudget } = require('./local-images.js');
|
|
22
26
|
const { entryWindowPayload } = require('./entry-window.js');
|
|
27
|
+
const { withPresentedFileStats } = require('./presented-file-stats.js');
|
|
23
28
|
|
|
24
29
|
const MESSAGES_DEFAULT_LIMIT = 50, TOOL_LOOKBACK_EVENTS = 120; // 默认条数 / 倒着翻页时多看几条(只为认出这一轮的工具结果)
|
|
25
30
|
|
|
@@ -64,9 +69,14 @@ async function buildMessagesSnapshot(ctx, sessionId, limit = MESSAGES_DEFAULT_LI
|
|
|
64
69
|
const budget = createTextImageBudget();
|
|
65
70
|
const resolvedEvents = await Promise.all(events.map((entry) => resolveEventAttachments(ctx, sessionId, entry.event || entry, { budget, referencesOnly: true }).then((event) => entry.event ? { ...entry, event } : event)));
|
|
66
71
|
const messages = deriveMessages(resolvedEvents, sessionId);
|
|
72
|
+
// 交付声明的 size/mtime 在**投影之后**补:`deriveMessages` 的结果(含缓存那份)保持只读,
|
|
73
|
+
// 补齐交出的是一份新数组。顺序是安全的 —— 同指纹的第二次调用直接在 `derived-cache` 里
|
|
74
|
+
// 命中并交回同一个数组引用,`withPresentedFileStats` 按那个引用记了自己的 WeakMap,
|
|
75
|
+
// 于是第二次不会再 stat 一次(见 presented-file-stats.js)。
|
|
76
|
+
const withStats = await withPresentedFileStats(ctx, sessionId, messages);
|
|
67
77
|
const lastSeq = events.length ? (eventSeqOf(events[events.length - 1]) ?? 0) : 0;
|
|
68
78
|
const hasMore = !!(value && value.hasMore);
|
|
69
|
-
return { ok: true, items:
|
|
79
|
+
return { ok: true, items: withStats, lastSeq, hasMore };
|
|
70
80
|
}
|
|
71
81
|
|
|
72
82
|
/** GET /api/mobile/v1/session/messages?sessionId=…&beforeSeq=…&afterSeq=…&limit=…(token) */
|
|
@@ -143,14 +153,19 @@ async function handleMessages(ctx, req, res) {
|
|
|
143
153
|
.then((resolved) => (entry.event ? { ...entry, event: resolved } : resolved));
|
|
144
154
|
}));
|
|
145
155
|
const messages = deriveMessages(resolvedEvents, sessionId, lookback);
|
|
156
|
+
// 交付声明的 size/mtime 在这里补一格(见 presented-file-stats.js):只在出现声明的路径上
|
|
157
|
+
// 做、并发封 8、整批有总预算,超时或失败就当没补 —— `/session/messages` 是开屏与轮询的
|
|
158
|
+
// 主路径,那两格字不许把整页拖住。`deriveMessages` 的缓存顺序:同一指纹第二次进来直接
|
|
159
|
+
// 在 `derived-cache` 命中、交回同一个数组引用,本函数按引用记了 WeakMap,不再 stat 一次。
|
|
160
|
+
const enriched = await withPresentedFileStats(ctx, sessionId, messages);
|
|
146
161
|
const hasMore = !!(value && value.hasMore);
|
|
147
162
|
const items = wantThin
|
|
148
|
-
? thinEntryMessages(
|
|
163
|
+
? thinEntryMessages(enriched, { hasMore })
|
|
149
164
|
// 结构行(压缩 / 命令行)照发:它们由整段生命周期折出来,一次也就一两条,按 id 覆盖
|
|
150
165
|
// 正好把屏幕上已经歪掉的那一行修回来 —— 按游标筛掉就永远修不回来。
|
|
151
166
|
: (afterSeq === null
|
|
152
|
-
?
|
|
153
|
-
:
|
|
167
|
+
? enriched
|
|
168
|
+
: enriched.filter((item) => item.kind === 'compaction' || item.kind === 'command'
|
|
154
169
|
|| (Number.isSafeInteger(item.seq) && item.seq > afterSeq)));
|
|
155
170
|
// 工具结果不产生消息行,但会把**缓存里已有的**工具卡从 running 补成最终态。补丁取自
|
|
156
171
|
// 整个窗口(调用方游标可能已越过结果 seq);已随消息块下发的只回状态(见 toolResultPatchesFor)。
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 会话级「启用智能体团队协作」的写路由。
|
|
5
|
+
*
|
|
6
|
+
* 这个开关不是模型目录里的附加选项:它落在**这条会话自己的日志**里(宿主
|
|
7
|
+
* `agentTeams.setEnabled` 往根会话追加一条 `team/mode` 事件),新会话默认关。
|
|
8
|
+
* 桌面端把它画在 composer 的模型菜单里,手机要跟着开同一件事,就不能只读投影。
|
|
9
|
+
*
|
|
10
|
+
* 三条口径写在下面,别顺手改成别的样子:
|
|
11
|
+
* 1. **只有根会话能改**:宿主对子会话一律回 `TEAM_LEAD_REQUIRED`,这里不提前拦、
|
|
12
|
+
* 让宿主自己判 —— 手机判断根不根依赖本地视图,判错了就是「界面上能点、点上没反应」。
|
|
13
|
+
* 2. **读仍然是投影**:当前值从 `/session/stats` 的 `agentTeam.enabled` 来,这条路由只写。
|
|
14
|
+
* 统计投影那条路本来就不带缓存(每次现读宿主 projections),所以写完立刻再读一次
|
|
15
|
+
* 就能看到新状态,不需要在这里作废什么。
|
|
16
|
+
* 3. **宿主没装团队插件是常态**:那种宿主没有 `agentTeams` 服务,网关会回
|
|
17
|
+
* `host-unavailable`;如实回报,界面据此说「这台电脑没开团队」,不要假装成功。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const { sendJson, readJsonBody } = require('../shared/http-util');
|
|
21
|
+
const { unwrap } = require('./envelope.js');
|
|
22
|
+
|
|
23
|
+
/** 调宿主 apiProxy.agentTeams.setEnabled(见 host-bridge/endpoints.js 里那一条)。 */
|
|
24
|
+
function callAgentTeams(ctx, payload) {
|
|
25
|
+
return ctx.apiProxy.agentTeams.setEnabled({ rpcId: 'mobile', method: 'agentTeams.setEnabled', payload });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 这台电脑装没装「智能体团队」:宿主有没有注册 `agentTeams` 服务。
|
|
30
|
+
*
|
|
31
|
+
* 挂在 `/status` 心跳上,是因为**新对话页没有会话号**:那一页读不到 `/session/stats` 的
|
|
32
|
+
* `agentTeam` 投影,判断「那一行画不画」在真会话上靠投影、在临时页上只能靠这一格。
|
|
33
|
+
* 不给这一格的话,用户新建对话时那一行会整个消失、进会话才冒出来(看着像坏了)。
|
|
34
|
+
*
|
|
35
|
+
* 同进程直查 `ctx.get`(与 schedule / jobController 那几条同一套):服务不在时只让这一格
|
|
36
|
+
* 变成 false,跑不进任何装配代码。拿不到 ctx 也按「没有」处理 —— 那是异常路径,不是能力。
|
|
37
|
+
*/
|
|
38
|
+
function hasAgentTeams(ctx) {
|
|
39
|
+
try {
|
|
40
|
+
if (!ctx || typeof ctx.get !== 'function') return false;
|
|
41
|
+
return Boolean(ctx.get('agentTeams'));
|
|
42
|
+
} catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** POST /api/mobile/v1/session/team-mode(token){ sessionId, enabled } */
|
|
48
|
+
async function handleSessionTeam(ctx, req, res) {
|
|
49
|
+
const body = await readJsonBody(req, 64 * 1024);
|
|
50
|
+
if (!body || typeof body.sessionId !== 'string' || !body.sessionId ||
|
|
51
|
+
typeof body.enabled !== 'boolean') {
|
|
52
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { sessionId, enabled }' } });
|
|
53
|
+
}
|
|
54
|
+
const { value, error } = unwrap(await callAgentTeams(ctx, { sessionId: body.sessionId, enabled: body.enabled }));
|
|
55
|
+
if (error) return sendJson(res, 400, { ok: false, error });
|
|
56
|
+
sendJson(res, 200, { ok: true, enabled: body.enabled, ...(value || {}) });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = { callAgentTeams, handleSessionTeam, hasAgentTeams };
|
|
@@ -19,9 +19,10 @@
|
|
|
19
19
|
* (不叠加,见 `stats-ledger.resetSessionUsage`),再拿「宿主累计 − 已折叠量」得差额,把翻
|
|
20
20
|
* 不到的更老那段记进记录的 `history`(没有日期:只进累计与 `all` 档,不进按天账、不进热力)。
|
|
21
21
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* 采集分三条路进来:宿主定期(`PERIODIC_INTERVAL_MS` + `PERIODIC_BUDGET_MS`,只读真有变动的
|
|
23
|
+
* 会话,**这是主路**)、统计页 / 下拉刷新顺带叫醒的一轮(`DEFAULT_BUDGET_MS`,回包不等它)、
|
|
24
|
+
* 删除前补采(`DELETING_COLLECT_BUDGET_MS`)。定期那一轮标成 `periodic`:只要有短预算的请求
|
|
25
|
+
* 那一轮要进来,它就在检查点收手(每条会话开始前、以及翻页途中各问一次),手机不排队。
|
|
25
26
|
*
|
|
26
27
|
* 模型维度(`book.models`)现在按 `request/header` 的真实归因记:同一个 step 的 header
|
|
27
28
|
* 出现在该 step 的 `assistant/message` 之前,按事件顺序维护「当前模型」即可。窗口开头
|
|
@@ -59,11 +60,11 @@ const CONCURRENCY = 4;
|
|
|
59
60
|
const RECENT_WINDOW_MS = 7 * 86400_000;
|
|
60
61
|
|
|
61
62
|
/**
|
|
62
|
-
* 一次采集的时间预算(统计页 /
|
|
63
|
+
* 一次采集的时间预算(统计页 / 下拉刷新那一发顺带叫醒的那一轮)。
|
|
63
64
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
65
|
+
* **回包不等它**(见 `stats.js` 的 `wakeCollect`):请求拿到的是账本现状,采集在后台往前挪
|
|
66
|
+
* 一点算一点。所以这里不需要为「一条会话都没读完」负责 —— 事件游标是接着走的,采一半不会
|
|
67
|
+
* 算错,下一轮(同一页面的下一次刷新,或宿主那轮 5 分钟的定期)接着采。
|
|
67
68
|
*/
|
|
68
69
|
const DEFAULT_BUDGET_MS = 1500;
|
|
69
70
|
|
|
@@ -96,6 +97,18 @@ const HISTORY_FOLD_WINDOW = 400;
|
|
|
96
97
|
|
|
97
98
|
/** 补断层时最多用 `beforeSeq` 往旧翻几页:防止一条病态长会话把一轮预算吃干。 */
|
|
98
99
|
const HISTORY_PAGE_LIMIT = 20;
|
|
100
|
+
/**
|
|
101
|
+
* 定期采集一轮里,单条会话最多往旧翻几页。
|
|
102
|
+
*
|
|
103
|
+
* 首次采集(没有游标)会把每条会话都当成重算,最多翻 `HISTORY_PAGE_LIMIT` 页、每页 400 条;
|
|
104
|
+
* 64 条会话的机器上,这就是几千次宿主历史读,而它跑在宿主自己的定时器里(25 秒预算、
|
|
105
|
+
* 每 5 分钟一轮),装好插件之后能连着好几分钟把宿主压住 —— issue #12 里那台就是被这一段
|
|
106
|
+
* 叠上隧道抖动压到 400% 的。
|
|
107
|
+
*
|
|
108
|
+
* 定期那一轮不需要一次翻到底:游标会记住读到哪儿,下一轮接着往前翻,几轮之后自然收敛。
|
|
109
|
+
* 真有人打开统计页(短预算轮)仍用完整的上限,那一刻用户在看数字,值得为它多翻。
|
|
110
|
+
*/
|
|
111
|
+
const PERIODIC_PAGE_LIMIT = 3;
|
|
99
112
|
|
|
100
113
|
const num = ledger.num;
|
|
101
114
|
const billedOf = ledger.billedOf;
|
|
@@ -555,12 +568,15 @@ function newestSeqOf(events) {
|
|
|
555
568
|
|
|
556
569
|
/**
|
|
557
570
|
* 读够折叠需要的事件:先读尾部 `HISTORY_FOLD_WINDOW` 条;`lastSeq` 在窗口之外(跨了一大段,
|
|
558
|
-
* 或首采要尽量往前翻)就用 `beforeSeq` 往旧翻,最多 `
|
|
559
|
-
*
|
|
571
|
+
* 或首采要尽量往前翻)就用 `beforeSeq` 往旧翻,最多 `pageLimit` 页、且不越过本轮预算。
|
|
572
|
+
* 按 seq 去重后返回,附带 `oldest` 与「按预算/上限停手时是否还有更老的没读」(truncated)。
|
|
573
|
+
*
|
|
574
|
+
* `pageLimit` 缺省是 `HISTORY_PAGE_LIMIT`(有人在等数字时的那一档);定期采集传
|
|
575
|
+
* `PERIODIC_PAGE_LIMIT`,靠游标一轮轮往前补齐。
|
|
560
576
|
*
|
|
561
577
|
* @returns {{events: object[], oldest: number|null, truncated: boolean}|null}
|
|
562
578
|
*/
|
|
563
|
-
async function readHistoryWindow(ctx, sessionId, lastSeq, deadline) {
|
|
579
|
+
async function readHistoryWindow(ctx, sessionId, lastSeq, deadline, shouldYield, pageLimit = HISTORY_PAGE_LIMIT) {
|
|
564
580
|
const first = await readHistoryPage(ctx, sessionId, null, HISTORY_FOLD_WINDOW);
|
|
565
581
|
if (first === null) return null;
|
|
566
582
|
let events = first.events.slice();
|
|
@@ -572,8 +588,11 @@ async function readHistoryWindow(ctx, sessionId, lastSeq, deadline) {
|
|
|
572
588
|
// 什么时候还要往旧翻:首采/重算时尽量往前翻(hasMore 就在翻);增量时翻到盖住 lastSeq 为止
|
|
573
589
|
// —— 窗口是连续的尾部,最老序号 <= lastSeq 就说明 seq > lastSeq 的全在手里了。
|
|
574
590
|
const needOlder = () => (lastSeq > 0 ? oldest !== null && oldest > lastSeq : hasMore);
|
|
575
|
-
while (hasMore && oldest !== null && oldest > 0 && pages <
|
|
591
|
+
while (hasMore && oldest !== null && oldest > 0 && pages < pageLimit && needOlder()) {
|
|
576
592
|
if (Date.now() > deadline) { truncated = true; break; }
|
|
593
|
+
// 让位也要在**翻页途中**问一次:一条会话的首采能翻到二十页,只在「每条会话开始前」
|
|
594
|
+
// 看一眼的写法,等于让手机排在一条会话后面等好几秒(现场就是那次超时)。
|
|
595
|
+
if (shouldYield && shouldYield()) { truncated = true; break; }
|
|
577
596
|
const older = await readHistoryPage(ctx, sessionId, oldest, HISTORY_FOLD_WINDOW);
|
|
578
597
|
if (older === null || older.events.length === 0) break;
|
|
579
598
|
const olderOldest = oldestSeqOf(older.events);
|
|
@@ -583,7 +602,7 @@ async function readHistoryWindow(ctx, sessionId, lastSeq, deadline) {
|
|
|
583
602
|
hasMore = older.hasMore;
|
|
584
603
|
pages += 1;
|
|
585
604
|
}
|
|
586
|
-
if (hasMore && oldest !== null && oldest > 0 && needOlder() && pages >=
|
|
605
|
+
if (hasMore && oldest !== null && oldest > 0 && needOlder() && pages >= pageLimit) truncated = true;
|
|
587
606
|
|
|
588
607
|
// 按 seq 去重(分页边界可能带回同一条),保留后读到的。
|
|
589
608
|
const seen = new Set();
|
|
@@ -677,6 +696,14 @@ async function collectOnce(ctx, options = {}) {
|
|
|
677
696
|
});
|
|
678
697
|
let cursor = 0;
|
|
679
698
|
|
|
699
|
+
/**
|
|
700
|
+
* 定期那一轮什么时候该收手:有短预算轮在等就立刻让位。
|
|
701
|
+
*
|
|
702
|
+
* 除了每条会话开始前,**翻页途中也问一次**(传进 `readHistoryWindow`):一条会话的首采能
|
|
703
|
+
* 翻到 `HISTORY_PAGE_LIMIT` 页,只按会话粒度让位等于让手机等一整条会话。
|
|
704
|
+
*/
|
|
705
|
+
const shouldYield = options.periodic === true ? () => urgent > 0 : null;
|
|
706
|
+
|
|
680
707
|
const worker = async () => {
|
|
681
708
|
for (;;) {
|
|
682
709
|
const index = cursor;
|
|
@@ -733,7 +760,10 @@ async function collectOnce(ctx, options = {}) {
|
|
|
733
760
|
const previousLastSeq = record ? num(record.lastSeq) : 0;
|
|
734
761
|
|
|
735
762
|
// 读事件窗口:先读尾部,够不着游标再用 beforeSeq 往旧翻(见 readHistoryWindow)。
|
|
736
|
-
|
|
763
|
+
// 定期那一轮少翻几页(见 PERIODIC_PAGE_LIMIT):游标会记住读到哪儿,下一轮接着往前,
|
|
764
|
+
// 几轮之后自然收敛。有人在等数字的短预算轮仍用完整上限。
|
|
765
|
+
const pageLimit = options.periodic === true ? PERIODIC_PAGE_LIMIT : HISTORY_PAGE_LIMIT;
|
|
766
|
+
const window = await readHistoryWindow(ctx, sessionId, previousLastSeq, deadline, shouldYield, pageLimit);
|
|
737
767
|
if (window === null) continue;
|
|
738
768
|
|
|
739
769
|
const projections = await readProjections(ctx, sessionId);
|