From 3830b210d62edd02ac93aa4e8dab726877f34d30 Mon Sep 17 00:00:00 2001 From: zyycn Date: Thu, 11 Jun 2026 22:17:56 +0800 Subject: [PATCH] feat: add v1 models endpoint Co-authored-by: Codex --- docs/implementation-status.md | 2 +- src/app.rs | 3 ++- src/http/v1.rs | 39 +++++++++++++++++++++++++++++----- tests/routes_responses_test.rs | 23 ++++++++++++++++++++ 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/docs/implementation-status.md b/docs/implementation-status.md index 6d77bfd79..a67b6eae5 100644 --- a/docs/implementation-status.md +++ b/docs/implementation-status.md @@ -15,7 +15,7 @@ Update this file before each feature commit. | Cookie persistence | Completed | `4710926` | `cargo test cookie_jar_captures_and_replays_account_scoped_cookies`; full `fmt/test/clippy` before commit | Adds account-scoped in-memory Cookie jar and repository shell; database persistence follows storage integration. | | Account pool and refresh | Completed | `0f97c5c` | `cargo test account_pool_skips_expired_disabled_banned_and_quota_exhausted_accounts`; full `fmt/test/clippy` before commit | Adds account statuses, acquisition filter, lifecycle events, token pair, OAuth config, and native refresh policy shell with no legacy compatibility layer. | | Translation | Completed | `eff182e` | `cargo test chat_completion_translates_to_codex_response_request`; full `fmt/test/clippy` before commit | Adds OpenAI Chat request types, Codex Responses request type, translation, and OpenAI-compatible error body helper. | -| HTTP routes | Completed | `ab0ad8f` | `cargo test v1_requires_client_api_key_not_admin_cookie`; full `fmt/test/clippy` before commit | Adds Axum router, health route, `/v1` unauthorized OpenAI error body, global request-id response header, and binary listener. | +| HTTP routes | Completed | `ab0ad8f` + v1 models route commit | `cargo test v1_requires_client_api_key_not_admin_cookie`; `cargo test models_route_returns_openai_compatible_codex_model_list`; full `fmt/test/clippy` before commit | Adds Axum router, health route, `/v1/models`, `/v1` unauthorized OpenAI error body, global request-id response header, and binary listener. | | Admin/frontend response contract | Completed | `1c34ea7` | `cargo test admin_envelope_serializes_lower_camel_case_request_id`; `cargo test v1_response_has_request_id_header_without_admin_body_field`; full `fmt/test/clippy` before commit | Adds reusable admin envelopes with body codes, lower camelCase JSON, paginated list shape, requestId bodies, and keeps `/v1/*` OpenAI-compatible. | | Upstream lifecycle | Completed | `88cad87` | `cargo test responses_route_rejects_non_codex_provider_models`; `cargo test v1_requires_client_api_key_not_admin_cookie`; full `fmt/test/clippy` before commit | `/v1` accepts only `Bearer cpr_...`, rejects non-Codex provider models, and returns OpenAI-compatible errors. | | Fingerprint updates | Completed | `dd26d31` | `cargo test update_manifest_updates_app_version_and_build_number`; full `fmt/test/clippy` before commit | Parses Codex Desktop update manifests into app version and build number updates. | diff --git a/src/app.rs b/src/app.rs index cbe9daeaa..7630df39e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,7 +6,7 @@ use axum::{ use tower_http::trace::TraceLayer; use crate::{ - http::{health::health, middleware::attach_request_id, v1::responses}, + http::{health::health, middleware::attach_request_id, v1::models, v1::responses}, state::AppState, }; @@ -15,6 +15,7 @@ pub fn build_router(state: AppState) -> Router { .route("/health", get(health)) .route("/v1/responses", post(responses)) .route("/v1/chat/completions", post(responses)) + .route("/v1/models", get(models)) .with_state(state) .layer(TraceLayer::new_for_http()) .layer(from_fn(attach_request_id)) diff --git a/src/http/v1.rs b/src/http/v1.rs index add2a617c..bda01adda 100644 --- a/src/http/v1.rs +++ b/src/http/v1.rs @@ -5,6 +5,7 @@ use axum::{ Json, }; use serde::Deserialize; +use serde_json::json; use crate::translation::codex_to_openai::openai_error; @@ -14,11 +15,7 @@ struct ResponsesBody { } pub async fn responses(headers: HeaderMap, body: Bytes) -> impl IntoResponse { - let auth = headers - .get("authorization") - .and_then(|value| value.to_str().ok()) - .unwrap_or_default(); - if !auth.starts_with("Bearer cpr_") { + if !has_client_api_key(&headers) { return ( StatusCode::UNAUTHORIZED, Json(openai_error("Missing client API key", "invalid_api_key")), @@ -44,3 +41,35 @@ pub async fn responses(headers: HeaderMap, body: Bytes) -> impl IntoResponse { )), ) } + +pub async fn models(headers: HeaderMap) -> impl IntoResponse { + if !has_client_api_key(&headers) { + return ( + StatusCode::UNAUTHORIZED, + Json(openai_error("Missing client API key", "invalid_api_key")), + ); + } + + ( + StatusCode::OK, + Json(json!({ + "object": "list", + "data": [ + { + "id": "gpt-5.4", + "object": "model", + "created": 0, + "owned_by": "openai" + } + ] + })), + ) +} + +fn has_client_api_key(headers: &HeaderMap) -> bool { + let auth = headers + .get("authorization") + .and_then(|value| value.to_str().ok()) + .unwrap_or_default(); + auth.starts_with("Bearer cpr_") +} diff --git a/tests/routes_responses_test.rs b/tests/routes_responses_test.rs index d4634036a..1b7b66ff9 100644 --- a/tests/routes_responses_test.rs +++ b/tests/routes_responses_test.rs @@ -107,3 +107,26 @@ async fn v1_response_has_request_id_header_without_admin_body_field() { let body: Value = serde_json::from_slice(&bytes).unwrap(); assert!(body.get("requestId").is_none()); } + +#[tokio::test] +async fn models_route_returns_openai_compatible_codex_model_list() { + let app = build_router(AppState::new(test_config())); + let response = app + .oneshot( + Request::builder() + .method("GET") + .uri("/v1/models") + .header("authorization", "Bearer cpr_test") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + + assert_eq!(response.status(), StatusCode::OK); + let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + let body: Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(body["object"], "list"); + assert_eq!(body["data"][0]["object"], "model"); + assert!(body["data"][0]["id"].as_str().unwrap().starts_with("gpt")); +}