Open History to see the last commit
1import { tokenizeCliArgs } from "@t3tools/shared/cliArgs";
2
3const T3CODE_CODEX_LAUNCH_ARGS_ENV = "T3CODE_CODEX_LAUNCH_ARGS";
4
5export const resolveCodexLaunchArgs = (
6 launchArgs?: string,
7 environment: NodeJS.ProcessEnv = process.env,
8) => environment[T3CODE_CODEX_LAUNCH_ARGS_ENV]?.trim() || launchArgs?.trim() || "";
9
10const codexLaunchArgv = (launchArgs?: string): ReadonlyArray<string> => tokenizeCliArgs(launchArgs);
11
12// T3 unsubscribes only when it unloads a thread; Codex's own unload delay would stack on that.
13export const codexAppServerArgs = (launchArgs?: string) => [
14 "app-server",
15 "-c",
16 "thread_unload_delay_secs=0",
17 ...codexLaunchArgv(launchArgs),
18];
19
20interface LaunchArgFilter {
21 readonly switches: ReadonlySet<string>;
22 readonly valueFlags: ReadonlySet<string>;
23 readonly inlinePrefixes: ReadonlyArray<string>;
24}
25
26const SHARED_VALUE_FLAGS = ["--config", "-c", "--enable", "--disable"];
27const SHARED_INLINE_PREFIXES = ["--config=", "-c=", "--enable=", "--disable="];
28
29const MCP_LIST_FILTER: LaunchArgFilter = {
30 switches: new Set(),
31 valueFlags: new Set([...SHARED_VALUE_FLAGS, "--profile", "-p"]),
32 inlinePrefixes: [...SHARED_INLINE_PREFIXES, "--profile=", "-p="],
33};
34
35const EXEC_FILTER: LaunchArgFilter = {
36 switches: new Set(["--strict-config", "--oss"]),
37 valueFlags: new Set([...SHARED_VALUE_FLAGS, "--local-provider", "--profile", "-p"]),
38 inlinePrefixes: [...SHARED_INLINE_PREFIXES, "--local-provider=", "--profile=", "-p="],
39};
40
41const filterLaunchArgs = (launchArgs: string | undefined, allowed: LaunchArgFilter) => {
42 const args = codexLaunchArgv(launchArgs);
43 const kept: Array<string> = [];
44
45 for (let index = 0; index < args.length; index++) {
46 const rawArg = args[index];
47 if (rawArg === undefined) continue;
48 const arg = rawArg.replace(/^(-[cp])(?=[^=])/u, "$1=");
49
50 if (allowed.switches.has(arg)) {
51 kept.push(arg);
52 } else if (allowed.valueFlags.has(arg)) {
53 const value = args[index + 1];
54 if (value !== undefined && !value.startsWith("-")) {
55 kept.push(arg, value);
56 index++;
57 }
58 } else if (allowed.inlinePrefixes.some((prefix) => arg.startsWith(prefix))) {
59 kept.push(arg);
60 }
61 }
62
63 return kept;
64};
65
66const HOOK_FEATURES = new Set(["hooks", "codex_hooks"]);
67
68// Feature toggles beat config overrides, so a launch arg's hook enable would undo ours.
69export const codexExecLaunchArgs = (launchArgs?: string) => {
70 const args = filterLaunchArgs(launchArgs, EXEC_FILTER);
71 return args.filter((arg, index) => {
72 if (arg.startsWith("--enable=")) return !HOOK_FEATURES.has(arg.slice("--enable=".length));
73 if (arg === "--enable") return !HOOK_FEATURES.has(args[index + 1] ?? "");
74 return !(args[index - 1] === "--enable" && HOOK_FEATURES.has(arg));
75 });
76};
77
78export const codexMcpListLaunchArgs = (launchArgs?: string) =>
79 filterLaunchArgs(launchArgs, MCP_LIST_FILTER);