Private
Public Access
1
0

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:
2025-12-12 13:27:05 +08:00
parent 47139e0b9c
commit 87fb54d4b1
2 changed files with 94 additions and 163 deletions

View File

@@ -14,7 +14,6 @@ param(
if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key } if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key }
# Configuration # Configuration
$DefaultBaseUrl = "https://api2.xcodecli.com"
$CodexConfigDir = "$env:USERPROFILE\.codex" $CodexConfigDir = "$env:USERPROFILE\.codex"
$CodexConfigFile = "$CodexConfigDir\config.toml" $CodexConfigFile = "$CodexConfigDir\config.toml"
@@ -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 to create Codex configuration
function New-Settings { function New-Settings {
param( param(
[string]$BaseUrl,
[string]$ApiKey [string]$ApiKey
) )
$config = @" $config = @"
model_provider = "xcodecli" model_provider = "xcodecli"
model = "gpt-5.1-codex" model = "gpt-5-codex"
model_reasoning_effort = "high" model_reasoning_effort = "high"
disable_response_storage = true
preferred_auth_method = "apikey"
[model_providers.xcodecli] [model_providers.xcodecli]
name = "xcodecli" name = "xcodecli"
base_url = "$BaseUrl/v1" base_url = "https://www.xcodecli.com/openai"
wire_api = "responses" wire_api = "responses"
env_key = "OPENAI_API_KEY" requires_openai_auth = true
env_key = "XCODECLI_OAI_KEY"
"@ "@
$authJson = @{ $authJson = @{
OPENAI_API_KEY = $ApiKey OPENAI_API_KEY = $null
} }
try { try {
@@ -203,13 +157,12 @@ env_key = "OPENAI_API_KEY"
Write-Success "Codex configuration written to: $CodexConfigFile" Write-Success "Codex configuration written to: $CodexConfigFile"
$authJsonPath = "$CodexConfigDir\auth.json" $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" Write-Success "Codex auth file written to: $authJsonPath"
# Set environment variables # Set environment variable XCODECLI_OAI_KEY
Write-Info "Setting environment variables..." Write-Info "Setting environment variables..."
Set-EnvVariable -Name "OPENAI_API_KEY" -Value $ApiKey Set-EnvVariable -Name "XCODECLI_OAI_KEY" -Value $ApiKey
Set-EnvVariable -Name "OPENAI_BASE_URL" -Value "$BaseUrl/v1"
Write-Success "Environment variables configured" Write-Success "Environment variables configured"
return $true return $true
@@ -235,17 +188,21 @@ function Show-Settings {
if (Test-Path $authJsonPath) { if (Test-Path $authJsonPath) {
Write-Host "" Write-Host ""
Write-Info "Auth file: $authJsonPath" Write-Info "Auth file: $authJsonPath"
Get-Content $authJsonPath
Write-Host "----------------------------------------" Write-Host "----------------------------------------"
$authContent = Get-Content $authJsonPath -Raw | ConvertFrom-Json }
if ($authContent.OPENAI_API_KEY) {
$maskedKey = if ($authContent.OPENAI_API_KEY.Length -gt 12) { # Show XCODECLI_OAI_KEY from environment
"$($authContent.OPENAI_API_KEY.Substring(0, 8))...$($authContent.OPENAI_API_KEY.Substring($authContent.OPENAI_API_KEY.Length - 4))" $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 { } else {
"$($authContent.OPENAI_API_KEY.Substring(0, [Math]::Min(4, $authContent.OPENAI_API_KEY.Length)))..." "$($envKey.Substring(0, [Math]::Min(4, $envKey.Length)))..."
} }
Write-Info "OPENAI_API_KEY: $maskedKey" Write-Info "XCODECLI_OAI_KEY (env): $maskedKey"
} } else {
Write-Host "----------------------------------------" Write-Warning "XCODECLI_OAI_KEY environment variable not set"
} }
} }
@@ -301,25 +258,34 @@ function Main {
Write-Info "API Key: $maskedKey" Write-Info "API Key: $maskedKey"
Write-Host "" Write-Host ""
# Test API connection and get working base URL # Test API connection with fixed URL
$BaseUrl = Test-ApiConnection -ApiKey $ApiKey $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) { if ($Test) {
exit 1 exit 1
} }
$continue = Read-Host "All API tests failed. Continue anyway? (y/N)" $continue = Read-Host "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
@@ -335,19 +301,17 @@ function Main {
Backup-Settings Backup-Settings
# Create new settings # Create new settings
if (New-Settings -BaseUrl $BaseUrl -ApiKey $ApiKey) { if (New-Settings -ApiKey $ApiKey) {
Write-Host "" Write-Host ""
Write-Success "Codex has been configured successfully!" Write-Success "Codex has been configured successfully!"
Write-Info "You can now use Codex with your XCodeCLI API router." Write-Info "You can now use Codex with your XCodeCLI API router."
Write-Info "" Write-Info ""
Write-Info "Configuration file: $CodexConfigFile" Write-Info "Configuration file: $CodexConfigFile"
Write-Info "Auth file: $CodexConfigDir\auth.json" Write-Info "Auth file: $CodexConfigDir\auth.json"
Write-Info "Environment variable: XCODECLI_OAI_KEY"
if (Test-Path $CodexConfigFile) {
Write-Host "" Write-Host ""
Write-Info "Current settings:" Show-Settings
Get-Content $CodexConfigFile
}
} else { } else {
Write-Error "Failed to create Codex settings" Write-Error "Failed to create Codex settings"
exit 1 exit 1

View File

@@ -23,8 +23,6 @@ print_error() {
} }
# Default values # Default values
DEFAULT_BASE_URL="https://api2.xcodecli.com"
BASE_URL=""
TEST_ONLY=false TEST_ONLY=false
SHOW_SETTINGS=false SHOW_SETTINGS=false
@@ -181,61 +179,9 @@ backup_config() {
fi fi
} }
# Function to test API connection and return working base URL
test_api_connection() {
local api_key="$1"
local test_urls=(
"https://api2.xcodecli.com"
"https://api.xcodecli.com"
)
print_info "Testing API connections..." >&2
for base_url in "${test_urls[@]}"; do
print_info "Testing $base_url..." >&2
local test_endpoint="$base_url/v1/models"
# Test with Authorization Bearer header (OpenAI API style)
local response
response=$(curl -s -w "%{http_code}" -o /tmp/codex_test_response \
-X GET "$test_endpoint" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
2>/dev/null || echo "000")
if [ "$response" = "200" ]; then
# Check if response contains models data
if grep -qE '"data".*\[|"object"' /tmp/codex_test_response 2>/dev/null; then
local model_count
model_count=$(grep -oE '"id"[[:space:]]*:' /tmp/codex_test_response | wc -l | tr -d ' ')
print_success "API connection successful! Found $model_count models at $base_url" >&2
rm -f /tmp/codex_test_response
echo "$base_url"
return 0
else
print_warning "API responded but no models found at $base_url" >&2
fi
elif [ "$response" = "401" ]; then
print_warning "API key authentication failed for $base_url" >&2
elif [ "$response" = "000" ]; then
print_warning "Cannot connect to $base_url" >&2
else
print_warning "API test failed for $base_url with HTTP status: $response" >&2
fi
rm -f /tmp/codex_test_response
done
print_error "All API connections failed. Please check your API key and internet connection." >&2
return 1
}
# Function to create Codex configuration # Function to create Codex configuration
create_codex_config() { create_codex_config() {
local base_url="$1" local api_key="$1"
local api_key="$2"
# Create config directory if it doesn't exist # Create config directory if it doesn't exist
mkdir -p "$HOME/.codex" mkdir -p "$HOME/.codex"
@@ -243,31 +189,34 @@ create_codex_config() {
# Create config.toml # Create config.toml
cat >"$HOME/.codex/config.toml" <<EOF cat >"$HOME/.codex/config.toml" <<EOF
model_provider = "xcodecli" model_provider = "xcodecli"
model = "gpt-5.1-codex" model = "gpt-5-codex"
model_reasoning_effort = "high" model_reasoning_effort = "high"
disable_response_storage = true
preferred_auth_method = "apikey"
[model_providers.xcodecli] [model_providers.xcodecli]
name = "xcodecli" name = "xcodecli"
base_url = "${base_url}/v1" base_url = "https://www.xcodecli.com/openai"
wire_api = "responses" wire_api = "responses"
env_key = "OPENAI_API_KEY" requires_openai_auth = true
env_key = "XCODECLI_OAI_KEY"
EOF EOF
# Create auth.json with OPENAI_API_KEY set to null
cat >"$HOME/.codex/auth.json" <<EOF cat >"$HOME/.codex/auth.json" <<EOF
{ {
"OPENAI_API_KEY": "$api_key" "OPENAI_API_KEY": null
} }
EOF EOF
print_success "Codex configuration written to: $HOME/.codex/config.toml" print_success "Codex configuration written to: $HOME/.codex/config.toml"
print_success "Codex auth file written to: $HOME/.codex/auth.json" print_success "Codex auth file written to: $HOME/.codex/auth.json"
# Write environment variables to shell config # Write environment variable XCODECLI_OAI_KEY to shell config
local rc_file local rc_file
rc_file=$(get_shell_rc) rc_file=$(get_shell_rc)
print_info "Writing environment variables to: $rc_file" print_info "Writing environment variables to: $rc_file"
write_env_to_shell "OPENAI_API_KEY" "$api_key" write_env_to_shell "XCODECLI_OAI_KEY" "$api_key"
write_env_to_shell "OPENAI_BASE_URL" "${base_url}/v1"
print_success "Environment variables written to shell config" print_success "Environment variables written to shell config"
return 0 return 0
@@ -291,14 +240,16 @@ show_current_settings() {
if [ -f "$HOME/.codex/auth.json" ]; then if [ -f "$HOME/.codex/auth.json" ]; then
print_info "Auth file: $HOME/.codex/auth.json" print_info "Auth file: $HOME/.codex/auth.json"
cat "$HOME/.codex/auth.json"
echo "----------------------------------------" echo "----------------------------------------"
local api_key
api_key=$(grep -o '"OPENAI_API_KEY"[[:space:]]*:[[:space:]]*"[^"]*"' "$HOME/.codex/auth.json" | sed 's/.*: *"//' | sed 's/"$//')
if [ -n "$api_key" ]; then
local masked_key="${api_key:0:8}...${api_key: -4}"
print_info "OPENAI_API_KEY: $masked_key"
fi fi
echo "----------------------------------------"
# Show XCODECLI_OAI_KEY from environment
if [ -n "$XCODECLI_OAI_KEY" ]; then
local masked_key="${XCODECLI_OAI_KEY:0:8}...${XCODECLI_OAI_KEY: -4}"
print_info "XCODECLI_OAI_KEY (env): $masked_key"
else
print_warning "XCODECLI_OAI_KEY environment variable not set"
fi fi
} }
@@ -351,27 +302,42 @@ main() {
print_info "API Key: $masked_key" print_info "API Key: $masked_key"
echo "" echo ""
# Test API connection and get working base URL # Test API connection with fixed URL
BASE_URL=$(test_api_connection "$API_KEY") local test_url="https://www.xcodecli.com/openai"
print_info "Testing API connection to $test_url..."
if [ $? -ne 0 ] || [ -z "$BASE_URL" ]; then local response
response=$(curl -s -w "%{http_code}" -o /tmp/codex_test_response \
-X GET "$test_url/models" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
2>/dev/null || echo "000")
if [ "$response" = "200" ]; then
print_success "API connection successful!"
elif [ "$response" = "401" ]; then
print_warning "API key authentication failed"
if [ "$TEST_ONLY" = true ]; then if [ "$TEST_ONLY" = true ]; then
rm -f /tmp/codex_test_response
exit 1 exit 1
fi fi
read -p "Continue anyway? (y/N): " -n 1 -r
read -p "All API tests failed. Continue anyway? (y/N): " -n 1 -r
echo echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Setup cancelled" print_info "Setup cancelled"
rm -f /tmp/codex_test_response
exit 1 exit 1
fi fi
elif [ "$response" = "000" ]; then
# If user chooses to continue anyway, use default URL print_warning "Cannot connect to API endpoint"
BASE_URL="$DEFAULT_BASE_URL" if [ "$TEST_ONLY" = true ]; then
print_warning "Using default URL: $BASE_URL" rm -f /tmp/codex_test_response
else exit 1
print_info "Selected working base URL: $BASE_URL"
fi fi
else
print_warning "API test returned HTTP status: $response"
fi
rm -f /tmp/codex_test_response
# Exit if test only # Exit if test only
if [ "$TEST_ONLY" = true ]; then if [ "$TEST_ONLY" = true ]; then
@@ -383,7 +349,7 @@ main() {
backup_config backup_config
# Create Codex configuration # Create Codex configuration
if ! create_codex_config "$BASE_URL" "$API_KEY"; then if ! create_codex_config "$API_KEY"; then
print_error "Failed to create Codex configuration" print_error "Failed to create Codex configuration"
exit 1 exit 1
fi fi
@@ -394,6 +360,7 @@ main() {
print_info "" print_info ""
print_info "Configuration file: $HOME/.codex/config.toml" print_info "Configuration file: $HOME/.codex/config.toml"
print_info "Auth file: $HOME/.codex/auth.json" print_info "Auth file: $HOME/.codex/auth.json"
print_info "Environment variable: XCODECLI_OAI_KEY"
# Show current settings # Show current settings
echo "" echo ""