build.mjs 5.3 KB

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