sync-cache.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Sync the built dev plugin into the CLI's plugin cache so the running zcode CLI
  2. // (which loads plugins from ~/.zcode/cli/plugins/cache/..., NOT from this repo)
  3. // picks up skill / MCP / manifest changes without a manual copy.
  4. //
  5. // This closes the last hop of the sync chain:
  6. // packages/zcode-cua/plugin --sync:skill--> dev plugin (here) --sync:cache--> CLI cache
  7. //
  8. // The cache dir name (e.g. 0.1.31) is just an install-time cache key; copying newer
  9. // content (plugin.json says 0.2.0) on top of it is tolerated by the CLI and is exactly
  10. // what makes a dev skill/MCP edit go live. Run after `pnpm sync:skill` (+ `pnpm build`
  11. // when the MCP wrapper or skill changed).
  12. //
  13. // Usage:
  14. // node scripts/sync-cache.mjs # auto-resolve cache path from marketplace.json
  15. // node scripts/sync-cache.mjs <cacheDir> # explicit cache dir
  16. import { cp, mkdir, readFile, readdir, stat } from "node:fs/promises";
  17. import { existsSync } from "node:fs";
  18. import { dirname, join, resolve } from "node:path";
  19. import { fileURLToPath, pathToFileURL } from "node:url";
  20. import { homedir } from "node:os";
  21. import { stageSharpIntoBundledAgents } from "../../../../../packages/desktop/scripts/sharp-package-assets.mjs";
  22. import { stageKoffiIntoBundledAgents } from "../../../../../packages/desktop/scripts/koffi-package-assets.mjs";
  23. const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  24. const devPlugin = packageRoot;
  25. // Build artifacts that constitute an installed plugin in the cache.
  26. const ENTRIES = [
  27. "skills",
  28. "docs",
  29. "scripts/computer-use-client.mjs",
  30. ".zcode-plugin",
  31. "package.json",
  32. // CUA screenshot/zoom 的 sharp native closure 属于 CUA plugin 自身;同步开发缓存时
  33. // 不能只复制 skill/docs,否则下一次 cache seed 会再次丢失原生依赖。
  34. "node_modules",
  35. ];
  36. /**
  37. * Resolve the CLI cache dir for zcode-cua. Prefer the cachePath declared in the
  38. * zcode-plugins-official marketplace.json; fall back to the single version dir under
  39. * the cache root. Throws if neither yields an existing dir (plugin not installed).
  40. */
  41. async function resolveCacheDir() {
  42. if (process.argv[2]) return resolve(process.argv[2]);
  43. const marketplace = join(
  44. homedir(),
  45. ".zcode/cli/plugins/marketplaces/zcode-plugins-official/marketplace.json",
  46. );
  47. try {
  48. const text = await readFile(marketplace, "utf8");
  49. const parsed = JSON.parse(text);
  50. const entry = (parsed.plugins ?? parsed.items ?? []).find(
  51. (p) => p?.name === "computer-use" && p?.cachePath,
  52. );
  53. if (entry?.cachePath && existsSync(entry.cachePath)) return entry.cachePath;
  54. } catch {
  55. // marketplace.json missing/unreadable — fall through to glob.
  56. }
  57. const cacheRoot = join(homedir(), ".zcode/cli/plugins/cache/zcode-plugins-official/computer-use");
  58. if (existsSync(cacheRoot)) {
  59. const dirs = await readdir(cacheRoot);
  60. if (dirs.length === 1) return join(cacheRoot, dirs[0]);
  61. if (dirs.length > 1) {
  62. // Pick the newest by mtime — the most recently installed version.
  63. let newest = null;
  64. for (const d of dirs) {
  65. const s = await stat(join(cacheRoot, d));
  66. if (!newest || s.mtimeMs > newest.mtimeMs) newest = { d, mtimeMs: s.mtimeMs };
  67. }
  68. if (newest) return join(cacheRoot, newest.d);
  69. }
  70. }
  71. throw new Error(
  72. `Could not resolve the zcode-cua CLI cache dir. The plugin does not appear to be ` +
  73. `installed — run it once from the zcode UI/TUI to install, then re-run sync:cache. ` +
  74. `(looked in marketplace.json and ${cacheRoot})`,
  75. );
  76. }
  77. const cacheDir = await resolveCacheDir();
  78. await mkdir(cacheDir, { recursive: true });
  79. const copied = [];
  80. for (const entry of ENTRIES) {
  81. const src = join(devPlugin, entry);
  82. if (!existsSync(src)) continue; // e.g. dist/ before first build
  83. const dest = join(cacheDir, entry);
  84. // cp with recursive + force replacement; file/dir both handled.
  85. await cp(pathToFileURL(src), pathToFileURL(dest), { recursive: true, force: true });
  86. copied.push(entry);
  87. }
  88. // Bug 根因:开发态 CLI 实际从 ~/.zcode/cli/plugins/cache 启动 MCP,
  89. // 只同步 dist 会让 externalized sharp 仍从一个隔离目录解析,导致 screenshot/zoom
  90. // 在源码和 Helper 都正确时依旧报 MODULE_NOT_FOUND。与产品打包共用同一个 staging
  91. // 函数,把当前平台的 JS/native 运行时放在插件自身 node_modules,避免两条链路漂移。
  92. stageSharpIntoBundledAgents({
  93. desktopPackageRoot: resolve(packageRoot, "../../../../packages/desktop"),
  94. glmDir: cacheDir,
  95. targetPlatform: { os: process.platform, arch: process.arch },
  96. });
  97. copied.push("node_modules/sharp runtime");
  98. stageKoffiIntoBundledAgents({
  99. koffiPackageRoot: resolve(packageRoot, "../adapters"),
  100. glmDir: cacheDir,
  101. targetPlatform: { os: process.platform, arch: process.arch },
  102. });
  103. copied.push("node_modules/koffi runtime");
  104. console.log(`sync:cache → ${cacheDir}`);
  105. console.log(` copied: ${copied.join(", ") || "(nothing)"}`);
  106. console.log(
  107. ` note: the cache dir name is an install-time key and may lag the plugin.json ` +
  108. `version inside — that is expected and does not affect loading.`,
  109. );