-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex.js
More file actions
533 lines (485 loc) · 15.4 KB
/
codex.js
File metadata and controls
533 lines (485 loc) · 15.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env node
import { spawn, spawnSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import { basename, delimiter, dirname, join, resolve as resolvePath } from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { normalizeAuthAlias, shouldHandleMultiAuthAuth } from "./codex-routing.js";
function hydrateCliVersionEnv() {
try {
const require = createRequire(import.meta.url);
const pkg = require("../package.json");
const version = typeof pkg?.version === "string" ? pkg.version.trim() : "";
if (version.length > 0) {
process.env.CODEX_MULTI_AUTH_CLI_VERSION = version;
}
} catch {
// Best effort only.
}
}
async function loadRunCodexMultiAuthCli() {
try {
const mod = await import("../dist/lib/codex-manager.js");
if (typeof mod.runCodexMultiAuthCli !== "function") {
console.error(
"dist/lib/codex-manager.js is missing required export: runCodexMultiAuthCli",
);
return null;
}
return mod.runCodexMultiAuthCli;
} catch (error) {
if (error && typeof error === "object" && "code" in error && error.code === "ERR_MODULE_NOT_FOUND") {
console.error(
[
"codex-multi-auth auth commands require built runtime files, but dist output is missing.",
"Run: npm run build",
].join("\n"),
);
return null;
}
throw error;
}
}
async function autoSyncManagerActiveSelectionIfEnabled() {
const enabled = (process.env.CODEX_MULTI_AUTH_AUTO_SYNC_ON_STARTUP ?? "1").trim() !== "0";
if (!enabled) return;
try {
const mod = await import("../dist/lib/codex-manager.js");
if (typeof mod.autoSyncActiveAccountToCodex !== "function") {
return;
}
await mod.autoSyncActiveAccountToCodex();
} catch (error) {
if (error && typeof error === "object" && "code" in error && error.code === "ERR_MODULE_NOT_FOUND") {
// Non-auth command path should keep forwarding even if dist is missing.
return;
}
// Best effort only: never block official Codex startup on sync failure.
}
}
function resolveRealCodexBin() {
const override = (process.env.CODEX_MULTI_AUTH_REAL_CODEX_BIN ?? "").trim();
if (override.length > 0) {
if (existsSync(override)) return override;
console.error(
`CODEX_MULTI_AUTH_REAL_CODEX_BIN is set but missing: ${override}`,
);
return null;
}
try {
const require = createRequire(import.meta.url);
const resolved = require.resolve("@openai/codex/bin/codex.js");
if (existsSync(resolved)) return resolved;
} catch {
// Fall through to sibling lookup.
}
const searchRoots = [];
const scriptDir = dirname(fileURLToPath(import.meta.url));
searchRoots.push(join(scriptDir, "..", ".."));
const invokedScript = process.argv[1];
if (typeof invokedScript === "string" && invokedScript.length > 0) {
searchRoots.push(join(dirname(invokedScript), "..", ".."));
}
const npmPrefix = (process.env.npm_config_prefix ?? process.env.PREFIX ?? "").trim();
if (npmPrefix.length > 0) {
searchRoots.push(join(npmPrefix, "node_modules"));
searchRoots.push(join(npmPrefix, "lib", "node_modules"));
}
for (const root of searchRoots) {
const candidate = join(root, "@openai", "codex", "bin", "codex.js");
if (existsSync(candidate)) return candidate;
}
const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
try {
const rootResult = spawnSync(npmCmd, ["root", "-g"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
if (rootResult.status === 0) {
const globalRoot = rootResult.stdout.trim();
if (globalRoot.length > 0) {
const globalBin = join(globalRoot, "@openai", "codex", "bin", "codex.js");
if (existsSync(globalBin)) return globalBin;
}
}
} catch {
// Ignore and fall through to null.
}
return null;
}
function forwardToRealCodex(codexBin, args) {
return new Promise((resolve) => {
const child = spawn(process.execPath, [codexBin, ...args], {
stdio: "inherit",
env: process.env,
});
child.once("error", (error) => {
console.error(`Failed to launch real Codex CLI: ${String(error)}`);
resolve(1);
});
child.once("exit", (code, signal) => {
if (signal) {
const signalNumber = signal === "SIGINT" ? 130 : 1;
resolve(signalNumber);
return;
}
resolve(typeof code === "number" ? code : 1);
});
});
}
function hasCliAuthCredentialsStoreOverride(args) {
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === "-c" || arg === "--config") {
const next = args[i + 1];
if (!next || !next.includes("=")) continue;
const [key] = next.split("=", 1);
if ((key ?? "").trim() === "cli_auth_credentials_store") {
return true;
}
continue;
}
if (typeof arg === "string" && arg.startsWith("--config=")) {
const assignment = arg.slice("--config=".length);
const [key] = assignment.split("=", 1);
if ((key ?? "").trim() === "cli_auth_credentials_store") {
return true;
}
}
}
return false;
}
function buildForwardArgs(rawArgs) {
const forceFileAuthStore = (process.env.CODEX_MULTI_AUTH_FORCE_FILE_AUTH_STORE ?? "1").trim() !== "0";
if (!forceFileAuthStore) return [...rawArgs];
if (hasCliAuthCredentialsStoreOverride(rawArgs)) return [...rawArgs];
return [
...rawArgs,
"-c",
'cli_auth_credentials_store="file"',
];
}
function normalizeExitCode(value) {
if (typeof value === "number" && Number.isInteger(value)) {
return value;
}
const parsed = Number(value);
if (Number.isInteger(parsed)) {
return parsed;
}
return 1;
}
const WINDOWS_SHIM_MARKER = "codex-multi-auth windows shim guardian v1";
const POWERSHELL_PROFILE_MARKER_START = "# >>> codex-multi-auth shell guard >>>";
const POWERSHELL_PROFILE_MARKER_END = "# <<< codex-multi-auth shell guard <<<";
function shouldInstallWindowsBatchShimGuard() {
if (process.platform !== "win32") return false;
const override = (process.env.CODEX_MULTI_AUTH_WINDOWS_BATCH_SHIM_GUARD ?? "1").trim();
return override !== "0";
}
function splitPathEntries(pathValue) {
if (typeof pathValue !== "string" || pathValue.trim().length === 0) {
return [];
}
return pathValue
.split(delimiter)
.map((entry) => entry.trim())
.filter((entry) => entry.length > 0);
}
function resolveWindowsShimDirectoryFromInvocation() {
const invokedScript = (process.argv[1] ?? "").trim();
if (invokedScript.length === 0) return null;
const resolvedScript = resolvePath(invokedScript);
const scriptDir = dirname(resolvedScript);
const packageRoot = dirname(scriptDir);
const nodeModulesDir = dirname(packageRoot);
if (basename(nodeModulesDir).toLowerCase() !== "node_modules") {
return null;
}
const shimDir = dirname(nodeModulesDir);
if (existsSync(join(shimDir, "codex-multi-auth.cmd"))) {
return shimDir;
}
return null;
}
function resolveWindowsShimDirectoryFromPath() {
const fromInvocation = resolveWindowsShimDirectoryFromInvocation();
if (fromInvocation) {
return fromInvocation;
}
const pathEntries = splitPathEntries(process.env.PATH ?? process.env.Path ?? "");
for (const entry of pathEntries) {
if (existsSync(join(entry, "codex-multi-auth.cmd"))) {
return entry;
}
}
return null;
}
function buildWindowsBatchShimContent() {
return [
"@ECHO off",
`:: ${WINDOWS_SHIM_MARKER}`,
"GOTO start",
":find_dp0",
"SET dp0=%~dp0",
"EXIT /b",
":start",
"SETLOCAL",
"CALL :find_dp0",
"",
'IF EXIST "%dp0%\\node.exe" (',
' SET "_prog=%dp0%\\node.exe"',
") ELSE (",
' SET "_prog=node"',
' SET PATHEXT=%PATHEXT:;.JS;=%',
")",
"",
'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\\node_modules\\codex-multi-auth\\scripts\\codex.js" %*',
].join("\r\n");
}
function buildWindowsCmdShimContent() {
return [
"@ECHO off",
`:: ${WINDOWS_SHIM_MARKER}`,
"GOTO start",
":find_dp0",
"SET dp0=%~dp0",
"EXIT /b",
":start",
"SETLOCAL",
"CALL :find_dp0",
"",
'IF EXIST "%dp0%\\node.exe" (',
' SET "_prog=%dp0%\\node.exe"',
") ELSE (",
' SET "_prog=node"',
' SET PATHEXT=%PATHEXT:;.JS;=%',
")",
"",
'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\\node_modules\\codex-multi-auth\\scripts\\codex.js" %*',
].join("\r\n");
}
function buildWindowsPowerShellShimContent() {
return [
`# ${WINDOWS_SHIM_MARKER}`,
"$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent",
"",
'$exe=""',
'if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {',
' $exe=".exe"',
"}",
"$ret=0",
'if (Test-Path "$basedir/node$exe") {',
" if ($MyInvocation.ExpectingInput) {",
' $input | & "$basedir/node$exe" "$basedir/node_modules/codex-multi-auth/scripts/codex.js" $args',
" } else {",
' & "$basedir/node$exe" "$basedir/node_modules/codex-multi-auth/scripts/codex.js" $args',
" }",
" $ret=$LASTEXITCODE",
"} else {",
" if ($MyInvocation.ExpectingInput) {",
' $input | & "node$exe" "$basedir/node_modules/codex-multi-auth/scripts/codex.js" $args',
" } else {",
' & "node$exe" "$basedir/node_modules/codex-multi-auth/scripts/codex.js" $args',
" }",
" $ret=$LASTEXITCODE",
"}",
"if ($null -eq $ret) {",
" exit 0",
"}",
"exit $ret",
].join("\r\n");
}
function ensureWindowsShellShim(filePath, desiredContent, options = {}) {
const {
overwriteCustomShim = false,
shimMarker = WINDOWS_SHIM_MARKER,
} = options;
let currentContent = "";
if (existsSync(filePath)) {
try {
currentContent = readFileSync(filePath, "utf8");
} catch {
return false;
}
if (currentContent === desiredContent || currentContent.includes(shimMarker)) {
if (currentContent !== desiredContent) {
try {
writeFileSync(filePath, desiredContent, { encoding: "utf8", mode: 0o755 });
return true;
} catch {
return false;
}
}
return false;
}
const looksLikeStockOpenAiShim =
currentContent.includes("node_modules\\@openai\\codex\\bin\\codex.js") ||
currentContent.includes("node_modules/@openai/codex/bin/codex.js");
if (looksLikeStockOpenAiShim) {
try {
writeFileSync(filePath, desiredContent, { encoding: "utf8", mode: 0o755 });
return true;
} catch {
return false;
}
}
if (!overwriteCustomShim) {
return false;
}
}
try {
writeFileSync(filePath, desiredContent, { encoding: "utf8", mode: 0o755 });
return true;
} catch {
return false;
}
}
function shouldInstallPowerShellProfileGuard() {
if (process.platform !== "win32") return false;
const override = (process.env.CODEX_MULTI_AUTH_PWSH_PROFILE_GUARD ?? "1").trim();
return override !== "0";
}
function resolveWindowsUserHomeDir() {
const userProfile = (process.env.USERPROFILE ?? "").trim();
if (userProfile.length > 0) return userProfile;
const homeDrive = (process.env.HOMEDRIVE ?? "").trim();
const homePath = (process.env.HOMEPATH ?? "").trim();
if (homeDrive.length > 0 && homePath.length > 0) {
return `${homeDrive}${homePath}`;
}
const home = (process.env.HOME ?? "").trim();
return home;
}
function buildPowerShellProfileGuardBlock(shimDirectory) {
const codexBatchPath = join(shimDirectory, "codex.bat").replace(/\\/g, "\\\\");
return [
POWERSHELL_PROFILE_MARKER_START,
`$CodexMultiAuthShim = "${codexBatchPath}"`,
"if (Test-Path $CodexMultiAuthShim) {",
" function global:codex {",
" & $CodexMultiAuthShim @args",
" }",
"}",
POWERSHELL_PROFILE_MARKER_END,
].join("\r\n");
}
function upsertPowerShellProfileGuard(profilePath, guardBlock) {
let content = "";
if (existsSync(profilePath)) {
try {
content = readFileSync(profilePath, "utf8");
} catch {
return false;
}
}
const normalizedCurrentContent = content.replace(/\r?\n$/, "");
const startIndex = content.indexOf(POWERSHELL_PROFILE_MARKER_START);
const endIndex = content.indexOf(POWERSHELL_PROFILE_MARKER_END);
let nextContent;
if (startIndex >= 0 && endIndex >= startIndex) {
const endWithMarker = endIndex + POWERSHELL_PROFILE_MARKER_END.length;
const prefix = content.slice(0, startIndex).replace(/\s*$/, "");
const suffix = content.slice(endWithMarker).replace(/^\s*/, "");
nextContent = `${prefix}\r\n\r\n${guardBlock}\r\n\r\n${suffix}`.trimEnd();
} else if (normalizedCurrentContent.trim().length === 0) {
nextContent = guardBlock;
} else {
nextContent = `${normalizedCurrentContent.replace(/\s*$/, "")}\r\n\r\n${guardBlock}`;
}
if (nextContent === normalizedCurrentContent) {
return false;
}
try {
mkdirSync(dirname(profilePath), { recursive: true });
writeFileSync(profilePath, `${nextContent}\r\n`, { encoding: "utf8", mode: 0o644 });
return true;
} catch {
return false;
}
}
function ensurePowerShellProfileGuard(shimDirectory) {
if (!shouldInstallPowerShellProfileGuard()) return false;
const homeDir = resolveWindowsUserHomeDir();
if (!homeDir) return false;
const guardBlock = buildPowerShellProfileGuardBlock(shimDirectory);
const profilePaths = [
join(homeDir, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1"),
join(homeDir, "Documents", "WindowsPowerShell", "Microsoft.PowerShell_profile.ps1"),
];
let changed = false;
for (const profilePath of profilePaths) {
changed = upsertPowerShellProfileGuard(profilePath, guardBlock) || changed;
}
return changed;
}
function ensureWindowsShellShimGuards() {
if (!shouldInstallWindowsBatchShimGuard()) return;
const shimDirectory = resolveWindowsShimDirectoryFromPath();
if (!shimDirectory) return;
const codexMultiAuthShimPath = join(shimDirectory, "codex-multi-auth.cmd");
if (!existsSync(codexMultiAuthShimPath)) return;
const overwriteCustomShim =
(process.env.CODEX_MULTI_AUTH_OVERWRITE_CUSTOM_BATCH_SHIM ?? "0").trim() === "1";
const installedBatch = ensureWindowsShellShim(
join(shimDirectory, "codex.bat"),
buildWindowsBatchShimContent(),
{ overwriteCustomShim },
);
const installedCmd = ensureWindowsShellShim(
join(shimDirectory, "codex.cmd"),
buildWindowsCmdShimContent(),
{ overwriteCustomShim },
);
const installedPs1 = ensureWindowsShellShim(
join(shimDirectory, "codex.ps1"),
buildWindowsPowerShellShimContent(),
{ overwriteCustomShim },
);
const installedAny = installedBatch || installedCmd || installedPs1;
const installedProfileGuard = ensurePowerShellProfileGuard(shimDirectory);
if (installedAny || installedProfileGuard) {
console.error(
"codex-multi-auth: installed Windows shell guards to keep multi-auth routing after codex npm updates.",
);
}
}
async function main() {
hydrateCliVersionEnv();
ensureWindowsShellShimGuards();
const rawArgs = process.argv.slice(2);
const normalizedArgs = normalizeAuthAlias(rawArgs);
const bypass = (process.env.CODEX_MULTI_AUTH_BYPASS ?? "").trim() === "1";
if (!bypass && shouldHandleMultiAuthAuth(normalizedArgs)) {
try {
const runCodexMultiAuthCli = await loadRunCodexMultiAuthCli();
if (!runCodexMultiAuthCli) {
return 1;
}
const exitCode = await runCodexMultiAuthCli(normalizedArgs);
return normalizeExitCode(exitCode);
} catch (error) {
console.error(
`codex-multi-auth runner failed: ${error instanceof Error ? error.message : String(error)}`,
);
return 1;
}
}
const realCodexBin = resolveRealCodexBin();
if (!realCodexBin) {
console.error(
[
"Could not locate the official Codex CLI binary (@openai/codex).",
"Install it globally: npm install -g @openai/codex",
"Or set CODEX_MULTI_AUTH_REAL_CODEX_BIN to a full bin/codex.js path.",
].join("\n"),
);
return 1;
}
await autoSyncManagerActiveSelectionIfEnabled();
const forwardArgs = buildForwardArgs(rawArgs);
return forwardToRealCodex(realCodexBin, forwardArgs);
}
const exitCode = await main();
process.exitCode = normalizeExitCode(exitCode);