Files
syops/frontend/src/features/kb/utils/kb-path-upload.utils.ts
T

71 lines
3.6 KiB
TypeScript

/**
* features/kb/utils/kb-path-upload.utils.ts
* 路径上传任务(path_upload job)解析与排序权重。
*/
import type { KbJob } from '../api/kb.api';
import type { PathUploadStatus, PathUploadTaskItem } from '../types/kb-workspace.types';
import type { KbTranslateFn } from './kb-i18n.types';
export const getPathUploadJobProgress = (job: KbJob | null) => {
const payload = job?.payload_json || {};
const result = job?.result_json || {};
const success = Number(result.success || 0);
const failed = Number(result.failed || 0);
const processed = Number(result.processed || success + failed || 0);
const total = Number(result.total || payload.total_files || 0);
return { total, success, failed, processed };
};
/** 用于在历史批次列表中优先展示有进度或进行中的任务 */
export const getPathUploadJobPriority = (job: KbJob) => {
const normalizedStatus = String(job.status || '').toLowerCase();
const { processed, success, failed } = getPathUploadJobProgress(job);
const hasProgress = processed > 0 || success > 0 || failed > 0 || Boolean(job.started_at) || Boolean(job.finished_at);
const statusScore = normalizedStatus === 'running'
? 50
: normalizedStatus === 'success'
? 40
: normalizedStatus === 'failed'
? 35
: ['queued', 'staging', 'draft'].includes(normalizedStatus)
? 20
: 0;
return (hasProgress ? 1000 : 0) + statusScore;
};
export const getPathUploadJobItems = (job: KbJob | null, t: KbTranslateFn): PathUploadTaskItem[] => {
if (!job || job.job_type !== 'path_upload') return [];
const payload = job.payload_json || {};
const files = Array.isArray(payload.files) ? payload.files : [];
const failedCount = files.filter((item: { status?: string }) => String(item?.status || '').toLowerCase() === 'failed').length;
return files.map((item: Record<string, unknown>, index: number) => ({
key: `${job.id}-${item.relative_path || item.original_name || index}`,
title: String(item.original_name || item.relative_path || t('kbWorkspace.errors.fileFallback', { index: index + 1 })),
displayPath: String(item.relative_path || item.original_name || ''),
rootLabel: String(item.relative_path || item.original_name || '').split('/')[0] || t('kbWorkspace.errors.localFolder'),
status: (['queued', 'uploading', 'success', 'failed'].includes(String(item.status))
? item.status
: 'queued') as PathUploadStatus,
error: (item.error as string | undefined)
|| (String(item.status || '').toLowerCase() === 'failed' && failedCount === 1
? (job.error_message || job.message || t('kbWorkspace.errors.uploadFailed'))
: undefined),
documentId: (item.document_id as number | undefined) || undefined,
retryable: Boolean(String(item.staged_path || '').trim()) && String(item.status || '').toLowerCase() === 'failed',
startedAt: undefined,
finishedAt: undefined,
}));
};
export const hasRetryablePathUploadFailure = (job: KbJob | null, t: KbTranslateFn) =>
getPathUploadJobItems(job, t).some((item) => item.retryable);
export const getPathUploadJobSummary = (job: KbJob | null, t: KbTranslateFn) => {
const items = getPathUploadJobItems(job, t);
const total = Number(job?.payload_json?.total_files || job?.result_json?.total || items.length || 0);
const success = Number(job?.result_json?.success || items.filter((item) => item.status === 'success').length || 0);
const failed = Number(job?.result_json?.failed || items.filter((item) => item.status === 'failed').length || 0);
const pending = Math.max(total - success - failed, 0);
return { total, success, failed, pending };
};