import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import * as Scope from "effect/Scope"; import * as Types from "effect/Types"; import * as ChildProcess from "effect/unstable/process/ChildProcess"; import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; import * as CodexClient from "effect-codex-app-server/client"; import * as CodexSchema from "effect-codex-app-server/schema"; import * as CodexErrors from "effect-codex-app-server/errors"; import type { CodexSettings, ServerProvider, ServerProviderAuth, ServerProviderSkill, ServerProviderState, ModelCapabilities, ProviderOptionDescriptor, ServerProviderModel, ServerProviderSkill, } from "@t3tools/contracts"; import { Cache, Data, Duration, Effect, Equal, FileSystem, Layer, Option, Path, Result, Stream, } from "effect"; import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import { createModelCapabilities } from "@t3tools/shared/model"; import { resolveSpawnCommand } from "@t3tools/shared/shell"; import { codexAppServerArgs, resolveCodexLaunchArgs } from "./codexLaunchArgs.ts"; import { AUTH_PROBE_TIMEOUT_MS, buildServerProvider, type ServerProviderDraft, } from "../providerSnapshot.ts"; import { makeManagedServerProvider } from "../makeManagedServerProvider.ts"; import { formatCodexCliUpgradeMessage, isCodexCliVersionSupported, parseCodexCliVersion, } from "../codexCliVersion.ts"; import { adjustCodexModelsForAccount, codexAuthSubLabel, codexAuthSubType, type CodexAccountSnapshot, } from "../codexAccount.ts"; import { type CodexDiscoverySnapshot, probeCodexDiscovery } from "../codexAppServer.ts"; import { BUILT_IN_CODEX_MODELS, DEFAULT_CODEX_MODEL_CAPABILITIES } from "../codexModels.ts"; import { CodexProvider } from "../Services/CodexProvider.ts"; import { ServerSettingsService } from "../../serverSettings.ts"; import { ServerSettingsError } from "@t3tools/contracts"; const PROVIDER = "codex" as const; const OPENAI_AUTH_PROVIDERS = new Set(["openai"]); class CodexDiscoveryCacheKey extends Data.Class<{ readonly binaryPath: string; readonly homePath?: string; readonly cwd: string; }> {} const makePendingCodexProvider = (codexSettings: CodexSettings): ServerProvider => { const checkedAt = new Date().toISOString(); const models = providerModelsFromSettings( BUILT_IN_CODEX_MODELS, PROVIDER, codexSettings.customModels, DEFAULT_CODEX_MODEL_CAPABILITIES, ); if (!codexSettings.enabled) { return buildServerProvider({ provider: PROVIDER, enabled: false, checkedAt, models, skills: [], probe: { installed: false, version: null, status: "warning", auth: { status: "unknown" }, message: "Codex is disabled in T3 Code settings.", }, }); } return buildServerProvider({ provider: PROVIDER, enabled: true, checkedAt, models, skills: [], probe: { installed: false, version: null, status: "warning", auth: { status: "unknown" }, message: "Codex provider status has not been checked in this session yet.", }, }); }; const REASONING_EFFORT_LABELS: Readonly> = { none: "None", minimal: "Minimal", low: "Low", medium: "Medium", high: "High", xhigh: "Extra High", max: "Max", ultra: "Ultra", }; const DEFAULT_SERVICE_TIER_ID = "default"; const CURRENT_CODEX_MODELS = new Set(["gpt-5.6-luna", "gpt-5.6-terra", "gpt-5.6-sol"]); export function isLegacyCodexModel(model: string): boolean { return !CURRENT_CODEX_MODELS.has(model); } function reasoningEffortLabel(reasoningEffort: string): string { return REASONING_EFFORT_LABELS[reasoningEffort] ?? reasoningEffort; } function codexAccountAuthLabel(account: CodexSchema.V2GetAccountResponse["account"]) { if (!account) return undefined; if (account.type === "apiKey") return "OpenAI API Key"; if (account.type === "amazonBedrock") return "Amazon Bedrock"; if (account.type !== "chatgpt") return undefined; switch (account.planType) { case "free": return "ChatGPT Free Subscription"; case "go": return "ChatGPT Go Subscription"; case "plus": return "ChatGPT Plus Subscription"; case "pro": return "ChatGPT Pro 20x Subscription"; case "prolite": return "ChatGPT Pro 5x Subscription"; case "team": return "ChatGPT Team Subscription"; case "self_serve_business_usage_based": case "business": return "ChatGPT Business Subscription"; case "enterprise_cbp_usage_based": case "enterprise": return "ChatGPT Enterprise Subscription"; case "edu": return "ChatGPT Edu Subscription"; case "unknown": return "ChatGPT Subscription"; default: account.planType satisfies never; return undefined; } } function codexAccountEmail(account: CodexSchema.V2GetAccountResponse["account"]) { if (!account || account.type !== "chatgpt") return undefined; return account.email; } export function mapCodexModelCapabilities( model: CodexSchema.V2ModelListResponse__Model, ): ModelCapabilities { const reasoningOptions = model.supportedReasoningEfforts.map(({ reasoningEffort }) => reasoningEffort === model.defaultReasoningEffort ? { id: reasoningEffort, label: reasoningEffortLabel(reasoningEffort), isDefault: true, } : { id: reasoningEffort, label: reasoningEffortLabel(reasoningEffort), }, ); const defaultReasoning = reasoningOptions.find((option) => option.isDefault)?.id; const serviceTiers = model.serviceTiers && model.serviceTiers.length > 0 ? model.serviceTiers : (model.additionalSpeedTiers ?? []).map((id) => ({ id, name: id === "fast" ? "Fast" : id, description: "", })); const catalogDefaultServiceTier = serviceTiers.some( (tier) => tier.id === model.defaultServiceTier, ) ? model.defaultServiceTier : null; const defaultServiceTier = catalogDefaultServiceTier ?? DEFAULT_SERVICE_TIER_ID; const optionDescriptors: ProviderOptionDescriptor[] = []; if (reasoningOptions.length > 0) { optionDescriptors.push({ id: "reasoningEffort", label: "Reasoning", type: "select", options: reasoningOptions, ...(defaultReasoning ? { currentValue: defaultReasoning } : {}), }); } if (serviceTiers.length > 0) { optionDescriptors.push({ id: "serviceTier", label: "Service Tier", type: "select", options: [ { id: DEFAULT_SERVICE_TIER_ID, label: "Standard", ...(defaultServiceTier === DEFAULT_SERVICE_TIER_ID ? { isDefault: true } : {}), }, ...serviceTiers.map((tier) => ({ id: tier.id, label: tier.name, ...(tier.description ? { description: tier.description } : {}), ...(defaultServiceTier === tier.id ? { isDefault: true } : {}), })), ], currentValue: defaultServiceTier, }); } return createModelCapabilities({ optionDescriptors, }); } const toDisplayName = (model: CodexSchema.V2ModelListResponse__Model): string => { // Capitalize 'gpt' to 'GPT-' and capitalize any letter following a dash return model.displayName .replace(/^gpt/i, "GPT") // Handle start with 'gpt' or 'GPT' .replace(/-([a-z])/g, (_, c) => "-" + c.toUpperCase()); }; function parseCodexModelListResponse( response: CodexSchema.V2ModelListResponse, ): ReadonlyArray { return response.data.map((model) => ({ slug: model.model, name: toDisplayName(model), isCustom: false, ...(model.isDefault ? { isDefault: true } : {}), ...(isLegacyCodexModel(model.model) ? { isLegacy: true } : {}), capabilities: mapCodexModelCapabilities(model), })); } /** * Prefer our own default-model ranking when one of the preferred slugs is in * the live catalog; otherwise keep whatever Codex itself flagged as default. */ export function applyPreferredCodexDefaultModel( models: ReadonlyArray, ): ReadonlyArray { const preferredSlug = PREFERRED_DEFAULT_CODEX_MODELS.find((slug) => models.some((model) => model.slug === slug && !model.isCustom), ); if (!preferredSlug) { return models; } return models.map((model) => { if (model.slug === preferredSlug) { return model.isDefault ? model : { ...model, isDefault: true }; } if (!model.isDefault) { return model; } const { isDefault: _isDefault, ...rest } = model; return rest; }); } function appendCustomCodexModels( models: ReadonlyArray, customModels: ReadonlyArray, ): ReadonlyArray { if (customModels.length === 0) { return models; } const seen = new Set(models.map((model) => model.slug)); const fallbackCapabilities = models.find((model) => model.capabilities)?.capabilities ?? null; const customEntries: ServerProviderModel[] = []; for (const rawModel of customModels) { const slug = rawModel.trim(); if (!slug || seen.has(slug)) { continue; } seen.add(slug); customEntries.push({ slug, name: slug, isCustom: true, capabilities: fallbackCapabilities, }); } return customEntries.length === 0 ? models : [...models, ...customEntries]; } function parseCodexSkillsListResponse( response: CodexSchema.V2SkillsListResponse, cwd: string, ): ReadonlyArray { const matchingEntry = response.data.find((entry) => entry.cwd === cwd); const skills = matchingEntry ? matchingEntry.skills : response.data.flatMap((entry) => entry.skills); return skills.map((skill) => { const shortDescription = skill.shortDescription ?? skill.interface?.shortDescription ?? undefined; const parsedSkill: Types.Mutable = { name: skill.name, path: skill.path, enabled: skill.enabled, }; if (skill.description) { parsedSkill.description = skill.description; } if (skill.scope) { parsedSkill.scope = skill.scope; } if (skill.interface?.displayName) { parsedSkill.displayName = skill.interface.displayName; } if (shortDescription) { parsedSkill.shortDescription = shortDescription; } return parsedSkill; }); } const requestAllCodexModels = Effect.fn("requestAllCodexModels")(function* ( client: CodexClient.CodexAppServerClient["Service"], ) { const models: ServerProviderModel[] = []; let cursor: string | null | undefined = undefined; do { const response: CodexSchema.V2ModelListResponse = yield* client.request( "model/list", cursor ? { cursor } : {}, ); models.push(...parseCodexModelListResponse(response)); cursor = response.nextCursor; } while (cursor); return models; }); export function buildCodexInitializeParams(): CodexSchema.V1InitializeParams { return { clientInfo: { name: "t3code_desktop", title: "T3 Code Desktop", version: packageJson.version, }, capabilities: { experimentalApi: true, }, }; } const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(function* (input: { readonly binaryPath: string; readonly homePath?: string; readonly launchArgs?: string; readonly cwd: string; }) => probeCodexDiscovery(input).pipe( Effect.timeoutOption(CAPABILITIES_PROBE_TIMEOUT_MS), Effect.result, Effect.map((result) => { if (Result.isFailure(result)) return undefined; return Option.isSome(result.success) ? result.success.value : undefined; }), ); const initialize = yield* client.request("initialize", { clientInfo: { name: "t3code_desktop", title: "T3 Code Desktop", version: "0.1.0", }, capabilities: { experimentalApi: true, }, }); yield* client.notify("initialized", undefined); // Extract the version string after the first '/' in userAgent, up to the next space or the end const versionMatch = initialize.userAgent.match(/\/([^\s]+)/); const version = versionMatch ? versionMatch[1] : undefined; const accountResponse = yield* client.request("account/read", {}); if (!accountResponse.account && accountResponse.requiresOpenaiAuth) { return { account: accountResponse, version, models: appendCustomCodexModels([], input.customModels ?? []), skills: [], } satisfies CodexAppServerProviderSnapshot; } const [skillsResponse, models] = yield* Effect.all( [ client.request("skills/list", { cwds: [input.cwd], }), requestAllCodexModels(client), ], { concurrency: "unbounded" }, ); return { account: accountResponse, version, models: applyPreferredCodexDefaultModel( appendCustomCodexModels(models, input.customModels ?? []), ), skills: parseCodexSkillsListResponse(skillsResponse, input.cwd), } satisfies CodexAppServerProviderSnapshot; }); export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(function* ( resolveAccount?: (input: { readonly binaryPath: string; readonly homePath?: string; }) => Effect.Effect, resolveSkills?: (input: { readonly binaryPath: string; readonly homePath?: string; readonly cwd: string; }) => Effect.Effect | undefined>, ): Effect.fn.Return< ServerProvider, ServerSettingsError, | ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path | ServerSettingsService > { const codexSettings = yield* Effect.service(ServerSettingsService).pipe( Effect.flatMap((service) => service.getSettings), Effect.map((settings) => settings.providers.codex), ); const checkedAt = new Date().toISOString(); const models = providerModelsFromSettings( BUILT_IN_CODEX_MODELS, PROVIDER, codexSettings.customModels, DEFAULT_CODEX_MODEL_CAPABILITIES, ); if (!codexSettings.enabled) { return buildServerProvider({ provider: PROVIDER, enabled: false, checkedAt, models, probe: { installed: false, version: null, status: "warning", auth: { status: "unknown" }, message: "Codex is disabled in T3 Code settings.", }, }); } const versionProbe = yield* runCodexCommand(["--version"]).pipe( Effect.timeoutOption(DEFAULT_TIMEOUT_MS), Effect.result, ); if (Result.isFailure(versionProbe)) { const error = versionProbe.failure; return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models, probe: { installed: !isCommandMissingCause(error), version: null, status: "error", auth: { status: "unknown" }, message: isCommandMissingCause(error) ? "Codex CLI (`codex`) is not installed or not on PATH." : `Failed to execute Codex CLI health check: ${error.message}.`, }, }); } if (Option.isNone(versionProbe.success)) { return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models, probe: { installed: true, version: null, status: "error", auth: { status: "unknown" }, message: "Codex CLI is installed but failed to run. Timed out while running command.", }, }); } const version = versionProbe.success.value; const parsedVersion = parseCodexCliVersion(`${version.stdout}\n${version.stderr}`) ?? parseGenericCliVersion(`${version.stdout}\n${version.stderr}`); if (version.code !== 0) { const detail = detailFromResult(version); return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models, probe: { installed: true, version: parsedVersion, status: "error", auth: { status: "unknown" }, message: detail ? `Codex CLI is installed but failed to run. ${detail}` : "Codex CLI is installed but failed to run.", }, }); } if (parsedVersion && !isCodexCliVersionSupported(parsedVersion)) { return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models, probe: { installed: true, version: parsedVersion, status: "error", auth: { status: "unknown" }, message: formatCodexCliUpgradeMessage(parsedVersion), }, }); } const skills = (resolveSkills ? yield* resolveSkills({ binaryPath: codexSettings.binaryPath, homePath: codexSettings.homePath, cwd: process.cwd(), }).pipe(Effect.orElseSucceed(() => undefined)) : undefined) ?? []; if (yield* hasCustomModelProvider) { return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models, skills, probe: { installed: true, version: parsedVersion, status: "ready", auth: { status: "unknown" }, message: "Using a custom Codex model provider; OpenAI login check skipped.", }, }); } const authProbe = yield* runCodexCommand(["login", "status"]).pipe( Effect.timeoutOption(DEFAULT_TIMEOUT_MS), Effect.result, ); const account = resolveAccount ? yield* resolveAccount({ binaryPath: codexSettings.binaryPath, homePath: codexSettings.homePath, }) : undefined; const resolvedModels = adjustCodexModelsForAccount(models, account); if (Result.isFailure(authProbe)) { const error = authProbe.failure; return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models: resolvedModels, skills, probe: { installed: true, version: parsedVersion, status: "warning", auth: { status: "unknown" }, message: `Could not verify Codex authentication status: ${error.message}.`, }, }); } if (Option.isNone(authProbe.success)) { return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models: resolvedModels, skills, probe: { installed: true, version: parsedVersion, status: "warning", auth: { status: "unknown" }, message: "Could not verify Codex authentication status. Timed out while running command.", }, }); } const parsed = parseAuthStatusFromOutput(authProbe.success.value); const authType = codexAuthSubType(account); const authLabel = codexAuthSubLabel(account); return buildServerProvider({ provider: PROVIDER, enabled: codexSettings.enabled, checkedAt, models: resolvedModels, skills, probe: { installed: true, version: parsedVersion, status: parsed.status, auth: { ...parsed.auth, ...(authType ? { type: authType } : {}), ...(authLabel ? { label: authLabel } : {}), }, ...(parsed.message ? { message: parsed.message } : {}), }, }); }); const applyCodexDiscoverySnapshot = ( snapshot: ServerProvider, discovery: CodexDiscoverySnapshot, ): ServerProvider => { const authType = codexAuthSubType(discovery.account); const authLabel = codexAuthSubLabel(discovery.account); return { ...snapshot, auth: { ...snapshot.auth, ...(authType ? { type: authType } : {}), ...(authLabel ? { label: authLabel } : {}), }, models: adjustCodexModelsForAccount(snapshot.models, discovery.account), skills: discovery.skills, }; }; const enrichCodexSnapshotViaDiscovery = (input: { readonly settings: CodexSettings; readonly snapshot: ServerProvider; readonly publishSnapshot: (snapshot: ServerProvider) => Effect.Effect; readonly getDiscovery: (input: { readonly binaryPath: string; readonly homePath?: string; readonly cwd: string; }) => Effect.Effect; }) => (input.settings.enabled && input.snapshot.installed ? input .getDiscovery({ binaryPath: input.settings.binaryPath, homePath: input.settings.homePath, cwd: process.cwd(), }) .pipe( Effect.flatMap((discovery) => discovery ? input.publishSnapshot(applyCodexDiscoverySnapshot(input.snapshot, discovery)) : Effect.void, ), ) : Effect.void ).pipe(Effect.catchCause((cause) => Effect.logError(cause))); export const CodexProviderLive = Layer.effect( CodexProvider, Effect.gen(function* () { const serverSettings = yield* ServerSettingsService; const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const accountProbeCache = yield* Cache.make({ capacity: 4, timeToLive: Duration.minutes(5), lookup: (key: CodexDiscoveryCacheKey) => { const { binaryPath, homePath, cwd } = key; return probeCodexCapabilities({ binaryPath, cwd, ...(homePath ? { homePath } : {}), }); }, }); }); const getDiscovery = (input: { readonly binaryPath: string; readonly homePath?: string; readonly cwd: string; }) => Cache.get(accountProbeCache, new CodexDiscoveryCacheKey(input)); const checkProvider = checkCodexProviderStatus().pipe( Effect.provideService(ServerSettingsService, serverSettings), Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), ); return yield* makeManagedServerProvider({ getSettings: serverSettings.getSettings.pipe( Effect.map((settings) => settings.providers.codex), Effect.orDie, ), streamSettings: serverSettings.streamChanges.pipe( Stream.map((settings) => settings.providers.codex), ), haveSettingsChanged: (previous, next) => !Equal.equals(previous, next), initialSnapshot: makePendingCodexProvider, checkProvider, enrichSnapshot: ({ settings, snapshot, publishSnapshot }) => enrichCodexSnapshotViaDiscovery({ settings, snapshot, publishSnapshot, getDiscovery, }), }); } const probeResult = yield* probe({ binaryPath: codexSettings.binaryPath, homePath: codexSettings.homePath, launchArgs: resolveCodexLaunchArgs(codexSettings.launchArgs, resolvedEnvironment), cwd: process.cwd(), customModels: codexSettings.customModels, environment: resolvedEnvironment, }).pipe( Effect.scoped, Effect.timeoutOption(Duration.millis(AUTH_PROBE_TIMEOUT_MS)), Effect.result, ); if (Result.isFailure(probeResult)) { const error = probeResult.failure; const installed = !isCodexAppServerSpawnError(error); return buildServerProvider({ presentation: CODEX_PRESENTATION, enabled: codexSettings.enabled, checkedAt, models: emptyModels, skills: [], probe: { installed, version: null, status: "error", auth: { status: "unknown" }, message: installed ? `Codex app-server provider probe failed: ${error.message}.` : "Codex CLI (`codex`) is not installed or not on PATH.", }, }); } if (Option.isNone(probeResult.success)) { return buildServerProvider({ presentation: CODEX_PRESENTATION, enabled: codexSettings.enabled, checkedAt, models: emptyModels, skills: [], probe: { installed: true, version: null, status: "error", auth: { status: "unknown" }, message: "Timed out while checking Codex app-server provider status.", }, }); } const snapshot = probeResult.success.value; const accountStatus = accountProbeStatus(snapshot.account); return buildServerProvider({ presentation: CODEX_PRESENTATION, enabled: codexSettings.enabled, checkedAt, models: snapshot.models, skills: snapshot.skills, probe: { installed: true, version: snapshot.version ?? null, status: accountStatus.status, auth: accountStatus.auth, ...(accountStatus.message ? { message: accountStatus.message } : {}), }, }); }); // NOTE: the singleton `CodexProviderLive` Layer has been removed as part of // the per-instance-driver refactor. `CodexDriver.create()` builds a managed // snapshot per instance (each with its own `CodexSettings`) and hands the // resulting `ServerProviderShape` back as `ProviderInstance.snapshot`. // // The `makePendingCodexProvider` and `checkCodexProviderStatus` helpers are // re-exported for use by `CodexDriver`. export { makePendingCodexProvider };