Open source CapCut alternative.
TypeScript
fix(cut): let the chat's undo take back its last tool call alone [rebuild]
David Le2922722fbee6
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsxindex 307f6079..3030d644 100644--- a/site/src/cut/components/AiPanel.tsx+++ b/site/src/cut/components/AiPanel.tsx@@ -130,7 +130,7 @@ import { formatDuration, useGenScene } from "@/cut/lib/genScene";import { lightboxItemFromRef, useLightbox } from "@/cut/lib/lightbox";import { refsFromDroppedFiles } from "@/cut/lib/refMedia";import { revealRef } from "@/cut/lib/refReveal";-import { useEditor } from "@/cut/lib/store";+import { CHAT_TURN_LABEL, useEditor } from "@/cut/lib/store";import { reportActivity } from "@/cut/lib/tabActivity";import { cn } from "@/lib/utils";import { cardIconButton } from "@/cut/components/iconButton";@@ -1459,9 +1459,11 @@ function ChatSession({// Coalesce every edit the assistant makes in one turn into a single undo// step, so ⌘Z reverts the whole turn rather than one tool call at a time.+ // Each tool call is a step inside it, so the chat's own undo mid-turn takes+ // back its last call alone.useEffect(() => {if (!busy) return;- useEditor.getState().beginHistoryBatch();+ useEditor.getState().beginHistoryBatch(CHAT_TURN_LABEL);return () => useEditor.getState().endHistoryBatch();}, [busy]);diff --git a/site/src/cut/components/Editor.tools.ts b/site/src/cut/components/Editor.tools.tsindex 26d6dded..2269e0c9 100644--- a/site/src/cut/components/Editor.tools.ts+++ b/site/src/cut/components/Editor.tools.ts@@ -20,12 +20,12 @@ export const EDITOR_TOOLS = [},{name: "undo",- description: "Undo the last edit (unlimited).",+ description: "Undo the last edit (unlimited). In a turn, each undo takes back your last change-making tool call alone, every write it made; past this turn's first call it takes back the previous turn whole. The result names what it reverted and what changed.",inputSchema: obj({}),},{name: "redo",- description: "Redo the last undone edit.",+ description: "Redo the last undone edit. The result names what it put back.",inputSchema: obj({}),},] as const satisfies readonly AiToolDef[];diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex f0c3afd5..53a0bb57 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -231,7 +231,7 @@ import { isStylePresetTemplate } from "./stylePresets";import { findLibraryAsset, listLibrary } from "./libraryList";import { loadLibraryLut, lutIdOf, lutLabel } from "./linkedLibrary";import { type RawFrame, type ReadGrade, rawFrameAt, sampleClipRawFrame, sourceProfileOf, toBaseRendering, toGradedRendering } from "./baseFrame";-import { applyOverlayPatchSettled, assetClipUses, assetIdsOnAnyTimeline, clipLen, footprints, nextFreeStart, track0Clips, laneGapAt, getClipSpans, overlayLaneOrder, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor } from "./store";+import { applyOverlayPatchSettled, assetClipUses, assetIdsOnAnyTimeline, clipLen, footprints, nextFreeStart, track0Clips, laneGapAt, getClipSpans, overlayLaneOrder, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor, CHAT_TURN_LABEL } from "./store";import { playheadAt } from "./playhead";import { mixSpecFor, renderProjectFrame } from "./exportRender";import { renderMix } from "./audioMix";@@ -5783,13 +5783,17 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {},undo: (s) => {- if (!s.undo()) throw new ToolError("Nothing to undo.");- return { ok: true };+ const before = useEditor.getState();+ const step = s.undoStep();+ if (!step) throw new ToolError("Nothing to undo.");+ return { reverted: stepName(step.label), changes: historyChanges(before, useEditor.getState()) };},redo: (s) => {- if (!s.redo()) throw new ToolError("Nothing to redo.");- return { ok: true };+ const before = useEditor.getState();+ const step = s.redoStep();+ if (!step) throw new ToolError("Nothing to redo.");+ return { restored: stepName(step.label), changes: historyChanges(before, useEditor.getState()) };},open_export: (s) => {@@ -6305,6 +6309,51 @@ function droppedByConnection(err: unknown): boolean {}async function dispatchTool(s: Editor, name: string, raw: Record<string, unknown>, operation?: ProjectOperation): Promise<unknown> {+ // Every call is its own undo step, named for the tool, however many writes+ // it makes: inside a chat turn's batch it is a step the chat's undo takes+ // back alone; outside one it is an ordinary history entry. Undo and redo+ // step the history themselves.+ if (name === "undo" || name === "redo") return dispatchRun(s, name, raw, operation);+ s.beginHistoryBatch(name);+ try {+ return await dispatchRun(s, name, raw, operation);+ } finally {+ s.endHistoryBatch();+ }+}++/** What an undo or redo moved across, for its result: the tool call the step+ * was, or what kind of edit it was when no tool made it. */+function stepName(label: string | null): string {+ if (label === CHAT_TURN_LABEL) return "an earlier chat turn, all of its edits at once";+ return label ?? "an edit made in the editor";+}++/** How a history step changed the timeline, by kind: what came, went or+ * changed, so an undo's result says what it took back. */+function historyChanges(before: Editor, after: Editor): string {+ const kinds: [string, { id: string }[], { id: string }[]][] = [+ ["video clip", before.clips, after.clips],+ ["sound clip", before.audioClips, after.audioClips],+ ["element", before.overlays, after.overlays],+ ["caption", before.subtitles.cues, after.subtitles.cues],+ ["transition", before.transitions, after.transitions],+ ];+ const parts: string[] = [];+ const count = (n: number, what: string, how: string) => {+ if (n > 0) parts.push(`${n} ${what}${n === 1 ? "" : "s"} ${how}`);+ };+ for (const [what, was, now] of kinds) {+ const old = new Map(was.map((x) => [x.id, x]));+ const ids = new Set(now.map((x) => x.id));+ count(was.filter((x) => !ids.has(x.id)).length, what, "removed");+ count(now.filter((x) => !old.has(x.id)).length, what, "back");+ count(now.filter((x) => old.has(x.id) && JSON.stringify(old.get(x.id)) !== JSON.stringify(x)).length, what, "changed");+ }+ return parts.length > 0 ? parts.join(", ") : "no timeline item changed";+}++async function dispatchRun(s: Editor, name: string, raw: Record<string, unknown>, operation?: ProjectOperation): Promise<unknown> {const input = snapSeconds(raw) as Record<string, unknown>;// "update_title" stays as an alias so older threads keep working.const key = name === "update_title" ? "update_overlay" : name;diff --git a/site/src/cut/lib/chatUndo.test.ts b/site/src/cut/lib/chatUndo.test.tsnew file mode 100644index 00000000..613bd74a--- /dev/null+++ b/site/src/cut/lib/chatUndo.test.ts@@ -0,0 +1,100 @@+import { beforeEach, expect, test } from "bun:test";++import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { emptySubtitles } from "./types";++// A chat turn is one undo step for ⌘Z, but the chat's own `undo` takes back+// one tool call: blocks, then the soundtrack, then a text run, and an undo of+// the text run leaves the blocks and the soundtrack where they are.++const s = () => useEditor.getState();++beforeEach(() => {+ // Close any batch a failed test left open.+ for (let i = 0; i < 4; i++) s().endHistoryBatch();+ useEditor.setState({+ projectId: "p1",+ loaded: true,+ aspect: "16:9",+ assets: [],+ clips: [],+ transitions: [],+ audioClips: [],+ overlays: [],+ subtitles: emptySubtitles(),+ selection: null,+ multiSelection: [],+ background: "#000000",+ });+});++test("the chat's undo takes back its last tool call, not the turn", async () => {+ s().beginHistoryBatch();+ try {+ await runAiTool("add_clip", { blocks: [{ seconds: 1, color: "#ff0000" }, { seconds: 1, color: "#00ff00" }] });+ await runAiTool("add_shape", { shape: "rect", start: 0, end: 2, lane: 3 });+ await runAiTool("add_text_sequence", { lines: [{ text: "ONE", start: 0, end: 1 }, { text: "TWO", start: 1, end: 2 }] });+ expect(s().clips.length).toBe(2);+ expect(s().overlays.length).toBeGreaterThan(2);++ const out = (await runAiTool("undo", {})) as { reverted: string };+ expect(s().clips.length).toBe(2);+ expect(s().overlays.length).toBe(1);+ expect(out.reverted).toContain("add_text_sequence");++ const again = (await runAiTool("undo", {})) as { reverted: string };+ expect(s().clips.length).toBe(2);+ expect(s().overlays.length).toBe(0);+ expect(again.reverted).toContain("add_shape");++ const back = (await runAiTool("redo", {})) as { restored: string };+ expect(s().overlays.length).toBe(1);+ expect(back.restored).toContain("add_shape");+ } finally {+ s().endHistoryBatch();+ }+ // The person's ⌘Z after the turn still takes the whole turn back at once.+ expect(s().undo()).toBe(true);+ expect(s().clips.length).toBe(0);+ expect(s().overlays.length).toBe(0);+});++test("a tool call outside a chat turn is one undo step however many writes it makes", async () => {+ await runAiTool("add_shape", { shape: "rect", start: 0, end: 2, lane: 3 });+ await runAiTool("add_text_sequence", { lines: [{ text: "ONE", start: 0, end: 1 }, { text: "TWO", start: 1, end: 2 }] });+ const out = (await runAiTool("undo", {})) as { reverted: string };+ expect(s().overlays.length).toBe(1);+ expect(out.reverted).toContain("add_text_sequence");+});++test("a read leaves no undo step of its own", async () => {+ s().beginHistoryBatch();+ try {+ await runAiTool("add_shape", { shape: "rect", start: 0, end: 2, lane: 3 });+ await runAiTool("get_state", {});+ const out = (await runAiTool("undo", {})) as { reverted: string };+ expect(out.reverted).toContain("add_shape");+ expect(s().overlays.length).toBe(0);+ } finally {+ s().endHistoryBatch();+ }+});++test("an undo past the turn's first call takes back the turn before, and says so", async () => {+ s().beginHistoryBatch("chat turn");+ await runAiTool("add_shape", { shape: "rect", start: 0, end: 2, lane: 3 });+ s().endHistoryBatch();+ s().beginHistoryBatch("chat turn");+ try {+ await runAiTool("add_shape", { shape: "rect", start: 2, end: 4, lane: 3 });+ await runAiTool("undo", {});+ expect(s().overlays.length).toBe(1);+ const out = (await runAiTool("undo", {})) as { reverted: string; changes: string };+ expect(s().overlays.length).toBe(0);+ expect(out.reverted).toContain("earlier chat turn");+ expect(out.changes).toBe("1 element removed");+ } finally {+ s().endHistoryBatch();+ }+});diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex 7f7bbbd9..665561be 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -1005,12 +1005,20 @@ export interface EditorState {/** Step the history; false when there is nothing to step to. */undo: () => boolean;redo: () => boolean;+ /** The same steps, answering what the step took back or put back: the+ * label its batch was opened with (a chat tool's name), or null when it+ * had none. Null when there is nothing to step to. */+ undoStep: () => { label: string | null } | null;+ redoStep: () => { label: string | null } | null;upsertRender: (r: RenderRecord) => void;removeRenders: (ids: string[]) => void;pushHistory: () => void;/** Coalesce every edit until the matching `endHistoryBatch` into one undo- * step. Used so a whole assistant turn reverts with a single ⌘Z. */- beginHistoryBatch: () => void;+ * step. Used so a whole assistant turn reverts with a single ⌘Z. A batch+ * opened directly inside another (one chat tool call inside the turn) is a+ * step of its own while the outer one is open, so an undo mid-turn takes+ * back that call alone; `label` names what the step was for. */+ beginHistoryBatch: (label?: string) => void;endHistoryBatch: () => void;/** Copy the selected clip/audio/overlay/title/cue(s) to the timeline clipboard. */copySelection: () => boolean;@@ -1033,6 +1041,10 @@ export interface EditorState {pasteCopies: (items: TimelineClipboardItem[], at: number) => NonNullable<Selection>[] | null;}+/** The label a chat turn's batch opens with, so an undo that steps back into+ * an earlier turn says that it took the whole turn. */+export const CHAT_TURN_LABEL = "chat turn";+// Per-project undo/redo stacks; both reset when a project loads. Capped so a// long session (each snapshot deep-copies every clip/cue) can't grow unbounded.const HISTORY_CAP = 100;@@ -1050,6 +1062,22 @@ let docSeq = 0;/** >0 while a run of edits is being coalesced into one undo step (see* beginHistoryBatch). One checkpoint is captured when it goes 0→1. */let batchDepth = 0;+/** While a batch is open, the checkpoints of the batches opened directly+ * inside it — one per chat tool call — so the chat's undo mid-turn takes back+ * its last call, not the whole turn so far. Gone once the outer batch closes:+ * after the turn, ⌘Z takes the turn back whole. */+const steps: DocSnapshot[] = [];+/** The inner batch's checkpoint, committed to `steps` as it closes if the doc+ * changed meanwhile (see sealStep). */+let stepPending: { snap: DocSnapshot; seq: number } | null = null;+/** What each checkpoint was taken for — the label its batch opened with —+ * kept off the snapshot so every copy of the stacks carries it for free. */+const historyLabels = new WeakMap<DocSnapshot, string>();+/** Redo entries an undo of an inner step left, which go back onto `steps`. */+const stepRedos = new WeakSet<DocSnapshot>();+/** Set when an undo inside the open batch took a step back, so the batch's+ * close checks whether anything of the turn is left to record. */+let stepUndone = false;/** The project-wide fields a history snapshot also carries, as they stood* when a timeline was closed. */@@ -1813,6 +1841,42 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {* the same chat turn would step back into the turn before. */const rearmBatch = () => {if (batchDepth > 0) pending = { snap: snapshot(), seq: docSeq };+ rearmStep();+ };++ /** The same for an inner batch an undo ran inside: the undo is not that+ * batch's edit to record. */+ const rearmStep = () => {+ if (batchDepth > 1) stepPending = { snap: snapshot(), seq: docSeq };+ };++ /** Seal the inner batch's checkpoint into `steps` if the doc changed under+ * it, the way flush seals an outer one. */+ const sealStep = () => {+ if (!stepPending) return;+ const p = stepPending;+ stepPending = null;+ if (docSeq !== p.seq) {+ steps.push(p.snap);+ if (steps.length > HISTORY_CAP) steps.shift();+ future.length = 0;+ }+ };++ /** Drop the open batch's checkpoint when the doc stands where it did as the+ * batch opened — every step of it undone — so the batch records no step+ * that changes nothing, and an undo past it reaches the edit before. */+ const dropIdlePending = () => {+ if (pending && JSON.stringify(snapshot()) === JSON.stringify(pending.snap)) pending = null;+ };++ /** A checkpoint of the doc as it stands, carrying `like`'s label: the entry+ * the other stack gets when a step moves across. */+ const labelledSnapshot = (like: DocSnapshot): DocSnapshot => {+ const snap = snapshot();+ const label = historyLabels.get(like);+ if (label !== undefined) historyLabels.set(snap, label);+ return snap;};const push = () => {@@ -1907,6 +1971,8 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {history.length = 0;future.length = 0;pending = null;+ steps.length = 0;+ stepPending = null;parkedHistory.clear();// A fresh project owns no live run — any prior run's render-owned ids are// stale, and the loaded clips are ordinary, fully-undoable content.@@ -2508,7 +2574,9 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {};for (const snap of history) scrub(snap);for (const snap of future) scrub(snap);+ for (const snap of steps) scrub(snap);if (pending) scrub(pending.snap);+ if (stepPending) scrub(stepPending.snap);},releaseGenClips: () => {@@ -2533,7 +2601,9 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {};for (const snap of history) graft(snap);for (const snap of future) graft(snap);+ for (const snap of steps) graft(snap);if (pending) graft(pending.snap);+ if (stepPending) graft(stepPending.snap);genClipIds.clear();genAudioIds.clear();},@@ -2542,16 +2612,29 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {pushHistory: push,- beginHistoryBatch: () => {+ beginHistoryBatch: (label) => {if (batchDepth === 0) {flush(); // seal any prior edit before opening the batchpending = { snap: snapshot(), seq: docSeq };+ if (label) historyLabels.set(pending.snap, label);+ } else if (batchDepth === 1) {+ // A batch inside the open one: a step of its own until the outer closes.+ sealStep();+ stepPending = { snap: snapshot(), seq: docSeq };+ if (label) historyLabels.set(stepPending.snap, label);}batchDepth++;},endHistoryBatch: () => {batchDepth = Math.max(0, batchDepth - 1);- if (batchDepth === 0) flush(); // commit the whole run as one undo step+ if (batchDepth === 1) sealStep();+ if (batchDepth === 0) {+ sealStep();+ steps.length = 0;+ if (stepUndone) dropIdlePending();+ stepUndone = false;+ flush(); // commit the whole run as one undo step+ }},setAspect: (a) => {@@ -2654,8 +2737,12 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {selectedKey: null,});// A batch still open (a chat turn's run of edits) keeps going on this- // timeline, as one undo step of its own.+ // timeline, as one undo step of its own; its inner steps stay behind+ // inside the step the leaving timeline just recorded.+ steps.length = 0;+ stepPending = null;if (batchDepth > 0) pending = { snap: snapshot(), seq: docSeq };+ rearmStep();},setColorSpace: (space) => {if (get().colorSpace === space || get().readOnly) return;@@ -2874,7 +2961,9 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {.map((a) => (a.soundFrom?.assetId === id ? withAssetField(a, "soundFrom", undefined) : a));for (const snap of history) scrub(snap);for (const snap of future) scrub(snap);+ for (const snap of steps) scrub(snap);if (pending) scrub(pending.snap);+ if (stepPending) scrub(stepPending.snap);for (const parked of parkedHistory.values()) [...parked.history, ...parked.future].forEach(scrub);const dropFile = () => {const s = get();@@ -5304,25 +5393,51 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {return selection;},- undo: () => {+ undo: () => get().undoStep() !== null,++ redo: () => get().redoStep() !== null,++ undoStep: () => {+ sealStep();+ // Inside an open batch the last inner step goes back alone.+ const step = batchDepth > 0 ? steps.pop() : undefined;+ if (step) {+ const now = labelledSnapshot(step);+ stepRedos.add(now);+ future.push(now);+ restoreDoc(step);+ stepUndone = true;+ rearmStep();+ return { label: historyLabels.get(step) ?? null };+ }+ if (batchDepth > 0 && stepUndone) dropIdlePending();flush(); // commit any uncommitted edit before stepping backconst prev = history.pop();- if (!prev) return false;- future.push(snapshot());+ if (!prev) return null;+ future.push(labelledSnapshot(prev));restoreDoc(prev);rearmBatch();- return true;+ return { label: historyLabels.get(prev) ?? null };},- redo: () => {- flush();+ redoStep: () => {+ sealStep();+ // An open batch keeps its checkpoint: the redo is part of its run.+ if (batchDepth === 0) flush();const next = future.pop();- if (!next) return false;- history.push(snapshot());- if (history.length > HISTORY_CAP) history.shift();+ if (!next) return null;+ const now = labelledSnapshot(next);+ const inner = batchDepth > 0 && stepRedos.has(next);+ if (inner) {+ steps.push(now);+ } else {+ history.push(now);+ if (history.length > HISTORY_CAP) history.shift();+ }restoreDoc(next);- rearmBatch();- return true;+ if (inner) rearmStep();+ else rearmBatch();+ return { label: historyLabels.get(next) ?? null };},};});