dsh-coding-subscription-oauth 0.5.7 → 0.6.0

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.
Files changed (141) hide show
  1. package/CHANGELOG.md +211 -182
  2. package/CONTRIBUTING.md +120 -109
  3. package/INSTALL.md +240 -209
  4. package/LICENSE +19 -19
  5. package/NOTICE +11 -11
  6. package/README.de.md +298 -293
  7. package/README.es.md +299 -294
  8. package/README.fr.md +299 -294
  9. package/README.ja.md +299 -294
  10. package/README.ko.md +299 -294
  11. package/README.md +313 -306
  12. package/README.pt-BR.md +299 -294
  13. package/README.ru.md +299 -294
  14. package/README.zh-CN.md +311 -304
  15. package/compatibility/dsh-bom.json +43 -0
  16. package/cordis.patch.yml +13 -13
  17. package/docs/00-project-rules.md +195 -195
  18. package/docs/02-architecture.md +134 -129
  19. package/docs/02-architecture.zh-CN.md +134 -129
  20. package/lib/auth-routes.d.ts +8 -15
  21. package/lib/auth-routes.d.ts.map +1 -1
  22. package/lib/bin.js +746 -1402
  23. package/lib/bin.js.map +4 -4
  24. package/lib/capability-routes.d.ts +3 -3
  25. package/lib/capability-routes.d.ts.map +1 -1
  26. package/lib/capability-settings.d.ts +5 -4
  27. package/lib/capability-settings.d.ts.map +1 -1
  28. package/lib/client.js +27 -8
  29. package/lib/client.js.map +4 -4
  30. package/lib/compatibility.d.ts +46 -0
  31. package/lib/compatibility.d.ts.map +1 -0
  32. package/lib/dsh-host-adapter.d.ts +18 -0
  33. package/lib/dsh-host-adapter.d.ts.map +1 -0
  34. package/lib/gateway-auth.d.ts +1 -2
  35. package/lib/gateway-auth.d.ts.map +1 -1
  36. package/lib/gateway-routes.d.ts +3 -4
  37. package/lib/gateway-routes.d.ts.map +1 -1
  38. package/lib/gateway.d.ts +4 -0
  39. package/lib/gateway.d.ts.map +1 -1
  40. package/lib/ids.d.ts +3 -32
  41. package/lib/ids.d.ts.map +1 -1
  42. package/lib/imagine-routes.d.ts +2 -0
  43. package/lib/imagine-routes.d.ts.map +1 -1
  44. package/lib/index.d.ts +19 -4
  45. package/lib/index.d.ts.map +1 -1
  46. package/lib/index.js +26573 -1023
  47. package/lib/index.js.map +4 -4
  48. package/lib/invariant.js.map +1 -1
  49. package/lib/oauth-import-routes.d.ts +3 -4
  50. package/lib/oauth-import-routes.d.ts.map +1 -1
  51. package/lib/proxy.d.ts +3 -16
  52. package/lib/proxy.d.ts.map +1 -1
  53. package/lib/web-origin.d.ts +28 -4
  54. package/lib/web-origin.d.ts.map +1 -1
  55. package/lib/web-routes.d.ts.map +1 -1
  56. package/media/en/settings_accounts.png +0 -0
  57. package/media/en/settings_capabilities.png +0 -0
  58. package/media/en/settings_gateway.png +0 -0
  59. package/media/zh-CN/settings_accounts.png +0 -0
  60. package/media/zh-CN/settings_capabilities.png +0 -0
  61. package/media/zh-CN/settings_gateway.png +0 -0
  62. package/package.json +209 -172
  63. package/patches/dsh-agy@0.1.2.patch +25 -25
  64. package/scripts/release.mjs +186 -166
  65. package/scripts/smoke-deployed-routes.mjs +146 -146
  66. package/scripts/verify-deployed-catalog.mjs +87 -87
  67. package/src/adapter.ts +287 -287
  68. package/src/alias-adapter.ts +153 -153
  69. package/src/auth-routes.ts +921 -871
  70. package/src/auth.ts +67 -67
  71. package/src/bin.ts +350 -350
  72. package/src/capability-routes.ts +279 -275
  73. package/src/capability-runtime.ts +313 -313
  74. package/src/capability-settings.ts +658 -657
  75. package/src/capability-tools.ts +666 -666
  76. package/src/catalog.ts +271 -271
  77. package/src/client/GrokBuildSettings.tsx +770 -718
  78. package/src/client/api.ts +88 -88
  79. package/src/client/components/AboutTab.tsx +30 -20
  80. package/src/client/components/AccountsTab.tsx +241 -224
  81. package/src/client/components/Badge.tsx +33 -33
  82. package/src/client/components/CapabilitiesTab.tsx +265 -227
  83. package/src/client/components/CliPullPreview.tsx +116 -107
  84. package/src/client/components/CopyButton.tsx +57 -57
  85. package/src/client/components/GatewayTab.tsx +469 -364
  86. package/src/client/components/NoticeBanner.tsx +46 -46
  87. package/src/client/components/ProgressBar.tsx +53 -52
  88. package/src/client/components/ProviderCard.tsx +606 -502
  89. package/src/client/components/SettingsTabs.tsx +75 -75
  90. package/src/client/components/ToggleSwitch.tsx +71 -62
  91. package/src/client/constants.ts +224 -206
  92. package/src/client/display.ts +61 -61
  93. package/src/client/dshClientAdapter.ts +109 -0
  94. package/src/client/gatewaySnippets.ts +37 -36
  95. package/src/client/index.tsx +156 -37
  96. package/src/client/locales.ts +535 -487
  97. package/src/client/microStyles.ts +52 -33
  98. package/src/client/parsers.ts +396 -386
  99. package/src/client/styles.ts +325 -312
  100. package/src/client/types.ts +197 -185
  101. package/src/codex-http.ts +447 -447
  102. package/src/codex-images.ts +485 -485
  103. package/src/codex-model-capabilities.ts +320 -320
  104. package/src/codex-search.ts +245 -245
  105. package/src/codex-usage.ts +263 -263
  106. package/src/compatibility.ts +55 -0
  107. package/src/dsh-host-adapter.ts +171 -0
  108. package/src/gateway-anthropic-messages.ts +84 -84
  109. package/src/gateway-auth.ts +102 -100
  110. package/src/gateway-backend.ts +274 -274
  111. package/src/gateway-body.ts +49 -49
  112. package/src/gateway-config.ts +76 -76
  113. package/src/gateway-http.ts +104 -104
  114. package/src/gateway-openai-chat.ts +124 -124
  115. package/src/gateway-openai-responses.ts +53 -53
  116. package/src/gateway-parse.ts +224 -224
  117. package/src/gateway-protocol.ts +52 -52
  118. package/src/gateway-routes.ts +158 -152
  119. package/src/gateway.ts +258 -242
  120. package/src/grok-errors.ts +24 -24
  121. package/src/grok-imagine.ts +1627 -1627
  122. package/src/grok-import.ts +151 -151
  123. package/src/http-json.ts +82 -82
  124. package/src/ids.ts +59 -45
  125. package/src/imagine-routes.ts +463 -461
  126. package/src/index.ts +748 -598
  127. package/src/invariant.ts +17 -17
  128. package/src/kimi-errors.ts +26 -26
  129. package/src/media-store.ts +927 -927
  130. package/src/oauth-import-routes.ts +324 -314
  131. package/src/oauth-providers.ts +152 -152
  132. package/src/oauth-session.ts +183 -183
  133. package/src/oauth-sources.ts +1104 -1104
  134. package/src/oauth.ts +620 -620
  135. package/src/provider.ts +128 -128
  136. package/src/proxy.ts +11 -99
  137. package/src/redact.ts +72 -72
  138. package/src/session.ts +218 -218
  139. package/src/store.ts +217 -217
  140. package/src/web-origin.ts +296 -60
  141. package/src/web-routes.ts +38 -75
@@ -1,320 +1,320 @@
1
- /**
2
- * Live Codex model service-tier cache and composable fast-route payload helpers.
3
- *
4
- * Fetches `GET /backend-api/codex/models?client_version=…`, caches `service_tiers`,
5
- * and never invents eligibility when the live catalog is missing or stale.
6
- * A stale TTL is unknown: Fast is default-deny until a live, non-stale catalog
7
- * explicitly lists `priority` for that model. `isEligible` is required and
8
- * fail-closed — omitting it or returning false injects nothing.
9
- *
10
- * Ordinary inference stays unchanged: the unwrapped provider is never mutated.
11
- * A wrapper may use a distinct profile provider id (`codex-oauth-fast`) while
12
- * restoring native `model.provider` for the base wire call and the same catalog.
13
- *
14
- * @module dsh-coding-subscription-oauth/codex-model-capabilities
15
- */
16
-
17
- import {
18
- type CodexAuthSession,
19
- type CodexFetch,
20
- type CodexHttpClient,
21
- createCodexHttpClient,
22
- isRecord,
23
- optionalNonEmptyString,
24
- } from "./codex-http.ts";
25
- import { CODEX_OAUTH_FAST_ROUTE } from "./ids.ts";
26
-
27
- /** Distinct optional Harness route; parent owns dynamic registration. */
28
- export { CODEX_OAUTH_FAST_ROUTE };
29
-
30
- export const CODEX_MODELS_URL = "https://chatgpt.com/backend-api/codex/models";
31
- export const DEFAULT_CODEX_CLIENT_VERSION = "0.144.0";
32
- export const DEFAULT_CODEX_SERVICE_TIER = "priority";
33
- export const CODEX_ROUTING_HINT_HEADER = "x-codex-routing-hint";
34
-
35
- const DEFAULT_TTL_MS = 15 * 60 * 1000;
36
-
37
- export interface CodexModelCapability {
38
- readonly id: string;
39
- readonly serviceTiers: readonly string[];
40
- }
41
-
42
- export interface CodexModelCapabilitiesOptions {
43
- readonly auth: CodexAuthSession;
44
- readonly http?: CodexHttpClient;
45
- readonly fetchImpl?: CodexFetch;
46
- readonly clientVersion?: string;
47
- readonly originator?: string;
48
- readonly userAgent?: string;
49
- readonly sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
50
- readonly now?: () => number;
51
- readonly ttlMs?: number;
52
- }
53
-
54
- export interface CodexModelCapabilities {
55
- refresh(signal?: AbortSignal): Promise<readonly CodexModelCapability[]>;
56
- clear(): void;
57
- getCached(): readonly CodexModelCapability[] | undefined;
58
- serviceTiers(modelId: string): readonly string[];
59
- isPriorityEligible(modelId: string): boolean;
60
- isTierEligible(modelId: string, tier: string): boolean;
61
- }
62
-
63
- export type CodexOnPayload = (payload: unknown, model: unknown) => unknown | undefined | Promise<unknown | undefined>;
64
-
65
- export interface CodexFastStreamOptions {
66
- onPayload?: CodexOnPayload;
67
- headers?: Record<string, string | null>;
68
- [key: string]: unknown;
69
- }
70
-
71
- /**
72
- * Fast-route composition is default-deny.
73
- * `isEligible` is required; missing or false means no header and no `service_tier`.
74
- */
75
- export interface CodexFastRoutingOptions {
76
- readonly isEligible: (modelId: string) => boolean;
77
- readonly serviceTier?: string;
78
- /** Distinct profile/route id for the wrapper (e.g. {@link CODEX_OAUTH_FAST_ROUTE}). */
79
- readonly profileProviderId?: string;
80
- /** Native provider id restored on `model.provider` before the base wire call. */
81
- readonly nativeProviderId?: string;
82
- }
83
-
84
- export interface CodexStreamModel {
85
- readonly id: string;
86
- readonly provider?: string;
87
- readonly [key: string]: unknown;
88
- }
89
-
90
- /** Structural pi-ai provider face; `never` parameters keep native Provider assignable. */
91
- export interface CodexStreamableProvider {
92
- readonly id: string;
93
- readonly headers?: Record<string, string | null>;
94
- stream: (model: never, context: never, options?: never) => unknown;
95
- streamSimple: (model: never, context: never, options?: never) => unknown;
96
- }
97
-
98
- /** Wrapper face with callable stream methods and a possibly distinct profile id. */
99
- export type CodexFastWrappedProvider<P extends CodexStreamableProvider> = Omit<
100
- P,
101
- "id" | "headers" | "stream" | "streamSimple"
102
- > & {
103
- readonly id: string;
104
- readonly headers?: Record<string, string | null>;
105
- stream: (model: CodexStreamModel, context: unknown, options?: CodexFastStreamOptions) => unknown;
106
- streamSimple: (model: CodexStreamModel, context: unknown, options?: CodexFastStreamOptions) => unknown;
107
- };
108
-
109
- function parseServiceTiers(value: unknown): string[] {
110
- if (!Array.isArray(value)) return [];
111
- const tiers: string[] = [];
112
- const seen = new Set<string>();
113
- for (const item of value) {
114
- const id =
115
- typeof item === "string"
116
- ? optionalNonEmptyString(item)
117
- : isRecord(item)
118
- ? optionalNonEmptyString(item["id"])
119
- : undefined;
120
- if (id === undefined || seen.has(id)) continue;
121
- seen.add(id);
122
- tiers.push(id);
123
- }
124
- return tiers;
125
- }
126
-
127
- /** Parse the live models envelope. Unknown shapes yield an empty catalog, never a hardcoded fallback. */
128
- export function parseCodexModelCapabilities(value: unknown): CodexModelCapability[] {
129
- if (!isRecord(value)) return [];
130
- const raw = value["models"];
131
- if (!Array.isArray(raw)) return [];
132
- const models: CodexModelCapability[] = [];
133
- const seen = new Set<string>();
134
- for (const item of raw) {
135
- if (!isRecord(item)) continue;
136
- const id = optionalNonEmptyString(item["slug"]) ?? optionalNonEmptyString(item["id"]);
137
- if (id === undefined || seen.has(id)) continue;
138
- seen.add(id);
139
- models.push({ id, serviceTiers: parseServiceTiers(item["service_tiers"]) });
140
- }
141
- return models;
142
- }
143
-
144
- export function codexModelsUrl(clientVersion: string): string {
145
- const url = new URL(CODEX_MODELS_URL);
146
- url.searchParams.set("client_version", clientVersion);
147
- return url.toString();
148
- }
149
-
150
- /** Exact per-model Fast header value. Never a bare static `priority`. */
151
- export function codexRoutingHint(modelId: string, tier = DEFAULT_CODEX_SERVICE_TIER): string {
152
- return `model=${modelId};tier=${tier}`;
153
- }
154
-
155
- function modelIdOf(payload: unknown, model: unknown): string | undefined {
156
- if (isRecord(model)) {
157
- const id = optionalNonEmptyString(model["id"]);
158
- if (id !== undefined) return id;
159
- }
160
- return isRecord(payload) ? optionalNonEmptyString(payload["model"]) : undefined;
161
- }
162
-
163
- function isFastEligible(options: CodexFastRoutingOptions | undefined, modelId: string): boolean {
164
- return options?.isEligible?.(modelId) === true;
165
- }
166
-
167
- function restoreNativeProvider(model: CodexStreamModel, nativeProviderId: string): CodexStreamModel {
168
- if (model.provider === nativeProviderId) return model;
169
- return { ...model, provider: nativeProviderId };
170
- }
171
-
172
- /**
173
- * Compose an existing `onPayload` with service_tier injection.
174
- * Does not override a payload that already set `service_tier`.
175
- * Fail-closed: injection happens only when `isEligible(modelId)` is exactly true.
176
- */
177
- export function composeCodexFastOnPayload(
178
- inner: CodexOnPayload | undefined,
179
- options: CodexFastRoutingOptions,
180
- ): CodexOnPayload {
181
- const serviceTier = options.serviceTier ?? DEFAULT_CODEX_SERVICE_TIER;
182
- return async (payload, model) => {
183
- const next = inner === undefined ? payload : await inner(payload, model);
184
- const body = next === undefined ? payload : next;
185
- if (!isRecord(body)) return next;
186
- const modelId = modelIdOf(body, model);
187
- if (modelId === undefined || !isFastEligible(options, modelId)) return next;
188
- if (optionalNonEmptyString(body["service_tier"]) !== undefined) return next;
189
- return { ...body, service_tier: serviceTier };
190
- };
191
- }
192
-
193
- /** Merge `x-codex-routing-hint=model=<slug>;tier=<tier>` without dropping existing keys. */
194
- export function composeCodexFastHeaders(
195
- headers: Record<string, string | null> | undefined,
196
- modelId: string,
197
- tier = DEFAULT_CODEX_SERVICE_TIER,
198
- ): Record<string, string | null> {
199
- return { ...(headers ?? {}), [CODEX_ROUTING_HINT_HEADER]: codexRoutingHint(modelId, tier) };
200
- }
201
-
202
- /** Apply fast-route payload + per-model header composition to one stream-options object. */
203
- export function applyCodexFastStreamOptions<T extends CodexFastStreamOptions>(
204
- options: T | undefined,
205
- config: CodexFastRoutingOptions,
206
- modelId: string,
207
- ): T {
208
- const eligible = isFastEligible(config, modelId);
209
- const tier = config.serviceTier ?? DEFAULT_CODEX_SERVICE_TIER;
210
- const onPayload = eligible
211
- ? composeCodexFastOnPayload(options?.onPayload, { ...config, isEligible: () => true })
212
- : options?.onPayload;
213
- const next: CodexFastStreamOptions = {
214
- ...(options ?? {}),
215
- ...(onPayload === undefined ? {} : { onPayload }),
216
- headers: eligible ? composeCodexFastHeaders(options?.headers, modelId, tier) : { ...(options?.headers ?? {}) },
217
- };
218
- return next as T;
219
- }
220
-
221
- /**
222
- * Wrap a pi-ai provider for a future `codex-oauth-fast` route.
223
- * The original provider object is not mutated. The wrapper may advertise a
224
- * distinct profile provider id while restoring native `model.provider` so the
225
- * base wire call and model catalog stay on the native provider.
226
- */
227
- export function withCodexFastRouting<P extends CodexStreamableProvider>(
228
- provider: P,
229
- options: CodexFastRoutingOptions,
230
- ): CodexFastWrappedProvider<P> {
231
- const nativeProviderId = options.nativeProviderId ?? provider.id;
232
- const profileProviderId = options.profileProviderId ?? provider.id;
233
-
234
- const forward =
235
- (method: "stream" | "streamSimple") =>
236
- (model: CodexStreamModel, context: unknown, streamOptions?: CodexFastStreamOptions) => {
237
- const wireModel = restoreNativeProvider(model, nativeProviderId);
238
- return provider[method](
239
- wireModel as never,
240
- context as never,
241
- applyCodexFastStreamOptions(streamOptions, options, model.id) as never,
242
- );
243
- };
244
-
245
- return {
246
- ...provider,
247
- id: profileProviderId,
248
- stream: forward("stream"),
249
- streamSimple: forward("streamSimple"),
250
- };
251
- }
252
-
253
- /**
254
- * Live `/codex/models` cache. Fetch failures leave ordinary inference alone.
255
- * Eligibility is false until a live, non-stale catalog explicitly lists the tier.
256
- * A stale TTL is treated as unknown, not as the last known catalog.
257
- */
258
- export function createCodexModelCapabilities(options: CodexModelCapabilitiesOptions): CodexModelCapabilities {
259
- const http =
260
- options.http ??
261
- createCodexHttpClient({
262
- auth: options.auth,
263
- ...(options.fetchImpl === undefined ? {} : { fetchImpl: options.fetchImpl }),
264
- ...(options.originator === undefined ? {} : { originator: options.originator }),
265
- ...(options.userAgent === undefined ? {} : { userAgent: options.userAgent }),
266
- ...(options.sleep === undefined ? {} : { sleep: options.sleep }),
267
- ...(options.now === undefined ? {} : { now: options.now }),
268
- });
269
- const clientVersion = options.clientVersion ?? DEFAULT_CODEX_CLIENT_VERSION;
270
- const now = options.now ?? Date.now;
271
- const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
272
- let cached: { fetchedAt: number; models: readonly CodexModelCapability[] } | undefined;
273
- let inFlight: Promise<readonly CodexModelCapability[]> | undefined;
274
- let epoch = 0;
275
-
276
- const isFresh = (): boolean => cached !== undefined && now() - cached.fetchedAt < ttlMs;
277
-
278
- const freshModels = (): readonly CodexModelCapability[] | undefined => (isFresh() ? cached?.models : undefined);
279
-
280
- const lookup = (modelId: string): CodexModelCapability | undefined =>
281
- freshModels()?.find((model) => model.id === modelId);
282
-
283
- const load = async (startedEpoch: number, signal?: AbortSignal): Promise<readonly CodexModelCapability[]> => {
284
- try {
285
- const payload = await http.requestJson({
286
- url: codexModelsUrl(clientVersion),
287
- method: "GET",
288
- headers: { "cache-control": "no-store" },
289
- ...(signal === undefined ? {} : { signal }),
290
- });
291
- const models = parseCodexModelCapabilities(payload);
292
- if (startedEpoch === epoch) cached = { fetchedAt: now(), models };
293
- return models;
294
- } catch {
295
- return freshModels() ?? [];
296
- }
297
- };
298
-
299
- return {
300
- async refresh(signal) {
301
- if (isFresh() && cached !== undefined) return cached.models;
302
- if (inFlight !== undefined) return inFlight;
303
- const startedEpoch = epoch;
304
- const current = load(startedEpoch, signal).finally(() => {
305
- if (inFlight === current) inFlight = undefined;
306
- });
307
- inFlight = current;
308
- return current;
309
- },
310
- clear() {
311
- epoch += 1;
312
- cached = undefined;
313
- inFlight = undefined;
314
- },
315
- getCached: () => freshModels(),
316
- serviceTiers: (modelId) => lookup(modelId)?.serviceTiers ?? [],
317
- isPriorityEligible: (modelId) => lookup(modelId)?.serviceTiers.includes(DEFAULT_CODEX_SERVICE_TIER) === true,
318
- isTierEligible: (modelId, tier) => lookup(modelId)?.serviceTiers.includes(tier) === true,
319
- };
320
- }
1
+ /**
2
+ * Live Codex model service-tier cache and composable fast-route payload helpers.
3
+ *
4
+ * Fetches `GET /backend-api/codex/models?client_version=…`, caches `service_tiers`,
5
+ * and never invents eligibility when the live catalog is missing or stale.
6
+ * A stale TTL is unknown: Fast is default-deny until a live, non-stale catalog
7
+ * explicitly lists `priority` for that model. `isEligible` is required and
8
+ * fail-closed — omitting it or returning false injects nothing.
9
+ *
10
+ * Ordinary inference stays unchanged: the unwrapped provider is never mutated.
11
+ * A wrapper may use a distinct profile provider id (`codex-oauth-fast`) while
12
+ * restoring native `model.provider` for the base wire call and the same catalog.
13
+ *
14
+ * @module dsh-coding-subscription-oauth/codex-model-capabilities
15
+ */
16
+
17
+ import {
18
+ type CodexAuthSession,
19
+ type CodexFetch,
20
+ type CodexHttpClient,
21
+ createCodexHttpClient,
22
+ isRecord,
23
+ optionalNonEmptyString,
24
+ } from "./codex-http.ts";
25
+ import { CODEX_OAUTH_FAST_ROUTE } from "./ids.ts";
26
+
27
+ /** Distinct optional Harness route; parent owns dynamic registration. */
28
+ export { CODEX_OAUTH_FAST_ROUTE };
29
+
30
+ export const CODEX_MODELS_URL = "https://chatgpt.com/backend-api/codex/models";
31
+ export const DEFAULT_CODEX_CLIENT_VERSION = "0.144.0";
32
+ export const DEFAULT_CODEX_SERVICE_TIER = "priority";
33
+ export const CODEX_ROUTING_HINT_HEADER = "x-codex-routing-hint";
34
+
35
+ const DEFAULT_TTL_MS = 15 * 60 * 1000;
36
+
37
+ export interface CodexModelCapability {
38
+ readonly id: string;
39
+ readonly serviceTiers: readonly string[];
40
+ }
41
+
42
+ export interface CodexModelCapabilitiesOptions {
43
+ readonly auth: CodexAuthSession;
44
+ readonly http?: CodexHttpClient;
45
+ readonly fetchImpl?: CodexFetch;
46
+ readonly clientVersion?: string;
47
+ readonly originator?: string;
48
+ readonly userAgent?: string;
49
+ readonly sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
50
+ readonly now?: () => number;
51
+ readonly ttlMs?: number;
52
+ }
53
+
54
+ export interface CodexModelCapabilities {
55
+ refresh(signal?: AbortSignal): Promise<readonly CodexModelCapability[]>;
56
+ clear(): void;
57
+ getCached(): readonly CodexModelCapability[] | undefined;
58
+ serviceTiers(modelId: string): readonly string[];
59
+ isPriorityEligible(modelId: string): boolean;
60
+ isTierEligible(modelId: string, tier: string): boolean;
61
+ }
62
+
63
+ export type CodexOnPayload = (payload: unknown, model: unknown) => unknown | undefined | Promise<unknown | undefined>;
64
+
65
+ export interface CodexFastStreamOptions {
66
+ onPayload?: CodexOnPayload;
67
+ headers?: Record<string, string | null>;
68
+ [key: string]: unknown;
69
+ }
70
+
71
+ /**
72
+ * Fast-route composition is default-deny.
73
+ * `isEligible` is required; missing or false means no header and no `service_tier`.
74
+ */
75
+ export interface CodexFastRoutingOptions {
76
+ readonly isEligible: (modelId: string) => boolean;
77
+ readonly serviceTier?: string;
78
+ /** Distinct profile/route id for the wrapper (e.g. {@link CODEX_OAUTH_FAST_ROUTE}). */
79
+ readonly profileProviderId?: string;
80
+ /** Native provider id restored on `model.provider` before the base wire call. */
81
+ readonly nativeProviderId?: string;
82
+ }
83
+
84
+ export interface CodexStreamModel {
85
+ readonly id: string;
86
+ readonly provider?: string;
87
+ readonly [key: string]: unknown;
88
+ }
89
+
90
+ /** Structural pi-ai provider face; `never` parameters keep native Provider assignable. */
91
+ export interface CodexStreamableProvider {
92
+ readonly id: string;
93
+ readonly headers?: Record<string, string | null>;
94
+ stream: (model: never, context: never, options?: never) => unknown;
95
+ streamSimple: (model: never, context: never, options?: never) => unknown;
96
+ }
97
+
98
+ /** Wrapper face with callable stream methods and a possibly distinct profile id. */
99
+ export type CodexFastWrappedProvider<P extends CodexStreamableProvider> = Omit<
100
+ P,
101
+ "id" | "headers" | "stream" | "streamSimple"
102
+ > & {
103
+ readonly id: string;
104
+ readonly headers?: Record<string, string | null>;
105
+ stream: (model: CodexStreamModel, context: unknown, options?: CodexFastStreamOptions) => unknown;
106
+ streamSimple: (model: CodexStreamModel, context: unknown, options?: CodexFastStreamOptions) => unknown;
107
+ };
108
+
109
+ function parseServiceTiers(value: unknown): string[] {
110
+ if (!Array.isArray(value)) return [];
111
+ const tiers: string[] = [];
112
+ const seen = new Set<string>();
113
+ for (const item of value) {
114
+ const id =
115
+ typeof item === "string"
116
+ ? optionalNonEmptyString(item)
117
+ : isRecord(item)
118
+ ? optionalNonEmptyString(item["id"])
119
+ : undefined;
120
+ if (id === undefined || seen.has(id)) continue;
121
+ seen.add(id);
122
+ tiers.push(id);
123
+ }
124
+ return tiers;
125
+ }
126
+
127
+ /** Parse the live models envelope. Unknown shapes yield an empty catalog, never a hardcoded fallback. */
128
+ export function parseCodexModelCapabilities(value: unknown): CodexModelCapability[] {
129
+ if (!isRecord(value)) return [];
130
+ const raw = value["models"];
131
+ if (!Array.isArray(raw)) return [];
132
+ const models: CodexModelCapability[] = [];
133
+ const seen = new Set<string>();
134
+ for (const item of raw) {
135
+ if (!isRecord(item)) continue;
136
+ const id = optionalNonEmptyString(item["slug"]) ?? optionalNonEmptyString(item["id"]);
137
+ if (id === undefined || seen.has(id)) continue;
138
+ seen.add(id);
139
+ models.push({ id, serviceTiers: parseServiceTiers(item["service_tiers"]) });
140
+ }
141
+ return models;
142
+ }
143
+
144
+ export function codexModelsUrl(clientVersion: string): string {
145
+ const url = new URL(CODEX_MODELS_URL);
146
+ url.searchParams.set("client_version", clientVersion);
147
+ return url.toString();
148
+ }
149
+
150
+ /** Exact per-model Fast header value. Never a bare static `priority`. */
151
+ export function codexRoutingHint(modelId: string, tier = DEFAULT_CODEX_SERVICE_TIER): string {
152
+ return `model=${modelId};tier=${tier}`;
153
+ }
154
+
155
+ function modelIdOf(payload: unknown, model: unknown): string | undefined {
156
+ if (isRecord(model)) {
157
+ const id = optionalNonEmptyString(model["id"]);
158
+ if (id !== undefined) return id;
159
+ }
160
+ return isRecord(payload) ? optionalNonEmptyString(payload["model"]) : undefined;
161
+ }
162
+
163
+ function isFastEligible(options: CodexFastRoutingOptions | undefined, modelId: string): boolean {
164
+ return options?.isEligible?.(modelId) === true;
165
+ }
166
+
167
+ function restoreNativeProvider(model: CodexStreamModel, nativeProviderId: string): CodexStreamModel {
168
+ if (model.provider === nativeProviderId) return model;
169
+ return { ...model, provider: nativeProviderId };
170
+ }
171
+
172
+ /**
173
+ * Compose an existing `onPayload` with service_tier injection.
174
+ * Does not override a payload that already set `service_tier`.
175
+ * Fail-closed: injection happens only when `isEligible(modelId)` is exactly true.
176
+ */
177
+ export function composeCodexFastOnPayload(
178
+ inner: CodexOnPayload | undefined,
179
+ options: CodexFastRoutingOptions,
180
+ ): CodexOnPayload {
181
+ const serviceTier = options.serviceTier ?? DEFAULT_CODEX_SERVICE_TIER;
182
+ return async (payload, model) => {
183
+ const next = inner === undefined ? payload : await inner(payload, model);
184
+ const body = next === undefined ? payload : next;
185
+ if (!isRecord(body)) return next;
186
+ const modelId = modelIdOf(body, model);
187
+ if (modelId === undefined || !isFastEligible(options, modelId)) return next;
188
+ if (optionalNonEmptyString(body["service_tier"]) !== undefined) return next;
189
+ return { ...body, service_tier: serviceTier };
190
+ };
191
+ }
192
+
193
+ /** Merge `x-codex-routing-hint=model=<slug>;tier=<tier>` without dropping existing keys. */
194
+ export function composeCodexFastHeaders(
195
+ headers: Record<string, string | null> | undefined,
196
+ modelId: string,
197
+ tier = DEFAULT_CODEX_SERVICE_TIER,
198
+ ): Record<string, string | null> {
199
+ return { ...(headers ?? {}), [CODEX_ROUTING_HINT_HEADER]: codexRoutingHint(modelId, tier) };
200
+ }
201
+
202
+ /** Apply fast-route payload + per-model header composition to one stream-options object. */
203
+ export function applyCodexFastStreamOptions<T extends CodexFastStreamOptions>(
204
+ options: T | undefined,
205
+ config: CodexFastRoutingOptions,
206
+ modelId: string,
207
+ ): T {
208
+ const eligible = isFastEligible(config, modelId);
209
+ const tier = config.serviceTier ?? DEFAULT_CODEX_SERVICE_TIER;
210
+ const onPayload = eligible
211
+ ? composeCodexFastOnPayload(options?.onPayload, { ...config, isEligible: () => true })
212
+ : options?.onPayload;
213
+ const next: CodexFastStreamOptions = {
214
+ ...(options ?? {}),
215
+ ...(onPayload === undefined ? {} : { onPayload }),
216
+ headers: eligible ? composeCodexFastHeaders(options?.headers, modelId, tier) : { ...(options?.headers ?? {}) },
217
+ };
218
+ return next as T;
219
+ }
220
+
221
+ /**
222
+ * Wrap a pi-ai provider for a future `codex-oauth-fast` route.
223
+ * The original provider object is not mutated. The wrapper may advertise a
224
+ * distinct profile provider id while restoring native `model.provider` so the
225
+ * base wire call and model catalog stay on the native provider.
226
+ */
227
+ export function withCodexFastRouting<P extends CodexStreamableProvider>(
228
+ provider: P,
229
+ options: CodexFastRoutingOptions,
230
+ ): CodexFastWrappedProvider<P> {
231
+ const nativeProviderId = options.nativeProviderId ?? provider.id;
232
+ const profileProviderId = options.profileProviderId ?? provider.id;
233
+
234
+ const forward =
235
+ (method: "stream" | "streamSimple") =>
236
+ (model: CodexStreamModel, context: unknown, streamOptions?: CodexFastStreamOptions) => {
237
+ const wireModel = restoreNativeProvider(model, nativeProviderId);
238
+ return provider[method](
239
+ wireModel as never,
240
+ context as never,
241
+ applyCodexFastStreamOptions(streamOptions, options, model.id) as never,
242
+ );
243
+ };
244
+
245
+ return {
246
+ ...provider,
247
+ id: profileProviderId,
248
+ stream: forward("stream"),
249
+ streamSimple: forward("streamSimple"),
250
+ };
251
+ }
252
+
253
+ /**
254
+ * Live `/codex/models` cache. Fetch failures leave ordinary inference alone.
255
+ * Eligibility is false until a live, non-stale catalog explicitly lists the tier.
256
+ * A stale TTL is treated as unknown, not as the last known catalog.
257
+ */
258
+ export function createCodexModelCapabilities(options: CodexModelCapabilitiesOptions): CodexModelCapabilities {
259
+ const http =
260
+ options.http ??
261
+ createCodexHttpClient({
262
+ auth: options.auth,
263
+ ...(options.fetchImpl === undefined ? {} : { fetchImpl: options.fetchImpl }),
264
+ ...(options.originator === undefined ? {} : { originator: options.originator }),
265
+ ...(options.userAgent === undefined ? {} : { userAgent: options.userAgent }),
266
+ ...(options.sleep === undefined ? {} : { sleep: options.sleep }),
267
+ ...(options.now === undefined ? {} : { now: options.now }),
268
+ });
269
+ const clientVersion = options.clientVersion ?? DEFAULT_CODEX_CLIENT_VERSION;
270
+ const now = options.now ?? Date.now;
271
+ const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
272
+ let cached: { fetchedAt: number; models: readonly CodexModelCapability[] } | undefined;
273
+ let inFlight: Promise<readonly CodexModelCapability[]> | undefined;
274
+ let epoch = 0;
275
+
276
+ const isFresh = (): boolean => cached !== undefined && now() - cached.fetchedAt < ttlMs;
277
+
278
+ const freshModels = (): readonly CodexModelCapability[] | undefined => (isFresh() ? cached?.models : undefined);
279
+
280
+ const lookup = (modelId: string): CodexModelCapability | undefined =>
281
+ freshModels()?.find((model) => model.id === modelId);
282
+
283
+ const load = async (startedEpoch: number, signal?: AbortSignal): Promise<readonly CodexModelCapability[]> => {
284
+ try {
285
+ const payload = await http.requestJson({
286
+ url: codexModelsUrl(clientVersion),
287
+ method: "GET",
288
+ headers: { "cache-control": "no-store" },
289
+ ...(signal === undefined ? {} : { signal }),
290
+ });
291
+ const models = parseCodexModelCapabilities(payload);
292
+ if (startedEpoch === epoch) cached = { fetchedAt: now(), models };
293
+ return models;
294
+ } catch {
295
+ return freshModels() ?? [];
296
+ }
297
+ };
298
+
299
+ return {
300
+ async refresh(signal) {
301
+ if (isFresh() && cached !== undefined) return cached.models;
302
+ if (inFlight !== undefined) return inFlight;
303
+ const startedEpoch = epoch;
304
+ const current = load(startedEpoch, signal).finally(() => {
305
+ if (inFlight === current) inFlight = undefined;
306
+ });
307
+ inFlight = current;
308
+ return current;
309
+ },
310
+ clear() {
311
+ epoch += 1;
312
+ cached = undefined;
313
+ inFlight = undefined;
314
+ },
315
+ getCached: () => freshModels(),
316
+ serviceTiers: (modelId) => lookup(modelId)?.serviceTiers ?? [],
317
+ isPriorityEligible: (modelId) => lookup(modelId)?.serviceTiers.includes(DEFAULT_CODEX_SERVICE_TIER) === true,
318
+ isTierEligible: (modelId, tier) => lookup(modelId)?.serviceTiers.includes(tier) === true,
319
+ };
320
+ }