fix(chat): Keep 1 history entry per session

This commit is contained in:
2025-12-28 21:15:29 +01:00
parent 5655b88b31
commit a06913ba63
+15 -4
View File
@@ -335,6 +335,7 @@
isBusy: false, isBusy: false,
abortController: null, abortController: null,
userHasScrolledUp: false, userHasScrolledUp: false,
sessionId: null,
models: [ models: [
{ name: "gpt-4.1", chat: chatWithOpenAI, key: "OPENAI_API_KEY" }, { name: "gpt-4.1", chat: chatWithOpenAI, key: "OPENAI_API_KEY" },
@@ -344,6 +345,7 @@
], ],
async init() { async init() {
this.sessionId = +new Date();
this.historyItems = loadHistory(); this.historyItems = loadHistory();
setTimeout(() => this.$refs.promptInput.focus(), 0); setTimeout(() => this.$refs.promptInput.focus(), 0);
}, },
@@ -370,7 +372,7 @@
handleScroll() { handleScroll() {
const el = this.$refs.msgBox; const el = this.$refs.msgBox;
this.userHasScrolledUp = el.scrollHeight - el.clientHeight - el.scrollTop > 50; this.userHasScrolledUp = el.scrollHeight - el.clientHeight - el.scrollTop > 100;
}, },
scrollToBottom() { scrollToBottom() {
@@ -442,7 +444,7 @@
} finally { } finally {
this.isBusy = false; this.isBusy = false;
this.abortController = null; this.abortController = null;
this.historyItems = saveHistory([...this.messages]); updateSessionHistory(this.sessionId, this.messages);
} }
}, },
@@ -638,6 +640,7 @@
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 loadHistory = () => { const loadHistory = () => {
try { try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}")[todayKey()] || []; return JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}")[todayKey()] || [];
@@ -645,11 +648,19 @@
return []; return [];
} }
}; };
const saveHistory = (messages) => {
const updateSessionHistory = (sessionId, messages) => {
const all = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}"); const all = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
const today = todayKey(); const today = todayKey();
all[today] ??= []; all[today] ??= [];
all[today].unshift({ time: new Date().toISOString(), messages });
const idx = all[today].findIndex((item) => item.sessionId === sessionId);
if (idx >= 0) {
all[today][idx] = { sessionId, time: all[today][idx].time, messages };
} else {
all[today].unshift({ sessionId, time: new Date().toISOString(), messages });
}
all[today] = all[today].slice(0, 5); all[today] = all[today].slice(0, 5);
localStorage.setItem(STORAGE_KEY, JSON.stringify(all)); localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
return all[today]; return all[today];