diff --git a/setup-claude-code.sh b/setup-claude-code.sh index 008be10..019633e 100644 --- a/setup-claude-code.sh +++ b/setup-claude-code.sh @@ -2,6 +2,7 @@ # Claude Code Configuration Script for XCodeCLI # This script configures Claude Code to use your XCodeCLI instance +# Automatically tests multiple API endpoints and selects the working one set -e @@ -73,55 +74,54 @@ validate_api_key() { return 0 } -# Function to test API connection +# Function to test API connection and return working base URL test_api_connection() { - local base_url="$1" - local api_key="$2" - - print_info "Testing API connection..." - - # Determine the correct endpoint based on whether this is a team URL - local test_endpoint - local balance_field - if [[ "$base_url" == */team ]]; then - test_endpoint="$base_url/api/v1/team/stats/spending" - balance_field="daily_remaining" - else - test_endpoint="$base_url/api/v1/claude/balance" - balance_field="balance" - fi - - # Test a simple request to the API - local response - response=$(curl -s -w "%{http_code}" -o /tmp/claude_test_response \ - -X GET "$test_endpoint" \ - -H "Content-Type: application/json" \ - -H "X-API-Key: $api_key" \ - 2>/dev/null || echo "000") - - if [ "$response" = "200" ]; then - local balance - balance=$(cat /tmp/claude_test_response | jq -r ".${balance_field}" 2>/dev/null || echo "unknown") - if [[ "$base_url" == */team ]]; then - print_success "API connection successful! Daily remaining: \$${balance}" + local api_key="$1" + + local test_urls=( + "https://api.xcodecli.com" + "https://newapi.sususu.cf" + ) + + print_info "Testing API connections..." + + for base_url in "${test_urls[@]}"; do + print_info "Testing $base_url..." + + local test_endpoint="$base_url/v1/models" + + # Test a simple request to the API + local response + response=$(curl -s -w "%{http_code}" -o /tmp/claude_test_response \ + -X GET "$test_endpoint" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $api_key" \ + 2>/dev/null || echo "000") + + if [ "$response" = "200" ]; then + local model_count + model_count=$(cat /tmp/claude_test_response | jq -r '.data | length' 2>/dev/null || echo "0") + if [ "$model_count" -gt "0" ]; then + print_success "API connection successful! Found $model_count models at $base_url" + rm -f /tmp/claude_test_response + echo "$base_url" + return 0 + else + print_warning "API responded but no models found at $base_url" + fi + elif [ "$response" = "401" ]; then + print_warning "API key authentication failed for $base_url" + elif [ "$response" = "000" ]; then + print_warning "Cannot connect to $base_url" else - print_success "API connection successful! Current balance: \$${balance}" + print_warning "API test failed for $base_url with HTTP status: $response" fi + rm -f /tmp/claude_test_response - return 0 - elif [ "$response" = "401" ]; then - print_error "API key authentication failed. Please check your API key." - rm -f /tmp/claude_test_response - return 1 - elif [ "$response" = "000" ]; then - print_error "Cannot connect to API server. Please check the URL and your internet connection." - rm -f /tmp/claude_test_response - return 1 - else - print_error "API test failed with HTTP status: $response" - rm -f /tmp/claude_test_response - return 1 - fi + done + + print_error "All API connections failed. Please check your API key and internet connection." + return 1 } # Function to create Claude Code settings @@ -135,12 +135,9 @@ create_settings() { "env": { "ANTHROPIC_BASE_URL": "$base_url", "ANTHROPIC_AUTH_TOKEN": "$api_key", - "CLAUDE_CODE_MAX_OUTPUT_TOKENS": 20000, "DISABLE_TELEMETRY": 1, "DISABLE_ERROR_REPORTING": 1, "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": 1, - "CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR": 1, - "MAX_THINKING_TOKENS": 12000 }, "model": "sonnet" } @@ -180,17 +177,17 @@ main() { check_jq # Parse command line arguments - local base_url="" local api_key="" local test_only=false local show_settings=false - + + # Check for environment variable API_KEY + if [ -n "$API_KEY" ]; then + api_key="$API_KEY" + fi + while [[ $# -gt 0 ]]; do case $1 in - -u|--url) - base_url="$2" - shift 2 - ;; -k|--key) api_key="$2" shift 2 @@ -207,20 +204,26 @@ main() { cat <