diff --git a/api/controllers/console/app/annotation.py b/api/controllers/console/app/annotation.py index 2b48afd550..41ac659d0f 100644 --- a/api/controllers/console/app/annotation.py +++ b/api/controllers/console/app/annotation.py @@ -260,6 +260,18 @@ class AnnotationHitHistoryListApi(Resource): } return response +class AnnotationClearAllApi(Resource): + @setup_required + @login_required + @account_initialization_required + def post(self, app_id): + if not current_user.is_editor: + raise Forbidden() + + app_id = str(app_id) + AppAnnotationService.clear_all_annotations(app_id) + return {"result": "success"}, 200 + api.add_resource(AnnotationReplyActionApi, "/apps//annotation-reply/") api.add_resource( @@ -273,3 +285,4 @@ api.add_resource(AnnotationBatchImportStatusApi, "/apps//annotation api.add_resource(AnnotationHitHistoryListApi, "/apps//annotations//hit-histories") api.add_resource(AppAnnotationSettingDetailApi, "/apps//annotation-setting") api.add_resource(AppAnnotationSettingUpdateApi, "/apps//annotation-settings/") +api.add_resource(AnnotationClearAllApi, "/apps//annotations/clear-all") diff --git a/api/services/annotation_service.py b/api/services/annotation_service.py index 8c950abc24..b7288efe51 100644 --- a/api/services/annotation_service.py +++ b/api/services/annotation_service.py @@ -446,3 +446,52 @@ class AppAnnotationService: "embedding_model_name": collection_binding_detail.model_name, }, } + + @classmethod + def clear_all_annotations(cls, app_id: str) -> dict: + """ + 清空指定应用的所有标注及其相关的命中历史记录。 + + :param app_id: 应用的ID + :return: 返回操作结果 + """ + # 获取应用信息 + app = ( + db.session.query(App) + .filter(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal") + .first() + ) + + if not app: + raise NotFound("App not found") + + # 删除所有标注 + annotations = db.session.query(MessageAnnotation).filter(MessageAnnotation.app_id == app_id).all() + for annotation in annotations: + # 删除标注的命中历史记录 + annotation_hit_histories = ( + db.session.query(AppAnnotationHitHistory) + .filter(AppAnnotationHitHistory.annotation_id == annotation.id) + .all() + ) + for annotation_hit_history in annotation_hit_histories: + db.session.delete(annotation_hit_history) + + # 删除标注本身 + db.session.delete(annotation) + + # 提交事务 + db.session.commit() + + # 如果标注回复功能已启用,清理相关的索引任务 + annotation_setting = ( + db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == app_id).first() + ) + if annotation_setting: + for annotation in annotations: + delete_annotation_index_task.delay( + annotation.id, app_id, current_user.current_tenant_id, annotation_setting.collection_binding_id + ) + + return {"result": "success"} + diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index eb397db55f..e072e577de 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -77,6 +77,18 @@ const HeaderOptions: FC = ({ const [showBulkImportModal, setShowBulkImportModal] = useState(false) + const handleClearAll = async () => { + await confirm({ + title: t('appAnnotation.table.header.clearAllConfirm'), + type: 'danger', + }) + try { + await clearAllAnnotations(appId) + onAdded() + } catch (e) { + console.error(e) + } + } const Operations = () => { return (
@@ -125,6 +137,15 @@ const HeaderOptions: FC = ({ +
) } @@ -138,7 +159,7 @@ const HeaderOptions: FC = ({
{t('appAnnotation.table.header.addAnnotation')}
} + htmlContent={} position="br" trigger="click" btnElement={ diff --git a/web/i18n/en-US/app-annotation.ts b/web/i18n/en-US/app-annotation.ts index 43f24a7619..54a1cb4c5f 100644 --- a/web/i18n/en-US/app-annotation.ts +++ b/web/i18n/en-US/app-annotation.ts @@ -17,6 +17,7 @@ const translation = { bulkImport: 'Bulk Import', bulkExport: 'Bulk Export', clearAll: 'Clear All Annotation', + clearAllConfirm: 'Are you sure you want to delete all annotations? This action cannot be undone.' }, }, editModal: { diff --git a/web/i18n/zh-Hans/app-annotation.ts b/web/i18n/zh-Hans/app-annotation.ts index 3a6cacf5b5..060d9163c9 100644 --- a/web/i18n/zh-Hans/app-annotation.ts +++ b/web/i18n/zh-Hans/app-annotation.ts @@ -19,6 +19,7 @@ const translation = { bulkImport: '批量导入', bulkExport: '批量导出', clearAll: '删除所有标注', + clearAllConfirm: '您是否确定要删除所有标注?这个动作不可撤回!', }, }, editModal: {