feat: 完善知识库批量导入与公共 API
- 新增批量异步导入、状态查询、失败重试与中断恢复链路 - 拆分知识库读取、导入、维护权限并完善 Public API 契约 - 补充数据库迁移、管理端交互、接口说明与相关测试
This commit is contained in:
@@ -13,6 +13,7 @@ import { api } from '#/api/request';
|
||||
import bookIcon from '#/assets/ai/knowledge/book.svg';
|
||||
import HeaderSearch from '#/components/headerSearch/HeaderSearch.vue';
|
||||
import { createLazyComponentController } from '#/utils/lazy-component';
|
||||
import DocumentImportBatchStatus from '#/views/ai/documentCollection/DocumentImportBatchStatus.vue';
|
||||
|
||||
const ChunkDocumentTable = defineAsyncComponent(
|
||||
() => import('#/views/ai/documentCollection/ChunkDocumentTable.vue'),
|
||||
@@ -188,6 +189,7 @@ const headerButtons = [
|
||||
];
|
||||
const panelMode = ref<'chunk' | 'list' | 'process'>('list');
|
||||
const documentTableRef = ref();
|
||||
const batchStatusRefreshKey = ref(0);
|
||||
const documentTitle = ref('');
|
||||
const handleSearch = (searchParams: string) => {
|
||||
documentTableRef.value?.search?.(searchParams);
|
||||
@@ -227,6 +229,7 @@ const backDoc = async () => {
|
||||
documentTitle.value = '';
|
||||
await nextTick();
|
||||
documentTableRef.value?.reload?.();
|
||||
batchStatusRefreshKey.value += 1;
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -273,7 +276,16 @@ const backDoc = async () => {
|
||||
:buttons="canManageCurrentKnowledge ? headerButtons : []"
|
||||
@search="handleSearch"
|
||||
@button-click="handleButtonClick"
|
||||
/>
|
||||
>
|
||||
<template #middle>
|
||||
<DocumentImportBatchStatus
|
||||
:knowledge-id="knowledgeId"
|
||||
:manageable="canManageCurrentKnowledge"
|
||||
:refresh-key="batchStatusRefreshKey"
|
||||
@continued="backDoc"
|
||||
/>
|
||||
</template>
|
||||
</HeaderSearch>
|
||||
</div>
|
||||
<div v-if="panelMode === 'chunk'" class="doc-sub-back">
|
||||
<ElButton @click="backDoc">
|
||||
@@ -343,6 +355,7 @@ const backDoc = async () => {
|
||||
:is="ImportKnowledgeDocFileComponent"
|
||||
v-if="ImportKnowledgeDocFileComponent"
|
||||
ref="importDocModalRef"
|
||||
enable-bulk-auto
|
||||
:knowledge-id-prop="String(knowledgeId)"
|
||||
@imported="backDoc"
|
||||
/>
|
||||
@@ -386,6 +399,10 @@ const backDoc = async () => {
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.doc-header :deep(.search-middle) {
|
||||
flex: 1 1 360px;
|
||||
}
|
||||
|
||||
.doc-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
@@ -284,7 +284,8 @@ const actions: ActionButton[] = [
|
||||
text: $t('button.offline'),
|
||||
permission: '/api/v1/documentCollection/save',
|
||||
placement: 'menu',
|
||||
visible: (row) => canAiResourceOffline(row.displayPublishStatus, row.publishStatus),
|
||||
visible: (row) =>
|
||||
canAiResourceOffline(row.displayPublishStatus, row.publishStatus),
|
||||
onClick(row) {
|
||||
if (!ensureManageKnowledgeItem(row)) {
|
||||
return;
|
||||
@@ -298,7 +299,8 @@ const actions: ActionButton[] = [
|
||||
tone: 'danger',
|
||||
permission: '/api/v1/documentCollection/remove',
|
||||
placement: 'menu',
|
||||
visible: (row) => canAiResourceDelete(row.displayPublishStatus, row.publishStatus),
|
||||
visible: (row) =>
|
||||
canAiResourceDelete(row.displayPublishStatus, row.publishStatus),
|
||||
onClick(row) {
|
||||
if (!ensureManageKnowledgeItem(row)) {
|
||||
return;
|
||||
@@ -325,8 +327,8 @@ const submitPublishAction = async (item: any) => {
|
||||
const confirmation = await confirmPublishSubmission({
|
||||
api,
|
||||
confirmMessage: isRepublishAction(item)
|
||||
? $t('documentCollection.submitRepublishApprovalConfirm')
|
||||
: $t('documentCollection.submitPublishApprovalConfirm'),
|
||||
? $t('documentCollection.submitRepublishApprovalConfirm')
|
||||
: $t('documentCollection.submitPublishApprovalConfirm'),
|
||||
id: item.id,
|
||||
resourcePath: '/api/v1/documentCollection',
|
||||
title: $t('message.noticeTitle'),
|
||||
@@ -356,12 +358,9 @@ const submitOfflineAction = async (item: any) => {
|
||||
const impactRes = await api.get<{
|
||||
data: OfflineImpactCheck;
|
||||
errorCode: number;
|
||||
}>(
|
||||
'/api/v1/documentCollection/offlineImpactCheck',
|
||||
{
|
||||
params: { id: item.id },
|
||||
},
|
||||
);
|
||||
}>('/api/v1/documentCollection/offlineImpactCheck', {
|
||||
params: { id: item.id },
|
||||
});
|
||||
if (impactRes.errorCode !== 0) {
|
||||
return;
|
||||
}
|
||||
@@ -399,9 +398,12 @@ const submitOfflineAction = async (item: any) => {
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const res = await api.post('/api/v1/documentCollection/submitOfflineApproval', {
|
||||
id: item.id,
|
||||
});
|
||||
const res = await api.post(
|
||||
'/api/v1/documentCollection/submitOfflineApproval',
|
||||
{
|
||||
id: item.id,
|
||||
},
|
||||
);
|
||||
if (res.errorCode === 0) {
|
||||
ElMessage.success(res.message || $t('message.saveOkMessage'));
|
||||
reloadKnowledgeList();
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import { flushPromises, mount } from '@vue/test-utils';
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import DocumentImportBatchStatus from './DocumentImportBatchStatus.vue';
|
||||
|
||||
const apiMocks = vi.hoisted(() => ({
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('#/api/request', () => ({ api: apiMocks }));
|
||||
|
||||
vi.mock('@easyflow/locales', () => ({
|
||||
$t: (key: string) => key,
|
||||
}));
|
||||
|
||||
function createBatch(status: 'COMPLETED' | 'RUNNING', completedCount: number) {
|
||||
return {
|
||||
batchId: 'batch-1',
|
||||
completedCount,
|
||||
failedCount: 0,
|
||||
importMode: 'AUTO',
|
||||
pendingCount: status === 'RUNNING' ? 1 : 0,
|
||||
processingCount: 0,
|
||||
progressPercent: status === 'COMPLETED' ? 100 : 50,
|
||||
skippedCount: 0,
|
||||
status,
|
||||
totalCount: 2,
|
||||
};
|
||||
}
|
||||
|
||||
describe('documentImportBatchStatus', () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('页面首次进入时不恢复已完成批次', async () => {
|
||||
apiMocks.get.mockResolvedValue({
|
||||
data: createBatch('COMPLETED', 2),
|
||||
errorCode: 0,
|
||||
});
|
||||
|
||||
const wrapper = mount(DocumentImportBatchStatus, {
|
||||
props: { knowledgeId: 'knowledge-1' },
|
||||
});
|
||||
await flushPromises();
|
||||
|
||||
expect(wrapper.find('.batch-status').exists()).toBe(false);
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it('当前页面跟踪的批次完成后继续展示结果', async () => {
|
||||
vi.useFakeTimers();
|
||||
apiMocks.get
|
||||
.mockResolvedValueOnce({
|
||||
data: createBatch('RUNNING', 1),
|
||||
errorCode: 0,
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
data: createBatch('COMPLETED', 2),
|
||||
errorCode: 0,
|
||||
});
|
||||
|
||||
const wrapper = mount(DocumentImportBatchStatus, {
|
||||
props: { knowledgeId: 'knowledge-1' },
|
||||
});
|
||||
await flushPromises();
|
||||
|
||||
expect(wrapper.find('.batch-status').exists()).toBe(true);
|
||||
|
||||
await vi.advanceTimersByTimeAsync(3000);
|
||||
await flushPromises();
|
||||
|
||||
expect(wrapper.find('.batch-status').exists()).toBe(true);
|
||||
expect(wrapper.text()).toContain(
|
||||
'documentCollection.importDoc.batchCompleted',
|
||||
);
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,312 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
|
||||
|
||||
import { $t } from '@easyflow/locales';
|
||||
|
||||
import { ElButton, ElProgress } from 'element-plus';
|
||||
|
||||
import { api } from '#/api/request';
|
||||
|
||||
interface BatchStatus {
|
||||
batchId: string;
|
||||
completedCount: number;
|
||||
failedCount: number;
|
||||
importMode: 'AUTO' | 'MANUAL';
|
||||
pendingCount: number;
|
||||
processingCount: number;
|
||||
progressPercent: number;
|
||||
retryableFailedCount?: number;
|
||||
skippedCount: number;
|
||||
status:
|
||||
| 'CANCELLED'
|
||||
| 'COMPLETED'
|
||||
| 'INTERRUPTED'
|
||||
| 'PARTIAL_SUCCEEDED'
|
||||
| 'RUNNING';
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
const props = defineProps({
|
||||
knowledgeId: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
refreshKey: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
manageable: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
});
|
||||
|
||||
const emit = defineEmits(['continued']);
|
||||
const batch = ref<BatchStatus>();
|
||||
const continuing = ref(false);
|
||||
let pollTimer: null | ReturnType<typeof setTimeout> = null;
|
||||
let disposed = false;
|
||||
let refreshGeneration = 0;
|
||||
|
||||
const canContinue = computed(
|
||||
() =>
|
||||
props.manageable &&
|
||||
(batch.value?.status === 'INTERRUPTED' ||
|
||||
batch.value?.status === 'PARTIAL_SUCCEEDED') &&
|
||||
(Number(batch.value?.retryableFailedCount || 0) > 0 ||
|
||||
Number(batch.value?.failedCount || 0) > 0),
|
||||
);
|
||||
|
||||
const processedCount = computed(
|
||||
() =>
|
||||
Number(batch.value?.completedCount || 0) +
|
||||
Number(batch.value?.processingCount || 0),
|
||||
);
|
||||
|
||||
const allFailed = computed(
|
||||
() =>
|
||||
Number(batch.value?.totalCount || 0) > 0 &&
|
||||
Number(batch.value?.failedCount || 0) ===
|
||||
Number(batch.value?.totalCount || 0),
|
||||
);
|
||||
|
||||
const statusLabel = computed(() => {
|
||||
const status = batch.value?.status;
|
||||
if (status === 'COMPLETED') {
|
||||
return $t('documentCollection.importDoc.batchCompleted');
|
||||
}
|
||||
if (status === 'INTERRUPTED') {
|
||||
return $t('documentCollection.importDoc.batchInterrupted');
|
||||
}
|
||||
if (status === 'PARTIAL_SUCCEEDED') {
|
||||
return $t(
|
||||
allFailed.value
|
||||
? 'documentCollection.importDoc.batchFailed'
|
||||
: 'documentCollection.importDoc.batchPartial',
|
||||
);
|
||||
}
|
||||
return $t('documentCollection.importDoc.batchRunning');
|
||||
});
|
||||
|
||||
async function refresh(hideCompletedOnRestore = false) {
|
||||
if (!props.knowledgeId) return;
|
||||
const currentGeneration = ++refreshGeneration;
|
||||
try {
|
||||
const response = await api.get('/api/v1/document/import/batch/current', {
|
||||
params: { knowledgeId: props.knowledgeId },
|
||||
});
|
||||
if (disposed || currentGeneration !== refreshGeneration) {
|
||||
return;
|
||||
}
|
||||
const restoredBatch =
|
||||
response.errorCode === 0 ? response.data || undefined : undefined;
|
||||
batch.value =
|
||||
hideCompletedOnRestore && restoredBatch?.status === 'COMPLETED'
|
||||
? undefined
|
||||
: restoredBatch;
|
||||
} finally {
|
||||
if (!disposed && currentGeneration === refreshGeneration) {
|
||||
schedulePoll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function schedulePoll() {
|
||||
if (pollTimer) clearTimeout(pollTimer);
|
||||
pollTimer = null;
|
||||
if (
|
||||
disposed ||
|
||||
!batch.value ||
|
||||
batch.value.status === 'COMPLETED' ||
|
||||
batch.value.status === 'PARTIAL_SUCCEEDED' ||
|
||||
batch.value.status === 'INTERRUPTED' ||
|
||||
batch.value.status === 'CANCELLED'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
pollTimer = setTimeout(refresh, 3000);
|
||||
}
|
||||
|
||||
async function continueBatch() {
|
||||
if (!batch.value || continuing.value) return;
|
||||
continuing.value = true;
|
||||
try {
|
||||
const response = await api.post('/api/v1/document/import/batch/continue', {
|
||||
batchId: batch.value.batchId,
|
||||
knowledgeId: props.knowledgeId,
|
||||
});
|
||||
if (response.errorCode === 0) {
|
||||
batch.value = response.data;
|
||||
emit('continued');
|
||||
schedulePoll();
|
||||
}
|
||||
} finally {
|
||||
continuing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
disposed = false;
|
||||
refresh(true);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
disposed = true;
|
||||
refreshGeneration += 1;
|
||||
if (pollTimer) clearTimeout(pollTimer);
|
||||
});
|
||||
|
||||
watch(
|
||||
() => [props.knowledgeId, props.refreshKey] as const,
|
||||
([knowledgeId], [previousKnowledgeId]) => {
|
||||
const knowledgeChanged = knowledgeId !== previousKnowledgeId;
|
||||
if (knowledgeChanged) {
|
||||
batch.value = undefined;
|
||||
}
|
||||
refresh(knowledgeChanged);
|
||||
},
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section
|
||||
v-if="batch"
|
||||
class="batch-status"
|
||||
:aria-label="$t('documentCollection.importDoc.batchStatus')"
|
||||
>
|
||||
<div class="batch-status__summary">
|
||||
<div class="batch-status__headline">
|
||||
<span class="batch-status__title">
|
||||
{{ $t('documentCollection.importDoc.autoImport') }}
|
||||
</span>
|
||||
<span class="batch-status__count">
|
||||
{{ processedCount }} / {{ batch.totalCount }}
|
||||
</span>
|
||||
<span class="batch-status__state">{{ statusLabel }}</span>
|
||||
</div>
|
||||
<ElProgress
|
||||
class="batch-status__progress"
|
||||
:percentage="Number(batch.progressPercent || 0)"
|
||||
:show-text="false"
|
||||
:stroke-width="7"
|
||||
:status="
|
||||
batch.status === 'COMPLETED'
|
||||
? 'success'
|
||||
: allFailed
|
||||
? 'exception'
|
||||
: undefined
|
||||
"
|
||||
/>
|
||||
<div class="batch-status__metrics">
|
||||
<span>
|
||||
{{ $t('documentCollection.importDoc.completedCount') }}
|
||||
{{ batch.completedCount }}
|
||||
</span>
|
||||
<span>
|
||||
{{ $t('documentCollection.importDoc.processingCount') }}
|
||||
{{ batch.processingCount }}
|
||||
</span>
|
||||
<span
|
||||
:class="{ 'batch-status__metric--danger': batch.failedCount > 0 }"
|
||||
>
|
||||
{{ $t('documentCollection.importDoc.failedCount') }}
|
||||
{{ batch.failedCount }}
|
||||
</span>
|
||||
<span>
|
||||
{{ $t('documentCollection.importDoc.pendingCount') }}
|
||||
{{ batch.pendingCount }}
|
||||
</span>
|
||||
<span v-if="batch.skippedCount > 0">
|
||||
{{ $t('documentCollection.importDoc.skippedCount') }}
|
||||
{{ batch.skippedCount }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<ElButton
|
||||
v-if="canContinue"
|
||||
class="batch-status__continue"
|
||||
type="primary"
|
||||
link
|
||||
:loading="continuing"
|
||||
@click="continueBatch"
|
||||
>
|
||||
{{ $t('documentCollection.importDoc.continueBatch') }}
|
||||
</ElButton>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.batch-status {
|
||||
display: flex;
|
||||
flex: 1 1 360px;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
min-width: min(360px, 100%);
|
||||
max-width: 620px;
|
||||
padding: 8px 12px;
|
||||
background: hsl(var(--surface-contrast-soft) / 74%);
|
||||
border: 1px solid var(--el-border-color-lighter);
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
.batch-status__summary {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.batch-status__headline,
|
||||
.batch-status__metrics {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 4px 10px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.batch-status__headline {
|
||||
margin-bottom: 5px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.batch-status__title {
|
||||
font-weight: 600;
|
||||
color: var(--el-text-color-primary);
|
||||
}
|
||||
|
||||
.batch-status__count {
|
||||
font-variant-numeric: tabular-nums;
|
||||
color: var(--el-color-primary);
|
||||
}
|
||||
|
||||
.batch-status__state {
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
|
||||
.batch-status__progress {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.batch-status__metrics {
|
||||
margin-top: 4px;
|
||||
overflow: hidden;
|
||||
font-size: 11px;
|
||||
line-height: 16px;
|
||||
color: var(--el-text-color-secondary);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.batch-status__metric--danger {
|
||||
color: var(--el-color-danger);
|
||||
}
|
||||
|
||||
.batch-status__continue {
|
||||
flex-shrink: 0;
|
||||
min-width: 48px;
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
.batch-status {
|
||||
flex-basis: 100%;
|
||||
max-width: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -32,6 +32,7 @@ import { buildKnowledgeShareUrl } from '#/api/knowledge-share';
|
||||
import { api, SseClient } from '#/api/request';
|
||||
import documentIcon from '#/assets/ai/knowledge/document.svg';
|
||||
import PageData from '#/components/page/PageData.vue';
|
||||
import { resolveDocumentTaskErrorText } from '#/views/ai/documentCollection/document-import-error';
|
||||
import { buildKnowledgePath } from '#/views/ai/documentCollection/share-path';
|
||||
|
||||
interface DocumentStatusPayload {
|
||||
@@ -40,6 +41,7 @@ interface DocumentStatusPayload {
|
||||
failedChunks?: number;
|
||||
knowledgeId?: number | string;
|
||||
lastTaskError?: string;
|
||||
lastTaskErrorCode?: string;
|
||||
parseCurrentStage?: string;
|
||||
parseStatusMessage?: string;
|
||||
processStatus?: string;
|
||||
@@ -86,6 +88,7 @@ const STREAM_RECONNECT_DELAY = 1500;
|
||||
const STREAM_RELOAD_DELAY = 250;
|
||||
|
||||
const pageDataRef = ref();
|
||||
const retryingDocumentIds = ref<Set<string>>(new Set());
|
||||
const taskStatusStreamClient = new SseClient();
|
||||
let reconnectTimer: null | ReturnType<typeof setTimeout> = null;
|
||||
let reloadTimer: null | ReturnType<typeof setTimeout> = null;
|
||||
@@ -102,7 +105,7 @@ defineExpose({
|
||||
},
|
||||
});
|
||||
|
||||
const processingStatuses = new Set(['INDEXING', 'PARSING']);
|
||||
const processingStatuses = new Set(['INDEXING', 'PARSING', 'SPLITTING']);
|
||||
|
||||
const isProcessingStatus = (status?: string) =>
|
||||
processingStatuses.has(status || '');
|
||||
@@ -113,7 +116,8 @@ const resolvedPermissions = computed(() => ({
|
||||
canDownloadContent: props.permissions?.canDownloadContent ?? true,
|
||||
}));
|
||||
|
||||
const hasPermission = (key: PermissionKey) => Boolean(resolvedPermissions.value[key]);
|
||||
const hasPermission = (key: PermissionKey) =>
|
||||
Boolean(resolvedPermissions.value[key]);
|
||||
|
||||
const statusMetaMap: Record<
|
||||
string,
|
||||
@@ -142,6 +146,14 @@ const statusMetaMap: Record<
|
||||
icon: Loading,
|
||||
toneClass: 'status-pill--warning',
|
||||
},
|
||||
SPLIT_FAILED: {
|
||||
icon: CloseBold,
|
||||
toneClass: 'status-pill--danger',
|
||||
},
|
||||
SPLITTING: {
|
||||
icon: Loading,
|
||||
toneClass: 'status-pill--warning',
|
||||
},
|
||||
READY_FOR_INDEX: {
|
||||
icon: Opportunity,
|
||||
toneClass: 'status-pill--primary',
|
||||
@@ -204,9 +216,21 @@ const parseStageLabels: Record<string, string> = {
|
||||
};
|
||||
|
||||
const getProcessingHint = (row: any) =>
|
||||
row.parseStatusMessage ||
|
||||
parseStageLabels[row.parseCurrentStage || ''] ||
|
||||
'';
|
||||
row.parseStatusMessage || parseStageLabels[row.parseCurrentStage || ''] || '';
|
||||
|
||||
const getErrorText = (row: any) => {
|
||||
const taskError = resolveDocumentTaskErrorText(row, $t);
|
||||
if (taskError) {
|
||||
return taskError;
|
||||
}
|
||||
if (
|
||||
row.processStatus === 'SPLIT_FAILED' ||
|
||||
row.processStatus === 'INDEX_FAILED'
|
||||
) {
|
||||
return $t('documentCollection.importDoc.splitOrIndexFailed');
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
const clearReconnectTimer = () => {
|
||||
if (!reconnectTimer) {
|
||||
@@ -242,6 +266,7 @@ const patchDocumentRow = (payload: DocumentStatusPayload) => {
|
||||
completedChunks: payload.completedChunks,
|
||||
failedChunks: payload.failedChunks,
|
||||
lastTaskError: payload.lastTaskError,
|
||||
lastTaskErrorCode: payload.lastTaskErrorCode,
|
||||
parseCurrentStage: payload.parseCurrentStage,
|
||||
parseStatusMessage: payload.parseStatusMessage,
|
||||
processStatus: payload.processStatus,
|
||||
@@ -363,17 +388,33 @@ const handleContinue = (row: any) => {
|
||||
emits('continueProcess', row);
|
||||
};
|
||||
|
||||
const handleRetryParse = async (row: any) => {
|
||||
await requestTaskAction(
|
||||
'/api/v1/document/import/task/retryParse',
|
||||
{
|
||||
knowledgeId: props.knowledgeId,
|
||||
documentId: row.id,
|
||||
},
|
||||
getStatusLabel('PARSING'),
|
||||
);
|
||||
const handleRetry = async (row: any) => {
|
||||
const documentId = String(row.id);
|
||||
if (retryingDocumentIds.value.has(documentId)) {
|
||||
return;
|
||||
}
|
||||
retryingDocumentIds.value = new Set([
|
||||
documentId,
|
||||
...retryingDocumentIds.value,
|
||||
]);
|
||||
try {
|
||||
await requestTaskAction(
|
||||
'/api/v1/document/import/task/retry',
|
||||
{
|
||||
knowledgeId: props.knowledgeId,
|
||||
documentId: row.id,
|
||||
},
|
||||
getStatusLabel('PARSING'),
|
||||
);
|
||||
} finally {
|
||||
const next = new Set(retryingDocumentIds.value);
|
||||
next.delete(documentId);
|
||||
retryingDocumentIds.value = next;
|
||||
}
|
||||
};
|
||||
|
||||
const isRetrying = (row: any) => retryingDocumentIds.value.has(String(row.id));
|
||||
|
||||
const handleView = (row: any) => {
|
||||
emits('viewDoc', row.id);
|
||||
};
|
||||
@@ -428,13 +469,18 @@ const primaryActionConfigs: Record<
|
||||
label: () => $t('button.viewSegmentation'),
|
||||
},
|
||||
INDEX_FAILED: {
|
||||
handler: handleContinue,
|
||||
label: () => $t('button.continueProcess'),
|
||||
handler: handleRetry,
|
||||
label: () => $t('documentCollection.importDoc.retry'),
|
||||
permission: 'canCreateContent',
|
||||
},
|
||||
PARSE_FAILED: {
|
||||
handler: handleRetryParse,
|
||||
label: () => $t('button.retryParse'),
|
||||
handler: handleRetry,
|
||||
label: () => $t('documentCollection.importDoc.retry'),
|
||||
permission: 'canCreateContent',
|
||||
},
|
||||
SPLIT_FAILED: {
|
||||
handler: handleRetry,
|
||||
label: () => $t('documentCollection.importDoc.retry'),
|
||||
permission: 'canCreateContent',
|
||||
},
|
||||
READY_FOR_INDEX: {
|
||||
@@ -583,7 +629,8 @@ watch(
|
||||
<div
|
||||
v-if="
|
||||
row.processStatus === 'INDEXING' ||
|
||||
row.processStatus === 'PARSING'
|
||||
row.processStatus === 'PARSING' ||
|
||||
row.processStatus === 'SPLITTING'
|
||||
"
|
||||
class="status-progress"
|
||||
>
|
||||
@@ -595,19 +642,24 @@ watch(
|
||||
{{ getProgressText(row) }}
|
||||
</span>
|
||||
<span
|
||||
v-if="row.processStatus === 'PARSING' && getProcessingHint(row)"
|
||||
v-if="
|
||||
row.processStatus === 'PARSING' && getProcessingHint(row)
|
||||
"
|
||||
class="status-progress__hint"
|
||||
>
|
||||
{{ getProcessingHint(row) }}
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
v-else-if="row.lastTaskError"
|
||||
class="status-error"
|
||||
:title="row.lastTaskError"
|
||||
<ElTooltip
|
||||
v-else-if="getErrorText(row)"
|
||||
:content="getErrorText(row)"
|
||||
placement="top"
|
||||
:show-after="300"
|
||||
>
|
||||
{{ row.lastTaskError }}
|
||||
</div>
|
||||
<div class="status-error">
|
||||
{{ getErrorText(row) }}
|
||||
</div>
|
||||
</ElTooltip>
|
||||
</div>
|
||||
</template>
|
||||
</ElTableColumn>
|
||||
@@ -624,6 +676,8 @@ watch(
|
||||
v-if="getPrimaryActionLabel(row)"
|
||||
link
|
||||
type="primary"
|
||||
:disabled="isRetrying(row)"
|
||||
:loading="isRetrying(row)"
|
||||
@click="handlePrimaryAction(row)"
|
||||
>
|
||||
{{ getPrimaryActionLabel(row) }}
|
||||
|
||||
@@ -5,7 +5,15 @@ import { useRoute } from 'vue-router';
|
||||
import { EasyFlowFormModal } from '@easyflow/common-ui';
|
||||
import { $t } from '@easyflow/locales';
|
||||
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { InfoFilled } from '@element-plus/icons-vue';
|
||||
import {
|
||||
ElButton,
|
||||
ElIcon,
|
||||
ElMessage,
|
||||
ElRadioButton,
|
||||
ElRadioGroup,
|
||||
ElTooltip,
|
||||
} from 'element-plus';
|
||||
|
||||
import { api } from '#/api/request';
|
||||
import ImportKnowledgeFileContainer from '#/views/ai/documentCollection/ImportKnowledgeFileContainer.vue';
|
||||
@@ -24,6 +32,10 @@ const props = defineProps({
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
enableBulkAuto: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
});
|
||||
|
||||
const emits = defineEmits(['imported']);
|
||||
@@ -32,22 +44,43 @@ const route = useRoute();
|
||||
const fileUploadRef = ref<InstanceType<typeof ImportKnowledgeFileContainer>>();
|
||||
const dialogVisible = ref(false);
|
||||
const submitting = ref(false);
|
||||
const batchReady = ref(false);
|
||||
const duplicatePolicy = ref<'OVERWRITE' | 'REIMPORT' | 'SKIP'>('SKIP');
|
||||
|
||||
const knowledgeId = computed(
|
||||
() => props.knowledgeIdProp || (route.query.id as string) || '',
|
||||
);
|
||||
|
||||
const resetDialogState = () => {
|
||||
batchReady.value = false;
|
||||
duplicatePolicy.value = 'SKIP';
|
||||
fileUploadRef.value?.reset?.();
|
||||
};
|
||||
|
||||
const closeDialog = () => {
|
||||
const handleBatchStateChange = (state: { ready: boolean }) => {
|
||||
batchReady.value = state.ready;
|
||||
};
|
||||
|
||||
const closeDialog = async () => {
|
||||
if (submitting.value) {
|
||||
return false;
|
||||
}
|
||||
dialogVisible.value = false;
|
||||
resetDialogState();
|
||||
return true;
|
||||
try {
|
||||
if (props.enableBulkAuto) {
|
||||
await fileUploadRef.value?.cancelCurrentBatch?.();
|
||||
} else {
|
||||
resetDialogState();
|
||||
}
|
||||
dialogVisible.value = false;
|
||||
batchReady.value = false;
|
||||
duplicatePolicy.value = 'SKIP';
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
ElMessage.error(
|
||||
error?.message || $t('documentCollection.importDoc.cancelBatchFailed'),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const openDialog = () => {
|
||||
@@ -108,6 +141,39 @@ const createTasks = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const startBatchImport = async (importMode: 'AUTO' | 'MANUAL') => {
|
||||
const selectedBatchId = fileUploadRef.value?.getBatchId?.();
|
||||
if (!selectedBatchId || !fileUploadRef.value?.isBatchReady?.()) {
|
||||
ElMessage.error($t('documentCollection.importDoc.uploadCompleteFirst'));
|
||||
return;
|
||||
}
|
||||
submitting.value = true;
|
||||
try {
|
||||
const res = await props.requestClient.post(
|
||||
'/api/v1/document/import/batch/start',
|
||||
{
|
||||
batchId: selectedBatchId,
|
||||
duplicatePolicy: duplicatePolicy.value,
|
||||
importMode,
|
||||
knowledgeId: knowledgeId.value,
|
||||
},
|
||||
);
|
||||
if (res.errorCode !== 0) {
|
||||
return;
|
||||
}
|
||||
ElMessage.success(
|
||||
importMode === 'AUTO'
|
||||
? $t('documentCollection.importDoc.autoImportStarted')
|
||||
: $t('documentCollection.importDoc.manualImportStarted'),
|
||||
);
|
||||
dialogVisible.value = false;
|
||||
resetDialogState();
|
||||
emits('imported');
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
defineExpose({
|
||||
closeDialog,
|
||||
openDialog,
|
||||
@@ -124,15 +190,69 @@ defineExpose({
|
||||
:confirm-loading="submitting"
|
||||
:confirm-text="$t('button.importFile')"
|
||||
:submitting="submitting"
|
||||
:show-footer="!enableBulkAuto"
|
||||
width="xl"
|
||||
@confirm="createTasks"
|
||||
>
|
||||
<div class="import-dialog">
|
||||
<p class="import-dialog__tip">
|
||||
{{ $t('documentCollection.importDoc.uploadCreateTip') }}
|
||||
{{
|
||||
$t(
|
||||
enableBulkAuto
|
||||
? 'documentCollection.importDoc.batchUploadTip'
|
||||
: 'documentCollection.importDoc.uploadCreateTip',
|
||||
)
|
||||
}}
|
||||
</p>
|
||||
|
||||
<ImportKnowledgeFileContainer ref="fileUploadRef" />
|
||||
<ImportKnowledgeFileContainer
|
||||
ref="fileUploadRef"
|
||||
:batch-mode="enableBulkAuto"
|
||||
:knowledge-id="knowledgeId"
|
||||
@batch-state-change="handleBatchStateChange"
|
||||
/>
|
||||
<div v-if="enableBulkAuto && batchReady" class="duplicate-policy">
|
||||
<span class="duplicate-policy__label">
|
||||
{{ $t('documentCollection.importDoc.duplicatePolicy') }}
|
||||
</span>
|
||||
<ElRadioGroup v-model="duplicatePolicy" size="small">
|
||||
<ElRadioButton value="SKIP">
|
||||
{{ $t('documentCollection.importDoc.skipDuplicates') }}
|
||||
</ElRadioButton>
|
||||
<ElRadioButton value="OVERWRITE">
|
||||
{{ $t('documentCollection.importDoc.overwriteDuplicates') }}
|
||||
</ElRadioButton>
|
||||
<ElRadioButton value="REIMPORT">
|
||||
{{ $t('documentCollection.importDoc.reimportDuplicates') }}
|
||||
</ElRadioButton>
|
||||
</ElRadioGroup>
|
||||
</div>
|
||||
<div v-if="enableBulkAuto" class="import-dialog__footer">
|
||||
<ElButton :disabled="submitting" @click="closeDialog">
|
||||
{{ $t('button.cancel') }}
|
||||
</ElButton>
|
||||
<ElButton
|
||||
:disabled="submitting || !batchReady"
|
||||
:loading="submitting"
|
||||
@click="startBatchImport('MANUAL')"
|
||||
>
|
||||
{{ $t('documentCollection.importDoc.manualImport') }}
|
||||
</ElButton>
|
||||
<ElTooltip
|
||||
:content="$t('documentCollection.importDoc.autoImportTip')"
|
||||
placement="top"
|
||||
>
|
||||
<ElButton
|
||||
type="primary"
|
||||
:disabled="submitting || !batchReady"
|
||||
:loading="submitting"
|
||||
@click="startBatchImport('AUTO')"
|
||||
>
|
||||
{{ $t('documentCollection.importDoc.autoImport') }}
|
||||
<ElIcon class="import-dialog__info"><InfoFilled /></ElIcon>
|
||||
</ElButton>
|
||||
</ElTooltip>
|
||||
</div>
|
||||
</div>
|
||||
</EasyFlowFormModal>
|
||||
</template>
|
||||
@@ -151,6 +271,36 @@ defineExpose({
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
|
||||
.import-dialog__footer {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
justify-content: flex-end;
|
||||
padding-top: 16px;
|
||||
border-top: 1px solid var(--el-border-color-lighter);
|
||||
}
|
||||
|
||||
.duplicate-policy {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px 12px;
|
||||
align-items: center;
|
||||
min-height: 32px;
|
||||
}
|
||||
|
||||
.duplicate-policy__label {
|
||||
font-size: 13px;
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
|
||||
.import-dialog__footer :deep(.el-button + .el-button) {
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
.import-dialog__info {
|
||||
margin-left: 6px;
|
||||
}
|
||||
|
||||
:deep(.upload-demo) {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,33 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { computed, h, ref, watch } from 'vue';
|
||||
|
||||
import { useAppConfig } from '@easyflow/hooks';
|
||||
import { $t } from '@easyflow/locales';
|
||||
import { useAccessStore } from '@easyflow/stores';
|
||||
|
||||
import { ElButton, ElProgress, ElTable, ElTableColumn } from 'element-plus';
|
||||
import { FolderOpened, UploadFilled } from '@element-plus/icons-vue';
|
||||
import {
|
||||
ElButton,
|
||||
ElIcon,
|
||||
ElMessage,
|
||||
ElProgress,
|
||||
ElTable,
|
||||
ElTableColumn,
|
||||
} from 'element-plus';
|
||||
import {
|
||||
ElTableV2,
|
||||
ElAutoResizer as TableV2AutoResizer,
|
||||
} from 'element-plus/es/components/table-v2/index.mjs';
|
||||
|
||||
import { formatFileSize } from '#/api/common/file';
|
||||
import { api } from '#/api/request';
|
||||
import DragFileUpload from '#/components/upload/DragFileUpload.vue';
|
||||
|
||||
interface FileInfo {
|
||||
import { resolveDocumentUploadResponse } from './document-import-upload-response';
|
||||
|
||||
import 'element-plus/es/components/table-v2/style/css.mjs';
|
||||
|
||||
interface LegacyFileInfo {
|
||||
uid: string;
|
||||
fileName: string;
|
||||
progressUpload: number;
|
||||
@@ -16,99 +35,607 @@ interface FileInfo {
|
||||
status: string;
|
||||
filePath: string;
|
||||
}
|
||||
const fileData = ref<FileInfo[]>([]);
|
||||
const filesPath = ref([]);
|
||||
|
||||
interface BatchFileInfo {
|
||||
clientFileKey: string;
|
||||
error?: string;
|
||||
file: File;
|
||||
fileName: string;
|
||||
fileSize: number;
|
||||
itemId?: string;
|
||||
progressUpload: number;
|
||||
relativePath: string;
|
||||
status: 'error' | 'queued' | 'success' | 'uploading';
|
||||
uid: string;
|
||||
}
|
||||
|
||||
interface BatchCreateResponse {
|
||||
batchId: string;
|
||||
items: Array<{
|
||||
clientFileKey: string;
|
||||
itemId: string;
|
||||
}>;
|
||||
uploadConcurrency: number;
|
||||
}
|
||||
|
||||
const props = defineProps({
|
||||
batchMode: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
knowledgeId: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
});
|
||||
const emit = defineEmits<{
|
||||
batchStateChange: [
|
||||
state: {
|
||||
ready: boolean;
|
||||
uploading: boolean;
|
||||
},
|
||||
];
|
||||
}>();
|
||||
|
||||
const MAX_FILE_COUNT = 2000;
|
||||
const MAX_FILE_SIZE_BYTES = 100 * 1024 * 1024;
|
||||
const MAX_TOTAL_SIZE_BYTES = 1024 * 1024 * 1024;
|
||||
const SUPPORTED_EXTENSIONS = new Set([
|
||||
'docx',
|
||||
'md',
|
||||
'pdf',
|
||||
'pptx',
|
||||
'txt',
|
||||
'xlsx',
|
||||
]);
|
||||
|
||||
const fileData = ref<LegacyFileInfo[]>([]);
|
||||
const filesPath = ref<any[]>([]);
|
||||
const dragUploadRef = ref<InstanceType<typeof DragFileUpload>>();
|
||||
const batchFiles = ref<BatchFileInfo[]>([]);
|
||||
const batchId = ref('');
|
||||
const folderInputRef = ref<HTMLInputElement>();
|
||||
const fileInputRef = ref<HTMLInputElement>();
|
||||
const activeRequests = new Set<XMLHttpRequest>();
|
||||
const accessStore = useAccessStore();
|
||||
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
||||
let batchGeneration = 0;
|
||||
|
||||
const batchUploading = computed(() =>
|
||||
batchFiles.value.some(
|
||||
(item) => item.status === 'queued' || item.status === 'uploading',
|
||||
),
|
||||
);
|
||||
const batchReady = computed(
|
||||
() =>
|
||||
Boolean(batchId.value) &&
|
||||
batchFiles.value.length > 0 &&
|
||||
batchFiles.value.every((item) => item.status === 'success'),
|
||||
);
|
||||
|
||||
watch(
|
||||
[batchReady, batchUploading],
|
||||
([ready, uploading]) => {
|
||||
emit('batchStateChange', { ready, uploading });
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
function resolveUploadProgressStatus(
|
||||
status: BatchFileInfo['status'],
|
||||
): 'exception' | 'success' | undefined {
|
||||
if (status === 'success') return 'success';
|
||||
if (status === 'error') return 'exception';
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const batchColumns = computed(() => [
|
||||
{
|
||||
cellRenderer: ({ rowData }: any) =>
|
||||
h(
|
||||
'span',
|
||||
{
|
||||
class: 'batch-file-name',
|
||||
title: rowData.relativePath,
|
||||
},
|
||||
rowData.relativePath,
|
||||
),
|
||||
dataKey: 'relativePath',
|
||||
flexGrow: 1,
|
||||
key: 'relativePath',
|
||||
title: $t('documentCollection.importDoc.fileName'),
|
||||
width: 420,
|
||||
},
|
||||
{
|
||||
cellRenderer: ({ rowData }: any) =>
|
||||
h(ElProgress, {
|
||||
percentage: Math.round(rowData.progressUpload || 0),
|
||||
status: resolveUploadProgressStatus(rowData.status),
|
||||
strokeWidth: 8,
|
||||
}),
|
||||
dataKey: 'progressUpload',
|
||||
key: 'progressUpload',
|
||||
title: $t('documentCollection.importDoc.progressUpload'),
|
||||
width: 220,
|
||||
},
|
||||
{
|
||||
cellRenderer: ({ rowData }: any) =>
|
||||
h('span', formatFileSize(rowData.fileSize)),
|
||||
dataKey: 'fileSize',
|
||||
key: 'fileSize',
|
||||
title: $t('documentCollection.importDoc.fileSize'),
|
||||
width: 132,
|
||||
},
|
||||
{
|
||||
cellRenderer: ({ rowData }: any) => {
|
||||
if (rowData.status === 'error') {
|
||||
return h(
|
||||
ElButton,
|
||||
{
|
||||
link: true,
|
||||
title:
|
||||
rowData.error || $t('documentCollection.importDoc.uploadFailed'),
|
||||
type: 'primary',
|
||||
onClick: () => retryBatchUpload(rowData),
|
||||
},
|
||||
() => $t('documentCollection.importDoc.retryUpload'),
|
||||
);
|
||||
}
|
||||
return h(
|
||||
'span',
|
||||
{
|
||||
class: rowData.status === 'success' ? 'upload-state--success' : '',
|
||||
title: rowData.error || '',
|
||||
},
|
||||
rowData.status === 'success'
|
||||
? $t('documentCollection.importDoc.uploaded')
|
||||
: $t('documentCollection.importDoc.uploading'),
|
||||
);
|
||||
},
|
||||
dataKey: 'status',
|
||||
key: 'status',
|
||||
title: $t('common.handle'),
|
||||
width: 112,
|
||||
},
|
||||
]);
|
||||
|
||||
function resetState() {
|
||||
batchGeneration += 1;
|
||||
for (const request of activeRequests) {
|
||||
request.abort();
|
||||
}
|
||||
activeRequests.clear();
|
||||
fileData.value = [];
|
||||
filesPath.value = [];
|
||||
batchFiles.value = [];
|
||||
batchId.value = '';
|
||||
dragUploadRef.value?.clearFiles?.();
|
||||
if (folderInputRef.value) folderInputRef.value.value = '';
|
||||
if (fileInputRef.value) fileInputRef.value.value = '';
|
||||
}
|
||||
|
||||
async function cancelCurrentBatch() {
|
||||
for (const request of activeRequests) {
|
||||
request.abort();
|
||||
}
|
||||
activeRequests.clear();
|
||||
const currentBatchId = batchId.value;
|
||||
if (currentBatchId) {
|
||||
const response = await api.post('/api/v1/document/import/batch/cancel', {
|
||||
batchId: currentBatchId,
|
||||
knowledgeId: props.knowledgeId,
|
||||
});
|
||||
if (response.errorCode !== 0) {
|
||||
throw new Error(
|
||||
response.message ||
|
||||
$t('documentCollection.importDoc.cancelBatchFailed'),
|
||||
);
|
||||
}
|
||||
}
|
||||
resetState();
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
cancelCurrentBatch,
|
||||
getBatchId() {
|
||||
return batchId.value;
|
||||
},
|
||||
getFilesData() {
|
||||
return fileData.value.filter((item) => item.filePath);
|
||||
},
|
||||
reset() {
|
||||
fileData.value = [];
|
||||
filesPath.value = [];
|
||||
dragUploadRef.value?.clearFiles?.();
|
||||
isBatchReady() {
|
||||
return batchReady.value;
|
||||
},
|
||||
isUploading() {
|
||||
return batchUploading.value;
|
||||
},
|
||||
reset: resetState,
|
||||
});
|
||||
|
||||
function handleSuccess(response: any) {
|
||||
filesPath.value = response.data;
|
||||
}
|
||||
|
||||
function handleChange(file: any) {
|
||||
const existingFile = fileData.value.find((item) => item.uid === file.uid);
|
||||
if (existingFile) {
|
||||
fileData.value = fileData.value.map((item) => {
|
||||
if (item.uid === file.uid) {
|
||||
return {
|
||||
...item,
|
||||
fileSize: file.size,
|
||||
progressUpload: file.percentage,
|
||||
status: file.status,
|
||||
filePath: file?.response?.data?.path,
|
||||
};
|
||||
}
|
||||
return item;
|
||||
});
|
||||
} else {
|
||||
fileData.value.push({
|
||||
uid: file.uid,
|
||||
fileName: file.name,
|
||||
progressUpload: file.percentage,
|
||||
fileSize: file.size,
|
||||
status: file.status,
|
||||
filePath: file?.response?.data?.path,
|
||||
});
|
||||
fileData.value = fileData.value.map((item) =>
|
||||
item.uid === file.uid
|
||||
? {
|
||||
...item,
|
||||
filePath: file?.response?.data?.path,
|
||||
fileSize: file.size,
|
||||
progressUpload: file.percentage,
|
||||
status: file.status,
|
||||
}
|
||||
: item,
|
||||
);
|
||||
return;
|
||||
}
|
||||
fileData.value.push({
|
||||
fileName: file.name,
|
||||
filePath: file?.response?.data?.path,
|
||||
fileSize: file.size,
|
||||
progressUpload: file.percentage,
|
||||
status: file.status,
|
||||
uid: file.uid,
|
||||
});
|
||||
}
|
||||
|
||||
function handleRemove(row: LegacyFileInfo) {
|
||||
fileData.value = fileData.value.filter((item) => item.uid !== row.uid);
|
||||
}
|
||||
|
||||
function triggerFileSelect() {
|
||||
if (!batchUploading.value) {
|
||||
fileInputRef.value?.click();
|
||||
}
|
||||
}
|
||||
|
||||
function handleRemove(row: any) {
|
||||
fileData.value = fileData.value.filter((item) => item.uid !== row.uid);
|
||||
function triggerFolderSelect(event: Event) {
|
||||
event.stopPropagation();
|
||||
if (!batchUploading.value) {
|
||||
folderInputRef.value?.click();
|
||||
}
|
||||
}
|
||||
|
||||
async function handleNativeSelection(event: Event) {
|
||||
const input = event.target as HTMLInputElement;
|
||||
const files = [...(input.files || [])];
|
||||
input.value = '';
|
||||
if (files.length > 0) {
|
||||
await prepareBatch(files);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDrop(event: DragEvent) {
|
||||
if (batchUploading.value) return;
|
||||
const files = [...(event.dataTransfer?.files || [])];
|
||||
if (files.length > 0) {
|
||||
await prepareBatch(files);
|
||||
}
|
||||
}
|
||||
|
||||
async function prepareBatch(files: File[]) {
|
||||
const generation = ++batchGeneration;
|
||||
const accepted: File[] = [];
|
||||
let totalBytes = 0;
|
||||
let ignoredCount = 0;
|
||||
for (const file of files) {
|
||||
const relativePath = getRelativePath(file);
|
||||
if (
|
||||
relativePath.includes('/__MACOSX/') ||
|
||||
relativePath.split('/').at(-1) === '.DS_Store'
|
||||
) {
|
||||
ignoredCount++;
|
||||
continue;
|
||||
}
|
||||
const extension = file.name.split('.').pop()?.toLowerCase() || '';
|
||||
if (!SUPPORTED_EXTENSIONS.has(extension)) {
|
||||
ignoredCount++;
|
||||
continue;
|
||||
}
|
||||
if (file.size > MAX_FILE_SIZE_BYTES) {
|
||||
ElMessage.warning($t('documentCollection.importDoc.singleFileLimit'));
|
||||
return;
|
||||
}
|
||||
totalBytes += file.size;
|
||||
accepted.push(file);
|
||||
}
|
||||
if (accepted.length === 0) {
|
||||
ElMessage.warning($t('documentCollection.importDoc.noSupportedFiles'));
|
||||
return;
|
||||
}
|
||||
if (accepted.length > MAX_FILE_COUNT) {
|
||||
ElMessage.warning($t('documentCollection.importDoc.fileCountLimit'));
|
||||
return;
|
||||
}
|
||||
if (totalBytes > MAX_TOTAL_SIZE_BYTES) {
|
||||
ElMessage.warning($t('documentCollection.importDoc.folderSizeLimit'));
|
||||
return;
|
||||
}
|
||||
if (ignoredCount > 0) {
|
||||
ElMessage.info($t('documentCollection.importDoc.unsupportedSkipped'));
|
||||
}
|
||||
|
||||
batchFiles.value = await Promise.all(
|
||||
accepted.map(async (file, index) => {
|
||||
const relativePath = getRelativePath(file);
|
||||
return {
|
||||
clientFileKey: await createClientFileKey(relativePath),
|
||||
file,
|
||||
fileName: file.name,
|
||||
fileSize: file.size,
|
||||
progressUpload: 0,
|
||||
relativePath,
|
||||
status: 'queued',
|
||||
uid: `${Date.now()}-${index}`,
|
||||
} satisfies BatchFileInfo;
|
||||
}),
|
||||
);
|
||||
batchId.value = '';
|
||||
|
||||
try {
|
||||
const response = await api.post('/api/v1/document/import/batch/create', {
|
||||
files: batchFiles.value.map((item) => ({
|
||||
clientFileKey: item.clientFileKey,
|
||||
fileName: item.fileName,
|
||||
fileSize: item.fileSize,
|
||||
relativePath: item.relativePath,
|
||||
})),
|
||||
knowledgeId: props.knowledgeId,
|
||||
});
|
||||
if (response.errorCode !== 0 || !response.data) {
|
||||
throw new Error(
|
||||
response.message ||
|
||||
$t('documentCollection.importDoc.createBatchFailed'),
|
||||
);
|
||||
}
|
||||
const data = response.data as BatchCreateResponse;
|
||||
if (generation !== batchGeneration) {
|
||||
await api.post('/api/v1/document/import/batch/cancel', {
|
||||
batchId: data.batchId,
|
||||
knowledgeId: props.knowledgeId,
|
||||
});
|
||||
return;
|
||||
}
|
||||
batchId.value = String(data.batchId);
|
||||
const itemMap = new Map(
|
||||
data.items.map((item) => [item.clientFileKey, String(item.itemId)]),
|
||||
);
|
||||
for (const item of batchFiles.value) {
|
||||
item.itemId = itemMap.get(item.clientFileKey);
|
||||
}
|
||||
await uploadWithWorkers(Math.max(1, Number(data.uploadConcurrency || 3)));
|
||||
} catch (error: any) {
|
||||
for (const item of batchFiles.value) {
|
||||
item.status = 'error';
|
||||
item.error =
|
||||
error?.message || $t('documentCollection.importDoc.createBatchFailed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadWithWorkers(concurrency: number) {
|
||||
let nextIndex = 0;
|
||||
const worker = async () => {
|
||||
while (nextIndex < batchFiles.value.length) {
|
||||
const index = nextIndex++;
|
||||
const item = batchFiles.value[index];
|
||||
if (item) await uploadBatchItem(item);
|
||||
}
|
||||
};
|
||||
await Promise.all(
|
||||
Array.from({ length: Math.min(concurrency, batchFiles.value.length) }, () =>
|
||||
worker(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function retryBatchUpload(item: BatchFileInfo) {
|
||||
if (!item.itemId || !batchId.value) return;
|
||||
await uploadBatchItem(item);
|
||||
}
|
||||
|
||||
function uploadBatchItem(item: BatchFileInfo) {
|
||||
return new Promise<void>((resolve) => {
|
||||
if (!item.itemId || !batchId.value) {
|
||||
item.status = 'error';
|
||||
item.error = $t('documentCollection.importDoc.createBatchFailed');
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
const request = new XMLHttpRequest();
|
||||
activeRequests.add(request);
|
||||
item.status = 'uploading';
|
||||
item.error = undefined;
|
||||
request.open(
|
||||
'POST',
|
||||
`${apiURL}/api/v1/document/import/batch/${batchId.value}/item/${item.itemId}/upload?knowledgeId=${encodeURIComponent(props.knowledgeId)}`,
|
||||
);
|
||||
if (accessStore.accessToken) {
|
||||
request.setRequestHeader('easyflow-token', accessStore.accessToken);
|
||||
}
|
||||
request.setRequestHeader('Accept', 'application/json');
|
||||
request.upload.addEventListener('progress', (progressEvent) => {
|
||||
if (progressEvent.lengthComputable) {
|
||||
item.progressUpload = Math.round(
|
||||
(progressEvent.loaded * 100) / progressEvent.total,
|
||||
);
|
||||
}
|
||||
});
|
||||
request.addEventListener('load', () => {
|
||||
activeRequests.delete(request);
|
||||
try {
|
||||
const response = resolveDocumentUploadResponse({
|
||||
fallbackMessage: $t('documentCollection.importDoc.uploadFailed'),
|
||||
responseText: request.responseText || '',
|
||||
status: request.status,
|
||||
statusText: request.statusText,
|
||||
});
|
||||
if (!response.success) {
|
||||
throw new Error(response.message);
|
||||
}
|
||||
item.progressUpload = 100;
|
||||
item.status = 'success';
|
||||
} catch (error: any) {
|
||||
item.status = 'error';
|
||||
item.error =
|
||||
error?.message || $t('documentCollection.importDoc.uploadFailed');
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
request.addEventListener('error', () => {
|
||||
activeRequests.delete(request);
|
||||
item.status = 'error';
|
||||
item.error = $t('documentCollection.importDoc.uploadFailed');
|
||||
resolve();
|
||||
});
|
||||
request.addEventListener('abort', () => {
|
||||
activeRequests.delete(request);
|
||||
resolve();
|
||||
});
|
||||
const formData = new FormData();
|
||||
formData.append('file', item.file, item.fileName);
|
||||
request.send(formData);
|
||||
});
|
||||
}
|
||||
|
||||
function getRelativePath(file: File) {
|
||||
return (
|
||||
(file as File & { webkitRelativePath?: string }).webkitRelativePath ||
|
||||
file.name
|
||||
).replaceAll('\\', '/');
|
||||
}
|
||||
|
||||
async function createClientFileKey(relativePath: string) {
|
||||
const source = relativePath;
|
||||
if (globalThis.crypto?.subtle) {
|
||||
const digest = await globalThis.crypto.subtle.digest(
|
||||
'SHA-256',
|
||||
new TextEncoder().encode(source),
|
||||
);
|
||||
return [...new Uint8Array(digest)]
|
||||
.map((value) => value.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
}
|
||||
return [...source]
|
||||
.reduce(
|
||||
(hash, char) => (hash * 31 + (char.codePointAt(0) || 0)) >>> 0,
|
||||
2_166_136_261,
|
||||
)
|
||||
.toString(16)
|
||||
.padStart(64, '0');
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="import-file-container">
|
||||
<DragFileUpload
|
||||
ref="dragUploadRef"
|
||||
@success="handleSuccess"
|
||||
@on-change="handleChange"
|
||||
/>
|
||||
<div class="import-file-container__table">
|
||||
<ElTable :data="fileData" style="width: 100%" size="large">
|
||||
<ElTableColumn
|
||||
prop="fileName"
|
||||
:label="$t('documentCollection.importDoc.fileName')"
|
||||
width="250"
|
||||
/>
|
||||
<ElTableColumn
|
||||
prop="progressUpload"
|
||||
:label="$t('documentCollection.importDoc.progressUpload')"
|
||||
width="180"
|
||||
<template v-if="batchMode">
|
||||
<input
|
||||
ref="fileInputRef"
|
||||
class="native-file-input"
|
||||
type="file"
|
||||
multiple
|
||||
accept=".txt,.pdf,.docx,.md,.pptx,.xlsx"
|
||||
@change="handleNativeSelection"
|
||||
/>
|
||||
<input
|
||||
ref="folderInputRef"
|
||||
class="native-file-input"
|
||||
type="file"
|
||||
multiple
|
||||
webkitdirectory
|
||||
@change="handleNativeSelection"
|
||||
/>
|
||||
<div
|
||||
class="batch-drop-zone"
|
||||
:class="{ 'batch-drop-zone--disabled': batchUploading }"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
@click="triggerFileSelect"
|
||||
@keydown.enter="triggerFileSelect"
|
||||
@dragover.prevent
|
||||
@drop.prevent="handleDrop"
|
||||
>
|
||||
<ElIcon class="batch-drop-zone__icon"><UploadFilled /></ElIcon>
|
||||
<div class="batch-drop-zone__title">
|
||||
{{ $t('documentCollection.importDoc.batchUploadTitle') }}
|
||||
</div>
|
||||
<div class="batch-drop-zone__description">
|
||||
{{ $t('documentCollection.importDoc.batchUploadDescription') }}
|
||||
</div>
|
||||
<ElButton
|
||||
:icon="FolderOpened"
|
||||
:disabled="batchUploading"
|
||||
@click="triggerFolderSelect"
|
||||
>
|
||||
<template #default="{ row }">
|
||||
<ElProgress
|
||||
:percentage="row.progressUpload"
|
||||
v-if="row.status === 'success'"
|
||||
status="success"
|
||||
{{ $t('documentCollection.importDoc.selectFolder') }}
|
||||
</ElButton>
|
||||
</div>
|
||||
|
||||
<div class="batch-table">
|
||||
<TableV2AutoResizer>
|
||||
<template #default="{ height, width }">
|
||||
<ElTableV2
|
||||
:columns="batchColumns"
|
||||
:data="batchFiles"
|
||||
:height="height"
|
||||
:width="width"
|
||||
:row-height="52"
|
||||
fixed
|
||||
row-key="uid"
|
||||
/>
|
||||
<ElProgress v-else :percentage="row.progressUpload" />
|
||||
</template>
|
||||
</ElTableColumn>
|
||||
<ElTableColumn
|
||||
prop="fileSize"
|
||||
:label="$t('documentCollection.importDoc.fileSize')"
|
||||
>
|
||||
<template #default="{ row }">
|
||||
<span>{{ formatFileSize(row.fileSize) }}</span>
|
||||
</template>
|
||||
</ElTableColumn>
|
||||
<ElTableColumn :label="$t('common.handle')">
|
||||
<template #default="{ row }">
|
||||
<ElButton type="danger" size="small" @click="handleRemove(row)">
|
||||
{{ $t('button.delete') }}
|
||||
</ElButton>
|
||||
</template>
|
||||
</ElTableColumn>
|
||||
</ElTable>
|
||||
</div>
|
||||
</TableV2AutoResizer>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
<DragFileUpload
|
||||
ref="dragUploadRef"
|
||||
@success="handleSuccess"
|
||||
@on-change="handleChange"
|
||||
/>
|
||||
<div class="import-file-container__table">
|
||||
<ElTable :data="fileData" style="width: 100%" size="large">
|
||||
<ElTableColumn
|
||||
prop="fileName"
|
||||
:label="$t('documentCollection.importDoc.fileName')"
|
||||
width="250"
|
||||
/>
|
||||
<ElTableColumn
|
||||
prop="progressUpload"
|
||||
:label="$t('documentCollection.importDoc.progressUpload')"
|
||||
width="180"
|
||||
>
|
||||
<template #default="{ row }">
|
||||
<ElProgress
|
||||
v-if="row.status === 'success'"
|
||||
:percentage="row.progressUpload"
|
||||
status="success"
|
||||
/>
|
||||
<ElProgress v-else :percentage="row.progressUpload" />
|
||||
</template>
|
||||
</ElTableColumn>
|
||||
<ElTableColumn
|
||||
prop="fileSize"
|
||||
:label="$t('documentCollection.importDoc.fileSize')"
|
||||
>
|
||||
<template #default="{ row }">
|
||||
<span>{{ formatFileSize(row.fileSize) }}</span>
|
||||
</template>
|
||||
</ElTableColumn>
|
||||
<ElTableColumn :label="$t('common.handle')">
|
||||
<template #default="{ row }">
|
||||
<ElButton type="danger" size="small" @click="handleRemove(row)">
|
||||
{{ $t('button.delete') }}
|
||||
</ElButton>
|
||||
</template>
|
||||
</ElTableColumn>
|
||||
</ElTable>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -116,10 +643,80 @@ function handleRemove(row: any) {
|
||||
.import-file-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.import-file-container__table {
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.native-file-input {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.batch-drop-zone {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 208px;
|
||||
padding: 24px;
|
||||
cursor: pointer;
|
||||
background: hsl(var(--surface-contrast-soft) / 54%);
|
||||
border: 1px dashed var(--el-color-primary-light-5);
|
||||
border-radius: 16px;
|
||||
transition:
|
||||
background-color 0.2s,
|
||||
border-color 0.2s;
|
||||
}
|
||||
|
||||
.batch-drop-zone:hover,
|
||||
.batch-drop-zone:focus-visible {
|
||||
background: var(--el-color-primary-light-9);
|
||||
border-color: var(--el-color-primary);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.batch-drop-zone--disabled {
|
||||
cursor: wait;
|
||||
opacity: 0.72;
|
||||
}
|
||||
|
||||
.batch-drop-zone__icon {
|
||||
font-size: 48px;
|
||||
color: var(--el-color-primary);
|
||||
}
|
||||
|
||||
.batch-drop-zone__title {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: var(--el-text-color-primary);
|
||||
}
|
||||
|
||||
.batch-drop-zone__description {
|
||||
margin-bottom: 4px;
|
||||
font-size: 13px;
|
||||
color: var(--el-text-color-secondary);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.batch-table {
|
||||
width: 100%;
|
||||
height: 312px;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--el-border-color-lighter);
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
:deep(.batch-file-name) {
|
||||
display: block;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
:deep(.upload-state--success) {
|
||||
color: var(--el-color-success);
|
||||
}
|
||||
</style>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,67 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { resolveDocumentTaskErrorText } from './document-import-error';
|
||||
|
||||
const translate = (key: string) => `translated:${key}`;
|
||||
|
||||
describe('resolveDocumentTaskErrorText', () => {
|
||||
it('uses the stable backend error code', () => {
|
||||
expect(
|
||||
resolveDocumentTaskErrorText(
|
||||
{ lastTaskErrorCode: 'parse_service_unavailable' },
|
||||
translate,
|
||||
),
|
||||
).toBe('translated:documentCollection.importDoc.parseServiceUnavailable');
|
||||
});
|
||||
|
||||
it('supports the error code stored in document options', () => {
|
||||
expect(
|
||||
resolveDocumentTaskErrorText(
|
||||
{ options: { 'task.errorCode': 'pending_timeout' } },
|
||||
translate,
|
||||
),
|
||||
).toBe('translated:documentCollection.importDoc.taskPendingTimeout');
|
||||
});
|
||||
|
||||
it('normalizes legacy MinerU 503 errors', () => {
|
||||
expect(
|
||||
resolveDocumentTaskErrorText(
|
||||
{
|
||||
lastTaskError:
|
||||
'MinerU request failed: path=/tasks, status=503, body=',
|
||||
},
|
||||
translate,
|
||||
),
|
||||
).toBe('translated:documentCollection.importDoc.parseServiceUnavailable');
|
||||
});
|
||||
|
||||
it('distinguishes legacy internal document access errors from MinerU errors', () => {
|
||||
expect(
|
||||
resolveDocumentTaskErrorText(
|
||||
{
|
||||
lastTaskError:
|
||||
'下载文档 URL 失败: http://127.0.0.1/file.docx; 远端文档地址不允许访问非公网目标',
|
||||
},
|
||||
translate,
|
||||
),
|
||||
).toBe('translated:documentCollection.importDoc.documentSourceUnavailable');
|
||||
});
|
||||
|
||||
it('uses the stable document source error code', () => {
|
||||
expect(
|
||||
resolveDocumentTaskErrorText(
|
||||
{ lastTaskErrorCode: 'document_source_unavailable' },
|
||||
translate,
|
||||
),
|
||||
).toBe('translated:documentCollection.importDoc.documentSourceUnavailable');
|
||||
});
|
||||
|
||||
it('keeps unknown business errors intact', () => {
|
||||
expect(
|
||||
resolveDocumentTaskErrorText(
|
||||
{ lastTaskError: '文档内容为空' },
|
||||
translate,
|
||||
),
|
||||
).toBe('文档内容为空');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
interface DocumentTaskErrorSource {
|
||||
lastTaskError?: string;
|
||||
lastTaskErrorCode?: string;
|
||||
options?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
const ERROR_MESSAGE_KEYS: Record<string, string> = {
|
||||
document_source_unavailable:
|
||||
'documentCollection.importDoc.documentSourceUnavailable',
|
||||
execution_interrupted:
|
||||
'documentCollection.importDoc.taskExecutionInterrupted',
|
||||
parse_service_timeout: 'documentCollection.importDoc.parseServiceTimeout',
|
||||
parse_service_unavailable:
|
||||
'documentCollection.importDoc.parseServiceUnavailable',
|
||||
pending_timeout: 'documentCollection.importDoc.taskPendingTimeout',
|
||||
};
|
||||
|
||||
const resolveLegacyErrorCode = (message: string) => {
|
||||
const normalized = message.toLowerCase();
|
||||
if (
|
||||
normalized.includes('下载文档 url 失败') ||
|
||||
normalized.includes('读取批量导入文件失败') ||
|
||||
normalized.includes('远端文档地址不允许访问非公网目标')
|
||||
) {
|
||||
return 'document_source_unavailable';
|
||||
}
|
||||
if (
|
||||
normalized.includes('status=502') ||
|
||||
normalized.includes('status=503') ||
|
||||
normalized.includes('status=504') ||
|
||||
normalized.includes('connection refused') ||
|
||||
normalized.includes('failed to connect') ||
|
||||
normalized.includes('failed to call mineru endpoint') ||
|
||||
normalized.includes('no route to host') ||
|
||||
normalized.includes('unknown host')
|
||||
) {
|
||||
return 'parse_service_unavailable';
|
||||
}
|
||||
if (
|
||||
normalized.includes('timed out') ||
|
||||
normalized.includes('timeout') ||
|
||||
normalized.includes('超时')
|
||||
) {
|
||||
return normalized.includes('排队')
|
||||
? 'pending_timeout'
|
||||
: 'parse_service_timeout';
|
||||
}
|
||||
if (normalized.includes('任务执行中断')) {
|
||||
return 'execution_interrupted';
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 将后端稳定错误码及历史原始错误转换为面向用户的本地化文案。
|
||||
*/
|
||||
export const resolveDocumentTaskErrorText = (
|
||||
row: DocumentTaskErrorSource,
|
||||
translate: (key: string) => string,
|
||||
) => {
|
||||
const storedCode =
|
||||
row.lastTaskErrorCode ||
|
||||
String(row.options?.['task.errorCode'] || '') ||
|
||||
resolveLegacyErrorCode(row.lastTaskError || '');
|
||||
const messageKey = ERROR_MESSAGE_KEYS[storedCode];
|
||||
return messageKey ? translate(messageKey) : row.lastTaskError || '';
|
||||
};
|
||||
@@ -0,0 +1,71 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { resolveDocumentUploadResponse } from './document-import-upload-response';
|
||||
|
||||
const fallbackMessage = '文件上传失败';
|
||||
|
||||
describe('resolveDocumentUploadResponse', () => {
|
||||
it.each([
|
||||
['数字错误码', '{"errorCode":0}'],
|
||||
['字符串错误码', '{"errorCode":"0"}'],
|
||||
['兼容 code 字段', '{"code":0}'],
|
||||
['带 BOM 的响应', '\uFEFF{"errorCode":0}'],
|
||||
])('接受%s', (_name, responseText) => {
|
||||
expect(
|
||||
resolveDocumentUploadResponse({
|
||||
fallbackMessage,
|
||||
responseText,
|
||||
status: 200,
|
||||
}),
|
||||
).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('接受无响应体的 204 响应', () => {
|
||||
expect(
|
||||
resolveDocumentUploadResponse({
|
||||
fallbackMessage,
|
||||
responseText: '',
|
||||
status: 204,
|
||||
}),
|
||||
).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('返回业务错误消息', () => {
|
||||
expect(
|
||||
resolveDocumentUploadResponse({
|
||||
fallbackMessage,
|
||||
responseText: '{"errorCode":4091,"message":"文件已存在"}',
|
||||
status: 409,
|
||||
}),
|
||||
).toEqual({
|
||||
message: '文件已存在',
|
||||
success: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('拒绝成功状态下的未知响应格式', () => {
|
||||
expect(
|
||||
resolveDocumentUploadResponse({
|
||||
fallbackMessage,
|
||||
responseText: 'binary-response',
|
||||
status: 200,
|
||||
}),
|
||||
).toEqual({
|
||||
message: fallbackMessage,
|
||||
success: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('拒绝缺少成功错误码的空响应', () => {
|
||||
expect(
|
||||
resolveDocumentUploadResponse({
|
||||
fallbackMessage,
|
||||
responseText: '',
|
||||
status: 200,
|
||||
}),
|
||||
).toEqual({
|
||||
message: fallbackMessage,
|
||||
success: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
interface UploadResponsePayload {
|
||||
code?: unknown;
|
||||
error?: unknown;
|
||||
errorCode?: unknown;
|
||||
message?: unknown;
|
||||
msg?: unknown;
|
||||
}
|
||||
|
||||
interface ResolveUploadResponseOptions {
|
||||
fallbackMessage: string;
|
||||
responseText: string;
|
||||
status: number;
|
||||
statusText?: string;
|
||||
}
|
||||
|
||||
interface UploadResponseResult {
|
||||
message?: string;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
function resolveMessage(
|
||||
payload: undefined | UploadResponsePayload,
|
||||
statusText: string | undefined,
|
||||
fallbackMessage: string,
|
||||
) {
|
||||
const message =
|
||||
payload?.message ?? payload?.error ?? payload?.msg ?? statusText;
|
||||
return typeof message === 'string' && message.trim()
|
||||
? message.trim()
|
||||
: fallbackMessage;
|
||||
}
|
||||
|
||||
export function resolveDocumentUploadResponse({
|
||||
fallbackMessage,
|
||||
responseText,
|
||||
status,
|
||||
statusText,
|
||||
}: ResolveUploadResponseOptions): UploadResponseResult {
|
||||
const isHttpSuccess = status >= 200 && status < 300;
|
||||
const normalizedBody = responseText.replace(/^\uFEFF/, '').trim();
|
||||
let payload: undefined | UploadResponsePayload;
|
||||
|
||||
if (normalizedBody) {
|
||||
try {
|
||||
payload = JSON.parse(normalizedBody) as UploadResponsePayload;
|
||||
} catch {
|
||||
return {
|
||||
message: fallbackMessage,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!isHttpSuccess) {
|
||||
return {
|
||||
message: resolveMessage(payload, statusText, fallbackMessage),
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
if (status === 204) {
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
const rawErrorCode = payload?.errorCode ?? payload?.code;
|
||||
const errorCode = Number(rawErrorCode);
|
||||
if (!Number.isFinite(errorCode) || errorCode !== 0) {
|
||||
return {
|
||||
message: resolveMessage(payload, statusText, fallbackMessage),
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
@@ -32,7 +32,10 @@ interface Entity {
|
||||
deptId: number | string;
|
||||
expiredAt: Date | null | string;
|
||||
permissionIds: (number | string)[]; // 绑定值:权限 ID 数组
|
||||
knowledgeShareEnabled: boolean;
|
||||
knowledgeReadEnabled: boolean;
|
||||
knowledgeImportEnabled: boolean;
|
||||
knowledgeMaintenanceEnabled: boolean;
|
||||
knowledgeShareEnabled?: boolean;
|
||||
workflowApiEnabled: boolean;
|
||||
id?: number; // 编辑时的主键
|
||||
}
|
||||
@@ -51,7 +54,9 @@ const entity = ref<Entity>({
|
||||
deptId: '',
|
||||
expiredAt: null,
|
||||
permissionIds: [],
|
||||
knowledgeShareEnabled: false,
|
||||
knowledgeReadEnabled: false,
|
||||
knowledgeImportEnabled: false,
|
||||
knowledgeMaintenanceEnabled: false,
|
||||
workflowApiEnabled: false,
|
||||
});
|
||||
// 加载状态
|
||||
@@ -121,7 +126,14 @@ function getResourcePermissionList() {
|
||||
|
||||
function createDefaultEntity(row: Partial<Entity> = {}): Entity {
|
||||
const permissionIds = row.permissionIds || [];
|
||||
const knowledgeShareEnabled = Boolean(row.knowledgeShareEnabled);
|
||||
const legacyKnowledgeEnabled = Boolean(row.knowledgeShareEnabled);
|
||||
const knowledgeReadEnabled = Boolean(
|
||||
row.knowledgeReadEnabled ?? legacyKnowledgeEnabled,
|
||||
);
|
||||
const knowledgeImportEnabled = Boolean(
|
||||
row.knowledgeImportEnabled ?? legacyKnowledgeEnabled,
|
||||
);
|
||||
const knowledgeMaintenanceEnabled = Boolean(row.knowledgeMaintenanceEnabled);
|
||||
const workflowApiEnabled = Boolean(row.workflowApiEnabled);
|
||||
return {
|
||||
apiKey: '',
|
||||
@@ -130,7 +142,9 @@ function createDefaultEntity(row: Partial<Entity> = {}): Entity {
|
||||
expiredAt: null,
|
||||
...row,
|
||||
permissionIds,
|
||||
knowledgeShareEnabled,
|
||||
knowledgeReadEnabled,
|
||||
knowledgeImportEnabled,
|
||||
knowledgeMaintenanceEnabled,
|
||||
workflowApiEnabled,
|
||||
};
|
||||
}
|
||||
@@ -189,7 +203,9 @@ function closeDialog() {
|
||||
deptId: '',
|
||||
expiredAt: null,
|
||||
permissionIds: [],
|
||||
knowledgeShareEnabled: false,
|
||||
knowledgeReadEnabled: false,
|
||||
knowledgeImportEnabled: false,
|
||||
knowledgeMaintenanceEnabled: false,
|
||||
workflowApiEnabled: false,
|
||||
};
|
||||
isAdd.value = true;
|
||||
@@ -264,10 +280,22 @@ defineExpose({
|
||||
</ElCheckbox>
|
||||
</ElCheckboxGroup>
|
||||
<ElCheckbox
|
||||
v-model="entity.knowledgeShareEnabled"
|
||||
v-model="entity.knowledgeReadEnabled"
|
||||
class="permission-checkbox"
|
||||
>
|
||||
{{ $t('sysApiKey.knowledgeSharePermission') }}
|
||||
{{ $t('sysApiKey.knowledgeReadPermission') }}
|
||||
</ElCheckbox>
|
||||
<ElCheckbox
|
||||
v-model="entity.knowledgeImportEnabled"
|
||||
class="permission-checkbox"
|
||||
>
|
||||
{{ $t('sysApiKey.knowledgeImportPermission') }}
|
||||
</ElCheckbox>
|
||||
<ElCheckbox
|
||||
v-model="entity.knowledgeMaintenanceEnabled"
|
||||
class="permission-checkbox"
|
||||
>
|
||||
{{ $t('sysApiKey.knowledgeMaintenancePermission') }}
|
||||
</ElCheckbox>
|
||||
<ElCheckbox
|
||||
v-model="entity.workflowApiEnabled"
|
||||
|
||||
Reference in New Issue
Block a user