@morshoto/framekit 0.1.6 → 0.1.10

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 (33) hide show
  1. package/bin/framekit.mjs +2 -1
  2. package/dist-package/adapters/final-cut/typescript/src/assets.js +157 -35
  3. package/dist-package/adapters/final-cut/typescript/src/background-export.js +384 -0
  4. package/dist-package/adapters/final-cut/typescript/src/canonical.js +189 -32
  5. package/dist-package/adapters/final-cut/typescript/src/connection.js +3 -3
  6. package/dist-package/adapters/final-cut/typescript/src/disposable-native.js +33 -7
  7. package/dist-package/adapters/final-cut/typescript/src/fcpxml.js +615 -49
  8. package/dist-package/adapters/final-cut/typescript/src/index.js +4 -0
  9. package/dist-package/adapters/final-cut/typescript/src/library-inspection.js +390 -0
  10. package/dist-package/adapters/final-cut/typescript/src/live.js +6 -3
  11. package/dist-package/adapters/final-cut/typescript/src/media.js +151 -0
  12. package/dist-package/adapters/final-cut/typescript/src/native-session.js +412 -0
  13. package/dist-package/adapters/final-cut/typescript/src/native.js +1192 -364
  14. package/dist-package/adapters/final-cut/typescript/src/publisher.js +225 -32
  15. package/dist-package/adapters/final-cut/typescript/src/session.js +145 -17
  16. package/dist-package/apps/mcp-server/src/main.js +26 -7
  17. package/dist-package/apps/mcp-server/src/routing.js +227 -39
  18. package/dist-package/apps/mcp-server/src/server.js +532 -97
  19. package/dist-package/apps/mcp-server/src/version.js +109 -1
  20. package/dist-package/packages/runtime/src/application/media-analysis-service.js +1 -3
  21. package/dist-package/packages/runtime/src/application/project-service.js +46 -7
  22. package/dist-package/packages/runtime/src/capabilities.js +55 -7
  23. package/dist-package/packages/runtime/src/context/project-catalog.js +159 -0
  24. package/dist-package/packages/runtime/src/domain/capabilities.js +33 -0
  25. package/dist-package/packages/runtime/src/domain/context.js +14 -1
  26. package/dist-package/packages/runtime/src/domain/index.js +1 -0
  27. package/dist-package/packages/runtime/src/domain/timeline-target.js +217 -0
  28. package/dist-package/packages/runtime/src/editing/edit-service.js +15 -1
  29. package/dist-package/packages/runtime/src/editing/intent.js +178 -2
  30. package/dist-package/packages/runtime/src/index.js +1 -0
  31. package/dist-package/packages/runtime/src/runtime.js +2 -2
  32. package/dist-package/packages/testkit/src/in-memory-editor.js +5 -4
  33. package/package.json +7 -2
@@ -3,7 +3,7 @@ import { mkdtemp, rm, stat } from "node:fs/promises";
3
3
  import { basename, join } from "node:path";
4
4
  import { tmpdir } from "node:os";
5
5
  import { promisify } from "node:util";
6
- import { canonicalSnapshotDigest, withCapabilityFamilies, } from "../../../../packages/runtime/src/index.js";
6
+ import { canonicalSnapshotDigest, assertTimelineTargetReadAfterWrite, createTimelineTarget, resolveTimelineTarget, reconcileProjectCatalog, validateProjectCatalog, withCapabilityFamilies, CapabilityUnavailableError, } from "../../../../packages/runtime/src/index.js";
7
7
  import { FcpxmlDocumentAdapter } from "./fcpxml.js";
8
8
  const execFile = promisify(execFileCallback);
9
9
  /** Reads the active Final Cut project through its headed Export XML command. */
@@ -86,6 +86,8 @@ end using terms from
86
86
 
87
87
  tell application "System Events"
88
88
  tell process "Final Cut Pro"
89
+ set frontmost to true
90
+ delay 0.1
89
91
  if not frontmost then error "FINAL_CUT_CANONICAL_NOT_FRONTMOST: Final Cut Pro must be frontmost"
90
92
  set finalCut to it
91
93
  set fileMenu to menu "File" of menu bar 1
@@ -159,6 +161,16 @@ export function createFinalCutNativeTargetResolver(native) {
159
161
  throw new Error(`AMBIGUOUS_PROJECT_TARGET: Final Cut timeline has multiple occurrences for ${query}`);
160
162
  }
161
163
  const occurrence = located.occurrences[0];
164
+ const occurrenceIdentity = occurrence.identity ?? occurrence.nativeIdentity;
165
+ if (!occurrenceIdentity) {
166
+ throw new Error(`TARGET_MISMATCH: native occurrence has no stable identity for ${clip.id}`);
167
+ }
168
+ if (!occurrence.sequenceId) {
169
+ throw new Error(`TARGET_MISMATCH: native occurrence has no stable sequence identity for ${clip.id}`);
170
+ }
171
+ if (occurrence.sequenceId !== snapshot.timeline.id) {
172
+ throw new Error(`TARGET_MISMATCH: native occurrence sequence changed for ${clip.id}`);
173
+ }
162
174
  if (occurrence.sourceIdentity && occurrence.sourceIdentity !== match.sourceIdentity) {
163
175
  throw new Error(`TARGET_MISMATCH: native occurrence source identity changed for ${clip.id}`);
164
176
  }
@@ -183,6 +195,7 @@ export class FinalCutCanonicalNativeProvider {
183
195
  native;
184
196
  readSnapshotSource;
185
197
  resolveTarget;
198
+ backgroundCatalog;
186
199
  lastDigest;
187
200
  lastSnapshot;
188
201
  revisionSequence = 0;
@@ -193,28 +206,44 @@ export class FinalCutCanonicalNativeProvider {
193
206
  this.native = options.native;
194
207
  this.readSnapshotSource = options.readSnapshot;
195
208
  this.resolveTarget = options.resolveTarget;
209
+ this.backgroundCatalog = options.backgroundCatalog;
196
210
  }
197
211
  async getIdentity() {
198
212
  const identity = await this.live.getIdentity();
199
213
  return { ...identity, backend: "final-cut-native-canonical" };
200
214
  }
201
- async getCapabilities() {
215
+ async getCapabilities(options = {}) {
216
+ const backgroundCatalog = await this.resolveBackgroundCatalog();
217
+ const snapshotProbe = backgroundCatalog && options.probeCanonicalSnapshot !== false
218
+ ? await this.probeCanonicalSnapshot()
219
+ : {
220
+ available: false,
221
+ reason: backgroundCatalog
222
+ ? "canonical snapshot availability was not checked"
223
+ : "canonical timeline snapshot requires a background project catalog",
224
+ };
225
+ const canonicalSnapshotCapability = snapshotProbe.available
226
+ ? true
227
+ : unavailableCanonicalSnapshot(snapshotProbe.reason);
202
228
  return withCapabilityFamilies({
203
229
  editor: {
204
- projectRead: true,
205
- timelineSnapshotRead: true,
206
- timelineWrite: true,
230
+ projectRead: snapshotProbe.available,
231
+ timelineSnapshotRead: snapshotProbe.available,
232
+ timelineWrite: snapshotProbe.available,
207
233
  timelineArtifactWrite: false,
208
- readAfterWrite: true,
234
+ readAfterWrite: snapshotProbe.available,
209
235
  incrementalChanges: true,
210
- rollback: true,
236
+ rollback: snapshotProbe.available,
211
237
  assetDiscovery: false,
212
238
  liveStateRead: true,
213
239
  playheadWrite: false,
214
240
  frameCapture: false,
215
241
  playbackControl: false,
216
- projectCatalogRead: true,
217
- projectSelection: true,
242
+ projectCatalogRead: Boolean(backgroundCatalog),
243
+ projectSelection: false,
244
+ projectSelectionMode: "unavailable",
245
+ backgroundLibraryInspection: Boolean(backgroundCatalog),
246
+ compositeTransactions: snapshotProbe.available,
218
247
  },
219
248
  analyzers: {
220
249
  speechTranscribe: false,
@@ -225,9 +254,35 @@ export class FinalCutCanonicalNativeProvider {
225
254
  }, {
226
255
  backend: "final-cut-native-canonical",
227
256
  connectionBackend: "workflow-extension-ipc",
228
- canonicalDocument: { read: true, write: true, artifactWrite: false },
257
+ canonicalDocument: {
258
+ read: canonicalSnapshotCapability,
259
+ write: canonicalSnapshotCapability,
260
+ artifactWrite: false,
261
+ },
262
+ observation: {
263
+ library: this.backgroundCatalog
264
+ ? {
265
+ available: true,
266
+ backend: this.backgroundCatalog.backend ?? "final-cut-background-library",
267
+ guarantee: "observed",
268
+ }
269
+ : false,
270
+ },
229
271
  });
230
272
  }
273
+ async probeCanonicalSnapshot() {
274
+ try {
275
+ await this.readSnapshotSource();
276
+ return { available: true };
277
+ }
278
+ catch (error) {
279
+ const detail = error instanceof Error ? error.message : String(error);
280
+ return {
281
+ available: false,
282
+ reason: `canonical snapshot provider unavailable: ${detail}`,
283
+ };
284
+ }
285
+ }
231
286
  async read() {
232
287
  return this.readProject();
233
288
  }
@@ -247,15 +302,37 @@ export class FinalCutCanonicalNativeProvider {
247
302
  this.lastSnapshot = { ...snapshot, revision: this.revision };
248
303
  return structuredClone(this.lastSnapshot);
249
304
  }
305
+ async previewTransaction(operations, expectedRevision) {
306
+ const before = await this.readProject();
307
+ assertSameRevision(expectedRevision, before.revision);
308
+ const operation = supportedCanonicalOperation(operations);
309
+ const clip = before.timeline.clips.find(({ id }) => id === operation.clipId);
310
+ if (!clip)
311
+ throw new Error(`CLIP_NOT_FOUND: ${operation.clipId}`);
312
+ const preview = structuredClone(before);
313
+ preview.timeline.clips = preview.timeline.clips.map((candidate) => (candidate.id === operation.clipId ? { ...candidate, name: operation.name } : candidate));
314
+ preview.revision = previewRevision(preview, before.revision);
315
+ return preview;
316
+ }
317
+ async applyTransaction(operations, expectedRevision) {
318
+ const operation = supportedCanonicalOperation(operations);
319
+ await this.apply(operation, expectedRevision);
320
+ }
250
321
  async apply(operation, expectedRevision) {
251
322
  const before = await this.readProject();
252
323
  assertSameRevision(expectedRevision, before.revision);
253
324
  if (operation.type !== "rename-clip") {
254
325
  throw new Error(`CAPABILITY_UNAVAILABLE: final-cut native canonical provider does not support ${operation.type}`);
255
326
  }
327
+ validateCanonicalRename(operation);
256
328
  const clip = before.timeline.clips.find(({ id }) => id === operation.clipId);
257
329
  if (!clip)
258
330
  throw new Error(`CLIP_NOT_FOUND: ${operation.clipId}`);
331
+ const timelineTarget = createTimelineTarget(before, {
332
+ occurrenceId: clip.id,
333
+ mediaId: clip.mediaId,
334
+ });
335
+ resolveTimelineTarget(before, timelineTarget);
259
336
  await this.resolveTarget(clip, before);
260
337
  const nativeResult = await this.native.renameSelectedClip(operation.name);
261
338
  if (!nativeResult.operationId || !nativeResult.undoAvailable) {
@@ -276,6 +353,7 @@ export class FinalCutCanonicalNativeProvider {
276
353
  if (canonicalSnapshotDigest(after) === this.pending.beforeDigest) {
277
354
  throw new Error("FINAL_CUT_CANONICAL_READBACK_FAILED: native edit did not change the canonical digest");
278
355
  }
356
+ assertTimelineTargetReadAfterWrite(timelineTarget, before, after);
279
357
  this.pending.afterRevision = after.revision;
280
358
  return after.revision;
281
359
  }
@@ -322,28 +400,19 @@ export class FinalCutCanonicalNativeProvider {
322
400
  this.pending = undefined;
323
401
  }
324
402
  async listProjects() {
325
- const snapshot = await this.readProject();
326
- return {
327
- projects: [{
328
- id: snapshot.projectId,
329
- name: snapshot.projectName,
330
- sequences: [{ id: snapshot.timeline.id, name: snapshot.timeline.name }],
331
- }],
332
- activeProjectId: snapshot.projectId,
333
- activeSequenceId: snapshot.timeline.id,
334
- };
403
+ const backgroundCatalog = await this.resolveBackgroundCatalog();
404
+ if (!backgroundCatalog) {
405
+ throw new CapabilityUnavailableError("project.list", "editor.projectCatalogRead", {
406
+ available: false,
407
+ backend: "final-cut-native-canonical",
408
+ guarantee: "none",
409
+ unavailableReason: "background project catalog is unavailable; canonical timeline snapshot export (File > Export XML) requires a headed Final Cut UI",
410
+ });
411
+ }
412
+ return this.readBackgroundCatalog(backgroundCatalog);
335
413
  }
336
414
  async selectProject(selection) {
337
- const catalog = await this.listProjects();
338
- const project = catalog.projects.find(({ id }) => id === selection.projectId);
339
- if (!project)
340
- throw new Error(`TARGET_MISMATCH: active project is not ${selection.projectId}`);
341
- const sequenceId = selection.sequenceId ?? (project.sequences.length === 1 ? project.sequences[0]?.id : undefined);
342
- if (!sequenceId)
343
- throw new Error(`AMBIGUOUS_PROJECT_TARGET: sequenceId is required for ${selection.projectId}`);
344
- if (sequenceId !== catalog.activeSequenceId)
345
- throw new Error(`TARGET_MISMATCH: active sequence is not ${sequenceId}`);
346
- return catalog;
415
+ throw new Error("CAPABILITY_UNAVAILABLE: Final Cut project selection is not exposed by the background provider");
347
416
  }
348
417
  async readLiveState() {
349
418
  return this.live.readLiveState();
@@ -353,13 +422,101 @@ export class FinalCutCanonicalNativeProvider {
353
422
  }
354
423
  async assertActiveTarget(snapshot) {
355
424
  const live = await this.live.readLiveState();
356
- if (live.project && live.project.name !== snapshot.projectName) {
425
+ if (!live.project || live.project.id !== snapshot.projectId) {
426
+ throw new Error(`TARGET_MISMATCH: active project identity ${live.project?.id ?? "<unavailable>"} does not match exported project ${snapshot.projectId}`);
427
+ }
428
+ if (live.project.name !== snapshot.projectName) {
357
429
  throw new Error(`TARGET_MISMATCH: exported project ${snapshot.projectName} is not active Final Cut project ${live.project.name}`);
358
430
  }
359
- if (live.sequence && live.sequence.name !== snapshot.timeline.name) {
431
+ if (!live.sequence || live.sequence.id !== snapshot.timeline.id) {
432
+ throw new Error(`TARGET_MISMATCH: active sequence identity ${live.sequence?.id ?? "<unavailable>"} does not match exported sequence ${snapshot.timeline.id}`);
433
+ }
434
+ if (live.sequence.name !== snapshot.timeline.name) {
360
435
  throw new Error(`TARGET_MISMATCH: exported sequence ${snapshot.timeline.name} is not active Final Cut sequence ${live.sequence.name}`);
361
436
  }
362
437
  }
438
+ async resolveBackgroundCatalog() {
439
+ if (this.backgroundCatalog)
440
+ return this.backgroundCatalog;
441
+ if (!this.live.listProjects || !this.live.getCapabilities)
442
+ return undefined;
443
+ try {
444
+ const capabilities = await this.live.getCapabilities();
445
+ if (!capabilities.editor.projectCatalogRead)
446
+ return undefined;
447
+ const identity = await this.live.getIdentity();
448
+ return {
449
+ backend: identity.backend,
450
+ listProjects: () => this.live.listProjects(),
451
+ };
452
+ }
453
+ catch {
454
+ return undefined;
455
+ }
456
+ }
457
+ async readBackgroundCatalog(provider) {
458
+ const before = await this.readLiveStateSafely();
459
+ const catalog = await provider.listProjects();
460
+ validateProjectCatalog(catalog);
461
+ const after = await this.readLiveStateSafely();
462
+ const liveIdentity = before && after ? await this.live.getIdentity() : undefined;
463
+ return reconcileProjectCatalog(catalog, {
464
+ ...(before && after ? { before, after } : {}),
465
+ provenance: {
466
+ catalog: {
467
+ source: "background-library",
468
+ backend: provider.backend ?? "final-cut-background-library",
469
+ guarantee: "observed",
470
+ },
471
+ ...(liveIdentity ? {
472
+ live: {
473
+ source: "live-socket",
474
+ backend: liveIdentity.backend,
475
+ guarantee: "observed",
476
+ },
477
+ } : {}),
478
+ selection: {
479
+ available: false,
480
+ mode: "unavailable",
481
+ unavailableReason: "project selection is not exposed by the background provider",
482
+ },
483
+ },
484
+ });
485
+ }
486
+ async readLiveStateSafely() {
487
+ try {
488
+ return await this.live.readLiveState();
489
+ }
490
+ catch {
491
+ return undefined;
492
+ }
493
+ }
494
+ }
495
+ function supportedCanonicalOperation(operations) {
496
+ if (operations.length !== 1 || operations[0]?.type !== "rename-clip") {
497
+ throw new Error("CAPABILITY_UNAVAILABLE: final-cut native canonical provider supports one rename-clip transaction");
498
+ }
499
+ return validateCanonicalRename(operations[0]);
500
+ }
501
+ function validateCanonicalRename(operation) {
502
+ if (!operation.name.trim())
503
+ throw new Error("INVALID_OPERATION: clip name cannot be empty");
504
+ return operation;
505
+ }
506
+ function unavailableCanonicalSnapshot(reason) {
507
+ return {
508
+ available: false,
509
+ backend: "final-cut-native-canonical",
510
+ guarantee: "none",
511
+ unavailableReason: reason,
512
+ };
513
+ }
514
+ function previewRevision(snapshot, before) {
515
+ return {
516
+ id: `canonical:${canonicalSnapshotDigest(snapshot)}`,
517
+ sequence: before.sequence + 1,
518
+ timestamp: new Date().toISOString(),
519
+ };
363
520
  }
364
521
  function assertSameRevision(expected, actual) {
365
522
  if (!sameRevision(expected, actual))
@@ -4,7 +4,7 @@ import { existsSync } from "node:fs";
4
4
  import { promisify } from "node:util";
5
5
  import { homedir } from "node:os";
6
6
  import { dirname, join, resolve } from "node:path";
7
- import { withCapabilityFamilies } from "../../../../packages/runtime/src/index.js";
7
+ import { withCanonicalTimelineMode, withCapabilityFamilies } from "../../../../packages/runtime/src/index.js";
8
8
  import { createFinalCutLiveAdapter, DEFAULT_FINAL_CUT_LIVE_SOCKET } from "./live.js";
9
9
  const execFile = promisify(execFileCallback);
10
10
  export function assertCanonicalProviderConfiguration(options) {
@@ -193,10 +193,10 @@ export class FinalCutConnectionManager {
193
193
  }
194
194
  }
195
195
  ready(result) {
196
- const capabilities = withCapabilityFamilies(result.capabilities, {
196
+ const capabilities = withCanonicalTimelineMode(withCapabilityFamilies(result.capabilities, {
197
197
  backend: result.identity.backend,
198
198
  connectionBackend: result.identity.backend,
199
- });
199
+ }));
200
200
  if (this.canonicalProviderRequired && capabilities.editor.canonicalTimelineMode !== "canonical-write") {
201
201
  return this.fail("FINAL_CUT_CANONICAL_PROVIDER_REQUIRED", `Canonical live provider at ${this.socketPath} reports ${capabilities.editor.canonicalTimelineMode}; canonical-write is required`, "needs-user-action");
202
202
  }
@@ -40,12 +40,37 @@ export class DisposableNativeEditWorkflow {
40
40
  return {
41
41
  previewToken,
42
42
  operation: { type: "rename-selected-clip", name: request.name },
43
- target: { clipId: target.id, name: target.name },
43
+ target: { clipId: target.id, name: target.name, ...(nativeTarget.identity ? { identity: nativeTarget.identity } : {}) },
44
+ projectId: before.projectId,
45
+ sequenceId: before.timeline.id,
46
+ targetIdentity: nativeTarget.identity ?? target.id,
44
47
  baseRevision: structuredClone(before.revision),
45
48
  expiresAt: new Date(expiresAt).toISOString(),
46
49
  };
47
50
  }
48
- async execute(previewToken) {
51
+ getPreview(previewToken) {
52
+ const preview = this.previews.get(previewToken);
53
+ if (!preview)
54
+ return undefined;
55
+ if (this.now() >= preview.expiresAt) {
56
+ this.previews.delete(previewToken);
57
+ return undefined;
58
+ }
59
+ const target = preview.before.timeline.clips.find((clip) => clip.id === preview.request.clipId);
60
+ if (!target)
61
+ return undefined;
62
+ return {
63
+ previewToken,
64
+ operation: { type: "rename-selected-clip", name: preview.request.name },
65
+ target: { clipId: target.id, name: target.name, ...(preview.nativeTarget.identity ? { identity: preview.nativeTarget.identity } : {}) },
66
+ projectId: preview.before.projectId,
67
+ sequenceId: preview.before.timeline.id,
68
+ targetIdentity: preview.nativeTarget.identity ?? target.id,
69
+ baseRevision: structuredClone(preview.before.revision),
70
+ expiresAt: new Date(preview.expiresAt).toISOString(),
71
+ };
72
+ }
73
+ async execute(previewToken, options = {}) {
49
74
  const preview = this.previews.get(previewToken);
50
75
  if (!preview)
51
76
  throw new Error(`PREVIEW_TOKEN_INVALID: unknown or already used preview ${previewToken}`);
@@ -60,8 +85,9 @@ export class DisposableNativeEditWorkflow {
60
85
  if (!currentTarget || currentTarget.name !== preview.before.timeline.clips.find((clip) => clip.id === preview.request.clipId)?.name) {
61
86
  throw new Error("TARGET_MISMATCH: canonical target changed after disposable native preview");
62
87
  }
63
- await this.assertNativeTarget(current, currentTarget.name, preview.nativeTarget);
64
- const native = await this.options.native.edit({ type: "rename-selected-clip", name: preview.request.name });
88
+ await this.assertNativeTarget(current, currentTarget.name, preview.nativeTarget, options.signal);
89
+ options.onMutationStart?.();
90
+ const native = await this.options.native.edit({ type: "rename-selected-clip", name: preview.request.name }, { signal: options.signal });
65
91
  let after;
66
92
  try {
67
93
  after = await this.readCanonical("read-after-write");
@@ -172,10 +198,10 @@ export class DisposableNativeEditWorkflow {
172
198
  throw new Error(`CAPABILITY_UNAVAILABLE: disposable native canonical snapshot (${String(error)})`);
173
199
  }
174
200
  }
175
- async assertNativeTarget(snapshot, expectedName, previousTarget) {
201
+ async assertNativeTarget(snapshot, expectedName, previousTarget, signal) {
176
202
  const capabilities = this.options.native.capabilities();
177
203
  assertNativeCapabilities(capabilities);
178
- const context = await this.options.native.inspect();
204
+ const context = await this.options.native.inspect({ signal });
179
205
  if (!context.available || !context.frontmost || !context.timelineWindowAvailable || !context.timelineFocused) {
180
206
  throw new Error("CAPABILITY_UNAVAILABLE: disposable native timeline preflight");
181
207
  }
@@ -188,7 +214,7 @@ export class DisposableNativeEditWorkflow {
188
214
  if (context.target.kind !== "selected-clip" || context.target.name !== expectedName) {
189
215
  throw new Error("TARGET_MISMATCH: native selected clip does not match the canonical target");
190
216
  }
191
- if (previousTarget?.identity && context.target.identity && previousTarget.identity !== context.target.identity) {
217
+ if (previousTarget?.identity && previousTarget.identity !== context.target.identity) {
192
218
  throw new Error("TARGET_MISMATCH: native selected clip identity changed after disposable preview");
193
219
  }
194
220
  return context.target;