# Codex Configuration Script for XCodeCLI (Windows) # This script configures Codex to use your XCodeCLI instance # Automatically tests multiple API endpoints and selects the working one # Run with: powershell -ExecutionPolicy Bypass -File setup-codex.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 $CodexConfigDir = "$env:USERPROFILE\.codex" $CodexConfigFile = "$CodexConfigDir\config.toml" # Function to write environment variable (User level) function Set-EnvVariable { param( [string]$Name, [string]$Value ) # Set for current session [Environment]::SetEnvironmentVariable($Name, $Value, [System.EnvironmentVariableTarget]::Process) # Set permanently for user [Environment]::SetEnvironmentVariable($Name, $Value, [System.EnvironmentVariableTarget]::User) Write-Info "Environment variable set: $Name" } # 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 @" Codex 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-codex.ps1 [OPTIONS] Options: -ApiKey Set the API key -Test Test API connections only (requires -ApiKey) -Show Show current settings and exit -Help Show this help message Examples: .\setup-codex.ps1 -ApiKey your-api-key-here .\setup-codex.ps1 -Test -ApiKey your-api-key-here .\setup-codex.ps1 -Show Interactive mode (no arguments): .\setup-codex.ps1 One-liner: `$key='YOUR_API_KEY'; iwr -useb https://your-domain.tld/setup-codex.ps1 | iex PowerShell Execution Policy: If you get an execution policy error, run: powershell -ExecutionPolicy Bypass -File setup-codex.ps1 "@ exit 0 } # Function to backup existing settings function Backup-Settings { if (Test-Path $CodexConfigFile) { $timestamp = Get-Date -Format "yyyyMMdd_HHmmss" $backupFile = "$CodexConfigFile.backup.$timestamp" Copy-Item -Path $CodexConfigFile -Destination $backupFile Write-Info "Backed up existing settings to: $backupFile" } $authJsonPath = "$CodexConfigDir\auth.json" if (Test-Path $authJsonPath) { $timestamp = Get-Date -Format "yyyyMMdd_HHmmss" $backupFile = "$authJsonPath.backup.$timestamp" Copy-Item -Path $authJsonPath -Destination $backupFile Write-Info "Backed up existing settings to: $backupFile" } } # Function to create settings directory function New-SettingsDirectory { if (-not (Test-Path $CodexConfigDir)) { New-Item -ItemType Directory -Path $CodexConfigDir -Force | Out-Null Write-Info "Created Codex configuration directory: $CodexConfigDir" } } # 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 create Codex configuration function New-Settings { param( [string]$ApiKey ) $config = @" model_provider = "xcodecli" model = "gpt-5-codex" model_reasoning_effort = "high" disable_response_storage = true preferred_auth_method = "apikey" [model_providers.xcodecli] name = "xcodecli" base_url = "https://www.xcodecli.com/openai" wire_api = "responses" requires_openai_auth = true env_key = "XCODECLI_OAI_KEY" "@ $authJson = @{ OPENAI_API_KEY = $null } try { Set-Content -Path $CodexConfigFile -Value $config -Encoding UTF8 Write-Success "Codex configuration written to: $CodexConfigFile" $authJsonPath = "$CodexConfigDir\auth.json" '{ "OPENAI_API_KEY": null }' | Set-Content -Path $authJsonPath -Encoding UTF8 Write-Success "Codex auth file written to: $authJsonPath" # Set environment variable XCODECLI_OAI_KEY Write-Info "Setting environment variables..." Set-EnvVariable -Name "XCODECLI_OAI_KEY" -Value $ApiKey Write-Success "Environment variables configured" return $true } catch { Write-Error "Failed to create configuration file: $($_.Exception.Message)" return $false } } # Function to display current settings function Show-Settings { if (Test-Path $CodexConfigFile) { Write-Info "Current Codex settings:" Write-Host "----------------------------------------" Get-Content $CodexConfigFile Write-Host "----------------------------------------" } else { Write-Info "No existing Codex configuration found." } $authJsonPath = "$CodexConfigDir\auth.json" if (Test-Path $authJsonPath) { Write-Host "" Write-Info "Auth file: $authJsonPath" Get-Content $authJsonPath Write-Host "----------------------------------------" } # Show XCODECLI_OAI_KEY from environment $envKey = [Environment]::GetEnvironmentVariable("XCODECLI_OAI_KEY", "User") if ($envKey) { $maskedKey = if ($envKey.Length -gt 12) { "$($envKey.Substring(0, 8))...$($envKey.Substring($envKey.Length - 4))" } else { "$($envKey.Substring(0, [Math]::Min(4, $envKey.Length)))..." } Write-Info "XCODECLI_OAI_KEY (env): $maskedKey" } else { Write-Warning "XCODECLI_OAI_KEY environment variable not set" } } # Main function function Main { Write-Info "Codex 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 with fixed URL $testUrl = "https://www.xcodecli.com/openai" Write-Info "Testing API connection to $testUrl..." try { $headers = @{ "Content-Type" = "application/json" "Authorization" = "Bearer $ApiKey" } $response = Invoke-RestMethod -Uri "$testUrl/models" -Method Get -Headers $headers -ErrorAction Stop Write-Success "API connection successful!" } catch { if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 401) { Write-Warning "API key authentication failed" } else { Write-Warning "API test failed: $($_.Exception.Message)" } if ($Test) { exit 1 } $continue = Read-Host "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 -ApiKey $ApiKey) { Write-Host "" Write-Success "Codex has been configured successfully!" Write-Info "You can now use Codex with your XCodeCLI API router." Write-Info "" Write-Info "Configuration file: $CodexConfigFile" Write-Info "Auth file: $CodexConfigDir\auth.json" Write-Info "Environment variable: XCODECLI_OAI_KEY" Write-Host "" Show-Settings } else { Write-Error "Failed to create Codex settings" exit 1 } } # Run main function Main