@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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