| 1 | use sqlx::PgPool; |
| 2 | use utopia_core::models::KnowledgeBase; |
| 3 | use utopia_core::{AppError, AppResult}; |
| 4 | use uuid::Uuid; |
| 5 | |
| 6 | pub async fn list(pool: &PgPool, workspace_id: Uuid) -> AppResult<Vec<KnowledgeBase>> { |
| 7 | let rows = |
| 8 | sqlx::query_as("SELECT * FROM knowledge_bases WHERE workspace_id = $1 ORDER BY created_at") |
| 9 | .bind(workspace_id) |
| 10 | .fetch_all(pool) |
| 11 | .await?; |
| 12 | Ok(rows) |
| 13 | } |
| 14 | |
| 15 | /// 用户可见的 KB:系统管理员全见;其余 = open 库 + 自己在矩阵里的 restricted 库。 |
| 16 | pub async fn list_visible( |
| 17 | pool: &PgPool, |
| 18 | workspace_id: Uuid, |
| 19 | user_id: Uuid, |
| 20 | is_admin: bool, |
| 21 | ) -> AppResult<Vec<KnowledgeBase>> { |
| 22 | let rows = sqlx::query_as( |
| 23 | "SELECT * FROM knowledge_bases k |
| 24 | WHERE k.workspace_id = $1 |
| 25 | AND ($3 |
| 26 | OR k.visibility = 'open' |
| 27 | OR EXISTS (SELECT 1 FROM kb_members m |
| 28 | WHERE m.kb_id = k.id AND m.user_id = $2)) |
| 29 | ORDER BY k.created_at", |
| 30 | ) |
| 31 | .bind(workspace_id) |
| 32 | .bind(user_id) |
| 33 | .bind(is_admin) |
| 34 | .fetch_all(pool) |
| 35 | .await?; |
| 36 | Ok(rows) |
| 37 | } |
| 38 | |
| 39 | pub async fn create( |
| 40 | pool: &PgPool, |
| 41 | workspace_id: Uuid, |
| 42 | name: &str, |
| 43 | kind: &str, |
| 44 | description: Option<&str>, |
| 45 | ) -> AppResult<KnowledgeBase> { |
| 46 | // 部署的第一个库自动成为默认库:公共空间,永远 open、不可删(API 强制 + DB CHECK) |
| 47 | // |
| 48 | // ontology_lang 取部署默认值:中文部署不该每建一个库就手动选一次。 |
| 49 | // 之后按库可改——同一个部署里完全可能一个库读中文合同、另一个读英文论文 |
| 50 | let kb = sqlx::query_as( |
| 51 | "INSERT INTO knowledge_bases |
| 52 | (id, workspace_id, name, kind, description, is_default, ontology_lang) |
| 53 | VALUES ($1, $2, $3, $4, $5, |
| 54 | NOT EXISTS (SELECT 1 FROM knowledge_bases WHERE workspace_id = $2), |
| 55 | COALESCE((SELECT default_ontology_lang FROM deployment_settings LIMIT 1), 'en')) |
| 56 | RETURNING *", |
| 57 | ) |
| 58 | .bind(Uuid::now_v7()) |
| 59 | .bind(workspace_id) |
| 60 | .bind(name) |
| 61 | .bind(kind) |
| 62 | .bind(description) |
| 63 | .fetch_one(pool) |
| 64 | .await?; |
| 65 | Ok(kb) |
| 66 | } |
| 67 | |
| 68 | pub async fn get(pool: &PgPool, id: Uuid) -> AppResult<KnowledgeBase> { |
| 69 | sqlx::query_as("SELECT * FROM knowledge_bases WHERE id = $1") |
| 70 | .bind(id) |
| 71 | .fetch_optional(pool) |
| 72 | .await? |
| 73 | .ok_or(AppError::NotFound) |
| 74 | } |
| 75 | |
| 76 | #[allow(clippy::too_many_arguments)] |
| 77 | pub async fn update( |
| 78 | pool: &PgPool, |
| 79 | id: Uuid, |
| 80 | name: Option<&str>, |
| 81 | description: Option<&str>, |
| 82 | visibility: Option<&str>, |
| 83 | auto_extend_ontology: Option<bool>, |
| 84 | ontology_lang: Option<&str>, |
| 85 | materialize_inferences: Option<bool>, |
| 86 | inference_interval_minutes: Option<i32>, |
| 87 | ) -> AppResult<KnowledgeBase> { |
| 88 | // 改语言不回头重写已有的类——它们已经是这个库的数据,可能有人手工调过。 |
| 89 | // 这一列往后管的是**新**描述(自动扩本体、AI 建议)写成什么语言 |
| 90 | if let Some(l) = ontology_lang { |
| 91 | if !matches!(l, "en" | "zh") { |
| 92 | return Err(AppError::invalid("bad_lang", "language must be en or zh")); |
| 93 | } |
| 94 | } |
| 95 | if let Some(v) = visibility { |
| 96 | if !matches!(v, "open" | "restricted") { |
| 97 | return Err(AppError::Validation( |
| 98 | "visibility must be open or restricted".into(), |
| 99 | )); |
| 100 | } |
| 101 | // 默认库永远 open:公共空间语义可依赖(改名/改描述不受限) |
| 102 | if v == "restricted" { |
| 103 | let current = get(pool, id).await?; |
| 104 | if current.is_default { |
| 105 | return Err(AppError::invalid( |
| 106 | "default_kb_open", |
| 107 | "The default knowledge base stays open to everyone.", |
| 108 | )); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | sqlx::query_as( |
| 113 | "UPDATE knowledge_bases |
| 114 | SET name = COALESCE($2, name), |
| 115 | description = COALESCE($3, description), |
| 116 | visibility = COALESCE($4, visibility), |
| 117 | auto_extend_ontology = COALESCE($5, auto_extend_ontology), |
| 118 | ontology_lang = COALESCE($6, ontology_lang), |
| 119 | materialize_inferences = COALESCE($7, materialize_inferences), |
| 120 | inference_interval_minutes = COALESCE($8, inference_interval_minutes), |
| 121 | updated_at = now() |
| 122 | WHERE id = $1 RETURNING *", |
| 123 | ) |
| 124 | .bind(id) |
| 125 | .bind(name) |
| 126 | .bind(description) |
| 127 | .bind(visibility) |
| 128 | .bind(auto_extend_ontology) |
| 129 | .bind(ontology_lang) |
| 130 | .bind(materialize_inferences) |
| 131 | .bind(inference_interval_minutes) |
| 132 | .fetch_optional(pool) |
| 133 | .await? |
| 134 | .ok_or(AppError::NotFound) |
| 135 | } |
| 136 | |
| 137 | pub async fn delete(pool: &PgPool, id: Uuid) -> AppResult<()> { |
| 138 | let current = get(pool, id).await?; |
| 139 | if current.is_default { |
| 140 | return Err(AppError::invalid( |
| 141 | "default_kb_undeletable", |
| 142 | "The default knowledge base cannot be deleted.", |
| 143 | )); |
| 144 | } |
| 145 | let res = sqlx::query("DELETE FROM knowledge_bases WHERE id = $1") |
| 146 | .bind(id) |
| 147 | .execute(pool) |
| 148 | .await?; |
| 149 | if res.rows_affected() == 0 { |
| 150 | return Err(AppError::NotFound); |
| 151 | } |
| 152 | Ok(()) |
| 153 | } |