()
| 94 | } |
| 95 | |
| 96 | export async function pruneChatCache() { |
| 97 | let records; |
| 98 | try { |
| 99 | records = await chatCacheReadAll(); |
| 100 | } catch { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | records.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0)); |
| 105 | let totalBytes = 0; |
| 106 | const removals = []; |
| 107 | |
| 108 | records.forEach((record, idx) => { |
| 109 | const sizeBytes = Number.isFinite(record.sizeBytes) ? record.sizeBytes : estimateCacheBytes(record); |
| 110 | totalBytes += sizeBytes; |
| 111 | const overCount = idx >= CHAT_CACHE_MAX_SESSIONS; |
| 112 | const overTotal = totalBytes > CHAT_CACHE_MAX_TOTAL_BYTES; |
| 113 | const overSingle = sizeBytes > CHAT_CACHE_MAX_SESSION_BYTES; |
| 114 | if (overCount || overTotal || overSingle) removals.push(record.cacheKey); |
| 115 | }); |
| 116 | |
| 117 | await Promise.all(removals.map(cacheKey => chatCacheDelete(cacheKey).catch(() => {}))); |
| 118 | } |
no test coverage detected