()
| 97 | |
| 98 | // 初始化程序 |
| 99 | async init() { |
| 100 | initAnalytics() |
| 101 | logger.info('Desktop app starting') |
| 102 | |
| 103 | // E2E 测试模式:跳过遗留数据迁移 |
| 104 | // 遗留迁移会删除 Documents/ChatLab,在本地测试时可能破坏用户数据 |
| 105 | if (!this.isTestMode) { |
| 106 | // 应用上次设置页登记的数据目录切换;必须早于任何数据库连接初始化 |
| 107 | this.applyPendingDataDirMigrationIfNeeded() |
| 108 | |
| 109 | // 清理上次切换目录后的旧数据目录 |
| 110 | cleanupPendingDeleteDir() |
| 111 | |
| 112 | // 执行数据目录迁移(从 Documents/ChatLab 迁移到 userData) |
| 113 | this.migrateDataIfNeeded() |
| 114 | |
| 115 | // 执行统一目录结构迁移(Electron 旧布局 → 双根目录) |
| 116 | this.migrateToUnifiedDirsIfNeeded() |
| 117 | } |
| 118 | |
| 119 | // 验证数据路径是否正确(安全网:防止 config.toml 指向空目录) |
| 120 | verifyDataPath() |
| 121 | |
| 122 | // 确保应用目录存在 |
| 123 | ensureAppDirs() |
| 124 | |
| 125 | let runtime: RuntimeIdentity |
| 126 | try { |
| 127 | runtime = assertDesktopDataDirCompatible(getPathProvider(), getDesktopAppVersion(app.getVersion())) |
| 128 | } catch (error) { |
| 129 | console.error('[Main] Data directory compatibility check failed:', error) |
| 130 | dialog.showErrorBox( |
| 131 | 'ChatLab Data Directory Incompatible', |
| 132 | `ChatLab cannot open this data directory with the current desktop version.\n\n${ |
| 133 | error instanceof Error ? error.message : String(error) |
| 134 | }` |
| 135 | ) |
| 136 | app.quit() |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | // 执行配置数据迁移(Migration Runner,Electron 和 CLI 共享) |
| 141 | await new MigrationRunner(ALL_MIGRATIONS, { |
| 142 | dataDir: getSystemDataDir(), |
| 143 | aiDataDir: getAiDataDir(), |
| 144 | logger: { |
| 145 | info: (_cat: string, msg: string) => console.log(`[Migration] ${msg}`), |
| 146 | warn: (_cat: string, msg: string) => console.warn(`[Migration] ${msg}`), |
| 147 | error: (_cat: string, msg: string, ...args: unknown[]) => console.error(`[Migration] ${msg}`, ...args), |
| 148 | }, |
| 149 | }).run() |
| 150 | |
| 151 | // 初始化主进程国际化(在 ensureAppDirs 之后,确保 settings 目录存在) |
| 152 | await initLocale() |
| 153 | |
| 154 | // 执行数据库 schema 迁移(确保所有数据库在 Worker 查询前已是最新 schema) |
| 155 | try { |
| 156 | this.migrateDatabasesIfNeeded(runtime) |
no test coverage detected