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

CodexManagedRuntime.ts

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