|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# ============================================================================ |
| 5 | +# Build @agentis/local — Standalone bundle |
| 6 | +# |
| 7 | +# Produces a self-contained Next.js standalone server in |
| 8 | +# packages/local-runner/bundle/ that can be run with just Node.js. |
| 9 | +# |
| 10 | +# The standalone output preserves the monorepo directory layout because |
| 11 | +# Next.js resolves modules relative to outputFileTracingRoot (repo root). |
| 12 | +# The entry point is bundle/apps/web/server.js and must be run with |
| 13 | +# CWD set to bundle/ (the CLI handles this automatically). |
| 14 | +# |
| 15 | +# Usage: |
| 16 | +# ./scripts/build-local.sh # Build the bundle |
| 17 | +# ./scripts/build-local.sh --clean # Clean and rebuild |
| 18 | +# ============================================================================ |
| 19 | + |
| 20 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 21 | +ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 22 | +WEB_DIR="$ROOT_DIR/apps/web" |
| 23 | +BUNDLE_DIR="$ROOT_DIR/packages/local-runner/bundle" |
| 24 | + |
| 25 | +# Colors |
| 26 | +GREEN='\033[0;32m' |
| 27 | +YELLOW='\033[1;33m' |
| 28 | +CYAN='\033[0;36m' |
| 29 | +NC='\033[0m' |
| 30 | + |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +# Parse flags |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | + |
| 35 | +if [[ "${1:-}" == "--clean" ]]; then |
| 36 | + echo -e "${YELLOW}Cleaning previous bundle...${NC}" |
| 37 | + rm -rf "$BUNDLE_DIR" |
| 38 | + rm -rf "$WEB_DIR/.next" |
| 39 | +fi |
| 40 | + |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | +# Step 1: Build the web app (turbo resolves package deps automatically) |
| 43 | +# --------------------------------------------------------------------------- |
| 44 | + |
| 45 | +echo -e "${CYAN}[1/6] Building web app (standalone)...${NC}" |
| 46 | +cd "$ROOT_DIR" |
| 47 | + |
| 48 | +# Build only @multiverse/web — NOT @agentis/local (avoids infinite recursion). |
| 49 | +AGENTIS_LOCAL_MODE=true \ |
| 50 | +NEXT_PUBLIC_AGENTIS_LOCAL=true \ |
| 51 | +NEXT_PUBLIC_ENABLE_INTERNAL_TRANSCRIPT=true \ |
| 52 | +pnpm turbo run build --filter=@multiverse/web --force |
| 53 | + |
| 54 | +# --------------------------------------------------------------------------- |
| 55 | +# Step 2: Verify standalone output exists |
| 56 | +# --------------------------------------------------------------------------- |
| 57 | + |
| 58 | +STANDALONE_DIR="$WEB_DIR/.next/standalone" |
| 59 | +STANDALONE_SERVER="$STANDALONE_DIR/apps/web/server.js" |
| 60 | + |
| 61 | +if [ ! -f "$STANDALONE_SERVER" ]; then |
| 62 | + echo "ERROR: Standalone server not found at $STANDALONE_SERVER" |
| 63 | + echo "Make sure next.config.ts has output: 'standalone' (not 'export')" |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +echo -e "${CYAN}[2/6] Standalone server found${NC}" |
| 68 | + |
| 69 | +# --------------------------------------------------------------------------- |
| 70 | +# Step 3: Copy static assets into standalone (Next.js doesn't include them) |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | + |
| 73 | +echo -e "${CYAN}[3/6] Copying static assets...${NC}" |
| 74 | + |
| 75 | +STANDALONE_WEB="$STANDALONE_DIR/apps/web" |
| 76 | + |
| 77 | +# public/ — sprites, demos, favicons |
| 78 | +cp -r "$WEB_DIR/public" "$STANDALONE_WEB/public" |
| 79 | + |
| 80 | +# .next/static/ — compiled JS/CSS chunks |
| 81 | +mkdir -p "$STANDALONE_WEB/.next" |
| 82 | +cp -r "$WEB_DIR/.next/static" "$STANDALONE_WEB/.next/static" |
| 83 | + |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | +# Step 4: Hoist pnpm virtual store into flat node_modules |
| 86 | +# |
| 87 | +# pnpm standalone output uses .pnpm/ virtual store but Node.js can't |
| 88 | +# resolve packages like 'styled-jsx' without top-level symlinks. |
| 89 | +# We create real copies (not symlinks) for portability. |
| 90 | +# --------------------------------------------------------------------------- |
| 91 | + |
| 92 | +echo -e "${CYAN}[4/6] Hoisting pnpm dependencies...${NC}" |
| 93 | + |
| 94 | +PNPM_STORE="$STANDALONE_DIR/node_modules/.pnpm" |
| 95 | +ROOT_NM="$STANDALONE_DIR/node_modules" |
| 96 | + |
| 97 | +if [ -d "$PNPM_STORE" ]; then |
| 98 | + # For each package version dir in .pnpm/, hoist its node_modules entries. |
| 99 | + # pnpm creates symlinks in node_modules/ pointing into .pnpm/ — we replace |
| 100 | + # those symlinks with real copies so the bundle is self-contained. |
| 101 | + for pkg_dir in "$PNPM_STORE"/*/node_modules/*; do |
| 102 | + [ -d "$pkg_dir" ] || continue |
| 103 | + pkg_name=$(basename "$pkg_dir") |
| 104 | + |
| 105 | + # Handle scoped packages (@img, @next, @swc, etc.) |
| 106 | + parent_dir=$(dirname "$pkg_dir") |
| 107 | + grandparent=$(basename "$(dirname "$parent_dir")") |
| 108 | + if [[ "$grandparent" == "node_modules" ]]; then |
| 109 | + scope_dir=$(basename "$(dirname "$pkg_dir")") |
| 110 | + if [[ "$scope_dir" == @* ]]; then |
| 111 | + mkdir -p "$ROOT_NM/$scope_dir" |
| 112 | + target="$ROOT_NM/$scope_dir/$pkg_name" |
| 113 | + # Replace symlinks with real copies; skip if already a real directory |
| 114 | + if [ -L "$target" ]; then |
| 115 | + rm "$target" |
| 116 | + elif [ -d "$target" ]; then |
| 117 | + continue |
| 118 | + fi |
| 119 | + cp -r "$pkg_dir" "$target" |
| 120 | + continue |
| 121 | + fi |
| 122 | + fi |
| 123 | + |
| 124 | + # Regular (non-scoped) package |
| 125 | + if [ "$pkg_name" == ".pnpm" ]; then |
| 126 | + continue |
| 127 | + fi |
| 128 | + target="$ROOT_NM/$pkg_name" |
| 129 | + # Replace symlinks with real copies; skip if already a real directory |
| 130 | + if [ -L "$target" ]; then |
| 131 | + rm "$target" |
| 132 | + elif [ -e "$target" ]; then |
| 133 | + continue |
| 134 | + fi |
| 135 | + cp -r "$pkg_dir" "$target" |
| 136 | + done |
| 137 | +fi |
| 138 | + |
| 139 | +# --------------------------------------------------------------------------- |
| 140 | +# Step 5: Copy entire standalone tree into bundle/ (preserving layout) |
| 141 | +# --------------------------------------------------------------------------- |
| 142 | + |
| 143 | +echo -e "${CYAN}[5/6] Assembling bundle...${NC}" |
| 144 | + |
| 145 | +rm -rf "$BUNDLE_DIR" |
| 146 | +cp -r "$STANDALONE_DIR" "$BUNDLE_DIR" |
| 147 | + |
| 148 | +# --------------------------------------------------------------------------- |
| 149 | +# Step 6: Copy LICENSE into package directory for npm |
| 150 | +# --------------------------------------------------------------------------- |
| 151 | + |
| 152 | +echo -e "${CYAN}[6/6] Copying LICENSE...${NC}" |
| 153 | + |
| 154 | +RUNNER_DIR="$ROOT_DIR/packages/local-runner" |
| 155 | +cp "$ROOT_DIR/LICENSE" "$RUNNER_DIR/LICENSE" |
| 156 | + |
| 157 | +# --------------------------------------------------------------------------- |
| 158 | +# Summary + size guard |
| 159 | +# --------------------------------------------------------------------------- |
| 160 | + |
| 161 | +BUNDLE_SIZE=$(du -sh "$BUNDLE_DIR" | cut -f1) |
| 162 | +BUNDLE_BYTES=$(du -s "$BUNDLE_DIR" | cut -f1) |
| 163 | +MAX_BUNDLE_KB=200000 # 200MB uncompressed — warn if exceeded |
| 164 | + |
| 165 | +echo "" |
| 166 | +echo -e "${GREEN}Bundle complete!${NC}" |
| 167 | +echo -e " Location: ${CYAN}$BUNDLE_DIR${NC}" |
| 168 | +echo -e " Size: ${CYAN}$BUNDLE_SIZE${NC}" |
| 169 | + |
| 170 | +if [ "$BUNDLE_BYTES" -gt "$MAX_BUNDLE_KB" ]; then |
| 171 | + echo "" |
| 172 | + echo -e "${YELLOW}WARNING: Bundle exceeds ${MAX_BUNDLE_KB}KB size budget${NC}" |
| 173 | + echo -e "${YELLOW}Consider auditing dependencies or static assets${NC}" |
| 174 | +fi |
| 175 | + |
| 176 | +echo "" |
| 177 | +echo "Test locally:" |
| 178 | +echo " node packages/local-runner/bin/agentis-local.js --no-open" |
| 179 | +echo "" |
| 180 | +echo "Package and test via npx:" |
| 181 | +echo " cd packages/local-runner && npm pack" |
| 182 | +echo " npx ./gpu-cli-agentis-0.1.0.tgz --no-open" |
0 commit comments