#!/bin/bash ################################################################################ # Claude Code Installation Script with Z.AI API Support ################################################################################ # This script optionally installs Claude Code and configures it with Z.AI API # from https://docs.z.ai/devpack/tool/claude # # Features: # - Automated Claude Code installation via npm # - Z.AI API configuration (GLM models) # - Manual configuration instructions # - Model configuration for glm-4.5-air, glm-4.7 # # Usage: ./install-claude-code.sh [options] # --auto Automatic installation with API key prompt # --manual Show manual installation steps only # --skip-install Skip Claude Code installation, just configure API ################################################################################ set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' MAGENTA='\033[0;35m' BOLD='\033[1m' NC='\033[0m' # No Color # Claude config directory CLAUDE_DIR="${HOME}/.claude" # Flags AUTO_MODE=true SKIP_INSTALL=false ################################################################################ # Helper Functions ################################################################################ log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } log_step() { echo -e "${CYAN}${BOLD}==>${NC} ${BOLD}$1${NC}" } print_banner() { echo -e "${MAGENTA}${BOLD}" cat << "EOF" ╔═══════════════════════════════════════════════════════════════╗ ║ ║ ║ ███████╗██╗ █████╗ ██████╗ ║ ║ ██╔════╝██║ ██╔══██╗██╔════╝ ║ ║ ███████╗██║ ███████║██║ ███╗ ║ ║ ╚════██║██║ ██╔══██║██║ ██║ ║ ║ ███████║███████╗██║ ██║╚██████╔╝ ║ ║ ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ║ ║ ║ ║ + Z.AI API Integration (GLM Models) ║ ║ ║ ║ Installation Script ║ ║ ║ ╚═══════════════════════════════════════════════════════════════╝ EOF echo -e "${NC}" echo "" echo -e "${CYAN}Official Documentation:${NC} https://docs.z.ai/devpack/tool/claude" echo "" } check_nodejs() { log_step "Checking for Node.js 18+..." if ! command -v node &> /dev/null; then log_error "Node.js not found!" echo "" echo -e "${YELLOW}Node.js 18 or newer is required to install Claude Code.${NC}" echo "" echo "Please install Node.js first:" echo " - Ubuntu/Debian: sudo apt install nodejs npm" echo " - Arch: sudo pacman -S nodejs npm" echo " - macOS: brew install node" echo " - Or visit: https://nodejs.org/" echo "" exit 1 fi local node_version=$(node -v | cut -d'v' -f2 | cut -d'.' -f1) if [ "$node_version" -lt 18 ]; then log_error "Node.js version $node_version is too old!" echo "Node.js 18 or newer is required." exit 1 fi log_success "Node.js $(node -v) found" } install_claude_code() { if [ "$SKIP_INSTALL" = true ]; then log_info "Skipping Claude Code installation (--skip-install flag)" return fi log_step "Installing Claude Code..." if command -v claude &> /dev/null; then local current_version=$(claude --version 2>/dev/null | head -1 || echo "unknown") log_info "Claude Code already installed: $current_version" echo "" read -p "Update to latest version? [y/N] " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then log_info "Updating Claude Code..." npm update -g @anthropic-ai/claude-code log_success "Claude Code updated" fi else log_info "Installing Claude Code via npm..." npm install -g @anthropic-ai/claude-code log_success "Claude Code installed" fi echo "" claude --version } get_zai_api_key() { log_step "Z.AI API Configuration" echo "" echo -e "${CYAN}To use Claude Code with Z.AI's GLM models, you need an API key.${NC}" echo "" echo -e "${YELLOW}Get your API key from:${NC} https://docs.z.ai/devpack/tool/claude" echo "" echo -e "${MAGENTA}Your API key format is: ${BOLD}your-api-key-here${NC}" echo "" read -p "Enter your Z.AI API key (or press Enter to skip): " -r API_KEY echo "" if [ -z "$API_KEY" ]; then log_warn "No API key provided. You can configure it later manually." return 1 fi return 0 } configure_zai_api() { local api_key="$1" log_step "Configuring Z.AI API..." # Ensure .claude directory exists mkdir -p "$CLAUDE_DIR" # Backup existing settings if [ -f "$CLAUDE_DIR/settings.json" ]; then cp "$CLAUDE_DIR/settings.json" "$CLAUDE_DIR/settings.json.backup-$(date +%Y%m%d_%H%M%S)" log_info "Backed up existing settings.json" fi # Create or update settings.json with Z.AI configuration if [ -f "$CLAUDE_DIR/settings.json" ]; then # Merge with existing settings python3 -c " import json import sys try: with open('$CLAUDE_DIR/settings.json', 'r') as f: settings = json.load(f) except: settings = {} # Add or update env section if 'env' not in settings: settings['env'] = {} # Configure Z.AI GLM models settings['env']['ANTHROPIC_API_KEY'] = '$api_key' settings['env']['ANTHROPIC_DEFAULT_HAIKU_MODEL'] = 'glm-4.5-air' settings['env']['ANTHROPIC_DEFAULT_SONNET_MODEL'] = 'glm-4.7' settings['env']['ANTHROPIC_DEFAULT_OPUS_MODEL'] = 'glm-4.7' with open('$CLAUDE_DIR/settings.json', 'w') as f: json.dump(settings, f, indent=2) " 2>/dev/null || { # Fallback if python3 not available cat > "$CLAUDE_DIR/settings.json" << EOF { "env": { "ANTHROPIC_API_KEY": "$api_key", "ANTHROPIC_DEFAULT_HAIKU_MODEL": "glm-4.5-air", "ANTHROPIC_DEFAULT_SONNET_MODEL": "glm-4.7", "ANTHROPIC_DEFAULT_OPUS_MODEL": "glm-4.7" } } EOF } else # Create new settings.json cat > "$CLAUDE_DIR/settings.json" << EOF { "env": { "ANTHROPIC_API_KEY": "$api_key", "ANTHROPIC_DEFAULT_HAIKU_MODEL": "glm-4.5-air", "ANTHROPIC_DEFAULT_SONNET_MODEL": "glm-4.7", "ANTHROPIC_DEFAULT_OPUS_MODEL": "glm-4.7" } } EOF fi log_success "Z.AI API configured" log_info "API key set to: ${api_key:0:10}...${api_key: -4}" } show_manual_instructions() { echo "" echo -e "${CYAN}${BOLD}═══════════════════════════════════════════════════════════════${NC}" echo -e "${CYAN}${BOLD} Manual Installation Instructions${NC}" echo -e "${CYAN}${BOLD}═══════════════════════════════════════════════════════════════${NC}" echo "" echo -e "${GREEN}Step 1: Install Claude Code${NC}" echo "─────────────────────────────────────────────────────────────────" echo "" echo " # Prerequisites: Node.js 18 or newer" echo " npm install -g @anthropic-ai/claude-code" echo "" echo " # Verify installation" echo " claude --version" echo "" echo -e "${GREEN}Step 2: Get Z.AI API Key${NC}" echo "─────────────────────────────────────────────────────────────────" echo "" echo " Visit: https://docs.z.ai/devpack/tool/claude" echo " Sign up and get your API key" echo "" echo -e "${GREEN}Step 3: Configure Z.AI API${NC}" echo "─────────────────────────────────────────────────────────────────" echo "" echo " Create or edit ~/.claude/settings.json:" echo "" echo -e "${YELLOW} {${NC}" echo -e "${YELLOW} \"env\": {${NC}" echo -e "${YELLOW} \"ANTHROPIC_API_KEY\": \"your-zai-api-key-here\",${NC}" echo -e "${YELLOW} \"ANTHROPIC_DEFAULT_HAIKU_MODEL\": \"glm-4.5-air\",${NC}" echo -e "${YELLOW} \"ANTHROPIC_DEFAULT_SONNET_MODEL\": \"glm-4.7\",${NC}" echo -e "${YELLOW} \"ANTHROPIC_DEFAULT_OPUS_MODEL\": \"glm-4.7\"${NC}" echo -e "${YELLOW} }${NC}" echo -e "${YELLOW} }${NC}" echo "" echo -e "${GREEN}Step 4: Start Claude Code${NC}" echo "─────────────────────────────────────────────────────────────────" echo "" echo " cd your-project-directory" echo " claude" echo "" echo " # Check model status with: /status" echo "" echo -e "${GREEN}Available GLM Models${NC}" echo "─────────────────────────────────────────────────────────────────" echo "" echo " • glm-4.5-air - Fast, efficient model (Haiku class)" echo " • glm-4.7 - Powerful model (Sonnet/Opus class)" echo "" echo -e "${CYAN}${BOLD}═══════════════════════════════════════════════════════════════${NC}" echo "" } print_success_summary() { echo "" echo -e "${GREEN}${BOLD}╔═══════════════════════════════════════════════════════════════╗${NC}" echo -e "${GREEN}${BOLD}║ Installation Complete! ║${NC}" echo -e "${GREEN}${BOLD}╚═══════════════════════════════════════════════════════════════╝${NC}" echo "" echo -e "${BOLD}Claude Code is ready with Z.AI GLM models!${NC}" echo "" echo -e "${CYAN}Quick Start:${NC}" echo -e " 1. ${YELLOW}cd your-project-directory${NC}" echo -e " 2. ${YELLOW}claude${NC}" echo -e " 3. ${YELLOW}/status${NC} - Check current model status" echo "" echo -e "${CYAN}Model Configuration:${NC}" echo -e " • ${GREEN}Haiku class${NC}: glm-4.5-air (fast, efficient)" echo -e " • ${GREEN}Sonnet class${NC}: glm-4.7 (powerful)" echo -e " • ${GREEN}Opus class${NC}: glm-4.7 (most powerful)" echo "" echo -e "${CYAN}Configuration File:${NC}" echo -e " ${YELLOW}~/.claude/settings.json${NC}" echo "" echo -e "${CYAN}Documentation:${NC}" echo -e " ${YELLOW}https://docs.z.ai/devpack/tool/claude${NC}" echo "" } ################################################################################ # Main Installation ################################################################################ main() { print_banner # Parse arguments while [[ $# -gt 0 ]]; do case $1 in --auto) AUTO_MODE=true shift ;; --manual) AUTO_MODE=false shift ;; --skip-install) SKIP_INSTALL=true shift ;; -h|--help) echo "Usage: $0 [options]" echo " --auto Automatic installation with API key prompt (default)" echo " --manual Show manual installation steps only" echo " --skip-install Skip Claude Code installation, just configure API" exit 0 ;; *) log_error "Unknown option: $1" exit 1 ;; esac done # Manual mode - just show instructions if [ "$AUTO_MODE" = false ]; then show_manual_instructions exit 0 fi # Automatic mode check_nodejs install_claude_code # Get and configure API key if get_zai_api_key; then configure_zai_api "$API_KEY" print_success_summary else echo "" log_info "You can configure the API key later by:" echo " 1. Editing ~/.claude/settings.json" echo " 2. Or running this script again" echo "" show_manual_instructions fi } # Run main function main "$@"