The best way to control Claude Code, Codex and more from any computer or phone

main
ts 439 lines 15.6 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as FileSystem from "effect/FileSystem";
3 import * as Option from "effect/Option";
4 import * as Path from "effect/Path";
5 import * as Schema from "effect/Schema";
6 import * as Scope from "effect/Scope";
7 import * as Stream from "effect/Stream";
8 import { ChildProcess, ChildProcessSpawner } from "effect/process";
9
10 import {
11 type CodexSettings,
12 DEFAULT_TEXT_GENERATION_REASONING_EFFORT,
13 type ServerProviderModel,
14 TextGenerationError,
15 } from "@t3tools/contracts";
16 import { resolveSpawnCommand } from "@t3tools/shared/shell";
17
18 import { resolveAttachmentPath } from "../attachmentStore.ts";
19 import * as ServerConfig from "../config.ts";
20 import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
21 import {
22 codexExecLaunchArgs,
23 codexMcpListLaunchArgs,
24 resolveCodexLaunchArgs,
25 } from "../provider/codexLaunchArgs.ts";
26 import { codexAppServerEnvironment } from "../provider/CodexProvider.ts";
27 import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations";
28 import {
29 normalizeCliError,
30 toJsonSchemaObject,
31 } from "@t3tools/provider-core/server/textGenerationUtils";
32 import {
33 codexModelFamily,
34 getModelSelectionStringOptionValue,
35 readCustomModelEntries,
36 } from "@t3tools/shared/model";
37 import { getCodexServiceTierOptionValue } from "../codexModelOptions.ts";
38 import * as HostProcess from "@t3tools/shared/HostProcess";
39
40 const CODEX_TIMEOUT_MS = 180_000;
41 const CODEX_MCP_LIST_TIMEOUT_MS = 10_000;
42 // Plugins/apps add MCP servers; use trailing config overrides because --disable loses to user -c flags.
43 // Codex also reads the legacy connectors key for apps.
44 const CODEX_NO_PLUGIN_ARGS = [
45 "--config",
46 "features.connectors=false",
47 "--config",
48 "features.plugins=false",
49 "--config",
50 "features.apps=false",
51 ] as const;
52 const CodexMcpServers = Schema.fromJsonString(
53 Schema.Array(Schema.Struct({ name: Schema.String, enabled: Schema.Boolean })),
54 );
55 const decodeCodexMcpServers = Schema.decodeEffect(CodexMcpServers);
56
57 // mcp_servers={} merges instead of replacing; disable named entries and quote dotted names.
58 const codexMcpServersOff = (names: ReadonlyArray<string>): string | null =>
59 names.length === 0
60 ? null
61 : `mcp_servers={${names.map((name) => `${JSON.stringify(name)}={enabled=false}`).join(",")}}`;
62 // Codex splits -c keys at the first "=" and on every dot, so only those names take a dotted flag.
63 const codexMcpServerOffFlags = (names: ReadonlyArray<string>) =>
64 names
65 .filter((name) => !name.includes(".") && !name.includes("="))
66 .flatMap((name) => ["--config", `mcp_servers.${name}.enabled=false`]);
67 const encodeJsonString = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
68 export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(function* (
69 codexConfig: CodexSettings,
70 environment?: NodeJS.ProcessEnv,
71 getModels: Effect.Effect<ReadonlyArray<ServerProviderModel>> = Effect.succeed([]),
72 resolveRuntime?: Effect.Effect<
73 import("../provider/CodexManagedRuntime.ts").CodexEffectiveRuntime,
74 import("@t3tools/contracts").ProviderSetupError,
75 Scope.Scope
76 >,
77 ) {
78 const fileSystem = yield* FileSystem.FileSystem;
79 const path = yield* Path.Path;
80 const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
81 const serverConfig = yield* Effect.service(ServerConfig.ServerConfig);
82 const resolvedEnvironment = environment ?? process.env;
83
84 const readStreamAsString = <E>(
85 operation: string,
86 stream: Stream.Stream<Uint8Array, E>,
87 ): Effect.Effect<string, TextGenerationError> =>
88 stream.pipe(
89 Stream.decodeText(),
90 Stream.runFold(
91 () => "",
92 (acc, chunk) => acc + chunk,
93 ),
94 Effect.mapError((cause) =>
95 normalizeCliError("codex", operation, cause, "Failed to collect process output"),
96 ),
97 );
98
99 const removeTempFileDir = (filePath: string): Effect.Effect<void, never> =>
100 fileSystem.remove(path.dirname(filePath), { recursive: true }).pipe(Effect.ignore);
101
102 // Background fibers may inherit closed scopes that immediately reap temp files; write unscoped.
103 const writeTempFile = (
104 operation: string,
105 prefix: string,
106 content: string,
107 ): Effect.Effect<string, TextGenerationError> =>
108 fileSystem
109 .makeTempFile({
110 prefix: `t3code-${prefix}-${process.pid}-`,
111 })
112 .pipe(
113 Effect.tap((filePath) =>
114 fileSystem
115 .writeFileString(filePath, content)
116 .pipe(Effect.onError(() => removeTempFileDir(filePath))),
117 ),
118 Effect.mapError(
119 (cause) =>
120 new TextGenerationError({
121 operation,
122 detail: `Failed to write temp file`,
123 cause,
124 }),
125 ),
126 );
127
128 const encodeJsonForOperation = (
129 operation: TextGenerationOperations.Operation,
130 value: unknown,
131 ): Effect.Effect<string, TextGenerationError> =>
132 encodeJsonString(value).pipe(
133 Effect.mapError(
134 (cause) =>
135 new TextGenerationError({
136 operation,
137 detail: "Failed to encode structured output schema.",
138 cause,
139 }),
140 ),
141 );
142
143 const materializeImageAttachments = Effect.fn("materializeImageAttachments")(function* (
144 attachments: TextGenerationOperations.Request<Schema.Top>["attachments"],
145 ) {
146 if (!attachments || attachments.length === 0) {
147 return [];
148 }
149
150 const imagePaths: string[] = [];
151 for (const attachment of attachments) {
152 if (attachment.type !== "image") {
153 continue;
154 }
155
156 const resolvedPath = resolveAttachmentPath({
157 attachmentsDir: serverConfig.attachmentsDir,
158 attachment,
159 });
160 if (!resolvedPath || !path.isAbsolute(resolvedPath)) {
161 continue;
162 }
163 const fileInfo = yield* fileSystem.stat(resolvedPath).pipe(Effect.orElseSucceed(() => null));
164 if (!fileInfo || fileInfo.type !== "File") {
165 continue;
166 }
167 imagePaths.push(resolvedPath);
168 }
169 return imagePaths;
170 });
171
172 const listEnabledMcpServers = Effect.fn("listEnabledMcpServers")(
173 function* (input: {
174 readonly binary: string;
175 readonly cwd: string;
176 readonly env: NodeJS.ProcessEnv;
177 readonly launchArgs: ReadonlyArray<string>;
178 }) {
179 const spawnCommand = yield* resolveSpawnCommand(
180 input.binary,
181 [...input.launchArgs, ...CODEX_NO_PLUGIN_ARGS, "mcp", "list", "--json"],
182 { env: input.env },
183 );
184 const child = yield* commandSpawner.spawn(
185 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
186 env: input.env,
187 cwd: input.cwd,
188 shell: spawnCommand.shell,
189 forceKillAfter: "1 second",
190 }),
191 );
192 const [stdout, , exitCode] = yield* Effect.all(
193 [
194 child.stdout.pipe(Stream.decodeText(), Stream.mkString),
195 child.stderr.pipe(Stream.runDrain),
196 child.exitCode,
197 ],
198 { concurrency: "unbounded" },
199 );
200 if (exitCode !== 0) {
201 yield* Effect.logWarning("codex mcp list failed; text generation keeps its MCP servers", {
202 reason: "exit",
203 exitCode,
204 });
205 return [];
206 }
207 const servers = yield* decodeCodexMcpServers(stdout).pipe(
208 Effect.tapError(() =>
209 Effect.logWarning("codex mcp list failed; text generation keeps its MCP servers", {
210 reason: "decode",
211 }),
212 ),
213 );
214 return servers.filter((server) => server.enabled).map((server) => server.name);
215 },
216 Effect.scoped,
217 Effect.timeoutOption(CODEX_MCP_LIST_TIMEOUT_MS),
218 Effect.flatMap(
219 Option.match({
220 onNone: () =>
221 Effect.logWarning("codex mcp list failed; text generation keeps its MCP servers", {
222 reason: "timeout",
223 }).pipe(Effect.as<ReadonlyArray<string>>([])),
224 onSome: (names) => Effect.succeed(names),
225 }),
226 ),
227 Effect.orElseSucceed((): ReadonlyArray<string> => []),
228 );
229
230 const runCodexJson = Effect.fn("runCodexJson")(function* <S extends Schema.Top>({
231 operation,
232 cwd,
233 prompt,
234 outputSchema: outputSchemaJson,
235 modelSelection,
236 attachments,
237 investigate,
238 }: TextGenerationOperations.Request<S>): Effect.fn.Return<
239 S["Type"],
240 TextGenerationError,
241 S["DecodingServices"]
242 > {
243 const imagePaths = yield* materializeImageAttachments(attachments);
244 const schemaJson = yield* encodeJsonForOperation(
245 operation,
246 toJsonSchemaObject(outputSchemaJson),
247 );
248 const schemaPath = yield* writeTempFile(operation, "codex-schema", schemaJson);
249 const outputPath = yield* writeTempFile(operation, "codex-output", "").pipe(
250 Effect.onError(() => removeTempFileDir(schemaPath)),
251 );
252
253 const runCodexCommand = Effect.fn("runCodexJson.runCodexCommand")(function* () {
254 const resolved = resolveRuntime
255 ? yield* resolveRuntime.pipe(
256 Effect.mapError(
257 (cause) => new TextGenerationError({ operation, detail: cause.detail }),
258 ),
259 )
260 : undefined;
261 const effectiveConfig = resolved?.config ?? codexConfig;
262 const effectiveEnvironment = resolved?.environment ?? resolvedEnvironment;
263 const models = yield* getModels;
264 const requestedModel = modelSelection.model;
265 const listed = models.filter((candidate) => !candidate.isCustom);
266 // An unlisted slug is one this account can't run, e.g. gpt-6-luna on a ChatGPT login.
267 const requestedFamily = codexModelFamily(requestedModel);
268 const matchedModel =
269 models.find((candidate) => candidate.slug === requestedModel)?.slug ??
270 // Managed snapshots omit configured custom models; send those as requested.
271 readCustomModelEntries(codexConfig.customModels).find(
272 (entry) => entry.slug === requestedModel,
273 )?.slug ??
274 listed.find((candidate) => codexModelFamily(candidate.slug) === requestedFamily)?.slug;
275 const model =
276 matchedModel ??
277 listed.find((candidate) => candidate.isDefault)?.slug ??
278 listed.find((candidate) => !candidate.isLegacy)?.slug ??
279 listed[0]?.slug ??
280 requestedModel;
281 // The selection's options were chosen for the requested model and may not fit a fallback.
282 const fellBack = matchedModel === undefined && model !== requestedModel;
283 const launchArgs = resolveCodexLaunchArgs(effectiveConfig.launchArgs, effectiveEnvironment);
284 const reasoningEffort =
285 (fellBack
286 ? undefined
287 : getModelSelectionStringOptionValue(modelSelection, "reasoningEffort")) ??
288 DEFAULT_TEXT_GENERATION_REASONING_EFFORT;
289 const serviceTier =
290 resolved || fellBack ? undefined : getCodexServiceTierOptionValue(modelSelection);
291 const binary = effectiveConfig.binaryPath || "codex";
292 const env = {
293 ...codexAppServerEnvironment(effectiveEnvironment),
294 ...(effectiveConfig.homePath
295 ? {
296 CODEX_HOME: expandHomePath(
297 effectiveConfig.homePath,
298 yield* HostProcess.HomeDirectory,
299 ),
300 }
301 : {}),
302 };
303 const enabledMcpServers = yield* listEnabledMcpServers({
304 binary,
305 cwd,
306 env,
307 launchArgs: codexMcpListLaunchArgs(launchArgs),
308 });
309 const mcpServersOff = codexMcpServersOff(enabledMcpServers);
310 const execLaunchArgs = codexExecLaunchArgs(launchArgs);
311 // A launch-arg mcp_servers={...} table replaces ours and can re-enable dotted names.
312 const launchArgsSetMcpTable = execLaunchArgs.some((arg) => /\bmcp_servers\s*=/.test(arg));
313 const spawnCommand = yield* resolveSpawnCommand(
314 binary,
315 [
316 "exec",
317 // Before launch args: a later table would replace servers their -c flags define.
318 ...(mcpServersOff ? ["--config", mcpServersOff] : []),
319 ...execLaunchArgs,
320 // A user's MCP server could act on the text-generation prompt.
321 ...CODEX_NO_PLUGIN_ARGS,
322 ...(mcpServersOff && launchArgsSetMcpTable ? ["--config", mcpServersOff] : []),
323 // After launch args, so a launch arg's enabled=true cannot turn a server back on.
324 ...codexMcpServerOffFlags(enabledMcpServers),
325 "--ephemeral",
326 "--skip-git-repo-check",
327 "-s",
328 "read-only",
329 "--model",
330 model,
331 "--config",
332 `model_reasoning_effort="${reasoningEffort}"`,
333 // Current Codex gives hooks precedence over the historical codex_hooks alias.
334 "--config",
335 "features.codex_hooks=false",
336 "--config",
337 "features.hooks=false",
338 ...(serviceTier ? ["--config", `service_tier="${serviceTier}"`] : []),
339 "--output-schema",
340 schemaPath,
341 "--output-last-message",
342 outputPath,
343 ...imagePaths.flatMap((imagePath) => ["--image", imagePath]),
344 "-",
345 ],
346 { env: effectiveEnvironment },
347 );
348 const command = ChildProcess.make(spawnCommand.command, spawnCommand.args, {
349 env,
350 cwd,
351 shell: spawnCommand.shell,
352 stdin: {
353 stream: Stream.encodeText(Stream.make(prompt)),
354 },
355 forceKillAfter: "1 second",
356 });
357
358 const child = yield* commandSpawner
359 .spawn(command)
360 .pipe(
361 Effect.mapError((cause) =>
362 normalizeCliError("codex", operation, cause, "Failed to spawn Codex CLI process"),
363 ),
364 );
365
366 const [stdout, stderr, exitCode] = yield* Effect.all(
367 [
368 readStreamAsString(operation, child.stdout),
369 readStreamAsString(operation, child.stderr),
370 child.exitCode.pipe(
371 Effect.mapError((cause) =>
372 normalizeCliError("codex", operation, cause, "Failed to read Codex CLI exit code"),
373 ),
374 ),
375 ],
376 { concurrency: "unbounded" },
377 );
378
379 if (exitCode !== 0) {
380 const stderrDetail = stderr.trim();
381 const stdoutDetail = stdout.trim();
382 const detail = stderrDetail.length > 0 ? stderrDetail : stdoutDetail;
383 return yield* new TextGenerationError({
384 operation,
385 detail:
386 detail.length > 0
387 ? `Codex CLI command failed: ${detail}`
388 : `Codex CLI command failed with code ${exitCode}.`,
389 });
390 }
391 });
392
393 const cleanup = Effect.all([removeTempFileDir(schemaPath), removeTempFileDir(outputPath)], {
394 concurrency: "unbounded",
395 }).pipe(Effect.asVoid);
396
397 return yield* Effect.gen(function* () {
398 yield* runCodexCommand().pipe(
399 Effect.scoped,
400 Effect.timeoutOption(investigate?.timeoutMs ?? CODEX_TIMEOUT_MS),
401 Effect.flatMap(
402 Option.match({
403 onNone: () =>
404 Effect.fail(
405 new TextGenerationError({ operation, detail: "Codex CLI request timed out." }),
406 ),
407 onSome: () => Effect.void,
408 }),
409 ),
410 );
411
412 const decodeOutput = Schema.decodeEffect(Schema.fromJsonString(outputSchemaJson));
413
414 return yield* fileSystem.readFileString(outputPath).pipe(
415 Effect.mapError(
416 (cause) =>
417 new TextGenerationError({
418 operation,
419 detail: "Failed to read Codex output file.",
420 cause,
421 }),
422 ),
423 Effect.flatMap(decodeOutput),
424 Effect.catchTags({
425 SchemaError: (cause) =>
426 Effect.fail(
427 new TextGenerationError({
428 operation,
429 detail: "Codex returned invalid structured output.",
430 cause,
431 }),
432 ),
433 }),
434 );
435 }).pipe(Effect.ensuring(cleanup));
436 });
437
438 return TextGenerationOperations.fromRunner("CodexTextGeneration", runCodexJson);
439 });