Private
Public Access
1
0

fix: replace claude setup symlinks with files

This commit is contained in:
2025-12-26 15:32:05 +08:00
parent 096437cc8d
commit d8440a1ccf
11 changed files with 1931 additions and 203 deletions

View File

@@ -17,6 +17,19 @@ if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key }
$DefaultBaseUrl = "https://api2.xcodecli.com"
$CodexConfigDir = "$env:USERPROFILE\.codex"
$CodexConfigFile = "$CodexConfigDir\config.toml"
$ToolCommand = "codex"
$ToolPackage = "@openai/codex"
$ToolName = "Codex"
# ========== 工具函数 ==========
function Test-Command {
param([string]$Name)
return [bool](Get-Command $Name -ErrorAction SilentlyContinue)
}
function Refresh-Path {
$env:Path = [Environment]::GetEnvironmentVariable("Path", "User") + ";" + [Environment]::GetEnvironmentVariable("Path", "Machine")
}
# Function to write environment variable (User level)
function Set-EnvVariable {
@@ -56,6 +69,155 @@ function Write-Error {
Write-Host " $Message"
}
# ========== Node.js 环境检测 ==========
function Get-NodeVersion {
if (Test-Command "node") {
try {
$versionStr = (& node --version 2>$null) -replace 'v', ''
$major = [int]($versionStr -split '\.')[0]
return @{ Version = $versionStr; Major = $major }
}
catch { }
}
return $null
}
function Install-Fnm {
Write-Host ""
Write-Info "正在安装 fnm (Fast Node Manager)..."
try {
if (Test-Command "winget") {
Write-Info "使用 winget 安装 fnm..."
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements
}
else {
Write-Info "使用 PowerShell 脚本安装 fnm..."
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }"
}
Refresh-Path
if (Test-Command "fnm") {
Write-Success "fnm 安装成功!"
$fnmEnv = & fnm env --use-on-cd 2>$null
if ($fnmEnv) {
$fnmEnv | Out-String | Invoke-Expression
}
return $true
}
else {
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
Write-Info "请重新打开 PowerShell 后再运行此脚本"
return $false
}
}
catch {
Write-Error "fnm 安装失败: $($_.Exception.Message)"
return $false
}
}
function Install-NodeWithFnm {
Write-Info "使用 fnm 安装 Node.js 24.x..."
try {
& fnm install 24
& fnm use 24
& fnm default 24
Refresh-Path
if (Test-Command "node") {
$nodeInfo = Get-NodeVersion
Write-Success "Node.js v$($nodeInfo.Version) 安装成功!"
return $true
}
else {
Write-Warning "Node.js 可能已安装,但需要重新打开终端才能生效"
return $false
}
}
catch {
Write-Error "Node.js 安装失败: $($_.Exception.Message)"
return $false
}
}
function Ensure-NodeEnvironment {
$nodeInfo = Get-NodeVersion
if ($nodeInfo) {
Write-Info "检测到 Node.js v$($nodeInfo.Version)"
if ($nodeInfo.Major -lt 20) {
Write-Warning "Node.js 版本过低 (需要 >= 20.x)"
$upgrade = Read-Host "是否使用 fnm 安装 Node.js 24.x? (Y/n)"
if ($upgrade -eq "n" -or $upgrade -eq "N") {
Write-Error "Node.js 版本不满足要求,请手动升级后重试"
return $false
}
if (-not (Test-Command "fnm")) {
if (-not (Install-Fnm)) { return $false }
}
return Install-NodeWithFnm
}
return $true
}
Write-Warning "未检测到 Node.js"
Write-Info "将使用 fnm 安装 Node.js 24.x"
$install = Read-Host "是否继续? (Y/n)"
if ($install -eq "n" -or $install -eq "N") {
return $false
}
if (-not (Test-Command "fnm")) {
if (-not (Install-Fnm)) { return $false }
}
return Install-NodeWithFnm
}
function Install-Tool {
if (-not (Ensure-NodeEnvironment)) {
return $false
}
Write-Info "使用 npm 安装 $ToolName..."
$installCmd = "npm install -g $ToolPackage"
Write-Host " 执行: $installCmd" -ForegroundColor Gray
try {
Invoke-Expression $installCmd
$exitCode = $LASTEXITCODE
Refresh-Path
if ($exitCode -ne 0) {
Write-Error "安装命令返回错误码: $exitCode"
return $false
}
if (Test-Command $ToolCommand) {
Write-Success "$ToolName 安装成功!"
return $true
}
else {
Write-Warning "$ToolName 可能已安装,但需要重新打开终端才能生效"
$continue = Read-Host "是否继续进行配置? (Y/n)"
return ($continue -ne "n" -and $continue -ne "N")
}
}
catch {
Write-Error "安装失败: $($_.Exception.Message)"
return $false
}
}
# Function to show help
function Show-Help {
Write-Host @"
@@ -128,6 +290,15 @@ function Test-ApiKey {
}
}
# Function to extract model count from API response (supports both data[] and models[])
function Get-ModelCount {
param([object]$Response)
if ($null -eq $Response) { return 0 }
if ($Response.data -is [Array] -and $Response.data.Count -gt 0) { return $Response.data.Count }
if ($Response.models -is [Array] -and $Response.models.Count -gt 0) { return $Response.models.Count }
return 0
}
# Function to test API connection and return working base URL
function Test-ApiConnection {
param([string]$ApiKey)
@@ -151,8 +322,8 @@ function Test-ApiConnection {
$testEndpoint = "$baseUrl/v1/models"
$response = Invoke-RestMethod -Uri $testEndpoint -Method Get -Headers $headers -ErrorAction Stop
if ($response.data) {
$modelCount = $response.data.Count
$modelCount = Get-ModelCount -Response $response
if ($modelCount -gt 0) {
Write-Success "API connection successful! Found $modelCount models at $baseUrl"
return $baseUrl
} else {
@@ -201,7 +372,8 @@ env_key = "XCODECLI_OAI_KEY"
Write-Success "Codex configuration written to: $CodexConfigFile"
$authJsonPath = "$CodexConfigDir\auth.json"
'{ "OPENAI_API_KEY": null }' | Set-Content -Path $authJsonPath -Encoding UTF8
$authJson = @{ OPENAI_API_KEY = $ApiKey } | ConvertTo-Json
$authJson | Set-Content -Path $authJsonPath -Encoding UTF8
Write-Success "Codex auth file written to: $authJsonPath"
# Set environment variable XCODECLI_OAI_KEY
@@ -266,6 +438,25 @@ function Main {
exit 0
}
# 检测工具是否已安装
if (-not (Test-Command $ToolCommand)) {
Write-Host ""
Write-Warning "$ToolName 未安装"
$install = Read-Host "是否立即安装? (Y/n)"
if ($install -eq "n" -or $install -eq "N") {
Write-Info "已取消"
exit 0
}
if (-not (Install-Tool)) {
exit 1
}
}
else {
Write-Success "$ToolName 已安装"
}
# Interactive mode if no API key provided
if ([string]::IsNullOrWhiteSpace($ApiKey)) {
Write-Info "Interactive setup mode"
@@ -346,6 +537,9 @@ function Main {
Write-Host ""
Show-Settings
Write-Host ""
Write-Warning "Please restart your terminal for environment variables to take effect."
} else {
Write-Error "Failed to create Codex settings"
exit 1

View File

@@ -26,9 +26,11 @@ print_error() {
TEST_ONLY=false
SHOW_SETTINGS=false
DEFAULT_BASE_URL="https://api2.xcodecli.com"
TOOL_COMMAND="codex"
TOOL_PACKAGE="@openai/codex"
TOOL_NAME="Codex"
# ========== Shell 环境变量配置 ==========
# 检测当前 shell 配置文件
get_shell_rc() {
if [ -n "${ZSH_VERSION:-}" ] || [ "${SHELL##*/}" = "zsh" ]; then
echo "$HOME/.zshrc"
@@ -45,26 +47,21 @@ get_shell_rc() {
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="export $var_name='$var_value'"
# 删除旧的同名变量行,添加新行
local tmp_file
tmp_file=$(mktemp)
if [ -s "$rc_file" ]; then
@@ -72,14 +69,138 @@ write_env_to_shell() {
fi
echo "$export_line" >>"$tmp_file"
# 保留原文件权限
cat "$tmp_file" >"$rc_file"
rm -f "$tmp_file"
# 立即生效
export "$var_name=$var_value"
}
# ========== Node.js 环境检测 ==========
get_node_version() {
if command -v node >/dev/null 2>&1; then
node --version 2>/dev/null | sed 's/v//'
fi
}
get_node_major_version() {
local version
version=$(get_node_version)
if [ -n "$version" ]; then
echo "$version" | cut -d. -f1
fi
}
install_fnm() {
echo ""
print_info "正在安装 fnm (Fast Node Manager)..."
if curl -fsSL https://fnm.vercel.app/install | bash; then
export PATH="$HOME/.local/share/fnm:$PATH"
if [ -f "$HOME/.local/share/fnm/fnm" ]; then
eval "$(~/.local/share/fnm/fnm env)"
fi
if command -v fnm >/dev/null 2>&1; then
print_success "fnm 安装成功!"
return 0
else
print_warning "fnm 可能已安装,但需要重新打开终端才能生效"
print_info "请重新打开终端后再运行此脚本"
return 1
fi
else
print_error "fnm 安装失败"
return 1
fi
}
install_node_with_fnm() {
print_info "使用 fnm 安装 Node.js 24.x..."
if fnm install 24 && fnm use 24 && fnm default 24; then
eval "$(fnm env)"
if command -v node >/dev/null 2>&1; then
local version
version=$(get_node_version)
print_success "Node.js v$version 安装成功!"
return 0
else
print_warning "Node.js 可能已安装,但需要重新打开终端才能生效"
return 1
fi
else
print_error "Node.js 安装失败"
return 1
fi
}
ensure_node_environment() {
local version major
version=$(get_node_version)
if [ -n "$version" ]; then
major=$(get_node_major_version)
print_info "检测到 Node.js v$version"
if [ "$major" -lt 20 ]; then
print_warning "Node.js 版本过低 (需要 >= 20.x)"
read -p "是否使用 fnm 安装 Node.js 24.x? (Y/n): " -r
if [[ $REPLY =~ ^[Nn]$ ]]; then
print_error "Node.js 版本不满足要求,请手动升级后重试"
return 1
fi
if ! command -v fnm >/dev/null 2>&1; then
install_fnm || return 1
fi
install_node_with_fnm || return 1
fi
return 0
fi
print_warning "未检测到 Node.js"
print_info "将使用 fnm 安装 Node.js 24.x"
read -p "是否继续? (Y/n): " -r
if [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
fi
if ! command -v fnm >/dev/null 2>&1; then
install_fnm || return 1
fi
install_node_with_fnm || return 1
}
install_tool() {
ensure_node_environment || return 1
print_info "使用 npm 安装 $TOOL_NAME..."
echo " 执行: npm install -g $TOOL_PACKAGE"
if npm install -g "$TOOL_PACKAGE"; then
if command -v "$TOOL_COMMAND" >/dev/null 2>&1; then
print_success "$TOOL_NAME 安装成功!"
return 0
else
print_warning "$TOOL_NAME 可能已安装,但需要重新打开终端才能生效"
read -p "是否继续进行配置? (Y/n): " -r
if [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
fi
return 0
fi
else
print_error "安装失败"
return 1
fi
}
# Function to validate API key format
validate_api_key() {
local api_key="$1"
@@ -90,6 +211,25 @@ validate_api_key() {
return 0
}
# Function to extract model count from API response (supports both data[] and models[])
get_model_count() {
local response_file="$1"
local count="0"
if command -v jq >/dev/null 2>&1; then
count=$(jq -r '
if (.data | type) == "array" and (.data | length) > 0 then (.data | length)
elif (.models | type) == "array" and (.models | length) > 0 then (.models | length)
else 0 end
' "$response_file" 2>/dev/null || echo "0")
else
if grep -qE '"(data|models)"[[:space:]]*:[[:space:]]*\[' "$response_file" 2>/dev/null; then
count=$(grep -oE '"id"[[:space:]]*:' "$response_file" | wc -l | tr -d ' ')
fi
fi
[ -z "$count" ] && count="0"
echo "$count"
}
# Check for environment variable API_KEY
if [ -n "$API_KEY" ]; then
API_KEY_FROM_ENV="$API_KEY"
@@ -199,9 +339,9 @@ test_api_connection() {
2>/dev/null || echo "000")
if [ "$response" = "200" ]; then
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 ' ')
local model_count
model_count=$(get_model_count /tmp/codex_test_response)
if [ "$model_count" -gt "0" ]; then
print_success "API connection successful! Found $model_count models at $base_url" >&2
rm -f /tmp/codex_test_response
echo "$base_url"
@@ -248,10 +388,10 @@ requires_openai_auth = true
env_key = "XCODECLI_OAI_KEY"
EOF
# Create auth.json with OPENAI_API_KEY set to null
# Create auth.json with OPENAI_API_KEY
cat >"$HOME/.codex/auth.json" <<EOF
{
"OPENAI_API_KEY": null
"OPENAI_API_KEY": "$api_key"
}
EOF
@@ -311,6 +451,21 @@ main() {
exit 0
fi
# 检测工具是否已安装
if ! command -v "$TOOL_COMMAND" >/dev/null 2>&1; then
echo ""
print_warning "$TOOL_NAME 未安装"
read -p "是否立即安装? (Y/n): " -r
if [[ $REPLY =~ ^[Nn]$ ]]; then
print_info "已取消"
exit 0
fi
install_tool || exit 1
else
print_success "$TOOL_NAME 已安装"
fi
# Interactive mode if no API key provided
if [ -z "$API_KEY" ]; then
print_info "Interactive setup mode"
@@ -396,6 +551,9 @@ main() {
# Show current settings
echo ""
show_current_settings
echo ""
print_warning "Please restart your terminal or run 'source $(get_shell_rc)' for environment variables to take effect."
}
# Run main function