Private
Public Access
1
0

refactor: Remove BaseUrl parameter and update API connection testing logic

This commit is contained in:
2025-11-03 17:05:33 +08:00
parent 882d9ba859
commit 6a37f69bf6

View File

@@ -1,10 +1,9 @@
# Claude Code Configuration Script for XCodeCLI (Windows) # Claude Code Configuration Script for XCodeCLI (Windows)
# This script configures Claude Code to use your XCodeCLI instance # This script configures Claude Code to use your XCodeCLI instance
# Run with: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 -BaseUrl https://api.xcodecli.com -ApiKey YOUR_KEY # Run with: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 -ApiKey YOUR_KEY
# Or via one-liner: & { $url='https://api.xcodecli.com'; $key='YOUR_KEY'; iwr -useb $url/setup-claude-code.ps1 | iex } # Or via one-liner: & { $key='YOUR_KEY'; iwr -useb https://api.xcodecli.com/setup-claude-code.ps1 | iex }
param( param(
[string]$BaseUrl,
[string]$ApiKey, [string]$ApiKey,
[switch]$Test, [switch]$Test,
[switch]$Show, [switch]$Show,
@@ -12,7 +11,6 @@ param(
) )
# Check for pre-set variables from one-liner command (use different names to avoid conflict) # Check for pre-set variables from one-liner command (use different names to avoid conflict)
if (-not $BaseUrl -and (Test-Path Variable:url)) { $BaseUrl = $url }
if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key } if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key }
# Configuration # Configuration
@@ -52,16 +50,19 @@ Claude Code Configuration Script for XCodeCLI (Windows)
Usage: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 [OPTIONS] Usage: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 [OPTIONS]
This script automatically tests multiple API endpoints and selects the working one:
- https://api.xcodecli.com
- https://newapi.sususu.cf
Options: Options:
-BaseUrl <URL> Set the XCodeCLI base URL (default: $DefaultBaseUrl)
-ApiKey <KEY> Set the API key -ApiKey <KEY> Set the API key
-Test Test API connection only (requires -BaseUrl and -ApiKey) -Test Test API connections only (requires -ApiKey)
-Show Show current settings and exit -Show Show current settings and exit
-Help Show this help message -Help Show this help message
Examples: Examples:
.\setup-claude-code.ps1 -BaseUrl https://api.xcodecli.com -ApiKey your-api-key-here .\setup-claude-code.ps1 -ApiKey your-api-key-here
.\setup-claude-code.ps1 -Test -BaseUrl https://api.xcodecli.com -ApiKey your-api-key-here .\setup-claude-code.ps1 -Test -ApiKey your-api-key-here
.\setup-claude-code.ps1 -Show .\setup-claude-code.ps1 -Show
Interactive mode (no arguments): Interactive mode (no arguments):
@@ -104,52 +105,51 @@ function Test-ApiKey {
} }
} }
# Function to test API connection # Function to test API connection and return working base URL
function Test-ApiConnection { function Test-ApiConnection {
param( param(
[string]$BaseUrl,
[string]$ApiKey [string]$ApiKey
) )
Write-Info "Testing API connection..." $testUrls = @(
"https://api.xcodecli.com",
try { "https://newapi.sususu.cf"
$headers = @{ )
"Content-Type" = "application/json"
"X-API-Key" = $ApiKey Write-Info "Testing API connections..."
foreach ($baseUrl in $testUrls) {
Write-Info "Testing $baseUrl..."
try {
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $ApiKey"
}
$uri = "$baseUrl/v1/models"
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers -ErrorAction Stop
if ($response.data -and $response.data.Count -gt 0) {
Write-Success "API connection successful! Found $($response.data.Count) models at $baseUrl"
return $baseUrl
} else {
Write-Warning "API responded but no models found at $baseUrl"
}
} }
catch {
# Determine the correct endpoint based on whether this is a team URL if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 401) {
if ($BaseUrl.EndsWith("/team")) { Write-Warning "API key authentication failed for $baseUrl"
$uri = "$BaseUrl/api/v1/team/stats/spending" } elseif ($_.Exception.Message -like "*Unable to connect*" -or $_.Exception.Message -like "*could not be resolved*") {
$balanceField = "daily_remaining" Write-Warning "Cannot connect to $baseUrl"
$balanceLabel = "Daily remaining" } else {
} else { Write-Warning "API test failed for $baseUrl`: $($_.Exception.Message)"
$uri = "$BaseUrl/api/v1/claude/balance" }
$balanceField = "balance"
$balanceLabel = "Current balance"
}
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers -ErrorAction Stop
if ($response.$balanceField) {
Write-Success "API connection successful! ${balanceLabel}: `$$($response.$balanceField)"
return $true
} else {
Write-Error "API test failed: Invalid response"
return $false
} }
} }
catch {
if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 401) { Write-Error "All API connections failed. Please check your API key and internet connection."
Write-Error "API key authentication failed. Please check your API key." return $null
} elseif ($_.Exception.Message -like "*Unable to connect*" -or $_.Exception.Message -like "*could not be resolved*") {
Write-Error "Cannot connect to API server. Please check the URL and your internet connection."
} else {
Write-Error "API test failed: $($_.Exception.Message)"
}
return $false
}
} }
# Function to create Claude Code settings # Function to create Claude Code settings
@@ -163,12 +163,9 @@ function New-Settings {
env = @{ env = @{
ANTHROPIC_BASE_URL = $BaseUrl ANTHROPIC_BASE_URL = $BaseUrl
ANTHROPIC_AUTH_TOKEN = $ApiKey ANTHROPIC_AUTH_TOKEN = $ApiKey
CLAUDE_CODE_MAX_OUTPUT_TOKENS = 20000
DISABLE_TELEMETRY = 1 DISABLE_TELEMETRY = 1
DISABLE_ERROR_REPORTING = 1 DISABLE_ERROR_REPORTING = 1
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC = 1 CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC = 1
CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR = 1
MAX_THINKING_TOKENS = 12000
} }
model = "sonnet" model = "sonnet"
} }
@@ -238,19 +235,11 @@ function Main {
exit 0 exit 0
} }
# Interactive mode if no URL or Key provided # Interactive mode if no Key provided
if (-not $BaseUrl -and -not $ApiKey) { if (-not $ApiKey) {
Write-Info "Interactive setup mode" Write-Info "Interactive setup mode"
Write-Host "" Write-Host ""
# Get base URL
$inputUrl = Read-Host "Enter XCodeCLI URL [$DefaultBaseUrl]"
if ([string]::IsNullOrWhiteSpace($inputUrl)) {
$BaseUrl = $DefaultBaseUrl
} else {
$BaseUrl = $inputUrl
}
# Get API key # Get API key
while ([string]::IsNullOrWhiteSpace($ApiKey)) { while ([string]::IsNullOrWhiteSpace($ApiKey)) {
$ApiKey = Read-Host "Enter your API key" $ApiKey = Read-Host "Enter your API key"
@@ -263,8 +252,8 @@ function Main {
} }
# Validate inputs # Validate inputs
if ([string]::IsNullOrWhiteSpace($BaseUrl) -or [string]::IsNullOrWhiteSpace($ApiKey)) { if ([string]::IsNullOrWhiteSpace($ApiKey)) {
Write-Error "Both URL and API key are required" Write-Error "API key is required"
Write-Info "Use -Help for usage information" Write-Info "Use -Help for usage information"
exit 1 exit 1
} }
@@ -274,32 +263,33 @@ function Main {
exit 1 exit 1
} }
# Remove trailing slash from URL $maskedKey = if ($ApiKey.Length -gt 12) {
$BaseUrl = $BaseUrl.TrimEnd('/') "$($ApiKey.Substring(0, 8))...$($ApiKey.Substring($ApiKey.Length - 4))"
} else {
Write-Info "Configuration:" "$($ApiKey.Substring(0, [Math]::Min(4, $ApiKey.Length)))..."
Write-Info " Base URL: $BaseUrl"
$maskedKey = if ($ApiKey.Length -gt 12) {
"$($ApiKey.Substring(0, 8))...$($ApiKey.Substring($ApiKey.Length - 4))"
} else {
"$($ApiKey.Substring(0, [Math]::Min(4, $ApiKey.Length)))..."
} }
Write-Info " API Key: $maskedKey" Write-Info "API Key: $maskedKey"
Write-Host "" Write-Host ""
# Test API connection # Test API connection and get working base URL
$connectionSuccess = Test-ApiConnection -BaseUrl $BaseUrl -ApiKey $ApiKey $BaseUrl = Test-ApiConnection -ApiKey $ApiKey
if (-not $connectionSuccess) { if ([string]::IsNullOrWhiteSpace($BaseUrl)) {
if ($Test) { if ($Test) {
exit 1 exit 1
} }
$continue = Read-Host "API test failed. Continue anyway? (y/N)" $continue = Read-Host "All API tests failed. Continue anyway? (y/N)"
if ($continue -notmatch '^[Yy]$') { if ($continue -notmatch '^[Yy]$') {
Write-Info "Setup cancelled" Write-Info "Setup cancelled"
exit 1 exit 1
} }
# If user chooses to continue anyway, use default URL
$BaseUrl = $DefaultBaseUrl
Write-Warning "Using default URL: $BaseUrl"
} else {
Write-Info "Selected working base URL: $BaseUrl"
} }
# Exit if test only # Exit if test only