Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(oauth): send client_version when listing ChatGPT models

aharon22c919c889bf
diff --git a/apps/desktop/electron/main/vendor-live-models.ts b/apps/desktop/electron/main/vendor-live-models.ts
index 5664d3874..f96c64821 100644
--- a/apps/desktop/electron/main/vendor-live-models.ts
+++ b/apps/desktop/electron/main/vendor-live-models.ts
@@ -9,6 +9,20 @@
const LIVE_MODELS_TIMEOUT_MS = 8_000;
const MAX_RETRY_DELAY_MS = 1_000;
+/**
+ * `client_version` sent to ChatGPT's `GET /codex/models`.
+ *
+ * The endpoint rejects a request without it (HTTP 400, "client_version Field
+ * required"). The value may also affect which models the list includes, so
+ * keep it at a current Codex CLI release.
+ * pi-ai does not call this endpoint and exposes no Codex client version, so
+ * this is the version of the official Codex CLI (github.com/openai/codex)
+ * that was verified to list the current ChatGPT models (0.159.2 lists
+ * `gpt-6.1-sol`, 2026-09-30). Bump it to a newer Codex CLI release when a
+ * model the account can use is missing from this list.
+ */
+export const CODEX_MODELS_CLIENT_VERSION = "0.159.2";
+
const NON_CONVERSATION_MODEL =
/(?:^|[-_/])(?:imagine|image|video|tts|stt|speech|embed(?:ding)?|whisper|aurora|flux|realtime|moderation)(?:$|[-_/])/i;
@@ -118,7 +132,7 @@ export function vendorModelListRequest(input: {
const accountId = chatgptAccountId(apiKey);
if (!accountId) return undefined;
return {
- url: `${base}/codex/models`,
+ url: `${base}/codex/models?client_version=${encodeURIComponent(CODEX_MODELS_CLIENT_VERSION)}`,
accountBaseUrl: base,
allowPolicyFallback: false,
headers: {
diff --git a/apps/desktop/test/vendor-live-models.test.mjs b/apps/desktop/test/vendor-live-models.test.mjs
index 3e703a511..6fe7cc027 100644
--- a/apps/desktop/test/vendor-live-models.test.mjs
+++ b/apps/desktop/test/vendor-live-models.test.mjs
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import {
+ CODEX_MODELS_CLIENT_VERSION,
parseVendorModelIds,
pinnedSiblingId,
vendorModelListRequest,
@@ -20,7 +21,11 @@ test("ChatGPT accounts list models from the Codex endpoint, not /models", () =>
vendorId: "openai-codex",
apiKey: codexToken(),
});
- assert.equal(request?.url, "https://chatgpt.com/backend-api/codex/models");
+ const url = new URL(request?.url ?? "");
+ assert.equal(`${url.origin}${url.pathname}`, "https://chatgpt.com/backend-api/codex/models");
+ // The endpoint answers 400 "client_version Field required" without it.
+ assert.equal(url.searchParams.get("client_version"), CODEX_MODELS_CLIENT_VERSION);
+ assert.match(CODEX_MODELS_CLIENT_VERSION, /^\d+\.\d+\.\d+$/);
assert.equal(request?.headers["chatgpt-account-id"], "acct_123");
assert.equal(parseVendorModelIds("openai-codex", {
models: [
@@ -108,3 +113,4 @@ test("Copilot only keeps a new id when its family has one wire API", () => {
"openai-codex-responses",
);
});
+
diff --git a/apps/desktop/test/vendor-oauth-login.test.mjs b/apps/desktop/test/vendor-oauth-login.test.mjs
index 6a1855d81..212c94ad4 100644
--- a/apps/desktop/test/vendor-oauth-login.test.mjs
+++ b/apps/desktop/test/vendor-oauth-login.test.mjs
@@ -769,3 +769,68 @@ test("a new Grok inherits grok-4.6 even when an older Grok is first in the pin",
assert.equal(binding.modelConfig.maxTokens, 500_000);
assert.deepEqual(binding.supportedThinkingLevels, ["low", "medium", "high", "xhigh"]);
});
+
+function codexProvider() {
+ return {
+ id: "openai-codex",
+ name: "ChatGPT",
+ baseUrl: "https://chatgpt.com/backend-api",
+ auth: { oauth: { name: "ChatGPT Plus/Pro", isSubscription: true, loginLabel: "Sign in" } },
+ };
+}
+
+function codexModel(id) {
+ return {
+ id,
+ name: id,
+ api: "openai-codex-responses",
+ provider: "openai-codex",
+ baseUrl: "https://chatgpt.com/backend-api",
+ input: ["text"],
+ reasoning: true,
+ thinkingLevelMap: { off: null, low: "low", medium: "medium", high: "high" },
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
+ contextWindow: 272_000,
+ maxTokens: 128_000,
+ };
+}
+
+/** A pasted code that makes `access-for-<code>` a ChatGPT-shaped JWT. */
+function codexLoginCode() {
+ const payload = Buffer.from(JSON.stringify({
+ "https://api.openai.com/auth": { chatgpt_account_id: "acct_123" },
+ })).toString("base64url");
+ return `h.${payload}.sig`;
+}
+
+test("a ChatGPT account lists the models /codex/models returns for its client version", async () => {
+ const seen = [];
+ // The live endpoint rejects a request without client_version.
+ const fetchModels = async (input) => {
+ const url = new URL(String(input));
+ seen.push(url);
+ if (!url.searchParams.get("client_version")) {
+ return new Response(JSON.stringify({
+ detail: [{ loc: ["query", "client_version"], msg: "Field required" }],
+ }), { status: 400 });
+ }
+ return new Response(JSON.stringify({
+ models: [{ slug: "gpt-6-luna", visibility: "list" }, { slug: "gpt-6.1-sol", visibility: "list" }],
+ }), { status: 200, headers: { "content-type": "application/json" } });
+ };
+ const { host, events, oauth } = harness({
+ fetch: fetchModels,
+ provider: codexProvider(),
+ models: [codexModel("gpt-6-luna")],
+ });
+ const { loginId } = await oauth.start("openai-codex");
+ const prompt = await waitFor(events, "prompt");
+ oauth.respond({ loginId, promptId: prompt.request.promptId, value: codexLoginCode() });
+ const done = await waitFor(events, "done");
+ const row = host.providers.get(done.providerId);
+ assert.deepEqual(row.models.map((model) => model.id), ["gpt-6-luna", "gpt-6.1-sol"]);
+ assert.equal(seen[0].pathname, "/backend-api/codex/models");
+ const binding = await oauth.bindingFor(done.providerId, "gpt-6.1-sol");
+ assert.equal(binding.baseUrl, "https://chatgpt.com/backend-api");
+});
+
diff --git a/docs/spec/03-runtime/11-provider-model-system.md b/docs/spec/03-runtime/11-provider-model-system.md
index cf31aefc8..adbf1a411 100644
--- a/docs/spec/03-runtime/11-provider-model-system.md
+++ b/docs/spec/03-runtime/11-provider-model-system.md
@@ -497,10 +497,13 @@ and the connection test still proves the account by resolving auth. pi-ai
when the account request fails or the payload is not a model list. The probe
is the endpoint that vendor actually publishes:
-- ChatGPT Plus/Pro (`openai-codex`): `GET {base}/codex/models`, with the
- account id taken from the access token. A `{ data: [...] }` payload is not
- accepted. A newly published id such as `gpt-6-luna` is selectable without a
- client update when that response includes it.
+- ChatGPT Plus/Pro (`openai-codex`): `GET {base}/codex/models?client_version=…`,
+ with the account id taken from the access token. The endpoint requires
+ `client_version` and hides models whose minimum Codex client is newer, so
+ the value is a pinned Codex CLI version (`CODEX_MODELS_CLIENT_VERSION`) that
+ is bumped when an account model goes missing. A `{ data: [...] }` payload is
+ not accepted. A newly published id such as `gpt-6-luna` is selectable
+ without a client update when that response includes it.
- GitHub Copilot: `GET {base}/models` with the pinned IDE identity headers and
`X-GitHub-Api-Version`. Only ids with `model_picker_enabled === true` (and
not policy-disabled) are kept. An id the pin does not know is added only when
diff --git a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
index 1a00893c7..4c0d3ed3c 100644
--- a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
+++ b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
@@ -406,9 +406,11 @@ sidecar 请求
这类行的模型发现读取已登录账户自己的模型列表;连接测试仍通过解析认证来证明
账户。请求失败,或返回的不是模型列表时,才回退到 pi-ai(`models.getAvailable`,
含厂商自己的 `filterModels`)。各厂商打自己的接口:ChatGPT Plus/Pro
-(`openai-codex`)是 `GET {base}/codex/models`,因此 `gpt-6-luna` 这类账户
-已经提供、pin 里还没有的 id 也能出现;普通 `{ data: [...] }` 不当成 Codex
-列表。Copilot 是带 IDE 身份头和 `X-GitHub-Api-Version` 的 `GET {base}/models`,
+(`openai-codex`)是 `GET {base}/codex/models?client_version=…`,因此
+`gpt-6-luna` 这类账户已经提供、pin 里还没有的 id 也能出现;该接口要求
+`client_version`,并隐藏最低 Codex 客户端版本更高的模型,所以取值是固定的
+Codex CLI 版本(`CODEX_MODELS_CLIENT_VERSION`),账户模型缺失时调高;普通
+`{ data: [...] }` 不当成 Codex 列表。Copilot 是带 IDE 身份头和 `X-GitHub-Api-Version` 的 `GET {base}/models`,
只保留 `model_picker_enabled === true` 且未被策略禁用的 id,pin 不认识的 id
只有在其家族已经对应唯一线路 API 时才加入。Anthropic 用 OAuth 身份头请求
`GET {base}/v1/models`。Kimi、Meta、xAI、OpenRouter 请求 `GET {base}/models`