Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions packages/opencode/src/plugin/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const ISSUER = "https://auth.openai.com"
const CODEX_API_ENDPOINT = "https://chatgpt.com/backend-api/codex/responses"
const OAUTH_PORT = 1455
const OAUTH_POLLING_SAFETY_MARGIN_MS = 3000
const CODEX_OAUTH_ALLOWED_MODELS = new Set([
"gpt-5.1-codex",
"gpt-5.1-codex-max",
"gpt-5.1-codex-mini",
"gpt-5.2",
"gpt-5.2-codex",
"gpt-5.3-codex",
"gpt-5.4",
"gpt-5.4-mini",
])

interface PkceCodes {
verifier: string
Expand Down Expand Up @@ -88,6 +98,16 @@ export function extractAccountId(tokens: TokenResponse): string | undefined {
return undefined
}

export function shouldKeepCodexOAuthModel(modelId: string, apiId: string): boolean {
if (modelId.includes("codex")) return true
if (CODEX_OAUTH_ALLOWED_MODELS.has(apiId)) return true
const match = apiId.match(/^gpt-(\d+)\.(\d+)/)
if (!match) return false
const major = Number(match[1])
const minor = Number(match[2])
return major > 5 || (major === 5 && minor > 4)
}

function buildAuthorizeUrl(redirectUri: string, pkce: PkceCodes, state: string): string {
const params = new URLSearchParams({
response_type: "code",
Expand Down Expand Up @@ -365,20 +385,8 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
const auth = await getAuth()
if (auth.type !== "oauth") return {}

// Filter models to only allowed Codex models for OAuth
const allowedModels = new Set([
"gpt-5.1-codex",
"gpt-5.1-codex-max",
"gpt-5.1-codex-mini",
"gpt-5.2",
"gpt-5.2-codex",
"gpt-5.3-codex",
"gpt-5.4",
"gpt-5.4-mini",
])
for (const [modelId, model] of Object.entries(provider.models)) {
if (modelId.includes("codex")) continue
if (allowedModels.has(model.api.id)) continue
if (shouldKeepCodexOAuthModel(modelId, model.api.id)) continue
delete provider.models[modelId]
}

Expand Down
21 changes: 21 additions & 0 deletions packages/opencode/test/plugin/codex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
parseJwtClaims,
extractAccountIdFromClaims,
extractAccountId,
shouldKeepCodexOAuthModel,
type IdTokenClaims,
} from "../../src/plugin/codex"

Expand Down Expand Up @@ -120,4 +121,24 @@ describe("plugin.codex", () => {
).toBe("acc-123")
})
})

describe("shouldKeepCodexOAuthModel", () => {
test("keeps explicit OAuth models and codex model ids", () => {
expect(shouldKeepCodexOAuthModel("gpt-5.4", "gpt-5.4")).toBe(true)
expect(shouldKeepCodexOAuthModel("some-codex-model", "custom-model")).toBe(true)
})

test("keeps future GPT minor versions using semantic comparison", () => {
expect(shouldKeepCodexOAuthModel("gpt-5.5", "gpt-5.5")).toBe(true)
expect(shouldKeepCodexOAuthModel("gpt-5.5-mini", "gpt-5.5-mini")).toBe(true)
expect(shouldKeepCodexOAuthModel("gpt-5.10", "gpt-5.10")).toBe(true)
expect(shouldKeepCodexOAuthModel("gpt-6.0", "gpt-6.0")).toBe(true)
})

test("drops older non-allowlisted GPT and unrelated models", () => {
expect(shouldKeepCodexOAuthModel("gpt-5.3", "gpt-5.3")).toBe(false)
expect(shouldKeepCodexOAuthModel("gpt-4.1", "gpt-4.1")).toBe(false)
expect(shouldKeepCodexOAuthModel("custom-model", "custom-model")).toBe(false)
})
})
})
Loading