@eum-agent/skill-management 1.0.0 → 1.1.0
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/dist/archive-worker.js +9 -0
- package/dist/http-BcaAiHxl.js +304 -0
- package/dist/index.js +4250 -1353
- package/package.json +6 -3
- package/ui/ui.js +5099 -361
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { a as unpackZip } from "./http-BcaAiHxl.js";
|
|
2
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
3
|
+
//#region src/archive-worker.ts
|
|
4
|
+
/** Inflates a completely checked archive away from the Gateway event loop. */
|
|
5
|
+
if (!parentPort) throw new Error("archive inflation requires a background worker");
|
|
6
|
+
parentPort.postMessage(unpackZip(workerData.bytes, workerData.maximum));
|
|
7
|
+
parentPort.close();
|
|
8
|
+
//#endregion
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { chmod, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join, normalize, sep } from "node:path";
|
|
3
|
+
import { unzipSync } from "fflate";
|
|
4
|
+
//#region src/install/zip.ts
|
|
5
|
+
/**
|
|
6
|
+
* A zip archive, unpacked into a directory — refusing any entry that would
|
|
7
|
+
* land outside it and any symbolic link, and keeping each file's executable
|
|
8
|
+
* bit.
|
|
9
|
+
*
|
|
10
|
+
* `fflate` inflates; the central directory is read here, because it is the
|
|
11
|
+
* only place a zip keeps what an entry *is* (a link, a directory, a file and
|
|
12
|
+
* its mode) and `fflate` does not hand that over. The archive comment is read
|
|
13
|
+
* the same way: GitHub puts the commit an archive was cut from there.
|
|
14
|
+
*/
|
|
15
|
+
const EOCD = 101010256;
|
|
16
|
+
const CENTRAL = 33639248;
|
|
17
|
+
function view(bytes) {
|
|
18
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
19
|
+
}
|
|
20
|
+
/** Where the end-of-central-directory record starts, searching back over
|
|
21
|
+
* the longest comment a zip can carry. */
|
|
22
|
+
function endRecord(bytes) {
|
|
23
|
+
const data = view(bytes);
|
|
24
|
+
const floor = Math.max(0, bytes.byteLength - 22 - 65535);
|
|
25
|
+
for (let at = bytes.byteLength - 22; at >= floor; at -= 1) if (data.getUint32(at, true) === EOCD) return at;
|
|
26
|
+
throw new InstallError("这个包无法解压");
|
|
27
|
+
}
|
|
28
|
+
/** The archive comment, as text. */
|
|
29
|
+
function zipComment(bytes) {
|
|
30
|
+
const at = endRecord(bytes);
|
|
31
|
+
const length = view(bytes).getUint16(at + 20, true);
|
|
32
|
+
return new TextDecoder().decode(bytes.subarray(at + 22, at + 22 + length));
|
|
33
|
+
}
|
|
34
|
+
function entries(bytes) {
|
|
35
|
+
const data = view(bytes);
|
|
36
|
+
const end = endRecord(bytes);
|
|
37
|
+
const count = data.getUint16(end + 10, true);
|
|
38
|
+
let at = data.getUint32(end + 16, true);
|
|
39
|
+
const found = [];
|
|
40
|
+
const decoder = new TextDecoder();
|
|
41
|
+
for (let index = 0; index < count; index += 1) {
|
|
42
|
+
if (at + 46 > bytes.byteLength || data.getUint32(at, true) !== CENTRAL) throw new InstallError("这个包无法解压");
|
|
43
|
+
const madeBy = data.getUint16(at + 4, true) >> 8;
|
|
44
|
+
const nameLength = data.getUint16(at + 28, true);
|
|
45
|
+
const extraLength = data.getUint16(at + 30, true);
|
|
46
|
+
const commentLength = data.getUint16(at + 32, true);
|
|
47
|
+
const external = data.getUint32(at + 38, true);
|
|
48
|
+
const name = decoder.decode(bytes.subarray(at + 46, at + 46 + nameLength));
|
|
49
|
+
const mode = madeBy === 3 ? external >>> 16 : void 0;
|
|
50
|
+
const type = mode === void 0 ? 0 : mode & 61440;
|
|
51
|
+
found.push({
|
|
52
|
+
name,
|
|
53
|
+
bytes: data.getUint32(at + 24, true),
|
|
54
|
+
directory: name.endsWith("/") || type === 16384 || (external & 16) !== 0,
|
|
55
|
+
link: type === 40960,
|
|
56
|
+
mode: mode === void 0 ? void 0 : mode & 511
|
|
57
|
+
});
|
|
58
|
+
at += 46 + nameLength + extraLength + commentLength;
|
|
59
|
+
}
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
/** The path an entry lands at, relative, or `undefined` when it would land
|
|
63
|
+
* anywhere but inside the target. */
|
|
64
|
+
function safeRelative(name) {
|
|
65
|
+
if (name.includes("\\") || name.startsWith("/") || /^[A-Za-z]:/.test(name)) return void 0;
|
|
66
|
+
const clean = normalize(name.replace(/\/+$/, ""));
|
|
67
|
+
if (clean === "." || clean === "") return "";
|
|
68
|
+
if (clean.split(sep).some((part) => part === "..")) return void 0;
|
|
69
|
+
return clean;
|
|
70
|
+
}
|
|
71
|
+
/** Checks the complete archive before inflating; no physical file IO or publication occurs. */
|
|
72
|
+
function unpackZip(bytes, maxExpandedBytes = 20971520) {
|
|
73
|
+
const list = entries(bytes);
|
|
74
|
+
if (list.length > 1e4 || !Number.isSafeInteger(maxExpandedBytes) || maxExpandedBytes < 1 || list.reduce((sum, entry) => sum + entry.bytes, 0) > maxExpandedBytes) throw new InstallError("read-limit: expanded archive exceeds its source byte budget");
|
|
75
|
+
for (const entry of list) {
|
|
76
|
+
if (entry.link) throw new InstallError(`包里有符号链接:${entry.name}`);
|
|
77
|
+
if (safeRelative(entry.name) === void 0) throw new InstallError(`包里有指向包外的路径:${entry.name}`);
|
|
78
|
+
}
|
|
79
|
+
if (new Set(list.map((entry) => entry.name)).size !== list.length) throw new InstallError("unsafe-entry: duplicate archive names");
|
|
80
|
+
const tops = new Set(list.map((entry) => entry.name.split("/")[0]));
|
|
81
|
+
const [only] = tops;
|
|
82
|
+
const strip = tops.size === 1 && only !== void 0 && list.every((entry) => entry.name.startsWith(`${only}/`)) ? `${only}/` : "";
|
|
83
|
+
let files;
|
|
84
|
+
try {
|
|
85
|
+
let remaining = maxExpandedBytes;
|
|
86
|
+
files = unzipSync(bytes, { filter(file) {
|
|
87
|
+
remaining -= file.originalSize;
|
|
88
|
+
if (remaining < 0) throw new InstallError("read-limit: expanded archive exceeds its source byte budget");
|
|
89
|
+
return true;
|
|
90
|
+
} });
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (error instanceof InstallError) throw error;
|
|
93
|
+
throw new InstallError("这个包无法解压");
|
|
94
|
+
}
|
|
95
|
+
const prepared = [];
|
|
96
|
+
for (const entry of list) {
|
|
97
|
+
const relative = safeRelative(entry.name.slice(strip.length));
|
|
98
|
+
if (relative === void 0) throw new InstallError(`unsafe-entry: archive path escapes: ${entry.name}`);
|
|
99
|
+
if (!relative) continue;
|
|
100
|
+
const content = entry.directory ? /* @__PURE__ */ new Uint8Array() : files[entry.name];
|
|
101
|
+
if (!content || content.byteLength !== (entry.directory ? 0 : entry.bytes)) throw new InstallError("read-limit: archive body does not match its complete central manifest");
|
|
102
|
+
prepared.push({
|
|
103
|
+
path: relative.split(sep).join("/"),
|
|
104
|
+
directory: entry.directory,
|
|
105
|
+
...entry.mode === void 0 ? {} : { mode: entry.mode },
|
|
106
|
+
content
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
entries: prepared,
|
|
111
|
+
...strip ? { top: strip.slice(0, -1) } : {}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** Resolves only after the worker actually exits; aborted inflation never publishes a late body. */
|
|
115
|
+
function createArchiveUnpacker(background) {
|
|
116
|
+
return async (bytes, maximum = 20971520, signal = new AbortController().signal) => {
|
|
117
|
+
signal.throwIfAborted();
|
|
118
|
+
try {
|
|
119
|
+
const result = await background.execute({
|
|
120
|
+
trustedModuleUrl: new URL(import.meta.resolve("@eum-agent/skill-management/archive-worker")),
|
|
121
|
+
value: {
|
|
122
|
+
bytes,
|
|
123
|
+
maximum
|
|
124
|
+
},
|
|
125
|
+
signal
|
|
126
|
+
});
|
|
127
|
+
signal.throwIfAborted();
|
|
128
|
+
return result;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
signal.throwIfAborted();
|
|
131
|
+
throw new InstallError(error instanceof Error ? error.message : "Archive inflation failed");
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** Publishes checked bounded entries asynchronously into the caller-owned scratch directory. */
|
|
136
|
+
async function unzipInto(bytes, target, maxExpandedBytes, unpack, signal) {
|
|
137
|
+
const prepared = await unpack(bytes, maxExpandedBytes, signal);
|
|
138
|
+
for (const entry of prepared.entries) {
|
|
139
|
+
signal?.throwIfAborted();
|
|
140
|
+
const path = join(target, ...entry.path.split("/"));
|
|
141
|
+
if (entry.directory) await mkdir(path, { recursive: true });
|
|
142
|
+
else {
|
|
143
|
+
await mkdir(join(path, ".."), { recursive: true });
|
|
144
|
+
await writeFile(path, entry.content, { signal });
|
|
145
|
+
if (process.platform !== "win32" && entry.mode !== void 0 && (entry.mode & 73) !== 0) await chmod(path, entry.mode | 384);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
signal?.throwIfAborted();
|
|
149
|
+
return prepared.top === void 0 ? {} : { top: prepared.top };
|
|
150
|
+
}
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/install/http.ts
|
|
153
|
+
const NETWORK_SERVICE = "network";
|
|
154
|
+
/** A failure whose message is fit to show a person as it is. */
|
|
155
|
+
var InstallError = class extends Error {
|
|
156
|
+
name = "InstallError";
|
|
157
|
+
/** Validation/download refusal has not published an installed skill. */
|
|
158
|
+
confirmedNotCommitted = true;
|
|
159
|
+
/** Failed physical release clears this flag before owner settlement. */
|
|
160
|
+
cleanupComplete = true;
|
|
161
|
+
};
|
|
162
|
+
const USER_AGENT = "yaoguang-skill-install";
|
|
163
|
+
/** How long a search may take. */
|
|
164
|
+
const SEARCH_TIMEOUT_MS = 1e4;
|
|
165
|
+
/** How long a download may go without a new byte. */
|
|
166
|
+
const IDLE_TIMEOUT_MS = 3e4;
|
|
167
|
+
/** Seconds until a rate limit lifts, from whichever header says so. */
|
|
168
|
+
function retryAfter(headers) {
|
|
169
|
+
for (const name of ["retry-after", "ratelimit-reset"]) {
|
|
170
|
+
const value = Number(headers.get(name));
|
|
171
|
+
if (Number.isFinite(value) && value > 0) return Math.ceil(value);
|
|
172
|
+
}
|
|
173
|
+
const reset = Number(headers.get("x-ratelimit-reset"));
|
|
174
|
+
if (Number.isFinite(reset) && reset > 0) {
|
|
175
|
+
const seconds = Math.ceil(reset - Date.now() / 1e3);
|
|
176
|
+
if (seconds > 0) return seconds;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** The sentence for a response that is not a success. */
|
|
180
|
+
function failureOf(label, response, what = "这个技能") {
|
|
181
|
+
const status = response.status;
|
|
182
|
+
if (status === 404) return new InstallError(`找不到${what}(${label})`);
|
|
183
|
+
if (status === 429) {
|
|
184
|
+
const wait = retryAfter(response.headers);
|
|
185
|
+
return new InstallError(wait === void 0 ? `${label} 限流,请稍后重试` : `${label} 限流,约 ${wait} 秒后可重试`);
|
|
186
|
+
}
|
|
187
|
+
if (status === 401 || status === 403) return new InstallError("这个技能需要登录才能下载,本版本暂不支持");
|
|
188
|
+
return new InstallError(`${label} 返回了错误(HTTP ${status})`);
|
|
189
|
+
}
|
|
190
|
+
function unreachable(label, error) {
|
|
191
|
+
if (error instanceof InstallError) return error;
|
|
192
|
+
if (error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError")) return new InstallError(`${label} 响应超时`);
|
|
193
|
+
return new InstallError(`连不上 ${label},请检查网络或代理`);
|
|
194
|
+
}
|
|
195
|
+
function createHttp(fetch, background) {
|
|
196
|
+
const headers = { "user-agent": USER_AGENT };
|
|
197
|
+
const http = {
|
|
198
|
+
unpackArchive: createArchiveUnpacker(background),
|
|
199
|
+
async json(url, label, signal, maxBytes = 1048576) {
|
|
200
|
+
const timeout = AbortSignal.timeout(SEARCH_TIMEOUT_MS);
|
|
201
|
+
const result = await http.bytes(url, label, {
|
|
202
|
+
signal: signal === void 0 ? timeout : AbortSignal.any([signal, timeout]),
|
|
203
|
+
maxBytes,
|
|
204
|
+
accept: "application/json"
|
|
205
|
+
});
|
|
206
|
+
try {
|
|
207
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(result.body));
|
|
208
|
+
} catch {
|
|
209
|
+
throw new InstallError(`${label} 返回的内容无法识别`);
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
async bytes(url, label, options = {}) {
|
|
213
|
+
const idle = new AbortController();
|
|
214
|
+
let timer = setTimeout(() => idle.abort(new DOMException("idle", "TimeoutError")), IDLE_TIMEOUT_MS);
|
|
215
|
+
const bump = () => {
|
|
216
|
+
clearTimeout(timer);
|
|
217
|
+
timer = setTimeout(() => idle.abort(new DOMException("idle", "TimeoutError")), IDLE_TIMEOUT_MS);
|
|
218
|
+
};
|
|
219
|
+
const signal = options.signal === void 0 ? idle.signal : AbortSignal.any([options.signal, idle.signal]);
|
|
220
|
+
try {
|
|
221
|
+
let response;
|
|
222
|
+
try {
|
|
223
|
+
response = await fetch(url, {
|
|
224
|
+
headers: {
|
|
225
|
+
...headers,
|
|
226
|
+
...options.accept ? { accept: options.accept } : {}
|
|
227
|
+
},
|
|
228
|
+
signal,
|
|
229
|
+
redirect: "follow"
|
|
230
|
+
});
|
|
231
|
+
} catch (error) {
|
|
232
|
+
throw unreachable(label, error);
|
|
233
|
+
}
|
|
234
|
+
if (!response.ok && !(options.pass ?? []).includes(response.status)) throw failureOf(label, response, options.what);
|
|
235
|
+
const maximum = options.maxBytes ?? 20971520;
|
|
236
|
+
if (!Number.isSafeInteger(maximum) || maximum < 1 || maximum > 20971520) {
|
|
237
|
+
await response.body?.cancel();
|
|
238
|
+
throw new InstallError("read-limit: invalid source byte budget");
|
|
239
|
+
}
|
|
240
|
+
const declared = response.headers.get("content-length");
|
|
241
|
+
if (declared !== null && /^\d+$/.test(declared) && BigInt(declared) > BigInt(maximum)) {
|
|
242
|
+
await response.body?.cancel();
|
|
243
|
+
throw new InstallError("read-limit: declared source body exceeds its byte budget");
|
|
244
|
+
}
|
|
245
|
+
let cleanupFailure = false;
|
|
246
|
+
const chunks = [];
|
|
247
|
+
let size = 0;
|
|
248
|
+
try {
|
|
249
|
+
const reader = response.body?.getReader();
|
|
250
|
+
if (reader !== void 0) {
|
|
251
|
+
let complete = false;
|
|
252
|
+
let readFailed = false;
|
|
253
|
+
let readFailure;
|
|
254
|
+
try {
|
|
255
|
+
for (;;) {
|
|
256
|
+
signal.throwIfAborted();
|
|
257
|
+
const { done, value } = await reader.read();
|
|
258
|
+
if (done) {
|
|
259
|
+
complete = true;
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
bump();
|
|
263
|
+
if (size + value.byteLength > maximum) throw new InstallError("read-limit: streamed source body exceeds its byte budget");
|
|
264
|
+
size += value.byteLength;
|
|
265
|
+
chunks.push(value);
|
|
266
|
+
}
|
|
267
|
+
} catch (error) {
|
|
268
|
+
readFailed = true;
|
|
269
|
+
readFailure = error;
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
if (!complete) await reader.cancel();
|
|
273
|
+
} catch (error) {
|
|
274
|
+
cleanupFailure = true;
|
|
275
|
+
throw error;
|
|
276
|
+
} finally {
|
|
277
|
+
reader.releaseLock();
|
|
278
|
+
}
|
|
279
|
+
if (readFailed) throw readFailure;
|
|
280
|
+
}
|
|
281
|
+
} catch (error) {
|
|
282
|
+
const failure = unreachable(label, error);
|
|
283
|
+
if (cleanupFailure) failure.cleanupComplete = false;
|
|
284
|
+
throw failure;
|
|
285
|
+
}
|
|
286
|
+
const body = new Uint8Array(size);
|
|
287
|
+
let offset = 0;
|
|
288
|
+
for (const chunk of chunks) {
|
|
289
|
+
body.set(chunk, offset);
|
|
290
|
+
offset += chunk.byteLength;
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
response,
|
|
294
|
+
body
|
|
295
|
+
};
|
|
296
|
+
} finally {
|
|
297
|
+
clearTimeout(timer);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
return http;
|
|
302
|
+
}
|
|
303
|
+
//#endregion
|
|
304
|
+
export { unpackZip as a, failureOf as i, NETWORK_SERVICE as n, unzipInto as o, createHttp as r, zipComment as s, InstallError as t };
|