feat: 更新 Codex 配置格式
配置变更: - model: gpt-5.1-codex -> gpt-5-codex - base_url: https://www.xcodecli.com/openai (固定) - 新增 disable_response_storage, preferred_auth_method, requires_openai_auth - env_key: OPENAI_API_KEY -> XCODECLI_OAI_KEY - auth.json 中 OPENAI_API_KEY 设为 null 环境变量: - 使用 XCODECLI_OAI_KEY 替代 OPENAI_API_KEY 清理: - 移除不再需要的多端点测试逻辑 - 移除 DEFAULT_BASE_URL 变量 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -14,7 +14,6 @@ param(
|
||||
if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key }
|
||||
|
||||
# Configuration
|
||||
$DefaultBaseUrl = "https://api2.xcodecli.com"
|
||||
$CodexConfigDir = "$env:USERPROFILE\.codex"
|
||||
$CodexConfigFile = "$CodexConfigDir\config.toml"
|
||||
|
||||
@@ -119,7 +118,7 @@ function New-SettingsDirectory {
|
||||
# Function to validate API key format
|
||||
function Test-ApiKey {
|
||||
param([string]$ApiKey)
|
||||
|
||||
|
||||
if ($ApiKey -match '^[A-Za-z0-9_-]+$') {
|
||||
return $true
|
||||
} else {
|
||||
@@ -128,74 +127,29 @@ function Test-ApiKey {
|
||||
}
|
||||
}
|
||||
|
||||
# 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"
|
||||
"Authorization" = "Bearer $ApiKey"
|
||||
}
|
||||
|
||||
$testEndpoint = "$baseUrl/v1/models"
|
||||
$response = Invoke-RestMethod -Uri $testEndpoint -Method Get -Headers $headers -ErrorAction Stop
|
||||
|
||||
# Check if response contains models data
|
||||
if ($response.data) {
|
||||
$modelCount = $response.data.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 Codex configuration
|
||||
function New-Settings {
|
||||
param(
|
||||
[string]$BaseUrl,
|
||||
[string]$ApiKey
|
||||
)
|
||||
|
||||
$config = @"
|
||||
model_provider = "xcodecli"
|
||||
model = "gpt-5.1-codex"
|
||||
model = "gpt-5-codex"
|
||||
model_reasoning_effort = "high"
|
||||
disable_response_storage = true
|
||||
preferred_auth_method = "apikey"
|
||||
|
||||
[model_providers.xcodecli]
|
||||
name = "xcodecli"
|
||||
base_url = "$BaseUrl/v1"
|
||||
base_url = "https://www.xcodecli.com/openai"
|
||||
wire_api = "responses"
|
||||
env_key = "OPENAI_API_KEY"
|
||||
requires_openai_auth = true
|
||||
env_key = "XCODECLI_OAI_KEY"
|
||||
"@
|
||||
|
||||
$authJson = @{
|
||||
OPENAI_API_KEY = $ApiKey
|
||||
OPENAI_API_KEY = $null
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -203,13 +157,12 @@ env_key = "OPENAI_API_KEY"
|
||||
Write-Success "Codex configuration written to: $CodexConfigFile"
|
||||
|
||||
$authJsonPath = "$CodexConfigDir\auth.json"
|
||||
$authJson | ConvertTo-Json | Set-Content -Path $authJsonPath -Encoding UTF8
|
||||
'{ "OPENAI_API_KEY": null }' | Set-Content -Path $authJsonPath -Encoding UTF8
|
||||
Write-Success "Codex auth file written to: $authJsonPath"
|
||||
|
||||
# Set environment variables
|
||||
# Set environment variable XCODECLI_OAI_KEY
|
||||
Write-Info "Setting environment variables..."
|
||||
Set-EnvVariable -Name "OPENAI_API_KEY" -Value $ApiKey
|
||||
Set-EnvVariable -Name "OPENAI_BASE_URL" -Value "$BaseUrl/v1"
|
||||
Set-EnvVariable -Name "XCODECLI_OAI_KEY" -Value $ApiKey
|
||||
Write-Success "Environment variables configured"
|
||||
|
||||
return $true
|
||||
@@ -235,17 +188,21 @@ function Show-Settings {
|
||||
if (Test-Path $authJsonPath) {
|
||||
Write-Host ""
|
||||
Write-Info "Auth file: $authJsonPath"
|
||||
Get-Content $authJsonPath
|
||||
Write-Host "----------------------------------------"
|
||||
$authContent = Get-Content $authJsonPath -Raw | ConvertFrom-Json
|
||||
if ($authContent.OPENAI_API_KEY) {
|
||||
$maskedKey = if ($authContent.OPENAI_API_KEY.Length -gt 12) {
|
||||
"$($authContent.OPENAI_API_KEY.Substring(0, 8))...$($authContent.OPENAI_API_KEY.Substring($authContent.OPENAI_API_KEY.Length - 4))"
|
||||
} else {
|
||||
"$($authContent.OPENAI_API_KEY.Substring(0, [Math]::Min(4, $authContent.OPENAI_API_KEY.Length)))..."
|
||||
}
|
||||
Write-Info "OPENAI_API_KEY: $maskedKey"
|
||||
}
|
||||
|
||||
# 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-Host "----------------------------------------"
|
||||
Write-Info "XCODECLI_OAI_KEY (env): $maskedKey"
|
||||
} else {
|
||||
Write-Warning "XCODECLI_OAI_KEY environment variable not set"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,25 +258,34 @@ function Main {
|
||||
Write-Info "API Key: $maskedKey"
|
||||
Write-Host ""
|
||||
|
||||
# Test API connection and get working base URL
|
||||
$BaseUrl = Test-ApiConnection -ApiKey $ApiKey
|
||||
# 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 (-not $BaseUrl) {
|
||||
if ($Test) {
|
||||
exit 1
|
||||
}
|
||||
|
||||
$continue = Read-Host "All API tests failed. Continue anyway? (y/N)"
|
||||
$continue = Read-Host "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
|
||||
@@ -335,19 +301,17 @@ function Main {
|
||||
Backup-Settings
|
||||
|
||||
# Create new settings
|
||||
if (New-Settings -BaseUrl $BaseUrl -ApiKey $ApiKey) {
|
||||
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"
|
||||
|
||||
if (Test-Path $CodexConfigFile) {
|
||||
Write-Host ""
|
||||
Write-Info "Current settings:"
|
||||
Get-Content $CodexConfigFile
|
||||
}
|
||||
Write-Host ""
|
||||
Show-Settings
|
||||
} else {
|
||||
Write-Error "Failed to create Codex settings"
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user