(limit = 5, cwd: string = process.cwd())
| 28 | } |
| 29 | |
| 30 | export function loadRecentSessions(limit = 5, cwd: string = process.cwd()): RecentSessionSummary[] { |
| 31 | const projects = resolveProjectIDsForCwd(cwd) |
| 32 | const targetProjects = projects.length > 0 ? projects : listAllProjectIDs() |
| 33 | if (targetProjects.length === 0) return [] |
| 34 | |
| 35 | const sessions: RecentSessionSummary[] = [] |
| 36 | for (const projectID of targetProjects) { |
| 37 | const sessionDir = path.join(SESSION_ROOT, projectID) |
| 38 | let files: string[] |
| 39 | try { |
| 40 | files = fs.readdirSync(sessionDir) |
| 41 | } catch { |
| 42 | continue |
| 43 | } |
| 44 | |
| 45 | for (const file of files) { |
| 46 | if (!file.endsWith(".json")) continue |
| 47 | const record = readJson<StoredSessionRecord>(path.join(sessionDir, file)) |
| 48 | if (!record || record.parentID) continue |
| 49 | const updated = record.time?.updated |
| 50 | if (typeof updated !== "number") continue |
| 51 | sessions.push({ |
| 52 | id: record.id ?? file.replace(/\.json$/, ""), |
| 53 | title: record.title, |
| 54 | updated, |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | sessions.sort((a, b) => (b.updated ?? 0) - (a.updated ?? 0)) |
| 60 | return sessions.slice(0, limit) |
| 61 | } |
| 62 | |
| 63 | function resolveProjectIDsForCwd(cwd: string): string[] { |
| 64 | const normalizedCwd = path.resolve(cwd) |
no test coverage detected