Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(document): the clip grows with the word, at the one funnel every write goes through

EtienneLescot09fa94bf0de8
diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.ts
index a1598db4b..3d724c425 100644
--- a/src/lib/ai-edition/document/timeline.ts
+++ b/src/lib/ai-edition/document/timeline.ts
@@ -18,7 +18,7 @@ import type { AxcutClip, AxcutDocument, AxcutTranscript, AxcutTrimRange } from "
*/
export type PlaybackSegment = AxcutClip & { extensionWordId?: string };
-import { clipParts } from "../timeline/clip-parts";
+import { clipParts, partsLengthSec } from "../timeline/clip-parts";
import { type Interval, subtractInterval } from "../timeline/intervals";
import { keptRawSpans } from "../timeline/programme-time";
import {
@@ -133,6 +133,35 @@ function collectWordRefs(
// after any structural change (insert / move / remove / trim) so the timeline
// never has gaps or overlaps between clips. Shared by useTimeline (UI) and
// the agent tool executor (main process) so both enforce the same invariant.
+/**
+ * Clip lengths, recomputed from the parts they now have.
+ *
+ * `timelineEndSec` is STORED, so something has to keep it true once a word can add media
+ * inside a clip — and the ruler reads it, so a clip left short draws a film that ends before
+ * the programme does. This is that something, and it is a recompute, not a reconciliation:
+ * the parts are derived from the words, so there is no second stored fact to drift from.
+ *
+ * Idempotent, and a no-op for a document with no added words.
+ */
+export function withClipsSizedToParts(document: AxcutDocument): AxcutDocument {
+ const wordsByAsset = new Map(document.transcripts.map((t) => [t.assetId, t.words]));
+ const clips = document.timeline.clips.map((clip) => {
+ const parts = clipParts(clip, wordsByAsset.get(clip.assetId) ?? []);
+ // No parts means the duration has not been probed yet; the prober owns the length.
+ if (parts.length === 0) return clip;
+ return { ...clip, timelineEndSec: clip.timelineStartSec + partsLengthSec(parts) };
+ });
+ const resequenced = resequenceClips(clips);
+ const unchanged = resequenced.every(
+ (clip, i) =>
+ Math.abs(clip.timelineStartSec - document.timeline.clips[i].timelineStartSec) < 1e-9 &&
+ Math.abs(clip.timelineEndSec - document.timeline.clips[i].timelineEndSec) < 1e-9,
+ );
+ return unchanged
+ ? document
+ : { ...document, timeline: { ...document.timeline, clips: resequenced } };
+}
+
export function resequenceClips(clips: AxcutClip[]): AxcutClip[] {
let cursor = 0;
return clips.map((c) => {
diff --git a/src/lib/ai-edition/document/transcript.test.ts b/src/lib/ai-edition/document/transcript.test.ts
index 1bc1f5451..b8c40f301 100644
--- a/src/lib/ai-edition/document/transcript.test.ts
+++ b/src/lib/ai-edition/document/transcript.test.ts
@@ -1,6 +1,13 @@
import { describe, expect, it } from "vitest";
-import { type AxcutTranscript, createEmptyDocument } from "../schema";
-import { carryOverWordEdits, setDocumentWordText, setWordText, withTranscript } from "./transcript";
+import { type AxcutDocument, type AxcutTranscript, createEmptyDocument } from "../schema";
+import {
+ carryOverWordEdits,
+ insertDocumentWord,
+ removeDocumentWords,
+ setDocumentWordText,
+ setWordText,
+ withTranscript,
+} from "./transcript";
function fixture(language = "en"): AxcutTranscript {
return {
@@ -483,3 +490,78 @@ describe("carryOverWordEdits", () => {
expect(carryOverWordEdits(null, next).transcript).toBe(next);
});
});
+
+// ─── The clip grows with the word ───────────────────────────────────────────
+// `timelineEndSec` is stored and the ruler reads it, so a clip left short draws a film that
+// ends before the programme does — the desync the previous attempt spent its life chasing.
+// Recomputed from the parts, at the one funnel every transcript write goes through.
+
+describe("adding a word sizes the clip it lands in", () => {
+ function docWithClip(): AxcutDocument {
+ return {
+ assets: [{ id: "a1", kind: "video" }],
+ project: { primaryAssetId: "a1" },
+ transcripts: [
+ {
+ assetId: "a1",
+ language: "en",
+ segments: [
+ {
+ id: "s1",
+ kind: "speech",
+ startSec: 0,
+ endSec: 6,
+ text: "hello there",
+ wordIds: ["w1", "w2"],
+ },
+ ],
+ words: [
+ { id: "w1", segmentId: "s1", startSec: 0, endSec: 1, text: "hello" },
+ { id: "w2", segmentId: "s1", startSec: 1, endSec: 2, text: "there" },
+ ],
+ },
+ ],
+ timeline: {
+ clips: [
+ {
+ id: "c1",
+ assetId: "a1",
+ sourceStartSec: 0,
+ sourceEndSec: 10,
+ timelineStartSec: 0,
+ timelineEndSec: 10,
+ wordRefs: [],
+ origin: "user",
+ reason: "",
+ },
+ ],
+ trimRanges: [],
+ },
+ } as unknown as AxcutDocument;
+ }
+
+ it("lengthens the clip by exactly the media the word needs", () => {
+ const after = insertDocumentWord(docWithClip(), "a1", "w2", "after", "really");
+ const [clip] = after.timeline.clips;
+ // "really" is 6 chars at 15/s.
+ expect(clip.timelineEndSec - clip.timelineStartSec).toBeCloseTo(10 + 6 / 15, 6);
+ // And takes not one frame of the recording with it.
+ expect(clip.sourceStartSec).toBe(0);
+ expect(clip.sourceEndSec).toBe(10);
+ });
+
+ it("gives the length back when the word goes", () => {
+ const before = docWithClip();
+ const added = insertDocumentWord(before, "a1", "w2", "after", "really");
+ const gone = removeDocumentWords(added, "a1", [
+ added.transcripts[0].words.find((w) => w.source === "synth")?.id ?? "",
+ ]);
+ expect(gone.timeline.clips).toEqual(before.timeline.clips);
+ });
+
+ it("leaves a document with no added words untouched", () => {
+ const before = docWithClip();
+ const after = setDocumentWordText(before, "a1", "w1", "HELLO");
+ expect(after.timeline.clips).toEqual(before.timeline.clips);
+ });
+});
diff --git a/src/lib/ai-edition/document/transcript.ts b/src/lib/ai-edition/document/transcript.ts
index dec291474..19b44dd9d 100644
--- a/src/lib/ai-edition/document/transcript.ts
+++ b/src/lib/ai-edition/document/transcript.ts
@@ -1,4 +1,5 @@
import type { AxcutDocument, AxcutTranscript, AxcutWord } from "../schema";
+import { withClipsSizedToParts } from "./timeline";
const CJK_EDGE = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]/u;
const CLOSING_PUNCTUATION = /^[,.;:!?%。,、;:!?…))\]}>》」』】〕]/u;
@@ -121,12 +122,14 @@ export function withTranscript(
...document.transcripts.filter((t) => t.assetId !== transcript.assetId),
transcript,
];
- return {
+ // Sized here, at the ONE funnel every transcript write goes through, so adding or
+ // removing a word cannot leave a clip claiming a length its parts no longer have.
+ return withClipsSizedToParts({
...document,
transcript:
document.project.primaryAssetId === transcript.assetId ? transcript : document.transcript,
transcripts,
- };
+ });
}
/**