| import { LocalStorageKeys } from 'librechat-data-provider'; |
|
|
| |
| const TIMESTAMP_SUFFIX = '_TIMESTAMP'; |
|
|
| |
| const CLEANUP_THRESHOLD = 2 * 24 * 60 * 60 * 1000; |
|
|
| |
| |
| |
| |
| const TIMESTAMPED_KEYS = [ |
| LocalStorageKeys.LAST_MCP_, |
| LocalStorageKeys.LAST_CODE_TOGGLE_, |
| LocalStorageKeys.LAST_WEB_SEARCH_TOGGLE_, |
| LocalStorageKeys.LAST_FILE_SEARCH_TOGGLE_, |
| LocalStorageKeys.LAST_ARTIFACTS_TOGGLE_, |
| LocalStorageKeys.PIN_MCP_, |
| ]; |
|
|
| |
| |
| |
| export function setTimestamp(key: string): void { |
| localStorage.setItem(`${key}${TIMESTAMP_SUFFIX}`, Date.now().toString()); |
| } |
|
|
| |
| |
| |
| export function setTimestampedValue(key: string, value: any): void { |
| localStorage.setItem(key, typeof value === 'string' ? value : JSON.stringify(value)); |
| localStorage.setItem(`${key}${TIMESTAMP_SUFFIX}`, Date.now().toString()); |
| } |
|
|
| |
| |
| |
| |
| export function getTimestampedValue(key: string): string | null { |
| const timestampKey = `${key}${TIMESTAMP_SUFFIX}`; |
| const timestamp = localStorage.getItem(timestampKey); |
|
|
| if (!timestamp) { |
| |
| return localStorage.getItem(key); |
| } |
|
|
| const age = Date.now() - parseInt(timestamp, 10); |
| if (age > CLEANUP_THRESHOLD) { |
| |
| localStorage.removeItem(key); |
| localStorage.removeItem(timestampKey); |
| return null; |
| } |
|
|
| return localStorage.getItem(key); |
| } |
|
|
| |
| |
| |
| export function removeTimestampedValue(key: string): void { |
| localStorage.removeItem(key); |
| localStorage.removeItem(`${key}${TIMESTAMP_SUFFIX}`); |
| } |
|
|
| |
| |
| |
| |
| export function cleanupTimestampedStorage(): void { |
| try { |
| const keysToRemove: string[] = []; |
| const now = Date.now(); |
|
|
| |
| for (let i = 0; i < localStorage.length; i++) { |
| const key = localStorage.key(i); |
| if (!key) continue; |
| if (key === LocalStorageKeys.PIN_MCP_) { |
| continue; |
| } |
|
|
| |
| const isTimestampedKey = TIMESTAMPED_KEYS.some( |
| (prefix) => key.startsWith(prefix) && !key.includes('pinned'), |
| ); |
|
|
| if (isTimestampedKey && !key.endsWith(TIMESTAMP_SUFFIX)) { |
| const timestampKey = `${key}${TIMESTAMP_SUFFIX}`; |
| const timestamp = localStorage.getItem(timestampKey); |
|
|
| if (!timestamp) { |
| |
| keysToRemove.push(key); |
| continue; |
| } |
|
|
| const age = now - parseInt(timestamp, 10); |
| if (age > CLEANUP_THRESHOLD) { |
| |
| keysToRemove.push(key); |
| keysToRemove.push(timestampKey); |
| } |
| } |
| } |
|
|
| keysToRemove.forEach((key) => localStorage.removeItem(key)); |
|
|
| if (keysToRemove.length > 0) { |
| console.log(`Cleaned up ${keysToRemove.length} old localStorage entries`); |
| } |
| } catch (error) { |
| console.error('Error during cleanup of timestamped storage:', error); |
| } |
| } |
|
|
| |
| |
| |
| |
| export function migrateExistingEntries(): void { |
| const now = Date.now().toString(); |
|
|
| for (let i = 0; i < localStorage.length; i++) { |
| const key = localStorage.key(i); |
| if (!key) continue; |
|
|
| const isTimestampedKey = TIMESTAMPED_KEYS.some((prefix) => key.startsWith(prefix)); |
|
|
| if (isTimestampedKey && !key.endsWith(TIMESTAMP_SUFFIX)) { |
| const timestampKey = `${key}${TIMESTAMP_SUFFIX}`; |
| const hasTimestamp = localStorage.getItem(timestampKey); |
|
|
| if (!hasTimestamp) { |
| |
| localStorage.setItem(timestampKey, now); |
| } |
| } |
| } |
| } |
|
|