配置变更: - 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>
372 lines
9.6 KiB
Bash
372 lines
9.6 KiB
Bash
#!/bin/bash
|
|
# Codex Configuration Script for XCodeCLI
|
|
# This script configures Codex to use your XCodeCLI instance
|
|
# Automatically tests multiple API endpoints and selects the working one
|
|
|
|
set -e
|
|
|
|
# Color functions for output
|
|
print_info() {
|
|
echo -e "\033[34m[INFO]\033[0m $1"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "\033[32m[SUCCESS]\033[0m $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "\033[33m[WARNING]\033[0m $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "\033[31m[ERROR]\033[0m $1"
|
|
}
|
|
|
|
# Default values
|
|
TEST_ONLY=false
|
|
SHOW_SETTINGS=false
|
|
|
|
# ========== Shell 环境变量配置 ==========
|
|
# 检测当前 shell 配置文件
|
|
get_shell_rc() {
|
|
if [ -n "${ZSH_VERSION:-}" ] || [ "${SHELL##*/}" = "zsh" ]; then
|
|
echo "$HOME/.zshrc"
|
|
elif [ -n "${BASH_VERSION:-}" ] || [ "${SHELL##*/}" = "bash" ]; then
|
|
if [ -f "$HOME/.bashrc" ]; then
|
|
echo "$HOME/.bashrc"
|
|
else
|
|
echo "$HOME/.bash_profile"
|
|
fi
|
|
elif [ "${SHELL##*/}" = "fish" ]; then
|
|
echo "$HOME/.config/fish/config.fish"
|
|
else
|
|
echo "$HOME/.profile"
|
|
fi
|
|
}
|
|
|
|
# 写入环境变量到 shell 配置文件
|
|
write_env_to_shell() {
|
|
local var_name="$1"
|
|
local var_value="$2"
|
|
local rc_file
|
|
rc_file=$(get_shell_rc)
|
|
|
|
# 确保配置文件存在
|
|
mkdir -p "$(dirname "$rc_file")"
|
|
touch "$rc_file"
|
|
|
|
# 确保文件末尾有换行
|
|
if [ -s "$rc_file" ] && [ "$(tail -c1 "$rc_file" | wc -l)" -eq 0 ]; then
|
|
echo "" >> "$rc_file"
|
|
fi
|
|
|
|
# 转义特殊字符(使用单引号包裹更安全)
|
|
local export_line
|
|
if [ "${SHELL##*/}" = "fish" ]; then
|
|
export_line="set -Ux $var_name '$var_value'"
|
|
else
|
|
export_line="export $var_name='$var_value'"
|
|
fi
|
|
|
|
# 删除旧的同名变量行,添加新行
|
|
local tmp_file
|
|
tmp_file=$(mktemp)
|
|
if [ -s "$rc_file" ]; then
|
|
grep -v "^export $var_name=" "$rc_file" | grep -v "^set -Ux $var_name " > "$tmp_file" 2>/dev/null || true
|
|
fi
|
|
echo "$export_line" >> "$tmp_file"
|
|
|
|
# 保留原文件权限
|
|
cat "$tmp_file" > "$rc_file"
|
|
rm -f "$tmp_file"
|
|
|
|
# 立即生效
|
|
export "$var_name=$var_value"
|
|
}
|
|
|
|
# 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
|
|
}
|
|
|
|
# Check for environment variable API_KEY
|
|
if [ -n "$API_KEY" ]; then
|
|
API_KEY_FROM_ENV="$API_KEY"
|
|
else
|
|
API_KEY_FROM_ENV=""
|
|
fi
|
|
API_KEY=""
|
|
|
|
# Function to show help
|
|
show_help() {
|
|
cat <<EOF
|
|
Codex Configuration Script for XCodeCLI
|
|
|
|
This script automatically tests multiple API endpoints and selects the working one:
|
|
- https://api2.xcodecli.com
|
|
- https://api.xcodecli.com
|
|
|
|
Usage: $0 [OPTIONS]
|
|
|
|
Options:
|
|
--key KEY Set the API key
|
|
--test Test API connections only (requires --key)
|
|
--show Show current settings and exit
|
|
--help Show this help message
|
|
|
|
Examples:
|
|
$0 --key your-api-key-here
|
|
$0 --test --key your-api-key-here
|
|
$0 --show
|
|
|
|
Interactive mode (no arguments):
|
|
$0
|
|
|
|
Environment Variables:
|
|
API_KEY API key for authentication
|
|
EOF
|
|
}
|
|
|
|
# Parse command line arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
--key)
|
|
API_KEY="$2"
|
|
shift 2
|
|
;;
|
|
--test)
|
|
TEST_ONLY=true
|
|
shift
|
|
;;
|
|
--show)
|
|
SHOW_SETTINGS=true
|
|
shift
|
|
;;
|
|
--help)
|
|
show_help
|
|
exit 0
|
|
;;
|
|
*)
|
|
print_error "Unknown option: $1"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Use environment variable if no --key provided
|
|
if [ -z "$API_KEY" ] && [ -n "$API_KEY_FROM_ENV" ]; then
|
|
API_KEY="$API_KEY_FROM_ENV"
|
|
fi
|
|
|
|
# Function to backup existing configuration
|
|
backup_config() {
|
|
if [ -f "$HOME/.codex/config.toml" ]; then
|
|
local timestamp=$(date +%Y%m%d_%H%M%S)
|
|
local backup_file="$HOME/.codex/config.toml.backup.$timestamp"
|
|
local backup_auth_file="$HOME/.codex/auth.json.backup.$timestamp"
|
|
cp "$HOME/.codex/config.toml" "$backup_file"
|
|
if [ -f "$HOME/.codex/auth.json" ]; then
|
|
cp "$HOME/.codex/auth.json" "$backup_auth_file"
|
|
print_info "Backed up existing auth file to: $backup_auth_file"
|
|
fi
|
|
print_info "Backed up existing configuration to: $backup_file"
|
|
fi
|
|
}
|
|
|
|
# Function to create Codex configuration
|
|
create_codex_config() {
|
|
local api_key="$1"
|
|
|
|
# Create config directory if it doesn't exist
|
|
mkdir -p "$HOME/.codex"
|
|
|
|
# Create config.toml
|
|
cat >"$HOME/.codex/config.toml" <<EOF
|
|
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"
|
|
EOF
|
|
|
|
# Create auth.json with OPENAI_API_KEY set to null
|
|
cat >"$HOME/.codex/auth.json" <<EOF
|
|
{
|
|
"OPENAI_API_KEY": null
|
|
}
|
|
EOF
|
|
|
|
print_success "Codex configuration written to: $HOME/.codex/config.toml"
|
|
print_success "Codex auth file written to: $HOME/.codex/auth.json"
|
|
|
|
# Write environment variable XCODECLI_OAI_KEY to shell config
|
|
local rc_file
|
|
rc_file=$(get_shell_rc)
|
|
print_info "Writing environment variables to: $rc_file"
|
|
write_env_to_shell "XCODECLI_OAI_KEY" "$api_key"
|
|
print_success "Environment variables written to shell config"
|
|
|
|
return 0
|
|
}
|
|
|
|
# Function to show current settings
|
|
show_current_settings() {
|
|
print_info "Current Codex settings:"
|
|
echo "----------------------------------------"
|
|
|
|
if [ -f "$HOME/.codex/config.toml" ]; then
|
|
print_info "Configuration file: $HOME/.codex/config.toml"
|
|
echo ""
|
|
cat "$HOME/.codex/config.toml"
|
|
echo ""
|
|
else
|
|
print_info "No configuration file found at $HOME/.codex/config.toml"
|
|
fi
|
|
|
|
echo "----------------------------------------"
|
|
|
|
if [ -f "$HOME/.codex/auth.json" ]; then
|
|
print_info "Auth file: $HOME/.codex/auth.json"
|
|
cat "$HOME/.codex/auth.json"
|
|
echo "----------------------------------------"
|
|
fi
|
|
|
|
# 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
|
|
}
|
|
|
|
# Main function
|
|
main() {
|
|
print_info "Codex Configuration Script for XCodeCLI"
|
|
echo "======================================="
|
|
echo ""
|
|
|
|
# Show current settings and exit if requested
|
|
if [ "$SHOW_SETTINGS" = true ]; then
|
|
show_current_settings
|
|
exit 0
|
|
fi
|
|
|
|
# Interactive mode if no API key provided
|
|
if [ -z "$API_KEY" ]; then
|
|
print_info "Interactive setup mode"
|
|
echo ""
|
|
|
|
# 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 "$API_KEY" ]; then
|
|
print_error "API key is required"
|
|
print_info "Use --help for usage information"
|
|
exit 1
|
|
fi
|
|
|
|
# Validate API key format
|
|
if ! validate_api_key "$API_KEY"; then
|
|
exit 1
|
|
fi
|
|
|
|
# Mask API key for display
|
|
if [ ${#API_KEY} -gt 12 ]; then
|
|
masked_key="${API_KEY:0:8}...${API_KEY: -4}"
|
|
else
|
|
masked_key="${API_KEY:0:4}..."
|
|
fi
|
|
print_info "API Key: $masked_key"
|
|
echo ""
|
|
|
|
# Test API connection with fixed URL
|
|
local test_url="https://www.xcodecli.com/openai"
|
|
print_info "Testing API connection to $test_url..."
|
|
|
|
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
|
|
rm -f /tmp/codex_test_response
|
|
exit 1
|
|
fi
|
|
read -p "Continue anyway? (y/N): " -n 1 -r
|
|
echo
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
print_info "Setup cancelled"
|
|
rm -f /tmp/codex_test_response
|
|
exit 1
|
|
fi
|
|
elif [ "$response" = "000" ]; then
|
|
print_warning "Cannot connect to API endpoint"
|
|
if [ "$TEST_ONLY" = true ]; then
|
|
rm -f /tmp/codex_test_response
|
|
exit 1
|
|
fi
|
|
else
|
|
print_warning "API test returned HTTP status: $response"
|
|
fi
|
|
rm -f /tmp/codex_test_response
|
|
|
|
# Exit if test only
|
|
if [ "$TEST_ONLY" = true ]; then
|
|
print_success "API test completed successfully"
|
|
exit 0
|
|
fi
|
|
|
|
# Backup existing configuration
|
|
backup_config
|
|
|
|
# Create Codex configuration
|
|
if ! create_codex_config "$API_KEY"; then
|
|
print_error "Failed to create Codex configuration"
|
|
exit 1
|
|
fi
|
|
|
|
echo ""
|
|
print_success "Codex has been configured successfully!"
|
|
print_info "You can now use Codex with your XCodeCLI API router."
|
|
print_info ""
|
|
print_info "Configuration file: $HOME/.codex/config.toml"
|
|
print_info "Auth file: $HOME/.codex/auth.json"
|
|
print_info "Environment variable: XCODECLI_OAI_KEY"
|
|
|
|
# Show current settings
|
|
echo ""
|
|
show_current_settings
|
|
}
|
|
|
|
# Run main function
|
|
main
|