# 磐石 API · Codex CLI 一键配置(Windows) # 默认以「独立配置档」方式接入,不动你原有的官方登录。 param([string]$Key) $ErrorActionPreference = 'Stop' $BaseUrl = 'https://api.panshiapi.com/v1' # Codex 要求带 /v1 $EnvVar = 'PANSHI_API_KEY' $Dir = Join-Path $env:USERPROFILE '.codex' $Cfg = Join-Path $Dir 'config.toml' function Info($m){ Write-Host " $m" } function Ok($m) { Write-Host " $m" -ForegroundColor Green } function Warn($m){ Write-Host " $m" -ForegroundColor Yellow } function Err($m) { Write-Host " $m" -ForegroundColor Red } Write-Host "" Write-Host " 磐石 API · Codex 配置工具" Write-Host " 只修改本机配置文件,不会上传任何信息" -ForegroundColor DarkGray Write-Host "" if (-not $Key) { $Key = Read-Host ' 请粘贴你的 API Key(sk- 开头)' } $Key = ($Key -replace '\s','') if (-not $Key) { Err '没有输入 Key,已退出'; exit 1 } # 自动挑一个这个 Key 能用的模型,Codex 用来写代码,优先 gpt 系 Info '正在读取你可用的模型…' $model = $null try { $ms = Invoke-RestMethod -Uri "$BaseUrl/models" -Headers @{ authorization = "Bearer $Key" } -TimeoutSec 30 $ids = @($ms.data | ForEach-Object { $_.id }) $model = ($ids | Where-Object { $_ -like 'gpt*' } | Select-Object -First 1) if (-not $model) { $model = ($ids | Select-Object -First 1) } } catch {} if (-not $model) { $model = 'gpt-5.5'; Warn "没读到模型列表,先用 $model 占位,之后可在配置里改" } else { Ok "将使用模型:$model" } New-Item -ItemType Directory -Force -Path $Dir | Out-Null if (-not (Test-Path $Cfg)) { New-Item -ItemType File -Path $Cfg | Out-Null } $backup = "$Cfg.bak.$(Get-Date -Format yyyyMMdd-HHmmss)" Copy-Item $Cfg $backup Info "已备份原配置 → $backup" # 去掉旧的磐石段落,避免重复 $lines = @(Get-Content $Cfg -Encoding UTF8) $out = New-Object System.Collections.Generic.List[string] $skip = $false foreach ($l in $lines) { if ($l -match '# >>> panshi') { $skip = $true; continue } if ($l -match '# <<< panshi') { $skip = $false; continue } if (-not $skip) { $out.Add($l) } } if ($lines.Count -ne $out.Count) { Info '检测到旧的磐石配置,已替换' } $block = @" # >>> panshi 磐石API(本段由配置脚本生成,可整段删除) [model_providers.panshi] name = "磐石API" base_url = "$BaseUrl" env_key = "$EnvVar" wire_api = "chat" [profiles.panshi] model_provider = "panshi" model = "$model" # <<< panshi "@ ($out -join "`n") + $block | Set-Content $Cfg -Encoding UTF8 Ok "配置已写入 $Cfg" # Key 写进用户环境变量(永久生效,新开终端可用) [Environment]::SetEnvironmentVariable($EnvVar, $Key, 'User') $env:PANSHI_API_KEY = $Key Info "Key 已写入用户环境变量 $EnvVar" Write-Host "" Info '正在验证连接…' $body = @{ model=$model; max_tokens=8; messages=@(@{role='user'; content='hi'}) } | ConvertTo-Json -Depth 5 $status = 0; $respText = '' try { $r = Invoke-WebRequest -Uri "$BaseUrl/chat/completions" -Method Post -TimeoutSec 90 ` -Headers @{ authorization = "Bearer $Key"; 'user-agent'='codex-cli/1.0' } ` -ContentType 'application/json' -Body $body -UseBasicParsing $status = [int]$r.StatusCode } catch { if ($_.Exception.Response) { $status = [int]$_.Exception.Response.StatusCode try { $respText = (New-Object IO.StreamReader($_.Exception.Response.GetResponseStream())).ReadToEnd() } catch {} } } Write-Host "" switch ($status) { 200 { Ok '✓ 配置成功' } 401 { Err '✗ Key 无效'; Info '检查有没有复制漏字符' } 403 { if ($respText -match 'balance') { Err '✗ 余额不足'; Info '请到 https://panshiapi.com 充值' } else { Err '✗ 访问被拒绝'; Info $respText } } 404 { Warn "! 模型 $model 不可用"; Info "登录后台查看该 Key 可用的模型,改 $Cfg 里的 model" } 429 { Warn '! 上游限流,稍后重试(配置没问题)' } 0 { Err '✗ 连不上服务器'; Info '检查网络' } default { Warn "! 返回 HTTP $status"; Info $respText } } Write-Host "" Write-Host " 用法:新开一个 PowerShell 窗口,然后运行" -ForegroundColor Green Write-Host " codex --profile panshi" -ForegroundColor DarkGray Write-Host "" Write-Host " 想设为默认(以后直接敲 codex 就走磐石):" -ForegroundColor DarkGray Write-Host " 在 $Cfg 顶部加一行 model_provider = `"panshi`"" -ForegroundColor DarkGray Write-Host " 如需还原:Copy-Item '$backup' '$Cfg' -Force" -ForegroundColor DarkGray Write-Host ""