|
| 1 | +import * as NodeOS from "node:os"; |
| 2 | +import { decodeJwt } from "jose"; |
| 3 | +import * as FileSystem from "effect/FileSystem"; |
| 4 | +import * as Path from "effect/Path"; |
1 | 5 | import * as DateTime from "effect/DateTime"; |
2 | 6 | import * as Duration from "effect/Duration"; |
3 | 7 | import * as Effect from "effect/Effect"; |
@@ -70,6 +74,7 @@ const CODEX_PRESENTATION = { |
70 | 74 | } as const; |
71 | 75 |
|
72 | 76 | export interface CodexAppServerProviderSnapshot { |
| 77 | + readonly accountId?: string; |
73 | 78 | readonly account: CodexSchema.V2GetAccountResponse; |
74 | 79 | readonly rateLimits?: CodexRateLimitsProbe; |
75 | 80 | readonly version: string | undefined; |
@@ -149,6 +154,48 @@ function codexAccountEmail(account: CodexSchema.V2GetAccountResponse["account"]) |
149 | 154 | return account.email; |
150 | 155 | } |
151 | 156 |
|
| 157 | +const CodexAuthIdentity = Schema.Struct({ |
| 158 | + tokens: Schema.optional( |
| 159 | + Schema.Struct({ |
| 160 | + account_id: Schema.optional(Schema.String), |
| 161 | + id_token: Schema.optional(Schema.String), |
| 162 | + }), |
| 163 | + ), |
| 164 | +}); |
| 165 | +const CodexIdentityClaims = Schema.Struct({ |
| 166 | + "https://api.openai.com/auth": Schema.optional( |
| 167 | + Schema.Struct({ |
| 168 | + chatgpt_account_id: Schema.optional(Schema.String), |
| 169 | + }), |
| 170 | + ), |
| 171 | +}); |
| 172 | + |
| 173 | +/** Reads only grouping identity from the same native home used by the app server. */ |
| 174 | +export const readCodexAccountId = Effect.fn("readCodexAccountId")(function* ( |
| 175 | + homePath: string, |
| 176 | + environment: NodeJS.ProcessEnv, |
| 177 | +) { |
| 178 | + const fs = yield* FileSystem.FileSystem; |
| 179 | + const path = yield* Path.Path; |
| 180 | + const home = homePath.trim() |
| 181 | + ? expandHomePath(homePath) |
| 182 | + : environment.CODEX_HOME || path.join(environment.HOME || NodeOS.homedir(), ".codex"); |
| 183 | + const auth = yield* fs.readFileString(path.join(home, "auth.json")).pipe( |
| 184 | + Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(CodexAuthIdentity))), |
| 185 | + Effect.orElseSucceed(() => undefined), |
| 186 | + ); |
| 187 | + const accountId = auth?.tokens?.account_id?.trim(); |
| 188 | + if (accountId) return accountId; |
| 189 | + const idToken = auth?.tokens?.id_token; |
| 190 | + if (!idToken) return undefined; |
| 191 | + // This is grouping metadata, not authentication; the CLI verifies its tokens. |
| 192 | + const claims = yield* Effect.try(() => decodeJwt(idToken)).pipe( |
| 193 | + Effect.flatMap(Schema.decodeUnknownEffect(CodexIdentityClaims)), |
| 194 | + Effect.orElseSucceed(() => undefined), |
| 195 | + ); |
| 196 | + return claims?.["https://api.openai.com/auth"]?.chatgpt_account_id?.trim() || undefined; |
| 197 | +}); |
| 198 | + |
152 | 199 | export function mapCodexModelCapabilities( |
153 | 200 | model: CodexSchema.V2ModelListResponse__Model, |
154 | 201 | ): ModelCapabilities { |
@@ -444,6 +491,10 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun |
444 | 491 | } satisfies CodexAppServerProviderSnapshot; |
445 | 492 | } |
446 | 493 |
|
| 494 | + const accountId = |
| 495 | + !input.skipNativeUsage && accountResponse.account?.type === "chatgpt" |
| 496 | + ? yield* readCodexAccountId(input.homePath ?? "", input.environment ?? process.env) |
| 497 | + : undefined; |
447 | 498 | const [skillsResponse, models, rateLimits] = yield* Effect.all( |
448 | 499 | [ |
449 | 500 | client.request("skills/list", { |
@@ -478,6 +529,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun |
478 | 529 |
|
479 | 530 | return { |
480 | 531 | account: accountResponse, |
| 532 | + ...(accountId ? { accountId } : {}), |
481 | 533 | ...(rateLimits ? { rateLimits } : {}), |
482 | 534 | version, |
483 | 535 | models: applyPreferredCodexDefaultModel( |
@@ -584,14 +636,14 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu |
584 | 636 | }) => Effect.Effect< |
585 | 637 | CodexAppServerProviderSnapshot, |
586 | 638 | CodexErrors.CodexAppServerError, |
587 | | - ChildProcessSpawner.ChildProcessSpawner | Scope.Scope |
| 639 | + ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path | Scope.Scope |
588 | 640 | > = probeCodexAppServerProvider, |
589 | 641 | environment?: NodeJS.ProcessEnv, |
590 | 642 | managedAuth?: ServerProvider["auth"], |
591 | 643 | ): Effect.fn.Return< |
592 | 644 | ServerProviderDraft, |
593 | 645 | ServerSettingsError, |
594 | | - ChildProcessSpawner.ChildProcessSpawner |
| 646 | + ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path |
595 | 647 | > { |
596 | 648 | const resolvedEnvironment = environment ?? process.env; |
597 | 649 | const checkedAt = DateTime.formatIso(yield* DateTime.now); |
@@ -707,7 +759,10 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu |
707 | 759 | installed: true, |
708 | 760 | version: snapshot.version ?? null, |
709 | 761 | status: accountStatus.status, |
710 | | - auth: accountStatus.auth, |
| 762 | + auth: { |
| 763 | + ...accountStatus.auth, |
| 764 | + ...(snapshot.accountId ? { accountId: snapshot.accountId } : {}), |
| 765 | + }, |
711 | 766 | ...(accountStatus.message ? { message: accountStatus.message } : {}), |
712 | 767 | ...(managedAuth ? {} : { usageLimits }), |
713 | 768 | }, |
|
0 commit comments