prepare-agent-node-bundle.mjs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #!/usr/bin/env node
  2. // 桌面打包态的 agent 运行时资产:把 agent 的 JS bundle(zcode.cjs)放进 bundled-agents/<platform>/glm,
  3. // 由 app 内置的 Electron Node runtime(ELECTRON_RUN_AS_NODE)执行,替代以前随包内置的独立 Node 二进制。
  4. //
  5. // 为什么这么做:
  6. // - agent 没有任何原生 NAPI 插件(ripgrep 是 WASM,其余纯 JS),可直接跑在 Electron 的 Node 上;
  7. // - Electron 41 内置 Node 24.x,与 zcode-cli 的目标运行时一致;
  8. // - 单平台体积从 ~180MB 降到 ~16MB,且同一份 JS 跨平台通用;
  9. // - app-server 命令路径不会加载 @zcode/tui,所以这里天然不打包 TUI。
  10. //
  11. // 远端(SSH/WSL/Docker)没有 Electron,由 Source Server archive 自带目标 Node,互不影响。
  12. // 修复原因:bootstrap 清理旧插件 dist 时使用 rmSync,漏导入会在构建前抛出 ReferenceError。
  13. import { cpSync, existsSync, mkdirSync, rmSync } from "node:fs";
  14. import { basename, dirname, resolve } from "node:path";
  15. import process from "node:process";
  16. import { fileURLToPath } from "node:url";
  17. import { runCommand } from "../../../scripts/spawn-command.mjs";
  18. import { stageAgentBundle } from "./stage-agent-bundle.mjs";
  19. import {
  20. isSharpRequiredByComputerUse,
  21. stageSharpIntoBundledAgents,
  22. } from "./sharp-package-assets.mjs";
  23. import { stageKoffiIntoBundledAgents } from "./koffi-package-assets.mjs";
  24. import { BUNDLED_NPM_VERSION, stageBundledNpm } from "./bundled-npm-assets.mjs";
  25. const scriptDir = dirname(fileURLToPath(import.meta.url));
  26. const desktopRoot = resolve(scriptDir, "..");
  27. const repoRoot = resolve(desktopRoot, "..", "..");
  28. const cliBundlePath = resolve(repoRoot, "apps/zcode-cli/packages/cli/dist/zcode.cjs");
  29. const adaptersRoot = resolve(repoRoot, "apps/zcode-cli/packages/adapters");
  30. const pnpmRunEnv = {
  31. ...process.env,
  32. // pnpm 11 会在 apps/zcode-cli 子 workspace 执行 run 前触发 install;
  33. // 子 workspace 不能解析根 workspace 的 @zcode/shared,Docker/web app 打包会因此卡在插件 runtime 构建。
  34. PNPM_CONFIG_VERIFY_DEPS_BEFORE_RUN: "false",
  35. };
  36. const BROWSER_USE_PLUGIN_PACKAGE_NAME = "@zcode/browser-use-plugin";
  37. const NODE_REPL_HOST_PACKAGE_NAME = "@zcode/node-repl-host";
  38. const CUA_PLUGIN_PACKAGE_NAME = "@zcode/zcode-cua-plugin";
  39. // 平台目录命名:darwin/win32/linux + x64/arm64,
  40. // 支持 ZCODE_TARGET_OS / ZCODE_TARGET_ARCH 覆盖(交叉打包时由 CI 注入)。
  41. function normalizePlatform(raw) {
  42. switch (raw) {
  43. case "mac":
  44. case "macos":
  45. case "darwin":
  46. case "osx":
  47. return "darwin";
  48. case "win":
  49. case "windows":
  50. case "win32":
  51. return "win32";
  52. case "linux":
  53. return "linux";
  54. default:
  55. return raw;
  56. }
  57. }
  58. function normalizeArch(raw) {
  59. switch (raw) {
  60. case "x86_64":
  61. case "x64":
  62. case "amd64":
  63. return "x64";
  64. case "aarch64":
  65. case "arm64":
  66. return "arm64";
  67. default:
  68. return raw;
  69. }
  70. }
  71. const platform = normalizePlatform(process.env.ZCODE_TARGET_OS || "") || process.platform;
  72. const arch = normalizeArch(process.env.ZCODE_TARGET_ARCH || "") || process.arch;
  73. const platformKey = `${platform}-${arch}`;
  74. const glmDir = resolve(desktopRoot, "bundled-agents", platformKey, "glm");
  75. // zcode.cjs / .node-bundle-meta.json 的落点由 stage-agent-bundle.mjs 自己解析(同源)。
  76. // node_repl 宿主抽成独立包
  77. // @zcode/node-repl-host 之后,browser-use 不再产出 dist/mcp/server.js,CUA 资产
  78. // (docs/computer-use.md、scripts/computer-use-client.mjs)也已归 @zcode/zcode-cua-plugin。
  79. // 这份清单当时漏改,打包准备阶段照旧去 browser-use 要那三个文件,直接 missing runtime 挂掉。
  80. // dev 链路走的是 scripts/build-desktop-agent-cli.mjs 的 requiredDevPluginRuntimeBuilds(那份改对了),
  81. // 两份平行清单各自维护,所以 dev 测不出来 —— 权威归属见 bootstrap/official-plugin-definitions.ts。
  82. const browserUseRequiredRuntimePaths = [
  83. "scripts/browser-client.mjs",
  84. "docs/api.json",
  85. "docs/documents.json",
  86. "docs/overview.md",
  87. // documents.json 已暴露 recording lookup,桌面安装包不能复用缺少正文的 runtime。
  88. "docs/recording.md",
  89. "docs/workflow.md",
  90. "skills/control-browser/SKILL.md",
  91. "skills/web-gui-tester/SKILL.md",
  92. ];
  93. // 文档插件(documents / pdf / presentations / spreadsheets)的查看面板由共享包
  94. // @zcode/office-runtime 构建进各插件 dist/;插件包自身没有 TypeScript 源码。
  95. const officeInvocationPaths = [
  96. "scripts/zcode_libreoffice.py",
  97. "scripts/libreoffice-profile/user/registrymodifications.xcu",
  98. "scripts/libreoffice-profile/user/basic/script.xlc",
  99. "scripts/libreoffice-profile/user/basic/dialog.xlc",
  100. "scripts/libreoffice-profile/user/basic/Standard/script.xlb",
  101. "scripts/libreoffice-profile/user/basic/Standard/dialog.xlb",
  102. "scripts/libreoffice-profile/user/basic/Standard/ZCode.xba",
  103. ];
  104. const officeViewerRuntimePaths = ["dist/server.mjs", "dist/ui/assets.json", "dist/ui/panel.html"];
  105. const officialPluginPackages = [
  106. {
  107. packageName: "@zcode/android-emulator-plugin",
  108. relativePath: "apps/zcode-cli/packages/android-emulator-plugin",
  109. requiresRuntime: true,
  110. requiredRuntimePaths: ["dist/mcp/server.js"],
  111. runtimeBuildScript: "scripts/build-mcp.mjs",
  112. stagedPath: "packages/android-emulator-plugin",
  113. },
  114. {
  115. packageName: "@zcode/documents-plugin",
  116. relativePath: "apps/zcode-cli/packages/documents-plugin",
  117. requiresRuntime: true,
  118. requiredRuntimePaths: officeViewerRuntimePaths,
  119. requiredSeedPaths: [
  120. "skills/docx/SKILL.md",
  121. "skills/docx-viewer/SKILL.md",
  122. ...officeInvocationPaths,
  123. ],
  124. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  125. stagedPath: "packages/documents-plugin",
  126. },
  127. {
  128. packageName: "@zcode/image-search-plugin",
  129. relativePath: "apps/zcode-cli/packages/image-search-plugin",
  130. requiresRuntime: false,
  131. requiredSeedPaths: [".mcp.json"],
  132. stagedPath: "packages/image-search-plugin",
  133. },
  134. {
  135. packageName: "@zcode/pdf-plugin",
  136. relativePath: "apps/zcode-cli/packages/pdf-plugin",
  137. requiresRuntime: true,
  138. requiredRuntimePaths: officeViewerRuntimePaths,
  139. requiredSeedPaths: [
  140. "skills/pdf/SKILL.md",
  141. "skills/pdf-viewer/SKILL.md",
  142. ...officeInvocationPaths,
  143. ],
  144. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  145. stagedPath: "packages/pdf-plugin",
  146. },
  147. {
  148. packageName: "@zcode/presentations-plugin",
  149. relativePath: "apps/zcode-cli/packages/presentations-plugin",
  150. requiresRuntime: true,
  151. requiredRuntimePaths: officeViewerRuntimePaths,
  152. requiredSeedPaths: [
  153. "skills/pptx/SKILL.md",
  154. "skills/pptx-viewer/SKILL.md",
  155. ...officeInvocationPaths,
  156. ],
  157. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  158. stagedPath: "packages/presentations-plugin",
  159. },
  160. {
  161. packageName: "@zcode/spreadsheets-plugin",
  162. relativePath: "apps/zcode-cli/packages/spreadsheets-plugin",
  163. requiresRuntime: true,
  164. requiredRuntimePaths: officeViewerRuntimePaths,
  165. requiredSeedPaths: [
  166. "skills/xlsx/SKILL.md",
  167. "skills/xlsx-viewer/SKILL.md",
  168. ...officeInvocationPaths,
  169. ],
  170. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  171. stagedPath: "packages/spreadsheets-plugin",
  172. },
  173. {
  174. packageName: "@zcode/visualize-plugin",
  175. relativePath: "apps/zcode-cli/packages/visualize-plugin",
  176. requiresRuntime: false,
  177. requiredSeedPaths: [
  178. "skills/visualize/SKILL.md",
  179. "skills/visualize/references/api.md",
  180. "skills/visualize/references/styles.md",
  181. "skills/visualize/tweak.md",
  182. "skills/visualize/LICENSE.md",
  183. "skills/visualize/scripts/render.py",
  184. "skills/visualize/assets/visualize.css",
  185. "skills/visualize/assets/visualize.html",
  186. "skills/visualize/assets/calendar.js",
  187. "skills/visualize/assets/runtime-manifest.json",
  188. "skills/visualize/scripts/vendor.py",
  189. "skills/visualize/assets/vendor/manifest.json",
  190. "skills/visualize/assets/vendor/floating-ui-core-1.7.3.min.js",
  191. "skills/visualize/assets/vendor/floating-ui-core-1.7.3.min.js.LICENSE",
  192. "skills/visualize/assets/vendor/floating-ui-dom-1.7.4.min.js",
  193. "skills/visualize/assets/vendor/floating-ui-dom-1.7.4.min.js.LICENSE",
  194. "skills/visualize/assets/vendor/lucide-1.17.0.js",
  195. "skills/visualize/assets/vendor/lucide-1.17.0.js.LICENSE",
  196. "skills/visualize/assets/vendor/d3-7.9.0.min.js",
  197. "skills/visualize/assets/vendor/d3-7.9.0.min.js.LICENSE",
  198. "skills/visualize/widgets/calendar.md",
  199. "skills/visualize/examples/calendar.html",
  200. "skills/visualize/assets/standalone-host-bridge.js",
  201. "skills/visualize/assets/standalone-shell.js",
  202. ],
  203. requiredRuntimePaths: [],
  204. stagedPath: "packages/visualize-plugin",
  205. },
  206. {
  207. // browser-use 只携带自己的 client script 与 skill/docs;node_repl MCP runtime 归
  208. // @zcode/node-repl-host(见上方常量注释)。
  209. packageName: "@zcode/browser-use-plugin",
  210. relativePath: "apps/zcode-cli/packages/browser-use-plugin",
  211. requiresRuntime: true,
  212. requiredRuntimePaths: browserUseRequiredRuntimePaths,
  213. runtimeBuildScript: "scripts/build.mjs",
  214. stagedPath: "packages/browser-use-plugin",
  215. },
  216. {
  217. packageName: "@zcode/ios-simulator-plugin",
  218. relativePath: "apps/zcode-cli/packages/ios-simulator-plugin",
  219. requiresRuntime: true,
  220. requiredRuntimePaths: ["dist/mcp/server.js"],
  221. runtimeBuildScript: "scripts/build-mcp.mjs",
  222. stagedPath: "packages/ios-simulator-plugin",
  223. },
  224. {
  225. // 修复原因:restore-legacy-sessions 已注册为官方插件,但 Electron 生产包只会拷贝
  226. // 这里列出的资源。漏掉它会导致用户生产包首次启动后 plugins list 里看不到该插件。
  227. packageName: "@zcode/restore-legacy-sessions-plugin",
  228. relativePath: "apps/zcode-cli/packages/restore-legacy-sessions-plugin",
  229. requiresRuntime: false,
  230. stagedPath: "packages/restore-legacy-sessions-plugin",
  231. },
  232. {
  233. packageName: "@zcode/skill-creator-plugin",
  234. relativePath: "apps/zcode-cli/packages/skill-creator-plugin",
  235. requiresRuntime: false,
  236. stagedPath: "packages/skill-creator-plugin",
  237. },
  238. {
  239. packageName: "@zcode/plugin-creator-plugin",
  240. relativePath: "apps/zcode-cli/packages/plugin-creator-plugin",
  241. requiresRuntime: false,
  242. stagedPath: "packages/plugin-creator-plugin",
  243. },
  244. {
  245. // 修复原因:zcode-guide 已注册为官方默认启用内容型插件(见 official-plugin-definitions.ts)。
  246. // Electron 生产包不是 SEA 路径,官方插件 seed 依赖这里 stage 的 resources/glm/packages/*-plugin;
  247. // 漏掉它会导致生产桌面包首启 seed 不到该插件,用户拿不到默认启用的配置指南技能。
  248. packageName: "@zcode/zcode-guide-plugin",
  249. relativePath: "apps/zcode-cli/packages/zcode-guide-plugin",
  250. requiresRuntime: false,
  251. stagedPath: "packages/zcode-guide-plugin",
  252. },
  253. {
  254. // ZCode Sites(默认开启)。漏 stage 会让生产桌面包里找不到它;Sites 面板的 MCP server 与页面由插件自己的
  255. // build 脚本构建进 dist/。
  256. packageName: "@zcode/zcode-site-plugin",
  257. relativePath: "apps/zcode-cli/packages/zcode-site-plugin",
  258. requiresRuntime: true,
  259. requiredRuntimePaths: ["dist/server.mjs", "dist/ui/panel.html"],
  260. runtimeBuildScript: "scripts/build.mjs",
  261. stagedPath: "packages/zcode-site-plugin",
  262. },
  263. {
  264. // node_repl 宿主:Browser Use 与 Computer Use 共用的 MCP runtime,本轮抽成独立包。
  265. // 它没有 listing(不进插件市场展示面),但生产包首启 seed 必须拿到它的 dist runtime,
  266. // 否则 bua/cua 任一开启时都会连不上 node_repl。
  267. packageName: "@zcode/node-repl-host",
  268. relativePath: "apps/zcode-cli/packages/node-repl-host",
  269. requiresRuntime: true,
  270. requiredRuntimePaths: ["dist/mcp/server.js"],
  271. runtimeBuildScript: "scripts/build.mjs",
  272. stagedPath: "packages/node-repl-host",
  273. },
  274. {
  275. // CUA 插件只携带 SDK/skill/docs;node_repl MCP runtime 归 @zcode/node-repl-host。
  276. packageName: "@zcode/zcode-cua-plugin",
  277. relativePath: "apps/zcode-cli/packages/zcode-cua-plugin",
  278. requiresRuntime: false,
  279. stagedPath: "packages/zcode-cua-plugin",
  280. },
  281. {
  282. // design(默认启用内容型插件):Electron 生产包靠这里 stage 的目录做首启 seed,漏掉它
  283. // 生产桌面包就没有设计技能。必需文件与 official-plugin-definitions.ts 的 requiredSeedPaths 一致。
  284. packageName: "@zcode/design-plugin",
  285. relativePath: "apps/zcode-cli/packages/design-plugin",
  286. requiresRuntime: false,
  287. requiredSeedPaths: [
  288. "skills/design/SKILL.md",
  289. "skills/design/references/app.md",
  290. "skills/design/references/catalog.md",
  291. "skills/design/references/check.md",
  292. "skills/design/references/create.md",
  293. "skills/design/references/deck.md",
  294. "skills/design/references/design-system.md",
  295. "skills/design/references/image.md",
  296. "skills/design/references/interact.md",
  297. "skills/design/references/motion.md",
  298. "skills/design/references/page.md",
  299. "skills/design/references/redesign.md",
  300. "skills/design/scripts/adapt-canvas.mjs",
  301. "skills/design/scripts/audit-page.js",
  302. "skills/design/scripts/audit.mjs",
  303. "skills/design/scripts/design-lint.mjs",
  304. "skills/design/scripts/design-tokens.mjs",
  305. "skills/design/scripts/import-design-system.mjs",
  306. "skills/design/scripts/probe.js",
  307. "hooks/hooks.json",
  308. "hooks/run-hook.cmd",
  309. "hooks/design-rules",
  310. ],
  311. requiredRuntimePaths: [],
  312. stagedPath: "packages/design-plugin",
  313. },
  314. // superpowers 已从内置插件下线,改走 UI 推荐区按需安装;这里不再 stage 它的资源。
  315. ];
  316. // 随 CLI 内置的技能包(不是插件):bootstrap 的 resolveBundledSkillRoots 沿官方插件同款候选目录
  317. // 在 zcode.cjs 旁找 packages/bundled-skills 并原地读取。漏 stage 它,桌面包的 /workflow 会展开成
  318. // 「先加载 dynamic-workflows 技能」而技能不存在——这正是把它从可卸载的 zcode-guide 插件搬出来要修的事故形态。
  319. const bundledSkillPack = {
  320. relativePath: "apps/zcode-cli/packages/bundled-skills",
  321. requiredPaths: [
  322. "skills/dynamic-workflows/SKILL.md",
  323. "skills/dynamic-workflows/patterns.md",
  324. "skills/dynamic-workflows/examples.md",
  325. ],
  326. stagedPath: "packages/bundled-skills",
  327. topLevelPaths: ["README.md", "skills"],
  328. };
  329. const includedOfficialPluginTopLevelPaths = new Set([
  330. ".mcp.json",
  331. ".zcode-plugin",
  332. "README.md",
  333. // Electron 生产资源复制有独立白名单,遗漏 agents 会让首启 filesystem seed 永久缺少子代理。
  334. "agents",
  335. "commands",
  336. "dist",
  337. "docs",
  338. "hooks",
  339. "output-styles",
  340. "package.json",
  341. "scripts",
  342. "skills",
  343. "templates",
  344. ]);
  345. const excludedOfficialPluginAssetNames = new Set([
  346. ".DS_Store",
  347. ".venv",
  348. "__pycache__",
  349. "node_modules",
  350. ]);
  351. function shouldCopyOfficialPluginAsset(sourcePath) {
  352. const name = basename(sourcePath);
  353. return !excludedOfficialPluginAssetNames.has(name) && !name.endsWith(".pyc");
  354. }
  355. const isBootstrapWithRemote = process.env.ZCODE_BOOTSTRAP_WITH_REMOTE === "1";
  356. function buildCliBundle() {
  357. console.log("[prepare:agent-bundle] building zcode-cli app-server bundle ...");
  358. // 复用仓库根脚本(turbo build:desktop-agent --filter=@zcode/cli),命中缓存时几乎瞬时。
  359. runCommand(process.execPath, [resolve(repoRoot, "scripts/build-desktop-agent-cli.mjs")], {
  360. cwd: repoRoot,
  361. env: pnpmRunEnv,
  362. });
  363. if (!existsSync(cliBundlePath)) {
  364. throw new Error(
  365. `[prepare:agent-bundle] expected cli bundle missing after build: ${cliBundlePath}`,
  366. );
  367. }
  368. }
  369. function buildOfficialPluginRuntimes() {
  370. for (const plugin of officialPluginPackages) {
  371. if (!plugin.requiresRuntime) continue;
  372. console.log(`[prepare:agent-bundle] building ${plugin.packageName} runtime ...`);
  373. if (isBootstrapWithRemote) {
  374. buildOfficialPluginRuntimeForBootstrap(plugin);
  375. assertOfficialPluginRuntime(plugin);
  376. continue;
  377. }
  378. runCommand(
  379. "pnpm",
  380. ["--dir", resolve(repoRoot, "apps/zcode-cli"), "--filter", plugin.packageName, "build"],
  381. {
  382. cwd: repoRoot,
  383. env: pnpmRunEnv,
  384. },
  385. );
  386. assertOfficialPluginRuntime(plugin);
  387. }
  388. }
  389. function buildOfficialPluginRuntimeForBootstrap(plugin) {
  390. const pluginRoot = resolve(repoRoot, plugin.relativePath);
  391. // Bugfix: bootstrap:with-remote 会连续构建 remote assets 和桌面 agent bundle。
  392. // 通过 pnpm/filter 进入插件 build 时,tsc shim 在本地低内存环境中容易被 SIGKILL;
  393. // 这里仅在 bootstrap 开关下用当前 Node 直接执行等价 tsc + build-mcp,不改变插件自身 build 脚本。
  394. // Bug 原因:过去除 browser-use 外只要 dist 文件齐全就复用,源码变化后 bootstrap 仍会
  395. // 打包上一代插件 runtime;所有代码型官方插件都必须在本轮重建。
  396. rmSync(resolve(pluginRoot, "dist"), { recursive: true, force: true });
  397. // 文档插件没有自己的 tsconfig(源码在 office-runtime),直接跑 tsc 会因找不到工程配置失败;
  398. // 只有带 tsconfig 的插件才需要这一步。
  399. if (existsSync(resolve(pluginRoot, "tsconfig.json"))) {
  400. runCommand(process.execPath, ["../../node_modules/typescript/bin/tsc"], {
  401. cwd: pluginRoot,
  402. env: process.env,
  403. });
  404. }
  405. runCommand(process.execPath, [plugin.runtimeBuildScript], {
  406. cwd: pluginRoot,
  407. env: process.env,
  408. });
  409. }
  410. function assertOfficialPluginRuntime(plugin) {
  411. const pluginRoot = resolve(repoRoot, plugin.relativePath);
  412. for (const relativePath of plugin.requiredRuntimePaths) {
  413. const runtimePath = resolve(pluginRoot, ...relativePath.split("/"));
  414. if (!existsSync(runtimePath)) {
  415. throw new Error(`[prepare:agent-bundle] missing official plugin runtime: ${runtimePath}`);
  416. }
  417. }
  418. }
  419. async function stageBundle() {
  420. // 实现已抽到 stage-agent-bundle.mjs:dev 链(scripts/build-desktop-agent-cli.mjs)
  421. // 必须用同一份,否则 dev 会继续跑上一次打包留下的陈旧 agent。
  422. await stageAgentBundle({ repoRoot, platformKey });
  423. }
  424. function stageOfficialPlugins() {
  425. for (const plugin of officialPluginPackages) {
  426. const sourceRoot = resolve(repoRoot, plugin.relativePath);
  427. const manifestPath = resolve(sourceRoot, ".zcode-plugin", "plugin.json");
  428. if (!existsSync(manifestPath)) {
  429. throw new Error(`[prepare:agent-bundle] missing official plugin manifest: ${manifestPath}`);
  430. }
  431. const targetRoot = resolve(glmDir, plugin.stagedPath);
  432. mkdirSync(targetRoot, { recursive: true });
  433. for (const entryName of includedOfficialPluginTopLevelPaths) {
  434. const sourcePath = resolve(sourceRoot, entryName);
  435. if (!existsSync(sourcePath)) continue;
  436. cpSync(sourcePath, resolve(targetRoot, entryName), {
  437. recursive: true,
  438. filter: shouldCopyOfficialPluginAsset,
  439. });
  440. }
  441. for (const relativePath of plugin.requiredSeedPaths ?? []) {
  442. const stagedAssetPath = resolve(targetRoot, ...relativePath.split("/"));
  443. if (!existsSync(stagedAssetPath)) {
  444. throw new Error(
  445. `[prepare:agent-bundle] missing staged official plugin seed asset: ${stagedAssetPath}`,
  446. );
  447. }
  448. }
  449. console.log(`[prepare:agent-bundle] staged official plugin ${plugin.stagedPath}`);
  450. }
  451. }
  452. function stageBundledSkillPack() {
  453. const sourceRoot = resolve(repoRoot, bundledSkillPack.relativePath);
  454. const targetRoot = resolve(glmDir, bundledSkillPack.stagedPath);
  455. mkdirSync(targetRoot, { recursive: true });
  456. for (const entryName of bundledSkillPack.topLevelPaths) {
  457. const sourcePath = resolve(sourceRoot, entryName);
  458. if (!existsSync(sourcePath)) continue;
  459. cpSync(sourcePath, resolve(targetRoot, entryName), {
  460. recursive: true,
  461. filter: shouldCopyOfficialPluginAsset,
  462. });
  463. }
  464. for (const relativePath of bundledSkillPack.requiredPaths) {
  465. const stagedAssetPath = resolve(targetRoot, ...relativePath.split("/"));
  466. if (!existsSync(stagedAssetPath)) {
  467. throw new Error(
  468. `[prepare:agent-bundle] missing staged bundled skill asset: ${stagedAssetPath}`,
  469. );
  470. }
  471. }
  472. console.log(`[prepare:agent-bundle] staged bundled skill pack ${bundledSkillPack.stagedPath}`);
  473. }
  474. // stdio MCP 的 npm/npx 回退(用户没装 Node 时由 Electron Node 运行随包 npm)。
  475. // tarball 按 sha512 缓存在 node_modules/.cache,重复打包不再下载。
  476. async function stageBundledNpmRuntime() {
  477. const stagedRoot = await stageBundledNpm({
  478. cacheDir: resolve(repoRoot, "node_modules", ".cache", "zcode-bundled-npm"),
  479. glmDir,
  480. });
  481. console.log(`[prepare:agent-bundle] staged npm@${BUNDLED_NPM_VERSION} into ${stagedRoot}`);
  482. }
  483. function stageSharpForNodeReplPlugin() {
  484. if (!isSharpRequiredByComputerUse({ desktopPackageRoot: desktopRoot })) {
  485. console.log(
  486. "[prepare:agent-bundle] Computer Use package does not declare sharp, skip sharp staging",
  487. );
  488. return;
  489. }
  490. // `sharp` 仍然外部化——它包含无法内联的 native .node,运行时从**所在包**的 node_modules
  491. // 解析。2026-09-11 起 node_repl 宿主的 bundle 归 @zcode/node-repl-host,所以 sharp 必须
  492. // stage 到宿主目录,否则宿主里的 `import sharp` 在打包后的 app 里解析不到。
  493. // CUA screenshot / get_app_state 的 native 依赖仍要放进 CUA plugin 自己的 node_modules;
  494. // Browser Use 目录保留一份兼容 staging。三份 staging 都位于 plugin 子目录下,避免
  495. // electron-builder 过滤 extraResources 根目录的 node_modules。
  496. const pluginStagedDirs = [
  497. [NODE_REPL_HOST_PACKAGE_NAME, resolve(glmDir, "packages", "node-repl-host")],
  498. [BROWSER_USE_PLUGIN_PACKAGE_NAME, resolve(glmDir, "packages", "browser-use-plugin")],
  499. [CUA_PLUGIN_PACKAGE_NAME, resolve(glmDir, "packages", "zcode-cua-plugin")],
  500. ];
  501. for (const [packageName, pluginStagedDir] of pluginStagedDirs) {
  502. if (!existsSync(resolve(pluginStagedDir, ".zcode-plugin", "plugin.json"))) {
  503. console.log(`[prepare:agent-bundle] ${packageName} not staged, skip sharp staging`);
  504. continue;
  505. }
  506. stageSharpIntoBundledAgents({
  507. desktopPackageRoot: desktopRoot,
  508. glmDir: pluginStagedDir,
  509. targetPlatform: { os: platform, arch },
  510. });
  511. console.log(
  512. `[prepare:agent-bundle] staged sharp into ${resolve(pluginStagedDir, "node_modules")} (${platform}-${arch})`,
  513. );
  514. }
  515. }
  516. function stageKoffiForCuaPlugin() {
  517. const cuaPluginStagedDir = resolve(glmDir, "packages", "zcode-cua-plugin");
  518. if (!existsSync(resolve(cuaPluginStagedDir, ".zcode-plugin", "plugin.json"))) {
  519. console.log("[prepare:agent-bundle] zcode-cua-plugin not staged, skip koffi staging");
  520. return;
  521. }
  522. stageKoffiIntoBundledAgents({
  523. koffiPackageRoot: adaptersRoot,
  524. glmDir: cuaPluginStagedDir,
  525. targetPlatform: { os: platform, arch },
  526. });
  527. console.log(
  528. `[prepare:agent-bundle] staged koffi into ${resolve(cuaPluginStagedDir, "node_modules")} (${platform}-${arch})`,
  529. );
  530. }
  531. // 修复原因:Electron 生产包只带 resources/glm/zcode.cjs 时,app-server 进程的
  532. // __dirname 附近没有官方插件目录,启动时 seed 找不到 source,用户侧不会自动得到内置插件。
  533. // 这里把官方插件按 bootstrap 的 rootCandidates 期望放到 glm/packages/*-plugin,
  534. // 让 Electron Node 运行 zcode.cjs 时复用同一套 filesystem seed 逻辑。
  535. // browser-use runtime 的声明生成依赖 @zcode/core/dist。CI 干净检出没有该产物,
  536. // 必须先构建 CLI 依赖,再构建官方插件;开发机残留的 dist 曾掩盖这个顺序问题。
  537. buildCliBundle();
  538. buildOfficialPluginRuntimes();
  539. await stageBundle();
  540. stageOfficialPlugins();
  541. stageBundledSkillPack();
  542. await stageBundledNpmRuntime();
  543. stageKoffiForCuaPlugin();
  544. stageSharpForNodeReplPlugin();