@morshoto/framekit 0.1.0 → 0.1.4
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/README.md +42 -17
- package/dist-package/adapters/final-cut/typescript/src/analyzers.js +57 -0
- package/dist-package/adapters/final-cut/typescript/src/connection.js +5 -1
- package/dist-package/adapters/final-cut/typescript/src/export.js +231 -8
- package/dist-package/adapters/final-cut/typescript/src/fcpxml.js +55 -5
- package/dist-package/adapters/final-cut/typescript/src/live.js +2 -2
- package/dist-package/adapters/final-cut/typescript/src/native.js +1000 -100
- package/dist-package/adapters/final-cut/typescript/src/publisher.js +40 -5
- package/dist-package/adapters/final-cut/typescript/src/session.js +21 -5
- package/dist-package/apps/mcp-server/src/main.js +9 -1
- package/dist-package/apps/mcp-server/src/routing.js +172 -0
- package/dist-package/apps/mcp-server/src/server.js +780 -33
- package/dist-package/packages/runtime/src/application/duration-policy-service.js +6 -0
- package/dist-package/packages/runtime/src/application/media-analysis-service.js +370 -0
- package/dist-package/packages/runtime/src/application/project-service.js +127 -0
- package/dist-package/packages/runtime/src/application/runtime-options.js +1 -0
- package/dist-package/packages/runtime/src/application/transaction-store.js +12 -0
- package/dist-package/packages/runtime/src/audio/dialogue-normalization.js +64 -0
- package/dist-package/packages/runtime/src/audio/noise-reduction.js +46 -0
- package/dist-package/packages/runtime/src/capabilities.js +112 -1
- package/dist-package/packages/runtime/src/color-correction.js +48 -0
- package/dist-package/packages/runtime/src/context/context-engine.js +11 -6
- package/dist-package/packages/runtime/src/context/context-service.js +30 -0
- package/dist-package/packages/runtime/src/context/revision.js +3 -0
- package/dist-package/packages/runtime/src/domain/capabilities.js +1 -0
- package/dist-package/packages/runtime/src/domain/context.js +1 -0
- package/dist-package/packages/runtime/src/domain/diff.js +1 -0
- package/dist-package/packages/runtime/src/domain/duration.js +1 -0
- package/dist-package/packages/runtime/src/domain/editing.js +1 -0
- package/dist-package/packages/runtime/src/domain/index.js +12 -0
- package/dist-package/packages/runtime/src/domain/media.js +7 -0
- package/dist-package/packages/runtime/src/domain/ports.js +1 -0
- package/dist-package/packages/runtime/src/domain/primitives.js +1 -0
- package/dist-package/packages/runtime/src/domain/project.js +1 -0
- package/dist-package/packages/runtime/src/domain/rough-cut.js +69 -0
- package/dist-package/packages/runtime/src/domain/skills.js +33 -0
- package/dist-package/packages/runtime/src/domain/types.js +5 -1
- package/dist-package/packages/runtime/src/domain/verification.js +1 -0
- package/dist-package/packages/runtime/src/editing/dialogue-normalization-service.js +141 -0
- package/dist-package/packages/runtime/src/editing/duration-policy.js +187 -0
- package/dist-package/packages/runtime/src/editing/edit-service.js +413 -0
- package/dist-package/packages/runtime/src/editing/filler-removal-service.js +337 -0
- package/dist-package/packages/runtime/src/editing/music-service.js +85 -0
- package/dist-package/packages/runtime/src/index.js +12 -3
- package/dist-package/packages/runtime/src/planning/rough-cut.js +65 -0
- package/dist-package/packages/runtime/src/runtime.js +130 -825
- package/dist-package/packages/runtime/src/skills/builtin.js +339 -0
- package/dist-package/packages/runtime/src/skills/index.js +3 -0
- package/dist-package/packages/runtime/src/skills/registry.js +74 -0
- package/dist-package/packages/runtime/src/skills/requirements.js +170 -0
- package/dist-package/packages/runtime/src/skills/runtime.js +239 -0
- package/dist-package/packages/runtime/src/speech/filler-detector.js +132 -0
- package/dist-package/packages/runtime/src/speech/safe-cut-resolver.js +314 -0
- package/dist-package/packages/runtime/src/timeline/rational-time.js +58 -0
- package/dist-package/packages/runtime/src/verification/verification.js +712 -1
- package/dist-package/packages/testkit/src/fixture-analyzers.js +27 -0
- package/dist-package/packages/testkit/src/in-memory-editor.js +286 -3
- package/dist-package/packages/testkit/src/index.js +1 -0
- package/dist-package/packages/testkit/src/skill-fixtures.js +64 -0
- package/package.json +15 -7
- /package/dist-package/packages/runtime/src/{diff/diff.js → timeline/snapshot-diff.js} +0 -0
- /package/dist-package/packages/runtime/src/{snapshot-digest.js → timeline/snapshot-digest.js} +0 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
const REQUEST_MORE_FOOTAGE = "Provide additional footage or confirm an explicit duration tradeoff";
|
|
2
|
+
/** Create an explicit, deterministic duration plan without mutating an editor. */
|
|
3
|
+
export function planDurationPolicy(request) {
|
|
4
|
+
validateRequest(request);
|
|
5
|
+
const footage = request.footage.map(normalizeFootage);
|
|
6
|
+
const uniqueDurationSeconds = sum(footage.map((item) => item.usableDurationSeconds));
|
|
7
|
+
const reusableDurationSeconds = sum(footage.filter((item) => item.reusable).map((item) => item.usableDurationSeconds));
|
|
8
|
+
const constraint = request.constraint ?? "soft";
|
|
9
|
+
const insufficient = request.requestedDurationSeconds > uniqueDurationSeconds;
|
|
10
|
+
const canReuse = request.permissions?.allowReuse === true && reusableDurationSeconds > 0;
|
|
11
|
+
const selectedAction = !insufficient
|
|
12
|
+
? "deliver-exact-duration"
|
|
13
|
+
: constraint === "hard" && canReuse
|
|
14
|
+
? "reuse-selected-b-roll"
|
|
15
|
+
: constraint === "hard"
|
|
16
|
+
? "request-additional-footage"
|
|
17
|
+
: "deliver-shorter-strong-edit";
|
|
18
|
+
const reusedRanges = selectedAction === "reuse-selected-b-roll"
|
|
19
|
+
? buildReusePlan(footage, request.requestedDurationSeconds - uniqueDurationSeconds)
|
|
20
|
+
: [];
|
|
21
|
+
const achievableDurationSeconds = insufficient && selectedAction !== "reuse-selected-b-roll"
|
|
22
|
+
? uniqueDurationSeconds
|
|
23
|
+
: request.requestedDurationSeconds;
|
|
24
|
+
const unmetConstraints = insufficient && selectedAction !== "reuse-selected-b-roll"
|
|
25
|
+
? ["Requested duration exceeds unique usable footage"]
|
|
26
|
+
: [];
|
|
27
|
+
if (constraint === "hard" && insufficient && selectedAction !== "reuse-selected-b-roll") {
|
|
28
|
+
unmetConstraints.push("Hard duration constraint cannot be met with the selected footage");
|
|
29
|
+
}
|
|
30
|
+
const alternatives = insufficient
|
|
31
|
+
? buildAlternatives({
|
|
32
|
+
request,
|
|
33
|
+
footage,
|
|
34
|
+
uniqueDurationSeconds,
|
|
35
|
+
reusableDurationSeconds,
|
|
36
|
+
recommendedAction: selectedAction,
|
|
37
|
+
})
|
|
38
|
+
: [];
|
|
39
|
+
const actualDurationSeconds = request.actualDurationSeconds ?? null;
|
|
40
|
+
const confirmationRequired = selectedAction === "request-additional-footage"
|
|
41
|
+
? [REQUEST_MORE_FOOTAGE]
|
|
42
|
+
: [];
|
|
43
|
+
return {
|
|
44
|
+
policy: {
|
|
45
|
+
constraint,
|
|
46
|
+
constraintWasExplicit: request.constraint !== undefined,
|
|
47
|
+
},
|
|
48
|
+
requestedDurationSeconds: request.requestedDurationSeconds,
|
|
49
|
+
availableFootage: {
|
|
50
|
+
uniqueDurationSeconds,
|
|
51
|
+
reusableDurationSeconds,
|
|
52
|
+
},
|
|
53
|
+
achievableDurationSeconds,
|
|
54
|
+
actualDurationSeconds,
|
|
55
|
+
durationReport: {
|
|
56
|
+
requestedDurationSeconds: request.requestedDurationSeconds,
|
|
57
|
+
achievableDurationSeconds,
|
|
58
|
+
actualDurationSeconds,
|
|
59
|
+
},
|
|
60
|
+
selectedAction,
|
|
61
|
+
reusedRanges,
|
|
62
|
+
unmetConstraints,
|
|
63
|
+
confirmationRequired,
|
|
64
|
+
alternatives,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function buildAlternatives(input) {
|
|
68
|
+
const { request, footage, uniqueDurationSeconds, reusableDurationSeconds, recommendedAction } = input;
|
|
69
|
+
const reuseRanges = reusableDurationSeconds > 0
|
|
70
|
+
? buildReusePlan(footage, request.requestedDurationSeconds - uniqueDurationSeconds)
|
|
71
|
+
: [];
|
|
72
|
+
return [
|
|
73
|
+
{
|
|
74
|
+
kind: "deliver-shorter-strong-edit",
|
|
75
|
+
status: recommendedAction === "deliver-shorter-strong-edit" ? "recommended" : "available",
|
|
76
|
+
resultingDurationSeconds: uniqueDurationSeconds,
|
|
77
|
+
tradeoffs: ["Preserves the strongest unique footage", "Does not satisfy the requested duration"],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
kind: "reuse-selected-b-roll",
|
|
81
|
+
status: request.permissions?.allowReuse && reusableDurationSeconds > 0 ? "available" : "requires-confirmation",
|
|
82
|
+
resultingDurationSeconds: reuseRanges.length > 0 ? request.requestedDurationSeconds : null,
|
|
83
|
+
tradeoffs: ["Meets the requested duration", "Repeats only the explicitly identified B-roll ranges"],
|
|
84
|
+
...(reuseRanges.length > 0 ? { reusedRanges: reuseRanges } : {}),
|
|
85
|
+
...(request.permissions?.allowReuse !== true ? { confirmationRequired: "Confirm intentional B-roll reuse" } : {}),
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
kind: "slow-motion",
|
|
89
|
+
status: "requires-confirmation",
|
|
90
|
+
resultingDurationSeconds: request.requestedDurationSeconds,
|
|
91
|
+
tradeoffs: ["May preserve coverage without adding footage", "Can harm motion quality or editorial pacing"],
|
|
92
|
+
confirmationRequired: "Confirm which footage is editorially appropriate for slow motion",
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
kind: "request-additional-footage",
|
|
96
|
+
status: "available",
|
|
97
|
+
resultingDurationSeconds: request.requestedDurationSeconds,
|
|
98
|
+
tradeoffs: ["Preserves natural playback speed and unique coverage", "Requires more source material"],
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
kind: "generated-interstitial",
|
|
102
|
+
status: request.permissions?.allowGeneratedAssets === true ? "requires-confirmation" : "not-permitted",
|
|
103
|
+
resultingDurationSeconds: request.requestedDurationSeconds,
|
|
104
|
+
tradeoffs: ["Can fill the remaining duration", "Introduces generated or external content"],
|
|
105
|
+
confirmationRequired: "Confirm generated or interstitial assets are allowed",
|
|
106
|
+
},
|
|
107
|
+
];
|
|
108
|
+
}
|
|
109
|
+
function normalizeFootage(item) {
|
|
110
|
+
const usableRanges = item.usableRanges?.map((range) => ({ ...range })).sort((left, right) => left.startSeconds - right.startSeconds) ?? [{
|
|
111
|
+
startSeconds: 0,
|
|
112
|
+
endSeconds: item.usableDurationSeconds ?? item.durationSeconds,
|
|
113
|
+
}];
|
|
114
|
+
for (const [index, range] of usableRanges.entries()) {
|
|
115
|
+
if (!Number.isFinite(range.startSeconds) || !Number.isFinite(range.endSeconds)
|
|
116
|
+
|| range.startSeconds < 0 || range.endSeconds <= range.startSeconds || range.endSeconds > item.durationSeconds) {
|
|
117
|
+
throw new Error(`INVALID_DURATION_FOOTAGE: invalid usable range for ${item.id}`);
|
|
118
|
+
}
|
|
119
|
+
const previous = usableRanges[index - 1];
|
|
120
|
+
if (previous && range.startSeconds < previous.endSeconds) {
|
|
121
|
+
throw new Error(`INVALID_DURATION_FOOTAGE: usable ranges overlap for ${item.id}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const usableDurationSeconds = sum(usableRanges.map((range) => range.endSeconds - range.startSeconds));
|
|
125
|
+
return { ...item, usableRanges, usableDurationSeconds };
|
|
126
|
+
}
|
|
127
|
+
function buildReusePlan(footage, requestedAdditionalSeconds) {
|
|
128
|
+
if (requestedAdditionalSeconds <= 0)
|
|
129
|
+
return [];
|
|
130
|
+
const reusable = footage.filter((item) => item.reusable);
|
|
131
|
+
const result = [];
|
|
132
|
+
let remaining = requestedAdditionalSeconds;
|
|
133
|
+
let occurrence = 1;
|
|
134
|
+
while (remaining > 0 && reusable.length > 0) {
|
|
135
|
+
let addedThisPass = 0;
|
|
136
|
+
for (const item of reusable) {
|
|
137
|
+
for (const range of item.usableRanges) {
|
|
138
|
+
if (remaining <= 0)
|
|
139
|
+
break;
|
|
140
|
+
const length = range.endSeconds - range.startSeconds;
|
|
141
|
+
const selectedLength = Math.min(length, remaining);
|
|
142
|
+
result.push({
|
|
143
|
+
footageId: item.id,
|
|
144
|
+
sourceRange: {
|
|
145
|
+
startSeconds: range.startSeconds,
|
|
146
|
+
endSeconds: range.startSeconds + selectedLength,
|
|
147
|
+
},
|
|
148
|
+
occurrence,
|
|
149
|
+
});
|
|
150
|
+
remaining -= selectedLength;
|
|
151
|
+
addedThisPass += selectedLength;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (addedThisPass === 0)
|
|
155
|
+
break;
|
|
156
|
+
occurrence += 1;
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
function validateRequest(request) {
|
|
161
|
+
if (!Number.isFinite(request.requestedDurationSeconds) || request.requestedDurationSeconds <= 0) {
|
|
162
|
+
throw new Error("INVALID_DURATION_POLICY: requested duration must be positive");
|
|
163
|
+
}
|
|
164
|
+
if (request.actualDurationSeconds !== undefined
|
|
165
|
+
&& (!Number.isFinite(request.actualDurationSeconds) || request.actualDurationSeconds < 0)) {
|
|
166
|
+
throw new Error("INVALID_DURATION_POLICY: actual duration must be non-negative");
|
|
167
|
+
}
|
|
168
|
+
const footageIds = new Set();
|
|
169
|
+
for (const item of request.footage) {
|
|
170
|
+
const normalizedId = item.id.trim();
|
|
171
|
+
if (!item.id.trim() || !Number.isFinite(item.durationSeconds) || item.durationSeconds <= 0) {
|
|
172
|
+
throw new Error("INVALID_DURATION_FOOTAGE: id and positive duration are required");
|
|
173
|
+
}
|
|
174
|
+
if (footageIds.has(normalizedId)) {
|
|
175
|
+
throw new Error(`INVALID_DURATION_FOOTAGE: duplicate footage id ${normalizedId}`);
|
|
176
|
+
}
|
|
177
|
+
footageIds.add(normalizedId);
|
|
178
|
+
if (item.usableDurationSeconds !== undefined
|
|
179
|
+
&& (!Number.isFinite(item.usableDurationSeconds) || item.usableDurationSeconds <= 0
|
|
180
|
+
|| item.usableDurationSeconds > item.durationSeconds)) {
|
|
181
|
+
throw new Error(`INVALID_DURATION_FOOTAGE: usable duration exceeds source duration for ${item.id}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function sum(values) {
|
|
186
|
+
return values.reduce((total, value) => total + value, 0);
|
|
187
|
+
}
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { resolve as resolvePath } from "node:path";
|
|
3
|
+
import { diffSnapshots } from "../timeline/snapshot-diff.js";
|
|
4
|
+
import { canonicalSnapshotDigest } from "../timeline/snapshot-digest.js";
|
|
5
|
+
import { sameRevision } from "../context/revision.js";
|
|
6
|
+
import { assertValidVerificationPolicy } from "../verification/verification.js";
|
|
7
|
+
export class EditService {
|
|
8
|
+
adapter;
|
|
9
|
+
project;
|
|
10
|
+
analysis;
|
|
11
|
+
verificationEngine;
|
|
12
|
+
options;
|
|
13
|
+
transactions;
|
|
14
|
+
editPreviews = new Map();
|
|
15
|
+
constructor(adapter, project, analysis, verificationEngine, options, transactions) {
|
|
16
|
+
this.adapter = adapter;
|
|
17
|
+
this.project = project;
|
|
18
|
+
this.analysis = analysis;
|
|
19
|
+
this.verificationEngine = verificationEngine;
|
|
20
|
+
this.options = options;
|
|
21
|
+
this.transactions = transactions;
|
|
22
|
+
}
|
|
23
|
+
async edit(operation, policy = {}) {
|
|
24
|
+
const verificationPolicy = structuredClone(policy);
|
|
25
|
+
assertValidVerificationPolicy(verificationPolicy);
|
|
26
|
+
const before = await this.project.inspectProject();
|
|
27
|
+
const capabilities = await this.adapter.getCapabilities();
|
|
28
|
+
const target = await this.defaultTarget(before, capabilities);
|
|
29
|
+
return this.editWithTarget(operation, verificationPolicy, target, before, capabilities);
|
|
30
|
+
}
|
|
31
|
+
async editArtifact(targetInput, operation, policy = {}) {
|
|
32
|
+
const target = await this.artifactTarget(targetInput);
|
|
33
|
+
return this.editWithTarget(operation, policy, target);
|
|
34
|
+
}
|
|
35
|
+
async editTimeline(targetInput, operation, policy = {}) {
|
|
36
|
+
const target = this.timelineTarget(targetInput);
|
|
37
|
+
if (!operation.baseRevision)
|
|
38
|
+
throw new Error("INVALID_TIMELINE_TARGET: baseRevision is required");
|
|
39
|
+
return this.editWithTarget(operation, policy, target);
|
|
40
|
+
}
|
|
41
|
+
async editWithTarget(operation, policy, target, existingBefore, existingCapabilities) {
|
|
42
|
+
const verificationPolicy = structuredClone(policy);
|
|
43
|
+
assertValidVerificationPolicy(verificationPolicy);
|
|
44
|
+
const before = existingBefore ?? await this.project.inspectProject();
|
|
45
|
+
const capabilities = existingCapabilities ?? await this.adapter.getCapabilities();
|
|
46
|
+
this.assertTarget(target, before);
|
|
47
|
+
if (operation.baseRevision && !sameRevision(operation.baseRevision, before.revision)) {
|
|
48
|
+
throw new Error("STALE_CONTEXT: operation base revision does not match current editor state");
|
|
49
|
+
}
|
|
50
|
+
this.assertSurfaceCapability(target, capabilities.editor);
|
|
51
|
+
if (!capabilities.editor.timelineSnapshotRead || !capabilities.editor.readAfterWrite || !capabilities.editor.rollback) {
|
|
52
|
+
throw new Error(`CAPABILITY_UNAVAILABLE: ${target.kind === "artifact" ? "artifact" : "editor timeline"} mutation requires snapshot, read-after-write, and rollback`);
|
|
53
|
+
}
|
|
54
|
+
const appliedRevision = await this.adapter.apply(operation, before.revision);
|
|
55
|
+
let attemptedAfter;
|
|
56
|
+
try {
|
|
57
|
+
attemptedAfter = await this.project.inspectProject();
|
|
58
|
+
}
|
|
59
|
+
catch (readError) {
|
|
60
|
+
try {
|
|
61
|
+
await this.adapter.restore(before, appliedRevision);
|
|
62
|
+
this.assertRestored(before, await this.project.inspectProject());
|
|
63
|
+
}
|
|
64
|
+
catch (rollbackError) {
|
|
65
|
+
throw new Error(`READ_AFTER_WRITE_FAILED: compensating rollback failed (${String(readError)}; ${String(rollbackError)})`);
|
|
66
|
+
}
|
|
67
|
+
throw new Error(`READ_AFTER_WRITE_FAILED: canonical state was restored (${String(readError)})`);
|
|
68
|
+
}
|
|
69
|
+
const artifactDigest = await this.readArtifactDigest(target);
|
|
70
|
+
const transaction = {
|
|
71
|
+
id: `txn-${randomUUID()}`,
|
|
72
|
+
target: structuredClone(target),
|
|
73
|
+
...(artifactDigest ? { artifactDigest } : {}),
|
|
74
|
+
operation,
|
|
75
|
+
intent: operation.type,
|
|
76
|
+
planned: [operation],
|
|
77
|
+
applied: [operation],
|
|
78
|
+
baseRevision: before.revision,
|
|
79
|
+
before,
|
|
80
|
+
after: attemptedAfter,
|
|
81
|
+
attemptedAfter,
|
|
82
|
+
diff: diffSnapshots(before, attemptedAfter),
|
|
83
|
+
verificationPolicy,
|
|
84
|
+
status: "APPLIED",
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
transaction.attemptedAfter = await this.analysis.reanalyzeAffectedRanges(transaction);
|
|
88
|
+
transaction.after = transaction.attemptedAfter;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
await this.adapter.restore(before, attemptedAfter.revision);
|
|
92
|
+
this.assertRestored(before, await this.project.inspectProject());
|
|
93
|
+
throw new Error(`ANALYSIS_FAILED: post-write verification analysis failed (${String(error)})`);
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
transaction.verification = {
|
|
97
|
+
...(await this.verificationEngine.verify(transaction, verificationPolicy)),
|
|
98
|
+
target: structuredClone(target),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (verificationError) {
|
|
102
|
+
try {
|
|
103
|
+
await this.adapter.restore(before, attemptedAfter.revision);
|
|
104
|
+
this.assertRestored(before, await this.project.inspectProject());
|
|
105
|
+
}
|
|
106
|
+
catch (rollbackError) {
|
|
107
|
+
throw new Error(`VERIFICATION_FAILED: compensating rollback failed (${String(verificationError)}; ${String(rollbackError)})`);
|
|
108
|
+
}
|
|
109
|
+
throw new Error(`VERIFICATION_FAILED: canonical state was restored (${String(verificationError)})`);
|
|
110
|
+
}
|
|
111
|
+
if (transaction.verification.passed) {
|
|
112
|
+
transaction.status = "VERIFIED";
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
await this.adapter.restore(before, attemptedAfter.revision);
|
|
116
|
+
transaction.after = await this.project.inspectProject();
|
|
117
|
+
this.assertRestored(before, transaction.after);
|
|
118
|
+
transaction.status = "ROLLED_BACK";
|
|
119
|
+
}
|
|
120
|
+
this.transactions.set(transaction);
|
|
121
|
+
return transaction;
|
|
122
|
+
}
|
|
123
|
+
async previewEdit(request) {
|
|
124
|
+
const verification = structuredClone(request.verification ?? {});
|
|
125
|
+
assertValidVerificationPolicy(verification);
|
|
126
|
+
const before = await this.project.inspectProject();
|
|
127
|
+
const capabilities = await this.adapter.getCapabilities();
|
|
128
|
+
const target = await this.defaultTarget(before, capabilities);
|
|
129
|
+
return this.previewEditWithTarget(request, target, before, capabilities);
|
|
130
|
+
}
|
|
131
|
+
async previewArtifactEdit(targetInput, request) {
|
|
132
|
+
const target = await this.artifactTarget(targetInput);
|
|
133
|
+
return this.previewEditWithTarget(request, target);
|
|
134
|
+
}
|
|
135
|
+
async previewTimelineEdit(targetInput, request) {
|
|
136
|
+
const target = this.timelineTarget(targetInput);
|
|
137
|
+
return this.previewEditWithTarget(request, target);
|
|
138
|
+
}
|
|
139
|
+
async previewEditWithTarget(request, target, existingBefore, existingCapabilities) {
|
|
140
|
+
const verification = structuredClone(request.verification ?? {});
|
|
141
|
+
assertValidVerificationPolicy(verification);
|
|
142
|
+
const before = existingBefore ?? await this.project.inspectProject();
|
|
143
|
+
const capabilities = existingCapabilities ?? await this.adapter.getCapabilities();
|
|
144
|
+
this.assertTarget(target, before);
|
|
145
|
+
if (!sameRevision(request.baseRevision, before.revision)) {
|
|
146
|
+
throw new Error("STALE_CONTEXT: preview base revision does not match current editor state");
|
|
147
|
+
}
|
|
148
|
+
if (request.operations.length === 0)
|
|
149
|
+
throw new Error("INVALID_OPERATION: composite edit requires operations");
|
|
150
|
+
await this.assertCompositeCapabilities(request.operations, target.kind, capabilities.editor);
|
|
151
|
+
if (!this.adapter.previewTransaction) {
|
|
152
|
+
throw new Error("CAPABILITY_UNAVAILABLE: editor composite transaction preview");
|
|
153
|
+
}
|
|
154
|
+
const expectedAfter = await this.adapter.previewTransaction(request.operations, before.revision);
|
|
155
|
+
const previewToken = `preview-${randomUUID()}`;
|
|
156
|
+
const preview = {
|
|
157
|
+
previewToken,
|
|
158
|
+
target: structuredClone(target),
|
|
159
|
+
baseRevision: structuredClone(before.revision),
|
|
160
|
+
operations: structuredClone(request.operations),
|
|
161
|
+
expectedDiff: diffSnapshots(before, expectedAfter),
|
|
162
|
+
warnings: [],
|
|
163
|
+
expiresAt: new Date(this.now() + (this.options.previewTtlMs ?? 30_000)).toISOString(),
|
|
164
|
+
verification,
|
|
165
|
+
};
|
|
166
|
+
this.pruneEditPreviews();
|
|
167
|
+
const maxActivePreviews = Number.isInteger(this.options.maxActivePreviews) && this.options.maxActivePreviews > 0
|
|
168
|
+
? this.options.maxActivePreviews
|
|
169
|
+
: 128;
|
|
170
|
+
while (this.editPreviews.size >= maxActivePreviews) {
|
|
171
|
+
const oldestToken = this.editPreviews.keys().next().value;
|
|
172
|
+
if (oldestToken === undefined)
|
|
173
|
+
break;
|
|
174
|
+
this.editPreviews.delete(oldestToken);
|
|
175
|
+
}
|
|
176
|
+
this.editPreviews.set(previewToken, preview);
|
|
177
|
+
return structuredClone(preview);
|
|
178
|
+
}
|
|
179
|
+
async executeEdit(previewToken, policy = {}) {
|
|
180
|
+
const requestedPolicy = structuredClone(policy);
|
|
181
|
+
assertValidVerificationPolicy(requestedPolicy);
|
|
182
|
+
const preview = this.editPreviews.get(previewToken);
|
|
183
|
+
if (!preview)
|
|
184
|
+
throw new Error(`PREVIEW_TOKEN_INVALID: unknown or already used preview ${previewToken}`);
|
|
185
|
+
this.editPreviews.delete(previewToken);
|
|
186
|
+
if (this.now() > Date.parse(preview.expiresAt)) {
|
|
187
|
+
throw new Error("PREVIEW_TOKEN_EXPIRED: composite edit preview has expired");
|
|
188
|
+
}
|
|
189
|
+
const before = await this.project.inspectProject();
|
|
190
|
+
if (!sameRevision(preview.baseRevision, before.revision)) {
|
|
191
|
+
throw new Error("STALE_CONTEXT: preview base revision does not match current editor state");
|
|
192
|
+
}
|
|
193
|
+
this.assertTarget(preview.target, before);
|
|
194
|
+
await this.assertCompositeCapabilities(preview.operations, preview.target.kind);
|
|
195
|
+
if (!this.adapter.applyTransaction) {
|
|
196
|
+
throw new Error("CAPABILITY_UNAVAILABLE: editor composite transaction execution");
|
|
197
|
+
}
|
|
198
|
+
const verificationPolicy = Object.keys(requestedPolicy).length > 0
|
|
199
|
+
? { ...structuredClone(preview.verification ?? {}), ...requestedPolicy }
|
|
200
|
+
: structuredClone(preview.verification ?? {});
|
|
201
|
+
assertValidVerificationPolicy(verificationPolicy);
|
|
202
|
+
try {
|
|
203
|
+
await this.adapter.applyTransaction(preview.operations, before.revision);
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
const partiallyApplied = await this.project.inspectProject();
|
|
207
|
+
if (!sameRevision(partiallyApplied.revision, before.revision)) {
|
|
208
|
+
try {
|
|
209
|
+
await this.adapter.restore(before, partiallyApplied.revision);
|
|
210
|
+
}
|
|
211
|
+
catch (rollbackError) {
|
|
212
|
+
throw new Error(`TRANSACTION_FAILED: ${String(error)}; rollback failed: ${String(rollbackError)}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
throw new Error(`TRANSACTION_FAILED: ${String(error)}; transaction was rolled back`);
|
|
216
|
+
}
|
|
217
|
+
const attemptedAfter = await this.project.inspectProject();
|
|
218
|
+
const artifactDigest = await this.readArtifactDigest(preview.target);
|
|
219
|
+
const transaction = {
|
|
220
|
+
id: `txn-${randomUUID()}`,
|
|
221
|
+
target: structuredClone(preview.target),
|
|
222
|
+
...(artifactDigest ? { artifactDigest } : {}),
|
|
223
|
+
intent: "composite-edit",
|
|
224
|
+
planned: structuredClone(preview.operations),
|
|
225
|
+
applied: structuredClone(preview.operations),
|
|
226
|
+
baseRevision: before.revision,
|
|
227
|
+
before,
|
|
228
|
+
after: attemptedAfter,
|
|
229
|
+
attemptedAfter,
|
|
230
|
+
diff: diffSnapshots(before, attemptedAfter),
|
|
231
|
+
verificationPolicy,
|
|
232
|
+
status: "APPLIED",
|
|
233
|
+
};
|
|
234
|
+
try {
|
|
235
|
+
transaction.attemptedAfter = await this.analysis.reanalyzeAffectedRanges(transaction);
|
|
236
|
+
transaction.after = transaction.attemptedAfter;
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
await this.adapter.restore(before, attemptedAfter.revision);
|
|
240
|
+
this.assertRestored(before, await this.project.inspectProject());
|
|
241
|
+
throw new Error(`ANALYSIS_FAILED: post-write verification analysis failed (${String(error)})`);
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
transaction.verification = {
|
|
245
|
+
...(await this.verificationEngine.verify(transaction, verificationPolicy)),
|
|
246
|
+
target: structuredClone(preview.target),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
catch (verificationError) {
|
|
250
|
+
try {
|
|
251
|
+
await this.adapter.restore(before, attemptedAfter.revision);
|
|
252
|
+
this.assertRestored(before, await this.project.inspectProject());
|
|
253
|
+
}
|
|
254
|
+
catch (rollbackError) {
|
|
255
|
+
throw new Error(`VERIFICATION_FAILED: compensating rollback failed (${String(verificationError)}; ${String(rollbackError)})`);
|
|
256
|
+
}
|
|
257
|
+
throw new Error(`VERIFICATION_FAILED: canonical state was restored (${String(verificationError)})`);
|
|
258
|
+
}
|
|
259
|
+
if (transaction.verification.passed) {
|
|
260
|
+
transaction.status = "VERIFIED";
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
await this.adapter.restore(before, attemptedAfter.revision);
|
|
264
|
+
transaction.after = await this.project.inspectProject();
|
|
265
|
+
this.assertRestored(before, transaction.after);
|
|
266
|
+
transaction.status = "ROLLED_BACK";
|
|
267
|
+
}
|
|
268
|
+
this.transactions.set(transaction);
|
|
269
|
+
return transaction;
|
|
270
|
+
}
|
|
271
|
+
getDiff(transactionId) {
|
|
272
|
+
return this.getTransaction(transactionId).diff;
|
|
273
|
+
}
|
|
274
|
+
getTransaction(transactionId) {
|
|
275
|
+
return this.transactions.get(transactionId);
|
|
276
|
+
}
|
|
277
|
+
async verifyTransaction(transactionId) {
|
|
278
|
+
return this.getTransaction(transactionId).verification;
|
|
279
|
+
}
|
|
280
|
+
async undo(transactionId) {
|
|
281
|
+
const transaction = this.getTransaction(transactionId);
|
|
282
|
+
const current = await this.project.inspectProject();
|
|
283
|
+
if (current.projectId !== transaction.before.projectId || current.timeline.id !== transaction.before.timeline.id) {
|
|
284
|
+
throw new Error(`TARGET_MISMATCH: cannot undo ${transaction.before.projectId}/${transaction.before.timeline.id} while ${current.projectId}/${current.timeline.id} is active`);
|
|
285
|
+
}
|
|
286
|
+
await this.adapter.restore(transaction.before, current.revision);
|
|
287
|
+
const restored = await this.project.inspectProject();
|
|
288
|
+
this.assertRestored(transaction.before, restored);
|
|
289
|
+
return restored;
|
|
290
|
+
}
|
|
291
|
+
assertRestored(expected, actual) {
|
|
292
|
+
if (canonicalSnapshotDigest(expected) !== canonicalSnapshotDigest(actual)) {
|
|
293
|
+
throw new Error("ROLLBACK_FAILED: restored canonical digest does not match pre-edit state");
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
async assertCompositeCapabilities(operations, targetKind, existingCapabilities) {
|
|
297
|
+
const capabilities = existingCapabilities ?? (await this.adapter.getCapabilities()).editor;
|
|
298
|
+
if (!capabilities.compositeTransactions || !capabilities.readAfterWrite || !capabilities.rollback) {
|
|
299
|
+
throw new Error("CAPABILITY_UNAVAILABLE: editor composite transactions");
|
|
300
|
+
}
|
|
301
|
+
if (operations.some((operation) => operation.type === "media.import") && !capabilities.mediaImport) {
|
|
302
|
+
throw new Error("CAPABILITY_UNAVAILABLE: media import");
|
|
303
|
+
}
|
|
304
|
+
if (operations.some((operation) => operation.type === "timeline.media.add") && !capabilities.mediaPlacement) {
|
|
305
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline media placement");
|
|
306
|
+
}
|
|
307
|
+
if (operations.some((operation) => operation.type === "timeline.title.add")
|
|
308
|
+
&& (!capabilities.titlePlacement || !capabilities.assetDiscovery)) {
|
|
309
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline title placement");
|
|
310
|
+
}
|
|
311
|
+
if (operations.some((operation) => operation.type !== "media.import")) {
|
|
312
|
+
if (targetKind === "artifact" && !capabilities.timelineArtifactWrite) {
|
|
313
|
+
throw new Error("CAPABILITY_UNAVAILABLE: artifact mutation");
|
|
314
|
+
}
|
|
315
|
+
if (targetKind === "editor.timeline" && !capabilities.timelineWrite) {
|
|
316
|
+
throw new Error("CAPABILITY_UNAVAILABLE: editor timeline mutation");
|
|
317
|
+
}
|
|
318
|
+
if (!targetKind && !capabilities.timelineWrite && !capabilities.timelineArtifactWrite) {
|
|
319
|
+
throw new Error("CAPABILITY_UNAVAILABLE: editor timeline mutation");
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (operations.some((operation) => operation.type === "timeline.media.move") && !capabilities.clipMove) {
|
|
323
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline media move");
|
|
324
|
+
}
|
|
325
|
+
if (operations.some((operation) => operation.type === "timeline.media.replace") && !capabilities.clipReplace) {
|
|
326
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline media replace");
|
|
327
|
+
}
|
|
328
|
+
if (operations.some((operation) => operation.type === "timeline.media.remove") && !capabilities.clipRemoval) {
|
|
329
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline media removal");
|
|
330
|
+
}
|
|
331
|
+
if (operations.some((operation) => operation.type === "timeline.transition.add")
|
|
332
|
+
&& (!capabilities.transitionPlacement || !capabilities.assetDiscovery)) {
|
|
333
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline transition placement");
|
|
334
|
+
}
|
|
335
|
+
if (operations.some((operation) => operation.type === "timeline.audio.attach") && !capabilities.audioAttachment) {
|
|
336
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline audio attachment");
|
|
337
|
+
}
|
|
338
|
+
if (operations.some((operation) => operation.type === "timeline.audio.mix") && !capabilities.audioMixing) {
|
|
339
|
+
throw new Error("CAPABILITY_UNAVAILABLE: timeline audio mixing");
|
|
340
|
+
}
|
|
341
|
+
if (operations.some((operation) => operation.type === "reduce-noise") && !capabilities.noiseReduction) {
|
|
342
|
+
throw new Error("CAPABILITY_UNAVAILABLE: noise reduction effect");
|
|
343
|
+
}
|
|
344
|
+
if (operations.some((operation) => operation.type === "set-color-correction") && !capabilities.colorCorrection) {
|
|
345
|
+
throw new Error("CAPABILITY_UNAVAILABLE: color correction");
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
async defaultTarget(before, capabilities) {
|
|
349
|
+
if (capabilities.editor.timelineArtifactWrite && !capabilities.editor.timelineWrite) {
|
|
350
|
+
if (!this.adapter.getManagedArtifact) {
|
|
351
|
+
throw new Error("CAPABILITY_UNAVAILABLE: managed FCPXML artifact");
|
|
352
|
+
}
|
|
353
|
+
return this.artifactDescriptor(await this.adapter.getManagedArtifact());
|
|
354
|
+
}
|
|
355
|
+
return {
|
|
356
|
+
kind: "editor.timeline",
|
|
357
|
+
projectId: before.projectId,
|
|
358
|
+
sequenceId: before.timeline.id,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
async artifactTarget(input) {
|
|
362
|
+
if (!input.artifactPath.trim())
|
|
363
|
+
throw new Error("INVALID_ARTIFACT_TARGET: artifactPath is required");
|
|
364
|
+
if (!this.adapter.getManagedArtifact) {
|
|
365
|
+
throw new Error("CAPABILITY_UNAVAILABLE: managed FCPXML artifact");
|
|
366
|
+
}
|
|
367
|
+
const managed = await this.adapter.getManagedArtifact();
|
|
368
|
+
if (resolvePath(managed.path) !== resolvePath(input.artifactPath.trim())) {
|
|
369
|
+
throw new Error(`TARGET_MISMATCH: requested artifact ${input.artifactPath} is not managed by this adapter`);
|
|
370
|
+
}
|
|
371
|
+
return this.artifactDescriptor(managed);
|
|
372
|
+
}
|
|
373
|
+
artifactDescriptor(artifact) {
|
|
374
|
+
return { kind: "artifact", artifactId: artifact.id, artifactPath: artifact.path };
|
|
375
|
+
}
|
|
376
|
+
async readArtifactDigest(target) {
|
|
377
|
+
if (target.kind !== "artifact" || !this.adapter.getManagedArtifactDigest)
|
|
378
|
+
return undefined;
|
|
379
|
+
return this.adapter.getManagedArtifactDigest();
|
|
380
|
+
}
|
|
381
|
+
timelineTarget(input) {
|
|
382
|
+
const projectId = input.projectId.trim();
|
|
383
|
+
const sequenceId = input.sequenceId.trim();
|
|
384
|
+
if (!projectId || !sequenceId) {
|
|
385
|
+
throw new Error("INVALID_TIMELINE_TARGET: projectId and sequenceId are required");
|
|
386
|
+
}
|
|
387
|
+
return { kind: "editor.timeline", projectId, sequenceId };
|
|
388
|
+
}
|
|
389
|
+
assertTarget(target, snapshot) {
|
|
390
|
+
if (target.kind === "editor.timeline"
|
|
391
|
+
&& (target.projectId !== snapshot.projectId || target.sequenceId !== snapshot.timeline.id)) {
|
|
392
|
+
throw new Error(`TARGET_MISMATCH: requested ${target.projectId}/${target.sequenceId} while ${snapshot.projectId}/${snapshot.timeline.id} is active`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
assertSurfaceCapability(target, capabilities) {
|
|
396
|
+
if (target.kind === "artifact" && !capabilities.timelineArtifactWrite) {
|
|
397
|
+
throw new Error("CAPABILITY_UNAVAILABLE: artifact mutation");
|
|
398
|
+
}
|
|
399
|
+
if (target.kind === "editor.timeline" && !capabilities.timelineWrite) {
|
|
400
|
+
throw new Error("CAPABILITY_UNAVAILABLE: editor timeline mutation");
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
now() {
|
|
404
|
+
return this.options.now?.() ?? Date.now();
|
|
405
|
+
}
|
|
406
|
+
pruneEditPreviews() {
|
|
407
|
+
const now = this.now();
|
|
408
|
+
for (const [previewToken, preview] of this.editPreviews) {
|
|
409
|
+
if (now > Date.parse(preview.expiresAt))
|
|
410
|
+
this.editPreviews.delete(previewToken);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|