@mengine/medeo-client 1.2.1-alpha.7 → 1.2.1-alpha.8

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/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { A as resolveSpeechOverlapByShiftingVideos, B as partUnionToDraft, C as solveVideoDocument, D as reassignSpeechesToVideoClipsByTime, E as fillMainTrackTimeGaps, F as safeDurationMs, H as videoDocumentMirrorSchema, I as DEFAULT_UNIT_TIME_MS, L as VIDEO_DOCUMENT_SCHEMA_VERSION, M as TIMELINE_SKELETON_DURATION_MS, N as isEmptyVideoClip, O as recalculateTimelineDuration, P as partDurationMs, R as effectiveVideoClipDurationMs, S as laneTrackId, T as arrangeMainTrackSeamlessly, U as base64ToBytes, V as recordEntries, W as bytesToBase64, _ as partUnionSchema, a as createMirrorVideoDocument, b as ensureLaneTrack, c as readVideoDocumentFromDraft, d as derivePositionFromAbs, f as fromVideoDocument, g as validateVideoDocument, h as assertValidVideoDocument, i as MirrorVideoDocumentAdapter, j as syncAggregatedClipsTimePosition, k as resolveAllSpeechOverlaps, l as buildInitialVideoDocument, m as VideoDocumentValidationError, n as createPlainMemoryAdapter, o as createMirrorVideoDocumentAdapter, p as toVideoDocument, r as generatePartId, s as writeVideoDocumentToDraft, t as PlainMemoryAdapter, u as buildSpeechHostMap, v as videoDocumentSchema, w as cascadeAfterVideoClipChanges, x as findLaneTrack, y as LANE_KINDS_IN_STACK_ORDER, z as speedOf } from "./document-49OdvviW.js";
1
+ import { $ as base64ToBytes, A as findLaneTrack, B as syncAggregatedClipsTimePosition, C as VideoDocumentValidationError, D as videoDocumentSchema, E as partUnionSchema, F as fillMainTrackTimeGaps, G as DEFAULT_UNIT_TIME_MS, H as isEmptyVideoClip, I as reassignSpeechesToVideoClipsByTime, J as effectiveVideoClipDurationMs, K as ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION, L as recalculateTimelineDuration, M as solveVideoDocument, N as cascadeAfterVideoClipChanges, O as LANE_KINDS_IN_STACK_ORDER, P as arrangeMainTrackSeamlessly, Q as videoDocumentMirrorSchema, R as resolveAllSpeechOverlaps, S as toVideoDocument, T as validateVideoDocument, U as partDurationMs, V as TIMELINE_SKELETON_DURATION_MS, W as safeDurationMs, X as partUnionToDraft, Y as speedOf, Z as recordEntries, _ as EntityTimelineProjectionError, a as applyEntityTimelineProjection, b as derivePositionFromAbs, c as writeVideoDocumentToDraft, d as resolveEntityTimelineLayout, et as bytesToBase64, f as decodeEntityRelationRows, g as hasSequence, h as createRelationId, i as MirrorVideoDocumentAdapter, j as laneTrackId, k as ensureLaneTrack, l as readVideoDocumentFromDraft, m as createEntityId, n as createPlainMemoryAdapter, o as createMirrorVideoDocument, p as isJsonObject, q as VIDEO_DOCUMENT_SCHEMA_VERSION, r as generatePartId, s as createMirrorVideoDocumentAdapter, t as PlainMemoryAdapter, u as projectEntityTimeline, v as buildInitialVideoDocument, w as assertValidVideoDocument, x as fromVideoDocument, y as buildSpeechHostMap, z as resolveSpeechOverlapByShiftingVideos } from "./document-Df6awJDH.js";
2
2
  import { addSpeechesInputSchema, addVideoClipsInputSchema, adjustBgmVolumeInputSchema, adjustSpeechVolumeInputSchema, adjustVideoClipDurationInputSchema, adjustVideoClipVolumeInputSchema, changeSpeechScriptInputSchema, changeSpeechVoiceInputSchema, deleteBgmInputSchema, deleteSpeechesInputSchema, deleteVideoClipsInputSchema, moveSpeechesInputSchema, moveVideoClipsByAnchorInputSchema, moveVideoClipsInputSchema, replaceVideoClipContentInputSchema, replaceVideoClipSequenceInputSchema, setBgmInputSchema, setCaptionStyleInputSchema, setCaptionVisibilityInputSchema, setVideoClipSpeedShiftInputSchema, t as schemas_exports } from "./schemas.js";
3
3
  import { LoroDoc, VersionVector } from "loro-crdt";
4
4
  import { ClientServerSynchronizer, DocManager } from "@mengine/sync";
@@ -12,7 +12,7 @@ import { BaseDocStorage, DummyConnection } from "@mengine/storage";
12
12
  * namespace (mirrors the cluster's `/oapi/v1` precedent for an independent
13
13
  * subsystem) rather than folding into the shared `/api/v2/*` space.
14
14
  */
15
- const MENGINE_API_PREFIX = "/api/mengine/v1";
15
+ const MENGINE_API_PREFIX$1 = "/api/mengine/v1";
16
16
  var MengineHttpRequestError = class extends Error {
17
17
  status;
18
18
  payload;
@@ -134,12 +134,12 @@ var MengineHttpClient = class {
134
134
  ...init,
135
135
  headers
136
136
  });
137
- const payload = await safeReadJson(response);
137
+ const payload = await safeReadJson$1(response);
138
138
  if (!response.ok) throw new MengineHttpRequestError(response.status, payload);
139
139
  return payload;
140
140
  }
141
141
  endpoint(path) {
142
- return `${this.options.httpOrigin.replace(/\/$/, "")}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(this.options.docId)}/${path}`;
142
+ return `${this.options.httpOrigin.replace(/\/$/, "")}${MENGINE_API_PREFIX$1}/docs/${encodeURIComponent(this.options.docId)}/${path}`;
143
143
  }
144
144
  };
145
145
  /**
@@ -156,7 +156,7 @@ function asPushRejection(error) {
156
156
  if (body.kind !== "rejected" || typeof body.code !== "string") return void 0;
157
157
  return new MenginePushRejectedError(body.code, body.message ?? "", body.server_version, error.status);
158
158
  }
159
- async function safeReadJson(response) {
159
+ async function safeReadJson$1(response) {
160
160
  const text = await response.text();
161
161
  if (text.length === 0) return null;
162
162
  try {
@@ -217,6 +217,2170 @@ function findSseFrameBoundary(buffer) {
217
217
  return Math.min(lf, crlf);
218
218
  }
219
219
  //#endregion
220
+ //#region src/entity-editor/entity-graph-http-client.ts
221
+ const MENGINE_API_PREFIX = "/api/mengine/v1";
222
+ const numericMarkerValidation$1 = { compareMarkerPoints: (_marker, _range, left, right) => {
223
+ if (typeof left !== "number" || !Number.isFinite(left) || typeof right !== "number" || !Number.isFinite(right)) throw new Error("entity-state requires finite numeric Marker coordinates");
224
+ return left < right ? -1 : left > right ? 1 : 0;
225
+ } };
226
+ /** Browser-safe authenticated CAS client for a document's Entity graph. */
227
+ var EntityGraphHttpClient = class {
228
+ options;
229
+ transport;
230
+ endpoint;
231
+ constructor(options) {
232
+ this.options = options;
233
+ this.transport = new MengineHttpClient(options);
234
+ const origin = options.httpOrigin.replace(/\/$/, "");
235
+ this.endpoint = `${origin}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(options.docId)}/entity-state`;
236
+ }
237
+ async fetchState() {
238
+ return this.request({ method: "GET" });
239
+ }
240
+ /**
241
+ * Replace the complete Entity graph under the baseline revision.
242
+ *
243
+ * Deletion intent is derived from baseline IDs omitted from `nextRows`, so a
244
+ * transient create-then-remove never becomes a request to delete unknown
245
+ * persisted data.
246
+ */
247
+ async commit(baseState, nextRows, options = {}) {
248
+ if (!isNonNegativeSafeInteger(baseState.revision)) throw new Error("Entity graph revision must be a non-negative safe integer");
249
+ const baseRows = cloneAndValidateRows(baseState.rows);
250
+ const next = cloneAndValidateRows(nextRows);
251
+ const nextEntityIds = new Set(next.entities.map((row) => row.entityId));
252
+ const nextRelationIds = new Set(next.relations.map((row) => row.relationId));
253
+ const deletedEntityIds = baseRows.entities.map((row) => row.entityId).filter((entityId) => !nextEntityIds.has(entityId)).sort();
254
+ const deletedRelationIds = baseRows.relations.map((row) => row.relationId).filter((relationId) => !nextRelationIds.has(relationId)).sort();
255
+ return this.request({
256
+ method: "POST",
257
+ body: JSON.stringify({
258
+ expected_revision: baseState.revision,
259
+ rows: toWireRows(next),
260
+ deleted_entity_ids: deletedEntityIds,
261
+ deleted_relation_ids: deletedRelationIds,
262
+ ...options.migrationBaseVv === void 0 ? {} : { migration_base_vv: options.migrationBaseVv }
263
+ })
264
+ });
265
+ }
266
+ async request(init) {
267
+ const response = await this.transport.fetch(this.endpoint, {
268
+ ...init,
269
+ headers: this.transport.headers()
270
+ });
271
+ const payload = await safeReadJson(response);
272
+ if (!response.ok) throw new MengineHttpRequestError(response.status, payload);
273
+ return parseWireState(payload, this.options.docId);
274
+ }
275
+ };
276
+ function parseWireState(value, expectedDocId) {
277
+ if (!isRecord(value) || typeof value.doc_id !== "string" || !isNonNegativeSafeInteger(value.revision)) throw new Error("Invalid entity-state response envelope");
278
+ if (value.doc_id !== expectedDocId) throw new Error(`Entity-state response doc_id mismatch: expected "${expectedDocId}"`);
279
+ const rows = parseWireRows(value.rows);
280
+ decodeEntityRelationRows(rows, numericMarkerValidation$1);
281
+ return {
282
+ revision: value.revision,
283
+ rows
284
+ };
285
+ }
286
+ function parseWireRows(value) {
287
+ if (!isRecord(value) || !Array.isArray(value.entities) || !Array.isArray(value.relations)) throw new Error("Invalid entity-state response rows");
288
+ return {
289
+ entities: value.entities.map(parseWireEntity),
290
+ relations: value.relations.map(parseWireRelation)
291
+ };
292
+ }
293
+ function parseWireEntity(value) {
294
+ if (!isRecord(value) || !isJsonObject(value.payload)) throw new Error("Invalid Entity row in entity-state response");
295
+ return {
296
+ entityId: createEntityId(requiredString(value.entity_id, "entity_id")),
297
+ entityKind: requiredString(value.entity_kind, "entity_kind"),
298
+ payload: structuredClone(value.payload)
299
+ };
300
+ }
301
+ function parseWireRelation(value) {
302
+ if (!isRecord(value) || !isJsonObject(value.metadata) || !isJsonObject(value.trace)) throw new Error("Invalid Relation row in entity-state response");
303
+ return {
304
+ relationId: createRelationId(requiredString(value.relation_id, "relation_id")),
305
+ relationKind: requiredString(value.relation_kind, "relation_kind"),
306
+ endpoint0EntityId: createEntityId(requiredString(value.endpoint_0_entity_id, "endpoint_0_entity_id")),
307
+ endpoint1EntityId: createEntityId(requiredString(value.endpoint_1_entity_id, "endpoint_1_entity_id")),
308
+ metadata: structuredClone(value.metadata),
309
+ trace: structuredClone(value.trace)
310
+ };
311
+ }
312
+ function cloneAndValidateRows(rows) {
313
+ const clone = structuredClone(rows);
314
+ decodeEntityRelationRows(clone, numericMarkerValidation$1);
315
+ return clone;
316
+ }
317
+ function toWireRows(rows) {
318
+ return {
319
+ entities: rows.entities.map((row) => ({
320
+ entity_id: row.entityId,
321
+ entity_kind: row.entityKind,
322
+ payload: row.payload
323
+ })),
324
+ relations: rows.relations.map((row) => ({
325
+ relation_id: row.relationId,
326
+ relation_kind: row.relationKind,
327
+ endpoint_0_entity_id: row.endpoint0EntityId,
328
+ endpoint_1_entity_id: row.endpoint1EntityId,
329
+ metadata: row.metadata,
330
+ trace: row.trace
331
+ }))
332
+ };
333
+ }
334
+ function requiredString(value, field) {
335
+ if (typeof value !== "string" || value.length === 0 || value.trim() !== value) throw new Error(`Invalid ${field} in entity-state response`);
336
+ return value;
337
+ }
338
+ function isRecord(value) {
339
+ return value !== null && typeof value === "object" && !Array.isArray(value);
340
+ }
341
+ function isNonNegativeSafeInteger(value) {
342
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
343
+ }
344
+ async function safeReadJson(response) {
345
+ const text = await response.text();
346
+ if (text.length === 0) return null;
347
+ try {
348
+ return JSON.parse(text);
349
+ } catch {
350
+ return text;
351
+ }
352
+ }
353
+ //#endregion
354
+ //#region src/entity-editor/media-asset-import.ts
355
+ const numericCoordinates = { compareMarkerPoints: (_marker, _range, left, right) => {
356
+ if (typeof left !== "number" || typeof right !== "number" || !Number.isFinite(left) || !Number.isFinite(right)) throw new Error("Media editor requires numeric coordinates");
357
+ return left - right;
358
+ } };
359
+ /** Import at the editor boundary, not at asset generation time. Existing provenance is retained. */
360
+ function importMediaAsset(initial, fact, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`, selection = {}) {
361
+ if (!fact.assetId || fact.assetId.trim() !== fact.assetId) throw new Error("Expected an asset id");
362
+ if (![
363
+ "image",
364
+ "video",
365
+ "audio",
366
+ "voice"
367
+ ].includes(fact.kind)) throw new Error("Unsupported media kind");
368
+ if (fact.kind !== "image" && (!Number.isFinite(fact.durationMs) || fact.durationMs <= 0)) throw new Error(`${fact.kind} ${fact.assetId} requires its factual positive duration`);
369
+ if ((fact.kind === "audio" || fact.kind === "voice") && fact.storageKey.trim() === "") throw new Error(`${fact.kind} ${fact.assetId} requires its physical storage key`);
370
+ decodeEntityRelationRows(initial, numericCoordinates);
371
+ const entities = structuredClone([...initial.entities]);
372
+ const relations = structuredClone([...initial.relations]);
373
+ const assets = entities.filter((row) => {
374
+ const external = row.payload.external;
375
+ return row.entityKind === "asset" && isJsonObject(external) && external.system === (fact.kind === "voice" ? "memota-speech" : "memota") && external.key === fact.assetId && (selection.assetEntityId === void 0 || row.entityId === selection.assetEntityId);
376
+ });
377
+ if (selection.assetEntityId !== void 0 && assets.length !== 1) throw new Error("Selected Asset does not match the recalled asset facts");
378
+ if (assets.length > 1) throw new Error(`Ambiguous Asset bindings for ${fact.assetId}; select assetEntityId explicitly`);
379
+ const mintEntity = () => {
380
+ const id = createEntityId(idFactory("entity"));
381
+ if (entities.some((row) => row.entityId === id)) throw new Error(`Duplicate entity id ${id}`);
382
+ return id;
383
+ };
384
+ let asset = assets[0];
385
+ if (asset === void 0) {
386
+ asset = {
387
+ entityId: mintEntity(),
388
+ entityKind: "asset",
389
+ payload: {
390
+ external: {
391
+ system: fact.kind === "voice" ? "memota-speech" : "memota",
392
+ key: fact.assetId
393
+ },
394
+ ...fact.storageKey === void 0 ? {} : { storageKey: fact.storageKey }
395
+ }
396
+ };
397
+ entities.push(asset);
398
+ } else if (fact.storageKey !== void 0) {
399
+ if (asset.payload.storageKey !== void 0 && asset.payload.storageKey !== fact.storageKey) throw new Error(`Media facts conflict with existing storage key for ${fact.assetId}`);
400
+ if (asset.payload.storageKey === void 0) {
401
+ asset = {
402
+ ...asset,
403
+ payload: {
404
+ ...asset.payload,
405
+ storageKey: fact.storageKey
406
+ }
407
+ };
408
+ const assetIndex = entities.findIndex((row) => row.entityId === asset.entityId);
409
+ entities[assetIndex] = asset;
410
+ }
411
+ }
412
+ const bindings = relations.filter((row) => row.relationKind === "physical-asset" && (row.endpoint0EntityId === asset.entityId || row.endpoint1EntityId === asset.entityId) && (selection.contentEntityId === void 0 || (row.endpoint0EntityId === asset.entityId ? row.endpoint1EntityId : row.endpoint0EntityId) === selection.contentEntityId));
413
+ const contentIds = new Set(bindings.map((row) => row.endpoint0EntityId === asset.entityId ? row.endpoint1EntityId : row.endpoint0EntityId));
414
+ if (selection.contentEntityId !== void 0 && contentIds.size !== 1) throw new Error("Selected media Entity is not bound to the selected Asset");
415
+ if (contentIds.size > 1) throw new Error(`Ambiguous media bindings for ${fact.assetId}; select contentEntityId explicitly`);
416
+ let content;
417
+ if (bindings[0] !== void 0) {
418
+ const relation = bindings[0];
419
+ content = entities.find((row) => row.entityId === (relation.endpoint0EntityId === asset.entityId ? relation.endpoint1EntityId : relation.endpoint0EntityId));
420
+ const extent = content?.payload.extent;
421
+ if (content?.entityKind !== fact.kind || fact.kind !== "image" && (!isJsonObject(extent) || extent.start !== 0 || extent.end !== fact.durationMs) || fact.kind === "voice" && !sameVoice(content.payload.voice, fact.voice)) throw new Error(`Media facts conflict with existing binding for ${fact.assetId}`);
422
+ } else {
423
+ const payload = {
424
+ extent: fact.kind === "image" ? {
425
+ kind: "unbounded",
426
+ start: 0
427
+ } : {
428
+ kind: "bounded",
429
+ start: 0,
430
+ end: fact.durationMs
431
+ },
432
+ sampling: fact.kind === "image" ? "constant" : "native",
433
+ coordinateSpace: "milliseconds",
434
+ ...fact.kind === "voice" ? { voice: {
435
+ system: fact.voice.system,
436
+ key: fact.voice.key,
437
+ ...fact.voice.name === void 0 ? {} : { name: fact.voice.name }
438
+ } } : {}
439
+ };
440
+ const createdContent = {
441
+ entityId: mintEntity(),
442
+ entityKind: fact.kind,
443
+ payload
444
+ };
445
+ content = createdContent;
446
+ entities.push(createdContent);
447
+ const relationId = createRelationId(idFactory("relation"));
448
+ if (relations.some((row) => row.relationId === relationId)) throw new Error(`Duplicate relation id ${relationId}`);
449
+ relations.push({
450
+ relationId,
451
+ relationKind: "physical-asset",
452
+ endpoint0EntityId: createdContent.entityId,
453
+ endpoint1EntityId: asset.entityId,
454
+ metadata: {},
455
+ trace: {}
456
+ });
457
+ }
458
+ const rows = {
459
+ entities,
460
+ relations
461
+ };
462
+ decodeEntityRelationRows(rows, numericCoordinates);
463
+ if (content === void 0) throw new Error(`Media Entity could not be resolved for ${fact.assetId}`);
464
+ return {
465
+ rows,
466
+ assetEntityId: asset.entityId,
467
+ contentEntityId: content.entityId
468
+ };
469
+ }
470
+ function sameVoice(left, right) {
471
+ return isJsonObject(left) && left.system === right.system && left.key === right.key && left.name === right.name && Object.keys(left).every((key) => key === "system" || key === "key" || key === "name");
472
+ }
473
+ //#endregion
474
+ //#region src/entity-editor/entity-timeline-editor.ts
475
+ const roleOrder = {
476
+ caption: 0,
477
+ video_clip: 1,
478
+ speech: 2,
479
+ bgm: 3
480
+ };
481
+ const defaultIdFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`;
482
+ /**
483
+ * Graph-native timeline authoring over authoritative Medeo Entity and Relation rows.
484
+ *
485
+ * Each mutation is applied to a detached graph, validated as a complete DSL set,
486
+ * and committed only after validation succeeds.
487
+ */
488
+ var EntityTimelineEditor = class {
489
+ idFactory;
490
+ state;
491
+ constructor(initialRows, idFactory = defaultIdFactory) {
492
+ this.idFactory = idFactory;
493
+ this.state = cloneRows(initialRows);
494
+ }
495
+ rows() {
496
+ return cloneRows(this.state);
497
+ }
498
+ insertClip(input) {
499
+ const requestedOrder = input.clipPayload?.order;
500
+ const order = typeof requestedOrder === "number" ? requestedOrder : nextSequentialOrder(this.state, createEntityId(input.trackEntityId));
501
+ return this.insertPlacedClip({
502
+ trackEntityId: input.trackEntityId,
503
+ contentEntityId: input.contentEntityId,
504
+ sourceRange: input.sourceRange,
505
+ duration: input.duration,
506
+ placement: input.targetRange === void 0 ? {
507
+ kind: "sequential",
508
+ order
509
+ } : {
510
+ kind: "absolute",
511
+ targetRange: input.targetRange
512
+ },
513
+ clipPayload: input.clipPayload
514
+ });
515
+ }
516
+ insertPlacedClip(input) {
517
+ return this.transact((draft) => this.insertPlacedClipIntoDraft(draft, input));
518
+ }
519
+ updateClipMarker(input) {
520
+ this.transact((draft) => {
521
+ const { marker } = requireClipTopology(draft, input.clipEntityId);
522
+ const payload = { ...cloneJsonObject(marker.payload, "Sequence Marker payload") };
523
+ if (input.sourceRange !== void 0) payload.sourceRange = cloneJsonValue(input.sourceRange, "sourceRange");
524
+ if (input.duration !== void 0) payload.duration = cloneJsonValue(input.duration, "duration");
525
+ if (Object.hasOwn(input, "targetRange")) if (input.targetRange === void 0) {
526
+ delete payload.targetRange;
527
+ if (!hasOutgoingClipAnchor(draft, input.clipEntityId)) {
528
+ const trackId = otherEndpoint(requireOneRelation(draft, createEntityId(input.clipEntityId), "track-clip"), input.clipEntityId);
529
+ const clipPayload = { ...cloneJsonObject(requireEntityKind(draft, input.clipEntityId, "clip").payload, "Clip payload") };
530
+ clipPayload.order ??= nextSequentialOrder(draft, trackId);
531
+ replaceEntity(draft, createEntityId(input.clipEntityId), {
532
+ ...requireEntityKind(draft, input.clipEntityId, "clip"),
533
+ payload: clipPayload
534
+ });
535
+ }
536
+ } else {
537
+ payload.targetRange = cloneJsonValue(input.targetRange, "targetRange");
538
+ const clip = requireEntityKind(draft, input.clipEntityId, "clip");
539
+ const clipPayload = { ...cloneJsonObject(clip.payload, "Clip payload") };
540
+ delete clipPayload.order;
541
+ replaceEntity(draft, clip.entityId, {
542
+ ...clip,
543
+ payload: clipPayload
544
+ });
545
+ }
546
+ if (Object.hasOwn(input, "timeRemapping")) if (input.timeRemapping === void 0) delete payload.timeRemapping;
547
+ else payload.timeRemapping = cloneJsonValue(input.timeRemapping, "timeRemapping");
548
+ replaceEntity(draft, marker.entityId, {
549
+ ...marker,
550
+ payload
551
+ });
552
+ });
553
+ }
554
+ setClipPlacement(input) {
555
+ this.transact((draft) => this.applyClipPlacement(draft, input.clipEntityId, input.placement));
556
+ }
557
+ moveSequentialClips(input) {
558
+ if (input.clipEntityIds.length === 0) throw new Error("At least one Clip is required");
559
+ this.transact((draft) => {
560
+ const original = resolveEntityTimelineLayout(draft);
561
+ this.normalizeMainSequential(draft);
562
+ const before = resolveEntityTimelineLayout(draft);
563
+ const targetIds = new Set(input.clipEntityIds);
564
+ if (targetIds.size !== input.clipEntityIds.length) throw new Error("Clip ids must be unique");
565
+ const placements = input.clipEntityIds.map((id) => before.clips.get(id));
566
+ if (placements.some((placement) => placement === void 0 || placement.mode !== "sequential")) throw new Error("moveSequentialClips accepts only sequential Clips");
567
+ const trackIds = new Set(placements.map((placement) => placement.trackEntityId));
568
+ if (trackIds.size !== 1) throw new Error("Moved Clips must belong to one Track");
569
+ const trackId = [...trackIds][0];
570
+ const track = before.tracks.find((candidate) => candidate.entityId === trackId);
571
+ if (track?.role !== "video_clip") throw new Error("Only the video_clip Track supports sequential block moves");
572
+ const ordered = track.clips.filter((placement) => placement.mode === "sequential").map((placement) => placement.clipEntityId);
573
+ if (input.anchor.position !== "trackStart" && targetIds.has(input.anchor.clipEntityId)) throw new Error("Move anchor cannot be one of the moved Clips");
574
+ const moved = ordered.filter((id) => targetIds.has(id));
575
+ if (moved.length !== targetIds.size) throw new Error("Every moved Clip must exist on the target Track");
576
+ const remaining = ordered.filter((id) => !targetIds.has(id));
577
+ let insertAt = 0;
578
+ if (input.anchor.position !== "trackStart") {
579
+ const anchorIndex = remaining.indexOf(input.anchor.clipEntityId);
580
+ if (anchorIndex < 0) throw new Error("Move anchor Clip does not exist on the target Track");
581
+ insertAt = anchorIndex + (input.anchor.position === "after" ? 1 : 0);
582
+ }
583
+ [
584
+ ...remaining.slice(0, insertAt),
585
+ ...moved,
586
+ ...remaining.slice(insertAt)
587
+ ].forEach((clipId, order) => setClipOrder(draft, clipId, order));
588
+ if (input.onAnchored === "keepAbsolute") {
589
+ const affected = outgoingAnchorChildren(draft, new Set(track.clips.map((clip) => clip.clipEntityId))).filter((childId) => clipRole(draft, childId) === "speech").map((childId) => {
590
+ const placement = original.clips.get(childId);
591
+ if (placement === void 0) throw new Error(`Anchored child Clip "${childId}" is not placed`);
592
+ return {
593
+ childId,
594
+ start: placement.start
595
+ };
596
+ });
597
+ const after = resolveEntityTimelineLayout(draft);
598
+ for (const child of affected) this.reanchorAtAbsolute(draft, child.childId, child.start, after);
599
+ }
600
+ this.resolveOneVoiceoverOverlap(draft);
601
+ });
602
+ }
603
+ moveClipsToStarts(input) {
604
+ if (input.moves.length === 0) throw new Error("At least one Clip move is required");
605
+ this.transact((draft) => {
606
+ const original = resolveEntityTimelineLayout(draft);
607
+ const originalVideo = original.tracks.find((track) => track.role === "video_clip");
608
+ if (originalVideo === void 0) throw new Error("The timeline has no video_clip Track");
609
+ const preservedVoiceovers = outgoingAnchorChildren(draft, new Set(originalVideo.clips.map((clip) => clip.clipEntityId))).filter((clipId) => clipRole(draft, clipId) === "speech").map((clipId) => ({
610
+ clipId,
611
+ start: original.clips.get(clipId).start
612
+ }));
613
+ this.normalizeMainSequential(draft);
614
+ for (const move of input.moves) {
615
+ finiteNonNegative(move.newStartMs, "newStartMs");
616
+ requireClipOnRole(draft, move.clipEntityId, "video_clip");
617
+ const track = resolveEntityTimelineLayout(draft).tracks.find((candidate) => candidate.role === "video_clip");
618
+ const ordered = track.clips.map((clip) => clip.clipEntityId);
619
+ const from = ordered.indexOf(move.clipEntityId);
620
+ if (from < 0) throw new Error(`Clip "${move.clipEntityId}" is not on the video_clip Track`);
621
+ const remaining = track.clips.filter((clip) => clip.clipEntityId !== move.clipEntityId);
622
+ let cursor = 0;
623
+ let target = remaining.length;
624
+ for (const [index, clip] of remaining.entries()) {
625
+ if (move.newStartMs <= cursor + clip.duration / 2) {
626
+ target = index;
627
+ break;
628
+ }
629
+ cursor += clip.duration;
630
+ }
631
+ const next = [...ordered];
632
+ next.splice(from, 1);
633
+ next.splice(target, 0, move.clipEntityId);
634
+ next.forEach((clipId, order) => setClipOrder(draft, clipId, order));
635
+ }
636
+ const after = resolveEntityTimelineLayout(draft);
637
+ for (const voiceover of preservedVoiceovers) this.reanchorAtAbsolute(draft, voiceover.clipId, voiceover.start, after);
638
+ this.resolveOneVoiceoverOverlap(draft);
639
+ });
640
+ }
641
+ moveClip(input) {
642
+ this.transact((draft) => {
643
+ const topology = requireClipTopology(draft, input.clipEntityId);
644
+ const track = requireEntityKind(draft, input.trackEntityId, "track");
645
+ requireTrackTimeline(draft, track.entityId);
646
+ this.relinkRelation(draft, topology.trackRelation, {
647
+ endpoint0EntityId: track.entityId,
648
+ endpoint1EntityId: topology.clip.entityId
649
+ });
650
+ });
651
+ }
652
+ replaceClipContent(input) {
653
+ this.transact((draft) => {
654
+ const topology = requireClipTopology(draft, input.clipEntityId);
655
+ const content = requireSequenceContent(draft, input.contentEntityId);
656
+ const payload = {
657
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
658
+ sourceRange: cloneJsonValue(input.sourceRange, "sourceRange"),
659
+ duration: cloneJsonValue(input.duration, "duration")
660
+ };
661
+ delete payload.targetRange;
662
+ delete payload.timeRemapping;
663
+ if (input.targetRange !== void 0) payload.targetRange = cloneJsonValue(input.targetRange, "targetRange");
664
+ if (input.timeRemapping !== void 0) payload.timeRemapping = cloneJsonValue(input.timeRemapping, "timeRemapping");
665
+ replaceEntity(draft, topology.marker.entityId, {
666
+ ...topology.marker,
667
+ payload
668
+ });
669
+ if (!hasOutgoingClipAnchor(draft, topology.clip.entityId)) {
670
+ const clipPayload = { ...cloneJsonObject(topology.clip.payload, "Clip payload") };
671
+ if (input.targetRange === void 0) clipPayload.order ??= nextSequentialOrder(draft, otherEndpoint(topology.trackRelation, topology.clip.entityId));
672
+ else delete clipPayload.order;
673
+ replaceEntity(draft, topology.clip.entityId, {
674
+ ...topology.clip,
675
+ payload: clipPayload
676
+ });
677
+ }
678
+ this.relinkRelation(draft, topology.markerContentRelation, {
679
+ endpoint0EntityId: topology.marker.entityId,
680
+ endpoint1EntityId: content.entityId
681
+ });
682
+ });
683
+ }
684
+ insertMediaClip(input) {
685
+ return this.transact((draft) => {
686
+ if (input.placement.kind === "sequential") this.normalizeMainSequential(draft);
687
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
688
+ replaceRows(draft, imported.rows);
689
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
690
+ const sourceRange = requirePositiveRange(input.sourceRange, "sourceRange");
691
+ const clipId = this.insertPlacedClipIntoDraft(draft, {
692
+ trackEntityId: track.entityId,
693
+ contentEntityId: imported.contentEntityId,
694
+ sourceRange,
695
+ duration: {
696
+ mode: "fixed",
697
+ value: sourceRange.end - sourceRange.start
698
+ },
699
+ placement: input.placement,
700
+ ...input.clipEntityId === void 0 ? {} : { clipEntityId: input.clipEntityId },
701
+ ...input.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(input.volume) } }
702
+ });
703
+ if (input.placement.kind === "sequential") this.resolveOneVoiceoverOverlap(draft);
704
+ return clipId;
705
+ });
706
+ }
707
+ insertMediaClips(input) {
708
+ if (input.clips.length === 0) throw new Error("At least one media Clip is required");
709
+ return this.transact((draft) => {
710
+ this.normalizeMainSequential(draft);
711
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
712
+ const ordered = resolveEntityTimelineLayout(draft).tracks.find((candidate) => candidate.entityId === track.entityId)?.clips ?? [];
713
+ let insertAt = ordered.length;
714
+ const insertion = input.insertion;
715
+ if (insertion.kind === "before" || insertion.kind === "after") {
716
+ const anchorIndex = ordered.findIndex((clip) => clip.clipEntityId === insertion.clipEntityId);
717
+ if (anchorIndex >= 0) insertAt = anchorIndex + (insertion.kind === "after" ? 1 : 0);
718
+ } else {
719
+ const startMs = finiteNonNegative(insertion.startMs, "firstStart.startMs");
720
+ let cursor = 0;
721
+ for (const [index, clip] of ordered.entries()) {
722
+ if (startMs <= cursor + clip.duration / 2) {
723
+ insertAt = index;
724
+ break;
725
+ }
726
+ cursor += clip.duration;
727
+ }
728
+ }
729
+ const created = [];
730
+ for (const [offset, clip] of input.clips.entries()) {
731
+ const imported = importMediaAsset(draft, clip.media, this.idFactory);
732
+ replaceRows(draft, imported.rows);
733
+ const sourceRange = requirePositiveRange(clip.sourceRange, "sourceRange");
734
+ created.push(this.insertPlacedClipIntoDraft(draft, {
735
+ trackEntityId: track.entityId,
736
+ contentEntityId: imported.contentEntityId,
737
+ sourceRange,
738
+ duration: {
739
+ mode: "fixed",
740
+ value: sourceRange.end - sourceRange.start
741
+ },
742
+ placement: {
743
+ kind: "sequential",
744
+ order: ordered.length + offset
745
+ },
746
+ ...clip.clipEntityId === void 0 ? {} : { clipEntityId: clip.clipEntityId },
747
+ ...clip.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(clip.volume) } }
748
+ }));
749
+ }
750
+ const desired = ordered.map((clip) => clip.clipEntityId);
751
+ desired.splice(insertAt, 0, ...created);
752
+ desired.forEach((clipId, order) => setClipOrder(draft, clipId, order));
753
+ this.resolveOneVoiceoverOverlap(draft);
754
+ return created;
755
+ });
756
+ }
757
+ replaceMediaClip(input) {
758
+ this.transact((draft) => {
759
+ this.normalizeMainSequential(draft);
760
+ const topology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
761
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
762
+ replaceRows(draft, imported.rows);
763
+ const sourceRange = requirePositiveRange(input.sourceRange, "sourceRange");
764
+ const markerPayloadValue = {
765
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
766
+ sourceRange,
767
+ duration: {
768
+ mode: "fixed",
769
+ value: sourceRange.end - sourceRange.start
770
+ }
771
+ };
772
+ delete markerPayloadValue.timeRemapping;
773
+ resizeAbsoluteTarget(markerPayloadValue, sourceRange.end - sourceRange.start);
774
+ replaceEntity(draft, topology.marker.entityId, {
775
+ ...topology.marker,
776
+ payload: markerPayloadValue
777
+ });
778
+ this.relinkRelation(draft, topology.markerContentRelation, {
779
+ endpoint0EntityId: topology.marker.entityId,
780
+ endpoint1EntityId: createEntityId(imported.contentEntityId)
781
+ });
782
+ this.resolveOneVoiceoverOverlap(draft);
783
+ });
784
+ }
785
+ setClipVolume(input) {
786
+ this.transact((draft) => {
787
+ const topology = requireClipTopology(draft, input.clipEntityId);
788
+ requireSupportedRole(draft, topology);
789
+ replaceEntity(draft, topology.clip.entityId, {
790
+ ...topology.clip,
791
+ payload: {
792
+ ...topology.clip.payload,
793
+ volume: requireVolume(input.volume)
794
+ }
795
+ });
796
+ });
797
+ }
798
+ setClipSpeed(input) {
799
+ this.transact((draft) => {
800
+ const initialTopology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
801
+ const content = requireSequenceContent(draft, otherEndpoint(initialTopology.markerContentRelation, initialTopology.marker.entityId));
802
+ if (content.entityKind !== "video" && content.entityKind !== "image") throw new Error("Clip speed is supported only for Video or Image content");
803
+ const initialPayload = cloneJsonObject(initialTopology.marker.payload, "Sequence Marker payload");
804
+ const priorSource = requirePositiveRange(initialPayload.sourceRange, "sourceRange");
805
+ if (input.timeRemapping === null && initialPayload.timeRemapping === void 0) return;
806
+ this.normalizeMainSequential(draft);
807
+ const topology = requireClipTopology(draft, input.clipEntityId);
808
+ const payload = { ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload") };
809
+ let sourceRange = priorSource;
810
+ if (content.entityKind === "image" && initialPayload.timeRemapping === void 0 && input.timeRemapping !== null) sourceRange = {
811
+ start: 0,
812
+ end: markerDuration(initialPayload, priorSource)
813
+ };
814
+ if (input.timeRemapping === null) {
815
+ delete payload.timeRemapping;
816
+ payload.sourceRange = sourceRange;
817
+ payload.duration = {
818
+ mode: "fixed",
819
+ value: sourceRange.end - sourceRange.start
820
+ };
821
+ } else {
822
+ const rate = finitePositive(input.timeRemapping.rate, "linear speed rate");
823
+ const duration = Math.round((sourceRange.end - sourceRange.start) / rate);
824
+ if (duration <= 0 || !Number.isSafeInteger(duration)) throw new Error("Linear speed produces an invalid duration");
825
+ payload.sourceRange = sourceRange;
826
+ payload.duration = {
827
+ mode: "fixed",
828
+ value: duration
829
+ };
830
+ payload.timeRemapping = {
831
+ kind: "linear",
832
+ rate,
833
+ ...input.timeRemapping.mode === void 0 ? {} : { mode: input.timeRemapping.mode }
834
+ };
835
+ }
836
+ resizeAbsoluteTarget(payload, markerDuration(payload, sourceRange));
837
+ replaceEntity(draft, topology.marker.entityId, {
838
+ ...topology.marker,
839
+ payload
840
+ });
841
+ this.resolveOneVoiceoverOverlap(draft);
842
+ });
843
+ }
844
+ trimClip(input) {
845
+ this.transact((draft) => {
846
+ this.normalizeMainSequential(draft);
847
+ const topology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
848
+ const content = requireSequenceContent(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId));
849
+ if (content.entityKind !== "video" && content.entityKind !== "image") throw new Error("Clip trimming is supported only for Video or Image content");
850
+ const sourceRange = requirePositiveRange(input.sourceRange, "sourceRange");
851
+ const payload = {
852
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
853
+ sourceRange
854
+ };
855
+ const rate = linearRate(payload.timeRemapping);
856
+ const duration = Math.round((sourceRange.end - sourceRange.start) / rate);
857
+ if (duration <= 0 || !Number.isSafeInteger(duration)) throw new Error("Trim produces an invalid duration");
858
+ payload.duration = {
859
+ mode: "fixed",
860
+ value: duration
861
+ };
862
+ resizeAbsoluteTarget(payload, duration);
863
+ replaceEntity(draft, topology.marker.entityId, {
864
+ ...topology.marker,
865
+ payload
866
+ });
867
+ this.resolveOneVoiceoverOverlap(draft);
868
+ });
869
+ }
870
+ replaceSequentialClips(input) {
871
+ if (input.oldClipEntityIds.length === 0 || input.newClips.length === 0) throw new Error("Replacement requires at least one old and one new Clip");
872
+ return this.transact((draft) => {
873
+ this.normalizeMainSequential(draft);
874
+ const before = resolveEntityTimelineLayout(draft);
875
+ const oldIds = new Set(input.oldClipEntityIds);
876
+ if (oldIds.size !== input.oldClipEntityIds.length) throw new Error("Old Clip ids must be unique");
877
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
878
+ const ordered = before.tracks.find((candidate) => candidate.entityId === track.entityId)?.clips.filter((clip) => clip.mode === "sequential").map((clip) => clip.clipEntityId);
879
+ if (ordered === void 0) throw new Error("The video_clip Track is not present in the current layout");
880
+ const indexes = input.oldClipEntityIds.map((id) => ordered.indexOf(id));
881
+ if (indexes.some((index) => index < 0)) throw new Error("Every replaced Clip must be sequential on video_clip");
882
+ const sortedIndexes = [...indexes].sort((left, right) => left - right);
883
+ if (sortedIndexes.some((index, offset) => index !== sortedIndexes[0] + offset)) throw new Error("Replaced Clips must form one contiguous timeline run");
884
+ if (input.oldClipEntityIds.some((id, index) => id !== ordered[sortedIndexes[0] + index])) throw new Error("Replaced Clip ids must be listed in timeline order");
885
+ const insertOrder = sortedIndexes[0];
886
+ const temporaryOrder = ordered.length;
887
+ const created = [];
888
+ for (const [offset, replacement] of input.newClips.entries()) {
889
+ if (replacement.clipEntityId !== void 0 && oldIds.has(replacement.clipEntityId)) throw new Error("A replacement Clip id cannot reuse an Entity being removed");
890
+ const imported = importMediaAsset(draft, replacement.media, this.idFactory);
891
+ replaceRows(draft, imported.rows);
892
+ const sourceRange = requirePositiveRange(replacement.sourceRange, "sourceRange");
893
+ created.push(this.insertPlacedClipIntoDraft(draft, {
894
+ trackEntityId: track.entityId,
895
+ contentEntityId: imported.contentEntityId,
896
+ sourceRange,
897
+ duration: {
898
+ mode: "fixed",
899
+ value: sourceRange.end - sourceRange.start
900
+ },
901
+ placement: {
902
+ kind: "sequential",
903
+ order: temporaryOrder + offset
904
+ },
905
+ ...replacement.clipEntityId === void 0 ? {} : { clipEntityId: replacement.clipEntityId },
906
+ ...replacement.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(replacement.volume) } }
907
+ }));
908
+ }
909
+ for (const [index, oldId] of input.oldClipEntityIds.entries()) {
910
+ const children = directAnchorChildren(draft, oldId);
911
+ if (input.onAnchored === "remap" && created[index] !== void 0) for (const childId of children) this.replaceAnchorHost(draft, childId, created[index]);
912
+ else {
913
+ const scheduled = /* @__PURE__ */ new Set();
914
+ for (const childId of children) {
915
+ scheduled.add(childId);
916
+ this.removeAnchoredDescendants(draft, childId, scheduled);
917
+ this.removeClipGraph(draft, childId);
918
+ }
919
+ }
920
+ }
921
+ for (const oldId of input.oldClipEntityIds) this.removeClipGraph(draft, oldId);
922
+ [
923
+ ...ordered.slice(0, insertOrder),
924
+ ...created,
925
+ ...ordered.slice(insertOrder + input.oldClipEntityIds.length)
926
+ ].forEach((clipId, order) => setClipOrder(draft, clipId, order));
927
+ this.resolveOneVoiceoverOverlap(draft);
928
+ return created;
929
+ });
930
+ }
931
+ deleteClip(input) {
932
+ this.transact((draft) => {
933
+ const topology = requireClipTopology(draft, input.clipEntityId);
934
+ assertOnlyStructuralIncidents(draft, topology.clip.entityId, new Set(["track-clip", "clip-marker"]));
935
+ assertOnlyStructuralIncidents(draft, topology.marker.entityId, new Set(["clip-marker", "marker-content"]));
936
+ const markerIsExclusive = incidentRelations(draft, topology.marker.entityId).filter((relation) => relation.relationKind === "clip-marker").length === 1;
937
+ const removedRelationIds = new Set([topology.trackRelation.relationId, topology.clipMarkerRelation.relationId]);
938
+ if (markerIsExclusive) for (const relation of incidentRelations(draft, topology.marker.entityId)) removedRelationIds.add(relation.relationId);
939
+ draft.relations = draft.relations.filter((relation) => !removedRelationIds.has(relation.relationId));
940
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== topology.clip.entityId && (!markerIsExclusive || entity.entityId !== topology.marker.entityId));
941
+ });
942
+ }
943
+ deleteClipTree(input) {
944
+ if (input.clipEntityIds.length === 0) throw new Error("At least one Clip is required");
945
+ this.transact((draft) => {
946
+ const targets = new Set(input.clipEntityIds);
947
+ if (targets.size !== input.clipEntityIds.length) throw new Error("Clip ids must be unique");
948
+ for (const target of targets) requireEntityKind(draft, target, "clip");
949
+ const includesVisual = [...targets].some((target) => clipRole(draft, target) === "video_clip");
950
+ const original = resolveEntityTimelineLayout(draft);
951
+ if (includesVisual) this.normalizeMainSequential(draft);
952
+ if (input.onAnchored === "detach") for (const childId of outgoingAnchorChildren(draft, targets)) {
953
+ if (targets.has(childId)) continue;
954
+ const placement = original.clips.get(childId);
955
+ if (placement === void 0) throw new Error(`Anchored child Clip "${childId}" is not placed`);
956
+ this.applyClipPlacement(draft, childId, {
957
+ kind: "absolute",
958
+ targetRange: {
959
+ start: placement.start,
960
+ end: placement.start + placement.duration
961
+ }
962
+ });
963
+ }
964
+ else for (const target of targets) this.removeAnchoredDescendants(draft, target, targets);
965
+ for (const target of targets) this.removeClipGraph(draft, target);
966
+ normalizeSequentialOrders(draft);
967
+ if (includesVisual) this.resolveOneVoiceoverOverlap(draft);
968
+ });
969
+ }
970
+ updateClip(input) {
971
+ this.transact((draft) => {
972
+ const clip = requireEntityKind(draft, input.clipEntityId, "clip");
973
+ const priorOrder = clip.payload.order;
974
+ replaceEntity(draft, clip.entityId, {
975
+ ...clip,
976
+ payload: {
977
+ ...cloneJsonObject(input.payload, "Clip payload"),
978
+ ...priorOrder === void 0 || Object.hasOwn(input.payload, "order") ? {} : { order: priorOrder }
979
+ }
980
+ });
981
+ });
982
+ }
983
+ upsertVoiceoverTake(input) {
984
+ return this.transact((draft) => {
985
+ this.normalizeMainSequential(draft);
986
+ finiteNonNegative(input.anchorOffset, "Voiceover anchorOffset");
987
+ requireVolume(input.volume);
988
+ requireClipOnRole(draft, input.hostClipEntityId, "video_clip");
989
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
990
+ replaceRows(draft, imported.rows);
991
+ const voice = requireEntityKind(draft, imported.contentEntityId, "voice");
992
+ const script = this.ensureAudioScript(draft, voice.entityId, input.scriptText);
993
+ const speechTrack = this.ensureRoleTrack(draft, input.timelineEntityId, "speech");
994
+ const existing = draft.entities.find((row) => row.entityId === input.voiceoverClipEntityId);
995
+ let voiceoverClipEntityId;
996
+ if (existing === void 0) voiceoverClipEntityId = this.insertPlacedClipIntoDraft(draft, {
997
+ clipEntityId: input.voiceoverClipEntityId,
998
+ trackEntityId: speechTrack.entityId,
999
+ contentEntityId: voice.entityId,
1000
+ sourceRange: {
1001
+ start: 0,
1002
+ end: input.media.durationMs
1003
+ },
1004
+ duration: {
1005
+ mode: "fixed",
1006
+ value: input.media.durationMs
1007
+ },
1008
+ placement: {
1009
+ kind: "anchored",
1010
+ hostClipEntityId: input.hostClipEntityId,
1011
+ anchorOffset: input.anchorOffset
1012
+ },
1013
+ clipPayload: { volume: input.volume }
1014
+ });
1015
+ else {
1016
+ const topology = requireClipOnRole(draft, input.voiceoverClipEntityId, "speech");
1017
+ voiceoverClipEntityId = topology.clip.entityId;
1018
+ replaceEntity(draft, topology.clip.entityId, {
1019
+ ...topology.clip,
1020
+ payload: {
1021
+ ...topology.clip.payload,
1022
+ volume: input.volume
1023
+ }
1024
+ });
1025
+ this.relinkRelation(draft, topology.markerContentRelation, {
1026
+ endpoint0EntityId: topology.marker.entityId,
1027
+ endpoint1EntityId: voice.entityId
1028
+ });
1029
+ this.applyClipPlacement(draft, topology.clip.entityId, {
1030
+ kind: "anchored",
1031
+ hostClipEntityId: input.hostClipEntityId,
1032
+ anchorOffset: input.anchorOffset
1033
+ });
1034
+ const placed = requireClipTopology(draft, topology.clip.entityId);
1035
+ replaceEntity(draft, placed.marker.entityId, {
1036
+ ...placed.marker,
1037
+ payload: {
1038
+ ...placed.marker.payload,
1039
+ sourceRange: {
1040
+ start: 0,
1041
+ end: input.media.durationMs
1042
+ },
1043
+ duration: {
1044
+ mode: "fixed",
1045
+ value: input.media.durationMs
1046
+ }
1047
+ }
1048
+ });
1049
+ }
1050
+ this.reconcileVoiceoverCaptions(draft, voiceoverClipEntityId, voice.entityId, script.entityId, input.captions);
1051
+ this.resolveOneVoiceoverOverlap(draft);
1052
+ return {
1053
+ voiceoverClipEntityId,
1054
+ voiceEntityId: voice.entityId,
1055
+ audioScriptEntityId: script.entityId,
1056
+ captionClipEntityIds: input.captions.map((caption) => caption.captionClipEntityId)
1057
+ };
1058
+ });
1059
+ }
1060
+ moveVoiceover(input) {
1061
+ this.transact((draft) => {
1062
+ requireClipOnRole(draft, input.voiceoverClipEntityId, "speech");
1063
+ this.normalizeMainSequential(draft);
1064
+ const layout = resolveEntityTimelineLayout(draft);
1065
+ this.reanchorAtAbsolute(draft, input.voiceoverClipEntityId, finiteNonNegative(input.newStartMs, "newStartMs"), layout);
1066
+ this.resolveOneVoiceoverOverlap(draft);
1067
+ });
1068
+ }
1069
+ deleteVoiceover(input) {
1070
+ this.transact((draft) => {
1071
+ this.normalizeMainSequential(draft);
1072
+ const targets = new Set(input.voiceoverClipEntityIds);
1073
+ if (targets.size !== input.voiceoverClipEntityIds.length) throw new Error("Voiceover Clip ids must be unique");
1074
+ for (const clipId of targets) {
1075
+ requireClipOnRole(draft, clipId, "speech");
1076
+ this.removeAnchoredDescendants(draft, clipId, targets);
1077
+ }
1078
+ for (const clipId of targets) this.removeClipGraph(draft, clipId);
1079
+ this.resolveOneVoiceoverOverlap(draft);
1080
+ });
1081
+ }
1082
+ setBgm(input) {
1083
+ return this.transact((draft) => {
1084
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
1085
+ replaceRows(draft, imported.rows);
1086
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "bgm");
1087
+ const existing = clipsOnTrack(draft, track.entityId);
1088
+ for (const clip of existing) this.removeClipGraph(draft, clip.entityId);
1089
+ const clipId = this.insertPlacedClipIntoDraft(draft, {
1090
+ clipEntityId: input.bgmClipEntityId,
1091
+ trackEntityId: track.entityId,
1092
+ contentEntityId: imported.contentEntityId,
1093
+ sourceRange: {
1094
+ start: 0,
1095
+ end: input.media.durationMs
1096
+ },
1097
+ duration: { mode: "from-source" },
1098
+ placement: {
1099
+ kind: "sequential",
1100
+ order: 0
1101
+ },
1102
+ clipPayload: { volume: requireVolume(input.volume) }
1103
+ });
1104
+ const topology = requireClipTopology(draft, clipId);
1105
+ replaceEntity(draft, topology.marker.entityId, {
1106
+ ...topology.marker,
1107
+ payload: {
1108
+ ...topology.marker.payload,
1109
+ durationPolicy: "timeline"
1110
+ }
1111
+ });
1112
+ return clipId;
1113
+ });
1114
+ }
1115
+ deleteBgm(input) {
1116
+ this.transact((draft) => {
1117
+ const track = findRoleTrack(draft, input.timelineEntityId, "bgm");
1118
+ if (track === void 0) return;
1119
+ for (const clip of clipsOnTrack(draft, track.entityId)) this.removeClipGraph(draft, clip.entityId);
1120
+ });
1121
+ }
1122
+ setCaptionVisibility(input) {
1123
+ this.transact((draft) => {
1124
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "caption");
1125
+ replaceEntity(draft, track.entityId, {
1126
+ ...track,
1127
+ payload: {
1128
+ ...track.payload,
1129
+ hidden: input.hidden
1130
+ }
1131
+ });
1132
+ });
1133
+ }
1134
+ patchCaptionStyle(input) {
1135
+ this.transact((draft) => {
1136
+ const track = findRoleTrack(draft, input.timelineEntityId, "caption");
1137
+ if (track === void 0) return;
1138
+ const patch = cloneJsonObject(input.style, "Caption style");
1139
+ for (const clip of clipsOnTrack(draft, track.entityId)) {
1140
+ const topology = requireClipTopology(draft, clip.entityId);
1141
+ const caption = requireEntityKind(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId), "caption");
1142
+ const prior = caption.payload.style;
1143
+ const style = isJsonObject(prior) ? prior : {};
1144
+ replaceEntity(draft, caption.entityId, {
1145
+ ...caption,
1146
+ payload: {
1147
+ ...caption.payload,
1148
+ style: {
1149
+ ...style,
1150
+ ...patch
1151
+ }
1152
+ }
1153
+ });
1154
+ }
1155
+ });
1156
+ }
1157
+ insertPlacedClipIntoDraft(draft, input) {
1158
+ const track = requireEntityKind(draft, input.trackEntityId, "track");
1159
+ requireTrackTimeline(draft, track.entityId);
1160
+ const content = requireSequenceContent(draft, input.contentEntityId);
1161
+ const clipId = input.clipEntityId === void 0 ? this.mintEntityId(draft) : requireAvailableEntityId(draft, input.clipEntityId);
1162
+ const markerId = this.mintEntityId(draft);
1163
+ const clipPayload = { ...cloneJsonObject(input.clipPayload ?? {}, "Clip payload") };
1164
+ delete clipPayload.order;
1165
+ const markerPlacement = applyPlacementPayload(clipPayload, input.placement);
1166
+ const clip = {
1167
+ entityId: clipId,
1168
+ entityKind: "clip",
1169
+ payload: clipPayload
1170
+ };
1171
+ const marker = {
1172
+ entityId: markerId,
1173
+ entityKind: "sequence-marker",
1174
+ payload: markerPayload({
1175
+ sourceRange: input.sourceRange,
1176
+ duration: input.duration,
1177
+ ...markerPlacement
1178
+ })
1179
+ };
1180
+ draft.entities.push(clip, marker);
1181
+ draft.relations.push(this.createRelation(draft, "track-clip", track.entityId, clipId), this.createRelation(draft, "clip-marker", clipId, markerId), this.createRelation(draft, "marker-content", markerId, content.entityId));
1182
+ if (input.placement.kind === "anchored") {
1183
+ const host = requireEntityKind(draft, input.placement.hostClipEntityId, "clip");
1184
+ if (host.entityId === clipId) throw new Error("A Clip cannot anchor to itself");
1185
+ draft.relations.push(this.createRelation(draft, "clip-anchor", clipId, host.entityId));
1186
+ }
1187
+ return clipId;
1188
+ }
1189
+ ensureRoleTrack(draft, timelineEntityId, role) {
1190
+ const existing = findRoleTrack(draft, timelineEntityId, role);
1191
+ if (existing !== void 0) {
1192
+ if (existing.payload.order === void 0) {
1193
+ const replacement = {
1194
+ ...existing,
1195
+ payload: {
1196
+ ...existing.payload,
1197
+ order: roleOrder[role]
1198
+ }
1199
+ };
1200
+ replaceEntity(draft, existing.entityId, replacement);
1201
+ return replacement;
1202
+ }
1203
+ return existing;
1204
+ }
1205
+ const timeline = requireEntityKind(draft, timelineEntityId, "timeline");
1206
+ const track = {
1207
+ entityId: this.mintEntityId(draft),
1208
+ entityKind: "track",
1209
+ payload: {
1210
+ role,
1211
+ order: roleOrder[role]
1212
+ }
1213
+ };
1214
+ draft.entities.push(track);
1215
+ draft.relations.push(this.createRelation(draft, "timeline-track", timeline.entityId, track.entityId));
1216
+ return track;
1217
+ }
1218
+ ensureAudioScript(draft, voiceEntityId, text) {
1219
+ const renders = draft.relations.filter((relation) => relation.relationKind === "audio-script-render" && relation.endpoint0EntityId === voiceEntityId);
1220
+ if (renders.length > 1) throw new Error(`Voice "${voiceEntityId}" has multiple AudioScript relations`);
1221
+ if (renders[0] !== void 0) {
1222
+ const script = requireEntityKind(draft, renders[0].endpoint1EntityId, "audio-script");
1223
+ const replacement = {
1224
+ ...script,
1225
+ payload: { segments: [{
1226
+ segmentId: "segment-0",
1227
+ text
1228
+ }] }
1229
+ };
1230
+ replaceEntity(draft, script.entityId, replacement);
1231
+ return replacement;
1232
+ }
1233
+ const script = {
1234
+ entityId: this.mintEntityId(draft),
1235
+ entityKind: "audio-script",
1236
+ payload: { segments: [{
1237
+ segmentId: "segment-0",
1238
+ text
1239
+ }] }
1240
+ };
1241
+ draft.entities.push(script);
1242
+ draft.relations.push(this.createRelation(draft, "audio-script-render", voiceEntityId, script.entityId));
1243
+ return script;
1244
+ }
1245
+ reconcileVoiceoverCaptions(draft, voiceoverClipEntityId, voiceEntityId, scriptEntityId, captions) {
1246
+ const requested = new Set(captions.map((caption) => caption.captionClipEntityId));
1247
+ if (requested.size !== captions.length) throw new Error("Caption Clip ids must be unique within a voiceover take");
1248
+ const existing = directAnchorChildren(draft, voiceoverClipEntityId).filter((clipId) => clipRole(draft, clipId) === "caption");
1249
+ for (const clipId of existing) if (!requested.has(clipId)) this.removeCaptionClipGraph(draft, clipId);
1250
+ const captionTrack = captions.length === 0 ? void 0 : this.ensureRoleTrackForClip(draft, voiceoverClipEntityId, "caption");
1251
+ for (const caption of captions) {
1252
+ const startMs = finiteNonNegative(caption.startMs, "Caption startMs");
1253
+ const durationMs = finitePositive(caption.durationMs, "Caption durationMs");
1254
+ if (draft.entities.find((entity) => entity.entityId === caption.captionClipEntityId) === void 0) {
1255
+ const content = {
1256
+ entityId: this.mintEntityId(draft),
1257
+ entityKind: "caption",
1258
+ payload: {
1259
+ extent: {
1260
+ kind: "bounded",
1261
+ start: 0,
1262
+ end: durationMs
1263
+ },
1264
+ sampling: "native",
1265
+ coordinateSpace: "milliseconds",
1266
+ text: caption.text,
1267
+ ...caption.style === void 0 ? {} : { style: cloneJsonObject(caption.style, "Caption style") }
1268
+ }
1269
+ };
1270
+ const asset = {
1271
+ entityId: this.mintEntityId(draft),
1272
+ entityKind: "asset",
1273
+ payload: { inline: {
1274
+ mediaType: "text/plain",
1275
+ text: caption.text
1276
+ } }
1277
+ };
1278
+ draft.entities.push(content, asset);
1279
+ draft.relations.push(this.createRelation(draft, "physical-asset", content.entityId, asset.entityId), this.createRelation(draft, "caption-alignment", content.entityId, voiceEntityId, { alignment: {
1280
+ start: startMs,
1281
+ end: startMs + durationMs
1282
+ } }), this.createRelation(draft, "caption-provenance", content.entityId, scriptEntityId, { segmentAlignment: [] }));
1283
+ this.insertPlacedClipIntoDraft(draft, {
1284
+ clipEntityId: caption.captionClipEntityId,
1285
+ trackEntityId: captionTrack.entityId,
1286
+ contentEntityId: content.entityId,
1287
+ sourceRange: {
1288
+ start: 0,
1289
+ end: durationMs
1290
+ },
1291
+ duration: {
1292
+ mode: "fixed",
1293
+ value: durationMs
1294
+ },
1295
+ placement: {
1296
+ kind: "anchored",
1297
+ hostClipEntityId: voiceoverClipEntityId,
1298
+ anchorOffset: startMs
1299
+ }
1300
+ });
1301
+ continue;
1302
+ }
1303
+ const topology = requireClipOnRole(draft, caption.captionClipEntityId, "caption");
1304
+ const content = requireEntityKind(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId), "caption");
1305
+ const priorStyle = isJsonObject(content.payload.style) ? content.payload.style : {};
1306
+ const style = caption.style === void 0 ? priorStyle : {
1307
+ ...priorStyle,
1308
+ ...cloneJsonObject(caption.style, "Caption style")
1309
+ };
1310
+ replaceEntity(draft, content.entityId, {
1311
+ ...content,
1312
+ payload: {
1313
+ ...content.payload,
1314
+ extent: {
1315
+ kind: "bounded",
1316
+ start: 0,
1317
+ end: durationMs
1318
+ },
1319
+ text: caption.text,
1320
+ ...Object.keys(style).length === 0 ? {} : { style }
1321
+ }
1322
+ });
1323
+ const asset = requireEntityKind(draft, otherEndpoint(requireOneRelation(draft, content.entityId, "physical-asset"), content.entityId), "asset");
1324
+ if (asset.payload.inline === void 0) throw new Error(`Caption "${caption.captionClipEntityId}" is not inline`);
1325
+ replaceEntity(draft, asset.entityId, {
1326
+ ...asset,
1327
+ payload: {
1328
+ ...asset.payload,
1329
+ inline: {
1330
+ mediaType: "text/plain",
1331
+ text: caption.text
1332
+ }
1333
+ }
1334
+ });
1335
+ this.applyClipPlacement(draft, topology.clip.entityId, {
1336
+ kind: "anchored",
1337
+ hostClipEntityId: voiceoverClipEntityId,
1338
+ anchorOffset: startMs
1339
+ });
1340
+ const placed = requireClipTopology(draft, topology.clip.entityId);
1341
+ replaceEntity(draft, placed.marker.entityId, {
1342
+ ...placed.marker,
1343
+ payload: {
1344
+ ...placed.marker.payload,
1345
+ sourceRange: {
1346
+ start: 0,
1347
+ end: durationMs
1348
+ },
1349
+ duration: {
1350
+ mode: "fixed",
1351
+ value: durationMs
1352
+ }
1353
+ }
1354
+ });
1355
+ this.replaceCaptionSemanticRelations(draft, content.entityId, voiceEntityId, scriptEntityId, startMs, durationMs);
1356
+ }
1357
+ }
1358
+ ensureRoleTrackForClip(draft, clipEntityId, role) {
1359
+ const topology = requireClipTopology(draft, clipEntityId);
1360
+ const track = requireEntityKind(draft, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track");
1361
+ const timeline = requireEntityKind(draft, otherEndpoint(requireOneRelation(draft, track.entityId, "timeline-track"), track.entityId), "timeline");
1362
+ return this.ensureRoleTrack(draft, timeline.entityId, role);
1363
+ }
1364
+ replaceCaptionSemanticRelations(draft, captionEntityId, voiceEntityId, scriptEntityId, startMs, durationMs) {
1365
+ const removed = new Set(incidentRelations(draft, captionEntityId).filter((relation) => relation.relationKind === "caption-alignment" || relation.relationKind === "caption-provenance").map((relation) => relation.relationId));
1366
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1367
+ draft.relations.push(this.createRelation(draft, "caption-alignment", captionEntityId, voiceEntityId, { alignment: {
1368
+ start: startMs,
1369
+ end: startMs + durationMs
1370
+ } }), this.createRelation(draft, "caption-provenance", captionEntityId, scriptEntityId, { segmentAlignment: [] }));
1371
+ }
1372
+ removeCaptionClipGraph(draft, clipEntityId) {
1373
+ const topology = requireClipOnRole(draft, clipEntityId, "caption");
1374
+ const caption = requireEntityKind(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId), "caption");
1375
+ const asset = requireEntityKind(draft, otherEndpoint(requireOneRelation(draft, caption.entityId, "physical-asset"), caption.entityId), "asset");
1376
+ const contentRelations = incidentRelations(draft, caption.entityId);
1377
+ const allowed = new Set([
1378
+ "physical-asset",
1379
+ "caption-alignment",
1380
+ "caption-provenance",
1381
+ "marker-content"
1382
+ ]);
1383
+ const unsupported = contentRelations.filter((relation) => !allowed.has(relation.relationKind));
1384
+ if (unsupported.length > 0) throw new Error(`Cannot delete Caption with unsupported Relation ${unsupported[0].relationId}`);
1385
+ this.removeClipGraph(draft, clipEntityId);
1386
+ if (incidentRelations(draft, caption.entityId).some((relation) => relation.relationKind === "marker-content")) return;
1387
+ const removed = new Set(incidentRelations(draft, caption.entityId).map((relation) => relation.relationId));
1388
+ const assetIncidents = incidentRelations(draft, asset.entityId);
1389
+ const deleteAsset = asset.payload.inline !== void 0 && assetIncidents.every((relation) => removed.has(relation.relationId));
1390
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1391
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== caption.entityId && (!deleteAsset || entity.entityId !== asset.entityId));
1392
+ }
1393
+ replaceAnchorHost(draft, childClipEntityId, hostClipEntityId) {
1394
+ const anchors = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === childClipEntityId);
1395
+ if (anchors.length !== 1) throw new Error(`Clip "${childClipEntityId}" must have exactly one host`);
1396
+ this.relinkRelation(draft, anchors[0], {
1397
+ endpoint0EntityId: createEntityId(childClipEntityId),
1398
+ endpoint1EntityId: createEntityId(hostClipEntityId)
1399
+ });
1400
+ }
1401
+ /** Rebuild the legacy edit baseline from current visual order before a high-level cascade. */
1402
+ normalizeMainSequential(draft) {
1403
+ const videoTrack = resolveEntityTimelineLayout(draft).tracks.find((track) => track.role === "video_clip");
1404
+ if (videoTrack === void 0) return;
1405
+ videoTrack.clips.forEach((clip, order) => {
1406
+ this.applyClipPlacement(draft, clip.clipEntityId, {
1407
+ kind: "sequential",
1408
+ order
1409
+ });
1410
+ });
1411
+ }
1412
+ resolveOneVoiceoverOverlap(draft) {
1413
+ const layout = resolveEntityTimelineLayout(draft);
1414
+ const speechTrack = layout.tracks.find((track) => track.role === "speech");
1415
+ if (speechTrack === void 0) return;
1416
+ const ranges = speechTrack.clips.map((speech) => {
1417
+ const captions = directAnchorChildren(draft, speech.clipEntityId).filter((clipId) => clipRole(draft, clipId) === "caption").map((clipId) => layout.clips.get(clipId)).filter((value) => value !== void 0);
1418
+ return {
1419
+ speech,
1420
+ start: Math.min(speech.start, ...captions.map((caption) => caption.start)),
1421
+ end: Math.max(speech.start + speech.duration, ...captions.map((caption) => caption.start + caption.duration))
1422
+ };
1423
+ }).sort((left, right) => left.start - right.start || left.speech.clipEntityId.localeCompare(right.speech.clipEntityId));
1424
+ for (let index = 1; index < ranges.length; index += 1) {
1425
+ const prior = ranges[index - 1];
1426
+ const current = ranges[index];
1427
+ if (current.start >= prior.end) continue;
1428
+ const hostId = current.speech.anchorClipEntityId;
1429
+ if (hostId === void 0) continue;
1430
+ const videoTrack = layout.tracks.find((track) => track.role === "video_clip");
1431
+ const hostIndex = videoTrack?.clips.findIndex((clip) => clip.clipEntityId === hostId) ?? -1;
1432
+ if (videoTrack === void 0 || hostIndex < 0) continue;
1433
+ const overlapMs = Math.round(prior.end - current.start);
1434
+ for (const [visualIndex, clip] of videoTrack.clips.entries()) this.applyClipPlacement(draft, clip.clipEntityId, {
1435
+ kind: "absolute",
1436
+ targetRange: {
1437
+ start: clip.start + (visualIndex >= hostIndex ? overlapMs : 0),
1438
+ end: clip.start + clip.duration + (visualIndex >= hostIndex ? overlapMs : 0)
1439
+ }
1440
+ });
1441
+ return;
1442
+ }
1443
+ }
1444
+ applyClipPlacement(draft, clipEntityId, placement) {
1445
+ const topology = requireClipTopology(draft, clipEntityId);
1446
+ const clipPayload = { ...cloneJsonObject(topology.clip.payload, "Clip payload") };
1447
+ const markerPayloadValue = { ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload") };
1448
+ delete clipPayload.order;
1449
+ delete markerPayloadValue.targetRange;
1450
+ delete markerPayloadValue.anchorOffset;
1451
+ const outgoingAnchors = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === topology.clip.entityId);
1452
+ const removed = new Set(outgoingAnchors.map((relation) => relation.relationId));
1453
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1454
+ if (placement.kind === "sequential") clipPayload.order = finiteNumber(placement.order, "Clip order");
1455
+ else if (placement.kind === "absolute") markerPayloadValue.targetRange = cloneJsonValue(placement.targetRange, "targetRange");
1456
+ else {
1457
+ const host = requireEntityKind(draft, placement.hostClipEntityId, "clip");
1458
+ if (host.entityId === topology.clip.entityId) throw new Error("A Clip cannot anchor to itself");
1459
+ markerPayloadValue.anchorOffset = finiteNonNegative(placement.anchorOffset, "anchorOffset");
1460
+ draft.relations.push(this.createRelation(draft, "clip-anchor", topology.clip.entityId, host.entityId));
1461
+ }
1462
+ replaceEntity(draft, topology.clip.entityId, {
1463
+ ...topology.clip,
1464
+ payload: clipPayload
1465
+ });
1466
+ replaceEntity(draft, topology.marker.entityId, {
1467
+ ...topology.marker,
1468
+ payload: markerPayloadValue
1469
+ });
1470
+ }
1471
+ reanchorAtAbsolute(draft, childClipEntityId, absoluteStart, layout) {
1472
+ const hosts = layout.tracks.find((track) => track.role === "video_clip")?.clips.filter((clip) => clip.mode === "sequential") ?? [];
1473
+ if (hosts.length === 0) {
1474
+ const child = layout.clips.get(childClipEntityId);
1475
+ if (child === void 0) throw new Error(`Clip "${childClipEntityId}" is not placed`);
1476
+ this.applyClipPlacement(draft, childClipEntityId, {
1477
+ kind: "absolute",
1478
+ targetRange: {
1479
+ start: absoluteStart,
1480
+ end: absoluteStart + child.duration
1481
+ }
1482
+ });
1483
+ return;
1484
+ }
1485
+ const host = hosts.find((candidate) => candidate.start <= absoluteStart && absoluteStart < candidate.start + candidate.duration) ?? (absoluteStart < hosts[0].start ? hosts[0] : hosts.at(-1));
1486
+ this.applyClipPlacement(draft, childClipEntityId, {
1487
+ kind: "anchored",
1488
+ hostClipEntityId: host.clipEntityId,
1489
+ anchorOffset: Math.max(0, Math.round(absoluteStart - host.start))
1490
+ });
1491
+ }
1492
+ removeAnchoredDescendants(draft, hostClipEntityId, scheduled) {
1493
+ const children = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint1EntityId === hostClipEntityId).map((relation) => String(relation.endpoint0EntityId));
1494
+ for (const child of children) {
1495
+ if (scheduled.has(child)) continue;
1496
+ const descendants = new Set(scheduled);
1497
+ descendants.add(child);
1498
+ this.removeAnchoredDescendants(draft, child, descendants);
1499
+ if (clipRole(draft, child) === "caption") this.removeCaptionClipGraph(draft, child);
1500
+ else this.removeClipGraph(draft, child);
1501
+ }
1502
+ }
1503
+ removeClipGraph(draft, clipEntityId) {
1504
+ const topology = requireClipTopology(draft, clipEntityId);
1505
+ assertOnlyStructuralIncidents(draft, topology.clip.entityId, new Set([
1506
+ "track-clip",
1507
+ "clip-marker",
1508
+ "clip-anchor"
1509
+ ]));
1510
+ assertOnlyStructuralIncidents(draft, topology.marker.entityId, new Set(["clip-marker", "marker-content"]));
1511
+ const markerIsExclusive = incidentRelations(draft, topology.marker.entityId).filter((relation) => relation.relationKind === "clip-marker").length === 1;
1512
+ const removed = new Set(incidentRelations(draft, topology.clip.entityId).map((relation) => relation.relationId));
1513
+ if (markerIsExclusive) for (const relation of incidentRelations(draft, topology.marker.entityId)) removed.add(relation.relationId);
1514
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1515
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== topology.clip.entityId && (!markerIsExclusive || entity.entityId !== topology.marker.entityId));
1516
+ }
1517
+ transact(edit) {
1518
+ const draft = cloneRows(this.state);
1519
+ const result = edit(draft);
1520
+ validateFinalRows(draft);
1521
+ this.state = draft;
1522
+ return result;
1523
+ }
1524
+ mintEntityId(draft) {
1525
+ const entityId = createEntityId(this.idFactory("entity"));
1526
+ if (draft.entities.some((entity) => entity.entityId === entityId)) throw new Error(`Entity id "${entityId}" already exists`);
1527
+ return entityId;
1528
+ }
1529
+ createRelation(draft, relationKind, endpoint0EntityId, endpoint1EntityId, metadata = {}) {
1530
+ return {
1531
+ relationId: this.mintRelationId(draft),
1532
+ relationKind,
1533
+ endpoint0EntityId,
1534
+ endpoint1EntityId,
1535
+ metadata,
1536
+ trace: {}
1537
+ };
1538
+ }
1539
+ relinkRelation(draft, relation, endpoints) {
1540
+ const relationId = this.mintRelationId(draft);
1541
+ const index = draft.relations.findIndex((candidate) => candidate.relationId === relation.relationId);
1542
+ if (index < 0) throw new Error(`Relation id "${relation.relationId}" does not exist`);
1543
+ draft.relations[index] = {
1544
+ ...relation,
1545
+ ...endpoints,
1546
+ relationId
1547
+ };
1548
+ }
1549
+ mintRelationId(draft) {
1550
+ const relationId = createRelationId(this.idFactory("relation"));
1551
+ if (draft.relations.some((relation) => relation.relationId === relationId)) throw new Error(`Relation id "${relationId}" already exists`);
1552
+ return relationId;
1553
+ }
1554
+ };
1555
+ function validateFinalRows(rows) {
1556
+ decodeEntityRelationRows(rows, numericMarkerValidation);
1557
+ validateTimelineHierarchy(rows);
1558
+ }
1559
+ function validateTimelineHierarchy(rows) {
1560
+ for (const entity of rows.entities) {
1561
+ if (entity.entityKind === "track") requireTrackTimeline(rows, entity.entityId);
1562
+ if (entity.entityKind !== "clip") continue;
1563
+ const trackRelations = incidentRelations(rows, entity.entityId).filter((relation) => relation.relationKind === "track-clip");
1564
+ if (trackRelations.length !== 1) throw new Error(`Clip Entity "${entity.entityId}" must belong to exactly one Track`);
1565
+ requireEntityKind(rows, otherEndpoint(trackRelations[0], entity.entityId), "track");
1566
+ }
1567
+ }
1568
+ function requireTrackTimeline(rows, trackEntityId) {
1569
+ const timelineRelations = incidentRelations(rows, trackEntityId).filter((relation) => relation.relationKind === "timeline-track");
1570
+ if (timelineRelations.filter((relation) => {
1571
+ const otherId = otherEndpoint(relation, trackEntityId);
1572
+ return rows.entities.some((entity) => entity.entityId === otherId && entity.entityKind === "timeline");
1573
+ }).length !== 1 || timelineRelations.length !== 1) throw new Error(`Track Entity "${trackEntityId}" must belong to exactly one Timeline`);
1574
+ }
1575
+ function requireClipTopology(rows, clipEntityId) {
1576
+ const clip = requireEntityKind(rows, clipEntityId, "clip");
1577
+ const trackRelation = requireOneRelation(rows, clip.entityId, "track-clip");
1578
+ requireEntityKind(rows, otherEndpoint(trackRelation, clip.entityId), "track");
1579
+ const clipMarkerRelation = requireOneRelation(rows, clip.entityId, "clip-marker");
1580
+ const marker = requireEntityKind(rows, otherEndpoint(clipMarkerRelation, clip.entityId), "sequence-marker");
1581
+ const markerContentRelation = requireOneRelation(rows, marker.entityId, "marker-content");
1582
+ requireSequenceContent(rows, otherEndpoint(markerContentRelation, marker.entityId));
1583
+ return {
1584
+ clip,
1585
+ marker,
1586
+ trackRelation,
1587
+ clipMarkerRelation,
1588
+ markerContentRelation
1589
+ };
1590
+ }
1591
+ function requireOneRelation(rows, entityId, relationKind) {
1592
+ const matches = incidentRelations(rows, entityId).filter((relation) => relation.relationKind === relationKind);
1593
+ if (matches.length !== 1) throw new Error(`Entity "${entityId}" must have exactly one ${relationKind} Relation`);
1594
+ return matches[0];
1595
+ }
1596
+ function requireEntityKind(rows, entityIdValue, entityKind) {
1597
+ const entityId = createEntityId(entityIdValue);
1598
+ const entity = rows.entities.find((candidate) => candidate.entityId === entityId);
1599
+ if (entity == null) throw new Error(`Entity id "${entityId}" does not exist`);
1600
+ if (entity.entityKind !== entityKind) throw new Error(`Entity "${entityId}" must have kind "${entityKind}", got "${entity.entityKind}"`);
1601
+ return entity;
1602
+ }
1603
+ function requireSequenceContent(rows, entityIdValue) {
1604
+ if (/^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(entityIdValue)) throw new Error("Timeline content must reference an existing Sequence media Entity id; a URL is not allowed");
1605
+ const entityId = createEntityId(entityIdValue);
1606
+ const row = rows.entities.find((candidate) => candidate.entityId === entityId);
1607
+ if (row == null) throw new Error(`Timeline content must reference an existing Sequence media Entity; "${entityId}" was not found`);
1608
+ if (!hasSequence({
1609
+ ...row.payload,
1610
+ entityId: row.entityId,
1611
+ entityKind: row.entityKind
1612
+ })) throw new Error(`Timeline content must reference a Sequence media Entity, but "${entityId}" has kind "${row.entityKind}"`);
1613
+ return row;
1614
+ }
1615
+ function assertOnlyStructuralIncidents(rows, entityId, allowedKinds) {
1616
+ const unsupported = incidentRelations(rows, entityId).filter((relation) => !allowedKinds.has(relation.relationKind));
1617
+ if (unsupported.length === 0) return;
1618
+ throw new Error(`Cannot delete Clip graph: Entity "${entityId}" has unsupported incident Relation id(s): ${unsupported.map((relation) => relation.relationId).sort().join(", ")}`);
1619
+ }
1620
+ function incidentRelations(rows, entityId) {
1621
+ return rows.relations.filter((relation) => relation.endpoint0EntityId === entityId || relation.endpoint1EntityId === entityId);
1622
+ }
1623
+ function hasOutgoingClipAnchor(rows, clipEntityId) {
1624
+ return rows.relations.some((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === clipEntityId);
1625
+ }
1626
+ function outgoingAnchorChildren(rows, hostIds) {
1627
+ return rows.relations.filter((relation) => relation.relationKind === "clip-anchor" && hostIds.has(relation.endpoint1EntityId)).map((relation) => String(relation.endpoint0EntityId));
1628
+ }
1629
+ function applyPlacementPayload(clipPayload, placement) {
1630
+ if (placement.kind === "sequential") {
1631
+ clipPayload.order = finiteNumber(placement.order, "Clip order");
1632
+ return {};
1633
+ }
1634
+ if (placement.kind === "absolute") return { targetRange: cloneJsonValue(placement.targetRange, "targetRange") };
1635
+ return { anchorOffset: finiteNonNegative(placement.anchorOffset, "anchorOffset") };
1636
+ }
1637
+ function requireAvailableEntityId(rows, value) {
1638
+ const entityId = createEntityId(value);
1639
+ if (rows.entities.some((entity) => entity.entityId === entityId)) throw new Error(`Entity id "${entityId}" already exists`);
1640
+ return entityId;
1641
+ }
1642
+ function setClipOrder(rows, clipEntityId, order) {
1643
+ const clip = requireEntityKind(rows, clipEntityId, "clip");
1644
+ replaceEntity(rows, clip.entityId, {
1645
+ ...clip,
1646
+ payload: {
1647
+ ...clip.payload,
1648
+ order
1649
+ }
1650
+ });
1651
+ }
1652
+ function normalizeSequentialOrders(rows) {
1653
+ for (const track of rows.entities.filter((entity) => entity.entityKind === "track")) rows.relations.filter((relation) => relation.relationKind === "track-clip" && relation.endpoint0EntityId === track.entityId).map((relation) => rows.entities.find((entity) => entity.entityId === relation.endpoint1EntityId)).filter((entity) => entity?.entityKind === "clip" && typeof entity.payload.order === "number").sort((left, right) => Number(left.payload.order) - Number(right.payload.order)).forEach((clip, order) => setClipOrder(rows, clip.entityId, order));
1654
+ }
1655
+ function findRoleTrack(rows, timelineEntityId, role) {
1656
+ const timeline = requireEntityKind(rows, timelineEntityId, "timeline");
1657
+ const tracks = incidentRelations(rows, timeline.entityId).filter((relation) => relation.relationKind === "timeline-track").map((relation) => requireEntityKind(rows, otherEndpoint(relation, timeline.entityId), "track")).filter((track) => track.payload.role === role);
1658
+ if (tracks.length > 1) throw new Error(`Timeline "${timelineEntityId}" has multiple ${role} Tracks`);
1659
+ return tracks[0];
1660
+ }
1661
+ function clipsOnTrack(rows, trackEntityId) {
1662
+ return incidentRelations(rows, trackEntityId).filter((relation) => relation.relationKind === "track-clip").map((relation) => requireEntityKind(rows, otherEndpoint(relation, trackEntityId), "clip"));
1663
+ }
1664
+ function clipRole(rows, clipEntityId) {
1665
+ const topology = requireClipTopology(rows, clipEntityId);
1666
+ return requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role;
1667
+ }
1668
+ function directAnchorChildren(rows, hostClipEntityId) {
1669
+ return rows.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint1EntityId === hostClipEntityId).map((relation) => String(relation.endpoint0EntityId));
1670
+ }
1671
+ function requireSupportedRole(rows, topology) {
1672
+ const role = requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role;
1673
+ if (role !== "video_clip" && role !== "speech" && role !== "caption" && role !== "bgm") throw new Error(`Clip "${topology.clip.entityId}" is not on a supported timeline Track`);
1674
+ return role;
1675
+ }
1676
+ function requireClipOnRole(rows, clipEntityId, role) {
1677
+ const topology = requireClipTopology(rows, clipEntityId);
1678
+ if (requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role !== role) throw new Error(`Clip "${clipEntityId}" must belong to the ${role} Track`);
1679
+ return topology;
1680
+ }
1681
+ function finiteNumber(value, label) {
1682
+ if (!Number.isFinite(value)) throw new Error(`${label} must be finite`);
1683
+ return value;
1684
+ }
1685
+ function finitePositive(value, label) {
1686
+ const result = finiteNumber(value, label);
1687
+ if (result <= 0) throw new Error(`${label} must be positive`);
1688
+ return result;
1689
+ }
1690
+ function requireVolume(value) {
1691
+ const volume = finiteNumber(value, "Clip volume");
1692
+ if (volume < -60 || volume > 20) throw new Error("Clip volume must be between -60 and 20 dB");
1693
+ return volume;
1694
+ }
1695
+ function requirePositiveRange(value, label) {
1696
+ if (!isJsonObject(value)) throw new Error(`${label} must be an object`);
1697
+ const start = finiteNonNegative(value.start, `${label}.start`);
1698
+ const end = finitePositive(value.end, `${label}.end`);
1699
+ if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || end <= start) throw new Error(`${label} must be a positive whole-millisecond range`);
1700
+ return {
1701
+ start,
1702
+ end
1703
+ };
1704
+ }
1705
+ function markerDuration(payload, sourceRange) {
1706
+ const duration = payload.duration;
1707
+ if (!isJsonObject(duration)) throw new Error("Sequence Marker duration must be an object");
1708
+ if (duration.mode === "from-source") return sourceRange.end - sourceRange.start;
1709
+ if (duration.mode !== "fixed") throw new Error("Sequence Marker duration mode is unsupported");
1710
+ return finitePositive(duration.value, "Sequence Marker fixed duration");
1711
+ }
1712
+ function linearRate(value) {
1713
+ if (value === void 0) return 1;
1714
+ if (!isJsonObject(value) || value.kind !== "linear") throw new Error("Only linear Clip time remapping is supported");
1715
+ return finitePositive(value.rate, "linear speed rate");
1716
+ }
1717
+ function resizeAbsoluteTarget(payload, duration) {
1718
+ if (payload.targetRange === void 0) return;
1719
+ const target = requirePositiveRange(payload.targetRange, "targetRange");
1720
+ payload.targetRange = {
1721
+ start: target.start,
1722
+ end: target.start + duration
1723
+ };
1724
+ }
1725
+ function replaceRows(rows, replacement) {
1726
+ rows.entities = structuredClone([...replacement.entities]);
1727
+ rows.relations = structuredClone([...replacement.relations]);
1728
+ }
1729
+ function finiteNonNegative(value, label) {
1730
+ const result = finiteNumber(value, label);
1731
+ if (result < 0) throw new Error(`${label} must be non-negative`);
1732
+ return result;
1733
+ }
1734
+ function nextSequentialOrder(rows, trackEntityId) {
1735
+ const orders = rows.relations.filter((relation) => relation.relationKind === "track-clip" && relation.endpoint0EntityId === trackEntityId).map((relation) => rows.entities.find((entity) => entity.entityId === relation.endpoint1EntityId)?.payload.order).filter((order) => typeof order === "number" && Number.isFinite(order));
1736
+ return orders.length === 0 ? 0 : Math.max(...orders) + 1;
1737
+ }
1738
+ function otherEndpoint(relation, entityId) {
1739
+ if (relation.endpoint0EntityId === entityId) return relation.endpoint1EntityId;
1740
+ if (relation.endpoint1EntityId === entityId) return relation.endpoint0EntityId;
1741
+ throw new Error(`Relation "${relation.relationId}" is not incident to Entity "${entityId}"`);
1742
+ }
1743
+ function replaceEntity(rows, entityId, replacement) {
1744
+ const index = rows.entities.findIndex((entity) => entity.entityId === entityId);
1745
+ if (index < 0) throw new Error(`Entity id "${entityId}" does not exist`);
1746
+ rows.entities[index] = replacement;
1747
+ }
1748
+ function markerPayload(value) {
1749
+ return cloneJsonObject(value, "Sequence Marker payload");
1750
+ }
1751
+ function cloneJsonObject(value, label) {
1752
+ const cloned = structuredClone(value);
1753
+ if (!isJsonObject(cloned)) throw new Error(`${label} must contain only JSON values`);
1754
+ return cloned;
1755
+ }
1756
+ function cloneJsonValue(value, label) {
1757
+ return cloneJsonObject({ value }, label).value;
1758
+ }
1759
+ function cloneRows(rows) {
1760
+ return {
1761
+ entities: rows.entities.map((entity) => structuredClone(entity)),
1762
+ relations: rows.relations.map((relation) => structuredClone(relation))
1763
+ };
1764
+ }
1765
+ const numericMarkerValidation = { compareMarkerPoints: (_marker, _range, left, right) => {
1766
+ if (typeof left !== "number" || !Number.isFinite(left) || typeof right !== "number" || !Number.isFinite(right)) throw new Error("Sequence Marker points must use finite numeric coordinates");
1767
+ return left - right;
1768
+ } };
1769
+ //#endregion
1770
+ //#region src/entity-editor/legacy-entity-migration.ts
1771
+ /** One-time import only. Ordinary editor operations must never reverse-diff a legacy document. */
1772
+ function migrateLegacyTimelineToEntities(legacy, assetFacts, initial = {
1773
+ entities: [],
1774
+ relations: []
1775
+ }, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`) {
1776
+ assertSupportedLegacy(legacy);
1777
+ if (initial.entities.some((row) => row.entityKind === "timeline")) fail("Timeline already exists; migration is not an editing operation");
1778
+ if (legacy.meta.schema_version === "video-document/entity-projection-v1") fail("An entity projection cannot be migrated as legacy state");
1779
+ const normalizedMainRanges = overlapNormalizedMainRanges(legacy);
1780
+ let entities = structuredClone([...initial.entities]);
1781
+ let relations = structuredClone([...initial.relations]);
1782
+ const addEntity = (kind, payload, existingId) => {
1783
+ const entityId = createEntityId(existingId ?? idFactory("entity"));
1784
+ if (entities.some((row) => row.entityId === entityId)) fail(`Migration entity identity collision: ${entityId}`);
1785
+ entities.push({
1786
+ entityId,
1787
+ entityKind: kind,
1788
+ payload
1789
+ });
1790
+ return entityId;
1791
+ };
1792
+ const link = (kind, from, to, metadata = {}) => {
1793
+ const relationId = createRelationId(idFactory("relation"));
1794
+ if (relations.some((row) => row.relationId === relationId)) fail(`Migration relation identity collision: ${relationId}`);
1795
+ relations.push({
1796
+ relationId,
1797
+ relationKind: kind,
1798
+ endpoint0EntityId: createEntityId(from),
1799
+ endpoint1EntityId: createEntityId(to),
1800
+ metadata,
1801
+ trace: {}
1802
+ });
1803
+ };
1804
+ const facts = /* @__PURE__ */ new Map();
1805
+ for (const fact of assetFacts) {
1806
+ if (facts.has(fact.assetId)) fail(`Duplicate asset facts for ${fact.assetId}`);
1807
+ facts.set(fact.assetId, fact);
1808
+ }
1809
+ const contentByPartId = /* @__PURE__ */ new Map();
1810
+ const scriptBySpeechClipId = /* @__PURE__ */ new Map();
1811
+ for (const track of legacy.tracks ?? []) for (const item of track.items ?? []) {
1812
+ const part = requirePart(legacy, item.part_id);
1813
+ if (part.video_clip !== void 0) {
1814
+ const assetId = part.video_clip.origin_media_id;
1815
+ const fact = facts.get(assetId);
1816
+ if (fact === void 0) fail(`Missing factual media metadata for ${assetId}`);
1817
+ if (fact.kind !== "image" && fact.kind !== "video") fail(`Legacy video Clip ${item.part_id} requires an image or video fact`);
1818
+ const imported = importMediaAsset({
1819
+ entities,
1820
+ relations
1821
+ }, fact, idFactory);
1822
+ entities = [...imported.rows.entities];
1823
+ relations = [...imported.rows.relations];
1824
+ contentByPartId.set(item.part_id, imported.contentEntityId);
1825
+ } else if (part.speech !== void 0) {
1826
+ const speech = part.speech;
1827
+ const imported = importMediaAsset({
1828
+ entities,
1829
+ relations
1830
+ }, {
1831
+ assetId: speech.origin_speech_id,
1832
+ kind: "voice",
1833
+ durationMs: speech.media_duration_ms,
1834
+ storageKey: speech.audio_storage_key,
1835
+ voice: {
1836
+ system: "voice-library",
1837
+ key: speech.voice.id,
1838
+ ...speech.voice.name === void 0 ? {} : { name: speech.voice.name }
1839
+ }
1840
+ }, idFactory);
1841
+ entities = [...imported.rows.entities];
1842
+ relations = [...imported.rows.relations];
1843
+ contentByPartId.set(item.part_id, imported.contentEntityId);
1844
+ scriptBySpeechClipId.set(item.part_id, ensureAudioScript(imported.contentEntityId, speech.audio_script, entities, relations, addEntity, link));
1845
+ } else if (part.bgm !== void 0) {
1846
+ const bgm = part.bgm;
1847
+ const fact = facts.get(bgm.origin_media_id);
1848
+ if (fact === void 0) fail(`Missing factual media metadata for ${bgm.origin_media_id}`);
1849
+ if (fact.kind !== "audio") fail(`Legacy BGM ${item.part_id} requires an audio fact`);
1850
+ if (fact.storageKey !== bgm.audio_storage_key) fail(`Factual storage key conflicts with legacy BGM ${item.part_id}`);
1851
+ const imported = importMediaAsset({
1852
+ entities,
1853
+ relations
1854
+ }, fact, idFactory);
1855
+ entities = [...imported.rows.entities];
1856
+ relations = [...imported.rows.relations];
1857
+ contentByPartId.set(item.part_id, imported.contentEntityId);
1858
+ }
1859
+ }
1860
+ const timeline = addEntity("timeline", legacy.timeline?.unit_time_ms === void 0 ? {} : { unit_time_ms: legacy.timeline.unit_time_ms });
1861
+ const trackIds = /* @__PURE__ */ new Map();
1862
+ for (const [order, oldTrack] of (legacy.tracks ?? []).entries()) {
1863
+ const track = addEntity("track", {
1864
+ role: oldTrack.parts_kind,
1865
+ order,
1866
+ ...oldTrack.is_hidden === void 0 ? {} : { hidden: oldTrack.is_hidden }
1867
+ }, oldTrack.id);
1868
+ trackIds.set(oldTrack, track);
1869
+ link("timeline-track", timeline, track);
1870
+ }
1871
+ for (const oldTrack of legacy.tracks ?? []) {
1872
+ const track = trackIds.get(oldTrack);
1873
+ for (const [order, item] of (oldTrack.items ?? []).entries()) {
1874
+ const part = requirePart(legacy, item.part_id);
1875
+ const role = oldTrack.parts_kind;
1876
+ const volume = part.video_clip?.volume ?? part.speech?.volume ?? part.bgm?.volume;
1877
+ const clip = addEntity("clip", {
1878
+ ...volume === void 0 ? {} : { volume },
1879
+ ...item.time_position.mode === "sequential" && !(role === "video_clip" && normalizedMainRanges !== void 0) || role === "bgm" ? { order: role === "bgm" ? 0 : order } : {}
1880
+ }, item.part_id);
1881
+ let content = contentByPartId.get(item.part_id);
1882
+ let duration;
1883
+ let markerPayload;
1884
+ if (part.video_clip !== void 0) {
1885
+ const video = part.video_clip;
1886
+ duration = effectiveVideoClipDurationMs(video);
1887
+ const fact = facts.get(video.origin_media_id);
1888
+ const rate = speedOf(video.speed_shift);
1889
+ markerPayload = {
1890
+ sourceRange: {
1891
+ start: video.play_in,
1892
+ end: video.play_out
1893
+ },
1894
+ duration: {
1895
+ mode: "fixed",
1896
+ value: duration
1897
+ },
1898
+ ...(fact.kind === "image" || fact.kind === "video") && video.speed_shift !== void 0 ? { timeRemapping: {
1899
+ kind: "linear",
1900
+ mode: video.speed_shift?.mode ?? "constant",
1901
+ rate
1902
+ } } : {}
1903
+ };
1904
+ } else if (part.speech !== void 0) {
1905
+ duration = part.speech.media_duration_ms;
1906
+ markerPayload = {
1907
+ sourceRange: {
1908
+ start: 0,
1909
+ end: duration
1910
+ },
1911
+ duration: {
1912
+ mode: "fixed",
1913
+ value: duration
1914
+ }
1915
+ };
1916
+ } else if (part.caption !== void 0) {
1917
+ const caption = part.caption;
1918
+ duration = caption.initial_duration_ms;
1919
+ content = addEntity("caption", {
1920
+ extent: {
1921
+ kind: "bounded",
1922
+ start: 0,
1923
+ end: duration
1924
+ },
1925
+ sampling: "native",
1926
+ coordinateSpace: "milliseconds",
1927
+ text: caption.text,
1928
+ ...caption.style === void 0 ? {} : { style: toEntityCaptionStyle(caption.style) }
1929
+ });
1930
+ const asset = addEntity("asset", { inline: {
1931
+ mediaType: "text/plain",
1932
+ text: caption.text
1933
+ } });
1934
+ link("physical-asset", content, asset);
1935
+ const voice = contentByPartId.get(caption.speech_part_id);
1936
+ const script = scriptBySpeechClipId.get(caption.speech_part_id);
1937
+ link("caption-alignment", content, voice, { alignment: {
1938
+ start: caption.start_ms,
1939
+ end: caption.start_ms + duration
1940
+ } });
1941
+ link("caption-provenance", content, script, { segmentAlignment: [] });
1942
+ markerPayload = {
1943
+ sourceRange: {
1944
+ start: 0,
1945
+ end: duration
1946
+ },
1947
+ duration: {
1948
+ mode: "fixed",
1949
+ value: duration
1950
+ }
1951
+ };
1952
+ } else {
1953
+ const bgm = part.bgm;
1954
+ const fact = facts.get(bgm.origin_media_id);
1955
+ if (fact.kind !== "audio") fail(`Legacy BGM ${item.part_id} requires an audio fact`);
1956
+ duration = fact.durationMs;
1957
+ markerPayload = {
1958
+ sourceRange: {
1959
+ start: 0,
1960
+ end: duration
1961
+ },
1962
+ duration: { mode: "from-source" },
1963
+ durationPolicy: "timeline"
1964
+ };
1965
+ }
1966
+ if (content === void 0) fail(`Migration could not resolve content for ${item.part_id}`);
1967
+ const normalizedMainRange = role === "video_clip" ? normalizedMainRanges?.get(item.part_id) : void 0;
1968
+ if (normalizedMainRange !== void 0) markerPayload = {
1969
+ ...markerPayload,
1970
+ targetRange: normalizedMainRange
1971
+ };
1972
+ else if (item.time_position.mode === "absolute" && role !== "bgm") markerPayload = {
1973
+ ...markerPayload,
1974
+ targetRange: {
1975
+ start: item.time_position.offsetMs,
1976
+ end: item.time_position.offsetMs + duration
1977
+ }
1978
+ };
1979
+ else if (item.time_position.mode === "anchored") markerPayload = {
1980
+ ...markerPayload,
1981
+ anchorOffset: item.time_position.offsetMs
1982
+ };
1983
+ const marker = addEntity("sequence-marker", markerPayload);
1984
+ link("track-clip", track, clip);
1985
+ link("clip-marker", clip, marker);
1986
+ link("marker-content", marker, content);
1987
+ if (item.time_position.mode === "anchored") link("clip-anchor", clip, item.time_position.anchorPartId);
1988
+ }
1989
+ }
1990
+ const rows = {
1991
+ entities,
1992
+ relations
1993
+ };
1994
+ assertEntityProjectionPreservesLegacy(legacy, projectEntityTimeline(rows, legacy.meta), rows);
1995
+ return rows;
1996
+ }
1997
+ /** Server gate: a cutover may change representation, never the existing render facts. */
1998
+ function assertEntityProjectionPreservesLegacy(legacy, projection, rows) {
1999
+ assertSupportedLegacy(legacy);
2000
+ if (projection.meta.schema_version !== "video-document/entity-projection-v1") fail("Expected an entity projection for migration");
2001
+ const normalizedMainRanges = overlapNormalizedMainRanges(legacy);
2002
+ const compatibilityFacts = (document) => {
2003
+ const { version: _version, ...view } = fromVideoDocument(document);
2004
+ for (const part of Object.values(view.part_library ?? {})) {
2005
+ const speech = part.speech;
2006
+ if (speech?.phoneme_script === null) delete speech.phoneme_script;
2007
+ }
2008
+ const { schema_version: _schemaVersion, version: _metaVersion, ...meta } = document.meta;
2009
+ return {
2010
+ meta,
2011
+ view
2012
+ };
2013
+ };
2014
+ if (canonical(compatibilityFacts(legacy)) !== canonical(compatibilityFacts(projection))) fail("Entity migration must preserve all existing timeline facts; edit after cutover");
2015
+ const trackOrders = [];
2016
+ for (const oldTrack of legacy.tracks ?? []) {
2017
+ const candidates = rows.entities.filter((row) => row.entityKind === "track" && (oldTrack.id === void 0 ? row.payload.role === oldTrack.parts_kind : row.entityId === oldTrack.id));
2018
+ if (candidates.length !== 1) fail("Migration must preserve every Track identity");
2019
+ const track = candidates[0];
2020
+ if (track.payload.role !== oldTrack.parts_kind || (track.payload.hidden ?? false) !== (oldTrack.is_hidden ?? false)) fail("Migration must preserve Track role and visibility");
2021
+ if (typeof track.payload.order !== "number" || !Number.isFinite(track.payload.order)) fail("Migration must preserve Track order");
2022
+ trackOrders.push(track.payload.order);
2023
+ const sequentialOrders = [];
2024
+ for (const item of oldTrack.items ?? []) {
2025
+ const clip = rows.entities.find((row) => row.entityId === item.part_id && row.entityKind === "clip");
2026
+ const binding = rows.relations.filter((row) => row.relationKind === "clip-marker" && row.endpoint0EntityId === item.part_id && rows.entities.some((entity) => entity.entityId === row.endpoint1EntityId && entity.entityKind === "sequence-marker"));
2027
+ const marker = rows.entities.find((row) => row.entityId === binding[0]?.endpoint1EntityId && row.entityKind === "sequence-marker");
2028
+ if (clip === void 0 || binding.length !== 1 || marker === void 0) fail("Migration must preserve Clip/Marker placement identity");
2029
+ const anchors = rows.relations.filter((row) => row.relationKind === "clip-anchor" && row.endpoint0EntityId === clip.entityId);
2030
+ const normalizedMainRange = oldTrack.parts_kind === "video_clip" ? normalizedMainRanges?.get(item.part_id) : void 0;
2031
+ if (normalizedMainRange !== void 0) {
2032
+ if (clip.payload.order !== void 0 || !isExactRange(marker.payload.targetRange, normalizedMainRange) || marker.payload.anchorOffset !== void 0 || anchors.length !== 0) fail("Migration must preserve the legacy overlap-normalized main Track");
2033
+ } else if (oldTrack.parts_kind === "bgm") {
2034
+ if (clip.payload.order !== 0 || marker.payload.targetRange !== void 0 || marker.payload.anchorOffset !== void 0 || marker.payload.durationPolicy !== "timeline" || anchors.length !== 0) fail("Migration must preserve timeline-filling BGM semantics");
2035
+ } else if (item.time_position.mode === "sequential") {
2036
+ if (marker.payload.targetRange !== void 0 || marker.payload.anchorOffset !== void 0 || typeof clip.payload.order !== "number" || !Number.isFinite(clip.payload.order) || anchors.length !== 0) fail("Migration must preserve sequential placement semantics");
2037
+ sequentialOrders.push(clip.payload.order);
2038
+ } else if (item.time_position.mode === "absolute") {
2039
+ const target = marker.payload.targetRange;
2040
+ if (clip.payload.order !== void 0 || !isRangeStartingAt(target, item.time_position.offsetMs) || marker.payload.anchorOffset !== void 0 || anchors.length !== 0) fail("Migration must preserve absolute placement semantics");
2041
+ } else if (clip.payload.order !== void 0 || marker.payload.targetRange !== void 0 || marker.payload.anchorOffset !== item.time_position.offsetMs || anchors.length !== 1 || anchors[0].endpoint1EntityId !== item.time_position.anchorPartId) fail("Migration must preserve anchored placement semantics");
2042
+ }
2043
+ if (sequentialOrders.some((order, index) => index > 0 && order <= sequentialOrders[index - 1])) fail("Migration must preserve sequential Clip order");
2044
+ }
2045
+ if (trackOrders.some((order, index) => index > 0 && order <= trackOrders[index - 1])) fail("Migration must preserve Track stacking order");
2046
+ }
2047
+ function assertSupportedLegacy(document) {
2048
+ assertValidVideoDocument(document);
2049
+ const tracks = document.tracks ?? [];
2050
+ assertKnownFields(document.timeline ?? {}, ["unit_time_ms"], "Timeline");
2051
+ if (document.timeline?.unit_time_ms !== void 0 && (typeof document.timeline.unit_time_ms !== "number" || !Number.isFinite(document.timeline.unit_time_ms) || document.timeline.unit_time_ms <= 0)) fail("Migration requires a positive finite unit_time_ms");
2052
+ const seenRoles = /* @__PURE__ */ new Set();
2053
+ const placed = /* @__PURE__ */ new Map();
2054
+ for (const track of tracks) {
2055
+ const role = track.parts_kind;
2056
+ if (role === void 0 || ![
2057
+ "video_clip",
2058
+ "speech",
2059
+ "caption",
2060
+ "bgm"
2061
+ ].includes(role)) fail("Every legacy Track requires a supported role");
2062
+ if (seenRoles.has(role)) fail(`The compatibility editor supports only one ${role} Track`);
2063
+ seenRoles.add(role);
2064
+ if (track.id !== void 0 && (track.id.trim() === "" || track.id.trim() !== track.id)) fail("Migration requires trimmed Track identities");
2065
+ for (const item of track.items ?? []) {
2066
+ if (item.part_id.trim() === "" || item.part_id.trim() !== item.part_id) fail("Migration requires trimmed Clip identities");
2067
+ if (placed.has(item.part_id)) fail(`Legacy part ${item.part_id} is placed more than once`);
2068
+ placed.set(item.part_id, {
2069
+ track,
2070
+ item
2071
+ });
2072
+ assertKnownFields(item.time_position, item.time_position.mode === "sequential" ? ["mode"] : [
2073
+ "mode",
2074
+ "anchorPartId",
2075
+ "offsetMs"
2076
+ ], "placement");
2077
+ if (item.time_position.mode === "absolute") requireMilliseconds(item.time_position.offsetMs, "absolute offset");
2078
+ if (item.time_position.mode === "anchored") {
2079
+ if (item.time_position.anchorPartId.trim() === "" || item.time_position.anchorPartId.trim() !== item.time_position.anchorPartId) fail("Migration requires a trimmed anchor identity");
2080
+ requireMilliseconds(item.time_position.offsetMs, "anchor offset");
2081
+ }
2082
+ if (item.fallback_abs_ms !== void 0) requireMilliseconds(item.fallback_abs_ms, "anchor fallback");
2083
+ assertKnownFields(item, [
2084
+ "part_id",
2085
+ "time_position",
2086
+ "fallback_abs_ms"
2087
+ ], "TrackItem");
2088
+ }
2089
+ assertKnownFields(track, [
2090
+ "id",
2091
+ "parts_kind",
2092
+ "is_hidden",
2093
+ "items"
2094
+ ], "Track");
2095
+ }
2096
+ for (const [id, part] of Object.entries(document.part_library ?? {})) {
2097
+ const placement = placed.get(id);
2098
+ if (placement === void 0) fail(`Cannot discard unplaced legacy part ${id}`);
2099
+ assertPartSupported(id, part, placement.track.parts_kind);
2100
+ }
2101
+ for (const id of placed.keys()) if (document.part_library?.[id] === void 0) fail(`Placed Clip ${id} has no part payload`);
2102
+ assertLegacyRelations(document, placed);
2103
+ }
2104
+ function assertPartSupported(id, part, role) {
2105
+ assertKnownFields(part, [
2106
+ "video_clip",
2107
+ "speech",
2108
+ "caption",
2109
+ "bgm"
2110
+ ], "Part");
2111
+ const present = [
2112
+ "video_clip",
2113
+ "speech",
2114
+ "caption",
2115
+ "bgm"
2116
+ ].filter((kind) => part[kind] !== void 0);
2117
+ if (present.length !== 1 || present[0] !== role) fail(`Legacy part ${id} must match its ${role} Track`);
2118
+ if (part.video_clip !== void 0) {
2119
+ const clip = part.video_clip;
2120
+ if (clip.id !== id || clip.kind !== "video_clip" || !trimmed(clip.origin_media_id) || clip.play_in === void 0 || clip.play_out === void 0) fail("Migration requires complete Clip identity, media and trim facts");
2121
+ requireMilliseconds(clip.play_in, "Clip play_in");
2122
+ requireMilliseconds(clip.play_out, "Clip play_out");
2123
+ if (clip.play_out <= clip.play_in) fail("Migration requires a non-empty Clip trim window");
2124
+ requireFinite(clip.volume, "Clip volume");
2125
+ const speed = clip.speed_shift;
2126
+ if (speed !== void 0 && (speed.category !== "linear" || speed.config?.linear === void 0 || speed.mode !== void 0 && typeof speed.mode !== "string")) fail("Nonlinear speed requires an entity remapping adapter");
2127
+ if (speed !== void 0) {
2128
+ const rate = speed.config?.linear?.speed;
2129
+ if (typeof rate !== "number" || !Number.isFinite(rate) || rate <= 0) fail("Migration requires a positive finite speed");
2130
+ assertKnownFields(speed, [
2131
+ "category",
2132
+ "mode",
2133
+ "config"
2134
+ ], "speed_shift");
2135
+ assertKnownFields(speed.config, ["linear"], "speed config");
2136
+ assertKnownFields(speed.config.linear, ["speed"], "linear speed");
2137
+ }
2138
+ assertKnownFields(clip, [
2139
+ "id",
2140
+ "kind",
2141
+ "origin_media_id",
2142
+ "play_in",
2143
+ "play_out",
2144
+ "volume",
2145
+ "speed_shift"
2146
+ ], "Clip");
2147
+ } else if (part.speech !== void 0) {
2148
+ const speech = part.speech;
2149
+ const phonemeScript = speech.phoneme_script;
2150
+ if (speech.id !== id || speech.kind !== "speech" || !trimmed(speech.origin_speech_id) || !trimmed(speech.audio_storage_key) || typeof speech.audio_script !== "string") fail("Migration requires complete Speech identity and physical take facts");
2151
+ requirePositiveMilliseconds(speech.media_duration_ms, "Speech duration");
2152
+ requireFinite(speech.volume, "Speech volume");
2153
+ if (!trimmed(speech.voice?.id)) fail("Migration requires an explicit voice-library identity");
2154
+ if (speech.voice?.name !== void 0 && typeof speech.voice.name !== "string") fail("Migration requires a string voice name when present");
2155
+ if (!Array.isArray(speech.caption_ids) || speech.caption_ids.some((captionId) => !trimmed(captionId))) fail("Migration requires explicit Speech caption identities");
2156
+ if (new Set(speech.caption_ids).size !== speech.caption_ids.length) fail("Migration requires unique Speech caption identities");
2157
+ if (phonemeScript !== void 0 && phonemeScript !== null) fail("Non-null phoneme_script requires a phonetic-script migration adapter");
2158
+ assertKnownFields(speech.voice, ["id", "name"], "voice");
2159
+ assertKnownFields(speech, [
2160
+ "id",
2161
+ "kind",
2162
+ "media_duration_ms",
2163
+ "audio_script",
2164
+ "volume",
2165
+ "audio_storage_key",
2166
+ "origin_speech_id",
2167
+ "voice",
2168
+ "caption_ids",
2169
+ "phoneme_script"
2170
+ ], "Speech");
2171
+ } else if (part.caption !== void 0) {
2172
+ const caption = part.caption;
2173
+ if (caption.id !== id || caption.kind !== "caption" || !trimmed(caption.speech_part_id) || typeof caption.text !== "string") fail("Migration requires complete Caption identity, Speech provenance and text");
2174
+ requireMilliseconds(caption.start_ms, "Caption source offset");
2175
+ requirePositiveMilliseconds(caption.initial_duration_ms, "Caption duration");
2176
+ if (caption.style !== void 0) assertCaptionStyle(caption.style);
2177
+ assertKnownFields(caption, [
2178
+ "id",
2179
+ "kind",
2180
+ "initial_duration_ms",
2181
+ "speech_part_id",
2182
+ "text",
2183
+ "start_ms",
2184
+ "style"
2185
+ ], "Caption");
2186
+ } else if (part.bgm !== void 0) {
2187
+ const bgm = part.bgm;
2188
+ if (bgm.id !== id || bgm.kind !== "bgm" || !trimmed(bgm.origin_media_id) || !trimmed(bgm.audio_storage_key)) fail("Migration requires complete BGM identity and physical media facts");
2189
+ requireFinite(bgm.volume, "BGM volume");
2190
+ assertKnownFields(bgm, [
2191
+ "id",
2192
+ "kind",
2193
+ "audio_storage_key",
2194
+ "volume",
2195
+ "origin_media_id"
2196
+ ], "BGM");
2197
+ }
2198
+ }
2199
+ function assertLegacyRelations(document, placed) {
2200
+ const captionIdsBySpeech = /* @__PURE__ */ new Map();
2201
+ for (const track of document.tracks ?? []) {
2202
+ if (track.parts_kind === "bgm" && (track.items?.length ?? 0) > 1) fail("The compatibility editor supports only one BGM Clip");
2203
+ for (const item of track.items ?? []) {
2204
+ const part = requirePart(document, item.part_id);
2205
+ const position = item.time_position;
2206
+ if (track.parts_kind === "video_clip" && position.mode === "anchored") fail("Anchored video Clips require an overlay adapter");
2207
+ if (track.parts_kind === "bgm") {
2208
+ if (position.mode !== "absolute" || position.offsetMs !== 0) fail("Legacy BGM must start at the Timeline origin");
2209
+ } else if (position.mode === "anchored") {
2210
+ const host = placed.get(position.anchorPartId);
2211
+ if (host === void 0) fail(`Legacy anchor ${position.anchorPartId} is not placed`);
2212
+ if (track.parts_kind === "speech" && host.track.parts_kind !== "video_clip") fail("Speech may only anchor to a video Clip");
2213
+ if (track.parts_kind === "caption") {
2214
+ if (host.track.parts_kind !== "speech") fail("Caption may only anchor to a Speech Clip");
2215
+ const caption = part.caption;
2216
+ if (position.anchorPartId !== caption.speech_part_id || position.offsetMs !== caption.start_ms) fail("Caption placement must agree with its Speech provenance and source offset");
2217
+ }
2218
+ } else if (track.parts_kind === "caption") fail("Caption placement requires an explicit Speech anchor");
2219
+ if (part.caption !== void 0) {
2220
+ const ids = captionIdsBySpeech.get(part.caption.speech_part_id) ?? [];
2221
+ ids.push(item.part_id);
2222
+ captionIdsBySpeech.set(part.caption.speech_part_id, ids);
2223
+ }
2224
+ }
2225
+ }
2226
+ for (const [id, placement] of placed) {
2227
+ const speech = requirePart(document, id).speech;
2228
+ if (speech === void 0) continue;
2229
+ const expected = captionIdsBySpeech.get(id) ?? [];
2230
+ if (canonical(speech.caption_ids) !== canonical(expected)) fail(`Speech ${id} caption_ids must agree with the Caption Track order`);
2231
+ if (placement.item.time_position.mode === "anchored") continue;
2232
+ if (placement.item.time_position.mode !== "absolute") fail("Speech placement must be anchored or absolute");
2233
+ }
2234
+ const intervals = (fromVideoDocument(document).main_track?.items ?? []).map((item) => {
2235
+ const clip = document.part_library?.[item.part_id]?.video_clip;
2236
+ if (clip === void 0) fail("Projection must preserve every video Clip");
2237
+ return {
2238
+ start: item.abs_time_position ?? 0,
2239
+ end: (item.abs_time_position ?? 0) + effectiveVideoClipDurationMs(clip)
2240
+ };
2241
+ }).sort((left, right) => left.start - right.start);
2242
+ if (intervals.some((interval, index) => index > 0 && interval.start < intervals[index - 1].end)) fail("Overlapping video Clips require an overlay adapter");
2243
+ }
2244
+ /**
2245
+ * Legacy reads perform one speech-overlap pass after laying the visual lane out
2246
+ * sequentially. Entity reads deliberately do not run that hidden cascade. When
2247
+ * the pass moved any visual Clip, cutover therefore records the complete solved
2248
+ * visual lane as explicit target ranges. Mixing absolute and sequential Clips
2249
+ * would create two unrelated placement chains and could reorder the lane.
2250
+ */
2251
+ function overlapNormalizedMainRanges(document) {
2252
+ const track = document.tracks?.find((candidate) => candidate.parts_kind === "video_clip");
2253
+ if (track === void 0 || (track.items?.length ?? 0) === 0) return void 0;
2254
+ const solved = fromVideoDocument(document).main_track?.items ?? [];
2255
+ const starts = new Map(solved.map((item) => [item.part_id, requireMilliseconds(item.abs_time_position, `Solved position for ${item.part_id ?? "unknown Clip"}`)]));
2256
+ let cursor = 0;
2257
+ let moved = false;
2258
+ const ranges = /* @__PURE__ */ new Map();
2259
+ for (const item of track.items ?? []) {
2260
+ const part = requirePart(document, item.part_id).video_clip;
2261
+ if (part === void 0) fail(`Projection must preserve video Clip ${item.part_id}`);
2262
+ const duration = effectiveVideoClipDurationMs(part);
2263
+ const start = starts.get(item.part_id);
2264
+ if (start === void 0) fail(`Projection must preserve video Clip ${item.part_id}`);
2265
+ if (start !== cursor) moved = true;
2266
+ const end = start + duration;
2267
+ requireMilliseconds(end, `Solved end for ${item.part_id}`);
2268
+ ranges.set(item.part_id, {
2269
+ start,
2270
+ end
2271
+ });
2272
+ cursor += duration;
2273
+ }
2274
+ return moved ? ranges : void 0;
2275
+ }
2276
+ function requirePart(document, partId) {
2277
+ const part = document.part_library?.[partId];
2278
+ if (part === void 0) fail(`Placed Clip ${partId} has no part payload`);
2279
+ return part;
2280
+ }
2281
+ function ensureAudioScript(voiceEntityId, text, entities, relations, addEntity, link) {
2282
+ const renders = relations.filter((relation) => relation.relationKind === "audio-script-render" && relation.endpoint0EntityId === voiceEntityId);
2283
+ if (renders.length > 1) fail(`Voice ${voiceEntityId} has ambiguous AudioScript provenance`);
2284
+ if (renders.length === 1) {
2285
+ const script = entities.find((entity) => entity.entityId === renders[0].endpoint1EntityId && entity.entityKind === "audio-script");
2286
+ if (script === void 0 || scriptText(script) !== text) fail(`Voice ${voiceEntityId} conflicts with the legacy Speech script`);
2287
+ return script.entityId;
2288
+ }
2289
+ const script = addEntity("audio-script", { segments: [{
2290
+ segmentId: "segment-0",
2291
+ text
2292
+ }] });
2293
+ link("audio-script-render", voiceEntityId, script);
2294
+ return script;
2295
+ }
2296
+ function scriptText(script) {
2297
+ const segments = script.payload.segments;
2298
+ if (!Array.isArray(segments)) fail("AudioScript requires segments");
2299
+ return segments.map((segment) => {
2300
+ if (segment === null || typeof segment !== "object" || Array.isArray(segment) || typeof segment.text !== "string") fail("AudioScript segment requires text");
2301
+ return segment.text;
2302
+ }).join("");
2303
+ }
2304
+ function toEntityCaptionStyle(style) {
2305
+ return {
2306
+ ...style.font_id === void 0 ? {} : { font: {
2307
+ system: "font-library",
2308
+ key: style.font_id
2309
+ } },
2310
+ ...style.font_size === void 0 ? {} : { fontSize: style.font_size },
2311
+ ...style.font_color === void 0 ? {} : { fontColor: style.font_color },
2312
+ ...style.font_weight === void 0 ? {} : { fontWeight: style.font_weight },
2313
+ ...style.entrance_animation === void 0 ? {} : { entranceAnimation: style.entrance_animation },
2314
+ ...style.entrance_animation_duration_ms === void 0 ? {} : { entranceAnimationDurationMs: style.entrance_animation_duration_ms },
2315
+ ...style.stroke_color === void 0 ? {} : { strokeColor: style.stroke_color },
2316
+ ...style.stroke_width === void 0 ? {} : { strokeWidth: style.stroke_width },
2317
+ ...style.position_x === void 0 ? {} : { positionX: style.position_x },
2318
+ ...style.position_y === void 0 ? {} : { positionY: style.position_y }
2319
+ };
2320
+ }
2321
+ function assertCaptionStyle(style) {
2322
+ if (style.font_id !== void 0 && !trimmed(style.font_id)) fail("Caption font_id must be a trimmed identity");
2323
+ for (const value of [
2324
+ style.font_size,
2325
+ style.font_weight,
2326
+ style.entrance_animation_duration_ms,
2327
+ style.stroke_width,
2328
+ style.position_x,
2329
+ style.position_y
2330
+ ]) if (value !== void 0) requireFinite(value, "Caption style number");
2331
+ for (const value of [
2332
+ style.font_color,
2333
+ style.entrance_animation,
2334
+ style.stroke_color
2335
+ ]) if (value !== void 0 && typeof value !== "string") fail("Caption style text must be a string");
2336
+ assertKnownFields(style, [
2337
+ "font_id",
2338
+ "font_size",
2339
+ "font_color",
2340
+ "font_weight",
2341
+ "entrance_animation",
2342
+ "entrance_animation_duration_ms",
2343
+ "stroke_color",
2344
+ "stroke_width",
2345
+ "position_x",
2346
+ "position_y"
2347
+ ], "Caption style");
2348
+ }
2349
+ function isRangeStartingAt(value, expectedStart) {
2350
+ return value !== null && typeof value === "object" && !Array.isArray(value) && value.start === expectedStart;
2351
+ }
2352
+ function isExactRange(value, expected) {
2353
+ return value !== null && typeof value === "object" && !Array.isArray(value) && value.start === expected.start && value.end === expected.end;
2354
+ }
2355
+ function requireFinite(value, label) {
2356
+ if (typeof value !== "number" || !Number.isFinite(value)) fail(`${label} must be finite`);
2357
+ return value;
2358
+ }
2359
+ function requireMilliseconds(value, label) {
2360
+ const result = requireFinite(value, label);
2361
+ if (!Number.isSafeInteger(result) || result < 0) fail(`${label} must be non-negative whole milliseconds`);
2362
+ return result;
2363
+ }
2364
+ function requirePositiveMilliseconds(value, label) {
2365
+ const result = requireMilliseconds(value, label);
2366
+ if (result <= 0) fail(`${label} must be positive`);
2367
+ return result;
2368
+ }
2369
+ function trimmed(value) {
2370
+ return typeof value === "string" && value.trim() !== "" && value.trim() === value;
2371
+ }
2372
+ function assertKnownFields(value, fields, label) {
2373
+ if (Object.entries(value).some(([key, field]) => field !== void 0 && !fields.includes(key))) fail(`Unknown legacy ${label} fields require an explicit migration adapter`);
2374
+ }
2375
+ function canonical(value) {
2376
+ if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
2377
+ if (value !== null && typeof value === "object") return `{${Object.entries(value).filter(([, field]) => field !== void 0).sort(([left], [right]) => left.localeCompare(right)).map(([key, field]) => `${JSON.stringify(key)}:${canonical(field)}`).join(",")}}`;
2378
+ return JSON.stringify(value) ?? "undefined";
2379
+ }
2380
+ function fail(message) {
2381
+ throw new EntityTimelineProjectionError(message);
2382
+ }
2383
+ //#endregion
220
2384
  //#region src/editor/snapshot-utils.ts
221
2385
  function isMap(value) {
222
2386
  return value instanceof Map;
@@ -2151,7 +4315,8 @@ var MengineDocSession = class {
2151
4315
  this.server = new MedeoHttpDocStorage({
2152
4316
  docId: options.docId,
2153
4317
  client: options.client,
2154
- sseReconnectDelayMs: options.sseReconnectDelayMs
4318
+ sseReconnectDelayMs: options.sseReconnectDelayMs,
4319
+ readonlyMode: options.readonlyMode
2155
4320
  });
2156
4321
  this.synchronizer = new ClientServerSynchronizer(this.local, this.server);
2157
4322
  this.manager = new DocManager(this.local, this.synchronizer);
@@ -2354,4 +4519,4 @@ function serverCovers(serverVV, target) {
2354
4519
  return covers(server, target);
2355
4520
  }
2356
4521
  //#endregion
2357
- export { DEFAULT_UNIT_TIME_MS, IMPLEMENTED_SEMANTIC_OP_KINDS, LANE_KINDS_IN_STACK_ORDER, ManualSyncDoc, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, MirrorVideoDocumentAdapter, PlainMemoryAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createEditSandbox, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, createPlainMemoryAdapter, decodeDocVersionMark, derivePositionFromAbs, effectiveVideoClipDurationMs, encodeDocVersionMark, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, laneTrackId, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, replayJournal, resolveAllSpeechOverlaps, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, speedOf, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema, writeVideoDocumentToDraft };
4522
+ export { DEFAULT_UNIT_TIME_MS, ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION, EntityGraphHttpClient, EntityTimelineEditor, EntityTimelineProjectionError, IMPLEMENTED_SEMANTIC_OP_KINDS, LANE_KINDS_IN_STACK_ORDER, ManualSyncDoc, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, MirrorVideoDocumentAdapter, PlainMemoryAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, applyEntityTimelineProjection, arrangeMainTrackSeamlessly, assertEntityProjectionPreservesLegacy, assertValidVideoDocument, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createEditSandbox, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, createPlainMemoryAdapter, decodeDocVersionMark, derivePositionFromAbs, effectiveVideoClipDurationMs, encodeDocVersionMark, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, importMediaAsset, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, laneTrackId, mainTrackRanges, migrateLegacyTimelineToEntities, partDurationMs, partUnionSchema, projectEntityTimeline, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, replayJournal, resolveAllSpeechOverlaps, resolveEntityTimelineLayout, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, speedOf, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema, writeVideoDocumentToDraft };