Open source CapCut alternative.
TypeScript
fix(cut): keep queued chat messages in IndexedDB
David Le785d4f0a7af1
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsxindex aa6f6137..d7e00719 100644--- a/site/src/cut/components/AiPanel.tsx+++ b/site/src/cut/components/AiPanel.tsx@@ -60,8 +60,8 @@ import {deleteStoredThread,isStoredThread,loadProjectThreads,- queueKey,readActiveChat,+ readQueuedRows,readRawThreads,recordThread,seenRepliesKey,@@ -69,6 +69,7 @@ import {threadSaveFailure,threadsLoaded,writeActiveChat,+ writeQueuedRows,writeRawThreads,subscribeChatThreads,type ThreadRecorder,@@ -398,8 +399,7 @@ function writeThreads(projectId: string, list: ChatThread[]) {if (!keptIds.has(t.id)) {useGenScene.getState().killThread(t.id);deleteCloudThread(projectId, t.id);- if (typeof window !== "undefined")- localStorage.removeItem(queueKey(projectId, t.id));+ writeQueuedRows(projectId, t.id, []);}}writeRawThreads(projectId, slimForStorage(kept));@@ -417,9 +417,7 @@ function readStoredQueue(): QueuedMessage[] {if (typeof window === "undefined") return [];try {- const raw = localStorage.getItem(queueKey(projectId, threadId));- if (!raw) return [];- return (JSON.parse(raw) as QueuedMessage[])+ return (readQueuedRows(projectId, threadId) as QueuedMessage[]).filter((m) =>m?.status === "queued" &&@@ -697,7 +695,7 @@ export function AiPanel({// into Media/Library stays.deleteChatAssets(id);deleteCloudThread(projectId, id);- localStorage.removeItem(queueKey(projectId, id));+ writeQueuedRows(projectId, id, []);// Its work dies with it too: a scene run the thread owned aborts and its// plan clears — nothing keeps running behind a conversation the user killed.useGenScene.getState().killThread(id);@@ -2067,14 +2065,7 @@ function ChatSession({// its slot.useEffect(() => {if (readOnly) return;- const waiting = queue.filter((m) => m.status === "queued");- if (waiting.length === 0)- localStorage.removeItem(queueKey(projectId, threadId));- else- localStorage.setItem(- queueKey(projectId, threadId),- JSON.stringify(waiting),- );+ writeQueuedRows(projectId, threadId, queue.filter((m) => m.status === "queued"));}, [queue, projectId, threadId, readOnly]);// An errored turn holds the queue for review — the drain only fires ondiff --git a/site/src/cut/lib/cache.ts b/site/src/cut/lib/cache.tsindex 51621a0d..a9aeeb3c 100644--- a/site/src/cut/lib/cache.ts+++ b/site/src/cut/lib/cache.ts@@ -18,12 +18,15 @@ import { currentEngineUser } from "./api";const DB_NAME = "cut";// v1 held only the per-project view state; v2 adds this store beside it; v3-// adds the chat history (chatThreads.ts), which outgrew localStorage.-const DB_VERSION = 3;+// adds the chat history (chatThreads.ts), which outgrew localStorage; v4 the+// composer's waiting messages, whose inline frame grabs outgrew it too.+const DB_VERSION = 4;const UI_STORE = "project-ui";const SNAPSHOT_STORE = "snapshots";/** Each project's chat thread list, keyed by project id. */export const CHAT_STORE = "chat-threads";+/** Each project's waiting composer messages, keyed by project id. */+export const QUEUE_STORE = "chat-queues";/** A snapshot older than this is dropped on read — too old to be a useful* head start, and old enough that painting it would be a lie. */@@ -43,6 +46,7 @@ export function openCutDb(): Promise<IDBDatabase> {if (!names.contains(UI_STORE)) req.result.createObjectStore(UI_STORE);if (!names.contains(SNAPSHOT_STORE)) req.result.createObjectStore(SNAPSHOT_STORE);if (!names.contains(CHAT_STORE)) req.result.createObjectStore(CHAT_STORE);+ if (!names.contains(QUEUE_STORE)) req.result.createObjectStore(QUEUE_STORE);};req.onsuccess = () => {// A newer tab upgrading the database waits on every open connection;diff --git a/site/src/cut/lib/chatThreads.test.ts b/site/src/cut/lib/chatThreads.test.tsindex 5b8e9a62..8294abd8 100644--- a/site/src/cut/lib/chatThreads.test.ts+++ b/site/src/cut/lib/chatThreads.test.ts@@ -11,6 +11,7 @@ import {mergeThreads,readActiveChat,readProjectThreads,+ readQueuedRows,readThreadIds,recordThread,subscribeThreadSaveFailure,@@ -21,6 +22,7 @@ import {useThreadStore,writeActiveChat,writeProjectThreads,+ writeQueuedRows,writeRawThreads,type ThreadStore,} from "./chatThreads";@@ -45,6 +47,7 @@ beforeEach(() => {* that refuses a write past it the way the browser does. */function memoryStore(quota = Infinity) {const lists = new Map<string, unknown[]>();+ const queues = new Map<string, Record<string, unknown[]>>();const size = () => [...lists.values()].reduce((n, list) => n + JSON.stringify(list).length, 0);const threadStore: ThreadStore = {read: async (projectId) => lists.get(projectId),@@ -56,10 +59,14 @@ function memoryStore(quota = Infinity) {throw new DOMException("The quota has been exceeded.", "QuotaExceededError");}},- remove: async (projectId) => { lists.delete(projectId); },+ remove: async (projectId) => { lists.delete(projectId); queues.delete(projectId); },projects: async () => [...lists.keys()],+ readQueues: async (projectId) => queues.get(projectId),+ writeQueues: async (projectId, record) => {+ if (Object.keys(record).length) queues.set(projectId, record); else queues.delete(projectId);+ },};- return { threadStore, lists, size };+ return { threadStore, lists, queues, size };}const big = (id: string, updatedAt: number, chars: number) => ({ id, updatedAt, messages: [{ id: "m", role: "user", parts: [{ type: "text", text: "x".repeat(chars) }] }] });@@ -247,6 +254,79 @@ describe("where threads are kept", () => {expect([...store.keys()]).toEqual(["cut-ai-queue-other-a"]);});+ test("waiting rows with frame grabs past localStorage's room are kept in the thread store", async () => {+ const { threadStore, queues } = memoryStore();+ // A browser's localStorage: about 5 MB for the whole origin.+ const local = (globalThis as unknown as { localStorage: Storage }).localStorage;+ const setItem = local.setItem;+ local.setItem = (k: string, v: string) => {+ const used = [...store.entries()].reduce((n, [key, val]) => n + (key === k ? 0 : key.length + val.length), 0);+ if (used + k.length + v.length > 5_000_000) throw new DOMException("The quota has been exceeded.", "QuotaExceededError");+ setItem(k, v);+ };+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ const grab = (id: string) => ({ id, text: "use this frame", status: "queued", attachments: [{ scope: "file", id, kind: "image", url: `data:image/jpeg;base64,${"x".repeat(3_000_000)}` }] });+ expect(() => {+ writeQueuedRows("p", "a", [grab("r1")]);+ writeQueuedRows("p", "b", [grab("r2")]);+ }).not.toThrow();+ await flushThreadSaves();+ expect([...store.keys()].filter((k) => k.startsWith("cut-ai-queue-"))).toEqual([]);+ expect(Object.keys(queues.get("p") ?? {}).sort()).toEqual(["a", "b"]);+ // A reload reads them back.+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ expect((readQueuedRows("p", "a") as { id: string }[]).map((m) => m.id)).toEqual(["r1"]);+ // An emptied queue clears its slot.+ writeQueuedRows("p", "a", []);+ await flushThreadSaves();+ expect(Object.keys(queues.get("p") ?? {})).toEqual(["b"]);+ expect(readQueuedRows("p", "a")).toEqual([]);+ });++ test("localStorage queues move to the store on first read, freeing the space", async () => {+ const { threadStore, lists, queues } = memoryStore();+ lists.set("p", [{ id: "a" }]);+ lists.set("p-2", [{ id: "c" }]);+ const row = (id: string) => ({ id, text: id, status: "queued", attachments: [] });+ store.set("cut-ai-queue-p-a", JSON.stringify([row("r1")]));+ store.set("cut-ai-queue-p-2-c", JSON.stringify([row("r2")]));+ // A queue whose project has no history in this browser waits in no thread.+ store.set("cut-ai-queue-gone-b", JSON.stringify([row("r3")]));+ store.set("cut-ai-model", "claude");+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ expect((readQueuedRows("p", "a") as { id: string }[]).map((m) => m.id)).toEqual(["r1"]);+ // Every project's queue moves, each to the project whose id it names.+ expect(Object.keys(queues.get("p") ?? {})).toEqual(["a"]);+ expect(Object.keys(queues.get("p-2") ?? {})).toEqual(["c"]);+ expect([...store.keys()]).toEqual(["cut-ai-model"]);+ });++ test("a deleted project takes its waiting rows from the store", async () => {+ const { threadStore, queues } = memoryStore();+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ writeProjectThreads("p", [{ id: "a", updatedAt: 1 }]);+ writeQueuedRows("p", "a", [{ id: "r", text: "t", status: "queued", attachments: [] }]);+ await flushThreadSaves();+ expect(queues.has("p")).toBe(true);+ clearProjectThreads("p");+ await flushThreadSaves();+ expect(queues.has("p")).toBe(false);+ expect(readQueuedRows("p", "a")).toEqual([]);+ });++ test("without a thread store, a full localStorage drops the waiting rows without throwing", () => {+ const local = (globalThis as unknown as { localStorage: Storage }).localStorage;+ local.setItem = () => { throw new DOMException("The quota has been exceeded.", "QuotaExceededError"); };+ store.set("cut-ai-queue-p-a", JSON.stringify([{ id: "old" }]));+ expect(() => writeQueuedRows("p", "a", [{ id: "new" }])).not.toThrow();+ // The stale copy goes too: a reload never brings back rows that already ran.+ expect(readQueuedRows("p", "a")).toEqual([]);+ });+test("threads of projects the listings no longer hold go once the project is confirmed gone", async () => {const { threadStore, lists } = memoryStore();lists.set("kept", [{ id: "a" }]);diff --git a/site/src/cut/lib/chatThreads.ts b/site/src/cut/lib/chatThreads.tsindex ca24cdef..42c38cc1 100644--- a/site/src/cut/lib/chatThreads.ts+++ b/site/src/cut/lib/chatThreads.ts@@ -61,18 +61,30 @@ export interface ThreadStore {read(projectId: string): Promise<unknown[] | undefined>;/** Rejects when the browser refuses the write (QuotaExceededError when full). */write(projectId: string, list: unknown[]): Promise<void>;+ /** Drops the project's threads and its waiting messages. */remove(projectId: string): Promise<void>;projects(): Promise<string[]>;+ /** The project's waiting composer messages, thread id → rows. */+ readQueues(projectId: string): Promise<Record<string, unknown[]> | undefined>;+ /** Replace them; an empty record clears the project's slot. */+ writeQueues(projectId: string, queues: Record<string, unknown[]>): Promise<void>;}-async function chatStore<T>(mode: IDBTransactionMode, op: (store: IDBObjectStore) => IDBRequest<T>): Promise<T> {+type ChatStoreName = "threads" | "queues";++async function chatStore<T>(+ mode: IDBTransactionMode,+ op: (store: IDBObjectStore) => IDBRequest<T>,+ name: ChatStoreName = "threads",+): Promise<T> {// Loaded on first use, so this module stays a leaf the generate store and// the server-free tests can import.- const { openCutDb, CHAT_STORE } = await import("./cache");+ const { openCutDb, CHAT_STORE, QUEUE_STORE } = await import("./cache");const conn = await openCutDb();+ const storeName = name === "queues" ? QUEUE_STORE : CHAT_STORE;return new Promise<T>((resolve, reject) => {- const tx = conn.transaction(CHAT_STORE, mode);- const req = op(tx.objectStore(CHAT_STORE));+ const tx = conn.transaction(storeName, mode);+ const req = op(tx.objectStore(storeName));tx.oncomplete = () => resolve(req.result);tx.onerror = tx.onabort = () => reject(tx.error ?? req.error ?? new Error("chat store transaction failed"));});@@ -81,8 +93,21 @@ async function chatStore<T>(mode: IDBTransactionMode, op: (store: IDBObjectStoreconst indexedDbStore: ThreadStore = {read: (projectId) => chatStore("readonly", (s) => s.get(projectId)).then((v) => (Array.isArray(v) ? v : undefined)),write: (projectId, list) => chatStore("readwrite", (s) => s.put(list, projectId)).then(() => undefined),- remove: (projectId) => chatStore("readwrite", (s) => s.delete(projectId)).then(() => undefined),+ remove: (projectId) =>+ Promise.all([+ chatStore("readwrite", (s) => s.delete(projectId)),+ chatStore("readwrite", (s) => s.delete(projectId), "queues"),+ ]).then(() => undefined),projects: () => chatStore("readonly", (s) => s.getAllKeys()).then((keys) => keys.map(String)),+ readQueues: (projectId) =>+ chatStore("readonly", (s) => s.get(projectId), "queues").then((v) =>+ v && typeof v === "object" ? (v as Record<string, unknown[]>) : undefined,+ ),+ writeQueues: (projectId, queues) =>+ (Object.keys(queues).length+ ? chatStore("readwrite", (s) => s.put(queues, projectId), "queues")+ : chatStore("readwrite", (s) => s.delete(projectId), "queues")+ ).then(() => undefined),};// The thread store in use: undefined until first needed, null when there is@@ -99,6 +124,9 @@ const cache = new Map<string, unknown[]>();const loads = new Map<string, Promise<void>>();const loaded = new Set<string>();const dirty = new Set<string>();+// Each loaded project's waiting composer messages, thread id → rows, read+// synchronously by a thread as it mounts (after its project's load).+const queues = new Map<string, Record<string, unknown[]>>();let migration: Promise<void> | null = null;let queue: Promise<void> = Promise.resolve();let flushScheduled = false;@@ -114,6 +142,7 @@ export function useThreadStore(store: ThreadStore | null): void {loads.clear();loaded.clear();dirty.clear();+ queues.clear();migration = null;queue = Promise.resolve();flushScheduled = false;@@ -159,10 +188,73 @@ function migrateLegacyThreads(store: ThreadStore): Promise<void> {console.warn(`[chat] couldn't move project ${projectId}'s chat history out of localStorage`, err);}}+ await migrateLegacyQueues(store);})();return migration;}+// "cut-ai-queue-": the key with no project, thread, or the hyphen joining them.+const legacyQueuePrefix = queueKey("", "").slice(0, -1);++function readLocalRows(key: string): unknown[] {+ try {+ const v = JSON.parse(localStorage.getItem(key) ?? "[]") as unknown;+ return Array.isArray(v) ? v : [];+ } catch {+ return [];+ }+}++function localKeys(prefix: string): string[] {+ const keys: string[] = [];+ try {+ for (let i = 0; i < localStorage.length; i++) {+ const key = localStorage.key(i);+ if (key?.startsWith(prefix)) keys.push(key);+ }+ } catch {+ return [];+ }+ return keys;+}++/** Move every waiting queue out of localStorage, where a few inline frame+ * grabs filled the origin's 5 MB. A key names its project and thread joined+ * by a hyphen both may contain, so it goes to the longest project id with+ * history here that prefixes it (`p-2-c` is project `p-2`, thread `c`); one+ * no project claims has no thread to wait in and is dropped. A project whose+ * write fails keeps its keys for the next try. */+async function migrateLegacyQueues(store: ThreadStore): Promise<void> {+ const keys = localKeys(legacyQueuePrefix);+ if (!keys.length) return;+ const projects = (await store.projects()).sort((a, b) => b.length - a.length);+ const moving = new Map<string, Record<string, unknown[]>>();+ const movingKeys = new Map<string, string[]>();+ for (const key of keys) {+ const projectId = projects.find((id) => key.startsWith(queueKey(id, "")));+ if (projectId === undefined) {+ removeLocal(key);+ continue;+ }+ const rows = readLocalRows(key);+ const record = moving.get(projectId) ?? {};+ if (rows.length) record[key.slice(queueKey(projectId, "").length)] = rows;+ moving.set(projectId, record);+ movingKeys.set(projectId, [...(movingKeys.get(projectId) ?? []), key]);+ }++ // Rows already in the store are newer than any localStorage copy.+ for (const [projectId, record] of moving) {+ try {+ const stored = (await store.readQueues(projectId)) ?? {};+ await store.writeQueues(projectId, { ...record, ...stored });+ if (backing === store) for (const key of movingKeys.get(projectId) ?? []) removeLocal(key);+ } catch (err) {+ console.warn(`[chat] couldn't move project ${projectId}'s waiting messages out of localStorage`, err);+ }+ }+}+function withTimeout<T>(work: Promise<T>, ms: number): Promise<T> {return new Promise<T>((resolve, reject) => {const timer = setTimeout(() => reject(new Error(`chat store didn't answer in ${ms}ms`)), ms);@@ -181,7 +273,10 @@ export function loadProjectThreads(projectId: string): Promise<void> {(async () => {await migrateLegacyThreads(store);const stored = (await store.read(projectId)) ?? [];+ const storedQueues = (await store.readQueues(projectId)) ?? {};if (backing !== store) return;+ // Rows written before the load finished are the newest.+ queues.set(projectId, { ...storedQueues, ...queues.get(projectId) });// Anything written before the load finished joins what was stored, as// does a localStorage copy the migration couldn't move.const early = [...readLegacy(projectId), ...(cache.get(projectId) ?? [])];@@ -241,6 +336,56 @@ export function writeRawThreads(projectId: string, list: unknown[]): void {schedule(projectId);}+/** A thread's waiting composer messages exactly as written. The AI panel owns+ * the rows; this module owns where they live. */+export function readQueuedRows(projectId: string, threadId: string): unknown[] {+ if (memory) return [];+ if (!threadStore()) return readLocalRows(queueKey(projectId, threadId));+ return queues.get(projectId)?.[threadId] ?? readLocalRows(queueKey(projectId, threadId));+}++/** Replace a thread's waiting messages; an empty list clears its slot. Never+ * throws: a queue that can't be saved costs its rows on a reload. */+export function writeQueuedRows(projectId: string, threadId: string, rows: unknown[]): void {+ if (memory) return;+ const key = queueKey(projectId, threadId);+ const store = threadStore();++ // No thread store: localStorage, and a refused write drops the stale copy+ // so a reload never brings back rows that already ran.+ if (!store) {+ if (!rows.length) {+ removeLocal(key);+ return;+ }+ try {+ localStorage.setItem(key, JSON.stringify(rows));+ } catch (err) {+ removeLocal(key);+ console.warn("[chat] couldn't save the waiting messages in this browser", err);+ }+ return;+ }++ // An empty queue that holds nothing is the common render; it writes nothing.+ const record = queues.get(projectId) ?? {};+ if (!rows.length && !(threadId in record)) return;+ const next = { ...record };+ if (rows.length) next[threadId] = rows;+ else delete next[threadId];+ queues.set(projectId, next);+ removeLocal(key);+ void enqueue(async () => {+ if (!loaded.has(projectId)) await loadProjectThreads(projectId);+ if (backing !== store) return;+ try {+ await store.writeQueues(projectId, queues.get(projectId) ?? {});+ } catch (err) {+ console.warn("[chat] couldn't save the waiting messages in this browser", err);+ }+ });+}+/** Run store work in order: a removal never races a save of the same project. */function enqueue(work: () => Promise<void>): Promise<void> {queue = queue.then(work, work);@@ -444,6 +589,7 @@ export function clearProjectThreads(projectId: string): void {return;}const store = threadStore();+ queues.delete(projectId);if (store) {cache.set(projectId, []);loaded.add(projectId);diff --git a/site/src/cut/lib/threadSweep.test.ts b/site/src/cut/lib/threadSweep.test.tsindex f1003e29..a370ecf3 100644--- a/site/src/cut/lib/threadSweep.test.ts+++ b/site/src/cut/lib/threadSweep.test.ts@@ -23,6 +23,8 @@ function heldThreads(...projectIds: string[]) {write: async (projectId, list) => void lists.set(projectId, list),remove: async (projectId) => void lists.delete(projectId),projects: async () => [...lists.keys()],+ readQueues: async () => undefined,+ writeQueues: async () => {},};return { lists, store };}