dsh-coding-subscription-oauth 0.6.2 → 0.6.3

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