()
| 57 | * Returns null if no chat directories exist |
| 58 | */ |
| 59 | export function getMostRecentChatDir(): string | null { |
| 60 | try { |
| 61 | const chatsDir = path.join(getProjectDataDir(), 'chats') |
| 62 | if (!statSync(chatsDir, { throwIfNoEntry: false })) { |
| 63 | return null |
| 64 | } |
| 65 | |
| 66 | const chatDirs = readdirSync(chatsDir) |
| 67 | .map((name) => { |
| 68 | const fullPath = path.join(chatsDir, name) |
| 69 | try { |
| 70 | const stat = statSync(fullPath) |
| 71 | return { name, fullPath, mtime: stat.mtime } |
| 72 | } catch { |
| 73 | return null |
| 74 | } |
| 75 | }) |
| 76 | .filter( |
| 77 | (item): item is { name: string; fullPath: string; mtime: Date } => |
| 78 | item !== null && statSync(item.fullPath).isDirectory(), |
| 79 | ) |
| 80 | |
| 81 | if (chatDirs.length === 0) { |
| 82 | return null |
| 83 | } |
| 84 | |
| 85 | // Sort by modification time, most recent first |
| 86 | chatDirs.sort((a, b) => b.mtime.getTime() - a.mtime.getTime()) |
| 87 | return chatDirs[0].fullPath |
| 88 | } catch { |
| 89 | return null |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | export function getCurrentChatDir(): string { |
| 94 | const chatId = getCurrentChatId() |
no test coverage detected