@morshoto/framekit 0.1.0 → 0.1.5

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.
Files changed (65) hide show
  1. package/README.md +42 -17
  2. package/dist-package/adapters/final-cut/typescript/src/analyzers.js +86 -2
  3. package/dist-package/adapters/final-cut/typescript/src/connection.js +5 -1
  4. package/dist-package/adapters/final-cut/typescript/src/export.js +231 -8
  5. package/dist-package/adapters/final-cut/typescript/src/fcpxml.js +70 -9
  6. package/dist-package/adapters/final-cut/typescript/src/live.js +2 -2
  7. package/dist-package/adapters/final-cut/typescript/src/native.js +1000 -100
  8. package/dist-package/adapters/final-cut/typescript/src/publisher.js +242 -30
  9. package/dist-package/adapters/final-cut/typescript/src/session.js +86 -9
  10. package/dist-package/apps/mcp-server/src/main.js +10 -1
  11. package/dist-package/apps/mcp-server/src/routing.js +172 -0
  12. package/dist-package/apps/mcp-server/src/server.js +867 -52
  13. package/dist-package/apps/mcp-server/src/version.js +34 -0
  14. package/dist-package/packages/runtime/src/application/duration-policy-service.js +6 -0
  15. package/dist-package/packages/runtime/src/application/media-analysis-service.js +473 -0
  16. package/dist-package/packages/runtime/src/application/project-service.js +146 -0
  17. package/dist-package/packages/runtime/src/application/runtime-options.js +1 -0
  18. package/dist-package/packages/runtime/src/application/transaction-store.js +12 -0
  19. package/dist-package/packages/runtime/src/audio/dialogue-normalization.js +64 -0
  20. package/dist-package/packages/runtime/src/audio/noise-reduction.js +46 -0
  21. package/dist-package/packages/runtime/src/capabilities.js +187 -1
  22. package/dist-package/packages/runtime/src/color-correction.js +48 -0
  23. package/dist-package/packages/runtime/src/context/context-engine.js +20 -6
  24. package/dist-package/packages/runtime/src/context/context-service.js +30 -0
  25. package/dist-package/packages/runtime/src/context/revision.js +3 -0
  26. package/dist-package/packages/runtime/src/domain/capabilities.js +1 -0
  27. package/dist-package/packages/runtime/src/domain/context.js +1 -0
  28. package/dist-package/packages/runtime/src/domain/diff.js +1 -0
  29. package/dist-package/packages/runtime/src/domain/duration.js +1 -0
  30. package/dist-package/packages/runtime/src/domain/editing.js +1 -0
  31. package/dist-package/packages/runtime/src/domain/index.js +12 -0
  32. package/dist-package/packages/runtime/src/domain/media.js +8 -0
  33. package/dist-package/packages/runtime/src/domain/ports.js +1 -0
  34. package/dist-package/packages/runtime/src/domain/primitives.js +1 -0
  35. package/dist-package/packages/runtime/src/domain/project.js +1 -0
  36. package/dist-package/packages/runtime/src/domain/rough-cut.js +69 -0
  37. package/dist-package/packages/runtime/src/domain/skills.js +33 -0
  38. package/dist-package/packages/runtime/src/domain/types.js +5 -1
  39. package/dist-package/packages/runtime/src/domain/verification.js +1 -0
  40. package/dist-package/packages/runtime/src/editing/dialogue-normalization-service.js +141 -0
  41. package/dist-package/packages/runtime/src/editing/duration-policy.js +187 -0
  42. package/dist-package/packages/runtime/src/editing/edit-service.js +449 -0
  43. package/dist-package/packages/runtime/src/editing/filler-removal-service.js +361 -0
  44. package/dist-package/packages/runtime/src/editing/music-service.js +85 -0
  45. package/dist-package/packages/runtime/src/index.js +14 -3
  46. package/dist-package/packages/runtime/src/planning/rough-cut.js +71 -0
  47. package/dist-package/packages/runtime/src/runtime.js +131 -826
  48. package/dist-package/packages/runtime/src/skills/builtin.js +339 -0
  49. package/dist-package/packages/runtime/src/skills/index.js +3 -0
  50. package/dist-package/packages/runtime/src/skills/registry.js +74 -0
  51. package/dist-package/packages/runtime/src/skills/requirements.js +170 -0
  52. package/dist-package/packages/runtime/src/skills/runtime.js +239 -0
  53. package/dist-package/packages/runtime/src/speech/analysis.js +301 -0
  54. package/dist-package/packages/runtime/src/speech/filler-detector.js +132 -0
  55. package/dist-package/packages/runtime/src/speech/mapping.js +227 -0
  56. package/dist-package/packages/runtime/src/speech/safe-cut-resolver.js +314 -0
  57. package/dist-package/packages/runtime/src/timeline/rational-time.js +63 -0
  58. package/dist-package/packages/runtime/src/verification/verification.js +712 -1
  59. package/dist-package/packages/testkit/src/fixture-analyzers.js +28 -0
  60. package/dist-package/packages/testkit/src/in-memory-editor.js +287 -3
  61. package/dist-package/packages/testkit/src/index.js +1 -0
  62. package/dist-package/packages/testkit/src/skill-fixtures.js +64 -0
  63. package/package.json +17 -8
  64. /package/dist-package/packages/runtime/src/{diff/diff.js → timeline/snapshot-diff.js} +0 -0
  65. /package/dist-package/packages/runtime/src/{snapshot-digest.js → timeline/snapshot-digest.js} +0 -0
@@ -0,0 +1,449 @@
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 { parseRational } from "../timeline/rational-time.js";
7
+ import { assertValidVerificationPolicy } from "../verification/verification.js";
8
+ export class EditService {
9
+ adapter;
10
+ project;
11
+ analysis;
12
+ verificationEngine;
13
+ options;
14
+ transactions;
15
+ editPreviews = new Map();
16
+ constructor(adapter, project, analysis, verificationEngine, options, transactions) {
17
+ this.adapter = adapter;
18
+ this.project = project;
19
+ this.analysis = analysis;
20
+ this.verificationEngine = verificationEngine;
21
+ this.options = options;
22
+ this.transactions = transactions;
23
+ }
24
+ async edit(operation, policy = {}) {
25
+ const verificationPolicy = structuredClone(policy);
26
+ assertValidVerificationPolicy(verificationPolicy);
27
+ const before = await this.project.inspectProject();
28
+ const capabilities = await this.adapter.getCapabilities();
29
+ const target = await this.defaultTarget(before, capabilities);
30
+ return this.editWithTarget(operation, verificationPolicy, target, before, capabilities);
31
+ }
32
+ async editArtifact(targetInput, operation, policy = {}) {
33
+ const target = await this.artifactTarget(targetInput);
34
+ return this.editWithTarget(operation, policy, target);
35
+ }
36
+ async editTimeline(targetInput, operation, policy = {}) {
37
+ const target = this.timelineTarget(targetInput);
38
+ if (!operation.baseRevision)
39
+ throw new Error("INVALID_TIMELINE_TARGET: baseRevision is required");
40
+ return this.editWithTarget(operation, policy, target);
41
+ }
42
+ async editWithTarget(operation, policy, target, existingBefore, existingCapabilities) {
43
+ const verificationPolicy = structuredClone(policy);
44
+ assertValidVerificationPolicy(verificationPolicy);
45
+ const before = existingBefore ?? await this.project.inspectProject();
46
+ const capabilities = existingCapabilities ?? await this.adapter.getCapabilities();
47
+ this.assertTarget(target, before);
48
+ if (operation.baseRevision && !sameRevision(operation.baseRevision, before.revision)) {
49
+ throw new Error("STALE_CONTEXT: operation base revision does not match current editor state");
50
+ }
51
+ assertValidWorkflowOperation(operation);
52
+ this.assertSurfaceCapability(target, capabilities.editor);
53
+ if (!capabilities.editor.timelineSnapshotRead || !capabilities.editor.readAfterWrite || !capabilities.editor.rollback) {
54
+ throw new Error(`CAPABILITY_UNAVAILABLE: ${target.kind === "artifact" ? "artifact" : "editor timeline"} mutation requires snapshot, read-after-write, and rollback`);
55
+ }
56
+ const appliedRevision = await this.adapter.apply(operation, before.revision);
57
+ let attemptedAfter;
58
+ try {
59
+ attemptedAfter = await this.project.inspectProject();
60
+ }
61
+ catch (readError) {
62
+ try {
63
+ await this.adapter.restore(before, appliedRevision);
64
+ this.assertRestored(before, await this.project.inspectProject());
65
+ }
66
+ catch (rollbackError) {
67
+ throw new Error(`READ_AFTER_WRITE_FAILED: compensating rollback failed (${String(readError)}; ${String(rollbackError)})`);
68
+ }
69
+ throw new Error(`READ_AFTER_WRITE_FAILED: canonical state was restored (${String(readError)})`);
70
+ }
71
+ const artifactDigest = await this.readArtifactDigest(target);
72
+ const transaction = {
73
+ id: `txn-${randomUUID()}`,
74
+ target: structuredClone(target),
75
+ ...(artifactDigest ? { artifactDigest } : {}),
76
+ operation,
77
+ intent: operation.type,
78
+ planned: [operation],
79
+ applied: [operation],
80
+ baseRevision: before.revision,
81
+ before,
82
+ after: attemptedAfter,
83
+ attemptedAfter,
84
+ diff: diffSnapshots(before, attemptedAfter),
85
+ verificationPolicy,
86
+ status: "APPLIED",
87
+ };
88
+ try {
89
+ await this.reanalyzeForVerification(transaction, verificationPolicy);
90
+ }
91
+ catch (error) {
92
+ await this.adapter.restore(before, attemptedAfter.revision);
93
+ this.assertRestored(before, await this.project.inspectProject());
94
+ throw new Error(`ANALYSIS_FAILED: post-write verification analysis failed (${String(error)})`);
95
+ }
96
+ try {
97
+ transaction.verification = {
98
+ ...(await this.verificationEngine.verify(transaction, verificationPolicy)),
99
+ target: structuredClone(target),
100
+ };
101
+ }
102
+ catch (verificationError) {
103
+ try {
104
+ await this.adapter.restore(before, attemptedAfter.revision);
105
+ this.assertRestored(before, await this.project.inspectProject());
106
+ }
107
+ catch (rollbackError) {
108
+ throw new Error(`VERIFICATION_FAILED: compensating rollback failed (${String(verificationError)}; ${String(rollbackError)})`);
109
+ }
110
+ throw new Error(`VERIFICATION_FAILED: canonical state was restored (${String(verificationError)})`);
111
+ }
112
+ if (transaction.verification.passed) {
113
+ transaction.status = "VERIFIED";
114
+ }
115
+ else {
116
+ await this.adapter.restore(before, attemptedAfter.revision);
117
+ transaction.after = await this.project.inspectProject();
118
+ this.assertRestored(before, transaction.after);
119
+ transaction.status = "ROLLED_BACK";
120
+ }
121
+ this.transactions.set(transaction);
122
+ return transaction;
123
+ }
124
+ async previewEdit(request) {
125
+ const verification = structuredClone(request.verification ?? {});
126
+ assertValidVerificationPolicy(verification);
127
+ const before = await this.project.inspectProject();
128
+ const capabilities = await this.adapter.getCapabilities();
129
+ const target = await this.defaultTarget(before, capabilities);
130
+ return this.previewEditWithTarget(request, target, before, capabilities);
131
+ }
132
+ async previewArtifactEdit(targetInput, request) {
133
+ const target = await this.artifactTarget(targetInput);
134
+ return this.previewEditWithTarget(request, target);
135
+ }
136
+ async previewTimelineEdit(targetInput, request) {
137
+ const target = this.timelineTarget(targetInput);
138
+ return this.previewEditWithTarget(request, target);
139
+ }
140
+ async previewEditWithTarget(request, target, existingBefore, existingCapabilities) {
141
+ const verification = structuredClone(request.verification ?? {});
142
+ assertValidVerificationPolicy(verification);
143
+ const before = existingBefore ?? await this.project.inspectProject();
144
+ const capabilities = existingCapabilities ?? await this.adapter.getCapabilities();
145
+ this.assertTarget(target, before);
146
+ if (!sameRevision(request.baseRevision, before.revision)) {
147
+ throw new Error("STALE_CONTEXT: preview base revision does not match current editor state");
148
+ }
149
+ if (request.operations.length === 0)
150
+ throw new Error("INVALID_OPERATION: composite edit requires operations");
151
+ request.operations.forEach(assertValidWorkflowOperation);
152
+ await this.assertCompositeCapabilities(request.operations, target.kind, capabilities.editor);
153
+ if (!this.adapter.previewTransaction) {
154
+ throw new Error("CAPABILITY_UNAVAILABLE: editor composite transaction preview");
155
+ }
156
+ const expectedAfter = await this.adapter.previewTransaction(request.operations, before.revision);
157
+ const previewToken = `preview-${randomUUID()}`;
158
+ const preview = {
159
+ previewToken,
160
+ target: structuredClone(target),
161
+ baseRevision: structuredClone(before.revision),
162
+ operations: structuredClone(request.operations),
163
+ expectedDiff: diffSnapshots(before, expectedAfter),
164
+ warnings: [],
165
+ expiresAt: new Date(this.now() + (this.options.previewTtlMs ?? 30_000)).toISOString(),
166
+ verification,
167
+ };
168
+ this.pruneEditPreviews();
169
+ const maxActivePreviews = Number.isInteger(this.options.maxActivePreviews) && this.options.maxActivePreviews > 0
170
+ ? this.options.maxActivePreviews
171
+ : 128;
172
+ while (this.editPreviews.size >= maxActivePreviews) {
173
+ const oldestToken = this.editPreviews.keys().next().value;
174
+ if (oldestToken === undefined)
175
+ break;
176
+ this.editPreviews.delete(oldestToken);
177
+ }
178
+ this.editPreviews.set(previewToken, preview);
179
+ return structuredClone(preview);
180
+ }
181
+ async executeEdit(previewToken, policy = {}) {
182
+ const requestedPolicy = structuredClone(policy);
183
+ assertValidVerificationPolicy(requestedPolicy);
184
+ const preview = this.editPreviews.get(previewToken);
185
+ if (!preview)
186
+ throw new Error(`PREVIEW_TOKEN_INVALID: unknown or already used preview ${previewToken}`);
187
+ this.editPreviews.delete(previewToken);
188
+ if (this.now() > Date.parse(preview.expiresAt)) {
189
+ throw new Error("PREVIEW_TOKEN_EXPIRED: composite edit preview has expired");
190
+ }
191
+ const before = await this.project.inspectProject();
192
+ if (!sameRevision(preview.baseRevision, before.revision)) {
193
+ throw new Error("STALE_CONTEXT: preview base revision does not match current editor state");
194
+ }
195
+ this.assertTarget(preview.target, before);
196
+ preview.operations.forEach(assertValidWorkflowOperation);
197
+ await this.assertCompositeCapabilities(preview.operations, preview.target.kind);
198
+ if (!this.adapter.applyTransaction) {
199
+ throw new Error("CAPABILITY_UNAVAILABLE: editor composite transaction execution");
200
+ }
201
+ const verificationPolicy = Object.keys(requestedPolicy).length > 0
202
+ ? { ...structuredClone(preview.verification ?? {}), ...requestedPolicy }
203
+ : structuredClone(preview.verification ?? {});
204
+ assertValidVerificationPolicy(verificationPolicy);
205
+ try {
206
+ await this.adapter.applyTransaction(preview.operations, before.revision);
207
+ }
208
+ catch (error) {
209
+ const partiallyApplied = await this.project.inspectProject();
210
+ if (!sameRevision(partiallyApplied.revision, before.revision)) {
211
+ try {
212
+ await this.adapter.restore(before, partiallyApplied.revision);
213
+ }
214
+ catch (rollbackError) {
215
+ throw new Error(`TRANSACTION_FAILED: ${String(error)}; rollback failed: ${String(rollbackError)}`);
216
+ }
217
+ }
218
+ throw new Error(`TRANSACTION_FAILED: ${String(error)}; transaction was rolled back`);
219
+ }
220
+ const attemptedAfter = await this.project.inspectProject();
221
+ const artifactDigest = await this.readArtifactDigest(preview.target);
222
+ const transaction = {
223
+ id: `txn-${randomUUID()}`,
224
+ target: structuredClone(preview.target),
225
+ ...(artifactDigest ? { artifactDigest } : {}),
226
+ intent: "composite-edit",
227
+ planned: structuredClone(preview.operations),
228
+ applied: structuredClone(preview.operations),
229
+ baseRevision: before.revision,
230
+ before,
231
+ after: attemptedAfter,
232
+ attemptedAfter,
233
+ diff: diffSnapshots(before, attemptedAfter),
234
+ verificationPolicy,
235
+ status: "APPLIED",
236
+ };
237
+ try {
238
+ await this.reanalyzeForVerification(transaction, verificationPolicy);
239
+ }
240
+ catch (error) {
241
+ await this.adapter.restore(before, attemptedAfter.revision);
242
+ this.assertRestored(before, await this.project.inspectProject());
243
+ throw new Error(`ANALYSIS_FAILED: post-write verification analysis failed (${String(error)})`);
244
+ }
245
+ try {
246
+ transaction.verification = {
247
+ ...(await this.verificationEngine.verify(transaction, verificationPolicy)),
248
+ target: structuredClone(preview.target),
249
+ };
250
+ }
251
+ catch (verificationError) {
252
+ try {
253
+ await this.adapter.restore(before, attemptedAfter.revision);
254
+ this.assertRestored(before, await this.project.inspectProject());
255
+ }
256
+ catch (rollbackError) {
257
+ throw new Error(`VERIFICATION_FAILED: compensating rollback failed (${String(verificationError)}; ${String(rollbackError)})`);
258
+ }
259
+ throw new Error(`VERIFICATION_FAILED: canonical state was restored (${String(verificationError)})`);
260
+ }
261
+ if (transaction.verification.passed) {
262
+ transaction.status = "VERIFIED";
263
+ }
264
+ else {
265
+ await this.adapter.restore(before, attemptedAfter.revision);
266
+ transaction.after = await this.project.inspectProject();
267
+ this.assertRestored(before, transaction.after);
268
+ transaction.status = "ROLLED_BACK";
269
+ }
270
+ this.transactions.set(transaction);
271
+ return transaction;
272
+ }
273
+ getDiff(transactionId) {
274
+ return this.getTransaction(transactionId).diff;
275
+ }
276
+ getTransaction(transactionId) {
277
+ return this.transactions.get(transactionId);
278
+ }
279
+ async verifyTransaction(transactionId) {
280
+ return this.getTransaction(transactionId).verification;
281
+ }
282
+ async reanalyzeForVerification(transaction, policy) {
283
+ const requirements = postWriteAnalysisRequirements(policy);
284
+ if (!Object.values(requirements).some(Boolean))
285
+ return;
286
+ transaction.attemptedAfter = await this.analysis.reanalyzeAffectedRanges(transaction, requirements);
287
+ transaction.after = transaction.attemptedAfter;
288
+ }
289
+ async undo(transactionId) {
290
+ const transaction = this.getTransaction(transactionId);
291
+ const current = await this.project.inspectProject();
292
+ if (current.projectId !== transaction.before.projectId || current.timeline.id !== transaction.before.timeline.id) {
293
+ throw new Error(`TARGET_MISMATCH: cannot undo ${transaction.before.projectId}/${transaction.before.timeline.id} while ${current.projectId}/${current.timeline.id} is active`);
294
+ }
295
+ if (!sameRevision(current.revision, transaction.after.revision)) {
296
+ throw new Error(`STALE_CONTEXT: transaction ${transactionId} changed after edit`);
297
+ }
298
+ await this.adapter.restore(transaction.before, current.revision);
299
+ const restored = await this.project.inspectProject();
300
+ this.assertRestored(transaction.before, restored);
301
+ return restored;
302
+ }
303
+ assertRestored(expected, actual) {
304
+ if (canonicalSnapshotDigest(expected) !== canonicalSnapshotDigest(actual)) {
305
+ throw new Error("ROLLBACK_FAILED: restored canonical digest does not match pre-edit state");
306
+ }
307
+ }
308
+ async assertCompositeCapabilities(operations, targetKind, existingCapabilities) {
309
+ const capabilities = existingCapabilities ?? (await this.adapter.getCapabilities()).editor;
310
+ if (!capabilities.compositeTransactions || !capabilities.readAfterWrite || !capabilities.rollback) {
311
+ throw new Error("CAPABILITY_UNAVAILABLE: editor composite transactions");
312
+ }
313
+ if (operations.some((operation) => operation.type === "media.import") && !capabilities.mediaImport) {
314
+ throw new Error("CAPABILITY_UNAVAILABLE: media import");
315
+ }
316
+ if (operations.some((operation) => operation.type === "timeline.media.add") && !capabilities.mediaPlacement) {
317
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline media placement");
318
+ }
319
+ if (operations.some((operation) => operation.type === "timeline.title.add")
320
+ && (!capabilities.titlePlacement || !capabilities.assetDiscovery)) {
321
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline title placement");
322
+ }
323
+ if (operations.some((operation) => operation.type !== "media.import")) {
324
+ if (targetKind === "artifact" && !capabilities.timelineArtifactWrite) {
325
+ throw new Error("CAPABILITY_UNAVAILABLE: artifact mutation");
326
+ }
327
+ if (targetKind === "editor.timeline" && !capabilities.timelineWrite) {
328
+ throw new Error("CAPABILITY_UNAVAILABLE: editor timeline mutation");
329
+ }
330
+ if (!targetKind && !capabilities.timelineWrite && !capabilities.timelineArtifactWrite) {
331
+ throw new Error("CAPABILITY_UNAVAILABLE: editor timeline mutation");
332
+ }
333
+ }
334
+ if (operations.some((operation) => operation.type === "timeline.media.move") && !capabilities.clipMove) {
335
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline media move");
336
+ }
337
+ if (operations.some((operation) => operation.type === "timeline.media.replace") && !capabilities.clipReplace) {
338
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline media replace");
339
+ }
340
+ if (operations.some((operation) => operation.type === "timeline.media.remove") && !capabilities.clipRemoval) {
341
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline media removal");
342
+ }
343
+ if (operations.some((operation) => operation.type === "timeline.transition.add")
344
+ && (!capabilities.transitionPlacement || !capabilities.assetDiscovery)) {
345
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline transition placement");
346
+ }
347
+ if (operations.some((operation) => operation.type === "timeline.audio.attach") && !capabilities.audioAttachment) {
348
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline audio attachment");
349
+ }
350
+ if (operations.some((operation) => operation.type === "timeline.audio.mix") && !capabilities.audioMixing) {
351
+ throw new Error("CAPABILITY_UNAVAILABLE: timeline audio mixing");
352
+ }
353
+ if (operations.some((operation) => operation.type === "reduce-noise") && !capabilities.noiseReduction) {
354
+ throw new Error("CAPABILITY_UNAVAILABLE: noise reduction effect");
355
+ }
356
+ if (operations.some((operation) => operation.type === "set-color-correction") && !capabilities.colorCorrection) {
357
+ throw new Error("CAPABILITY_UNAVAILABLE: color correction");
358
+ }
359
+ }
360
+ async defaultTarget(before, capabilities) {
361
+ if (capabilities.editor.timelineArtifactWrite && !capabilities.editor.timelineWrite) {
362
+ if (!this.adapter.getManagedArtifact) {
363
+ throw new Error("CAPABILITY_UNAVAILABLE: managed FCPXML artifact");
364
+ }
365
+ return this.artifactDescriptor(await this.adapter.getManagedArtifact());
366
+ }
367
+ return {
368
+ kind: "editor.timeline",
369
+ projectId: before.projectId,
370
+ sequenceId: before.timeline.id,
371
+ };
372
+ }
373
+ async artifactTarget(input) {
374
+ if (!input.artifactPath.trim())
375
+ throw new Error("INVALID_ARTIFACT_TARGET: artifactPath is required");
376
+ if (!this.adapter.getManagedArtifact) {
377
+ throw new Error("CAPABILITY_UNAVAILABLE: managed FCPXML artifact");
378
+ }
379
+ const managed = await this.adapter.getManagedArtifact();
380
+ if (resolvePath(managed.path) !== resolvePath(input.artifactPath.trim())) {
381
+ throw new Error(`TARGET_MISMATCH: requested artifact ${input.artifactPath} is not managed by this adapter`);
382
+ }
383
+ return this.artifactDescriptor(managed);
384
+ }
385
+ artifactDescriptor(artifact) {
386
+ return { kind: "artifact", artifactId: artifact.id, artifactPath: artifact.path };
387
+ }
388
+ async readArtifactDigest(target) {
389
+ if (target.kind !== "artifact" || !this.adapter.getManagedArtifactDigest)
390
+ return undefined;
391
+ return this.adapter.getManagedArtifactDigest();
392
+ }
393
+ timelineTarget(input) {
394
+ const projectId = input.projectId.trim();
395
+ const sequenceId = input.sequenceId.trim();
396
+ if (!projectId || !sequenceId) {
397
+ throw new Error("INVALID_TIMELINE_TARGET: projectId and sequenceId are required");
398
+ }
399
+ return { kind: "editor.timeline", projectId, sequenceId };
400
+ }
401
+ assertTarget(target, snapshot) {
402
+ if (target.kind === "editor.timeline"
403
+ && (target.projectId !== snapshot.projectId || target.sequenceId !== snapshot.timeline.id)) {
404
+ throw new Error(`TARGET_MISMATCH: requested ${target.projectId}/${target.sequenceId} while ${snapshot.projectId}/${snapshot.timeline.id} is active`);
405
+ }
406
+ }
407
+ assertSurfaceCapability(target, capabilities) {
408
+ if (target.kind === "artifact" && !capabilities.timelineArtifactWrite) {
409
+ throw new Error("CAPABILITY_UNAVAILABLE: artifact mutation");
410
+ }
411
+ if (target.kind === "editor.timeline" && !capabilities.timelineWrite) {
412
+ throw new Error("CAPABILITY_UNAVAILABLE: editor timeline mutation");
413
+ }
414
+ }
415
+ now() {
416
+ return this.options.now?.() ?? Date.now();
417
+ }
418
+ pruneEditPreviews() {
419
+ const now = this.now();
420
+ for (const [previewToken, preview] of this.editPreviews) {
421
+ if (now > Date.parse(preview.expiresAt))
422
+ this.editPreviews.delete(previewToken);
423
+ }
424
+ }
425
+ }
426
+ function postWriteAnalysisRequirements(policy) {
427
+ const assertions = policy.assertions ?? [];
428
+ return {
429
+ speech: policy.requireSpeechContinuity === true,
430
+ audio: policy.maxTruePeakDb !== undefined
431
+ || policy.targetLufs !== undefined
432
+ || assertions.some((assertion) => assertion.type === "audio-audibility" || assertion.type === "audio-loudness"),
433
+ noise: assertions.some((assertion) => assertion.type === "audio-noise"),
434
+ visual: assertions.some((assertion) => assertion.type === "visual-content"),
435
+ };
436
+ }
437
+ function assertValidWorkflowOperation(operation) {
438
+ if (operation.type !== "trim-clip")
439
+ return;
440
+ if (!Number.isFinite(operation.duration) || operation.duration <= 0) {
441
+ throw new Error("INVALID_OPERATION: clip duration must be positive");
442
+ }
443
+ if (!operation.durationTime)
444
+ return;
445
+ const duration = parseRational(operation.durationTime, "INVALID_OPERATION");
446
+ if (duration.value <= 0n) {
447
+ throw new Error("INVALID_OPERATION: clip duration must be positive");
448
+ }
449
+ }