fix(chat): Clear old conversations
This commit is contained in:
1 parent
74e887db04
commit
9627aaa31f
1 file changed
+46
-15
+46
-15
@@ -441,7 +441,7 @@
|
|||||||
<summary>History</summary>
|
<summary>History</summary>
|
||||||
<div class="history-list">
|
<div class="history-list">
|
||||||
<template x-if="historyItems.length === 0">
|
<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>
|
||||||
<template x-for="(item, idx) in historyItems" :key="idx">
|
<template x-for="(item, idx) in historyItems" :key="idx">
|
||||||
<div class="history-item" @click="restoreConversation(item.messages)">
|
<div class="history-item" @click="restoreConversation(item.messages)">
|
||||||
@@ -526,7 +526,7 @@
|
|||||||
pendingImages: [],
|
pendingImages: [],
|
||||||
attachment: initialAttachment,
|
attachment: initialAttachment,
|
||||||
promptText: initialPromptText,
|
promptText: initialPromptText,
|
||||||
activeModelName: "gemini-3.8-flash",
|
activeModelName: "claude-haiku-5-5",
|
||||||
historyItems: [],
|
historyItems: [],
|
||||||
isBusy: false,
|
isBusy: false,
|
||||||
abortController: null,
|
abortController: null,
|
||||||
@@ -537,7 +537,7 @@
|
|||||||
{ name: "gpt-6-luna", chat: chatWithOpenAI, key: window.env.OPENAI_API_KEY },
|
{ 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.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: "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() {
|
async init() {
|
||||||
@@ -640,7 +640,7 @@
|
|||||||
} finally {
|
} finally {
|
||||||
this.isBusy = false;
|
this.isBusy = false;
|
||||||
this.abortController = null;
|
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);
|
const idx = this.messages.indexOf(msg);
|
||||||
if (idx === -1) return;
|
if (idx === -1) return;
|
||||||
this.messages = this.messages.slice(0, idx);
|
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.promptText = msg.content;
|
||||||
this.pendingImages = msg.images ? JSON.parse(JSON.stringify(msg.images)) : [];
|
this.pendingImages = msg.images ? JSON.parse(JSON.stringify(msg.images)) : [];
|
||||||
this.$nextTick(() => this.$refs.promptInput.focus());
|
this.$nextTick(() => this.$refs.promptInput.focus());
|
||||||
@@ -977,29 +977,60 @@
|
|||||||
const STORAGE_KEY = "chat_conversations";
|
const STORAGE_KEY = "chat_conversations";
|
||||||
const todayKey = () => new Date().toISOString().slice(0, 10);
|
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 = () => {
|
const loadHistory = () => {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}")[todayKey()] || [];
|
return historyItemsFrom(readHistory());
|
||||||
} catch {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateSessionHistory = (sessionId, messages) => {
|
const updateSessionHistory = (sessionId, messages) => {
|
||||||
const all = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
|
const all = readHistory();
|
||||||
const today = todayKey();
|
const day = Object.keys(all).find((day) => all[day].some((item) => item.sessionId === sessionId)) || todayKey();
|
||||||
all[today] ??= [];
|
all[day] ??= [];
|
||||||
|
|
||||||
const idx = all[today].findIndex((item) => item.sessionId === sessionId);
|
const idx = all[day].findIndex((item) => item.sessionId === sessionId);
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
all[today][idx] = { sessionId, time: all[today][idx].time, messages };
|
all[day][idx] = { sessionId, time: all[day][idx].time, messages };
|
||||||
} else {
|
} 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);
|
all[day] = all[day].slice(0, 5);
|
||||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
|
while (true) {
|
||||||
return all[today];
|
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 = () => {
|
const clearAllHistory = () => {
|
||||||
|
|||||||
Reference in new issue
Block a user