<# .SYNOPSIS Codex + CC Switch 一键接入配置脚本(Liangfeng Relay / ai.liangfeng.net.cn) .DESCRIPTION 生成 CC Switch 一键导入深链(ccswitch://);可选 -WriteConfig 直接写入 ~/.codex/config.toml 与 ~/.codex/auth.json(写入前自动备份,检测到已有 model_provider 配置时不覆盖,避免破坏 CC Switch 的管理结果)。 .EXAMPLE .\configure-codex-ccswitch.ps1 -ApiKey "sk-你的密钥" .EXAMPLE .\configure-codex-ccswitch.ps1 -ApiKey "sk-你的密钥" -WriteConfig #> param( [Parameter(Mandatory = $true)][string]$ApiKey, [string]$BaseUrl = "https://ai.liangfeng.net.cn/v1", [string]$Model = "gpt-5.6-terra", [string]$ProviderName = "Liangfeng", [switch]$WriteConfig ) $ErrorActionPreference = "Stop" # 1. 生成 CC Switch 深链 $deeplink = "ccswitch://v1/import?resource=provider&app=codex&name=$([uri]::EscapeDataString($ProviderName))&endpoint=$([uri]::EscapeDataString($BaseUrl))&apiKey=$([uri]::EscapeDataString($ApiKey))&model=$Model" Write-Host "=== CC Switch 一键导入链接 ===" -ForegroundColor Cyan Write-Host $deeplink Write-Host "" Write-Host "提示:将上面的链接复制到浏览器地址栏访问(或直接点击),CC Switch 会弹出导入确认。" Write-Host " 导入后可在 CC Switch 供应商编辑页勾选「启用远程压缩」(写入 provider name = OpenAI)。" Write-Host "" # 2. 可选:直接写入 Codex 配置文件 if (-not $WriteConfig) { Write-Host "如需直接写入 Codex 配置文件(不经 CC Switch 界面),请加 -WriteConfig 参数重新运行。" exit 0 } $codexDir = Join-Path $env:USERPROFILE ".codex" New-Item -ItemType Directory -Force -Path $codexDir | Out-Null $configPath = Join-Path $codexDir "config.toml" $authPath = Join-Path $codexDir "auth.json" $providerSection = @" model_provider = "liangfeng" model = "$Model" model_reasoning_effort = "high" disable_response_storage = true [model_providers.liangfeng] name = "OpenAI" base_url = "$BaseUrl" wire_api = "responses" requires_openai_auth = true "@ if (Test-Path $configPath) { Copy-Item $configPath "$configPath.bak" -Force $existing = Get-Content $configPath -Raw if ($existing -match '(?m)^\s*model_provider\s*=') { Write-Host "[!] $configPath 中已存在 model_provider 配置,未自动写入(避免覆盖 CC Switch 的管理结果)。" -ForegroundColor Yellow Write-Host " 已备份到 $configPath.bak。建议改用上方 CC Switch 深链导入,或在 CC Switch 内维护。" } else { Add-Content -Path $configPath -Value ("`n" + $providerSection) -Encoding UTF8 Write-Host "[OK] 已追加 provider 段到 $configPath(原文件已备份)" -ForegroundColor Green } } else { [System.IO.File]::WriteAllText($configPath, $providerSection, [System.Text.UTF8Encoding]::new($false)) Write-Host "[OK] 已写入 $configPath" -ForegroundColor Green } # auth.json 合并(保留其他键) $auth = @{} if (Test-Path $authPath) { Copy-Item $authPath "$authPath.bak" -Force try { $auth = Get-Content $authPath -Raw -Encoding UTF8 | ConvertFrom-Json -AsHashtable } catch { $auth = @{} } } $auth["OPENAI_API_KEY"] = $ApiKey [System.IO.File]::WriteAllText($authPath, ($auth | ConvertTo-Json -Depth 6), [System.Text.UTF8Encoding]::new($false)) Write-Host "[OK] 已更新 $authPath 中的 OPENAI_API_KEY" -ForegroundColor Green Write-Host "" Write-Host "注意:provider name 已写为 ""OpenAI"" 以启用 Codex 远程压缩;不需要时删除该 name 行即可。" -ForegroundColor DarkGray