MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / mergeImportFiles

Function mergeImportFiles

src/stores/session.ts:498–604  ·  view source on GitHub ↗

* 合并导入多个文件为一个会话

(filePaths: string[])

Source from the content-addressed store, hash-verified

496 * 合并导入多个文件为一个会话
497 */
498 async function mergeImportFiles(filePaths: string[]): Promise<MergeImportResult> {
499 if (filePaths.length < 2) {
500 return { success: false, error: '合并导入至少需要2个文件' }
501 }
502
503 // 阶段最小显示时间(和单文件导入保持一致)
504 const MIN_STAGE_TIME = 800
505
506 isMergeImporting.value = true
507 mergeError.value = null
508 mergeResult.value = null
509 mergeStage.value = 'parsing'
510
511 // 初始化文件列表
512 mergeFiles.value = filePaths.map((path) => ({
513 path,
514 name: path.split('/').pop() || path.split('\\').pop() || path,
515 status: 'pending' as MergeFileStatus,
516 }))
517
518 let stageStartTime = Date.now()
519
520 try {
521 // 阶段1:串行解析所有文件
522 for (let i = 0; i < mergeFiles.value.length; i++) {
523 const file = mergeFiles.value[i]
524 const fileStartTime = Date.now()
525 file.status = 'parsing'
526
527 try {
528 const info = await window.mergeApi.parseFileInfo(file.path)
529 file.info = info
530
531 // 确保每个文件的解析状态至少显示一定时间
532 const elapsed = Date.now() - fileStartTime
533 const minFileTime = Math.max(300, MIN_STAGE_TIME / filePaths.length)
534 if (elapsed < minFileTime) {
535 await new Promise((resolve) => setTimeout(resolve, minFileTime - elapsed))
536 }
537
538 file.status = 'done'
539 } catch (err) {
540 throw new Error(`解析文件失败: ${file.name} - ${err instanceof Error ? err.message : String(err)}`)
541 }
542 }
543
544 // 确保解析阶段至少显示 MIN_STAGE_TIME
545 const parsingElapsed = Date.now() - stageStartTime
546 if (parsingElapsed < MIN_STAGE_TIME) {
547 await new Promise((resolve) => setTimeout(resolve, MIN_STAGE_TIME - parsingElapsed))
548 }
549
550 // 阶段2:执行合并
551 stageStartTime = Date.now()
552 mergeStage.value = 'merging'
553
554 // 智能命名:如果所有文件群名相同则用该名,否则用第一个文件的群名
555 const names = mergeFiles.value.map((f) => f.info?.name).filter(Boolean)

Callers

nothing calls this directly

Calls 7

useSessionIndexServiceFunction · 0.90
getSessionGapThresholdFunction · 0.90
setTimeoutFunction · 0.85
loadSessionsFunction · 0.85
generateMethod · 0.65
errorMethod · 0.65
nowMethod · 0.45

Tested by

no test coverage detected