Created
June 13, 2025 09:40
-
-
Save boxabirds/4b11340468b030faec2ed34fe695e5ca to your computer and use it in GitHub Desktop.
Install "ai" command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# AI Shell Installation Script for Unix-like systems (macOS/Linux) | |
# This script installs Node.js/npm if needed, then installs @builder.io/ai-shell | |
echo "======================================" | |
echo "AI Shell Installation Script" | |
echo "======================================" | |
echo "" | |
# Function to check if a command exists | |
command_exists() { | |
command -v "$1" >/dev/null 2>&1 | |
} | |
# Function to detect OS | |
detect_os() { | |
if [[ "$OSTYPE" == "darwin"* ]]; then | |
echo "macos" | |
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then | |
if [ -f /etc/debian_version ]; then | |
echo "debian" | |
elif [ -f /etc/redhat-release ]; then | |
echo "redhat" | |
elif [ -f /etc/arch-release ]; then | |
echo "arch" | |
else | |
echo "linux" | |
fi | |
else | |
echo "unknown" | |
fi | |
} | |
# Function to detect shell configuration file | |
get_shell_config() { | |
if [ -n "$ZSH_VERSION" ]; then | |
echo "$HOME/.zshrc" | |
elif [ -n "$BASH_VERSION" ]; then | |
if [[ "$OSTYPE" == "darwin"* ]]; then | |
# macOS uses .bash_profile by default | |
echo "$HOME/.bash_profile" | |
else | |
echo "$HOME/.bashrc" | |
fi | |
else | |
# Default to .profile | |
echo "$HOME/.profile" | |
fi | |
} | |
# Install Node.js/npm based on OS | |
install_nodejs() { | |
local os=$(detect_os) | |
echo "Installing Node.js and npm for $os..." | |
echo "" | |
case $os in | |
"macos") | |
if command_exists brew; then | |
echo "Installing Node.js via Homebrew..." | |
brew install node | |
else | |
echo "Homebrew not found. Installing Homebrew first..." | |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
# Add Homebrew to PATH for this session | |
if [[ -d "/opt/homebrew/bin" ]]; then | |
export PATH="/opt/homebrew/bin:$PATH" | |
fi | |
# Now install Node.js | |
brew install node | |
fi | |
;; | |
"debian") | |
echo "Installing Node.js via NodeSource repository..." | |
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - | |
sudo apt-get install -y nodejs | |
;; | |
"redhat") | |
echo "Installing Node.js via NodeSource repository..." | |
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash - | |
sudo yum install -y nodejs | |
;; | |
"arch") | |
echo "Installing Node.js via pacman..." | |
sudo pacman -S nodejs npm | |
;; | |
*) | |
echo "Unable to automatically install Node.js on this system." | |
echo "Please visit https://nodejs.org/ to download and install Node.js manually." | |
exit 1 | |
;; | |
esac | |
} | |
# Check if npm is installed | |
if command_exists npm; then | |
echo "✓ npm is already installed (version: $(npm --version))" | |
echo "" | |
else | |
echo "✗ npm is not installed" | |
echo "" | |
# Try to install Node.js/npm | |
install_nodejs | |
# Verify installation | |
if command_exists npm; then | |
echo "" | |
echo "✓ npm has been successfully installed (version: $(npm --version))" | |
echo "" | |
else | |
echo "" | |
echo "✗ Failed to install npm. Please install Node.js manually from:" | |
echo " https://nodejs.org/" | |
exit 1 | |
fi | |
fi | |
# Install @builder.io/ai-shell globally | |
echo "Installing @builder.io/ai-shell globally..." | |
echo "" | |
if npm install -g @builder.io/ai-shell; then | |
echo "" | |
echo "✓ @builder.io/ai-shell has been successfully installed!" | |
echo "" | |
else | |
echo "" | |
echo "✗ Failed to install @builder.io/ai-shell" | |
echo " You may need to use sudo: sudo npm install -g @builder.io/ai-shell" | |
exit 1 | |
fi | |
# Check and set up API keys | |
echo "======================================" | |
echo "API Key Configuration" | |
echo "======================================" | |
echo "" | |
SHELL_CONFIG=$(get_shell_config) | |
api_key_found=false | |
keys_to_add="" | |
# Check existing keys | |
if [ -n "$OPENAI_API_KEY" ]; then | |
echo "✓ OPENAI_API_KEY is already set" | |
api_key_found=true | |
fi | |
if [ -n "$GEMINI_API_KEY" ]; then | |
echo "✓ GEMINI_API_KEY is already set" | |
api_key_found=true | |
fi | |
if [ -n "$ANTHROPIC_API_KEY" ]; then | |
echo "✓ ANTHROPIC_API_KEY is already set" | |
api_key_found=true | |
fi | |
# If no keys found, help user set them up | |
if [ "$api_key_found" = false ]; then | |
echo "" | |
echo "No API keys found. Let's set one up!" | |
echo "" | |
echo "AI Shell needs at least one API key to work." | |
echo "Choose which service you'd like to use:" | |
echo "" | |
echo "1) OpenAI (GPT-4, GPT-3.5)" | |
echo "2) Google Gemini" | |
echo "3) Anthropic (Claude)" | |
echo "4) Skip for now" | |
echo "" | |
read -p "Enter your choice (1-4): " choice | |
case $choice in | |
1) | |
echo "" | |
echo "To get an OpenAI API key:" | |
echo "1. Go to https://platform.openai.com/api-keys" | |
echo "2. Sign in or create an account" | |
echo "3. Click 'Create new secret key'" | |
echo "4. Copy the key (it starts with 'sk-')" | |
echo "" | |
read -p "Paste your OpenAI API key here (or press Enter to skip): " openai_key | |
if [ -n "$openai_key" ]; then | |
keys_to_add="${keys_to_add}export OPENAI_API_KEY=\"$openai_key\"\n" | |
export OPENAI_API_KEY="$openai_key" | |
api_key_found=true | |
fi | |
;; | |
2) | |
echo "" | |
echo "To get a Google Gemini API key:" | |
echo "1. Go to https://makersuite.google.com/app/apikey" | |
echo "2. Sign in with your Google account" | |
echo "3. Click 'Create API key'" | |
echo "4. Copy the key" | |
echo "" | |
read -p "Paste your Gemini API key here (or press Enter to skip): " gemini_key | |
if [ -n "$gemini_key" ]; then | |
keys_to_add="${keys_to_add}export GEMINI_API_KEY=\"$gemini_key\"\n" | |
export GEMINI_API_KEY="$gemini_key" | |
api_key_found=true | |
fi | |
;; | |
3) | |
echo "" | |
echo "To get an Anthropic API key:" | |
echo "1. Go to https://console.anthropic.com/api-keys" | |
echo "2. Sign in or create an account" | |
echo "3. Click 'Create Key'" | |
echo "4. Copy the key" | |
echo "" | |
read -p "Paste your Anthropic API key here (or press Enter to skip): " anthropic_key | |
if [ -n "$anthropic_key" ]; then | |
keys_to_add="${keys_to_add}export ANTHROPIC_API_KEY=\"$anthropic_key\"\n" | |
export ANTHROPIC_API_KEY="$anthropic_key" | |
api_key_found=true | |
fi | |
;; | |
4) | |
echo "" | |
echo "Skipping API key setup..." | |
;; | |
*) | |
echo "Invalid choice. Skipping API key setup..." | |
;; | |
esac | |
fi | |
# Add keys to shell configuration if any were provided | |
if [ -n "$keys_to_add" ]; then | |
echo "" | |
echo "Adding API key(s) to $SHELL_CONFIG..." | |
# Add a comment if this is the first time | |
if ! grep -q "AI Shell API Keys" "$SHELL_CONFIG" 2>/dev/null; then | |
echo "" >> "$SHELL_CONFIG" | |
echo "# AI Shell API Keys" >> "$SHELL_CONFIG" | |
fi | |
# Add the keys | |
echo -e "$keys_to_add" >> "$SHELL_CONFIG" | |
echo "✓ API key(s) saved to $SHELL_CONFIG" | |
fi | |
echo "" | |
echo "======================================" | |
echo "Installation Summary" | |
echo "======================================" | |
echo "" | |
if [ "$api_key_found" = true ]; then | |
echo "✓ AI Shell is installed and configured!" | |
echo "" | |
echo "You can now use AI Shell by running:" | |
echo " ai \"your question or command\"" | |
echo "" | |
echo "Example:" | |
echo " ai \"list all PDF files in the current directory\"" | |
echo "" | |
if [ -n "$keys_to_add" ]; then | |
echo "Note: Restart your terminal or run this command to use AI Shell immediately:" | |
echo " source $SHELL_CONFIG" | |
fi | |
else | |
echo "✓ AI Shell is installed but needs an API key to work." | |
echo "" | |
echo "To add an API key later, run this script again or manually set one of:" | |
echo " export OPENAI_API_KEY=\"your-key\"" | |
echo " export GEMINI_API_KEY=\"your-key\"" | |
echo " export ANTHROPIC_API_KEY=\"your-key\"" | |
echo "" | |
echo "Add it to $SHELL_CONFIG to make it permanent." | |
fi | |
echo "" | |
echo "======================================" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment