//! Codex OAuth Tauri Commands //! //! 提供 OpenAI ChatGPT Plus/Pro OAuth 认证相关的 Tauri 命令。 //! //! 大部分认证命令通过通用 `auth_*` 命令(参见 `commands::auth`)暴露给前端, //! 此处定义 State wrapper 以及 Codex OAuth 专属的订阅额度查询命令。 use crate::proxy::providers::codex_oauth_auth::CodexOAuthManager; use crate::services::subscription::{query_codex_quota, CredentialStatus, SubscriptionQuota}; use std::sync::Arc; use tauri::State; use tokio::sync::RwLock; /// Codex OAuth 认证状态 pub struct CodexOAuthState(pub Arc>); /// 查询 Codex OAuth (ChatGPT Plus/Pro) 订阅额度 /// /// - `account_id` 未指定时回退到 `CodexOAuthManager` 的默认账号 /// - 没有任何账号时返回 `not_found`,前端 `SubscriptionQuotaView` 会静默不渲染 /// - 复用 `services::subscription::query_codex_quota`,因此 wham/usage 端点协议 /// 与 Codex CLI 路径完全一致 #[tauri::command(rename_all = "camelCase")] pub async fn get_codex_oauth_quota( account_id: Option, state: State<'_, CodexOAuthState>, ) -> Result { let manager = state.0.read().await; // 解析最终使用的账号 ID:显式 > 默认账号 > 无账号 (not_found) let resolved = match account_id { Some(id) => Some(id), None => manager.default_account_id().await, }; let Some(id) = resolved else { return Ok(SubscriptionQuota::not_found("codex_oauth")); }; // 获取(必要时自动刷新)access_token let token = match manager.get_valid_token_for_account(&id).await { Ok(t) => t, Err(e) => { return Ok(SubscriptionQuota::error( "codex_oauth", CredentialStatus::Expired, format!("Codex OAuth token unavailable: {e}"), )); } }; Ok(query_codex_quota( &token, Some(&id), "codex_oauth", "Codex OAuth access token expired or rejected. Please re-login via cc-switch.", ) .await) }