Skip to content

Commit 90801ba

Browse files
ngutmansaamuelng601-pixelhclsys
authored
fix(openai-codex): normalize stale transport metadata in resolution and discovery (#67635)
Merged via squash. Supersedes: - #66969 by @saamuelng601-pixel - #67159 by @hclsys Co-authored-by: saamuelng601-pixel <[email protected]> Co-authored-by: hclsys <[email protected]>
1 parent f697b01 commit 90801ba

8 files changed

Lines changed: 245 additions & 20 deletions

File tree

‎extensions/openai/base-url.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ describe("openai base URL helpers", () => {
1717
it("recognizes Codex ChatGPT backend routes", () => {
1818
expect(isOpenAICodexBaseUrl("https://chatgpt.com/backend-api")).toBe(true);
1919
expect(isOpenAICodexBaseUrl("https://chatgpt.com/backend-api/")).toBe(true);
20+
expect(isOpenAICodexBaseUrl("https://chatgpt.com/backend-api/v1")).toBe(true);
21+
expect(isOpenAICodexBaseUrl("https://chatgpt.com/backend-api/v1/")).toBe(true);
2022
});
2123

2224
it("rejects non-Codex backend routes", () => {
2325
expect(isOpenAICodexBaseUrl("https://api.openai.com/v1")).toBe(false);
2426
expect(isOpenAICodexBaseUrl("https://chatgpt.com")).toBe(false);
27+
expect(isOpenAICodexBaseUrl("https://chatgpt.com/backend-api/v2")).toBe(false);
28+
expect(isOpenAICodexBaseUrl("https://chatgpt.com/backend-api/codex")).toBe(false);
2529
expect(isOpenAICodexBaseUrl(undefined)).toBe(false);
2630
});
2731
});

‎extensions/openai/base-url.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export function isOpenAICodexBaseUrl(baseUrl?: string): boolean {
1313
if (!trimmed) {
1414
return false;
1515
}
16-
return /^https?:\/\/chatgpt\.com\/backend-api\/?$/i.test(trimmed);
16+
return /^https?:\/\/chatgpt\.com\/backend-api(?:\/v1)?\/?$/i.test(trimmed);
1717
}

‎extensions/openai/openai-codex-provider.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,70 @@ describe("openai codex provider", () => {
375375
name: "gpt-5.4",
376376
});
377377
});
378+
379+
it("defaults missing codex api metadata to openai-codex-responses", () => {
380+
const provider = buildOpenAICodexProviderPlugin();
381+
382+
const model = provider.normalizeResolvedModel?.({
383+
provider: "openai-codex",
384+
model: {
385+
id: "gpt-5.4",
386+
name: "gpt-5.4",
387+
provider: "openai-codex",
388+
baseUrl: "https://chatgpt.com/backend-api",
389+
reasoning: true,
390+
input: ["text", "image"],
391+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
392+
contextWindow: 1_050_000,
393+
contextTokens: 272_000,
394+
maxTokens: 128_000,
395+
},
396+
} as never);
397+
398+
expect(model).toMatchObject({
399+
api: "openai-codex-responses",
400+
baseUrl: "https://chatgpt.com/backend-api",
401+
});
402+
});
403+
404+
it("normalizes stale /backend-api/v1 codex metadata to the canonical base url", () => {
405+
const provider = buildOpenAICodexProviderPlugin();
406+
407+
const model = provider.normalizeResolvedModel?.({
408+
provider: "openai-codex",
409+
model: {
410+
id: "gpt-5.4",
411+
name: "gpt-5.4",
412+
provider: "openai-codex",
413+
api: "openai-codex-responses",
414+
baseUrl: "https://chatgpt.com/backend-api/v1",
415+
reasoning: true,
416+
input: ["text", "image"],
417+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
418+
contextWindow: 1_050_000,
419+
contextTokens: 272_000,
420+
maxTokens: 128_000,
421+
},
422+
} as never);
423+
424+
expect(model).toMatchObject({
425+
api: "openai-codex-responses",
426+
baseUrl: "https://chatgpt.com/backend-api",
427+
});
428+
});
429+
430+
it("normalizes transport metadata for stale /backend-api/v1 codex routes", () => {
431+
const provider = buildOpenAICodexProviderPlugin();
432+
433+
expect(
434+
provider.normalizeTransport?.({
435+
provider: "openai-codex",
436+
api: "openai-codex-responses",
437+
baseUrl: "https://chatgpt.com/backend-api/v1",
438+
} as never),
439+
).toEqual({
440+
api: "openai-codex-responses",
441+
baseUrl: "https://chatgpt.com/backend-api",
442+
});
443+
});
378444
});

‎extensions/openai/openai-codex-provider.ts‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ const OPENAI_CODEX_MODERN_MODEL_IDS = [
9494
OPENAI_CODEX_GPT_53_MODEL_ID,
9595
OPENAI_CODEX_GPT_53_SPARK_MODEL_ID,
9696
] as const;
97+
98+
function normalizeCodexTransportFields(params: {
99+
api?: ProviderRuntimeModel["api"] | null;
100+
baseUrl?: string;
101+
}): {
102+
api?: ProviderRuntimeModel["api"];
103+
baseUrl?: string;
104+
} {
105+
const useCodexTransport =
106+
!params.baseUrl || isOpenAIApiBaseUrl(params.baseUrl) || isOpenAICodexBaseUrl(params.baseUrl);
107+
const api =
108+
useCodexTransport && (!params.api || params.api === "openai-responses")
109+
? "openai-codex-responses"
110+
: (params.api ?? undefined);
111+
const baseUrl =
112+
api === "openai-codex-responses" && useCodexTransport ? OPENAI_CODEX_BASE_URL : params.baseUrl;
113+
return { api, baseUrl };
114+
}
115+
97116
function normalizeCodexTransport(model: ProviderRuntimeModel): ProviderRuntimeModel {
98117
const lowerModelId = normalizeLowercaseStringOrEmpty(model.id);
99118
const canonicalModelId =
@@ -102,14 +121,12 @@ function normalizeCodexTransport(model: ProviderRuntimeModel): ProviderRuntimeMo
102121
normalizeLowercaseStringOrEmpty(model.name) === OPENAI_CODEX_GPT_54_LEGACY_MODEL_ID
103122
? OPENAI_CODEX_GPT_54_MODEL_ID
104123
: model.name;
105-
const useCodexTransport =
106-
!model.baseUrl || isOpenAIApiBaseUrl(model.baseUrl) || isOpenAICodexBaseUrl(model.baseUrl);
107-
const api =
108-
useCodexTransport && model.api === "openai-responses" ? "openai-codex-responses" : model.api;
109-
const baseUrl =
110-
api === "openai-codex-responses" && (!model.baseUrl || isOpenAIApiBaseUrl(model.baseUrl))
111-
? OPENAI_CODEX_BASE_URL
112-
: model.baseUrl;
124+
const normalizedTransport = normalizeCodexTransportFields({
125+
api: model.api,
126+
baseUrl: model.baseUrl,
127+
});
128+
const api = normalizedTransport.api ?? model.api;
129+
const baseUrl = normalizedTransport.baseUrl ?? model.baseUrl;
113130
if (
114131
api === model.api &&
115132
baseUrl === model.baseUrl &&
@@ -335,6 +352,16 @@ export function buildOpenAICodexProviderPlugin(): ProviderPlugin {
335352
}
336353
return normalizeCodexTransport(ctx.model);
337354
},
355+
normalizeTransport: ({ provider, api, baseUrl }) => {
356+
if (normalizeProviderId(provider) !== PROVIDER_ID) {
357+
return undefined;
358+
}
359+
const normalized = normalizeCodexTransportFields({ api, baseUrl });
360+
if (normalized.api === api && normalized.baseUrl === baseUrl) {
361+
return undefined;
362+
}
363+
return normalized;
364+
},
338365
resolveUsageAuth: async (ctx) => await ctx.resolveOAuthToken(),
339366
fetchUsageSnapshot: async (ctx) =>
340367
await fetchCodexUsage(ctx.token, ctx.accountId, ctx.timeoutMs, ctx.fetchFn),

‎src/agents/pi-embedded-runner/model.provider-runtime.test-support.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OpenRouterModelCapabilities } from "./openrouter-model-capabilitie
33

44
const OPENAI_BASE_URL = "https://api.openai.com/v1";
55
const OPENAI_CODEX_BASE_URL = "https://chatgpt.com/backend-api";
6+
const OPENAI_CODEX_LEGACY_BASE_URL = "https://chatgpt.com/backend-api/v1";
67
const OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1";
78
const ANTHROPIC_BASE_URL = "https://api.anthropic.com";
89
const XAI_BASE_URL = "https://api.x.ai/v1";
@@ -64,20 +65,23 @@ function cloneTemplate(
6465
} as ResolvedModelLike;
6566
}
6667

68+
function isNativeOpenAICodexBaseUrl(baseUrl?: string): boolean {
69+
return baseUrl === OPENAI_CODEX_BASE_URL || baseUrl === OPENAI_CODEX_LEGACY_BASE_URL;
70+
}
71+
6772
function normalizeDynamicModel(params: { provider: string; model: ResolvedModelLike }) {
6873
if (params.provider !== "openai-codex") {
6974
return undefined;
7075
}
7176
const baseUrl = typeof params.model.baseUrl === "string" ? params.model.baseUrl : undefined;
77+
const useCodexTransport =
78+
!baseUrl || baseUrl === OPENAI_BASE_URL || isNativeOpenAICodexBaseUrl(baseUrl);
7279
const nextApi =
73-
params.model.api === "openai-responses" &&
74-
(!baseUrl || baseUrl === OPENAI_BASE_URL || baseUrl === OPENAI_CODEX_BASE_URL)
80+
useCodexTransport && (!params.model.api || params.model.api === "openai-responses")
7581
? "openai-codex-responses"
7682
: params.model.api;
7783
const nextBaseUrl =
78-
nextApi === "openai-codex-responses" && (!baseUrl || baseUrl === OPENAI_BASE_URL)
79-
? OPENAI_CODEX_BASE_URL
80-
: baseUrl;
84+
nextApi === "openai-codex-responses" && useCodexTransport ? OPENAI_CODEX_BASE_URL : baseUrl;
8185
if (nextApi !== params.model.api || nextBaseUrl !== baseUrl) {
8286
return { ...params.model, api: nextApi, baseUrl: nextBaseUrl };
8387
}
@@ -96,6 +100,14 @@ function normalizeTransport(params: {
96100
params.context.api === "openai-completions" &&
97101
(params.context.baseUrl === XAI_BASE_URL ||
98102
(params.provider === "xai" && !params.context.baseUrl));
103+
const isNativeOpenAICodexTransport =
104+
params.provider === "openai-codex" &&
105+
((!params.context.api &&
106+
(!params.context.baseUrl || isNativeOpenAICodexBaseUrl(params.context.baseUrl))) ||
107+
(params.context.api === "openai-responses" &&
108+
(!params.context.baseUrl ||
109+
params.context.baseUrl === OPENAI_BASE_URL ||
110+
isNativeOpenAICodexBaseUrl(params.context.baseUrl))));
99111
if (
100112
params.context.api === "google-generative-ai" &&
101113
params.context.baseUrl === "https://generativelanguage.googleapis.com"
@@ -117,6 +129,12 @@ function normalizeTransport(params: {
117129
baseUrl: params.context.baseUrl,
118130
};
119131
}
132+
if (isNativeOpenAICodexTransport) {
133+
return {
134+
api: "openai-codex-responses",
135+
baseUrl: OPENAI_CODEX_BASE_URL,
136+
};
137+
}
120138
return undefined;
121139
}
122140

‎src/agents/pi-embedded-runner/model.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,50 @@ describe("resolveModel", () => {
10691069
});
10701070
});
10711071

1072+
it("normalizes stale discovered openai-codex /backend-api/v1 metadata", () => {
1073+
mockDiscoveredModel(discoverModels, {
1074+
provider: "openai-codex",
1075+
modelId: "gpt-5.4",
1076+
templateModel: {
1077+
...buildOpenAICodexForwardCompatExpectation("gpt-5.4"),
1078+
name: "GPT-5.4",
1079+
baseUrl: "https://chatgpt.com/backend-api/v1",
1080+
},
1081+
});
1082+
1083+
const result = resolveModelForTest("openai-codex", "gpt-5.4", "/tmp/agent");
1084+
1085+
expect(result.error).toBeUndefined();
1086+
expect(result.model).toMatchObject({
1087+
provider: "openai-codex",
1088+
id: "gpt-5.4",
1089+
api: "openai-codex-responses",
1090+
baseUrl: "https://chatgpt.com/backend-api",
1091+
});
1092+
});
1093+
1094+
it("normalizes discovered openai-codex metadata when api is missing", () => {
1095+
mockDiscoveredModel(discoverModels, {
1096+
provider: "openai-codex",
1097+
modelId: "gpt-5.4",
1098+
templateModel: {
1099+
...buildOpenAICodexForwardCompatExpectation("gpt-5.4"),
1100+
name: "GPT-5.4",
1101+
api: undefined,
1102+
},
1103+
});
1104+
1105+
const result = resolveModelForTest("openai-codex", "gpt-5.4", "/tmp/agent");
1106+
1107+
expect(result.error).toBeUndefined();
1108+
expect(result.model).toMatchObject({
1109+
provider: "openai-codex",
1110+
id: "gpt-5.4",
1111+
api: "openai-codex-responses",
1112+
baseUrl: "https://chatgpt.com/backend-api",
1113+
});
1114+
});
1115+
10721116
it("passes configured workspaceDir to runtime preference hooks", () => {
10731117
mockDiscoveredModel(discoverModels, {
10741118
provider: "openai-codex",

‎src/agents/pi-model-discovery.auth.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describe, expect, it } from "vitest";
55
import { resolvePiCredentialMapFromStore } from "./pi-auth-credentials.js";
66
import {
77
addEnvBackedPiCredentials,
8+
normalizeDiscoveredPiModel,
89
scrubLegacyStaticAuthJsonEntriesForDiscovery,
910
} from "./pi-model-discovery.js";
1011

@@ -153,4 +154,57 @@ describe("discoverAuthStorage", () => {
153154
}
154155
}
155156
});
157+
158+
it("normalizes stale discovered openai-codex rows when api metadata is missing", () => {
159+
const normalized = normalizeDiscoveredPiModel(
160+
{
161+
id: "gpt-5.4",
162+
name: "gpt-5.4",
163+
provider: "openai-codex",
164+
baseUrl: "https://chatgpt.com/backend-api",
165+
reasoning: true,
166+
input: ["text", "image"],
167+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
168+
contextWindow: 1_050_000,
169+
contextTokens: 272_000,
170+
maxTokens: 128_000,
171+
},
172+
"/tmp/agent",
173+
) as {
174+
api?: string;
175+
baseUrl?: string;
176+
};
177+
178+
expect(normalized).toMatchObject({
179+
api: "openai-codex-responses",
180+
baseUrl: "https://chatgpt.com/backend-api",
181+
});
182+
});
183+
184+
it("canonicalizes stale discovered openai-codex backend-api/v1 rows", () => {
185+
const normalized = normalizeDiscoveredPiModel(
186+
{
187+
id: "gpt-5.4",
188+
name: "gpt-5.4",
189+
provider: "openai-codex",
190+
api: "openai-codex-responses",
191+
baseUrl: "https://chatgpt.com/backend-api/v1",
192+
reasoning: true,
193+
input: ["text", "image"],
194+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
195+
contextWindow: 1_050_000,
196+
contextTokens: 272_000,
197+
maxTokens: 128_000,
198+
},
199+
"/tmp/agent",
200+
) as {
201+
api?: string;
202+
baseUrl?: string;
203+
};
204+
205+
expect(normalized).toMatchObject({
206+
api: "openai-codex-responses",
207+
baseUrl: "https://chatgpt.com/backend-api",
208+
});
209+
});
156210
});

0 commit comments

Comments
 (0)