build-desktop-agent-cli.mjs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { access } from "node:fs/promises";
  2. import { dirname, resolve } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import { stageAgentBundle } from "../packages/desktop/scripts/stage-agent-bundle.mjs";
  5. import { runCommand } from "./spawn-command.mjs";
  6. // adapters tsc 在内存受限机器上会 OOM(exit 134),给整条构建链路提高堆上限。
  7. process.env.NODE_OPTIONS = `${process.env.NODE_OPTIONS ? process.env.NODE_OPTIONS + " " : ""}--max-old-space-size=8192`;
  8. import { stageDevCuaPluginRuntime } from "./stage-dev-cua-plugin-runtime.mjs";
  9. import { resolveBuiltinProviderBuildEnvironment } from "./builtin-provider-config.mjs";
  10. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  11. const useTurboBuild = process.env.ZCODE_DESKTOP_AGENT_BUILD_MODE === "turbo";
  12. const useBootstrapWithRemoteBuild = process.env.ZCODE_BOOTSTRAP_WITH_REMOTE === "1";
  13. const pnpmRunEnv = {
  14. ...process.env,
  15. ZCODE_ENV: await resolveBuiltinProviderBuildEnvironment({ root: repoRoot }),
  16. // pnpm 11 的 verify-deps-before-run 会在 apps/zcode-cli 子 workspace
  17. // 执行每个 run 前触发 pnpm install;子 workspace 运行时依赖根仓库 @zcode/shared,
  18. // 自动 install 无法解析根 workspace 包,导致 dev:desktop:test 和 E2E onPrepare 失败。
  19. PNPM_CONFIG_VERIFY_DEPS_BEFORE_RUN: "false",
  20. };
  21. // Bug 根因:filesystem plugin seed 若缺少 Sharp closure,会一直到 screenshot/zoom 才报错。
  22. // 此脚本同时服务本地 Dev、prebuild、server CLI 和 packaged desktop agent;三条构建分支
  23. // 都必须补齐同一份可复制 runtime。ZCODE_CUA_DEV_MODE 只传给 staging helper,不进入最终
  24. // product/runtime 环境,因此这里描述的是 staging 输入,而不是产品运行模式。
  25. const cuaPluginRuntimeStagingEnv = {
  26. ...process.env,
  27. ZCODE_CUA_DEV_MODE: "1",
  28. };
  29. // 修复原因:桌面 Agent 构建有普通 pnpm 和 bootstrap:with-remote 直跑 tsc 两条路径。
  30. // 过去两条路径分别维护依赖顺序,新增 @zcode/telemetry 后只更新了 bootstrap 依赖,
  31. // 干净 CI 中 telemetry/dist 尚不存在,bootstrap 会因无法解析类型入口而失败。
  32. // 两条路径统一从这一份有序清单派生,避免后续新增 workspace 依赖时再次漂移。
  33. const cliWorkspaceBuilds = [
  34. { packageName: "@zcode/shared-types", packageDir: "shared-types" },
  35. { packageName: "@zcode/contracts", packageDir: "contracts" },
  36. // dynamic-workflow 的 tsc 构建依赖 gitignored 的 libs.generated.ts,
  37. // 而 bare-tsc 路径(runBootstrapWithRemoteBuild)不会执行 package build script,
  38. // 所以需要先跑生成脚本;必须排在 @zcode/core 之前,core 依赖 dynamic-workflow。
  39. {
  40. packageName: "@zcode/dynamic-workflow",
  41. packageDir: "dynamic-workflow",
  42. prepareScript: "scripts/generate-libs.mjs",
  43. },
  44. // dynamic-workflow-runtime 的类型入口是 dist/index.d.ts,必须先于 bootstrap 构建。
  45. { packageName: "@zcode/dynamic-workflow-runtime", packageDir: "dynamic-workflow-runtime" },
  46. { packageName: "@zcode/core", packageDir: "core" },
  47. { packageName: "@zcode/adapters", packageDir: "adapters" },
  48. { packageName: "@zcode/i18n", packageDir: "i18n" },
  49. { packageName: "@zcode/telemetry", packageDir: "telemetry" },
  50. { packageName: "@zcode/bootstrap", packageDir: "bootstrap" },
  51. ];
  52. // 官方插件 manifest 可以在 server.js 缺失时被 filesystem seed,直到 session
  53. // 连接 MCP 才报错,造成“Helper ready 但 CUA 工具不存在”的半启动状态。所有普通 Dev 必需的
  54. // 独立 MCP runtime 必须集中登记,并在构建后验证真实入口文件,再允许 Agent bundle 启动。
  55. // Bug 原因:本脚本同时是 Source Server archive staging(stageCli.ts ensureAgentBundle)唯一的
  56. // 插件构建入口。legacy prepare-prebuilds.mjs 删除后,android-emulator / ios-simulator 的
  57. // dist/mcp/server.js 不再被任何远端构建步骤产出,而插件 dist 被 gitignore,干净 CI 会把缺 MCP
  58. // 入口的插件打进 archive。凡 plugin.json 声明了 mcpServers 的官方插件都必须登记在此。
  59. const requiredDevPluginRuntimeBuilds = [
  60. {
  61. packageName: "@zcode/android-emulator-plugin",
  62. artifactPath: "android-emulator-plugin/dist/mcp/server.js",
  63. },
  64. {
  65. packageName: "@zcode/ios-simulator-plugin",
  66. artifactPath: "ios-simulator-plugin/dist/mcp/server.js",
  67. },
  68. {
  69. // node_repl 宿主:Browser Use 与 Computer Use 共用,产物归属独立包。
  70. packageName: "@zcode/node-repl-host",
  71. artifactPath: "node-repl-host/dist/mcp/server.js",
  72. },
  73. {
  74. // browser-use 自己的 runtime 只剩 browser-client;宿主不再由它携带。
  75. packageName: "@zcode/browser-use-plugin",
  76. artifactPath: "browser-use-plugin/scripts/browser-client.mjs",
  77. },
  78. // 文档插件的查看面板 server 由 office-runtime 构建;dev 若不构建,面板入口可见但 MCP 启动失败。
  79. ...["documents", "pdf", "presentations", "spreadsheets"].map((name) => ({
  80. packageName: `@zcode/${name}-plugin`,
  81. artifactPath: `${name}-plugin/dist/server.mjs`,
  82. })),
  83. // Sites 面板的 MCP server 由插件自己构建;不构建时面板入口可见但 MCP 启动失败。
  84. { packageName: "@zcode/zcode-site-plugin", artifactPath: "zcode-site-plugin/dist/server.mjs" },
  85. ];
  86. const defaultBuildFilters = [
  87. ...cliWorkspaceBuilds.map(({ packageName }) => packageName),
  88. ...requiredDevPluginRuntimeBuilds.map(({ packageName }) => packageName),
  89. ];
  90. async function verifyRequiredDevPluginRuntimeArtifacts() {
  91. for (const runtime of requiredDevPluginRuntimeBuilds) {
  92. const artifactPath = resolve(repoRoot, "apps/zcode-cli/packages", runtime.artifactPath);
  93. try {
  94. await access(artifactPath);
  95. } catch (error) {
  96. throw new Error(
  97. `[build-desktop-agent-cli] ${runtime.packageName} build succeeded without required MCP runtime: ${artifactPath}`,
  98. { cause: error },
  99. );
  100. }
  101. }
  102. }
  103. /**
  104. * 把刚构建出的 agent bundle 暂存进 bundled-agents。
  105. *
  106. * 必须做:dev 未打包时 agent 二进制由 desktopRuntimeEnv.ts 的
  107. * resolveBundledZCodeAgentBinaryPath() 解析,候选**只有** bundled-agents/,没有
  108. * cli/dist/。只靠打包链暂存会让 dev 一直跑上一次打包留下的那份 —— 实测陈旧
  109. * 3 天,任何 agent CLI 侧改动在 dev 里静默不生效,把「改动没进去」伪装成「代码没作用」。
  110. * 实现与打包链共用 stage-agent-bundle.mjs,两边不可能再各自漂移。
  111. *
  112. * dev 只跑宿主平台,所以 platformKey 直接取 process;打包链的跨平台 target 由它自己解析。
  113. */
  114. function stageDevAgentBundle() {
  115. stageAgentBundle({
  116. repoRoot,
  117. platformKey: `${process.platform}-${process.arch}`,
  118. });
  119. }
  120. function runBootstrapWithRemoteBuild() {
  121. // Bug 原因:bootstrap 过去把 dist/zcode.cjs 存在当作本轮已构建,源码变化后仍会
  122. // 把上一代 Agent 放进 Source Server archive。bootstrap 成功必须代表本轮真实重建。
  123. for (const { packageDir, prepareScript } of cliWorkspaceBuilds) {
  124. // bootstrap:with-remote 会在 remote assets 阶段构建 agent bundle。
  125. // 通过 pnpm 逐包执行 tsc 时会再走 shim/env node 层,低内存本地环境里容易长时间卡住。
  126. // 这里只有 bootstrap 专用环境变量生效,直接复用当前 Node 启动 TypeScript CLI。
  127. // bare tsc 绕过 package build script,所以带 prepareScript 的包(dynamic-workflow)
  128. // 必须先手动跑生成脚本补齐 gitignored 的 libs.generated.ts,否则 tsc 因缺文件报错。
  129. if (prepareScript) {
  130. runCommand(process.execPath, [prepareScript], {
  131. cwd: `apps/zcode-cli/packages/${packageDir}`,
  132. env: pnpmRunEnv,
  133. stdio: "inherit",
  134. });
  135. }
  136. runCommand(process.execPath, ["../../node_modules/typescript/bin/tsc"], {
  137. cwd: `apps/zcode-cli/packages/${packageDir}`,
  138. env: pnpmRunEnv,
  139. stdio: "inherit",
  140. });
  141. }
  142. runCommand(process.execPath, ["scripts/build.mjs", "--desktop-agent"], {
  143. cwd: "apps/zcode-cli/packages/cli",
  144. env: pnpmRunEnv,
  145. stdio: "inherit",
  146. });
  147. }
  148. if (useBootstrapWithRemoteBuild) {
  149. runBootstrapWithRemoteBuild();
  150. stageDevAgentBundle();
  151. stageDevCuaPluginRuntime({ env: cuaPluginRuntimeStagingEnv });
  152. process.exit(0);
  153. }
  154. if (!useTurboBuild) {
  155. // Linux 容器 demo 里没有仓库级 turbo 根,`turbo --cwd apps/zcode-cli`
  156. // 会把 apps/zcode-cli 当根目录,并拒绝 turbo.json 中指向 ../../packages/shared 的 inputs。
  157. // 同时 agent 子 workspace 不包含根 packages/shared,但 agent 包依赖 @zcode/shared。
  158. // 因此默认改用仓库根 workspace 的明确 pnpm 包顺序构建,避免 WDIO 前置构建卡在子 workspace 解析。
  159. for (const filter of defaultBuildFilters) {
  160. runCommand("pnpm", ["--filter", filter, "build"], {
  161. env: pnpmRunEnv,
  162. stdio: "inherit",
  163. });
  164. }
  165. await verifyRequiredDevPluginRuntimeArtifacts();
  166. runCommand("pnpm", ["--filter", "@zcode/cli", "build:desktop-agent"], {
  167. env: pnpmRunEnv,
  168. stdio: "inherit",
  169. });
  170. stageDevAgentBundle();
  171. stageDevCuaPluginRuntime({ env: cuaPluginRuntimeStagingEnv });
  172. process.exit(0);
  173. }
  174. runCommand(
  175. "pnpm",
  176. [
  177. "exec",
  178. "turbo",
  179. "--skip-infer",
  180. "--cwd",
  181. "apps/zcode-cli",
  182. "run",
  183. "build:desktop-agent",
  184. "--filter=@zcode/cli",
  185. ],
  186. {
  187. env: pnpmRunEnv,
  188. stdio: "inherit",
  189. },
  190. );
  191. stageDevAgentBundle();
  192. stageDevCuaPluginRuntime({ env: cuaPluginRuntimeStagingEnv });