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

CodexManagedRuntime.ts

112 lines4.2 KB

1import { resolveManagedCodexHomeLayout } from "./CodexManagedHome.ts";
2import { CodexSettings, ProviderSetupError, type ProviderInstanceId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7import * as ServerConfig from "../config.ts";
8import * as CodexInstallation from "./CodexInstallation.ts";
9import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts";
10import { materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts";
11​
12export interface CodexEffectiveRuntime {
13 readonly config: CodexSettings;
14 readonly environment: NodeJS.ProcessEnv;
15 readonly revision: string;
17const decodeSettings = Schema.decodeSync(CodexSettings);
18// Managed sign-in stores tokens in T3's credential store and never writes native auth.json.
19const managedCodexLaunchArgs = [
20 'model_provider="openai_token_sharing"',
21 'model_providers.openai_token_sharing.name="OpenAI Token Sharing"',
22 'model_providers.openai_token_sharing.base_url="https://api.openai.com/v1"',
23 'model_providers.openai_token_sharing.model_catalog_url="https://api.openai.com/v1/models"',
24 "features.api_key_model_discovery=true",
25 'model_providers.openai_token_sharing.env_key="ACCESS_TOKEN"',
26 'model_providers.openai_token_sharing.wire_api="responses"',
27 "model_providers.openai_token_sharing.requires_openai_auth=false",
28 "model_providers.openai_token_sharing.supports_websockets=false",
30 .map((value) => `-c '${value}'`)
31 .join(" ");
32​
33export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(function* (options: {
34 readonly instanceId: ProviderInstanceId;
35 readonly enabled: boolean;
36 readonly environment: NodeJS.ProcessEnv;
37 readonly config: CodexSettings;
38}) {
39 const installation = yield* CodexInstallation.CodexInstallation;
40 const config = yield* ServerConfig.ServerConfig;
41 const fs = yield* FileSystem.FileSystem;
42 const path = yield* Path.Path;
43 const auth = yield* makeCodexChatGptAuth({
44 instanceId: options.instanceId,
45 defaultReturnUrl: new URL(
46 "/welcome",
47 config.devUrl ?? `http://localhost:${config.port}`,
48 ).toString(),
49 });
50 const homeLayout = yield* resolveManagedCodexHomeLayout(
51 config.stateDir,
52 options.instanceId,
53 options.config,
54 );
55 const homePath = homeLayout.effectiveHomePath ?? homeLayout.sharedHomePath;
56 const resolve = Effect.gen(function* () {
57 const executable = yield* installation.acquire().pipe(
58 Effect.mapError(
59 () =>
60 new ProviderSetupError({
61 instanceId: options.instanceId,
62 operation: "install",
63 detail: "Set up managed Codex before starting a session.",
64 }),
65 ),
66 );
67 const credentials = yield* auth.access;
68 yield* materializeCodexShadowHome(homeLayout).pipe(
69 Effect.provideService(FileSystem.FileSystem, fs),
70 Effect.provideService(Path.Path, path),
71 Effect.mapError(
72 (cause) =>
73 new ProviderSetupError({
74 instanceId: options.instanceId,
75 operation: "runtime",
76 detail: cause.message,
77 }),
78 ),
79 );
80 yield* fs.makeDirectory(homePath, { recursive: true }).pipe(
81 Effect.mapError(
82 () =>
83 new ProviderSetupError({
84 instanceId: options.instanceId,
85 operation: "runtime",
86 detail: "Could not prepare the managed Codex runtime.",
87 }),
88 ),
89 );
90 // Ambient CLI overrides cannot redirect a T3-owned token to a different provider.
91 const environment: NodeJS.ProcessEnv = {
92 ...options.environment,
93 ACCESS_TOKEN: credentials.accessToken,
94 CODEX_HOME: homePath,
95 };
96 delete environment.T3CODE_CODEX_LAUNCH_ARGS;
97 delete environment.OPENAI_API_KEY;
98 delete environment.OPENAI_BASE_URL;
99 return {
100 config: decodeSettings({
101 enabled: options.enabled,
102 setupMode: "managed",
103 binaryPath: executable.executablePath,
104 homePath,
105 launchArgs: managedCodexLaunchArgs,
106 }),
107 environment,
108 revision: credentials.accessToken,
109 } satisfies CodexEffectiveRuntime;
110 });
111 return { auth, resolve, installation, homePath, homeLayout };
112});