feat: support multi-tag for document knowledge (#1790)

* feat: support multi-tag for document knowledge

- Add knowledge_tag_relations join table for many-to-many tag-knowledge mapping
- Migrate existing single tag_id data to the new table
- Update Knowledge struct, filter, payload types (TagID -> Tags/TagIDs)
- Rewrite repository layer: SetKnowledgeTags, GetKnowledgeTags, ListIDsByTagIDs
- Update tag count references to query knowledge_tag_relations
- Update service/handler layers for multi-tag create/list/batch-update
- Frontend: multi-select tag sidebar, multi-tag chip display, tag toggle in popup
- FAQ tags remain single-tag model (unchanged)

* feat(knowledge): tag edit dialog with dynamic overflow and inline creation

- Add TagEditDialog component with search, multi-select, and confirm
- Replace inline t-popup tag selector with dialog-based editing
- Tags overflow with +N badge, visible count adapts to container width via ResizeObserver
- Date/card layout: fixed date width, tags flex to fill remaining space
- Add persistent new-tag input bar in dialog (Enter to create, auto-select)
- Tag creation emits callback to refresh parent tag list immediately
- Apply same overflow + dialog trigger to both card view and list view
- Add i18n keys: tagEditDialogTitle, tagEditSearch, tagSelectedCount, tagOverflowTip, tagNewPlaceholder

* fix: exclude soft-deleted knowledge from tag reference counts

CountReferences, BatchCountReferences, and DeleteUnusedTags now
JOIN with knowledges table to filter out soft-deleted records
when counting knowledge_tag_relations. Previously they queried
knowledge_tag_relations directly without the deleted_at check,
causing stale tag counts after knowledge deletion.

* fix: refresh tag counts after async knowledge deletion completes

Add a second delayed tag list refresh (2.5s) after tag deletion to capture
updated reference counts for remaining tags. The original immediate refresh
handles removing the deleted tag from the list, while this delayed refresh
ensures other tags' counts are correct after the async deletion finishes.

---------

Co-authored-by: lyingbug <xiayula2014@gmail.com>
This commit is contained in:
Anky
2026-06-26 00:18:48 +08:00
committed by GitHub
parent 5c0e97c12e
commit c007a897cf
27 changed files with 1176 additions and 190 deletions
+12 -10
View File
@@ -160,12 +160,12 @@ export function togglePinKnowledgeBase(id: string) {
}
// 知识文件 API(基于具体知识库)
// data.tag_id: 可选,指定知识所属的分类ID
// data.tag_ids: 可选,指定知识所属的多个分类ID
export function uploadKnowledgeFile(
kbId: string,
data: {
file: File
tag_id?: string
tag_ids?: string[]
fileName?: string
process_config?: KnowledgeProcessOverrides | string
[key: string]: any
@@ -176,7 +176,9 @@ export function uploadKnowledgeFile(
Object.keys(data).forEach(key => {
const value = data[key];
if (value === undefined) return;
if (key === 'process_config' && value && typeof value !== 'string') {
if (key === 'tag_ids' && Array.isArray(value)) {
formData.append(key, value.join(','));
} else if (key === 'process_config' && value && typeof value !== 'string') {
formData.append(key, JSON.stringify(value));
} else {
formData.append(key, value);
@@ -186,23 +188,23 @@ export function uploadKnowledgeFile(
}
// 从URL创建知识
// data.tag_id: 可选,指定知识所属的分类ID
// data.tag_ids: 可选,指定知识所属的多个分类ID
export function createKnowledgeFromURL(
kbId: string,
data: { url: string; enable_multimodel?: boolean; tag_id?: string; process_config?: KnowledgeProcessOverrides },
data: { url: string; enable_multimodel?: boolean; tag_ids?: string[]; process_config?: KnowledgeProcessOverrides },
) {
return post(`/api/v1/knowledge-bases/${kbId}/knowledge/url`, data);
}
// 手工创建知识
// data.tag_id: 可选,指定知识所属的分类ID
// data.tag_ids: 可选,指定知识所属的分类ID
export function createManualKnowledge(
kbId: string,
data: {
title: string
content: string
status: string
tag_id?: string
tag_ids?: string[]
process_config?: KnowledgeProcessOverrides
},
) {
@@ -214,7 +216,7 @@ export function listKnowledgeFiles(
params: {
page: number;
page_size: number;
tag_id?: string;
tag_ids?: string;
keyword?: string;
file_type?: string;
parse_status?: string;
@@ -226,7 +228,7 @@ export function listKnowledgeFiles(
const query = new URLSearchParams();
query.append('page', String(params.page));
query.append('page_size', String(params.page_size));
if (params.tag_id) query.append('tag_id', params.tag_id);
if (params.tag_ids) query.append('tag_ids', params.tag_ids);
if (params.keyword) query.append('keyword', params.keyword);
if (params.file_type) query.append('file_type', params.file_type);
if (params.parse_status) query.append('parse_status', params.parse_status);
@@ -331,7 +333,7 @@ export function deleteKnowledgeBaseTag(kbId: string, tagSeqId: number, params?:
return del(`/api/v1/knowledge-bases/${kbId}/tags/${tagSeqId}${forceQuery}`);
}
export function updateKnowledgeTagBatch(data: { updates: Record<string, string | null> }) {
export function updateKnowledgeTagBatch(data: { updates: Record<string, string[]> }) {
return put(`/api/v1/knowledge/tags`, data);
}
@@ -611,20 +611,20 @@ const handleSave = async (targetStatus: ManualStatus) => {
saving.value = true
savingAction.value = targetStatus
try {
const tagIdToUpload = uiStore.selectedTagId !== '__untagged__' ? uiStore.selectedTagId : undefined
const tagIdsToUpload = uiStore.selectedTagIds.length > 0 ? [...uiStore.selectedTagIds] : undefined
const payload: {
title: string
content: string
status: string
tag_id?: string
tag_ids?: string[]
process_config?: KnowledgeProcessOverrides
} = {
title: form.title.trim(),
content: form.content,
status: targetStatus,
}
if (tagIdToUpload) {
payload.tag_id = tagIdToUpload
if (tagIdsToUpload && tagIdsToUpload.length > 0) {
payload.tag_ids = tagIdsToUpload
}
if (targetStatus === 'publish') {
@@ -649,7 +649,7 @@ const handleSave = async (targetStatus: ManualStatus) => {
knowledgeId: knowledgeId.value || undefined,
title: payload.title,
content: payload.content,
tagId: tagIdToUpload,
tagIds: tagIdsToUpload,
},
})
payload.process_config = confirmResult.processConfig
+4 -4
View File
@@ -42,7 +42,7 @@ export default function (knowledgeBaseId?: string) {
query: {
page: number;
page_size: number;
tag_id?: string;
tag_ids?: string;
keyword?: string;
file_type?: string;
parse_status?: string;
@@ -155,9 +155,9 @@ export default function (knowledgeBaseId?: string) {
// 获取当前选中的分类ID
const uiStore = useUIStore();
const tagIdToUpload = uiStore.selectedTagId !== '__untagged__' ? uiStore.selectedTagId : undefined;
uploadKnowledgeFile(currentKbId, { file, tag_id: tagIdToUpload })
const tagIdsToUpload = uiStore.selectedTagIds.length > 0 ? [...uiStore.selectedTagIds] : undefined;
uploadKnowledgeFile(currentKbId, { file, tag_ids: tagIdsToUpload })
.then((result: any) => {
if (result.success) {
MessagePlugin.info(t('knowledgeBase.uploadSuccess'));
+5
View File
@@ -370,6 +370,11 @@ export default {
publishToWeb: 'Publish to Web',
documentCategoryTitle: 'Document Categories',
tagUpdateSuccess: 'Tag updated successfully',
tagEditDialogTitle: 'Edit Tags — {name}',
tagEditSearch: 'Search tags...',
tagSelectedCount: '{count} selected',
tagOverflowTip: 'Click to edit tags',
tagNewPlaceholder: 'New tag name, press Enter to add',
category: 'Category',
faqCategoryTitle: 'FAQ Categories',
untagged: 'Uncategorized',
+5
View File
@@ -372,6 +372,11 @@ export default {
untagged: "미분류",
tagClearAction: "분류 해제",
tagUpdateSuccess: "태그 업데이트 성공",
tagEditDialogTitle: '태그 편집 — {name}',
tagEditSearch: '태그 검색...',
tagSelectedCount: '{count}개 선택됨',
tagOverflowTip: '클릭하여 태그 편집',
tagNewPlaceholder: '새 태그 이름 입력, Enter로 추가',
tagSearchTooltip: "태그 검색",
category: "분류",
tagCreateAction: "태그 생성",
+5
View File
@@ -313,6 +313,11 @@ export default {
tagClearAction: 'Снять категорию',
tagSearchTooltip: 'Поиск тегов',
tagUpdateSuccess: 'Тег успешно обновлен',
tagEditDialogTitle: 'Редактировать теги — {name}',
tagEditSearch: 'Поиск тегов...',
tagSelectedCount: 'Выбрано: {count}',
tagOverflowTip: 'Нажмите, чтобы изменить теги',
tagNewPlaceholder: 'Название нового тега, Enter для добавления',
category: 'Категория',
tagCreateAction: 'Создать тег',
tagSearchPlaceholder: 'Введите название тега',
+5
View File
@@ -373,6 +373,11 @@ export default {
untagged: "未分类",
tagClearAction: "取消分类",
tagUpdateSuccess: "标签更新成功",
tagEditDialogTitle: '编辑标签 — {name}',
tagEditSearch: "搜索标签...",
tagSelectedCount: "已选 {count} 个标签",
tagOverflowTip: "点击编辑标签",
tagNewPlaceholder: "输入新标签名称,回车添加",
tagSearchTooltip: "搜索标签",
category: "分类",
tagCreateAction: "新建标签",
+12 -3
View File
@@ -8,7 +8,7 @@ export const useUIStore = defineStore('ui', {
currentKBId: null as string | null,
kbEditorType: 'document' as 'document' | 'faq',
// 当前选中的分类ID,用于文件上传时传递
selectedTagId: '__untagged__' as string,
selectedTagIds: [] as string[],
kbEditorInitialSection: null as string | null,
settingsInitialSection: null as string | null,
settingsInitialSubSection: null as string | null,
@@ -107,8 +107,17 @@ export const useUIStore = defineStore('ui', {
},
// 设置当前选中的分类ID
setSelectedTagId(tagId: string) {
this.selectedTagId = tagId
toggleSelectedTagId(tagId: string) {
const idx = this.selectedTagIds.indexOf(tagId)
if (idx >= 0) {
this.selectedTagIds.splice(idx, 1)
} else {
this.selectedTagIds.push(tagId)
}
},
clearSelectedTagIds() {
this.selectedTagIds = []
},
toggleSidebar() {
+1 -1
View File
@@ -8,7 +8,7 @@ export interface UploadConfirmManualSource {
knowledgeId?: string
title: string
content: string
tagId?: string
tagIds?: string[]
}
export interface UploadConfirmReparseSource {
+269 -67
View File
@@ -45,6 +45,7 @@ import FAQEntryManager from './components/FAQEntryManager.vue';
import DocumentListView from './components/DocumentListView.vue';
import DocumentBatchBar from './components/DocumentBatchBar.vue';
import KbUploadSourceDropdown from './components/KbUploadSourceDropdown.vue';
import TagEditDialog from './components/TagEditDialog.vue';
import type { KnowledgeProcessOverrides } from '@/types/knowledgeProcess';
import { useUploadConfirmStore, type UploadConfirmResult } from '@/stores/uploadConfirm';
import WikiBrowser from './wiki/WikiBrowser.vue';
@@ -490,7 +491,7 @@ const confirmBatchReparse = async () => {
}
};
const selectedTagId = ref<string>('');
const selectedTagIds = ref<string[]>([]);
const tagList = ref<any[]>([]);
const tagLoading = ref(false);
const tagSearchQuery = ref('');
@@ -556,7 +557,7 @@ const disableFutureDate = { after: new Date(new Date().setHours(23, 59, 59, 999)
const filterParams = computed(() => {
const [start, end] = updatedTimeRange.value || [];
return {
tag_id: selectedTagId.value || undefined,
tag_ids: selectedTagIds.value.length > 0 ? selectedTagIds.value.join(',') : undefined,
keyword: docSearchKeyword.value ? docSearchKeyword.value.trim() : undefined,
file_type: selectedFileType.value || undefined,
parse_status: selectedParseStatus.value || undefined,
@@ -568,10 +569,23 @@ const filterParams = computed(() => {
type TagInputInstance = ComponentPublicInstance<{ focus: () => void; select: () => void }>;
const onPickTag = (item: any, tagId: string | number) => {
if (item) item.isTagPopup = false;
const currentId = item?.tag_id ? String(item.tag_id) : '';
const nextId = tagId ? String(tagId) : '';
if (currentId === nextId) return;
handleKnowledgeTagChange(item.id, nextId);
const currentTags = item?.tags || [];
const tagIdStr = tagId ? String(tagId) : '';
let updatedTags: string[];
if (!tagIdStr) {
// Clear all tags
updatedTags = [];
} else {
const idx = currentTags.findIndex((t: any) => t.id === tagIdStr);
if (idx >= 0) {
// Remove tag
updatedTags = currentTags.filter((t: any) => t.id !== tagIdStr).map((t: any) => t.id);
} else {
// Add tag
updatedTags = [...currentTags.map((t: any) => t.id), tagIdStr];
}
}
handleKnowledgeTagChange(item.id, updatedTags);
};
const tagMap = computed<Record<string, any>>(() => {
const map: Record<string, any> = {};
@@ -605,6 +619,74 @@ const newTagName = ref('');
const editingTagId = ref<string | null>(null);
const editingTagName = ref('');
const editingTagSubmitting = ref(false);
// 标签编辑弹窗
const tagEditDialogVisible = ref(false);
const tagEditTarget = ref<KnowledgeCard | null>(null);
// 动态标签可见数量(根据卡片容器宽度自适应)
const TAG_EST_WIDTH = 82; // 单个标签估算宽度(含间距)
const TAG_OVERFLOW_MIN = 32; // +N 徽章预留宽度
const tagVisibleLimit = reactive<Record<string, number>>({});
const tagItemTotalMap = new Map<string, number>();
let tagChipsRO: ResizeObserver | null = null;
function setupTagChipsObserver(el: Element | null, itemId: string, totalCount: number) {
if (!el) return;
const htmlEl = el as HTMLElement;
htmlEl.dataset.tagItemId = itemId;
tagItemTotalMap.set(itemId, totalCount);
if (!tagChipsRO) {
tagChipsRO = new ResizeObserver((entries) => {
for (const entry of entries) {
const target = entry.target as HTMLElement;
const id = target.dataset.tagItemId;
if (!id) continue;
const w = entry.contentRect.width;
const total = tagItemTotalMap.get(id) ?? 0;
if (total <= 0) { tagVisibleLimit[id] = 99; continue; }
// 计算能容纳几个标签:总宽 - 溢出徽章预留,除以单标签宽
const maxFit = Math.floor((w - TAG_OVERFLOW_MIN) / TAG_EST_WIDTH);
const limit = Math.max(1, Math.min(maxFit, total));
tagVisibleLimit[id] = limit >= total ? 99 : limit;
}
});
}
tagChipsRO.observe(htmlEl);
// 初始计算
requestAnimationFrame(() => {
const w = htmlEl.clientWidth;
const total = totalCount;
if (total <= 0) { tagVisibleLimit[itemId] = 99; return; }
const maxFit = Math.floor((w - TAG_OVERFLOW_MIN) / TAG_EST_WIDTH);
const limit = Math.max(1, Math.min(maxFit, total));
tagVisibleLimit[itemId] = limit >= total ? 99 : limit;
});
}
function getTagLimit(itemId: string): number {
return tagVisibleLimit[itemId] ?? 99;
}
function hasTagOverflow(itemId: string, total: number): boolean {
const limit = tagVisibleLimit[itemId] ?? 99;
return total > limit;
}
function getOverflowCount(itemId: string, total: number): number {
const limit = tagVisibleLimit[itemId] ?? 99;
return Math.max(0, total - limit);
}
function openTagEditDialog(item: KnowledgeCard) {
tagEditTarget.value = item;
tagEditDialogVisible.value = true;
}
function onTagEditConfirm(tagIds: string[]) {
if (tagEditTarget.value) {
handleKnowledgeTagChange(tagEditTarget.value.id, tagIds);
}
}
const getPageSize = () => {
const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
const itemHeight = 148;
@@ -731,10 +813,11 @@ const loadTags = async (kbIdValue: string, reset = false) => {
}
};
const handleTagFilterChange = (value: string) => {
selectedTagId.value = value;
// 同步更新 store 中的 selectedTagId,供 menu.vue 上传时使用
uiStore.setSelectedTagId(value);
const handleTagFilterChange = (tagIds: string[]) => {
selectedTagIds.value = tagIds;
// 同步更新 store 中的 selectedTagIds,供 menu.vue 上传时使用
uiStore.clearSelectedTagIds();
tagIds.forEach(id => uiStore.toggleSelectedTagId(id));
resetPage();
loadKnowledgeFiles(kbId.value);
};
@@ -748,11 +831,14 @@ const handleTagRowClick = (tagId: string) => {
editingTagId.value = null;
editingTagName.value = '';
}
if (selectedTagId.value === tagId) {
handleTagFilterChange('');
return;
// Toggle selection
const idx = selectedTagIds.value.indexOf(tagId);
if (idx >= 0) {
selectedTagIds.value.splice(idx, 1);
} else {
selectedTagIds.value.push(tagId);
}
handleTagFilterChange(tagId);
handleTagFilterChange([...selectedTagIds.value]);
};
const startCreateTag = () => {
@@ -862,28 +948,30 @@ const confirmDeleteTag = (tag: any) => {
deleteKnowledgeBaseTag(kbId.value, tag.seq_id, { force: true })
.then(() => {
MessagePlugin.success(t('knowledgeBase.tagDeleteSuccess'));
if (selectedTagId.value === tag.id) {
if (selectedTagIds.value.includes(tag.id)) {
// Reset to show all entries when current tag is deleted
selectedTagId.value = '';
handleTagFilterChange('');
selectedTagIds.value = selectedTagIds.value.filter(id => id !== tag.id);
handleTagFilterChange([...selectedTagIds.value]);
}
loadTags(kbId.value, true);
// 由于后端是异步删除文档,延迟刷新以确保看到最新数据
// 由于后端是异步删除文档,延迟刷新文档列表
setTimeout(() => {
resetPage(); // Reset page counter when reloading files after tag deletion
loadKnowledgeFiles(kbId.value);
}, 500);
}, 800);
// 再次延迟刷新标签列表,确保异步删除完成后其他标签的数量正确更新
setTimeout(() => {
loadTags(kbId.value, true);
}, 2500);
})
.catch((error: any) => {
MessagePlugin.error(error?.message || t('common.operationFailed'));
});
};
const handleKnowledgeTagChange = async (knowledgeId: string, tagValue: string) => {
const handleKnowledgeTagChange = async (knowledgeId: string, tagIds: string[]) => {
try {
// Pass the tag value directly (empty string means no tag)
const tagIdToUpdate = tagValue || null;
await updateKnowledgeTagBatch({ updates: { [knowledgeId]: tagIdToUpdate } });
await updateKnowledgeTagBatch({ updates: { [knowledgeId]: tagIds } });
MessagePlugin.success(t('knowledgeBase.tagUpdateSuccess'));
resetPage(); // Reset page counter to 1 when reloading files after tag change
loadKnowledgeFiles(kbId.value);
@@ -906,9 +994,10 @@ const loadKnowledgeBaseInfo = async (targetKbId: string, force = false) => {
if (!isCurrentKb(targetKbId)) return;
kbInfo.value = data;
selectedTagId.value = '';
selectedTagIds.value = [];
uiStore.clearSelectedTagIds();
// 重置store中的标签选择状态,避免上传文档时自动带上之前选择的标签
uiStore.setSelectedTagId('');
uiStore.clearSelectedTagIds();
if (!isFAQ.value) {
docListLoading.value = true;
loadKnowledgeFiles(targetKbId);
@@ -988,14 +1077,14 @@ watch(() => kbId.value, (newKbId, oldKbId) => {
resetPage();
tagSearchQuery.value = '';
tagPage.value = 1;
uiStore.setSelectedTagId('');
uiStore.clearSelectedTagIds();
}
loadKnowledgeBaseInfo(newKbId);
}, { immediate: true });
watch(selectedTagId, (newVal, oldVal) => {
watch(selectedTagIds, (newVal, oldVal) => {
if (oldVal === undefined) return
if (newVal !== oldVal && kbId.value) {
if (kbId.value) {
loadKnowledgeFiles(kbId.value);
}
});
@@ -1173,7 +1262,7 @@ type KnowledgeCard = {
isMore?: boolean;
metadata?: any;
error_message?: string;
tag_id?: string;
tags?: Array<{ id: string; name: string; color?: string }>;
};
// needsStatusPolling decides whether a card row is still "in flight"
// enough that the doc list should keep refreshing it. Keep in sync with
@@ -1530,7 +1619,7 @@ const executeUploadBatch = async (
return { successCount: 0, failCount: files.length };
}
const tagIdToUpload = selectedTagId.value !== '__untagged__' ? selectedTagId.value : undefined;
const tagIdsToUpload = selectedTagIds.value.length > 0 ? [...selectedTagIds.value] : undefined;
let successCount = 0;
let failCount = 0;
const totalCount = files.length;
@@ -1543,10 +1632,10 @@ const executeUploadBatch = async (
try {
const uploadData: {
file: File
tag_id?: string
tag_ids?: string[]
fileName?: string
process_config?: KnowledgeProcessOverrides
} = { file, tag_id: tagIdToUpload };
} = { file, tag_ids: tagIdsToUpload };
const fileName = getFolderUploadFileName(file);
if (fileName) uploadData.fileName = fileName;
@@ -1602,11 +1691,11 @@ const executeUrlImport = async (url: string, processConfig?: KnowledgeProcessOve
return;
}
const tagIdToUpload = selectedTagId.value !== '__untagged__' ? selectedTagId.value : undefined;
const tagIdsToUpload = selectedTagIds.value.length > 0 ? [...selectedTagIds.value] : undefined;
try {
const responseData: any = await createKnowledgeFromURL(targetKbId, {
url,
tag_id: tagIdToUpload,
tag_ids: tagIdsToUpload,
process_config: processConfig,
});
window.dispatchEvent(new CustomEvent('knowledgeFileUploaded', {
@@ -2010,7 +2099,7 @@ const handleListAction = (
// Clear selection on filter/tag/kb change to avoid acting on hidden items.
watch(
[selectedTagId, docSearchKeyword, selectedFileType, selectedParseStatus, selectedSource, updatedTimeRange, kbId],
[selectedTagIds, docSearchKeyword, selectedFileType, selectedParseStatus, selectedSource, updatedTimeRange, kbId],
() => {
clearSelection();
},
@@ -2223,7 +2312,7 @@ async function createNewSession(value: string): Promise<void> {
<template v-if="filteredTags.length">
<div v-for="tag in filteredTags" :key="tag.id" class="tag-list-item"
:class="{ active: selectedTagId === tag.id, editing: editingTagId === tag.id }"
:class="{ active: selectedTagIds.includes(tag.id), editing: editingTagId === tag.id }"
@click="handleTagRowClick(tag.id)">
<div class="tag-list-left">
<span class="tag-hash-icon">#</span>
@@ -2534,39 +2623,92 @@ async function createNewSession(value: string): Promise<void> {
<span class="card-time">{{ formatDocTime(item.updated_at) }}</span>
<div class="card-bottom-right">
<div v-if="tagList.length" class="card-tag-selector" @click.stop>
<t-popup v-if="canEdit" v-model="item.isTagPopup" trigger="click" placement="bottom-right"
overlayClassName="card-tag-popup" destroy-on-close>
<template #content>
<div class="tag-popup-list">
<div v-for="tag in tagList" :key="tag.id" class="tag-popup-item"
:class="{ 'is-selected': String(item.tag_id) === String(tag.id) }"
@click="onPickTag(item, tag.id)">
<t-icon class="tag-popup-check"
:class="{ visible: String(item.tag_id) === String(tag.id) }" name="check" />
<span class="tag-popup-name">{{ tag.name }}</span>
<!-- 可编辑模式:点击打开弹窗 -->
<template v-if="canEdit">
<template v-if="(item.tags || []).length > 0">
<t-tooltip
v-if="hasTagOverflow(item.id, (item.tags || []).length)"
:content="(item.tags || []).map((t: any) => t.name).join(', ')"
placement="top"
>
<div
class="card-tag-chips"
:ref="(el: any) => setupTagChipsObserver(el, item.id, (item.tags || []).length)"
@click="openTagEditDialog(item)"
>
<t-tag
v-for="tag in (item.tags || []).slice(0, getTagLimit(item.id))"
:key="tag.id"
size="small"
variant="light-outline"
class="card-tag-chip"
>
<span class="tag-text">{{ tag.name }}</span>
</t-tag>
<span class="card-tag-overflow">+{{ getOverflowCount(item.id, (item.tags || []).length) }}</span>
</div>
<template v-if="item.tag_id">
<div class="tag-popup-divider"></div>
<div class="tag-popup-item is-action" @click="onPickTag(item, '')">
<t-icon class="tag-popup-check" name="close" />
<span class="tag-popup-name">{{ t('knowledgeBase.tagClearAction') }}</span>
</div>
</template>
</t-tooltip>
<div
v-else
class="card-tag-chips"
:ref="(el: any) => setupTagChipsObserver(el, item.id, (item.tags || []).length)"
@click="openTagEditDialog(item)"
>
<t-tag
v-for="tag in (item.tags || []).slice(0, getTagLimit(item.id))"
:key="tag.id"
size="small"
variant="light-outline"
class="card-tag-chip"
>
<span class="tag-text">{{ tag.name }}</span>
</t-tag>
</div>
</template>
<t-tag v-if="getTagName(item.tag_id)" size="small" variant="light-outline"
class="card-tag-chip">
<span class="tag-text">{{ getTagName(item.tag_id) }}</span>
</t-tag>
<span v-else class="card-tag-add">
<span v-else class="card-tag-add" @click="openTagEditDialog(item)">
<t-icon name="add" size="12px" />
<span>{{ t('knowledgeBase.tagLabel') }}</span>
</span>
</t-popup>
<t-tag v-else-if="getTagName(item.tag_id)" size="small" variant="light-outline"
class="card-tag-chip">
<span class="tag-text">{{ getTagName(item.tag_id) }}</span>
</t-tag>
</template>
<!-- 只读模式 -->
<template v-else-if="(item.tags || []).length > 0">
<t-tooltip
v-if="hasTagOverflow(item.id, (item.tags || []).length)"
:content="(item.tags || []).map((t: any) => t.name).join(', ')"
placement="top"
>
<div
class="card-tag-chips"
:ref="(el: any) => setupTagChipsObserver(el, item.id, (item.tags || []).length)"
>
<t-tag
v-for="tag in (item.tags || []).slice(0, getTagLimit(item.id))"
:key="tag.id"
size="small"
variant="light-outline"
class="card-tag-chip"
>
<span class="tag-text">{{ tag.name }}</span>
</t-tag>
<span class="card-tag-overflow">+{{ getOverflowCount(item.id, (item.tags || []).length) }}</span>
</div>
</t-tooltip>
<div
v-else
class="card-tag-chips"
:ref="(el: any) => setupTagChipsObserver(el, item.id, (item.tags || []).length)"
>
<t-tag
v-for="tag in (item.tags || []).slice(0, getTagLimit(item.id))"
:key="tag.id"
size="small"
variant="light-outline"
class="card-tag-chip"
>
<span class="tag-text">{{ tag.name }}</span>
</t-tag>
</div>
</template>
</div>
<div class="card-type">
<span>{{ getKnowledgeType(item) }}</span>
@@ -2615,9 +2757,9 @@ async function createNewSession(value: string): Promise<void> {
}}</span>
<span v-if="(hoveredCardItem as any).channel && (hoveredCardItem as any).channel !== 'web'"
class="card-popover-channel">{{ getChannelLabel((hoveredCardItem as any).channel) }}</span>
<span v-if="getTagName(hoveredCardItem.tag_id)" class="card-popover-tag">{{
getTagName(hoveredCardItem.tag_id)
}}</span>
<span v-if="(hoveredCardItem as any).tags && (hoveredCardItem as any).tags.length > 0" class="card-popover-tag">{{
(hoveredCardItem as any).tags.map((t: any) => t.name).join(', ')
}}</span>
<span class="card-popover-type">{{ getKnowledgeType(hoveredCardItem) }}</span>
</div>
<div class="card-popover-hint">{{ t('knowledgeBase.clickToViewFull') }}</div>
@@ -2629,7 +2771,8 @@ async function createNewSession(value: string): Promise<void> {
<DocumentListView :items="cardList" :selected-ids="selectedIds" :tag-list="tagList"
:can-edit="canEdit" @open="(item: any) => openKnowledgeItem(item)" @toggle-row="toggleSelectRow"
@toggle-all="toggleSelectAll"
@action="(action: any, item: any) => handleListAction(action, item)" />
@action="(action: any, item: any) => handleListAction(action, item)"
@tag-edit="(item: any) => openTagEditDialog(item)" />
</template>
<template v-else-if="!docListLoading">
<div class="doc-empty-state">
@@ -2665,6 +2808,18 @@ async function createNewSession(value: string): Promise<void> {
@update:visible="(val) => val ? null : uiStore.closeKBEditor()" @success="handleKBEditorSuccess" />
<ContextualGuide tour="kbDetail" :when="showKbDetailContextualGuide" />
<!-- 标签编辑弹窗 -->
<TagEditDialog
:visible="tagEditDialogVisible"
:knowledge-name="tagEditTarget?.display_name || tagEditTarget?.file_name || tagEditTarget?.title || ''"
:kb-id="kbId"
:tag-list="tagList"
:selected-tags="tagEditTarget?.tags || []"
@update:visible="tagEditDialogVisible = $event"
@confirm="onTagEditConfirm"
@tag-created="loadTags(kbId, true)"
/>
</template>
<style>
/* 下拉菜单容器样式已统一至 @/assets/dropdown-menu.less */
@@ -3528,6 +3683,36 @@ async function createNewSession(value: string): Promise<void> {
display: flex;
align-items: center;
.card-tag-chips {
display: inline-flex;
align-items: center;
gap: 4px;
flex-wrap: nowrap;
cursor: pointer;
}
.card-tag-overflow {
display: inline-flex;
align-items: center;
justify-content: center;
height: 18px;
min-width: 18px;
padding: 0 5px;
border-radius: 999px;
border: 1px solid var(--td-component-stroke);
color: var(--td-text-color-placeholder);
font-size: 10px;
line-height: 1;
cursor: pointer;
transition: all 0.2s ease;
&:hover {
border-color: var(--td-brand-color);
color: var(--td-brand-color);
background: var(--td-bg-color-secondarycontainer);
}
}
:deep(.t-tag) {
cursor: pointer;
max-width: 120px;
@@ -3585,9 +3770,13 @@ async function createNewSession(value: string): Promise<void> {
.card-bottom-right {
flex: 1 1 auto;
min-width: 0;
display: flex;
align-items: center;
justify-content: flex-end;
gap: 6px;
overflow: hidden;
}
.faq-manager-wrapper {
@@ -4105,13 +4294,16 @@ async function createNewSession(value: string): Promise<void> {
}
.card-time {
flex-shrink: 0;
color: var(--td-text-color-secondary);
font-family: var(--app-font-family);
font-size: 12px;
font-weight: 400;
white-space: nowrap;
}
.card-type {
flex-shrink: 0;
color: var(--td-text-color-placeholder);
font-family: var(--app-font-family);
font-size: 11px;
@@ -4122,6 +4314,16 @@ async function createNewSession(value: string): Promise<void> {
}
}
.card-bottom-right {
flex: 1 1 auto;
min-width: 0;
display: flex;
align-items: center;
justify-content: flex-end;
gap: 6px;
overflow: hidden;
}
.knowledge-card:hover {
border-color: color-mix(in srgb, var(--td-component-stroke) 55%, var(--td-brand-color));
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.07);
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { formatFileSize, getFileIcon } from '@/utils/files';
@@ -15,7 +15,7 @@ interface KnowledgeItem {
file_type?: string;
file_size?: number | string;
type?: string;
tag_id?: string | number;
tags?: Tag[];
parse_status?: string;
summary_status?: string;
updated_at?: string;
@@ -38,10 +38,63 @@ const emit = defineEmits<{
(e: 'toggle-row', id: string, checked: boolean, shiftKey: boolean): void;
(e: 'toggle-all', checked: boolean): void;
(e: 'action', action: 'edit' | 'reparse' | 'cancel-parse' | 'move' | 'delete', item: KnowledgeItem): void;
(e: 'tag-edit', item: KnowledgeItem): void;
}>();
const { t } = useI18n();
// 动态标签可见数量(根据列宽自适应)
const TAG_EST_WIDTH = 82;
const TAG_OVERFLOW_MIN = 32;
const tagVisibleLimit = reactive<Record<string, number>>({});
const tagItemTotalMap = new Map<string, number>();
let tagChipsRO: ResizeObserver | null = null;
function setupTagChipsObserver(el: Element | null, itemId: string, totalCount: number) {
if (!el) return;
const htmlEl = el as HTMLElement;
htmlEl.dataset.listTagItemId = itemId;
tagItemTotalMap.set(itemId, totalCount);
if (!tagChipsRO) {
tagChipsRO = new ResizeObserver((entries) => {
for (const entry of entries) {
const target = entry.target as HTMLElement;
const id = target.dataset.listTagItemId;
if (!id) continue;
const w = entry.contentRect.width;
const total = tagItemTotalMap.get(id) ?? 0;
if (total <= 0) { tagVisibleLimit[id] = 99; continue; }
const maxFit = Math.floor((w - TAG_OVERFLOW_MIN) / TAG_EST_WIDTH);
const limit = Math.max(1, Math.min(maxFit, total));
tagVisibleLimit[id] = limit >= total ? 99 : limit;
}
});
}
tagChipsRO.observe(htmlEl);
requestAnimationFrame(() => {
const w = htmlEl.clientWidth;
const total = totalCount;
if (total <= 0) { tagVisibleLimit[itemId] = 99; return; }
const maxFit = Math.floor((w - TAG_OVERFLOW_MIN) / TAG_EST_WIDTH);
const limit = Math.max(1, Math.min(maxFit, total));
tagVisibleLimit[itemId] = limit >= total ? 99 : limit;
});
}
function getTagLimit(itemId: string): number {
return tagVisibleLimit[itemId] ?? 99;
}
function hasTagOverflow(itemId: string, total: number): boolean {
const limit = tagVisibleLimit[itemId] ?? 99;
return total > limit;
}
function getOverflowCount(itemId: string, total: number): number {
const limit = tagVisibleLimit[itemId] ?? 99;
return Math.max(0, total - limit);
}
const tagMap = computed(() => {
const map: Record<string, Tag> = {};
for (const tag of props.tagList) map[String(tag.id)] = tag;
@@ -248,10 +301,51 @@ const handleAction = (action: 'edit' | 'reparse' | 'cancel-parse' | 'move' | 'de
<div class="cell cell-tag">
<t-tag v-if="getTagName(item.tag_id)" size="small" variant="light-outline" class="row-tag">
{{ getTagName(item.tag_id) }}
</t-tag>
<span v-else class="row-muted">--</span>
<template v-if="item.tags && item.tags.length > 0">
<t-tooltip
v-if="hasTagOverflow(item.id, item.tags.length)"
:content="item.tags.map((t: any) => t.name).join(', ')"
placement="top"
>
<div
class="row-tag-chips"
:ref="(el: any) => setupTagChipsObserver(el, item.id, item.tags.length)"
:class="{ 'is-clickable': canEdit }"
@click.stop="canEdit && emit('tag-edit', item)"
>
<t-tag
v-for="tag in item.tags.slice(0, getTagLimit(item.id))"
:key="tag.id"
size="small"
variant="light-outline"
class="row-tag"
>
{{ tag.name }}
</t-tag>
<span class="row-tag-overflow">+{{ getOverflowCount(item.id, item.tags.length) }}</span>
</div>
</t-tooltip>
<div
v-else
class="row-tag-chips"
:ref="(el: any) => setupTagChipsObserver(el, item.id, item.tags.length)"
:class="{ 'is-clickable': canEdit }"
@click.stop="canEdit && emit('tag-edit', item)"
>
<t-tag
v-for="tag in item.tags.slice(0, getTagLimit(item.id))"
:key="tag.id"
size="small"
variant="light-outline"
class="row-tag"
>
{{ tag.name }}
</t-tag>
</div>
</template>
<span v-else class="row-tag-chips is-clickable" @click.stop="canEdit && emit('tag-edit', item)">
<span class="row-tag-add">+ {{ t('knowledgeBase.tagLabel') }}</span>
</span>
</div>
<div class="cell cell-source">
@@ -597,6 +691,58 @@ const handleAction = (action: 'edit' | 'reparse' | 'cancel-parse' | 'move' | 'de
}
}
.row-tag-chips {
display: inline-flex;
align-items: center;
gap: 4px;
flex-wrap: nowrap;
&.is-clickable {
cursor: pointer;
}
}
.row-tag-overflow {
display: inline-flex;
align-items: center;
justify-content: center;
height: 20px;
min-width: 20px;
padding: 0 4px;
border-radius: 999px;
border: 1px solid var(--td-component-stroke);
color: var(--td-text-color-placeholder);
font-size: 10px;
line-height: 1;
cursor: pointer;
transition: all 0.2s ease;
&:hover {
border-color: var(--td-brand-color);
color: var(--td-brand-color);
background: var(--td-bg-color-secondarycontainer);
}
}
.row-tag-add {
font-size: 11px;
color: var(--td-text-color-placeholder);
border: 1px dashed var(--td-component-stroke);
border-radius: 999px;
padding: 0 6px;
height: 20px;
display: inline-flex;
align-items: center;
white-space: nowrap;
&:hover {
border-color: var(--td-brand-color);
color: var(--td-brand-color);
background: var(--td-bg-color-secondarycontainer);
border-style: solid;
}
}
.row-muted {
color: var(--td-text-color-disabled, #bbb);
}
@@ -0,0 +1,350 @@
<template>
<t-dialog
:visible="visible"
:header="title"
:footer="false"
width="480px"
:close-on-overlay-click="false"
destroy-on-close
@close="handleClose"
>
<!-- 搜索框 -->
<div class="tag-edit-search">
<t-input
v-model="searchQuery"
:placeholder="$t('knowledgeBase.tagEditSearch')"
clearable
size="medium"
>
<template #prefix-icon>
<t-icon name="search" />
</template>
</t-input>
</div>
<!-- 标签列表 -->
<div class="tag-edit-list">
<template v-if="filteredTags.length > 0">
<div
v-for="tag in filteredTags"
:key="tag.id"
class="tag-edit-item"
:class="{ 'is-selected': selectedSet.has(tag.id) }"
@click="toggleTag(tag.id)"
>
<t-checkbox
:checked="selectedSet.has(tag.id)"
:value="tag.id"
@change="toggleTag(tag.id)"
@click.stop
/>
<span class="tag-edit-name">{{ tag.name }}</span>
<span v-if="tag.knowledge_count !== undefined" class="tag-edit-count">{{ tag.knowledge_count }}</span>
</div>
</template>
<template v-else>
<div class="tag-edit-empty">
<template v-if="searchQuery.trim()">
<p class="tag-edit-empty-text">{{ $t('knowledgeBase.tagEmptyResult') }}</p>
<t-button
variant="text"
theme="primary"
size="small"
:loading="creatingTag"
@click="handleCreateTag"
>
<t-icon name="add" />
{{ $t('knowledgeBase.tagCreateAction') }} "{{ searchQuery.trim() }}"
</t-button>
</template>
<template v-else>
<p class="tag-edit-empty-text">{{ $t('knowledgeBase.noTags') }}</p>
</template>
</div>
</template>
</div>
<!-- 新增标签栏 -->
<div class="tag-edit-new-bar">
<t-input
v-model="newTagName"
:placeholder="$t('knowledgeBase.tagNewPlaceholder')"
size="small"
clearable
:disabled="creatingTag"
@keydown.enter.prevent="handleAddNewTag"
>
<template #suffix>
<t-button
theme="primary"
variant="text"
size="small"
:loading="creatingTag"
:disabled="!newTagName.trim()"
@click="handleAddNewTag"
>
<t-icon name="add" />
</t-button>
</template>
</t-input>
</div>
<!-- 底部操作栏 -->
<div class="tag-edit-footer">
<div class="tag-edit-footer-left">
<span class="tag-edit-selected-count">
{{ $t('knowledgeBase.tagSelectedCount', { count: selectedSet.size }) }}
</span>
<t-button
v-if="selectedSet.size > 0"
variant="text"
size="small"
theme="default"
@click="clearAll"
>
{{ $t('knowledgeBase.tagClearAction') }}
</t-button>
</div>
<div class="tag-edit-footer-right">
<t-button variant="outline" size="small" @click="handleClose">
{{ $t('common.cancel') }}
</t-button>
<t-button theme="primary" size="small" :loading="saving" @click="handleConfirm">
{{ $t('common.confirm') }}
</t-button>
</div>
</div>
</t-dialog>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { MessagePlugin } from 'tdesign-vue-next';
import { createKnowledgeBaseTag } from '@/api/knowledge-base';
interface Tag {
id: string;
name: string;
color?: string;
knowledge_count?: number;
}
const props = defineProps<{
visible: boolean;
knowledgeName: string;
kbId: string;
tagList: Tag[];
selectedTags: Tag[];
}>();
const emit = defineEmits<{
(e: 'update:visible', value: boolean): void;
(e: 'confirm', tagIds: string[]): void;
(e: 'tag-created'): void;
}>();
const { t } = useI18n();
const searchQuery = ref('');
const selectedSet = ref<Set<string>>(new Set());
const creatingTag = ref(false);
const saving = ref(false);
const newTagName = ref('');
const title = computed(() => {
return t('knowledgeBase.tagEditDialogTitle', { name: props.knowledgeName });
});
// 初始化已选标签
watch(
() => props.visible,
(val) => {
if (val) {
selectedSet.value = new Set(props.selectedTags.map((t) => t.id));
searchQuery.value = '';
newTagName.value = '';
}
},
);
// 过滤标签列表,按照关键字匹配,已选的排前面
const filteredTags = computed(() => {
const query = searchQuery.value.trim().toLowerCase();
let list = props.tagList;
if (query) {
list = list.filter((tag) => (tag.name || '').toLowerCase().includes(query));
}
// 已选的排前面
const selected: Tag[] = [];
const unselected: Tag[] = [];
for (const tag of list) {
if (selectedSet.value.has(tag.id)) {
selected.push(tag);
} else {
unselected.push(tag);
}
}
return [...selected, ...unselected];
});
function toggleTag(tagId: string) {
const next = new Set(selectedSet.value);
if (next.has(tagId)) {
next.delete(tagId);
} else {
next.add(tagId);
}
selectedSet.value = next;
}
function clearAll() {
selectedSet.value = new Set();
}
async function handleCreateTag() {
const name = searchQuery.value.trim();
if (!name) return;
creatingTag.value = true;
try {
const res: any = await createKnowledgeBaseTag(props.kbId, { name });
const newTag = res?.data || res;
const next = new Set(selectedSet.value);
next.add(newTag.id);
selectedSet.value = next;
searchQuery.value = '';
emit('tag-created');
MessagePlugin.success(t('knowledgeBase.tagCreateSuccess'));
} catch (error: any) {
MessagePlugin.error(error?.message || t('common.operationFailed'));
} finally {
creatingTag.value = false;
}
}
async function handleAddNewTag() {
const name = newTagName.value.trim();
if (!name) return;
// 检查是否已存在同名标签
const exists = props.tagList.find((t) => t.name === name);
if (exists) {
// 已存在则直接选中
const next = new Set(selectedSet.value);
next.add(exists.id);
selectedSet.value = next;
newTagName.value = '';
return;
}
creatingTag.value = true;
try {
const res: any = await createKnowledgeBaseTag(props.kbId, { name });
const newTag = res?.data || res;
const next = new Set(selectedSet.value);
next.add(newTag.id);
selectedSet.value = next;
newTagName.value = '';
emit('tag-created');
MessagePlugin.success(t('knowledgeBase.tagCreateSuccess'));
} catch (error: any) {
MessagePlugin.error(error?.message || t('common.operationFailed'));
} finally {
creatingTag.value = false;
}
}
async function handleConfirm() {
saving.value = true;
try {
emit('confirm', Array.from(selectedSet.value));
emit('update:visible', false);
} finally {
saving.value = false;
}
}
function handleClose() {
emit('update:visible', false);
}
</script>
<style scoped>
.tag-edit-search {
margin-bottom: 12px;
}
.tag-edit-list {
max-height: 320px;
overflow-y: auto;
border: 1px solid var(--td-component-stroke);
border-radius: 6px;
padding: 4px 0;
margin-bottom: 12px;
}
.tag-edit-item {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
cursor: pointer;
transition: background 0.15s;
}
.tag-edit-item:hover {
background: var(--td-bg-color-container-hover);
}
.tag-edit-item.is-selected {
background: var(--td-brand-color-light);
}
.tag-edit-name {
flex: 1;
font-size: 14px;
color: var(--td-text-color-primary);
}
.tag-edit-count {
font-size: 12px;
color: var(--td-text-color-placeholder);
}
.tag-edit-empty {
padding: 24px 12px;
text-align: center;
}
.tag-edit-empty-text {
margin: 0 0 8px;
font-size: 13px;
color: var(--td-text-color-placeholder);
}
.tag-edit-new-bar {
margin-bottom: 12px;
}
.tag-edit-footer {
display: flex;
align-items: center;
justify-content: space-between;
}
.tag-edit-footer-left {
display: flex;
align-items: center;
gap: 8px;
}
.tag-edit-selected-count {
font-size: 13px;
color: var(--td-text-color-secondary);
}
.tag-edit-footer-right {
display: flex;
align-items: center;
gap: 8px;
margin-left: auto;
}
</style>