build.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { build } from "esbuild";
  2. import {
  3. readFileSync,
  4. existsSync,
  5. mkdirSync,
  6. rmSync,
  7. cpSync,
  8. chmodSync,
  9. writeFileSync,
  10. } from "node:fs";
  11. import { createRequire } from "node:module";
  12. import { execFileSync } from 'node:child_process';
  13. import { fileURLToPath } from "node:url";
  14. import path from "node:path";
  15. import { copyLocalRuntimeAssets } from "./lib/local-runtime-assets.mjs";
  16. import { createTuiBundleModuleLocationConfig } from "./lib/tui-npm-bundle-profile.mjs";
  17. import { shouldCopyTuiRuntimeResource } from "./lib/tui-package-privacy.mjs";
  18. import { TUI_DISABLED_BUILTIN_SKILL_NAMES } from "./lib/builtin-skills.mjs";
  19. import { copyMcodeToolsArtifact } from './lib/mcode-tools-artifact.mjs';
  20. import { readExtraction } from "./lib/release-metadata.mjs";
  21. import { cliBuildVersion, cliExternalModules } from './lib/cli-release.mjs';
  22. const root = fileURLToPath(new URL("../", import.meta.url));
  23. const metadata = readExtraction(root);
  24. const packages = new Map(
  25. metadata.packageRoots.map((directory) => {
  26. const manifest = JSON.parse(
  27. readFileSync(path.join(root, directory, "package.json"), "utf8"),
  28. );
  29. return [manifest.name, { directory, manifest }];
  30. }),
  31. );
  32. const location = createTuiBundleModuleLocationConfig();
  33. const outdir = path.join(root, "dist");
  34. rmSync(outdir, { recursive: true, force: true });
  35. mkdirSync(outdir, { recursive: true });
  36. // Bundle checked-in workspace sources and resolve npm dependencies from each importer.
  37. // Native and optional platform integrations keep their installed module locations.
  38. const sourcePlugin = {
  39. name: "standalone-workspace-sources",
  40. setup(bundler) {
  41. bundler.onResolve({ filter: /^[^./]/ }, ({ path: specifier }) => {
  42. const parts = specifier.split("/");
  43. const name = specifier.startsWith("@")
  44. ? parts.slice(0, 2).join("/")
  45. : parts[0];
  46. const pkg = packages.get(name);
  47. if (!pkg) return undefined;
  48. const subpath =
  49. specifier === name ? "." : `.${specifier.slice(name.length)}`;
  50. const exports = pkg.manifest.exports;
  51. const exported =
  52. exports?.[subpath] ?? (subpath === "." ? exports : undefined);
  53. const target =
  54. (typeof exported === "string"
  55. ? exported
  56. : (exported?.types ?? exported?.import ?? exported?.default)) ??
  57. (subpath === "." ? pkg.manifest.types : undefined);
  58. if (typeof target !== "string" || !target.startsWith("./"))
  59. throw new Error(`Unmapped workspace export: ${specifier}`);
  60. const source = target
  61. .replace(/^\.\/dist\//, "./src/")
  62. .replace(/\.d\.ts$/, ".ts")
  63. .replace(/\.js$/, ".ts");
  64. const directory = path.join(root, pkg.directory);
  65. const resolved = path.resolve(directory, source);
  66. if (
  67. path.relative(directory, resolved).startsWith("..") ||
  68. !existsSync(resolved)
  69. )
  70. throw new Error(`Missing workspace source: ${specifier}`);
  71. return { path: resolved };
  72. });
  73. },
  74. };
  75. const version = cliBuildVersion(root);
  76. const result = await build({
  77. absWorkingDir: root,
  78. entryPoints: {
  79. cli: "packages/tui/src/index.ts",
  80. "image-preview-worker": "packages/tui/src/host/image-preview-worker.ts",
  81. 'mcode-tools': 'packages/tui/src/cli/mcode-tools-entry.ts',
  82. 'matrix-mcp-stdio': 'packages/agent-tools/src/desktop/matrix-mcp-stdio.ts',
  83. },
  84. external: cliExternalModules,
  85. outdir,
  86. bundle: true,
  87. splitting: true,
  88. format: "esm",
  89. platform: "node",
  90. minifyIdentifiers: true,
  91. target: "node22",
  92. chunkNames: "chunks/[name]-[hash]",
  93. banner: { js: location.banner },
  94. plugins: [sourcePlugin],
  95. metafile: true,
  96. define: {
  97. ...location.define,
  98. __CLI_VERSION__: JSON.stringify(version),
  99. __CLI_CHANNEL__: '"source"',
  100. __IS_NPM_BUILD__: "true",
  101. __BUILD_PROFILE__: '"tui"',
  102. __TUI_BUILD_ENV__: '"prod"',
  103. __TUI_BUILD_VARIANT__: '"standard"',
  104. __TUI_NPM_DIST_TAG__: '"latest"',
  105. },
  106. logLevel: "info",
  107. });
  108. copyLocalRuntimeAssets({
  109. repositoryRoot: root,
  110. outputDir: outdir,
  111. filter: shouldCopyTuiRuntimeResource,
  112. excludedBuiltinSkillNames: TUI_DISABLED_BUILTIN_SKILL_NAMES,
  113. });
  114. for (const name of ["configs", "native"])
  115. cpSync(path.join(root, "packages/tui", name), path.join(outdir, name), {
  116. recursive: true,
  117. });
  118. for (const name of ["seccomp", "srt-win", "java-proxy-agent"]) {
  119. cpSync(
  120. path.join(root, "third_party/sandbox-runtime/vendor", name),
  121. path.join(outdir, "vendor", name),
  122. { recursive: true },
  123. );
  124. }
  125. chmodSync(path.join(outdir, "cli.js"), 0o755);
  126. await copyMcodeToolsArtifact(root, outdir);
  127. cpSync(path.join(root, 'packages/tui/src/cli/mcode-tools-launchers'), path.join(outdir, 'internal-bin'), { recursive: true });
  128. for (const name of ['internal-bin/mcode-tools', 'mcode-tools.js', 'matrix-mcp-stdio.js'])
  129. chmodSync(path.join(outdir, name), 0o755);
  130. writeFileSync(
  131. path.join(outdir, "metafile.json"),
  132. JSON.stringify(result.metafile, null, 2) + "\n",
  133. );
  134. console.log(
  135. `Built MiniMax Code ${version} from ${Object.keys(result.metafile.inputs).length} source files.`,
  136. );
  137. writeFileSync(
  138. path.join(outdir, "package.json"),
  139. JSON.stringify(
  140. {
  141. name: "@minimax-ai/code", version, type: "module", private: true,
  142. ...(process.env.MCODE_RELEASE_TAG ? {
  143. gitHead: execFileSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim(),
  144. } : {}),
  145. },
  146. null,
  147. 2,
  148. ) + "\n",
  149. );
  150. const piRequire = createRequire(
  151. path.join(root, "third_party/pi-mono/packages/coding-agent/package.json"),
  152. );
  153. cpSync(
  154. piRequire.resolve("@silvia-odwyer/photon-node/photon_rs_bg.wasm"),
  155. path.join(outdir, "photon_rs_bg.wasm"),
  156. );