dsh-mobile-ios 0.2.16 → 0.2.18
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/LICENSE +21 -21
- package/README.md +311 -284
- package/cordis.patch.yml +20 -20
- package/entries/plugin.mjs +197 -223
- package/entries/sidecar.mjs +92 -111
- package/lib/client.js +1754 -1621
- package/lib/vendor/qrcode-generator.cjs +2297 -2297
- package/lib/vendor/qrcode-utf8.cjs +26 -26
- package/package.json +3 -2
- package/src/adapter/artifacts.mjs +235 -235
- package/src/adapter/cordis-host.mjs +1444 -1225
- package/src/adapter/mock-host.mjs +498 -430
- package/src/adapter/mux-host.mjs +99 -99
- package/src/adapter/session-history.mjs +241 -241
- package/src/cloudflare.mjs +735 -698
- package/src/dns-label.mjs +32 -32
- package/src/dns-probe.mjs +110 -110
- package/src/file-log.mjs +68 -68
- package/src/gateway.mjs +285 -285
- package/src/host-name.mjs +42 -0
- package/src/pairing.mjs +300 -300
- package/src/protocol.mjs +59 -59
- package/src/provision-local.mjs +883 -560
- package/src/provision-select.mjs +105 -0
- package/src/rest.mjs +958 -894
- package/src/ring.mjs +148 -148
- package/src/server.mjs +246 -246
- package/src/tunnel-error.mjs +91 -0
- package/src/tunnel-state.mjs +122 -0
- package/src/tunnel.mjs +544 -544
- package/src/version.mjs +24 -24
- package/src/ws.mjs +332 -332
- package/src/zone-health.mjs +430 -251
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
//---------------------------------------------------------------------
|
|
2
|
-
//
|
|
3
|
-
// QR Code Generator for JavaScript UTF8 Support (optional)
|
|
4
|
-
//
|
|
5
|
-
// Copyright (c) 2011 Kazuhiko Arase
|
|
6
|
-
//
|
|
7
|
-
// URL: http://www.d-project.com/
|
|
8
|
-
//
|
|
9
|
-
// Licensed under the MIT license:
|
|
10
|
-
// http://www.opensource.org/licenses/mit-license.php
|
|
11
|
-
//
|
|
12
|
-
// The word 'QR Code' is registered trademark of
|
|
13
|
-
// DENSO WAVE INCORPORATED
|
|
14
|
-
// http://www.denso-wave.com/qrcode/faqpatent-e.html
|
|
15
|
-
//
|
|
16
|
-
//---------------------------------------------------------------------
|
|
17
|
-
|
|
18
|
-
!function(qrcode) {
|
|
19
|
-
|
|
20
|
-
//---------------------------------------------------------------------
|
|
21
|
-
// overwrite qrcode.stringToBytes
|
|
22
|
-
//---------------------------------------------------------------------
|
|
23
|
-
|
|
24
|
-
qrcode.stringToBytes = qrcode.stringToBytesFuncs['UTF-8'];
|
|
25
|
-
|
|
26
|
-
}(qrcode);
|
|
1
|
+
//---------------------------------------------------------------------
|
|
2
|
+
//
|
|
3
|
+
// QR Code Generator for JavaScript UTF8 Support (optional)
|
|
4
|
+
//
|
|
5
|
+
// Copyright (c) 2011 Kazuhiko Arase
|
|
6
|
+
//
|
|
7
|
+
// URL: http://www.d-project.com/
|
|
8
|
+
//
|
|
9
|
+
// Licensed under the MIT license:
|
|
10
|
+
// http://www.opensource.org/licenses/mit-license.php
|
|
11
|
+
//
|
|
12
|
+
// The word 'QR Code' is registered trademark of
|
|
13
|
+
// DENSO WAVE INCORPORATED
|
|
14
|
+
// http://www.denso-wave.com/qrcode/faqpatent-e.html
|
|
15
|
+
//
|
|
16
|
+
//---------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
!function(qrcode) {
|
|
19
|
+
|
|
20
|
+
//---------------------------------------------------------------------
|
|
21
|
+
// overwrite qrcode.stringToBytes
|
|
22
|
+
//---------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
qrcode.stringToBytes = qrcode.stringToBytesFuncs['UTF-8'];
|
|
25
|
+
|
|
26
|
+
}(qrcode);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mobile-ios",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH iOS移动伴侣:iPhone/iPad 扫码连接 DSH 桌面端,局域网直连 + Cloudflare Tunnel 固定域名远程两种通道",
|
|
6
6
|
"main": "entries/plugin.mjs",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"scripts": {
|
|
30
30
|
"start": "node entries/sidecar.mjs --host=mock --dev",
|
|
31
31
|
"start:lan": "node entries/sidecar.mjs --host=mock --dev --lan=true",
|
|
32
|
-
"test": "node test/selftest.mjs && node test/version.mjs && node test/lan-ip.mjs && node test/update-dialog.mjs && node test/ops.mjs && node test/resilience.mjs && node test/borrowed.mjs && node test/mux-table.mjs && node test/live-frames.mjs && node test/artifacts.mjs && node test/attachments.mjs && node test/session-actions.mjs && node test/enhance.mjs && node test/tunnel-named.mjs && node test/tar.mjs && node test/cloudflared-install.mjs && node test/install-continue.mjs && node test/dns-recheck.mjs && node test/plugin-selfupdate.mjs && node test/zone-moved.mjs"
|
|
32
|
+
"test": "node test/selftest.mjs && node test/version.mjs && node test/lan-ip.mjs && node test/update-dialog.mjs && node test/ops.mjs && node test/resilience.mjs && node test/borrowed.mjs && node test/mux-table.mjs && node test/live-frames.mjs && node test/artifacts.mjs && node test/attachments.mjs && node test/session-actions.mjs && node test/workspace-actions.mjs && node test/workspace-index.mjs && node test/enhance.mjs && node test/tunnel-named.mjs && node test/tar.mjs && node test/cloudflared-install.mjs && node test/install-continue.mjs && node test/dns-recheck.mjs && node test/tunnel-credentials.mjs && node test/plugin-selfupdate.mjs && node test/zone-moved.mjs",
|
|
33
|
+
"probe": "node spike0/probe.mjs"
|
|
33
34
|
},
|
|
34
35
|
"dsh": {
|
|
35
36
|
"bundle": {
|
|
@@ -1,235 +1,235 @@
|
|
|
1
|
-
// artifacts.mjs — 从会话事件里「如实」还原产物清单
|
|
2
|
-
//
|
|
3
|
-
// 为什么是推导而不是查询:宿主没有「会话产物」这条端点(`workspace/*` 是另一套
|
|
4
|
-
// 语义,不是会话的产出)。而会话事件里**已经有事实**:写文件的工具调用带完整
|
|
5
|
-
// 路径。把工具调用里的路径取出来,再去磁盘核对一遍,得到的就是「这个任务真的
|
|
6
|
-
// 落下了哪些文件」——不是猜的,也不是把工具名当产物。
|
|
7
|
-
//
|
|
8
|
-
// 三条不变量:
|
|
9
|
-
// 1. **只认写类工具**:Read/Glob/Grep 也带路径,但那是输入,不是产出。
|
|
10
|
-
// 2. **必须落在工作目录内**:事件里的路径可能指向系统文件,手机端不该能看到。
|
|
11
|
-
// 3. **文件必须真的存在**:任务被回滚 / 文件被删掉之后,清单要跟着消失,
|
|
12
|
-
// 不能给用户点开一个 404。
|
|
13
|
-
|
|
14
|
-
import { statSync, realpathSync } from 'node:fs';
|
|
15
|
-
import path from 'node:path';
|
|
16
|
-
|
|
17
|
-
const MAX_ARTIFACTS = 40;
|
|
18
|
-
const MAX_BYTES = 25 * 1024 * 1024;
|
|
19
|
-
|
|
20
|
-
/** 写类工具名。命中即认为它写出来的路径值得进产物清单。 */
|
|
21
|
-
const WRITE_TOOL = /(write|edit|create|save|patch|apply|replace|insert|rename|move|append|mkdir|touch|present)/i;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 真实宿主的工具调用长这样:`name=run_code`、`arguments={code:"await tools.write({file_path:\"a.md\"})"}`。
|
|
25
|
-
* 也就是说「哪个工具在写」和「写到哪里」都埋在代码字符串里,靠参数键根本取不到。
|
|
26
|
-
* 所以对这种调用,在代码文本里逐段找 `tools.<name>(` ,只有写类名字的那一段才收路径。
|
|
27
|
-
*/
|
|
28
|
-
const CODE_TOOL_CALL = /tools\.([a-zA-Z_]+)\s*\(/g;
|
|
29
|
-
|
|
30
|
-
/** 代码里的路径键:`file_path: "x"`、`path: 'x'`、`{ "path": \`x\` }` 都算。 */
|
|
31
|
-
const PATH_IN_CODE =
|
|
32
|
-
/(?:file_path|filePath|filepath|target_file|"path"|'path'|path)\s*:\s*(?:"([^"\n]{1,400})"|'([^'\n]{1,400})'|`([^`\n]{1,400})`)/g;
|
|
33
|
-
|
|
34
|
-
function collectPathsFromCode(code, out) {
|
|
35
|
-
const text = String(code ?? '');
|
|
36
|
-
if (!text) return;
|
|
37
|
-
const marks = [];
|
|
38
|
-
CODE_TOOL_CALL.lastIndex = 0;
|
|
39
|
-
for (let m = CODE_TOOL_CALL.exec(text); m; m = CODE_TOOL_CALL.exec(text)) {
|
|
40
|
-
marks.push({ name: m[1], start: m.index, bodyStart: m.index + m[0].length });
|
|
41
|
-
}
|
|
42
|
-
for (let i = 0; i < marks.length; i += 1) {
|
|
43
|
-
const { name, bodyStart } = marks[i];
|
|
44
|
-
if (!WRITE_TOOL.test(name)) continue;
|
|
45
|
-
// 一段调用最多看到下一次 tools.* 调用为止,避免把后面的读路径算进来
|
|
46
|
-
const end = i + 1 < marks.length ? marks[i + 1].start : Math.min(text.length, bodyStart + 4000);
|
|
47
|
-
const segment = text.slice(bodyStart, end);
|
|
48
|
-
PATH_IN_CODE.lastIndex = 0;
|
|
49
|
-
for (let p = PATH_IN_CODE.exec(segment); p; p = PATH_IN_CODE.exec(segment)) {
|
|
50
|
-
const candidate = (p[1] ?? p[2] ?? p[3] ?? '').trim();
|
|
51
|
-
if (candidate) out.push(candidate);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** 常见工具把路径放在这些键里;键名大小写/下划线各不相同,一律归一后比较。 */
|
|
57
|
-
const PATH_KEYS = new Set([
|
|
58
|
-
'filepath', 'path', 'targetfile', 'filename', 'file', 'notebookpath',
|
|
59
|
-
'outputpath', 'destination', 'dest', 'to',
|
|
60
|
-
]);
|
|
61
|
-
|
|
62
|
-
const IMAGE_EXT = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.heic', '.svg']);
|
|
63
|
-
const LOG_EXT = new Set(['.log', '.txt']);
|
|
64
|
-
const DIFF_EXT = new Set(['.diff', '.patch']);
|
|
65
|
-
|
|
66
|
-
const MIME = {
|
|
67
|
-
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif',
|
|
68
|
-
'.webp': 'image/webp', '.heic': 'image/heic', '.svg': 'image/svg+xml',
|
|
69
|
-
'.pdf': 'application/pdf', '.json': 'application/json', '.md': 'text/markdown',
|
|
70
|
-
'.txt': 'text/plain; charset=utf-8', '.log': 'text/plain; charset=utf-8',
|
|
71
|
-
'.html': 'text/html; charset=utf-8', '.htm': 'text/html; charset=utf-8',
|
|
72
|
-
'.css': 'text/css; charset=utf-8', '.js': 'text/javascript; charset=utf-8',
|
|
73
|
-
'.mjs': 'text/javascript; charset=utf-8', '.ts': 'text/plain; charset=utf-8',
|
|
74
|
-
'.swift': 'text/plain; charset=utf-8', '.py': 'text/plain; charset=utf-8',
|
|
75
|
-
'.yml': 'text/plain; charset=utf-8', '.yaml': 'text/plain; charset=utf-8',
|
|
76
|
-
'.csv': 'text/csv; charset=utf-8', '.zip': 'application/zip',
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const normalizeKey = (k) => String(k).toLowerCase().replace(/[_-]/g, '');
|
|
80
|
-
|
|
81
|
-
function collectPaths(value, out, depth = 0) {
|
|
82
|
-
if (depth > 4 || value === null || value === undefined) return;
|
|
83
|
-
if (typeof value === 'string') return;
|
|
84
|
-
if (Array.isArray(value)) {
|
|
85
|
-
for (const v of value) collectPaths(v, out, depth + 1);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
if (typeof value !== 'object') return;
|
|
89
|
-
for (const [key, v] of Object.entries(value)) {
|
|
90
|
-
if (typeof v === 'string' && PATH_KEYS.has(normalizeKey(key)) && v.trim()) {
|
|
91
|
-
out.push(v.trim());
|
|
92
|
-
} else if (typeof v === 'object') {
|
|
93
|
-
collectPaths(v, out, depth + 1);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function parseArguments(raw) {
|
|
99
|
-
if (!raw) return null;
|
|
100
|
-
if (typeof raw === 'object') return raw;
|
|
101
|
-
try {
|
|
102
|
-
return JSON.parse(String(raw));
|
|
103
|
-
} catch {
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** 绝对化 + 收进工作目录内;越界返回 null。 */
|
|
109
|
-
function resolveInside(cwd, candidate) {
|
|
110
|
-
const resolved = path.isAbsolute(candidate)
|
|
111
|
-
? path.normalize(candidate)
|
|
112
|
-
: path.resolve(cwd, candidate);
|
|
113
|
-
const root = cwd.endsWith(path.sep) ? cwd : cwd + path.sep;
|
|
114
|
-
return resolved.startsWith(root) ? resolved : null;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function kindOf(ext) {
|
|
118
|
-
if (IMAGE_EXT.has(ext)) return 'image';
|
|
119
|
-
if (DIFF_EXT.has(ext)) return 'diff';
|
|
120
|
-
if (LOG_EXT.has(ext)) return 'log';
|
|
121
|
-
return 'file';
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function mimeOf(name) {
|
|
125
|
-
return MIME[path.extname(name).toLowerCase()] ?? 'application/octet-stream';
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** `artifactId` = 相对工作目录的路径(base64url)。不带主机路径,不泄露目录结构。 */
|
|
129
|
-
export function encodeArtifactId(cwd, absolutePath) {
|
|
130
|
-
const rel = path.relative(cwd, absolutePath).split(path.sep).join('/');
|
|
131
|
-
return Buffer.from(rel, 'utf8').toString('base64url');
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function decodeArtifactId(cwd, artifactId) {
|
|
135
|
-
let rel;
|
|
136
|
-
try {
|
|
137
|
-
rel = Buffer.from(String(artifactId), 'base64url').toString('utf8');
|
|
138
|
-
} catch {
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
if (!rel || rel.includes('\0')) return null;
|
|
142
|
-
const resolved = resolveInside(cwd, rel);
|
|
143
|
-
if (!resolved) return null;
|
|
144
|
-
try {
|
|
145
|
-
// 符号链接可以指向工作目录之外,必须按真实路径再判一次
|
|
146
|
-
const real = realpathSync(resolved);
|
|
147
|
-
const realRoot = realpathSync(cwd);
|
|
148
|
-
const inside = real.startsWith(realRoot.endsWith(path.sep) ? realRoot : realRoot + path.sep);
|
|
149
|
-
return inside && statSync(real).isFile() ? real : null;
|
|
150
|
-
} catch {
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* 从会话事件还原产物清单。
|
|
157
|
-
*
|
|
158
|
-
* @param {Array} records 宿主事件记录(可能带 {event} 包层)
|
|
159
|
-
* @param {{cwd?: string|null, now?: number}} options
|
|
160
|
-
* @returns {{items: Array<{artifactId,name,kind,sizeBytes,mime,updatedAt}>, skipped: Record<string, number>}}
|
|
161
|
-
*/
|
|
162
|
-
export function deriveArtifacts(records = [], { cwd = null } = {}) {
|
|
163
|
-
const skipped = Object.create(null);
|
|
164
|
-
if (!cwd) return { items: [], skipped: { 'no-cwd': 1 } };
|
|
165
|
-
|
|
166
|
-
const seen = new Map(); // 绝对路径 → { at, tool }
|
|
167
|
-
for (const raw of records) {
|
|
168
|
-
const e = raw && typeof raw === 'object' && raw.event ? raw.event : raw;
|
|
169
|
-
if (!e || e.type !== 'tool/call') continue;
|
|
170
|
-
const d = e.data ?? {};
|
|
171
|
-
const tool = String(d.name ?? '');
|
|
172
|
-
const args = parseArguments(d.arguments);
|
|
173
|
-
if (!args) {
|
|
174
|
-
skipped['unparsable-arguments'] = (skipped['unparsable-arguments'] ?? 0) + 1;
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
const candidates = [];
|
|
178
|
-
if (WRITE_TOOL.test(tool)) {
|
|
179
|
-
// 结构化的写工具:路径就在参数键上(Write/Edit/present…)
|
|
180
|
-
collectPaths(args, candidates);
|
|
181
|
-
} else {
|
|
182
|
-
// 代码型工具(真实宿主的 run_code):写动作埋在 code 里。
|
|
183
|
-
// 只扫「代码类」字段——绝不能扫 Write 的 content,那里面的示例路径
|
|
184
|
-
// 只要恰好存在,就会被当成产物列出来。
|
|
185
|
-
for (const key of ['code', 'script', 'command', 'input']) {
|
|
186
|
-
if (typeof args[key] === 'string') collectPathsFromCode(args[key], candidates);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if (!candidates.length) {
|
|
190
|
-
if (WRITE_TOOL.test(tool)) skipped['no-path'] = (skipped['no-path'] ?? 0) + 1;
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
for (const candidate of candidates) {
|
|
194
|
-
const resolved = resolveInside(cwd, candidate);
|
|
195
|
-
if (!resolved) {
|
|
196
|
-
skipped['outside-cwd'] = (skipped['outside-cwd'] ?? 0) + 1;
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
let info;
|
|
200
|
-
try {
|
|
201
|
-
info = statSync(resolved);
|
|
202
|
-
} catch {
|
|
203
|
-
skipped['missing'] = (skipped['missing'] ?? 0) + 1;
|
|
204
|
-
continue;
|
|
205
|
-
}
|
|
206
|
-
if (!info.isFile()) continue;
|
|
207
|
-
if (info.size > MAX_BYTES) {
|
|
208
|
-
skipped['too-large'] = (skipped['too-large'] ?? 0) + 1;
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
const at = typeof e.time === 'number' ? e.time : 0;
|
|
212
|
-
const previous = seen.get(resolved);
|
|
213
|
-
// 同一文件被多次改写:以最后一次写入为准(产物清单是「最终结果」)
|
|
214
|
-
if (!previous || at >= previous.at) seen.set(resolved, { at, tool, size: info.size });
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const items = [...seen.entries()]
|
|
219
|
-
.sort((a, b) => b[1].at - a[1].at)
|
|
220
|
-
.slice(0, MAX_ARTIFACTS)
|
|
221
|
-
.map(([absolute, meta]) => {
|
|
222
|
-
const name = path.basename(absolute);
|
|
223
|
-
return {
|
|
224
|
-
artifactId: encodeArtifactId(cwd, absolute),
|
|
225
|
-
name,
|
|
226
|
-
kind: kindOf(path.extname(name).toLowerCase()),
|
|
227
|
-
sizeBytes: meta.size,
|
|
228
|
-
mime: mimeOf(name),
|
|
229
|
-
updatedAt: meta.at ? new Date(meta.at).toISOString() : undefined,
|
|
230
|
-
tool: meta.tool,
|
|
231
|
-
};
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
return { items, skipped };
|
|
235
|
-
}
|
|
1
|
+
// artifacts.mjs — 从会话事件里「如实」还原产物清单
|
|
2
|
+
//
|
|
3
|
+
// 为什么是推导而不是查询:宿主没有「会话产物」这条端点(`workspace/*` 是另一套
|
|
4
|
+
// 语义,不是会话的产出)。而会话事件里**已经有事实**:写文件的工具调用带完整
|
|
5
|
+
// 路径。把工具调用里的路径取出来,再去磁盘核对一遍,得到的就是「这个任务真的
|
|
6
|
+
// 落下了哪些文件」——不是猜的,也不是把工具名当产物。
|
|
7
|
+
//
|
|
8
|
+
// 三条不变量:
|
|
9
|
+
// 1. **只认写类工具**:Read/Glob/Grep 也带路径,但那是输入,不是产出。
|
|
10
|
+
// 2. **必须落在工作目录内**:事件里的路径可能指向系统文件,手机端不该能看到。
|
|
11
|
+
// 3. **文件必须真的存在**:任务被回滚 / 文件被删掉之后,清单要跟着消失,
|
|
12
|
+
// 不能给用户点开一个 404。
|
|
13
|
+
|
|
14
|
+
import { statSync, realpathSync } from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
|
|
17
|
+
const MAX_ARTIFACTS = 40;
|
|
18
|
+
const MAX_BYTES = 25 * 1024 * 1024;
|
|
19
|
+
|
|
20
|
+
/** 写类工具名。命中即认为它写出来的路径值得进产物清单。 */
|
|
21
|
+
const WRITE_TOOL = /(write|edit|create|save|patch|apply|replace|insert|rename|move|append|mkdir|touch|present)/i;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 真实宿主的工具调用长这样:`name=run_code`、`arguments={code:"await tools.write({file_path:\"a.md\"})"}`。
|
|
25
|
+
* 也就是说「哪个工具在写」和「写到哪里」都埋在代码字符串里,靠参数键根本取不到。
|
|
26
|
+
* 所以对这种调用,在代码文本里逐段找 `tools.<name>(` ,只有写类名字的那一段才收路径。
|
|
27
|
+
*/
|
|
28
|
+
const CODE_TOOL_CALL = /tools\.([a-zA-Z_]+)\s*\(/g;
|
|
29
|
+
|
|
30
|
+
/** 代码里的路径键:`file_path: "x"`、`path: 'x'`、`{ "path": \`x\` }` 都算。 */
|
|
31
|
+
const PATH_IN_CODE =
|
|
32
|
+
/(?:file_path|filePath|filepath|target_file|"path"|'path'|path)\s*:\s*(?:"([^"\n]{1,400})"|'([^'\n]{1,400})'|`([^`\n]{1,400})`)/g;
|
|
33
|
+
|
|
34
|
+
function collectPathsFromCode(code, out) {
|
|
35
|
+
const text = String(code ?? '');
|
|
36
|
+
if (!text) return;
|
|
37
|
+
const marks = [];
|
|
38
|
+
CODE_TOOL_CALL.lastIndex = 0;
|
|
39
|
+
for (let m = CODE_TOOL_CALL.exec(text); m; m = CODE_TOOL_CALL.exec(text)) {
|
|
40
|
+
marks.push({ name: m[1], start: m.index, bodyStart: m.index + m[0].length });
|
|
41
|
+
}
|
|
42
|
+
for (let i = 0; i < marks.length; i += 1) {
|
|
43
|
+
const { name, bodyStart } = marks[i];
|
|
44
|
+
if (!WRITE_TOOL.test(name)) continue;
|
|
45
|
+
// 一段调用最多看到下一次 tools.* 调用为止,避免把后面的读路径算进来
|
|
46
|
+
const end = i + 1 < marks.length ? marks[i + 1].start : Math.min(text.length, bodyStart + 4000);
|
|
47
|
+
const segment = text.slice(bodyStart, end);
|
|
48
|
+
PATH_IN_CODE.lastIndex = 0;
|
|
49
|
+
for (let p = PATH_IN_CODE.exec(segment); p; p = PATH_IN_CODE.exec(segment)) {
|
|
50
|
+
const candidate = (p[1] ?? p[2] ?? p[3] ?? '').trim();
|
|
51
|
+
if (candidate) out.push(candidate);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 常见工具把路径放在这些键里;键名大小写/下划线各不相同,一律归一后比较。 */
|
|
57
|
+
const PATH_KEYS = new Set([
|
|
58
|
+
'filepath', 'path', 'targetfile', 'filename', 'file', 'notebookpath',
|
|
59
|
+
'outputpath', 'destination', 'dest', 'to',
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
const IMAGE_EXT = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.heic', '.svg']);
|
|
63
|
+
const LOG_EXT = new Set(['.log', '.txt']);
|
|
64
|
+
const DIFF_EXT = new Set(['.diff', '.patch']);
|
|
65
|
+
|
|
66
|
+
const MIME = {
|
|
67
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif',
|
|
68
|
+
'.webp': 'image/webp', '.heic': 'image/heic', '.svg': 'image/svg+xml',
|
|
69
|
+
'.pdf': 'application/pdf', '.json': 'application/json', '.md': 'text/markdown',
|
|
70
|
+
'.txt': 'text/plain; charset=utf-8', '.log': 'text/plain; charset=utf-8',
|
|
71
|
+
'.html': 'text/html; charset=utf-8', '.htm': 'text/html; charset=utf-8',
|
|
72
|
+
'.css': 'text/css; charset=utf-8', '.js': 'text/javascript; charset=utf-8',
|
|
73
|
+
'.mjs': 'text/javascript; charset=utf-8', '.ts': 'text/plain; charset=utf-8',
|
|
74
|
+
'.swift': 'text/plain; charset=utf-8', '.py': 'text/plain; charset=utf-8',
|
|
75
|
+
'.yml': 'text/plain; charset=utf-8', '.yaml': 'text/plain; charset=utf-8',
|
|
76
|
+
'.csv': 'text/csv; charset=utf-8', '.zip': 'application/zip',
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const normalizeKey = (k) => String(k).toLowerCase().replace(/[_-]/g, '');
|
|
80
|
+
|
|
81
|
+
function collectPaths(value, out, depth = 0) {
|
|
82
|
+
if (depth > 4 || value === null || value === undefined) return;
|
|
83
|
+
if (typeof value === 'string') return;
|
|
84
|
+
if (Array.isArray(value)) {
|
|
85
|
+
for (const v of value) collectPaths(v, out, depth + 1);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (typeof value !== 'object') return;
|
|
89
|
+
for (const [key, v] of Object.entries(value)) {
|
|
90
|
+
if (typeof v === 'string' && PATH_KEYS.has(normalizeKey(key)) && v.trim()) {
|
|
91
|
+
out.push(v.trim());
|
|
92
|
+
} else if (typeof v === 'object') {
|
|
93
|
+
collectPaths(v, out, depth + 1);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function parseArguments(raw) {
|
|
99
|
+
if (!raw) return null;
|
|
100
|
+
if (typeof raw === 'object') return raw;
|
|
101
|
+
try {
|
|
102
|
+
return JSON.parse(String(raw));
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 绝对化 + 收进工作目录内;越界返回 null。 */
|
|
109
|
+
function resolveInside(cwd, candidate) {
|
|
110
|
+
const resolved = path.isAbsolute(candidate)
|
|
111
|
+
? path.normalize(candidate)
|
|
112
|
+
: path.resolve(cwd, candidate);
|
|
113
|
+
const root = cwd.endsWith(path.sep) ? cwd : cwd + path.sep;
|
|
114
|
+
return resolved.startsWith(root) ? resolved : null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function kindOf(ext) {
|
|
118
|
+
if (IMAGE_EXT.has(ext)) return 'image';
|
|
119
|
+
if (DIFF_EXT.has(ext)) return 'diff';
|
|
120
|
+
if (LOG_EXT.has(ext)) return 'log';
|
|
121
|
+
return 'file';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function mimeOf(name) {
|
|
125
|
+
return MIME[path.extname(name).toLowerCase()] ?? 'application/octet-stream';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** `artifactId` = 相对工作目录的路径(base64url)。不带主机路径,不泄露目录结构。 */
|
|
129
|
+
export function encodeArtifactId(cwd, absolutePath) {
|
|
130
|
+
const rel = path.relative(cwd, absolutePath).split(path.sep).join('/');
|
|
131
|
+
return Buffer.from(rel, 'utf8').toString('base64url');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function decodeArtifactId(cwd, artifactId) {
|
|
135
|
+
let rel;
|
|
136
|
+
try {
|
|
137
|
+
rel = Buffer.from(String(artifactId), 'base64url').toString('utf8');
|
|
138
|
+
} catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
if (!rel || rel.includes('\0')) return null;
|
|
142
|
+
const resolved = resolveInside(cwd, rel);
|
|
143
|
+
if (!resolved) return null;
|
|
144
|
+
try {
|
|
145
|
+
// 符号链接可以指向工作目录之外,必须按真实路径再判一次
|
|
146
|
+
const real = realpathSync(resolved);
|
|
147
|
+
const realRoot = realpathSync(cwd);
|
|
148
|
+
const inside = real.startsWith(realRoot.endsWith(path.sep) ? realRoot : realRoot + path.sep);
|
|
149
|
+
return inside && statSync(real).isFile() ? real : null;
|
|
150
|
+
} catch {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 从会话事件还原产物清单。
|
|
157
|
+
*
|
|
158
|
+
* @param {Array} records 宿主事件记录(可能带 {event} 包层)
|
|
159
|
+
* @param {{cwd?: string|null, now?: number}} options
|
|
160
|
+
* @returns {{items: Array<{artifactId,name,kind,sizeBytes,mime,updatedAt}>, skipped: Record<string, number>}}
|
|
161
|
+
*/
|
|
162
|
+
export function deriveArtifacts(records = [], { cwd = null } = {}) {
|
|
163
|
+
const skipped = Object.create(null);
|
|
164
|
+
if (!cwd) return { items: [], skipped: { 'no-cwd': 1 } };
|
|
165
|
+
|
|
166
|
+
const seen = new Map(); // 绝对路径 → { at, tool }
|
|
167
|
+
for (const raw of records) {
|
|
168
|
+
const e = raw && typeof raw === 'object' && raw.event ? raw.event : raw;
|
|
169
|
+
if (!e || e.type !== 'tool/call') continue;
|
|
170
|
+
const d = e.data ?? {};
|
|
171
|
+
const tool = String(d.name ?? '');
|
|
172
|
+
const args = parseArguments(d.arguments);
|
|
173
|
+
if (!args) {
|
|
174
|
+
skipped['unparsable-arguments'] = (skipped['unparsable-arguments'] ?? 0) + 1;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const candidates = [];
|
|
178
|
+
if (WRITE_TOOL.test(tool)) {
|
|
179
|
+
// 结构化的写工具:路径就在参数键上(Write/Edit/present…)
|
|
180
|
+
collectPaths(args, candidates);
|
|
181
|
+
} else {
|
|
182
|
+
// 代码型工具(真实宿主的 run_code):写动作埋在 code 里。
|
|
183
|
+
// 只扫「代码类」字段——绝不能扫 Write 的 content,那里面的示例路径
|
|
184
|
+
// 只要恰好存在,就会被当成产物列出来。
|
|
185
|
+
for (const key of ['code', 'script', 'command', 'input']) {
|
|
186
|
+
if (typeof args[key] === 'string') collectPathsFromCode(args[key], candidates);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!candidates.length) {
|
|
190
|
+
if (WRITE_TOOL.test(tool)) skipped['no-path'] = (skipped['no-path'] ?? 0) + 1;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
for (const candidate of candidates) {
|
|
194
|
+
const resolved = resolveInside(cwd, candidate);
|
|
195
|
+
if (!resolved) {
|
|
196
|
+
skipped['outside-cwd'] = (skipped['outside-cwd'] ?? 0) + 1;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
let info;
|
|
200
|
+
try {
|
|
201
|
+
info = statSync(resolved);
|
|
202
|
+
} catch {
|
|
203
|
+
skipped['missing'] = (skipped['missing'] ?? 0) + 1;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (!info.isFile()) continue;
|
|
207
|
+
if (info.size > MAX_BYTES) {
|
|
208
|
+
skipped['too-large'] = (skipped['too-large'] ?? 0) + 1;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const at = typeof e.time === 'number' ? e.time : 0;
|
|
212
|
+
const previous = seen.get(resolved);
|
|
213
|
+
// 同一文件被多次改写:以最后一次写入为准(产物清单是「最终结果」)
|
|
214
|
+
if (!previous || at >= previous.at) seen.set(resolved, { at, tool, size: info.size });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const items = [...seen.entries()]
|
|
219
|
+
.sort((a, b) => b[1].at - a[1].at)
|
|
220
|
+
.slice(0, MAX_ARTIFACTS)
|
|
221
|
+
.map(([absolute, meta]) => {
|
|
222
|
+
const name = path.basename(absolute);
|
|
223
|
+
return {
|
|
224
|
+
artifactId: encodeArtifactId(cwd, absolute),
|
|
225
|
+
name,
|
|
226
|
+
kind: kindOf(path.extname(name).toLowerCase()),
|
|
227
|
+
sizeBytes: meta.size,
|
|
228
|
+
mime: mimeOf(name),
|
|
229
|
+
updatedAt: meta.at ? new Date(meta.at).toISOString() : undefined,
|
|
230
|
+
tool: meta.tool,
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
return { items, skipped };
|
|
235
|
+
}
|