@opengeni/codex 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ import type { CodexFetch } from "./device-code";
2
+ import { type CodexRateLimitResetConsumeResponse, type CodexRateLimitResetCreditsDetails } from "./reset-credits";
3
+ export type CodexAuthHeaders = {
4
+ accessToken: string;
5
+ chatgptAccountId: string | null;
6
+ isFedramp: boolean;
7
+ clientVersion: string;
8
+ };
9
+ export type ResetCreditFetchFailureReason = "http_error" | "invalid_response" | "network_error" | "timeout";
10
+ /** GET /codex/models — login-check + live catalog. A 200 means the token is accepted. spec §1.4/§F */
11
+ export declare function fetchCodexModels(a: CodexAuthHeaders, fetchImpl?: CodexFetch, timeoutMs?: number): Promise<{
12
+ ok: boolean;
13
+ status: number;
14
+ slugs: string[];
15
+ }>;
16
+ /** GET /wham/usage — authoritative limits. NB the WHAM base is /backend-api, NOT /codex (spec §1.8a). */
17
+ export declare function fetchCodexUsage(a: CodexAuthHeaders, fetchImpl?: CodexFetch, timeoutMs?: number): Promise<{
18
+ status: number;
19
+ payload: unknown;
20
+ }>;
21
+ /**
22
+ * GET /wham/rate-limit-reset-credits — detailed earned reset credits.
23
+ *
24
+ * A non-2xx or malformed body returns an explicit non-ok result. The caller may
25
+ * fall back to the count-only summary embedded in /wham/usage, but must never
26
+ * invent actionable rows from that count.
27
+ */
28
+ export declare function fetchCodexRateLimitResetCredits(a: CodexAuthHeaders, fetchImpl?: CodexFetch, timeoutMs?: number): Promise<{
29
+ ok: true;
30
+ status: number;
31
+ details: CodexRateLimitResetCreditsDetails;
32
+ } | {
33
+ ok: false;
34
+ status: number;
35
+ reason: ResetCreditFetchFailureReason;
36
+ }>;
37
+ /**
38
+ * POST /wham/rate-limit-reset-credits/consume with the exact v0.144.6 body.
39
+ * `idempotencyKey` identifies one logical human redemption and MUST be reused
40
+ * by the server on retries. Supplying `creditId` is preferred; omission leaves
41
+ * provider selection in control and is therefore not used by OpenGeni's
42
+ * human-only flow.
43
+ */
44
+ export declare function consumeCodexRateLimitResetCredit(a: CodexAuthHeaders, input: {
45
+ idempotencyKey: string;
46
+ creditId?: string | undefined;
47
+ }, fetchImpl?: CodexFetch, timeoutMs?: number): Promise<{
48
+ ok: true;
49
+ status: number;
50
+ result: CodexRateLimitResetConsumeResponse;
51
+ } | {
52
+ ok: false;
53
+ status: number;
54
+ reason: ResetCreditFetchFailureReason | "invalid_request";
55
+ }>;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Pure NECESSARY condition for a Codex-billed turn: the model id is namespaced
3
+ * for the Codex subscription provider (`codex/<slug>`).
4
+ *
5
+ * This is NOT sufficient to bypass billing — an active, connected workspace
6
+ * credential and the deployment flag (`settings.codexSubscriptionEnabled`) are
7
+ * ALSO required (see `isCodexBilledTurn`/`workspaceCodexSubscriptionActive` in
8
+ * `@opengeni/db`). Used only as a cheap, synchronous short-circuit so the common
9
+ * non-codex path never issues a credential read.
10
+ */
11
+ export declare function isCodexBilledModel(model: string | null | undefined): boolean;
@@ -0,0 +1,16 @@
1
+ export type CodexOperationFailureReason = "network_error" | "timeout";
2
+ /**
3
+ * Bound the complete provider operation, including response-body consumption.
4
+ *
5
+ * AbortController makes native fetch release its socket, while Promise.race is
6
+ * the backstop for injected/custom fetch implementations that ignore `signal`.
7
+ * The losing operation is rejection-handled and can never become an unhandled
8
+ * promise after the caller has received the timeout result.
9
+ */
10
+ export declare function runBoundedCodexOperation<T>(operation: (signal: AbortSignal) => Promise<T>, timeoutMs: number): Promise<{
11
+ ok: true;
12
+ value: T;
13
+ } | {
14
+ ok: false;
15
+ reason: CodexOperationFailureReason;
16
+ }>;
@@ -1,35 +1,33 @@
1
- declare const CODEX_ISSUER = "https://auth.openai.com";
2
- declare const CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann";
3
- declare const CODEX_AUTH_BASE = "https://auth.openai.com/api/accounts";
4
- declare const CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token";
5
- declare const CODEX_DEVICE_VERIFICATION_URL = "https://auth.openai.com/codex/device";
6
- declare const CODEX_DEVICE_REDIRECT_URI = "https://auth.openai.com/deviceauth/callback";
7
- declare const CODEX_RESPONSES_BASE = "https://chatgpt.com/backend-api/codex";
8
- declare const CODEX_WHAM_BASE = "https://chatgpt.com/backend-api";
9
- declare const CODEX_ORIGINATOR = "codex_cli_rs";
10
- declare const CODEX_ID_TOKEN_AUTH_CLAIM = "https://api.openai.com/auth";
11
- declare const CODEX_PROVIDER_ID = "codex-subscription";
12
- declare const CODEX_PROVIDER_BASE_URL = "https://chatgpt.com/backend-api";
13
- declare const CODEX_MODEL_ID_PREFIX = "codex/";
14
- declare const CODEX_FALLBACK_MODEL_SLUGS: readonly ["gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna"];
15
- declare const CODEX_MODEL_CONTEXT_WINDOW_TOKENS = 272000;
16
- declare const CODEX_EFFECTIVE_CONTEXT_WINDOW_PERCENT = 95;
17
- declare const CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS: number;
18
- declare const CODEX_AUTO_COMPACTION_PERCENT = 90;
19
- declare const CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT: number;
20
- declare const CODEX_CLIENT_VERSION = "0.144.6";
21
- declare const CODEX_REFRESH_WINDOW_MS: number;
22
- declare const CODEX_REFRESH_FALLBACK_MS: number;
23
- declare const CODEX_RESPONSE_HEADERS_TIMEOUT_MS: number;
24
- declare const CODEX_RESPONSE_STREAM_IDLE_TIMEOUT_MS: number;
25
- declare const CODEX_RESPONSE_WHOLE_TIMEOUT_MS: number;
26
- declare const CODEX_RESPONSE_NO_BYTE_RETRIES = 0;
27
- declare const CODEX_RESPONSE_RETRY_BACKOFF_MS = 1000;
28
- declare const CODEX_RESPONSE_SDK_OUTER_TIMEOUT_MS: number;
29
- declare const CODEX_APPS_MCP_SERVER_ID = "codex_apps";
30
- declare const CODEX_APPS_MCP_SERVER_NAME = "codex_apps";
31
- declare const CODEX_APPS_MCP_URL = "https://chatgpt.com/backend-api/ps/mcp";
32
- declare const CODEX_APPS_STARTUP_TIMEOUT_MS = 30000;
33
- declare const CODEX_APPS_REQUIRED_SCOPES: readonly ["api.connectors.read", "api.connectors.invoke"];
34
-
35
- export { CODEX_APPS_MCP_SERVER_ID, CODEX_APPS_MCP_SERVER_NAME, CODEX_APPS_MCP_URL, CODEX_APPS_REQUIRED_SCOPES, CODEX_APPS_STARTUP_TIMEOUT_MS, CODEX_AUTH_BASE, CODEX_AUTO_COMPACTION_PERCENT, CODEX_CLIENT_ID, CODEX_CLIENT_VERSION, CODEX_DEVICE_REDIRECT_URI, CODEX_DEVICE_VERIFICATION_URL, CODEX_EFFECTIVE_CONTEXT_WINDOW_PERCENT, CODEX_FALLBACK_MODEL_SLUGS, CODEX_ID_TOKEN_AUTH_CLAIM, CODEX_ISSUER, CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT, CODEX_MODEL_CONTEXT_WINDOW_TOKENS, CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS, CODEX_MODEL_ID_PREFIX, CODEX_ORIGINATOR, CODEX_PROVIDER_BASE_URL, CODEX_PROVIDER_ID, CODEX_REFRESH_FALLBACK_MS, CODEX_REFRESH_WINDOW_MS, CODEX_RESPONSES_BASE, CODEX_RESPONSE_HEADERS_TIMEOUT_MS, CODEX_RESPONSE_NO_BYTE_RETRIES, CODEX_RESPONSE_RETRY_BACKOFF_MS, CODEX_RESPONSE_SDK_OUTER_TIMEOUT_MS, CODEX_RESPONSE_STREAM_IDLE_TIMEOUT_MS, CODEX_RESPONSE_WHOLE_TIMEOUT_MS, CODEX_TOKEN_URL, CODEX_WHAM_BASE };
1
+ export declare const CODEX_ISSUER = "https://auth.openai.com";
2
+ export declare const CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann";
3
+ export declare const CODEX_AUTH_BASE = "https://auth.openai.com/api/accounts";
4
+ export declare const CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token";
5
+ export declare const CODEX_DEVICE_VERIFICATION_URL = "https://auth.openai.com/codex/device";
6
+ export declare const CODEX_DEVICE_REDIRECT_URI = "https://auth.openai.com/deviceauth/callback";
7
+ export declare const CODEX_RESPONSES_BASE = "https://chatgpt.com/backend-api/codex";
8
+ export declare const CODEX_WHAM_BASE = "https://chatgpt.com/backend-api";
9
+ export declare const CODEX_ORIGINATOR = "codex_cli_rs";
10
+ export declare const CODEX_ID_TOKEN_AUTH_CLAIM = "https://api.openai.com/auth";
11
+ export declare const CODEX_PROVIDER_ID = "codex-subscription";
12
+ export declare const CODEX_PROVIDER_BASE_URL = "https://chatgpt.com/backend-api";
13
+ export declare const CODEX_MODEL_ID_PREFIX = "codex/";
14
+ export declare const CODEX_FALLBACK_MODEL_SLUGS: readonly ["gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna"];
15
+ export declare const CODEX_MODEL_CONTEXT_WINDOW_TOKENS = 272000;
16
+ export declare const CODEX_EFFECTIVE_CONTEXT_WINDOW_PERCENT = 95;
17
+ export declare const CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS: number;
18
+ export declare const CODEX_AUTO_COMPACTION_PERCENT = 90;
19
+ export declare const CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT: number;
20
+ export declare const CODEX_CLIENT_VERSION = "0.144.6";
21
+ export declare const CODEX_REFRESH_WINDOW_MS: number;
22
+ export declare const CODEX_REFRESH_FALLBACK_MS: number;
23
+ export declare const CODEX_RESPONSE_HEADERS_TIMEOUT_MS: number;
24
+ export declare const CODEX_RESPONSE_STREAM_IDLE_TIMEOUT_MS: number;
25
+ export declare const CODEX_RESPONSE_WHOLE_TIMEOUT_MS: number;
26
+ export declare const CODEX_RESPONSE_NO_BYTE_RETRIES = 0;
27
+ export declare const CODEX_RESPONSE_RETRY_BACKOFF_MS = 1000;
28
+ export declare const CODEX_RESPONSE_SDK_OUTER_TIMEOUT_MS: number;
29
+ export declare const CODEX_APPS_MCP_SERVER_ID = "codex_apps";
30
+ export declare const CODEX_APPS_MCP_SERVER_NAME = "codex_apps";
31
+ export declare const CODEX_APPS_MCP_URL = "https://chatgpt.com/backend-api/ps/mcp";
32
+ export declare const CODEX_APPS_STARTUP_TIMEOUT_MS = 30000;
33
+ export declare const CODEX_APPS_REQUIRED_SCOPES: readonly ["api.connectors.read", "api.connectors.invoke"];
@@ -0,0 +1,36 @@
1
+ export type CodexFetch = (input: string | URL, init?: RequestInit) => Promise<Response>;
2
+ export type CodexDeviceStart = {
3
+ deviceAuthId: string;
4
+ userCode: string;
5
+ verificationUri: string;
6
+ intervalSeconds: number;
7
+ };
8
+ export type CodexTokens = {
9
+ idToken: string;
10
+ accessToken: string;
11
+ refreshToken: string;
12
+ };
13
+ export type CodexPollResult = {
14
+ status: "pending";
15
+ } | {
16
+ status: "expired";
17
+ } | {
18
+ status: "authorized";
19
+ authorizationCode: string;
20
+ codeVerifier: string;
21
+ };
22
+ export declare class CodexDeviceError extends Error {
23
+ constructor(message: string);
24
+ }
25
+ /** Step 1: POST {auth}/deviceauth/usercode {client_id}. device_code_auth.rs:67-95 */
26
+ export declare function startDeviceCode(fetchImpl?: CodexFetch): Promise<CodexDeviceStart>;
27
+ /** Step 3 (single, non-blocking): POST {auth}/deviceauth/token. 403/404 => pending. device_code_auth.rs:106-145 */
28
+ export declare function pollDeviceCode(input: {
29
+ deviceAuthId: string;
30
+ userCode: string;
31
+ }, fetchImpl?: CodexFetch): Promise<CodexPollResult>;
32
+ /** Step 4: POST {issuer}/oauth/token form-encoded grant_type=authorization_code. server.rs:732-766 */
33
+ export declare function exchangeDeviceCode(input: {
34
+ authorizationCode: string;
35
+ codeVerifier: string;
36
+ }, fetchImpl?: CodexFetch): Promise<CodexTokens>;
@@ -0,0 +1,84 @@
1
+ import { type CodexUsageHeaderSnapshot } from "./request-context";
2
+ export type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
3
+ /**
4
+ * Internal provenance marker copied onto buffered non-OK Codex responses.
5
+ * OpenAI's APIError preserves response headers, which lets the worker
6
+ * distinguish a model-provider refusal from an unrelated sandbox/MCP HTTP
7
+ * error that happened during the same Codex turn.
8
+ */
9
+ export declare const CODEX_TRANSPORT_ERROR_HEADER = "x-opengeni-codex-transport-error";
10
+ /** True only for an error produced from this Codex transport's non-OK response. */
11
+ export declare function isCodexTransportError(error: unknown): boolean;
12
+ export type CodexEncryptedArtifactRejection = {
13
+ status: 400;
14
+ kind: "encrypted_content_rejected";
15
+ };
16
+ /**
17
+ * Classify only the provider's definitive request rejection for an opaque
18
+ * reasoning artifact that it can no longer decrypt/parse. A Codex transport
19
+ * marker plus HTTP 400 proves this request was rejected before inference; the
20
+ * semantic match prevents unrelated malformed prompts from entering recovery.
21
+ */
22
+ export declare function classifyCodexEncryptedArtifactRejection(error: unknown): CodexEncryptedArtifactRejection | null;
23
+ /**
24
+ * Multi-account P4 (Part A): scrape the full usage snapshot the codex backend
25
+ * stamps on every `/codex/responses` response in `x-codex-primary-*` /
26
+ * `x-codex-secondary-*` headers (integer-identical to GET /wham/usage, for free).
27
+ *
28
+ * CRITICAL clobber-fix: return null unless BOTH windows expose a valid used-percent
29
+ * integer. recordCodexAccountUsage writes all five columns unconditionally, so a
30
+ * primary-only snapshot would null the weekly column. Both windows are always
31
+ * emitted together on `/codex/responses`; gating on both makes every write a full
32
+ * 5-column snapshot byte-identical to the poll path, and a malformed/absent header
33
+ * set simply no-ops (the /wham/usage poll fallback still covers it).
34
+ */
35
+ export declare function parseCodexUsageHeaders(headers: Headers): CodexUsageHeaderSnapshot | null;
36
+ export declare function codexSubscriptionFetch(base?: FetchLike): FetchLike;
37
+ /** The codex backend's hard-cap error type (ChatGPT/Codex usage limit reached). */
38
+ export declare const CODEX_USAGE_LIMIT_ERROR_TYPE = "usage_limit_reached";
39
+ export type CodexUsageLimitInfo = {
40
+ /** Seconds until the usage cap resets, when the backend reported it. */
41
+ resetsInSeconds: number | null;
42
+ };
43
+ /**
44
+ * Classify a thrown error as a ChatGPT/Codex usage-cap (429 usage_limit_reached)
45
+ * and extract the reset window. The SDK surfaces the codex backend's 429 as an
46
+ * OpenAI APIError whose `.type` (and `.error.type`) is `usage_limit_reached` and
47
+ * whose `.error.resets_in_seconds` carries the cap reset. Walks the cause chain
48
+ * and tolerates the message-only shape so it survives any SDK re-wrapping.
49
+ * Returns null for anything that is not a usage cap.
50
+ */
51
+ export declare function classifyCodexUsageLimitError(error: unknown): CodexUsageLimitInfo | null;
52
+ export type CodexSseFailureProjection = {
53
+ status: number;
54
+ error: {
55
+ type: string;
56
+ code: string;
57
+ message: string;
58
+ param?: string;
59
+ event_type?: string;
60
+ response_id?: string;
61
+ response_status?: string;
62
+ diagnostic_truncated?: true;
63
+ };
64
+ headers: Headers;
65
+ };
66
+ /**
67
+ * A provider terminal carried inside an accepted HTTP-200 stream. The OpenAI
68
+ * SDK cannot turn that late terminal into a non-2xx APIError because headers
69
+ * have already been accepted, so the body transform throws this equivalent
70
+ * bounded shape. Provider-supplied message/param text is intentionally absent:
71
+ * the worker may persist Error.message, while identifiers/classifications are
72
+ * sufficient for retry, compaction, and incident diagnostics.
73
+ */
74
+ export declare class CodexStreamingTerminalError extends Error {
75
+ readonly status: number;
76
+ readonly code: string;
77
+ readonly type: string;
78
+ readonly eventType?: string;
79
+ readonly responseId?: string;
80
+ readonly responseStatus?: string;
81
+ readonly headers: Headers;
82
+ readonly error: Record<string, unknown>;
83
+ constructor(projection: CodexSseFailureProjection, publicMessage: string);
84
+ }