@opengeni/codex 0.2.5 → 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.
- package/dist/api-client.d.ts +55 -0
- package/dist/billing.d.ts +11 -0
- package/dist/bounded-operation.d.ts +16 -0
- package/dist/{chunk-AMRVHBCD.js → chunk-YGKQDUY7.js} +13 -1
- package/dist/{chunk-AMRVHBCD.js.map → chunk-YGKQDUY7.js.map} +1 -1
- package/dist/constants.d.ts +33 -29
- package/dist/constants.js +13 -1
- package/dist/device-code.d.ts +36 -0
- package/dist/fetch.d.ts +84 -0
- package/dist/index.d.ts +13 -377
- package/dist/index.js +684 -68
- package/dist/index.js.map +1 -1
- package/dist/mcp-sanitize.d.ts +33 -0
- package/dist/model-output-truncation.d.ts +36 -0
- package/dist/normalize.d.ts +8 -0
- package/dist/oversized-image-card.d.ts +17 -0
- package/dist/refresh.d.ts +28 -0
- package/dist/request-context.d.ts +99 -0
- package/dist/reset-credits.d.ts +33 -0
- package/dist/response-timeout.d.ts +28 -0
- package/dist/usage-normalize.d.ts +61 -0
- package/package.json +3 -3
- package/src/api-client.ts +131 -22
- package/src/bounded-operation.ts +43 -0
- package/src/constants.ts +18 -0
- package/src/fetch.ts +416 -11
- package/src/index.ts +2 -0
- package/src/normalize.ts +6 -3
- package/src/refresh.ts +21 -10
- package/src/request-context.ts +58 -0
- package/src/reset-credits.ts +157 -0
- package/src/response-timeout.ts +159 -0
- package/src/usage-normalize.ts +20 -2
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { FetchLike } from "./fetch";
|
|
2
|
+
/**
|
|
3
|
+
* Maps connector tool names to a Responses-API-legal charset and back. One
|
|
4
|
+
* instance per codex_apps transport (i.e. per turn): tools/list populates it,
|
|
5
|
+
* tools/call reads it. Idempotent across repeat listings.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ToolNameMapper {
|
|
8
|
+
private readonly sanitizedToOriginal;
|
|
9
|
+
private readonly used;
|
|
10
|
+
/** Return a legal, unique name (<= EFFECTIVE_MAX_TOOL_NAME_LEN, so `<prefix>__name` <= 64) for `original`, recording the reverse mapping. */
|
|
11
|
+
sanitize(original: string): string;
|
|
12
|
+
/** Reverse a sanitized name back to the MCP server's original, if known. */
|
|
13
|
+
toOriginal(sanitized: string): string | undefined;
|
|
14
|
+
}
|
|
15
|
+
/** Sanitize a single JSON body (application/json MCP response). */
|
|
16
|
+
export declare function sanitizeMcpJsonBody(text: string, mapper?: ToolNameMapper, namespaceSink?: Set<string>): string;
|
|
17
|
+
/** Sanitize an SSE body: each JSON-RPC message rides on a `data:` line. */
|
|
18
|
+
export declare function sanitizeMcpSseBody(text: string, mapper?: ToolNameMapper, namespaceSink?: Set<string>): string;
|
|
19
|
+
/** Reverse a sanitized tools/call name back to the original; returns null if no rewrite is needed. */
|
|
20
|
+
export declare function remapToolCallRequestBody(body: string, mapper: ToolNameMapper): string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Wrap a base fetch so the codex_apps MCP transport is Responses-API-compatible:
|
|
23
|
+
* tools/list responses get their names sanitized + bad outputSchemas dropped (and
|
|
24
|
+
* the name mapping recorded), and tools/call requests get their name reversed back
|
|
25
|
+
* to the MCP server's original. Only the POST request/response is buffered; the
|
|
26
|
+
* long-lived GET notification SSE stream is passed through untouched.
|
|
27
|
+
*
|
|
28
|
+
* P4 (Part B.1): an optional `namespaceSink` Set accumulates the ORIGINAL-dotted
|
|
29
|
+
* connector namespaces seen across every tools/list this turn (captured before the
|
|
30
|
+
* dot is sanitized away). The worker reads the (live, by-reference) Set after the
|
|
31
|
+
* turn to cache the serving account's connector set — packages/codex stays db-free.
|
|
32
|
+
*/
|
|
33
|
+
export declare function codexAppsSanitizingFetch(base?: FetchLike, namespaceSink?: Set<string>): FetchLike;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical model-facing tool-output truncation.
|
|
3
|
+
*
|
|
4
|
+
* Ported from openai/codex `rust-v0.144.6` (commit
|
|
5
|
+
* 5d1fbf26c43abc65a203928b2e31561cb039e06d):
|
|
6
|
+
*
|
|
7
|
+
* - `codex-rs/utils/string/src/truncate.rs`
|
|
8
|
+
* - `codex-rs/utils/output-truncation/src/lib.rs`
|
|
9
|
+
* - `codex-rs/core/src/context_manager/history.rs`
|
|
10
|
+
*
|
|
11
|
+
* The live gpt-5.6 model catalog declares a 10,000-token truncation policy.
|
|
12
|
+
* Codex applies a 1.2x allowance before serializing a function-call output, so
|
|
13
|
+
* the effective textual payload budget is 12,000 approximate tokens. Images,
|
|
14
|
+
* files, and encrypted content are preserved; textual content shares one
|
|
15
|
+
* sequential budget and carries an explicit head/tail truncation marker.
|
|
16
|
+
*
|
|
17
|
+
* This module deliberately has no database or Agents SDK dependency. Both the
|
|
18
|
+
* runtime request seam and the database history boundary call the same pure
|
|
19
|
+
* function, so replayed conversation truth is identical to live model input.
|
|
20
|
+
*/
|
|
21
|
+
export { MODEL_TOOL_OUTPUT_OVERSIZED_IMAGE_CARD_DATA_URL } from "./oversized-image-card";
|
|
22
|
+
export type ModelHistoryItem = Record<string, unknown>;
|
|
23
|
+
export declare const CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS = 10000;
|
|
24
|
+
export declare const CODEX_TOOL_OUTPUT_SERIALIZATION_ALLOWANCE = 1.2;
|
|
25
|
+
export declare const DEFAULT_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS = 10000;
|
|
26
|
+
export declare const MODEL_TOOL_OUTPUT_OPAQUE_PAYLOAD_MAX_BYTES: number;
|
|
27
|
+
export declare function modelToolOutputSerializationBudgetTokens(policyTokens?: number): number;
|
|
28
|
+
export declare function approximateTokenCount(value: string): number;
|
|
29
|
+
/** Exact Codex-style middle truncation for a token policy. */
|
|
30
|
+
export declare function truncateMiddleWithTokenBudget(value: string, maxTokens: number): string;
|
|
31
|
+
/**
|
|
32
|
+
* Bound every model-visible tool-result item. Non-result items are returned by
|
|
33
|
+
* reference. Result items are cloned only when their textual output changes.
|
|
34
|
+
*/
|
|
35
|
+
export declare function boundModelToolOutputItem<T extends ModelHistoryItem>(item: T, policyTokens?: number): T;
|
|
36
|
+
export declare function boundModelToolOutputItems<T extends ModelHistoryItem>(items: readonly T[], policyTokens?: number): T[];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Mutates a parsed Responses request body in place and returns it. Pure + synchronous + unit-testable. */
|
|
2
|
+
export declare function normalizeCodexRequestBody(body: Record<string, unknown>, resolveModel: (slug: string) => string): Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Build a longest-prefix model resolver. Catalog slugs come from GET /models
|
|
5
|
+
* (api-client.ts). One leading `namespace/` segment is stripped first; an
|
|
6
|
+
* unknown slug returns the fallback (caller should log — spec §1.4 step 4).
|
|
7
|
+
*/
|
|
8
|
+
export declare function buildModelResolver(liveSlugs: readonly string[], fallbackSlug: string): (slug: string) => string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol-valid image shown to the model when an inline tool image exceeds the
|
|
3
|
+
* hard model-input allowance. The image channel cannot carry a text marker, so
|
|
4
|
+
* the omission itself is rendered as a legible PNG instead of corrupting the
|
|
5
|
+
* original base64 or pretending the placeholder is the real screenshot.
|
|
6
|
+
*
|
|
7
|
+
* Generated with the dependency-free bitmap/PNG encoder documented in
|
|
8
|
+
* `scripts/gen-screenshot-error-card.mjs`; the source image is 1,076x284 RGBA
|
|
9
|
+
* and 5,255 bytes. Rendered text:
|
|
10
|
+
*
|
|
11
|
+
* SCREEN CAPTURE OMITTED
|
|
12
|
+
* THE SCREEN CAPTURE IS TOO LARGE.
|
|
13
|
+
* THIS IS A PLACEHOLDER, NOT THE REAL SCREEN.
|
|
14
|
+
* DO NOT SAY THIS IS THE REAL SCREEN.
|
|
15
|
+
* TELL THE USER TO TAKE A SMALLER CAPTURE.
|
|
16
|
+
*/
|
|
17
|
+
export declare const MODEL_TOOL_OUTPUT_OVERSIZED_IMAGE_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NQcOG5ffjQChoWH8cCAUNQcP5TdBwPrR/CRoIGjZEB1IXWkHjY/NLTz1/QSPT+tN9PjsQCxrOb31+74KG/avSfPaSUUFD0BA0BA2/H0HD78v4CBqChqAhaNi/BA37l6CBoGHDEjQEDUHDgVDQsP4IGoKGoCFoCBqChv1L0EDQEDRcKAQNQUPQEDQcCK1vgoagIWgIGoKG/UvQEDQy/2BXdft8F4pZ47/7+0aPx+nflwv72flj/cl1Yay2vlX//Z5eP83P3p9fff+qNj7V17/T58Pq+5egIWgIGoKGoCFoCBqChvVH0BA0BA1Bw/4laAgaggaChqAhaDjwCxoOhIKGoCFoCBrmp6AhaAgagoaggaAhaLhQCBqChqAhaAgagoagIWgIGoKGoCFoCBqCBgAAAICgAQAAACBoAAAAAIIGAAAAgKABAAAAIGgAAAAAggYAAACAoAEAAAAgaAAAAACCBgAAAICgAQAAACBoAAAAAAgaAAAAgKABAAAAIGgAAAAACBoAAACAoAEAAAAgaAAAAAAIGgAAAICgAQAAACBoAAAAAAgaAAAAgKABAAAAIGgAAAAAbPAXSdffkpULfXUAAAAASUVORK5CYII=";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CodexFetch } from "./device-code";
|
|
2
|
+
/** Permanent — the workspace must reconnect (status => needs_relogin). */
|
|
3
|
+
export declare class CodexReloginRequired extends Error {
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
/** Transient — safe to retry later. */
|
|
7
|
+
export declare class CodexRefreshTransient extends Error {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
/** Only present fields are returned (the server may rotate any subset). */
|
|
11
|
+
export type CodexRefreshTokens = {
|
|
12
|
+
idToken?: string | undefined;
|
|
13
|
+
accessToken?: string | undefined;
|
|
14
|
+
refreshToken?: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
/** POST {issuer}/oauth/token JSON {client_id, grant_type:"refresh_token", refresh_token}. manager.rs:1336-1340 */
|
|
17
|
+
export declare function refreshCodexToken(refreshToken: string, fetchImpl?: CodexFetch, timeoutMs?: number): Promise<CodexRefreshTokens>;
|
|
18
|
+
/** Decode a JWT payload (base64url, no signature check). */
|
|
19
|
+
export declare function decodeJwtPayload(jwt: string): Record<string, unknown> | null;
|
|
20
|
+
/** access-token `exp` claim -> Date | null. token_data.rs:101-105 */
|
|
21
|
+
export declare function accessTokenExpiry(accessToken: string): Date | null;
|
|
22
|
+
/** id_token -> {chatgptAccountId, planType, isFedramp}. server.rs:827-832; token_data.rs:71-99 */
|
|
23
|
+
export declare function parseIdToken(idToken: string): {
|
|
24
|
+
chatgptAccountId: string | null;
|
|
25
|
+
planType: string | null;
|
|
26
|
+
isFedramp: boolean;
|
|
27
|
+
email: string | null;
|
|
28
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
export type CodexTokenSnapshot = {
|
|
3
|
+
accessToken: string;
|
|
4
|
+
chatgptAccountId: string | null;
|
|
5
|
+
isFedramp: boolean;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Multi-account P4 (Part A): a full usage snapshot scraped FOR FREE from the
|
|
9
|
+
* `x-codex-primary-*` / `x-codex-secondary-*` response headers the codex backend
|
|
10
|
+
* stamps on every `/codex/responses` turn (success AND 429 hard-cap). Integer-
|
|
11
|
+
* identical to GET /wham/usage but with zero extra round-trip. parseCodexUsageHeaders
|
|
12
|
+
* returns this only when BOTH windows parse, so a write is always a full 5-column
|
|
13
|
+
* snapshot (no partial-window clobber). Shape mirrors db's CodexAccountUsageSnapshot
|
|
14
|
+
* (non-null here: a partial read is filtered to null upstream, never half-written).
|
|
15
|
+
*/
|
|
16
|
+
export type CodexUsageHeaderSnapshot = {
|
|
17
|
+
primaryUsedPercent: number;
|
|
18
|
+
primaryResetAt: Date;
|
|
19
|
+
secondaryUsedPercent: number;
|
|
20
|
+
secondaryResetAt: Date;
|
|
21
|
+
checkedAt: Date;
|
|
22
|
+
};
|
|
23
|
+
export type CodexResponseTimeoutClass = "connect" | "headers" | "idle_stream" | "whole_request";
|
|
24
|
+
export type CodexResponseTimeoutPolicy = {
|
|
25
|
+
/** Maximum wait for response headers, including DNS/TCP/TLS establishment. */
|
|
26
|
+
headersTimeoutMs: number;
|
|
27
|
+
/** Maximum silence between response-body chunks after headers arrive. */
|
|
28
|
+
streamIdleTimeoutMs: number;
|
|
29
|
+
/** Maximum wall time for one logical Responses request. */
|
|
30
|
+
wholeRequestTimeoutMs: number;
|
|
31
|
+
/**
|
|
32
|
+
* Reserved compatibility field. It is currently normalized to zero because
|
|
33
|
+
* an absent response does not prove that the provider never accepted a
|
|
34
|
+
* request, so automatic replay is not safe without an operation receipt.
|
|
35
|
+
*/
|
|
36
|
+
noByteRetries: number;
|
|
37
|
+
retryBackoffMs: number;
|
|
38
|
+
};
|
|
39
|
+
export type CodexModelRequestEvent = {
|
|
40
|
+
requestId: string;
|
|
41
|
+
transportAttempt: number;
|
|
42
|
+
phase: "started" | "headers" | "first_byte" | "completed" | "failed" | "timed_out";
|
|
43
|
+
model?: string;
|
|
44
|
+
durationMs: number;
|
|
45
|
+
responseObserved: boolean;
|
|
46
|
+
timeoutPolicy: CodexResponseTimeoutPolicy;
|
|
47
|
+
timeoutClass?: CodexResponseTimeoutClass;
|
|
48
|
+
providerRequestId?: string;
|
|
49
|
+
status?: number;
|
|
50
|
+
willRetry?: boolean;
|
|
51
|
+
};
|
|
52
|
+
export type CodexRequestContext = {
|
|
53
|
+
clientVersion: string;
|
|
54
|
+
/**
|
|
55
|
+
* Stable per-session affinity id, sent as the `session_id` header on every
|
|
56
|
+
* request. This is the backend's STICKY CACHE-ROUTING key — measured
|
|
57
|
+
* 2026-07-12 with byte-identical ~99k-token gpt-5.6-sol requests on one idle
|
|
58
|
+
* account: without the header, repeat requests hit the prompt cache ~50% of
|
|
59
|
+
* the time (a per-request routing lottery across cache shards; matches the
|
|
60
|
+
* prod fleet's 48.6%); with a stable session_id, 10/10 requests hit at the
|
|
61
|
+
* 99.0% ceiling — Codex CLI parity (the CLI always sends it; its own last-3d
|
|
62
|
+
* token-weighted rate here is 94%). `prompt_cache_key` in the body only
|
|
63
|
+
* influences routing and does NOT pin it. Use the SAME value as
|
|
64
|
+
* prompt_cache_key (the OpenGeni sessionId) so routing and cache key agree.
|
|
65
|
+
*/
|
|
66
|
+
sessionId?: string;
|
|
67
|
+
/** Worker-supplied: proactive refresh + single-flight + db persist. */
|
|
68
|
+
getToken: () => Promise<CodexTokenSnapshot>;
|
|
69
|
+
/** Forced refresh used for the 401 retry. */
|
|
70
|
+
refresh: () => Promise<CodexTokenSnapshot>;
|
|
71
|
+
/** Model-slug resolver (longest-prefix against the live catalog). */
|
|
72
|
+
resolveModel: (slug: string) => string;
|
|
73
|
+
/**
|
|
74
|
+
* Multi-account P4 (Part A): fire-and-forget usage-header sink. Called by
|
|
75
|
+
* codexSubscriptionFetch on EVERY response (sync, non-throwing, never awaited)
|
|
76
|
+
* with the parsed full-window snapshot. The worker records the latest into the
|
|
77
|
+
* P2 usage cache once per turn in its `finally` — packages/codex stays db-free.
|
|
78
|
+
*/
|
|
79
|
+
onUsageHeaders?: (snapshot: CodexUsageHeaderSnapshot) => void;
|
|
80
|
+
/** Optional per-run override, primarily for deterministic transport tests. */
|
|
81
|
+
responseTimeoutPolicy?: Partial<CodexResponseTimeoutPolicy>;
|
|
82
|
+
/** Worker-owned durable audit sink; payloads never contain request bodies or auth. */
|
|
83
|
+
onModelRequestEvent?: (event: CodexModelRequestEvent) => Promise<void> | void;
|
|
84
|
+
/** Stable request identity supplied by the owning durable execution. */
|
|
85
|
+
nextRequestId?: () => string;
|
|
86
|
+
/**
|
|
87
|
+
* Optional Codex beta feature flags advertised as `x-codex-beta-features`
|
|
88
|
+
* (comma-separated). Used for remote compaction v2 (`remote_compaction_v2`).
|
|
89
|
+
*/
|
|
90
|
+
betaFeatures?: readonly string[];
|
|
91
|
+
/**
|
|
92
|
+
* Optional turn analytics / routing metadata sent as `x-codex-turn-metadata`
|
|
93
|
+
* (JSON). Body `metadata` is stripped by normalize — never put request_kind there.
|
|
94
|
+
*/
|
|
95
|
+
turnMetadata?: Record<string, unknown>;
|
|
96
|
+
};
|
|
97
|
+
export declare const codexRequestStorage: AsyncLocalStorage<CodexRequestContext>;
|
|
98
|
+
/** Nest a Codex ALS scope with header overrides (e.g. remote compaction v2). */
|
|
99
|
+
export declare function withCodexRequestOverrides<T>(overrides: Pick<CodexRequestContext, "betaFeatures" | "turnMetadata">, fn: () => T): T;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const CODEX_RATE_LIMIT_RESET_OUTCOMES: readonly ["reset", "nothingToReset", "noCredit", "alreadyRedeemed"];
|
|
2
|
+
export type CodexRateLimitResetOutcome = (typeof CODEX_RATE_LIMIT_RESET_OUTCOMES)[number];
|
|
3
|
+
export type CodexRateLimitResetType = "codexRateLimits" | "unknown";
|
|
4
|
+
export type CodexRateLimitResetCreditStatus = "available" | "redeeming" | "redeemed" | "unknown";
|
|
5
|
+
export type CodexRateLimitResetCredit = {
|
|
6
|
+
id: string;
|
|
7
|
+
resetType: CodexRateLimitResetType;
|
|
8
|
+
status: CodexRateLimitResetCreditStatus;
|
|
9
|
+
/** Unix seconds, matching account/rateLimits/read in Codex v0.144.6. */
|
|
10
|
+
grantedAt: number;
|
|
11
|
+
/** Unix seconds, or null when the provider says the credit does not expire. */
|
|
12
|
+
expiresAt: number | null;
|
|
13
|
+
title: string | null;
|
|
14
|
+
description: string | null;
|
|
15
|
+
};
|
|
16
|
+
export type CodexRateLimitResetCreditsDetails = {
|
|
17
|
+
availableCount: number;
|
|
18
|
+
credits: CodexRateLimitResetCredit[];
|
|
19
|
+
};
|
|
20
|
+
export type CodexRateLimitResetCreditsSummary = {
|
|
21
|
+
availableCount: number;
|
|
22
|
+
/** null means the provider supplied an authoritative count but no detail rows. */
|
|
23
|
+
credits: null;
|
|
24
|
+
};
|
|
25
|
+
export type CodexRateLimitResetConsumeResponse = {
|
|
26
|
+
outcome: CodexRateLimitResetOutcome;
|
|
27
|
+
};
|
|
28
|
+
/** Parse the exact detailed-credit backend response. Unknown rows stay view-only. */
|
|
29
|
+
export declare function parseCodexRateLimitResetCreditsDetails(payload: unknown): CodexRateLimitResetCreditsDetails | null;
|
|
30
|
+
/** Parse the count-only summary carried by GET /wham/usage. */
|
|
31
|
+
export declare function parseCodexRateLimitResetCreditsSummary(payload: unknown): CodexRateLimitResetCreditsSummary | null;
|
|
32
|
+
/** Parse one of the exact four v0.144.6 consume outcomes. Unknowns fail closed. */
|
|
33
|
+
export declare function parseCodexRateLimitResetConsumeResponse(payload: unknown): CodexRateLimitResetConsumeResponse | null;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CodexResponseTimeoutClass, CodexResponseTimeoutPolicy } from "./request-context";
|
|
2
|
+
export declare const CODEX_RESPONSE_TIMEOUT_ERROR_TYPE = "opengeni_codex_response_timeout";
|
|
3
|
+
export declare const DEFAULT_CODEX_RESPONSE_TIMEOUT_POLICY: CodexResponseTimeoutPolicy;
|
|
4
|
+
export declare function resolveCodexResponseTimeoutPolicy(override: Partial<CodexResponseTimeoutPolicy> | undefined): CodexResponseTimeoutPolicy;
|
|
5
|
+
export declare class CodexResponseTimeoutError extends Error {
|
|
6
|
+
readonly timeoutClass: CodexResponseTimeoutClass;
|
|
7
|
+
readonly requestId: string;
|
|
8
|
+
readonly responseObserved: boolean;
|
|
9
|
+
readonly code = "opengeni_codex_response_timeout";
|
|
10
|
+
readonly type = "opengeni_codex_response_timeout";
|
|
11
|
+
constructor(timeoutClass: CodexResponseTimeoutClass, requestId: string, responseObserved: boolean, message?: string);
|
|
12
|
+
}
|
|
13
|
+
export type CodexResponseTimeoutInfo = {
|
|
14
|
+
timeoutClass: CodexResponseTimeoutClass;
|
|
15
|
+
requestId: string | null;
|
|
16
|
+
responseObserved: boolean;
|
|
17
|
+
message: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Recover structured transport timeouts through SDK wrapping. The optional
|
|
21
|
+
* legacy match is deliberately opt-in: `Request timed out.` alone has no
|
|
22
|
+
* provider provenance and the worker enables it only for a confirmed Codex
|
|
23
|
+
* subscription turn.
|
|
24
|
+
*/
|
|
25
|
+
export declare function classifyCodexResponseTimeoutError(error: unknown, options?: {
|
|
26
|
+
allowLegacyRequestTimeout?: boolean;
|
|
27
|
+
}): CodexResponseTimeoutInfo | null;
|
|
28
|
+
export declare function isPreHeadersTimeoutError(error: unknown): CodexResponseTimeoutClass | null;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type CodexRateLimitResetCreditsSummary } from "./reset-credits";
|
|
2
|
+
/** The 5-hour (primary) window's `limit_window_seconds`. */
|
|
3
|
+
export declare const CODEX_FIVE_HOUR_WINDOW_SECONDS = 18000;
|
|
4
|
+
/** The weekly (secondary) window's `limit_window_seconds`. */
|
|
5
|
+
export declare const CODEX_WEEKLY_WINDOW_SECONDS = 604800;
|
|
6
|
+
/** One normalized usage window (applied to BOTH primary_window and secondary_window). */
|
|
7
|
+
export type CodexUsageWindow = {
|
|
8
|
+
used: number;
|
|
9
|
+
limit: number;
|
|
10
|
+
remaining: number;
|
|
11
|
+
percent: number;
|
|
12
|
+
resetAt: string | null;
|
|
13
|
+
resetAfterSeconds: number | null;
|
|
14
|
+
limitWindowSeconds: number;
|
|
15
|
+
};
|
|
16
|
+
/** One additional (per-feature) limit (forward-compat; P2 renders nothing from it). */
|
|
17
|
+
export type CodexAdditionalLimit = {
|
|
18
|
+
limitName: string;
|
|
19
|
+
meteredFeature: string;
|
|
20
|
+
fiveHour: CodexUsageWindow | null;
|
|
21
|
+
weekly: CodexUsageWindow | null;
|
|
22
|
+
};
|
|
23
|
+
export type CodexUsageStatus = "ok" | "limit_reached" | "error" | "no-data";
|
|
24
|
+
/** The normalized usage payload — the P2/P3 contract. */
|
|
25
|
+
export type CodexUsagePayload = {
|
|
26
|
+
status: CodexUsageStatus;
|
|
27
|
+
planType: string | null;
|
|
28
|
+
fiveHour: CodexUsageWindow | null;
|
|
29
|
+
weekly: CodexUsageWindow | null;
|
|
30
|
+
limitReached: boolean;
|
|
31
|
+
fetchedAt: string;
|
|
32
|
+
/**
|
|
33
|
+
* Authoritative count-only reset-credit summary from the usage response.
|
|
34
|
+
* Detail rows are fetched separately and are never synthesized from this.
|
|
35
|
+
*/
|
|
36
|
+
rateLimitResetCredits: CodexRateLimitResetCreditsSummary | null;
|
|
37
|
+
/** Present only on a refresh/auth failure path; carries the precise reason. */
|
|
38
|
+
reason?: "needs_relogin" | undefined;
|
|
39
|
+
additionalLimits?: CodexAdditionalLimit[] | undefined;
|
|
40
|
+
credits?: {
|
|
41
|
+
hasCredits: boolean;
|
|
42
|
+
unlimited: boolean;
|
|
43
|
+
overageLimitReached: boolean;
|
|
44
|
+
balance: string;
|
|
45
|
+
} | undefined;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Build a normalized window from the PERSISTED cache columns (used_percent +
|
|
49
|
+
* absolute reset timestamp). The same 0–100 synthesis as the live path, with the
|
|
50
|
+
* skew-free countdown derived from `resetAt − now` at read time. Returns null when
|
|
51
|
+
* there is no cached percent yet. `limitWindowSeconds` is the constant that
|
|
52
|
+
* identifies the window (18000 ⇒ 5h, 604800 ⇒ weekly).
|
|
53
|
+
*/
|
|
54
|
+
export declare function buildCodexUsageWindowFromCache(usedPercent: number | null | undefined, resetAt: Date | string | null | undefined, limitWindowSeconds: number): CodexUsageWindow | null;
|
|
55
|
+
/**
|
|
56
|
+
* Normalize a /wham/usage fetch result into the P2/P3 contract.
|
|
57
|
+
*
|
|
58
|
+
* @param httpStatus the HTTP status from fetchCodexUsage (404 ⇒ a limit body)
|
|
59
|
+
* @param rawPayload the parsed JSON body (or null when the body was unreadable)
|
|
60
|
+
*/
|
|
61
|
+
export declare function normalizeCodexUsage(httpStatus: number, rawPayload: unknown): CodexUsagePayload;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengeni/codex",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "ChatGPT/Codex subscription auth + transport: device-code login, token refresh, and the Responses-backend fetch. Pure HTTP + transforms; no database dependency.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"provenance": true
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
|
-
"typecheck": "
|
|
36
|
-
"build": "
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"build": "bun ../../scripts/build-typescript-package.ts",
|
|
37
37
|
"prepublishOnly": "bash ../../scripts/prepublish-guard"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
package/src/api-client.ts
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
|
|
4
4
|
import { CODEX_ORIGINATOR, CODEX_RESPONSES_BASE, CODEX_WHAM_BASE } from "./constants";
|
|
5
5
|
import type { CodexFetch } from "./device-code";
|
|
6
|
+
import {
|
|
7
|
+
parseCodexRateLimitResetConsumeResponse,
|
|
8
|
+
parseCodexRateLimitResetCreditsDetails,
|
|
9
|
+
type CodexRateLimitResetConsumeResponse,
|
|
10
|
+
type CodexRateLimitResetCreditsDetails,
|
|
11
|
+
} from "./reset-credits";
|
|
12
|
+
import { runBoundedCodexOperation } from "./bounded-operation";
|
|
6
13
|
|
|
7
14
|
export type CodexAuthHeaders = {
|
|
8
15
|
accessToken: string;
|
|
@@ -11,6 +18,16 @@ export type CodexAuthHeaders = {
|
|
|
11
18
|
clientVersion: string;
|
|
12
19
|
};
|
|
13
20
|
|
|
21
|
+
const CODEX_READ_TIMEOUT_MS = 5_000;
|
|
22
|
+
const RESET_CREDIT_DETAILS_TIMEOUT_MS = 5_000;
|
|
23
|
+
const RESET_CREDIT_CONSUME_TIMEOUT_MS = 10_000;
|
|
24
|
+
|
|
25
|
+
export type ResetCreditFetchFailureReason =
|
|
26
|
+
| "http_error"
|
|
27
|
+
| "invalid_response"
|
|
28
|
+
| "network_error"
|
|
29
|
+
| "timeout";
|
|
30
|
+
|
|
14
31
|
function subscriptionHeaders(a: CodexAuthHeaders): Record<string, string> {
|
|
15
32
|
return {
|
|
16
33
|
Authorization: `Bearer ${a.accessToken}`,
|
|
@@ -26,34 +43,126 @@ function subscriptionHeaders(a: CodexAuthHeaders): Record<string, string> {
|
|
|
26
43
|
export async function fetchCodexModels(
|
|
27
44
|
a: CodexAuthHeaders,
|
|
28
45
|
fetchImpl: CodexFetch = fetch,
|
|
46
|
+
timeoutMs = CODEX_READ_TIMEOUT_MS,
|
|
29
47
|
): Promise<{ ok: boolean; status: number; slugs: string[] }> {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
method: "GET",
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
const fetched = await runBoundedCodexOperation(async (signal) => {
|
|
49
|
+
const res = await fetchImpl(
|
|
50
|
+
`${CODEX_RESPONSES_BASE}/models?client_version=${encodeURIComponent(a.clientVersion)}`,
|
|
51
|
+
{ method: "GET", headers: subscriptionHeaders(a), signal },
|
|
52
|
+
);
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
await res.arrayBuffer().catch(() => undefined);
|
|
55
|
+
return { ok: false, status: res.status, slugs: [] as string[] };
|
|
56
|
+
}
|
|
57
|
+
const body = (await res.json()) as { models?: Array<{ slug?: string }> };
|
|
58
|
+
const slugs = (body.models ?? [])
|
|
59
|
+
.map((model) => model.slug)
|
|
60
|
+
.filter((slug): slug is string => typeof slug === "string");
|
|
61
|
+
return { ok: true, status: res.status, slugs };
|
|
62
|
+
}, timeoutMs);
|
|
63
|
+
return fetched.ok ? fetched.value : { ok: false, status: 0, slugs: [] };
|
|
45
64
|
}
|
|
46
65
|
|
|
47
66
|
/** GET /wham/usage — authoritative limits. NB the WHAM base is /backend-api, NOT /codex (spec §1.8a). */
|
|
48
67
|
export async function fetchCodexUsage(
|
|
49
68
|
a: CodexAuthHeaders,
|
|
50
69
|
fetchImpl: CodexFetch = fetch,
|
|
70
|
+
timeoutMs = CODEX_READ_TIMEOUT_MS,
|
|
51
71
|
): Promise<{ status: number; payload: unknown }> {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
72
|
+
const fetched = await runBoundedCodexOperation(async (signal) => {
|
|
73
|
+
const res = await fetchImpl(`${CODEX_WHAM_BASE}/wham/usage`, {
|
|
74
|
+
method: "GET",
|
|
75
|
+
headers: subscriptionHeaders(a),
|
|
76
|
+
signal,
|
|
77
|
+
});
|
|
78
|
+
// A 404 may carry a usage-limit body; the route layer normalizes it to a limits state (spec §1.8c).
|
|
79
|
+
const payload = res.ok || res.status === 404 ? await res.json().catch(() => null) : null;
|
|
80
|
+
if (!res.ok && res.status !== 404) await res.arrayBuffer().catch(() => undefined);
|
|
81
|
+
return { status: res.status, payload };
|
|
82
|
+
}, timeoutMs);
|
|
83
|
+
if (!fetched.ok) throw new Error(`Codex usage request ${fetched.reason}`);
|
|
84
|
+
return fetched.value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* GET /wham/rate-limit-reset-credits — detailed earned reset credits.
|
|
89
|
+
*
|
|
90
|
+
* A non-2xx or malformed body returns an explicit non-ok result. The caller may
|
|
91
|
+
* fall back to the count-only summary embedded in /wham/usage, but must never
|
|
92
|
+
* invent actionable rows from that count.
|
|
93
|
+
*/
|
|
94
|
+
export async function fetchCodexRateLimitResetCredits(
|
|
95
|
+
a: CodexAuthHeaders,
|
|
96
|
+
fetchImpl: CodexFetch = fetch,
|
|
97
|
+
timeoutMs = RESET_CREDIT_DETAILS_TIMEOUT_MS,
|
|
98
|
+
): Promise<
|
|
99
|
+
| { ok: true; status: number; details: CodexRateLimitResetCreditsDetails }
|
|
100
|
+
| { ok: false; status: number; reason: ResetCreditFetchFailureReason }
|
|
101
|
+
> {
|
|
102
|
+
const fetched = await runBoundedCodexOperation(async (signal) => {
|
|
103
|
+
const res = await fetchImpl(`${CODEX_WHAM_BASE}/wham/rate-limit-reset-credits`, {
|
|
104
|
+
method: "GET",
|
|
105
|
+
headers: subscriptionHeaders(a),
|
|
106
|
+
signal,
|
|
107
|
+
});
|
|
108
|
+
if (!res.ok) {
|
|
109
|
+
// Drain the body without retaining/logging it. Provider error bodies may
|
|
110
|
+
// contain account-specific details and are not part of this contract.
|
|
111
|
+
await res.arrayBuffer().catch(() => undefined);
|
|
112
|
+
return { ok: false as const, status: res.status, reason: "http_error" as const };
|
|
113
|
+
}
|
|
114
|
+
const details = parseCodexRateLimitResetCreditsDetails(await res.json().catch(() => null));
|
|
115
|
+
return details
|
|
116
|
+
? { ok: true as const, status: res.status, details }
|
|
117
|
+
: { ok: false as const, status: res.status, reason: "invalid_response" as const };
|
|
118
|
+
}, timeoutMs);
|
|
119
|
+
return fetched.ok ? fetched.value : { ok: false, status: 0, reason: fetched.reason };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* POST /wham/rate-limit-reset-credits/consume with the exact v0.144.6 body.
|
|
124
|
+
* `idempotencyKey` identifies one logical human redemption and MUST be reused
|
|
125
|
+
* by the server on retries. Supplying `creditId` is preferred; omission leaves
|
|
126
|
+
* provider selection in control and is therefore not used by OpenGeni's
|
|
127
|
+
* human-only flow.
|
|
128
|
+
*/
|
|
129
|
+
export async function consumeCodexRateLimitResetCredit(
|
|
130
|
+
a: CodexAuthHeaders,
|
|
131
|
+
input: { idempotencyKey: string; creditId?: string | undefined },
|
|
132
|
+
fetchImpl: CodexFetch = fetch,
|
|
133
|
+
timeoutMs = RESET_CREDIT_CONSUME_TIMEOUT_MS,
|
|
134
|
+
): Promise<
|
|
135
|
+
| { ok: true; status: number; result: CodexRateLimitResetConsumeResponse }
|
|
136
|
+
| {
|
|
137
|
+
ok: false;
|
|
138
|
+
status: number;
|
|
139
|
+
reason: ResetCreditFetchFailureReason | "invalid_request";
|
|
140
|
+
}
|
|
141
|
+
> {
|
|
142
|
+
if (input.idempotencyKey.length === 0 || input.creditId === "") {
|
|
143
|
+
return { ok: false, status: 0, reason: "invalid_request" };
|
|
144
|
+
}
|
|
145
|
+
const fetched = await runBoundedCodexOperation(async (signal) => {
|
|
146
|
+
const res = await fetchImpl(`${CODEX_WHAM_BASE}/wham/rate-limit-reset-credits/consume`, {
|
|
147
|
+
method: "POST",
|
|
148
|
+
headers: {
|
|
149
|
+
...subscriptionHeaders(a),
|
|
150
|
+
"content-type": "application/json",
|
|
151
|
+
},
|
|
152
|
+
body: JSON.stringify({
|
|
153
|
+
redeem_request_id: input.idempotencyKey,
|
|
154
|
+
...(input.creditId ? { credit_id: input.creditId } : {}),
|
|
155
|
+
}),
|
|
156
|
+
signal,
|
|
157
|
+
});
|
|
158
|
+
if (!res.ok) {
|
|
159
|
+
await res.arrayBuffer().catch(() => undefined);
|
|
160
|
+
return { ok: false as const, status: res.status, reason: "http_error" as const };
|
|
161
|
+
}
|
|
162
|
+
const result = parseCodexRateLimitResetConsumeResponse(await res.json().catch(() => null));
|
|
163
|
+
return result
|
|
164
|
+
? { ok: true as const, status: res.status, result }
|
|
165
|
+
: { ok: false as const, status: res.status, reason: "invalid_response" as const };
|
|
166
|
+
}, timeoutMs);
|
|
167
|
+
return fetched.ok ? fetched.value : { ok: false, status: 0, reason: fetched.reason };
|
|
59
168
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type CodexOperationFailureReason = "network_error" | "timeout";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Bound the complete provider operation, including response-body consumption.
|
|
5
|
+
*
|
|
6
|
+
* AbortController makes native fetch release its socket, while Promise.race is
|
|
7
|
+
* the backstop for injected/custom fetch implementations that ignore `signal`.
|
|
8
|
+
* The losing operation is rejection-handled and can never become an unhandled
|
|
9
|
+
* promise after the caller has received the timeout result.
|
|
10
|
+
*/
|
|
11
|
+
export async function runBoundedCodexOperation<T>(
|
|
12
|
+
operation: (signal: AbortSignal) => Promise<T>,
|
|
13
|
+
timeoutMs: number,
|
|
14
|
+
): Promise<{ ok: true; value: T } | { ok: false; reason: CodexOperationFailureReason }> {
|
|
15
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
16
|
+
throw new Error("Codex operation timeout must be positive");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const controller = new AbortController();
|
|
20
|
+
let timedOut = false;
|
|
21
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
22
|
+
const work = operation(controller.signal).then(
|
|
23
|
+
(value) => ({ ok: true as const, value }),
|
|
24
|
+
() => ({
|
|
25
|
+
ok: false as const,
|
|
26
|
+
reason:
|
|
27
|
+
timedOut || controller.signal.aborted ? ("timeout" as const) : ("network_error" as const),
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
const deadline = new Promise<{ ok: false; reason: "timeout" }>((resolve) => {
|
|
31
|
+
timeout = setTimeout(() => {
|
|
32
|
+
timedOut = true;
|
|
33
|
+
controller.abort();
|
|
34
|
+
resolve({ ok: false, reason: "timeout" });
|
|
35
|
+
}, timeoutMs);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
return await Promise.race([work, deadline]);
|
|
40
|
+
} finally {
|
|
41
|
+
if (timeout) clearTimeout(timeout);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -57,6 +57,24 @@ export const CODEX_CLIENT_VERSION = "0.144.6";
|
|
|
57
57
|
export const CODEX_REFRESH_WINDOW_MS = 5 * 60 * 1000; // proactive refresh when within 5 min of exp (spec §1.1)
|
|
58
58
|
export const CODEX_REFRESH_FALLBACK_MS = 8 * 24 * 60 * 60 * 1000; // 8 days when exp is unparseable
|
|
59
59
|
|
|
60
|
+
// Codex Responses transport deadlines. The OpenAI SDK's own timeout only covers
|
|
61
|
+
// the wait for response headers and erases the underlying timeout class into the
|
|
62
|
+
// bare `Request timed out.` error. Keep the provider-specific budgets here so
|
|
63
|
+
// the transport can enforce and durably report them without enabling the SDK's
|
|
64
|
+
// blind request replay.
|
|
65
|
+
export const CODEX_RESPONSE_HEADERS_TIMEOUT_MS = 4 * 60_000;
|
|
66
|
+
export const CODEX_RESPONSE_STREAM_IDLE_TIMEOUT_MS = 5 * 60_000;
|
|
67
|
+
export const CODEX_RESPONSE_WHOLE_TIMEOUT_MS = 30 * 60_000;
|
|
68
|
+
// Kept as a compatibility-shaped policy field, but automatic replay is disabled
|
|
69
|
+
// until a provider-specific operation receipt can prove non-acceptance or resume
|
|
70
|
+
// the same operation identity. An absent response does not prove that the
|
|
71
|
+
// provider never accepted the request.
|
|
72
|
+
export const CODEX_RESPONSE_NO_BYTE_RETRIES = 0;
|
|
73
|
+
export const CODEX_RESPONSE_RETRY_BACKOFF_MS = 1_000;
|
|
74
|
+
// Must exceed the transport-owned whole-response deadline. This SDK guard is a
|
|
75
|
+
// last-resort envelope; the inner transport emits the typed/durable failure.
|
|
76
|
+
export const CODEX_RESPONSE_SDK_OUTER_TIMEOUT_MS = 35 * 60_000;
|
|
77
|
+
|
|
60
78
|
// ── Apps / connectors MCP (spec §1.10, §E) ───────────────────────────────────
|
|
61
79
|
// One server-side MCP exposes ALL the user's ChatGPT/Codex connectors
|
|
62
80
|
// (gmail/github/linear/slack/sentry/drive/calendar/…). Streamable HTTP, always.
|