2026-07-12 23:42:20 +00:00
#!/bin/bash
set -euo pipefail
PACKAGE = "rea-agents"
REPOSITORY = "morluto/rea"
2026-10-07 23:53:26 +08:00
numeric_identifier = '(0|[1-9][0-9]*)'
prerelease_identifier = "( ${ numeric_identifier } |[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*)"
build_identifier = '[0-9A-Za-z-]+'
version_pattern = " ${ numeric_identifier } \\. ${ numeric_identifier } \\. ${ numeric_identifier } (- ${ prerelease_identifier } (\\. ${ prerelease_identifier } )*)?(\\+ ${ build_identifier } (\\. ${ build_identifier } )*)?"
2026-07-13 15:13:45 +08:00
version = " ${ REA_VERSION :- } "
dry_run = false
start_setup = true
verbose = false
2026-07-12 23:42:20 +00:00
fail() {
printf 'REA installation failed: %s\n' " $1 " >& 2
exit 1
}
2026-07-13 15:13:45 +08:00
usage() {
cat <<'EOF'
Usage: install.sh [--version VERSION] [--dry-run] [--no-setup] [--no-prompt] [--verbose]
2026-07-13 19:17:15 +00:00
Installs only the REA CLI. Run `rea setup` separately to configure Hopper and agents.
2026-07-13 15:13:45 +08:00
EOF
2026-07-12 23:42:20 +00:00
}
2026-07-13 15:13:45 +08:00
while (( $# > 0)) ; do
case " $1 " in
--version)
(( $# >= 2)) || fail "--version requires a value."
version = " $2 "
shift 2
;;
--dry-run)
dry_run = true
shift
;;
--no-setup| --no-prompt)
start_setup = false
shift
;;
--verbose)
verbose = true
shift
;;
--help| -h)
usage
exit 0
;;
*) fail "unknown option: $1 " ;;
2026-07-12 23:42:20 +00:00
esac
2026-07-13 15:13:45 +08:00
done
2026-07-12 23:42:20 +00:00
2026-07-13 15:13:45 +08:00
if [[ " $verbose " == true ]] ; then
set -x
2026-07-12 23:42:20 +00:00
fi
2026-07-13 15:13:45 +08:00
platform = " $( uname -s) "
[[ " $platform " == "Darwin" || " $platform " == "Linux" ]] || fail "REA supports macOS and selected 64-bit Linux distributions."
2026-07-13 19:09:27 +00:00
command -v curl >/dev/null 2>& 1 || fail "curl is required. Install curl, then rerun this installer."
2026-10-05 22:13:22 -07:00
command -v node >/dev/null 2>& 1 || fail "Node.js 22.x (>=22.19), 24.x (>=24.11), or 26+ is required; install it from https://nodejs.org and retry."
2026-07-13 15:13:45 +08:00
command -v npm >/dev/null 2>& 1 || fail "npm is required; install it with Node.js and retry."
2026-07-12 23:42:20 +00:00
node_version = " $( node -p 'process.versions.node' 2>/dev/null || true ) "
2026-07-13 15:13:45 +08:00
IFS = . read -r node_major node_minor _node_patch <<< " $node_version "
2026-07-13 19:09:27 +00:00
[[ " $node_major " = ~ ^[ 0-9] +$ && " $node_minor " = ~ ^[ 0-9] +$ ]] || fail "the active Node.js version could not be read. Check that node works and is on PATH, then retry."
2026-10-05 22:13:22 -07:00
if ! [[ " $_node_patch " = ~ ^[ 0-9] +( \+ [ 0-9A-Za-z.-] +) ?$ ]] ||
! (( node_major == 22 && node_minor >= 19 || node_major == 24 && node_minor >= 11 || node_major >= 26)) ; then
fail "Node.js $node_version is unsupported; use Node.js 22.x (>=22.19), 24.x (>=24.11), or 26+."
2026-07-12 23:42:20 +00:00
fi
2026-07-13 15:13:45 +08:00
if [[ -n " $version " ]] ; then
2026-10-07 23:53:26 +08:00
[[ " $version " = ~ ^${ version_pattern } $ ]] || fail "version must be an exact semantic version."
2026-07-12 23:42:20 +00:00
else
2026-07-13 19:09:27 +00:00
release_json = " $( curl -fsSL --proto '=https' --tlsv1.2 --retry 3 -H 'Accept: application/vnd.github+json' "https://api.github.com/repos/ $REPOSITORY /releases/latest" ) " || fail "the latest REA release could not be resolved. Check network access or pass --version VERSION, then retry."
tag = " $( printf '%s' " $release_json " | node -e 'let s="";process.stdin.on("data",c=>s+=c).on("end",()=>{const v=JSON.parse(s).tag_name;if(typeof v!=="string")process.exit(1);process.stdout.write(v)})' 2>/dev/null) " || fail "the release response was invalid. Retry later or pass --version VERSION."
2026-10-07 23:53:26 +08:00
[[ " $tag " = ~ ^rea-agents-( ${ version_pattern } ) $ ]] || fail "the latest release tag was invalid. Retry later or pass --version VERSION."
2026-07-12 23:42:20 +00:00
version = " ${ BASH_REMATCH [1] } "
fi
2026-10-07 23:53:26 +08:00
# npm's exact-version resolver cleans build metadata from the requested version,
# so it may install the base release and make the post-install identity check fail.
if [[ " $version " = ~ \+ ]] ; then
fail "npm cannot install an exact version with build metadata. Pass a version without build metadata, then retry."
fi
2026-10-11 06:41:51 +08:00
install_args =( install --global)
2026-07-12 23:42:20 +00:00
if [[ " $platform " == "Linux" ]] ; then
2026-10-11 06:41:51 +08:00
install_args +=( --prefix " $HOME /.local" )
2026-07-13 15:13:45 +08:00
install_bin = " $HOME /.local/bin/rea"
2026-07-12 23:42:20 +00:00
else
2026-07-13 19:09:27 +00:00
npm_prefix = " $( npm prefix --global) " || fail "the npm global prefix could not be read. Repair the npm configuration, then retry."
2026-07-13 15:13:45 +08:00
install_bin = " $npm_prefix /bin/rea"
2026-07-12 23:42:20 +00:00
fi
2026-10-11 06:41:51 +08:00
install_args +=( " $PACKAGE @ $version " )
2026-07-12 23:42:20 +00:00
2026-07-13 15:13:45 +08:00
printf 'REA install plan\n'
printf ' Version: %s\n' " $version "
printf ' Runtime: Node.js %s\n' " $node_version "
2026-10-11 06:41:51 +08:00
printf ' Command:'
printf ' %q' npm " ${ install_args [@] } "
printf '\n'
2026-07-13 15:13:45 +08:00
printf ' Binary: %s\n' " $install_bin "
printf ' Setup: %s\n' " $( [[ " $start_setup " == true ]] && printf 'start when a terminal is available' || printf 'skipped' ) "
2026-07-12 23:42:20 +00:00
2026-07-13 15:13:45 +08:00
if [[ " $dry_run " == true ]] ; then
printf 'Dry run complete; no changes made.\n'
exit 0
2026-07-12 23:42:20 +00:00
fi
2026-07-13 15:13:45 +08:00
printf 'Installing %s@%s...\n' " $PACKAGE " " $version "
2026-10-11 06:41:51 +08:00
npm " ${ install_args [@] } " || fail "npm could not install REA. Check registry access and npm permissions, then retry."
2026-07-13 19:09:27 +00:00
[[ -x " $install_bin " ]] || fail "npm completed without installing the rea command. Check the npm global bin directory and PATH, then retry."
installed_version = " $( " $install_bin " --version 2>/dev/null | tr -d '[:space:]' ) " || fail "the installed REA version could not be read. Reinstall the requested version, then retry."
2026-10-03 23:22:07 +08:00
[[ " $installed_version " == " $version " ]] || fail "installed version $installed_version does not match $version . Reinstall the requested version, then retry."
2026-07-13 15:13:45 +08:00
printf 'REA %s is installed.\n' " $version "
if [[ " $start_setup " == true && -r /dev/tty && -w /dev/tty && -t 1 ]] ; then
printf 'Starting REA setup...\n'
" $install_bin " setup </dev/tty >/dev/tty
else
2026-07-13 19:17:15 +00:00
printf 'Run %s setup to configure Hopper and agents.\n' " $install_bin "
2026-07-12 23:42:20 +00:00
fi