(opts: { sourcePath: string })
| 25 | } |
| 26 | |
| 27 | export async function detectMigration(opts: { sourcePath: string }): Promise<MigrationPlan> { |
| 28 | const src = opts.sourcePath; |
| 29 | |
| 30 | const hasConfig = existsSync(sourceConfigToml(src)); |
| 31 | const hasMcp = existsSync(sourceMcpJson(src)); |
| 32 | const hasUserHistory = existsSync(sourceUserHistoryDir(src)); |
| 33 | |
| 34 | const oauthCredentials = await listDirSafe(sourceCredentialsDir(src), (n) => |
| 35 | n.endsWith('.json'), |
| 36 | ); |
| 37 | const detectedPlugins = await listDirSafe(sourcePluginsDir(src), () => true); |
| 38 | const detectedMcpOauthServers = await listDirSafe(sourceMcpOauthDir(src), () => true); |
| 39 | |
| 40 | // Reverse-lookup workdir from kimi.json |
| 41 | const workdirMap = new Map<string, WorkdirMeta>(); |
| 42 | try { |
| 43 | const text = await readFile(sourceKimiJson(src), 'utf-8'); |
| 44 | const parsed = OldKimiJsonSchema.parse(JSON.parse(text)); |
| 45 | for (const wd of parsed.work_dirs) { |
| 46 | workdirMap.set(oldMd5BucketName(wd.path), { path: wd.path, kaos: wd.kaos }); |
| 47 | } |
| 48 | } catch { |
| 49 | // no kimi.json or unparseable — sessions list will be empty |
| 50 | } |
| 51 | |
| 52 | const workdirs: WorkDirEntry[] = []; |
| 53 | let totalSessions = 0; |
| 54 | |
| 55 | try { |
| 56 | const sessionsRoot = sourceSessionsDir(src); |
| 57 | const bucketNames = await readdir(sessionsRoot); |
| 58 | for (const bucketName of bucketNames) { |
| 59 | // Skip non-local-kaos buckets (`<kaos>_<md5>`) and unknown formats. |
| 60 | if (!MD5_HEX_RE.test(bucketName)) continue; |
| 61 | const wd = workdirMap.get(bucketName); |
| 62 | if (wd === undefined) continue; |
| 63 | if (wd.kaos !== 'local') continue; |
| 64 | |
| 65 | const bucketPath = join(sessionsRoot, bucketName); |
| 66 | let uuids: string[]; |
| 67 | try { |
| 68 | uuids = await readdir(bucketPath); |
| 69 | } catch { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | const sessions: SessionEntry[] = []; |
| 74 | for (const uuid of uuids) { |
| 75 | const sessionDir = join(bucketPath, uuid); |
| 76 | const cls = await classifySessionDir(sessionDir); |
| 77 | if (cls !== 'real') continue; |
| 78 | const wireMtime = await readWireMtime(sessionDir); |
| 79 | sessions.push({ uuid, oldDir: sessionDir, wireMtime }); |
| 80 | totalSessions++; |
| 81 | } |
| 82 | |
| 83 | if (sessions.length > 0) { |
| 84 | workdirs.push({ oldHashDir: bucketPath, workdirPath: wd.path, sessions }); |
no test coverage detected