()
| 299 | * Load session from current terminal's session file |
| 300 | */ |
| 301 | export function loadSession(): Session | null { |
| 302 | try { |
| 303 | // For resume, we need to find the most recent session |
| 304 | const sessionDir = getSessionDir(); |
| 305 | if (!fs.existsSync(sessionDir)) { |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | const files = fs |
| 310 | .readdirSync(sessionDir) |
| 311 | .filter((f) => f.endsWith(".json") && f !== "sessions.json") |
| 312 | .map((f) => ({ |
| 313 | name: f, |
| 314 | path: path.join(sessionDir, f), |
| 315 | mtime: fs.statSync(path.join(sessionDir, f)).mtime, |
| 316 | })) |
| 317 | .sort((a, b) => b.mtime.getTime() - a.mtime.getTime()); |
| 318 | |
| 319 | if (files.length === 0) { |
| 320 | return null; |
| 321 | } |
| 322 | |
| 323 | // Load the most recent session |
| 324 | const session: Session = JSON.parse(fs.readFileSync(files[0].path, "utf8")); |
| 325 | // Set this as the current session for future saves |
| 326 | SessionManager.getInstance().setSession(session); |
| 327 | return session; |
| 328 | } catch (error) { |
| 329 | logger.error("Error loading session:", error); |
| 330 | return null; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Create a new session |
no test coverage detected