feat: add v1 models endpoint

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
zyycn
2026-06-11 22:17:56 +08:00
co-authored by Codex
parent 762764cd4b
commit 3830b210d6
4 changed files with 60 additions and 7 deletions
+1 -1
View File
@@ -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. |
+2 -1
View File
@@ -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))
+34 -5
View File
@@ -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_")
}
+23
View File
@@ -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"));
}