diff --git a/chat.html b/chat.html index 1a1db93..ac282e4 100644 --- a/chat.html +++ b/chat.html @@ -335,6 +335,7 @@ isBusy: false, abortController: null, userHasScrolledUp: false, + sessionId: null, models: [ { name: "gpt-4.1", chat: chatWithOpenAI, key: "OPENAI_API_KEY" }, @@ -344,6 +345,7 @@ ], async init() { + this.sessionId = +new Date(); this.historyItems = loadHistory(); setTimeout(() => this.$refs.promptInput.focus(), 0); }, @@ -370,7 +372,7 @@ handleScroll() { const el = this.$refs.msgBox; - this.userHasScrolledUp = el.scrollHeight - el.clientHeight - el.scrollTop > 50; + this.userHasScrolledUp = el.scrollHeight - el.clientHeight - el.scrollTop > 100; }, scrollToBottom() { @@ -442,7 +444,7 @@ } finally { this.isBusy = false; this.abortController = null; - this.historyItems = saveHistory([...this.messages]); + updateSessionHistory(this.sessionId, this.messages); } }, @@ -638,6 +640,7 @@ const STORAGE_KEY = "chat_conversations"; const todayKey = () => new Date().toISOString().slice(0, 10); + const loadHistory = () => { try { return JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}")[todayKey()] || []; @@ -645,11 +648,19 @@ return []; } }; - const saveHistory = (messages) => { + + const updateSessionHistory = (sessionId, messages) => { const all = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}"); const today = todayKey(); 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); localStorage.setItem(STORAGE_KEY, JSON.stringify(all)); return all[today];