pi-openai-toolkit 0.14.4 → 0.14.6

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 (43) hide show
  1. package/README.md +119 -147
  2. package/README.zh.md +119 -151
  3. package/docs/internals.md +42 -21
  4. package/package.json +127 -127
  5. package/src/auto-mode/classifier.ts +165 -165
  6. package/src/auto-mode/policy.ts +146 -146
  7. package/src/auto-mode/prompt.ts +279 -279
  8. package/src/auto-mode/reviewer.ts +325 -325
  9. package/src/auto-mode/transcript.ts +133 -133
  10. package/src/auto-mode/types.ts +137 -137
  11. package/src/codex-astra/effort-planner.ts +175 -175
  12. package/src/codex-astra/extension.ts +122 -122
  13. package/src/config.ts +621 -592
  14. package/src/context-management/codex-provider.ts +260 -260
  15. package/src/context-management/history-notes.ts +357 -325
  16. package/src/context-management/messages.ts +112 -106
  17. package/src/context-management/namespace-tools.ts +426 -426
  18. package/src/context-management/tool-contract.ts +23 -23
  19. package/src/context-management/tools.ts +306 -311
  20. package/src/context-management/types.ts +168 -161
  21. package/src/context-management/window-budget.ts +83 -83
  22. package/src/context-management/window-manager.ts +60 -51
  23. package/src/context-management/window-request.ts +125 -125
  24. package/src/debug.ts +1 -1
  25. package/src/extension-runtime.ts +34 -9
  26. package/src/image-generation/artifacts.ts +237 -237
  27. package/src/image-generation/client.ts +248 -248
  28. package/src/image-generation/eligibility.ts +18 -18
  29. package/src/image-generation/extension.ts +19 -2
  30. package/src/image-generation/protocol.ts +473 -405
  31. package/src/image-generation/references.ts +194 -194
  32. package/src/image-generation/render.ts +252 -252
  33. package/src/image-generation/service.ts +249 -243
  34. package/src/image-generation/types.ts +189 -184
  35. package/src/model-scope.ts +17 -17
  36. package/src/provider-headers.ts +23 -23
  37. package/src/responses-headers.ts +173 -108
  38. package/src/runtime.ts +513 -477
  39. package/src/types.ts +11 -0
  40. package/src/web-search/extension.ts +80 -80
  41. package/src/web-search/payload.ts +96 -96
  42. package/src/web-search/prompt.ts +20 -20
  43. package/src/web-search/types.ts +45 -45
@@ -1,260 +1,260 @@
1
- import type { Api, Model, ProviderHeaders } from "@earendil-works/pi-ai";
2
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
3
- import { mergeProviderHeaders } from "../provider-headers";
4
- import { CODEX_CLIENT_VERSION } from "../responses-headers";
5
- import { normalizeBaseUrl } from "../runtime";
6
- import { isExactModelAllowed } from "../model-scope";
7
- import {
8
- isNonEmptyString,
9
- type CodexContextProvider,
10
- type CodexContextProviderResolution,
11
- } from "./types";
12
-
13
- const CODEX_PROVIDER = "openai-codex";
14
- const CODEX_API = "openai-codex-responses";
15
- const GATEWAY_API = "openai-responses";
16
- const CODEX_TOOL_ORIGINATOR = "codex_cli_rs";
17
- const CODEX_GATEWAY_ORIGINATOR = "codex_cli_rs";
18
- const CODEX_TOOL_VERSION = "0.0.0";
19
- const CODEX_SESSION_ID_MAX_LENGTH = 64;
20
-
21
- export const CODEX_AFFINITY_SCOPE = "codex-session-v1";
22
-
23
- export const CODEX_CONTEXT_PROVIDER_ERROR =
24
- "Remote Context management requires a configured Codex-compatible backend";
25
-
26
- export function isNativeCodexModel(
27
- model: ExtensionContext["model"] | undefined,
28
- ): boolean {
29
- return model?.provider === CODEX_PROVIDER && model.api === CODEX_API;
30
- }
31
-
32
- export function isCodexGatewayModel(
33
- model: ExtensionContext["model"] | undefined,
34
- gatewayModels: readonly string[] = [],
35
- ): boolean {
36
- // Gateway coverage is an operator allowlist of exact "provider/model" keys
37
- // (compaction.gatewayContextModels). The backend relay must actually pass
38
- // hosted window markers through; models outside the list stay on remote
39
- // compaction v2.
40
- if (!model || model.api !== GATEWAY_API) return false;
41
- return isExactModelAllowed(model, gatewayModels);
42
- }
43
-
44
- export function normalizeCodexBackendBaseUrl(baseUrl: string | undefined | null): string | undefined {
45
- const normalized = normalizeBaseUrl(baseUrl);
46
- if (!normalized) return undefined;
47
- try {
48
- const url = new URL(normalized);
49
- const pathname = url.pathname.replace(/\/+$/, "");
50
- if (pathname.endsWith("/codex/responses")) {
51
- url.pathname = pathname.slice(0, -"/responses".length);
52
- return url.toString().replace(/\/$/, "");
53
- }
54
- if (pathname.endsWith("/codex")) return normalized;
55
- if (pathname.endsWith("/backend-api") || pathname.endsWith("/api")) {
56
- return `${normalized}/codex`;
57
- }
58
- if (pathname === "") return `${normalized}/api/codex`;
59
- if (pathname === "/") return `${normalized}/api/codex`;
60
- return undefined;
61
- } catch {
62
- return undefined;
63
- }
64
- }
65
-
66
- function headerValue(headers: ProviderHeaders | undefined, name: string): string | undefined {
67
- const wanted = name.toLowerCase();
68
- for (const [key, value] of Object.entries(headers ?? {})) {
69
- if (key.toLowerCase() === wanted && typeof value === "string" && value.trim()) {
70
- return value.trim();
71
- }
72
- }
73
- return undefined;
74
- }
75
-
76
- function bearerToken(value: string | undefined): string | undefined {
77
- if (!value) return undefined;
78
- const match = value.trim().match(/^Bearer\s+(.+)$/i);
79
- return (match?.[1] ?? value).trim() || undefined;
80
- }
81
-
82
- function decodeJwtPayload(token: string): Record<string, unknown> | undefined {
83
- const parts = token.split(".");
84
- if (parts.length !== 3) return undefined;
85
- try {
86
- const value: unknown = JSON.parse(Buffer.from(parts[1]!, "base64url").toString("utf8"));
87
- return value && typeof value === "object" && !Array.isArray(value)
88
- ? value as Record<string, unknown>
89
- : undefined;
90
- } catch {
91
- return undefined;
92
- }
93
- }
94
-
95
- function accountIdFromToken(token: string): string | undefined {
96
- const payload = decodeJwtPayload(token);
97
- const direct = payload?.chatgpt_account_id;
98
- if (isNonEmptyString(direct)) return direct.trim();
99
- const auth = payload?.["https://api.openai.com/auth"];
100
- if (!auth || typeof auth !== "object" || Array.isArray(auth)) return undefined;
101
- const nested = (auth as Record<string, unknown>).chatgpt_account_id;
102
- return isNonEmptyString(nested) ? nested.trim() : undefined;
103
- }
104
-
105
- function userAgent(version: string): string {
106
- const platform = typeof process !== "undefined" ? process.platform : "unknown";
107
- const arch = typeof process !== "undefined" ? process.arch : "unknown";
108
- return `codex_cli_rs/${version} (${platform}; ${arch})`;
109
- }
110
-
111
- function clampSessionId(value: string | undefined): string | undefined {
112
- if (!value) return undefined;
113
- const chars = Array.from(value.trim());
114
- if (chars.length === 0) return undefined;
115
- return chars.length <= CODEX_SESSION_ID_MAX_LENGTH
116
- ? chars.join("")
117
- : chars.slice(0, CODEX_SESSION_ID_MAX_LENGTH).join("");
118
- }
119
-
120
- function removeSensitiveGatewayHeaders(headers: ProviderHeaders): Record<string, string> {
121
- const result: Record<string, string> = {};
122
- for (const [name, value] of Object.entries(headers)) {
123
- if (typeof value !== "string") continue;
124
- const lower = name.toLowerCase();
125
- if (lower === "authorization" || lower === "cookie" || lower === "chatgpt-account-id" || lower === "x-api-key") {
126
- continue;
127
- }
128
- result[name] = value;
129
- }
130
- return result;
131
- }
132
-
133
- export type CodexContextProviderHeaderOptions = {
134
- sessionId?: string;
135
- clientRequestId?: string;
136
- };
137
-
138
- export function codexContextProviderHeaders(
139
- provider: CodexContextProvider,
140
- options: CodexContextProviderHeaderOptions = {},
141
- ): Headers {
142
- const headers = new Headers();
143
- for (const [name, value] of Object.entries(provider.headers)) {
144
- if (typeof value === "string") headers.set(name, value);
145
- }
146
-
147
- if (provider.kind === "native-codex") {
148
- headers.set("Authorization", `Bearer ${provider.token}`);
149
- headers.set("ChatGPT-Account-ID", provider.accountId);
150
- headers.set("originator", CODEX_TOOL_ORIGINATOR);
151
- headers.set("User-Agent", userAgent(CODEX_TOOL_VERSION));
152
- headers.set("version", CODEX_TOOL_VERSION);
153
- } else {
154
- headers.set("Authorization", `Bearer ${provider.apiKey}`);
155
- headers.delete("ChatGPT-Account-ID");
156
- headers.delete("Cookie");
157
- headers.set("originator", CODEX_GATEWAY_ORIGINATOR);
158
- headers.set("User-Agent", userAgent(CODEX_CLIENT_VERSION));
159
- headers.set("version", CODEX_CLIENT_VERSION);
160
- headers.set("X-Codex-Affinity-Scope", CODEX_AFFINITY_SCOPE);
161
- headers.set("X-Codex-Model", provider.model);
162
- }
163
-
164
- const sessionId = clampSessionId(options.sessionId);
165
- if (sessionId) {
166
- headers.set("Session-Id", sessionId);
167
- headers.set("X-Client-Request-Id", options.clientRequestId?.trim() || sessionId);
168
- }
169
- headers.set("content-type", "application/json");
170
- return headers;
171
- }
172
-
173
- export async function resolveCodexContextProvider(
174
- ctx: ExtensionContext,
175
- modelOverride: ExtensionContext["model"] = ctx.model,
176
- gatewayModels: readonly string[] = [],
177
- ): Promise<CodexContextProviderResolution> {
178
- const model = modelOverride;
179
- const descriptor = {
180
- provider: model?.provider,
181
- api: model?.api,
182
- model: model?.id,
183
- baseUrl: normalizeBaseUrl(model?.baseUrl),
184
- };
185
- if (!model) return { ok: false, reason: "unsupported-model" };
186
-
187
- const isNative = isNativeCodexModel(model);
188
- const isGateway = isCodexGatewayModel(model, gatewayModels);
189
- if (!isNative && !isGateway) {
190
- return { ok: false, reason: "unsupported-model", ...descriptor };
191
- }
192
- if (!isNative && model.api !== GATEWAY_API) {
193
- return { ok: false, reason: "unsupported-api", ...descriptor };
194
- }
195
- if (isNative && model.api !== CODEX_API) {
196
- return { ok: false, reason: "unsupported-api", ...descriptor };
197
- }
198
-
199
- let auth: Awaited<ReturnType<typeof ctx.modelRegistry.getApiKeyAndHeaders>>;
200
- try {
201
- auth = await ctx.modelRegistry.getApiKeyAndHeaders(model as Model<Api>);
202
- } catch {
203
- return { ok: false, reason: "auth-resolution-failed", ...descriptor };
204
- }
205
- if (!auth.ok) return { ok: false, reason: "auth-resolution-failed", ...descriptor };
206
-
207
- const rawBaseUrl = normalizeBaseUrl(auth.baseUrl) ?? normalizeBaseUrl(model.baseUrl);
208
- if (!rawBaseUrl) return { ok: false, reason: "missing-base-url", ...descriptor };
209
-
210
- if (isGateway) {
211
- const apiKey = bearerToken(auth.apiKey) ?? bearerToken(headerValue(auth.headers, "authorization"));
212
- if (!apiKey) return { ok: false, reason: "missing-api-key", ...descriptor, baseUrl: rawBaseUrl };
213
- return {
214
- ok: true,
215
- provider: {
216
- kind: "codex-gateway",
217
- route: "codex-gateway",
218
- provider: model.provider,
219
- api: GATEWAY_API,
220
- model: model.id,
221
- baseUrl: rawBaseUrl,
222
- apiKey,
223
- headers: removeSensitiveGatewayHeaders(mergeProviderHeaders(model.headers, auth.headers)),
224
- },
225
- };
226
- }
227
-
228
- const token = bearerToken(auth.apiKey) ?? bearerToken(headerValue(auth.headers, "authorization"));
229
- if (!token) return { ok: false, reason: "missing-token", ...descriptor };
230
- const accountId = headerValue(auth.headers, "chatgpt-account-id") ?? accountIdFromToken(token);
231
- if (!accountId) return { ok: false, reason: "missing-account-id", ...descriptor };
232
- const baseUrl = normalizeCodexBackendBaseUrl(rawBaseUrl);
233
- if (!baseUrl) return { ok: false, reason: "unsupported-backend", ...descriptor };
234
-
235
- const headers = mergeProviderHeaders(model.headers, auth.headers);
236
- return {
237
- ok: true,
238
- provider: {
239
- kind: "native-codex",
240
- route: "openai-codex",
241
- provider: CODEX_PROVIDER,
242
- api: CODEX_API,
243
- model: model.id,
244
- baseUrl,
245
- token,
246
- accountId,
247
- headers,
248
- },
249
- };
250
- }
251
-
252
- export const _codexProviderTest = {
253
- accountIdFromToken,
254
- bearerToken,
255
- clampSessionId,
256
- decodeJwtPayload,
257
- headerValue,
258
- isCodexGatewayModel,
259
- userAgent,
260
- };
1
+ import type { Api, Model, ProviderHeaders } from "@earendil-works/pi-ai";
2
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
3
+ import { mergeProviderHeaders } from "../provider-headers";
4
+ import {
5
+ CODEX_AFFINITY_SCOPE,
6
+ CODEX_CLIENT_VERSION,
7
+ CODEX_GATEWAY_ORIGINATOR,
8
+ buildCodexCliUserAgent,
9
+ filterCodexGatewayHeaders,
10
+ } from "../responses-headers";
11
+ export { CODEX_AFFINITY_SCOPE } from "../responses-headers";
12
+ import { normalizeBaseUrl } from "../runtime";
13
+ import { isExactModelAllowed } from "../model-scope";
14
+ import {
15
+ isNonEmptyString,
16
+ type CodexContextProvider,
17
+ type CodexContextProviderResolution,
18
+ } from "./types";
19
+
20
+ const CODEX_PROVIDER = "openai-codex";
21
+ const CODEX_API = "openai-codex-responses";
22
+ const GATEWAY_API = "openai-responses";
23
+ const CODEX_TOOL_ORIGINATOR = CODEX_GATEWAY_ORIGINATOR;
24
+ const CODEX_TOOL_VERSION = "0.0.0";
25
+ const CODEX_SESSION_ID_MAX_LENGTH = 64;
26
+
27
+ export const CODEX_CONTEXT_PROVIDER_ERROR =
28
+ "Remote Context management requires a configured Codex-compatible backend";
29
+
30
+ export function isNativeCodexModel(
31
+ model: ExtensionContext["model"] | undefined,
32
+ ): boolean {
33
+ return model?.provider === CODEX_PROVIDER && model.api === CODEX_API;
34
+ }
35
+
36
+ export function isCodexGatewayModel(
37
+ model: ExtensionContext["model"] | undefined,
38
+ gatewayModels: readonly string[] = [],
39
+ ): boolean {
40
+ // Gateway coverage is an operator allowlist of exact "provider/model" keys
41
+ // (compaction.gatewayContextModels). The backend relay must actually pass
42
+ // hosted window markers through; models outside the list stay on remote
43
+ // compaction v2.
44
+ if (!model || model.api !== GATEWAY_API) return false;
45
+ return isExactModelAllowed(model, gatewayModels);
46
+ }
47
+
48
+ export function normalizeCodexBackendBaseUrl(baseUrl: string | undefined | null): string | undefined {
49
+ const normalized = normalizeBaseUrl(baseUrl);
50
+ if (!normalized) return undefined;
51
+ try {
52
+ const url = new URL(normalized);
53
+ const pathname = url.pathname.replace(/\/+$/, "");
54
+ if (pathname.endsWith("/codex/responses")) {
55
+ url.pathname = pathname.slice(0, -"/responses".length);
56
+ return url.toString().replace(/\/$/, "");
57
+ }
58
+ if (pathname.endsWith("/codex")) return normalized;
59
+ if (pathname.endsWith("/backend-api") || pathname.endsWith("/api")) {
60
+ return `${normalized}/codex`;
61
+ }
62
+ if (pathname === "") return `${normalized}/api/codex`;
63
+ if (pathname === "/") return `${normalized}/api/codex`;
64
+ return undefined;
65
+ } catch {
66
+ return undefined;
67
+ }
68
+ }
69
+
70
+ function headerValue(headers: ProviderHeaders | undefined, name: string): string | undefined {
71
+ const wanted = name.toLowerCase();
72
+ for (const [key, value] of Object.entries(headers ?? {})) {
73
+ if (key.toLowerCase() === wanted && typeof value === "string" && value.trim()) {
74
+ return value.trim();
75
+ }
76
+ }
77
+ return undefined;
78
+ }
79
+
80
+ function bearerToken(value: string | undefined): string | undefined {
81
+ if (!value) return undefined;
82
+ const match = value.trim().match(/^Bearer\s+(.+)$/i);
83
+ return (match?.[1] ?? value).trim() || undefined;
84
+ }
85
+
86
+ function decodeJwtPayload(token: string): Record<string, unknown> | undefined {
87
+ const parts = token.split(".");
88
+ if (parts.length !== 3) return undefined;
89
+ try {
90
+ const value: unknown = JSON.parse(Buffer.from(parts[1]!, "base64url").toString("utf8"));
91
+ return value && typeof value === "object" && !Array.isArray(value)
92
+ ? value as Record<string, unknown>
93
+ : undefined;
94
+ } catch {
95
+ return undefined;
96
+ }
97
+ }
98
+
99
+ function accountIdFromToken(token: string): string | undefined {
100
+ const payload = decodeJwtPayload(token);
101
+ const direct = payload?.chatgpt_account_id;
102
+ if (isNonEmptyString(direct)) return direct.trim();
103
+ const auth = payload?.["https://api.openai.com/auth"];
104
+ if (!auth || typeof auth !== "object" || Array.isArray(auth)) return undefined;
105
+ const nested = (auth as Record<string, unknown>).chatgpt_account_id;
106
+ return isNonEmptyString(nested) ? nested.trim() : undefined;
107
+ }
108
+
109
+ function userAgent(version: string): string {
110
+ return buildCodexCliUserAgent(version);
111
+ }
112
+
113
+ function clampSessionId(value: string | undefined): string | undefined {
114
+ if (!value) return undefined;
115
+ const chars = Array.from(value.trim());
116
+ if (chars.length === 0) return undefined;
117
+ return chars.length <= CODEX_SESSION_ID_MAX_LENGTH
118
+ ? chars.join("")
119
+ : chars.slice(0, CODEX_SESSION_ID_MAX_LENGTH).join("");
120
+ }
121
+
122
+ function resolveContextSessionId(ctx: ExtensionContext): string | undefined {
123
+ try {
124
+ return clampSessionId(ctx.sessionManager.getSessionId());
125
+ } catch {
126
+ return undefined;
127
+ }
128
+ }
129
+
130
+ export type CodexContextProviderHeaderOptions = {
131
+ sessionId?: string;
132
+ clientRequestId?: string;
133
+ };
134
+
135
+ export function codexContextProviderHeaders(
136
+ provider: CodexContextProvider,
137
+ options: CodexContextProviderHeaderOptions = {},
138
+ ): Headers {
139
+ const headers = new Headers();
140
+ for (const [name, value] of Object.entries(provider.headers)) {
141
+ if (typeof value === "string") headers.set(name, value);
142
+ }
143
+
144
+ if (provider.kind === "native-codex") {
145
+ headers.set("Authorization", `Bearer ${provider.token}`);
146
+ headers.set("ChatGPT-Account-ID", provider.accountId);
147
+ headers.set("originator", CODEX_TOOL_ORIGINATOR);
148
+ headers.set("User-Agent", userAgent(CODEX_TOOL_VERSION));
149
+ headers.set("version", CODEX_TOOL_VERSION);
150
+ } else {
151
+ headers.set("Authorization", `Bearer ${provider.apiKey}`);
152
+ headers.delete("ChatGPT-Account-ID");
153
+ headers.delete("Cookie");
154
+ headers.set("originator", CODEX_GATEWAY_ORIGINATOR);
155
+ headers.set("User-Agent", userAgent(CODEX_CLIENT_VERSION));
156
+ headers.set("version", CODEX_CLIENT_VERSION);
157
+ headers.set("X-Codex-Affinity-Scope", CODEX_AFFINITY_SCOPE);
158
+ headers.set("X-Codex-Model", provider.model);
159
+ }
160
+
161
+ const sessionId = clampSessionId(options.sessionId);
162
+ if (sessionId) {
163
+ headers.set("Session-Id", sessionId);
164
+ headers.set("X-Client-Request-Id", clampSessionId(options.clientRequestId) ?? sessionId);
165
+ }
166
+ headers.set("content-type", "application/json");
167
+ return headers;
168
+ }
169
+
170
+ export async function resolveCodexContextProvider(
171
+ ctx: ExtensionContext,
172
+ modelOverride: ExtensionContext["model"] = ctx.model,
173
+ gatewayModels: readonly string[] = [],
174
+ ): Promise<CodexContextProviderResolution> {
175
+ const model = modelOverride;
176
+ const descriptor = {
177
+ provider: model?.provider,
178
+ api: model?.api,
179
+ model: model?.id,
180
+ baseUrl: normalizeBaseUrl(model?.baseUrl),
181
+ };
182
+ if (!model) return { ok: false, reason: "unsupported-model" };
183
+
184
+ const isNative = isNativeCodexModel(model);
185
+ const isGateway = isCodexGatewayModel(model, gatewayModels);
186
+ if (!isNative && !isGateway) {
187
+ return { ok: false, reason: "unsupported-model", ...descriptor };
188
+ }
189
+ if (!isNative && model.api !== GATEWAY_API) {
190
+ return { ok: false, reason: "unsupported-api", ...descriptor };
191
+ }
192
+ if (isNative && model.api !== CODEX_API) {
193
+ return { ok: false, reason: "unsupported-api", ...descriptor };
194
+ }
195
+
196
+ let auth: Awaited<ReturnType<typeof ctx.modelRegistry.getApiKeyAndHeaders>>;
197
+ try {
198
+ auth = await ctx.modelRegistry.getApiKeyAndHeaders(model as Model<Api>);
199
+ } catch {
200
+ return { ok: false, reason: "auth-resolution-failed", ...descriptor };
201
+ }
202
+ if (!auth.ok) return { ok: false, reason: "auth-resolution-failed", ...descriptor };
203
+
204
+ const rawBaseUrl = normalizeBaseUrl(auth.baseUrl) ?? normalizeBaseUrl(model.baseUrl);
205
+ if (!rawBaseUrl) return { ok: false, reason: "missing-base-url", ...descriptor };
206
+
207
+ if (isGateway) {
208
+ if (!resolveContextSessionId(ctx)) {
209
+ return { ok: false, reason: "missing-session-id", ...descriptor, baseUrl: rawBaseUrl };
210
+ }
211
+ const apiKey = bearerToken(auth.apiKey) ?? bearerToken(headerValue(auth.headers, "authorization"));
212
+ if (!apiKey) return { ok: false, reason: "missing-api-key", ...descriptor, baseUrl: rawBaseUrl };
213
+ return {
214
+ ok: true,
215
+ provider: {
216
+ kind: "codex-gateway",
217
+ route: "codex-gateway",
218
+ provider: model.provider,
219
+ api: GATEWAY_API,
220
+ model: model.id,
221
+ baseUrl: rawBaseUrl,
222
+ apiKey,
223
+ headers: filterCodexGatewayHeaders(mergeProviderHeaders(model.headers, auth.headers)),
224
+ },
225
+ };
226
+ }
227
+
228
+ const token = bearerToken(auth.apiKey) ?? bearerToken(headerValue(auth.headers, "authorization"));
229
+ if (!token) return { ok: false, reason: "missing-token", ...descriptor };
230
+ const accountId = headerValue(auth.headers, "chatgpt-account-id") ?? accountIdFromToken(token);
231
+ if (!accountId) return { ok: false, reason: "missing-account-id", ...descriptor };
232
+ const baseUrl = normalizeCodexBackendBaseUrl(rawBaseUrl);
233
+ if (!baseUrl) return { ok: false, reason: "unsupported-backend", ...descriptor };
234
+
235
+ const headers = mergeProviderHeaders(model.headers, auth.headers);
236
+ return {
237
+ ok: true,
238
+ provider: {
239
+ kind: "native-codex",
240
+ route: "openai-codex",
241
+ provider: CODEX_PROVIDER,
242
+ api: CODEX_API,
243
+ model: model.id,
244
+ baseUrl,
245
+ token,
246
+ accountId,
247
+ headers,
248
+ },
249
+ };
250
+ }
251
+
252
+ export const _codexProviderTest = {
253
+ accountIdFromToken,
254
+ bearerToken,
255
+ clampSessionId,
256
+ decodeJwtPayload,
257
+ headerValue,
258
+ isCodexGatewayModel,
259
+ userAgent,
260
+ };