// serve/web/chats-ui.js - the chat history on the page: the saved-chats list, and edit / regenerate / go back / branch / undo. // One design for the proposals in PRs #1787 (list), #2615 (edit, regenerate, undo) and issue #3823 (rewind asks: here or a branch); // the pure part is serve/web/history.js, the browser store is serve/web/chatstore.js. // // Both are opt-in (Chat settings > Chats) and off by default: off, the page keeps the one chat it always had, with "New chat" // clearing it (and an Undo). Nothing here goes to the server. // // This file is loaded before app.js and uses its globals when its functions run: $, esc, icon, node, toast, store, settings, // messages, attachments, busy, pending, setBusy, renderChat, renderAttachments, autosize, generateReply, releasePending. // app.js calls initChats() once, from start(). "use strict"; const H = StrataHistory; const untitled = () => tr("chat.untitled", "New chat"); const node = (tag, cls, text) => { const n = document.createElement(tag); if (cls) n.className = cls; if (text != null) n.textContent = text; return n; }; // ------------------------------------------------------------------ state let listOn = false; // the saved-chats list is on let chats = [], chat = null, chatId = ""; let sidebarQuery = "", sidebarOpen = true; let undoState = null; // one change, in memory until a new question, another chat or a reload let panel = null; // the open "Rewind to here" panel or answer editor: {kind, index, el} const chatStore = StrataChatStore.createChatStore({ onProblem(kind) { toast("warn", tr("store.title", "Chat list not saved"), kind === "memory" ? tr("store.memory", "This browser will not keep the chat list. It stays in memory until you close the page.") : kind === "quota" ? tr("store.quota", "This browser cannot keep more. The chats on the screen are still there.") : tr("store.failed", "The chat list could not be saved. The chats on the screen are still there."), 6000); }, }); // the open chat's messages are the page's `messages`; with the list on they are also the chat's, the same array function setMessages(next) { messages = next; if (chat) chat.messages = next; } function saveChat() { if (!listOn) { store.set("chat", H.cleanMessages(messages)); return; } // off: one chat, as before chat.time = Date.now(); chatStore.put(H.cleanChat(chat)); renderSidebar(); } function stillWriting() { toast("warn", tr("busy.stillWriting", "Still writing"), tr("busy.stopFirst", "Stop the answer first.")); } // ------------------------------------------------------------------ the saved-chats list (#1787) // Turning the list on: the chat on the screen becomes the first record. Only when the switch is flipped in this visit: on a page // load the list is the list, so a fresh chat stays empty and the single chat of before ("strata.chat") is read once, when the // store is empty. async function openChats(fromScreen) { await chatStore.start(); chats = await chatStore.all(); if (!chats.length) { const old = fromScreen ? messages : store.get("chat", []); if (old.length) { const c = {...H.newChat(), messages: old, time: old[old.length - 1].time || Date.now()}; chats = [c]; chatStore.put(H.cleanChat(c)); // the migrated chat is a record like the others } } chat = chats.find((c) => c.id === store.get("chatId", "")) || chats[0] || H.newChat(); if (!chats.includes(chat)) chats.push(chat); chatId = chat.id; setMessages(chat.messages); } async function setChatList(on, fromScreen) { listOn = on; $("sidebar").hidden = !on; $("new-btn").hidden = on; // the list has its own New chat if (!on) { // off writes the single chat, so a reload shows the chat you were in if (chat) messages = chat.messages; chat = null; store.set("chat", H.cleanMessages(messages)); renderChat(); return; } await openChats(fromScreen); renderChat(); } function groupName(key) { return {today: tr("group.today", "Today"), yesterday: tr("group.yesterday", "Yesterday"), week: tr("group.week", "Previous 7 days"), month: tr("group.month", "Previous 30 days"), older: tr("group.older", "Older")}[key]; } function chatItem(c) { const t = H.chatTitle(c, untitled()); const row = node("div", `chat-item${c.id === chatId ? " on" : ""}`); row.dataset.id = c.id; row.innerHTML = `` + `` + `` + `` + ``; return row; } function renderSidebar() { if (!listOn) return; // the search reads every chat's message text, so the whole store is in memory after the first load const list = H.filterChats(chats, sidebarQuery, untitled()); const box = $("sidebar-list"); box.innerHTML = ""; if (!list.length) { box.appendChild(node("p", "muted small sidebar-empty", sidebarQuery.trim() ? tr("sidebar.noMatch", "No chat matches") : tr("sidebar.none", "No chats yet"))); } else { let group = ""; for (const c of list) { const g = H.groupKey(c.time); if (g !== group) { group = g; box.appendChild(node("p", "sidebar-group", groupName(g))); } box.appendChild(chatItem(c)); } } $("sidebar-foot").textContent = tr("sidebar.foot", {one: "{n} chat · kept in this browser", other: "{n} chats · kept in this browser"}, {n: chats.length}); } function showChat(c) { clearUndo(); chat = c; chatId = c.id; messages = c.messages; store.set("chatId", chatId); chatStore.put(H.cleanChat(chat)); // renderChat renders the list, so the write here does not render it again renderChat(); } function openChat(id) { if (busy) { stillWriting(); return; } const c = chats.find((x) => x.id === id); if (!c || c === chat) return; releasePending(); closePanel(); showChat(c); } function startChat() { if (busy) { stillWriting(); return; } if (!messages.length && !chat.parent) { $("input").focus(); return; } // already a fresh chat releasePending(); closePanel(); const prev = chatId, c = H.newChat(); chats.push(c); showChat(c); $("input").focus(); toast("info", tr("chat.newToast", "New chat"), tr("chat.newToastText", "The one you were in is in the list on the left."), 6000, {label: tr("undo", "Undo"), run: () => openChat(prev)}); } function deleteChat(c) { if (busy && c.id === chatId) { stillWriting(); return; } const i = chats.indexOf(c), wasOpen = c.id === chatId; if (i < 0) return; // the list only ever passes a chat it holds chats.splice(i, 1); chatStore.remove(c.id); // the record; the Undo puts it back if (wasOpen) { releasePending(); closePanel(); const next = chats[0] || H.newChat(); if (!chats.includes(next)) chats.push(next); showChat(next); } else renderSidebar(); toast("info", tr("chat.deleted", "Chat deleted"), H.chatTitle(c, untitled()), 6000, {label: tr("undo", "Undo"), run: () => { if (chats.includes(c)) return; // already put back chats.splice(Math.min(i, chats.length), 0, c); // the display is sorted by time, so only the index matters if (wasOpen) showChat(c); else { chatStore.put(H.cleanChat(c)); renderSidebar(); } }}); } // rename: the row turns into a field; Enter or a click outside keeps the name, Escape gives it up function renameChat(row, c) { const title = row.querySelector(".chat-item__title"); const input = node("input", "st-input chat-item__input"); input.value = H.chatTitle(c, untitled()); input.setAttribute("aria-label", tr("sidebar.nameFor", "Name for this chat")); title.replaceWith(input); input.focus(); input.select(); let done = false; const finish = (keep) => { if (done) return; done = true; if (keep) { c.title = input.value.trim(); chatStore.put(H.cleanChat(c)); } renderSidebar(); }; input.addEventListener("keydown", (e) => { if (e.key === "Enter") { e.preventDefault(); finish(true); } else if (e.key === "Escape") { e.preventDefault(); finish(false); } }); input.addEventListener("blur", () => finish(true)); } function setSidebar(open, save) { sidebarOpen = open; $("sidebar").dataset.open = String(open); const t = open ? tr("sidebar.collapse", "Collapse the chat list") : tr("sidebar.expand", "Open the chat list"); $("sidebar-toggle").title = t; $("sidebar-toggle").setAttribute("aria-label", t); $("sidebar-toggle").setAttribute("aria-expanded", String(open)); if (save) store.set("sidebar", open); } // a chat that was branched from another says so, and links back while the original is still in the list function renderParent() { const bar = $("chat-parent"); const p = listOn && chat ? chat.parent : null; bar.hidden = !p; if (!p) return; const alive = chats.some((c) => c.id === p.id); bar.innerHTML = ""; bar.appendChild(node("span", "muted", tr("branch.from", "Branched from “{title}”", {title: p.title}))); if (alive) { const b = node("button", "st-btn st-btn--secondary", tr("branch.open", "Open the original")); b.type = "button"; b.onclick = () => openChat(p.id); bar.appendChild(b); } } // ------------------------------------------------------------------ edit, regenerate, go back (#2615, #3823) // Every change keeps the messages it replaced for one Undo: a new question, another chat or a reload ends it. The controls // are off while an answer is written (stop it first), while something is open, and while a message waits to be sent. const historyLocked = () => !!busy || !!panel || !!pending; function clearUndo() { undoState = null; renderUndoBar(); } function renderUndoBar() { let el = $("history-undo"); if (!settings.chatEdit || !undoState) { if (el) el.remove(); return; } if (!el) { el = node("div", "history-undo"); el.id = "history-undo"; el.setAttribute("role", "status"); el.innerHTML = ""; el.querySelector("button").onclick = undoHistory; $("composer").before(el); } el.querySelector("span").textContent = undoState.text; el.querySelector("button").textContent = tr("undo", "Undo"); el.querySelector("button").disabled = historyLocked(); } // replace the messages with `next`; text says what happened, for the Undo bar function commit(next, text) { undoState = {messages, text, inserted: undefined, draftBefore: null}; setMessages(next); saveChat(); renderChat(); return undoState; } function undoHistory() { if (!settings.chatEdit || !undoState || historyLocked()) return false; const u = undoState; setMessages(u.messages); if (u.inserted !== undefined && $("input").value === u.inserted && u.draftBefore) { // the box still holds what the rewind put there $("input").value = u.draftBefore.text; attachments = u.draftBefore.attachments; renderAttachments(); autosize(); } undoState = null; saveChat(); renderChat(); $("input").focus(); return true; } const hiddenText = (n) => tr("hist.hidden", {one: "{n} message hidden", other: "{n} messages hidden"}, {n}); function msgActions(m, i) { if (!settings.chatEdit) return null; if (busy && busy.msg === m) return null; // the answer being written has no buttons until it is done const row = node("div", "history-actions"); const lock = historyLocked(); const add = (label, title, run) => { const b = node("button", "st-btn st-btn--secondary", label); b.type = "button"; b.title = title; b.disabled = lock; b.onclick = run; row.appendChild(b); }; const last = i === messages.length - 1; if (m.role === "assistant" || last) { add(tr("hist.regenerate", "Regenerate"), tr("hist.regenerateTitle", "Ask for a new answer to this question. Tools may run again."), () => regenerateAt(i)); } if (m.role === "assistant") add(tr("hist.editAnswer", "Edit answer"), tr("hist.editAnswerTitle", "Correct the answer's text. No request is sent."), () => editAnswerAt(i)); add(tr("hist.rewind", "Rewind to here"), tr("hist.rewindTitle", "Go back to this point in the chat"), () => openRewind(i)); return row; } // true when a history change may start now (and says why not when it may not) function historyReady() { if (!settings.chatEdit) return false; if (busy) { stillWriting(); return false; } if (panel) { toast("warn", tr("hist.finishFirst", "Finish what is open first"), tr("hist.finishFirstText", "Save it or press Cancel.")); return false; } if (pending) { toast("warn", tr("hist.pendingFirst", "A message is waiting"), tr("hist.pendingFirstText", "Send it or take it back first.")); return false; } return true; } // the action rows under the messages follow the switch, without replacing any message (an answer may be streaming into one) function syncActions() { for (const el of $("chat").querySelectorAll(".st-msg")) { const i = +el.dataset.i; const old = el.querySelector(".history-actions"); if (old) old.remove(); const row = messages[i] ? msgActions(messages[i], i) : null; if (row) el.appendChild(row); } } function closePanel() { if (!panel) return; const {el} = panel; const open = el.querySelector(".history-panel, .history-editor"); if (open) open.remove(); delete el.dataset.editing; panel = null; setBusy(!!busy); } function regenerateAt(i) { if (!historyReady()) return false; const plan = H.regenerate(messages, i); if (!plan) return false; if (H.lostAttachments(messages[plan.keep.length - 1])) { toast("warn", tr("hist.lostTitle", "Attachments need to be added again"), tr("hist.lostText", "This browser kept their names, not their contents. Start a new chat and attach them again."), 7000); return false; } commit(plan.keep, tr("hist.replaced", "The answer was replaced")); return generateReply(); } // edit an answer: its text, in place. The old thinking and tool calls go, and so do the messages after it. function editAnswerAt(i) { if (!historyReady() || !messages[i]) return false; const m = messages[i], el = $("chat").querySelector(`[data-i="${i}"]`); const later = messages.length - i - 1; const box = node("div", "history-editor"); box.innerHTML = "
" + "