mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 22:58:02 +00:00
fix(studio): undo offers reload the open composition and name only a change still in effect
This commit is contained in:
@@ -1678,10 +1678,26 @@ describe("claim: a writer that records after writing", () => {
|
||||
await history.claim(you, "Moved Title", ["index.html"]);
|
||||
await history.step("back", you);
|
||||
|
||||
expect(await history.step("back", you)).toMatchObject({ ok: false });
|
||||
expect(await history.step("back", you)).toMatchObject({
|
||||
ok: false,
|
||||
changedSince: { id: turn.id, label: "Agent turn", paths: ["index.html"] },
|
||||
});
|
||||
expect(history.changedSince(history.next("back", you)!.id)?.id).toBe(turn.id);
|
||||
});
|
||||
|
||||
it("names no later change when none of them is still in effect", async () => {
|
||||
const { history, write } = await project({ "index.html": "A" });
|
||||
const first = await change(history, agent, "Agent turn", () => write("index.html", "B"));
|
||||
write("index.html", "C");
|
||||
await history.claim(you, "Moved Title", ["index.html"]);
|
||||
await change(history, agent, "Agent turn 2", () => write("index.html", "D"));
|
||||
await history.undo(first.id, { who: agent, mode: "just-this" });
|
||||
|
||||
const refused = await history.step("back", you);
|
||||
expect(refused).toMatchObject({ ok: false });
|
||||
expect(refused).not.toHaveProperty("changedSince", expect.anything());
|
||||
});
|
||||
|
||||
it("names the agent's earlier turn, not its undo of a later one or the turn it undid", async () => {
|
||||
const { history, write } = await project({ "index.html": "A" });
|
||||
write("index.html", "B");
|
||||
|
||||
@@ -108,9 +108,12 @@ export interface HistoryListItem extends HistoryEntry {
|
||||
undone: boolean;
|
||||
}
|
||||
|
||||
/** The later change a refused undo can offer to undo first. */
|
||||
export type HistoryName = { id: string; label: string; paths: string[] };
|
||||
|
||||
export type HistoryResult =
|
||||
| { ok: true; entry: HistoryEntry | null }
|
||||
| { ok: false; conflict: { files: string[]; newer: string[] } };
|
||||
| { ok: false; conflict: { files: string[]; newer: string[] }; changedSince?: HistoryName };
|
||||
|
||||
export interface HistoryWindow {
|
||||
readonly id: string;
|
||||
@@ -147,7 +150,7 @@ export interface ProjectHistory {
|
||||
step(direction: "back" | "forward", who: HistoryWho, options?: Writing): Promise<HistoryResult>;
|
||||
/** The entry `who`'s next step reverts, pending changes included, as of the last scan (a step scans first). */
|
||||
next(direction: "back" | "forward", who: HistoryWho): HistoryEntry | undefined;
|
||||
changedSince(id: string): HistoryEntry | undefined;
|
||||
changedSince(id: string): HistoryName | undefined;
|
||||
/** A file changed since returns a conflict; `mode` takes a choice (keep-later-edits: null when none is left). */
|
||||
undo(id: string, options: { who: HistoryWho; mode?: UndoMode } & Writing): Promise<HistoryResult>;
|
||||
/** Makes the files equal what they were right after `point` (an entry id, or START). */
|
||||
@@ -879,7 +882,13 @@ class Engine {
|
||||
async undoNow(id: string, who: HistoryWho, mode?: UndoMode): Promise<HistoryResult> {
|
||||
const entry = this.entry(id);
|
||||
const changed = this.movedOn(entry);
|
||||
if (changed.length && !mode) return { ok: false, conflict: this.conflict(entry, changed) };
|
||||
if (changed.length && !mode) {
|
||||
return {
|
||||
ok: false,
|
||||
conflict: this.conflict(entry, changed),
|
||||
changedSince: this.blockerOf(entry, changed),
|
||||
};
|
||||
}
|
||||
if (mode === "back-to-before") {
|
||||
const index = this.log.entries.indexOf(entry);
|
||||
const point = index > 0 ? this.log.entries[index - 1]!.id : START;
|
||||
@@ -903,6 +912,22 @@ class Engine {
|
||||
return entry.files.filter((file) => (this.tracked.get(file.path)?.hash ?? null) !== file.after);
|
||||
}
|
||||
|
||||
/** The newest later change to `changed` that an undo can still take, so undoing it first unblocks `entry`. */
|
||||
blockerOf(entry: HistoryEntry, changed: HistoryFileChange[]): HistoryName | undefined {
|
||||
const paths = new Set(changed.map((file) => file.path));
|
||||
const undone = undoneIds(this.log.entries);
|
||||
const later = this.log.entries.slice(this.log.entries.indexOf(entry) + 1).reverse();
|
||||
const blocker = later.find(
|
||||
(candidate) =>
|
||||
!candidate.undoes &&
|
||||
!undone.has(candidate.id) &&
|
||||
candidate.files.some((file) => paths.has(file.path)) &&
|
||||
!this.movedOn(candidate).length,
|
||||
);
|
||||
if (!blocker) return undefined;
|
||||
return { id: blocker.id, label: blocker.label, paths: blocker.files.map((file) => file.path) };
|
||||
}
|
||||
|
||||
next(direction: "back" | "forward", who: HistoryWho): HistoryEntry | undefined {
|
||||
// An outside change has no known author, so it is everyone's; a step commits it after the held claim.
|
||||
const mine = (author: HistoryWho) => sameWho(author, who) || sameWho(author, OUTSIDE);
|
||||
@@ -1018,10 +1043,7 @@ class Engine {
|
||||
this.assertOpen();
|
||||
const entry = this.log.entries.find((candidate) => candidate.id === id);
|
||||
const changed = entry ? this.movedOn(entry) : [];
|
||||
if (!entry || !changed.length) return undefined;
|
||||
const undone = undoneIds(this.log.entries);
|
||||
const newer = this.conflict(entry, changed).newer.map((later) => this.entry(later));
|
||||
return newer.reverse().find((later) => !later.undoes && !undone.has(later.id));
|
||||
return entry && changed.length ? this.blockerOf(entry, changed) : undefined;
|
||||
},
|
||||
readBlob: async (hash) => {
|
||||
this.assertOpen();
|
||||
|
||||
@@ -245,7 +245,7 @@ describe("history routes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("offer no later change on a refused redo", async () => {
|
||||
it("name the later change on a refused redo too", async () => {
|
||||
const { projectDir, history, call } = await demoProject();
|
||||
writeFileSync(join(projectDir, "index.html"), "B");
|
||||
await call("/claim", { label: "Moved Title", paths: ["index.html"] });
|
||||
@@ -255,8 +255,7 @@ describe("history routes", () => {
|
||||
await window.close();
|
||||
|
||||
const refused = await (await call("/step", { direction: "forward" })).json();
|
||||
expect(refused).toMatchObject({ ok: false });
|
||||
expect(refused).not.toHaveProperty("changedSince");
|
||||
expect(refused).toMatchObject({ ok: false, changedSince: { label: "Agent turn" } });
|
||||
});
|
||||
|
||||
it("name the next later change when undoing the offered one is refused too", async () => {
|
||||
|
||||
@@ -4,11 +4,10 @@ import { createWriteToken } from "../helpers/fileVersion.js";
|
||||
import {
|
||||
MAX_WINDOW_IDLE_MS,
|
||||
UNDO_MODES,
|
||||
type HistoryResult,
|
||||
type HistoryWindow,
|
||||
type ProjectHistory,
|
||||
} from "../history/projectHistory.js";
|
||||
import type { HistoryEntry, HistoryWho } from "../history/historyLog.js";
|
||||
import type { HistoryWho } from "../history/historyLog.js";
|
||||
|
||||
const YOU: HistoryWho = { kind: "person", name: "You" };
|
||||
|
||||
@@ -59,18 +58,10 @@ function nextStep(history: ProjectHistory, direction: "back" | "forward") {
|
||||
const target = history.next(direction, YOU);
|
||||
if (!target) return null;
|
||||
const paths = target.files.map((file) => file.path);
|
||||
const changedSince = direction === "back" ? nameOf(history.changedSince(target.id)) : undefined;
|
||||
const changedSince = direction === "back" ? history.changedSince(target.id) : undefined;
|
||||
return { id: target.id, label: target.label, endedAt: target.endedAt, paths, changedSince };
|
||||
}
|
||||
|
||||
function nameOf(entry: HistoryEntry | undefined) {
|
||||
return entry && { id: entry.id, label: entry.label, paths: entry.files.map((file) => file.path) };
|
||||
}
|
||||
|
||||
function withChangedSince(history: ProjectHistory, refused: HistoryResult, id: string | undefined) {
|
||||
return { ...refused, changedSince: nameOf(id ? history.changedSince(id) : undefined) };
|
||||
}
|
||||
|
||||
/** Runs `task` on the project's history; no history is a 404, an engine refusal ("no longer kept") a 409. */
|
||||
async function withHistory(
|
||||
adapter: StudioApiAdapter,
|
||||
@@ -99,19 +90,14 @@ export function registerHistoryRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
}),
|
||||
);
|
||||
api.post(`${base}/step`, (c) =>
|
||||
withHistory(adapter, c, async (history, body) => {
|
||||
const direction = body.direction === "forward" ? "forward" : "back";
|
||||
const result = await history.step(direction, YOU, writing(c));
|
||||
if (result.ok || direction === "forward") return result;
|
||||
return withChangedSince(history, result, history.next("back", YOU)?.id);
|
||||
}),
|
||||
withHistory(adapter, c, (history, body) =>
|
||||
history.step(body.direction === "forward" ? "forward" : "back", YOU, writing(c)),
|
||||
),
|
||||
);
|
||||
api.post(`${base}/undo`, (c) =>
|
||||
withHistory(adapter, c, async (history, body) => {
|
||||
withHistory(adapter, c, (history, body) => {
|
||||
const mode = UNDO_MODES.find((known) => known === body.mode);
|
||||
const entryId = text(body.entryId) ?? "";
|
||||
const result = await history.undo(entryId, { who: whoOf(body), mode, ...writing(c) });
|
||||
return result.ok ? result : withChangedSince(history, result, entryId);
|
||||
return history.undo(text(body.entryId) ?? "", { who: whoOf(body), mode, ...writing(c) });
|
||||
}),
|
||||
);
|
||||
api.post(`${base}/restore`, (c) =>
|
||||
|
||||
@@ -45,9 +45,10 @@ function mount(result: {
|
||||
actions = useEditHistoryActions(deps);
|
||||
return null;
|
||||
}
|
||||
const render = () => act(() => root!.render(createElement(Probe)));
|
||||
root = createRoot(document.createElement("div"));
|
||||
act(() => root!.render(createElement(Probe)));
|
||||
return { deps, actions };
|
||||
render();
|
||||
return { deps, actions, render };
|
||||
}
|
||||
|
||||
describe("useEditHistoryActions", () => {
|
||||
@@ -95,6 +96,26 @@ describe("useEditHistoryActions", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("reloads the SDK session for the composition open when the offer is clicked", async () => {
|
||||
const { deps, actions, render } = mount({
|
||||
ok: false,
|
||||
reason: "content-mismatch",
|
||||
paths: ["scene.html"],
|
||||
changedSince: { id: "turn-1", label: "Agent turn", paths: ["scene.html"] },
|
||||
});
|
||||
deps.editHistory.undoEntry.mockResolvedValue({
|
||||
ok: true,
|
||||
label: "Undid: Agent turn",
|
||||
paths: ["scene.html"],
|
||||
});
|
||||
await act(() => actions.undo());
|
||||
const [, , offer] = deps.showToast.mock.calls[0]!;
|
||||
deps.activeCompPath = "scene.html";
|
||||
render();
|
||||
await act(async () => offer.run());
|
||||
await vi.waitFor(() => expect(deps.forceReloadSdkSession).toHaveBeenCalled());
|
||||
});
|
||||
|
||||
it("offers no undo of the later change when a redo is refused", async () => {
|
||||
const { deps, actions } = mount({
|
||||
ok: false,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// fallow-ignore-file complexity
|
||||
import { useCallback, useMemo } from "react";
|
||||
import { useCallback, useMemo, useRef } from "react";
|
||||
import { STUDIO_MOTION_PATH } from "../components/editor/studioMotion";
|
||||
import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator";
|
||||
import type { ToastAction } from "../utils/studioHelpers";
|
||||
@@ -60,6 +60,9 @@ export function useEditHistoryActions({
|
||||
activeCompPath,
|
||||
forceReloadSdkSession,
|
||||
}: UseEditHistoryActionsOptions) {
|
||||
// Offers run long after the render that showed them; read the composition open at click time.
|
||||
const activeCompPathRef = useRef(activeCompPath);
|
||||
activeCompPathRef.current = activeCompPath;
|
||||
const readHistoryFile = useCallback(
|
||||
(path: string): Promise<string> =>
|
||||
path === STUDIO_MOTION_PATH ? readOptionalProjectFile(path) : readProjectFile(path),
|
||||
@@ -104,7 +107,8 @@ export function useEditHistoryActions({
|
||||
if (result.ok && result.label) {
|
||||
const restore = { paths: result.paths, files: result.files };
|
||||
onAfterUndoRedo?.(restore);
|
||||
if (activeCompPath && result.paths?.includes(activeCompPath)) {
|
||||
const openPath = activeCompPathRef.current;
|
||||
if (openPath && result.paths?.includes(openPath)) {
|
||||
forceReloadSdkSession?.();
|
||||
}
|
||||
await syncHistoryPreviewAfterApply(restore);
|
||||
@@ -119,7 +123,6 @@ export function useEditHistoryActions({
|
||||
waitForPendingDomEditSaves,
|
||||
serializeHistoryFiles,
|
||||
onAfterUndoRedo,
|
||||
activeCompPath,
|
||||
forceReloadSdkSession,
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user