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

codexLaunchArgs.ts

148 lines5.1 KB

1import * as NodeOS from "node:os";
2​
3import { tokenizeCliArgs } from "@t3tools/shared/cliArgs";
4import * as Effect from "effect/Effect";
5import type * as FileSystem from "effect/FileSystem";
6import type * as Path from "effect/Path";
7import { parse as parseToml } from "smol-toml";
8​
9import { expandHomePath } from "../../pathExpansion.ts";
10​
11export const T3CODE_CODEX_LAUNCH_ARGS_ENV = "T3CODE_CODEX_LAUNCH_ARGS";
12export const T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV = "T3CODE_CODEX_APPEND_LAUNCH_ARGS";
13export const T3CODE_CODEX_CUA_LAUNCH_ARGS_ENV = "T3CODE_CODEX_CUA_LAUNCH_ARGS";
14​
15export type CodexLaunchArgs = string | ReadonlyArray<string> | undefined;
16​
17const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
18 typeof value === "object" && value !== null && !Array.isArray(value);
19​
20const parsedConfigHasCuaDriver = (config: unknown): boolean => {
21 if (!isRecord(config)) return false;
22 const mcpServers = config.mcp_servers;
23 return isRecord(mcpServers) && Object.hasOwn(mcpServers, "cua-driver");
24};
25​
26const configOverrideHasCuaDriver = (override: string): boolean => {
27 const assignmentIndex = override.indexOf("=");
28 const key = (assignmentIndex === -1 ? override : override.slice(0, assignmentIndex)).trim();
29 if (/^mcp_servers\.(?:cua-driver|"cua-driver"|'cua-driver')(?:\.|$)/.test(key)) {
30 return true;
31 }
32 try {
33 return parsedConfigHasCuaDriver(parseToml(override));
34 } catch {
35 return false;
36 }
37};
38​
39const launchArgsHaveCuaDriver = (launchArgs: CodexLaunchArgs): boolean => {
40 const args = codexLaunchArgv(launchArgs);
41 for (let index = 0; index < args.length; index++) {
42 const argument = args[index];
43 if (argument === undefined) continue;
44​
45 if (argument === "-c" || argument === "--config") {
46 const override = args[index + 1];
47 if (override !== undefined && configOverrideHasCuaDriver(override)) return true;
48 index++;
49 continue;
50 }
51​
52 if (argument.startsWith("-c=") || argument.startsWith("--config=")) {
53 const override = argument.slice(argument.indexOf("=") + 1);
54 if (configOverrideHasCuaDriver(override)) return true;
55 }
56 }
57 return false;
58};
59​
60export const hasConfiguredCuaDriver = (
61 launchArgs: CodexLaunchArgs,
62 configToml: string | undefined,
63): boolean => {
64 if (launchArgsHaveCuaDriver(launchArgs)) return true;
65 if (configToml === undefined) return false;
66 try {
67 return parsedConfigHasCuaDriver(parseToml(configToml));
68 } catch {
69 return false;
70 }
71};
72​
73export const readCodexConfigToml = (
74 environment: NodeJS.ProcessEnv,
75 fileSystem: FileSystem.FileSystem,
76 path: Path.Path,
77): Effect.Effect<string | undefined> => {
78 if (!environment[T3CODE_CODEX_CUA_LAUNCH_ARGS_ENV]?.trim()) {
79 return Effect.succeed(undefined);
80 }
81 const configuredHome = environment.CODEX_HOME?.trim();
82 const homePath = configuredHome
83 ? expandHomePath(configuredHome)
84 : path.join(NodeOS.homedir(), ".codex");
85 return fileSystem
86 .readFileString(path.join(homePath, "config.toml"))
87 .pipe(Effect.orElseSucceed(() => undefined));
88};
89​
90export const resolveCodexLaunchArgs = (
91 launchArgs?: string,
92 environment: NodeJS.ProcessEnv = process.env,
93 options: { readonly configToml?: string | undefined } = {},
94) => {
95 const configured = environment[T3CODE_CODEX_LAUNCH_ARGS_ENV]?.trim() || launchArgs?.trim() || "";
96 const appended = environment[T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV]?.trim() || "";
97 const cuaLaunchArgs = environment[T3CODE_CODEX_CUA_LAUNCH_ARGS_ENV]?.trim() || "";
98 const configuredArgv = tokenizeCliArgs(configured);
99 const appendedArgv = tokenizeCliArgs(appended);
100 const existingCuaDriver =
101 cuaLaunchArgs.length > 0 &&
102 hasConfiguredCuaDriver([...configuredArgv, ...appendedArgv], options.configToml);
103 return [
104 ...configuredArgv,
105 ...appendedArgv,
106 ...(existingCuaDriver ? [] : tokenizeCliArgs(cuaLaunchArgs)),
107 ];
108};
109​
110export const codexLaunchArgv = (launchArgs: CodexLaunchArgs): ReadonlyArray<string> =>
111 typeof launchArgs === "string" ? tokenizeCliArgs(launchArgs) : (launchArgs ?? []);
112​
113export const codexAppServerArgs = (launchArgs: CodexLaunchArgs) => [
114 "app-server",
115 ...codexLaunchArgv(launchArgs),
116];
117​
118export const codexExecLaunchArgs = (launchArgs: CodexLaunchArgs) => {
119 const args = codexLaunchArgv(launchArgs);
120 const execArgs: Array<string> = [];
121​
122 for (let index = 0; index < args.length; index++) {
123 const arg = args[index];
124 if (arg === undefined) continue;
125​
126 if (arg === "--strict-config" || arg.startsWith("--config=") || arg.startsWith("-c=")) {
127 execArgs.push(arg);
128 } else if (arg === "--config" || arg === "-c" || arg === "--enable" || arg === "--disable") {
129 const value = args[index + 1];
130 if (value !== undefined && !value.startsWith("-")) {
131 execArgs.push(arg, value);
132 index++;
133 }
134 } else if (arg.startsWith("--enable=") || arg.startsWith("--disable=")) {
135 execArgs.push(arg);
136 }
137 }
138​
139 return execArgs;
140};
141​
142export const codexSessionAppServerArgs = (
143 appServerArgs: ReadonlyArray<string> | undefined,
144 launchArgs: CodexLaunchArgs,
145) => {
146 const launchAppServerArgs = codexAppServerArgs(launchArgs);
147 return appServerArgs ? [...launchAppServerArgs, ...appServerArgs] : launchAppServerArgs;
148};