From c27cadb998bc162becedec82e57837e10775989c Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Sat, 15 Feb 2025 20:44:19 +0800 Subject: [PATCH 01/13] add console api for clear all annotations --- api/controllers/console/app/annotation.py | 13 +++++ api/services/annotation_service.py | 49 +++++++++++++++++++ .../app/annotation/header-opts/index.tsx | 23 ++++++++- web/i18n/en-US/app-annotation.ts | 1 + web/i18n/zh-Hans/app-annotation.ts | 1 + 5 files changed, 86 insertions(+), 1 deletion(-) 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: { From 30159a4677cc50649a9abaca89990b858e8ce958 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Sun, 16 Feb 2025 10:49:42 +0800 Subject: [PATCH 02/13] fix --- api/services/annotation_service.py | 12 ------------ .../components/app/annotation/header-opts/index.tsx | 3 ++- web/service/annotation.ts | 4 ++++ 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/api/services/annotation_service.py b/api/services/annotation_service.py index b7288efe51..8445b56911 100644 --- a/api/services/annotation_service.py +++ b/api/services/annotation_service.py @@ -449,13 +449,6 @@ class AppAnnotationService: @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") @@ -465,10 +458,8 @@ class AppAnnotationService: 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) @@ -477,13 +468,10 @@ class AppAnnotationService: 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() ) diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index e072e577de..2f14af0755 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -22,6 +22,7 @@ import { ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows import I18n from '@/context/i18n' import { fetchExportAnnotationList } from '@/service/annotation' +import { clearAllAnnotations } from '@/service/annotation' import { LanguagesSupported } from '@/i18n/language' const CSV_HEADER_QA_EN = ['Question', 'Answer'] @@ -141,7 +142,7 @@ const HeaderOptions: FC = ({ onClick={handleClearAll} className='h-9 py-2 px-3 mx-1 flex items-center space-x-2 hover:bg-red-50 rounded-lg cursor-pointer disabled:opacity-50 w-[calc(100%_-_8px)] text-red-600' > - + {t('appAnnotation.table.header.clearAll')} diff --git a/web/service/annotation.ts b/web/service/annotation.ts index 5096a4f58a..0b268ed5d0 100644 --- a/web/service/annotation.ts +++ b/web/service/annotation.ts @@ -63,3 +63,7 @@ export const delAnnotation = (appId: string, annotationId: string) => { export const fetchHitHistoryList = (appId: string, annotationId: string, params: Record) => { return get(`apps/${appId}/annotations/${annotationId}/hit-histories`, { params }) } + +export const clearAllAnnotations = (appId: string): Promise => { + return post(`apps/${appId}/annotations/${annotationId}/clear-all`) +} From 9147a1a1792f43ad2efc2e2b364345d8db458748 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Sun, 16 Feb 2025 20:28:34 +0800 Subject: [PATCH 03/13] modify --- .../components/app/annotation/header-opts/index.tsx | 12 +++++++----- web/service/annotation.ts | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index 2f14af0755..5b7841ee94 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -79,15 +79,17 @@ const HeaderOptions: FC = ({ const [showBulkImportModal, setShowBulkImportModal] = useState(false) const handleClearAll = async () => { - await confirm({ + const isConfirmed= await confirm({ title: t('appAnnotation.table.header.clearAllConfirm'), type: 'danger', }) - try { - await clearAllAnnotations(appId) - onAdded() - } catch (e) { + if(isConfirmed){ + try { + await clearAllAnnotations(appId) + onAdded() + } catch (e) { console.error(e) + } } } const Operations = () => { diff --git a/web/service/annotation.ts b/web/service/annotation.ts index 0b268ed5d0..c7891604df 100644 --- a/web/service/annotation.ts +++ b/web/service/annotation.ts @@ -65,5 +65,5 @@ export const fetchHitHistoryList = (appId: string, annotationId: string, params: } export const clearAllAnnotations = (appId: string): Promise => { - return post(`apps/${appId}/annotations/${annotationId}/clear-all`) + return post(`apps/${appId}/annotations/clear-all`) } From ae7a5af7ff297ad6ad22a6c617f86adc710c379e Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Sun, 16 Feb 2025 20:33:34 +0800 Subject: [PATCH 04/13] modify --- web/app/components/app/annotation/header-opts/index.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index 5b7841ee94..269e4eeb2c 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -5,6 +5,7 @@ import { useTranslation } from 'react-i18next' import { RiAddLine, RiMoreFill, + RiDeleteBinLine } from '@remixicon/react' import { useContext } from 'use-context-selector' import { @@ -144,7 +145,7 @@ const HeaderOptions: FC = ({ onClick={handleClearAll} className='h-9 py-2 px-3 mx-1 flex items-center space-x-2 hover:bg-red-50 rounded-lg cursor-pointer disabled:opacity-50 w-[calc(100%_-_8px)] text-red-600' > - + {t('appAnnotation.table.header.clearAll')} From f575f89fee2d92b7db4332f705250b73eb04c4c3 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Sun, 16 Feb 2025 20:44:02 +0800 Subject: [PATCH 05/13] modify --- web/app/components/app/annotation/header-opts/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index 269e4eeb2c..da28c4e89b 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -79,9 +79,9 @@ const HeaderOptions: FC = ({ const [showBulkImportModal, setShowBulkImportModal] = useState(false) - const handleClearAll = async () => { + const handleClearAll = async () => { const isConfirmed= await confirm({ - title: t('appAnnotation.table.header.clearAllConfirm'), + message: t('appAnnotation.table.header.clearAllConfirm'), type: 'danger', }) if(isConfirmed){ From ed507b5e353540d651ab8ff0922acfa3a6729a17 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Sun, 16 Feb 2025 21:06:42 +0800 Subject: [PATCH 06/13] modify --- web/app/components/app/annotation/header-opts/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index da28c4e89b..0d4aa4804c 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -89,7 +89,6 @@ const HeaderOptions: FC = ({ await clearAllAnnotations(appId) onAdded() } catch (e) { - console.error(e) } } } From acaed249e8e3429ca0c65ca82f60b684bab50f92 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Mon, 17 Feb 2025 09:51:50 +0800 Subject: [PATCH 07/13] change api --- web/service/annotation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/service/annotation.ts b/web/service/annotation.ts index c7891604df..9f025f8eb9 100644 --- a/web/service/annotation.ts +++ b/web/service/annotation.ts @@ -65,5 +65,5 @@ export const fetchHitHistoryList = (appId: string, annotationId: string, params: } export const clearAllAnnotations = (appId: string): Promise => { - return post(`apps/${appId}/annotations/clear-all`) + return del(`apps/${appId}/annotations`) } From 2f670a65bcf6704ca39221b971ca4ddfb7a55731 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Mon, 17 Feb 2025 09:52:34 +0800 Subject: [PATCH 08/13] change api --- api/controllers/console/app/annotation.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/controllers/console/app/annotation.py b/api/controllers/console/app/annotation.py index 41ac659d0f..0b0be7fd71 100644 --- a/api/controllers/console/app/annotation.py +++ b/api/controllers/console/app/annotation.py @@ -123,6 +123,17 @@ class AnnotationListApi(Resource): } return response, 200 + @setup_required + @login_required + @account_initialization_required + def delete(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 + class AnnotationExportApi(Resource): @setup_required @@ -285,4 +296,3 @@ 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") From 1bbde1031989c0b0e4e43ecafef227d51c70ca6b Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Mon, 17 Feb 2025 09:56:10 +0800 Subject: [PATCH 09/13] remove useless code --- api/controllers/console/app/annotation.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/api/controllers/console/app/annotation.py b/api/controllers/console/app/annotation.py index 0b0be7fd71..77aff5e58c 100644 --- a/api/controllers/console/app/annotation.py +++ b/api/controllers/console/app/annotation.py @@ -271,18 +271,6 @@ 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( From 2245d55cee46143a6f9112559e2122f3ad877c37 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Mon, 17 Feb 2025 10:02:44 +0800 Subject: [PATCH 10/13] fix ci --- api/services/annotation_service.py | 1 - 1 file changed, 1 deletion(-) diff --git a/api/services/annotation_service.py b/api/services/annotation_service.py index 8445b56911..d006ce2668 100644 --- a/api/services/annotation_service.py +++ b/api/services/annotation_service.py @@ -482,4 +482,3 @@ class AppAnnotationService: ) return {"result": "success"} - From 2473000f38b2c9a0083de0ff118d2bd30b87a4e6 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Wed, 5 Mar 2025 23:01:46 +0800 Subject: [PATCH 11/13] fix CI --- web/app/components/app/annotation/header-opts/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index 0d4aa4804c..115eea6e8a 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -4,8 +4,8 @@ import React, { Fragment, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { RiAddLine, - RiMoreFill, - RiDeleteBinLine + RiDeleteBinLine, + RiMoreFill } from '@remixicon/react' import { useContext } from 'use-context-selector' import { From 43d63985eda4f9797381a7243cc08f593c3d3416 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Wed, 5 Mar 2025 23:17:08 +0800 Subject: [PATCH 12/13] fix CI --- .../app/annotation/header-opts/index.tsx | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/web/app/components/app/annotation/header-opts/index.tsx b/web/app/components/app/annotation/header-opts/index.tsx index 115eea6e8a..664c93a412 100644 --- a/web/app/components/app/annotation/header-opts/index.tsx +++ b/web/app/components/app/annotation/header-opts/index.tsx @@ -2,11 +2,7 @@ import type { FC } from 'react' import React, { Fragment, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' -import { - RiAddLine, - RiDeleteBinLine, - RiMoreFill -} from '@remixicon/react' +import { RiAddLine, RiDeleteBinLine, RiMoreFill } from '@remixicon/react' import { useContext } from 'use-context-selector' import { useCSVDownloader, @@ -70,8 +66,11 @@ const HeaderOptions: FC = ({ } useEffect(() => { - fetchList() - }, []) + const fetchData = async () => { + await fetchList() + } + fetchData() +}, [fetchList]) useEffect(() => { if (controlUpdateList) fetchList() @@ -80,15 +79,16 @@ const HeaderOptions: FC = ({ const [showBulkImportModal, setShowBulkImportModal] = useState(false) const handleClearAll = async () => { - const isConfirmed= await confirm({ + const isConfirmed = await confirm({ message: t('appAnnotation.table.header.clearAllConfirm'), type: 'danger', }) - if(isConfirmed){ + if (isConfirmed){ try { await clearAllAnnotations(appId) onAdded() - } catch (e) { + } catch (_) { + console.error('Failed to clear annotations') } } } From 59feff9a5a38876a10efafbc839218147eca3781 Mon Sep 17 00:00:00 2001 From: leslie2046 <253605712@qq.com> Date: Wed, 5 Mar 2025 23:22:21 +0800 Subject: [PATCH 13/13] fix CI --- web/i18n/en-US/app-annotation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/i18n/en-US/app-annotation.ts b/web/i18n/en-US/app-annotation.ts index 54a1cb4c5f..86554029be 100644 --- a/web/i18n/en-US/app-annotation.ts +++ b/web/i18n/en-US/app-annotation.ts @@ -17,7 +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.' + clearAllConfirm: 'Are you sure you want to delete all annotations? This action cannot be undone.', }, }, editModal: {