dsh-coding-subscription-oauth 0.6.3 → 0.6.4

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 (116) hide show
  1. package/CHANGELOG.md +258 -250
  2. package/CONTRIBUTING.md +129 -129
  3. package/INSTALL.md +256 -255
  4. package/LICENSE +19 -19
  5. package/NOTICE +11 -11
  6. package/README.de.md +303 -303
  7. package/README.es.md +304 -304
  8. package/README.fr.md +304 -304
  9. package/README.ja.md +304 -304
  10. package/README.ko.md +304 -304
  11. package/README.md +320 -320
  12. package/README.pt-BR.md +304 -304
  13. package/README.ru.md +304 -304
  14. package/README.zh-CN.md +318 -318
  15. package/compatibility/dsh-bom.json +30 -30
  16. package/cordis.patch.yml +13 -13
  17. package/docs/00-project-rules.md +212 -212
  18. package/docs/02-architecture.md +138 -138
  19. package/docs/02-architecture.zh-CN.md +138 -138
  20. package/lib/bin.js +1333 -569
  21. package/lib/bin.js.map +4 -4
  22. package/lib/client.js +1 -1
  23. package/lib/client.js.map +2 -2
  24. package/lib/index.js +689 -25492
  25. package/lib/index.js.map +4 -4
  26. package/lib/invariant.js.map +1 -1
  27. package/media/en/settings_accounts.png +0 -0
  28. package/media/en/settings_capabilities.png +0 -0
  29. package/media/en/settings_gateway.png +0 -0
  30. package/media/settings_accounts.png +0 -0
  31. package/media/settings_capabilities.png +0 -0
  32. package/media/settings_gateway.png +0 -0
  33. package/media/settings_overview.png +0 -0
  34. package/media/zh-CN/settings_accounts.png +0 -0
  35. package/media/zh-CN/settings_capabilities.png +0 -0
  36. package/media/zh-CN/settings_gateway.png +0 -0
  37. package/package.json +223 -223
  38. package/patches/dsh-agy@0.1.2.patch +25 -25
  39. package/scripts/release.mjs +186 -186
  40. package/scripts/smoke-deployed-routes.mjs +146 -146
  41. package/scripts/verify-deployed-catalog.mjs +87 -87
  42. package/src/adapter.ts +348 -348
  43. package/src/alias-adapter.ts +147 -147
  44. package/src/auth-routes.ts +921 -921
  45. package/src/auth.ts +67 -67
  46. package/src/bin.ts +350 -350
  47. package/src/capability-routes.ts +279 -279
  48. package/src/capability-runtime.ts +314 -314
  49. package/src/capability-settings.ts +671 -671
  50. package/src/capability-tools.ts +685 -685
  51. package/src/catalog.ts +271 -271
  52. package/src/client/GrokBuildSettings.tsx +771 -771
  53. package/src/client/api.ts +88 -88
  54. package/src/client/components/AboutTab.tsx +30 -30
  55. package/src/client/components/AccountsTab.tsx +241 -241
  56. package/src/client/components/Badge.tsx +33 -33
  57. package/src/client/components/CapabilitiesTab.tsx +265 -265
  58. package/src/client/components/CliPullPreview.tsx +116 -116
  59. package/src/client/components/CopyButton.tsx +57 -57
  60. package/src/client/components/GatewayTab.tsx +469 -469
  61. package/src/client/components/NoticeBanner.tsx +46 -46
  62. package/src/client/components/ProgressBar.tsx +53 -53
  63. package/src/client/components/ProviderCard.tsx +606 -606
  64. package/src/client/components/SettingsTabs.tsx +75 -75
  65. package/src/client/components/ToggleSwitch.tsx +71 -71
  66. package/src/client/constants.ts +230 -230
  67. package/src/client/display.ts +61 -61
  68. package/src/client/dshClientAdapter.ts +127 -127
  69. package/src/client/gatewaySnippets.ts +37 -37
  70. package/src/client/index.tsx +156 -156
  71. package/src/client/locales.ts +540 -540
  72. package/src/client/microStyles.ts +52 -52
  73. package/src/client/parsers.ts +398 -398
  74. package/src/client/styles.ts +325 -325
  75. package/src/client/types.ts +199 -199
  76. package/src/codex-http.ts +447 -447
  77. package/src/codex-images.ts +503 -503
  78. package/src/codex-model-capabilities.ts +320 -320
  79. package/src/codex-search.ts +245 -245
  80. package/src/codex-usage.ts +263 -263
  81. package/src/compatibility.ts +55 -55
  82. package/src/dsh-host-adapter.ts +173 -173
  83. package/src/gateway-anthropic-messages.ts +84 -84
  84. package/src/gateway-auth.ts +102 -102
  85. package/src/gateway-backend.ts +274 -274
  86. package/src/gateway-body.ts +49 -49
  87. package/src/gateway-config.ts +76 -76
  88. package/src/gateway-http.ts +104 -104
  89. package/src/gateway-openai-chat.ts +124 -124
  90. package/src/gateway-openai-responses.ts +53 -53
  91. package/src/gateway-parse.ts +224 -224
  92. package/src/gateway-protocol.ts +52 -52
  93. package/src/gateway-routes.ts +158 -158
  94. package/src/gateway.ts +258 -258
  95. package/src/grok-errors.ts +24 -24
  96. package/src/grok-imagine.ts +1627 -1627
  97. package/src/grok-import.ts +151 -151
  98. package/src/http-json.ts +82 -82
  99. package/src/ids.ts +59 -59
  100. package/src/imagine-routes.ts +463 -463
  101. package/src/index.ts +735 -735
  102. package/src/invariant.ts +17 -17
  103. package/src/kimi-errors.ts +26 -26
  104. package/src/media-store.ts +927 -927
  105. package/src/oauth-import-routes.ts +324 -324
  106. package/src/oauth-providers.ts +152 -152
  107. package/src/oauth-session.ts +183 -183
  108. package/src/oauth-sources.ts +1104 -1104
  109. package/src/oauth.ts +620 -620
  110. package/src/provider.ts +128 -128
  111. package/src/proxy.ts +11 -11
  112. package/src/redact.ts +72 -72
  113. package/src/session.ts +218 -218
  114. package/src/store.ts +217 -217
  115. package/src/web-origin.ts +296 -296
  116. package/src/web-routes.ts +38 -38
@@ -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
+ }