- ClaudeCode/: Claude Code 配置脚本 - GeminiCLI/: Gemini CLI 配置脚本 - codex/: Codex 配置脚本 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
304 lines
8.5 KiB
PowerShell
304 lines
8.5 KiB
PowerShell
# Gemini CLI Configuration Script for XCodeCLI (Windows)
|
|
# This script configures Gemini CLI to use your XCodeCLI instance
|
|
# Automatically tests multiple API endpoints and selects the working one
|
|
# Run with: powershell -ExecutionPolicy Bypass -File setup-gemini.ps1 -ApiKey YOUR_KEY
|
|
|
|
param(
|
|
[string]$ApiKey,
|
|
[switch]$Test,
|
|
[switch]$Show,
|
|
[switch]$Help
|
|
)
|
|
|
|
# Check for pre-set variables from one-liner command
|
|
if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key }
|
|
|
|
# Configuration
|
|
$DefaultBaseUrl = "https://api2.xcodecli.com"
|
|
$GeminiConfigDir = "$env:USERPROFILE\.gemini"
|
|
$GeminiEnvFile = "$GeminiConfigDir\.env"
|
|
|
|
# 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 @"
|
|
Gemini CLI Configuration Script for XCodeCLI (Windows)
|
|
|
|
This script automatically tests multiple API endpoints and selects the working one:
|
|
- https://api2.xcodecli.com
|
|
- https://api.xcodecli.com
|
|
|
|
Usage: powershell -ExecutionPolicy Bypass -File setup-gemini.ps1 [OPTIONS]
|
|
|
|
Options:
|
|
-ApiKey <KEY> Set the API key
|
|
-Test Test API connections only (requires -ApiKey)
|
|
-Show Show current settings and exit
|
|
-Help Show this help message
|
|
|
|
Examples:
|
|
.\setup-gemini.ps1 -ApiKey your-api-key-here
|
|
.\setup-gemini.ps1 -Test -ApiKey your-api-key-here
|
|
.\setup-gemini.ps1 -Show
|
|
|
|
Interactive mode (no arguments):
|
|
.\setup-gemini.ps1
|
|
|
|
One-liner:
|
|
`$key='YOUR_API_KEY'; iwr -useb https://your-domain.tld/setup-gemini.ps1 | iex
|
|
|
|
PowerShell Execution Policy:
|
|
If you get an execution policy error, run:
|
|
powershell -ExecutionPolicy Bypass -File setup-gemini.ps1
|
|
"@
|
|
exit 0
|
|
}
|
|
|
|
# Function to backup existing settings
|
|
function Backup-Settings {
|
|
if (Test-Path $GeminiEnvFile) {
|
|
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
|
|
$backupFile = "$GeminiEnvFile.backup.$timestamp"
|
|
Copy-Item -Path $GeminiEnvFile -Destination $backupFile
|
|
Write-Info "Backed up existing settings to: $backupFile"
|
|
}
|
|
}
|
|
|
|
# Function to create settings directory
|
|
function New-SettingsDirectory {
|
|
if (-not (Test-Path $GeminiConfigDir)) {
|
|
New-Item -ItemType Directory -Path $GeminiConfigDir -Force | Out-Null
|
|
Write-Info "Created Gemini configuration directory: $GeminiConfigDir"
|
|
}
|
|
}
|
|
|
|
# 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 and return working base URL
|
|
function Test-ApiConnection {
|
|
param([string]$ApiKey)
|
|
|
|
$testUrls = @(
|
|
"https://api2.xcodecli.com",
|
|
"https://api.xcodecli.com"
|
|
)
|
|
|
|
Write-Info "Testing API connections..."
|
|
|
|
foreach ($baseUrl in $testUrls) {
|
|
Write-Info "Testing $baseUrl..."
|
|
|
|
try {
|
|
$headers = @{
|
|
"Content-Type" = "application/json"
|
|
"x-goog-api-key" = $ApiKey
|
|
}
|
|
|
|
$testEndpoint = "$baseUrl/v1/models"
|
|
$response = Invoke-RestMethod -Uri $testEndpoint -Method Get -Headers $headers -ErrorAction Stop
|
|
|
|
# Check if response contains models
|
|
if ($response.models) {
|
|
$modelCount = $response.models.Count
|
|
Write-Success "API connection successful! Found $modelCount models at $baseUrl"
|
|
return $baseUrl
|
|
} else {
|
|
Write-Warning "API responded but no models found at $baseUrl"
|
|
}
|
|
}
|
|
catch {
|
|
if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 401) {
|
|
Write-Warning "API key authentication failed for $baseUrl"
|
|
} elseif ($_.Exception.Message -like "*Unable to connect*" -or $_.Exception.Message -like "*could not be resolved*") {
|
|
Write-Warning "Cannot connect to $baseUrl"
|
|
} else {
|
|
Write-Warning "API test failed for $baseUrl`: $($_.Exception.Message)"
|
|
}
|
|
}
|
|
}
|
|
|
|
Write-Error "All API connections failed. Please check your API key and internet connection."
|
|
return $null
|
|
}
|
|
|
|
# Function to create Gemini CLI settings
|
|
function New-Settings {
|
|
param(
|
|
[string]$BaseUrl,
|
|
[string]$ApiKey
|
|
)
|
|
|
|
$envContent = @"
|
|
GOOGLE_GEMINI_BASE_URL="$BaseUrl"
|
|
GEMINI_API_KEY="$ApiKey"
|
|
"@
|
|
|
|
try {
|
|
Set-Content -Path $GeminiEnvFile -Value $envContent -Encoding UTF8
|
|
Write-Success "Gemini CLI settings written to: $GeminiEnvFile"
|
|
return $true
|
|
}
|
|
catch {
|
|
Write-Error "Failed to create .env file: $($_.Exception.Message)"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# Function to display current settings
|
|
function Show-Settings {
|
|
if (Test-Path $GeminiEnvFile) {
|
|
Write-Info "Current Gemini CLI settings file ($GeminiEnvFile):"
|
|
Write-Host "----------------------------------------"
|
|
Get-Content $GeminiEnvFile
|
|
Write-Host "----------------------------------------"
|
|
} else {
|
|
Write-Info "No existing Gemini CLI .env file found."
|
|
}
|
|
|
|
|
|
}
|
|
|
|
# Main function
|
|
function Main {
|
|
Write-Info "Gemini CLI 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 API key provided
|
|
if ([string]::IsNullOrWhiteSpace($ApiKey)) {
|
|
Write-Info "Interactive setup mode"
|
|
Write-Host ""
|
|
|
|
# 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($ApiKey)) {
|
|
Write-Error "API key is required"
|
|
Write-Info "Use -Help for usage information"
|
|
exit 1
|
|
}
|
|
|
|
# Validate API key
|
|
if (-not (Test-ApiKey $ApiKey)) {
|
|
exit 1
|
|
}
|
|
|
|
$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 and get working base URL
|
|
$BaseUrl = Test-ApiConnection -ApiKey $ApiKey
|
|
|
|
if (-not $BaseUrl) {
|
|
if ($Test) {
|
|
exit 1
|
|
}
|
|
|
|
$continue = Read-Host "All API tests failed. Continue anyway? (y/N)"
|
|
if ($continue -notmatch '^[Yy]$') {
|
|
Write-Info "Setup cancelled"
|
|
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
|
|
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 ""
|
|
Write-Success "Gemini CLI has been configured successfully!"
|
|
Write-Info "You can now use Gemini CLI with your API router."
|
|
Write-Info ""
|
|
Write-Info "To verify the setup, run:"
|
|
Write-Info " gemini --version"
|
|
Write-Info ""
|
|
Write-Info "Configuration file location: $GeminiEnvFile"
|
|
|
|
if (Test-Path $GeminiEnvFile) {
|
|
Write-Host ""
|
|
Write-Info "Current settings:"
|
|
Get-Content $GeminiEnvFile
|
|
}
|
|
} else {
|
|
Write-Error "Failed to create Gemini CLI settings"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Run main function
|
|
Main
|