prepare-agent-node-bundle.mjs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 officeViewerRuntimePaths = ["dist/server.mjs", "dist/ui/assets.json", "dist/ui/panel.html"];
  96. const officialPluginPackages = [
  97. {
  98. packageName: "@zcode/android-emulator-plugin",
  99. relativePath: "apps/zcode-cli/packages/android-emulator-plugin",
  100. requiresRuntime: true,
  101. requiredRuntimePaths: ["dist/mcp/server.js"],
  102. runtimeBuildScript: "scripts/build-mcp.mjs",
  103. stagedPath: "packages/android-emulator-plugin",
  104. },
  105. {
  106. packageName: "@zcode/documents-plugin",
  107. relativePath: "apps/zcode-cli/packages/documents-plugin",
  108. requiresRuntime: true,
  109. requiredRuntimePaths: officeViewerRuntimePaths,
  110. requiredSeedPaths: [
  111. "agents/visual-judge.md",
  112. "skills/docx/SKILL.md",
  113. "skills/docx-viewer/SKILL.md",
  114. ],
  115. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  116. stagedPath: "packages/documents-plugin",
  117. },
  118. {
  119. packageName: "@zcode/image-search-plugin",
  120. relativePath: "apps/zcode-cli/packages/image-search-plugin",
  121. requiresRuntime: false,
  122. requiredSeedPaths: [".mcp.json"],
  123. stagedPath: "packages/image-search-plugin",
  124. },
  125. {
  126. packageName: "@zcode/pdf-plugin",
  127. relativePath: "apps/zcode-cli/packages/pdf-plugin",
  128. requiresRuntime: true,
  129. requiredRuntimePaths: officeViewerRuntimePaths,
  130. requiredSeedPaths: [
  131. "agents/visual-judge.md",
  132. "skills/pdf/SKILL.md",
  133. "skills/pdf-viewer/SKILL.md",
  134. ],
  135. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  136. stagedPath: "packages/pdf-plugin",
  137. },
  138. {
  139. packageName: "@zcode/presentations-plugin",
  140. relativePath: "apps/zcode-cli/packages/presentations-plugin",
  141. requiresRuntime: true,
  142. requiredRuntimePaths: officeViewerRuntimePaths,
  143. requiredSeedPaths: [
  144. "agents/visual-judge.md",
  145. "skills/pptx/SKILL.md",
  146. "skills/pptx-viewer/SKILL.md",
  147. ],
  148. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  149. stagedPath: "packages/presentations-plugin",
  150. },
  151. {
  152. packageName: "@zcode/spreadsheets-plugin",
  153. relativePath: "apps/zcode-cli/packages/spreadsheets-plugin",
  154. requiresRuntime: true,
  155. requiredRuntimePaths: officeViewerRuntimePaths,
  156. requiredSeedPaths: [
  157. "agents/visual-judge.md",
  158. "skills/xlsx/SKILL.md",
  159. "skills/xlsx-viewer/SKILL.md",
  160. ],
  161. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  162. stagedPath: "packages/spreadsheets-plugin",
  163. },
  164. {
  165. packageName: "@zcode/visualize-plugin",
  166. relativePath: "apps/zcode-cli/packages/visualize-plugin",
  167. requiresRuntime: false,
  168. requiredSeedPaths: [
  169. "skills/visualize/SKILL.md",
  170. "skills/visualize/references/api.md",
  171. "skills/visualize/references/styles.md",
  172. "skills/visualize/tweak.md",
  173. "skills/visualize/LICENSE.md",
  174. "skills/visualize/scripts/render.py",
  175. "skills/visualize/assets/visualize.css",
  176. "skills/visualize/assets/visualize.html",
  177. "skills/visualize/assets/calendar.js",
  178. "skills/visualize/assets/runtime-manifest.json",
  179. "skills/visualize/scripts/vendor.py",
  180. "skills/visualize/assets/vendor/manifest.json",
  181. "skills/visualize/assets/vendor/floating-ui-core-1.7.3.min.js",
  182. "skills/visualize/assets/vendor/floating-ui-core-1.7.3.min.js.LICENSE",
  183. "skills/visualize/assets/vendor/floating-ui-dom-1.7.4.min.js",
  184. "skills/visualize/assets/vendor/floating-ui-dom-1.7.4.min.js.LICENSE",
  185. "skills/visualize/assets/vendor/lucide-1.17.0.js",
  186. "skills/visualize/assets/vendor/lucide-1.17.0.js.LICENSE",
  187. "skills/visualize/assets/vendor/d3-7.9.0.min.js",
  188. "skills/visualize/assets/vendor/d3-7.9.0.min.js.LICENSE",
  189. "skills/visualize/widgets/calendar.md",
  190. "skills/visualize/examples/calendar.html",
  191. "skills/visualize/assets/standalone-host-bridge.js",
  192. "skills/visualize/assets/standalone-shell.js",
  193. ],
  194. requiredRuntimePaths: [],
  195. stagedPath: "packages/visualize-plugin",
  196. },
  197. {
  198. // browser-use 只携带自己的 client script 与 skill/docs;node_repl MCP runtime 归
  199. // @zcode/node-repl-host(见上方常量注释)。
  200. packageName: "@zcode/browser-use-plugin",
  201. relativePath: "apps/zcode-cli/packages/browser-use-plugin",
  202. requiresRuntime: true,
  203. requiredRuntimePaths: browserUseRequiredRuntimePaths,
  204. runtimeBuildScript: "scripts/build.mjs",
  205. stagedPath: "packages/browser-use-plugin",
  206. },
  207. {
  208. packageName: "@zcode/ios-simulator-plugin",
  209. relativePath: "apps/zcode-cli/packages/ios-simulator-plugin",
  210. requiresRuntime: true,
  211. requiredRuntimePaths: ["dist/mcp/server.js"],
  212. runtimeBuildScript: "scripts/build-mcp.mjs",
  213. stagedPath: "packages/ios-simulator-plugin",
  214. },
  215. {
  216. // 修复原因:restore-legacy-sessions 已注册为官方插件,但 Electron 生产包只会拷贝
  217. // 这里列出的资源。漏掉它会导致用户生产包首次启动后 plugins list 里看不到该插件。
  218. packageName: "@zcode/restore-legacy-sessions-plugin",
  219. relativePath: "apps/zcode-cli/packages/restore-legacy-sessions-plugin",
  220. requiresRuntime: false,
  221. stagedPath: "packages/restore-legacy-sessions-plugin",
  222. },
  223. {
  224. packageName: "@zcode/skill-creator-plugin",
  225. relativePath: "apps/zcode-cli/packages/skill-creator-plugin",
  226. requiresRuntime: false,
  227. stagedPath: "packages/skill-creator-plugin",
  228. },
  229. {
  230. packageName: "@zcode/plugin-creator-plugin",
  231. relativePath: "apps/zcode-cli/packages/plugin-creator-plugin",
  232. requiresRuntime: false,
  233. stagedPath: "packages/plugin-creator-plugin",
  234. },
  235. {
  236. // 修复原因:zcode-guide 已注册为官方默认启用内容型插件(见 official-plugin-definitions.ts)。
  237. // Electron 生产包不是 SEA 路径,官方插件 seed 依赖这里 stage 的 resources/glm/packages/*-plugin;
  238. // 漏掉它会导致生产桌面包首启 seed 不到该插件,用户拿不到默认启用的配置指南技能。
  239. packageName: "@zcode/zcode-guide-plugin",
  240. relativePath: "apps/zcode-cli/packages/zcode-guide-plugin",
  241. requiresRuntime: false,
  242. stagedPath: "packages/zcode-guide-plugin",
  243. },
  244. {
  245. // ZCode Sites(默认开启)。漏 stage 会让生产桌面包里找不到它;Sites 面板的 MCP server 与页面由插件自己的
  246. // build 脚本构建进 dist/。
  247. packageName: "@zcode/zcode-site-plugin",
  248. relativePath: "apps/zcode-cli/packages/zcode-site-plugin",
  249. requiresRuntime: true,
  250. requiredRuntimePaths: ["dist/server.mjs", "dist/ui/panel.html"],
  251. runtimeBuildScript: "scripts/build.mjs",
  252. stagedPath: "packages/zcode-site-plugin",
  253. },
  254. {
  255. // node_repl 宿主:Browser Use 与 Computer Use 共用的 MCP runtime,本轮抽成独立包。
  256. // 它没有 listing(不进插件市场展示面),但生产包首启 seed 必须拿到它的 dist runtime,
  257. // 否则 bua/cua 任一开启时都会连不上 node_repl。
  258. packageName: "@zcode/node-repl-host",
  259. relativePath: "apps/zcode-cli/packages/node-repl-host",
  260. requiresRuntime: true,
  261. requiredRuntimePaths: ["dist/mcp/server.js"],
  262. runtimeBuildScript: "scripts/build.mjs",
  263. stagedPath: "packages/node-repl-host",
  264. },
  265. {
  266. // CUA 插件只携带 SDK/skill/docs;node_repl MCP runtime 归 @zcode/node-repl-host。
  267. packageName: "@zcode/zcode-cua-plugin",
  268. relativePath: "apps/zcode-cli/packages/zcode-cua-plugin",
  269. requiresRuntime: false,
  270. stagedPath: "packages/zcode-cua-plugin",
  271. },
  272. {
  273. // design(默认启用内容型插件):Electron 生产包靠这里 stage 的目录做首启 seed,漏掉它
  274. // 生产桌面包就没有设计技能。必需文件与 official-plugin-definitions.ts 的 requiredSeedPaths 一致。
  275. packageName: "@zcode/design-plugin",
  276. relativePath: "apps/zcode-cli/packages/design-plugin",
  277. requiresRuntime: false,
  278. requiredSeedPaths: [
  279. "skills/design/SKILL.md",
  280. "skills/design/references/catalog.md",
  281. "skills/design/references/check.md",
  282. "skills/design/references/create.md",
  283. "skills/design/references/design-system.md",
  284. "skills/design/references/interact.md",
  285. "skills/design/references/output.md",
  286. "skills/design/references/redesign.md",
  287. "skills/design/scripts/adapt-canvas.mjs",
  288. "skills/design/scripts/design-lint.mjs",
  289. "skills/design/scripts/design-tokens.mjs",
  290. "skills/design/scripts/import-design-system.mjs",
  291. "hooks/hooks.json",
  292. "hooks/run-hook.cmd",
  293. "hooks/design-rules",
  294. ],
  295. requiredRuntimePaths: [],
  296. stagedPath: "packages/design-plugin",
  297. },
  298. // superpowers 已从内置插件下线,改走 UI 推荐区按需安装;这里不再 stage 它的资源。
  299. ];
  300. // 随 CLI 内置的技能包(不是插件):bootstrap 的 resolveBundledSkillRoots 沿官方插件同款候选目录
  301. // 在 zcode.cjs 旁找 packages/bundled-skills 并原地读取。漏 stage 它,桌面包的 /workflow 会展开成
  302. // 「先加载 dynamic-workflows 技能」而技能不存在——这正是把它从可卸载的 zcode-guide 插件搬出来要修的事故形态。
  303. const bundledSkillPack = {
  304. relativePath: "apps/zcode-cli/packages/bundled-skills",
  305. requiredPaths: [
  306. "skills/dynamic-workflows/SKILL.md",
  307. "skills/dynamic-workflows/patterns.md",
  308. "skills/dynamic-workflows/examples.md",
  309. ],
  310. stagedPath: "packages/bundled-skills",
  311. topLevelPaths: ["README.md", "skills"],
  312. };
  313. const includedOfficialPluginTopLevelPaths = new Set([
  314. ".mcp.json",
  315. ".zcode-plugin",
  316. "README.md",
  317. // Electron 生产资源复制有独立白名单,遗漏 agents 会让首启 filesystem seed 永久缺少子代理。
  318. "agents",
  319. "commands",
  320. "dist",
  321. "docs",
  322. "hooks",
  323. "output-styles",
  324. "package.json",
  325. "scripts",
  326. "skills",
  327. "templates",
  328. ]);
  329. const excludedOfficialPluginAssetNames = new Set([
  330. ".DS_Store",
  331. ".venv",
  332. "__pycache__",
  333. "node_modules",
  334. ]);
  335. function shouldCopyOfficialPluginAsset(sourcePath) {
  336. const name = basename(sourcePath);
  337. return !excludedOfficialPluginAssetNames.has(name) && !name.endsWith(".pyc");
  338. }
  339. const isBootstrapWithRemote = process.env.ZCODE_BOOTSTRAP_WITH_REMOTE === "1";
  340. function buildCliBundle() {
  341. console.log("[prepare:agent-bundle] building zcode-cli app-server bundle ...");
  342. // 复用仓库根脚本(turbo build:desktop-agent --filter=@zcode/cli),命中缓存时几乎瞬时。
  343. runCommand(process.execPath, [resolve(repoRoot, "scripts/build-desktop-agent-cli.mjs")], {
  344. cwd: repoRoot,
  345. env: pnpmRunEnv,
  346. });
  347. if (!existsSync(cliBundlePath)) {
  348. throw new Error(
  349. `[prepare:agent-bundle] expected cli bundle missing after build: ${cliBundlePath}`,
  350. );
  351. }
  352. }
  353. function buildOfficialPluginRuntimes() {
  354. for (const plugin of officialPluginPackages) {
  355. if (!plugin.requiresRuntime) continue;
  356. console.log(`[prepare:agent-bundle] building ${plugin.packageName} runtime ...`);
  357. if (isBootstrapWithRemote) {
  358. buildOfficialPluginRuntimeForBootstrap(plugin);
  359. assertOfficialPluginRuntime(plugin);
  360. continue;
  361. }
  362. runCommand(
  363. "pnpm",
  364. ["--dir", resolve(repoRoot, "apps/zcode-cli"), "--filter", plugin.packageName, "build"],
  365. {
  366. cwd: repoRoot,
  367. env: pnpmRunEnv,
  368. },
  369. );
  370. assertOfficialPluginRuntime(plugin);
  371. }
  372. }
  373. function buildOfficialPluginRuntimeForBootstrap(plugin) {
  374. const pluginRoot = resolve(repoRoot, plugin.relativePath);
  375. // Bugfix: bootstrap:with-remote 会连续构建 remote assets 和桌面 agent bundle。
  376. // 通过 pnpm/filter 进入插件 build 时,tsc shim 在本地低内存环境中容易被 SIGKILL;
  377. // 这里仅在 bootstrap 开关下用当前 Node 直接执行等价 tsc + build-mcp,不改变插件自身 build 脚本。
  378. // Bug 原因:过去除 browser-use 外只要 dist 文件齐全就复用,源码变化后 bootstrap 仍会
  379. // 打包上一代插件 runtime;所有代码型官方插件都必须在本轮重建。
  380. rmSync(resolve(pluginRoot, "dist"), { recursive: true, force: true });
  381. // 文档插件没有自己的 tsconfig(源码在 office-runtime),直接跑 tsc 会因找不到工程配置失败;
  382. // 只有带 tsconfig 的插件才需要这一步。
  383. if (existsSync(resolve(pluginRoot, "tsconfig.json"))) {
  384. runCommand(process.execPath, ["../../node_modules/typescript/bin/tsc"], {
  385. cwd: pluginRoot,
  386. env: process.env,
  387. });
  388. }
  389. runCommand(process.execPath, [plugin.runtimeBuildScript], {
  390. cwd: pluginRoot,
  391. env: process.env,
  392. });
  393. }
  394. function assertOfficialPluginRuntime(plugin) {
  395. const pluginRoot = resolve(repoRoot, plugin.relativePath);
  396. for (const relativePath of plugin.requiredRuntimePaths) {
  397. const runtimePath = resolve(pluginRoot, ...relativePath.split("/"));
  398. if (!existsSync(runtimePath)) {
  399. throw new Error(`[prepare:agent-bundle] missing official plugin runtime: ${runtimePath}`);
  400. }
  401. }
  402. }
  403. async function stageBundle() {
  404. // 实现已抽到 stage-agent-bundle.mjs:dev 链(scripts/build-desktop-agent-cli.mjs)
  405. // 必须用同一份,否则 dev 会继续跑上一次打包留下的陈旧 agent。
  406. await stageAgentBundle({ repoRoot, platformKey });
  407. }
  408. function stageOfficialPlugins() {
  409. for (const plugin of officialPluginPackages) {
  410. const sourceRoot = resolve(repoRoot, plugin.relativePath);
  411. const manifestPath = resolve(sourceRoot, ".zcode-plugin", "plugin.json");
  412. if (!existsSync(manifestPath)) {
  413. throw new Error(`[prepare:agent-bundle] missing official plugin manifest: ${manifestPath}`);
  414. }
  415. const targetRoot = resolve(glmDir, plugin.stagedPath);
  416. mkdirSync(targetRoot, { recursive: true });
  417. for (const entryName of includedOfficialPluginTopLevelPaths) {
  418. const sourcePath = resolve(sourceRoot, entryName);
  419. if (!existsSync(sourcePath)) continue;
  420. cpSync(sourcePath, resolve(targetRoot, entryName), {
  421. recursive: true,
  422. filter: shouldCopyOfficialPluginAsset,
  423. });
  424. }
  425. for (const relativePath of plugin.requiredSeedPaths ?? []) {
  426. const stagedAssetPath = resolve(targetRoot, ...relativePath.split("/"));
  427. if (!existsSync(stagedAssetPath)) {
  428. throw new Error(
  429. `[prepare:agent-bundle] missing staged official plugin seed asset: ${stagedAssetPath}`,
  430. );
  431. }
  432. }
  433. console.log(`[prepare:agent-bundle] staged official plugin ${plugin.stagedPath}`);
  434. }
  435. }
  436. function stageBundledSkillPack() {
  437. const sourceRoot = resolve(repoRoot, bundledSkillPack.relativePath);
  438. const targetRoot = resolve(glmDir, bundledSkillPack.stagedPath);
  439. mkdirSync(targetRoot, { recursive: true });
  440. for (const entryName of bundledSkillPack.topLevelPaths) {
  441. const sourcePath = resolve(sourceRoot, entryName);
  442. if (!existsSync(sourcePath)) continue;
  443. cpSync(sourcePath, resolve(targetRoot, entryName), {
  444. recursive: true,
  445. filter: shouldCopyOfficialPluginAsset,
  446. });
  447. }
  448. for (const relativePath of bundledSkillPack.requiredPaths) {
  449. const stagedAssetPath = resolve(targetRoot, ...relativePath.split("/"));
  450. if (!existsSync(stagedAssetPath)) {
  451. throw new Error(
  452. `[prepare:agent-bundle] missing staged bundled skill asset: ${stagedAssetPath}`,
  453. );
  454. }
  455. }
  456. console.log(`[prepare:agent-bundle] staged bundled skill pack ${bundledSkillPack.stagedPath}`);
  457. }
  458. // stdio MCP 的 npm/npx 回退(用户没装 Node 时由 Electron Node 运行随包 npm)。
  459. // tarball 按 sha512 缓存在 node_modules/.cache,重复打包不再下载。
  460. async function stageBundledNpmRuntime() {
  461. const stagedRoot = await stageBundledNpm({
  462. cacheDir: resolve(repoRoot, "node_modules", ".cache", "zcode-bundled-npm"),
  463. glmDir,
  464. });
  465. console.log(`[prepare:agent-bundle] staged npm@${BUNDLED_NPM_VERSION} into ${stagedRoot}`);
  466. }
  467. function stageSharpForNodeReplPlugin() {
  468. if (!isSharpRequiredByComputerUse({ desktopPackageRoot: desktopRoot })) {
  469. console.log(
  470. "[prepare:agent-bundle] Computer Use package does not declare sharp, skip sharp staging",
  471. );
  472. return;
  473. }
  474. // `sharp` 仍然外部化——它包含无法内联的 native .node,运行时从**所在包**的 node_modules
  475. // 解析。2026-09-11 起 node_repl 宿主的 bundle 归 @zcode/node-repl-host,所以 sharp 必须
  476. // stage 到宿主目录,否则宿主里的 `import sharp` 在打包后的 app 里解析不到。
  477. // CUA screenshot / get_app_state 的 native 依赖仍要放进 CUA plugin 自己的 node_modules;
  478. // Browser Use 目录保留一份兼容 staging。三份 staging 都位于 plugin 子目录下,避免
  479. // electron-builder 过滤 extraResources 根目录的 node_modules。
  480. const pluginStagedDirs = [
  481. [NODE_REPL_HOST_PACKAGE_NAME, resolve(glmDir, "packages", "node-repl-host")],
  482. [BROWSER_USE_PLUGIN_PACKAGE_NAME, resolve(glmDir, "packages", "browser-use-plugin")],
  483. [CUA_PLUGIN_PACKAGE_NAME, resolve(glmDir, "packages", "zcode-cua-plugin")],
  484. ];
  485. for (const [packageName, pluginStagedDir] of pluginStagedDirs) {
  486. if (!existsSync(resolve(pluginStagedDir, ".zcode-plugin", "plugin.json"))) {
  487. console.log(`[prepare:agent-bundle] ${packageName} not staged, skip sharp staging`);
  488. continue;
  489. }
  490. stageSharpIntoBundledAgents({
  491. desktopPackageRoot: desktopRoot,
  492. glmDir: pluginStagedDir,
  493. targetPlatform: { os: platform, arch },
  494. });
  495. console.log(
  496. `[prepare:agent-bundle] staged sharp into ${resolve(pluginStagedDir, "node_modules")} (${platform}-${arch})`,
  497. );
  498. }
  499. }
  500. function stageKoffiForCuaPlugin() {
  501. const cuaPluginStagedDir = resolve(glmDir, "packages", "zcode-cua-plugin");
  502. if (!existsSync(resolve(cuaPluginStagedDir, ".zcode-plugin", "plugin.json"))) {
  503. console.log("[prepare:agent-bundle] zcode-cua-plugin not staged, skip koffi staging");
  504. return;
  505. }
  506. stageKoffiIntoBundledAgents({
  507. koffiPackageRoot: adaptersRoot,
  508. glmDir: cuaPluginStagedDir,
  509. targetPlatform: { os: platform, arch },
  510. });
  511. console.log(
  512. `[prepare:agent-bundle] staged koffi into ${resolve(cuaPluginStagedDir, "node_modules")} (${platform}-${arch})`,
  513. );
  514. }
  515. // 修复原因:Electron 生产包只带 resources/glm/zcode.cjs 时,app-server 进程的
  516. // __dirname 附近没有官方插件目录,启动时 seed 找不到 source,用户侧不会自动得到内置插件。
  517. // 这里把官方插件按 bootstrap 的 rootCandidates 期望放到 glm/packages/*-plugin,
  518. // 让 Electron Node 运行 zcode.cjs 时复用同一套 filesystem seed 逻辑。
  519. // browser-use runtime 的声明生成依赖 @zcode/core/dist。CI 干净检出没有该产物,
  520. // 必须先构建 CLI 依赖,再构建官方插件;开发机残留的 dist 曾掩盖这个顺序问题。
  521. buildCliBundle();
  522. buildOfficialPluginRuntimes();
  523. await stageBundle();
  524. stageOfficialPlugins();
  525. stageBundledSkillPack();
  526. await stageBundledNpmRuntime();
  527. stageKoffiForCuaPlugin();
  528. stageSharpForNodeReplPlugin();