From 55c39e77326966cd25da3916aad0c12cae9edcf0 Mon Sep 17 00:00:00 2001 From: Momentry Studio Date: Tue, 20 Oct 2026 17:57:27 +0800 Subject: [PATCH] feat: keep only last 2 hours of search history - Filter history to show only items from last 2 hours - Pinned items are always kept regardless of age - Auto-cleanup old unpinned items from database - Increased fetch limit to 50 to ensure enough items for filtering --- src/composables/useSearchHistory.ts | 34 +++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/composables/useSearchHistory.ts b/src/composables/useSearchHistory.ts index d9b3587..e722287 100644 --- a/src/composables/useSearchHistory.ts +++ b/src/composables/useSearchHistory.ts @@ -15,12 +15,25 @@ export interface HistoryItem { export function useSearchHistory() { const history = ref([]) const loading = ref(false) + const HISTORY_HOURS = 2 // Keep last 2 hours of history async function loadHistory() { loading.value = true try { - const result = await apiCall('get_search_history', { limit: 30 }) - history.value = Array.isArray(result) ? result : [] + const result = await apiCall('get_search_history', { limit: 50 }) + const allHistory = Array.isArray(result) ? result : [] + + // Filter to last 2 hours (keep pinned items regardless of age) + const cutoffTime = Date.now() - (HISTORY_HOURS * 60 * 60 * 1000) + history.value = allHistory.filter((item: HistoryItem) => { + if (item.pinned) return true // Always keep pinned items + if (!item.updated_at) return false + const itemTime = new Date(item.updated_at).getTime() + return itemTime >= cutoffTime + }) + + // Clean up old unpinned items from database + cleanupOldHistory(cutoffTime) } catch (e) { console.error('Failed to load history:', e) } finally { @@ -28,6 +41,23 @@ export function useSearchHistory() { } } + async function cleanupOldHistory(cutoffTime: number) { + try { + const result = await apiCall('get_search_history', { limit: 100 }) + const allHistory = Array.isArray(result) ? result : [] + for (const item of allHistory) { + if (item.pinned) continue + if (!item.updated_at) continue + const itemTime = new Date(item.updated_at).getTime() + if (itemTime < cutoffTime) { + await apiCall('delete_search_history', { id: item.id }) + } + } + } catch (e) { + console.error('Failed to cleanup old history:', e) + } + } + async function saveToHistory(query: string, chatState: string, mode: string) { const id = `h_${Date.now()}_${Math.random().toString(36).slice(2, 8)}` try {