perf(kb):1.优化 PDF只对可疑页做 image box 分析避免整本重复扫描/重复 reopen;2.doc/.docx 不再因为“有图片”就强制 OCR;3.只在 intake 路由 明确要求 OCR 时才跑

This commit is contained in:
runny committed 2026-06-06 16:53:05 +08:00
1 parent a3123cd602
commit c58ed37cc1
11 files changed
+717 -122

No files matched your search

+182 -25
View File
@@ -86,6 +86,8 @@ const failureModalType = ref<'upload' | 'clean' | 'sync' | null>(null);
const failureModalPage = ref(1);
const retryingFailureKey = ref('');
const documentStageFilter = ref<'all' | 'upload' | 'clean' | 'sync'>('all');
const documentListPage = ref(1);
const documentListPageSize = ref(50);
const batchSubmitting = ref(false);
const selectedDocumentIds = ref<number[]>([]);
const pathUploadDraftText = ref('');
@@ -176,10 +178,15 @@ type PathUploadTaskItem = {
finishedAt?: string;
};
type LoadListOptions = {
silent?: boolean;
};
const FAILURE_PAGE_SIZE = 15;
const PATH_UPLOAD_BATCH_SIZE = 30;
const PATH_UPLOAD_BATCH_INTERVAL_MS = 500;
const PATH_UPLOAD_CONCURRENCY = 3;
const DOCUMENT_PAGE_SIZE_OPTIONS = [20, 50, 100, 200] as const;
const pathUploadTask = ref<{
running: boolean;
groupId: number | null;
@@ -397,6 +404,24 @@ const filteredDocuments = computed(() =>
documents.value.filter((doc) => matchesDocumentStageFilter(doc, documentStageFilter.value))
);
const filteredDocumentTotalPages = computed(() =>
Math.max(1, Math.ceil(filteredDocuments.value.length / documentListPageSize.value))
);
const pagedFilteredDocuments = computed(() => {
const start = (documentListPage.value - 1) * documentListPageSize.value;
return filteredDocuments.value.slice(start, start + documentListPageSize.value);
});
const pagedDocumentRange = computed(() => {
if (filteredDocuments.value.length === 0) {
return { start: 0, end: 0 };
}
const start = (documentListPage.value - 1) * documentListPageSize.value + 1;
const end = Math.min(filteredDocuments.value.length, documentListPage.value * documentListPageSize.value);
return { start, end };
});
const selectedDocuments = computed(() =>
documents.value.filter((doc) => selectedDocumentIds.value.includes(doc.id))
);
@@ -657,8 +682,32 @@ const selectedPathRoots = computed(() => {
return Array.from(roots);
});
const loadGroups = async () => {
isLoading.value = true;
const mergeStableItems = <T extends { id: number | string }>(current: T[], incoming: T[]) => {
const incomingMap = new Map(incoming.map((item) => [String(item.id), item] as const));
const next: T[] = [];
const seen = new Set<string>();
current.forEach((item) => {
const key = String(item.id);
const incomingItem = incomingMap.get(key);
if (!incomingItem) return;
next.push(incomingItem);
seen.add(key);
});
incoming.forEach((item) => {
const key = String(item.id);
if (seen.has(key)) return;
next.push(item);
});
return next;
};
const loadGroups = async (options: LoadListOptions = {}) => {
if (!options.silent) {
isLoading.value = true;
}
try {
const result = await getKbGroupsSafe();
if (!result.ok) {
@@ -666,17 +715,20 @@ const loadGroups = async () => {
feedbackType.value = 'error';
return;
}
groups.value = result.data;
groups.value = options.silent ? mergeStableItems(groups.value, result.data) : result.data;
const firstGroup = groups.value[0];
if (!selectedGroupId.value && firstGroup && viewMode.value === 'group_detail') {
selectedGroupId.value = firstGroup.id;
}
} finally {
isLoading.value = false;
if (!options.silent) {
isLoading.value = false;
}
}
};
const loadJobs = async () => {
const hadActiveKbJobs = kbJobPollingActive.value;
const result = await getKbJobsSafe();
if (!result.ok) {
feedback.value = result.error || '加载任务列表失败';
@@ -695,9 +747,23 @@ const loadJobs = async () => {
startPathUploadPolling();
} else {
stopPathUploadPollTimer();
if (hadActiveKbJobs && viewMode.value === 'group_detail') {
await Promise.all([
loadDocuments(selectedGroupId.value, { silent: true }),
loadGroups({ silent: true }),
]);
}
}
};
const refreshWorkflowStatusOnly = async () => {
await Promise.all([
loadJobs(),
loadGroups({ silent: true }),
loadDocuments(selectedGroupId.value, { silent: true }),
]);
};
const refreshCurrentView = async () => {
if (viewMode.value === 'group_overview') {
await Promise.all([loadGroups(), loadJobs()]);
@@ -809,6 +875,33 @@ const getWorkflowBadges = (doc: KbDocumentItem) => {
];
};
const getMarkdownUnavailableMessage = (
doc: KbDocumentItem,
fallback?: string | null,
) => {
const latestStatus = String(doc.latest_version?.status || '').toLowerCase();
const normalizedReason = normalizeWorkflowErrorMessage(
doc.latest_version?.error_message,
'clean',
);
if (latestStatus === 'failed') {
return normalizedReason
? `当前文档清洗失败,尚未生成 Markdown。失败原因:${normalizedReason}`
: '当前文档清洗失败,尚未生成 Markdown,请重新清洗后重试';
}
if (latestStatus === 'processing') {
return '当前文档正在清洗中,暂未生成 Markdown,请稍后再试';
}
if (latestStatus === 'uploaded') {
return '当前文档待清洗,暂未生成 Markdown,请先发起清洗';
}
return fallback || '当前文档尚未生成 Markdown';
};
const resetGroupForm = () => {
editingGroupId.value = null;
groupForm.value = {
@@ -905,8 +998,10 @@ const handleDeleteGroup = async (group: KbGroup) => {
});
};
const loadDocuments = async (groupId: number | null) => {
isDocumentsLoading.value = true;
const loadDocuments = async (groupId: number | null, options: LoadListOptions = {}) => {
if (!options.silent) {
isDocumentsLoading.value = true;
}
try {
if (!groupId) {
documents.value = [];
@@ -921,7 +1016,10 @@ const loadDocuments = async (groupId: number | null) => {
feedbackType.value = 'error';
return;
}
documents.value = result.data;
const nextDocuments = options.silent
? mergeStableItems(documents.value, result.data)
: result.data;
documents.value = nextDocuments;
if (selectedDocumentId.value && !documents.value.some((item) => item.id === selectedDocumentId.value)) {
selectedDocumentId.value = null;
selectedDocumentVersions.value = [];
@@ -935,7 +1033,9 @@ const loadDocuments = async (groupId: number | null) => {
}
selectedDocumentIds.value = selectedDocumentIds.value.filter((id) => documents.value.some((item) => item.id === id));
} finally {
isDocumentsLoading.value = false;
if (!options.silent) {
isDocumentsLoading.value = false;
}
}
};
@@ -1200,7 +1300,11 @@ const startPathUploadPolling = () => {
stopPathUploadPollTimer();
return;
}
void Promise.all([loadJobs(), loadDocuments(selectedGroupId.value), loadGroups()]);
void Promise.all([
loadJobs(),
loadDocuments(selectedGroupId.value, { silent: true }),
loadGroups({ silent: true }),
]);
}, 3000);
};
@@ -1307,7 +1411,7 @@ const handleConfirmPathUpload = async () => {
localItem.error = undefined;
updatePathUploadFeedback();
}
await Promise.all([loadDocuments(selectedGroupId.value), loadJobs()]);
await Promise.all([loadDocuments(selectedGroupId.value, { silent: true }), loadJobs()]);
};
const files = [...pendingFiles];
@@ -1328,7 +1432,7 @@ const handleConfirmPathUpload = async () => {
feedbackType.value = 'success';
startPathUploadPolling();
resetPathUploadDraft();
await Promise.all([loadJobs(), loadDocuments(selectedGroupId.value), loadGroups()]);
await refreshWorkflowStatusOnly();
};
const handleDeletePathUploadItem = async (job: KbJob | null, item: PathUploadTaskItem) => {
@@ -1345,7 +1449,7 @@ const handleDeletePathUploadItem = async (job: KbJob | null, item: PathUploadTas
if (pathUploadTask.value.items.length <= 1) {
showPathUploadDetails.value = false;
}
await Promise.all([loadJobs(), loadDocuments(selectedGroupId.value), loadGroups()]);
await refreshWorkflowStatusOnly();
});
};
@@ -1402,7 +1506,7 @@ const handleRetryFailedPathUpload = async (
startPathUploadPolling();
feedback.value = relativePaths?.length === 1 ? '已重新提交该失败文件' : '已重新提交失败文件';
feedbackType.value = 'success';
await Promise.all([loadJobs(), loadDocuments(selectedGroupId.value), loadGroups()]);
await refreshWorkflowStatusOnly();
};
const toggleDocumentSelection = (documentId: number) => {
@@ -1471,7 +1575,7 @@ const handleBatchSync = async () => {
if (successCount > 0) {
startPathUploadPolling();
}
await Promise.all([loadGroups(), loadDocuments(selectedGroupId.value), loadJobs()]);
await refreshWorkflowStatusOnly();
} finally {
batchSubmitting.value = false;
}
@@ -1507,7 +1611,7 @@ const handleBatchClean = async () => {
if (successCount > 0) {
startPathUploadPolling();
}
await Promise.all([loadGroups(), loadDocuments(selectedGroupId.value), loadJobs()]);
await refreshWorkflowStatusOnly();
} finally {
batchSubmitting.value = false;
}
@@ -1584,7 +1688,7 @@ const handleRetryFailedItem = async (
feedbackType.value = 'success';
startPathUploadPolling();
}
await Promise.all([loadGroups(), loadDocuments(selectedGroupId.value), loadJobs()]);
await refreshWorkflowStatusOnly();
if (selectedDocumentId.value) {
await loadDocumentVersions(selectedDocumentId.value);
}
@@ -1679,9 +1783,7 @@ const handleFileSelected = async (event: Event) => {
input.value = '';
isSubmitting.value = false;
await loadGroups();
await loadDocuments(selectedGroupId.value);
await loadJobs();
await refreshWorkflowStatusOnly();
if (selectedDocumentId.value) {
await loadDocumentVersions(selectedDocumentId.value);
}
@@ -1700,7 +1802,7 @@ const handleSyncDocument = async (documentId: number) => {
if (!result.ok) {
feedback.value = result.error ? `同步到 Dify 失败:${result.error}` : '同步到 Dify 失败';
feedbackType.value = 'error';
await loadDocuments(selectedGroupId.value);
await loadDocuments(selectedGroupId.value, { silent: true });
await loadJobs();
return;
}
@@ -1708,7 +1810,7 @@ const handleSyncDocument = async (documentId: number) => {
feedback.value = result.job?.message || '同步任务已提交,等待后台处理';
feedbackType.value = 'success';
startPathUploadPolling();
await loadDocuments(selectedGroupId.value);
await loadDocuments(selectedGroupId.value, { silent: true });
await loadJobs();
selectedDocumentId.value = documentId;
await loadDocumentVersions(documentId);
@@ -1734,7 +1836,7 @@ const openMarkdownEditor = async (doc: KbDocumentItem) => {
const result = await getKbDocumentMarkdown(doc.id);
isMarkdownLoading.value = false;
if (!result.ok || !result.data) {
markdownError.value = result.error || '读取清洗后的 Markdown 失败';
markdownError.value = getMarkdownUnavailableMessage(doc, result.error || '读取清洗后的 Markdown 失败');
return;
}
applyMarkdownPayload(result.data);
@@ -1761,7 +1863,10 @@ const handleSaveMarkdown = async () => {
isMarkdownSaving.value = false;
if (!result.ok || !result.data) {
markdownError.value = result.error || '保存 Markdown 失败';
const currentDoc = documents.value.find((item) => item.id === markdownDocumentId.value) || null;
markdownError.value = currentDoc
? getMarkdownUnavailableMessage(currentDoc, result.error || '保存 Markdown 失败')
: (result.error || '保存 Markdown 失败');
return;
}
@@ -1778,6 +1883,7 @@ const handleSaveMarkdown = async () => {
watch(selectedGroupId, (groupId) => {
feedback.value = '';
feedbackType.value = '';
documentListPage.value = 1;
selectedDocumentId.value = null;
selectedDocumentIds.value = [];
selectedDocumentVersions.value = [];
@@ -1786,6 +1892,20 @@ watch(selectedGroupId, (groupId) => {
applyPathUploadJob(latestPathUploadJob.value);
});
watch(documentStageFilter, () => {
documentListPage.value = 1;
});
watch(documentListPageSize, () => {
documentListPage.value = 1;
});
watch(filteredDocumentTotalPages, (totalPages) => {
if (documentListPage.value > totalPages) {
documentListPage.value = totalPages;
}
});
watch(selectedPathUploadHistoryJob, (job) => {
const validKeys = new Set(getPathUploadJobItems(job).map((item) => item.key));
selectedPathUploadHistoryItemKeys.value = selectedPathUploadHistoryItemKeys.value.filter((key) => validKeys.has(key));
@@ -2198,7 +2318,20 @@ onUnmounted(() => {
<span class="rounded-full bg-surface-soft px-2 py-0.5 text-xs ">{{ tab.count }}</span>
</button>
</div>
<div class="text-sm text-muted">已选 {{ selectedDocuments.length }} 条 · 共 {{ filteredDocuments.length }} 条</div>
<div class="flex flex-wrap items-center gap-3 text-sm text-muted">
<label class="inline-flex items-center gap-2">
<span>每页</span>
<select
v-model.number="documentListPageSize"
class="rounded-xl border border-default bg-surface px-3 py-1.5 text-sm text-secondary outline-none transition-colors focus:border-brand-300"
>
<option v-for="size in DOCUMENT_PAGE_SIZE_OPTIONS" :key="size" :value="size">{{ size }}</option>
</select>
<span>条</span>
</label>
<span>已选 {{ selectedDocuments.length }} 条 · 共 {{ filteredDocuments.length }} 条</span>
<span v-if="filteredDocuments.length > 0">当前显示 {{ pagedDocumentRange.start }}-{{ pagedDocumentRange.end }} 条</span>
</div>
</div>
<div v-if="!selectedGroupId" class="rounded-2xl border border-dashed border-default px-4 py-12 text-center text-sm text-muted ">
@@ -2212,7 +2345,7 @@ onUnmounted(() => {
</div>
<div v-else class="space-y-3">
<div
v-for="doc in filteredDocuments"
v-for="doc in pagedFilteredDocuments"
:key="doc.id"
class="rounded-2xl border px-4 py-4 transition-colors"
:class="selectedDocumentId === doc.id
@@ -2314,6 +2447,30 @@ onUnmounted(() => {
</div>
</div>
</div>
<div
v-if="filteredDocuments.length > documentListPageSize"
class="flex flex-wrap items-center justify-between gap-3 rounded-2xl border border-default px-4 py-3 "
>
<div class="text-sm text-muted">
第 {{ documentListPage }} / {{ filteredDocumentTotalPages }} 页 · 共 {{ filteredDocuments.length }} 条
</div>
<div class="flex items-center gap-2">
<button
@click="documentListPage = Math.max(1, documentListPage - 1)"
:disabled="documentListPage === 1"
class="inline-flex items-center justify-center rounded-xl border border-default px-4 py-2 text-sm font-medium text-secondary transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-60 hover-surface-soft"
>
上一页
</button>
<button
@click="documentListPage = Math.min(filteredDocumentTotalPages, documentListPage + 1)"
:disabled="documentListPage === filteredDocumentTotalPages"
class="inline-flex items-center justify-center rounded-xl border border-default px-4 py-2 text-sm font-medium text-secondary transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-60 hover-surface-soft"
>
下一页
</button>
</div>
</div>
</div>
</div>
</section>