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

CodexDriver.ts

430 lines16.7 KB

1import { resolveProviderProbeTimeoutMs } from "@t3tools/provider-core/server/snapshotProbe";
2import { CodexSettings, ProviderDriverKind } from "@t3tools/contracts";
3import * as Digest from "effect/crypto/Digest";
4import * as SecureRandom from "effect/crypto/SecureRandom";
5import type * as Signature from "effect/crypto/Signature";
6import * as Duration from "effect/Duration";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Option from "effect/Option";
10import * as Path from "effect/Path";
11import * as Schema from "effect/Schema";
12import * as Stream from "effect/Stream";
13import { HttpClient, HttpClientResponse } from "effect/http";
14import { ChildProcessSpawner } from "effect/process";
15​
16import type * as CodexUsageHistory from "../../usage/CodexUsageHistory.ts";
17import { makeCodexTextGeneration } from "../../textGeneration/CodexTextGeneration.ts";
18import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
19import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
20import * as ServerConfig from "../../config.ts";
21import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
22import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
23import {
24 createCodexAdapterV2,
25 type CodexAdapterV2DriverEnv,
26} from "../../orchestration-v2/Adapters/CodexAdapterV2.ts";
27import { ProviderDriverError } from "../Errors.ts";
28import * as ResetCreditCoordinator from "../resetCreditCoordinator.ts";
29import {
30 checkCodexProviderStatus,
31 makePendingCodexProvider,
32 probeCodexSkillsForCwd,
33 withCodexAppServerClient,
34} from "../CodexProvider.ts";
35import { resolveCodexLaunchArgs } from "../codexLaunchArgs.ts";
36import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
37import * as ModelCatalog from "@t3tools/provider-core/server/ModelCatalog";
38import { applyCodexModelCatalog } from "../codexModelCatalog.ts";
39import type { ProviderDriver, ProviderInstance } from "@t3tools/provider-core/server/driver";
40import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity";
41import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
42import {
43 enrichProviderSnapshotWithVersionAdvisory,
44 makeCachedProviderMaintenanceResolution,
45 makePackageManagedProviderMaintenanceResolver,
46 resolveProviderMaintenanceCapabilitiesEffect,
47 type ProviderMaintenanceCapabilities,
48} from "@t3tools/provider-core/server/maintenanceResolver";
49import {
50 haveProviderSnapshotSettingsChanged,
51 makeProviderSnapshotSettingsSource,
52 type ProviderSnapshotSettings,
53} from "@t3tools/provider-core/server/snapshotSettings";
54import {
55 codexContinuationIdentity,
56 materializeCodexShadowHome,
57 resolveCodexHomeLayout,
58} from "./CodexHomeLayout.ts";
59import { makeManagedCodexProvider } from "./CodexManagedProvider.ts";
60import { codexUsageReader } from "./codexUsage.ts";
61import * as CodexInstallation from "../CodexInstallation.ts";
62import * as CodexManagedTokenBroker from "../CodexManagedTokenBroker.ts";
63import * as ServerSecretStore from "../../auth/ServerSecretStore.ts";
64import * as ServerEnvironment from "../../environment/ServerEnvironment.ts";
65import * as HostProcess from "@t3tools/shared/HostProcess";
66const decodeCodexSettings = Schema.decodeSync(CodexSettings);
67​
68const DRIVER_KIND = ProviderDriverKind.make("codex");
69const CODEX_STANDALONE_HOME_PATTERN = /^(.*)\/packages\/standalone\//is;
70​
71function codexStandaloneHome(commandPath: string): string | null {
72 // Slash replacement preserves indices; Unicode lowercasing can change the path length.
73 const match = CODEX_STANDALONE_HOME_PATTERN.exec(commandPath.replaceAll("\\", "/"));
74 if (!match) {
75 return null;
76 }
77 const home = match[1]!;
78 // Keep root separators: `C:` is drive-relative, and an empty home is not `/`.
79 const isRoot = home === "" || /^[a-z]:$/i.test(home);
80 return commandPath.slice(0, home.length + (isRoot ? 1 : 0));
82​
83// Standalone updates locate their install through CODEX_HOME. Account and auth-overlay
84// homes can share an executable, so only the updater uses the executable's owning home.
85function makeCodexMaintenanceResolver(sharedHomePath: string) {
86 return makePackageManagedProviderMaintenanceResolver({
87 provider: DRIVER_KIND,
88 npmPackageName: "@openai/codex",
89 mise: { tool: "codex" },
90 nativeUpdate: {
91 args: ["update"],
92 isCommandPath: (commandPath) => codexStandaloneHome(commandPath) !== null,
93 requiresCommandPath: true,
94 env: (context) => ({
95 CODEX_HOME:
96 codexStandaloneHome(context.realCommandPath) ??
97 codexStandaloneHome(context.resolvedCommandPath) ??
98 sharedHomePath,
99 }),
100 },
101 });
103​
104// `codex update` installs this channel's release, which can lag npm's latest tag.
105const CODEX_NATIVE_CHANNEL = "https://releases.openai.com/codex/channels/latest";
106const CodexNativeRelease = Schema.Struct({ tag_name: Schema.String });
107​
108function withCodexNativeLatestVersion(
109 capabilities: ProviderMaintenanceCapabilities,
110 http: HttpClient.HttpClient,
111): ProviderMaintenanceCapabilities {
112 if (capabilities.update?.lockKey !== `${DRIVER_KIND}-native`) return capabilities;
113 return {
114 ...capabilities,
115 latestVersion: null,
116 latestVersionCacheKey: CODEX_NATIVE_CHANNEL,
117 lookupLatestVersion: http.get(CODEX_NATIVE_CHANNEL).pipe(
118 Effect.flatMap(HttpClientResponse.filterStatusOk),
119 Effect.flatMap((response) => response.json),
120 Effect.flatMap(Schema.decodeUnknownEffect(CodexNativeRelease)),
121 Effect.map((release) => /^rust-v(\d+\.\d+\.\d+)$/.exec(release.tag_name)?.[1] ?? null),
122 Effect.timeoutOption("4 seconds"),
123 Effect.map(Option.getOrNull),
124 Effect.orElseSucceed(() => null),
125 ),
126 };
128​
129export type CodexDriverEnv =
130 | CodexAdapterV2DriverEnv
131 | ProviderHost.ProviderHost
132 | ChildProcessSpawner.ChildProcessSpawner
133 | ResetCreditCoordinator.ResetCreditCoordinator
134 | Digest.Digest
135 | SecureRandom.SecureRandom
136 | Signature.Signature
137 | FileSystem.FileSystem
138 | HttpClient.HttpClient
139 | ProviderLatestVersions.ProviderLatestVersions
140 | ModelCatalog.ModelCatalog
141 | Path.Path
142 | ProviderEventLoggers.ProviderEventLoggers
143 | ServerConfig.ServerConfig
144 | ServerSecretStore.ServerSecretStore
145 | ServerEnvironment.ServerEnvironmentIdentity
146 | CodexInstallation.CodexInstallation
147 | CodexManagedTokenBroker.CodexManagedTokenBroker;
148​
149export const CodexDriver: ProviderDriver<
150 CodexSettings,
151 CodexDriverEnv,
152 Path.Path | CodexUsageHistory.CodexUsageHistory
153> = {
154 driverKind: DRIVER_KIND,
155 metadata: {
156 displayName: "Codex",
157 supportsMultipleInstances: true,
158 },
159 configSchema: CodexSettings,
160 defaultConfig: (): CodexSettings => decodeCodexSettings({}),
161 usage: codexUsageReader,
162 create: Effect.fn("CodexDriver.create")(function* ({
163 instanceId,
164 displayName,
165 accentColor,
166 environment,
167 enabled,
168 config,
169 }) {
170 if (config.setupMode === "managed")
171 return yield* makeManagedCodexProvider({
172 instanceId,
173 displayName,
174 accentColor,
175 environment,
176 enabled,
177 config,
178 });
179 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
180 const resetCreditCoordinator = yield* ResetCreditCoordinator.ResetCreditCoordinator;
181 const fileSystem = yield* FileSystem.FileSystem;
182 const pathService = yield* Path.Path;
183 const httpClient = yield* HttpClient.HttpClient;
184 const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions;
185 const installation = yield* CodexInstallation.CodexInstallation;
186 const modelCatalog = yield* ModelCatalog.ModelCatalog;
187 const currentCatalog = modelCatalog.current(DRIVER_KIND);
188 const processEnv = yield* mergeProviderInstanceEnvironment(
189 environment,
190 yield* HostProcess.Environment,
191 );
192 const environmentHome = processEnv.CODEX_HOME;
193 const homeConfig =
194 !config.homePath.trim() && environmentHome?.trim()
195 ? { ...config, homePath: environmentHome }
196 : config;
197 const homeLayout = yield* resolveCodexHomeLayout(homeConfig);
198 const continuationIdentity = codexContinuationIdentity(homeLayout);
199 const stampIdentity = withInstanceIdentity({
200 instanceId,
201 driverKind: DRIVER_KIND,
202 displayName,
203 accentColor,
204 continuationGroupKey: continuationIdentity.continuationKey,
205 });
206 yield* materializeCodexShadowHome(homeLayout).pipe(
207 Effect.mapError(
208 (cause) =>
209 new ProviderDriverError({
210 driver: DRIVER_KIND,
211 instanceId,
212 detail: cause.message,
213 cause,
214 }),
215 ),
216 );
217 const effectiveConfig = {
218 ...config,
219 enabled,
220 binaryPath: expandHomePath(config.binaryPath, yield* HostProcess.HomeDirectory),
221 homePath: homeLayout.effectiveHomePath ?? "",
222 } satisfies CodexSettings;
223 const resolveInstallation = yield* makeCachedProviderMaintenanceResolution(
224 resolveProviderMaintenanceCapabilitiesEffect(
225 makeCodexMaintenanceResolver(homeLayout.sharedHomePath),
226 {
227 binaryPath: effectiveConfig.binaryPath,
228 env: processEnv,
229 },
230 ).pipe(
231 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
232 Effect.provideService(FileSystem.FileSystem, fileSystem),
233 Effect.provideService(Path.Path, pathService),
234 ),
235 );
236 const resolveMaintenance: typeof resolveInstallation = (options) =>
237 resolveInstallation(options).pipe(
238 Effect.map((capabilities) => withCodexNativeLatestVersion(capabilities, httpClient)),
239 );
240​
241 const orchestrationAdapter = yield* createCodexAdapterV2(
242 {
243 instanceId,
244 displayName,
245 accentColor,
246 environment,
247 enabled,
248 config: homeConfig,
249 },
250 {
251 onUsageLimits: (update) => snapshot.applyUsageLimits(update),
252 models: Effect.suspend(() => snapshot.getSnapshot).pipe(
253 Effect.map((value) => value.models),
254 ),
255 compatibility: installation.compatibility,
256 },
257 ).pipe(
258 Effect.mapError(
259 (cause) =>
260 new ProviderDriverError({
261 driver: DRIVER_KIND,
262 instanceId,
263 detail: "Failed to build Codex orchestration adapter.",
264 cause,
265 }),
266 ),
267 );
268​
269 // Kick the TTL-gated manifest refresh in the background and classify with the in-memory
270 // manifest, so a slow or hung fetch never delays the provider check.
271 const checkProvider = modelCatalog.refreshInBackground.pipe(
272 Effect.andThen(
273 Effect.zipWith(
274 checkCodexProviderStatus(effectiveConfig, undefined, processEnv),
275 currentCatalog,
276 (draft, catalog) => stampIdentity(applyCodexModelCatalog(draft, catalog)),
277 { concurrent: true },
278 ),
279 ),
280 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
281 Effect.provideService(FileSystem.FileSystem, fileSystem),
282 Effect.provideService(Path.Path, pathService),
283 );
284 const snapshotSettings = yield* makeProviderSnapshotSettingsSource(effectiveConfig);
285 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<CodexSettings>>({
286 resolveMaintenance,
287 getSettings: snapshotSettings.getSettings,
288 streamSettings: snapshotSettings.streamSettings,
289 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
290 initialSnapshot: (settings) =>
291 Effect.zipWith(
292 makePendingCodexProvider(settings.provider),
293 currentCatalog,
294 (draft, catalog) => stampIdentity(applyCodexModelCatalog(draft, catalog)),
295 ),
296 checkProvider,
297 enrichSnapshot: ({ settings, snapshot, publishSnapshot }) =>
298 resolveMaintenance().pipe(
299 Effect.flatMap((maintenanceCapabilities) =>
300 enrichProviderSnapshotWithVersionAdvisory(snapshot, maintenanceCapabilities, {
301 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
302 }),
303 ),
304 Effect.provideService(HttpClient.HttpClient, httpClient),
305 Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, latestVersions),
306 Effect.flatMap((enrichedSnapshot) => publishSnapshot(enrichedSnapshot)),
307 ),
308 }).pipe(
309 Effect.mapError(
310 (cause) =>
311 new ProviderDriverError({
312 driver: DRIVER_KIND,
313 instanceId,
314 detail: `Failed to build Codex snapshot: ${cause.message ?? String(cause)}`,
315 cause,
316 }),
317 ),
318 );
319 const textGeneration = yield* makeCodexTextGeneration(
320 effectiveConfig,
321 processEnv,
322 snapshot.getSnapshot.pipe(Effect.map((value) => value.models)),
323 );
324 const snapshotForCwd = (cwd: string) =>
325 !effectiveConfig.enabled
326 ? snapshot.getSnapshot
327 : Effect.all([
328 snapshot.getSnapshot,
329 probeCodexSkillsForCwd({
330 binaryPath: effectiveConfig.binaryPath,
331 homePath: effectiveConfig.homePath,
332 launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv),
333 cwd,
334 environment: processEnv,
335 }).pipe(
336 Effect.scoped,
337 Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv)),
338 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
339 ),
340 ]).pipe(
341 Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills })),
342 Effect.mapError(
343 (cause) =>
344 new ProviderDriverError({
345 driver: DRIVER_KIND,
346 instanceId,
347 detail: `Failed to probe Codex skills for '${cwd}'`,
348 cause,
349 }),
350 ),
351 );
352​
353 // Redemption spends account credit; serialize by the auth.json directory, keep one idempotency key
354 // until the outcome, and bound the wait so a hung app-server cannot hold the account lock.
355 // Auth overlays use effectiveHomePath; the continuation key would conflate them with shared homes.
356 const accountKey = homeLayout.effectiveHomePath ?? homeLayout.sharedHomePath;
357 const consumeResetCredit: NonNullable<ProviderInstance["consumeResetCredit"]> = () =>
358 resetCreditCoordinator
359 .redeem(accountKey, (idempotencyKey) =>
360 Effect.gen(function* () {
361 const { client } = yield* withCodexAppServerClient({
362 binaryPath: effectiveConfig.binaryPath,
363 homePath: effectiveConfig.homePath,
364 launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv),
365 cwd: process.cwd(),
366 environment: processEnv,
367 });
368 const response = yield* client.request("account/rateLimitResetCredit/consume", {
369 idempotencyKey,
370 });
371 return response.outcome;
372 }).pipe(Effect.scoped, Effect.timeout("20 seconds")),
373 )
374 .pipe(
375 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
376 Effect.mapError(
377 (cause) =>
378 new ProviderDriverError({
379 driver: DRIVER_KIND,
380 instanceId,
381 detail: "Codex could not redeem the reset credit.",
382 cause,
383 }),
384 ),
385 Effect.tap((outcome) =>
386 ResetCreditCoordinator.refreshAfterRedemption({
387 outcome,
388 getSnapshot: snapshot.getSnapshot,
389 refresh: snapshot.refresh,
390 }).pipe(
391 Effect.filterOrFail(
392 (confirmed) => confirmed,
393 () =>
394 new ProviderDriverError({
395 driver: DRIVER_KIND,
396 instanceId,
397 detail:
398 "The reset was applied, but Codex could not confirm the new limits. Refresh to check.",
399 }),
400 ),
401 ),
402 ),
403 );
404​
405 // `codex login` and `codex logout` rewrite auth.json outside T3.
406 if (effectiveConfig.enabled) {
407 yield* fileSystem.watch(accountKey).pipe(
408 Stream.filter((event) => pathService.basename(event.path) === "auth.json"),
409 Stream.debounce(Duration.millis(250)),
410 Stream.runForEach(() => snapshot.refresh.pipe(Effect.ignoreCause({ log: true }))),
411 Effect.catchReason("PlatformError", "NotFound", () => Effect.void),
412 Effect.ignoreCause({ log: true }),
413 Effect.forkScoped,
414 );
415 }
416 return {
417 instanceId,
418 driverKind: DRIVER_KIND,
419 continuationIdentity,
420 displayName,
421 accentColor,
422 enabled,
423 snapshot,
424 snapshotForCwd,
425 consumeResetCredit,
426 orchestrationAdapter,
427 textGeneration,
428 } satisfies ProviderInstance;
429 }),
430};