From 19bff8c0d18fb42e1ed9bb709aa85e0fe5cc4fdf Mon Sep 17 00:00:00 2001 From: Zyy Date: Fri, 12 Jun 2026 13:13:34 +0800 Subject: [PATCH] feat: add admin model refresh route Co-authored-by: Codex --- docs/implementation-status.md | 4 +- .../2026-06-12-openai-gpt-codex-parity.md | 8 +- ...26-06-12-openai-gpt-codex-parity-design.md | 2 +- src/app.rs | 5 +- src/codex/client.rs | 64 ++++- src/http/admin.rs | 157 +++++++++++ tests/admin_models_route_test.rs | 255 ++++++++++++++++++ 7 files changed, 485 insertions(+), 10 deletions(-) create mode 100644 tests/admin_models_route_test.rs diff --git a/docs/implementation-status.md b/docs/implementation-status.md index f9bca0e80..ed9c46011 100644 --- a/docs/implementation-status.md +++ b/docs/implementation-status.md @@ -16,10 +16,10 @@ Update this file before each feature commit. | Account pool and refresh | Partial | `0f97c5c` + `2f25db8` + `4dc0c9a` + `c2cf7a6` + `636fa79` + `fdb9374` + `3ecd5b6` + `3960a35` | `cargo test account_pool_skips_expired_disabled_banned_and_quota_exhausted_accounts`; `cargo test --test account_pool_scheduling_test`; `cargo test --test account_refresh_test`; `cargo test --test oauth_refresh_test`; `cargo test --test v1_upstream_route_test`; `cargo test --test account_pool_startup_test`; Task 5 targeted `cargo test --test v1_upstream_route_test`, `cargo test --test chat_completions_route_test`, and `cargo test --test account_pool_scheduling_test`; full `fmt/test/clippy` before commit | Implements base account status filtering, refresh scheduler semantics, real OpenAI OAuth refresh-token exchange, 401-triggered refresh and single retry for `/v1/responses`, max concurrent slots, stale slot cleanup, least-used/round-robin/sticky strategies, tier priority, quota-cache filtering, Cloudflare cooldown skip, request staggering metadata, preferred account selection, exclude IDs, capacity summary, settings-backed pool construction, same-process import-to-pool sync, startup pool snapshots from SQLite with usage-based last-used metadata, in-memory 429 quota cooldown marking, Cloudflare cooldown mutation, pool status mutation for upstream fallback, and shared fallback acquisition for non-streaming Responses, HTTP SSE Responses setup, and Chat Completions. Still missing durable release-time slot state, WebSocket/history fallback policy, durable quota cooldown persistence, and admin account CRUD. | | Account repository | Completed | `14fe706` | `cargo test --test account_repository_test`; `cargo fmt --check`; `cargo clippy --all-targets --all-features --locked -- -D warnings`; `cargo test` | Adds encrypted SQLite account token storage, decrypting reads, cursor pagination, status/label updates, release-time usage counter persistence, and refresh-token-preserving token updates. | | Translation | Completed | `eff182e` + `b9c30ba` | `cargo test --test routes_chat_test`; `cargo test --test chat_completions_route_test`; full `cargo fmt --check && cargo test && cargo clippy --all-targets --all-features --locked -- -D warnings` before `b9c30ba` | Adds OpenAI Chat request types, expanded Codex Responses request type, OpenAI Chat to Codex translation for system/developer instructions, multimodal content, tool/function calls, response formats, reasoning effort, service tier, and parallel tool calls. Codex SSE now converts back to OpenAI chat completions and chat chunks with text deltas, reasoning content, tool calls, usage details, and OpenAI-compatible error bodies. | -| HTTP routes | Partial | `ab0ad8f` + `3830b21` + `6179d44` + `cfec8ba` + `6341336` + `228febd` + `c2cf7a6` + `e9fdea0` + `ee0ed90` + `fdb9374` + `9ca5c7d` + `b9c30ba` + `350ffbe` + `3ecd5b6` + `3960a35` + `1738f24` + `0b6cc7e` | `cargo test v1_requires_client_api_key_not_admin_cookie`; `cargo test models_route_returns_openai_compatible_codex_model_list`; `cargo test --test admin_settings_route_test`; `cargo test --test model_catalog_test`; `cargo test --test admin_accounts_route_test`; `cargo test --test admin_api_keys_route_test`; `cargo test --test admin_usage_stats_route_test`; `cargo test --test v1_upstream_route_test`; `cargo test --test chat_completions_route_test`; `cargo test --test codex_websocket_test`; full `cargo fmt --check && cargo test && cargo clippy --all-targets --all-features --locked -- -D warnings` before `b9c30ba` and `350ffbe`; Task 5 targeted verification before `3ecd5b6` and `3960a35`; full `fmt/test/clippy` before `1738f24`; full `fmt/test/clippy` plus real-file smoke before `0b6cc7e` | Adds Axum router, health route, `/v1/models`, `/v1/models/catalog`, `/v1/models/{id}`, `/v1/models/{id}/info`, `/debug/models`, `/admin/accounts`, `/admin/accounts/import`, `/admin/api-keys`, `/admin/usage-stats`, `/admin/usage-stats/summary`, `/v1/responses` HTTP SSE collection with output item/delta reconstruction plus `output_text` sync for non-streaming clients, default streaming Responses passthrough when `stream` is omitted, WebSocket-backed Responses streaming for `previous_response_id`, non-streaming and HTTP SSE setup fallback to another imported account after 429/402/403 account-state failures, Responses field parity for service tier, tool choice, parallel tool calls, text format, prompt cache key, include, client metadata, reasoning include default, and Codex context headers, plus a dedicated `/v1/chat/completions` route backed by imported Codex accounts with fallback for buffered non-streaming and client-streaming output conversion. `/v1` unauthorized and model errors remain OpenAI-compatible, cached backend model snapshots are used by `/v1/models*` when present, global request-id response headers are preserved, `gpt-5.5` remains the default/test model baseline, and `/admin/settings` is read-only with admin-session auth and lower camelCase envelope. `/admin/accounts` reads metadata only and does not decrypt tokens for list views. `/admin/accounts/import` accepts native and Sub2API OpenAI OAuth account exports, returns `sourceFormat`, and ignores proxy-only metadata. Still missing OAuth/device/CLI account login flows, admin account mutation/health/quota/Cookie routes, full admin API-key mutation/export/import routes, settings mutation/persistence, diagnostics, live backend model refresh, live Chat streaming transform, and WebSocket/history fallback policy. | +| HTTP routes | Partial | `ab0ad8f` + `3830b21` + `6179d44` + `cfec8ba` + `6341336` + `228febd` + `c2cf7a6` + `e9fdea0` + `ee0ed90` + `fdb9374` + `9ca5c7d` + `b9c30ba` + `350ffbe` + `3ecd5b6` + `3960a35` + `1738f24` + `0b6cc7e` + pending | `cargo test v1_requires_client_api_key_not_admin_cookie`; `cargo test models_route_returns_openai_compatible_codex_model_list`; `cargo test --test admin_settings_route_test`; `cargo test --test model_catalog_test`; `cargo test --test admin_accounts_route_test`; `cargo test --test admin_api_keys_route_test`; `cargo test --test admin_usage_stats_route_test`; `cargo test --test v1_upstream_route_test`; `cargo test --test chat_completions_route_test`; `cargo test --test codex_websocket_test`; full `cargo fmt --check && cargo test && cargo clippy --all-targets --all-features --locked -- -D warnings` before `b9c30ba` and `350ffbe`; Task 5 targeted verification before `3ecd5b6` and `3960a35`; full `fmt/test/clippy` before `1738f24`; full `fmt/test/clippy` plus real-file smoke before `0b6cc7e`; Task 6 refresh targeted tests before pending | Adds Axum router, health route, `/v1/models`, `/v1/models/catalog`, `/v1/models/{id}`, `/v1/models/{id}/info`, `/debug/models`, `/admin/accounts`, `/admin/accounts/import`, `/admin/api-keys`, `/admin/usage-stats`, `/admin/usage-stats/summary`, `/admin/refresh-models`, `/v1/responses` HTTP SSE collection with output item/delta reconstruction plus `output_text` sync for non-streaming clients, default streaming Responses passthrough when `stream` is omitted, WebSocket-backed Responses streaming for `previous_response_id`, non-streaming and HTTP SSE setup fallback to another imported account after 429/402/403 account-state failures, Responses field parity for service tier, tool choice, parallel tool calls, text format, prompt cache key, include, client metadata, reasoning include default, and Codex context headers, plus a dedicated `/v1/chat/completions` route backed by imported Codex accounts with fallback for buffered non-streaming and client-streaming output conversion. `/v1` unauthorized and model errors remain OpenAI-compatible, cached backend model snapshots are used by `/v1/models*` when present, global request-id response headers are preserved, `gpt-5.5` remains the default/test model baseline, and `/admin/settings` is read-only with admin-session auth and lower camelCase envelope. `/admin/accounts` reads metadata only and does not decrypt tokens for list views. `/admin/accounts/import` accepts native and Sub2API OpenAI OAuth account exports, returns `sourceFormat`, and ignores proxy-only metadata. `/admin/refresh-models` fetches backend model lists through imported accounts only and stores plan snapshots. Still missing OAuth/device/CLI account login flows, admin account mutation/health/quota/Cookie routes, full admin API-key mutation/export/import routes, settings mutation/persistence, diagnostics, live Chat streaming transform, and WebSocket/history fallback policy. | | Admin/frontend response contract | Completed | `1c34ea7` + `b0c10f2` | `cargo test admin_envelope_serializes_lower_camel_case_request_id`; `cargo test admin_logs_reject_missing_admin_session_cookie`; `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, admin session cookie enforcement, and keeps `/v1/*` OpenAI-compatible. | | HTTP auth boundary | Completed | `cec4607` + `e9fdea0` | `cargo test --test http_auth_test`; `cargo test --test routes_responses_test`; `cargo test --test admin_api_keys_route_test`; `cargo test --test admin_logs_route_test`; `cargo fmt --check`; `cargo clippy --all-targets --all-features --locked -- -D warnings`; `cargo test` | Adds a real `src/http/auth.rs` boundary for client API key and admin session cookie parsing. `/v1/*` accepts only enabled local `Bearer cpr_...` client API keys verified from SQLite, admin session parsing ignores client API keys, and route handlers no longer own ad hoc credential parsing. | -| Model catalog | Partial | `6341336` + `1738f24` | `cargo test --test model_catalog_test`; `cargo test model_catalog_route_returns_codex_metadata_without_alias_entries`; `cargo test --test storage_schema_test`; `cargo test --test routes_responses_test model_catalog_route_returns_cached_backend_models`; full `fmt/test/clippy` before `1738f24` | Adds a native static Codex model catalog with `gpt-5.5` default, configured aliases, reasoning/service-tier suffix parsing including `none` and `minimal`, OpenAI-compatible `/v1/models`, extended catalog/detail/info routes, debug summary, SQLite-backed backend model snapshots by plan, cached backend catalog merge, and model-to-plan allowlist derivation. Still missing live backend Codex model fetching per plan, `/admin/refresh-models`, runtime pool allowlist refresh from fetched models, and full model metadata parity. | +| Model catalog | Partial | `6341336` + `1738f24` + pending | `cargo test --test model_catalog_test`; `cargo test model_catalog_route_returns_codex_metadata_without_alias_entries`; `cargo test --test storage_schema_test`; `cargo test --test routes_responses_test model_catalog_route_returns_cached_backend_models`; `cargo test --test admin_models_route_test`; full `fmt/test/clippy` before `1738f24`; Task 6 refresh targeted tests before pending | Adds a native static Codex model catalog with `gpt-5.5` default, configured aliases, reasoning/service-tier suffix parsing including `none` and `minimal`, OpenAI-compatible `/v1/models`, extended catalog/detail/info routes, debug summary, SQLite-backed backend model snapshots by plan, cached backend catalog merge, model-to-plan allowlist derivation, and an admin refresh route that probes Codex model endpoints with one imported account per plan. Still missing runtime pool allowlist refresh from fetched models and full model metadata parity. | | Upstream lifecycle | Partial | `88cad87` + `225fbff` + `c2cf7a6` + `636fa79` + `ee0ed90` + `fdb9374` + `5bf012c` + `9ca5c7d` + `b9c30ba` + `350ffbe` + `3ecd5b6` + `3960a35` | `cargo test responses_route_rejects_non_codex_provider_models`; `cargo test v1_requires_client_api_key_not_admin_cookie`; `cargo test --test codex_sse_usage_test`; `cargo test --test codex_client_test`; `cargo test --test oauth_refresh_test`; `cargo test --test v1_upstream_route_test`; `cargo test --test account_pool_startup_test`; `cargo test --test codex_websocket_test`; Task 5 targeted `cargo test --test v1_upstream_route_test`, `cargo test --test chat_completions_route_test`, and `cargo test --test account_pool_scheduling_test`; full `cargo fmt --check && cargo test && cargo clippy --all-targets --all-features --locked -- -D warnings` before `b9c30ba` and `350ffbe` | `/v1` enforces client API key shape, rejects non-Codex provider models through the model catalog, returns OpenAI-compatible errors, includes reusable SSE/usage helpers, sends Codex Desktop HTTP SSE requests through imported accounts, sends WebSocket `response.create` requests when `previous_response_id` requires upstream history, replays/captures encrypted Cookies, forwards Codex request context headers, normalizes `service_tier: fast` to upstream `priority`, collects `response.completed`, reconstructs missing assistant output from `response.output_item.done` or text deltas, synchronizes `output_text` for non-streaming clients, passes through Responses streaming SSE by default, adapts Chat Completions responses/chunks, refreshes and retries once after upstream 401 when a refresh token is stored, parses Retry-After plus Codex reset metadata from upstream errors, records 429 request attempts, classifies upstream 429/402/403 into account cooldown/quota/banned/Cloudflare state changes, retries non-streaming `/v1/responses`, HTTP SSE `/v1/responses` setup, and `/v1/chat/completions` with another imported account when available, records token usage, writes v1 lifecycle event logs, records refresh failure classification for upstream 401, and restores the account pool from SQLite on startup. Still missing SSE error/failed mapping, successful rate-limit header capture, WebSocket/history retry policy, durable 429 quota cooldown persistence, previous-response account affinity, and live Chat streaming transform. | | Codex WebSocket boundary | Completed | `cc0516d` + `b9c30ba` + `350ffbe` | `cargo test --test codex_websocket_test`; `cargo test --test v1_upstream_route_test v1_responses_should_use_websocket_for_previous_response_id_streaming`; full `cargo fmt --check && cargo test && cargo clippy --all-targets --all-features --locked -- -D warnings` before `b9c30ba` and `350ffbe` | Adds `src/codex/websocket.rs` with tested transport classification for HTTP SSE versus WebSocket-only requests. `previous_response_id` and explicit WebSocket requests use typed transport selection, local-only `use_websocket` is not serialized into upstream JSON, WebSocket `response.create` carries Codex headers and parity fields, incoming WebSocket messages are emitted as SSE-compatible events, and `/v1/responses` returns WebSocket-backed `text/event-stream` output for `previous_response_id` streaming requests. | | Fingerprint updates | Partial | `dd26d31` + `e32220a` | `cargo test update_manifest_updates_app_version_and_build_number`; `cargo test fingerprint_updater_fetches_manifest_and_persists_history`; full `fmt/test/clippy` before commit | Parses a JSON update manifest and persists app version/build number history to SQLite. Still missing original appcast XML polling, update-state persistence, runtime header fingerprint selection from the latest stored fingerprint, and optional local Codex.app Chromium extraction. | diff --git a/docs/superpowers/plans/2026-06-12-openai-gpt-codex-parity.md b/docs/superpowers/plans/2026-06-12-openai-gpt-codex-parity.md index f8e474ca9..41fbb8fb2 100644 --- a/docs/superpowers/plans/2026-06-12-openai-gpt-codex-parity.md +++ b/docs/superpowers/plans/2026-06-12-openai-gpt-codex-parity.md @@ -8,7 +8,7 @@ **Tech Stack:** Rust, Axum, Reqwest/rustls, serde, sqlx/SQLite, wiremock, tokio, futures, optional WebSocket transport dependency. -**Checkpoint 2026-06-12:** Commit `b9c30ba` completed Tasks 1-3 for the in-scope OpenAI GPT/Codex path: Chat Completions route/translation/output, Responses request field parity, default Responses streaming, Codex context headers, and local-only WebSocket flag serialization cleanup. Commit `350ffbe` completed Task 4: `previous_response_id` requests use WebSocket `response.create` and return SSE-compatible output. Commit `3ecd5b6` partially completed Task 5 for non-streaming `/v1/responses`: 429/402/403 account-state classification and fallback retry across imported accounts. Commit `3960a35` extends the same fallback path to HTTP SSE Responses setup and Chat Completions. Commit `1738f24` adds Task 6 cache groundwork: persisted backend model snapshots and cached `/v1/models*` reads. Commit `0b6cc7e` adds Task 7 import compatibility for Sub2API OpenAI OAuth exports and marks the detected source format while ignoring proxy-only fields. Remaining major work continues with WebSocket/history fallback policy, live model refresh, and scoped admin operations. +**Checkpoint 2026-06-12:** Commit `b9c30ba` completed Tasks 1-3 for the in-scope OpenAI GPT/Codex path: Chat Completions route/translation/output, Responses request field parity, default Responses streaming, Codex context headers, and local-only WebSocket flag serialization cleanup. Commit `350ffbe` completed Task 4: `previous_response_id` requests use WebSocket `response.create` and return SSE-compatible output. Commit `3ecd5b6` partially completed Task 5 for non-streaming `/v1/responses`: 429/402/403 account-state classification and fallback retry across imported accounts. Commit `3960a35` extends the same fallback path to HTTP SSE Responses setup and Chat Completions. Commit `1738f24` adds Task 6 cache groundwork: persisted backend model snapshots and cached `/v1/models*` reads. Commit `0b6cc7e` adds Task 7 import compatibility for Sub2API OpenAI OAuth exports and marks the detected source format while ignoring proxy-only fields. Pending Task 6 refresh work adds `/admin/refresh-models` backed by imported accounts. Remaining major work continues with WebSocket/history fallback policy and scoped admin operations. --- @@ -97,9 +97,9 @@ - [x] Write failing tests for backend model snapshots, suffix parsing including `none` and `minimal`, plan allowlist generation, cached `/v1/models/catalog`, and model snapshot storage. - [x] Run: `cargo test --test model_catalog_test`; `cargo test --test storage_schema_test`; `cargo test --test routes_responses_test model_catalog_route_returns_cached_backend_models` - [x] Add SQLite-backed model snapshot cache and make `/v1/models*` use cached backend snapshots when present. -- [ ] Write failing tests for `/admin/refresh-models`. -- [ ] Add refresh route using existing imported Codex accounts, without proxy-pool support. -- [ ] Run: `cargo test --test model_catalog_test` +- [x] Write failing tests for `/admin/refresh-models`. +- [x] Add refresh route using existing imported Codex accounts, without proxy-pool support. +- [x] Run: `cargo test --test admin_models_route_test` ### Task 7: Scoped Admin Account Operations diff --git a/docs/superpowers/specs/2026-06-12-openai-gpt-codex-parity-design.md b/docs/superpowers/specs/2026-06-12-openai-gpt-codex-parity-design.md index 69457a4ed..86f12c08c 100644 --- a/docs/superpowers/specs/2026-06-12-openai-gpt-codex-parity-design.md +++ b/docs/superpowers/specs/2026-06-12-openai-gpt-codex-parity-design.md @@ -18,7 +18,7 @@ OpenAI Chat compatibility is implemented as a real route, not an alias to Respon Responses compatibility keeps native Responses passthrough semantics but adds the TS implementation's in-scope fields: reasoning/service tier, tool choice, parallel tool calls, text formats, prompt cache key, include, client metadata, and Codex context headers. `previous_response_id` uses WebSocket transport rather than being rejected. -Current progress: Tasks 1-4 are implemented for the OpenAI GPT/Codex scope. Commit `3ecd5b6` partially implements Task 5 for non-streaming `/v1/responses`: upstream 429/402/403 responses classify account state and retry another imported account when available. Commit `3960a35` extends that fallback to HTTP SSE Responses setup and Chat Completions. Commit `1738f24` adds Task 6 cache groundwork: backend model snapshots are stored by account plan and `/v1/models*` reads cached backend models when present. Commit `0b6cc7e` adds Task 7 import compatibility for Sub2API OpenAI OAuth exports and marks the detected source format while ignoring proxy-only fields. WebSocket-backed `previous_response_id` fallback remains a Task 5 follow-up because account affinity must be preserved. +Current progress: Tasks 1-4 are implemented for the OpenAI GPT/Codex scope. Commit `3ecd5b6` partially implements Task 5 for non-streaming `/v1/responses`: upstream 429/402/403 responses classify account state and retry another imported account when available. Commit `3960a35` extends that fallback to HTTP SSE Responses setup and Chat Completions. Commit `1738f24` adds Task 6 cache groundwork: backend model snapshots are stored by account plan and `/v1/models*` reads cached backend models when present. Commit `0b6cc7e` adds Task 7 import compatibility for Sub2API OpenAI OAuth exports and marks the detected source format while ignoring proxy-only fields. Pending Task 6 refresh work adds `/admin/refresh-models` backed by imported accounts. WebSocket-backed `previous_response_id` fallback remains a Task 5 follow-up because account affinity must be preserved. ## Components diff --git a/src/app.rs b/src/app.rs index 3128262f2..08b40aa12 100644 --- a/src/app.rs +++ b/src/app.rs @@ -8,8 +8,8 @@ use tower_http::trace::TraceLayer; use crate::{ http::{ admin::{ - accounts, api_keys, create_api_key, import_accounts, login, logs, settings, - usage_stats, usage_stats_summary, + accounts, api_keys, create_api_key, import_accounts, login, logs, refresh_models, + settings, usage_stats, usage_stats_summary, }, health::health, middleware::attach_request_id, @@ -34,6 +34,7 @@ pub fn build_router(state: AppState) -> Router { .route("/admin/login", post(login)) .route("/admin/logs", get(logs)) .route("/admin/settings", get(settings)) + .route("/admin/refresh-models", post(refresh_models)) .route("/admin/usage-stats", get(usage_stats)) .route("/admin/usage-stats/summary", get(usage_stats_summary)) .route("/admin/accounts", get(accounts)) diff --git a/src/codex/client.rs b/src/codex/client.rs index f97154262..68ff229e2 100644 --- a/src/codex/client.rs +++ b/src/codex/client.rs @@ -1,5 +1,7 @@ use reqwest::{ - header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE, COOKIE, RETRY_AFTER, SET_COOKIE}, + header::{ + HeaderMap, HeaderName, HeaderValue, ACCEPT, CONTENT_TYPE, COOKIE, RETRY_AFTER, SET_COOKIE, + }, Client, Response as ReqwestResponse, StatusCode, }; use serde_json::Value; @@ -17,6 +19,7 @@ use crate::{ }, }, fingerprint::model::Fingerprint, + models::catalog::BackendModelEntry, }; #[derive(Debug, Error)] @@ -39,6 +42,8 @@ pub enum CodexClientError { body: String, retry_after_seconds: Option, }, + #[error("backend model catalog is unavailable")] + ModelsUnavailable, } pub type CodexClientResult = Result; @@ -160,6 +165,36 @@ impl CodexBackendClient { }) } + pub async fn fetch_models( + &self, + context: CodexRequestContext<'_>, + ) -> CodexClientResult> { + let endpoints = [ + format!( + "{}/codex/models?client_version={}", + self.base_url, self.fingerprint.app_version + ), + format!("{}/models", self.base_url), + format!("{}/sentinel/chat-requirements", self.base_url), + ]; + + for endpoint in endpoints { + let mut headers = self.request_headers(context)?; + headers.insert(ACCEPT, HeaderValue::from_static("application/json")); + let response = self.client.get(endpoint).headers(headers).send().await?; + if !response.status().is_success() { + continue; + } + let parsed = response.json::().await?; + let models = extract_backend_model_entries(&parsed); + if !models.is_empty() { + return Ok(models); + } + } + + Err(CodexClientError::ModelsUnavailable) + } + async fn send_response_request( &self, request: &CodexResponsesRequest, @@ -220,6 +255,33 @@ impl CodexBackendClient { } } +fn extract_backend_model_entries(value: &Value) -> Vec { + let Some(models) = value + .pointer("/chat_models/models") + .or_else(|| value.get("models")) + .or_else(|| value.get("data")) + .or_else(|| value.get("categories")) + .and_then(Value::as_array) + else { + return Vec::new(); + }; + + let mut entries = Vec::new(); + for model in models { + if let Some(nested) = model.get("models").and_then(Value::as_array) { + entries.extend(nested.iter().filter_map(parse_backend_model_entry)); + } else if let Some(entry) = parse_backend_model_entry(model) { + entries.push(entry); + } + } + entries +} + +fn parse_backend_model_entry(value: &Value) -> Option { + let entry = serde_json::from_value::(value.clone()).ok()?; + (entry.slug.is_some() || entry.id.is_some() || entry.name.is_some()).then_some(entry) +} + fn insert_optional_header( headers: &mut HeaderMap, name: &'static str, diff --git a/src/http/admin.rs b/src/http/admin.rs index 2d4c0cbe4..b3b0a9860 100644 --- a/src/http/admin.rs +++ b/src/http/admin.rs @@ -24,8 +24,11 @@ use crate::{ }, auth::admin_session::verify_admin_password, auth::api_key_repository::StoredClientApiKey, + codex::client::{build_reqwest_client, CodexBackendClient, CodexRequestContext}, config::{AppConfig, QuotaWarningThresholds}, + fingerprint::model::Fingerprint, http::{auth::admin_session_id, middleware::RequestId}, + models::catalog::ModelPlanSnapshot, pagination::{clamp_limit, Page}, state::AppState, }; @@ -226,6 +229,14 @@ pub struct AccountImportData { pub source_format: String, } +#[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RefreshModelsData { + pub refreshed_plans: usize, + pub model_count: usize, + pub failed_plans: usize, +} + #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct CreateApiKeyRequest { @@ -560,6 +571,136 @@ pub async fn settings( .into_response() } +pub async fn refresh_models( + State(state): State, + Extension(request_id): Extension, + headers: HeaderMap, +) -> Response { + let request_id = request_id.as_str().to_string(); + let Some(pool) = state.db() else { + return AdminResponse::new( + StatusCode::INTERNAL_SERVER_ERROR, + AdminEnvelope::new(50001, "Database is not initialized", (), request_id), + ) + .into_response(); + }; + if let Err(response) = require_admin_session(pool, &headers, &request_id).await { + return response; + } + let Some(account_repo) = state.account_repository() else { + return AdminResponse::new( + StatusCode::INTERNAL_SERVER_ERROR, + AdminEnvelope::new( + 50001, + "Account repository is not initialized", + (), + request_id, + ), + ) + .into_response(); + }; + let Some(model_repo) = state.model_snapshot_repository() else { + return AdminResponse::new( + StatusCode::INTERNAL_SERVER_ERROR, + AdminEnvelope::new(50001, "Model repository is not initialized", (), request_id), + ) + .into_response(); + }; + let accounts = match account_repo.list_pool_accounts().await { + Ok(accounts) => accounts, + Err(_) => { + return AdminResponse::new( + StatusCode::INTERNAL_SERVER_ERROR, + AdminEnvelope::new(50001, "Failed to list accounts", (), request_id), + ) + .into_response(); + } + }; + let plan_accounts = distinct_active_plan_accounts(accounts); + if plan_accounts.is_empty() { + return AdminResponse::new( + StatusCode::BAD_REQUEST, + AdminEnvelope::new( + 40001, + "No accounts available for model refresh", + (), + request_id, + ), + ) + .into_response(); + } + let client = match build_reqwest_client(state.config().tls.force_http11) { + Ok(client) => CodexBackendClient::new( + client, + state.config().api.base_url.clone(), + Fingerprint::default_codex_desktop(), + ), + Err(_) => { + return AdminResponse::new( + StatusCode::INTERNAL_SERVER_ERROR, + AdminEnvelope::new(50001, "Failed to build Codex client", (), request_id), + ) + .into_response(); + } + }; + + let mut refreshed_plans = 0usize; + let mut model_count = 0usize; + let mut failed_plans = 0usize; + for (plan_type, account) in plan_accounts { + let context = CodexRequestContext { + access_token: &account.access_token, + account_id: account.account_id.as_deref(), + request_id: &request_id, + turn_state: None, + turn_metadata: None, + beta_features: None, + include_timing_metrics: None, + version: None, + codex_window_id: None, + parent_thread_id: None, + cookie_header: None, + }; + let entries = match client.fetch_models(context).await { + Ok(entries) if !entries.is_empty() => entries, + Ok(_) => { + failed_plans += 1; + continue; + } + Err(error) => { + tracing::warn!(?error, plan_type, "failed to refresh backend models"); + failed_plans += 1; + continue; + } + }; + let snapshot = ModelPlanSnapshot::from_backend_entries(plan_type, entries); + model_count += snapshot.models.len(); + if model_repo.replace_plan_snapshot(&snapshot).await.is_err() { + return AdminResponse::new( + StatusCode::INTERNAL_SERVER_ERROR, + AdminEnvelope::new(50001, "Failed to store model snapshot", (), request_id), + ) + .into_response(); + } + refreshed_plans += 1; + } + + let data = RefreshModelsData { + refreshed_plans, + model_count, + failed_plans, + }; + if refreshed_plans == 0 { + return AdminResponse::new( + StatusCode::BAD_GATEWAY, + AdminEnvelope::new(50201, "Failed to refresh backend models", data, request_id), + ) + .into_response(); + } + + AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(data, request_id)).into_response() +} + pub async fn usage_stats( State(state): State, Extension(request_id): Extension, @@ -1048,6 +1189,22 @@ fn normalized_account_id(id: Option) -> String { .unwrap_or_else(|| format!("acct_{}", Uuid::new_v4().simple())) } +fn distinct_active_plan_accounts(accounts: Vec) -> Vec<(String, Account)> { + let mut by_plan = std::collections::BTreeMap::new(); + for account in accounts { + if account.status != AccountStatus::Active { + continue; + } + let plan_type = account + .plan_type + .clone() + .unwrap_or_else(|| "unknown".to_string()); + // 同一 plan 的模型列表一致,刷新时只需要取一个账号,避免重复打上游。 + by_plan.entry(plan_type).or_insert(account); + } + by_plan.into_iter().collect() +} + fn parse_account_import_payload(payload: &Value) -> ParsedAccountImportPayload { if let Some(accounts) = parse_sub2api_oauth_payload(payload) { return ParsedAccountImportPayload { diff --git a/tests/admin_models_route_test.rs b/tests/admin_models_route_test.rs new file mode 100644 index 000000000..e4fe216c0 --- /dev/null +++ b/tests/admin_models_route_test.rs @@ -0,0 +1,255 @@ +use std::collections::BTreeMap; + +use axum::{ + body::{to_bytes, Body}, + http::{Request, StatusCode}, +}; +use chrono::Utc; +use secrecy::SecretString; +use serde_json::{json, Value}; +use tower::ServiceExt; +use wiremock::{ + matchers::{method, path}, + Mock, MockServer, ResponseTemplate, +}; + +use codex_proxy_rs::{ + accounts::{ + model::AccountStatus, + repository::{AccountRepository, NewAccount}, + }, + app::build_router, + auth::{api_key::ApiKeyHasher, api_key_repository::ClientApiKeyRepository}, + config::{ + AdminConfig, ApiConfig, AppConfig, AuthConfig, DatabaseConfig, LoggingConfig, ModelConfig, + QuotaConfig, QuotaWarningThresholds, SecurityConfig, ServerConfig, TlsConfig, + UsageStatsConfig, + }, + crypto::SecretBox, + models::repository::ModelSnapshotRepository, + state::AppState, + storage::db::connect_sqlite, +}; + +fn test_config(database_url: String, base_url: String) -> AppConfig { + AppConfig { + server: ServerConfig { + host: "127.0.0.1".to_string(), + port: 0, + }, + api: ApiConfig { base_url }, + model: ModelConfig { + default_model: "gpt-5.5".to_string(), + default_reasoning_effort: None, + service_tier: None, + aliases: BTreeMap::new(), + }, + auth: AuthConfig { + refresh_margin_seconds: 300, + refresh_enabled: true, + refresh_concurrency: 2, + max_concurrent_per_account: 3, + request_interval_ms: 50, + rotation_strategy: "least_used".to_string(), + tier_priority: Vec::new(), + }, + quota: QuotaConfig { + refresh_interval_minutes: 5, + warning_thresholds: QuotaWarningThresholds { + primary: vec![80, 90], + secondary: vec![80, 90], + }, + skip_exhausted: true, + }, + usage_stats: UsageStatsConfig { + history_retention_days: None, + }, + database: DatabaseConfig { url: database_url }, + security: SecurityConfig { + master_key_file: "data/master.key".to_string(), + api_key_pepper_file: "data/api-key-pepper.key".to_string(), + }, + tls: TlsConfig { + force_http11: false, + }, + admin: AdminConfig { + session_ttl_minutes: 1440, + }, + logging: LoggingConfig { + directory: "logs".to_string(), + max_file_bytes: 10_485_760, + retention_days: 14, + enabled: false, + capacity: 2_000, + capture_body: false, + }, + } +} + +#[tokio::test] +async fn admin_refresh_models_should_require_admin_session_cookie() { + let server = MockServer::start().await; + let dir = tempfile::tempdir().unwrap(); + let db = dir.path().join("admin-models.sqlite"); + let url = format!("sqlite://{}", db.display()); + let pool = connect_sqlite(&url).await.unwrap(); + let app = build_router(AppState::with_pool_and_secret_box( + test_config(url, server.uri()), + pool, + SecretBox::new([31u8; 32]), + )); + + let response = app + .oneshot( + Request::builder() + .method("POST") + .uri("/admin/refresh-models") + .header("x-request-id", "req_refresh_models") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); + let body = response_json(response).await; + assert_eq!(body["code"], 40101); + assert_eq!(body["requestId"], "req_refresh_models"); +} + +#[tokio::test] +async fn admin_refresh_models_should_fetch_snapshots_for_distinct_account_plans() { + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/codex/models")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "models": [{ + "slug": "gpt-refresh", + "display_name": "GPT Refresh", + "description": "Backend refreshed model", + "default_reasoning_level": "minimal", + "supported_reasoning_levels": [{ + "effort": "minimal", + "description": "Minimal" + }], + "contextWindow": 200000 + }] + }))) + .expect(2) + .mount(&server) + .await; + let dir = tempfile::tempdir().unwrap(); + let db = dir.path().join("admin-models.sqlite"); + let url = format!("sqlite://{}", db.display()); + let pool = connect_sqlite(&url).await.unwrap(); + seed_admin_session(&pool, "session_1").await; + let secret_box = SecretBox::new([32u8; 32]); + let hasher = ApiKeyHasher::new([33u8; 32]); + let generated = hasher.generate_client_api_key("test"); + ClientApiKeyRepository::new(pool.clone()) + .insert_generated("test", &generated) + .await + .unwrap(); + let account_repo = AccountRepository::new(pool.clone(), secret_box.clone()); + insert_account(&account_repo, "acct_plus_a", "plus", "access-plus-a").await; + insert_account(&account_repo, "acct_plus_b", "plus", "access-plus-b").await; + insert_account(&account_repo, "acct_team", "team", "access-team").await; + let app = build_router(AppState::with_pool_secret_and_api_key_hasher( + test_config(url, server.uri()), + pool.clone(), + secret_box, + hasher, + )); + + let response = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri("/admin/refresh-models") + .header("cookie", "cpr_admin_session=session_1") + .header("x-request-id", "req_refresh_models") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + + assert_eq!(response.status(), StatusCode::OK); + let body = response_json(response).await; + assert_eq!(body["data"]["refreshedPlans"], 2); + assert_eq!(body["data"]["modelCount"], 2); + assert_eq!(body["data"]["failedPlans"], 0); + let snapshots = ModelSnapshotRepository::new(pool.clone()) + .list_plan_snapshots() + .await + .unwrap(); + assert_eq!(snapshots.len(), 2); + assert_eq!(snapshots[0].models[0].id, "gpt-refresh"); + + let catalog_response = app + .oneshot( + Request::builder() + .method("GET") + .uri("/v1/models/catalog") + .header("authorization", format!("Bearer {}", generated.plaintext)) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + let catalog = response_json(catalog_response).await; + assert_eq!(catalog[0]["id"], "gpt-refresh"); + assert_eq!(catalog[0]["source"], "backend"); +} + +async fn insert_account( + repo: &AccountRepository, + id: &'static str, + plan_type: &'static str, + token: &'static str, +) { + repo.insert(NewAccount { + id: id.to_string(), + email: None, + account_id: Some(format!("{id}-chatgpt")), + user_id: None, + label: None, + plan_type: Some(plan_type.to_string()), + access_token: SecretString::new(token.to_string().into()), + refresh_token: Some(SecretString::new(format!("refresh-{id}").into())), + access_token_expires_at: None, + status: AccountStatus::Active, + }) + .await + .unwrap(); +} + +async fn seed_admin_session(pool: &sqlx::SqlitePool, session_id: &str) { + let now = Utc::now().to_rfc3339(); + sqlx::query( + "insert into admin_users (id, password_hash, created_at, updated_at) values (?, ?, ?, ?)", + ) + .bind("admin_1") + .bind("hash") + .bind(&now) + .bind(&now) + .execute(pool) + .await + .unwrap(); + sqlx::query( + "insert into admin_sessions (id, user_id, expires_at, created_at) values (?, ?, ?, ?)", + ) + .bind(session_id) + .bind("admin_1") + .bind("2999-01-01T00:00:00Z") + .bind(now) + .execute(pool) + .await + .unwrap(); +} + +async fn response_json(response: axum::response::Response) -> Value { + let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + serde_json::from_slice(&bytes).unwrap() +}