build.mjs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { mkdir, readFile } from "node:fs/promises";
  2. import { dirname, resolve } from "node:path";
  3. import { pathToFileURL } from "node:url";
  4. import { build } from "esbuild";
  5. const packageRoot = resolve(import.meta.dirname, "..");
  6. // 见 browser-use-plugin/scripts/build.mjs 的同名修复:esbuild 的 esm 产物里
  7. // __require shim 在 ESM 作用域没有 require 可用,@zcode/core 拖进来的 CJS 依赖(yaml →
  8. // require("process"))会在**模块求值阶段**抛错,plugin host 的 await import() 直接失败,
  9. // 表现为注册 0 个工具、模型侧完全看不到 mcp__node_repl__js。注入真实 createRequire。
  10. const nodeRequireBanner = `import { createRequire as __zcodeCreateRequire } from "node:module";
  11. const require = __zcodeCreateRequire(import.meta.url);`;
  12. // 正式包上 Computer Use 曾完全
  13. // 不可用 —— 每个 CUA 调用要么等到 MCP 客户端超时(实测 60/110/120s),要么等满 ~64s 后返回
  14. // 「permission broker socket is not accepting connections yet」。
  15. //
  16. // 链路:懒启动(services/src/node.ts 的 `懒启动` 注释处)把 darwin 上 Helper 的
  17. // 安装/拉起从宿主搬到了「SDK 首次 CUA 调用时自拉」,而那条路径跑在**本包**里 ——
  18. // `helperInstaller` 因此随本 bundle 进入正式包。但 `__ZCODE_CUA_HELPER_BUILD_ID__` 此前
  19. // **只有** packages/desktop/tsup.config.ts 注入(Electron main / app.asar),本文件的
  20. // esbuild 调用一个 define 都没有。于是正式包里 producer 的 `ZCODE_CUA_HELPER_BUILD_ID`
  21. // 折叠成空串(见 zcode-cua/src/broker/shared/cua-version.ts 的兜底),
  22. // `resolveExpectedCuaHelperBuildId()` 返回 null,helperInstaller 撞上 fail-closed 守卫:
  23. //
  24. // "Packaged ZCode is missing its embedded Computer Use Helper build identity;
  25. // refusing an unpinned Helper install"
  26. //
  27. // 结果 Helper 既不装也不起、一行日志都不写,而这句 throw 被 MCP 层翻成超时,没有落盘点 ——
  28. // 所以它一直是隐形的。实证:宿主日志里 `[cua-product-helper]` 在 09-11(宿主路径,有 define)
  29. // 有 5 行,09-14 为 0 行;`~/.zcode/computer-use/logs/` 从未创建;把已安装的 Helper 改名后
  30. // 也不会重装。dev 不受影响(ALLOW_UNSIGNED_LOCAL + 非 production runtime 绕过该守卫),
  31. // 所以只在正式包暴露,本地怎么测都测不出来。
  32. //
  33. // 取值与 desktop 侧保持同一来源:CI 注入 ZCODE_CUA_HELPER_BUILD_ID env;dev 为空串走兜底
  34. // (dev Helper 不走下载/pin 校验)。见 packages/desktop/tsup.config.ts 同名 define 的注释。
  35. const resolveCuaHelperBuildId = (env = process.env) =>
  36. env.ZCODE_CUA_HELPER_BUILD_ID?.trim() ?? "";
  37. export const buildNodeReplHostBundle = async ({
  38. outfile = resolve(packageRoot, "dist", "mcp", "server.js"),
  39. cuaHelperBuildId = resolveCuaHelperBuildId(),
  40. } = {}) => {
  41. await mkdir(dirname(outfile), { recursive: true });
  42. await build({
  43. banner: { js: nodeRequireBanner },
  44. bundle: true,
  45. define: {
  46. __ZCODE_CUA_HELPER_BUILD_ID__: JSON.stringify(cuaHelperBuildId),
  47. },
  48. entryPoints: [resolve(packageRoot, "src", "server.ts")],
  49. format: "esm",
  50. legalComments: "none",
  51. outfile,
  52. platform: "node",
  53. target: "node24",
  54. });
  55. // 构建期守卫:define 名一旦漂移(改名、被 createSharedDefines 之类重构吞掉),
  56. // 产物会静默退回空串,而症状只在正式包出现且表现为超时。这里立刻失败,别再让它溜到用户手上。
  57. if (cuaHelperBuildId) {
  58. const bundled = await readFile(outfile, "utf8");
  59. if (!bundled.includes(cuaHelperBuildId)) {
  60. throw new Error(
  61. `[node-repl-host] ZCODE_CUA_HELPER_BUILD_ID=${cuaHelperBuildId} 未折叠进 ${outfile}:` +
  62. "__ZCODE_CUA_HELPER_BUILD_ID__ define 没有生效,正式包的 Helper 安装会被 fail-closed 拒绝。",
  63. );
  64. }
  65. }
  66. return { outfile, cuaHelperBuildId };
  67. };
  68. // 这里原先写成 `file://${process.argv[1]}`。
  69. // Windows 上 argv[1] 是 `C:\...\build.mjs`,而 import.meta.url 是 `file:///C:/.../build.mjs`,
  70. // 两者永远不相等 —— 脚本被当成纯模块导入,什么都不做就退出:构建"成功"却没有产物,
  71. // 直到 dev 守卫报「build succeeded without required MCP runtime」才暴露。
  72. // browser-use 的同名脚本与仓库其他入口都用 pathToFileURL,抽包时我漏了这一处。
  73. const entryPath = process.argv[1];
  74. if (entryPath && import.meta.url === pathToFileURL(entryPath).href) {
  75. const { outfile } = await buildNodeReplHostBundle();
  76. console.log(`[node-repl-host] ${outfile}`);
  77. }