dev-desktop-env.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { spawn } from "node:child_process";
  2. import { readFileSync } from "node:fs";
  3. import { fileURLToPath } from "node:url";
  4. import { dirname, join, resolve } from "node:path";
  5. import { isRealComputerUseProducerInstalled } from "../packages/desktop/scripts/computer-use-producer.mjs";
  6. import { resolveDevDesktopWindowsCuaEnv } from "./dev-desktop-cua-env.mjs";
  7. import { withPinnedNodePath } from "./mise-toolchain-env.mjs";
  8. import { quoteArgsForWindowsShell } from "./spawn-command.mjs";
  9. const requestedEnv = process.argv[2]?.trim().toLowerCase();
  10. const agentBytecode = process.argv.slice(3).includes("--agent-bytecode");
  11. if (requestedEnv !== "test" && requestedEnv !== "production") {
  12. console.error("Usage: node scripts/dev-desktop-env.mjs <test|production> [--agent-bytecode]");
  13. process.exit(1);
  14. }
  15. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  16. const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  17. // Computer Use Helper 只由真实 producer 提供;开源占位包下不构建 Helper、不注入 Helper 环境。
  18. // 见 docs/desktop/computer-use-producer-detection.md。
  19. const hasComputerUseProducer = isRealComputerUseProducerInstalled({
  20. desktopPackageRoot: join(repoRoot, "packages", "desktop"),
  21. });
  22. const computerUseDevEnv = hasComputerUseProducer ? await resolveComputerUseDevEnv() : {};
  23. async function resolveComputerUseDevEnv() {
  24. const { DEV_HELPER_APP_NAME } = await import("@zcode/zcode-cua/broker/helperConstants");
  25. const sourceBuiltCuaHelperVersion = JSON.parse(
  26. readFileSync(join(repoRoot, "package.json"), "utf8"),
  27. ).version;
  28. if (typeof sourceBuiltCuaHelperVersion !== "string" || !sourceBuiltCuaHelperVersion.trim()) {
  29. throw new Error("Root package.json must define the Dev Computer Use Helper version");
  30. }
  31. const sourceBuiltCuaHelperApp = join(
  32. repoRoot,
  33. "packages",
  34. "desktop",
  35. "dist-cua-helper",
  36. DEV_HELPER_APP_NAME,
  37. );
  38. return {
  39. // Standard dev launch must test the Helper built from this checkout.
  40. // Falling back to ~/.zcode can silently reuse a same-version stale
  41. // Helper and make native changes appear verified when they never ran.
  42. ZCODE_CUA_BUNDLED_HELPER_APP_PATH: sourceBuiltCuaHelperApp,
  43. ZCODE_CUA_HELPER_VERSION: sourceBuiltCuaHelperVersion,
  44. // Keep Dev Desktop separate from signed Stable/Preview and standalone MCP installs.
  45. ZCODE_CUA_HELPER_INSTALL_VARIANT: "dev-desktop",
  46. // 修复原因:仅传 bundled path 不会进入 unsigned-local 的同版本内容刷新分支,
  47. // 导致标准 dev 启动继续复用 ~/.zcode 中 native ABI 已过期的 Helper。
  48. ...(process.platform === "darwin" ? { ZCODE_CUA_HELPER_ALLOW_UNSIGNED_LOCAL: "1" } : {}),
  49. // Windows 侧对应物:缺省把 CUA runtime 绑到本 checkout 已安装的 producer,
  50. // 否则 host 会去读 dev 下不存在的 resources/tools/cua-helper。见 dev-desktop-cua-env.mjs。
  51. ...resolveDevDesktopWindowsCuaEnv({
  52. platform: process.platform,
  53. repoRoot,
  54. env: process.env,
  55. }),
  56. };
  57. }
  58. function run(command, args) {
  59. return new Promise((resolveRun, rejectRun) => {
  60. // Windows 下 shell:true 只按空格拼接参数;仓库路径含空格(如 E:\Z Code\...)时
  61. // node <script> 的脚本路径会被 cmd 截断成 E:\Z 并报 Cannot find module,因此先补引号。
  62. const spawnArgs = process.platform === "win32" ? quoteArgsForWindowsShell(args) : args;
  63. const child = spawn(command, spawnArgs, {
  64. cwd: repoRoot,
  65. env: withPinnedNodePath(
  66. {
  67. ...process.env,
  68. ZCODE_ENV: requestedEnv,
  69. ZCODE_DESKTOP_AGENT_BYTECODE: agentBytecode ? "1" : "0",
  70. ...computerUseDevEnv,
  71. },
  72. process.execPath,
  73. ),
  74. stdio: "inherit",
  75. // Windows .cmd/.bat executables (pnpm.cmd, npm.cmd, etc.) require shell: true
  76. shell: process.platform === "win32",
  77. });
  78. child.on("error", rejectRun);
  79. child.on("exit", (code, signal) => {
  80. if (code === 0) {
  81. resolveRun();
  82. return;
  83. }
  84. rejectRun(
  85. new Error(
  86. signal
  87. ? `${command} exited with signal ${signal}`
  88. : `${command} exited with code ${code ?? "unknown"}`,
  89. ),
  90. );
  91. });
  92. });
  93. }
  94. try {
  95. if (process.platform === "darwin" && hasComputerUseProducer) {
  96. await run(process.execPath, [
  97. resolve(repoRoot, "scripts/build-cua-helper-app.mjs"),
  98. "--allow-unsigned-launcher-local-dev",
  99. ]);
  100. }
  101. // The public dev scripts delegate here instead of invoking the package's
  102. // `dev` lifecycle directly, so pnpm will not run `pre-dev` automatically.
  103. // Preserve its runtime-asset preparation and stale `out` cleanup explicitly
  104. // before rebuilding bundles or starting Electron.
  105. await run(pnpmCommand, ["--filter", "@zcode/desktop", "pre-dev"]);
  106. // On Windows, use "node" (resolved via PATHEXT) to avoid "C:\Program Files\..." space issues
  107. await run(process.platform === "win32" ? "node" : process.execPath, [
  108. resolve(repoRoot, "scripts/build-desktop-agent-cli.mjs"),
  109. ]);
  110. if (agentBytecode) {
  111. await run(process.platform === "win32" ? "node" : process.execPath, [
  112. resolve(repoRoot, "scripts/build-desktop-agent-bytecode.mjs"),
  113. ]);
  114. }
  115. await run(pnpmCommand, ["--filter", "@zcode/desktop", "dev:runtime"]);
  116. } catch (error) {
  117. console.error(error instanceof Error ? error.message : error);
  118. process.exit(1);
  119. }