stage-agent-bundle.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Agent bundle 的暂存动作:把 apps/zcode-cli/packages/cli/dist/zcode.cjs 放进
  2. // bundled-agents/<平台>/glm,并写 meta。
  3. //
  4. // dev 与打包**必须**用同一份暂存实现。
  5. // 只有打包链(prepare-agent-node-bundle.mjs)会暂存是不够的,dev 链
  6. // (scripts/build-desktop-agent-cli.mjs)不会;而 dev 未打包时的 agent 二进制由
  7. // desktopRuntimeEnv.ts 的 resolveBundledZCodeAgentBinaryPath() 解析,候选**只有**
  8. // bundled-agents/,没有 cli/dist/。于是 dev 一直跑着上一次打包时留下的那份 ——
  9. // 实测陈旧 3 天,任何 agent CLI 侧改动在 dev 里静默不生效,排查时会把「改动没生效」
  10. // 误判成「代码没起作用」。两边共用这一份,dev 与打包不可能再各自漂移。
  11. import { copyFileSync, existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
  12. import { resolve } from "node:path";
  13. export const AGENT_BUNDLE_SOURCE_RELATIVE = "apps/zcode-cli/packages/cli/dist/zcode.cjs";
  14. export function resolveAgentBundlePaths({ repoRoot, platformKey }) {
  15. const glmDir = resolve(repoRoot, "packages", "desktop", "bundled-agents", platformKey, "glm");
  16. return {
  17. cliBundlePath: resolve(repoRoot, AGENT_BUNDLE_SOURCE_RELATIVE),
  18. glmDir,
  19. stagedBundlePath: resolve(glmDir, "zcode.cjs"),
  20. stagedMetaPath: resolve(glmDir, ".node-bundle-meta.json"),
  21. };
  22. }
  23. /**
  24. * 干净重建 glm 目录再拷贝。清空是刻意的:electron-builder 整目录拷贝
  25. * bundled-agents/<平台>/glm → resources/glm,本地工作树里上一次构建残留的原生二进制
  26. * (zcode-agent / zcode-acp 等)和旧 meta 会被一并打进安装包(CI 干净检出不会有,本地会)。
  27. */
  28. export function stageAgentBundle({ repoRoot, platformKey, log = console.log }) {
  29. const { cliBundlePath, glmDir, stagedBundlePath, stagedMetaPath } = resolveAgentBundlePaths({
  30. repoRoot,
  31. platformKey,
  32. });
  33. if (!existsSync(cliBundlePath)) {
  34. throw new Error(`[stage:agent-bundle] agent bundle 源产物不存在:${cliBundlePath}`);
  35. }
  36. rmSync(glmDir, { recursive: true, force: true });
  37. mkdirSync(glmDir, { recursive: true });
  38. copyFileSync(cliBundlePath, stagedBundlePath);
  39. const meta = {
  40. runtime: "electron-node",
  41. entry: "zcode.cjs",
  42. platform: platformKey,
  43. source: AGENT_BUNDLE_SOURCE_RELATIVE,
  44. };
  45. writeFileSync(stagedMetaPath, `${JSON.stringify(meta, null, 2)}\n`, "utf8");
  46. log(`[stage:agent-bundle] staged ${stagedBundlePath}`);
  47. return { stagedBundlePath, stagedMetaPath };
  48. }