openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

Add Codex OAuth provider support

Open junjunjunbong opened this issue 6 months ago • 5 comments

Summary

  • Adds Codex/ChatGPT OAuth as an OpenAI provider sign-in option in the AI settings flow.
  • Imports an existing Codex CLI login from ~/.codex/auth.json and persists it as the OpenAI OAuth profile.
  • Routes OpenAI OAuth-backed inference through the ChatGPT Codex backend with required account and client-version metadata.
  • Accepts both OpenAI-style { data: [...] } and Codex-style { models: [...] } model catalog responses.
  • Hardens the setup flow so existing API-key credentials are preserved until Codex setup succeeds, localized errors use stable codes, and CodeRabbit review findings are resolved.

Problem

OpenAI provider setup could use API keys, but not the user's existing Codex/ChatGPT OAuth session. After adding the OAuth route, the Codex backend model catalog also rejected requests missing client_version, then returned a { models: [...] } payload that the OpenAI-compatible catalog parser did not accept.

Solution

  • Add OpenAI OAuth RPC operations and UI wiring for importing Codex CLI credentials.
  • Store and refresh OAuth token-set credentials under the OpenAI provider profile.
  • Add provider support for extra query params so Codex OAuth requests include client_version.
  • Normalize provider catalog responses from OpenAI-compatible data[].id, Codex models[].slug, and string model entries.
  • Delay OpenAI API-key clearing until Codex provider flush/persist succeeds, and keep normal API-key routing tolerant of broken OAuth metadata.
  • Add Windows home-directory fallback and structured diagnostics for Codex CLI auth import.

Submission Checklist

If a section does not apply to this change, mark the item as N/A with a one-line reason. Do not delete items.

  • [x] Tests added or updated (happy path + at least one failure / edge case) per Testing Strategy
  • [x] Diff coverage >= 80% — N/A locally: full lcov diff coverage was not run; focused Rust/app tests cover the changed OAuth/catalog paths, and CI remains authoritative.
  • [x] Coverage matrix updated — N/A: provider-auth/model-catalog behavior change, no feature matrix row added or removed.
  • [x] All affected feature IDs from the matrix are listed in the PR description under ## Related — N/A: no coverage-matrix feature ID changed.
  • [x] No new external network dependencies introduced (mock backend used per Testing Strategy)
  • [x] Manual smoke checklist updated if this touches release-cut surfaces (docs/RELEASE-MANUAL-SMOKE.md) — N/A: targeted provider setup fix only.
  • [x] Linked issue closed via Closes #NNN in the ## Related section — N/A: user-reported local regression, no GitHub issue.

Impact

Desktop app users can connect OpenAI through Codex OAuth and list/use Codex backend models without the client_version or missing data array failures. No migration is required; existing API-key OpenAI provider behavior is preserved.

Related

  • Closes: N/A
  • Follow-up PR(s)/TODOs: N/A

AI Authored PR Metadata (required for Codex/Linear PRs)

Keep this section for AI-authored PRs. For human-only PRs, mark each field N/A.

Linear Issue

  • Key: N/A
  • URL: N/A

Commit & Branch

  • Branch: codex/add-codex-oauth-provider
  • Commit SHA: 0db55dc6990c53469e3c6ee965f02ebd4621e922

Validation Run

  • [x] pnpm --filter openhuman-app exec vitest run --config test/vitest.config.ts src/components/settings/panels/__tests__/AIPanel.test.tsx src/services/api/__tests__/aiSettingsApi.test.ts
  • [x] pnpm debug rust openai_oauth
  • [x] pnpm debug rust lookup_key_for_slug_routes_openai_oauth_lookup_path
  • [x] pnpm debug rust model_catalog_accepts; pnpm debug rust openai_codex_models_url_includes_client_version_query; pnpm debug rust extra_query_params_are_applied_to_codex_urls
  • [x] pnpm i18n:check
  • [x] pnpm lint (passes with existing warnings)
  • [x] pnpm typecheck
  • [x] pnpm format:check with Rust toolchain PATH configured
  • [x] GGML_NATIVE=OFF cargo check --manifest-path Cargo.toml
  • [x] Pre-push hook: format:check, lint, compile, rust:check

Validation Blocked

  • command: pnpm i18n:bundle:check
  • error: verify-i18n-bundle: dist directory does not exist or is not a directory: /Users/junwon/Projects/openhuman/app/dist
  • impact: Bundle verification requires a built app/dist; i18n source coverage passed via pnpm i18n:check.

Behavior Changes

  • Intended behavior change: OpenAI Codex OAuth can import Codex CLI credentials and list/use Codex backend models.
  • User-visible effect: AI settings OpenAI/Codex auth no longer fails on model listing due to missing client_version or Codex models response shape.

Parity Contract

  • Legacy behavior preserved: API-key OpenAI and generic OpenAI-compatible providers still use existing auth headers and data model catalogs.
  • Guard/fallback/dispatch parity checks: New query params are opt-in for Codex OAuth; catalog parsing still rejects responses without data or models arrays.

Duplicate / Superseded PR Handling

  • Duplicate PR(s): None found for junjunjunbong:codex/add-codex-oauth-provider.
  • Canonical PR: This PR.
  • Resolution (closed/superseded/updated): N/A

junjunjunbong avatar May 27 '26 09:05 junjunjunbong

[!NOTE]

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • [ ] ▶️ Resume reviews
  • [ ] 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds Codex CLI ChatGPT auth import and account_id-aware OpenAI OAuth handling: import from CODEX_HOME/~/.codex/auth.json, persist OAuth profile with optional account_id, expose RPC/controller/schema for import, route OAuth-backed OpenAI requests via a Codex backend with extra headers/query params, and add frontend UI, API facades, and tests to trigger/import Codex OAuth.

Changes

OpenAI Codex CLI OAuth & Account ID Routing

Layer / File(s) Summary
Flow, RPC handler, controller schema & tests
src/openhuman/inference/openai_oauth/flow.rs, src/openhuman/inference/ops.rs, src/openhuman/inference/schemas.rs, src/openhuman/inference/schemas_tests.rs
Adds import_openai_oauth_from_codex_cli, inference_openai_oauth_import_codex_cli RPC op, public controller schema/handler registration, and schema/tests asserting registration and no inputs.
OAuth store: account_id, JWT helpers, Codex CLI import
src/openhuman/inference/openai_oauth/store.rs
Introduces OpenAiOAuthCredentials { access_token, account_id }, refactors JWT payload decoding, updates persistence to accept/store optional account_id, adds lookup_openai_oauth_credentials, and implements Codex CLI auth.json import (CODEX_HOME/~/ .codex/auth.json) with token normalization and persistence.
Provider: extra headers & query params + tests
src/openhuman/inference/provider/compatible.rs, src/openhuman/inference/provider/compatible_tests.rs
Adds extra_headers and extra_query_params to OpenAiCompatibleProvider, builder helpers with_extra_header/with_extra_query_param, helpers to apply headers and append query params (URL parsing with fallback), ensures streaming requests include extra headers, and unit tests.
Provider ops & factory: OAuth-aware routing and catalog parsing
src/openhuman/inference/provider/ops.rs, src/openhuman/inference/provider/factory.rs, src/openhuman/inference/provider/openai_codex.rs
list_configured_models_from_config detects OpenAI OAuth usage, selects Codex backend when OAuth-active, appends client_version, conditionally includes ChatGPT-Account-ID header, refactors model-catalog parsing to accept OpenAI data, Codex models, and string entries; factory routes openai providers via Codex backend and injects headers/query param when appropriate.
Frontend API facades & tests
app/src/services/api/aiSettingsApi.ts, app/src/services/api/__tests__/aiSettingsApi.test.ts
Adds error-code constants and RPC facades startOpenAiCodexOAuth, completeOpenAiCodexOAuth, importOpenAiCodexCliAuth, types for start result, and tests asserting error behavior for missing auth/callback URLs.
Frontend UI: AIPanel Codex button, connect flow and tests
app/src/components/settings/panels/AIPanel.tsx, app/src/components/settings/panels/__tests__/AIPanel.test.tsx
Adds LuKeyRound icon and a “Codex 인증” button, codexAuthError state, extends connectProvider with credentialMode: 'codex_oauth' (suppresses stored-key writes and skips /models probing), implements connectOpenAiViaCodexAuth that imports Codex CLI auth and persists provider state, clears stored OpenAI key on successful Codex import, and updates/adds tests to validate flows and UI.
Flow & import tests (Rust)
src/openhuman/inference/openai_oauth/flow_tests.rs
Adds tests verifying successful Codex CLI auth import stores OAuth profile and token lookups, and that importing a missing file yields an error suggesting codex login.
i18n: Codex OAuth messages
app/src/lib/i18n/*, app/src/lib/i18n/en.ts
Adds settings.ai.codexOauthMissingAuthUrl and settings.ai.codexOauthMissingCallbackUrl translation entries across many locale chunks and English core.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • tinyhumansai/openhuman#2265: Earlier work on OpenAI OAuth start/complete/status/disconnect and key lookup that this PR extends with Codex CLI import.

Suggested reviewers

  • senamakel
  • graycyrus

"🐰 I found a key in ~/.codex today,
I hopped it in and stowed away,
Tokens tidy, account IDs sewn,
Backends routed, headers grown,
Now connect clicks greet the day!"

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.65% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add Codex OAuth provider support' directly and clearly summarizes the main change: adding Codex OAuth as an OpenAI-provider authentication option.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 27 '26 09:05 coderabbitai[bot]

CI is now green on the latest head (2de8ab37). The earlier Rust Core Coverage failure was a GitHub runner disk issue (), so I pushed an empty rerun commit and the rerun passed, including diff-cover, Rust coverage, Tauri coverage, Linux/Windows/macOS E2E, build, and smoke checks.\n\nCould you please re-review/approve when you get a chance?

junjunjunbong avatar May 27 '26 16:05 junjunjunbong

CI is now green on the latest head (2de8ab37). The earlier Rust Core Coverage failure was a GitHub runner disk issue: No space left on device. I pushed an empty rerun commit and the rerun passed, including diff-cover, Rust coverage, Tauri coverage, Linux/Windows/macOS E2E, build, and smoke checks.

Could you please re-review/approve when you get a chance?

junjunjunbong avatar May 27 '26 16:05 junjunjunbong

@junjunjunbong unresolved review feedback — please address before we review.

graycyrus avatar May 27 '26 19:05 graycyrus

Unresolved review feedback from coderabbitai[bot] — please address before we review.

graycyrus avatar May 27 '26 19:05 graycyrus