@mengine/medeo-client 1.3.1-alpha.5 → 1.3.1-alpha.7

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,6 +1,5 @@
1
- import { t as __exportAll } from "./chunk-D7D4PA-g.js";
2
- import { A as partDurationMs, C as reassignSpeechesToVideoClipsByTime, D as syncAggregatedClipsTimePosition, E as resolveSpeechOverlapByShiftingVideos, F as recordEntries, I as videoDocumentMirrorSchema, L as base64ToBytes, M as DEFAULT_UNIT_TIME_MS, N as effectiveVideoClipDurationMs, O as TIMELINE_SKELETON_DURATION_MS, P as partUnionToDraft, R as bytesToBase64, S as fillMainTrackTimeGaps, T as resolveAllSpeechOverlaps, _ as findLaneTrack, a as buildInitialVideoDocument, b as cascadeAfterVideoClipChanges, c as fromVideoDocument, d as assertValidVideoDocument, f as validateVideoDocument, g as ensureLaneTrack, h as LANE_KINDS_IN_STACK_ORDER, i as readVideoDocumentFromDraft, j as safeDurationMs, k as isEmptyVideoClip, l as toVideoDocument, m as videoDocumentSchema, n as createMirrorVideoDocument, o as buildSpeechHostMap, p as partUnionSchema, r as createMirrorVideoDocumentAdapter, s as derivePositionFromAbs, t as MirrorVideoDocumentAdapter, u as VideoDocumentValidationError, v as laneTrackId, w as recalculateTimelineDuration, x as arrangeMainTrackSeamlessly, y as solveVideoDocument } from "./document-CkFhI8ir.js";
3
- import { z } from "zod";
1
+ import { $ as isEmptyVideoClip, A as buildSpeechHostMap, B as ensureLaneTrack, C as variantBaseEntityIds, D as hasSequence, E as createRelationId, F as assertValidVideoDocument, G as arrangeMainTrackSeamlessly, H as laneTrackId, I as validateVideoDocument, J as recalculateTimelineDuration, K as fillMainTrackTimeGaps, L as partUnionSchema, M as fromVideoDocument, N as toVideoDocument, O as isMediaAssetVariantKind, P as VideoDocumentValidationError, Q as TIMELINE_SKELETON_DURATION_MS, R as videoDocumentSchema, S as updateEntityFields, T as createEntityId, U as solveVideoDocument, V as findLaneTrack, W as cascadeAfterVideoClipChanges, X as resolveSpeechOverlapByShiftingVideos, Y as resolveAllSpeechOverlaps, Z as syncAggregatedClipsTimePosition, _ as assembleEntityContent, a as applyEntityTimelineProjection, at as effectiveVideoClipDurationMs, b as findComposedAudioScript, c as writeVideoDocumentToDraft, ct as recordEntries, d as EntityProjectionGraph, dt as bytesToBase64, et as partDurationMs, f as resolveEntityTimelineLayout, g as assembleCaptionContent, h as ScriptCompositionError, i as MirrorVideoDocumentAdapter, it as VIDEO_DOCUMENT_SCHEMA_VERSION, j as derivePositionFromAbs, k as buildInitialVideoDocument, l as readVideoDocumentFromDraft, lt as videoDocumentMirrorSchema, m as decodeEntityRelationRows, n as createPlainMemoryAdapter, nt as DEFAULT_UNIT_TIME_MS, o as createMirrorVideoDocument, ot as speedOf, p as EntityTimelineProjectionError, q as reassignSpeechesToVideoClipsByTime, r as generatePartId, rt as ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION, s as createMirrorVideoDocumentAdapter, st as partUnionToDraft, t as PlainMemoryAdapter, tt as safeDurationMs, u as projectEntityTimeline, ut as base64ToBytes, v as assemblePhoneticScriptContent, w as isJsonObject, x as selectAudioScriptSegments, y as assembleScriptText, z as LANE_KINDS_IN_STACK_ORDER } from "./document-BLYcbv8E.js";
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";
4
3
  import { LoroDoc, VersionVector } from "loro-crdt";
5
4
  import { ClientServerSynchronizer, DocManager } from "@mengine/sync";
6
5
  import { DisposableSet, EventBus, Task } from "@mengine/utils";
@@ -13,7 +12,7 @@ import { BaseDocStorage, DummyConnection } from "@mengine/storage";
13
12
  * namespace (mirrors the cluster's `/oapi/v1` precedent for an independent
14
13
  * subsystem) rather than folding into the shared `/api/v2/*` space.
15
14
  */
16
- const MENGINE_API_PREFIX = "/api/mengine/v1";
15
+ const MENGINE_API_PREFIX$1 = "/api/mengine/v1";
17
16
  var MengineHttpRequestError = class extends Error {
18
17
  status;
19
18
  payload;
@@ -135,12 +134,12 @@ var MengineHttpClient = class {
135
134
  ...init,
136
135
  headers
137
136
  });
138
- const payload = await safeReadJson(response);
137
+ const payload = await safeReadJson$1(response);
139
138
  if (!response.ok) throw new MengineHttpRequestError(response.status, payload);
140
139
  return payload;
141
140
  }
142
141
  endpoint(path) {
143
- 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}`;
144
143
  }
145
144
  };
146
145
  /**
@@ -157,7 +156,7 @@ function asPushRejection(error) {
157
156
  if (body.kind !== "rejected" || typeof body.code !== "string") return void 0;
158
157
  return new MenginePushRejectedError(body.code, body.message ?? "", body.server_version, error.status);
159
158
  }
160
- async function safeReadJson(response) {
159
+ async function safeReadJson$1(response) {
161
160
  const text = await response.text();
162
161
  if (text.length === 0) return null;
163
162
  try {
@@ -195,588 +194,2470 @@ async function readMengineEventStream(options) {
195
194
  }
196
195
  }
197
196
  }
198
- function handleSseFrame(frame, onUpdate) {
199
- const event = parseSseFrame(frame);
200
- if (event.event !== "update" || event.data.length === 0) return;
201
- onUpdate(JSON.parse(event.data.join("\n")));
197
+ function handleSseFrame(frame, onUpdate) {
198
+ const event = parseSseFrame(frame);
199
+ if (event.event !== "update" || event.data.length === 0) return;
200
+ onUpdate(JSON.parse(event.data.join("\n")));
201
+ }
202
+ function parseSseFrame(frame) {
203
+ let event = null;
204
+ const data = [];
205
+ for (const line of frame.split(/\r?\n/)) if (line.startsWith("event:")) event = line.slice(6).trim();
206
+ else if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
207
+ return {
208
+ event,
209
+ data
210
+ };
211
+ }
212
+ function findSseFrameBoundary(buffer) {
213
+ const lf = buffer.indexOf("\n\n");
214
+ const crlf = buffer.indexOf("\r\n\r\n");
215
+ if (lf < 0) return crlf;
216
+ if (crlf < 0) return lf;
217
+ return Math.min(lf, crlf);
218
+ }
219
+ //#endregion
220
+ //#region src/entity-editor/document-audio-script.ts
221
+ /** Resolve only the document association; other AudioScripts remain independent resources. */
222
+ function readDocumentAudioScript(state) {
223
+ const id = state.audioScriptEntityId;
224
+ if (id === null) return null;
225
+ if (typeof id !== "string" || id.trim() !== id || !id) throw new EntityTimelineProjectionError("Invalid document AudioScript association");
226
+ const entity = state.rows.entities.find((row) => row.entityId === id);
227
+ if (!entity || entity.entityKind !== "audio-script") throw new EntityTimelineProjectionError(`Document AudioScript ${id} must name an AudioScript in this document`);
228
+ const content = assembleEntityContent(state.rows, createEntityId(id));
229
+ assembleScriptText(content);
230
+ return {
231
+ entityId: id,
232
+ segments: structuredClone(content.payload.segments)
233
+ };
234
+ }
235
+ //#endregion
236
+ //#region src/entity-editor/editor-resources.ts
237
+ const roleOrder$1 = {
238
+ caption: 0,
239
+ video_clip: 1,
240
+ speech: 2,
241
+ bgm: 3
242
+ };
243
+ /**
244
+ * MEngine editor policy, not a cardinality restriction on the general-purpose DSL.
245
+ *
246
+ * Media variants share one entity identity with their Asset: every
247
+ * Image/Video/Audio/Voice Entity must carry its own external locator, every
248
+ * external asset id has exactly one claimant, and no media variant may be
249
+ * bound through a legacy `physical-asset` Relation. Dual-entity media graphs
250
+ * are rejected, not migrated. Caption composes AudioScript text and has no physical text Asset.
251
+ */
252
+ function assertCanonicalEditorResources(rows) {
253
+ if (rows.entities.filter((row) => row.entityKind === "timeline").length > 1) throw new EntityTimelineProjectionError("Editor requires exactly one Timeline");
254
+ for (const role of Object.keys(roleOrder$1)) if (rows.entities.filter((row) => row.entityKind === "track" && row.payload.role === role).length > 1) throw new EntityTimelineProjectionError(`Editor has multiple ${role} Tracks`);
255
+ const identityByAssetKey = /* @__PURE__ */ new Map();
256
+ const kinds = new Map(rows.entities.map((row) => [String(row.entityId), row.entityKind]));
257
+ for (const entity of rows.entities) {
258
+ if (isMediaAssetVariantKind(entity.entityKind) && memotaLocator(entity.payload.external) === void 0) throw new EntityTimelineProjectionError(`Media Entity ${entity.entityId} must carry its own Asset external identity`);
259
+ if (entity.entityKind !== "asset" && !isMediaAssetVariantKind(entity.entityKind)) continue;
260
+ const external = entity.payload.external;
261
+ if (!isJsonObject(external) || external.system !== "memota" && external.system !== "memota-speech" || typeof external.key !== "string") continue;
262
+ const key = JSON.stringify([external.system, external.key]);
263
+ const previous = identityByAssetKey.get(key);
264
+ if (previous !== void 0 && previous !== entity.entityId) throw new EntityTimelineProjectionError(`Duplicate Asset for ${key}; reuse ${previous}`);
265
+ identityByAssetKey.set(key, entity.entityId);
266
+ }
267
+ for (const relation of rows.relations) {
268
+ if (relation.relationKind !== "physical-asset") continue;
269
+ const mediaEndpoint = [relation.endpoint0EntityId, relation.endpoint1EntityId].find((endpoint) => isMediaAssetVariantKind(kinds.get(String(endpoint)) ?? ""));
270
+ if (mediaEndpoint !== void 0) throw new EntityTimelineProjectionError(`Cannot bind media Entity ${mediaEndpoint} through physical-asset Relation ${relation.relationId}; media owns its Asset identity directly`);
271
+ }
272
+ }
273
+ /**
274
+ * Commit-transition policy shared by the editor, the HTTP commit boundary, the
275
+ * host tool, and the entity stores. It compares the authoritative before and
276
+ * after rows of one CAS commit and enforces the single-identity write rules
277
+ * on the complete after state, with no exemption for historical graphs:
278
+ *
279
+ * 1. every Image/Video/Audio/Voice Entity carries its own Asset `external`
280
+ * identity;
281
+ * 2. no `physical-asset` Relation may bind a media variant or Caption;
282
+ * 3. a self-carried media identity is immutable — it cannot be rewritten to
283
+ * another asset id.
284
+ *
285
+ * Dual-entity media graphs are rejected instead of being migrated or merged
286
+ * implicitly. Deleting media is explicit and allowed.
287
+ */
288
+ function assertMediaAssetWritePolicy(before, after) {
289
+ const beforeEntities = new Map(before.entities.map((row) => [String(row.entityId), row]));
290
+ const afterKinds = new Map(after.entities.map((row) => [String(row.entityId), row.entityKind]));
291
+ for (const row of after.entities) {
292
+ if (!isMediaAssetVariantKind(row.entityKind)) continue;
293
+ if (memotaLocator(row.payload.external) === void 0) throw new EntityTimelineProjectionError(`Media Entity ${row.entityId} must carry its own Asset external identity`);
294
+ }
295
+ for (const relation of after.relations) {
296
+ if (relation.relationKind !== "physical-asset") continue;
297
+ const mediaEndpoint = [relation.endpoint0EntityId, relation.endpoint1EntityId].find((endpoint) => isMediaAssetVariantKind(afterKinds.get(String(endpoint)) ?? ""));
298
+ if (mediaEndpoint !== void 0) throw new EntityTimelineProjectionError(`Cannot bind media Entity ${mediaEndpoint} through physical-asset Relation ${relation.relationId}; media owns its Asset identity directly`);
299
+ }
300
+ for (const row of after.entities) {
301
+ if (!isMediaAssetVariantKind(row.entityKind)) continue;
302
+ const priorExternal = memotaLocator(beforeEntities.get(String(row.entityId))?.payload.external);
303
+ if (priorExternal === void 0) continue;
304
+ const nextExternal = memotaLocator(row.payload.external);
305
+ if (nextExternal !== void 0 && (nextExternal.system !== priorExternal.system || nextExternal.key !== priorExternal.key)) throw new EntityTimelineProjectionError(`Media Entity ${row.entityId} cannot rewrite its Asset external identity; create a new Entity instead`);
306
+ }
307
+ }
308
+ function memotaLocator(value) {
309
+ if (!isJsonObject(value) || value.system !== "memota" && value.system !== "memota-speech" || typeof value.key !== "string" || value.key.trim() === "") return;
310
+ return {
311
+ system: value.system,
312
+ key: value.key
313
+ };
314
+ }
315
+ /** Add only missing editor structure; existing identities and owned settings are never replaced. */
316
+ function ensureEditorFoundation(initial, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`, timelinePayload = {}) {
317
+ const entities = structuredClone([...initial.entities]);
318
+ const relations = structuredClone([...initial.relations]);
319
+ const roots = entities.filter((row) => row.entityKind === "timeline");
320
+ if (roots.length > 1) throw new EntityTimelineProjectionError("Editor requires exactly one Timeline");
321
+ const mintEntity = () => {
322
+ const id = createEntityId(idFactory("entity"));
323
+ if (entities.some((row) => row.entityId === id)) throw new Error(`Duplicate entity id ${id}`);
324
+ return id;
325
+ };
326
+ let timeline = roots[0];
327
+ if (timeline === void 0) {
328
+ timeline = {
329
+ entityId: mintEntity(),
330
+ entityKind: "timeline",
331
+ payload: structuredClone(timelinePayload)
332
+ };
333
+ entities.push(timeline);
334
+ }
335
+ const trackEntityIds = {};
336
+ for (const role of Object.keys(roleOrder$1)) {
337
+ const matches = entities.filter((row) => row.entityKind === "track" && row.payload.role === role);
338
+ if (matches.length > 1) throw new EntityTimelineProjectionError(`Editor has multiple ${role} Tracks`);
339
+ let track = matches[0];
340
+ if (track === void 0) {
341
+ track = {
342
+ entityId: mintEntity(),
343
+ entityKind: "track",
344
+ payload: {
345
+ role,
346
+ order: roleOrder$1[role]
347
+ }
348
+ };
349
+ entities.push(track);
350
+ }
351
+ const bindings = relations.filter((row) => row.relationKind === "timeline-track" && (row.endpoint0EntityId === track.entityId || row.endpoint1EntityId === track.entityId));
352
+ if (bindings.some((row) => row.endpoint0EntityId !== timeline.entityId && row.endpoint1EntityId !== timeline.entityId)) throw new EntityTimelineProjectionError(`Track ${track.entityId} belongs to another Timeline`);
353
+ if (bindings.length === 0) {
354
+ const relationId = createRelationId(idFactory("relation"));
355
+ if (relations.some((row) => row.relationId === relationId)) throw new Error(`Duplicate relation id ${relationId}`);
356
+ relations.push({
357
+ relationId,
358
+ relationKind: "timeline-track",
359
+ endpoint0EntityId: timeline.entityId,
360
+ endpoint1EntityId: track.entityId,
361
+ metadata: {},
362
+ trace: {}
363
+ });
364
+ }
365
+ trackEntityIds[role] = track.entityId;
366
+ }
367
+ return {
368
+ rows: {
369
+ entities,
370
+ relations
371
+ },
372
+ timelineEntityId: timeline.entityId,
373
+ trackEntityIds
374
+ };
375
+ }
376
+ //#endregion
377
+ //#region src/entity-editor/entity-graph-http-client.ts
378
+ const MENGINE_API_PREFIX = "/api/mengine/v1";
379
+ const numericMarkerValidation$1 = { compareMarkerPoints: (_marker, _range, left, right) => {
380
+ if (typeof left !== "number" || !Number.isFinite(left) || typeof right !== "number" || !Number.isFinite(right)) throw new Error("entity-state requires finite numeric Marker coordinates");
381
+ return left < right ? -1 : left > right ? 1 : 0;
382
+ } };
383
+ /** Browser-safe authenticated CAS client for a document's Entity graph. */
384
+ var EntityGraphHttpClient = class {
385
+ options;
386
+ transport;
387
+ endpoint;
388
+ constructor(options) {
389
+ this.options = options;
390
+ this.transport = new MengineHttpClient(options);
391
+ const origin = options.httpOrigin.replace(/\/$/, "");
392
+ this.endpoint = `${origin}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(options.docId)}/entity-state`;
393
+ }
394
+ async fetchState() {
395
+ return this.request({ method: "GET" });
396
+ }
397
+ /**
398
+ * Replace the complete Entity graph under the baseline revision.
399
+ *
400
+ * Deletion intent is derived from baseline IDs omitted from `nextRows`, so a
401
+ * transient create-then-remove never becomes a request to delete unknown
402
+ * persisted data.
403
+ */
404
+ async commit(baseState, nextRows, options = {}) {
405
+ if (!isNonNegativeSafeInteger(baseState.revision)) throw new Error("Entity graph revision must be a non-negative safe integer");
406
+ const baseRows = cloneAndValidateRows(baseState.rows);
407
+ const next = cloneAndValidateRows(nextRows);
408
+ assertMediaAssetWritePolicy(baseRows, next);
409
+ const audioScriptEntityId = options.audioScriptEntityId === void 0 ? baseState.audioScriptEntityId : options.audioScriptEntityId;
410
+ readDocumentAudioScript({
411
+ rows: next,
412
+ audioScriptEntityId
413
+ });
414
+ const nextEntityIds = new Set(next.entities.map((row) => row.entityId));
415
+ const nextRelationIds = new Set(next.relations.map((row) => row.relationId));
416
+ const deletedEntityIds = baseRows.entities.map((row) => row.entityId).filter((entityId) => !nextEntityIds.has(entityId)).sort();
417
+ const deletedRelationIds = baseRows.relations.map((row) => row.relationId).filter((relationId) => !nextRelationIds.has(relationId)).sort();
418
+ return this.request({
419
+ method: "POST",
420
+ body: JSON.stringify({
421
+ expected_revision: baseState.revision,
422
+ audio_script_entity_id: audioScriptEntityId,
423
+ rows: toWireRows(next),
424
+ deleted_entity_ids: deletedEntityIds,
425
+ deleted_relation_ids: deletedRelationIds,
426
+ ...options.migrationBaseVv === void 0 ? {} : { migration_base_vv: options.migrationBaseVv }
427
+ })
428
+ });
429
+ }
430
+ async request(init) {
431
+ const response = await this.transport.fetch(this.endpoint, {
432
+ ...init,
433
+ headers: this.transport.headers()
434
+ });
435
+ const payload = await safeReadJson(response);
436
+ if (!response.ok) throw new MengineHttpRequestError(response.status, payload);
437
+ return parseWireState(payload, this.options.docId);
438
+ }
439
+ };
440
+ function parseWireState(value, expectedDocId) {
441
+ if (!isRecord(value) || typeof value.doc_id !== "string" || !isNonNegativeSafeInteger(value.revision)) throw new Error("Invalid entity-state response envelope");
442
+ if (value.doc_id !== expectedDocId) throw new Error(`Entity-state response doc_id mismatch: expected "${expectedDocId}"`);
443
+ const rows = parseWireRows(value.rows);
444
+ decodeEntityRelationRows(rows, numericMarkerValidation$1);
445
+ const audioScriptEntityId = value.audio_script_entity_id;
446
+ if (audioScriptEntityId !== null && typeof audioScriptEntityId !== "string") throw new Error("Invalid document AudioScript association");
447
+ const state = {
448
+ revision: value.revision,
449
+ rows,
450
+ audioScriptEntityId
451
+ };
452
+ readDocumentAudioScript(state);
453
+ return state;
454
+ }
455
+ function parseWireRows(value) {
456
+ if (!isRecord(value) || !Array.isArray(value.entities) || !Array.isArray(value.relations)) throw new Error("Invalid entity-state response rows");
457
+ return {
458
+ entities: value.entities.map(parseWireEntity),
459
+ relations: value.relations.map(parseWireRelation)
460
+ };
461
+ }
462
+ function parseWireEntity(value) {
463
+ if (!isRecord(value) || !isJsonObject(value.payload)) throw new Error("Invalid Entity row in entity-state response");
464
+ return {
465
+ entityId: createEntityId(requiredString(value.entity_id, "entity_id")),
466
+ entityKind: requiredString(value.entity_kind, "entity_kind"),
467
+ payload: structuredClone(value.payload)
468
+ };
469
+ }
470
+ function parseWireRelation(value) {
471
+ if (!isRecord(value) || !isJsonObject(value.metadata) || !isJsonObject(value.trace)) throw new Error("Invalid Relation row in entity-state response");
472
+ return {
473
+ relationId: createRelationId(requiredString(value.relation_id, "relation_id")),
474
+ relationKind: requiredString(value.relation_kind, "relation_kind"),
475
+ endpoint0EntityId: createEntityId(requiredString(value.endpoint_0_entity_id, "endpoint_0_entity_id")),
476
+ endpoint1EntityId: createEntityId(requiredString(value.endpoint_1_entity_id, "endpoint_1_entity_id")),
477
+ metadata: structuredClone(value.metadata),
478
+ trace: structuredClone(value.trace)
479
+ };
480
+ }
481
+ function cloneAndValidateRows(rows) {
482
+ const clone = structuredClone(rows);
483
+ decodeEntityRelationRows(clone, numericMarkerValidation$1);
484
+ return clone;
485
+ }
486
+ function toWireRows(rows) {
487
+ return {
488
+ entities: rows.entities.map((row) => ({
489
+ entity_id: row.entityId,
490
+ entity_kind: row.entityKind,
491
+ payload: row.payload
492
+ })),
493
+ relations: rows.relations.map((row) => ({
494
+ relation_id: row.relationId,
495
+ relation_kind: row.relationKind,
496
+ endpoint_0_entity_id: row.endpoint0EntityId,
497
+ endpoint_1_entity_id: row.endpoint1EntityId,
498
+ metadata: row.metadata,
499
+ trace: row.trace
500
+ }))
501
+ };
502
+ }
503
+ function requiredString(value, field) {
504
+ if (typeof value !== "string" || value.length === 0 || value.trim() !== value) throw new Error(`Invalid ${field} in entity-state response`);
505
+ return value;
506
+ }
507
+ function isRecord(value) {
508
+ return value !== null && typeof value === "object" && !Array.isArray(value);
509
+ }
510
+ function isNonNegativeSafeInteger(value) {
511
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
512
+ }
513
+ async function safeReadJson(response) {
514
+ const text = await response.text();
515
+ if (text.length === 0) return null;
516
+ try {
517
+ return JSON.parse(text);
518
+ } catch {
519
+ return text;
520
+ }
521
+ }
522
+ //#endregion
523
+ //#region src/entity-editor/media-asset-import.ts
524
+ const numericCoordinates = { compareMarkerPoints: (_marker, _range, left, right) => {
525
+ if (typeof left !== "number" || typeof right !== "number" || !Number.isFinite(left) || !Number.isFinite(right)) throw new Error("Media editor requires numeric coordinates");
526
+ return left - right;
527
+ } };
528
+ /**
529
+ * Import at the editor boundary, not at asset generation time. Existing
530
+ * provenance is retained.
531
+ *
532
+ * A media variant is a logical facet of its Asset and shares one entity
533
+ * identity: the import mints exactly one entity of the media kind that carries
534
+ * the external locator itself, and deduplicates only against existing variants
535
+ * that already carry the same `(system, key)`. Legacy dual-entity graphs
536
+ * (`asset` row plus a `physical-asset` binding) are not reused and are
537
+ * rejected by the canonical resource policy instead of being migrated.
538
+ */
539
+ function importMediaAsset(initial, fact, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`) {
540
+ if (!fact.assetId || fact.assetId.trim() !== fact.assetId) throw new Error("Expected an asset id");
541
+ if (![
542
+ "image",
543
+ "video",
544
+ "audio",
545
+ "voice"
546
+ ].includes(fact.kind)) throw new Error("Unsupported media kind");
547
+ if (fact.kind !== "image" && (!Number.isFinite(fact.durationMs) || fact.durationMs <= 0)) throw new Error(`${fact.kind} ${fact.assetId} requires its factual positive duration`);
548
+ if ((fact.kind === "audio" || fact.kind === "voice") && fact.storageKey.trim() === "") throw new Error(`${fact.kind} ${fact.assetId} requires its physical storage key`);
549
+ decodeEntityRelationRows(initial, numericCoordinates);
550
+ assertCanonicalEditorResources(initial);
551
+ const entities = structuredClone([...initial.entities]);
552
+ const relations = structuredClone([...initial.relations]);
553
+ const system = fact.kind === "voice" ? "memota-speech" : "memota";
554
+ const existing = entities.filter((row) => {
555
+ const external = row.payload.external;
556
+ return isMediaAssetVariantKind(row.entityKind) && isJsonObject(external) && external.system === system && external.key === fact.assetId;
557
+ });
558
+ if (existing.length > 1) throw new Error(`Duplicate media identities for ${fact.assetId}; reuse ${existing[0].entityId}`);
559
+ if (existing[0] !== void 0) {
560
+ const variant = requireMatchingVariant(existing[0], fact);
561
+ replaceEntity$1(entities, variant.entityId, variant);
562
+ return {
563
+ rows: {
564
+ entities,
565
+ relations
566
+ },
567
+ contentEntityId: variant.entityId
568
+ };
569
+ }
570
+ const entityId = createEntityId(idFactory("entity"));
571
+ if (entities.some((row) => row.entityId === entityId)) throw new Error(`Duplicate entity id ${entityId}`);
572
+ const variant = {
573
+ entityId,
574
+ entityKind: fact.kind,
575
+ payload: {
576
+ extent: fact.kind === "image" ? {
577
+ kind: "unbounded",
578
+ start: 0
579
+ } : {
580
+ kind: "bounded",
581
+ start: 0,
582
+ end: fact.durationMs
583
+ },
584
+ sampling: fact.kind === "image" ? "constant" : "native",
585
+ coordinateSpace: "milliseconds",
586
+ external: {
587
+ system,
588
+ key: fact.assetId
589
+ },
590
+ ...fact.storageKey === void 0 ? {} : { storageKey: fact.storageKey },
591
+ ...fact.kind === "voice" && fact.voice !== void 0 ? { voice: {
592
+ system: fact.voice.system,
593
+ key: fact.voice.key,
594
+ ...fact.voice.name === void 0 ? {} : { name: fact.voice.name }
595
+ } } : {}
596
+ }
597
+ };
598
+ entities.push(variant);
599
+ const rows = {
600
+ entities,
601
+ relations
602
+ };
603
+ decodeEntityRelationRows(rows, numericCoordinates);
604
+ assertCanonicalEditorResources(rows);
605
+ return {
606
+ rows,
607
+ contentEntityId: entityId
608
+ };
609
+ }
610
+ function requireMatchingVariant(row, fact) {
611
+ const extent = row.payload.extent;
612
+ if (row.entityKind !== fact.kind || fact.kind !== "image" && (!isJsonObject(extent) || extent.start !== 0 || extent.end !== fact.durationMs) || fact.kind === "voice" && !sameVoice(row.payload.voice, fact.voice)) throw new Error(`Media facts conflict with existing identity for ${fact.assetId}`);
613
+ if (fact.storageKey !== void 0 && row.payload.storageKey !== fact.storageKey) {
614
+ if (row.payload.storageKey !== void 0) throw new Error(`Media facts conflict with existing storage key for ${fact.assetId}`);
615
+ return {
616
+ ...row,
617
+ payload: {
618
+ ...row.payload,
619
+ storageKey: fact.storageKey
620
+ }
621
+ };
622
+ }
623
+ return row;
624
+ }
625
+ function replaceEntity$1(entities, entityId, replacement) {
626
+ const index = entities.findIndex((row) => row.entityId === entityId);
627
+ if (index < 0) throw new Error(`Entity id ${entityId} does not exist`);
628
+ entities[index] = replacement;
629
+ }
630
+ function sameVoice(left, right) {
631
+ if (right === void 0) return left === void 0;
632
+ 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");
633
+ }
634
+ //#endregion
635
+ //#region src/entity-editor/entity-timeline-editor.ts
636
+ /**
637
+ * Resolve the AudioScript a PhoneticScript composes. The composition is the
638
+ * required text source: a variant without it has no content and fails here.
639
+ */
640
+ function requireComposedAudioScript(rows, phoneticScriptEntityId) {
641
+ return assemblePhoneticScriptContent(rows, phoneticScriptEntityId).audioScript.entityId;
642
+ }
643
+ function captionSelections(selections) {
644
+ return structuredClone(selections);
645
+ }
646
+ function captionBaseIds(ids) {
647
+ if (!Array.isArray(ids) || ids.length === 0) throw new Error("Caption requires non-empty baseEntityIds");
648
+ return ids.map(createEntityId);
649
+ }
650
+ function assertCaptionScript(rows, captionId, scriptId) {
651
+ if (assembleCaptionContent(rows, captionId).audioScript.entityId !== scriptId) throw new Error("Voiceover Caption must compose the same AudioScript as the PhoneticScript");
652
+ }
653
+ const roleOrder = {
654
+ caption: 0,
655
+ video_clip: 1,
656
+ speech: 2,
657
+ bgm: 3
658
+ };
659
+ const defaultIdFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`;
660
+ /**
661
+ * Graph-native timeline authoring over authoritative Medeo Entity and Relation rows.
662
+ *
663
+ * Each mutation is applied to a detached graph, validated as a complete DSL set,
664
+ * and committed only after validation succeeds.
665
+ */
666
+ var EntityTimelineEditor = class {
667
+ idFactory;
668
+ state;
669
+ constructor(initialRows, idFactory = defaultIdFactory) {
670
+ this.idFactory = idFactory;
671
+ this.state = cloneRows(initialRows);
672
+ }
673
+ rows() {
674
+ return cloneRows(this.state);
675
+ }
676
+ insertClip(input) {
677
+ const requestedOrder = input.clipPayload?.order;
678
+ const order = typeof requestedOrder === "number" ? requestedOrder : nextSequentialOrder(this.state, createEntityId(input.trackEntityId));
679
+ return this.insertPlacedClip({
680
+ trackEntityId: input.trackEntityId,
681
+ contentEntityId: input.contentEntityId,
682
+ sourceRange: input.sourceRange,
683
+ duration: input.duration,
684
+ placement: input.targetRange === void 0 ? {
685
+ kind: "sequential",
686
+ order
687
+ } : {
688
+ kind: "absolute",
689
+ targetRange: input.targetRange
690
+ },
691
+ clipPayload: input.clipPayload
692
+ });
693
+ }
694
+ insertPlacedClip(input) {
695
+ return this.transact((draft) => this.insertPlacedClipIntoDraft(draft, input));
696
+ }
697
+ updateClipMarker(input) {
698
+ this.transact((draft) => {
699
+ const { marker } = requireClipTopology(draft, input.clipEntityId);
700
+ const payload = { ...cloneJsonObject(marker.payload, "Sequence Marker payload") };
701
+ if (input.sourceRange !== void 0) payload.sourceRange = cloneJsonValue(input.sourceRange, "sourceRange");
702
+ if (input.duration !== void 0) payload.duration = cloneJsonValue(input.duration, "duration");
703
+ if (Object.hasOwn(input, "targetRange")) if (input.targetRange === void 0) {
704
+ delete payload.targetRange;
705
+ if (!hasOutgoingClipAnchor(draft, input.clipEntityId)) {
706
+ const trackId = otherEndpoint(requireOneRelation(draft, createEntityId(input.clipEntityId), "track-clip"), input.clipEntityId);
707
+ const clipPayload = { ...cloneJsonObject(requireEntityKind(draft, input.clipEntityId, "clip").payload, "Clip payload") };
708
+ clipPayload.order ??= nextSequentialOrder(draft, trackId);
709
+ replaceEntity(draft, createEntityId(input.clipEntityId), {
710
+ ...requireEntityKind(draft, input.clipEntityId, "clip"),
711
+ payload: clipPayload
712
+ });
713
+ }
714
+ } else {
715
+ payload.targetRange = cloneJsonValue(input.targetRange, "targetRange");
716
+ const clip = requireEntityKind(draft, input.clipEntityId, "clip");
717
+ const clipPayload = { ...cloneJsonObject(clip.payload, "Clip payload") };
718
+ delete clipPayload.order;
719
+ replaceEntity(draft, clip.entityId, {
720
+ ...clip,
721
+ payload: clipPayload
722
+ });
723
+ }
724
+ if (Object.hasOwn(input, "timeRemapping")) if (input.timeRemapping === void 0) delete payload.timeRemapping;
725
+ else payload.timeRemapping = cloneJsonValue(input.timeRemapping, "timeRemapping");
726
+ replaceEntity(draft, marker.entityId, {
727
+ ...marker,
728
+ payload
729
+ });
730
+ });
731
+ }
732
+ setClipPlacement(input) {
733
+ this.transact((draft) => this.applyClipPlacement(draft, input.clipEntityId, input.placement));
734
+ }
735
+ moveSequentialClips(input) {
736
+ if (input.clipEntityIds.length === 0) throw new Error("At least one Clip is required");
737
+ this.transact((draft) => {
738
+ const original = resolveEntityTimelineLayout(draft);
739
+ this.normalizeMainSequential(draft);
740
+ const before = resolveEntityTimelineLayout(draft);
741
+ const targetIds = new Set(input.clipEntityIds);
742
+ if (targetIds.size !== input.clipEntityIds.length) throw new Error("Clip ids must be unique");
743
+ const placements = input.clipEntityIds.map((id) => before.clips.get(id));
744
+ if (placements.some((placement) => placement === void 0 || placement.mode !== "sequential")) throw new Error("moveSequentialClips accepts only sequential Clips");
745
+ const trackIds = new Set(placements.map((placement) => placement.trackEntityId));
746
+ if (trackIds.size !== 1) throw new Error("Moved Clips must belong to one Track");
747
+ const trackId = [...trackIds][0];
748
+ const track = before.tracks.find((candidate) => candidate.entityId === trackId);
749
+ if (track?.role !== "video_clip") throw new Error("Only the video_clip Track supports sequential block moves");
750
+ const ordered = track.clips.filter((placement) => placement.mode === "sequential").map((placement) => placement.clipEntityId);
751
+ if (input.anchor.position !== "trackStart" && targetIds.has(input.anchor.clipEntityId)) throw new Error("Move anchor cannot be one of the moved Clips");
752
+ const moved = ordered.filter((id) => targetIds.has(id));
753
+ if (moved.length !== targetIds.size) throw new Error("Every moved Clip must exist on the target Track");
754
+ const remaining = ordered.filter((id) => !targetIds.has(id));
755
+ let insertAt = 0;
756
+ if (input.anchor.position !== "trackStart") {
757
+ const anchorIndex = remaining.indexOf(input.anchor.clipEntityId);
758
+ if (anchorIndex < 0) throw new Error("Move anchor Clip does not exist on the target Track");
759
+ insertAt = anchorIndex + (input.anchor.position === "after" ? 1 : 0);
760
+ }
761
+ [
762
+ ...remaining.slice(0, insertAt),
763
+ ...moved,
764
+ ...remaining.slice(insertAt)
765
+ ].forEach((clipId, order) => setClipOrder(draft, clipId, order));
766
+ if (input.onAnchored === "keepAbsolute") {
767
+ const affected = outgoingAnchorChildren(draft, new Set(track.clips.map((clip) => clip.clipEntityId))).filter((childId) => clipRole(draft, childId) === "speech").map((childId) => {
768
+ const placement = original.clips.get(childId);
769
+ if (placement === void 0) throw new Error(`Anchored child Clip "${childId}" is not placed`);
770
+ return {
771
+ childId,
772
+ start: placement.start
773
+ };
774
+ });
775
+ const after = resolveEntityTimelineLayout(draft);
776
+ for (const child of affected) this.reanchorAtAbsolute(draft, child.childId, child.start, after);
777
+ }
778
+ this.resolveOneVoiceoverOverlap(draft);
779
+ });
780
+ }
781
+ moveClipsToStarts(input) {
782
+ if (input.moves.length === 0) throw new Error("At least one Clip move is required");
783
+ this.transact((draft) => {
784
+ const original = resolveEntityTimelineLayout(draft);
785
+ const originalVideo = original.tracks.find((track) => track.role === "video_clip");
786
+ if (originalVideo === void 0) throw new Error("The timeline has no video_clip Track");
787
+ const preservedVoiceovers = outgoingAnchorChildren(draft, new Set(originalVideo.clips.map((clip) => clip.clipEntityId))).filter((clipId) => clipRole(draft, clipId) === "speech").map((clipId) => ({
788
+ clipId,
789
+ start: original.clips.get(clipId).start
790
+ }));
791
+ this.normalizeMainSequential(draft);
792
+ for (const move of input.moves) {
793
+ finiteNonNegative(move.newStartMs, "newStartMs");
794
+ requireClipOnRole(draft, move.clipEntityId, "video_clip");
795
+ const track = resolveEntityTimelineLayout(draft).tracks.find((candidate) => candidate.role === "video_clip");
796
+ const ordered = track.clips.map((clip) => clip.clipEntityId);
797
+ const from = ordered.indexOf(move.clipEntityId);
798
+ if (from < 0) throw new Error(`Clip "${move.clipEntityId}" is not on the video_clip Track`);
799
+ const remaining = track.clips.filter((clip) => clip.clipEntityId !== move.clipEntityId);
800
+ let cursor = 0;
801
+ let target = remaining.length;
802
+ for (const [index, clip] of remaining.entries()) {
803
+ if (move.newStartMs <= cursor + clip.duration / 2) {
804
+ target = index;
805
+ break;
806
+ }
807
+ cursor += clip.duration;
808
+ }
809
+ const next = [...ordered];
810
+ next.splice(from, 1);
811
+ next.splice(target, 0, move.clipEntityId);
812
+ next.forEach((clipId, order) => setClipOrder(draft, clipId, order));
813
+ }
814
+ const after = resolveEntityTimelineLayout(draft);
815
+ for (const voiceover of preservedVoiceovers) this.reanchorAtAbsolute(draft, voiceover.clipId, voiceover.start, after);
816
+ this.resolveOneVoiceoverOverlap(draft);
817
+ });
818
+ }
819
+ moveClip(input) {
820
+ this.transact((draft) => {
821
+ const topology = requireClipTopology(draft, input.clipEntityId);
822
+ const track = requireEntityKind(draft, input.trackEntityId, "track");
823
+ requireTrackTimeline(draft, track.entityId);
824
+ this.relinkRelation(draft, topology.trackRelation, {
825
+ endpoint0EntityId: track.entityId,
826
+ endpoint1EntityId: topology.clip.entityId
827
+ });
828
+ });
829
+ }
830
+ replaceClipContent(input) {
831
+ this.transact((draft) => {
832
+ const topology = requireClipTopology(draft, input.clipEntityId);
833
+ const content = requireSequenceContent(draft, input.contentEntityId);
834
+ const payload = {
835
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
836
+ sourceRange: cloneJsonValue(input.sourceRange, "sourceRange"),
837
+ duration: cloneJsonValue(input.duration, "duration")
838
+ };
839
+ delete payload.targetRange;
840
+ delete payload.timeRemapping;
841
+ if (input.targetRange !== void 0) payload.targetRange = cloneJsonValue(input.targetRange, "targetRange");
842
+ if (input.timeRemapping !== void 0) payload.timeRemapping = cloneJsonValue(input.timeRemapping, "timeRemapping");
843
+ replaceEntity(draft, topology.marker.entityId, {
844
+ ...topology.marker,
845
+ payload
846
+ });
847
+ if (!hasOutgoingClipAnchor(draft, topology.clip.entityId)) {
848
+ const clipPayload = { ...cloneJsonObject(topology.clip.payload, "Clip payload") };
849
+ if (input.targetRange === void 0) clipPayload.order ??= nextSequentialOrder(draft, otherEndpoint(topology.trackRelation, topology.clip.entityId));
850
+ else delete clipPayload.order;
851
+ replaceEntity(draft, topology.clip.entityId, {
852
+ ...topology.clip,
853
+ payload: clipPayload
854
+ });
855
+ }
856
+ this.relinkRelation(draft, topology.markerContentRelation, {
857
+ endpoint0EntityId: topology.marker.entityId,
858
+ endpoint1EntityId: content.entityId
859
+ });
860
+ });
861
+ }
862
+ insertMediaClip(input) {
863
+ return this.transact((draft) => {
864
+ if (input.placement.kind === "sequential") this.normalizeMainSequential(draft);
865
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
866
+ replaceRows(draft, imported.rows);
867
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
868
+ const sourceRange = requirePositiveRange(input.sourceRange, "sourceRange");
869
+ const clipId = this.insertPlacedClipIntoDraft(draft, {
870
+ trackEntityId: track.entityId,
871
+ contentEntityId: imported.contentEntityId,
872
+ sourceRange,
873
+ duration: {
874
+ mode: "fixed",
875
+ value: sourceRange.end - sourceRange.start
876
+ },
877
+ placement: input.placement,
878
+ ...input.clipEntityId === void 0 ? {} : { clipEntityId: input.clipEntityId },
879
+ ...input.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(input.volume) } }
880
+ });
881
+ if (input.placement.kind === "sequential") this.resolveOneVoiceoverOverlap(draft);
882
+ return clipId;
883
+ });
884
+ }
885
+ insertMediaClips(input) {
886
+ if (input.clips.length === 0) throw new Error("At least one media Clip is required");
887
+ return this.transact((draft) => {
888
+ this.normalizeMainSequential(draft);
889
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
890
+ const ordered = resolveEntityTimelineLayout(draft).tracks.find((candidate) => candidate.entityId === track.entityId)?.clips ?? [];
891
+ let insertAt = ordered.length;
892
+ const insertion = input.insertion;
893
+ if (insertion.kind === "before" || insertion.kind === "after") {
894
+ const anchorIndex = ordered.findIndex((clip) => clip.clipEntityId === insertion.clipEntityId);
895
+ if (anchorIndex >= 0) insertAt = anchorIndex + (insertion.kind === "after" ? 1 : 0);
896
+ } else {
897
+ const startMs = finiteNonNegative(insertion.startMs, "firstStart.startMs");
898
+ let cursor = 0;
899
+ for (const [index, clip] of ordered.entries()) {
900
+ if (startMs <= cursor + clip.duration / 2) {
901
+ insertAt = index;
902
+ break;
903
+ }
904
+ cursor += clip.duration;
905
+ }
906
+ }
907
+ const created = [];
908
+ for (const [offset, clip] of input.clips.entries()) {
909
+ const imported = importMediaAsset(draft, clip.media, this.idFactory);
910
+ replaceRows(draft, imported.rows);
911
+ const sourceRange = requirePositiveRange(clip.sourceRange, "sourceRange");
912
+ created.push(this.insertPlacedClipIntoDraft(draft, {
913
+ trackEntityId: track.entityId,
914
+ contentEntityId: imported.contentEntityId,
915
+ sourceRange,
916
+ duration: {
917
+ mode: "fixed",
918
+ value: sourceRange.end - sourceRange.start
919
+ },
920
+ placement: {
921
+ kind: "sequential",
922
+ order: ordered.length + offset
923
+ },
924
+ ...clip.clipEntityId === void 0 ? {} : { clipEntityId: clip.clipEntityId },
925
+ ...clip.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(clip.volume) } }
926
+ }));
927
+ }
928
+ const desired = ordered.map((clip) => clip.clipEntityId);
929
+ desired.splice(insertAt, 0, ...created);
930
+ desired.forEach((clipId, order) => setClipOrder(draft, clipId, order));
931
+ this.resolveOneVoiceoverOverlap(draft);
932
+ return created;
933
+ });
934
+ }
935
+ replaceMediaClip(input) {
936
+ this.transact((draft) => {
937
+ this.normalizeMainSequential(draft);
938
+ const topology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
939
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
940
+ replaceRows(draft, imported.rows);
941
+ const sourceRange = requirePositiveRange(input.sourceRange, "sourceRange");
942
+ const markerPayloadValue = {
943
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
944
+ sourceRange,
945
+ duration: {
946
+ mode: "fixed",
947
+ value: sourceRange.end - sourceRange.start
948
+ }
949
+ };
950
+ delete markerPayloadValue.timeRemapping;
951
+ resizeAbsoluteTarget(markerPayloadValue, sourceRange.end - sourceRange.start);
952
+ replaceEntity(draft, topology.marker.entityId, {
953
+ ...topology.marker,
954
+ payload: markerPayloadValue
955
+ });
956
+ this.relinkRelation(draft, topology.markerContentRelation, {
957
+ endpoint0EntityId: topology.marker.entityId,
958
+ endpoint1EntityId: createEntityId(imported.contentEntityId)
959
+ });
960
+ this.resolveOneVoiceoverOverlap(draft);
961
+ });
962
+ }
963
+ setClipVolume(input) {
964
+ this.transact((draft) => {
965
+ const topology = requireClipTopology(draft, input.clipEntityId);
966
+ requireSupportedRole(draft, topology);
967
+ replaceEntity(draft, topology.clip.entityId, {
968
+ ...topology.clip,
969
+ payload: {
970
+ ...topology.clip.payload,
971
+ volume: requireVolume(input.volume)
972
+ }
973
+ });
974
+ });
975
+ }
976
+ setClipSpeed(input) {
977
+ this.transact((draft) => {
978
+ const initialTopology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
979
+ const content = requireSequenceContent(draft, otherEndpoint(initialTopology.markerContentRelation, initialTopology.marker.entityId));
980
+ if (content.entityKind !== "video" && content.entityKind !== "image") throw new Error("Clip speed is supported only for Video or Image content");
981
+ const initialPayload = cloneJsonObject(initialTopology.marker.payload, "Sequence Marker payload");
982
+ const priorSource = requirePositiveRange(initialPayload.sourceRange, "sourceRange");
983
+ if (input.timeRemapping === null && initialPayload.timeRemapping === void 0) return;
984
+ this.normalizeMainSequential(draft);
985
+ const topology = requireClipTopology(draft, input.clipEntityId);
986
+ const payload = { ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload") };
987
+ let sourceRange = priorSource;
988
+ if (content.entityKind === "image" && initialPayload.timeRemapping === void 0 && input.timeRemapping !== null) sourceRange = {
989
+ start: 0,
990
+ end: markerDuration(initialPayload, priorSource)
991
+ };
992
+ if (input.timeRemapping === null) {
993
+ delete payload.timeRemapping;
994
+ payload.sourceRange = sourceRange;
995
+ payload.duration = {
996
+ mode: "fixed",
997
+ value: sourceRange.end - sourceRange.start
998
+ };
999
+ } else {
1000
+ const rate = finitePositive(input.timeRemapping.rate, "linear speed rate");
1001
+ const duration = Math.round((sourceRange.end - sourceRange.start) / rate);
1002
+ if (duration <= 0 || !Number.isSafeInteger(duration)) throw new Error("Linear speed produces an invalid duration");
1003
+ payload.sourceRange = sourceRange;
1004
+ payload.duration = {
1005
+ mode: "fixed",
1006
+ value: duration
1007
+ };
1008
+ payload.timeRemapping = {
1009
+ kind: "linear",
1010
+ rate,
1011
+ ...input.timeRemapping.mode === void 0 ? {} : { mode: input.timeRemapping.mode }
1012
+ };
1013
+ }
1014
+ resizeAbsoluteTarget(payload, markerDuration(payload, sourceRange));
1015
+ replaceEntity(draft, topology.marker.entityId, {
1016
+ ...topology.marker,
1017
+ payload
1018
+ });
1019
+ this.resolveOneVoiceoverOverlap(draft);
1020
+ });
1021
+ }
1022
+ trimClip(input) {
1023
+ this.transact((draft) => {
1024
+ this.normalizeMainSequential(draft);
1025
+ const topology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
1026
+ const content = requireSequenceContent(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId));
1027
+ if (content.entityKind !== "video" && content.entityKind !== "image") throw new Error("Clip trimming is supported only for Video or Image content");
1028
+ const sourceRange = requirePositiveRange(input.sourceRange, "sourceRange");
1029
+ const payload = {
1030
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
1031
+ sourceRange
1032
+ };
1033
+ const rate = linearRate(payload.timeRemapping);
1034
+ const duration = Math.round((sourceRange.end - sourceRange.start) / rate);
1035
+ if (duration <= 0 || !Number.isSafeInteger(duration)) throw new Error("Trim produces an invalid duration");
1036
+ payload.duration = {
1037
+ mode: "fixed",
1038
+ value: duration
1039
+ };
1040
+ resizeAbsoluteTarget(payload, duration);
1041
+ replaceEntity(draft, topology.marker.entityId, {
1042
+ ...topology.marker,
1043
+ payload
1044
+ });
1045
+ this.resolveOneVoiceoverOverlap(draft);
1046
+ });
1047
+ }
1048
+ replaceSequentialClips(input) {
1049
+ if (input.oldClipEntityIds.length === 0 || input.newClips.length === 0) throw new Error("Replacement requires at least one old and one new Clip");
1050
+ return this.transact((draft) => {
1051
+ this.normalizeMainSequential(draft);
1052
+ const before = resolveEntityTimelineLayout(draft);
1053
+ const oldIds = new Set(input.oldClipEntityIds);
1054
+ if (oldIds.size !== input.oldClipEntityIds.length) throw new Error("Old Clip ids must be unique");
1055
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
1056
+ const ordered = before.tracks.find((candidate) => candidate.entityId === track.entityId)?.clips.filter((clip) => clip.mode === "sequential").map((clip) => clip.clipEntityId);
1057
+ if (ordered === void 0) throw new Error("The video_clip Track is not present in the current layout");
1058
+ const indexes = input.oldClipEntityIds.map((id) => ordered.indexOf(id));
1059
+ if (indexes.some((index) => index < 0)) throw new Error("Every replaced Clip must be sequential on video_clip");
1060
+ const sortedIndexes = [...indexes].sort((left, right) => left - right);
1061
+ if (sortedIndexes.some((index, offset) => index !== sortedIndexes[0] + offset)) throw new Error("Replaced Clips must form one contiguous timeline run");
1062
+ if (input.oldClipEntityIds.some((id, index) => id !== ordered[sortedIndexes[0] + index])) throw new Error("Replaced Clip ids must be listed in timeline order");
1063
+ const insertOrder = sortedIndexes[0];
1064
+ const temporaryOrder = ordered.length;
1065
+ const created = [];
1066
+ for (const [offset, replacement] of input.newClips.entries()) {
1067
+ if (replacement.clipEntityId !== void 0 && oldIds.has(replacement.clipEntityId)) throw new Error("A replacement Clip id cannot reuse an Entity being removed");
1068
+ const imported = importMediaAsset(draft, replacement.media, this.idFactory);
1069
+ replaceRows(draft, imported.rows);
1070
+ const sourceRange = requirePositiveRange(replacement.sourceRange, "sourceRange");
1071
+ created.push(this.insertPlacedClipIntoDraft(draft, {
1072
+ trackEntityId: track.entityId,
1073
+ contentEntityId: imported.contentEntityId,
1074
+ sourceRange,
1075
+ duration: {
1076
+ mode: "fixed",
1077
+ value: sourceRange.end - sourceRange.start
1078
+ },
1079
+ placement: {
1080
+ kind: "sequential",
1081
+ order: temporaryOrder + offset
1082
+ },
1083
+ ...replacement.clipEntityId === void 0 ? {} : { clipEntityId: replacement.clipEntityId },
1084
+ ...replacement.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(replacement.volume) } }
1085
+ }));
1086
+ }
1087
+ for (const [index, oldId] of input.oldClipEntityIds.entries()) {
1088
+ const children = directAnchorChildren(draft, oldId);
1089
+ if (input.onAnchored === "remap" && created[index] !== void 0) for (const childId of children) this.replaceAnchorHost(draft, childId, created[index]);
1090
+ else {
1091
+ const scheduled = /* @__PURE__ */ new Set();
1092
+ for (const childId of children) {
1093
+ scheduled.add(childId);
1094
+ this.removeAnchoredDescendants(draft, childId, scheduled);
1095
+ this.removeClipGraph(draft, childId);
1096
+ }
1097
+ }
1098
+ }
1099
+ for (const oldId of input.oldClipEntityIds) this.removeClipGraph(draft, oldId);
1100
+ [
1101
+ ...ordered.slice(0, insertOrder),
1102
+ ...created,
1103
+ ...ordered.slice(insertOrder + input.oldClipEntityIds.length)
1104
+ ].forEach((clipId, order) => setClipOrder(draft, clipId, order));
1105
+ this.resolveOneVoiceoverOverlap(draft);
1106
+ return created;
1107
+ });
1108
+ }
1109
+ deleteClip(input) {
1110
+ this.transact((draft) => {
1111
+ const topology = requireClipTopology(draft, input.clipEntityId);
1112
+ assertOnlyStructuralIncidents(draft, topology.clip.entityId, new Set(["track-clip", "clip-marker"]));
1113
+ assertOnlyStructuralIncidents(draft, topology.marker.entityId, new Set(["clip-marker", "marker-content"]));
1114
+ const markerIsExclusive = incidentRelations(draft, topology.marker.entityId).filter((relation) => relation.relationKind === "clip-marker").length === 1;
1115
+ const removedRelationIds = new Set([topology.trackRelation.relationId, topology.clipMarkerRelation.relationId]);
1116
+ if (markerIsExclusive) for (const relation of incidentRelations(draft, topology.marker.entityId)) removedRelationIds.add(relation.relationId);
1117
+ draft.relations = draft.relations.filter((relation) => !removedRelationIds.has(relation.relationId));
1118
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== topology.clip.entityId && (!markerIsExclusive || entity.entityId !== topology.marker.entityId));
1119
+ });
1120
+ }
1121
+ deleteClipTree(input) {
1122
+ if (input.clipEntityIds.length === 0) throw new Error("At least one Clip is required");
1123
+ this.transact((draft) => {
1124
+ const targets = new Set(input.clipEntityIds);
1125
+ if (targets.size !== input.clipEntityIds.length) throw new Error("Clip ids must be unique");
1126
+ for (const target of targets) requireEntityKind(draft, target, "clip");
1127
+ const includesVisual = [...targets].some((target) => clipRole(draft, target) === "video_clip");
1128
+ const original = resolveEntityTimelineLayout(draft);
1129
+ if (includesVisual) this.normalizeMainSequential(draft);
1130
+ if (input.onAnchored === "detach") for (const childId of outgoingAnchorChildren(draft, targets)) {
1131
+ if (targets.has(childId)) continue;
1132
+ const placement = original.clips.get(childId);
1133
+ if (placement === void 0) throw new Error(`Anchored child Clip "${childId}" is not placed`);
1134
+ this.applyClipPlacement(draft, childId, {
1135
+ kind: "absolute",
1136
+ targetRange: {
1137
+ start: placement.start,
1138
+ end: placement.start + placement.duration
1139
+ }
1140
+ });
1141
+ }
1142
+ else for (const target of targets) this.removeAnchoredDescendants(draft, target, targets);
1143
+ for (const target of targets) this.removeClipGraph(draft, target);
1144
+ normalizeSequentialOrders(draft);
1145
+ if (includesVisual) this.resolveOneVoiceoverOverlap(draft);
1146
+ });
1147
+ }
1148
+ updateClip(input) {
1149
+ this.transact((draft) => {
1150
+ const clip = requireEntityKind(draft, input.clipEntityId, "clip");
1151
+ const priorOrder = clip.payload.order;
1152
+ replaceEntity(draft, clip.entityId, {
1153
+ ...clip,
1154
+ payload: {
1155
+ ...cloneJsonObject(input.payload, "Clip payload"),
1156
+ ...priorOrder === void 0 || Object.hasOwn(input.payload, "order") ? {} : { order: priorOrder }
1157
+ }
1158
+ });
1159
+ });
1160
+ }
1161
+ upsertVoiceoverTake(input) {
1162
+ return this.transact((draft) => {
1163
+ this.normalizeMainSequential(draft);
1164
+ const placement = input.placement ?? {
1165
+ kind: "anchored",
1166
+ hostClipEntityId: input.hostClipEntityId,
1167
+ anchorOffset: input.anchorOffset
1168
+ };
1169
+ if (input.placement !== void 0 && (input.hostClipEntityId !== void 0 || input.anchorOffset !== void 0)) throw new Error("Specify Voice placement or hostClipEntityId/anchorOffset, not both");
1170
+ requireVolume(input.volume);
1171
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
1172
+ replaceRows(draft, imported.rows);
1173
+ const voice = requireEntityKind(draft, imported.contentEntityId, "voice");
1174
+ const phonetic = this.requireComposedPhoneticScript(draft, input.phoneticScriptEntityId);
1175
+ this.ensurePhoneticRender(draft, voice.entityId, phonetic.entityId);
1176
+ const speechTrack = this.ensureRoleTrack(draft, input.timelineEntityId, "speech");
1177
+ const existing = draft.entities.find((row) => row.entityId === input.voiceoverClipEntityId);
1178
+ let voiceoverClipEntityId;
1179
+ if (existing === void 0) voiceoverClipEntityId = this.insertPlacedClipIntoDraft(draft, {
1180
+ clipEntityId: input.voiceoverClipEntityId,
1181
+ trackEntityId: speechTrack.entityId,
1182
+ contentEntityId: voice.entityId,
1183
+ sourceRange: {
1184
+ start: 0,
1185
+ end: input.media.durationMs
1186
+ },
1187
+ duration: {
1188
+ mode: "fixed",
1189
+ value: input.media.durationMs
1190
+ },
1191
+ placement,
1192
+ clipPayload: { volume: input.volume }
1193
+ });
1194
+ else {
1195
+ const topology = requireClipOnRole(draft, input.voiceoverClipEntityId, "speech");
1196
+ voiceoverClipEntityId = topology.clip.entityId;
1197
+ replaceEntity(draft, topology.clip.entityId, {
1198
+ ...topology.clip,
1199
+ payload: {
1200
+ ...topology.clip.payload,
1201
+ volume: input.volume
1202
+ }
1203
+ });
1204
+ this.relinkRelation(draft, topology.markerContentRelation, {
1205
+ endpoint0EntityId: topology.marker.entityId,
1206
+ endpoint1EntityId: voice.entityId
1207
+ });
1208
+ this.applyClipPlacement(draft, topology.clip.entityId, placement);
1209
+ const placed = requireClipTopology(draft, topology.clip.entityId);
1210
+ replaceEntity(draft, placed.marker.entityId, {
1211
+ ...placed.marker,
1212
+ payload: {
1213
+ ...placed.marker.payload,
1214
+ sourceRange: {
1215
+ start: 0,
1216
+ end: input.media.durationMs
1217
+ },
1218
+ duration: {
1219
+ mode: "fixed",
1220
+ value: input.media.durationMs
1221
+ }
1222
+ }
1223
+ });
1224
+ }
1225
+ this.reconcileVoiceoverCaptions(draft, voiceoverClipEntityId, voice.entityId, phonetic.entityId, input.captions);
1226
+ this.resolveOneVoiceoverOverlap(draft);
1227
+ return {
1228
+ voiceoverClipEntityId,
1229
+ voiceEntityId: voice.entityId,
1230
+ phoneticScriptEntityId: phonetic.entityId,
1231
+ audioScriptEntityId: requireComposedAudioScript(draft, phonetic.entityId),
1232
+ captionClipEntityIds: input.captions.map((caption) => caption.captionClipEntityId)
1233
+ };
1234
+ });
1235
+ }
1236
+ moveVoiceover(input) {
1237
+ this.transact((draft) => {
1238
+ requireClipOnRole(draft, input.voiceoverClipEntityId, "speech");
1239
+ this.normalizeMainSequential(draft);
1240
+ const layout = resolveEntityTimelineLayout(draft);
1241
+ this.reanchorAtAbsolute(draft, input.voiceoverClipEntityId, finiteNonNegative(input.newStartMs, "newStartMs"), layout);
1242
+ this.resolveOneVoiceoverOverlap(draft);
1243
+ });
1244
+ }
1245
+ deleteVoiceover(input) {
1246
+ this.transact((draft) => {
1247
+ this.normalizeMainSequential(draft);
1248
+ const targets = new Set(input.voiceoverClipEntityIds);
1249
+ if (targets.size !== input.voiceoverClipEntityIds.length) throw new Error("Voiceover Clip ids must be unique");
1250
+ for (const clipId of targets) {
1251
+ requireClipOnRole(draft, clipId, "speech");
1252
+ this.removeAnchoredDescendants(draft, clipId, targets);
1253
+ }
1254
+ for (const clipId of targets) this.removeClipGraph(draft, clipId);
1255
+ this.resolveOneVoiceoverOverlap(draft);
1256
+ });
1257
+ }
1258
+ setBgm(input) {
1259
+ return this.transact((draft) => {
1260
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
1261
+ replaceRows(draft, imported.rows);
1262
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "bgm");
1263
+ const existing = clipsOnTrack(draft, track.entityId);
1264
+ for (const clip of existing) this.removeClipGraph(draft, clip.entityId);
1265
+ const clipId = this.insertPlacedClipIntoDraft(draft, {
1266
+ clipEntityId: input.bgmClipEntityId,
1267
+ trackEntityId: track.entityId,
1268
+ contentEntityId: imported.contentEntityId,
1269
+ sourceRange: {
1270
+ start: 0,
1271
+ end: input.media.durationMs
1272
+ },
1273
+ duration: { mode: "from-source" },
1274
+ placement: {
1275
+ kind: "sequential",
1276
+ order: 0
1277
+ },
1278
+ clipPayload: { volume: requireVolume(input.volume) }
1279
+ });
1280
+ const topology = requireClipTopology(draft, clipId);
1281
+ replaceEntity(draft, topology.marker.entityId, {
1282
+ ...topology.marker,
1283
+ payload: {
1284
+ ...topology.marker.payload,
1285
+ durationPolicy: "timeline"
1286
+ }
1287
+ });
1288
+ return clipId;
1289
+ });
1290
+ }
1291
+ deleteBgm(input) {
1292
+ this.transact((draft) => {
1293
+ const track = findRoleTrack(draft, input.timelineEntityId, "bgm");
1294
+ if (track === void 0) return;
1295
+ for (const clip of clipsOnTrack(draft, track.entityId)) this.removeClipGraph(draft, clip.entityId);
1296
+ });
1297
+ }
1298
+ setCaptionVisibility(input) {
1299
+ this.transact((draft) => {
1300
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "caption");
1301
+ replaceEntity(draft, track.entityId, {
1302
+ ...track,
1303
+ payload: {
1304
+ ...track.payload,
1305
+ hidden: input.hidden
1306
+ }
1307
+ });
1308
+ });
1309
+ }
1310
+ patchCaptionStyle(input) {
1311
+ this.transact((draft) => {
1312
+ const track = findRoleTrack(draft, input.timelineEntityId, "caption");
1313
+ if (track === void 0) return;
1314
+ const patch = cloneJsonObject(input.style, "Caption style");
1315
+ const assembled = new EntityProjectionGraph(draft);
1316
+ for (const clip of clipsOnTrack(draft, track.entityId)) {
1317
+ const topology = requireClipTopology(draft, clip.entityId);
1318
+ const caption = requireEntityKind(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId), "caption");
1319
+ const prior = assembled.entity(caption.entityId).payload.style;
1320
+ const style = isJsonObject(prior) ? prior : {};
1321
+ for (const updated of updateEntityFields(draft, caption.entityId, { style: {
1322
+ ...style,
1323
+ ...patch
1324
+ } })) replaceEntity(draft, updated.entityId, updated);
1325
+ }
1326
+ });
1327
+ }
1328
+ /**
1329
+ * Place a Caption as a composed variant of an existing AudioScript: the
1330
+ * entity stores baseEntityIds, selection, optional style, and inherent cue-bounded
1331
+ * Sequence. This helper does not copy base text. The Clip and its display Marker describe this use;
1332
+ * moving or stretching the returned Clip later changes the Clip/Marker,
1333
+ * never the Caption entity or the AudioScript.
1334
+ */
1335
+ insertCaptionClip(input) {
1336
+ return this.transact((draft) => {
1337
+ const durationMs = finitePositive(input.durationMs, "Caption durationMs");
1338
+ const selections = captionSelections(input.selections);
1339
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "caption");
1340
+ const content = {
1341
+ entityId: this.mintEntityId(draft),
1342
+ entityKind: "caption",
1343
+ payload: {
1344
+ extent: {
1345
+ kind: "bounded",
1346
+ start: 0,
1347
+ end: durationMs
1348
+ },
1349
+ sampling: "native",
1350
+ coordinateSpace: "milliseconds",
1351
+ baseEntityIds: captionBaseIds(input.baseEntityIds),
1352
+ selections,
1353
+ ...input.style === void 0 ? {} : { style: cloneJsonObject(input.style, "Caption style") }
1354
+ }
1355
+ };
1356
+ draft.entities.push(content);
1357
+ assembleCaptionContent(draft, content.entityId);
1358
+ return this.insertPlacedClipIntoDraft(draft, {
1359
+ trackEntityId: track.entityId,
1360
+ contentEntityId: content.entityId,
1361
+ sourceRange: {
1362
+ start: 0,
1363
+ end: durationMs
1364
+ },
1365
+ duration: {
1366
+ mode: "fixed",
1367
+ value: durationMs
1368
+ },
1369
+ placement: input.placement,
1370
+ ...input.captionClipEntityId === void 0 ? {} : { clipEntityId: input.captionClipEntityId }
1371
+ });
1372
+ });
1373
+ }
1374
+ insertPlacedClipIntoDraft(draft, input) {
1375
+ const track = requireEntityKind(draft, input.trackEntityId, "track");
1376
+ requireTrackTimeline(draft, track.entityId);
1377
+ const content = requireSequenceContent(draft, input.contentEntityId);
1378
+ const clipId = input.clipEntityId === void 0 ? this.mintEntityId(draft) : requireAvailableEntityId(draft, input.clipEntityId);
1379
+ const markerId = this.mintEntityId(draft);
1380
+ const clipPayload = { ...cloneJsonObject(input.clipPayload ?? {}, "Clip payload") };
1381
+ delete clipPayload.order;
1382
+ const markerPlacement = applyPlacementPayload(clipPayload, input.placement);
1383
+ const clip = {
1384
+ entityId: clipId,
1385
+ entityKind: "clip",
1386
+ payload: clipPayload
1387
+ };
1388
+ const marker = {
1389
+ entityId: markerId,
1390
+ entityKind: "sequence-marker",
1391
+ payload: markerPayload({
1392
+ sourceRange: input.sourceRange,
1393
+ duration: input.duration,
1394
+ ...markerPlacement
1395
+ })
1396
+ };
1397
+ draft.entities.push(clip, marker);
1398
+ 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));
1399
+ if (input.placement.kind === "anchored") {
1400
+ const host = requireEntityKind(draft, input.placement.hostClipEntityId, "clip");
1401
+ if (host.entityId === clipId) throw new Error("A Clip cannot anchor to itself");
1402
+ draft.relations.push(this.createRelation(draft, "clip-anchor", clipId, host.entityId));
1403
+ }
1404
+ return clipId;
1405
+ }
1406
+ ensureRoleTrack(draft, timelineEntityId, role) {
1407
+ const existing = findRoleTrack(draft, timelineEntityId, role);
1408
+ if (existing !== void 0) {
1409
+ if (existing.payload.order === void 0) {
1410
+ const replacement = {
1411
+ ...existing,
1412
+ payload: {
1413
+ ...existing.payload,
1414
+ order: roleOrder[role]
1415
+ }
1416
+ };
1417
+ replaceEntity(draft, existing.entityId, replacement);
1418
+ return replacement;
1419
+ }
1420
+ return existing;
1421
+ }
1422
+ const timeline = requireEntityKind(draft, timelineEntityId, "timeline");
1423
+ const track = {
1424
+ entityId: this.mintEntityId(draft),
1425
+ entityKind: "track",
1426
+ payload: {
1427
+ role,
1428
+ order: roleOrder[role]
1429
+ }
1430
+ };
1431
+ draft.entities.push(track);
1432
+ draft.relations.push(this.createRelation(draft, "timeline-track", timeline.entityId, track.entityId));
1433
+ return track;
1434
+ }
1435
+ /** The existing pronunciation variant a Voice consumes; composition is required. */
1436
+ requireComposedPhoneticScript(draft, phoneticScriptEntityId) {
1437
+ const phonetic = requireEntityKind(draft, phoneticScriptEntityId, "phonetic-script");
1438
+ requireComposedAudioScript(draft, phonetic.entityId);
1439
+ return phonetic;
1440
+ }
1441
+ /** Preserve the factual synthesis input, regardless of persisted endpoint positions. */
1442
+ ensurePhoneticRender(draft, voiceEntityId, phoneticScriptEntityId) {
1443
+ const renders = draft.relations.filter((relation) => relation.relationKind === "phonetic-script-render" && (relation.endpoint0EntityId === voiceEntityId || relation.endpoint1EntityId === voiceEntityId));
1444
+ if (renders.length > 1) throw new Error(`Voice "${voiceEntityId}" has multiple PhoneticScript render relations`);
1445
+ if (renders[0] !== void 0) {
1446
+ if (otherEndpoint(renders[0], voiceEntityId) === phoneticScriptEntityId) return;
1447
+ throw new Error(`Voice "${voiceEntityId}" already has a different PhoneticScript synthesis input`);
1448
+ }
1449
+ draft.relations.push(this.createRelation(draft, "phonetic-script-render", voiceEntityId, phoneticScriptEntityId));
1450
+ }
1451
+ reconcileVoiceoverCaptions(draft, voiceoverClipEntityId, voiceEntityId, phoneticScriptEntityId, captions) {
1452
+ const audioScriptEntityId = requireComposedAudioScript(draft, phoneticScriptEntityId);
1453
+ const requested = new Set(captions.map((caption) => caption.captionClipEntityId));
1454
+ if (requested.size !== captions.length) throw new Error("Caption Clip ids must be unique within a voiceover take");
1455
+ const existing = directAnchorChildren(draft, voiceoverClipEntityId).filter((clipId) => clipRole(draft, clipId) === "caption");
1456
+ for (const clipId of existing) if (!requested.has(clipId)) this.removeCaptionClipGraph(draft, clipId);
1457
+ const captionTrack = captions.length === 0 ? void 0 : this.ensureRoleTrackForClip(draft, voiceoverClipEntityId, "caption");
1458
+ for (const caption of captions) {
1459
+ const startMs = finiteNonNegative(caption.startMs, "Caption startMs");
1460
+ const durationMs = finitePositive(caption.durationMs, "Caption durationMs");
1461
+ const selections = captionSelections(caption.selections);
1462
+ const baseEntityIds = captionBaseIds(caption.baseEntityIds);
1463
+ if (draft.entities.find((entity) => entity.entityId === caption.captionClipEntityId) === void 0) {
1464
+ const content = {
1465
+ entityId: this.mintEntityId(draft),
1466
+ entityKind: "caption",
1467
+ payload: {
1468
+ extent: {
1469
+ kind: "bounded",
1470
+ start: 0,
1471
+ end: durationMs
1472
+ },
1473
+ sampling: "native",
1474
+ coordinateSpace: "milliseconds",
1475
+ baseEntityIds,
1476
+ selections,
1477
+ ...caption.style === void 0 ? {} : { style: cloneJsonObject(caption.style, "Caption style") }
1478
+ }
1479
+ };
1480
+ draft.entities.push(content);
1481
+ assertCaptionScript(draft, content.entityId, audioScriptEntityId);
1482
+ draft.relations.push(this.createRelation(draft, "caption-alignment", content.entityId, voiceEntityId, { alignment: {
1483
+ start: startMs,
1484
+ end: startMs + durationMs
1485
+ } }));
1486
+ this.insertPlacedClipIntoDraft(draft, {
1487
+ clipEntityId: caption.captionClipEntityId,
1488
+ trackEntityId: captionTrack.entityId,
1489
+ contentEntityId: content.entityId,
1490
+ sourceRange: {
1491
+ start: 0,
1492
+ end: durationMs
1493
+ },
1494
+ duration: {
1495
+ mode: "fixed",
1496
+ value: durationMs
1497
+ },
1498
+ placement: {
1499
+ kind: "anchored",
1500
+ hostClipEntityId: voiceoverClipEntityId,
1501
+ anchorOffset: startMs
1502
+ }
1503
+ });
1504
+ continue;
1505
+ }
1506
+ const topology = requireClipOnRole(draft, caption.captionClipEntityId, "caption");
1507
+ const content = requireEntityKind(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId), "caption");
1508
+ const priorStyle = isJsonObject(content.payload.style) ? content.payload.style : {};
1509
+ const style = caption.style === void 0 ? priorStyle : {
1510
+ ...priorStyle,
1511
+ ...cloneJsonObject(caption.style, "Caption style")
1512
+ };
1513
+ replaceEntity(draft, content.entityId, {
1514
+ ...content,
1515
+ payload: {
1516
+ ...content.payload,
1517
+ baseEntityIds,
1518
+ selections,
1519
+ ...Object.keys(style).length === 0 ? {} : { style }
1520
+ }
1521
+ });
1522
+ assertCaptionScript(draft, content.entityId, audioScriptEntityId);
1523
+ this.applyClipPlacement(draft, topology.clip.entityId, {
1524
+ kind: "anchored",
1525
+ hostClipEntityId: voiceoverClipEntityId,
1526
+ anchorOffset: startMs
1527
+ });
1528
+ const placed = requireClipTopology(draft, topology.clip.entityId);
1529
+ replaceEntity(draft, placed.marker.entityId, {
1530
+ ...placed.marker,
1531
+ payload: {
1532
+ ...placed.marker.payload,
1533
+ duration: {
1534
+ mode: "fixed",
1535
+ value: durationMs
1536
+ }
1537
+ }
1538
+ });
1539
+ this.replaceCaptionAlignment(draft, content.entityId, voiceEntityId, startMs, durationMs);
1540
+ }
1541
+ }
1542
+ ensureRoleTrackForClip(draft, clipEntityId, role) {
1543
+ const topology = requireClipTopology(draft, clipEntityId);
1544
+ const track = requireEntityKind(draft, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track");
1545
+ const timeline = requireEntityKind(draft, otherEndpoint(requireOneRelation(draft, track.entityId, "timeline-track"), track.entityId), "timeline");
1546
+ return this.ensureRoleTrack(draft, timeline.entityId, role);
1547
+ }
1548
+ replaceCaptionAlignment(draft, captionEntityId, voiceEntityId, startMs, durationMs) {
1549
+ const removed = new Set(incidentRelations(draft, captionEntityId).filter((relation) => relation.relationKind === "caption-alignment").map((relation) => relation.relationId));
1550
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1551
+ draft.relations.push(this.createRelation(draft, "caption-alignment", captionEntityId, voiceEntityId, { alignment: {
1552
+ start: startMs,
1553
+ end: startMs + durationMs
1554
+ } }));
1555
+ }
1556
+ /**
1557
+ * Remove one placed Caption use. The composed AudioScript outlives the
1558
+ * caption: it may anchor further captions and remains the text owner.
1559
+ */
1560
+ removeCaptionClipGraph(draft, clipEntityId) {
1561
+ const topology = requireClipOnRole(draft, clipEntityId, "caption");
1562
+ const caption = requireEntityKind(draft, otherEndpoint(topology.markerContentRelation, topology.marker.entityId), "caption");
1563
+ const contentRelations = incidentRelations(draft, caption.entityId);
1564
+ const allowed = new Set(["caption-alignment", "marker-content"]);
1565
+ const unsupported = contentRelations.filter((relation) => !allowed.has(relation.relationKind));
1566
+ if (unsupported.length > 0) throw new Error(`Cannot delete Caption with unsupported Relation ${unsupported[0].relationId}`);
1567
+ this.removeClipGraph(draft, clipEntityId);
1568
+ if (incidentRelations(draft, caption.entityId).some((relation) => relation.relationKind === "marker-content")) return;
1569
+ const removed = new Set(incidentRelations(draft, caption.entityId).map((relation) => relation.relationId));
1570
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1571
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== caption.entityId);
1572
+ }
1573
+ replaceAnchorHost(draft, childClipEntityId, hostClipEntityId) {
1574
+ const anchors = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === childClipEntityId);
1575
+ if (anchors.length !== 1) throw new Error(`Clip "${childClipEntityId}" must have exactly one host`);
1576
+ this.relinkRelation(draft, anchors[0], {
1577
+ endpoint0EntityId: createEntityId(childClipEntityId),
1578
+ endpoint1EntityId: createEntityId(hostClipEntityId)
1579
+ });
1580
+ }
1581
+ /** Rebuild the legacy edit baseline from current visual order before a high-level cascade. */
1582
+ normalizeMainSequential(draft) {
1583
+ const videoTrack = resolveEntityTimelineLayout(draft).tracks.find((track) => track.role === "video_clip");
1584
+ if (videoTrack === void 0) return;
1585
+ videoTrack.clips.forEach((clip, order) => {
1586
+ this.applyClipPlacement(draft, clip.clipEntityId, {
1587
+ kind: "sequential",
1588
+ order
1589
+ });
1590
+ });
1591
+ }
1592
+ resolveOneVoiceoverOverlap(draft) {
1593
+ const layout = resolveEntityTimelineLayout(draft);
1594
+ const speechTrack = layout.tracks.find((track) => track.role === "speech");
1595
+ if (speechTrack === void 0) return;
1596
+ const ranges = speechTrack.clips.map((speech) => {
1597
+ const captions = directAnchorChildren(draft, speech.clipEntityId).filter((clipId) => clipRole(draft, clipId) === "caption").map((clipId) => layout.clips.get(clipId)).filter((value) => value !== void 0);
1598
+ return {
1599
+ speech,
1600
+ start: Math.min(speech.start, ...captions.map((caption) => caption.start)),
1601
+ end: Math.max(speech.start + speech.duration, ...captions.map((caption) => caption.start + caption.duration))
1602
+ };
1603
+ }).sort((left, right) => left.start - right.start || left.speech.clipEntityId.localeCompare(right.speech.clipEntityId));
1604
+ for (let index = 1; index < ranges.length; index += 1) {
1605
+ const prior = ranges[index - 1];
1606
+ const current = ranges[index];
1607
+ if (current.start >= prior.end) continue;
1608
+ const hostId = current.speech.anchorClipEntityId;
1609
+ if (hostId === void 0) continue;
1610
+ const videoTrack = layout.tracks.find((track) => track.role === "video_clip");
1611
+ const hostIndex = videoTrack?.clips.findIndex((clip) => clip.clipEntityId === hostId) ?? -1;
1612
+ if (videoTrack === void 0 || hostIndex < 0) continue;
1613
+ const overlapMs = Math.round(prior.end - current.start);
1614
+ for (const [visualIndex, clip] of videoTrack.clips.entries()) this.applyClipPlacement(draft, clip.clipEntityId, {
1615
+ kind: "absolute",
1616
+ targetRange: {
1617
+ start: clip.start + (visualIndex >= hostIndex ? overlapMs : 0),
1618
+ end: clip.start + clip.duration + (visualIndex >= hostIndex ? overlapMs : 0)
1619
+ }
1620
+ });
1621
+ return;
1622
+ }
1623
+ }
1624
+ applyClipPlacement(draft, clipEntityId, placement) {
1625
+ const topology = requireClipTopology(draft, clipEntityId);
1626
+ const clipPayload = { ...cloneJsonObject(topology.clip.payload, "Clip payload") };
1627
+ const markerPayloadValue = { ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload") };
1628
+ delete clipPayload.order;
1629
+ delete markerPayloadValue.targetRange;
1630
+ delete markerPayloadValue.anchorOffset;
1631
+ const outgoingAnchors = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === topology.clip.entityId);
1632
+ const removed = new Set(outgoingAnchors.map((relation) => relation.relationId));
1633
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1634
+ if (placement.kind === "sequential") clipPayload.order = finiteNumber(placement.order, "Clip order");
1635
+ else if (placement.kind === "absolute") markerPayloadValue.targetRange = cloneJsonValue(placement.targetRange, "targetRange");
1636
+ else {
1637
+ const host = requireEntityKind(draft, placement.hostClipEntityId, "clip");
1638
+ if (host.entityId === topology.clip.entityId) throw new Error("A Clip cannot anchor to itself");
1639
+ markerPayloadValue.anchorOffset = finiteNonNegative(placement.anchorOffset, "anchorOffset");
1640
+ draft.relations.push(this.createRelation(draft, "clip-anchor", topology.clip.entityId, host.entityId));
1641
+ }
1642
+ replaceEntity(draft, topology.clip.entityId, {
1643
+ ...topology.clip,
1644
+ payload: clipPayload
1645
+ });
1646
+ replaceEntity(draft, topology.marker.entityId, {
1647
+ ...topology.marker,
1648
+ payload: markerPayloadValue
1649
+ });
1650
+ }
1651
+ reanchorAtAbsolute(draft, childClipEntityId, absoluteStart, layout) {
1652
+ const hosts = layout.tracks.find((track) => track.role === "video_clip")?.clips.filter((clip) => clip.mode === "sequential") ?? [];
1653
+ if (hosts.length === 0) {
1654
+ const child = layout.clips.get(childClipEntityId);
1655
+ if (child === void 0) throw new Error(`Clip "${childClipEntityId}" is not placed`);
1656
+ this.applyClipPlacement(draft, childClipEntityId, {
1657
+ kind: "absolute",
1658
+ targetRange: {
1659
+ start: absoluteStart,
1660
+ end: absoluteStart + child.duration
1661
+ }
1662
+ });
1663
+ return;
1664
+ }
1665
+ const host = hosts.find((candidate) => candidate.start <= absoluteStart && absoluteStart < candidate.start + candidate.duration) ?? (absoluteStart < hosts[0].start ? hosts[0] : hosts.at(-1));
1666
+ this.applyClipPlacement(draft, childClipEntityId, {
1667
+ kind: "anchored",
1668
+ hostClipEntityId: host.clipEntityId,
1669
+ anchorOffset: Math.max(0, Math.round(absoluteStart - host.start))
1670
+ });
1671
+ }
1672
+ removeAnchoredDescendants(draft, hostClipEntityId, scheduled) {
1673
+ const children = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint1EntityId === hostClipEntityId).map((relation) => String(relation.endpoint0EntityId));
1674
+ for (const child of children) {
1675
+ if (scheduled.has(child)) continue;
1676
+ const descendants = new Set(scheduled);
1677
+ descendants.add(child);
1678
+ this.removeAnchoredDescendants(draft, child, descendants);
1679
+ if (clipRole(draft, child) === "caption") this.removeCaptionClipGraph(draft, child);
1680
+ else this.removeClipGraph(draft, child);
1681
+ }
1682
+ }
1683
+ removeClipGraph(draft, clipEntityId) {
1684
+ const topology = requireClipTopology(draft, clipEntityId);
1685
+ assertOnlyStructuralIncidents(draft, topology.clip.entityId, new Set([
1686
+ "track-clip",
1687
+ "clip-marker",
1688
+ "clip-anchor"
1689
+ ]));
1690
+ assertOnlyStructuralIncidents(draft, topology.marker.entityId, new Set(["clip-marker", "marker-content"]));
1691
+ const markerIsExclusive = incidentRelations(draft, topology.marker.entityId).filter((relation) => relation.relationKind === "clip-marker").length === 1;
1692
+ const removed = new Set(incidentRelations(draft, topology.clip.entityId).map((relation) => relation.relationId));
1693
+ if (markerIsExclusive) for (const relation of incidentRelations(draft, topology.marker.entityId)) removed.add(relation.relationId);
1694
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1695
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== topology.clip.entityId && (!markerIsExclusive || entity.entityId !== topology.marker.entityId));
1696
+ }
1697
+ transact(edit) {
1698
+ const draft = cloneRows(this.state);
1699
+ const result = edit(draft);
1700
+ validateFinalRows(draft);
1701
+ assertMediaAssetWritePolicy(this.state, draft);
1702
+ this.state = draft;
1703
+ return result;
1704
+ }
1705
+ mintEntityId(draft) {
1706
+ const entityId = createEntityId(this.idFactory("entity"));
1707
+ if (draft.entities.some((entity) => entity.entityId === entityId)) throw new Error(`Entity id "${entityId}" already exists`);
1708
+ return entityId;
1709
+ }
1710
+ createRelation(draft, relationKind, endpoint0EntityId, endpoint1EntityId, metadata = {}) {
1711
+ return {
1712
+ relationId: this.mintRelationId(draft),
1713
+ relationKind,
1714
+ endpoint0EntityId,
1715
+ endpoint1EntityId,
1716
+ metadata,
1717
+ trace: {}
1718
+ };
1719
+ }
1720
+ relinkRelation(draft, relation, endpoints) {
1721
+ const relationId = this.mintRelationId(draft);
1722
+ const index = draft.relations.findIndex((candidate) => candidate.relationId === relation.relationId);
1723
+ if (index < 0) throw new Error(`Relation id "${relation.relationId}" does not exist`);
1724
+ draft.relations[index] = {
1725
+ ...relation,
1726
+ ...endpoints,
1727
+ relationId
1728
+ };
1729
+ }
1730
+ mintRelationId(draft) {
1731
+ const relationId = createRelationId(this.idFactory("relation"));
1732
+ if (draft.relations.some((relation) => relation.relationId === relationId)) throw new Error(`Relation id "${relationId}" already exists`);
1733
+ return relationId;
1734
+ }
1735
+ };
1736
+ function validateFinalRows(rows) {
1737
+ decodeEntityRelationRows(rows, numericMarkerValidation);
1738
+ assertCanonicalEditorResources(rows);
1739
+ validateTimelineHierarchy(rows);
1740
+ }
1741
+ function validateTimelineHierarchy(rows) {
1742
+ for (const entity of rows.entities) {
1743
+ if (entity.entityKind === "track") requireTrackTimeline(rows, entity.entityId);
1744
+ if (entity.entityKind !== "clip") continue;
1745
+ const trackRelations = incidentRelations(rows, entity.entityId).filter((relation) => relation.relationKind === "track-clip");
1746
+ if (trackRelations.length !== 1) throw new Error(`Clip Entity "${entity.entityId}" must belong to exactly one Track`);
1747
+ requireEntityKind(rows, otherEndpoint(trackRelations[0], entity.entityId), "track");
1748
+ }
1749
+ }
1750
+ function requireTrackTimeline(rows, trackEntityId) {
1751
+ const timelineRelations = incidentRelations(rows, trackEntityId).filter((relation) => relation.relationKind === "timeline-track");
1752
+ if (timelineRelations.filter((relation) => {
1753
+ const otherId = otherEndpoint(relation, trackEntityId);
1754
+ return rows.entities.some((entity) => entity.entityId === otherId && entity.entityKind === "timeline");
1755
+ }).length !== 1 || timelineRelations.length !== 1) throw new Error(`Track Entity "${trackEntityId}" must belong to exactly one Timeline`);
1756
+ }
1757
+ function requireClipTopology(rows, clipEntityId) {
1758
+ const clip = requireEntityKind(rows, clipEntityId, "clip");
1759
+ const trackRelation = requireOneRelation(rows, clip.entityId, "track-clip");
1760
+ requireEntityKind(rows, otherEndpoint(trackRelation, clip.entityId), "track");
1761
+ const clipMarkerRelation = requireOneRelation(rows, clip.entityId, "clip-marker");
1762
+ const marker = requireEntityKind(rows, otherEndpoint(clipMarkerRelation, clip.entityId), "sequence-marker");
1763
+ const markerContentRelation = requireOneRelation(rows, marker.entityId, "marker-content");
1764
+ requireSequenceContent(rows, otherEndpoint(markerContentRelation, marker.entityId));
1765
+ return {
1766
+ clip,
1767
+ marker,
1768
+ trackRelation,
1769
+ clipMarkerRelation,
1770
+ markerContentRelation
1771
+ };
1772
+ }
1773
+ function requireOneRelation(rows, entityId, relationKind) {
1774
+ const matches = incidentRelations(rows, entityId).filter((relation) => relation.relationKind === relationKind);
1775
+ if (matches.length !== 1) throw new Error(`Entity "${entityId}" must have exactly one ${relationKind} Relation`);
1776
+ return matches[0];
1777
+ }
1778
+ function requireEntityKind(rows, entityIdValue, entityKind) {
1779
+ const entityId = createEntityId(entityIdValue);
1780
+ const entity = rows.entities.find((candidate) => candidate.entityId === entityId);
1781
+ if (entity == null) throw new Error(`Entity id "${entityId}" does not exist`);
1782
+ if (entity.entityKind !== entityKind) throw new Error(`Entity "${entityId}" must have kind "${entityKind}", got "${entity.entityKind}"`);
1783
+ return entity;
1784
+ }
1785
+ function requireSequenceContent(rows, entityIdValue) {
1786
+ 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");
1787
+ const entityId = createEntityId(entityIdValue);
1788
+ const row = rows.entities.find((candidate) => candidate.entityId === entityId);
1789
+ if (row == null) throw new Error(`Timeline content must reference an existing Sequence media Entity; "${entityId}" was not found`);
1790
+ if (!hasSequence({
1791
+ ...row.payload,
1792
+ entityId: row.entityId,
1793
+ entityKind: row.entityKind
1794
+ })) throw new Error(`Timeline content must reference a Sequence media Entity, but "${entityId}" has kind "${row.entityKind}"`);
1795
+ return row;
1796
+ }
1797
+ function assertOnlyStructuralIncidents(rows, entityId, allowedKinds) {
1798
+ const unsupported = incidentRelations(rows, entityId).filter((relation) => !allowedKinds.has(relation.relationKind));
1799
+ if (unsupported.length === 0) return;
1800
+ throw new Error(`Cannot delete Clip graph: Entity "${entityId}" has unsupported incident Relation id(s): ${unsupported.map((relation) => relation.relationId).sort().join(", ")}`);
1801
+ }
1802
+ function incidentRelations(rows, entityId) {
1803
+ return rows.relations.filter((relation) => relation.endpoint0EntityId === entityId || relation.endpoint1EntityId === entityId);
1804
+ }
1805
+ function hasOutgoingClipAnchor(rows, clipEntityId) {
1806
+ return rows.relations.some((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === clipEntityId);
1807
+ }
1808
+ function outgoingAnchorChildren(rows, hostIds) {
1809
+ return rows.relations.filter((relation) => relation.relationKind === "clip-anchor" && hostIds.has(relation.endpoint1EntityId)).map((relation) => String(relation.endpoint0EntityId));
1810
+ }
1811
+ function applyPlacementPayload(clipPayload, placement) {
1812
+ if (placement.kind === "sequential") {
1813
+ clipPayload.order = finiteNumber(placement.order, "Clip order");
1814
+ return {};
1815
+ }
1816
+ if (placement.kind === "absolute") return { targetRange: cloneJsonValue(placement.targetRange, "targetRange") };
1817
+ return { anchorOffset: finiteNonNegative(placement.anchorOffset, "anchorOffset") };
1818
+ }
1819
+ function requireAvailableEntityId(rows, value) {
1820
+ const entityId = createEntityId(value);
1821
+ if (rows.entities.some((entity) => entity.entityId === entityId)) throw new Error(`Entity id "${entityId}" already exists`);
1822
+ return entityId;
1823
+ }
1824
+ function setClipOrder(rows, clipEntityId, order) {
1825
+ const clip = requireEntityKind(rows, clipEntityId, "clip");
1826
+ replaceEntity(rows, clip.entityId, {
1827
+ ...clip,
1828
+ payload: {
1829
+ ...clip.payload,
1830
+ order
1831
+ }
1832
+ });
1833
+ }
1834
+ function normalizeSequentialOrders(rows) {
1835
+ 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));
1836
+ }
1837
+ function findRoleTrack(rows, timelineEntityId, role) {
1838
+ const timeline = requireEntityKind(rows, timelineEntityId, "timeline");
1839
+ 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);
1840
+ if (tracks.length > 1) throw new Error(`Timeline "${timelineEntityId}" has multiple ${role} Tracks`);
1841
+ return tracks[0];
1842
+ }
1843
+ function clipsOnTrack(rows, trackEntityId) {
1844
+ return incidentRelations(rows, trackEntityId).filter((relation) => relation.relationKind === "track-clip").map((relation) => requireEntityKind(rows, otherEndpoint(relation, trackEntityId), "clip"));
1845
+ }
1846
+ function clipRole(rows, clipEntityId) {
1847
+ const topology = requireClipTopology(rows, clipEntityId);
1848
+ return requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role;
1849
+ }
1850
+ function directAnchorChildren(rows, hostClipEntityId) {
1851
+ return rows.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint1EntityId === hostClipEntityId).map((relation) => String(relation.endpoint0EntityId));
1852
+ }
1853
+ function requireSupportedRole(rows, topology) {
1854
+ const role = requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role;
1855
+ if (role !== "video_clip" && role !== "speech" && role !== "caption" && role !== "bgm") throw new Error(`Clip "${topology.clip.entityId}" is not on a supported timeline Track`);
1856
+ return role;
1857
+ }
1858
+ function requireClipOnRole(rows, clipEntityId, role) {
1859
+ const topology = requireClipTopology(rows, clipEntityId);
1860
+ if (requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role !== role) throw new Error(`Clip "${clipEntityId}" must belong to the ${role} Track`);
1861
+ return topology;
1862
+ }
1863
+ function finiteNumber(value, label) {
1864
+ if (!Number.isFinite(value)) throw new Error(`${label} must be finite`);
1865
+ return value;
1866
+ }
1867
+ function finitePositive(value, label) {
1868
+ const result = finiteNumber(value, label);
1869
+ if (result <= 0) throw new Error(`${label} must be positive`);
1870
+ return result;
1871
+ }
1872
+ function requireVolume(value) {
1873
+ const volume = finiteNumber(value, "Clip volume");
1874
+ if (volume < -60 || volume > 20) throw new Error("Clip volume must be between -60 and 20 dB");
1875
+ return volume;
1876
+ }
1877
+ function requirePositiveRange(value, label) {
1878
+ if (!isJsonObject(value)) throw new Error(`${label} must be an object`);
1879
+ const start = finiteNonNegative(value.start, `${label}.start`);
1880
+ const end = finitePositive(value.end, `${label}.end`);
1881
+ if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || end <= start) throw new Error(`${label} must be a positive whole-millisecond range`);
1882
+ return {
1883
+ start,
1884
+ end
1885
+ };
1886
+ }
1887
+ function markerDuration(payload, sourceRange) {
1888
+ const duration = payload.duration;
1889
+ if (!isJsonObject(duration)) throw new Error("Sequence Marker duration must be an object");
1890
+ if (duration.mode === "from-source") return sourceRange.end - sourceRange.start;
1891
+ if (duration.mode !== "fixed") throw new Error("Sequence Marker duration mode is unsupported");
1892
+ return finitePositive(duration.value, "Sequence Marker fixed duration");
1893
+ }
1894
+ function linearRate(value) {
1895
+ if (value === void 0) return 1;
1896
+ if (!isJsonObject(value) || value.kind !== "linear") throw new Error("Only linear Clip time remapping is supported");
1897
+ return finitePositive(value.rate, "linear speed rate");
1898
+ }
1899
+ function resizeAbsoluteTarget(payload, duration) {
1900
+ if (payload.targetRange === void 0) return;
1901
+ const target = requirePositiveRange(payload.targetRange, "targetRange");
1902
+ payload.targetRange = {
1903
+ start: target.start,
1904
+ end: target.start + duration
1905
+ };
1906
+ }
1907
+ function replaceRows(rows, replacement) {
1908
+ rows.entities = structuredClone([...replacement.entities]);
1909
+ rows.relations = structuredClone([...replacement.relations]);
1910
+ }
1911
+ function finiteNonNegative(value, label) {
1912
+ const result = finiteNumber(value, label);
1913
+ if (result < 0) throw new Error(`${label} must be non-negative`);
1914
+ return result;
1915
+ }
1916
+ function nextSequentialOrder(rows, trackEntityId) {
1917
+ 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));
1918
+ return orders.length === 0 ? 0 : Math.max(...orders) + 1;
1919
+ }
1920
+ function otherEndpoint(relation, entityId) {
1921
+ if (relation.endpoint0EntityId === entityId) return relation.endpoint1EntityId;
1922
+ if (relation.endpoint1EntityId === entityId) return relation.endpoint0EntityId;
1923
+ throw new Error(`Relation "${relation.relationId}" is not incident to Entity "${entityId}"`);
1924
+ }
1925
+ function replaceEntity(rows, entityId, replacement) {
1926
+ const index = rows.entities.findIndex((entity) => entity.entityId === entityId);
1927
+ if (index < 0) throw new Error(`Entity id "${entityId}" does not exist`);
1928
+ rows.entities[index] = replacement;
1929
+ }
1930
+ function markerPayload(value) {
1931
+ return cloneJsonObject(value, "Sequence Marker payload");
1932
+ }
1933
+ function cloneJsonObject(value, label) {
1934
+ const cloned = structuredClone(value);
1935
+ if (!isJsonObject(cloned)) throw new Error(`${label} must contain only JSON values`);
1936
+ return cloned;
1937
+ }
1938
+ function cloneJsonValue(value, label) {
1939
+ return cloneJsonObject({ value }, label).value;
1940
+ }
1941
+ function cloneRows(rows) {
1942
+ return {
1943
+ entities: rows.entities.map((entity) => structuredClone(entity)),
1944
+ relations: rows.relations.map((relation) => structuredClone(relation))
1945
+ };
1946
+ }
1947
+ const numericMarkerValidation = { compareMarkerPoints: (_marker, _range, left, right) => {
1948
+ if (typeof left !== "number" || !Number.isFinite(left) || typeof right !== "number" || !Number.isFinite(right)) throw new Error("Sequence Marker points must use finite numeric coordinates");
1949
+ return left - right;
1950
+ } };
1951
+ //#endregion
1952
+ //#region src/entity-editor/legacy-entity-migration.ts
1953
+ /** One-time import only. Ordinary editor operations must never reverse-diff a legacy document. */
1954
+ function migrateLegacyTimelineToEntities(legacy, assetFacts, initial = {
1955
+ entities: [],
1956
+ relations: []
1957
+ }, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`) {
1958
+ assertSupportedLegacy(legacy);
1959
+ if (initial.entities.some((row) => row.entityKind === "timeline")) fail("Timeline already exists; migration is not an editing operation");
1960
+ if (legacy.meta.schema_version === "video-document/entity-projection-v1") fail("An entity projection cannot be migrated as legacy state");
1961
+ const normalizedMainRanges = overlapNormalizedMainRanges(legacy);
1962
+ let entities = structuredClone([...initial.entities]);
1963
+ let relations = structuredClone([...initial.relations]);
1964
+ const addEntity = (kind, payload, existingId) => {
1965
+ const entityId = createEntityId(existingId ?? idFactory("entity"));
1966
+ if (entities.some((row) => row.entityId === entityId)) fail(`Migration entity identity collision: ${entityId}`);
1967
+ entities.push({
1968
+ entityId,
1969
+ entityKind: kind,
1970
+ payload
1971
+ });
1972
+ return entityId;
1973
+ };
1974
+ const link = (kind, from, to, metadata = {}) => {
1975
+ const relationId = createRelationId(idFactory("relation"));
1976
+ if (relations.some((row) => row.relationId === relationId)) fail(`Migration relation identity collision: ${relationId}`);
1977
+ relations.push({
1978
+ relationId,
1979
+ relationKind: kind,
1980
+ endpoint0EntityId: createEntityId(from),
1981
+ endpoint1EntityId: createEntityId(to),
1982
+ metadata,
1983
+ trace: {}
1984
+ });
1985
+ };
1986
+ const facts = /* @__PURE__ */ new Map();
1987
+ for (const fact of assetFacts) {
1988
+ if (facts.has(fact.assetId)) fail(`Duplicate asset facts for ${fact.assetId}`);
1989
+ facts.set(fact.assetId, fact);
1990
+ }
1991
+ const contentByPartId = /* @__PURE__ */ new Map();
1992
+ for (const track of legacy.tracks ?? []) for (const item of track.items ?? []) {
1993
+ const part = requirePart(legacy, item.part_id);
1994
+ if (part.video_clip !== void 0) {
1995
+ const assetId = part.video_clip.origin_media_id;
1996
+ const fact = facts.get(assetId);
1997
+ if (fact === void 0) fail(`Missing factual media metadata for ${assetId}`);
1998
+ if (fact.kind !== "image" && fact.kind !== "video") fail(`Legacy video Clip ${item.part_id} requires an image or video fact`);
1999
+ const imported = importMediaAsset({
2000
+ entities,
2001
+ relations
2002
+ }, fact, idFactory);
2003
+ entities = [...imported.rows.entities];
2004
+ relations = [...imported.rows.relations];
2005
+ contentByPartId.set(item.part_id, imported.contentEntityId);
2006
+ } else if (part.speech !== void 0) {
2007
+ const speech = part.speech;
2008
+ const imported = importMediaAsset({
2009
+ entities,
2010
+ relations
2011
+ }, {
2012
+ assetId: speech.origin_speech_id,
2013
+ kind: "voice",
2014
+ durationMs: speech.media_duration_ms,
2015
+ storageKey: speech.audio_storage_key,
2016
+ voice: {
2017
+ system: "voice-library",
2018
+ key: speech.voice.id,
2019
+ ...speech.voice.name === void 0 ? {} : { name: speech.voice.name }
2020
+ }
2021
+ }, idFactory);
2022
+ entities = [...imported.rows.entities];
2023
+ relations = [...imported.rows.relations];
2024
+ contentByPartId.set(item.part_id, imported.contentEntityId);
2025
+ const phonemeScript = speech.phoneme_script;
2026
+ ensureSpeechPhoneticChain(imported.contentEntityId, speech.audio_script, typeof phonemeScript === "string" ? phonemeScript : void 0, entities, relations, addEntity, link);
2027
+ } else if (part.bgm !== void 0) {
2028
+ const bgm = part.bgm;
2029
+ const fact = facts.get(bgm.origin_media_id);
2030
+ if (fact === void 0) fail(`Missing factual media metadata for ${bgm.origin_media_id}`);
2031
+ if (fact.kind !== "audio") fail(`Legacy BGM ${item.part_id} requires an audio fact`);
2032
+ if (fact.storageKey !== bgm.audio_storage_key) fail(`Factual storage key conflicts with legacy BGM ${item.part_id}`);
2033
+ const imported = importMediaAsset({
2034
+ entities,
2035
+ relations
2036
+ }, fact, idFactory);
2037
+ entities = [...imported.rows.entities];
2038
+ relations = [...imported.rows.relations];
2039
+ contentByPartId.set(item.part_id, imported.contentEntityId);
2040
+ }
2041
+ }
2042
+ const timeline = addEntity("timeline", legacy.timeline?.unit_time_ms === void 0 ? {} : { unit_time_ms: legacy.timeline.unit_time_ms });
2043
+ const trackIds = /* @__PURE__ */ new Map();
2044
+ for (const [order, oldTrack] of (legacy.tracks ?? []).entries()) {
2045
+ const track = addEntity("track", {
2046
+ role: oldTrack.parts_kind,
2047
+ order,
2048
+ ...oldTrack.is_hidden === void 0 ? {} : { hidden: oldTrack.is_hidden }
2049
+ }, oldTrack.id);
2050
+ trackIds.set(oldTrack, track);
2051
+ link("timeline-track", timeline, track);
2052
+ }
2053
+ for (const oldTrack of legacy.tracks ?? []) {
2054
+ const track = trackIds.get(oldTrack);
2055
+ for (const [order, item] of (oldTrack.items ?? []).entries()) {
2056
+ const part = requirePart(legacy, item.part_id);
2057
+ const role = oldTrack.parts_kind;
2058
+ const volume = part.video_clip?.volume ?? part.speech?.volume ?? part.bgm?.volume;
2059
+ const clip = addEntity("clip", {
2060
+ ...volume === void 0 ? {} : { volume },
2061
+ ...item.time_position.mode === "sequential" && !(role === "video_clip" && normalizedMainRanges !== void 0) || role === "bgm" ? { order: role === "bgm" ? 0 : order } : {}
2062
+ }, item.part_id);
2063
+ let content = contentByPartId.get(item.part_id);
2064
+ let duration;
2065
+ let markerPayload;
2066
+ if (part.video_clip !== void 0) {
2067
+ const video = part.video_clip;
2068
+ duration = effectiveVideoClipDurationMs(video);
2069
+ const fact = facts.get(video.origin_media_id);
2070
+ const rate = speedOf(video.speed_shift);
2071
+ markerPayload = {
2072
+ sourceRange: {
2073
+ start: video.play_in,
2074
+ end: video.play_out
2075
+ },
2076
+ duration: {
2077
+ mode: "fixed",
2078
+ value: duration
2079
+ },
2080
+ ...(fact.kind === "image" || fact.kind === "video") && video.speed_shift !== void 0 ? { timeRemapping: {
2081
+ kind: "linear",
2082
+ mode: video.speed_shift?.mode ?? "constant",
2083
+ rate
2084
+ } } : {}
2085
+ };
2086
+ } else if (part.speech !== void 0) {
2087
+ duration = part.speech.media_duration_ms;
2088
+ markerPayload = {
2089
+ sourceRange: {
2090
+ start: 0,
2091
+ end: duration
2092
+ },
2093
+ duration: {
2094
+ mode: "fixed",
2095
+ value: duration
2096
+ }
2097
+ };
2098
+ } else if (part.caption !== void 0) {
2099
+ const caption = part.caption;
2100
+ duration = caption.initial_duration_ms;
2101
+ const voice = contentByPartId.get(caption.speech_part_id);
2102
+ const render = relations.find((relation) => relation.relationKind === "phonetic-script-render" && relation.endpoint0EntityId === voice);
2103
+ const phonetic = entities.find((entity) => entity.entityId === render?.endpoint1EntityId);
2104
+ const script = phonetic === void 0 ? void 0 : composedAudioScript(phonetic, entities);
2105
+ if (script === void 0) fail("Legacy Caption has no composed AudioScript");
2106
+ const sourceText = scriptText(script);
2107
+ const captionText = caption.text;
2108
+ const offset = sourceText.indexOf(captionText);
2109
+ if (!captionText.trim() || offset < 0 || sourceText.indexOf(captionText, offset + 1) >= 0) fail("Legacy Caption needs explicit unambiguous AudioScript selections before cutover");
2110
+ const selections = [{
2111
+ segmentId: "segment-0",
2112
+ ...sourceText === captionText ? {} : { textRange: {
2113
+ start: Array.from(sourceText.slice(0, offset)).length,
2114
+ end: Array.from(sourceText.slice(0, offset + captionText.length)).length
2115
+ } }
2116
+ }];
2117
+ content = addEntity("caption", {
2118
+ extent: {
2119
+ kind: "bounded",
2120
+ start: 0,
2121
+ end: duration
2122
+ },
2123
+ sampling: "native",
2124
+ coordinateSpace: "milliseconds",
2125
+ baseEntityIds: [script.entityId],
2126
+ selections,
2127
+ ...caption.style === void 0 ? {} : { style: toEntityCaptionStyle(caption.style) }
2128
+ });
2129
+ link("caption-alignment", content, voice, { alignment: {
2130
+ start: caption.start_ms,
2131
+ end: caption.start_ms + duration
2132
+ } });
2133
+ markerPayload = {
2134
+ sourceRange: {
2135
+ start: 0,
2136
+ end: duration
2137
+ },
2138
+ duration: {
2139
+ mode: "fixed",
2140
+ value: duration
2141
+ }
2142
+ };
2143
+ } else {
2144
+ const bgm = part.bgm;
2145
+ const fact = facts.get(bgm.origin_media_id);
2146
+ if (fact.kind !== "audio") fail(`Legacy BGM ${item.part_id} requires an audio fact`);
2147
+ duration = fact.durationMs;
2148
+ markerPayload = {
2149
+ sourceRange: {
2150
+ start: 0,
2151
+ end: duration
2152
+ },
2153
+ duration: { mode: "from-source" },
2154
+ durationPolicy: "timeline"
2155
+ };
2156
+ }
2157
+ if (content === void 0) fail(`Migration could not resolve content for ${item.part_id}`);
2158
+ const normalizedMainRange = role === "video_clip" ? normalizedMainRanges?.get(item.part_id) : void 0;
2159
+ if (normalizedMainRange !== void 0) markerPayload = {
2160
+ ...markerPayload,
2161
+ targetRange: normalizedMainRange
2162
+ };
2163
+ else if (item.time_position.mode === "absolute" && role !== "bgm") markerPayload = {
2164
+ ...markerPayload,
2165
+ targetRange: {
2166
+ start: item.time_position.offsetMs,
2167
+ end: item.time_position.offsetMs + duration
2168
+ }
2169
+ };
2170
+ else if (item.time_position.mode === "anchored") markerPayload = {
2171
+ ...markerPayload,
2172
+ anchorOffset: item.time_position.offsetMs
2173
+ };
2174
+ const marker = addEntity("sequence-marker", markerPayload);
2175
+ link("track-clip", track, clip);
2176
+ link("clip-marker", clip, marker);
2177
+ link("marker-content", marker, content);
2178
+ if (item.time_position.mode === "anchored") link("clip-anchor", clip, item.time_position.anchorPartId);
2179
+ }
2180
+ }
2181
+ const rows = {
2182
+ entities,
2183
+ relations
2184
+ };
2185
+ assertEntityProjectionPreservesLegacy(legacy, projectEntityTimeline(rows, legacy.meta), rows);
2186
+ return rows;
2187
+ }
2188
+ /** Server gate: a cutover may change representation, never the existing render facts. */
2189
+ function assertEntityProjectionPreservesLegacy(legacy, projection, rows) {
2190
+ assertSupportedLegacy(legacy);
2191
+ if (projection.meta.schema_version !== "video-document/entity-projection-v1") fail("Expected an entity projection for migration");
2192
+ const normalizedMainRanges = overlapNormalizedMainRanges(legacy);
2193
+ const compatibilityFacts = (document) => {
2194
+ const { version: _version, ...view } = fromVideoDocument(document);
2195
+ for (const part of Object.values(view.part_library ?? {})) {
2196
+ const speech = part.speech;
2197
+ if (speech?.phoneme_script === null) delete speech.phoneme_script;
2198
+ }
2199
+ const { schema_version: _schemaVersion, version: _metaVersion, ...meta } = document.meta;
2200
+ return {
2201
+ meta,
2202
+ view
2203
+ };
2204
+ };
2205
+ if (canonical(compatibilityFacts(legacy)) !== canonical(compatibilityFacts(projection))) fail("Entity migration must preserve all existing timeline facts; edit after cutover");
2206
+ const trackOrders = [];
2207
+ for (const oldTrack of legacy.tracks ?? []) {
2208
+ const candidates = rows.entities.filter((row) => row.entityKind === "track" && (oldTrack.id === void 0 ? row.payload.role === oldTrack.parts_kind : row.entityId === oldTrack.id));
2209
+ if (candidates.length !== 1) fail("Migration must preserve every Track identity");
2210
+ const track = candidates[0];
2211
+ if (track.payload.role !== oldTrack.parts_kind || (track.payload.hidden ?? false) !== (oldTrack.is_hidden ?? false)) fail("Migration must preserve Track role and visibility");
2212
+ if (typeof track.payload.order !== "number" || !Number.isFinite(track.payload.order)) fail("Migration must preserve Track order");
2213
+ trackOrders.push(track.payload.order);
2214
+ const sequentialOrders = [];
2215
+ for (const item of oldTrack.items ?? []) {
2216
+ const clip = rows.entities.find((row) => row.entityId === item.part_id && row.entityKind === "clip");
2217
+ 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"));
2218
+ const marker = rows.entities.find((row) => row.entityId === binding[0]?.endpoint1EntityId && row.entityKind === "sequence-marker");
2219
+ if (clip === void 0 || binding.length !== 1 || marker === void 0) fail("Migration must preserve Clip/Marker placement identity");
2220
+ const anchors = rows.relations.filter((row) => row.relationKind === "clip-anchor" && row.endpoint0EntityId === clip.entityId);
2221
+ const normalizedMainRange = oldTrack.parts_kind === "video_clip" ? normalizedMainRanges?.get(item.part_id) : void 0;
2222
+ if (normalizedMainRange !== void 0) {
2223
+ 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");
2224
+ } else if (oldTrack.parts_kind === "bgm") {
2225
+ 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");
2226
+ } else if (item.time_position.mode === "sequential") {
2227
+ 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");
2228
+ sequentialOrders.push(clip.payload.order);
2229
+ } else if (item.time_position.mode === "absolute") {
2230
+ const target = marker.payload.targetRange;
2231
+ 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");
2232
+ } 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");
2233
+ }
2234
+ if (sequentialOrders.some((order, index) => index > 0 && order <= sequentialOrders[index - 1])) fail("Migration must preserve sequential Clip order");
2235
+ }
2236
+ if (trackOrders.some((order, index) => index > 0 && order <= trackOrders[index - 1])) fail("Migration must preserve Track stacking order");
2237
+ }
2238
+ function assertSupportedLegacy(document) {
2239
+ assertValidVideoDocument(document);
2240
+ const tracks = document.tracks ?? [];
2241
+ assertKnownFields(document.timeline ?? {}, ["unit_time_ms"], "Timeline");
2242
+ 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");
2243
+ const seenRoles = /* @__PURE__ */ new Set();
2244
+ const placed = /* @__PURE__ */ new Map();
2245
+ for (const track of tracks) {
2246
+ const role = track.parts_kind;
2247
+ if (role === void 0 || ![
2248
+ "video_clip",
2249
+ "speech",
2250
+ "caption",
2251
+ "bgm"
2252
+ ].includes(role)) fail("Every legacy Track requires a supported role");
2253
+ if (seenRoles.has(role)) fail(`The compatibility editor supports only one ${role} Track`);
2254
+ seenRoles.add(role);
2255
+ if (track.id !== void 0 && (track.id.trim() === "" || track.id.trim() !== track.id)) fail("Migration requires trimmed Track identities");
2256
+ for (const item of track.items ?? []) {
2257
+ if (item.part_id.trim() === "" || item.part_id.trim() !== item.part_id) fail("Migration requires trimmed Clip identities");
2258
+ if (placed.has(item.part_id)) fail(`Legacy part ${item.part_id} is placed more than once`);
2259
+ placed.set(item.part_id, {
2260
+ track,
2261
+ item
2262
+ });
2263
+ assertKnownFields(item.time_position, item.time_position.mode === "sequential" ? ["mode"] : [
2264
+ "mode",
2265
+ "anchorPartId",
2266
+ "offsetMs"
2267
+ ], "placement");
2268
+ if (item.time_position.mode === "absolute") requireMilliseconds(item.time_position.offsetMs, "absolute offset");
2269
+ if (item.time_position.mode === "anchored") {
2270
+ if (item.time_position.anchorPartId.trim() === "" || item.time_position.anchorPartId.trim() !== item.time_position.anchorPartId) fail("Migration requires a trimmed anchor identity");
2271
+ requireMilliseconds(item.time_position.offsetMs, "anchor offset");
2272
+ }
2273
+ if (item.fallback_abs_ms !== void 0) requireMilliseconds(item.fallback_abs_ms, "anchor fallback");
2274
+ assertKnownFields(item, [
2275
+ "part_id",
2276
+ "time_position",
2277
+ "fallback_abs_ms"
2278
+ ], "TrackItem");
2279
+ }
2280
+ assertKnownFields(track, [
2281
+ "id",
2282
+ "parts_kind",
2283
+ "is_hidden",
2284
+ "items"
2285
+ ], "Track");
2286
+ }
2287
+ for (const [id, part] of Object.entries(document.part_library ?? {})) {
2288
+ const placement = placed.get(id);
2289
+ if (placement === void 0) fail(`Cannot discard unplaced legacy part ${id}`);
2290
+ assertPartSupported(id, part, placement.track.parts_kind);
2291
+ }
2292
+ for (const id of placed.keys()) if (document.part_library?.[id] === void 0) fail(`Placed Clip ${id} has no part payload`);
2293
+ assertLegacyRelations(document, placed);
202
2294
  }
203
- function parseSseFrame(frame) {
204
- let event = null;
205
- const data = [];
206
- for (const line of frame.split(/\r?\n/)) if (line.startsWith("event:")) event = line.slice(6).trim();
207
- else if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
208
- return {
209
- event,
210
- data
211
- };
2295
+ function assertPartSupported(id, part, role) {
2296
+ assertKnownFields(part, [
2297
+ "video_clip",
2298
+ "speech",
2299
+ "caption",
2300
+ "bgm"
2301
+ ], "Part");
2302
+ const present = [
2303
+ "video_clip",
2304
+ "speech",
2305
+ "caption",
2306
+ "bgm"
2307
+ ].filter((kind) => part[kind] !== void 0);
2308
+ if (present.length !== 1 || present[0] !== role) fail(`Legacy part ${id} must match its ${role} Track`);
2309
+ if (part.video_clip !== void 0) {
2310
+ const clip = part.video_clip;
2311
+ 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");
2312
+ requireMilliseconds(clip.play_in, "Clip play_in");
2313
+ requireMilliseconds(clip.play_out, "Clip play_out");
2314
+ if (clip.play_out <= clip.play_in) fail("Migration requires a non-empty Clip trim window");
2315
+ requireFinite(clip.volume, "Clip volume");
2316
+ const speed = clip.speed_shift;
2317
+ 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");
2318
+ if (speed !== void 0) {
2319
+ const rate = speed.config?.linear?.speed;
2320
+ if (typeof rate !== "number" || !Number.isFinite(rate) || rate <= 0) fail("Migration requires a positive finite speed");
2321
+ assertKnownFields(speed, [
2322
+ "category",
2323
+ "mode",
2324
+ "config"
2325
+ ], "speed_shift");
2326
+ assertKnownFields(speed.config, ["linear"], "speed config");
2327
+ assertKnownFields(speed.config.linear, ["speed"], "linear speed");
2328
+ }
2329
+ assertKnownFields(clip, [
2330
+ "id",
2331
+ "kind",
2332
+ "origin_media_id",
2333
+ "play_in",
2334
+ "play_out",
2335
+ "volume",
2336
+ "speed_shift"
2337
+ ], "Clip");
2338
+ } else if (part.speech !== void 0) {
2339
+ const speech = part.speech;
2340
+ const phonemeScript = speech.phoneme_script;
2341
+ 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");
2342
+ requirePositiveMilliseconds(speech.media_duration_ms, "Speech duration");
2343
+ requireFinite(speech.volume, "Speech volume");
2344
+ if (!trimmed(speech.voice?.id)) fail("Migration requires an explicit voice-library identity");
2345
+ if (speech.voice?.name !== void 0 && typeof speech.voice.name !== "string") fail("Migration requires a string voice name when present");
2346
+ if (!Array.isArray(speech.caption_ids) || speech.caption_ids.some((captionId) => !trimmed(captionId))) fail("Migration requires explicit Speech caption identities");
2347
+ if (new Set(speech.caption_ids).size !== speech.caption_ids.length) fail("Migration requires unique Speech caption identities");
2348
+ if (phonemeScript !== void 0 && phonemeScript !== null && typeof phonemeScript !== "string") fail("Migration requires a string phoneme_script when present");
2349
+ assertKnownFields(speech.voice, ["id", "name"], "voice");
2350
+ assertKnownFields(speech, [
2351
+ "id",
2352
+ "kind",
2353
+ "media_duration_ms",
2354
+ "audio_script",
2355
+ "volume",
2356
+ "audio_storage_key",
2357
+ "origin_speech_id",
2358
+ "voice",
2359
+ "caption_ids",
2360
+ "phoneme_script"
2361
+ ], "Speech");
2362
+ } else if (part.caption !== void 0) {
2363
+ const caption = part.caption;
2364
+ 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");
2365
+ requireMilliseconds(caption.start_ms, "Caption source offset");
2366
+ requirePositiveMilliseconds(caption.initial_duration_ms, "Caption duration");
2367
+ if (caption.style !== void 0) assertCaptionStyle(caption.style);
2368
+ assertKnownFields(caption, [
2369
+ "id",
2370
+ "kind",
2371
+ "initial_duration_ms",
2372
+ "speech_part_id",
2373
+ "text",
2374
+ "start_ms",
2375
+ "style"
2376
+ ], "Caption");
2377
+ } else if (part.bgm !== void 0) {
2378
+ const bgm = part.bgm;
2379
+ 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");
2380
+ requireFinite(bgm.volume, "BGM volume");
2381
+ assertKnownFields(bgm, [
2382
+ "id",
2383
+ "kind",
2384
+ "audio_storage_key",
2385
+ "volume",
2386
+ "origin_media_id"
2387
+ ], "BGM");
2388
+ }
212
2389
  }
213
- function findSseFrameBoundary(buffer) {
214
- const lf = buffer.indexOf("\n\n");
215
- const crlf = buffer.indexOf("\r\n\r\n");
216
- if (lf < 0) return crlf;
217
- if (crlf < 0) return lf;
218
- return Math.min(lf, crlf);
2390
+ function assertLegacyRelations(document, placed) {
2391
+ const captionIdsBySpeech = /* @__PURE__ */ new Map();
2392
+ for (const track of document.tracks ?? []) {
2393
+ if (track.parts_kind === "bgm" && (track.items?.length ?? 0) > 1) fail("The compatibility editor supports only one BGM Clip");
2394
+ for (const item of track.items ?? []) {
2395
+ const part = requirePart(document, item.part_id);
2396
+ const position = item.time_position;
2397
+ if (track.parts_kind === "video_clip" && position.mode === "anchored") fail("Anchored video Clips require an overlay adapter");
2398
+ if (track.parts_kind === "bgm") {
2399
+ if (position.mode !== "absolute" || position.offsetMs !== 0) fail("Legacy BGM must start at the Timeline origin");
2400
+ } else if (position.mode === "anchored") {
2401
+ const host = placed.get(position.anchorPartId);
2402
+ if (host === void 0) fail(`Legacy anchor ${position.anchorPartId} is not placed`);
2403
+ if (track.parts_kind === "speech" && host.track.parts_kind !== "video_clip") fail("Speech may only anchor to a video Clip");
2404
+ if (track.parts_kind === "caption") {
2405
+ if (host.track.parts_kind !== "speech") fail("Caption may only anchor to a Speech Clip");
2406
+ const caption = part.caption;
2407
+ if (position.anchorPartId !== caption.speech_part_id || position.offsetMs !== caption.start_ms) fail("Caption placement must agree with its Speech provenance and source offset");
2408
+ }
2409
+ } else if (track.parts_kind === "caption") fail("Caption placement requires an explicit Speech anchor");
2410
+ if (part.caption !== void 0) {
2411
+ const ids = captionIdsBySpeech.get(part.caption.speech_part_id) ?? [];
2412
+ ids.push(item.part_id);
2413
+ captionIdsBySpeech.set(part.caption.speech_part_id, ids);
2414
+ }
2415
+ }
2416
+ }
2417
+ for (const [id, placement] of placed) {
2418
+ const speech = requirePart(document, id).speech;
2419
+ if (speech === void 0) continue;
2420
+ const expected = captionIdsBySpeech.get(id) ?? [];
2421
+ if (canonical(speech.caption_ids) !== canonical(expected)) fail(`Speech ${id} caption_ids must agree with the Caption Track order`);
2422
+ if (placement.item.time_position.mode === "anchored") continue;
2423
+ if (placement.item.time_position.mode !== "absolute") fail("Speech placement must be anchored or absolute");
2424
+ }
2425
+ const intervals = (fromVideoDocument(document).main_track?.items ?? []).map((item) => {
2426
+ const clip = document.part_library?.[item.part_id]?.video_clip;
2427
+ if (clip === void 0) fail("Projection must preserve every video Clip");
2428
+ return {
2429
+ start: item.abs_time_position ?? 0,
2430
+ end: (item.abs_time_position ?? 0) + effectiveVideoClipDurationMs(clip)
2431
+ };
2432
+ }).sort((left, right) => left.start - right.start);
2433
+ if (intervals.some((interval, index) => index > 0 && interval.start < intervals[index - 1].end)) fail("Overlapping video Clips require an overlay adapter");
219
2434
  }
220
- //#endregion
221
- //#region src/editor/id-gen.ts
222
2435
  /**
223
- * Part-id generation, aligned with the online ecosystem.
224
- *
225
- * The authoritative online producers — agent-harness (`@harness/shared`
226
- * `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
227
- * ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
228
- * shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
229
- * previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
230
- * different encoding — the sole cross-repo id divergence. This module removes it
231
- * by emitting the same `<prefix>_<ULID>` bytes.
232
- *
233
- * The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
234
- * rather than pulling the `ulid` npm package: the randomness class matches the
235
- * old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
236
- * dependency-free for a purely mechanical id string. Part ids only need to be
237
- * unique and lexicographically time-sortable, which this satisfies.
2436
+ * Legacy reads perform one speech-overlap pass after laying the visual lane out
2437
+ * sequentially. Entity reads deliberately do not run that hidden cascade. When
2438
+ * the pass moved any visual Clip, cutover therefore records the complete solved
2439
+ * visual lane as explicit target ranges. Mixing absolute and sequential Clips
2440
+ * would create two unrelated placement chains and could reorder the lane.
238
2441
  */
239
- /** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
240
- const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
241
- const TIME_LEN = 10;
242
- const RANDOM_LEN = 16;
243
- function encodeTime(now) {
244
- let out = "";
245
- let ms = now;
246
- for (let i = TIME_LEN - 1; i >= 0; i--) {
247
- const mod = ms % 32;
248
- out = CROCKFORD[mod] + out;
249
- ms = (ms - mod) / 32;
2442
+ function overlapNormalizedMainRanges(document) {
2443
+ const track = document.tracks?.find((candidate) => candidate.parts_kind === "video_clip");
2444
+ if (track === void 0 || (track.items?.length ?? 0) === 0) return void 0;
2445
+ const solved = fromVideoDocument(document).main_track?.items ?? [];
2446
+ const starts = new Map(solved.map((item) => [item.part_id, requireMilliseconds(item.abs_time_position, `Solved position for ${item.part_id ?? "unknown Clip"}`)]));
2447
+ let cursor = 0;
2448
+ let moved = false;
2449
+ const ranges = /* @__PURE__ */ new Map();
2450
+ for (const item of track.items ?? []) {
2451
+ const part = requirePart(document, item.part_id).video_clip;
2452
+ if (part === void 0) fail(`Projection must preserve video Clip ${item.part_id}`);
2453
+ const duration = effectiveVideoClipDurationMs(part);
2454
+ const start = starts.get(item.part_id);
2455
+ if (start === void 0) fail(`Projection must preserve video Clip ${item.part_id}`);
2456
+ if (start !== cursor) moved = true;
2457
+ const end = start + duration;
2458
+ requireMilliseconds(end, `Solved end for ${item.part_id}`);
2459
+ ranges.set(item.part_id, {
2460
+ start,
2461
+ end
2462
+ });
2463
+ cursor += duration;
250
2464
  }
251
- return out;
2465
+ return moved ? ranges : void 0;
252
2466
  }
253
- function encodeRandom() {
254
- let out = "";
255
- for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
256
- return out;
2467
+ function requirePart(document, partId) {
2468
+ const part = document.part_library?.[partId];
2469
+ if (part === void 0) fail(`Placed Clip ${partId} has no part payload`);
2470
+ return part;
257
2471
  }
258
- /** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
259
- function ulid() {
260
- return encodeTime(Date.now()) + encodeRandom();
2472
+ function ensureSpeechPhoneticChain(voiceEntityId, audioScriptText, phonemeScript, entities, relations, addEntity, link) {
2473
+ const renders = relations.filter((relation) => relation.relationKind === "phonetic-script-render" && relation.endpoint0EntityId === voiceEntityId);
2474
+ if (renders.length > 1) fail(`Voice ${voiceEntityId} has ambiguous PhoneticScript provenance`);
2475
+ if (renders.length === 1) {
2476
+ const phonetic = entities.find((entity) => entity.entityId === renders[0].endpoint1EntityId && entity.entityKind === "phonetic-script");
2477
+ if (phonetic === void 0) fail(`Voice ${voiceEntityId} has a dangling PhoneticScript render`);
2478
+ const script = composedAudioScript(phonetic, entities);
2479
+ if (script === void 0 || scriptText(script) !== audioScriptText) fail(`Voice ${voiceEntityId} conflicts with the legacy Speech script`);
2480
+ return phonetic.entityId;
2481
+ }
2482
+ const phonetic = addEntity("phonetic-script", {
2483
+ baseEntityIds: [addEntity("audio-script", { segments: [{
2484
+ segmentId: "segment-0",
2485
+ text: audioScriptText
2486
+ }] })],
2487
+ ...phonemeScript === void 0 ? {} : { phonemeScript }
2488
+ });
2489
+ link("phonetic-script-render", voiceEntityId, phonetic);
2490
+ return phonetic;
261
2491
  }
262
- function generatePartId(prefix) {
263
- return `${prefix}_${ulid()}`;
2492
+ function composedAudioScript(phonetic, entities) {
2493
+ return findComposedAudioScript({
2494
+ entities,
2495
+ relations: []
2496
+ }, phonetic.entityId, "phonetic-script");
2497
+ }
2498
+ function scriptText(script) {
2499
+ const segments = script.payload.segments;
2500
+ if (!Array.isArray(segments)) fail("AudioScript requires segments");
2501
+ return segments.map((segment) => {
2502
+ if (segment === null || typeof segment !== "object" || Array.isArray(segment) || typeof segment.text !== "string") fail("AudioScript segment requires text");
2503
+ return segment.text;
2504
+ }).join("");
2505
+ }
2506
+ function toEntityCaptionStyle(style) {
2507
+ return {
2508
+ ...style.font_id === void 0 ? {} : { font: {
2509
+ system: "font-library",
2510
+ key: style.font_id
2511
+ } },
2512
+ ...style.font_size === void 0 ? {} : { fontSize: style.font_size },
2513
+ ...style.font_color === void 0 ? {} : { fontColor: style.font_color },
2514
+ ...style.font_weight === void 0 ? {} : { fontWeight: style.font_weight },
2515
+ ...style.entrance_animation === void 0 ? {} : { entranceAnimation: style.entrance_animation },
2516
+ ...style.entrance_animation_duration_ms === void 0 ? {} : { entranceAnimationDurationMs: style.entrance_animation_duration_ms },
2517
+ ...style.stroke_color === void 0 ? {} : { strokeColor: style.stroke_color },
2518
+ ...style.stroke_width === void 0 ? {} : { strokeWidth: style.stroke_width },
2519
+ ...style.position_x === void 0 ? {} : { positionX: style.position_x },
2520
+ ...style.position_y === void 0 ? {} : { positionY: style.position_y }
2521
+ };
2522
+ }
2523
+ function assertCaptionStyle(style) {
2524
+ if (style.font_id !== void 0 && !trimmed(style.font_id)) fail("Caption font_id must be a trimmed identity");
2525
+ for (const value of [
2526
+ style.font_size,
2527
+ style.font_weight,
2528
+ style.entrance_animation_duration_ms,
2529
+ style.stroke_width,
2530
+ style.position_x,
2531
+ style.position_y
2532
+ ]) if (value !== void 0) requireFinite(value, "Caption style number");
2533
+ for (const value of [
2534
+ style.font_color,
2535
+ style.entrance_animation,
2536
+ style.stroke_color
2537
+ ]) if (value !== void 0 && typeof value !== "string") fail("Caption style text must be a string");
2538
+ assertKnownFields(style, [
2539
+ "font_id",
2540
+ "font_size",
2541
+ "font_color",
2542
+ "font_weight",
2543
+ "entrance_animation",
2544
+ "entrance_animation_duration_ms",
2545
+ "stroke_color",
2546
+ "stroke_width",
2547
+ "position_x",
2548
+ "position_y"
2549
+ ], "Caption style");
2550
+ }
2551
+ function isRangeStartingAt(value, expectedStart) {
2552
+ return value !== null && typeof value === "object" && !Array.isArray(value) && value.start === expectedStart;
2553
+ }
2554
+ function isExactRange(value, expected) {
2555
+ return value !== null && typeof value === "object" && !Array.isArray(value) && value.start === expected.start && value.end === expected.end;
2556
+ }
2557
+ function requireFinite(value, label) {
2558
+ if (typeof value !== "number" || !Number.isFinite(value)) fail(`${label} must be finite`);
2559
+ return value;
2560
+ }
2561
+ function requireMilliseconds(value, label) {
2562
+ const result = requireFinite(value, label);
2563
+ if (!Number.isSafeInteger(result) || result < 0) fail(`${label} must be non-negative whole milliseconds`);
2564
+ return result;
2565
+ }
2566
+ function requirePositiveMilliseconds(value, label) {
2567
+ const result = requireMilliseconds(value, label);
2568
+ if (result <= 0) fail(`${label} must be positive`);
2569
+ return result;
2570
+ }
2571
+ function trimmed(value) {
2572
+ return typeof value === "string" && value.trim() !== "" && value.trim() === value;
2573
+ }
2574
+ function assertKnownFields(value, fields, label) {
2575
+ if (Object.entries(value).some(([key, field]) => field !== void 0 && !fields.includes(key))) fail(`Unknown legacy ${label} fields require an explicit migration adapter`);
2576
+ }
2577
+ function canonical(value) {
2578
+ if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
2579
+ 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(",")}}`;
2580
+ return JSON.stringify(value) ?? "undefined";
2581
+ }
2582
+ function fail(message) {
2583
+ throw new EntityTimelineProjectionError(message);
264
2584
  }
265
2585
  //#endregion
266
- //#region src/editor/schemas/shared.ts
267
- const clipIdSchema = z.string().min(1).describe("The clip part ID on the timeline");
268
- const clipIdsSchema = z.array(clipIdSchema).min(1).refine((ids) => new Set(ids).size === ids.length, { message: "Duplicate clip IDs are not allowed" }).describe("List of clip part IDs (no duplicates allowed)");
269
- const mediaIdSchema = z.string().min(1).describe("The media asset ID");
270
- const speechIdSchema = z.string().min(1).describe("The speech part ID on the timeline");
271
- const timelineMsSchema = z.number().int().min(0).describe("Time position in milliseconds on the timeline (>= 0)");
272
- const positiveMsSchema = z.number().int().positive().describe("Duration in milliseconds (> 0)");
273
- const volumeSchema = z.number().min(-60).max(20).describe("Volume in decibels (-60.0 to 20.0; 0.0 = original, -60 = mute, +20 = max)");
274
- const speechIdsSchema = z.array(speechIdSchema).min(1).refine((ids) => new Set(ids).size === ids.length, { message: "Duplicate speech IDs are not allowed" }).describe("List of speech part IDs (no duplicates allowed)");
275
- const tangentHandleSchema = z.object({
276
- x: z.number().finite(),
277
- y: z.number().finite()
278
- }).describe("Bezier tangent handle (x, y)");
279
- const speedKeyframeSchema = z.object({
280
- position: z.number().min(0).max(1),
281
- rate: z.number().min(0),
282
- in_tangent: tangentHandleSchema.optional(),
283
- out_tangent: tangentHandleSchema.optional()
284
- }).describe("A speed keyframe: normalized position (0..1), rate, optional tangents");
285
- /**
286
- * A clip's playback-speed fact, the only thing `SetVideoClipSpeedShift` writes.
287
- * Mirrors the IDL `SpeedShift`: `category` is `linear` | `curve`, and `config`
288
- * is a discriminated union — `{ linear: { speed } }` for a constant multiplier
289
- * (the multiplier projection reads at `config.linear.speed`) or `{ curve: {
290
- * keyframes } }` for a Bezier-controlled variable speed (RFC 02 / `reference/16`
291
- * §0). Exactly one of `linear` / `curve` is present.
292
- */
293
- const speedShiftSchema = z.object({
294
- category: z.enum(["linear", "curve"]),
295
- mode: z.string(),
296
- config: z.union([z.object({ linear: z.object({ speed: z.number().finite().positive() }) }), z.object({ curve: z.object({ keyframes: z.array(speedKeyframeSchema).min(2) }) })])
297
- }).describe("Speed shift: linear multiplier or Bezier curve, mirroring the IDL shape");
298
- const voiceSchema = z.object({
299
- id: z.string().min(1),
300
- name: z.string()
301
- }).describe("TTS voice summary attached to a speech");
302
- //#endregion
303
- //#region src/editor/schemas/speech-assets.ts
304
- /**
305
- * The materialized TTS result shared by `AddSpeeches` / `ChangeSpeechScript` /
306
- * `ChangeSpeechVoice` (see `results/phase-4-side-effect-payload-contract.md`
307
- * §1/§2). The side effect (TTS/ASR + billing) runs upstream; the op receives the
308
- * stable speech + caption parts and writes them as authoritative facts. No
309
- * cascade runs on write — the projection derives absolute positions on read.
310
- *
311
- * Each speech carries the anchoring fact directly (RFC 02 §4): the host video
312
- * clip `anchor_part_id` and the `offset_ms` within it. The upstream caller
313
- * already knows which clip a speech attaches to, so the op writes
314
- * `{ mode:'anchored', anchorPartId, offsetMs }` verbatim — no write-time
315
- * host-picking. Captions anchor to their speech via the caption part's
316
- * `start_ms` (offset within the speech).
317
- */
318
- const speechAssetSchema = z.object({
319
- speech_id: speechIdSchema.describe("The speech part ID (= side-effect speech_parts[].id)"),
320
- anchor_part_id: clipIdSchema.describe("Host video clip part ID the speech anchors to (RFC 02 §4)"),
321
- offset_ms: timelineMsSchema.describe("Offset within the host clip (speech.abs = host.abs + offset_ms)"),
322
- audio_storage_key: z.string().min(1),
323
- duration_ms: positiveMsSchema,
324
- audio_script: z.string(),
325
- volume: volumeSchema,
326
- voice: voiceSchema,
327
- origin_speech_id: z.string().min(1),
328
- caption_ids: z.array(z.string().min(1)).describe("Caption part IDs owned by this speech")
329
- });
330
- const captionAssetSchema = z.object({
331
- caption_id: z.string().min(1).describe("The caption part ID (= side-effect created_caption_parts[].id)"),
332
- speech_part_id: speechIdSchema.describe("The owning speech part ID"),
333
- text: z.string(),
334
- start_ms: timelineMsSchema.describe("Offset within the host speech (caption.abs = speech.abs + start_ms)"),
335
- duration_ms: positiveMsSchema
336
- });
337
- /** A materialized speech-subtree write (speeches + their captions). */
338
- const speechAssetsSchema = z.object({
339
- speeches: z.array(speechAssetSchema).min(1).describe("Materialized speech parts to write"),
340
- captions: z.array(captionAssetSchema).describe("Materialized caption parts owned by the speeches")
341
- });
342
- //#endregion
343
- //#region src/editor/schemas/add-speeches.ts
344
- /**
345
- * Add speeches (and their captions). TTS runs upstream; the stable speech /
346
- * caption parts arrive materialized (see `speech-assets.ts`). The op writes the
347
- * parts and each speech's `{ mode:'anchored', anchorPartId, offsetMs }` fact
348
- * verbatim — no write-time host-picking, no cascade (RFC 02 §4). The projection
349
- * derives absolute positions on read.
350
- */
351
- const addSpeechesInputSchema = speechAssetsSchema.describe("Materialized speeches + captions to add");
352
- //#endregion
353
- //#region src/editor/schemas/add-video-clips.ts
354
- /**
355
- * Add video clips to a track. Each clip's duration facts are separated so a
356
- * single number is never overloaded (RFC 02 / `reference/16` §0b):
357
- *
358
- * - `media_duration_ms` is the source media's intrinsic full length (a resource
359
- * fact, written to the part);
360
- * - `play_in` / `play_out` are the optional trim window into that media; when
361
- * omitted the whole media is used (`play_in=0`, `play_out=media_duration_ms`).
362
- *
363
- * The clip's effective timeline duration is derived by the projection from the
364
- * trim window and `speed_shift` — it is never an input here.
365
- */
366
- const addVideoClipsInputSchema = z.object({
367
- clips: z.array(z.object({
368
- media_id: mediaIdSchema.describe("The media asset ID for the video clip"),
369
- start_ms: timelineMsSchema.optional().describe("Absolute start time in milliseconds on the timeline"),
370
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
371
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
372
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)"),
373
- track_id: z.string().min(1).optional().describe("Target track ID (optional, defaults to main track)")
374
- })).min(1).describe("List of video clips to create"),
375
- before_clip_id: z.string().min(1).optional().describe("Insert new clips before this clip ID"),
376
- after_clip_id: z.string().min(1).optional().describe("Insert new clips after this clip ID")
377
- }).superRefine((data, ctx) => {
378
- if (data.before_clip_id != null && data.after_clip_id != null) {
379
- ctx.addIssue({
380
- code: z.ZodIssueCode.custom,
381
- message: "Cannot provide both before_clip_id and after_clip_id"
382
- });
383
- return;
384
- }
385
- const hasRelative = data.before_clip_id != null || data.after_clip_id != null;
386
- for (let i = 0; i < data.clips.length; i++) {
387
- const clip = data.clips[i];
388
- if (hasRelative && clip.start_ms != null) ctx.addIssue({
389
- code: z.ZodIssueCode.custom,
390
- message: `clips[${i}].start_ms must not be provided when using before_clip_id or after_clip_id`,
391
- path: [
392
- "clips",
393
- i,
394
- "start_ms"
395
- ]
396
- });
397
- if (!hasRelative && clip.start_ms == null) ctx.addIssue({
398
- code: z.ZodIssueCode.custom,
399
- message: `clips[${i}].start_ms is required when not using relative positioning`,
400
- path: [
401
- "clips",
402
- i,
403
- "start_ms"
404
- ]
405
- });
406
- const playIn = clip.play_in ?? 0;
407
- const playOut = clip.play_out ?? clip.media_duration_ms;
408
- if (playOut > clip.media_duration_ms) ctx.addIssue({
409
- code: z.ZodIssueCode.custom,
410
- message: `clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
411
- path: [
412
- "clips",
413
- i,
414
- "play_out"
415
- ]
416
- });
417
- if (playIn >= playOut) ctx.addIssue({
418
- code: z.ZodIssueCode.custom,
419
- message: `clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
420
- path: [
421
- "clips",
422
- i,
423
- "play_in"
424
- ]
425
- });
426
- }
427
- });
428
- //#endregion
429
- //#region src/editor/schemas/adjust-bgm-volume.ts
430
- const adjustBgmVolumeInputSchema = z.object({ bgm: z.array(z.object({
431
- bgm_id: clipIdSchema.describe("The bgm part ID to adjust volume for"),
432
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
433
- })).min(1).describe("List of bgm parts with their new volume settings") });
434
- //#endregion
435
- //#region src/editor/schemas/adjust-speech-volume.ts
436
- const adjustSpeechVolumeInputSchema = z.object({ speeches: z.array(z.object({
437
- speech_id: speechIdSchema.describe("The speech part ID to adjust volume for"),
438
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
439
- })).min(1).describe("List of speeches with their new volume settings") });
440
- //#endregion
441
- //#region src/editor/schemas/adjust-video-clip-duration.ts
442
- /**
443
- * Re-trim existing video clips (the user-facing "adjust duration" gesture is a
444
- * trim of the source window). The new `play_in` / `play_out` are the facts; the
445
- * effective timeline duration is derived from them and the clip's `speed_shift`,
446
- * and the change reflows downstream clips, speeches, and the timeline inside the
447
- * op's transaction (no caller-materialized cascade).
448
- */
449
- const adjustVideoClipDurationInputSchema = z.object({ clips: z.array(z.object({
450
- clip_id: clipIdSchema.describe("The video clip part ID to re-trim"),
451
- play_in: timelineMsSchema.describe("New trim window start in the source media"),
452
- play_out: positiveMsSchema.describe("New trim window end in the source media")
453
- })).min(1).describe("Video clips with their new trim windows") });
454
- //#endregion
455
- //#region src/editor/schemas/adjust-video-clip-volume.ts
456
- const adjustVideoClipVolumeInputSchema = z.object({ clips: z.array(z.object({
457
- clip_id: clipIdSchema.describe("The video clip part ID to adjust volume for"),
458
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
459
- })).min(1).describe("List of video clips with their new volume settings") });
460
- //#endregion
461
- //#region src/editor/schemas/change-speech.ts
462
- /**
463
- * Change a speech's script or voice. Both re-run TTS upstream and return the
464
- * regenerated speech / caption parts in the same materialized shape as
465
- * `AddSpeeches` (`speech-assets.ts`); the op upserts them by id (the speech part
466
- * id is preserved across a re-TTS), re-seats at `start_ms`, and reflows. Old
467
- * caption parts no longer owned by the speech are removed via `caption_ids`.
468
- */
469
- const changeSpeechScriptInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new script)");
470
- const changeSpeechVoiceInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new voice)");
471
- //#endregion
472
- //#region src/editor/schemas/delete-bgm.ts
473
- /**
474
- * Remove the document BGM. Pure document edit: clears the bgm lane and removes
475
- * the bgm part. Takes no input (a document holds at most one bgm); an empty
476
- * object keeps the op signature uniform with the rest.
477
- */
478
- const deleteBgmInputSchema = z.object({}).describe("Remove the document BGM (no parameters)");
479
- //#endregion
480
- //#region src/editor/schemas/delete-speeches.ts
481
- /**
482
- * Delete speeches with their captions. Pure document edit (no side effect): the
483
- * op removes each speech part, cascade-deletes the captions it owns (via
484
- * `caption_ids` / `speech_part_id`), drops their track items, and reflows.
485
- */
486
- const deleteSpeechesInputSchema = z.object({ speech_ids: speechIdsSchema.describe("Speech part IDs to delete (their captions cascade-delete)") });
487
- //#endregion
488
- //#region src/editor/schemas/delete-video-clips.ts
489
- /**
490
- * How a delete handles the anchored subtree (speeches anchored to a deleted clip,
491
- * and their captions) — a delete-op policy, not a data-model field (reference/17
492
- * §6). `cascade` (default) removes the subtree; `detach` keeps the direct
493
- * anchored children, re-pinning them to `absolute` so they stay on the timeline.
494
- */
495
- const anchoredDeletePolicySchema = z.enum(["cascade", "detach"]);
496
- const deleteVideoClipsInputSchema = z.object({
497
- clip_ids: clipIdsSchema.describe("List of video clip part IDs to delete from the main track"),
498
- on_anchored: anchoredDeletePolicySchema.optional().describe("How to treat anchored children (default cascade)")
499
- });
500
- //#endregion
501
- //#region src/editor/schemas/move-speeches.ts
502
- /**
503
- * Move speeches in time. Pure document edit: the op re-seats each speech at its
504
- * new absolute `start_ms`; the cascade reassigns it to the host video clip,
505
- * resolves overlaps, and reflows. Captions follow their speech.
506
- */
507
- const moveSpeechesInputSchema = z.object({ speeches: z.array(z.object({
508
- speech_id: speechIdSchema.describe("The speech part ID to move"),
509
- new_start_ms: timelineMsSchema.describe("New absolute start time on the timeline")
510
- })).min(1).describe("Speeches to move to new positions") });
511
- //#endregion
512
- //#region src/editor/schemas/move-video-clips-by-anchor.ts
513
- /**
514
- * Where the moved block lands on the main track.
515
- *
516
- * A discriminated union rather than two optional `before_clip_id` /
517
- * `after_clip_id` fields (the shape `addVideoClips` had to use, because there the
518
- * two modes share a whole clip description): here the alternatives carry nothing
519
- * in common, so making them mutually exclusive *by type* removes three runtime
520
- * `superRefine` checks that would otherwise have to be written and tested.
521
- *
522
- * `track_start` is an explicit member, not the absence of an anchor. The
523
- * agent-harness mutation this maps from treats "neither anchor given" as
524
- * "move to the front" (`applyBatchMoveVideoClips` falls back to `insertIndex = 0`),
525
- * which is a default buried in a tool description. Requiring the caller to name
526
- * that intent keeps a forgotten field from silently reordering the timeline.
527
- */
528
- const moveAnchorSchema = z.discriminatedUnion("position", [
529
- z.object({
530
- position: z.literal("before"),
531
- clip_id: clipIdSchema.describe("The moved block lands immediately before this clip")
532
- }),
533
- z.object({
534
- position: z.literal("after"),
535
- clip_id: clipIdSchema.describe("The moved block lands immediately after this clip")
536
- }),
537
- z.object({ position: z.literal("track_start") })
538
- ]).describe("Where the moved block lands: before/after a reference clip, or at the head of the track");
539
- /**
540
- * What happens to the speeches anchored to the clips being moved.
541
- *
542
- * - `follow` keeps each speech anchored where it is, so it travels with its clip
543
- * to the new position. In the anchored model this is the *no-op* branch: a
544
- * speech's authoritative fact is `{ anchorPartId, offsetMs }` and its absolute
545
- * time is derived on read from the host's position, so moving the host moves
546
- * the speech with no write to the speech at all.
547
- * - `keep_absolute` preserves each speech's current absolute landing instead, then
548
- * re-anchors it to whichever clip now covers that time (RFC 02 §9.1/§11.1). This
549
- * is the branch that costs an extra pass, and the one the FE timeline uses.
550
- *
551
- * **Required, with no default**, matching `deleteVideoClips` and
552
- * `replaceVideoClipSequence`. The two branches decide which picture the user's
553
- * narration ends up over, which is too consequential to infer from a missing
554
- * field — and neither branch is "safe enough" to be the implicit one.
555
- */
556
- const movedClipAnchoredPolicySchema = z.enum(["follow", "keep_absolute"]);
557
- /**
558
- * Reorder a set of main-track clips relative to a reference clip.
559
- *
560
- * Distinct from `moveVideoClips`, which positions clips by absolute time
561
- * (`new_start_ms`) and is what the FE timeline dispatches after a drag. Main-track
562
- * clips are `sequential`-positioned, so absolute time is not authoritative state
563
- * there (RFC 02 §4/§7): `moveVideoClips` has to *guess* an index back out of the
564
- * time it was handed, whereas an anchor already is the ordinal fact being changed.
565
- * Keeping them separate also isolates blast radius — this method can diverge on
566
- * speech policy without touching the FE path.
567
- *
568
- * `clip_ids` need not be contiguous. They move as one block, keeping their
569
- * relative order, which is the agent-harness `batch_move_video_clips` contract.
570
- */
571
- const moveVideoClipsByAnchorInputSchema = z.object({
572
- clip_ids: clipIdsSchema.describe("Clips to move as one block, keeping their relative order. Need not be contiguous on the track."),
573
- anchor: moveAnchorSchema,
574
- on_anchored: movedClipAnchoredPolicySchema.describe("What happens to speeches anchored to the moved clips (required — see the policy doc)")
575
- });
576
- //#endregion
577
- //#region src/editor/schemas/move-video-clips.ts
578
- const moveVideoClipsInputSchema = z.object({ clips: z.array(z.object({
579
- clip_id: clipIdSchema.describe("The video clip part ID to move"),
580
- new_start_ms: timelineMsSchema.describe("New absolute start time in milliseconds on the timeline"),
581
- new_track_id: z.string().min(1).optional().describe("Target track ID to move the clip to (optional)")
582
- })).min(1).describe("List of video clips to move to new positions") });
583
- //#endregion
584
- //#region src/editor/schemas/replace-video-clip-content.ts
585
- /**
586
- * Replace the media backing existing video clips. The media import runs upstream
587
- * (Director); its stable result — the new media id, intrinsic length, and the
588
- * reset trim window — arrives materialized (see
589
- * `results/phase-4-side-effect-payload-contract.md` §4). Director resets
590
- * `play_in=0` / `play_out=media_duration_ms` and clears `speed_shift` on
591
- * replacement. The clip `part_id`s (hence their track items) are unchanged; the
592
- * editor reflows the main track from the new effective durations.
593
- */
594
- const replaceVideoClipContentInputSchema = z.object({ clips: z.array(z.object({
595
- clip_id: clipIdSchema.describe("Existing video clip part ID to re-point"),
596
- origin_media_id: mediaIdSchema.describe("The new media asset ID"),
597
- media_duration_ms: positiveMsSchema.describe("The new media's intrinsic full length"),
598
- play_in: timelineMsSchema.describe("Trim window start in the new media (usually 0)"),
599
- play_out: positiveMsSchema.describe("Trim window end in the new media (usually = media_duration_ms)"),
600
- volume: volumeSchema
601
- })).min(1).describe("Video clips whose media is being replaced") });
602
- //#endregion
603
- //#region src/editor/schemas/replace-video-clip-sequence.ts
604
- /**
605
- * What happens to the speeches anchored to the clips being replaced.
606
- *
607
- * - `remap` re-anchors each surviving speech to the new clip in the SAME POSITION
608
- * of the sequence, keeping its offset — old[i]'s children become new[i]'s
609
- * children. An old clip with no counterpart (fewer new clips than old) has its
610
- * subtree deleted, because there is nothing left to anchor to.
611
- * - `cascade` deletes every anchored speech (and its captions) outright, like
612
- * `deleteVideoClips`.
613
- *
614
- * **Required, with no default.** The two branches differ in whether the user's
615
- * narration survives, and the agent tool that drives this op makes its
616
- * `preserve_speeches` flag required for that reason. A default here would let a
617
- * caller that forgot the field silently delete speech.
618
- */
619
- const anchoredReplacePolicySchema = z.enum(["remap", "cascade"]);
620
- /**
621
- * Replace a contiguous run of main-track clips with a new run.
622
- *
623
- * A composite of delete + insert that cannot be expressed as the two ops in
624
- * sequence, because the anchored speeches have to survive *across* the swap: with
625
- * `remap` they are re-anchored positionally, which needs both the old and the new
626
- * ids in the same transaction (ADR 0009 — the cascade stays in the editor, callers
627
- * never re-wire anchors themselves).
628
- *
629
- * Duration facts follow `addVideoClips`: `media_duration_ms` is the source's
630
- * intrinsic length and the trim window defaults to the whole media. The effective
631
- * timeline duration is derived by the projection, never an input.
632
- *
633
- * `media_id` is optional: omitting it creates a **deliberate empty placeholder
634
- * clip** (`origin_media_id: ''`) — structure with no picture. This is the only op
635
- * that can produce one, and it is authoritative state, unlike the gap fillers the
636
- * read-side solve mints (which never enter the document).
637
- */
638
- const replaceVideoClipSequenceInputSchema = z.object({
639
- old_clip_ids: clipIdsSchema.describe("The clips being replaced: a contiguous main-track run, listed in timeline order"),
640
- new_clips: z.array(z.object({
641
- media_id: mediaIdSchema.optional().describe("The replacement media asset ID. Omit to create an empty placeholder clip."),
642
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
643
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
644
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)")
645
- })).min(1).describe("The replacement clips, in the order they take on the track"),
646
- on_anchored: anchoredReplacePolicySchema.describe("What happens to speeches anchored to the replaced clips (required — see the policy doc)")
647
- }).superRefine((data, ctx) => {
648
- for (let i = 0; i < data.new_clips.length; i++) {
649
- const clip = data.new_clips[i];
650
- const playIn = clip.play_in ?? 0;
651
- const playOut = clip.play_out ?? clip.media_duration_ms;
652
- if (playOut > clip.media_duration_ms) ctx.addIssue({
653
- code: z.ZodIssueCode.custom,
654
- message: `new_clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
655
- path: [
656
- "new_clips",
657
- i,
658
- "play_out"
659
- ]
660
- });
661
- if (playIn >= playOut) ctx.addIssue({
662
- code: z.ZodIssueCode.custom,
663
- message: `new_clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
664
- path: [
665
- "new_clips",
666
- i,
667
- "play_in"
668
- ]
669
- });
2586
+ //#region src/entity-editor/caption-content.ts
2587
+ /** Author Caption content from semantic intent; callers never reconcile variant bases or assembled fields. */
2588
+ function prepareCaptionContent(rows, input) {
2589
+ const entityId = createEntityId(input.contentEntityId);
2590
+ const scriptId = createEntityId(input.audioScriptEntityId);
2591
+ if (!Number.isFinite(input.durationMs) || input.durationMs <= 0) throw new Error("Caption duration must be positive");
2592
+ const previous = rows.entities.find((row) => row.entityId === entityId);
2593
+ if (previous !== void 0 && previous.entityKind !== "caption") throw new Error("Caption content identity has a different kind");
2594
+ let baseEntityIds = [scriptId];
2595
+ if (previous !== void 0) {
2596
+ const source = assembleCaptionContent(rows, entityId).audioScript.entityId;
2597
+ const bases = variantBaseEntityIds(previous);
2598
+ if (source !== scriptId && !bases.includes(source)) throw new Error("Replacing an indirectly composed AudioScript requires an explicit composition change");
2599
+ baseEntityIds = bases.map((id) => id === source ? scriptId : id);
2600
+ }
2601
+ const candidate = {
2602
+ entityId,
2603
+ entityKind: "caption",
2604
+ payload: {
2605
+ ...previous?.payload,
2606
+ baseEntityIds,
2607
+ extent: previous?.payload.extent ?? {
2608
+ kind: "bounded",
2609
+ start: 0,
2610
+ end: input.durationMs
2611
+ },
2612
+ sampling: previous?.payload.sampling ?? "native",
2613
+ coordinateSpace: previous?.payload.coordinateSpace ?? "milliseconds"
2614
+ }
2615
+ };
2616
+ const graph = {
2617
+ ...rows,
2618
+ entities: [...rows.entities.filter((row) => row.entityId !== entityId), candidate]
2619
+ };
2620
+ const textSource = {
2621
+ ...assembleEntityContent(graph, entityId),
2622
+ entityKind: "audio-script"
2623
+ };
2624
+ const selected = {
2625
+ ...candidate,
2626
+ payload: {
2627
+ ...candidate.payload,
2628
+ selections: structuredClone(input.selections ?? selectCaptionText(textSource, input.text))
2629
+ }
2630
+ };
2631
+ if (assembleCaptionContent({
2632
+ ...graph,
2633
+ entities: graph.entities.map((row) => row.entityId === entityId ? selected : row)
2634
+ }, entityId).text !== input.text) throw new Error("Caption review text differs from its assembled variant content");
2635
+ return selected;
2636
+ }
2637
+ function selectCaptionText(script, text) {
2638
+ if (!text.trim()) throw new Error("Caption text selection must not be blank");
2639
+ const source = script;
2640
+ const fullText = assembleScriptText(source);
2641
+ const segments = source.payload.segments;
2642
+ if (text === fullText) return segments.map((segment) => ({ segmentId: segment.segmentId }));
2643
+ const matches = [];
2644
+ for (const segment of segments) {
2645
+ let offset = segment.text.indexOf(text);
2646
+ while (offset !== -1) {
2647
+ const start = Array.from(segment.text.slice(0, offset)).length;
2648
+ matches.push({
2649
+ segmentId: segment.segmentId,
2650
+ textRange: {
2651
+ start,
2652
+ end: start + Array.from(text).length
2653
+ }
2654
+ });
2655
+ offset = segment.text.indexOf(text, offset + 1);
2656
+ }
670
2657
  }
671
- });
672
- //#endregion
673
- //#region src/editor/schemas/set-bgm.ts
674
- /**
675
- * Set the document BGM. The media's stable result (storage key) arrives
676
- * materialized from upstream (see
677
- * `results/phase-4-side-effect-payload-contract.md` §3). The op upserts the bgm
678
- * part and seats it on the bgm lane; its effective length is always the whole
679
- * timeline, derived by the projection on read — so there is no `duration_ms`
680
- * input or fact (RFC 02 / `reference/16` §0b). A `bgm_id` lets the op replace an
681
- * existing bgm part by id.
682
- */
683
- const setBgmInputSchema = z.object({
684
- bgm_id: z.string().min(1).describe("The bgm part ID to write"),
685
- audio_storage_key: z.string().min(1),
686
- origin_media_id: mediaIdSchema,
687
- volume: volumeSchema
688
- });
689
- //#endregion
690
- //#region src/editor/schemas/set-caption-style.ts
691
- /**
692
- * Set the caption visual style. GLOBAL by design: the style applies to every
693
- * caption part in the document — it carries NO `caption_id`. This mirrors the FE,
694
- * whose caption-style store (`caption-style.ts:persistCaptionStylePatch`) iterates
695
- * ALL captions and writes the same normalized style to each; the product has a
696
- * single document-wide caption style, not per-caption styling.
697
- *
698
- * Every field is optional and maps to a `CaptionStyle` attribute (snake_case
699
- * IDL). A field present in the input is written to every caption; a field ABSENT
700
- * from the input is left untouched on each caption (the editor merges the patch
701
- * onto each caption's existing style — this is a value edit, not a full-style
702
- * replace, so a partial patch such as "recolor only" does not wipe font size).
703
- *
704
- * Pure document edit, no cascade — captions keep their positions; only the style
705
- * sub-map of each caption part changes.
706
- */
707
- const setCaptionStyleInputSchema = z.object({
708
- font_id: z.string().min(1).optional().describe("Font ID referencing a font from the font library"),
709
- font_size: z.number().positive().optional().describe("Font size in points"),
710
- font_color: z.string().min(1).optional().describe("Font color as hex string, e.g. \"#FFFFFF\""),
711
- font_weight: z.number().int().optional().describe("Numeric font weight, e.g. 400 or 700"),
712
- entrance_animation: z.string().optional().describe("Entrance animation preset ID, e.g. \"fade\" or \"none\""),
713
- entrance_animation_duration_ms: z.number().min(0).optional().describe("Entrance animation duration in ms"),
714
- stroke_color: z.string().min(1).optional().describe("Outline/stroke color as hex string, e.g. \"#000000\""),
715
- stroke_width: z.number().min(0).optional().describe("Outline/stroke width in pixels"),
716
- position_x: z.number().optional().describe("Caption center X as a fraction (0.0 to 1.0)"),
717
- position_y: z.number().optional().describe("Caption center Y as a fraction (0.0 to 1.0)")
718
- }).describe("Document-wide caption style patch (no caption_id; applies to every caption)");
719
- //#endregion
720
- //#region src/editor/schemas/set-caption-visibility.ts
721
- /**
722
- * Toggle caption visibility (the caption track's `is_hidden` flag). Pure
723
- * document edit, no cascade — captions keep their positions; only the lane's
724
- * hidden flag changes.
725
- */
726
- const setCaptionVisibilityInputSchema = z.object({ is_hidden: z.boolean().describe("Whether the caption track is hidden") });
727
- //#endregion
728
- //#region src/editor/schemas/set-video-clip-speed-shift.ts
729
- /**
730
- * Set the playback speed of existing video clips. Per the speed-shift decision
731
- * (`reference/16` §0): the op writes only the `speed_shift` fact — it does NOT
732
- * store an effective `duration_ms` (projection derives it from the trim window /
733
- * speed) and does NOT scale anchored speeches' relative offsets (offsets stay
734
- * put; the cascade reflows absolute positions). A `null` speed_shift clears the
735
- * speed back to original (1×).
736
- */
737
- const setVideoClipSpeedShiftInputSchema = z.object({ clips: z.array(z.object({
738
- clip_id: clipIdSchema.describe("The video clip part ID to set speed for"),
739
- speed_shift: speedShiftSchema.nullable().describe("The new speed setting, or null to reset to 1×")
740
- })).min(1).describe("Video clips with their new speed settings") });
741
- //#endregion
742
- //#region src/editor/schemas/index.ts
743
- var schemas_exports = /* @__PURE__ */ __exportAll({
744
- addSpeechesInputSchema: () => addSpeechesInputSchema,
745
- addVideoClipsInputSchema: () => addVideoClipsInputSchema,
746
- adjustBgmVolumeInputSchema: () => adjustBgmVolumeInputSchema,
747
- adjustSpeechVolumeInputSchema: () => adjustSpeechVolumeInputSchema,
748
- adjustVideoClipDurationInputSchema: () => adjustVideoClipDurationInputSchema,
749
- adjustVideoClipVolumeInputSchema: () => adjustVideoClipVolumeInputSchema,
750
- anchoredDeletePolicySchema: () => anchoredDeletePolicySchema,
751
- anchoredReplacePolicySchema: () => anchoredReplacePolicySchema,
752
- changeSpeechScriptInputSchema: () => changeSpeechScriptInputSchema,
753
- changeSpeechVoiceInputSchema: () => changeSpeechVoiceInputSchema,
754
- clipIdSchema: () => clipIdSchema,
755
- clipIdsSchema: () => clipIdsSchema,
756
- deleteBgmInputSchema: () => deleteBgmInputSchema,
757
- deleteSpeechesInputSchema: () => deleteSpeechesInputSchema,
758
- deleteVideoClipsInputSchema: () => deleteVideoClipsInputSchema,
759
- mediaIdSchema: () => mediaIdSchema,
760
- moveAnchorSchema: () => moveAnchorSchema,
761
- moveSpeechesInputSchema: () => moveSpeechesInputSchema,
762
- moveVideoClipsByAnchorInputSchema: () => moveVideoClipsByAnchorInputSchema,
763
- moveVideoClipsInputSchema: () => moveVideoClipsInputSchema,
764
- movedClipAnchoredPolicySchema: () => movedClipAnchoredPolicySchema,
765
- positiveMsSchema: () => positiveMsSchema,
766
- replaceVideoClipContentInputSchema: () => replaceVideoClipContentInputSchema,
767
- replaceVideoClipSequenceInputSchema: () => replaceVideoClipSequenceInputSchema,
768
- setBgmInputSchema: () => setBgmInputSchema,
769
- setCaptionStyleInputSchema: () => setCaptionStyleInputSchema,
770
- setCaptionVisibilityInputSchema: () => setCaptionVisibilityInputSchema,
771
- setVideoClipSpeedShiftInputSchema: () => setVideoClipSpeedShiftInputSchema,
772
- speechAssetsSchema: () => speechAssetsSchema,
773
- speechIdSchema: () => speechIdSchema,
774
- speechIdsSchema: () => speechIdsSchema,
775
- speedShiftSchema: () => speedShiftSchema,
776
- timelineMsSchema: () => timelineMsSchema,
777
- voiceSchema: () => voiceSchema,
778
- volumeSchema: () => volumeSchema
779
- });
2658
+ if (matches.length !== 1) throw new Error("Caption text must select a unique AudioScript range; provide explicit selections");
2659
+ return matches;
2660
+ }
780
2661
  //#endregion
781
2662
  //#region src/editor/snapshot-utils.ts
782
2663
  function isMap(value) {
@@ -1126,9 +3007,11 @@ function readPartLibrary(draft) {
1126
3007
  var SemanticEditor = class {
1127
3008
  doc;
1128
3009
  validator;
1129
- constructor(doc, validator = new SchemaValidator()) {
3010
+ idFactory;
3011
+ constructor(doc, validator = new SchemaValidator(), idFactory = generatePartId) {
1130
3012
  this.doc = doc;
1131
3013
  this.validator = validator;
3014
+ this.idFactory = idFactory;
1132
3015
  }
1133
3016
  async moveVideoClips(input, options) {
1134
3017
  this.validator.validateMoveVideoClips(input, this.doc);
@@ -1208,7 +3091,7 @@ var SemanticEditor = class {
1208
3091
  track.items ??= [];
1209
3092
  let at = insertIndex;
1210
3093
  for (const clip of input.clips) {
1211
- const partId = generatePartId("clip");
3094
+ const partId = this.idFactory("clip");
1212
3095
  setPart(draft, partId, { video_clip: {
1213
3096
  id: partId,
1214
3097
  kind: "video_clip",
@@ -1255,7 +3138,7 @@ var SemanticEditor = class {
1255
3138
  const newItems = [];
1256
3139
  const newClipIds = [];
1257
3140
  for (const clip of input.new_clips) {
1258
- const partId = generatePartId("clip");
3141
+ const partId = this.idFactory("clip");
1259
3142
  setPart(draft, partId, { video_clip: {
1260
3143
  id: partId,
1261
3144
  kind: "video_clip",
@@ -1875,6 +3758,111 @@ function deleteAnchoredSubtree(draft, clipId) {
1875
3758
  for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
1876
3759
  }
1877
3760
  //#endregion
3761
+ //#region src/editor/journal.ts
3762
+ /**
3763
+ * Wire a plain-memory adapter + editor that share a recording id factory, so
3764
+ * every mutating transact lands in `journal` with ordered `generated_ids`.
3765
+ */
3766
+ function createEditSandbox(document, options) {
3767
+ const adapter = createPlainMemoryAdapter(document, options);
3768
+ return {
3769
+ adapter,
3770
+ editor: new SemanticEditor(adapter, new SchemaValidator(), adapter.idFactory),
3771
+ journal: adapter.journal
3772
+ };
3773
+ }
3774
+ /**
3775
+ * Re-drive `doc` from a recorded journal, forcing each entry's `generated_ids`
3776
+ * through a queue-backed id factory. Never mints fresh ids: an empty queue on
3777
+ * demand throws `unrecorded id`; leftover ids after an entry throws
3778
+ * `unconsumed ids`. Legacy entries without `generated_ids` are treated as `[]`.
3779
+ */
3780
+ async function replayJournal(doc, journal) {
3781
+ const queue = [];
3782
+ const idFactory = (_prefix) => {
3783
+ const id = queue.shift();
3784
+ if (id == null) throw new Error("unrecorded id");
3785
+ return id;
3786
+ };
3787
+ const editor = new SemanticEditor(doc, new SchemaValidator(), idFactory);
3788
+ for (const entry of journal) {
3789
+ queue.push(...entry.generated_ids ?? []);
3790
+ await dispatchEntry(editor, entry);
3791
+ if (queue.length > 0) throw new Error("unconsumed ids");
3792
+ }
3793
+ }
3794
+ /** Dispatch one journal entry to the matching editor method; payload is passed through. */
3795
+ async function dispatchEntry(editor, entry) {
3796
+ const payload = entry.payload;
3797
+ const options = entry.intent != null ? { intent: entry.intent } : void 0;
3798
+ switch (entry.kind) {
3799
+ case "MoveVideoClips":
3800
+ await editor.moveVideoClips(payload, options);
3801
+ return;
3802
+ case "MoveVideoClipsByAnchor":
3803
+ await editor.moveVideoClipsByAnchor(payload, options);
3804
+ return;
3805
+ case "DeleteVideoClips":
3806
+ await editor.deleteVideoClips(payload, options);
3807
+ return;
3808
+ case "AddVideoClips":
3809
+ await editor.addVideoClips(payload, options);
3810
+ return;
3811
+ case "AdjustVideoClipVolume":
3812
+ await editor.adjustVideoClipVolume(payload, options);
3813
+ return;
3814
+ case "SetVideoClipSpeedShift":
3815
+ await editor.setVideoClipSpeedShift(payload, options);
3816
+ return;
3817
+ case "ReplaceVideoClipContent":
3818
+ await editor.replaceVideoClipContent(payload, options);
3819
+ return;
3820
+ case "ReplaceVideoClipSequence":
3821
+ await editor.replaceVideoClipSequence(payload, options);
3822
+ return;
3823
+ case "AdjustVideoClipDuration":
3824
+ await editor.adjustVideoClipDuration(payload, options);
3825
+ return;
3826
+ case "AddSpeeches":
3827
+ await editor.addSpeeches(payload, options);
3828
+ return;
3829
+ case "DeleteSpeeches":
3830
+ await editor.deleteSpeeches(payload, options);
3831
+ return;
3832
+ case "MoveSpeeches":
3833
+ await editor.moveSpeeches(payload, options);
3834
+ return;
3835
+ case "ChangeSpeechScript":
3836
+ await editor.changeSpeechScript(payload, options);
3837
+ return;
3838
+ case "ChangeSpeechVoice":
3839
+ await editor.changeSpeechVoice(payload, options);
3840
+ return;
3841
+ case "AdjustSpeechVolume":
3842
+ await editor.adjustSpeechVolume(payload, options);
3843
+ return;
3844
+ case "SetCaptionVisibility":
3845
+ await editor.setCaptionVisibility(payload, options);
3846
+ return;
3847
+ case "SetCaptionStyle":
3848
+ await editor.setCaptionStyle(payload, options);
3849
+ return;
3850
+ case "SetBgm":
3851
+ await editor.setBgm(payload, options);
3852
+ return;
3853
+ case "DeleteBgm":
3854
+ await editor.deleteBgm(payload, options);
3855
+ return;
3856
+ case "AdjustBgmVolume":
3857
+ await editor.adjustBgmVolume(payload, options);
3858
+ return;
3859
+ default: {
3860
+ const _exhaustive = entry.kind;
3861
+ throw new Error(`replayJournal: unsupported kind ${String(_exhaustive)}`);
3862
+ }
3863
+ }
3864
+ }
3865
+ //#endregion
1878
3866
  //#region src/editor/types.ts
1879
3867
  /** Runtime list of the frozen, implemented kinds (for guards / introspection). */
1880
3868
  const IMPLEMENTED_SEMANTIC_OP_KINDS = [
@@ -2035,24 +4023,17 @@ var ManualSyncDoc = class ManualSyncDoc {
2035
4023
  * Fetches the snapshot up front rather than starting empty and converging: the
2036
4024
  * agent's first act is to read the document, so there is no useful state before
2037
4025
  * the snapshot lands. This also fails fast and loudly on a document that does
2038
- * not exist or whose first snapshot is incomplete or invalid.
4026
+ * not exist, instead of `MengineDocSession.waitForContent()`'s behavior of
4027
+ * waiting forever for content that will never arrive.
2039
4028
  */
2040
4029
  static async open(options) {
2041
- const response = await options.client.fetchSnapshot();
2042
4030
  const doc = new LoroDoc();
2043
- let manualSyncDoc;
2044
- try {
2045
- if (options.peerId != null) doc.setPeerId(options.peerId);
2046
- if (doc.import(base64ToBytes(response.snapshot)).pending != null) throw new Error("Initial mengine snapshot has missing dependencies");
2047
- manualSyncDoc = new ManualSyncDoc(options.client, doc);
2048
- assertValidVideoDocument(manualSyncDoc.snapshot());
2049
- manualSyncDoc.watermark = manualSyncDoc.serverVVFrom(response.version.server_vv) ?? doc.oplogVersion();
2050
- return manualSyncDoc;
2051
- } catch (error) {
2052
- manualSyncDoc?.adapter.dispose();
2053
- doc.free();
2054
- throw error;
2055
- }
4031
+ if (options.peerId != null) doc.setPeerId(options.peerId);
4032
+ const response = await options.client.fetchSnapshot();
4033
+ doc.import(base64ToBytes(response.snapshot));
4034
+ const manualSyncDoc = new ManualSyncDoc(options.client, doc);
4035
+ manualSyncDoc.watermark = manualSyncDoc.serverVVFrom(response.version.server_vv) ?? doc.oplogVersion();
4036
+ return manualSyncDoc;
2056
4037
  }
2057
4038
  /** Current document read model (authoritative shape). */
2058
4039
  snapshot() {
@@ -2071,18 +4052,31 @@ var ManualSyncDoc = class ManualSyncDoc {
2071
4052
  editorPeerId() {
2072
4053
  return this.doc.peerIdStr;
2073
4054
  }
2074
- /** Current content projected into the compatible track and part read shape. */
2075
- content() {
4055
+ /** Current document projected to the legacy `VideoDraft` read shape. */
4056
+ draft() {
2076
4057
  return fromVideoDocument(this.adapter.snapshot());
2077
4058
  }
2078
4059
  /**
4060
+ * Replay a deterministic sandbox journal into this document.
4061
+ *
4062
+ * The adapter stays private so callers cannot bypass the manual-sync
4063
+ * lifecycle; journal replay is the one host-side operation that needs the
4064
+ * adapter's transaction boundary and recorded id queue. Durability is still
4065
+ * explicit: call {@link push} after replay and report success only from its
4066
+ * verdict.
4067
+ */
4068
+ async replayJournal(journal) {
4069
+ await replayJournal(this.adapter, journal);
4070
+ }
4071
+ /**
2079
4072
  * Mark the document state the caller has just observed, for a later
2080
4073
  * {@link hasChangedSince}.
2081
4074
  *
2082
4075
  * This pair replaces the legacy integer-version comparison that
2083
4076
  * `DraftVersionDetector` used to tell the LLM "the draft was modified
2084
- * externally, reload before editing". Comparing Loro version vectors covers
2085
- * all collaborative edits without a business revision field in the document.
4077
+ * externally, reload before editing". Under mengine `meta.version` never
4078
+ * changes, so that detector would go permanently silent; comparing version
4079
+ * vectors restores the same capability.
2086
4080
  *
2087
4081
  * Same capability, not a stronger one: like the legacy detector, this only
2088
4082
  * reports what changed between two moments the caller chose to sample.
@@ -2166,7 +4160,6 @@ var ManualSyncDoc = class ManualSyncDoc {
2166
4160
  if (serverVV != null) this.watermark = serverVV;
2167
4161
  return {
2168
4162
  kind: response.kind,
2169
- mengineUpdateSeq: response.version?.update_seq ?? response.update_seq ?? void 0,
2170
4163
  updateSeq: response.update_seq ?? void 0,
2171
4164
  collaborated: serverVV != null && !covers(localVersion, serverVV)
2172
4165
  };
@@ -2285,7 +4278,6 @@ var SseConnection = class {
2285
4278
  for (const update of event.updates) this.onUpdate(update);
2286
4279
  }
2287
4280
  });
2288
- if (!signal.aborted) this.setStatus("connecting", /* @__PURE__ */ new Error("SSE stream closed"));
2289
4281
  } catch (error) {
2290
4282
  if (signal.aborted) break;
2291
4283
  this.setStatus("connecting", error instanceof Error ? error : new Error(String(error)));
@@ -2338,7 +4330,6 @@ var MedeoHttpDocStorage = class {
2338
4330
  async getDoc(docId) {
2339
4331
  this.assertDocId(docId);
2340
4332
  const snapshot = await this.client.fetchSnapshot();
2341
- this.reportServerVersion(snapshot.version?.server_vv);
2342
4333
  const now = /* @__PURE__ */ new Date();
2343
4334
  return {
2344
4335
  docId,
@@ -2350,7 +4341,6 @@ var MedeoHttpDocStorage = class {
2350
4341
  async getDocDiff(docId, knownVersion) {
2351
4342
  this.assertDocId(docId);
2352
4343
  const response = await this.client.sync(knownVersion);
2353
- this.reportServerVersion(response.server_vv);
2354
4344
  return {
2355
4345
  docId,
2356
4346
  missing: base64ToBytes(response.update),
@@ -2381,7 +4371,7 @@ var MedeoHttpDocStorage = class {
2381
4371
  if (update.data.byteLength === 0) return {};
2382
4372
  try {
2383
4373
  const response = await this.client.pushUpdate(update.data);
2384
- const serverVV = this.reportServerVersion(response.version?.server_vv);
4374
+ const serverVV = decodeServerVV(response.version?.server_vv);
2385
4375
  this.events.emit("pushOutcome", {
2386
4376
  kind: response.kind,
2387
4377
  updateSeq: response.update_seq ?? void 0,
@@ -2409,15 +4399,6 @@ var MedeoHttpDocStorage = class {
2409
4399
  subscribePushOutcome(callback) {
2410
4400
  return this.events.on("pushOutcome", callback);
2411
4401
  }
2412
- /** Authoritative HTTP reads and accepted pushes, never inferred from SSE bytes. */
2413
- subscribeServerVersion(callback) {
2414
- return this.events.on("serverVersion", callback);
2415
- }
2416
- reportServerVersion(encoded) {
2417
- const version = decodeServerVV(encoded);
2418
- if (version != null) this.events.emit("serverVersion", version);
2419
- return version;
2420
- }
2421
4402
  async deleteDoc(_docId) {
2422
4403
  throw new Error("MedeoHttpDocStorage does not support deleteDoc");
2423
4404
  }
@@ -2599,15 +4580,8 @@ var MengineDocSession = class {
2599
4580
  adapterValue = null;
2600
4581
  editorValue = null;
2601
4582
  started = false;
2602
- startTask = null;
2603
- destroyed = false;
2604
- lifetime = null;
2605
- hasConnected = false;
2606
- initialVersionValue = null;
2607
- localVersionValue = null;
2608
- pushErrorValue = null;
2609
4583
  /**
2610
- * Latest server oplog version seen on an authoritative HTTP response. Lets `waitForServerAck`
4584
+ * Latest server oplog version seen on a push outcome. Lets `waitForServerAck`
2611
4585
  * return without waiting when the server is already known to cover the local
2612
4586
  * doc (e.g. nothing was edited since the last confirmed push).
2613
4587
  */
@@ -2619,7 +4593,8 @@ var MengineDocSession = class {
2619
4593
  this.server = new MedeoHttpDocStorage({
2620
4594
  docId: options.docId,
2621
4595
  client: options.client,
2622
- sseReconnectDelayMs: options.sseReconnectDelayMs
4596
+ sseReconnectDelayMs: options.sseReconnectDelayMs,
4597
+ readonlyMode: options.readonlyMode
2623
4598
  });
2624
4599
  this.synchronizer = new ClientServerSynchronizer(this.local, this.server);
2625
4600
  this.manager = new DocManager(this.local, this.synchronizer);
@@ -2635,14 +4610,14 @@ var MengineDocSession = class {
2635
4610
  return this.server.subscribePushOutcome(callback);
2636
4611
  }
2637
4612
  /**
2638
- * Resolve once the server has durably accepted the local commits as of
4613
+ * Resolve once the server has durably accepted the local document state as of
2639
4614
  * *now* — the capability an Agent tool needs to answer "did my edit land?"
2640
4615
  * before reporting success.
2641
4616
  *
2642
4617
  * Call it right after the editor ops whose durability matters. It snapshots the
2643
- * latest local commit's version (or the loaded baseline before any edit) and
2644
- * resolves when an authoritative server version covers it. Remote-only
2645
- * imports do not extend this target, matching the displayed confirmation state.
4618
+ * local doc's current version and resolves on the first push outcome whose
4619
+ * `serverVV` covers that version, i.e. the server log is at least as advanced as
4620
+ * the local doc was when this was called.
2646
4621
  *
2647
4622
  * Version coverage, not byte identity, is the acceptance test — for two reasons:
2648
4623
  * the ops may reach the server inside a merged blob rather than as the exact
@@ -2657,39 +4632,31 @@ var MengineDocSession = class {
2657
4632
  * @throws {MengineAckFailedError} on timeout, rejection, or transport failure.
2658
4633
  */
2659
4634
  async waitForServerAck(options = {}) {
2660
- if (this.adapterValue == null || this.lifetime == null) throw new Error("mengine doc session is not started");
4635
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
2661
4636
  const timeoutMs = options.timeoutMs ?? 15e3;
2662
- const target = this.confirmationTarget() ?? this.manager.connectDoc(this.docId).version();
2663
- const signal = this.lifetime.signal;
4637
+ const target = this.manager.connectDoc(this.docId).version();
2664
4638
  if (this.serverVVValue != null && serverCovers(this.serverVVValue, target)) return;
2665
4639
  await new Promise((resolve, reject) => {
2666
- const cleanup = [];
4640
+ let off = () => {};
2667
4641
  let done = false;
2668
4642
  const settle = (error) => {
2669
4643
  if (done) return;
2670
4644
  done = true;
2671
4645
  clearTimeout(timer);
2672
- cleanup.forEach((off) => off());
4646
+ off();
2673
4647
  if (error == null) resolve();
2674
4648
  else reject(error);
2675
4649
  };
2676
4650
  const timer = setTimeout(() => {
2677
4651
  settle(new MengineAckFailedError("timeout", void 0, void 0, `mengine did not acknowledge the update within ${timeoutMs}ms`));
2678
4652
  }, timeoutMs);
2679
- const onAbort = () => settle(new MengineAckFailedError("failed", void 0, void 0, "mengine doc session destroyed"));
2680
- signal.addEventListener("abort", onAbort, { once: true });
2681
- cleanup.push(() => signal.removeEventListener("abort", onAbort));
2682
- cleanup.push(this.events.on("syncState", () => {
2683
- if (this.serverVVValue != null && serverCovers(this.serverVVValue, target)) settle(void 0);
2684
- }));
2685
- cleanup.push(this.server.subscribePushOutcome((outcome) => {
4653
+ off = this.server.subscribePushOutcome((outcome) => {
2686
4654
  if (outcome.kind === "rejected" || outcome.kind === "failed") {
2687
4655
  settle(new MengineAckFailedError(outcome.kind, outcome.code, outcome.error, outcome.error?.message ?? `mengine push ${outcome.kind}`));
2688
4656
  return;
2689
4657
  }
2690
- }));
2691
- if (signal.aborted) onAbort();
2692
- else this.synchronizer.requestSync(this.docId);
4658
+ if (outcome.serverVV != null && serverCovers(outcome.serverVV, target)) settle(void 0);
4659
+ });
2693
4660
  });
2694
4661
  }
2695
4662
  /**
@@ -2702,6 +4669,24 @@ var MengineDocSession = class {
2702
4669
  if (this.editorValue == null) throw new Error("mengine doc session is not started");
2703
4670
  return this.editorValue;
2704
4671
  }
4672
+ /**
4673
+ * Opaque version token of the local oplog (base64 `VersionVector.encode`).
4674
+ * Equality-comparable only: equal means no observed change (local or
4675
+ * remote-arrived) since the token was taken. Throws when not started.
4676
+ */
4677
+ version() {
4678
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
4679
+ return bytesToBase64(this.adapterValue.doc.oplogVersion().encode());
4680
+ }
4681
+ /**
4682
+ * The live document adapter, exposed for journal replay (commit channel).
4683
+ * Replaying through it still goes SemanticEditor → Loro → mengine-server —
4684
+ * no write bypass. Typed by the narrow interface on purpose.
4685
+ */
4686
+ get documentAdapter() {
4687
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
4688
+ return this.adapterValue;
4689
+ }
2705
4690
  /** Current document snapshot (read model). */
2706
4691
  snapshot() {
2707
4692
  if (this.adapterValue == null) throw new Error("mengine doc session is not started");
@@ -2714,100 +4699,37 @@ var MengineDocSession = class {
2714
4699
  * read an initial snapshot. Remote convergence continues in the background and
2715
4700
  * surfaces through `subscribe`.
2716
4701
  */
2717
- start() {
2718
- if (this.destroyed) return Promise.reject(/* @__PURE__ */ new Error("mengine doc session destroyed"));
2719
- if (this.startTask != null) return this.startTask;
4702
+ async start() {
4703
+ if (this.started) return this.snapshot();
2720
4704
  this.started = true;
2721
- this.lifetime = new AbortController();
2722
- this.startTask = this.open().catch((error) => {
2723
- const wasDestroyed = this.destroyed;
2724
- this.destroy();
2725
- if (wasDestroyed) throw new Error("mengine doc session destroyed");
2726
- throw error;
2727
- });
2728
- return this.startTask;
2729
- }
2730
- async open() {
2731
- const signal = this.lifetime.signal;
2732
- this.disposables.add(() => this.local.connection.disconnect());
2733
4705
  this.local.connection.connect();
2734
- await this.local.connection.waitForConnected().abortOn(signal);
2735
- const cached = await Task.spawn(() => this.local.getDoc(this.docId)).abortOn(signal);
2736
- const initial = cached?.data.byteLength ? void 0 : await Task.spawn(() => this.options.client.fetchSnapshot()).abortOn(signal);
2737
- const data = initial == null ? cached.data : base64ToBytes(initial.snapshot);
2738
- if (this.destroyed) throw new Error("mengine doc session destroyed");
2739
- this.disposables.add(this.server.subscribeServerVersion((version) => {
2740
- this.serverVVValue = version;
2741
- if (this.getSyncState().confirmation === "confirmed") this.pushErrorValue = null;
2742
- this.emitSyncState();
2743
- }));
2744
- this.disposables.add(this.server.subscribePushOutcome((outcome) => {
2745
- this.pushErrorValue = outcome.kind === "failed" || outcome.kind === "rejected" ? outcome.error ?? /* @__PURE__ */ new Error(`mengine push ${outcome.kind}`) : null;
2746
- this.emitSyncState();
2747
- }));
2748
- this.disposables.add(this.server.connection.onStatusChanged((status) => {
2749
- if (status === "connected") this.hasConnected = true;
2750
- this.emitSyncState();
2751
- }));
2752
- this.disposables.add(() => this.server.connection.disconnect());
2753
4706
  this.server.connection.connect();
4707
+ this.disposables.add(() => this.local.connection.disconnect());
4708
+ this.disposables.add(() => this.server.connection.disconnect());
2754
4709
  this.synchronizer.start();
2755
4710
  this.manager.start();
2756
4711
  this.disposables.add(() => this.synchronizer.stop());
2757
4712
  this.disposables.add(() => this.manager.stop());
2758
4713
  const doc = this.manager.connectDoc(this.docId);
2759
- this.disposables.add(() => this.manager.disconnectDoc(this.docId));
2760
4714
  if (this.options.peerId != null) doc.setPeerId(this.options.peerId);
2761
- if (doc.import(data).pending != null) throw new Error("Initial mengine snapshot has missing dependencies");
2762
4715
  this.adapterValue = new MirrorVideoDocumentAdapter(doc);
2763
- const adapter = this.adapterValue;
2764
- this.disposables.add(() => adapter.dispose());
2765
- assertValidVideoDocument(this.adapterValue.snapshot());
2766
- this.initialVersionValue = doc.version();
2767
- if (initial != null) this.serverVVValue = base64ToBytes(initial.version.server_vv);
2768
4716
  this.editorValue = new SemanticEditor(this.adapterValue);
4717
+ this.disposables.add(() => this.manager.disconnectDoc(this.docId));
2769
4718
  const unsubscribe = doc.subscribe((event) => {
2770
4719
  if (this.adapterValue == null) return;
2771
- if (event.by === "local") this.localVersionValue = doc.version();
2772
- this.emitSyncState();
2773
4720
  this.events.emit("update", {
2774
4721
  source: event.by === "local" ? "local" : "remote",
2775
4722
  snapshot: this.adapterValue.snapshot()
2776
4723
  });
2777
4724
  });
2778
4725
  this.disposables.add(unsubscribe);
2779
- this.disposables.add(this.manager.onDocStateChange(this.docId, (state) => {
2780
- this.events.emit("state", state);
2781
- this.emitSyncState();
4726
+ this.disposables.add(this.manager.onDocStateChange(this.docId, (state) => this.events.emit("state", state)));
4727
+ this.disposables.add(this.server.subscribePushOutcome((outcome) => {
4728
+ if (outcome.serverVV != null) this.serverVVValue = outcome.serverVV;
2782
4729
  }));
2783
- await this.waitForLocalLoad();
2784
- assertValidVideoDocument(this.snapshot());
2785
- this.initialVersionValue = doc.version();
2786
- this.emitSyncState();
4730
+ await this.waitForContent();
2787
4731
  return this.snapshot();
2788
4732
  }
2789
- getSyncState() {
2790
- const state = this.getState();
2791
- const target = this.confirmationTarget();
2792
- const confirmation = this.serverVVValue == null || target == null ? this.localVersionValue == null ? "unknown" : "pending" : serverCovers(this.serverVVValue, target) ? "confirmed" : "pending";
2793
- const connection = !this.started ? "disconnected" : this.server.connection.status === "connected" ? "connected" : this.hasConnected || this.server.connection.error != null ? "disconnected" : "connecting";
2794
- return {
2795
- confirmation,
2796
- phase: connection === "disconnected" ? "offline" : this.pushErrorValue != null || state.error != null ? "failed" : connection === "connecting" ? "connecting" : state.syncing ? "syncing" : "idle"
2797
- };
2798
- }
2799
- confirmationTarget() {
2800
- return this.localVersionValue ?? this.initialVersionValue;
2801
- }
2802
- /** Immediately supplies current state; unsubscribe when the host changes docs. */
2803
- subscribeSyncState(cb) {
2804
- const off = this.events.on("syncState", cb);
2805
- cb(this.getSyncState());
2806
- return off;
2807
- }
2808
- emitSyncState() {
2809
- this.events.emit("syncState", this.getSyncState());
2810
- }
2811
4733
  subscribe(cb) {
2812
4734
  return this.events.on("update", cb);
2813
4735
  }
@@ -2818,39 +4740,40 @@ var MengineDocSession = class {
2818
4740
  return this.manager.getDocState(this.docId);
2819
4741
  }
2820
4742
  destroy() {
2821
- if (this.destroyed) return;
2822
- this.destroyed = true;
2823
- this.started = false;
2824
- this.lifetime?.abort();
2825
4743
  this.disposables.dispose();
2826
- this.emitSyncState();
2827
4744
  this.events.clearAll();
2828
4745
  this.adapterValue = null;
2829
4746
  this.editorValue = null;
2830
4747
  this.started = false;
2831
4748
  }
2832
- /** Merge persisted local edits before exposing the initial content. */
2833
- async waitForLocalLoad() {
2834
- const signal = this.lifetime.signal;
2835
- await new Promise((resolve, reject) => {
2836
- let off = () => {};
2837
- const finish = (error) => {
2838
- off();
2839
- signal.removeEventListener("abort", onAbort);
2840
- if (error != null) reject(error);
2841
- else resolve();
2842
- };
2843
- const onAbort = () => finish(/* @__PURE__ */ new Error("mengine doc session destroyed"));
2844
- const check = (state) => {
2845
- if (state.loaded) finish();
2846
- else if (state.error != null) finish(new Error(state.error));
2847
- };
2848
- off = this.onStateChange(check);
2849
- signal.addEventListener("abort", onAbort, { once: true });
2850
- if (signal.aborted) onAbort();
2851
- else check(this.getState());
4749
+ /**
4750
+ * Resolve once the Loro doc holds the document root.
4751
+ *
4752
+ * `DocManager` loads from local storage, which starts empty for a fresh
4753
+ * client; the server snapshot arrives asynchronously via the first sync job.
4754
+ * `loaded` only means the local load ran, so we wait for actual content
4755
+ * (populated schema roots) instead, surfaced by the doc subscription set up
4756
+ * in `start()`.
4757
+ */
4758
+ async waitForContent() {
4759
+ if (this.hasContent()) return;
4760
+ await new Promise((resolve) => {
4761
+ const off = this.events.on("update", () => {
4762
+ if (this.hasContent()) {
4763
+ off();
4764
+ resolve();
4765
+ }
4766
+ });
2852
4767
  });
2853
4768
  }
4769
+ hasContent() {
4770
+ if (this.adapterValue == null) return false;
4771
+ try {
4772
+ return this.adapterValue.hasContent();
4773
+ } catch {
4774
+ return false;
4775
+ }
4776
+ }
2854
4777
  };
2855
4778
  /**
2856
4779
  * True when the *encoded* server version `serverVV` includes everything in
@@ -2874,4 +4797,4 @@ function serverCovers(serverVV, target) {
2874
4797
  return covers(server, target);
2875
4798
  }
2876
4799
  //#endregion
2877
- export { DEFAULT_UNIT_TIME_MS, IMPLEMENTED_SEMANTIC_OP_KINDS, LANE_KINDS_IN_STACK_ORDER, ManualSyncDoc, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, MirrorVideoDocumentAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, decodeDocVersionMark, derivePositionFromAbs, encodeDocVersionMark, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, laneTrackId, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, resolveAllSpeechOverlaps, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema };
4800
+ 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, ScriptCompositionError, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, applyEntityTimelineProjection, arrangeMainTrackSeamlessly, assembleCaptionContent, assembleEntityContent, assemblePhoneticScriptContent, assembleScriptText, assertCanonicalEditorResources, assertEntityProjectionPreservesLegacy, assertMediaAssetWritePolicy, assertValidVideoDocument, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createEditSandbox, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, createPlainMemoryAdapter, decodeDocVersionMark, derivePositionFromAbs, effectiveVideoClipDurationMs, encodeDocVersionMark, ensureEditorFoundation, ensureLaneTrack, fillMainTrackTimeGaps, findComposedAudioScript, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, importMediaAsset, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, laneTrackId, mainTrackRanges, migrateLegacyTimelineToEntities, partDurationMs, partUnionSchema, prepareCaptionContent, projectEntityTimeline, readDocumentAudioScript, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, replayJournal, resolveAllSpeechOverlaps, resolveEntityTimelineLayout, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, selectAudioScriptSegments, snapshotToPlain, solveVideoDocument, speedOf, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema, writeVideoDocumentToDraft };