Files
t3code/apps/server/src/provider/CodexManagedRuntime.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

113 lines
4.2 KiB
TypeScript

import { resolveManagedCodexHomeLayout } from "./CodexManagedHome.ts";
import { CodexSettings, ProviderSetupError, type ProviderInstanceId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as ServerConfig from "../config.ts";
import * as CodexInstallation from "./CodexInstallation.ts";
import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts";
import { materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts";
export interface CodexEffectiveRuntime {
readonly config: CodexSettings;
readonly environment: NodeJS.ProcessEnv;
readonly revision: string;
}
const decodeSettings = Schema.decodeSync(CodexSettings);
// Managed sign-in stores tokens in T3's credential store and never writes native auth.json.
const managedCodexLaunchArgs = [
'model_provider="openai_token_sharing"',
'model_providers.openai_token_sharing.name="OpenAI Token Sharing"',
'model_providers.openai_token_sharing.base_url="https://api.openai.com/v1"',
'model_providers.openai_token_sharing.model_catalog_url="https://api.openai.com/v1/models"',
"features.api_key_model_discovery=true",
'model_providers.openai_token_sharing.env_key="ACCESS_TOKEN"',
'model_providers.openai_token_sharing.wire_api="responses"',
"model_providers.openai_token_sharing.requires_openai_auth=false",
"model_providers.openai_token_sharing.supports_websockets=false",
]
.map((value) => `-c '${value}'`)
.join(" ");
export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(function* (options: {
readonly instanceId: ProviderInstanceId;
readonly enabled: boolean;
readonly environment: NodeJS.ProcessEnv;
readonly config: CodexSettings;
}) {
const installation = yield* CodexInstallation.CodexInstallation;
const config = yield* ServerConfig.ServerConfig;
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const auth = yield* makeCodexChatGptAuth({
instanceId: options.instanceId,
defaultReturnUrl: new URL(
"/welcome",
config.devUrl ?? `http://localhost:${config.port}`,
).toString(),
});
const homeLayout = yield* resolveManagedCodexHomeLayout(
config.stateDir,
options.instanceId,
options.config,
);
const homePath = homeLayout.effectiveHomePath ?? homeLayout.sharedHomePath;
const resolve = Effect.gen(function* () {
const executable = yield* installation.acquire().pipe(
Effect.mapError(
() =>
new ProviderSetupError({
instanceId: options.instanceId,
operation: "install",
detail: "Set up managed Codex before starting a session.",
}),
),
);
const credentials = yield* auth.access;
yield* materializeCodexShadowHome(homeLayout).pipe(
Effect.provideService(FileSystem.FileSystem, fs),
Effect.provideService(Path.Path, path),
Effect.mapError(
(cause) =>
new ProviderSetupError({
instanceId: options.instanceId,
operation: "runtime",
detail: cause.message,
}),
),
);
yield* fs.makeDirectory(homePath, { recursive: true }).pipe(
Effect.mapError(
() =>
new ProviderSetupError({
instanceId: options.instanceId,
operation: "runtime",
detail: "Could not prepare the managed Codex runtime.",
}),
),
);
// Ambient CLI overrides cannot redirect a T3-owned token to a different provider.
const environment: NodeJS.ProcessEnv = {
...options.environment,
ACCESS_TOKEN: credentials.accessToken,
CODEX_HOME: homePath,
};
delete environment.T3CODE_CODEX_LAUNCH_ARGS;
delete environment.OPENAI_API_KEY;
delete environment.OPENAI_BASE_URL;
return {
config: decodeSettings({
enabled: options.enabled,
setupMode: "managed",
binaryPath: executable.executablePath,
homePath,
launchArgs: managedCodexLaunchArgs,
}),
environment,
revision: credentials.accessToken,
} satisfies CodexEffectiveRuntime;
});
return { auth, resolve, installation, homePath, homeLayout };
});