diff --git a/server/routers/data_router.py b/server/routers/data_router.py index c310db9c5..0112519b7 100644 --- a/server/routers/data_router.py +++ b/server/routers/data_router.py @@ -184,3 +184,18 @@ async def add_graph_entity(file_path: str = Body(...), kgdb_name: Optional[str] logger.error(f"添加实体失败: {e}, {traceback.format_exc()}") return {"message": f"添加实体失败: {e}", "status": "failed"} +@data.post("/update") +async def update_database_info( + db_id: str = Body(...), + name: str = Body(...), + description: str = Body(...), + current_user: User = Depends(get_admin_user) +): + logger.debug(f"Update database {db_id} info: {name}, {description}") + try: + database = knowledge_base.update_database(db_id, name, description) + return {"message": "更新成功", "database": database} + except Exception as e: + logger.error(f"更新数据库失败 {e}, {traceback.format_exc()}") + raise HTTPException(status_code=400, detail=f"更新数据库失败: {e}") + diff --git a/src/core/kb_db_manager.py b/src/core/kb_db_manager.py index 691ca19f0..959436b90 100644 --- a/src/core/kb_db_manager.py +++ b/src/core/kb_db_manager.py @@ -127,6 +127,21 @@ def delete_database(self, db_id): return True return False + def update_database(self, db_id, name, description): + """更新知识库信息""" + with self.get_session() as session: + db = session.query(KnowledgeDatabase).filter_by(db_id=db_id).first() + if not db: + raise ValueError(f"数据库 {db_id} 不存在") + + # 更新字段 + db.name = name + db.description = description + session.commit() + + # 返回更新后的数据库信息 + return self._to_dict_safely(db) + # 文件操作方法 def add_file(self, db_id, file_id, filename, path, file_type, status="waiting"): """添加文件""" diff --git a/src/core/knowledgebase.py b/src/core/knowledgebase.py index fcb246fce..69f8a6c24 100644 --- a/src/core/knowledgebase.py +++ b/src/core/knowledgebase.py @@ -136,8 +136,15 @@ def get_database_info(self, db_id): else: db_copy = db_dict.copy() try: + # 保存原始描述 + original_description = db_copy.get("description") + milvus_info = self.get_collection_info(db_id) db_copy.update(milvus_info) + + # 如果原始描述不为空,恢复它 + if original_description: + db_copy["description"] = original_description except Exception as e: logger.warning(f"获取知识库 ID: {db_id} 的Milvus信息失败: {e}") # 添加一个默认的Milvus状态 @@ -553,3 +560,14 @@ def check_embed_model(self, db_id): db = self.db_manager.get_database_by_id(db_id) return db["embed_model"] == self.embed_model.embed_model_fullname + def update_database(self, db_id, name, description): + """更新知识库信息""" + # 检查知识库是否存在 + db = self.db_manager.get_database_by_id(db_id) + if db is None: + raise Exception(f"数据库不存在: {db_id}") + + # 调用db_manager更新知识库信息 + updated_db = self.db_manager.update_database(db_id, name, description) + return updated_db + diff --git a/web/src/apis/admin_api.js b/web/src/apis/admin_api.js index 15dd3bdd6..80b85dc87 100644 --- a/web/src/apis/admin_api.js +++ b/web/src/apis/admin_api.js @@ -199,6 +199,20 @@ export const knowledgeBaseApi = { checkAdminPermission() return apiPost('/api/data/url-to-chunk', data, {}, true) }, + + /** + * 更新知识库信息 + * @param {string} dbId - 知识库ID + * @param {Object} data - 包含name和description的数据对象 + * @returns {Promise} - 更新结果 + */ + updateDatabaseInfo: async (dbId, data) => { + checkAdminPermission() + return apiPost('/api/data/update', { + db_id: dbId, + ...data + }, {}, true) + }, } // 图数据库管理API diff --git a/web/src/views/DataBaseInfoView.vue b/web/src/views/DataBaseInfoView.vue index ed566b8d8..90266fe25 100644 --- a/web/src/views/DataBaseInfoView.vue +++ b/web/src/views/DataBaseInfoView.vue @@ -14,12 +14,28 @@ 返回 + + + - 删除数据库 + + + + + + + + + + + + + +
@@ -333,6 +349,7 @@ import { LoadingOutlined, FileOutlined, LinkOutlined, + EditOutlined, } from '@ant-design/icons-vue' @@ -806,6 +823,54 @@ const getAuthHeaders = () => { return userStore.getAuthHeaders(); }; +// 编辑知识库表单 +const editModalVisible = ref(false); +const editFormRef = ref(null); +const editForm = reactive({ + name: '', + description: '' +}); + +const rules = { + name: [{ required: true, message: '请输入知识库名称' }] +}; + +// 显示编辑对话框 +const showEditModal = () => { + editForm.name = database.value.name || ''; + editForm.description = database.value.description || ''; + editModalVisible.value = true; +}; + +// 提交编辑表单 +const handleEditSubmit = () => { + editFormRef.value.validate().then(() => { + updateDatabaseInfo(); + }).catch(err => { + console.error('表单验证失败:', err); + }); +}; + +// 更新知识库信息 +const updateDatabaseInfo = async () => { + try { + state.lock = true; + const response = await knowledgeBaseApi.updateDatabaseInfo(databaseId.value, { + name: editForm.name, + description: editForm.description + }); + + message.success('知识库信息更新成功'); + editModalVisible.value = false; + await getDatabaseInfo(); // 刷新数据 + } catch (error) { + console.error(error); + message.error(error.message || '更新失败'); + } finally { + state.lock = false; + } +}; +