jbc-dssi 1.1.116 → 1.1.117
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/index.js +58 -15
- package/lib/protocol.js +15 -0
- package/lib/workspace-bytes.js +78 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -26,6 +26,7 @@ import { createGroupsEventBuffer, shouldBufferGroupEvent } from './lib/groups-ev
|
|
|
26
26
|
import { createScheduleEngine } from './lib/schedules.js';
|
|
27
27
|
import { createProcEngine } from './lib/proc-commands.js';
|
|
28
28
|
import { createAppReleaseWatch } from './lib/app-release-watch.js';
|
|
29
|
+
import { MAX_FILE_BYTES, mimeOfFile, resolveWorkspaceFilePath } from './lib/workspace-bytes.js';
|
|
29
30
|
|
|
30
31
|
/*
|
|
31
32
|
* ★ 协议层与瘦身层**直接引用 core**,本目录不再保有自己的副本。
|
|
@@ -152,21 +153,6 @@ const PROTOCOL_VERSION = 1;
|
|
|
152
153
|
*/
|
|
153
154
|
const MAX_IMAGE_BYTES = 2 * 1024 * 1024;
|
|
154
155
|
|
|
155
|
-
/** 按扩展名判图片类型;不是图片返回 null(这是"只读图片"那道闸门的判据)。 */
|
|
156
|
-
function mimeOfImage(p) {
|
|
157
|
-
const m = /\.([a-z0-9]+)$/i.exec(p);
|
|
158
|
-
if (!m) return null;
|
|
159
|
-
switch (m[1].toLowerCase()) {
|
|
160
|
-
case 'png': return 'image/png';
|
|
161
|
-
case 'jpg':
|
|
162
|
-
case 'jpeg': return 'image/jpeg';
|
|
163
|
-
case 'gif': return 'image/gif';
|
|
164
|
-
case 'webp': return 'image/webp';
|
|
165
|
-
case 'bmp': return 'image/bmp';
|
|
166
|
-
default: return null;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
156
|
/** 手机上没人回答时,最多等这么久就把审批交回浏览器处理。 */
|
|
171
157
|
const APPROVAL_TIMEOUT_MS = 5 * 60 * 1000;
|
|
172
158
|
|
|
@@ -2317,6 +2303,14 @@ class BridgeConnection {
|
|
|
2317
2303
|
this.#fire('read-image', this.#readImage(payload));
|
|
2318
2304
|
break;
|
|
2319
2305
|
|
|
2306
|
+
/*
|
|
2307
|
+
* 工作区文件原始字节(2026-11-15 老谢定):手机内嵌看 PDF / Excel / Word
|
|
2308
|
+
* 与「用其他应用打开」共用这一条 —— 只传字节,解析在手机侧。
|
|
2309
|
+
*/
|
|
2310
|
+
case FRAME.READ_FILE_BYTES:
|
|
2311
|
+
this.#fire('read-file-bytes', this.#readFileBytes(payload));
|
|
2312
|
+
break;
|
|
2313
|
+
|
|
2320
2314
|
case FRAME.FOCUS: {
|
|
2321
2315
|
this.focused = payload?.active === true;
|
|
2322
2316
|
/*
|
|
@@ -3464,6 +3458,55 @@ class BridgeConnection {
|
|
|
3464
3458
|
}
|
|
3465
3459
|
}
|
|
3466
3460
|
|
|
3461
|
+
/**
|
|
3462
|
+
* 读一个工作区文件的原始字节(2026-11-15 老谢定)。
|
|
3463
|
+
*
|
|
3464
|
+
* 用途一:手机内嵌看 PDF / Excel / Word —— 这三类不是文本,
|
|
3465
|
+
* `read_workspace_file` 只回元数据,解析全在手机侧(不引第三方渲染库)。
|
|
3466
|
+
* 用途二:「用其他应用打开」—— 手机把字节落缓存再交给系统的 WPS/Office。
|
|
3467
|
+
*
|
|
3468
|
+
* 安全:与 read-image 同一个思路,但更严 —— 绝对路径也必须在工作区内。
|
|
3469
|
+
*/
|
|
3470
|
+
async #readFileBytes(payload) {
|
|
3471
|
+
const raw = typeof payload?.path === 'string' ? payload.path.trim() : '';
|
|
3472
|
+
const resolved = resolveWorkspaceFilePath({
|
|
3473
|
+
workspacesDir: this.dssAgent?.workspacesDir,
|
|
3474
|
+
entityId: payload?.entityId,
|
|
3475
|
+
raw,
|
|
3476
|
+
});
|
|
3477
|
+
if (resolved.error) {
|
|
3478
|
+
this.send(FRAME.FILE_BYTES, { path: raw, error: resolved.error });
|
|
3479
|
+
return;
|
|
3480
|
+
}
|
|
3481
|
+
const abs = resolved.path;
|
|
3482
|
+
try {
|
|
3483
|
+
const st = await fs.promises.stat(abs);
|
|
3484
|
+
if (!st.isFile()) {
|
|
3485
|
+
this.send(FRAME.FILE_BYTES, { path: raw, error: '不是文件' });
|
|
3486
|
+
return;
|
|
3487
|
+
}
|
|
3488
|
+
if (st.size > MAX_FILE_BYTES) {
|
|
3489
|
+
const mb = (st.size / 1024 / 1024).toFixed(1);
|
|
3490
|
+
this.send(FRAME.FILE_BYTES, {
|
|
3491
|
+
path: raw,
|
|
3492
|
+
error: `文件太大(${mb} MB),超过 5 MB 请在电脑上打开`,
|
|
3493
|
+
});
|
|
3494
|
+
return;
|
|
3495
|
+
}
|
|
3496
|
+
const buf = await fs.promises.readFile(abs);
|
|
3497
|
+
const name = abs.split('/').pop();
|
|
3498
|
+
this.send(FRAME.FILE_BYTES, {
|
|
3499
|
+
path: raw,
|
|
3500
|
+
name,
|
|
3501
|
+
mime: mimeOfFile(name),
|
|
3502
|
+
size: st.size,
|
|
3503
|
+
data: buf.toString('base64'),
|
|
3504
|
+
});
|
|
3505
|
+
} catch (err) {
|
|
3506
|
+
this.send(FRAME.FILE_BYTES, { path: raw, error: `读取失败:${err?.message ?? err}` });
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
|
|
3467
3510
|
async #readEarlier(payload) {
|
|
3468
3511
|
const sessionId = typeof payload?.sessionId === 'string' ? payload.sessionId.trim() : '';
|
|
3469
3512
|
if (sessionId === '') {
|
package/lib/protocol.js
CHANGED
|
@@ -102,6 +102,21 @@ export const FRAME = {
|
|
|
102
102
|
* 与 HEALTH 类帧一样:业务失败不发;内容来自本机文件,不是手机可控输入。
|
|
103
103
|
*/
|
|
104
104
|
APP_RELEASE: 0x3c,
|
|
105
|
+
/**
|
|
106
|
+
* 请求一个「工作区文件」的原始字节:{ path, entityId } -> FILE_BYTES。
|
|
107
|
+
*
|
|
108
|
+
* 为什么需要它(2026-11-15 老谢定:手机上要能看 PDF / Excel / Word):
|
|
109
|
+
* 这三类都不是文本,`read_workspace_file` 只回元数据;手机要内嵌解析,
|
|
110
|
+
* 就必须先把原始字节拿到手。「用其他应用打开」同理(先落手机缓存再交系统)。
|
|
111
|
+
*
|
|
112
|
+
* 与 READ_IMAGE 的分工:
|
|
113
|
+
* · READ_IMAGE 只放行图片扩展名、上限 2 MB(看图场景);
|
|
114
|
+
* · 本条放行 PDF / Office / 其它文档,上限 5 MB(base64 后 ≈6.7 MB,在 8 MB 帧内)。
|
|
115
|
+
* ⚠️ 电脑侧必须自己校验:路径必须落在**该智能体工作区**内、拒绝 `..` 穿越。
|
|
116
|
+
*/
|
|
117
|
+
READ_FILE_BYTES: 0x3d,
|
|
118
|
+
/** 文件字节回应:{ path, name, mime, size, data(base64) };失败 { path, error }。 */
|
|
119
|
+
FILE_BYTES: 0x3e,
|
|
105
120
|
FORK_DONE: 0x35,
|
|
106
121
|
/**
|
|
107
122
|
* 团队快照(bots 模块):{ v, seq, asOf, teams:[...] }。
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工作区文件字节通道的纯逻辑(2026-11-15 老谢定:手机上能看 PDF / Excel / Word)。
|
|
3
|
+
*
|
|
4
|
+
* 抽成独立模块的理由:**路径校验是这条通道唯一的安全边界**,
|
|
5
|
+
* "只放开该智能体工作区、拒绝 `..`、绝对路径也要在界内"这三条必须能被单测钉死
|
|
6
|
+
* (见 `test/workspace-bytes.test.mjs`),不能躺在 index.js 的私有方法里靠人读。
|
|
7
|
+
*
|
|
8
|
+
* 分工:index.js 只管读文件与发帧,这里的函数不碰 fs、不碰连接。
|
|
9
|
+
*/
|
|
10
|
+
import { join, resolve, sep } from 'node:path';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 单个文件字节上限(5 MB 原文件)。
|
|
14
|
+
*
|
|
15
|
+
* 为什么是 5 MB:帧上限 8 MB,base64 再胀 1.33 倍 → 5 MB 原文件 ≈ 6.7 MB 帧;
|
|
16
|
+
* 再大就在电脑上打开 —— 一帧 8 MB 在 4G 上要十几秒,期间所有消息排在它后面。
|
|
17
|
+
*/
|
|
18
|
+
export const MAX_FILE_BYTES = 5 * 1024 * 1024;
|
|
19
|
+
|
|
20
|
+
/** 按扩展名给文档一个 MIME(「用其他应用打开」靠它挑 App);认不出给 octet-stream。 */
|
|
21
|
+
export function mimeOfFile(p) {
|
|
22
|
+
const m = /\.([a-z0-9]+)$/i.exec(String(p ?? ''));
|
|
23
|
+
if (!m) return 'application/octet-stream';
|
|
24
|
+
switch (m[1].toLowerCase()) {
|
|
25
|
+
case 'pdf': return 'application/pdf';
|
|
26
|
+
case 'doc': return 'application/msword';
|
|
27
|
+
case 'docx': return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
|
28
|
+
case 'xls': return 'application/vnd.ms-excel';
|
|
29
|
+
case 'xlsx': return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
30
|
+
case 'ppt': return 'application/vnd.ms-powerpoint';
|
|
31
|
+
case 'pptx': return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
|
|
32
|
+
case 'csv': return 'text/csv';
|
|
33
|
+
case 'txt':
|
|
34
|
+
case 'md':
|
|
35
|
+
case 'markdown': return 'text/plain';
|
|
36
|
+
case 'zip': return 'application/zip';
|
|
37
|
+
default: return 'application/octet-stream';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 把手机传来的路径解析成工作区内的绝对路径。
|
|
43
|
+
*
|
|
44
|
+
* 支持:工作区相对路径(`artifacts/x.pdf`)、工作区内的绝对路径、`file://` 形式。
|
|
45
|
+
* 拒绝:空路径、`..` 穿越、跨工作区、不合法的 entityId、电脑侧工作区未就绪。
|
|
46
|
+
*
|
|
47
|
+
* @param {object} opts
|
|
48
|
+
* @param {string} opts.workspacesDir 电脑侧工作区根(通常 `<dssAgent root>/workspaces`)
|
|
49
|
+
* @param {string} opts.entityId 智能体 id(必须是合法的简单 id)
|
|
50
|
+
* @param {string} opts.raw 手机传来的原始路径
|
|
51
|
+
* @returns {{path: string} | {error: string}}
|
|
52
|
+
*/
|
|
53
|
+
export function resolveWorkspaceFilePath({ workspacesDir, entityId, raw }) {
|
|
54
|
+
let p = String(raw ?? '').trim();
|
|
55
|
+
if (p === '') return { error: '缺少文件路径' };
|
|
56
|
+
if (p.toLowerCase().startsWith('file://')) {
|
|
57
|
+
p = p.slice('file://'.length);
|
|
58
|
+
try {
|
|
59
|
+
p = decodeURIComponent(p);
|
|
60
|
+
} catch {
|
|
61
|
+
// 解码失败按原样处理,交给后面校验
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
p = p.replace(/\\/gu, '/');
|
|
65
|
+
if (p.split('/').some((seg) => seg === '..')) return { error: '文件路径不合法' };
|
|
66
|
+
const ws = String(workspacesDir ?? '').trim();
|
|
67
|
+
if (ws === '') return { error: '电脑侧工作区还没就绪' };
|
|
68
|
+
const id = String(entityId ?? '').trim();
|
|
69
|
+
if (!/^[A-Za-z0-9-]{1,64}$/u.test(id)) {
|
|
70
|
+
return { error: '需要智能体 id(请在智能体会话里打开)' };
|
|
71
|
+
}
|
|
72
|
+
const root = resolve(ws, id);
|
|
73
|
+
const abs = p.startsWith('/') ? resolve(p) : resolve(join(root, p));
|
|
74
|
+
if (abs !== root && !abs.startsWith(`${root}${sep}`)) {
|
|
75
|
+
return { error: '只能查看这个智能体工作区里的文件' };
|
|
76
|
+
}
|
|
77
|
+
return { path: abs };
|
|
78
|
+
}
|