stage-agent-bundle.mjs 4.8 KB

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