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) => {
9 const override = environment[T3CODE_CODEX_LAUNCH_ARGS_ENV];
10 return override?.trim() ? override : launchArgs?.trim() || "";
11};
12
13const codexLaunchArgv = (launchArgs?: string): ReadonlyArray<string> => tokenizeCliArgs(launchArgs);
14
15// T3 unsubscribes only when it unloads a thread; Codex's own unload delay would stack on that.
16export const codexAppServerArgs = (launchArgs?: string) => [
17 "app-server",
18 "-c",
19 "thread_unload_delay_secs=0",
20 ...codexLaunchArgv(launchArgs),
21];
22
23interface LaunchArgFilter {
24 readonly switches: ReadonlySet<string>;
25 readonly valueFlags: ReadonlySet<string>;
26 readonly inlinePrefixes: ReadonlyArray<string>;
27}
28
29const SHARED_VALUE_FLAGS = ["--config", "-c", "--enable", "--disable"];
30const SHARED_INLINE_PREFIXES = ["--config=", "-c=", "--enable=", "--disable="];
31
32const MCP_LIST_FILTER: LaunchArgFilter = {
33 switches: new Set(),
34 valueFlags: new Set([...SHARED_VALUE_FLAGS, "--profile", "-p"]),
35 inlinePrefixes: [...SHARED_INLINE_PREFIXES, "--profile=", "-p="],
36};
37
38const EXEC_FILTER: LaunchArgFilter = {
39 switches: new Set(["--strict-config", "--oss"]),
40 valueFlags: new Set([...SHARED_VALUE_FLAGS, "--local-provider", "--profile", "-p"]),
41 inlinePrefixes: [...SHARED_INLINE_PREFIXES, "--local-provider=", "--profile=", "-p="],
42};
43
44const filterLaunchArgs = (launchArgs: string | undefined, allowed: LaunchArgFilter) => {
45 const args = codexLaunchArgv(launchArgs);
46 const kept: Array<string> = [];
47
48 for (let index = 0; index < args.length; index++) {
49 const rawArg = args[index];
50 if (rawArg === undefined) continue;
51 const arg = rawArg.replace(/^(-[cp])(?=[^=])/u, "$1=");
52
53 if (allowed.switches.has(arg)) {
54 kept.push(arg);
55 } else if (allowed.valueFlags.has(arg)) {
56 const value = args[index + 1];
57 if (value !== undefined && !value.startsWith("-")) {
58 kept.push(arg, value);
59 index++;
60 }
61 } else if (allowed.inlinePrefixes.some((prefix) => arg.startsWith(prefix))) {
62 kept.push(arg);
63 }
64 }
65
66 return kept;
67};
68
69const MCP_LIST_DISABLED_FEATURES = new Set(["apps", "connectors", "plugins"]);
70const EXEC_DISABLED_FEATURES = new Set([...MCP_LIST_DISABLED_FEATURES, "hooks", "codex_hooks"]);
71
72// Codex can apply explicit --enable flags after trailing -c overrides.
73const omitFeatureEnables = (args: ReadonlyArray<string>, features: ReadonlySet<string>) =>
74 args.filter((arg, index) => {
75 if (arg.startsWith("--enable=")) return !features.has(arg.slice("--enable=".length));
76 if (arg === "--enable") return !features.has(args[index + 1] ?? "");
77 return !(args[index - 1] === "--enable" && features.has(arg));
78 });
79
80export const codexExecLaunchArgs = (launchArgs?: string) =>
81 omitFeatureEnables(filterLaunchArgs(launchArgs, EXEC_FILTER), EXEC_DISABLED_FEATURES);
82
83export const codexMcpListLaunchArgs = (launchArgs?: string) =>
84 omitFeatureEnables(filterLaunchArgs(launchArgs, MCP_LIST_FILTER), MCP_LIST_DISABLED_FEATURES);