Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
Add cross-platform npm install with auto-dependency installation
trohitga069974cc705
diff --git a/.github/workflows/test-install.yml b/.github/workflows/test-install.ymlnew file mode 100644index 00000000..c3d1b113--- /dev/null+++ b/.github/workflows/test-install.yml@@ -0,0 +1,88 @@+name: Test npm Install++on:+ push:+ branches: [main]+ paths:+ - 'scripts/**'+ - 'package.json'+ - '.github/workflows/test-install.yml'+ pull_request:+ branches: [main]+ workflow_dispatch:++jobs:+ test-install:+ runs-on: ${{ matrix.os }}+ strategy:+ fail-fast: false+ matrix:+ os: [ubuntu-latest, macos-latest, windows-latest]+ node-version: [18, 20, 22]++ steps:+ - name: Checkout+ uses: actions/checkout@v4++ - name: Setup Node.js ${{ matrix.node-version }}+ uses: actions/setup-node@v4+ with:+ node-version: ${{ matrix.node-version }}++ - name: Setup Python 3.12+ uses: actions/setup-python@v5+ with:+ python-version: '3.12'++ - name: Install uv+ uses: astral-sh/setup-uv@v5++ - name: Pack npm package+ run: npm pack++ - name: Test global install+ shell: bash+ run: |+ # Install the packed tarball globally+ npm install -g machinaos-*.tgz++ # Verify CLI is available+ machinaos --help++ # Check installed files exist+ INSTALL_PATH=$(npm root -g)/machinaos+ echo "Installed to: $INSTALL_PATH"+ ls -la "$INSTALL_PATH"++ # Verify key files+ test -f "$INSTALL_PATH/.env" && echo ".env created"+ test -d "$INSTALL_PATH/client" && echo "client/ exists"+ test -d "$INSTALL_PATH/server" && echo "server/ exists"+ test -f "$INSTALL_PATH/server/whatsapp-rpc/bin/whatsapp-rpc-server"* && echo "WhatsApp binary exists"++ - name: Verify Python environment+ shell: bash+ run: |+ INSTALL_PATH=$(npm root -g)/machinaos+ cd "$INSTALL_PATH/server"++ # Check venv was created+ if [ -d ".venv" ]; then+ echo "Python venv created successfully"+ ls -la .venv/bin 2>/dev/null || ls -la .venv/Scripts 2>/dev/null+ else+ echo "Warning: .venv not found"+ fi++ - name: Verify client build+ shell: bash+ run: |+ INSTALL_PATH=$(npm root -g)/machinaos++ # Check client dist was built+ if [ -d "$INSTALL_PATH/client/dist" ]; then+ echo "Client built successfully"+ ls -la "$INSTALL_PATH/client/dist"+ else+ echo "Warning: client/dist not found"+ fidiff --git a/package.json b/package.jsonindex 5e97d0a8..2052effa 100644--- a/package.json+++ b/package.json@@ -35,8 +35,23 @@"files": ["bin/","scripts/",- "client/",+ "client/src/",+ "client/public/",+ "client/*.json",+ "client/*.html",+ "client/*.js",+ "client/*.ts",+ "client/*.conf",+ "client/Dockerfile","server/",+ "!server/.venv/",+ "!server/**/__pycache__/",+ "!server/data/",+ "!server/whatsapp-rpc/node_modules/",+ "!server/whatsapp-rpc/bin/",+ "!server/whatsapp-rpc/data/",+ "!**/node_modules/",+ "!**/.vite/",".env.template","README.md","install.sh",diff --git a/scripts/build.js b/scripts/build.jsindex 415a668a..0ad0b2b7 100644--- a/scripts/build.js+++ b/scripts/build.js@@ -29,17 +29,14 @@ const isCI = process.env.CI === 'true' || process.env.GITHUB_ACTIONS === 'true';// Ensure Python UTF-8 encodingprocess.env.PYTHONUTF8 = '1';-// Use stderr for all output so npm shows it during postinstall-const print = (msg) => process.stderr.write(msg + '\n');-// Skip build entirely in CI (workflows handle this)if (isCI && isPostInstall) {- print('CI environment detected, skipping postinstall build.');+ console.log('CI environment detected, skipping postinstall build.');process.exit(0);}function run(cmd, cwd = ROOT) {- execSync(cmd, { cwd, stdio: ['inherit', process.stderr, process.stderr], shell: true });+ execSync(cmd, { cwd, stdio: 'inherit', shell: true });}function runSilent(cmd) {@@ -53,20 +50,14 @@ function runSilent(cmd) {function getVersion(cmd) {try {- return execSync(cmd, { encoding: 'utf-8', shell: true }).trim();+ return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], shell: true }).trim();} catch {return null;}}-function log(step, msg) {- print(`[${step}] ${msg}`);-}-function npmInstall(cwd = ROOT) {- // Use npm install directly with visible output via stderr- // npm ci requires package-lock.json which may not exist in all directories- execSync('npm install', { cwd, stdio: ['inherit', process.stderr, process.stderr], shell: true });+ execSync('npm install', { cwd, stdio: 'inherit', shell: true });}// ============================================================================@@ -74,27 +65,25 @@ function npmInstall(cwd = ROOT) {// ============================================================================function installPython() {- print(' Installing Python 3.11+...');+ console.log(' Installing Python 3.11+...');if (isWindows) {- // Try winget first, then chocoif (runSilent('winget --version')) {run('winget install Python.Python.3.12 --accept-package-agreements --accept-source-agreements');} else if (runSilent('choco --version')) {run('choco install python312 -y');} else {- print(' Error: Please install Python manually from https://python.org/');- print(' Or install winget/chocolatey first.');+ console.log(' Error: Please install Python manually from https://python.org/');+ console.log(' Or install winget/chocolatey first.');process.exit(1);}} else if (isMac) {if (runSilent('brew --version')) {run('brew install [email protected]');} else {- print(' Error: Please install Homebrew first: https://brew.sh/');+ console.log(' Error: Please install Homebrew first: https://brew.sh/');process.exit(1);}} else {- // Linuxif (runSilent('apt --version')) {run('sudo apt update && sudo apt install -y python3.12 python3.12-venv');} else if (runSilent('dnf --version')) {@@ -102,20 +91,19 @@ function installPython() {} else if (runSilent('pacman --version')) {run('sudo pacman -S --noconfirm python');} else {- print(' Error: Please install Python manually from https://python.org/');+ console.log(' Error: Please install Python manually from https://python.org/');process.exit(1);}}}function installUv() {- print(' Installing uv...');+ console.log(' Installing uv...');if (isWindows) {run('powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"');} else {run('curl -LsSf https://astral.sh/uv/install.sh | sh');}- // Add to PATH for current sessionif (isWindows) {process.env.PATH = `${process.env.USERPROFILE}\\.local\\bin;${process.env.PATH}`;} else {@@ -124,21 +112,21 @@ function installUv() {}function installGo() {- print(' Installing Go...');+ console.log(' Installing Go...');if (isWindows) {if (runSilent('winget --version')) {run('winget install GoLang.Go --accept-package-agreements --accept-source-agreements');} else if (runSilent('choco --version')) {run('choco install golang -y');} else {- print(' Error: Please install Go manually from https://go.dev/dl/');+ console.log(' Error: Please install Go manually from https://go.dev/dl/');process.exit(1);}} else if (isMac) {if (runSilent('brew --version')) {run('brew install go');} else {- print(' Error: Please install Homebrew first: https://brew.sh/');+ console.log(' Error: Please install Homebrew first: https://brew.sh/');process.exit(1);}} else {@@ -149,7 +137,7 @@ function installGo() {} else if (runSilent('pacman --version')) {run('sudo pacman -S --noconfirm go');} else {- print(' Error: Please install Go manually from https://go.dev/dl/');+ console.log(' Error: Please install Go manually from https://go.dev/dl/');process.exit(1);}}@@ -159,14 +147,13 @@ function installGo() {// Check and Install Dependencies// ============================================================================-print('Checking dependencies...\n');+console.log('Checking dependencies...\n');-// Node.js (must already be installed to run this script)const nodeVersion = getVersion('node --version');-print(` Node.js: ${nodeVersion}`);+console.log(` Node.js: ${nodeVersion}`);const npmVersion = getVersion('npm --version');-print(` npm: ${npmVersion}`);+console.log(` npm: ${npmVersion}`);// Pythonlet pyCmd = null;@@ -182,31 +169,30 @@ if (pyCmd) {if (match) {const [, major, minor] = match.map(Number);if (major >= 3 && minor >= 11) {- print(` ${pyVersion}`);+ console.log(` ${pyVersion}`);} else {- print(` ${pyVersion} (too old, need 3.11+)`);+ console.log(` ${pyVersion} (too old, need 3.11+)`);installPython();}}} else {installPython();- // Re-checkpyCmd = runSilent('python --version') ? 'python' : 'python3';- print(` ${getVersion(`${pyCmd} --version`)}`);+ console.log(` ${getVersion(`${pyCmd} --version`)}`);}// uvlet uvVersion = getVersion('uv --version');if (uvVersion) {- print(` uv: ${uvVersion}`);+ console.log(` uv: ${uvVersion}`);} else {installUv();uvVersion = getVersion('uv --version');if (uvVersion) {- print(` uv: ${uvVersion}`);+ console.log(` uv: ${uvVersion}`);} else {- print(' Error: Failed to install uv. Please install manually.');- print(' https://docs.astral.sh/uv/getting-started/installation/');+ console.log(' Error: Failed to install uv. Please install manually.');+ console.log(' https://docs.astral.sh/uv/getting-started/installation/');process.exit(1);}}@@ -215,21 +201,21 @@ if (uvVersion) {let goVersionFull = getVersion('go version');if (goVersionFull) {const goVersion = goVersionFull.match(/go\d+\.\d+(\.\d+)?/)?.[0] || 'go';- print(` Go: ${goVersion}`);+ console.log(` Go: ${goVersion}`);} else {installGo();goVersionFull = getVersion('go version');if (goVersionFull) {const goVersion = goVersionFull.match(/go\d+\.\d+(\.\d+)?/)?.[0] || 'go';- print(` Go: ${goVersion}`);+ console.log(` Go: ${goVersion}`);} else {- print(' Error: Failed to install Go. Please install manually.');- print(' https://go.dev/dl/');+ console.log(' Error: Failed to install Go. Please install manually.');+ console.log(' https://go.dev/dl/');process.exit(1);}}-print('\nAll dependencies ready.\n');+console.log('\nAll dependencies ready.\n');// ============================================================================// Build@@ -241,43 +227,43 @@ try {const templatePath = resolve(ROOT, '.env.template');if (!existsSync(envPath) && existsSync(templatePath)) {copyFileSync(templatePath, envPath);- log('0/5', 'Created .env from template');+ console.log('[0/6] Created .env from template');}// Step 1: Install root dependencies (skip if postinstall - npm already did this)if (!isPostInstall) {- log('1/6', 'Installing root dependencies...');+ console.log('[1/6] Installing root dependencies...');npmInstall(ROOT);} else {- log('1/6', 'Root dependencies already installed by npm');+ console.log('[1/6] Root dependencies already installed by npm');}// Step 2: Install client dependencies- log('2/6', 'Installing client dependencies...');+ console.log('[2/6] Installing client dependencies...');npmInstall(resolve(ROOT, 'client'));// Step 3: Build client- log('3/6', 'Building client...');+ console.log('[3/6] Building client...');run('npm run build', resolve(ROOT, 'client'));// Step 4: Install Python dependencies- log('4/6', 'Installing Python dependencies...');+ console.log('[4/6] Installing Python dependencies...');const serverDir = resolve(ROOT, 'server');run('uv venv', serverDir);run('uv sync', serverDir);// Step 5: Install WhatsApp dependencies- log('5/6', 'Installing WhatsApp dependencies...');+ console.log('[5/6] Installing WhatsApp dependencies...');const whatsappDir = resolve(ROOT, 'server/whatsapp-rpc');npmInstall(whatsappDir);// Step 6: Build WhatsApp server (Go binary)- log('6/6', 'Building WhatsApp server...');+ console.log('[6/6] Building WhatsApp server...');run('npm run build', whatsappDir);- print('\nBuild complete.');+ console.log('\nBuild complete.');} catch (err) {- print(`\nBuild failed: ${err.message}`);+ console.log(`\nBuild failed: ${err.message}`);process.exit(1);}diff --git a/scripts/install.js b/scripts/install.jsnew file mode 100644index 00000000..fff0cb79--- /dev/null+++ b/scripts/install.js@@ -0,0 +1,211 @@+#!/usr/bin/env node+/**+ * MachinaOS Installation Script+ *+ * Called by postinstall.js after npm install.+ * Installs all dependencies including Python, uv, and builds the project.+ */+import { execSync } from 'child_process';+import { existsSync, copyFileSync } from 'fs';+import { resolve, dirname } from 'path';+import { fileURLToPath } from 'url';++const __dirname = dirname(fileURLToPath(import.meta.url));+const ROOT = resolve(__dirname, '..');++const isWindows = process.platform === 'win32';+const isMac = process.platform === 'darwin';++process.env.PYTHONUTF8 = '1';++function run(cmd, cwd = ROOT) {+ execSync(cmd, { cwd, stdio: 'inherit', shell: true });+}++function runSilent(cmd) {+ try {+ execSync(cmd, { stdio: 'pipe', shell: true });+ return true;+ } catch {+ return false;+ }+}++function getVersion(cmd) {+ try {+ return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], shell: true }).trim();+ } catch {+ return null;+ }+}++function checkPython() {+ for (const cmd of ['python3', 'python']) {+ const version = getVersion(`${cmd} --version`);+ if (version) {+ const match = version.match(/Python (\d+)\.(\d+)/);+ if (match) {+ const [, major, minor] = match.map(Number);+ if (major >= 3 && minor >= 11) {+ return { cmd, version };+ }+ }+ }+ }+ return null;+}++function checkUv() {+ return getVersion('uv --version');+}++function checkGo() {+ const version = getVersion('go version');+ return version ? { version } : null;+}++function installPython() {+ console.log('Installing Python 3.11+...');+ if (isWindows) {+ if (runSilent('winget --version')) {+ run('winget install Python.Python.3.12 --accept-package-agreements --accept-source-agreements');+ } else if (runSilent('choco --version')) {+ run('choco install python312 -y');+ } else {+ console.log('ERROR: Cannot auto-install Python. Please install manually:');+ console.log(' https://python.org/downloads/');+ process.exit(1);+ }+ } else if (isMac) {+ if (runSilent('brew --version')) {+ run('brew install [email protected]');+ } else {+ console.log('ERROR: Homebrew not found. Install it first:');+ console.log(' /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"');+ process.exit(1);+ }+ } else {+ // Linux - try package managers+ if (runSilent('apt-get --version')) {+ run('apt-get update && apt-get install -y python3 python3-venv python3-pip curl');+ } else if (runSilent('dnf --version')) {+ run('dnf install -y python3 python3-pip');+ } else if (runSilent('pacman --version')) {+ run('pacman -S --noconfirm python python-pip');+ } else if (runSilent('apk --version')) {+ run('apk add --no-cache python3 py3-pip');+ } else {+ console.log('ERROR: Cannot auto-install Python. Please install manually:');+ console.log(' https://python.org/downloads/');+ process.exit(1);+ }+ }+}++function installUv() {+ console.log('Installing uv...');+ if (isWindows) {+ run('powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"');+ process.env.PATH = `${process.env.USERPROFILE}\\.local\\bin;${process.env.PATH}`;+ } else {+ run('curl -LsSf https://astral.sh/uv/install.sh | sh');+ process.env.PATH = `${process.env.HOME}/.local/bin:${process.env.PATH}`;+ }+}++// ============================================================================+// Main+// ============================================================================++console.log('');+console.log('Checking dependencies...');+console.log('');+console.log(` Node.js: ${getVersion('node --version')}`);+console.log(` npm: ${getVersion('npm --version')}`);++// Check/Install Python+let python = checkPython();+if (python) {+ console.log(` Python: ${python.version}`);+} else {+ installPython();+ python = checkPython();+ if (python) {+ console.log(` Python: ${python.version}`);+ } else {+ console.log('ERROR: Python installation failed');+ process.exit(1);+ }+}++// Check/Install uv+let uvVersion = checkUv();+if (uvVersion) {+ console.log(` uv: ${uvVersion}`);+} else {+ installUv();+ uvVersion = checkUv();+ if (uvVersion) {+ console.log(` uv: ${uvVersion}`);+ } else {+ console.log('ERROR: uv installation failed');+ process.exit(1);+ }+}++// Check Go (optional)+const go = checkGo();+if (go) {+ console.log(` Go: ${go.version}`);+} else {+ console.log(' Go: not found (using pre-built binary)');+}++console.log('');+console.log('Installing...');+console.log('');++try {+ // Create .env if needed+ const envPath = resolve(ROOT, '.env');+ const templatePath = resolve(ROOT, '.env.template');+ if (!existsSync(envPath) && existsSync(templatePath)) {+ copyFileSync(templatePath, envPath);+ console.log('[1/5] Created .env from template');+ } else {+ console.log('[1/5] .env exists');+ }++ // Install client dependencies+ console.log('[2/5] Installing client dependencies...');+ run('npm install', resolve(ROOT, 'client'));++ // Build client+ console.log('[3/5] Building client...');+ run('npm run build', resolve(ROOT, 'client'));++ // Install Python dependencies+ console.log('[4/5] Installing Python dependencies...');+ const serverDir = resolve(ROOT, 'server');+ run('uv venv', serverDir);+ run('uv sync', serverDir);++ // WhatsApp service+ console.log('[5/5] Setting up WhatsApp service...');+ const whatsappDir = resolve(ROOT, 'server/whatsapp-rpc');+ run('npm install', whatsappDir);++ const binPath = resolve(whatsappDir, 'bin', isWindows ? 'whatsapp-rpc-server.exe' : 'whatsapp-rpc-server');+ if (!existsSync(binPath) && go) {+ console.log(' Building WhatsApp from source...');+ run('npm run build', whatsappDir);+ }++ console.log('');+ console.log('Done!');++} catch (err) {+ console.log('');+ console.log(`Failed: ${err.message}`);+ process.exit(1);+}diff --git a/scripts/postinstall.js b/scripts/postinstall.jsindex 3d4a521a..0beadc18 100644--- a/scripts/postinstall.js+++ b/scripts/postinstall.js@@ -1,16 +1,10 @@#!/usr/bin/env node/*** Postinstall script for MachinaOS.- * Outputs to stderr so npm shows the progress (npm suppresses stdout during postinstall).- *- * Scenarios:- * 1. npm install -g machinaos → Run full install with output- * 2. npm install (local) → Run full install with output- * 3. GitHub Actions CI → Skip (workflow handles build separately)** Runs:- * 1. download-binaries.js (optional binary download)- * 2. build.js (full build)+ * 1. download-binaries.js (download pre-built WhatsApp binary)+ * 2. install.js (check deps, install npm/Python packages, build)*/import { spawn } from 'child_process';import { resolve, dirname } from 'path';@@ -19,37 +13,27 @@ import { fileURLToPath } from 'url';const __dirname = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(__dirname, '..');-// Output to stderr so npm shows it during install-const print = (msg) => process.stderr.write(msg + '\n');--// Environment detectionconst isCI = process.env.CI === 'true' || process.env.GITHUB_ACTIONS === 'true';-// Skip in CI - GitHub Actions workflow handles build separatelyif (isCI) {- print('CI environment detected, skipping postinstall.');+ console.log('CI detected, skipping postinstall.');process.exit(0);}-print('');-print('========================================');-print(' MachinaOS - Installing...');-print('========================================');-print('');+console.log('');+console.log('========================================');+console.log(' MachinaOS - Installing...');+console.log('========================================');+console.log('');-// Run a script and pipe all output to stderr (npm shows stderr during install)function runScript(scriptPath) {return new Promise((resolve, reject) => {const child = spawn(process.execPath, [scriptPath], {cwd: ROOT,- stdio: ['inherit', 'pipe', 'pipe'],+ stdio: 'inherit',env: { ...process.env, FORCE_COLOR: '1' }});- // Pipe stdout to stderr so npm shows it- child.stdout.on('data', (data) => process.stderr.write(data));- child.stderr.on('data', (data) => process.stderr.write(data));-child.on('error', reject);child.on('close', (code) => {if (code === 0) {@@ -63,40 +47,38 @@ function runScript(scriptPath) {async function main() {try {- // Step 1: Try to download pre-built binaries (optional, non-fatal)- print('[1/2] Checking for pre-built binaries...');+ // Download pre-built binaries (optional, non-fatal)+ console.log('[1/2] Downloading pre-built binaries...');try {await runScript(resolve(__dirname, 'download-binaries.js'));} catch (e) {- print(' Binary download skipped or failed, will build from source.');+ console.log(' Skipped (will build from source if Go available)');}- // Step 2: Run the full build- print('');- print('[2/2] Building MachinaOS...');- print('');- await runScript(resolve(__dirname, 'build.js'));+ // Run full installation+ console.log('');+ console.log('[2/2] Installing...');+ await runScript(resolve(__dirname, 'install.js'));- print('');- print('========================================');- print(' MachinaOS installed successfully!');- print('========================================');- print('');- print('Run: machinaos start');- print('Open: http://localhost:3000');- print('');+ console.log('');+ console.log('========================================');+ console.log(' MachinaOS installed successfully!');+ console.log('========================================');+ console.log('');+ console.log('Run: machinaos start');+ console.log('Open: http://localhost:3000');+ console.log('');} catch (err) {- print('');- print('========================================');- print(' Installation failed!');- print('========================================');- print('');- print(`Error: ${err.message}`);- print('');- print('Try running manually:');- print(' machinaos build');- print('');+ console.log('');+ console.log('========================================');+ console.log(' Installation failed!');+ console.log('========================================');+ console.log('');+ console.log(`Error: ${err.message}`);+ console.log('');+ console.log('Try: machinaos build');+ console.log('');process.exit(1);}}