-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_codex_external_api.ps1
More file actions
154 lines (126 loc) · 4.51 KB
/
Copy pathprepare_codex_external_api.ps1
File metadata and controls
154 lines (126 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
param(
[Parameter(Mandatory = $true)]
[string]$BaseUrl,
[string]$ApiKey,
[switch]$ApiKeyStdin,
[string]$Model = "gpt-5.5",
[string]$Provider = "custom",
[ValidateSet("responses", "chat")]
[string]$WireApi = "responses",
[string]$CodexHome = "$env:USERPROFILE\.codex",
[string]$SwitchExe,
[switch]$ClearWebProfile
)
$ErrorActionPreference = "Stop"
function Write-Step {
param([string]$Message)
Write-Host ""
Write-Host "==> $Message"
}
function Stop-CodexProcesses {
$names = @("ChatGPT", "Codex", "codex", "codex-plus-plus")
$processes = Get-Process -ErrorAction SilentlyContinue | Where-Object { $names -contains $_.ProcessName }
if (-not $processes) {
Write-Host "No running Codex processes found."
return
}
foreach ($process in $processes) {
Write-Host "Stopping $($process.ProcessName) pid=$($process.Id)"
Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue
}
Start-Sleep -Seconds 2
}
function Resolve-SwitchExe {
param([string]$ExplicitPath)
if ($ExplicitPath) {
return (Resolve-Path -LiteralPath $ExplicitPath -ErrorAction Stop).Path
}
$scriptDir = Split-Path -Parent $MyInvocation.ScriptName
$candidates = @(
(Join-Path $scriptDir "codex-api-switch.exe"),
(Join-Path $scriptDir "dist-x64\codex-api-switch.exe"),
(Join-Path $scriptDir "dist\codex-api-switch.exe")
)
foreach ($candidate in $candidates) {
if (Test-Path -LiteralPath $candidate) {
return $candidate
}
}
throw "Cannot find codex-api-switch.exe. Pass -SwitchExe or place it next to this script."
}
function Resolve-CodexHome {
param([string]$InputPath)
$expanded = [Environment]::ExpandEnvironmentVariables($InputPath)
if ([System.IO.Path]::IsPathRooted($expanded)) {
return [System.IO.Path]::GetFullPath($expanded)
}
return [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $expanded))
}
function Backup-And-ClearWebProfile {
$webDir = Join-Path $env:APPDATA "Codex\web"
if (-not (Test-Path -LiteralPath $webDir)) {
Write-Host "No Codex web profile found: $webDir"
return
}
$backupRoot = Join-Path $CodexHome "backups"
New-Item -ItemType Directory -Force -Path $backupRoot | Out-Null
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$backupDir = Join-Path $backupRoot "codex-web-profile-$stamp"
try {
Move-Item -LiteralPath $webDir -Destination $backupDir -Force -ErrorAction Stop
Write-Host "Moved Codex web profile to: $backupDir"
return
} catch {
Write-Warning "Could not move the WebView profile directory. Falling back to copy-and-clear."
New-Item -ItemType Directory -Force -Path $backupDir | Out-Null
Copy-Item -LiteralPath (Join-Path $webDir "*") -Destination $backupDir -Recurse -Force -ErrorAction SilentlyContinue
Get-ChildItem -LiteralPath $webDir -Force -ErrorAction SilentlyContinue |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Copied Codex web profile backup to: $backupDir"
Write-Host "Cleared removable WebView profile contents."
}
}
function Get-ApiKeyValue {
if ($ApiKeyStdin) {
return ([Console]::In.ReadToEnd()).Trim()
}
if ($ApiKey) {
return $ApiKey.Trim()
}
if ($env:OPENAI_API_KEY) {
return $env:OPENAI_API_KEY.Trim()
}
throw "Missing API key. Pass -ApiKey, -ApiKeyStdin, or set OPENAI_API_KEY."
}
Write-Step "Closing Codex"
Stop-CodexProcesses
Write-Step "Configuring external API"
$resolvedSwitchExe = Resolve-SwitchExe $SwitchExe
$resolvedCodexHome = Resolve-CodexHome $CodexHome
$key = Get-ApiKeyValue
$switchArgs = @(
"--codex-home", $resolvedCodexHome,
"--base-url", $BaseUrl,
"--api-key-stdin",
"--provider", $Provider,
"--wire-api", $WireApi
)
if ($Model) {
$switchArgs += @("--model", $Model)
}
$key | & $resolvedSwitchExe @switchArgs
if ($LASTEXITCODE -ne 0) {
throw "codex-api-switch.exe failed with exit code $LASTEXITCODE"
}
Write-Step "Removing official ChatGPT web profile"
if ($ClearWebProfile) {
Backup-And-ClearWebProfile
} else {
Write-Host "Skipped. Pass -ClearWebProfile to move the Codex WebView profile out of APPDATA."
}
Write-Step "Result"
Write-Host "Codex home: $resolvedCodexHome"
Write-Host "Provider: $Provider"
Write-Host "Base URL: $($BaseUrl.TrimEnd('/'))"
Write-Host "Auth mode: OPENAI_API_KEY only"
Write-Host "Restart Codex after this script completes."