Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

codexLaunchArgs.ts

91 lines3.6 KB

1import { tokenizeCliArgs } from "@t3tools/shared/cliArgs";
2import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver";
3​
4const T3CODE_CODEX_LAUNCH_ARGS_ENV = "T3CODE_CODEX_LAUNCH_ARGS";
5​
6export const resolveCodexLaunchArgs = (
7 launchArgs?: string,
8 environment: NodeJS.ProcessEnv = process.env,
9) => {
10 const override = environment[T3CODE_CODEX_LAUNCH_ARGS_ENV];
11 return override?.trim() ? override : launchArgs?.trim() ? launchArgs : "";
12};
13​
14const codexLaunchArgv = (launchArgs?: string): ReadonlyArray<string> => tokenizeCliArgs(launchArgs);
15​
16export const codexUsesStrictConfig = (launchArgs?: string) =>
17 codexLaunchArgv(launchArgs).includes("--strict-config");
18​
19export const codexAppServerArgs = (launchArgs?: string, strictConfigVersion?: string | null) => {
20 const args = codexLaunchArgv(launchArgs);
21 // Strict config rejects this key before Codex 0.154. Leave unknown versions untouched.
22 const setUnloadDelay =
23 !args.includes("--strict-config") ||
24 (strictConfigVersion != null &&
25 parseSemver(strictConfigVersion) !== null &&
26 compareSemverVersions(strictConfigVersion, "0.154.0") >= 0);
27 return ["app-server", ...(setUnloadDelay ? ["-c", "thread_unload_delay_secs=0"] : []), ...args];
28};
29​
30interface LaunchArgFilter {
31 readonly switches: ReadonlySet<string>;
32 readonly valueFlags: ReadonlySet<string>;
33 readonly inlinePrefixes: ReadonlyArray<string>;
35​
36const SHARED_VALUE_FLAGS = ["--config", "-c", "--enable", "--disable"];
37const SHARED_INLINE_PREFIXES = ["--config=", "-c=", "--enable=", "--disable="];
38​
39const MCP_LIST_FILTER: LaunchArgFilter = {
40 switches: new Set(),
41 valueFlags: new Set([...SHARED_VALUE_FLAGS, "--profile", "-p"]),
42 inlinePrefixes: [...SHARED_INLINE_PREFIXES, "--profile=", "-p="],
43};
44​
45const EXEC_FILTER: LaunchArgFilter = {
46 switches: new Set(["--strict-config", "--oss"]),
47 valueFlags: new Set([...SHARED_VALUE_FLAGS, "--local-provider", "--profile", "-p"]),
48 inlinePrefixes: [...SHARED_INLINE_PREFIXES, "--local-provider=", "--profile=", "-p="],
49};
50​
51const filterLaunchArgs = (launchArgs: string | undefined, allowed: LaunchArgFilter) => {
52 const args = codexLaunchArgv(launchArgs);
53 const kept: Array<string> = [];
54​
55 for (let index = 0; index < args.length; index++) {
56 const rawArg = args[index];
57 if (rawArg === undefined) continue;
58 const arg = rawArg.replace(/^(-[cp])(?=[^=])/u, "$1=");
59​
60 if (allowed.switches.has(arg)) {
61 kept.push(arg);
62 } else if (allowed.valueFlags.has(arg)) {
63 const value = args[index + 1];
64 if (value !== undefined && !value.startsWith("-")) {
65 kept.push(arg, value);
66 index++;
67 }
68 } else if (allowed.inlinePrefixes.some((prefix) => arg.startsWith(prefix))) {
69 kept.push(arg);
70 }
71 }
72​
73 return kept;
74};
75​
76const MCP_LIST_DISABLED_FEATURES = new Set(["apps", "connectors", "plugins"]);
77const EXEC_DISABLED_FEATURES = new Set([...MCP_LIST_DISABLED_FEATURES, "hooks", "codex_hooks"]);
78​
79// Codex can apply explicit --enable flags after trailing -c overrides.
80const omitFeatureEnables = (args: ReadonlyArray<string>, features: ReadonlySet<string>) =>
81 args.filter((arg, index) => {
82 if (arg.startsWith("--enable=")) return !features.has(arg.slice("--enable=".length));
83 if (arg === "--enable") return !features.has(args[index + 1] ?? "");
84 return !(args[index - 1] === "--enable" && features.has(arg));
85 });
86​
87export const codexExecLaunchArgs = (launchArgs?: string) =>
88 omitFeatureEnables(filterLaunchArgs(launchArgs, EXEC_FILTER), EXEC_DISABLED_FEATURES);
89​
90export const codexMcpListLaunchArgs = (launchArgs?: string) =>
91 omitFeatureEnables(filterLaunchArgs(launchArgs, MCP_LIST_FILTER), MCP_LIST_DISABLED_FEATURES);