fix(chat): Clear old conversations

This commit is contained in:
abdus committed 2026-10-09 13:02:57 +02:00
1 parent 74e887db04
commit 9627aaa31f
1 file changed
+46 -15
+46 -15
View File
@@ -441,7 +441,7 @@
<summary>History</summary>
<div class="history-list">
<template x-if="historyItems.length === 0">
<div style="opacity: 0.7">No conversations today</div>
<div style="opacity: 0.7">No conversations in the last 24 hours</div>
</template>
<template x-for="(item, idx) in historyItems" :key="idx">
<div class="history-item" @click="restoreConversation(item.messages)">
@@ -526,7 +526,7 @@
pendingImages: [],
attachment: initialAttachment,
promptText: initialPromptText,
activeModelName: "gemini-3.8-flash",
activeModelName: "claude-haiku-5-5",
historyItems: [],
isBusy: false,
abortController: null,
@@ -537,7 +537,7 @@
{ name: "gpt-6-luna", chat: chatWithOpenAI, key: window.env.OPENAI_API_KEY },
{ name: "gemini-3.5-flash-lite", chat: chatWithGemini, key: window.env.GEMINI_API_KEY },
{ name: "gemini-3.8-flash", chat: chatWithGemini, key: window.env.GEMINI_API_KEY },
{ name: "claude-haiku-4-5-20251001", chat: chatWithClaude, key: window.env.ANTHROPIC_API_KEY },
{ name: "claude-haiku-5-5", chat: chatWithClaude, key: window.env.ANTHROPIC_API_KEY },
],
async init() {
@@ -640,7 +640,7 @@
} finally {
this.isBusy = false;
this.abortController = null;
if (!this.attachment) updateSessionHistory(this.sessionId, this.messages);
if (!this.attachment) this.historyItems = updateSessionHistory(this.sessionId, this.messages);
}
},
@@ -669,7 +669,7 @@
const idx = this.messages.indexOf(msg);
if (idx === -1) return;
this.messages = this.messages.slice(0, idx);
if (!this.attachment) updateSessionHistory(this.sessionId, this.messages);
if (!this.attachment) this.historyItems = updateSessionHistory(this.sessionId, this.messages);
this.promptText = msg.content;
this.pendingImages = msg.images ? JSON.parse(JSON.stringify(msg.images)) : [];
this.$nextTick(() => this.$refs.promptInput.focus());
@@ -977,29 +977,60 @@
const STORAGE_KEY = "chat_conversations";
const todayKey = () => new Date().toISOString().slice(0, 10);
const readHistory = () => {
const all = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
const cutoff = Date.now() - 24 * 60 * 60 * 1000;
let changed = false;
for (const day of Object.keys(all)) {
const recent = all[day].filter((item) => Date.parse(item.time) >= cutoff);
if (recent.length !== all[day].length) changed = true;
if (recent.length) all[day] = recent;
else {
delete all[day];
changed = true;
}
}
if (changed) localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
return all;
};
const historyItemsFrom = (all) => Object.values(all).flat().sort((a, b) => Date.parse(b.time) - Date.parse(a.time));
const loadHistory = () => {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}")[todayKey()] || [];
return historyItemsFrom(readHistory());
} catch {
return [];
}
};
const updateSessionHistory = (sessionId, messages) => {
const all = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
const today = todayKey();
all[today] ??= [];
const all = readHistory();
const day = Object.keys(all).find((day) => all[day].some((item) => item.sessionId === sessionId)) || todayKey();
all[day] ??= [];
const idx = all[today].findIndex((item) => item.sessionId === sessionId);
const idx = all[day].findIndex((item) => item.sessionId === sessionId);
if (idx >= 0) {
all[today][idx] = { sessionId, time: all[today][idx].time, messages };
all[day][idx] = { sessionId, time: all[day][idx].time, messages };
} else {
all[today].unshift({ sessionId, time: new Date().toISOString(), messages });
all[day].unshift({ sessionId, time: new Date().toISOString(), messages });
}
all[today] = all[today].slice(0, 5);
localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
return all[today];
all[day] = all[day].slice(0, 5);
while (true) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
break;
} catch (error) {
if (error.name !== "QuotaExceededError") throw error;
const oldestDay = Object.keys(all).sort().find((day) => all[day].some((item) => item.sessionId !== sessionId));
if (!oldestDay) throw error;
const idx = all[oldestDay].findLastIndex((item) => item.sessionId !== sessionId);
all[oldestDay].splice(idx, 1);
if (!all[oldestDay].length) delete all[oldestDay];
}
}
return historyItemsFrom(all);
};
const clearAllHistory = () => {