Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
import { defineConfig } from "tsup";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { sep } from "node:path";
// ADR-017: `ink` resolves to the committed, patched build in vendor/ink
// (regenerated by scripts/vendor-ink.cjs) and is bundled into dist, so npm
// installs, `--ignore-scripts` installs, Termux and the Bun standalone
// binaries all run the same patched renderer — no postinstall step.
const VENDORED_INK = fileURLToPath(new URL("./vendor/ink/build/index.js", import.meta.url));
const pkg = JSON.parse(readFileSync("./package.json", "utf8"));
// Dev-channel builds (scripts/publish-dev.sh) stamp a version like
// `1.2.8-dev.20260929.abc1234` so `mercury --version` identifies a dev
// binary. Stable builds never set MERCURY_CHANNEL_VERSION → pkg.version.
const channelVersion = process.env.MERCURY_CHANNEL_VERSION || pkg.version;
// Separate marker for the dev stamp: dev builds set it; stable builds get ''
// so runtime precedence never changes for them (disk package.json remains the
// source of truth when no channel version was baked at compile time).
const channelStamp = process.env.MERCURY_CHANNEL_VERSION || '';
export default defineConfig({
// dist/index.js is the launcher (see src/launcher.ts); dist/mercury.js is
// the real bundle it loads once the Node version check has passed.
entry: { index: "src/launcher.ts", mercury: "src/index.ts" },
format: ["esm"],
target: "node22",
outDir: "dist",
clean: true,
bundle: true,
splitting: false,
sourcemap: true,
minify: false,
// createRequire: ink's bundled CommonJS dependencies (signal-exit 3, …)
// `require()` node builtins, which an ESM bundle has no global for. Then a
// Node version gate, which runs before any Mercury code.
banner: {
js: [
"#!/usr/bin/env node",
'import { createRequire as __mercuryCreateRequire } from "node:module";',
"const require = __mercuryCreateRequire(import.meta.url);",
// Node 22 is required (ADR-019: ink 8). Say so in plain words instead
// of crashing. Bun reports its own Node-compat version and is exempt.
// Under a service manager (`--daemon`), exit 0: launchd and systemd
// restart a daemon that exits non-zero, which would loop forever;
// the message still lands in the daemon log.
'if (!process.versions.bun && Number(process.versions.node.split(".")[0]) < 22) { console.error("Mercury needs Node.js 22 or newer — this is Node " + process.version + ".\\n Upgrade Node: nvm install 22 && nvm alias default 22 (or https://nodejs.org)\\n Then reinstall Mercury with that Node: npm install -g @cosmicstack/mercury-agent"); process.exit(process.argv.includes("--daemon") ? 0 : 1); }',
].join("\n"),
},
// Inject the version so standalone binaries (Bun --compile) can read it
// without trying to load package.json from disk.
define: {
"globalThis.__MERCURY_VERSION__": JSON.stringify(channelVersion),
"globalThis.__MERCURY_CHANNEL_VERSION__": JSON.stringify(channelStamp),
},
// React is bundled too. The vendored ink bundles react-reconciler, a
// CommonJS module whose `require("react")` would otherwise stay a runtime
// lookup: fine with node_modules on disk, fatal in a Bun-compiled binary
// ("Cannot find package 'react'" at the first render). Bundling also
// guarantees one React instance. post-build.cjs fails on any such
// runtime require of a non-builtin package.
noExternal: ["ink", /^react($|\/)/],
esbuildOptions(options) {
options.alias = { ...(options.alias ?? {}), ink: VENDORED_INK };
},
esbuildPlugins: [
{
// ink's reconciler does `await import('./devtools.js')` when DEV=true.
// With splitting off, esbuild inlines that module — and its static
// `react-devtools-core` import, which crashes at load ("self is not
// defined"). Mercury never attaches React DevTools to the TUI, so the
// vendored devtools module is replaced with an empty one.
name: "mercury-ink-no-devtools",
setup(build) {
build.onResolve({ filter: /^\.\/devtools\.js$/ }, (args) =>
args.importer.includes(`vendor${sep}ink${sep}`)
? { path: "ink-devtools-stub", namespace: "mercury-stub" }
: undefined);
build.onLoad({ filter: /.*/, namespace: "mercury-stub" }, () => ({ contents: "export {};", loader: "js" }));
},
},
],
external: [
"ai",
"@ai-sdk/anthropic",
"@ai-sdk/deepseek",
"@ai-sdk/openai",
"@grammyjs/auto-retry",
"chalk",
"commander",
"dotenv",
"grammy",
// Only reachable from ink's DEV=true devtools hook (dynamic import in
// vendor/ink/build/reconciler.js) — never bundle it.
"react-devtools-core",
"js-tiktoken",
"marked",
"node-cron",
"ollama-ai-provider",
"pino",
"react",
"react-dom",
"yaml",
"zod",
"better-sqlite3",
// Runtime-provided SQLite engines (src/utils/sqlite-driver.ts): resolved
// at run time via createRequire, never bundled. bun:sqlite exists only
// inside Bun (the standalone binaries); node:sqlite only on Node >= 22.5.
"bun:sqlite",
"node:sqlite",
"@hono/node-server",
"sql.js",
],
});