stage-agent-bundle.mjs 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, cpSync, 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. /**
  15. * 修复原因(2026-10-24 真机):node_repl MCP server(含内联的 CUA agent runtime)
  16. * 不在 zcode.cjs 里 —— agent 从 ~/.zcode plugin cache 启动它。dev 的 zcode.cjs 是
  17. * 纯 esbuild bundle(无 SEA 资产),bootstrap 的 filesystem seed rootCandidates 又
  18. * 解析不到 apps/zcode-cli/packages/node-repl-host(cwd 是工作区不是仓库布局),
  19. * 于是 dev agent 只能消费 cache 现有内容;cache 的唯一写入者是生产版 agent 的
  20. * SEA seed(内嵌旧副本),agent 侧改动在 dev 里永远跑不上。
  21. * 与生产包布局对齐:把源码包 stage 到 glm/packages/node-repl-host,dev agent 的
  22. * entrypointDir + rootCandidates[0] 即可命中,每次 dev 启动 seed 都从这份最新
  23. * 构建刷新 cache(生产版翻写只影响它自己后续起的会话——node_repl server 进程
  24. * 加载代码后不受 cache 变化影响)。
  25. * 打包链(prepare-agent-node-bundle.mjs)随后会用更完整的 officialPluginPackages
  26. * 逻辑重 stage 同一目录,这里重复暂存同源内容无害。
  27. */
  28. const STAGED_NODE_REPL_HOST_ENTRIES = ["dist", ".zcode-plugin", "package.json", "README.md", "scripts"];
  29. export function resolveAgentBundlePaths({ repoRoot, platformKey }) {
  30. const glmDir = resolve(repoRoot, "packages", "desktop", "bundled-agents", platformKey, "glm");
  31. return {
  32. cliBundlePath: resolve(repoRoot, AGENT_BUNDLE_SOURCE_RELATIVE),
  33. glmDir,
  34. stagedBundlePath: resolve(glmDir, "zcode.cjs"),
  35. stagedMetaPath: resolve(glmDir, ".node-bundle-meta.json"),
  36. };
  37. }
  38. /**
  39. * 干净重建 glm 目录再拷贝。清空是刻意的:electron-builder 整目录拷贝
  40. * bundled-agents/<平台>/glm → resources/glm,本地工作树里上一次构建残留的原生二进制
  41. * (zcode-agent / zcode-acp 等)和旧 meta 会被一并打进安装包(CI 干净检出不会有,本地会)。
  42. */
  43. export function stageAgentBundle({ repoRoot, platformKey, log = console.log }) {
  44. const { cliBundlePath, glmDir, stagedBundlePath, stagedMetaPath } = resolveAgentBundlePaths({
  45. repoRoot,
  46. platformKey,
  47. });
  48. if (!existsSync(cliBundlePath)) {
  49. throw new Error(`[stage:agent-bundle] agent bundle 源产物不存在:${cliBundlePath}`);
  50. }
  51. rmSync(glmDir, { recursive: true, force: true });
  52. mkdirSync(glmDir, { recursive: true });
  53. copyFileSync(cliBundlePath, stagedBundlePath);
  54. const meta = {
  55. runtime: "electron-node",
  56. entry: "zcode.cjs",
  57. platform: platformKey,
  58. source: AGENT_BUNDLE_SOURCE_RELATIVE,
  59. };
  60. writeFileSync(stagedMetaPath, `${JSON.stringify(meta, null, 2)}\n`, "utf8");
  61. // node-repl-host 暂存(见 STAGED_NODE_REPL_HOST_ENTRIES 注释):dist 缺失说明调用方
  62. // 还没构建它(build-desktop-agent-cli 的 defaultBuildFilters 会先建再 stage),跳过
  63. // 并留给打包链报错,不在共享实现里重复校验。
  64. const nodeReplHostSource = resolve(repoRoot, "apps", "zcode-cli", "packages", "node-repl-host");
  65. const nodeReplHostTarget = resolve(glmDir, "packages", "node-repl-host");
  66. if (existsSync(resolve(nodeReplHostSource, "dist", "mcp", "server.js"))) {
  67. mkdirSync(nodeReplHostTarget, { recursive: true });
  68. for (const entry of STAGED_NODE_REPL_HOST_ENTRIES) {
  69. const sourcePath = resolve(nodeReplHostSource, entry);
  70. if (existsSync(sourcePath)) {
  71. cpSync(sourcePath, resolve(nodeReplHostTarget, entry), { recursive: true });
  72. }
  73. }
  74. log(`[stage:agent-bundle] staged node-repl-host runtime for dev self-seed`);
  75. }
  76. log(`[stage:agent-bundle] staged ${stagedBundlePath}`);
  77. return { stagedBundlePath, stagedMetaPath };
  78. }