@morshoto/framekit 0.1.12 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-package/adapters/final-cut/typescript/src/canonical.js +407 -71
- package/dist-package/adapters/final-cut/typescript/src/fcpxml.js +26 -7
- package/dist-package/adapters/final-cut/typescript/src/library-inspection.js +2 -0
- package/dist-package/apps/mcp-server/src/headless-sessions.js +47 -4
- package/dist-package/apps/mcp-server/src/main.js +13 -1
- package/dist-package/apps/mcp-server/src/server.js +51 -16
- package/dist-package/packages/runtime/src/context/context-engine.js +313 -8
- package/dist-package/packages/runtime/src/context/context-service.js +5 -2
- package/dist-package/packages/runtime/src/context/project-catalog.js +12 -1
- package/dist-package/packages/runtime/src/domain/incremental-sync.js +153 -0
- package/dist-package/packages/runtime/src/domain/index.js +1 -0
- package/dist-package/packages/runtime/src/editing/edit-service.js +8 -2
- package/dist-package/packages/runtime/src/runtime.js +5 -2
- package/dist-package/packages/runtime/src/timeline/editing-session.js +13 -0
- package/dist-package/packages/runtime/src/timeline/snapshot-diff.js +169 -10
- package/package.json +2 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
export const CANONICAL_INCREMENTAL_SYNC_CONTRACT_VERSION = 1;
|
|
2
|
+
/**
|
|
3
|
+
* Validates the structural guarantees shared by timeline.changes and
|
|
4
|
+
* context.changes. Provider-specific payload validation belongs at the edge.
|
|
5
|
+
*/
|
|
6
|
+
export function assertCanonicalSyncResult(result) {
|
|
7
|
+
if (result.contractVersion !== CANONICAL_INCREMENTAL_SYNC_CONTRACT_VERSION) {
|
|
8
|
+
throw new Error("CANONICAL_SYNC_INVALID: unsupported contract version");
|
|
9
|
+
}
|
|
10
|
+
if (!result.ok) {
|
|
11
|
+
assertFailure(result);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
assertTarget(result.target);
|
|
15
|
+
assertCursor(result.from);
|
|
16
|
+
assertCursor(result.to);
|
|
17
|
+
if (!sameTarget(result.target, result.from.target) || !sameTarget(result.target, result.to.target)) {
|
|
18
|
+
throw new Error("CANONICAL_SYNC_INVALID: result cursors are not bound to the target");
|
|
19
|
+
}
|
|
20
|
+
if (result.to.revision.sequence < result.from.revision.sequence) {
|
|
21
|
+
throw new Error("CANONICAL_SYNC_INVALID: to revision precedes from revision");
|
|
22
|
+
}
|
|
23
|
+
assertProvenance(result.provenance);
|
|
24
|
+
if (result.provenance.source.evidenceTier === "metadata-only") {
|
|
25
|
+
throw new Error("CANONICAL_SYNC_INVALID: metadata-only evidence cannot return canonical changes");
|
|
26
|
+
}
|
|
27
|
+
assertChanges(result.changes, result.from.revision, result.to.revision);
|
|
28
|
+
}
|
|
29
|
+
function assertFailure(result) {
|
|
30
|
+
if (!result.failure.message.trim()) {
|
|
31
|
+
throw new Error("CANONICAL_SYNC_INVALID: failure message is required");
|
|
32
|
+
}
|
|
33
|
+
if (result.status === "unavailable"
|
|
34
|
+
&& !["CAPABILITY_UNAVAILABLE", "METADATA_ONLY"].includes(result.failure.code)) {
|
|
35
|
+
throw new Error("CANONICAL_SYNC_INVALID: unavailable result has the wrong failure code");
|
|
36
|
+
}
|
|
37
|
+
if (result.status === "stale" && result.failure.code !== "STALE_CURSOR") {
|
|
38
|
+
throw new Error("CANONICAL_SYNC_INVALID: stale result has the wrong failure code");
|
|
39
|
+
}
|
|
40
|
+
if (result.status === "stale" && !result.cursor) {
|
|
41
|
+
throw new Error("CANONICAL_SYNC_INVALID: stale result requires a revision cursor");
|
|
42
|
+
}
|
|
43
|
+
if (result.status === "ambiguous" && result.failure.code !== "AMBIGUOUS_TARGET") {
|
|
44
|
+
throw new Error("CANONICAL_SYNC_INVALID: ambiguous result has the wrong failure code");
|
|
45
|
+
}
|
|
46
|
+
if (result.target)
|
|
47
|
+
assertTarget(result.target);
|
|
48
|
+
if (result.cursor)
|
|
49
|
+
assertCursor(result.cursor);
|
|
50
|
+
if (result.target && result.cursor && !sameTarget(result.target, result.cursor.target)) {
|
|
51
|
+
throw new Error("CANONICAL_SYNC_INVALID: failure target does not match its cursor");
|
|
52
|
+
}
|
|
53
|
+
if (result.provenance)
|
|
54
|
+
assertProvenance(result.provenance);
|
|
55
|
+
}
|
|
56
|
+
function assertCursor(cursor) {
|
|
57
|
+
assertTarget(cursor.target);
|
|
58
|
+
assertRevision(cursor.revision);
|
|
59
|
+
}
|
|
60
|
+
function assertTarget(target) {
|
|
61
|
+
if (!target.projectId.trim() || !target.sequenceId.trim()) {
|
|
62
|
+
throw new Error("CANONICAL_SYNC_INVALID: stable project and sequence identities are required");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function assertRevision(revision) {
|
|
66
|
+
if (!revision.id.trim() || !Number.isInteger(revision.sequence) || revision.sequence < 0) {
|
|
67
|
+
throw new Error("CANONICAL_SYNC_INVALID: valid revision identity is required");
|
|
68
|
+
}
|
|
69
|
+
if (!Number.isFinite(Date.parse(revision.timestamp))) {
|
|
70
|
+
throw new Error("CANONICAL_SYNC_INVALID: revision timestamp is invalid");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function assertProvenance(provenance) {
|
|
74
|
+
if (!provenance.source.provider.trim() || !provenance.source.backend.trim()) {
|
|
75
|
+
throw new Error("CANONICAL_SYNC_INVALID: provider and backend provenance are required");
|
|
76
|
+
}
|
|
77
|
+
if (!Number.isFinite(Date.parse(provenance.observedAt))) {
|
|
78
|
+
throw new Error("CANONICAL_SYNC_INVALID: observation timestamp is invalid");
|
|
79
|
+
}
|
|
80
|
+
const allowedTiers = SURFACE_EVIDENCE_TIERS[provenance.source.surface];
|
|
81
|
+
if (!allowedTiers.includes(provenance.source.evidenceTier)) {
|
|
82
|
+
throw new Error(`CANONICAL_SYNC_INVALID: evidence tier ${provenance.source.evidenceTier} is incompatible with surface ${provenance.source.surface}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const SURFACE_EVIDENCE_TIERS = {
|
|
86
|
+
live: ["metadata-only", "canonical-read", "canonical-live", "headed-native"],
|
|
87
|
+
artifact: ["artifact-only", "canonical-read"],
|
|
88
|
+
fixture: ["fixture"],
|
|
89
|
+
};
|
|
90
|
+
function assertChanges(changes, from, to) {
|
|
91
|
+
let previous;
|
|
92
|
+
changes.forEach((change, index) => {
|
|
93
|
+
if (change.order !== index) {
|
|
94
|
+
throw new Error("CANONICAL_SYNC_INVALID: change order must be contiguous");
|
|
95
|
+
}
|
|
96
|
+
assertRevision(change.revision);
|
|
97
|
+
if (change.revision.sequence < from.sequence || change.revision.sequence > to.sequence) {
|
|
98
|
+
throw new Error("CANONICAL_SYNC_INVALID: change revision is outside the result cursor");
|
|
99
|
+
}
|
|
100
|
+
if (!change.entityId.trim()) {
|
|
101
|
+
throw new Error("CANONICAL_SYNC_INVALID: change entity identity is required");
|
|
102
|
+
}
|
|
103
|
+
const hasBefore = Object.prototype.hasOwnProperty.call(change, "before");
|
|
104
|
+
const hasAfter = Object.prototype.hasOwnProperty.call(change, "after");
|
|
105
|
+
if (change.operation === "added" && (!hasAfter || hasBefore)) {
|
|
106
|
+
throw new Error("CANONICAL_SYNC_INVALID: added change must have after only");
|
|
107
|
+
}
|
|
108
|
+
if (change.operation === "removed" && (!hasBefore || hasAfter)) {
|
|
109
|
+
throw new Error("CANONICAL_SYNC_INVALID: removed change must have before only");
|
|
110
|
+
}
|
|
111
|
+
if (change.operation === "modified" && (!hasBefore || !hasAfter)) {
|
|
112
|
+
throw new Error("CANONICAL_SYNC_INVALID: modified change must have before and after");
|
|
113
|
+
}
|
|
114
|
+
if (previous && compareChanges(previous, change) > 0) {
|
|
115
|
+
throw new Error("CANONICAL_SYNC_INVALID: changes must be deterministically ordered");
|
|
116
|
+
}
|
|
117
|
+
previous = change;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const ENTITY_ORDER = {
|
|
121
|
+
clip: 0,
|
|
122
|
+
"connected-item": 1,
|
|
123
|
+
marker: 2,
|
|
124
|
+
caption: 3,
|
|
125
|
+
role: 4,
|
|
126
|
+
audio: 5,
|
|
127
|
+
playhead: 6,
|
|
128
|
+
};
|
|
129
|
+
const OPERATION_ORDER = {
|
|
130
|
+
added: 0,
|
|
131
|
+
modified: 1,
|
|
132
|
+
removed: 2,
|
|
133
|
+
};
|
|
134
|
+
function compareChanges(left, right) {
|
|
135
|
+
return left.revision.sequence - right.revision.sequence
|
|
136
|
+
|| ENTITY_ORDER[left.entity] - ENTITY_ORDER[right.entity]
|
|
137
|
+
|| compareCodePointStrings(left.entityId, right.entityId)
|
|
138
|
+
|| OPERATION_ORDER[left.operation] - OPERATION_ORDER[right.operation];
|
|
139
|
+
}
|
|
140
|
+
function compareCodePointStrings(left, right) {
|
|
141
|
+
const leftCodePoints = Array.from(left, (character) => character.codePointAt(0));
|
|
142
|
+
const rightCodePoints = Array.from(right, (character) => character.codePointAt(0));
|
|
143
|
+
const length = Math.min(leftCodePoints.length, rightCodePoints.length);
|
|
144
|
+
for (let index = 0; index < length; index += 1) {
|
|
145
|
+
if (leftCodePoints[index] !== rightCodePoints[index]) {
|
|
146
|
+
return leftCodePoints[index] < rightCodePoints[index] ? -1 : 1;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return leftCodePoints.length - rightCodePoints.length;
|
|
150
|
+
}
|
|
151
|
+
function sameTarget(left, right) {
|
|
152
|
+
return left.projectId === right.projectId && left.sequenceId === right.sequenceId;
|
|
153
|
+
}
|
|
@@ -479,8 +479,14 @@ function postWriteAnalysisRequirements(policy) {
|
|
|
479
479
|
}
|
|
480
480
|
function sameDiffContent(left, right) {
|
|
481
481
|
const comparable = (diff) => {
|
|
482
|
-
const { from: _from, to: _to, ...content } = diff;
|
|
483
|
-
return
|
|
482
|
+
const { from: _from, to: _to, provenance, ...content } = diff;
|
|
483
|
+
return {
|
|
484
|
+
target: provenance ? {
|
|
485
|
+
projectId: provenance.projectId,
|
|
486
|
+
sequenceId: provenance.sequenceId,
|
|
487
|
+
} : undefined,
|
|
488
|
+
...content,
|
|
489
|
+
};
|
|
484
490
|
};
|
|
485
491
|
return JSON.stringify(comparable(left)) === JSON.stringify(comparable(right));
|
|
486
492
|
}
|
|
@@ -144,8 +144,11 @@ export class AgentVideoRuntime {
|
|
|
144
144
|
async changesSince(revision) {
|
|
145
145
|
return this.contexts.changesSince(revision);
|
|
146
146
|
}
|
|
147
|
-
async
|
|
148
|
-
return this.contexts.
|
|
147
|
+
async timelineChangesSince(request) {
|
|
148
|
+
return this.contexts.timelineChangesSince(request);
|
|
149
|
+
}
|
|
150
|
+
async contextChangesSince(cursorOrRevision, waitMs = 0) {
|
|
151
|
+
return this.contexts.contextChangesSince(cursorOrRevision, waitMs);
|
|
149
152
|
}
|
|
150
153
|
async analyzeSpeech(mediaId, range) {
|
|
151
154
|
return this.media.analyzeSpeech(mediaId, range);
|
|
@@ -51,7 +51,15 @@ export class EditingSession {
|
|
|
51
51
|
serialize() {
|
|
52
52
|
return stableJson(this.value);
|
|
53
53
|
}
|
|
54
|
+
observeProviderRevision(providerRevision) {
|
|
55
|
+
if (sameRevision(this.value.base.revision, providerRevision))
|
|
56
|
+
return "unchanged";
|
|
57
|
+
if (this.value.state !== "conflicted")
|
|
58
|
+
this.value.state = "possibly_stale";
|
|
59
|
+
return "changed";
|
|
60
|
+
}
|
|
54
61
|
preview(operations, expectedRevision = this.value.desired.revision) {
|
|
62
|
+
this.assertEditable();
|
|
55
63
|
assertRevision(this.value.desired.revision, expectedRevision);
|
|
56
64
|
const before = structuredClone(this.value.desired);
|
|
57
65
|
const after = this.nextTimeline(before, operations);
|
|
@@ -145,6 +153,11 @@ export class EditingSession {
|
|
|
145
153
|
validateTimelineIr(after);
|
|
146
154
|
return after;
|
|
147
155
|
}
|
|
156
|
+
assertEditable() {
|
|
157
|
+
if (this.value.state === "possibly_stale" || this.value.state === "conflicted") {
|
|
158
|
+
throw new Error("RECONCILIATION_REQUIRED: session must be reconciled before editing");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
148
161
|
}
|
|
149
162
|
export function createTimelineIrFromProjectSnapshot(snapshot, provider) {
|
|
150
163
|
const binding = provider ? (kind, identity) => ({
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseRational, subtractRationalTimes } from "./rational-time.js";
|
|
1
2
|
function withoutId(value) {
|
|
2
3
|
const { id: _id, ...content } = value;
|
|
3
4
|
return content;
|
|
@@ -6,6 +7,8 @@ function rangesForClip(clip) {
|
|
|
6
7
|
return clip ? [{ start: clip.start, end: clip.start + clip.duration, startTime: clip.startTime, durationTime: clip.durationTime }] : [];
|
|
7
8
|
}
|
|
8
9
|
function diffByKey(before, after, keyOf, comparable = (item) => item) {
|
|
10
|
+
assertUniqueKeys(before, keyOf, "before");
|
|
11
|
+
assertUniqueKeys(after, keyOf, "after");
|
|
9
12
|
const beforeById = new Map(before.map((item) => [keyOf(item), item]));
|
|
10
13
|
const afterById = new Map(after.map((item) => [keyOf(item), item]));
|
|
11
14
|
return {
|
|
@@ -13,7 +16,7 @@ function diffByKey(before, after, keyOf, comparable = (item) => item) {
|
|
|
13
16
|
removed: [...beforeById.values()].filter((item) => !afterById.has(keyOf(item))),
|
|
14
17
|
modified: [...afterById.values()].filter((item) => {
|
|
15
18
|
const previous = beforeById.get(keyOf(item));
|
|
16
|
-
return previous !== undefined &&
|
|
19
|
+
return previous !== undefined && stableJson(comparable(previous)) !== stableJson(comparable(item));
|
|
17
20
|
}).map((afterItem) => ({ after: afterItem, before: beforeById.get(keyOf(afterItem)) })),
|
|
18
21
|
};
|
|
19
22
|
}
|
|
@@ -29,6 +32,9 @@ function mediaRegistryFields(media) {
|
|
|
29
32
|
};
|
|
30
33
|
}
|
|
31
34
|
export function diffSnapshots(before, after) {
|
|
35
|
+
if (before.projectId !== after.projectId || before.timeline.id !== after.timeline.id) {
|
|
36
|
+
throw new Error("TARGET_MISMATCH: cannot diff snapshots from different project or sequence targets");
|
|
37
|
+
}
|
|
32
38
|
const clips = diffById(before.timeline.clips, after.timeline.clips);
|
|
33
39
|
const added = clips.added.map((clip) => ({ type: "ITEM_ADDED", itemId: clip.id, after: clip }));
|
|
34
40
|
const removed = clips.removed.map((clip) => ({ type: "ITEM_REMOVED", itemId: clip.id, before: clip }));
|
|
@@ -40,10 +46,11 @@ export function diffSnapshots(before, after) {
|
|
|
40
46
|
}));
|
|
41
47
|
const markers = diffById(before.timeline.markers, after.timeline.markers);
|
|
42
48
|
const markerChanges = [
|
|
43
|
-
...markers.added.map((marker) => ({ type: "MARKER_ADDED", marker })),
|
|
44
|
-
...markers.removed.map((marker) => ({ type: "MARKER_REMOVED", marker })),
|
|
49
|
+
...markers.added.map((marker) => ({ type: "MARKER_ADDED", itemId: marker.id, marker, after: marker })),
|
|
50
|
+
...markers.removed.map((marker) => ({ type: "MARKER_REMOVED", itemId: marker.id, marker, before: marker })),
|
|
45
51
|
...markers.modified.map(({ before: previous, after: current }) => ({
|
|
46
52
|
type: "MARKER_MODIFIED",
|
|
53
|
+
itemId: current.id,
|
|
47
54
|
marker: current,
|
|
48
55
|
before: previous,
|
|
49
56
|
after: current,
|
|
@@ -51,10 +58,11 @@ export function diffSnapshots(before, after) {
|
|
|
51
58
|
];
|
|
52
59
|
const captions = diffById(before.timeline.captions, after.timeline.captions);
|
|
53
60
|
const captionChanges = [
|
|
54
|
-
...captions.added.map((caption) => ({ type: "CAPTION_ADDED", caption })),
|
|
55
|
-
...captions.removed.map((caption) => ({ type: "CAPTION_REMOVED", caption })),
|
|
61
|
+
...captions.added.map((caption) => ({ type: "CAPTION_ADDED", itemId: caption.id, caption, after: caption })),
|
|
62
|
+
...captions.removed.map((caption) => ({ type: "CAPTION_REMOVED", itemId: caption.id, caption, before: caption })),
|
|
56
63
|
...captions.modified.map(({ before: previous, after: current }) => ({
|
|
57
64
|
type: "CAPTION_MODIFIED",
|
|
65
|
+
itemId: current.id,
|
|
58
66
|
caption: current,
|
|
59
67
|
before: previous,
|
|
60
68
|
after: current,
|
|
@@ -62,10 +70,11 @@ export function diffSnapshots(before, after) {
|
|
|
62
70
|
];
|
|
63
71
|
const storyElements = diffById(before.timeline.storyElements, after.timeline.storyElements);
|
|
64
72
|
const storyElementChanges = [
|
|
65
|
-
...storyElements.added.map((element) => ({ type: "STORY_ELEMENT_ADDED", element, after: element })),
|
|
66
|
-
...storyElements.removed.map((element) => ({ type: "STORY_ELEMENT_REMOVED", element, before: element })),
|
|
73
|
+
...storyElements.added.map((element) => ({ type: "STORY_ELEMENT_ADDED", itemId: element.id, element, after: element })),
|
|
74
|
+
...storyElements.removed.map((element) => ({ type: "STORY_ELEMENT_REMOVED", itemId: element.id, element, before: element })),
|
|
67
75
|
...storyElements.modified.map(({ before: previous, after: current }) => ({
|
|
68
76
|
type: "STORY_ELEMENT_MODIFIED",
|
|
77
|
+
itemId: current.id,
|
|
69
78
|
element: current,
|
|
70
79
|
before: previous,
|
|
71
80
|
after: current,
|
|
@@ -73,15 +82,81 @@ export function diffSnapshots(before, after) {
|
|
|
73
82
|
];
|
|
74
83
|
const media = diffByKey(before.media, after.media, (item) => item.mediaId, mediaRegistryFields);
|
|
75
84
|
const mediaChanges = [
|
|
76
|
-
...media.added.map((item) => ({ type: "MEDIA_ADDED", media: item, after: item })),
|
|
77
|
-
...media.removed.map((item) => ({ type: "MEDIA_REMOVED", media: item, before: item })),
|
|
85
|
+
...media.added.map((item) => ({ type: "MEDIA_ADDED", itemId: item.mediaId, media: item, after: item })),
|
|
86
|
+
...media.removed.map((item) => ({ type: "MEDIA_REMOVED", itemId: item.mediaId, media: item, before: item })),
|
|
78
87
|
...media.modified.map(({ before: previous, after: current }) => ({
|
|
79
88
|
type: "MEDIA_MODIFIED",
|
|
89
|
+
itemId: current.mediaId,
|
|
80
90
|
media: current,
|
|
81
91
|
before: previous,
|
|
82
92
|
after: current,
|
|
83
93
|
})),
|
|
84
94
|
];
|
|
95
|
+
const changes = [
|
|
96
|
+
...added.map((change) => ({
|
|
97
|
+
scope: "clip",
|
|
98
|
+
type: change.type,
|
|
99
|
+
itemId: change.itemId,
|
|
100
|
+
after: change.after,
|
|
101
|
+
})),
|
|
102
|
+
...removed.map((change) => ({
|
|
103
|
+
scope: "clip",
|
|
104
|
+
type: change.type,
|
|
105
|
+
itemId: change.itemId,
|
|
106
|
+
before: change.before,
|
|
107
|
+
})),
|
|
108
|
+
...modified.map((change) => ({
|
|
109
|
+
scope: "clip",
|
|
110
|
+
type: change.type,
|
|
111
|
+
itemId: change.itemId,
|
|
112
|
+
before: change.before,
|
|
113
|
+
after: change.after,
|
|
114
|
+
})),
|
|
115
|
+
...markerChanges.map((change) => {
|
|
116
|
+
const before = change.before ?? (change.type === "MARKER_REMOVED" ? change.marker : undefined);
|
|
117
|
+
const after = change.after ?? (change.type === "MARKER_ADDED" ? change.marker : undefined);
|
|
118
|
+
return {
|
|
119
|
+
scope: "marker",
|
|
120
|
+
type: change.type,
|
|
121
|
+
itemId: after?.id ?? before?.id ?? change.marker.id,
|
|
122
|
+
...(before ? { before } : {}),
|
|
123
|
+
...(after ? { after } : {}),
|
|
124
|
+
};
|
|
125
|
+
}),
|
|
126
|
+
...captionChanges.map((change) => {
|
|
127
|
+
const before = change.before ?? (change.type === "CAPTION_REMOVED" ? change.caption : undefined);
|
|
128
|
+
const after = change.after ?? (change.type === "CAPTION_ADDED" ? change.caption : undefined);
|
|
129
|
+
return {
|
|
130
|
+
scope: "caption",
|
|
131
|
+
type: change.type,
|
|
132
|
+
itemId: after?.id ?? before?.id ?? change.caption.id,
|
|
133
|
+
...(before ? { before } : {}),
|
|
134
|
+
...(after ? { after } : {}),
|
|
135
|
+
};
|
|
136
|
+
}),
|
|
137
|
+
...storyElementChanges.map((change) => {
|
|
138
|
+
const before = change.before ?? (change.type === "STORY_ELEMENT_REMOVED" ? change.element : undefined);
|
|
139
|
+
const after = change.after ?? (change.type === "STORY_ELEMENT_ADDED" ? change.element : undefined);
|
|
140
|
+
return {
|
|
141
|
+
scope: "story-element",
|
|
142
|
+
type: change.type,
|
|
143
|
+
itemId: after?.id ?? before?.id ?? change.element.id,
|
|
144
|
+
...(before ? { before } : {}),
|
|
145
|
+
...(after ? { after } : {}),
|
|
146
|
+
};
|
|
147
|
+
}),
|
|
148
|
+
...mediaChanges.map((change) => {
|
|
149
|
+
const before = change.before ?? (change.type === "MEDIA_REMOVED" ? change.media : undefined);
|
|
150
|
+
const after = change.after ?? (change.type === "MEDIA_ADDED" ? change.media : undefined);
|
|
151
|
+
return {
|
|
152
|
+
scope: "media",
|
|
153
|
+
type: change.type,
|
|
154
|
+
itemId: after?.mediaId ?? before?.mediaId ?? change.media.mediaId,
|
|
155
|
+
...(before ? { before } : {}),
|
|
156
|
+
...(after ? { after } : {}),
|
|
157
|
+
};
|
|
158
|
+
}),
|
|
159
|
+
].sort(compareTimelineChanges);
|
|
85
160
|
const affectedRanges = uniqueRanges([
|
|
86
161
|
...added.flatMap((change) => rangesForClip(change.after)),
|
|
87
162
|
...removed.flatMap((change) => rangesForClip(change.before)),
|
|
@@ -103,21 +178,105 @@ export function diffSnapshots(before, after) {
|
|
|
103
178
|
end: Math.max(previous ? previous.start + previous.duration : 0, element.start + element.duration),
|
|
104
179
|
}]),
|
|
105
180
|
]);
|
|
181
|
+
const playheadChange = !sameRationalTime(before.playheadTime, after.playheadTime)
|
|
182
|
+
? {
|
|
183
|
+
...(before.playheadTime ? { before: { ...before.playheadTime } } : {}),
|
|
184
|
+
...(after.playheadTime ? { after: { ...after.playheadTime } } : {}),
|
|
185
|
+
}
|
|
186
|
+
: undefined;
|
|
106
187
|
return {
|
|
107
188
|
from: before.revision,
|
|
108
189
|
to: after.revision,
|
|
190
|
+
provenance: {
|
|
191
|
+
source: "project-snapshot",
|
|
192
|
+
projectId: before.projectId,
|
|
193
|
+
sequenceId: before.timeline.id,
|
|
194
|
+
fromRevision: before.revision,
|
|
195
|
+
toRevision: after.revision,
|
|
196
|
+
},
|
|
109
197
|
added,
|
|
110
198
|
removed,
|
|
111
199
|
modified,
|
|
112
200
|
durationDelta: after.timeline.duration - before.timeline.duration,
|
|
113
|
-
durationDeltaTime: decimalToRational(after.timeline.duration
|
|
201
|
+
durationDeltaTime: subtractRationalTimes(after.timeline.durationTime ?? decimalToRational(after.timeline.duration), before.timeline.durationTime ?? decimalToRational(before.timeline.duration), "INVALID_PROJECT_STATE"),
|
|
202
|
+
...(playheadChange ? { playheadChange } : {}),
|
|
114
203
|
markerChanges,
|
|
115
204
|
captionChanges,
|
|
116
205
|
storyElementChanges,
|
|
117
206
|
mediaChanges,
|
|
207
|
+
changes,
|
|
118
208
|
affectedRanges,
|
|
119
209
|
};
|
|
120
210
|
}
|
|
211
|
+
function compareTimelineChanges(left, right) {
|
|
212
|
+
const leftPosition = changePosition(left);
|
|
213
|
+
const rightPosition = changePosition(right);
|
|
214
|
+
if (leftPosition && rightPosition) {
|
|
215
|
+
const comparison = compareRational(leftPosition, rightPosition);
|
|
216
|
+
if (comparison !== 0)
|
|
217
|
+
return comparison;
|
|
218
|
+
}
|
|
219
|
+
else if (leftPosition) {
|
|
220
|
+
return -1;
|
|
221
|
+
}
|
|
222
|
+
else if (rightPosition) {
|
|
223
|
+
return 1;
|
|
224
|
+
}
|
|
225
|
+
const scopeComparison = compareCodePoints(left.scope, right.scope);
|
|
226
|
+
if (scopeComparison !== 0)
|
|
227
|
+
return scopeComparison;
|
|
228
|
+
return compareCodePoints(left.itemId, right.itemId);
|
|
229
|
+
}
|
|
230
|
+
function changePosition(change) {
|
|
231
|
+
const item = change.after ?? change.before;
|
|
232
|
+
if (!item || change.scope === "media")
|
|
233
|
+
return undefined;
|
|
234
|
+
if ("startTime" in item && item.startTime)
|
|
235
|
+
return item.startTime;
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
function compareCodePoints(left, right) {
|
|
239
|
+
const leftPoints = Array.from(left, (character) => character.codePointAt(0));
|
|
240
|
+
const rightPoints = Array.from(right, (character) => character.codePointAt(0));
|
|
241
|
+
const length = Math.min(leftPoints.length, rightPoints.length);
|
|
242
|
+
for (let index = 0; index < length; index += 1) {
|
|
243
|
+
const difference = leftPoints[index] - rightPoints[index];
|
|
244
|
+
if (difference !== 0)
|
|
245
|
+
return difference;
|
|
246
|
+
}
|
|
247
|
+
return leftPoints.length - rightPoints.length;
|
|
248
|
+
}
|
|
249
|
+
function compareRational(left, right) {
|
|
250
|
+
const leftValue = BigInt(left.value) * BigInt(right.timescale);
|
|
251
|
+
const rightValue = BigInt(right.value) * BigInt(left.timescale);
|
|
252
|
+
return leftValue < rightValue ? -1 : leftValue > rightValue ? 1 : 0;
|
|
253
|
+
}
|
|
254
|
+
function assertUniqueKeys(items, keyOf, side) {
|
|
255
|
+
const seen = new Set();
|
|
256
|
+
for (const item of items) {
|
|
257
|
+
const key = keyOf(item);
|
|
258
|
+
if (seen.has(key))
|
|
259
|
+
throw new Error(`AMBIGUOUS_TIMELINE_IDENTITY: duplicate ${side} item ${key}`);
|
|
260
|
+
seen.add(key);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function stableJson(value) {
|
|
264
|
+
if (Array.isArray(value))
|
|
265
|
+
return `[${value.map(stableJson).join(",")}]`;
|
|
266
|
+
if (value && typeof value === "object") {
|
|
267
|
+
return `{${Object.keys(value).sort()
|
|
268
|
+
.filter((key) => value[key] !== undefined)
|
|
269
|
+
.map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(",")}}`;
|
|
270
|
+
}
|
|
271
|
+
return value === undefined ? "undefined" : JSON.stringify(value);
|
|
272
|
+
}
|
|
273
|
+
function sameRationalTime(left, right) {
|
|
274
|
+
if (!left || !right)
|
|
275
|
+
return left === right;
|
|
276
|
+
const leftParts = parseRational(left, "INVALID_PROJECT_STATE");
|
|
277
|
+
const rightParts = parseRational(right, "INVALID_PROJECT_STATE");
|
|
278
|
+
return leftParts.value * rightParts.timescale === rightParts.value * leftParts.timescale;
|
|
279
|
+
}
|
|
121
280
|
function uniqueRanges(ranges) {
|
|
122
281
|
const seen = new Set();
|
|
123
282
|
return ranges.filter((range) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morshoto/framekit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"packageManager": "pnpm@12.5.1",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"test:final-cut-headless": "tsx --test tests/integration/connection.test.ts tests/integration/finalcut-mcp.test.ts tests/integration/native.test.ts",
|
|
39
39
|
"test:final-cut-overlay-headed": "node scripts/final-cut-overlay-headed-e2e.mjs",
|
|
40
40
|
"test:final-cut-canonical-read-headed": "node scripts/final-cut-canonical-read-headed-e2e.mjs",
|
|
41
|
+
"test:final-cut-incremental-sync-headed": "node --import tsx scripts/final-cut-incremental-sync-headed-e2e.mjs",
|
|
41
42
|
"test:final-cut-publisher-headed": "node scripts/final-cut-publisher-headed-e2e.mjs",
|
|
42
43
|
"test:final-cut-project-selection-headed": "node scripts/final-cut-project-selection-headed-e2e.mjs",
|
|
43
44
|
"test:final-cut-canonical-headed": "node scripts/final-cut-canonical-headed-e2e.mjs",
|