(chatId: string)
| 127 | * Delete a saved chat session from local history. |
| 128 | */ |
| 129 | export function deleteChatSession(chatId: string): boolean { |
| 130 | try { |
| 131 | const safeChatId = chatId.trim() |
| 132 | if ( |
| 133 | !safeChatId || |
| 134 | safeChatId === '.' || |
| 135 | safeChatId === '..' || |
| 136 | path.basename(safeChatId) !== safeChatId |
| 137 | ) { |
| 138 | logger.warn({ chatId }, 'Refusing to delete invalid chat id') |
| 139 | return false |
| 140 | } |
| 141 | |
| 142 | const chatsDir = getChatsDir() |
| 143 | const chatPath = path.join(chatsDir, safeChatId) |
| 144 | |
| 145 | if (!fs.existsSync(chatPath)) { |
| 146 | return false |
| 147 | } |
| 148 | |
| 149 | const stat = fs.statSync(chatPath) |
| 150 | if (!stat.isDirectory()) { |
| 151 | logger.warn( |
| 152 | { chatId, chatPath }, |
| 153 | 'Refusing to delete non-directory chat path', |
| 154 | ) |
| 155 | return false |
| 156 | } |
| 157 | |
| 158 | fs.rmSync(chatPath, { recursive: true, force: false }) |
| 159 | return true |
| 160 | } catch (error) { |
| 161 | logger.error( |
| 162 | { chatId, error: error instanceof Error ? error.message : String(error) }, |
| 163 | 'Failed to delete chat session', |
| 164 | ) |
| 165 | return false |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Format a timestamp relative to now (e.g., "2 hours ago", "yesterday") |
no test coverage detected