koffi-package-assets.mjs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Koffi native runtime staging for the bundled CUA MCP server.
  2. //
  3. // The CUA server externalizes koffi because esbuild cannot bundle its
  4. // platform-dispatching `.node` requires. The installed agent has no hoisted
  5. // node_modules, so keep only the target binary beside the plugin.
  6. import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from "node:fs";
  7. import { resolve } from "node:path";
  8. function resolveKoffiRoot(koffiPackageRoot) {
  9. const candidates = [
  10. resolve(koffiPackageRoot),
  11. resolve(koffiPackageRoot, "node_modules", "koffi"),
  12. ];
  13. const virtualStore = resolve(koffiPackageRoot, "..", "..", "..", "..", "node_modules", ".pnpm");
  14. candidates.push(resolve(virtualStore, "..", "koffi"));
  15. if (existsSync(virtualStore)) {
  16. for (const entry of readdirSync(virtualStore)) {
  17. if (entry.startsWith("koffi@"))
  18. candidates.push(resolve(virtualStore, entry, "node_modules", "koffi"));
  19. }
  20. }
  21. for (const candidate of candidates) {
  22. const packageJson = resolve(candidate, "package.json");
  23. if (!existsSync(packageJson)) continue;
  24. try {
  25. if (JSON.parse(readFileSync(packageJson, "utf8")).name === "koffi") return candidate;
  26. } catch {
  27. // Continue searching other installation locations.
  28. }
  29. }
  30. throw new Error(
  31. `[koffi-package-assets] cannot resolve installed koffi package from ${koffiPackageRoot}`,
  32. );
  33. }
  34. function koffiPlatformKey(targetPlatform) {
  35. return `${targetPlatform.os}_${targetPlatform.arch}`;
  36. }
  37. export function stageKoffiIntoBundledAgents({ koffiPackageRoot, glmDir, targetPlatform }) {
  38. if (!koffiPackageRoot || !glmDir || !targetPlatform?.os || !targetPlatform?.arch) {
  39. throw new Error(
  40. "[koffi-package-assets] koffiPackageRoot, glmDir and targetPlatform are required",
  41. );
  42. }
  43. const sourceRoot = resolveKoffiRoot(koffiPackageRoot);
  44. const platformKey = koffiPlatformKey(targetPlatform);
  45. const sourceNativeDir = resolve(sourceRoot, "build", "koffi", platformKey);
  46. if (!existsSync(resolve(sourceNativeDir, "koffi.node"))) {
  47. throw new Error(
  48. `[koffi-package-assets] missing target native addon: ${sourceNativeDir}/koffi.node`,
  49. );
  50. }
  51. const targetRoot = resolve(glmDir, "node_modules", "koffi");
  52. rmSync(targetRoot, { recursive: true, force: true });
  53. mkdirSync(resolve(targetRoot, "build", "koffi", platformKey), { recursive: true });
  54. for (const file of ["index.js", "package.json", "index.d.ts"]) {
  55. cpSync(resolve(sourceRoot, file), resolve(targetRoot, file));
  56. }
  57. cpSync(
  58. resolve(sourceNativeDir, "koffi.node"),
  59. resolve(targetRoot, "build", "koffi", platformKey, "koffi.node"),
  60. );
  61. return resolve(targetRoot, "build", "koffi", platformKey, "koffi.node");
  62. }
  63. export function verifyStagedKoffi({
  64. resourcesDir,
  65. targetPlatform,
  66. pluginRelativePath = "packages/zcode-cua-plugin",
  67. }) {
  68. const platformKey = koffiPlatformKey(targetPlatform);
  69. const koffiRoot = resolve(resourcesDir, "glm", pluginRelativePath, "node_modules", "koffi");
  70. const nativePath = resolve(koffiRoot, "build", "koffi", platformKey, "koffi.node");
  71. return existsSync(nativePath) && existsSync(resolve(koffiRoot, "index.js"))
  72. ? []
  73. : [`missing staged koffi runtime for ${platformKey}: ${nativePath}`];
  74. }