| 50 | |
| 51 | /** Sessions for a workspace, most recently updated first. */ |
| 52 | export function listSessions(cwd: string): SessionData[] { |
| 53 | let files: string[] |
| 54 | try { |
| 55 | files = fs.readdirSync(getSessionsDir()).filter((f) => f.endsWith(".json")) |
| 56 | } catch { |
| 57 | return [] |
| 58 | } |
| 59 | const sessions: SessionData[] = [] |
| 60 | for (const file of files) { |
| 61 | try { |
| 62 | const data = JSON.parse(fs.readFileSync(path.join(getSessionsDir(), file), "utf8")) as SessionData |
| 63 | if (data.cwd === cwd && Array.isArray(data.messages) && data.messages.length > 0) { |
| 64 | sessions.push(data) |
| 65 | } |
| 66 | } catch { |
| 67 | // skip unreadable session files |
| 68 | } |
| 69 | } |
| 70 | sessions.sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : -1)) |
| 71 | return sessions.slice(0, MAX_SESSIONS_LISTED) |
| 72 | } |