feat: Implement delete functionality for generated questions in knowledge base, including API and UI updates

This commit is contained in:
wizardchen
2025-12-04 01:26:30 +08:00
parent 740974c53e
commit a50f53d205
18 changed files with 415 additions and 18 deletions
+5
View File
@@ -101,6 +101,11 @@ export function getChunkByIdOnly(chunkId: string) {
return get(`/api/v1/chunks/by-id/${chunkId}`);
}
// Delete a single generated question from a chunk by question ID
export function deleteGeneratedQuestion(chunkId: string, questionId: string) {
return del(`/api/v1/chunks/by-id/${chunkId}/questions`, { question_id: questionId });
}
export function listKnowledgeTags(
kbId: string,
params?: { page?: number; page_size?: number; keyword?: string },
+116 -8
View File
@@ -1,8 +1,8 @@
<script setup lang="ts">
import { marked } from "marked";
import { onMounted, ref, nextTick, onUnmounted, onUpdated, watch } from "vue";
import { downKnowledgeDetails } from "@/api/knowledge-base/index";
import { MessagePlugin } from "tdesign-vue-next";
import { downKnowledgeDetails, deleteGeneratedQuestion } from "@/api/knowledge-base/index";
import { MessagePlugin, DialogPlugin } from "tdesign-vue-next";
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security';
import { useI18n } from 'vue-i18n';
@@ -52,7 +52,7 @@ renderer.image = function (href, title, text) {
</figure>`;
};
const props = defineProps(["visible", "details", "knowledgeType", "sourceInfo"]);
const emit = defineEmits(["closeDoc", "getDoc"]);
const emit = defineEmits(["closeDoc", "getDoc", "questionDeleted"]);
watch(() => props.details.md, (newVal) => {
nextTick(async () => {
const images = mdContentWrap.value.querySelectorAll('img.markdown-image');
@@ -185,12 +185,26 @@ const getChunkMeta = (item: any) => {
return parts.join(' · ');
};
// 生成的问题类型
interface GeneratedQuestion {
id: string;
question: string;
}
// 解析生成的问题
const getGeneratedQuestions = (item: any): string[] => {
const getGeneratedQuestions = (item: any): GeneratedQuestion[] => {
if (!item || !item.metadata) return [];
try {
const metadata = typeof item.metadata === 'string' ? JSON.parse(item.metadata) : item.metadata;
return metadata.generated_questions || [];
const questions = metadata.generated_questions || [];
// 兼容旧格式(字符串数组)和新格式(对象数组)
return questions.map((q: string | GeneratedQuestion, index: number) => {
if (typeof q === 'string') {
// 旧格式:字符串,生成临时ID
return { id: `legacy-${index}`, question: q };
}
return q;
});
} catch {
return [];
}
@@ -211,6 +225,63 @@ const toggleQuestions = (index: number) => {
const isExpanded = (index: number) => expandedChunks.value.has(index);
// 删除中的状态
const deletingQuestion = ref<{ chunkIndex: number; questionId: string } | null>(null);
// 删除生成的问题
const handleDeleteQuestion = async (item: any, chunkIndex: number, question: GeneratedQuestion) => {
if (!item || !item.id) {
MessagePlugin.error(t('common.error') || '操作失败');
return;
}
// 检查是否是旧格式数据(无法删除)
if (question.id.startsWith('legacy-')) {
MessagePlugin.warning(t('knowledgeBase.legacyQuestionCannotDelete') || '旧格式问题无法删除,请重新生成问题');
return;
}
const confirmDialog = DialogPlugin.confirm({
header: t('common.confirmDelete') || '确认删除',
body: t('knowledgeBase.confirmDeleteQuestion') || '确定要删除这个问题吗?删除后将同时移除对应的向量索引。',
confirmBtn: t('common.confirm') || '确认',
cancelBtn: t('common.cancel') || '取消',
onConfirm: async () => {
confirmDialog.hide();
deletingQuestion.value = { chunkIndex, questionId: question.id };
try {
await deleteGeneratedQuestion(item.id, question.id);
MessagePlugin.success(t('common.deleteSuccess') || '删除成功');
// 更新本地数据
const metadata = typeof item.metadata === 'string' ? JSON.parse(item.metadata) : item.metadata;
if (metadata && metadata.generated_questions) {
const idx = metadata.generated_questions.findIndex((q: GeneratedQuestion) => q.id === question.id);
if (idx > -1) {
metadata.generated_questions.splice(idx, 1);
}
item.metadata = typeof item.metadata === 'string' ? JSON.stringify(metadata) : metadata;
}
// 通知父组件刷新数据
emit('questionDeleted', { chunkId: item.id, questionId: question.id });
} catch (error: any) {
MessagePlugin.error(error?.message || t('common.deleteFailed') || '删除失败');
} finally {
deletingQuestion.value = null;
}
},
onClose: () => {
confirmDialog.hide();
}
});
};
// 检查是否正在删除某个问题
const isDeleting = (chunkIndex: number, questionId: string) => {
return deletingQuestion.value?.chunkIndex === chunkIndex && deletingQuestion.value?.questionId === questionId;
};
const downloadFile = () => {
downKnowledgeDetails(props.details.id)
.then((result) => {
@@ -327,12 +398,24 @@ const handleDetailsScroll = () => {
</div>
<div v-show="isExpanded(index)" class="questions-list">
<div
v-for="(question, qIndex) in getGeneratedQuestions(item)"
:key="qIndex"
v-for="question in getGeneratedQuestions(item)"
:key="question.id"
class="question-item"
>
<t-icon name="help-circle" size="14px" class="question-icon" />
<span>{{ question }}</span>
<span class="question-text">{{ question.question }}</span>
<t-button
theme="default"
variant="text"
size="small"
class="delete-question-btn"
:loading="isDeleting(index, question.id)"
@click.stop="handleDeleteQuestion(item, index, question)"
>
<template #icon>
<t-icon name="delete" size="14px" />
</template>
</t-button>
</div>
</div>
</div>
@@ -597,12 +680,37 @@ const handleDetailsScroll = () => {
font-size: 13px;
color: #1d2129;
line-height: 1.5;
transition: background-color 0.2s ease;
&:hover {
background: #e6f7ed;
.delete-question-btn {
opacity: 1;
}
}
.question-icon {
color: #059669;
flex-shrink: 0;
margin-top: 2px;
}
.question-text {
flex: 1;
word-break: break-word;
}
.delete-question-btn {
opacity: 0;
flex-shrink: 0;
color: #999;
transition: opacity 0.2s ease, color 0.2s ease;
&:hover {
color: #e34d59;
}
}
}
.md-content {
+5 -1
View File
@@ -94,6 +94,8 @@ export default {
segment: 'Segment',
questions: 'Questions',
generatedQuestions: 'Generated Questions',
confirmDeleteQuestion: 'Are you sure you want to delete this question? The corresponding vector index will also be removed.',
legacyQuestionCannotDelete: 'Legacy format questions cannot be deleted. Please regenerate questions.',
notInitialized: 'Knowledge base is not initialized. Please configure models in settings before uploading files',
getInfoFailed: 'Failed to get knowledge base information, file upload is not possible',
missingId: 'Knowledge base ID is missing',
@@ -628,7 +630,9 @@ export default {
on: 'On',
off: 'Off',
resetToDefault: 'Reset to default',
confirmDelete: 'Confirm Delete'
confirmDelete: 'Confirm Delete',
deleteSuccess: 'Deleted successfully',
deleteFailed: 'Delete failed',
},
file: {
upload: 'Upload File',
+5 -1
View File
@@ -94,6 +94,8 @@ export default {
segment: 'Фрагмент',
questions: 'Вопросы',
generatedQuestions: 'Сгенерированные вопросы',
confirmDeleteQuestion: 'Вы уверены, что хотите удалить этот вопрос? Соответствующий векторный индекс также будет удален.',
legacyQuestionCannotDelete: 'Вопросы в устаревшем формате нельзя удалить. Пожалуйста, сгенерируйте вопросы заново.',
notInitialized: 'База знаний не инициализирована. Пожалуйста, настройте модели в разделе настроек перед загрузкой файлов',
getInfoFailed: 'Не удалось получить информацию о базе знаний, загрузка файла невозможна',
missingId: 'Отсутствует ID базы знаний',
@@ -629,7 +631,9 @@ export default {
on: 'Вкл',
off: 'Выкл',
resetToDefault: 'Сбросить по умолчанию',
confirmDelete: 'Подтвердить удаление'
confirmDelete: 'Подтвердить удаление',
deleteSuccess: 'Успешно удалено',
deleteFailed: 'Ошибка удаления',
},
file: {
upload: 'Загрузить файл',
+4
View File
@@ -93,6 +93,8 @@ export default {
segment: "片段",
questions: "问题",
generatedQuestions: "生成的问题",
confirmDeleteQuestion: "确定要删除这个问题吗?删除后将同时移除对应的向量索引。",
legacyQuestionCannotDelete: "旧格式问题无法删除,请重新生成问题",
docActionUnsupported: "当前知识库类型不支持该操作",
notInitialized:
"该知识库尚未完成初始化配置,请先前往设置页面配置模型信息后再上传文件",
@@ -722,6 +724,8 @@ export default {
off: "关闭",
resetToDefault: "恢复默认",
confirmDelete: "确认删除",
deleteSuccess: "删除成功",
deleteFailed: "删除失败",
},
file: {
upload: "上传文件",