@eum-agent/sandbox-presets 1.0.0 → 1.0.2

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 (3) hide show
  1. package/dist/index.js +69 -52
  2. package/package.json +6 -6
  3. package/ui/ui.js +1 -1
package/dist/index.js CHANGED
@@ -134,7 +134,6 @@ const StoredProfileSchema = z.object({
134
134
  /** Complete verified snapshot used for new selections. */
135
135
  const SnapshotSchema = z.object({
136
136
  commit: ShaSchema,
137
- treeSha: ShaSchema,
138
137
  syncedAt: z.string(),
139
138
  profiles: z.array(StoredProfileSchema).min(1).max(128)
140
139
  }).strict();
@@ -239,22 +238,11 @@ function decodeUtf8(bytes) {
239
238
  }
240
239
  //#endregion
241
240
  //#region src/sandbox-presets/catalog.ts
242
- /** Same-commit Git tree and generated bundle verification for the fixed public repository. */
241
+ /** Generated bundle verification for one pinned commit of the fixed public repository. */
243
242
  /** Computes Git's SHA-1 blob identity over exact UTF-8 bytes. */
244
243
  function blobSha(bytes) {
245
244
  return createHash("sha1").update(`blob ${bytes.length}\0`).update(bytes).digest("hex");
246
245
  }
247
- /** Bounded Git tree fields; GitHub may add harmless transport metadata. */
248
- const TreeSchema = z.object({
249
- sha: ShaSchema,
250
- truncated: z.literal(false),
251
- tree: z.array(z.object({
252
- path: z.string().max(4096),
253
- mode: z.string(),
254
- type: z.string(),
255
- sha: ShaSchema
256
- })).max(2e4)
257
- });
258
246
  const BundleSchema = z.object({ presets: z.array(z.object({
259
247
  id: IdSchema,
260
248
  path: z.string().max(160),
@@ -269,22 +257,20 @@ function verifyProfile(stored) {
269
257
  const result = ProfileSchema.safeParse(parseJson(stored.content));
270
258
  if (!result.success || result.data.id !== stored.id || JSON.stringify(result.data) !== JSON.stringify(stored.profile)) invalid("Profile content and identity disagree");
271
259
  }
272
- /** Validates complete ordinary-file coverage and raw hashes before returning any categories. */
273
- function decodeCatalog(commit, rawTree, bytes, syncedAt = (/* @__PURE__ */ new Date()).toISOString()) {
260
+ /**
261
+ * Decodes the generated bundle read from one pinned commit into a complete snapshot.
262
+ * Every category must have a unique id, live at `sandbox/<id>/paths.json`, and carry content
263
+ * whose Git blob identity matches its declared digest. Throws `PresetError` with
264
+ * `CATALOG_INVALID` for malformed or inconsistent data and `LIMIT_EXCEEDED` for oversized input.
265
+ */
266
+ function decodeCatalog(commit, bytes, syncedAt = (/* @__PURE__ */ new Date()).toISOString()) {
274
267
  if (bytes.length > 8388608) throw new PresetError("LIMIT_EXCEEDED", "Catalog exceeds 8 MiB");
275
- const tree = TreeSchema.safeParse(rawTree), bundle = BundleSchema.safeParse(parseJson(decodeUtf8(bytes)));
276
- if (!ShaSchema.safeParse(commit).success || !tree.success || !bundle.success) invalid("Invalid catalog tree or bundle");
277
- const files = new Map(tree.data.tree.map((item) => [item.path, item]));
278
- if (files.size !== tree.data.tree.length) invalid("Duplicate tree path");
279
- for (const item of tree.data.tree) if (item.path.startsWith("sandbox/") || item.path === "sandbox" || item.path === "schema" || item.path === "schema/sandbox.schema.json") {
280
- if (item.path.split("/").some((part) => !part || part === "." || part === "..") || !(item.mode === "040000" && item.type === "tree" || item.mode === "100644" && item.type === "blob")) invalid("Catalog contains linked, executable, or invalid files");
281
- }
282
- for (const p of ["sandbox/index.json", "schema/sandbox.schema.json"]) if (files.get(p)?.mode !== "100644") invalid("Catalog index or schema is missing");
283
- if (files.get("sandbox/index.json")?.sha !== blobSha(bytes)) invalid("Index blob digest differs from tree");
268
+ const bundle = BundleSchema.safeParse(parseJson(decodeUtf8(bytes)));
269
+ if (!ShaSchema.safeParse(commit).success || !bundle.success) invalid("Invalid catalog bundle");
284
270
  const profiles = [], ids = /* @__PURE__ */ new Set();
285
271
  let entries = 0;
286
272
  for (const item of bundle.data.presets) {
287
- if (ids.has(item.id) || item.path !== `sandbox/${item.id}/paths.json` || files.get(item.path)?.sha !== item.blobSha || files.get(item.path)?.mode !== "100644" || files.get(`sandbox/${item.id}/README.md`)?.mode !== "100644" || files.get(`sandbox/${item.id}`)?.mode !== "040000") invalid("Category identity, coverage, or mode mismatch");
273
+ if (ids.has(item.id) || item.path !== `sandbox/${item.id}/paths.json`) invalid("Category identity or path mismatch");
288
274
  if (Buffer.byteLength(item.content) > 131072) throw new PresetError("LIMIT_EXCEEDED", "Profile exceeds 128 KiB");
289
275
  const parsed = ProfileSchema.safeParse(parseJson(item.content));
290
276
  if (!parsed.success) invalid(`Invalid profile: ${item.id}`);
@@ -301,12 +287,8 @@ function decodeCatalog(commit, rawTree, bytes, syncedAt = (/* @__PURE__ */ new D
301
287
  ids.add(item.id);
302
288
  profiles.push(profile);
303
289
  }
304
- const paths = tree.data.tree.filter((item) => /^sandbox\/[^/]+\/paths\.json$/.test(item.path));
305
- const dirs = tree.data.tree.filter((item) => /^sandbox\/[^/]+$/.test(item.path) && item.type === "tree");
306
- if (paths.length !== profiles.length || dirs.length !== profiles.length || paths.some((item) => !ids.has(item.path.split("/")[1] ?? ""))) invalid("Bundle omits or adds a category");
307
290
  return {
308
291
  commit,
309
- treeSha: tree.data.sha,
310
292
  syncedAt,
311
293
  profiles: profiles.sort((a, b) => a.id < b.id ? -1 : 1)
312
294
  };
@@ -429,23 +411,17 @@ function resolveProfile(profile, commit, host, reads) {
429
411
  }
430
412
  //#endregion
431
413
  //#region src/sandbox-presets/sync.ts
432
- const api = "https://api.github.com/repos/yanxuan-lc/yaoguang-presets";
433
- const commitSchema = z.object({
434
- sha: ShaSchema,
435
- commit: z.object({ tree: z.object({ sha: ShaSchema }) })
436
- });
437
- async function readResponse(network, path, signal, raw = false) {
414
+ const refsUrl = "https://github.com/yanxuan-lc/yaoguang-presets.git/info/refs?service=git-upload-pack";
415
+ const rawBase = "https://raw.githubusercontent.com/yanxuan-lc/yaoguang-presets";
416
+ /** Reads one bounded public response; failures map to stable preset error codes. */
417
+ async function readResponse(network, url, signal) {
438
418
  signal.throwIfAborted();
439
419
  let response;
440
420
  try {
441
- response = await network.fetch(`${api}/${path}`, {
421
+ response = await network.fetch(url, {
442
422
  signal,
443
423
  redirect: "error",
444
- headers: {
445
- Accept: raw ? "application/vnd.github.raw+json" : "application/vnd.github+json",
446
- "X-GitHub-Api-Version": "2026-03-10",
447
- "User-Agent": "YaoGuang-presets"
448
- }
424
+ headers: { "User-Agent": "YaoGuang-presets" }
449
425
  });
450
426
  } catch {
451
427
  signal.throwIfAborted();
@@ -483,22 +459,45 @@ async function readResponse(network, path, signal, raw = false) {
483
459
  reader.releaseLock();
484
460
  }
485
461
  }
486
- /** Pins commit, recursive tree, and raw bundle; a cache hit performs only the branch lookup. */
462
+ /**
463
+ * Extracts the `refs/heads/main` commit from a Git smart HTTP upload-pack ref advertisement.
464
+ * Requires the `# service=git-upload-pack` announcement and well-formed pkt-lines; throws
465
+ * `PresetError` with `CATALOG_INVALID` otherwise.
466
+ */
467
+ function mainCommit(text) {
468
+ const lines = [];
469
+ for (let at = 0; at < text.length;) {
470
+ const header = text.slice(at, at + 4);
471
+ if (!/^[0-9a-f]{4}$/.test(header)) invalid("Malformed ref advertisement");
472
+ const length = Number.parseInt(header, 16);
473
+ if (length === 0) {
474
+ at += 4;
475
+ continue;
476
+ }
477
+ if (length < 4 || at + length > text.length) invalid("Malformed ref advertisement");
478
+ lines.push(text.slice(at + 4, at + length).replace(/\n$/, ""));
479
+ at += length;
480
+ }
481
+ if (lines[0] !== "# service=git-upload-pack") invalid("Missing upload-pack announcement");
482
+ for (const line of lines.slice(1)) {
483
+ const match = /^([0-9a-f]{40}) ([^\0]+)(?:\0.*)?$/s.exec(line);
484
+ if (match?.[2] === "refs/heads/main") return match[1];
485
+ }
486
+ return invalid("Branch main is not advertised");
487
+ }
488
+ /**
489
+ * Resolves `main` with one ref-advertisement request, then reads `sandbox/index.json` pinned to
490
+ * that commit. An unchanged commit reuses `previous` with a fresh sync time and no second request.
491
+ */
487
492
  async function synchronize(network, signal, previous) {
488
- const commit = commitSchema.safeParse(parseJson(decodeUtf8(await readResponse(network, "commits/main", signal))));
489
- if (!commit.success) invalid("Invalid branch commit response");
490
- if (previous?.commit === commit.data.sha && previous.treeSha === commit.data.commit.tree.sha) return {
493
+ const commit = mainCommit(decodeUtf8(await readResponse(network, refsUrl, signal)));
494
+ if (previous?.commit === commit) return {
491
495
  ...previous,
492
496
  syncedAt: (/* @__PURE__ */ new Date()).toISOString()
493
497
  };
494
- const tree = TreeSchema.safeParse(parseJson(decodeUtf8(await readResponse(network, `git/trees/${commit.data.commit.tree.sha}?recursive=1`, signal))));
495
- if (!tree.success || tree.data.sha !== commit.data.commit.tree.sha) invalid("Invalid or incomplete commit tree");
496
- const index = tree.data.tree.find((item) => item.path === "sandbox/index.json");
497
- if (!index) throw new PresetError("CATALOG_EMPTY", "No sandbox catalog is available");
498
- if (index.mode !== "100644" || index.type !== "blob") invalid("Index is not an ordinary file");
499
- const bytes = await readResponse(network, `git/blobs/${index.sha}`, signal, true);
498
+ const bytes = await readResponse(network, `${rawBase}/${commit}/sandbox/index.json`, signal);
500
499
  signal.throwIfAborted();
501
- return decodeCatalog(commit.data.sha, tree.data, bytes);
500
+ return decodeCatalog(commit, bytes);
502
501
  }
503
502
  //#endregion
504
503
  //#region src/sandbox-presets/service.ts
@@ -731,6 +730,22 @@ function createPresetService(deps) {
731
730
  };
732
731
  }
733
732
  const service = {
733
+ /** Reads committed enabled names without touching path projection or initialization. */
734
+ summary() {
735
+ try {
736
+ guard();
737
+ const state = store.readApplied();
738
+ return {
739
+ status: "ready",
740
+ names: [...new Set(state.profiles.filter((profile) => profile.profile.platforms[host.platform]).map((profile) => profile.profile.name))]
741
+ };
742
+ } catch {
743
+ return {
744
+ status: "unavailable",
745
+ names: []
746
+ };
747
+ }
748
+ },
734
749
  list,
735
750
  preview(selections) {
736
751
  guard();
@@ -1145,6 +1160,7 @@ function registerSandboxPresets(ctx) {
1145
1160
  })
1146
1161
  });
1147
1162
  ctx.onDispose(() => service.dispose());
1163
+ ctx.services.provide("sandbox-preset-summary", { current: service.summary });
1148
1164
  async function result(work) {
1149
1165
  try {
1150
1166
  return {
@@ -1200,6 +1216,7 @@ const plugin = {
1200
1216
  name: "@eum-agent/sandbox-presets",
1201
1217
  requires: ["sandbox-path-locator", "network"],
1202
1218
  optionalServices: ["sandbox-readable-projection"],
1219
+ provides: ["sandbox-preset-summary"],
1203
1220
  apply(ctx) {
1204
1221
  registerSandboxPresets(ctx);
1205
1222
  const module = fileURLToPath(new URL("../ui/ui.js", import.meta.url));
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "en-US": "Sandbox Presets",
5
5
  "zh-CN": "沙箱预设"
6
6
  },
7
- "version": "1.0.0",
7
+ "version": "1.0.2",
8
8
  "type": "module",
9
9
  "description": "Sandbox Presets",
10
10
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "zod": "^4.4.3"
22
22
  },
23
23
  "devDependencies": {
24
- "@eum-agent/plugin": "1.0.0",
24
+ "@eum-agent/plugin": "1.0.1",
25
25
  "@types/react": "^19.2.18",
26
26
  "@types/react-dom": "^19.2.5",
27
27
  "react": "^19.2.0",
@@ -37,8 +37,8 @@
37
37
  "lucide-react": "^1.40.0",
38
38
  "@types/semver": "7.8.0",
39
39
  "i18next": "^26.4.1",
40
- "@eum-agent/infra-local": "1.0.0",
41
- "@eum-agent/storage-sqlite": "1.0.0"
40
+ "@eum-agent/infra-local": "1.0.1",
41
+ "@eum-agent/storage-sqlite": "1.0.1"
42
42
  },
43
43
  "yaoguang": {
44
44
  "kind": "extension",
@@ -48,8 +48,8 @@
48
48
  },
49
49
  "dependencies": {},
50
50
  "foundationDependencies": {
51
- "@eum-agent/infra-local": "1.0.0",
52
- "@eum-agent/storage-sqlite": "1.0.0"
51
+ "@eum-agent/infra-local": "1.0.1",
52
+ "@eum-agent/storage-sqlite": "1.0.1"
53
53
  }
54
54
  },
55
55
  "scripts": {
package/ui/ui.js CHANGED
@@ -165,7 +165,7 @@ function createPresetAcquisition(api, owner) {
165
165
  ...snapshot,
166
166
  syncing: true
167
167
  });
168
- operationId = owner.uuid?.() ?? crypto.randomUUID();
168
+ operationId = owner.uuid();
169
169
  const id = operationId;
170
170
  const running = {
171
171
  epoch,