| 1 | //! 系统管理 API(仅系统管理员):部署配置 + 代开账号。 |
| 2 | |
| 3 | use axum::extract::State; |
| 4 | use axum::Json; |
| 5 | use serde::Deserialize; |
| 6 | use serde_json::json; |
| 7 | use utopia_core::models::{Role, User}; |
| 8 | use utopia_core::AppError; |
| 9 | |
| 10 | use crate::auth::{self, AuthUser}; |
| 11 | use crate::error::ApiResult; |
| 12 | use crate::state::AppState; |
| 13 | |
| 14 | fn require_admin(user: &User) -> Result<(), AppError> { |
| 15 | if user.is_admin { |
| 16 | Ok(()) |
| 17 | } else { |
| 18 | Err(AppError::Forbidden) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | pub async fn get_deployment( |
| 23 | State(state): State<AppState>, |
| 24 | AuthUser(user): AuthUser, |
| 25 | ) -> ApiResult<Json<serde_json::Value>> { |
| 26 | require_admin(&user)?; |
| 27 | let open = utopia_store::access::open_registration(&state.pool).await?; |
| 28 | let workers = utopia_store::access::worker_concurrency(&state.pool).await?; |
| 29 | let onto_lang = utopia_store::access::default_ontology_lang(&state.pool).await?; |
| 30 | let (limits, dflt) = utopia_store::model_limits::list(&state.pool).await?; |
| 31 | let in_use = utopia_store::model_limits::models_in_use(&state.pool).await?; |
| 32 | Ok(Json(json!({ |
| 33 | "open_registration": open, |
| 34 | "worker_concurrency": workers, |
| 35 | "default_ontology_lang": onto_lang, |
| 36 | "model_limits": limits, |
| 37 | "default_model_concurrency": dflt, |
| 38 | "models_in_use": in_use.into_iter().map(|(b, m, k)| json!({"base_url": b, "model": m, "kind": k})).collect::<Vec<_>>(), |
| 39 | }))) |
| 40 | } |
| 41 | |
| 42 | #[derive(Deserialize)] |
| 43 | pub struct DeploymentReq { |
| 44 | pub open_registration: bool, |
| 45 | /// 任务 worker 并发(1-256):**外层兜底**,防任务无限堆积。 |
| 46 | /// 真正的节流是按模型的限额,这个值应当明显大于各模型限额之和 |
| 47 | #[serde(default)] |
| 48 | pub worker_concurrency: Option<i32>, |
| 49 | /// 未单独配置的模型走的并发缺省 |
| 50 | #[serde(default)] |
| 51 | pub default_model_concurrency: Option<i32>, |
| 52 | /// 单个模型的并发;`max_concurrent` 为 null 表示删掉专属配置、回落到缺省 |
| 53 | #[serde(default)] |
| 54 | pub model_limit: Option<ModelLimitReq>, |
| 55 | /// 新建知识库时本体用哪种语言播种。**不是界面语言**——那个在客户端 |
| 56 | #[serde(default)] |
| 57 | pub default_ontology_lang: Option<String>, |
| 58 | } |
| 59 | |
| 60 | #[derive(Deserialize)] |
| 61 | pub struct ModelLimitReq { |
| 62 | pub base_url: String, |
| 63 | pub model: String, |
| 64 | pub max_concurrent: Option<i32>, |
| 65 | } |
| 66 | |
| 67 | pub async fn put_deployment( |
| 68 | State(state): State<AppState>, |
| 69 | AuthUser(user): AuthUser, |
| 70 | Json(req): Json<DeploymentReq>, |
| 71 | ) -> ApiResult<Json<serde_json::Value>> { |
| 72 | require_admin(&user)?; |
| 73 | utopia_store::access::set_open_registration(&state.pool, req.open_registration).await?; |
| 74 | if let Some(n) = req.worker_concurrency { |
| 75 | utopia_store::access::set_worker_concurrency(&state.pool, n).await?; |
| 76 | // 热生效:调度循环每轮读这个值,无需重启 |
| 77 | state |
| 78 | .worker_concurrency |
| 79 | .store(n as usize, std::sync::atomic::Ordering::Relaxed); |
| 80 | } |
| 81 | // 按模型的限额即时生效:闸门每次调用前读库,发现限额变了就换一把新信号量 |
| 82 | if let Some(l) = &req.default_ontology_lang { |
| 83 | utopia_store::access::set_default_ontology_lang(&state.pool, l).await?; |
| 84 | } |
| 85 | if let Some(n) = req.default_model_concurrency { |
| 86 | utopia_store::model_limits::set_default(&state.pool, n).await?; |
| 87 | } |
| 88 | if let Some(m) = req.model_limit { |
| 89 | utopia_store::model_limits::set(&state.pool, &m.base_url, &m.model, m.max_concurrent) |
| 90 | .await?; |
| 91 | } |
| 92 | Ok(Json(json!({ "ok": true }))) |
| 93 | } |
| 94 | |
| 95 | #[derive(Deserialize)] |
| 96 | pub struct CreateUserReq { |
| 97 | pub email: String, |
| 98 | pub display_name: String, |
| 99 | pub password: String, |
| 100 | /// 部署角色:admin | editor | viewer |
| 101 | #[serde(default)] |
| 102 | pub role: Option<String>, |
| 103 | } |
| 104 | |
| 105 | /// 管理员代开账号(注册关闭后的唯一入口)。 |
| 106 | pub async fn create_user( |
| 107 | State(state): State<AppState>, |
| 108 | AuthUser(user): AuthUser, |
| 109 | Json(req): Json<CreateUserReq>, |
| 110 | ) -> ApiResult<Json<serde_json::Value>> { |
| 111 | require_admin(&user)?; |
| 112 | if !req.email.contains('@') || req.email.len() > 254 { |
| 113 | return Err(AppError::invalid("bad_email", "Invalid email address").into()); |
| 114 | } |
| 115 | if req.password.chars().count() < 8 { |
| 116 | return Err(AppError::invalid( |
| 117 | "password_too_short", |
| 118 | "Password must be at least 8 characters", |
| 119 | ) |
| 120 | .into()); |
| 121 | } |
| 122 | if req.display_name.trim().is_empty() || req.display_name.chars().count() > 64 { |
| 123 | return Err( |
| 124 | AppError::invalid("bad_display_name", "Display name must be 1-64 characters").into(), |
| 125 | ); |
| 126 | } |
| 127 | let role = match req.role.as_deref().unwrap_or("editor") { |
| 128 | "admin" => Role::Admin, |
| 129 | "editor" => Role::Editor, |
| 130 | "viewer" => Role::Viewer, |
| 131 | _ => { |
| 132 | return Err(AppError::Validation("role must be admin, editor or viewer".into()).into()) |
| 133 | } |
| 134 | }; |
| 135 | let hash = auth::hash_password(&req.password)?; |
| 136 | let created = utopia_store::accounts::admin_create_user( |
| 137 | &state.pool, |
| 138 | req.email.trim(), |
| 139 | &hash, |
| 140 | req.display_name.trim(), |
| 141 | role, |
| 142 | ) |
| 143 | .await?; |
| 144 | Ok(Json(json!({ "user": created }))) |
| 145 | } |