Five overlapping proposals, one set of controls (Chat settings > Chats, all off by default; off, the page is the page from before: one chat, New chat clears it with an Undo, Send is off while an answer is written, no new request field). - Saved chats list (#1787): the chats kept in this browser (IndexedDB, one record per chat, names of attachments only), a list with day groups, search over titles and text, rename, delete with Undo, collapse; a private window or full store keeps the list in memory and says so once. Nothing goes to the server. The list has the New chat button, so the composer's one is hidden while it is on. - Edit and rewind messages (#2615 + #3823): under each message, Regenerate (a new answer to the same question), Edit answer (its text only: thinking and tool calls go, no request) and Rewind to here. Rewind asks, as #3823 proposes: "Edit and send again here" hides this message and the ones after it, puts the question and its attachments in the box and keeps one Undo bar ("2 messages hidden"); "Branch from here" starts a new saved chat with the messages before it and leaves this one alone. A branch keeps a title that says where it came from ("Branch of ...", also at the very first message) and links back. One control does what #2615 had as Edit on a question and as Remove from here. - Send while answering (#4004, for the web page): "Wait" (default, as before), "Send after" (the message waits and goes when the answer is done) or "Interrupt" (the answer stops where it is, what is written stays and is marked interrupted, the message goes at once; a running tool finishes first). One message waits; Take back returns it to the box; Stop keeps it for you to send. The pure logic is serve/web/history.js, the store serve/web/chatstore.js, the page's part serve/web/chats-ui.js. Tests: serve/test_web_history.js (Node, with the IndexedDB stub and cases of #1787), serve/test_web_history.py, and 17 browser tests (tools/webtest/browser_test.mjs). Screenshots: tools/webtest/shots.mjs. Also: the Sampling drawer is now Chat settings (its button says so), a wide window lets a message use the column (#3176 widened only the column). Ported by hand and merged: #1787 by Scotty Hudson, #2615 by Joyer Huang (uppet), issues #3823 and #4004 by xvcezrqpbxtpzblrt (their proposals, for the desktop app, carried to the web page). Co-authored-by: Scotty Hudson <14501954+Scotty-Hudson@users.noreply.github.com> Co-authored-by: Joyer Huang <110209+uppet@users.noreply.github.com> Co-authored-by: xvcezrqpbxtpzblrt <333138892+xvcezrqpbxtpzblrt@users.noreply.github.com>
67 lines
3.3 KiB
JavaScript
67 lines
3.3 KiB
JavaScript
// serve/web/chatstore.js - the saved chats, kept in this browser (IndexedDB, database "strata", store "chats", one record per
|
|
// chat). Ported from PR #1787 by Scotty-Hudson. A chat can be several megabytes and localStorage holds about 5 MB for the whole
|
|
// page, so the chats live here and only small keys (which chat is open, the settings) stay in localStorage. Nothing goes to the
|
|
// server: the server has no endpoint for chats and never sees a title.
|
|
//
|
|
// Where the browser will not keep it (a private window, a blocked or full store) the list lives in memory for the visit, and
|
|
// onProblem(kind) tells the page once: "memory" (no store), "quota" (full), "failed" (a write the browser refused).
|
|
"use strict";
|
|
(function (root) {
|
|
function createChatStore({indexedDB: idb = root.indexedDB, onProblem = () => {}} = {}) {
|
|
let db = null, told = false;
|
|
const problem = (kind) => { if (!told) { told = true; onProblem(kind); } };
|
|
|
|
function open() {
|
|
return new Promise((resolve) => {
|
|
let r;
|
|
try { r = idb.open("strata", 1); } catch (e) { resolve(null); return; } // private mode: memory only
|
|
r.onupgradeneeded = () => {
|
|
const d = r.result;
|
|
if (!d.objectStoreNames.contains("chats")) d.createObjectStore("chats", {keyPath: "id"});
|
|
};
|
|
// a database already at version 1 does not run onupgradeneeded, so the store may not be there
|
|
r.onsuccess = () => resolve(r.result.objectStoreNames.contains("chats") ? r.result : null);
|
|
r.onerror = () => resolve(null);
|
|
r.onblocked = () => resolve(null); // another tab holds an older version of the database
|
|
});
|
|
}
|
|
// true when the store is there; false: memory only (and the page has been told)
|
|
async function start() {
|
|
db = await open();
|
|
if (!db) problem("memory");
|
|
return !!db;
|
|
}
|
|
function all() {
|
|
return new Promise((resolve) => {
|
|
if (!db) { resolve([]); return; }
|
|
// the result is on the request, not on the store: reading it at oncomplete gives nothing
|
|
const req = db.transaction("chats", "readonly").objectStore("chats").getAll();
|
|
req.onsuccess = () => resolve(req.result || []);
|
|
req.onerror = () => resolve([]);
|
|
req.transaction.onabort = () => resolve([]); // an aborted read fires abort, not error: do not hang
|
|
});
|
|
}
|
|
// a request that fails because the storage is full aborts the transaction, so it fires abort rather than error
|
|
function watch(t) {
|
|
const warn = () => problem(t.error && t.error.name === "QuotaExceededError" ? "quota" : "failed");
|
|
t.onerror = warn; t.onabort = warn;
|
|
}
|
|
function put(chat) { // one record per chat: not the whole list for one message
|
|
if (!db) return;
|
|
const t = db.transaction("chats", "readwrite");
|
|
t.objectStore("chats").put(chat);
|
|
watch(t);
|
|
}
|
|
function remove(id) {
|
|
if (!db) return;
|
|
const t = db.transaction("chats", "readwrite");
|
|
t.objectStore("chats").delete(id);
|
|
watch(t);
|
|
}
|
|
return {start, all, put, remove, get usable() { return !!db; }};
|
|
}
|
|
const api = {createChatStore};
|
|
root.StrataChatStore = api;
|
|
if (typeof module !== "undefined" && module.exports) module.exports = api;
|
|
})(typeof globalThis !== "undefined" ? globalThis : this);
|