prepare-agent-node-bundle.mjs 21 KB

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