| 1 | use axum::extract::{Path, State}; |
| 2 | use axum::Json; |
| 3 | use serde::Deserialize; |
| 4 | use serde_json::json; |
| 5 | use utopia_core::models::Role; |
| 6 | use utopia_llm::ChatMessage; |
| 7 | use uuid::Uuid; |
| 8 | |
| 9 | use crate::auth::AuthUser; |
| 10 | use crate::error::ApiResult; |
| 11 | use crate::llm_util; |
| 12 | use crate::state::AppState; |
| 13 | |
| 14 | /// GET:脱敏视图(密钥只回传是否已配置)。 |
| 15 | pub async fn get( |
| 16 | State(state): State<AppState>, |
| 17 | AuthUser(user): AuthUser, |
| 18 | Path(workspace_id): Path<Uuid>, |
| 19 | ) -> ApiResult<Json<serde_json::Value>> { |
| 20 | utopia_store::workspaces::require_role(&state.pool, user.id, workspace_id, Role::Admin).await?; |
| 21 | let s = utopia_store::settings::get(&state.pool, workspace_id).await?; |
| 22 | Ok(Json(match s { |
| 23 | None => json!({}), |
| 24 | Some(s) => json!({ |
| 25 | "chat_base_url": s.chat_base_url, |
| 26 | "chat_model": s.chat_model, |
| 27 | "has_chat_key": s.chat_api_key.as_deref().is_some_and(|k| !k.is_empty()), |
| 28 | "embed_base_url": s.embed_base_url, |
| 29 | "embed_model": s.embed_model, |
| 30 | "embed_dim": s.embed_dim, |
| 31 | "has_embed_key": s.embed_api_key.as_deref().is_some_and(|k| !k.is_empty()), |
| 32 | }), |
| 33 | })) |
| 34 | } |
| 35 | |
| 36 | #[derive(Deserialize)] |
| 37 | pub struct PutSettingsReq { |
| 38 | pub chat_base_url: Option<String>, |
| 39 | /// None 或空串 = 保留旧密钥 |
| 40 | pub chat_api_key: Option<String>, |
| 41 | pub chat_model: Option<String>, |
| 42 | pub embed_base_url: Option<String>, |
| 43 | pub embed_api_key: Option<String>, |
| 44 | pub embed_model: Option<String>, |
| 45 | pub embed_dim: Option<i32>, |
| 46 | } |
| 47 | |
| 48 | pub async fn put( |
| 49 | State(state): State<AppState>, |
| 50 | AuthUser(user): AuthUser, |
| 51 | Path(workspace_id): Path<Uuid>, |
| 52 | Json(req): Json<PutSettingsReq>, |
| 53 | ) -> ApiResult<Json<serde_json::Value>> { |
| 54 | utopia_store::workspaces::require_role(&state.pool, user.id, workspace_id, Role::Admin).await?; |
| 55 | let nonempty = |v: &Option<String>| -> Option<String> { |
| 56 | v.as_deref() |
| 57 | .map(str::trim) |
| 58 | .filter(|s| !s.is_empty()) |
| 59 | .map(String::from) |
| 60 | }; |
| 61 | utopia_store::settings::upsert( |
| 62 | &state.pool, |
| 63 | workspace_id, |
| 64 | nonempty(&req.chat_base_url).as_deref(), |
| 65 | nonempty(&req.chat_api_key).as_deref(), |
| 66 | nonempty(&req.chat_model).as_deref(), |
| 67 | nonempty(&req.embed_base_url).as_deref(), |
| 68 | nonempty(&req.embed_api_key).as_deref(), |
| 69 | nonempty(&req.embed_model).as_deref(), |
| 70 | req.embed_dim, |
| 71 | ) |
| 72 | .await?; |
| 73 | Ok(Json(json!({ "ok": true }))) |
| 74 | } |
| 75 | |
| 76 | /// 连通性测试:对话发一条最小消息;embedding 试算一条并返回维度。 |
| 77 | pub async fn test( |
| 78 | State(state): State<AppState>, |
| 79 | AuthUser(user): AuthUser, |
| 80 | Path(workspace_id): Path<Uuid>, |
| 81 | ) -> ApiResult<Json<serde_json::Value>> { |
| 82 | utopia_store::workspaces::require_role(&state.pool, user.id, workspace_id, Role::Admin).await?; |
| 83 | let Some(s) = utopia_store::settings::get(&state.pool, workspace_id).await? else { |
| 84 | return Ok(Json( |
| 85 | json!({ "chat": { "ok": false, "error": "Not configured" }, |
| 86 | "embed": { "ok": false, "error": "Not configured" } }), |
| 87 | )); |
| 88 | }; |
| 89 | |
| 90 | let chat_result = match llm_util::chat_client(&s) { |
| 91 | None => json!({ "ok": false, "error": "Not configured" }), |
| 92 | Some(client) => { |
| 93 | let msg = [ChatMessage { |
| 94 | role: "user".into(), |
| 95 | content: "Reply with exactly one word: OK".into(), |
| 96 | }]; |
| 97 | match client.chat(&msg).await { |
| 98 | Ok(reply) => { |
| 99 | json!({ "ok": true, "reply": reply.chars().take(50).collect::<String>() }) |
| 100 | } |
| 101 | Err(e) => json!({ "ok": false, "error": e.to_string() }), |
| 102 | } |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | let embed_result = match llm_util::embed_client(&s) { |
| 107 | None => json!({ "ok": false, "error": "Not configured" }), |
| 108 | Some(client) => match client.embed(&["connectivity test".to_string()]).await { |
| 109 | Ok(v) if !v.is_empty() => json!({ "ok": true, "dim": v[0].len() }), |
| 110 | Ok(_) => json!({ "ok": false, "error": "Empty response" }), |
| 111 | Err(e) => json!({ "ok": false, "error": e.to_string() }), |
| 112 | }, |
| 113 | }; |
| 114 | |
| 115 | Ok(Json(json!({ "chat": chat_result, "embed": embed_result }))) |
| 116 | } |