Private
Public Access
1
0

初始化

This commit is contained in:
2025-11-03 11:46:18 +08:00
commit 187938515d
2 changed files with 696 additions and 0 deletions

364
setup-claude-code.ps1 Normal file
View File

@@ -0,0 +1,364 @@
# Claude Code Configuration Script for XCodeCLI (Windows)
# 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
# Or via one-liner: & { $url='https://api.xcodecli.com'; $key='YOUR_KEY'; iwr -useb $url/setup-claude-code.ps1 | iex }
param(
[string]$BaseUrl,
[string]$ApiKey,
[switch]$Test,
[switch]$Show,
[switch]$Help
)
# 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 }
# Configuration
$DefaultBaseUrl = "https://api.xcodecli.com"
$ClaudeConfigDir = "$env:USERPROFILE\.claude"
$ClaudeSettingsFile = "$ClaudeConfigDir\settings.json"
# Color functions for output
function Write-Info {
param([string]$Message)
Write-Host "[INFO]" -ForegroundColor Blue -NoNewline
Write-Host " $Message"
}
function Write-Success {
param([string]$Message)
Write-Host "[SUCCESS]" -ForegroundColor Green -NoNewline
Write-Host " $Message"
}
function Write-Warning {
param([string]$Message)
Write-Host "[WARNING]" -ForegroundColor Yellow -NoNewline
Write-Host " $Message"
}
function Write-Error {
param([string]$Message)
Write-Host "[ERROR]" -ForegroundColor Red -NoNewline
Write-Host " $Message"
}
# Function to show help
function Show-Help {
Write-Host @"
Claude Code Configuration Script for XCodeCLI (Windows)
Usage: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 [OPTIONS]
Options:
-BaseUrl <URL> Set the XCodeCLI base URL (default: $DefaultBaseUrl)
-ApiKey <KEY> Set the API key
-Test Test API connection only (requires -BaseUrl and -ApiKey)
-Show Show current settings and exit
-Help Show this help message
Examples:
.\setup-claude-code.ps1 -BaseUrl https://api.xcodecli.com -ApiKey your-api-key-here
.\setup-claude-code.ps1 -Test -BaseUrl https://api.xcodecli.com -ApiKey your-api-key-here
.\setup-claude-code.ps1 -Show
Interactive mode (no arguments):
.\setup-claude-code.ps1
PowerShell Execution Policy:
If you get an execution policy error, run:
powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1
"@
exit 0
}
# Function to backup existing settings
function Backup-Settings {
if (Test-Path $ClaudeSettingsFile) {
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$backupFile = "$ClaudeSettingsFile.backup.$timestamp"
Copy-Item -Path $ClaudeSettingsFile -Destination $backupFile
Write-Info "Backed up existing settings to: $backupFile"
}
}
# Function to create settings directory
function New-SettingsDirectory {
if (-not (Test-Path $ClaudeConfigDir)) {
New-Item -ItemType Directory -Path $ClaudeConfigDir -Force | Out-Null
Write-Info "Created Claude configuration directory: $ClaudeConfigDir"
}
}
# Function to validate API key format
function Test-ApiKey {
param([string]$ApiKey)
if ($ApiKey -match '^[A-Za-z0-9_-]+$') {
return $true
} else {
Write-Error "Invalid API key format. API key should contain only alphanumeric characters, hyphens, and underscores."
return $false
}
}
# Function to test API connection
function Test-ApiConnection {
param(
[string]$BaseUrl,
[string]$ApiKey
)
Write-Info "Testing API connection..."
try {
$headers = @{
"Content-Type" = "application/json"
"X-API-Key" = $ApiKey
}
# Determine the correct endpoint based on whether this is a team URL
if ($BaseUrl.EndsWith("/team")) {
$uri = "$BaseUrl/api/v1/team/stats/spending"
$balanceField = "daily_remaining"
$balanceLabel = "Daily remaining"
} else {
$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 "API key authentication failed. Please check your API key."
} 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 New-Settings {
param(
[string]$BaseUrl,
[string]$ApiKey
)
$settings = @{
env = @{
ANTHROPIC_BASE_URL = $BaseUrl
ANTHROPIC_AUTH_TOKEN = $ApiKey
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"
}
try {
$json = $settings | ConvertTo-Json -Depth 10
Set-Content -Path $ClaudeSettingsFile -Value $json -Encoding UTF8
Write-Success "Claude Code settings written to: $ClaudeSettingsFile"
return $true
}
catch {
Write-Error "Failed to create settings file: $($_.Exception.Message)"
return $false
}
}
# Function to display current settings
function Show-Settings {
if (Test-Path $ClaudeSettingsFile) {
Write-Info "Current Claude Code settings:"
Write-Host "----------------------------------------"
$settings = Get-Content $ClaudeSettingsFile -Raw | ConvertFrom-Json
$settings | ConvertTo-Json -Depth 10
Write-Host "----------------------------------------"
} else {
Write-Info "No existing Claude Code settings found."
}
Write-Host ""
Write-Info "Current environment variables:"
Write-Host "----------------------------------------"
$baseUrl = [Environment]::GetEnvironmentVariable("ANTHROPIC_BASE_URL", [EnvironmentVariableTarget]::User)
$authToken = [Environment]::GetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", [EnvironmentVariableTarget]::User)
if ($baseUrl) {
Write-Info "ANTHROPIC_BASE_URL: $baseUrl"
} else {
Write-Info "ANTHROPIC_BASE_URL: (not set)"
}
if ($authToken) {
$maskedToken = if ($authToken.Length -gt 12) {
"$($authToken.Substring(0, 8))...$($authToken.Substring($authToken.Length - 4))"
} else {
"$($authToken.Substring(0, [Math]::Min(4, $authToken.Length)))..."
}
Write-Info "ANTHROPIC_AUTH_TOKEN: $maskedToken"
} else {
Write-Info "ANTHROPIC_AUTH_TOKEN: (not set)"
}
Write-Host "----------------------------------------"
}
# Main function
function Main {
Write-Info "Claude Code Configuration Script for XCodeCLI"
Write-Host "======================================================="
Write-Host ""
# Handle command line arguments
if ($Help) {
Show-Help
}
if ($Show) {
Show-Settings
exit 0
}
# Interactive mode if no URL or Key provided
if (-not $BaseUrl -and -not $ApiKey) {
Write-Info "Interactive setup mode"
Write-Host ""
# Get base URL
$inputUrl = Read-Host "Enter XCodeCLI URL [$DefaultBaseUrl]"
if ([string]::IsNullOrWhiteSpace($inputUrl)) {
$BaseUrl = $DefaultBaseUrl
} else {
$BaseUrl = $inputUrl
}
# Get API key
while ([string]::IsNullOrWhiteSpace($ApiKey)) {
$ApiKey = Read-Host "Enter your API key"
if ([string]::IsNullOrWhiteSpace($ApiKey)) {
Write-Warning "API key is required"
} elseif (-not (Test-ApiKey $ApiKey)) {
$ApiKey = ""
}
}
}
# Validate inputs
if ([string]::IsNullOrWhiteSpace($BaseUrl) -or [string]::IsNullOrWhiteSpace($ApiKey)) {
Write-Error "Both URL and API key are required"
Write-Info "Use -Help for usage information"
exit 1
}
# Validate API key
if (-not (Test-ApiKey $ApiKey)) {
exit 1
}
# Remove trailing slash from URL
$BaseUrl = $BaseUrl.TrimEnd('/')
Write-Info "Configuration:"
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-Host ""
# Test API connection
$connectionSuccess = Test-ApiConnection -BaseUrl $BaseUrl -ApiKey $ApiKey
if (-not $connectionSuccess) {
if ($Test) {
exit 1
}
$continue = Read-Host "API test failed. Continue anyway? (y/N)"
if ($continue -notmatch '^[Yy]$') {
Write-Info "Setup cancelled"
exit 1
}
}
# Exit if test only
if ($Test) {
Write-Success "API test completed successfully"
exit 0
}
# Create settings directory
New-SettingsDirectory
# Backup existing settings
Backup-Settings
# Create new settings
if (New-Settings -BaseUrl $BaseUrl -ApiKey $ApiKey) {
Write-Host ""
# Also set environment variables for Windows
Write-Info "Setting environment variables..."
try {
# Set user environment variables (persistent across sessions)
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", $BaseUrl, [EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", $ApiKey, [EnvironmentVariableTarget]::User)
# Also set for current session
$env:ANTHROPIC_BASE_URL = $BaseUrl
$env:ANTHROPIC_AUTH_TOKEN = $ApiKey
Write-Success "Environment variables set successfully"
}
catch {
Write-Warning "Failed to set environment variables: $($_.Exception.Message)"
Write-Info "You may need to set them manually:"
Write-Info " ANTHROPIC_BASE_URL=$BaseUrl"
Write-Info " ANTHROPIC_AUTH_TOKEN=$ApiKey"
}
Write-Host ""
Write-Success "Claude Code has been configured successfully!"
Write-Info "You can now use Claude Code with your API router."
Write-Info ""
Write-Info "To verify the setup, run:"
Write-Info " claude --version"
Write-Info ""
Write-Info "Configuration file location: $ClaudeSettingsFile"
Write-Info "Environment variables have been set for ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN"
if (Test-Path $ClaudeSettingsFile) {
Write-Host ""
Write-Info "Current settings:"
$settings = Get-Content $ClaudeSettingsFile -Raw | ConvertFrom-Json
$settings | ConvertTo-Json -Depth 10
}
} else {
Write-Error "Failed to create Claude Code settings"
exit 1
}
}
# Run main function
Main

332
setup-claude-code.sh Normal file
View File

@@ -0,0 +1,332 @@
#!/bin/bash
# Claude Code Configuration Script for xcodecli
# This script configures Claude Code to use your xcodecli instance
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
DEFAULT_BASE_URL="https://api.xcodecli.com"
CLAUDE_CONFIG_DIR="$HOME/.claude"
CLAUDE_SETTINGS_FILE="$CLAUDE_CONFIG_DIR/settings.json"
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if jq is installed
check_jq() {
if ! command -v jq &> /dev/null; then
print_error "jq is required but not installed."
print_info "Please install jq:"
print_info " macOS: brew install jq"
print_info " Ubuntu/Debian: sudo apt-get install jq"
print_info " CentOS/RHEL: sudo yum install jq"
exit 1
fi
}
# Function to backup existing settings
backup_settings() {
if [ -f "$CLAUDE_SETTINGS_FILE" ]; then
local backup_file="${CLAUDE_SETTINGS_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
cp "$CLAUDE_SETTINGS_FILE" "$backup_file"
print_info "Backed up existing settings to: $backup_file"
fi
}
# Function to create settings directory
create_settings_dir() {
if [ ! -d "$CLAUDE_CONFIG_DIR" ]; then
mkdir -p "$CLAUDE_CONFIG_DIR"
print_info "Created Claude configuration directory: $CLAUDE_CONFIG_DIR"
fi
}
# Function to validate API key format
validate_api_key() {
local api_key="$1"
if [[ ! "$api_key" =~ ^[A-Za-z0-9_-]+$ ]]; then
print_error "Invalid API key format. API key should contain only alphanumeric characters, hyphens, and underscores."
return 1
fi
return 0
}
# Function to test API connection
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}"
else
print_success "API connection successful! Current balance: \$${balance}"
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
}
# Function to create Claude Code settings
create_settings() {
local base_url="$1"
local api_key="$2"
local settings_json
settings_json=$(cat <<EOF
{
"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"
}
EOF
)
# Validate JSON
if ! echo "$settings_json" | jq . > /dev/null 2>&1; then
print_error "Generated settings JSON is invalid"
return 1
fi
# Write settings file
echo "$settings_json" > "$CLAUDE_SETTINGS_FILE"
print_success "Claude Code settings written to: $CLAUDE_SETTINGS_FILE"
}
# Function to display current settings
display_settings() {
if [ -f "$CLAUDE_SETTINGS_FILE" ]; then
print_info "Current Claude Code settings:"
echo "----------------------------------------"
cat "$CLAUDE_SETTINGS_FILE" | jq .
echo "----------------------------------------"
else
print_info "No existing Claude Code settings found."
fi
}
# Main function
main() {
print_info "Claude Code Configuration Script for xcodecli"
echo "======================================================="
echo
# Check dependencies
check_jq
# Parse command line arguments
local base_url=""
local api_key=""
local test_only=false
local show_settings=false
while [[ $# -gt 0 ]]; do
case $1 in
-u|--url)
base_url="$2"
shift 2
;;
-k|--key)
api_key="$2"
shift 2
;;
-t|--test)
test_only=true
shift
;;
-s|--show)
show_settings=true
shift
;;
-h|--help)
cat <<EOF
Usage: $0 [OPTIONS]
Options:
-u, --url URL Set the xcodecli base URL (default: $DEFAULT_BASE_URL)
-k, --key KEY Set the API key
-t, --test Test API connection only (requires -u and -k)
-s, --show Show current settings and exit
-h, --help Show this help message
Examples:
$0 --url https://your-domain.tld --key your-api-key-here
$0 --test --url https://your-domain.tld --key your-api-key-here
$0 --show
Interactive mode (no arguments):
$0
EOF
exit 0
;;
*)
print_error "Unknown option: $1"
print_info "Use --help for usage information"
exit 1
;;
esac
done
# Show settings and exit if requested
if [ "$show_settings" = true ]; then
display_settings
exit 0
fi
# Interactive mode if no arguments provided
if [ -z "$base_url" ] && [ -z "$api_key" ]; then
print_info "Interactive setup mode"
echo
# Get base URL
read -p "Enter xcodecli URL [$DEFAULT_BASE_URL]: " base_url
if [ -z "$base_url" ]; then
base_url="$DEFAULT_BASE_URL"
fi
# Get API key
while [ -z "$api_key" ]; do
read -p "Enter your API key: " api_key
if [ -z "$api_key" ]; then
print_warning "API key is required"
elif ! validate_api_key "$api_key"; then
api_key=""
fi
done
fi
# Validate inputs
if [ -z "$base_url" ] || [ -z "$api_key" ]; then
print_error "Both URL and API key are required"
print_info "Use --help for usage information"
exit 1
fi
# Validate API key
if ! validate_api_key "$api_key"; then
exit 1
fi
# Remove trailing slash from URL
base_url="${base_url%/}"
print_info "Configuration:"
print_info " Base URL: $base_url"
print_info " API Key: ${api_key:0:8}...${api_key: -4}"
echo
# Test API connection
if ! test_api_connection "$base_url" "$api_key"; then
if [ "$test_only" = true ]; then
exit 1
fi
read -p "API test failed. Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Setup cancelled"
exit 1
fi
fi
# Exit if test only
if [ "$test_only" = true ]; then
print_success "API test completed successfully"
exit 0
fi
# Create settings directory
create_settings_dir
# Backup existing settings
backup_settings
# Create new settings
if create_settings "$base_url" "$api_key"; then
echo
print_success "Claude Code has been configured successfully!"
print_info "You can now use Claude Code with your API router."
print_info ""
print_info "To verify the setup, run:"
print_info " claude --version"
print_info ""
print_info "Configuration file location: $CLAUDE_SETTINGS_FILE"
if [ -f "$CLAUDE_SETTINGS_FILE" ]; then
echo
print_info "Current settings:"
cat "$CLAUDE_SETTINGS_FILE" | jq .
fi
else
print_error "Failed to create Claude Code settings"
exit 1
fi
}
# Run main function
main "$@"