Files
syops/frontend/scripts/extract-kb-workspace-composable.mjs
T

301 lines
9.3 KiB
JavaScript

/**
* scripts/extract-kb-workspace-composable.mjs
* 将 KbWorkspaceView.vue 的 script 提取为 useKbWorkspace.ts 骨架。
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const vuePath = path.resolve(__dirname, '../src/features/kb/views/KbWorkspaceView.vue');
const outPath = path.resolve(__dirname, '../src/features/kb/composables/useKbWorkspace.ts');
const vue = fs.readFileSync(vuePath, 'utf8');
const scriptMatch = vue.match(/<script setup lang="ts">([\s\S]*?)<\/script>/);
if (!scriptMatch) throw new Error('script block not found');
let body = scriptMatch[1]
.replace(/^\s*\/\*\*[\s\S]*?\*\/\s*/m, '')
.replace(/const props = defineProps<\{[\s\S]*?\}\>\(\);/, 'const embedded = options.embedded ?? false;')
.replace(/props\.embedded/g, 'embedded');
const header = `/**
* features/kb/composables/useKbWorkspace.ts
* 知识库作业面编排:分组导航、文档列表、路径上传、清洗同步与弹窗状态。
*/
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { getStoredAuthUser } from '@/shared/utils/auth';
import {
createKbDocument,
createKbPathUploadJob,
deleteKbPathJobFile,
deleteKbDocument,
getKbDocumentMarkdown,
getKbDocumentSyncRecords,
getKbDocumentVersionSourceFile,
getKbDocumentVersions,
getKbGroupFilesSafe,
getKbGroupsSafe,
getKbJobsSafe,
markKbPathJobFileFailed,
retryFailedKbPathUploadJob,
submitKbDocumentClean,
syncKbDocumentToDify,
uploadKbPathJobFile,
uploadKbDocumentVersion,
updateKbDocumentMarkdown,
type KbDocumentItem,
type KbJob,
type KbDocumentMarkdown,
type KbSyncRecordItem,
type KbDocumentVersionItem,
type KbGroup,
} from '../api/kb.api';
import {
DOCUMENT_PAGE_SIZE_OPTIONS,
FAILURE_PAGE_SIZE,
IDLE_BADGE_CLASS,
PATH_UPLOAD_BATCH_INTERVAL_MS,
PATH_UPLOAD_BATCH_SIZE,
PATH_UPLOAD_CONCURRENCY,
type FailureModalItem,
type KbWorkspaceViewMode,
type LoadListOptions,
type PathUploadTaskItem,
type WorkflowStageState,
} from '../types/kb-workspace.types';
import { mergeStableItems } from '../utils/kb-list.utils';
import {
formatDateTime,
formatFileSize,
getDocumentDisplayDescription,
getDocumentDisplayTitle,
getStatusBadgeClass,
getStatusLabel,
normalizeDuplicateDocumentError,
} from '../utils/kb-display.utils';
import {
getPathUploadJobItems,
getPathUploadJobPriority,
getPathUploadJobProgress,
getPathUploadJobSummary,
hasRetryablePathUploadFailure,
} from '../utils/kb-path-upload.utils';
import {
buildGetLatestJobForDocument,
getDocumentWorkflowState,
getMarkdownUnavailableMessage,
getWorkflowBadges,
normalizeWorkflowErrorMessage,
} from '../utils/kb-workflow.utils';
export interface UseKbWorkspaceOptions {
embedded?: boolean;
}
export function useKbWorkspace(options: UseKbWorkspaceOptions = {}) {
`;
const footer = `
return {
embedded,
router,
t,
isKbAdmin,
isLoading,
isDocumentsLoading,
isSubmitting,
isRecordLoading,
groups,
documents,
jobs,
selectedGroupId,
selectedDocumentId,
activeUploadDocumentId,
syncingDocumentId,
selectedDocumentVersions,
selectedDocumentSyncRecords,
feedback,
feedbackType,
viewMode,
showMarkdownModal,
showRecordModal,
showSourceInfoModal,
showFailureModal,
showPathUploadModal,
showPathUploadHistoryModal,
showDeleteConfirmDialog,
deleteConfirmMessage,
showPathUploadDetails,
fileInputRef,
pathUploadInputRef,
markdownDocumentId,
sourceInfoDocumentId,
markdownContent,
markdownKind,
markdownVersionLabel,
markdownError,
isMarkdownLoading,
isMarkdownSaving,
isSourceInfoLoading,
openingSourceVersionId,
failureModalType,
failureModalPage,
retryingFailureKey,
documentStageFilter,
documentListPage,
documentListPageSize,
documentKeyword,
batchSubmitting,
selectedDocumentIds,
pathUploadDraftText,
pathUploadSelectedFiles,
isPathUploadStaging,
pathUploadJobId,
selectedPathUploadHistoryJobId,
selectedPathUploadHistoryItemKeys,
retryingPathUploadKey,
deletingPathUploadBatch,
openDeleteConfirmDialog,
closeDeleteConfirmDialog,
handleDeleteConfirm,
selectedDocument,
sourceInfoDocument,
selectedGroup,
markdownModalTitle,
sourceInfoVersions,
activeGroupJobs,
pathUploadTask,
groupSummaryItems,
stageFilterTabs,
filteredDocuments,
filteredDocumentTotalPages,
pagedFilteredDocuments,
pagedDocumentRange,
selectedDocuments,
selectedCleanableDocuments,
isAllFilteredSelected,
failureModalItems,
failureModalTotalPages,
pagedFailureModalItems,
failureModalTitle,
documentById,
latestPathUploadJob,
pathUploadHistoryJobs,
selectedPathUploadHistoryJob,
selectedPathUploadHistoryItems,
selectedPathUploadHistorySelectedItems,
isAllPathUploadHistoryItemsSelected,
pathUploadSummary,
recentPathUploadItems,
pathUploadRunning,
kbJobPollingActive,
selectedPathRoots,
loadGroups,
loadJobs,
refreshWorkflowStatusOnly,
refreshCurrentView,
formatDateTime,
formatFileSize,
getDocumentDisplayTitle,
getDocumentDisplayDescription,
getStatusLabel,
getStatusBadgeClass,
getWorkflowBadges,
getDocumentWorkflowState,
openGroupDetail,
backToGroupOverview,
loadDocuments,
loadDocumentVersions,
openDocumentDetail,
closeDocumentDetail,
openSourceInfo,
closeSourceInfoModal,
openOriginalSourceFile,
openFailureModal,
closeFailureModal,
resetPathUploadDraft,
openPathUploadModal,
closePathUploadModal,
triggerPathUploadSelection,
handlePathFilesSelected,
removeSelectedPathFile,
openPathUploadHistoryModal,
closePathUploadHistoryModal,
togglePathUploadHistoryItemSelection,
toggleSelectAllPathUploadHistoryItems,
handleConfirmPathUpload,
handleDeletePathUploadItem,
handleBatchDeletePathUploadItems,
handleRetryFailedPathUpload,
toggleDocumentSelection,
toggleSelectAllFiltered,
handleDeleteDocument,
handleBatchSync,
handleBatchClean,
handleBatchDelete,
handleRetryFailedItem,
triggerUploadForDocument,
handleFileSelected,
triggerMultiUpload,
handleSyncDocument,
openMarkdownEditor,
closeMarkdownModal,
handleSaveMarkdown,
getPathUploadItemDisplayState,
getPathUploadJobItems,
hasRetryablePathUploadFailure,
getPathUploadJobSummary,
DOCUMENT_PAGE_SIZE_OPTIONS,
FAILURE_PAGE_SIZE,
};
}
`;
// Remove duplicate imports from body
body = body.replace(/^import[\s\S]*?from ['"][^'"]+['"];?\s*/gm, '');
body = body.replace(/type WorkflowStageState[\s\S]*?type LoadListOptions[\s\S]*?};\s*/m, '');
body = body.replace(/const FAILURE_PAGE_SIZE[\s\S]*?const DOCUMENT_PAGE_SIZE_OPTIONS[\s\S]*?as const;\s*/m, '');
body = body.replace(/const idleBadgeClass[\s\S]*?;/m, 'const idleBadgeClass = IDLE_BADGE_CLASS;');
body = body.replace(/const mergeStableItems[\s\S]*?};\s*/m, '');
body = body.replace(/const getLatestJobForDocument[\s\S]*?};\s*/m, '');
body = body.replace(/const isUnreadableErrorMessage[\s\S]*?};\s*/m, '');
body = body.replace(/const normalizeWorkflowErrorMessage[\s\S]*?};\s*/m, '');
body = body.replace(/const getDocumentWorkflowState[\s\S]*?};\s*/m, '');
body = body.replace(/const formatDateTime[\s\S]*?};\s*/m, '');
body = body.replace(/const formatFileSize[\s\S]*?};\s*/m, '');
body = body.replace(/const trimPathText[\s\S]*?};\s*/m, '');
body = body.replace(/const getDocumentDisplayTitle[\s\S]*?};\s*/m, '');
body = body.replace(/const getDocumentDisplayDescription[\s\S]*?};\s*/m, '');
body = body.replace(/const normalizeDuplicateDocumentError[\s\S]*?};\s*/m, '');
body = body.replace(/const getStatusLabel[\s\S]*?};\s*/m, '');
body = body.replace(/const getStatusBadgeClass[\s\S]*?};\s*/m, '');
body = body.replace(/const getWorkflowBadges[\s\S]*?};\s*/m, '');
body = body.replace(/const getMarkdownUnavailableMessage[\s\S]*?};\s*/m, '');
body = body.replace(/const getPathUploadJobProgress[\s\S]*?};\s*/m, '');
body = body.replace(/const getPathUploadJobPriority[\s\S]*?};\s*/m, '');
body = body.replace(/const getPathUploadJobItems[\s\S]*?};\s*/m, '');
body = body.replace(/const hasRetryablePathUploadFailure[\s\S]*?;/m, '');
body = body.replace(/const getPathUploadJobSummary[\s\S]*?};\s*/m, '');
const getLatestJobBlock = `
const getLatestJobForDocument = buildGetLatestJobForDocument(() => activeGroupJobs.value);
const resolveWorkflowState = (doc: KbDocumentItem) =>
getDocumentWorkflowState(doc, getLatestJobForDocument);
`;
body = body.replace(
/const groupSummaryItems = computed/,
`${getLatestJobBlock}\nconst groupSummaryItems = computed`,
);
body = body.replace(/getDocumentWorkflowState\(/g, 'resolveWorkflowState(');
body = body.replace(/getWorkflowBadges = \(doc: KbDocumentItem\) => \{[\s\S]*?\};/m,
'const getWorkflowBadgesForDoc = (doc: KbDocumentItem) => getWorkflowBadges(doc, getLatestJobForDocument);');
body = body.replace(/getWorkflowBadges\(/g, 'getWorkflowBadgesForDoc(');
fs.mkdirSync(path.dirname(outPath), { recursive: true });
fs.writeFileSync(outPath, `${header}${body}${footer}`, 'utf8');
console.log('Wrote', outPath);