@mengine/medeo-client 2.0.1-alpha.3 → 2.0.1-alpha.31

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,7 +1,8 @@
1
- import { t as __exportAll } from "./chunk-D7D4PA-g.js";
2
- import { A as isEmptyVideoClip, C as fillMainTrackTimeGaps, D as resolveSpeechOverlapByShiftingVideos, E as resolveAllSpeechOverlaps, F as partUnionToDraft, I as recordEntries, L as videoDocumentMirrorSchema, M as safeDurationMs, N as DEFAULT_UNIT_TIME_MS, O as syncAggregatedClipsTimePosition, P as effectiveVideoClipDurationMs, R as base64ToBytes, S as arrangeMainTrackSeamlessly, T as recalculateTimelineDuration, _ as ensureLaneTrack, a as DocumentMutationGuard, b as solveVideoDocument, c as derivePositionFromAbs, d as VideoDocumentValidationError, f as assertValidVideoDocument, g as LANE_KINDS_IN_STACK_ORDER, h as videoDocumentSchema, i as readVideoDocumentFromDraft, j as partDurationMs, k as TIMELINE_SKELETON_DURATION_MS, l as fromVideoDocument, m as partUnionSchema, n as createMirrorVideoDocument, o as buildInitialVideoDocument, p as validateVideoDocument, r as createMirrorVideoDocumentAdapter, s as buildSpeechHostMap, t as MirrorVideoDocumentAdapter, u as toVideoDocument, v as findLaneTrack, w as reassignSpeechesToVideoClipsByTime, x as cascadeAfterVideoClipChanges, y as laneTrackId, z as bytesToBase64 } from "./document-B_JQwrC5.js";
3
- import { z } from "zod";
4
- import { LoroDoc, UndoManager, VersionVector, decodeImportBlobMeta } from "loro-crdt";
1
+ import { $ as buildSpeechHostMap, A as EntityProjectionGraph, At as recordEntries, B as assembleEntityContent, C as scriptSourceAssetOf, Ct as isEmptyVideoClip, D as assertFieldChanges, Dt as effectiveVideoClipDurationMs, E as applyFieldChanges, Et as DEFAULT_UNIT_TIME_MS, F as relationsFrom, G as updateEntityFields, H as assembleScriptText, I as relationsTo, J as createEntityId, K as variantBaseEntityIds, L as scriptContentHash, M as EntityTimelineProjectionError, Mt as base64ToBytes, N as validateEntityRelationTransition, Nt as bytesToBase64, O as resolveFieldPath, Ot as speedOf, P as decodeEntityRelationRows, Q as buildInitialVideoDocument, R as ScriptCompositionError, S as phoneticScriptAssetOf, St as TIMELINE_SKELETON_DURATION_MS, T as equalJson, Tt as safeDurationMs, U as findComposedAudioScript, V as assemblePhoneticScriptContent, W as selectAudioScriptSegment, X as hasSequence, Y as createRelationId, Z as isMediaAssetVariantKind, _ as audioScriptAssetHash, _t as reassignSpeechesToVideoClipsByTime, a as createMirrorVideoDocument, at as validateVideoDocument, b as phoneticScriptAssetContent, bt as resolveSpeechOverlapByShiftingVideos, c as DocumentMutationGuard, ct as LANE_KINDS_IN_STACK_ORDER, d as LoroEntityDraft, dt as hasResolvedPlacement, et as derivePositionFromAbs, f as readEntityDocument, ft as laneTrackId, g as audioScriptAssetFields, gt as fillMainTrackTimeGaps, h as audioScriptAssetContent, ht as arrangeMainTrackSeamlessly, i as MirrorVideoDocumentAdapter, it as assertValidVideoDocument, j as resolveEntityTimelineLayout, jt as videoDocumentMirrorSchema, k as projectEntityTimeline, kt as partUnionToDraft, l as LORO_ENTITY_SCHEMA, lt as ensureLaneTrack, m as entityOrderGroups, mt as cascadeAfterVideoClipChanges, n as createPlainMemoryAdapter, nt as toVideoDocument, o as createMirrorVideoDocumentAdapter, ot as partUnionSchema, p as readEntityRelationRecords, pt as solveVideoDocument, q as isJsonObject, r as generatePartId, rt as VideoDocumentValidationError, s as readVideoDocumentFromDraft, st as videoDocumentSchema, t as PlainMemoryAdapter, tt as fromVideoDocument, u as LoroEntityDocument, ut as findLaneTrack, v as audioScriptAssetOf, vt as recalculateTimelineDuration, w as canonicalJson, wt as partDurationMs, x as phoneticScriptAssetHash, xt as syncAggregatedClipsTimePosition, y as isTranscribedAudioScript, yt as resolveAllSpeechOverlaps, z as assembleCaptionContent } from "./document-BCQE5ofl.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";
3
+ import { sha256 } from "@noble/hashes/sha2.js";
4
+ import { bytesToHex } from "@noble/hashes/utils.js";
5
+ import { LoroDoc, LoroMap, UndoManager, VersionVector } from "loro-crdt";
5
6
  import { ClientServerSynchronizer, DocManager } from "@mengine/sync";
6
7
  import { DisposableSet, EventBus, Task } from "@mengine/utils";
7
8
  import { BaseDocStorage, DummyConnection } from "@mengine/storage";
@@ -13,7 +14,7 @@ import { BaseDocStorage, DummyConnection } from "@mengine/storage";
13
14
  * namespace (mirrors the cluster's `/oapi/v1` precedent for an independent
14
15
  * subsystem) rather than folding into the shared `/api/v2/*` space.
15
16
  */
16
- const MENGINE_API_PREFIX = "/api/mengine/v1";
17
+ const MENGINE_API_PREFIX$1 = "/api/mengine/v1";
17
18
  var MengineHttpRequestError = class extends Error {
18
19
  status;
19
20
  payload;
@@ -129,12 +130,12 @@ var MengineHttpClient = class {
129
130
  ...init,
130
131
  headers
131
132
  });
132
- const payload = await safeReadJson(response);
133
+ const payload = await safeReadJson$1(response);
133
134
  if (!response.ok) throw new MengineHttpRequestError(response.status, payload);
134
135
  return payload;
135
136
  }
136
137
  endpoint(path) {
137
- return `${this.options.httpOrigin.replace(/\/$/, "")}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(this.options.docId)}/${path}`;
138
+ return `${this.options.httpOrigin.replace(/\/$/, "")}${MENGINE_API_PREFIX$1}/docs/${encodeURIComponent(this.options.docId)}/${path}`;
138
139
  }
139
140
  };
140
141
  /**
@@ -151,627 +152,3038 @@ function asPushRejection(error) {
151
152
  if (body.kind !== "rejected" || typeof body.code !== "string") return void 0;
152
153
  return new MenginePushRejectedError(body.code, body.message ?? "", body.server_version, error.status);
153
154
  }
154
- async function safeReadJson(response) {
155
- const text = await response.text();
156
- if (text.length === 0) return null;
157
- try {
158
- return JSON.parse(text);
159
- } catch {
160
- return text;
155
+ async function safeReadJson$1(response) {
156
+ const text = await response.text();
157
+ if (text.length === 0) return null;
158
+ try {
159
+ return JSON.parse(text);
160
+ } catch {
161
+ return text;
162
+ }
163
+ }
164
+ //#endregion
165
+ //#region src/client/sse.ts
166
+ async function readMengineEventStream(options) {
167
+ const response = await options.client.fetch(options.client.eventsUrl(), {
168
+ headers: options.client.headers(),
169
+ signal: options.signal
170
+ });
171
+ if (!response.ok) throw new Error(`mengine event stream failed: ${response.status}`);
172
+ const reader = response.body?.getReader();
173
+ if (reader == null) throw new Error("mengine event stream response has no readable body");
174
+ options.onOpen?.();
175
+ const decoder = new TextDecoder();
176
+ let buffer = "";
177
+ while (options.signal?.aborted !== true) {
178
+ const { done, value } = await reader.read();
179
+ if (done) return;
180
+ buffer += decoder.decode(value, { stream: true });
181
+ let splitAt = findSseFrameBoundary(buffer);
182
+ while (splitAt >= 0) {
183
+ const frame = buffer.slice(0, splitAt);
184
+ buffer = buffer.slice(buffer[splitAt] === "\r" ? splitAt + 4 : splitAt + 2);
185
+ handleSseFrame(frame, options);
186
+ splitAt = findSseFrameBoundary(buffer);
187
+ }
188
+ }
189
+ }
190
+ function handleSseFrame(frame, options) {
191
+ const event = parseSseFrame(frame);
192
+ if (event.event === "resync") {
193
+ options.onResync?.();
194
+ return;
195
+ }
196
+ if (event.event !== "update" || event.data.length === 0) return;
197
+ options.onUpdate(JSON.parse(event.data.join("\n")));
198
+ }
199
+ function parseSseFrame(frame) {
200
+ let event = null;
201
+ const data = [];
202
+ for (const line of frame.split(/\r?\n/)) if (line.startsWith("event:")) event = line.slice(6).trim();
203
+ else if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
204
+ return {
205
+ event,
206
+ data
207
+ };
208
+ }
209
+ function findSseFrameBoundary(buffer) {
210
+ const lf = buffer.indexOf("\n\n");
211
+ const crlf = buffer.indexOf("\r\n\r\n");
212
+ if (lf < 0) return crlf;
213
+ if (crlf < 0) return lf;
214
+ return Math.min(lf, crlf);
215
+ }
216
+ //#endregion
217
+ //#region src/entity-crdt/compile-rows.ts
218
+ /** Lower a detached ordinary-object edit against its actual causal Loro baseline. */
219
+ function compileEntityRows(snapshot, rows, validate, options = {}) {
220
+ const doc = new LoroDoc();
221
+ doc.import(snapshot);
222
+ return applyEntityRows(new LoroEntityDocument(doc, validate), rows, options);
223
+ }
224
+ /** Publish final rows as imported causal operations, outside the caller's local undo history. */
225
+ function publishEntityRows(editor, rows, options = {}) {
226
+ const from = editor.doc.oplogVersion();
227
+ const compiled = compileEntityRows(editor.doc.export({ mode: "snapshot" }), rows, void 0, options);
228
+ editor.import(compiled.update);
229
+ return {
230
+ update: editor.doc.export({
231
+ mode: "update",
232
+ from
233
+ }),
234
+ state: editor.snapshot()
235
+ };
236
+ }
237
+ /** Compile an edit on an existing peer, retaining its native local undo history. */
238
+ function applyEntityRows(editor, rows, options = {}) {
239
+ const baseline = editor.snapshot();
240
+ const before = baseline.rows;
241
+ const oldEntities = new Map(before.entities.map((row) => [row.entityId, row]));
242
+ const newEntities = new Map(rows.entities.map((row) => [row.entityId, row]));
243
+ const oldRelations = new Map(before.relations.map((row) => [row.relationId, row]));
244
+ const newRelations = new Map(rows.relations.map((row) => [row.relationId, row]));
245
+ const oldClipContent = new Map(before.relations.filter((row) => row.relationKind === "contain" && oldEntities.get(row.endpoint0EntityId)?.entityKind === "clip").map((row) => [row.endpoint0EntityId, row.relationId]));
246
+ const newClipContent = new Map(rows.relations.filter((row) => row.relationKind === "contain" && newEntities.get(row.endpoint0EntityId)?.entityKind === "clip").map((row) => [row.endpoint0EntityId, row.relationId]));
247
+ if (newEntities.size !== rows.entities.length || newRelations.size !== rows.relations.length) throw new Error("Duplicate domain identity");
248
+ validateEntityRelationTransition(before, rows);
249
+ return {
250
+ update: editor.transact((draft) => {
251
+ for (const row of before.relations) {
252
+ const next = newRelations.get(row.relationId);
253
+ if (!next) draft.unlink(row.relationId);
254
+ else {
255
+ if (next.relationKind !== row.relationKind || next.endpoint0EntityId !== row.endpoint0EntityId || next.endpoint1EntityId !== row.endpoint1EntityId) throw new Error("Relation identity cannot change kind or endpoint positions");
256
+ if (!equalJson(row.trace, next.trace)) draft.patchRelationTrace(row.relationId, next.trace);
257
+ }
258
+ }
259
+ let attached = baseline.project.audioScriptEntityId !== void 0;
260
+ for (const row of rows.entities) {
261
+ const previous = oldEntities.get(row.entityId);
262
+ if (!previous) {
263
+ draft.create(row);
264
+ if (!attached && row.entityKind === "audio-script") {
265
+ draft.attach("audioScriptEntityId", String(row.entityId));
266
+ attached = true;
267
+ }
268
+ } else {
269
+ if (previous.entityKind !== row.entityKind) throw new Error("Entity kind cannot change");
270
+ if (!equalJson(previous.payload, row.payload)) draft.replaceOwned(row.entityId, row.payload);
271
+ }
272
+ }
273
+ for (const row of rows.relations) if (!oldRelations.has(row.relationId)) draft.link(row);
274
+ for (const [clipId, relationId] of newClipContent) if (oldClipContent.get(clipId) !== relationId) draft.selectClipContent(relationId);
275
+ const beforeOrder = entityOrderGroups(before), afterOrder = entityOrderGroups(rows);
276
+ for (const group of new Set([...beforeOrder.keys(), ...afterOrder.keys()])) {
277
+ const next = afterOrder.get(group) ?? [];
278
+ if (!equalJson(beforeOrder.get(group) ?? [], next)) draft.order(group, next);
279
+ }
280
+ for (const row of before.entities) if (!newEntities.has(row.entityId)) draft.delete(row.entityId);
281
+ for (const [slot, entityId] of Object.entries(options.projectAttachments ?? {})) draft.attach(slot, entityId);
282
+ for (const [clipEntityId, phoneticScriptEntityId] of Object.entries(options.phoneticScriptSelections ?? {})) draft.selectPhoneticScript(clipEntityId, phoneticScriptEntityId);
283
+ }),
284
+ state: editor.snapshot()
285
+ };
286
+ }
287
+ //#endregion
288
+ //#region src/entity-editor/document-audio-script.ts
289
+ /** Read only the attached version and preserve every segment; never aggregate other scripts. */
290
+ function readDocumentAudioScript(state) {
291
+ const id = state.audioScriptEntityId;
292
+ if (id === null) throw new EntityTimelineProjectionError("Document AudioScript is not initialized");
293
+ if (typeof id !== "string" || id.trim() !== id || !id) throw new EntityTimelineProjectionError("Invalid document AudioScript identity");
294
+ const entity = state.rows.entities.find((row) => row.entityId === id);
295
+ if (!entity || entity.entityKind !== "audio-script") throw new EntityTimelineProjectionError(`Document AudioScript ${id} must name the project AudioScript`);
296
+ const content = assembleEntityContent(state.rows, createEntityId(id));
297
+ assembleScriptText(content);
298
+ return {
299
+ entityId: id,
300
+ segments: structuredClone(content.payload.segments)
301
+ };
302
+ }
303
+ //#endregion
304
+ //#region src/entity-editor/editor-resources.ts
305
+ const roleOrder$1 = {
306
+ caption: 0,
307
+ video_clip: 1,
308
+ speech: 2,
309
+ bgm: 3
310
+ };
311
+ /**
312
+ * MEngine editor policy, not a cardinality restriction on the general-purpose DSL.
313
+ *
314
+ * Resources live on Asset entities and are reached through `from-asset`: no
315
+ * other entity carries a locator, one Asset claims each `(system, key)` pair,
316
+ * and media reaches at most one Asset. Media whose bytes are not named through
317
+ * that Relation is rejected rather than guessed at.
318
+ */
319
+ function assertCanonicalEditorResources(rows) {
320
+ if (rows.entities.filter((row) => row.entityKind === "timeline").length > 1) throw new EntityTimelineProjectionError("Editor requires exactly one Timeline");
321
+ 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`);
322
+ const identityByAssetKey = /* @__PURE__ */ new Map();
323
+ for (const entity of rows.entities) {
324
+ if (entity.entityKind === "asset") {
325
+ const locator = memotaLocator(entity.payload);
326
+ if (locator === void 0) throw new EntityTimelineProjectionError(`Asset ${entity.entityId} must state where its bytes live`);
327
+ const key = JSON.stringify([locator.system, locator.key]);
328
+ const previous = identityByAssetKey.get(key);
329
+ if (previous !== void 0 && previous !== entity.entityId) throw new EntityTimelineProjectionError(`Duplicate Asset for ${key}; reuse ${previous}`);
330
+ identityByAssetKey.set(key, entity.entityId);
331
+ continue;
332
+ }
333
+ if (memotaLocator(entity.payload) !== void 0) throw new EntityTimelineProjectionError(`Entity ${entity.entityId} must not carry a locator; link its Asset with from-asset`);
334
+ }
335
+ for (const entity of rows.entities) {
336
+ if (!isMediaAssetVariantKind(entity.entityKind)) continue;
337
+ if (assetIdsOf(rows, entity.entityId).length !== 1) throw new EntityTimelineProjectionError(`Media Entity ${entity.entityId} requires exactly one from-asset Asset`);
338
+ }
339
+ }
340
+ /** The Asset ids one entity reaches, in row order. */
341
+ function assetIdsOf(rows, entityId) {
342
+ const assets = new Set(rows.entities.filter((row) => row.entityKind === "asset").map((row) => String(row.entityId)));
343
+ return rows.relations.filter((relation) => relation.relationKind === "from-asset").map((relation) => String(relation.endpoint0EntityId) === entityId ? String(relation.endpoint1EntityId) : String(relation.endpoint1EntityId) === entityId ? String(relation.endpoint0EntityId) : void 0).filter((id) => id !== void 0 && assets.has(id));
344
+ }
345
+ /**
346
+ * Commit-transition policy shared by the editor, the HTTP commit boundary, the
347
+ * host tool, and the entity stores. It compares the authoritative before and
348
+ * after rows of one CAS commit and enforces the resource rules on the complete
349
+ * after state, with no exemption for historical graphs:
350
+ *
351
+ * 1. every media Entity reaches exactly one Asset through `from-asset`;
352
+ * 2. no entity other than an Asset carries a locator;
353
+ * 3. an Asset's locator is immutable — pointing it at other bytes would
354
+ * silently rewrite every entity already made from it.
355
+ *
356
+ * Deleting media is explicit and allowed.
357
+ */
358
+ function assertMediaAssetWritePolicy(before, after) {
359
+ const beforeEntities = new Map(before.entities.map((row) => [String(row.entityId), row]));
360
+ assertCanonicalEditorResources(after);
361
+ for (const relation of after.relations) {
362
+ if (relation.relationKind !== "from-asset" && relation.relationKind !== "to-asset") continue;
363
+ if (!isTranscribedAudioScript(after, relation.endpoint0EntityId)) continue;
364
+ if (before.relations.some((prior) => prior.relationKind === relation.relationKind && prior.endpoint0EntityId === relation.endpoint0EntityId && prior.endpoint1EntityId === relation.endpoint1EntityId)) continue;
365
+ throw new EntityTimelineProjectionError("ASR resources belong to Caption, not the transcribed AudioScript");
366
+ }
367
+ for (const row of after.entities) {
368
+ if (row.entityKind !== "asset") continue;
369
+ const prior = memotaLocator(beforeEntities.get(String(row.entityId))?.payload);
370
+ if (prior === void 0) continue;
371
+ const next = memotaLocator(row.payload);
372
+ if (next !== void 0 && (next.system !== prior.system || next.key !== prior.key)) throw new EntityTimelineProjectionError(`Asset ${row.entityId} cannot be pointed at other bytes; create a new Asset instead`);
373
+ }
374
+ }
375
+ function memotaLocator(value) {
376
+ if (!isJsonObject(value) || value.system !== "memota" && value.system !== "memota-speech" || typeof value.key !== "string" || value.key.trim() === "") return;
377
+ return {
378
+ system: value.system,
379
+ key: value.key
380
+ };
381
+ }
382
+ /**
383
+ * Bootstrap an empty script when the foundation has no candidate. Without an
384
+ * explicit project attachment, multiple candidates are ambiguous. Existing
385
+ * content IDs are reused here; Loro editing advances their immutable versions.
386
+ */
387
+ function ensureDocumentAudioScript(entities, selectedEntityId) {
388
+ if (selectedEntityId != null) {
389
+ if (!entities.some((row) => row.entityKind === "audio-script" && row.entityId === selectedEntityId)) throw new EntityTimelineProjectionError(`Selected AudioScript ${selectedEntityId} does not exist`);
390
+ return selectedEntityId;
391
+ }
392
+ const scripts = entities.filter((row) => row.entityKind === "audio-script");
393
+ if (scripts.length > 1) throw new EntityTimelineProjectionError("Select an AudioScript for the editor panel when multiple scripts exist");
394
+ return scripts[0]?.entityId ?? null;
395
+ }
396
+ /** Add only missing editor structure; existing identities and owned settings are never replaced. */
397
+ function ensureEditorFoundation(initial, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`, timelinePayload = {}, selectedAudioScriptEntityId) {
398
+ const entities = structuredClone([...initial.entities]);
399
+ const relations = structuredClone([...initial.relations]);
400
+ const roots = entities.filter((row) => row.entityKind === "timeline");
401
+ if (roots.length > 1) throw new EntityTimelineProjectionError("Editor requires exactly one Timeline");
402
+ const mintEntity = () => {
403
+ const id = createEntityId(idFactory("entity"));
404
+ if (entities.some((row) => row.entityId === id)) throw new Error(`Duplicate entity id ${id}`);
405
+ return id;
406
+ };
407
+ let timeline = roots[0];
408
+ if (timeline === void 0) {
409
+ timeline = {
410
+ entityId: mintEntity(),
411
+ entityKind: "timeline",
412
+ payload: structuredClone(timelinePayload)
413
+ };
414
+ entities.push(timeline);
415
+ }
416
+ const trackEntityIds = {};
417
+ for (const role of Object.keys(roleOrder$1)) {
418
+ const matches = entities.filter((row) => row.entityKind === "track" && row.payload.role === role);
419
+ if (matches.length > 1) throw new EntityTimelineProjectionError(`Editor has multiple ${role} Tracks`);
420
+ let track = matches[0];
421
+ if (track === void 0) {
422
+ track = {
423
+ entityId: mintEntity(),
424
+ entityKind: "track",
425
+ payload: {
426
+ role,
427
+ order: roleOrder$1[role]
428
+ }
429
+ };
430
+ entities.push(track);
431
+ }
432
+ const bindings = relations.filter((row) => row.relationKind === "contain" && (row.endpoint0EntityId === track.entityId || row.endpoint1EntityId === track.entityId));
433
+ if (bindings.some((row) => row.endpoint0EntityId !== timeline.entityId && row.endpoint1EntityId !== timeline.entityId)) throw new EntityTimelineProjectionError(`Track ${track.entityId} belongs to another Timeline`);
434
+ if (bindings.length === 0) {
435
+ const relationId = createRelationId(idFactory("relation"));
436
+ if (relations.some((row) => row.relationId === relationId)) throw new Error(`Duplicate relation id ${relationId}`);
437
+ relations.push({
438
+ relationId,
439
+ relationKind: "contain",
440
+ endpoint0EntityId: timeline.entityId,
441
+ endpoint1EntityId: track.entityId,
442
+ metadata: {},
443
+ trace: {}
444
+ });
445
+ }
446
+ trackEntityIds[role] = track.entityId;
447
+ }
448
+ const audioScriptEntityId = ensureDocumentAudioScript(entities, selectedAudioScriptEntityId);
449
+ return {
450
+ rows: {
451
+ entities,
452
+ relations
453
+ },
454
+ timelineEntityId: timeline.entityId,
455
+ trackEntityIds,
456
+ audioScriptEntityId
457
+ };
458
+ }
459
+ //#endregion
460
+ //#region src/entity-editor/entity-graph-http-client.ts
461
+ const MENGINE_API_PREFIX = "/api/mengine/v1";
462
+ var EntityGraphRevisionConflictError = class extends MengineHttpRequestError {
463
+ expectedRevision;
464
+ currentRevision;
465
+ constructor(expectedRevision, currentRevision, payload) {
466
+ super(409, payload);
467
+ this.expectedRevision = expectedRevision;
468
+ this.currentRevision = currentRevision;
469
+ this.name = "EntityGraphRevisionConflictError";
470
+ }
471
+ };
472
+ /** Browser-safe authenticated Loro client for a document's Entity graph. */
473
+ var EntityGraphHttpClient = class {
474
+ options;
475
+ transport;
476
+ endpoint;
477
+ constructor(options) {
478
+ this.options = options;
479
+ this.transport = new MengineHttpClient(options);
480
+ const origin = options.httpOrigin.replace(/\/$/, "");
481
+ this.endpoint = `${origin}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(options.docId)}/entity-state`;
482
+ }
483
+ async fetchState() {
484
+ return this.request({ method: "GET" });
485
+ }
486
+ /**
487
+ * Compile edits against the original causal snapshot and publish native Loro operations.
488
+ * The base revision is sent as an optimistic precondition, so a stale adoption
489
+ * fails loudly instead of letting Loro choose between concurrent selections.
490
+ *
491
+ * Deletion intent is derived from baseline IDs omitted from `nextRows`, so a
492
+ * transient create-then-remove never becomes a request to delete unknown
493
+ * persisted data.
494
+ */
495
+ async commit(baseState, nextRows, options = {}) {
496
+ if (options.migrationBaseVv !== void 0) throw new Error("Legacy entity migration is not supported by the Loro contract");
497
+ const next = cloneAndValidateRows(nextRows);
498
+ assertMediaAssetWritePolicy(baseState.rows, next);
499
+ const compiled = compileEntityRows(base64ToBytes(baseState.loroSnapshot), next);
500
+ return this.commitUpdate(bytesToBase64(compiled.update), baseState.revision);
501
+ }
502
+ /** Send prepared bytes directly; omit `expectedRevision` for immutable fact publication. */
503
+ async commitUpdate(update, expectedRevision) {
504
+ if (expectedRevision !== void 0 && !isNonNegativeSafeInteger(expectedRevision)) throw new Error("expectedRevision must be a nonnegative safe integer");
505
+ return this.request({
506
+ method: "POST",
507
+ body: JSON.stringify({
508
+ update,
509
+ ...expectedRevision === void 0 ? {} : { expected_revision: expectedRevision }
510
+ })
511
+ });
512
+ }
513
+ async request(init) {
514
+ const response = await this.transport.fetch(this.endpoint, {
515
+ ...init,
516
+ headers: this.transport.headers()
517
+ });
518
+ const payload = await safeReadJson(response);
519
+ if (!response.ok) throw parseRevisionConflict(response.status, payload) ?? new MengineHttpRequestError(response.status, payload);
520
+ return parseWireState(payload, this.options.docId);
521
+ }
522
+ };
523
+ function parseRevisionConflict(status, value) {
524
+ if (status !== 409 || !isRecord(value) || !isRecord(value.error) || value.error.code !== "revision_conflict") return;
525
+ const error = value.error;
526
+ if (typeof error.message !== "string" || !isNonNegativeSafeInteger(error.expected_revision) || !isNonNegativeSafeInteger(error.current_revision)) return;
527
+ return new EntityGraphRevisionConflictError(error.expected_revision, error.current_revision, { error: {
528
+ code: "revision_conflict",
529
+ message: error.message,
530
+ expected_revision: error.expected_revision,
531
+ current_revision: error.current_revision
532
+ } });
533
+ }
534
+ function parseWireState(value, expectedDocId) {
535
+ if (!isRecord(value) || typeof value.doc_id !== "string" || !isNonNegativeSafeInteger(value.revision)) throw new Error("Invalid entity-state response envelope");
536
+ if (value.doc_id !== expectedDocId) throw new Error(`Entity-state response doc_id mismatch: expected "${expectedDocId}"`);
537
+ const rows = parseWireRows(value.rows);
538
+ decodeEntityRelationRows(rows, {});
539
+ const attached = value.audio_script_entity_id;
540
+ if (attached !== null && typeof attached !== "string") throw new Error("Invalid document AudioScript identity");
541
+ if (typeof value.loro_snapshot !== "string") throw new Error("Missing causal Loro snapshot");
542
+ const state = {
543
+ revision: value.revision,
544
+ loroSnapshot: value.loro_snapshot,
545
+ rows,
546
+ audioScriptEntityId: attached
547
+ };
548
+ if (attached !== null) readDocumentAudioScript(state);
549
+ return state;
550
+ }
551
+ function parseWireRows(value) {
552
+ if (!isRecord(value) || !Array.isArray(value.entities) || !Array.isArray(value.relations)) throw new Error("Invalid entity-state response rows");
553
+ return {
554
+ entities: value.entities.map(parseWireEntity),
555
+ relations: value.relations.map(parseWireRelation)
556
+ };
557
+ }
558
+ function parseWireEntity(value) {
559
+ if (!isRecord(value) || !isJsonObject(value.payload)) throw new Error("Invalid Entity row in entity-state response");
560
+ return {
561
+ entityId: createEntityId(requiredString(value.entity_id, "entity_id")),
562
+ entityKind: requiredString(value.entity_kind, "entity_kind"),
563
+ payload: structuredClone(value.payload)
564
+ };
565
+ }
566
+ function parseWireRelation(value) {
567
+ if (!isRecord(value) || !isJsonObject(value.metadata) || !isJsonObject(value.trace)) throw new Error("Invalid Relation row in entity-state response");
568
+ return {
569
+ relationId: createRelationId(requiredString(value.relation_id, "relation_id")),
570
+ relationKind: requiredString(value.relation_kind, "relation_kind"),
571
+ endpoint0EntityId: createEntityId(requiredString(value.endpoint_0_entity_id, "endpoint_0_entity_id")),
572
+ endpoint1EntityId: createEntityId(requiredString(value.endpoint_1_entity_id, "endpoint_1_entity_id")),
573
+ metadata: structuredClone(value.metadata),
574
+ trace: structuredClone(value.trace)
575
+ };
576
+ }
577
+ function cloneAndValidateRows(rows) {
578
+ const clone = structuredClone(rows);
579
+ decodeEntityRelationRows(clone, {});
580
+ return clone;
581
+ }
582
+ function requiredString(value, field) {
583
+ if (typeof value !== "string" || value.length === 0 || value.trim() !== value) throw new Error(`Invalid ${field} in entity-state response`);
584
+ return value;
585
+ }
586
+ function isRecord(value) {
587
+ return value !== null && typeof value === "object" && !Array.isArray(value);
588
+ }
589
+ function isNonNegativeSafeInteger(value) {
590
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
591
+ }
592
+ async function safeReadJson(response) {
593
+ const text = await response.text();
594
+ if (text.length === 0) return null;
595
+ try {
596
+ return JSON.parse(text);
597
+ } catch {
598
+ return text;
599
+ }
600
+ }
601
+ //#endregion
602
+ //#region src/entity-editor/media-asset-import.ts
603
+ /**
604
+ * Import at the editor boundary, not at asset generation time. Existing
605
+ * provenance is retained.
606
+ *
607
+ * The bytes are named once, on an Asset entity, and the media entity reaches
608
+ * them through a `from-asset` Relation. Re-importing the same `(system, key)`
609
+ * reuses that Asset and the media already made from it, so one stored resource
610
+ * is never copied into several rows.
611
+ */
612
+ function importMediaAsset(initial, fact, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`) {
613
+ if (!fact.assetId || fact.assetId.trim() !== fact.assetId) throw new Error("Expected an asset id");
614
+ if (![
615
+ "image",
616
+ "video",
617
+ "audio",
618
+ "voiceover-recording"
619
+ ].includes(fact.kind)) throw new Error("Unsupported media kind");
620
+ if (fact.kind !== "image" && (!Number.isFinite(fact.durationMs) || fact.durationMs <= 0)) throw new Error(`${fact.kind} ${fact.assetId} requires its factual positive duration`);
621
+ if ((fact.kind === "audio" || fact.kind === "voiceover-recording") && fact.storageKey.trim() === "") throw new Error(`${fact.kind} ${fact.assetId} requires its physical storage key`);
622
+ decodeEntityRelationRows(initial, {});
623
+ assertCanonicalEditorResources(initial);
624
+ const entities = structuredClone([...initial.entities]);
625
+ const relations = structuredClone([...initial.relations]);
626
+ const system = fact.kind === "voiceover-recording" ? "memota-speech" : "memota";
627
+ const entityKind = fact.kind;
628
+ const timbre = fact.kind === "voiceover-recording" ? fact.voice : void 0;
629
+ const durationMs = fact.kind === "image" ? null : fact.durationMs;
630
+ const asset = ensureAsset(entities, {
631
+ system,
632
+ key: fact.assetId,
633
+ storageKey: fact.storageKey
634
+ }, idFactory);
635
+ const madeFromAsset = entities.filter((row) => row.entityId !== asset.entityId && assetIdOf(relations, row.entityId) === asset.entityId);
636
+ if (madeFromAsset.length > 1) throw new Error(`Duplicate media identities for ${fact.assetId}; reuse ${madeFromAsset[0].entityId}`);
637
+ const existing = madeFromAsset[0];
638
+ if (existing !== void 0) {
639
+ if (existing.entityKind !== entityKind || existing.payload.durationMs !== durationMs) throw new Error(`Media facts conflict with existing identity for ${fact.assetId}`);
640
+ const voiceEntityId = bindVoiceTimbre(entities, relations, existing.entityId, timbre, idFactory);
641
+ return {
642
+ rows: {
643
+ entities,
644
+ relations
645
+ },
646
+ contentEntityId: existing.entityId,
647
+ assetEntityId: asset.entityId,
648
+ ...voiceEntityId
649
+ };
650
+ }
651
+ const entityId = createEntityId(idFactory("entity"));
652
+ if (entities.some((row) => row.entityId === entityId)) throw new Error(`Duplicate entity id ${entityId}`);
653
+ entities.push({
654
+ entityId,
655
+ entityKind,
656
+ payload: { durationMs }
657
+ });
658
+ relations.push(assetLink(entityId, asset.entityId, idFactory));
659
+ if (fact.kind === "voiceover-recording" && fact.phoneticScriptEntityId !== void 0) {
660
+ const phonetic = entities.find((row) => String(row.entityId) === fact.phoneticScriptEntityId);
661
+ if (phonetic?.entityKind !== "phonetic-script") throw new Error(`Voiceover ${fact.assetId} requires the PhoneticScript it was spoken from`);
662
+ relations.push({
663
+ relationId: createRelationId(idFactory("relation")),
664
+ relationKind: "derived-from",
665
+ endpoint0EntityId: entityId,
666
+ endpoint1EntityId: phonetic.entityId,
667
+ metadata: {},
668
+ trace: {}
669
+ });
670
+ }
671
+ const voiceEntityId = bindVoiceTimbre(entities, relations, entityId, timbre, idFactory);
672
+ const rows = {
673
+ entities,
674
+ relations
675
+ };
676
+ decodeEntityRelationRows(rows, {});
677
+ assertCanonicalEditorResources(rows);
678
+ return {
679
+ rows,
680
+ contentEntityId: entityId,
681
+ assetEntityId: asset.entityId,
682
+ ...voiceEntityId
683
+ };
684
+ }
685
+ /** One Asset per `(system, key)`: the bytes are named in exactly one row. */
686
+ function ensureAsset(entities, locator, idFactory) {
687
+ const found = entities.find((row) => row.entityKind === "asset" && row.payload.system === locator.system && row.payload.key === locator.key);
688
+ if (found !== void 0) {
689
+ if (locator.storageKey === void 0 || found.payload.storageKey === locator.storageKey) return found;
690
+ if (found.payload.storageKey !== void 0) throw new Error(`Media facts conflict with existing storage key for ${locator.key}`);
691
+ const filled = {
692
+ ...found,
693
+ payload: {
694
+ ...found.payload,
695
+ storageKey: locator.storageKey
696
+ }
697
+ };
698
+ replaceEntity$1(entities, found.entityId, filled);
699
+ return filled;
700
+ }
701
+ const asset = {
702
+ entityId: createEntityId(idFactory("entity")),
703
+ entityKind: "asset",
704
+ payload: {
705
+ system: locator.system,
706
+ key: locator.key,
707
+ ...locator.storageKey === void 0 ? {} : { storageKey: locator.storageKey }
708
+ }
709
+ };
710
+ entities.push(asset);
711
+ return asset;
712
+ }
713
+ function assetLink(entityId, assetEntityId, idFactory) {
714
+ return {
715
+ relationId: createRelationId(idFactory("relation")),
716
+ relationKind: "from-asset",
717
+ endpoint0EntityId: createEntityId(entityId),
718
+ endpoint1EntityId: createEntityId(assetEntityId),
719
+ metadata: {},
720
+ trace: {}
721
+ };
722
+ }
723
+ function assetIdOf(relations, entityId) {
724
+ const link = relations.find((relation) => relation.relationKind === "from-asset" && (relation.endpoint0EntityId === entityId || relation.endpoint1EntityId === entityId));
725
+ if (link === void 0) return void 0;
726
+ return String(link.endpoint0EntityId === entityId ? link.endpoint1EntityId : link.endpoint0EntityId);
727
+ }
728
+ /**
729
+ * Attach the timbre a synthesized voiceover was rendered with. One Voice
730
+ * identity is shared by every take naming the same library entry, so an
731
+ * existing row is reused rather than duplicated. Recorded media declares no
732
+ * timbre and keeps no link.
733
+ */
734
+ function bindVoiceTimbre(entities, relations, audioEntityId, descriptor, idFactory) {
735
+ const linked = relations.filter((relation) => relation.relationKind === "voice-timbre" && (relation.endpoint0EntityId === audioEntityId || relation.endpoint1EntityId === audioEntityId));
736
+ if (linked.length > 1) throw new Error(`Voiceover ${audioEntityId} has multiple Voice timbre relations`);
737
+ if (descriptor === void 0) {
738
+ if (linked.length > 0) throw new Error(`Recorded media ${audioEntityId} cannot keep a synthesized Voice timbre`);
739
+ return {};
740
+ }
741
+ const voice = entities.find((row) => row.entityKind === "voice" && sameVoiceDescriptor(row.payload.voice, descriptor)) ?? appendVoiceIdentity(entities, descriptor, idFactory);
742
+ const existing = linked[0];
743
+ if (existing === void 0) relations.push({
744
+ relationId: createRelationId(idFactory("relation")),
745
+ relationKind: "voice-timbre",
746
+ endpoint0EntityId: createEntityId(audioEntityId),
747
+ endpoint1EntityId: createEntityId(voice.entityId),
748
+ metadata: {},
749
+ trace: {}
750
+ });
751
+ else if (existing.endpoint0EntityId !== audioEntityId || existing.endpoint1EntityId !== voice.entityId) throw new Error(`Voiceover ${audioEntityId} already carries a different Voice timbre`);
752
+ return { voiceEntityId: voice.entityId };
753
+ }
754
+ function appendVoiceIdentity(entities, descriptor, idFactory) {
755
+ const voice = {
756
+ entityId: createEntityId(idFactory("entity")),
757
+ entityKind: "voice",
758
+ payload: { voice: {
759
+ system: descriptor.system,
760
+ key: descriptor.key,
761
+ ...descriptor.name === void 0 ? {} : { name: descriptor.name }
762
+ } }
763
+ };
764
+ entities.push(voice);
765
+ return voice;
766
+ }
767
+ /** Two takes share a Voice identity only when they name the same library entry. */
768
+ function sameVoiceDescriptor(stored, descriptor) {
769
+ return isJsonObject(stored) && stored.system === descriptor.system && stored.key === descriptor.key && stored.name === descriptor.name;
770
+ }
771
+ function replaceEntity$1(entities, entityId, replacement) {
772
+ const index = entities.findIndex((row) => row.entityId === entityId);
773
+ if (index < 0) throw new Error(`Entity id ${entityId} does not exist`);
774
+ entities[index] = replacement;
775
+ }
776
+ //#endregion
777
+ //#region src/entity-editor/entity-timeline-editor.ts
778
+ /**
779
+ * Resolve the AudioScript a PhoneticScript composes. The composition is the
780
+ * required text source: a variant without it has no content and fails here.
781
+ */
782
+ function requireComposedAudioScript(rows, phoneticScriptEntityId) {
783
+ return assemblePhoneticScriptContent(rows, phoneticScriptEntityId).audioScript.entityId;
784
+ }
785
+ function captionSelection(selection) {
786
+ return structuredClone(selection);
787
+ }
788
+ function captionBaseIds(ids) {
789
+ if (!Array.isArray(ids) || ids.length === 0) throw new Error("Caption requires non-empty baseEntityIds");
790
+ return ids.map(createEntityId);
791
+ }
792
+ function assertCaptionScript(rows, captionId, scriptId) {
793
+ if (assembleCaptionContent(rows, captionId).audioScript.entityId !== scriptId) throw new Error("Voiceover Caption must compose the same AudioScript as the PhoneticScript");
794
+ }
795
+ const roleOrder = {
796
+ caption: 0,
797
+ video_clip: 1,
798
+ speech: 2,
799
+ bgm: 3
800
+ };
801
+ const defaultIdFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`;
802
+ /**
803
+ * Graph-native timeline authoring over authoritative Medeo Entity and Relation rows.
804
+ *
805
+ * Each mutation is applied to a detached graph, validated as a complete DSL set,
806
+ * and committed only after validation succeeds.
807
+ */
808
+ var EntityTimelineEditor = class {
809
+ idFactory;
810
+ state;
811
+ constructor(initialRows, idFactory = defaultIdFactory) {
812
+ this.idFactory = idFactory;
813
+ this.state = cloneRows(initialRows);
814
+ }
815
+ rows() {
816
+ return cloneRows(this.state);
817
+ }
818
+ insertClip(input) {
819
+ const requestedOrder = input.order;
820
+ const order = typeof requestedOrder === "number" ? requestedOrder : nextSequentialOrder(this.state, createEntityId(input.trackEntityId));
821
+ return this.insertPlacedClip({
822
+ trackEntityId: input.trackEntityId,
823
+ contentEntityId: input.contentEntityId,
824
+ inMs: input.inMs,
825
+ outMs: input.outMs,
826
+ placement: input.startMs === void 0 ? {
827
+ kind: "sequential",
828
+ order
829
+ } : {
830
+ kind: "absolute",
831
+ startMs: input.startMs
832
+ },
833
+ clipPayload: input.clipPayload
834
+ });
835
+ }
836
+ insertPlacedClip(input) {
837
+ return this.transact((draft) => this.insertPlacedClipIntoDraft(draft, input));
838
+ }
839
+ updateClipMarker(input) {
840
+ this.transact((draft) => {
841
+ const { marker } = requireClipTopology(draft, input.clipEntityId);
842
+ const payload = { ...cloneJsonObject(marker.payload, "Sequence Marker payload") };
843
+ if (input.inMs !== void 0) payload.inMs = finiteNonNegative(input.inMs, "inMs");
844
+ if (input.outMs !== void 0) payload.outMs = finitePositive(input.outMs, "outMs");
845
+ if (Object.hasOwn(input, "startMs")) if (input.startMs === void 0) {
846
+ delete payload.startMs;
847
+ if (!hasOutgoingClipAnchor(draft, input.clipEntityId)) {}
848
+ } else payload.startMs = finiteNonNegative(input.startMs, "startMs");
849
+ if (Object.hasOwn(input, "rate")) if (input.rate === void 0) delete payload.rate;
850
+ else payload.rate = finitePositive(input.rate, "linear speed rate");
851
+ replaceEntity(draft, marker.entityId, {
852
+ ...marker,
853
+ payload
854
+ });
855
+ });
856
+ }
857
+ setClipPlacement(input) {
858
+ this.transact((draft) => this.applyClipPlacement(draft, input.clipEntityId, input.placement));
859
+ }
860
+ moveSequentialClips(input) {
861
+ if (input.clipEntityIds.length === 0) throw new Error("At least one Clip is required");
862
+ this.transact((draft) => {
863
+ const original = resolveEntityTimelineLayout(draft);
864
+ this.normalizeMainSequential(draft);
865
+ const before = resolveEntityTimelineLayout(draft);
866
+ const targetIds = new Set(input.clipEntityIds);
867
+ if (targetIds.size !== input.clipEntityIds.length) throw new Error("Clip ids must be unique");
868
+ const placements = input.clipEntityIds.map((id) => before.clips.get(id));
869
+ if (placements.some((placement) => placement === void 0 || placement.mode !== "sequential")) throw new Error("moveSequentialClips accepts only sequential Clips");
870
+ const trackIds = new Set(placements.map((placement) => placement.trackEntityId));
871
+ if (trackIds.size !== 1) throw new Error("Moved Clips must belong to one Track");
872
+ const trackId = [...trackIds][0];
873
+ const track = before.tracks.find((candidate) => candidate.entityId === trackId);
874
+ if (track?.role !== "video_clip") throw new Error("Only the video_clip Track supports sequential block moves");
875
+ const ordered = track.clips.filter((placement) => placement.mode === "sequential").map((placement) => placement.clipEntityId);
876
+ if (input.anchor.position !== "trackStart" && targetIds.has(input.anchor.clipEntityId)) throw new Error("Move anchor cannot be one of the moved Clips");
877
+ const moved = ordered.filter((id) => targetIds.has(id));
878
+ if (moved.length !== targetIds.size) throw new Error("Every moved Clip must exist on the target Track");
879
+ const remaining = ordered.filter((id) => !targetIds.has(id));
880
+ let insertAt = 0;
881
+ if (input.anchor.position !== "trackStart") {
882
+ const anchorIndex = remaining.indexOf(input.anchor.clipEntityId);
883
+ if (anchorIndex < 0) throw new Error("Move anchor Clip does not exist on the target Track");
884
+ insertAt = anchorIndex + (input.anchor.position === "after" ? 1 : 0);
885
+ }
886
+ [
887
+ ...remaining.slice(0, insertAt),
888
+ ...moved,
889
+ ...remaining.slice(insertAt)
890
+ ].forEach((clipId, order) => setClipOrder(draft, clipId, order));
891
+ if (input.onAnchored === "keepAbsolute") {
892
+ const affected = outgoingAnchorChildren(draft, new Set(track.clips.map((clip) => clip.clipEntityId))).filter((childId) => clipRole(draft, childId) === "speech").map((childId) => {
893
+ const placement = original.clips.get(childId);
894
+ if (placement === void 0) throw new Error(`Anchored child Clip "${childId}" is not placed`);
895
+ return {
896
+ childId,
897
+ start: placement.start
898
+ };
899
+ });
900
+ const after = resolveEntityTimelineLayout(draft);
901
+ for (const child of affected) this.reanchorAtAbsolute(draft, child.childId, child.start, after);
902
+ }
903
+ this.resolveOneVoiceoverOverlap(draft);
904
+ });
905
+ }
906
+ moveClipsToStarts(input) {
907
+ if (input.moves.length === 0) throw new Error("At least one Clip move is required");
908
+ this.transact((draft) => {
909
+ const original = resolveEntityTimelineLayout(draft);
910
+ const originalVideo = original.tracks.find((track) => track.role === "video_clip");
911
+ if (originalVideo === void 0) throw new Error("The timeline has no video_clip Track");
912
+ const preservedVoiceovers = outgoingAnchorChildren(draft, new Set(originalVideo.clips.map((clip) => clip.clipEntityId))).filter((clipId) => clipRole(draft, clipId) === "speech").map((clipId) => ({
913
+ clipId,
914
+ start: original.clips.get(clipId).start
915
+ }));
916
+ this.normalizeMainSequential(draft);
917
+ for (const move of input.moves) {
918
+ finiteNonNegative(move.newStartMs, "newStartMs");
919
+ requireClipOnRole(draft, move.clipEntityId, "video_clip");
920
+ const track = resolveEntityTimelineLayout(draft).tracks.find((candidate) => candidate.role === "video_clip");
921
+ const ordered = track.clips.map((clip) => clip.clipEntityId);
922
+ const from = ordered.indexOf(move.clipEntityId);
923
+ if (from < 0) throw new Error(`Clip "${move.clipEntityId}" is not on the video_clip Track`);
924
+ const remaining = track.clips.filter((clip) => clip.clipEntityId !== move.clipEntityId);
925
+ let cursor = 0;
926
+ let target = remaining.length;
927
+ for (const [index, clip] of remaining.entries()) {
928
+ if (move.newStartMs <= cursor + clip.duration / 2) {
929
+ target = index;
930
+ break;
931
+ }
932
+ cursor += clip.duration;
933
+ }
934
+ const next = [...ordered];
935
+ next.splice(from, 1);
936
+ next.splice(target, 0, move.clipEntityId);
937
+ next.forEach((clipId, order) => setClipOrder(draft, clipId, order));
938
+ }
939
+ const after = resolveEntityTimelineLayout(draft);
940
+ for (const voiceover of preservedVoiceovers) this.reanchorAtAbsolute(draft, voiceover.clipId, voiceover.start, after);
941
+ this.resolveOneVoiceoverOverlap(draft);
942
+ });
943
+ }
944
+ moveClip(input) {
945
+ this.transact((draft) => {
946
+ const topology = requireClipTopology(draft, input.clipEntityId);
947
+ const track = requireEntityKind(draft, input.trackEntityId, "track");
948
+ requireTrackTimeline(draft, track.entityId);
949
+ this.relinkRelation(draft, topology.trackRelation, {
950
+ endpoint0EntityId: track.entityId,
951
+ endpoint1EntityId: topology.clip.entityId
952
+ });
953
+ });
954
+ }
955
+ replaceClipContent(input) {
956
+ this.transact((draft) => {
957
+ const topology = requireClipTopology(draft, input.clipEntityId);
958
+ const content = requireSequenceContent(draft, input.contentEntityId);
959
+ const payload = {
960
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
961
+ ...requireWindow(input.inMs, input.outMs)
962
+ };
963
+ delete payload.startMs;
964
+ delete payload.rate;
965
+ if (input.startMs !== void 0) payload.startMs = finiteNonNegative(input.startMs, "startMs");
966
+ if (input.rate !== void 0) payload.rate = finitePositive(input.rate, "linear speed rate");
967
+ replaceEntity(draft, topology.marker.entityId, {
968
+ ...topology.marker,
969
+ payload
970
+ });
971
+ this.relinkRelation(draft, topology.contentRelation, {
972
+ endpoint0EntityId: topology.clip.entityId,
973
+ endpoint1EntityId: content.entityId
974
+ });
975
+ });
976
+ }
977
+ insertMediaClip(input) {
978
+ return this.transact((draft) => {
979
+ if (input.placement.kind === "sequential") this.normalizeMainSequential(draft);
980
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
981
+ replaceRows(draft, imported.rows);
982
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
983
+ const window = requireWindow(input.inMs, input.outMs);
984
+ const clipId = this.insertPlacedClipIntoDraft(draft, {
985
+ trackEntityId: track.entityId,
986
+ contentEntityId: imported.contentEntityId,
987
+ ...window,
988
+ placement: input.placement,
989
+ ...input.clipEntityId === void 0 ? {} : { clipEntityId: input.clipEntityId },
990
+ ...input.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(input.volume) } }
991
+ });
992
+ if (input.placement.kind === "sequential") this.resolveOneVoiceoverOverlap(draft);
993
+ return clipId;
994
+ });
995
+ }
996
+ insertMediaClips(input) {
997
+ if (input.clips.length === 0) throw new Error("At least one media Clip is required");
998
+ return this.transact((draft) => {
999
+ this.normalizeMainSequential(draft);
1000
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
1001
+ const ordered = resolveEntityTimelineLayout(draft).tracks.find((candidate) => candidate.entityId === track.entityId)?.clips ?? [];
1002
+ let insertAt = ordered.length;
1003
+ const insertion = input.insertion;
1004
+ if (insertion.kind === "before" || insertion.kind === "after") {
1005
+ const anchorIndex = ordered.findIndex((clip) => clip.clipEntityId === insertion.clipEntityId);
1006
+ if (anchorIndex >= 0) insertAt = anchorIndex + (insertion.kind === "after" ? 1 : 0);
1007
+ } else {
1008
+ const startMs = finiteNonNegative(insertion.startMs, "firstStart.startMs");
1009
+ let cursor = 0;
1010
+ for (const [index, clip] of ordered.entries()) {
1011
+ if (startMs <= cursor + clip.duration / 2) {
1012
+ insertAt = index;
1013
+ break;
1014
+ }
1015
+ cursor += clip.duration;
1016
+ }
1017
+ }
1018
+ const created = [];
1019
+ for (const [offset, clip] of input.clips.entries()) {
1020
+ const imported = importMediaAsset(draft, clip.media, this.idFactory);
1021
+ replaceRows(draft, imported.rows);
1022
+ const window = requireWindow(clip.inMs, clip.outMs);
1023
+ created.push(this.insertPlacedClipIntoDraft(draft, {
1024
+ trackEntityId: track.entityId,
1025
+ contentEntityId: imported.contentEntityId,
1026
+ ...window,
1027
+ placement: {
1028
+ kind: "sequential",
1029
+ order: ordered.length + offset
1030
+ },
1031
+ ...clip.clipEntityId === void 0 ? {} : { clipEntityId: clip.clipEntityId },
1032
+ ...clip.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(clip.volume) } }
1033
+ }));
1034
+ }
1035
+ const desired = ordered.map((clip) => clip.clipEntityId);
1036
+ desired.splice(insertAt, 0, ...created);
1037
+ desired.forEach((clipId, order) => setClipOrder(draft, clipId, order));
1038
+ this.resolveOneVoiceoverOverlap(draft);
1039
+ return created;
1040
+ });
1041
+ }
1042
+ replaceMediaClip(input) {
1043
+ this.transact((draft) => {
1044
+ this.normalizeMainSequential(draft);
1045
+ const topology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
1046
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
1047
+ replaceRows(draft, imported.rows);
1048
+ const window = requireWindow(input.inMs, input.outMs);
1049
+ const markerPayloadValue = {
1050
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
1051
+ ...window
1052
+ };
1053
+ delete markerPayloadValue.rate;
1054
+ replaceEntity(draft, topology.marker.entityId, {
1055
+ ...topology.marker,
1056
+ payload: markerPayloadValue
1057
+ });
1058
+ this.relinkRelation(draft, topology.contentRelation, {
1059
+ endpoint0EntityId: topology.clip.entityId,
1060
+ endpoint1EntityId: createEntityId(imported.contentEntityId)
1061
+ });
1062
+ this.resolveOneVoiceoverOverlap(draft);
1063
+ });
1064
+ }
1065
+ setClipVolume(input) {
1066
+ this.transact((draft) => {
1067
+ const topology = requireClipTopology(draft, input.clipEntityId);
1068
+ requireSupportedRole(draft, topology);
1069
+ replaceEntity(draft, topology.clip.entityId, {
1070
+ ...topology.clip,
1071
+ payload: {
1072
+ ...topology.clip.payload,
1073
+ volume: requireVolume(input.volume)
1074
+ }
1075
+ });
1076
+ });
1077
+ }
1078
+ setClipSpeed(input) {
1079
+ this.transact((draft) => {
1080
+ const initialTopology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
1081
+ const content = requireSequenceContent(draft, otherEndpoint(initialTopology.contentRelation, initialTopology.clip.entityId));
1082
+ if (content.entityKind !== "video" && content.entityKind !== "image") throw new Error("Clip speed is supported only for Video or Image content");
1083
+ const initialPayload = cloneJsonObject(initialTopology.marker.payload, "Sequence Marker payload");
1084
+ const priorWindow = markerWindow(initialPayload);
1085
+ if (input.rate === null && initialPayload.rate === void 0) return;
1086
+ this.normalizeMainSequential(draft);
1087
+ const topology = requireClipTopology(draft, input.clipEntityId);
1088
+ const payload = { ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload") };
1089
+ const window = content.entityKind === "image" && initialPayload.rate === void 0 && input.rate !== null ? {
1090
+ inMs: 0,
1091
+ outMs: markerDuration(initialPayload, priorWindow)
1092
+ } : priorWindow;
1093
+ Object.assign(payload, window);
1094
+ if (input.rate === null) delete payload.rate;
1095
+ else {
1096
+ const rate = finitePositive(input.rate, "linear speed rate");
1097
+ const duration = Math.round((window.outMs - window.inMs) / rate);
1098
+ if (duration <= 0 || !Number.isSafeInteger(duration)) throw new Error("Linear speed produces an invalid duration");
1099
+ payload.rate = rate;
1100
+ }
1101
+ replaceEntity(draft, topology.marker.entityId, {
1102
+ ...topology.marker,
1103
+ payload
1104
+ });
1105
+ this.resolveOneVoiceoverOverlap(draft);
1106
+ });
1107
+ }
1108
+ trimClip(input) {
1109
+ this.transact((draft) => {
1110
+ this.normalizeMainSequential(draft);
1111
+ const topology = requireClipOnRole(draft, input.clipEntityId, "video_clip");
1112
+ const content = requireSequenceContent(draft, otherEndpoint(topology.contentRelation, topology.clip.entityId));
1113
+ if (content.entityKind !== "video" && content.entityKind !== "image") throw new Error("Clip trimming is supported only for Video or Image content");
1114
+ const window = requireWindow(input.inMs, input.outMs);
1115
+ const payload = {
1116
+ ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload"),
1117
+ ...window
1118
+ };
1119
+ const duration = markerDuration(payload, window);
1120
+ if (duration <= 0 || !Number.isSafeInteger(duration)) throw new Error("Trim produces an invalid duration");
1121
+ replaceEntity(draft, topology.marker.entityId, {
1122
+ ...topology.marker,
1123
+ payload
1124
+ });
1125
+ this.resolveOneVoiceoverOverlap(draft);
1126
+ });
1127
+ }
1128
+ replaceSequentialClips(input) {
1129
+ if (input.oldClipEntityIds.length === 0 || input.newClips.length === 0) throw new Error("Replacement requires at least one old and one new Clip");
1130
+ return this.transact((draft) => {
1131
+ this.normalizeMainSequential(draft);
1132
+ const before = resolveEntityTimelineLayout(draft);
1133
+ const oldIds = new Set(input.oldClipEntityIds);
1134
+ if (oldIds.size !== input.oldClipEntityIds.length) throw new Error("Old Clip ids must be unique");
1135
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "video_clip");
1136
+ const ordered = before.tracks.find((candidate) => candidate.entityId === track.entityId)?.clips.filter((clip) => clip.mode === "sequential").map((clip) => clip.clipEntityId);
1137
+ if (ordered === void 0) throw new Error("The video_clip Track is not present in the current layout");
1138
+ const indexes = input.oldClipEntityIds.map((id) => ordered.indexOf(id));
1139
+ if (indexes.some((index) => index < 0)) throw new Error("Every replaced Clip must be sequential on video_clip");
1140
+ const sortedIndexes = [...indexes].sort((left, right) => left - right);
1141
+ if (sortedIndexes.some((index, offset) => index !== sortedIndexes[0] + offset)) throw new Error("Replaced Clips must form one contiguous timeline run");
1142
+ if (input.oldClipEntityIds.some((id, index) => id !== ordered[sortedIndexes[0] + index])) throw new Error("Replaced Clip ids must be listed in timeline order");
1143
+ const insertOrder = sortedIndexes[0];
1144
+ const temporaryOrder = ordered.length;
1145
+ const created = [];
1146
+ for (const [offset, replacement] of input.newClips.entries()) {
1147
+ if (replacement.clipEntityId !== void 0 && oldIds.has(replacement.clipEntityId)) throw new Error("A replacement Clip id cannot reuse an Entity being removed");
1148
+ const imported = importMediaAsset(draft, replacement.media, this.idFactory);
1149
+ replaceRows(draft, imported.rows);
1150
+ const window = requireWindow(replacement.inMs, replacement.outMs);
1151
+ created.push(this.insertPlacedClipIntoDraft(draft, {
1152
+ trackEntityId: track.entityId,
1153
+ contentEntityId: imported.contentEntityId,
1154
+ ...window,
1155
+ placement: {
1156
+ kind: "sequential",
1157
+ order: temporaryOrder + offset
1158
+ },
1159
+ ...replacement.clipEntityId === void 0 ? {} : { clipEntityId: replacement.clipEntityId },
1160
+ ...replacement.volume === void 0 ? {} : { clipPayload: { volume: requireVolume(replacement.volume) } }
1161
+ }));
1162
+ }
1163
+ for (const [index, oldId] of input.oldClipEntityIds.entries()) {
1164
+ const children = directAnchorChildren(draft, oldId);
1165
+ if (input.onAnchored === "remap" && created[index] !== void 0) for (const childId of children) this.replaceAnchorHost(draft, childId, created[index]);
1166
+ else {
1167
+ const scheduled = /* @__PURE__ */ new Set();
1168
+ for (const childId of children) {
1169
+ scheduled.add(childId);
1170
+ this.removeAnchoredDescendants(draft, childId, scheduled);
1171
+ this.removeClipGraph(draft, childId);
1172
+ }
1173
+ }
1174
+ }
1175
+ for (const oldId of input.oldClipEntityIds) this.removeClipGraph(draft, oldId);
1176
+ [
1177
+ ...ordered.slice(0, insertOrder),
1178
+ ...created,
1179
+ ...ordered.slice(insertOrder + input.oldClipEntityIds.length)
1180
+ ].forEach((clipId, order) => setClipOrder(draft, clipId, order));
1181
+ this.resolveOneVoiceoverOverlap(draft);
1182
+ return created;
1183
+ });
1184
+ }
1185
+ deleteClip(input) {
1186
+ this.transact((draft) => {
1187
+ const topology = requireClipTopology(draft, input.clipEntityId);
1188
+ assertOnlyStructuralIncidents(draft, topology.clip.entityId, new Set(["contain", "marker"]));
1189
+ assertOnlyStructuralIncidents(draft, topology.marker.entityId, new Set(["marker"]));
1190
+ const markerIsExclusive = incidentRelations(draft, topology.marker.entityId).filter((relation) => relation.relationKind === "marker").length === 1;
1191
+ const removedRelationIds = new Set([
1192
+ topology.trackRelation.relationId,
1193
+ topology.clipMarkerRelation.relationId,
1194
+ topology.contentRelation.relationId
1195
+ ]);
1196
+ if (markerIsExclusive) for (const relation of incidentRelations(draft, topology.marker.entityId)) removedRelationIds.add(relation.relationId);
1197
+ draft.relations = draft.relations.filter((relation) => !removedRelationIds.has(relation.relationId));
1198
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== topology.clip.entityId && (!markerIsExclusive || entity.entityId !== topology.marker.entityId));
1199
+ });
1200
+ }
1201
+ deleteClipTree(input) {
1202
+ if (input.clipEntityIds.length === 0) throw new Error("At least one Clip is required");
1203
+ this.transact((draft) => {
1204
+ const targets = new Set(input.clipEntityIds);
1205
+ if (targets.size !== input.clipEntityIds.length) throw new Error("Clip ids must be unique");
1206
+ for (const target of targets) requireEntityKind(draft, target, "clip");
1207
+ const includesVisual = [...targets].some((target) => clipRole(draft, target) === "video_clip");
1208
+ const original = resolveEntityTimelineLayout(draft);
1209
+ if (includesVisual) this.normalizeMainSequential(draft);
1210
+ if (input.onAnchored === "detach") for (const childId of outgoingAnchorChildren(draft, targets)) {
1211
+ if (targets.has(childId)) continue;
1212
+ const placement = original.clips.get(childId);
1213
+ if (placement === void 0) throw new Error(`Anchored child Clip "${childId}" is not placed`);
1214
+ this.applyClipPlacement(draft, childId, {
1215
+ kind: "absolute",
1216
+ startMs: placement.start
1217
+ });
1218
+ }
1219
+ else for (const target of targets) this.removeAnchoredDescendants(draft, target, targets);
1220
+ for (const target of targets) this.removeClipGraph(draft, target);
1221
+ if (includesVisual) this.resolveOneVoiceoverOverlap(draft);
1222
+ });
1223
+ }
1224
+ updateClip(input) {
1225
+ this.transact((draft) => {
1226
+ const clip = requireEntityKind(draft, input.clipEntityId, "clip");
1227
+ replaceEntity(draft, clip.entityId, {
1228
+ ...clip,
1229
+ payload: cloneJsonObject(input.payload, "Clip payload")
1230
+ });
1231
+ });
1232
+ }
1233
+ upsertVoiceoverTake(input) {
1234
+ return this.transact((draft) => {
1235
+ this.normalizeMainSequential(draft);
1236
+ const placement = input.placement ?? {
1237
+ kind: "anchored",
1238
+ hostClipEntityId: input.hostClipEntityId,
1239
+ startMs: input.startMs
1240
+ };
1241
+ if (input.placement !== void 0 && (input.hostClipEntityId !== void 0 || input.startMs !== void 0)) throw new Error("Specify voiceover placement or hostClipEntityId/startMs, not both");
1242
+ requireVolume(input.volume);
1243
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
1244
+ replaceRows(draft, imported.rows);
1245
+ const voiceover = requireEntityKind(draft, imported.contentEntityId, "voiceover-recording");
1246
+ const phonetic = this.requireComposedPhoneticScript(draft, input.phoneticScriptEntityId);
1247
+ this.ensurePhoneticRender(draft, voiceover.entityId, phonetic.entityId);
1248
+ const speechTrack = this.ensureRoleTrack(draft, input.timelineEntityId, "speech");
1249
+ const existing = draft.entities.find((row) => row.entityId === input.voiceoverClipEntityId);
1250
+ let voiceoverClipEntityId;
1251
+ if (existing === void 0) voiceoverClipEntityId = this.insertPlacedClipIntoDraft(draft, {
1252
+ clipEntityId: input.voiceoverClipEntityId,
1253
+ trackEntityId: speechTrack.entityId,
1254
+ contentEntityId: voiceover.entityId,
1255
+ inMs: 0,
1256
+ outMs: input.media.durationMs,
1257
+ placement,
1258
+ clipPayload: { volume: input.volume }
1259
+ });
1260
+ else {
1261
+ const topology = requireClipOnRole(draft, input.voiceoverClipEntityId, "speech");
1262
+ voiceoverClipEntityId = topology.clip.entityId;
1263
+ replaceEntity(draft, topology.clip.entityId, {
1264
+ ...topology.clip,
1265
+ payload: {
1266
+ ...topology.clip.payload,
1267
+ volume: input.volume
1268
+ }
1269
+ });
1270
+ this.relinkRelation(draft, topology.contentRelation, {
1271
+ endpoint0EntityId: topology.clip.entityId,
1272
+ endpoint1EntityId: voiceover.entityId
1273
+ });
1274
+ this.applyClipPlacement(draft, topology.clip.entityId, placement);
1275
+ const placed = requireClipTopology(draft, topology.clip.entityId);
1276
+ replaceEntity(draft, placed.marker.entityId, {
1277
+ ...placed.marker,
1278
+ payload: {
1279
+ ...placed.marker.payload,
1280
+ inMs: 0,
1281
+ outMs: input.media.durationMs
1282
+ }
1283
+ });
1284
+ }
1285
+ this.reconcileVoiceoverCaptions(draft, voiceoverClipEntityId, phonetic.entityId, input.captions);
1286
+ this.resolveOneVoiceoverOverlap(draft);
1287
+ return {
1288
+ voiceoverClipEntityId,
1289
+ voiceoverAudioEntityId: voiceover.entityId,
1290
+ ...imported.voiceEntityId === void 0 ? {} : { voiceEntityId: imported.voiceEntityId },
1291
+ phoneticScriptEntityId: phonetic.entityId,
1292
+ audioScriptEntityId: requireComposedAudioScript(draft, phonetic.entityId),
1293
+ captionClipEntityIds: input.captions.map((caption) => caption.captionClipEntityId)
1294
+ };
1295
+ });
1296
+ }
1297
+ moveVoiceover(input) {
1298
+ this.transact((draft) => {
1299
+ requireClipOnRole(draft, input.voiceoverClipEntityId, "speech");
1300
+ this.normalizeMainSequential(draft);
1301
+ const layout = resolveEntityTimelineLayout(draft);
1302
+ this.reanchorAtAbsolute(draft, input.voiceoverClipEntityId, finiteNonNegative(input.newStartMs, "newStartMs"), layout);
1303
+ this.resolveOneVoiceoverOverlap(draft);
1304
+ });
1305
+ }
1306
+ deleteVoiceover(input) {
1307
+ this.transact((draft) => {
1308
+ this.normalizeMainSequential(draft);
1309
+ const targets = new Set(input.voiceoverClipEntityIds);
1310
+ if (targets.size !== input.voiceoverClipEntityIds.length) throw new Error("Voiceover Clip ids must be unique");
1311
+ for (const clipId of targets) {
1312
+ requireClipOnRole(draft, clipId, "speech");
1313
+ this.removeAnchoredDescendants(draft, clipId, targets);
1314
+ }
1315
+ for (const clipId of targets) this.removeClipGraph(draft, clipId);
1316
+ this.resolveOneVoiceoverOverlap(draft);
1317
+ });
1318
+ }
1319
+ setBgm(input) {
1320
+ return this.transact((draft) => {
1321
+ const imported = importMediaAsset(draft, input.media, this.idFactory);
1322
+ replaceRows(draft, imported.rows);
1323
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "bgm");
1324
+ const existing = clipsOnTrack(draft, track.entityId);
1325
+ for (const clip of existing) this.removeClipGraph(draft, clip.entityId);
1326
+ const clipId = this.insertPlacedClipIntoDraft(draft, {
1327
+ clipEntityId: input.bgmClipEntityId,
1328
+ trackEntityId: track.entityId,
1329
+ contentEntityId: imported.contentEntityId,
1330
+ inMs: 0,
1331
+ outMs: input.media.durationMs,
1332
+ placement: {
1333
+ kind: "sequential",
1334
+ order: 0
1335
+ },
1336
+ clipPayload: { volume: requireVolume(input.volume) }
1337
+ });
1338
+ const topology = requireClipTopology(draft, clipId);
1339
+ replaceEntity(draft, topology.marker.entityId, {
1340
+ ...topology.marker,
1341
+ payload: {
1342
+ ...topology.marker.payload,
1343
+ fillTimeline: true
1344
+ }
1345
+ });
1346
+ return clipId;
1347
+ });
1348
+ }
1349
+ deleteBgm(input) {
1350
+ this.transact((draft) => {
1351
+ const track = findRoleTrack(draft, input.timelineEntityId, "bgm");
1352
+ if (track === void 0) return;
1353
+ for (const clip of clipsOnTrack(draft, track.entityId)) this.removeClipGraph(draft, clip.entityId);
1354
+ });
1355
+ }
1356
+ setCaptionVisibility(input) {
1357
+ this.transact((draft) => {
1358
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "caption");
1359
+ replaceEntity(draft, track.entityId, {
1360
+ ...track,
1361
+ payload: {
1362
+ ...track.payload,
1363
+ hidden: input.hidden
1364
+ }
1365
+ });
1366
+ });
1367
+ }
1368
+ patchCaptionStyle(input) {
1369
+ this.transact((draft) => {
1370
+ const track = findRoleTrack(draft, input.timelineEntityId, "caption");
1371
+ if (track === void 0) return;
1372
+ const patch = cloneJsonObject(input.style, "Caption style");
1373
+ const assembled = new EntityProjectionGraph(draft);
1374
+ for (const clip of clipsOnTrack(draft, track.entityId)) {
1375
+ const topology = requireClipTopology(draft, clip.entityId);
1376
+ const caption = requireEntityKind(draft, otherEndpoint(topology.contentRelation, topology.clip.entityId), "caption");
1377
+ const prior = assembled.entity(caption.entityId).payload.style;
1378
+ const style = isJsonObject(prior) ? prior : {};
1379
+ for (const updated of updateEntityFields(draft, caption.entityId, { style: {
1380
+ ...style,
1381
+ ...patch
1382
+ } })) replaceEntity(draft, updated.entityId, updated);
1383
+ }
1384
+ });
1385
+ }
1386
+ /**
1387
+ * Place a Caption as a composed variant of an existing AudioScript: the
1388
+ * entity stores baseEntityIds, selection, optional style, and inherent cue-bounded
1389
+ * Sequence. This helper does not copy base text. The Clip and its display Marker describe this use;
1390
+ * moving or stretching the returned Clip later changes the Clip/Marker,
1391
+ * never the Caption entity or the AudioScript.
1392
+ */
1393
+ insertCaptionClip(input) {
1394
+ return this.transact((draft) => {
1395
+ const durationMs = finitePositive(input.durationMs, "Caption durationMs");
1396
+ const selection = captionSelection(input.selection);
1397
+ const track = this.ensureRoleTrack(draft, input.timelineEntityId, "caption");
1398
+ const content = {
1399
+ entityId: input.captionEntityId === void 0 ? this.mintEntityId(draft) : requireAvailableEntityId(draft, input.captionEntityId),
1400
+ entityKind: "caption",
1401
+ payload: {
1402
+ durationMs,
1403
+ baseEntityIds: captionBaseIds(input.baseEntityIds),
1404
+ selection,
1405
+ ...input.style === void 0 ? {} : { style: cloneJsonObject(input.style, "Caption style") }
1406
+ }
1407
+ };
1408
+ draft.entities.push(content);
1409
+ assembleCaptionContent(draft, content.entityId);
1410
+ return this.insertPlacedClipIntoDraft(draft, {
1411
+ trackEntityId: track.entityId,
1412
+ contentEntityId: content.entityId,
1413
+ inMs: 0,
1414
+ outMs: durationMs,
1415
+ placement: input.placement,
1416
+ ...input.captionClipEntityId === void 0 ? {} : { clipEntityId: input.captionClipEntityId }
1417
+ });
1418
+ });
1419
+ }
1420
+ insertPlacedClipIntoDraft(draft, input) {
1421
+ const track = requireEntityKind(draft, input.trackEntityId, "track");
1422
+ requireTrackTimeline(draft, track.entityId);
1423
+ const content = requireSequenceContent(draft, input.contentEntityId);
1424
+ const clipId = input.clipEntityId === void 0 ? this.mintEntityId(draft) : requireAvailableEntityId(draft, input.clipEntityId);
1425
+ const markerId = this.mintEntityId(draft);
1426
+ const clipPayload = { ...cloneJsonObject(input.clipPayload ?? {}, "Clip payload") };
1427
+ const markerPlacement = applyPlacementPayload(input.placement);
1428
+ const clip = {
1429
+ entityId: clipId,
1430
+ entityKind: "clip",
1431
+ payload: clipPayload
1432
+ };
1433
+ const marker = {
1434
+ entityId: markerId,
1435
+ entityKind: "sequence-marker",
1436
+ payload: markerPayload({
1437
+ ...requireWindow(input.inMs, input.outMs),
1438
+ ...markerPlacement
1439
+ })
1440
+ };
1441
+ draft.entities.push(clip, marker);
1442
+ draft.relations.push(this.createRelation(draft, "contain", track.entityId, clipId), this.createRelation(draft, "marker", clipId, markerId), this.createRelation(draft, "contain", clipId, content.entityId));
1443
+ if (input.placement.kind === "anchored") {
1444
+ const host = requireEntityKind(draft, input.placement.hostClipEntityId, "clip");
1445
+ if (host.entityId === clipId) throw new Error("A Clip cannot anchor to itself");
1446
+ draft.relations.push(this.createRelation(draft, "clip-anchor", clipId, host.entityId));
1447
+ }
1448
+ return clipId;
1449
+ }
1450
+ ensureRoleTrack(draft, timelineEntityId, role) {
1451
+ const existing = findRoleTrack(draft, timelineEntityId, role);
1452
+ if (existing !== void 0) return existing;
1453
+ const timeline = requireEntityKind(draft, timelineEntityId, "timeline");
1454
+ const track = {
1455
+ entityId: this.mintEntityId(draft),
1456
+ entityKind: "track",
1457
+ payload: {
1458
+ role,
1459
+ order: roleOrder[role]
1460
+ }
1461
+ };
1462
+ draft.entities.push(track);
1463
+ draft.relations.push(this.createRelation(draft, "contain", timeline.entityId, track.entityId));
1464
+ return track;
1465
+ }
1466
+ /** The existing pronunciation variant a voiceover consumes; composition is required. */
1467
+ requireComposedPhoneticScript(draft, phoneticScriptEntityId) {
1468
+ const phonetic = requireEntityKind(draft, phoneticScriptEntityId, "phonetic-script");
1469
+ requireComposedAudioScript(draft, phonetic.entityId);
1470
+ return phonetic;
1471
+ }
1472
+ /** Preserve the factual synthesis input as `[rendered Audio, PhoneticScript]`. */
1473
+ ensurePhoneticRender(draft, voiceoverAudioEntityId, phoneticScriptEntityId) {
1474
+ const renders = draft.relations.filter((relation) => relation.relationKind === "derived-from" && (relation.endpoint0EntityId === voiceoverAudioEntityId || relation.endpoint1EntityId === voiceoverAudioEntityId));
1475
+ if (renders.length > 1) throw new Error(`Voiceover "${voiceoverAudioEntityId}" has multiple PhoneticScript render relations`);
1476
+ if (renders[0] !== void 0) {
1477
+ if (otherEndpoint(renders[0], voiceoverAudioEntityId) === phoneticScriptEntityId) return;
1478
+ throw new Error(`Voiceover "${voiceoverAudioEntityId}" already has a different PhoneticScript synthesis input`);
1479
+ }
1480
+ draft.relations.push(this.createRelation(draft, "derived-from", voiceoverAudioEntityId, phoneticScriptEntityId));
1481
+ }
1482
+ reconcileVoiceoverCaptions(draft, voiceoverClipEntityId, phoneticScriptEntityId, captions) {
1483
+ const audioScriptEntityId = requireComposedAudioScript(draft, phoneticScriptEntityId);
1484
+ const requested = new Set(captions.map((caption) => caption.captionClipEntityId));
1485
+ if (requested.size !== captions.length) throw new Error("Caption Clip ids must be unique within a voiceover take");
1486
+ const existing = directAnchorChildren(draft, voiceoverClipEntityId).filter((clipId) => clipRole(draft, clipId) === "caption");
1487
+ for (const clipId of existing) if (!requested.has(clipId)) this.removeCaptionClipGraph(draft, clipId);
1488
+ const captionTrack = captions.length === 0 ? void 0 : this.ensureRoleTrackForClip(draft, voiceoverClipEntityId, "caption");
1489
+ for (const caption of captions) {
1490
+ const startMs = finiteNonNegative(caption.startMs, "Caption startMs");
1491
+ const durationMs = finitePositive(caption.durationMs, "Caption durationMs");
1492
+ const selection = captionSelection(caption.selection);
1493
+ const baseEntityIds = captionBaseIds(caption.baseEntityIds);
1494
+ if (draft.entities.find((entity) => entity.entityId === caption.captionClipEntityId) === void 0) {
1495
+ const content = {
1496
+ entityId: this.mintEntityId(draft),
1497
+ entityKind: "caption",
1498
+ payload: {
1499
+ durationMs,
1500
+ baseEntityIds,
1501
+ selection,
1502
+ ...caption.style === void 0 ? {} : { style: cloneJsonObject(caption.style, "Caption style") }
1503
+ }
1504
+ };
1505
+ draft.entities.push(content);
1506
+ assertCaptionScript(draft, content.entityId, audioScriptEntityId);
1507
+ this.insertPlacedClipIntoDraft(draft, {
1508
+ clipEntityId: caption.captionClipEntityId,
1509
+ trackEntityId: captionTrack.entityId,
1510
+ contentEntityId: content.entityId,
1511
+ inMs: 0,
1512
+ outMs: durationMs,
1513
+ placement: {
1514
+ kind: "anchored",
1515
+ hostClipEntityId: voiceoverClipEntityId,
1516
+ startMs
1517
+ }
1518
+ });
1519
+ continue;
1520
+ }
1521
+ const topology = requireClipOnRole(draft, caption.captionClipEntityId, "caption");
1522
+ const content = requireEntityKind(draft, otherEndpoint(topology.contentRelation, topology.clip.entityId), "caption");
1523
+ const priorStyle = isJsonObject(content.payload.style) ? content.payload.style : {};
1524
+ const style = caption.style === void 0 ? priorStyle : {
1525
+ ...priorStyle,
1526
+ ...cloneJsonObject(caption.style, "Caption style")
1527
+ };
1528
+ replaceEntity(draft, content.entityId, {
1529
+ ...content,
1530
+ payload: {
1531
+ ...content.payload,
1532
+ baseEntityIds,
1533
+ selection,
1534
+ ...Object.keys(style).length === 0 ? {} : { style }
1535
+ }
1536
+ });
1537
+ assertCaptionScript(draft, content.entityId, audioScriptEntityId);
1538
+ this.applyClipPlacement(draft, topology.clip.entityId, {
1539
+ kind: "anchored",
1540
+ hostClipEntityId: voiceoverClipEntityId,
1541
+ startMs
1542
+ });
1543
+ const placed = requireClipTopology(draft, topology.clip.entityId);
1544
+ const window = markerWindow(placed.marker.payload);
1545
+ const rate = (window.outMs - window.inMs) / durationMs;
1546
+ replaceEntity(draft, placed.marker.entityId, {
1547
+ ...placed.marker,
1548
+ payload: {
1549
+ ...placed.marker.payload,
1550
+ ...rate === 1 ? {} : { rate }
1551
+ }
1552
+ });
1553
+ }
1554
+ }
1555
+ ensureRoleTrackForClip(draft, clipEntityId, role) {
1556
+ const topology = requireClipTopology(draft, clipEntityId);
1557
+ const track = requireEntityKind(draft, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track");
1558
+ const timeline = requireEntityKind(draft, otherEndpoint(requireOneContain(draft, track.entityId, "contained"), track.entityId), "timeline");
1559
+ return this.ensureRoleTrack(draft, timeline.entityId, role);
1560
+ }
1561
+ /**
1562
+ * Remove one placed Caption use. The composed AudioScript outlives the
1563
+ * caption: it may anchor further captions and remains the text owner.
1564
+ */
1565
+ removeCaptionClipGraph(draft, clipEntityId) {
1566
+ const topology = requireClipOnRole(draft, clipEntityId, "caption");
1567
+ const caption = requireEntityKind(draft, otherEndpoint(topology.contentRelation, topology.clip.entityId), "caption");
1568
+ const contentRelations = incidentRelations(draft, caption.entityId);
1569
+ const allowed = new Set(["caption-alignment", "contain"]);
1570
+ const unsupported = contentRelations.filter((relation) => !allowed.has(relation.relationKind));
1571
+ if (unsupported.length > 0) throw new Error(`Cannot delete Caption with unsupported Relation ${unsupported[0].relationId}`);
1572
+ this.removeClipGraph(draft, clipEntityId);
1573
+ if (containing(draft, caption.entityId, "contained").length > 0) return;
1574
+ const removed = new Set(incidentRelations(draft, caption.entityId).map((relation) => relation.relationId));
1575
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1576
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== caption.entityId);
1577
+ }
1578
+ replaceAnchorHost(draft, childClipEntityId, hostClipEntityId) {
1579
+ const anchors = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === childClipEntityId);
1580
+ if (anchors.length !== 1) throw new Error(`Clip "${childClipEntityId}" must have exactly one host`);
1581
+ this.relinkRelation(draft, anchors[0], {
1582
+ endpoint0EntityId: createEntityId(childClipEntityId),
1583
+ endpoint1EntityId: createEntityId(hostClipEntityId)
1584
+ });
1585
+ }
1586
+ /** Rebuild the legacy edit baseline from current visual order before a high-level cascade. */
1587
+ normalizeMainSequential(draft) {
1588
+ const videoTrack = resolveEntityTimelineLayout(draft).tracks.find((track) => track.role === "video_clip");
1589
+ if (videoTrack === void 0) return;
1590
+ videoTrack.clips.forEach((clip, order) => {
1591
+ this.applyClipPlacement(draft, clip.clipEntityId, {
1592
+ kind: "sequential",
1593
+ order
1594
+ });
1595
+ });
1596
+ }
1597
+ resolveOneVoiceoverOverlap(draft) {
1598
+ const layout = resolveEntityTimelineLayout(draft);
1599
+ const speechTrack = layout.tracks.find((track) => track.role === "speech");
1600
+ if (speechTrack === void 0) return;
1601
+ const ranges = speechTrack.clips.map((speech) => {
1602
+ const captions = directAnchorChildren(draft, speech.clipEntityId).filter((clipId) => clipRole(draft, clipId) === "caption").map((clipId) => layout.clips.get(clipId)).filter((value) => value !== void 0);
1603
+ return {
1604
+ speech,
1605
+ start: Math.min(speech.start, ...captions.map((caption) => caption.start)),
1606
+ end: Math.max(speech.start + speech.duration, ...captions.map((caption) => caption.start + caption.duration))
1607
+ };
1608
+ }).sort((left, right) => left.start - right.start || left.speech.clipEntityId.localeCompare(right.speech.clipEntityId));
1609
+ for (let index = 1; index < ranges.length; index += 1) {
1610
+ const prior = ranges[index - 1];
1611
+ const current = ranges[index];
1612
+ if (current.start >= prior.end) continue;
1613
+ const hostId = current.speech.anchorClipEntityId;
1614
+ if (hostId === void 0) continue;
1615
+ const videoTrack = layout.tracks.find((track) => track.role === "video_clip");
1616
+ const hostIndex = videoTrack?.clips.findIndex((clip) => clip.clipEntityId === hostId) ?? -1;
1617
+ if (videoTrack === void 0 || hostIndex < 0) continue;
1618
+ const overlapMs = Math.round(prior.end - current.start);
1619
+ for (const [visualIndex, clip] of videoTrack.clips.entries()) this.applyClipPlacement(draft, clip.clipEntityId, {
1620
+ kind: "absolute",
1621
+ startMs: clip.start + (visualIndex >= hostIndex ? overlapMs : 0)
1622
+ });
1623
+ return;
1624
+ }
1625
+ }
1626
+ applyClipPlacement(draft, clipEntityId, placement) {
1627
+ const topology = requireClipTopology(draft, clipEntityId);
1628
+ const clipPayload = { ...cloneJsonObject(topology.clip.payload, "Clip payload") };
1629
+ const markerPayloadValue = { ...cloneJsonObject(topology.marker.payload, "Sequence Marker payload") };
1630
+ delete markerPayloadValue.startMs;
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") setClipOrder(draft, topology.clip.entityId, placement.order);
1635
+ else if (placement.kind === "absolute") markerPayloadValue.startMs = finiteNonNegative(placement.startMs, "startMs");
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.startMs = finiteNonNegative(placement.startMs, "startMs");
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
+ if (layout.clips.get(childClipEntityId) === void 0) throw new Error(`Clip "${childClipEntityId}" is not placed`);
1655
+ this.applyClipPlacement(draft, childClipEntityId, {
1656
+ kind: "absolute",
1657
+ startMs: absoluteStart
1658
+ });
1659
+ return;
1660
+ }
1661
+ const host = hosts.find((candidate) => candidate.start <= absoluteStart && absoluteStart < candidate.start + candidate.duration) ?? (absoluteStart < hosts[0].start ? hosts[0] : hosts.at(-1));
1662
+ this.applyClipPlacement(draft, childClipEntityId, {
1663
+ kind: "anchored",
1664
+ hostClipEntityId: host.clipEntityId,
1665
+ startMs: Math.max(0, Math.round(absoluteStart - host.start))
1666
+ });
1667
+ }
1668
+ removeAnchoredDescendants(draft, hostClipEntityId, scheduled) {
1669
+ const children = draft.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint1EntityId === hostClipEntityId).map((relation) => String(relation.endpoint0EntityId));
1670
+ for (const child of children) {
1671
+ if (scheduled.has(child)) continue;
1672
+ const descendants = new Set(scheduled);
1673
+ descendants.add(child);
1674
+ this.removeAnchoredDescendants(draft, child, descendants);
1675
+ if (clipRole(draft, child) === "caption") this.removeCaptionClipGraph(draft, child);
1676
+ else this.removeClipGraph(draft, child);
1677
+ }
1678
+ }
1679
+ removeClipGraph(draft, clipEntityId) {
1680
+ const topology = requireClipTopology(draft, clipEntityId);
1681
+ assertOnlyStructuralIncidents(draft, topology.clip.entityId, new Set([
1682
+ "contain",
1683
+ "marker",
1684
+ "clip-anchor"
1685
+ ]));
1686
+ assertOnlyStructuralIncidents(draft, topology.marker.entityId, new Set(["marker"]));
1687
+ const markerIsExclusive = incidentRelations(draft, topology.marker.entityId).filter((relation) => relation.relationKind === "marker").length === 1;
1688
+ const removed = new Set(incidentRelations(draft, topology.clip.entityId).map((relation) => relation.relationId));
1689
+ if (markerIsExclusive) for (const relation of incidentRelations(draft, topology.marker.entityId)) removed.add(relation.relationId);
1690
+ draft.relations = draft.relations.filter((relation) => !removed.has(relation.relationId));
1691
+ draft.entities = draft.entities.filter((entity) => entity.entityId !== topology.clip.entityId && (!markerIsExclusive || entity.entityId !== topology.marker.entityId));
1692
+ }
1693
+ transact(edit) {
1694
+ const draft = cloneRows(this.state);
1695
+ const result = edit(draft);
1696
+ validateFinalRows(draft);
1697
+ assertMediaAssetWritePolicy(this.state, draft);
1698
+ this.state = draft;
1699
+ return result;
1700
+ }
1701
+ mintEntityId(draft) {
1702
+ const entityId = createEntityId(this.idFactory("entity"));
1703
+ if (draft.entities.some((entity) => entity.entityId === entityId)) throw new Error(`Entity id "${entityId}" already exists`);
1704
+ return entityId;
1705
+ }
1706
+ createRelation(draft, relationKind, endpoint0EntityId, endpoint1EntityId, metadata = {}) {
1707
+ return {
1708
+ relationId: this.mintRelationId(draft),
1709
+ relationKind,
1710
+ endpoint0EntityId,
1711
+ endpoint1EntityId,
1712
+ metadata,
1713
+ trace: {}
1714
+ };
1715
+ }
1716
+ relinkRelation(draft, relation, endpoints) {
1717
+ const relationId = this.mintRelationId(draft);
1718
+ const index = draft.relations.findIndex((candidate) => candidate.relationId === relation.relationId);
1719
+ if (index < 0) throw new Error(`Relation id "${relation.relationId}" does not exist`);
1720
+ draft.relations[index] = {
1721
+ ...relation,
1722
+ ...endpoints,
1723
+ relationId
1724
+ };
1725
+ }
1726
+ mintRelationId(draft) {
1727
+ const relationId = createRelationId(this.idFactory("relation"));
1728
+ if (draft.relations.some((relation) => relation.relationId === relationId)) throw new Error(`Relation id "${relationId}" already exists`);
1729
+ return relationId;
1730
+ }
1731
+ };
1732
+ function validateFinalRows(rows) {
1733
+ decodeEntityRelationRows(rows, {});
1734
+ assertCanonicalEditorResources(rows);
1735
+ validateTimelineHierarchy(rows);
1736
+ }
1737
+ function validateTimelineHierarchy(rows) {
1738
+ for (const entity of rows.entities) {
1739
+ if (entity.entityKind === "track") requireTrackTimeline(rows, entity.entityId);
1740
+ if (entity.entityKind !== "clip") continue;
1741
+ const trackRelations = containing(rows, entity.entityId, "contained");
1742
+ if (trackRelations.length !== 1) throw new Error(`Clip Entity "${entity.entityId}" must belong to exactly one Track`);
1743
+ requireEntityKind(rows, otherEndpoint(trackRelations[0], entity.entityId), "track");
1744
+ }
1745
+ }
1746
+ function requireTrackTimeline(rows, trackEntityId) {
1747
+ const timelineRelations = containing(rows, createEntityId(trackEntityId), "contained");
1748
+ if (timelineRelations.filter((relation) => {
1749
+ const otherId = otherEndpoint(relation, trackEntityId);
1750
+ return rows.entities.some((entity) => entity.entityId === otherId && entity.entityKind === "timeline");
1751
+ }).length !== 1 || timelineRelations.length !== 1) throw new Error(`Track Entity "${trackEntityId}" must belong to exactly one Timeline`);
1752
+ }
1753
+ function requireClipTopology(rows, clipEntityId) {
1754
+ const clip = requireEntityKind(rows, clipEntityId, "clip");
1755
+ const trackRelation = requireOneContain(rows, clip.entityId, "contained");
1756
+ requireEntityKind(rows, otherEndpoint(trackRelation, clip.entityId), "track");
1757
+ const clipMarkerRelation = requireOneRelation(rows, clip.entityId, "marker");
1758
+ const marker = requireEntityKind(rows, otherEndpoint(clipMarkerRelation, clip.entityId), "sequence-marker");
1759
+ const contentRelation = requireOneContain(rows, clip.entityId, "container");
1760
+ requireSequenceContent(rows, otherEndpoint(contentRelation, clip.entityId));
1761
+ return {
1762
+ clip,
1763
+ marker,
1764
+ trackRelation,
1765
+ clipMarkerRelation,
1766
+ contentRelation
1767
+ };
1768
+ }
1769
+ /**
1770
+ * One `contain` Relation with this entity on the stated side.
1771
+ *
1772
+ * A Clip is contained by its Track and contains its content; a Track is
1773
+ * contained by the Timeline and contains its Clips. Both are `contain`, so the
1774
+ * endpoint position is the only thing that tells them apart.
1775
+ */
1776
+ function requireOneContain(rows, entityId, side) {
1777
+ const matches = containing(rows, entityId, side);
1778
+ if (matches.length !== 1) throw new Error(`Entity "${entityId}" must be the ${side} of exactly one contain Relation`);
1779
+ return matches[0];
1780
+ }
1781
+ /** `contain` is ordered, so a lookup says which side it wants. */
1782
+ function containing(rows, entityId, side) {
1783
+ return (side === "container" ? relationsFrom : relationsTo)(rows.relations, "contain", entityId);
1784
+ }
1785
+ function requireOneRelation(rows, entityId, relationKind) {
1786
+ const matches = incidentRelations(rows, entityId).filter((relation) => relation.relationKind === relationKind);
1787
+ if (matches.length !== 1) throw new Error(`Entity "${entityId}" must have exactly one ${relationKind} Relation`);
1788
+ return matches[0];
1789
+ }
1790
+ function requireEntityKind(rows, entityIdValue, entityKind) {
1791
+ const entityId = createEntityId(entityIdValue);
1792
+ const entity = rows.entities.find((candidate) => candidate.entityId === entityId);
1793
+ if (entity == null) throw new Error(`Entity id "${entityId}" does not exist`);
1794
+ if (entity.entityKind !== entityKind) throw new Error(`Entity "${entityId}" must have kind "${entityKind}", got "${entity.entityKind}"`);
1795
+ return entity;
1796
+ }
1797
+ function requireSequenceContent(rows, entityIdValue) {
1798
+ 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");
1799
+ const entityId = createEntityId(entityIdValue);
1800
+ const row = rows.entities.find((candidate) => candidate.entityId === entityId);
1801
+ if (row == null) throw new Error(`Timeline content must reference an existing Sequence media Entity; "${entityId}" was not found`);
1802
+ if (!hasSequence({
1803
+ ...row.payload,
1804
+ entityId: row.entityId,
1805
+ entityKind: row.entityKind
1806
+ })) throw new Error(`Timeline content must reference a Sequence media Entity, but "${entityId}" has kind "${row.entityKind}"`);
1807
+ return row;
1808
+ }
1809
+ function assertOnlyStructuralIncidents(rows, entityId, allowedKinds) {
1810
+ const unsupported = incidentRelations(rows, entityId).filter((relation) => !allowedKinds.has(relation.relationKind));
1811
+ if (unsupported.length === 0) return;
1812
+ throw new Error(`Cannot delete Clip graph: Entity "${entityId}" has unsupported incident Relation id(s): ${unsupported.map((relation) => relation.relationId).sort().join(", ")}`);
1813
+ }
1814
+ function incidentRelations(rows, entityId) {
1815
+ return rows.relations.filter((relation) => relation.endpoint0EntityId === entityId || relation.endpoint1EntityId === entityId);
1816
+ }
1817
+ function hasOutgoingClipAnchor(rows, clipEntityId) {
1818
+ return rows.relations.some((relation) => relation.relationKind === "clip-anchor" && relation.endpoint0EntityId === clipEntityId);
1819
+ }
1820
+ function outgoingAnchorChildren(rows, hostIds) {
1821
+ return rows.relations.filter((relation) => relation.relationKind === "clip-anchor" && hostIds.has(relation.endpoint1EntityId)).map((relation) => String(relation.endpoint0EntityId));
1822
+ }
1823
+ /** What the placement puts on the Marker; a sequential one puts nothing there. */
1824
+ function applyPlacementPayload(placement) {
1825
+ if (placement.kind === "sequential") return {};
1826
+ return { startMs: finiteNonNegative(placement.startMs, "startMs") };
1827
+ }
1828
+ function requireAvailableEntityId(rows, value) {
1829
+ const entityId = createEntityId(value);
1830
+ if (rows.entities.some((entity) => entity.entityId === entityId)) throw new Error(`Entity id "${entityId}" already exists`);
1831
+ return entityId;
1832
+ }
1833
+ /**
1834
+ * Put a Clip at `order` among the Clips its Track contains.
1835
+ *
1836
+ * Where a Clip sits is the position of its membership Relation, so moving one
1837
+ * is moving that Relation — nothing on the Clip changes, and there are no
1838
+ * sibling positions left to keep in step.
1839
+ */
1840
+ function setClipOrder(rows, clipEntityId, order) {
1841
+ const membership = relationsTo(rows.relations, "contain", clipEntityId)[0];
1842
+ if (membership === void 0) throw new Error(`Clip "${clipEntityId}" is contained by no Track`);
1843
+ const slots = [];
1844
+ const siblings = [];
1845
+ for (const [index, relation] of rows.relations.entries()) {
1846
+ if (relation.relationKind !== "contain" || relation.endpoint0EntityId !== membership.endpoint0EntityId) continue;
1847
+ slots.push(index);
1848
+ siblings.push(relation);
1849
+ }
1850
+ const from = siblings.indexOf(membership);
1851
+ const moved = siblings.filter((relation) => relation !== membership);
1852
+ moved.splice(Math.max(0, Math.min(order, moved.length)), 0, membership);
1853
+ if (from < 0) return;
1854
+ slots.forEach((slot, at) => rows.relations[slot] = moved[at]);
1855
+ }
1856
+ function findRoleTrack(rows, timelineEntityId, role) {
1857
+ const timeline = requireEntityKind(rows, timelineEntityId, "timeline");
1858
+ const tracks = containing(rows, timeline.entityId, "container").map((relation) => requireEntityKind(rows, otherEndpoint(relation, timeline.entityId), "track")).filter((track) => track.payload.role === role);
1859
+ if (tracks.length > 1) throw new Error(`Timeline "${timelineEntityId}" has multiple ${role} Tracks`);
1860
+ return tracks[0];
1861
+ }
1862
+ function clipsOnTrack(rows, trackEntityId) {
1863
+ return containing(rows, trackEntityId, "container").map((relation) => requireEntityKind(rows, otherEndpoint(relation, trackEntityId), "clip"));
1864
+ }
1865
+ function clipRole(rows, clipEntityId) {
1866
+ const topology = requireClipTopology(rows, clipEntityId);
1867
+ return requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role;
1868
+ }
1869
+ function directAnchorChildren(rows, hostClipEntityId) {
1870
+ return rows.relations.filter((relation) => relation.relationKind === "clip-anchor" && relation.endpoint1EntityId === hostClipEntityId).map((relation) => String(relation.endpoint0EntityId));
1871
+ }
1872
+ function requireSupportedRole(rows, topology) {
1873
+ const role = requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role;
1874
+ if (role !== "video_clip" && role !== "speech" && role !== "caption" && role !== "bgm") throw new Error(`Clip "${topology.clip.entityId}" is not on a supported timeline Track`);
1875
+ return role;
1876
+ }
1877
+ function requireClipOnRole(rows, clipEntityId, role) {
1878
+ const topology = requireClipTopology(rows, clipEntityId);
1879
+ if (requireEntityKind(rows, otherEndpoint(topology.trackRelation, topology.clip.entityId), "track").payload.role !== role) throw new Error(`Clip "${clipEntityId}" must belong to the ${role} Track`);
1880
+ return topology;
1881
+ }
1882
+ function finiteNumber(value, label) {
1883
+ if (!Number.isFinite(value)) throw new Error(`${label} must be finite`);
1884
+ return value;
1885
+ }
1886
+ function finitePositive(value, label) {
1887
+ const result = finiteNumber(value, label);
1888
+ if (result <= 0) throw new Error(`${label} must be positive`);
1889
+ return result;
1890
+ }
1891
+ function requireVolume(value) {
1892
+ const volume = finiteNumber(value, "Clip volume");
1893
+ if (volume < -60 || volume > 20) throw new Error("Clip volume must be between -60 and 20 dB");
1894
+ return volume;
1895
+ }
1896
+ function requireWindow(inMs, outMs, label = "Sequence Marker") {
1897
+ const start = finiteNonNegative(inMs, `${label} inMs`);
1898
+ const end = finitePositive(outMs, `${label} outMs`);
1899
+ if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || end <= start) throw new Error(`${label} must state a positive whole-millisecond window`);
1900
+ return {
1901
+ inMs: start,
1902
+ outMs: end
1903
+ };
1904
+ }
1905
+ function markerWindow(payload) {
1906
+ return requireWindow(payload.inMs, payload.outMs);
1907
+ }
1908
+ /**
1909
+ * How long the Clip runs. The window and the rate are the only stored facts;
1910
+ * the played length follows from them rather than being kept in step by hand.
1911
+ */
1912
+ function markerDuration(payload, window = markerWindow(payload)) {
1913
+ return Math.round((window.outMs - window.inMs) / markerRate(payload));
1914
+ }
1915
+ function markerRate(payload) {
1916
+ return payload.rate === void 0 ? 1 : finitePositive(payload.rate, "linear speed rate");
1917
+ }
1918
+ function replaceRows(rows, replacement) {
1919
+ rows.entities = structuredClone([...replacement.entities]);
1920
+ rows.relations = structuredClone([...replacement.relations]);
1921
+ }
1922
+ function finiteNonNegative(value, label) {
1923
+ const result = finiteNumber(value, label);
1924
+ if (result < 0) throw new Error(`${label} must be non-negative`);
1925
+ return result;
1926
+ }
1927
+ /** How many Clips the Track already holds — the index a new one lands at. */
1928
+ function nextSequentialOrder(rows, trackEntityId) {
1929
+ return relationsFrom(rows.relations, "contain", trackEntityId).length;
1930
+ }
1931
+ function otherEndpoint(relation, entityId) {
1932
+ if (relation.endpoint0EntityId === entityId) return relation.endpoint1EntityId;
1933
+ if (relation.endpoint1EntityId === entityId) return relation.endpoint0EntityId;
1934
+ throw new Error(`Relation "${relation.relationId}" is not incident to Entity "${entityId}"`);
1935
+ }
1936
+ function replaceEntity(rows, entityId, replacement) {
1937
+ const index = rows.entities.findIndex((entity) => entity.entityId === entityId);
1938
+ if (index < 0) throw new Error(`Entity id "${entityId}" does not exist`);
1939
+ rows.entities[index] = replacement;
1940
+ }
1941
+ function markerPayload(value) {
1942
+ return cloneJsonObject(value, "Sequence Marker payload");
1943
+ }
1944
+ function cloneJsonObject(value, label) {
1945
+ const cloned = structuredClone(value);
1946
+ if (!isJsonObject(cloned)) throw new Error(`${label} must contain only JSON values`);
1947
+ return cloned;
1948
+ }
1949
+ function cloneRows(rows) {
1950
+ return {
1951
+ entities: rows.entities.map((entity) => structuredClone(entity)),
1952
+ relations: rows.relations.map((relation) => structuredClone(relation))
1953
+ };
1954
+ }
1955
+ //#endregion
1956
+ //#region src/entity-editor/visual-assets-effect.ts
1957
+ const receiptKey = (effectId) => `editorEffectReceipt:${effectId}`;
1958
+ const effectEntityId = (role, effectId, index) => createEntityId(`entity_visual_effect_${role}_${effectId}_${index}`);
1959
+ const effectRelationId = (role, effectId, index) => createRelationId(`relation_visual_effect_${role}_${effectId}_${index}`);
1960
+ /** Read the authoritative timeline receipt, not a media-id heuristic. */
1961
+ function hasVisualAssetsEffect(rows, effectId) {
1962
+ return rows.entities.find((row) => row.entityKind === "timeline")?.payload[receiptKey(effectId)] === true;
1963
+ }
1964
+ /**
1965
+ * Materialize a host-resolved visual command into native Entity/Relation rows.
1966
+ * The result is suitable for one revision-checked `EntityGraphHttpClient.commit`.
1967
+ * This does not fetch Assets or write remotely; callers must supply factual media
1968
+ * and lineage inputs after their own ownership checks.
1969
+ */
1970
+ function applyVisualAssetsEffect(rows, input) {
1971
+ if (!input.effect_id) throw new Error("Visual effect requires an effect_id");
1972
+ if (hasVisualAssetsEffect(rows, input.effect_id)) return rows;
1973
+ if (input.clips.length === 0) throw new Error("Visual effect requires clips");
1974
+ if (input.before_clip_id !== void 0 && input.after_clip_id !== void 0) throw new Error("Visual effect accepts only one placement anchor");
1975
+ const timeline = rows.entities.find((row) => row.entityKind === "timeline");
1976
+ if (!timeline) throw new Error("Visual effect requires a Timeline");
1977
+ if (!rows.entities.some((row) => row.entityKind === "track" && row.payload.role === "video_clip")) throw new Error("Visual effect requires a video Track");
1978
+ const anchor = input.before_clip_id ?? input.after_clip_id;
1979
+ if (anchor && !resolveEntityTimelineLayout(rows).tracks.find((track) => track.role === "video_clip")?.clips.some((clip) => clip.clipEntityId === anchor)) throw new Error(`Visual effect anchor ${anchor} no longer exists`);
1980
+ let prepared = rows;
1981
+ for (const clip of input.clips) {
1982
+ prepared = importEffectMedia(prepared, visualMediaFact(clip));
1983
+ for (const source of clip.lineage?.sources ?? []) if (source.assetId !== clip.media_id) prepared = importEffectMedia(prepared, source);
1984
+ if (clip.lineage?.script) prepared = materializeScript(prepared, clip.lineage.script);
1985
+ }
1986
+ const entityIds = input.clips.map((_, index) => effectEntityId("marker", input.effect_id, index));
1987
+ const relationIds = input.clips.flatMap((_, index) => [
1988
+ effectRelationId("track_clip", input.effect_id, index),
1989
+ effectRelationId("marker", input.effect_id, index),
1990
+ effectRelationId("content", input.effect_id, index)
1991
+ ]);
1992
+ const editor = new EntityTimelineEditor(prepared, (prefix) => {
1993
+ const id = (prefix === "entity" ? entityIds : relationIds).shift();
1994
+ if (!id) throw new Error(`Visual effect exhausted deterministic ${prefix} identities`);
1995
+ return id;
1996
+ });
1997
+ for (const clip of input.clips) {
1998
+ if (clip.media_kind !== "image" && clip.media_kind !== "video") throw new Error("Expected visual media");
1999
+ if (clip.lineage?.script && clip.media_kind !== "video") throw new Error("Only Video may derive from Script");
2000
+ }
2001
+ editor.insertMediaClips({
2002
+ timelineEntityId: timeline.entityId,
2003
+ clips: input.clips.map((clip, index) => ({
2004
+ clipEntityId: effectEntityId("clip", input.effect_id, index),
2005
+ media: visualMediaFact(clip),
2006
+ inMs: clip.play_in ?? 0,
2007
+ outMs: clip.play_out ?? clip.media_duration_ms,
2008
+ ...clip.volume === void 0 ? {} : { volume: clip.volume }
2009
+ })),
2010
+ insertion: input.before_clip_id !== void 0 ? {
2011
+ kind: "before",
2012
+ clipEntityId: input.before_clip_id
2013
+ } : input.after_clip_id !== void 0 ? {
2014
+ kind: "after",
2015
+ clipEntityId: input.after_clip_id
2016
+ } : {
2017
+ kind: "firstStart",
2018
+ startMs: input.clips.find((clip) => clip.start_ms !== void 0)?.start_ms ?? 0
2019
+ }
2020
+ });
2021
+ let next = editor.rows();
2022
+ const relations = [...next.relations];
2023
+ for (const [clipIndex, clip] of input.clips.entries()) {
2024
+ const output = findMediaId(next, clip.media_id);
2025
+ const seen = /* @__PURE__ */ new Set();
2026
+ for (const [sourceIndex, sourceFact] of (clip.lineage?.sources ?? []).entries()) {
2027
+ if (sourceFact.assetId === clip.media_id || seen.has(sourceFact.assetId)) continue;
2028
+ seen.add(sourceFact.assetId);
2029
+ const source = findMediaId(next, sourceFact.assetId, sourceFact.kind);
2030
+ if (hasDerivation(relations, output, source)) continue;
2031
+ relations.push({
2032
+ relationId: createRelationId(`relation_visual_effect_${input.effect_id}_${clipIndex}_${sourceIndex}`),
2033
+ relationKind: "derived-from",
2034
+ endpoint0EntityId: output,
2035
+ endpoint1EntityId: source,
2036
+ metadata: {},
2037
+ trace: { taskId: clip.lineage.generation_job_id }
2038
+ });
2039
+ }
2040
+ const script = clip.lineage?.script;
2041
+ if (!script) continue;
2042
+ if (relations.filter((relation) => relation.relationKind === "derived-from" && relation.endpoint0EntityId === output && next.entities.some((row) => row.entityId === relation.endpoint1EntityId && row.entityKind === "script")).some((relation) => relation.endpoint1EntityId !== script.entity_id)) throw new Error(`Video ${clip.media_id} already derives from another Script`);
2043
+ if (hasDerivation(relations, output, createEntityId(script.entity_id))) continue;
2044
+ relations.push({
2045
+ relationId: createRelationId(`relation_visual_effect_${input.effect_id}_script_${clipIndex}`),
2046
+ relationKind: "derived-from",
2047
+ endpoint0EntityId: output,
2048
+ endpoint1EntityId: createEntityId(script.entity_id),
2049
+ metadata: {},
2050
+ trace: { taskId: clip.lineage.generation_job_id }
2051
+ });
2052
+ }
2053
+ next = {
2054
+ entities: next.entities,
2055
+ relations
2056
+ };
2057
+ return {
2058
+ entities: next.entities.map((row) => row.entityId === timeline.entityId ? {
2059
+ ...row,
2060
+ payload: {
2061
+ ...row.payload,
2062
+ [receiptKey(input.effect_id)]: true
2063
+ }
2064
+ } : row),
2065
+ relations: next.relations
2066
+ };
2067
+ }
2068
+ function visualMediaFact(clip) {
2069
+ return clip.media_kind === "image" ? {
2070
+ assetId: clip.media_id,
2071
+ kind: "image",
2072
+ ...clip.storage_key ? { storageKey: clip.storage_key } : {}
2073
+ } : {
2074
+ assetId: clip.media_id,
2075
+ kind: "video",
2076
+ durationMs: clip.media_duration_ms,
2077
+ ...clip.storage_key ? { storageKey: clip.storage_key } : {}
2078
+ };
2079
+ }
2080
+ function materializeScript(rows, script) {
2081
+ const payload = {
2082
+ segments: script.segments.map((segment) => ({
2083
+ segmentId: segment.segment_id,
2084
+ text: segment.text,
2085
+ ...segment.language === void 0 ? {} : { language: segment.language }
2086
+ })),
2087
+ contentHash: script.content_hash
2088
+ };
2089
+ if (payload.contentHash !== scriptContentHash(payload.segments)) throw new Error(`Script ${script.entity_id} content hash is invalid`);
2090
+ const existing = rows.entities.find((row) => row.entityId === script.entity_id);
2091
+ if (existing) {
2092
+ if (existing.entityKind !== "script" || existing.payload.contentHash !== payload.contentHash || JSON.stringify(existing.payload.segments) !== JSON.stringify(payload.segments)) throw new Error(`Script ${script.entity_id} identity/content conflict`);
2093
+ return rows;
2094
+ }
2095
+ return {
2096
+ entities: [...rows.entities, {
2097
+ entityId: createEntityId(script.entity_id),
2098
+ entityKind: "script",
2099
+ payload
2100
+ }],
2101
+ relations: rows.relations
2102
+ };
2103
+ }
2104
+ function importEffectMedia(rows, fact) {
2105
+ const system = fact.kind === "voiceover-recording" ? "memota-speech" : "memota";
2106
+ const resolved = findCanonicalMedia(rows, fact.assetId, system);
2107
+ if (resolved.asset && fact.storageKey !== void 0 && resolved.asset.payload.storageKey !== fact.storageKey) throw new Error(`Media ${fact.assetId} storageKey conflicts with its immutable Asset`);
2108
+ const prepared = resolved.asset ? rows : {
2109
+ entities: [...rows.entities, {
2110
+ entityId: createEntityId(`entity_visual_media_${fact.assetId}_0`),
2111
+ entityKind: "asset",
2112
+ payload: {
2113
+ system,
2114
+ key: fact.assetId,
2115
+ ...fact.storageKey ? { storageKey: fact.storageKey } : {}
2116
+ }
2117
+ }],
2118
+ relations: rows.relations
2119
+ };
2120
+ const voiceDescriptor = fact.kind === "voiceover-recording" ? fact.voice : void 0;
2121
+ const existingVoice = voiceDescriptor ? rows.entities.find((row) => {
2122
+ const voice = row.payload.voice;
2123
+ return row.entityKind === "voice" && voice !== void 0 && voice.system === voiceDescriptor.system && voice.key === voiceDescriptor.key && voice.name === voiceDescriptor.name;
2124
+ }) : void 0;
2125
+ const hasTimbre = resolved.media !== void 0 && rows.relations.some((relation) => relation.relationKind === "voice-timbre" && relation.endpoint0EntityId === resolved.media.entityId);
2126
+ const entities = [...resolved.media ? [] : [createEntityId(`entity_visual_media_${fact.assetId}_1`)], ...fact.kind === "voiceover-recording" && fact.voice && !existingVoice ? [createEntityId(`entity_visual_media_${fact.assetId}_2`)] : []];
2127
+ const relations = [...resolved.media ? [] : [createRelationId(`relation_visual_media_${fact.assetId}_0`)], ...fact.kind === "voiceover-recording" && fact.voice && !hasTimbre ? [createRelationId(`relation_visual_media_${fact.assetId}_1`)] : []];
2128
+ return importMediaAsset(prepared, fact, (prefix) => {
2129
+ const id = (prefix === "entity" ? entities : relations).shift();
2130
+ if (!id) throw new Error(`Media ${fact.assetId} exhausted deterministic ${prefix} slots`);
2131
+ return id;
2132
+ }).rows;
2133
+ }
2134
+ function findMediaId(rows, mediaId, kind) {
2135
+ const media = findCanonicalMedia(rows, mediaId, kind === "voiceover-recording" ? "memota-speech" : "memota").media;
2136
+ if (!media) throw new Error(`Imported media ${mediaId} is missing`);
2137
+ return media.entityId;
2138
+ }
2139
+ function findCanonicalMedia(rows, mediaId, system = "memota") {
2140
+ const assets = rows.entities.filter((row) => row.entityKind === "asset" && row.payload.system === system && row.payload.key === mediaId);
2141
+ if (assets.length > 1) throw new Error(`Imported media ${mediaId} has duplicate Asset identities`);
2142
+ const asset = assets[0];
2143
+ if (!asset) return {};
2144
+ const links = rows.relations.filter((relation) => relation.relationKind === "from-asset" && relation.endpoint1EntityId === asset.entityId);
2145
+ if (rows.relations.some((relation) => relation.relationKind === "from-asset" && relation.endpoint0EntityId === asset.entityId)) throw new Error(`Imported media ${mediaId} has reversed from-asset relation`);
2146
+ if (links.length > 1) throw new Error(`Imported media ${mediaId} has duplicate media identities`);
2147
+ const media = rows.entities.find((row) => row.entityId === links[0]?.endpoint0EntityId);
2148
+ if (links.length && (!media || media.entityKind !== "image" && media.entityKind !== "video" && media.entityKind !== "audio" && !(system === "memota-speech" && media.entityKind === "voiceover-recording"))) throw new Error(`Imported media ${mediaId} has invalid media identity`);
2149
+ return {
2150
+ asset,
2151
+ ...media ? { media } : {}
2152
+ };
2153
+ }
2154
+ function hasDerivation(relations, output, source) {
2155
+ return relations.some((relation) => relation.relationKind === "derived-from" && relation.endpoint0EntityId === output && relation.endpoint1EntityId === source);
2156
+ }
2157
+ //#endregion
2158
+ //#region src/entity-editor/revise-audio-script.ts
2159
+ /** Revise text as an independent authored document, never as a version-family mutation. */
2160
+ function reviseAudioScript(rows, input) {
2161
+ if (typeof input.editId !== "string" || !input.editId.trim()) throw new Error("editId is required");
2162
+ if (!Array.isArray(input.edits) || !input.edits.length) throw new Error("At least one segment edit is required");
2163
+ const edits = /* @__PURE__ */ new Map();
2164
+ for (const edit of input.edits) {
2165
+ if (!edit.segmentId?.trim() || typeof edit.text !== "string" || !edit.text.trim()) throw new Error("Segment text must be nonempty");
2166
+ if (edits.has(edit.segmentId)) throw new Error("Duplicate segment edit");
2167
+ edits.set(edit.segmentId, edit.text);
2168
+ }
2169
+ const signature = hash({
2170
+ ...input,
2171
+ captionUsages: input.captionUsages ?? []
2172
+ });
2173
+ const prior = rows.relations.filter((r) => r.relationKind === "derived-from" && r.trace.command === "revise-audio-script" && r.trace.editId === input.editId);
2174
+ if (prior.length && (prior.length !== 1 || prior[0].trace.signature !== signature)) throw new Error("AudioScript edit identity conflict");
2175
+ const source = rows.entities.find((row) => row.entityId === input.sourceAudioScriptEntityId);
2176
+ if (source?.entityKind !== "audio-script") throw new Error("AudioScript source is missing");
2177
+ assembleScriptText({
2178
+ ...source,
2179
+ entityKind: "audio-script"
2180
+ });
2181
+ const payload = structuredClone(source.payload);
2182
+ if (!Array.isArray(payload.segments)) throw new Error("AudioScript must own its segments");
2183
+ const segmentIds = new Set(payload.segments.filter(isJsonObject).map((segment) => segment.segmentId));
2184
+ for (const segmentId of edits.keys()) if (!segmentIds.has(segmentId)) throw new Error(`Unknown segment ${segmentId}`);
2185
+ const changedSegments = /* @__PURE__ */ new Set();
2186
+ payload.segments = payload.segments.map((segment) => {
2187
+ if (segment === null || typeof segment !== "object" || Array.isArray(segment)) throw new Error("Invalid segment");
2188
+ const text = edits.get(String(segment.segmentId)) ?? segment.text;
2189
+ if (text !== segment.text) changedSegments.add(String(segment.segmentId));
2190
+ return {
2191
+ ...segment,
2192
+ text
2193
+ };
2194
+ });
2195
+ if (!changedSegments.size) {
2196
+ if (prior.length) throw new Error("AudioScript edit identity conflict");
2197
+ return {
2198
+ rows,
2199
+ audioScriptEntityId: source.entityId,
2200
+ captionUsages: [],
2201
+ changed: false
2202
+ };
2203
+ }
2204
+ delete payload.assetContentHash;
2205
+ const id = hash(input.editId);
2206
+ const audioScriptEntityId = createEntityId(`entity_script_revision_${id}`);
2207
+ const audioScript = {
2208
+ entityId: audioScriptEntityId,
2209
+ entityKind: "audio-script",
2210
+ payload
2211
+ };
2212
+ const provenance = {
2213
+ relationId: createRelationId(`relation_script_revision_${id}`),
2214
+ relationKind: "derived-from",
2215
+ endpoint0EntityId: audioScriptEntityId,
2216
+ endpoint1EntityId: source.entityId,
2217
+ metadata: {},
2218
+ trace: {
2219
+ command: "revise-audio-script",
2220
+ editId: input.editId,
2221
+ signature
2222
+ }
2223
+ };
2224
+ const selectedMarkers = /* @__PURE__ */ new Set();
2225
+ const planCaption = (usage, index, targetAudioScriptEntityId) => {
2226
+ if (selectedMarkers.has(usage.markerEntityId)) throw new Error("Duplicate caption usage");
2227
+ selectedMarkers.add(usage.markerEntityId);
2228
+ const caption = rows.entities.find((row) => row.entityId === usage.captionEntityId);
2229
+ if (caption?.entityKind !== "caption") throw new Error("Caption is missing");
2230
+ const assembled = assembleCaptionContent(rows, caption.entityId);
2231
+ if (assembled.audioScript.entityId !== source.entityId) throw new Error("Caption does not use the source AudioScript");
2232
+ const selection = assembled.caption.payload.selection;
2233
+ if (isJsonObject(selection) && selection.textRange !== void 0 && typeof selection.segmentId === "string" && changedSegments.has(selection.segmentId) && usage.selection === void 0) throw new Error("Changed partial Caption requires an explicit selection");
2234
+ const bases = caption.payload.baseEntityIds;
2235
+ if (!Array.isArray(bases) || !bases.includes(source.entityId)) throw new Error("Caption requires a direct AudioScript base");
2236
+ const clipMarkers = rows.relations.filter((r) => r.relationKind === "marker" && r.endpoint0EntityId === usage.clipEntityId);
2237
+ const clipContents = rows.relations.filter((r) => r.relationKind === "contain" && r.endpoint0EntityId === usage.clipEntityId);
2238
+ const markerOwners = rows.relations.filter((r) => r.relationKind === "marker" && r.endpoint1EntityId === usage.markerEntityId);
2239
+ if (clipMarkers.length !== 1 || clipMarkers[0].endpoint1EntityId !== usage.markerEntityId || markerOwners.length !== 1 || clipContents.length !== 1) throw new Error("Caption usage conflict");
2240
+ const newCaptionId = createEntityId(`entity_caption_revision_${id}_${index}`);
2241
+ return {
2242
+ clone: {
2243
+ ...caption,
2244
+ entityId: newCaptionId,
2245
+ payload: {
2246
+ ...structuredClone(caption.payload),
2247
+ baseEntityIds: bases.map((base) => base === source.entityId ? targetAudioScriptEntityId : base),
2248
+ ...usage.selection === void 0 ? {} : { selection: structuredClone(usage.selection) }
2249
+ }
2250
+ },
2251
+ clipContent: clipContents[0],
2252
+ newCaptionId,
2253
+ relationId: createRelationId(`relation_caption_revision_${id}_${index}`),
2254
+ trace: {
2255
+ command: "revise-audio-script",
2256
+ editId: input.editId,
2257
+ usageIndex: index
2258
+ }
2259
+ };
2260
+ };
2261
+ if (prior.length) {
2262
+ const currentAudioScriptEntityId = prior[0].endpoint0EntityId;
2263
+ if (!equalJson(prior[0], {
2264
+ ...provenance,
2265
+ relationId: prior[0].relationId,
2266
+ endpoint0EntityId: currentAudioScriptEntityId
2267
+ })) throw new Error("AudioScript edit identity conflict");
2268
+ const preservedScripts = rows.entities.filter((row) => row.entityId === currentAudioScriptEntityId);
2269
+ const preservedScript = preservedScripts[0]?.entityKind === "audio-script" ? {
2270
+ ...preservedScripts[0],
2271
+ payload: Object.fromEntries(Object.entries(preservedScripts[0].payload).filter(([key]) => key !== "assetContentHash"))
2272
+ } : preservedScripts[0];
2273
+ if (preservedScripts.length !== 1 || !equalJson(preservedScript, {
2274
+ ...audioScript,
2275
+ entityId: currentAudioScriptEntityId
2276
+ })) throw new Error("AudioScript edit identity conflict");
2277
+ let relations = [...rows.relations];
2278
+ let changed = false;
2279
+ const captionUsages = [];
2280
+ for (const [index, usage] of (input.captionUsages ?? []).entries()) {
2281
+ const plan = planCaption(usage, index, currentAudioScriptEntityId);
2282
+ const preservedCaptions = rows.entities.filter((row) => row.entityId === plan.newCaptionId);
2283
+ if (preservedCaptions.length !== 1 || !equalJson(preservedCaptions[0], plan.clone)) throw new Error("Caption usage conflict after revision");
2284
+ const plannedEdge = {
2285
+ relationId: plan.relationId,
2286
+ relationKind: "contain",
2287
+ endpoint0EntityId: plan.clipContent.endpoint0EntityId,
2288
+ endpoint1EntityId: plan.newCaptionId,
2289
+ metadata: structuredClone(plan.clipContent.metadata),
2290
+ trace: plan.trace
2291
+ };
2292
+ if (plan.clipContent.relationId === plan.relationId) {
2293
+ if (!equalJson(plan.clipContent, plannedEdge)) throw new Error("Caption usage conflict after revision");
2294
+ } else {
2295
+ if (plan.clipContent.endpoint1EntityId !== usage.captionEntityId) throw new Error("Caption usage conflict after revision");
2296
+ relations = relations.filter((row) => row.relationId !== plan.clipContent.relationId);
2297
+ relations.push(plannedEdge);
2298
+ changed = true;
2299
+ }
2300
+ captionUsages.push({
2301
+ clipEntityId: usage.clipEntityId,
2302
+ markerEntityId: usage.markerEntityId,
2303
+ previousCaptionEntityId: usage.captionEntityId,
2304
+ captionEntityId: plan.newCaptionId
2305
+ });
2306
+ }
2307
+ if (!changed) return {
2308
+ rows,
2309
+ audioScriptEntityId: currentAudioScriptEntityId,
2310
+ captionUsages,
2311
+ changed: false
2312
+ };
2313
+ const revised = {
2314
+ entities: rows.entities,
2315
+ relations
2316
+ };
2317
+ decodeAndValidate(rows, revised);
2318
+ return {
2319
+ rows: revised,
2320
+ audioScriptEntityId: currentAudioScriptEntityId,
2321
+ captionUsages,
2322
+ changed: true
2323
+ };
2324
+ }
2325
+ const result = {
2326
+ entities: [...rows.entities, audioScript],
2327
+ relations: [...rows.relations, provenance]
2328
+ };
2329
+ const entities = [...result.entities];
2330
+ let relations = [...result.relations];
2331
+ const captionUsages = [];
2332
+ for (const [index, usage] of (input.captionUsages ?? []).entries()) {
2333
+ const plan = planCaption(usage, index, audioScriptEntityId);
2334
+ if (plan.clipContent.endpoint1EntityId !== usage.captionEntityId) throw new Error("Caption usage conflict");
2335
+ entities.push(plan.clone);
2336
+ assembleCaptionContent({
2337
+ entities,
2338
+ relations
2339
+ }, plan.newCaptionId);
2340
+ relations = relations.filter((r) => r.relationId !== plan.clipContent.relationId);
2341
+ relations.push({
2342
+ relationId: plan.relationId,
2343
+ relationKind: "contain",
2344
+ endpoint0EntityId: plan.clipContent.endpoint0EntityId,
2345
+ endpoint1EntityId: plan.newCaptionId,
2346
+ metadata: structuredClone(plan.clipContent.metadata),
2347
+ trace: plan.trace
2348
+ });
2349
+ captionUsages.push({
2350
+ clipEntityId: usage.clipEntityId,
2351
+ markerEntityId: usage.markerEntityId,
2352
+ previousCaptionEntityId: usage.captionEntityId,
2353
+ captionEntityId: plan.newCaptionId
2354
+ });
2355
+ }
2356
+ const revised = {
2357
+ entities,
2358
+ relations
2359
+ };
2360
+ decodeAndValidate(rows, revised);
2361
+ return {
2362
+ rows: revised,
2363
+ audioScriptEntityId,
2364
+ captionUsages,
2365
+ changed: true
2366
+ };
2367
+ }
2368
+ function decodeAndValidate(previous, revised) {
2369
+ decodeEntityRelationRows(revised, {});
2370
+ validateEntityRelationTransition(previous, revised);
2371
+ }
2372
+ function hash(value) {
2373
+ return bytesToHex(sha256(new TextEncoder().encode(canonicalJson(value))));
2374
+ }
2375
+ //#endregion
2376
+ //#region src/entity-editor/legacy-entity-migration.ts
2377
+ /** One-time import only. Ordinary editor operations must never reverse-diff a legacy document. */
2378
+ function migrateLegacyTimelineToEntities(legacy, assetFacts, initial = {
2379
+ entities: [],
2380
+ relations: []
2381
+ }, idFactory = (prefix) => `${prefix}_${globalThis.crypto.randomUUID()}`) {
2382
+ assertSupportedLegacy(legacy);
2383
+ if (initial.entities.some((row) => row.entityKind === "timeline")) fail("Timeline already exists; migration is not an editing operation");
2384
+ if (hasResolvedPlacement(legacy)) fail("An entity projection cannot be migrated as legacy state");
2385
+ const normalizedMainRanges = overlapNormalizedMainRanges(legacy);
2386
+ let entities = structuredClone([...initial.entities]);
2387
+ let relations = structuredClone([...initial.relations]);
2388
+ const documentAudioScriptEntityId = ensureDocumentAudioScript(entities) ?? (() => {
2389
+ const id = createEntityId(idFactory("entity"));
2390
+ entities.push({
2391
+ entityId: id,
2392
+ entityKind: "audio-script",
2393
+ payload: { segments: [] }
2394
+ });
2395
+ return String(id);
2396
+ })();
2397
+ const addEntity = (kind, payload, existingId) => {
2398
+ const entityId = createEntityId(existingId ?? idFactory("entity"));
2399
+ if (entities.some((row) => row.entityId === entityId)) fail(`Migration entity identity collision: ${entityId}`);
2400
+ entities.push({
2401
+ entityId,
2402
+ entityKind: kind,
2403
+ payload
2404
+ });
2405
+ return entityId;
2406
+ };
2407
+ const link = (kind, from, to, metadata = {}) => {
2408
+ const relationId = createRelationId(idFactory("relation"));
2409
+ if (relations.some((row) => row.relationId === relationId)) fail(`Migration relation identity collision: ${relationId}`);
2410
+ relations.push({
2411
+ relationId,
2412
+ relationKind: kind,
2413
+ endpoint0EntityId: createEntityId(from),
2414
+ endpoint1EntityId: createEntityId(to),
2415
+ metadata,
2416
+ trace: {}
2417
+ });
2418
+ };
2419
+ const facts = /* @__PURE__ */ new Map();
2420
+ for (const fact of assetFacts) {
2421
+ if (facts.has(fact.assetId)) fail(`Duplicate asset facts for ${fact.assetId}`);
2422
+ facts.set(fact.assetId, fact);
2423
+ }
2424
+ const contentByPartId = /* @__PURE__ */ new Map();
2425
+ for (const track of legacy.tracks ?? []) for (const item of track.items ?? []) {
2426
+ const part = requirePart(legacy, item.part_id);
2427
+ if (part.video_clip !== void 0) {
2428
+ const assetId = part.video_clip.origin_media_id;
2429
+ const fact = facts.get(assetId);
2430
+ if (fact === void 0) fail(`Missing factual media metadata for ${assetId}`);
2431
+ if (fact.kind !== "image" && fact.kind !== "video") fail(`Legacy video Clip ${item.part_id} requires an image or video fact`);
2432
+ const imported = importMediaAsset({
2433
+ entities,
2434
+ relations
2435
+ }, fact, idFactory);
2436
+ entities = [...imported.rows.entities];
2437
+ relations = [...imported.rows.relations];
2438
+ contentByPartId.set(item.part_id, imported.contentEntityId);
2439
+ } else if (part.speech !== void 0) {
2440
+ const speech = part.speech;
2441
+ const imported = importMediaAsset({
2442
+ entities,
2443
+ relations
2444
+ }, {
2445
+ assetId: speech.origin_speech_id,
2446
+ kind: "voiceover-recording",
2447
+ durationMs: speech.media_duration_ms,
2448
+ storageKey: speech.audio_storage_key,
2449
+ voice: {
2450
+ system: "voice-library",
2451
+ key: speech.voice.id,
2452
+ ...speech.voice.name === void 0 ? {} : { name: speech.voice.name }
2453
+ }
2454
+ }, idFactory);
2455
+ entities = [...imported.rows.entities];
2456
+ relations = [...imported.rows.relations];
2457
+ contentByPartId.set(item.part_id, imported.contentEntityId);
2458
+ const phonemeScript = speech.phoneme_script;
2459
+ ensureSpeechPhoneticChain(imported.contentEntityId, speech.audio_script, typeof phonemeScript === "string" ? phonemeScript : void 0, entities, relations, addEntity, link, documentAudioScriptEntityId);
2460
+ } else if (part.bgm !== void 0) {
2461
+ const bgm = part.bgm;
2462
+ const fact = facts.get(bgm.origin_media_id);
2463
+ if (fact === void 0) fail(`Missing factual media metadata for ${bgm.origin_media_id}`);
2464
+ if (fact.kind !== "audio") fail(`Legacy BGM ${item.part_id} requires an audio fact`);
2465
+ if (fact.storageKey !== bgm.audio_storage_key) fail(`Factual storage key conflicts with legacy BGM ${item.part_id}`);
2466
+ const imported = importMediaAsset({
2467
+ entities,
2468
+ relations
2469
+ }, fact, idFactory);
2470
+ entities = [...imported.rows.entities];
2471
+ relations = [...imported.rows.relations];
2472
+ contentByPartId.set(item.part_id, imported.contentEntityId);
2473
+ }
2474
+ }
2475
+ const timeline = addEntity("timeline", legacy.timeline?.unit_time_ms === void 0 ? {} : { unit_time_ms: legacy.timeline.unit_time_ms });
2476
+ const trackIds = /* @__PURE__ */ new Map();
2477
+ for (const [order, oldTrack] of (legacy.tracks ?? []).entries()) {
2478
+ const track = addEntity("track", {
2479
+ role: oldTrack.parts_kind,
2480
+ order,
2481
+ ...oldTrack.is_hidden === void 0 ? {} : { hidden: oldTrack.is_hidden }
2482
+ }, oldTrack.id);
2483
+ trackIds.set(oldTrack, track);
2484
+ link("contain", timeline, track);
2485
+ }
2486
+ for (const oldTrack of legacy.tracks ?? []) {
2487
+ const track = trackIds.get(oldTrack);
2488
+ for (const [order, item] of (oldTrack.items ?? []).entries()) {
2489
+ const part = requirePart(legacy, item.part_id);
2490
+ const role = oldTrack.parts_kind;
2491
+ const volume = part.video_clip?.volume ?? part.speech?.volume ?? part.bgm?.volume;
2492
+ const clip = addEntity("clip", {
2493
+ ...volume === void 0 ? {} : { volume },
2494
+ order: role === "bgm" ? 0 : order
2495
+ }, item.part_id);
2496
+ let content = contentByPartId.get(item.part_id);
2497
+ let duration;
2498
+ let markerPayload;
2499
+ if (part.video_clip !== void 0) {
2500
+ const video = part.video_clip;
2501
+ duration = effectiveVideoClipDurationMs(video);
2502
+ const fact = facts.get(video.origin_media_id);
2503
+ const rate = speedOf(video.speed_shift);
2504
+ markerPayload = {
2505
+ inMs: video.play_in,
2506
+ outMs: video.play_out,
2507
+ ...(fact.kind === "image" || fact.kind === "video") && video.speed_shift !== void 0 ? { rate } : {}
2508
+ };
2509
+ } else if (part.speech !== void 0) {
2510
+ duration = part.speech.media_duration_ms;
2511
+ markerPayload = {
2512
+ inMs: 0,
2513
+ outMs: duration
2514
+ };
2515
+ } else if (part.caption !== void 0) {
2516
+ const caption = part.caption;
2517
+ duration = caption.initial_duration_ms;
2518
+ const voiceover = contentByPartId.get(caption.speech_part_id);
2519
+ const render = relations.find((relation) => relation.relationKind === "derived-from" && relation.endpoint0EntityId === voiceover);
2520
+ const phonetic = entities.find((entity) => entity.entityId === render?.endpoint1EntityId);
2521
+ const script = phonetic === void 0 ? void 0 : composedAudioScript(phonetic, entities);
2522
+ if (script === void 0) fail("Legacy Caption has no composed AudioScript");
2523
+ const sourceText = scriptText(script);
2524
+ const captionText = caption.text;
2525
+ const offset = sourceText.indexOf(captionText);
2526
+ if (!captionText.trim() || offset < 0 || sourceText.indexOf(captionText, offset + 1) >= 0) fail("Legacy Caption needs explicit unambiguous AudioScript selection before cutover");
2527
+ const selection = {
2528
+ segmentId: "segment-0",
2529
+ ...sourceText === captionText ? {} : { textRange: {
2530
+ start: Array.from(sourceText.slice(0, offset)).length,
2531
+ end: Array.from(sourceText.slice(0, offset + captionText.length)).length
2532
+ } }
2533
+ };
2534
+ content = addEntity("caption", {
2535
+ durationMs: duration,
2536
+ baseEntityIds: [script.entityId],
2537
+ selection,
2538
+ ...caption.style === void 0 ? {} : { style: toEntityCaptionStyle(caption.style) }
2539
+ });
2540
+ markerPayload = {
2541
+ inMs: 0,
2542
+ outMs: duration
2543
+ };
2544
+ } else {
2545
+ const bgm = part.bgm;
2546
+ const fact = facts.get(bgm.origin_media_id);
2547
+ if (fact.kind !== "audio") fail(`Legacy BGM ${item.part_id} requires an audio fact`);
2548
+ duration = fact.durationMs;
2549
+ markerPayload = {
2550
+ inMs: 0,
2551
+ outMs: duration,
2552
+ fillTimeline: true
2553
+ };
2554
+ }
2555
+ if (content === void 0) fail(`Migration could not resolve content for ${item.part_id}`);
2556
+ const normalizedMainRange = role === "video_clip" ? normalizedMainRanges?.get(item.part_id) : void 0;
2557
+ if (normalizedMainRange !== void 0) markerPayload = {
2558
+ ...markerPayload,
2559
+ startMs: normalizedMainRange.start
2560
+ };
2561
+ else if (item.time_position.mode === "absolute" && role !== "bgm" || item.time_position.mode === "anchored") markerPayload = {
2562
+ ...markerPayload,
2563
+ startMs: item.time_position.offsetMs
2564
+ };
2565
+ const marker = addEntity("sequence-marker", markerPayload);
2566
+ link("contain", track, clip);
2567
+ link("marker", clip, marker);
2568
+ link("contain", clip, content);
2569
+ if (item.time_position.mode === "anchored") link("clip-anchor", clip, item.time_position.anchorPartId);
2570
+ }
2571
+ }
2572
+ const rows = {
2573
+ entities,
2574
+ relations
2575
+ };
2576
+ assertEntityProjectionPreservesLegacy(legacy, projectEntityTimeline(rows), rows);
2577
+ return rows;
2578
+ }
2579
+ /** Server gate: a cutover may change representation, never the existing render facts. */
2580
+ function assertEntityProjectionPreservesLegacy(legacy, projection, rows) {
2581
+ assertSupportedLegacy(legacy);
2582
+ if (!hasResolvedPlacement(projection)) fail("Expected an entity projection for migration");
2583
+ const normalizedMainRanges = overlapNormalizedMainRanges(legacy);
2584
+ const compatibilityFacts = (document, placementResolved) => {
2585
+ const view = fromVideoDocument(document, { placementResolved });
2586
+ for (const part of Object.values(view.part_library ?? {})) {
2587
+ const speech = part.speech;
2588
+ if (speech?.phoneme_script === null) delete speech.phoneme_script;
2589
+ const speed = part.video_clip?.speed_shift;
2590
+ if (speed !== void 0) delete speed.mode;
2591
+ }
2592
+ return { view };
2593
+ };
2594
+ if (canonical(compatibilityFacts(legacy, false)) !== canonical(compatibilityFacts(projection, true))) fail("Entity migration must preserve all existing timeline facts; edit after cutover");
2595
+ const trackOrders = [];
2596
+ for (const oldTrack of legacy.tracks ?? []) {
2597
+ const candidates = rows.entities.filter((row) => row.entityKind === "track" && (oldTrack.id === void 0 ? row.payload.role === oldTrack.parts_kind : row.entityId === oldTrack.id));
2598
+ if (candidates.length !== 1) fail("Migration must preserve every Track identity");
2599
+ const track = candidates[0];
2600
+ if (track.payload.role !== oldTrack.parts_kind || (track.payload.hidden ?? false) !== (oldTrack.is_hidden ?? false)) fail("Migration must preserve Track role and visibility");
2601
+ if (typeof track.payload.order !== "number" || !Number.isFinite(track.payload.order)) fail("Migration must preserve Track order");
2602
+ trackOrders.push(track.payload.order);
2603
+ const sequentialOrders = [];
2604
+ for (const item of oldTrack.items ?? []) {
2605
+ const clip = rows.entities.find((row) => row.entityId === item.part_id && row.entityKind === "clip");
2606
+ const binding = rows.relations.filter((row) => row.relationKind === "marker" && row.endpoint0EntityId === item.part_id && rows.entities.some((entity) => entity.entityId === row.endpoint1EntityId && entity.entityKind === "sequence-marker"));
2607
+ const marker = rows.entities.find((row) => row.entityId === binding[0]?.endpoint1EntityId && row.entityKind === "sequence-marker");
2608
+ if (clip === void 0 || binding.length !== 1 || marker === void 0) fail("Migration must preserve Clip/Marker placement identity");
2609
+ const anchors = rows.relations.filter((row) => row.relationKind === "clip-anchor" && row.endpoint0EntityId === clip.entityId);
2610
+ const normalizedMainRange = oldTrack.parts_kind === "video_clip" ? normalizedMainRanges?.get(item.part_id) : void 0;
2611
+ if (normalizedMainRange !== void 0) {
2612
+ if (marker.payload.startMs !== normalizedMainRange.start || anchors.length !== 0) fail("Migration must preserve the legacy overlap-normalized main Track");
2613
+ } else if (oldTrack.parts_kind === "bgm") {
2614
+ if (clip.payload.order !== 0 || marker.payload.startMs !== void 0 || marker.payload.fillTimeline !== true || anchors.length !== 0) fail("Migration must preserve timeline-filling BGM semantics");
2615
+ } else if (item.time_position.mode === "sequential") {
2616
+ if (marker.payload.startMs !== void 0 || typeof clip.payload.order !== "number" || !Number.isFinite(clip.payload.order) || anchors.length !== 0) fail("Migration must preserve sequential placement semantics");
2617
+ sequentialOrders.push(clip.payload.order);
2618
+ } else if (item.time_position.mode === "absolute") {
2619
+ if (marker.payload.startMs !== item.time_position.offsetMs || anchors.length !== 0) fail("Migration must preserve absolute placement semantics");
2620
+ } else if (marker.payload.startMs !== item.time_position.offsetMs || anchors.length !== 1 || anchors[0].endpoint1EntityId !== item.time_position.anchorPartId) fail("Migration must preserve anchored placement semantics");
2621
+ }
2622
+ if (sequentialOrders.some((order, index) => index > 0 && order <= sequentialOrders[index - 1])) fail("Migration must preserve sequential Clip order");
2623
+ }
2624
+ if (trackOrders.some((order, index) => index > 0 && order <= trackOrders[index - 1])) fail("Migration must preserve Track stacking order");
2625
+ }
2626
+ function assertSupportedLegacy(document) {
2627
+ assertValidVideoDocument(document);
2628
+ const tracks = document.tracks ?? [];
2629
+ assertKnownFields(document.timeline ?? {}, ["unit_time_ms"], "Timeline");
2630
+ 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");
2631
+ const seenRoles = /* @__PURE__ */ new Set();
2632
+ const placed = /* @__PURE__ */ new Map();
2633
+ for (const track of tracks) {
2634
+ const role = track.parts_kind;
2635
+ if (role === void 0 || ![
2636
+ "video_clip",
2637
+ "speech",
2638
+ "caption",
2639
+ "bgm"
2640
+ ].includes(role)) fail("Every legacy Track requires a supported role");
2641
+ if (seenRoles.has(role)) fail(`The compatibility editor supports only one ${role} Track`);
2642
+ seenRoles.add(role);
2643
+ if (track.id !== void 0 && (track.id.trim() === "" || track.id.trim() !== track.id)) fail("Migration requires trimmed Track identities");
2644
+ for (const item of track.items ?? []) {
2645
+ if (item.part_id.trim() === "" || item.part_id.trim() !== item.part_id) fail("Migration requires trimmed Clip identities");
2646
+ if (placed.has(item.part_id)) fail(`Legacy part ${item.part_id} is placed more than once`);
2647
+ placed.set(item.part_id, {
2648
+ track,
2649
+ item
2650
+ });
2651
+ assertKnownFields(item.time_position, item.time_position.mode === "sequential" ? ["mode"] : [
2652
+ "mode",
2653
+ "anchorPartId",
2654
+ "offsetMs"
2655
+ ], "placement");
2656
+ if (item.time_position.mode === "absolute") requireMilliseconds(item.time_position.offsetMs, "absolute offset");
2657
+ if (item.time_position.mode === "anchored") {
2658
+ if (item.time_position.anchorPartId.trim() === "" || item.time_position.anchorPartId.trim() !== item.time_position.anchorPartId) fail("Migration requires a trimmed anchor identity");
2659
+ requireMilliseconds(item.time_position.offsetMs, "anchor offset");
2660
+ }
2661
+ if (item.fallback_abs_ms !== void 0) requireMilliseconds(item.fallback_abs_ms, "anchor fallback");
2662
+ assertKnownFields(item, [
2663
+ "part_id",
2664
+ "time_position",
2665
+ "fallback_abs_ms"
2666
+ ], "TrackItem");
2667
+ }
2668
+ assertKnownFields(track, [
2669
+ "id",
2670
+ "parts_kind",
2671
+ "is_hidden",
2672
+ "items"
2673
+ ], "Track");
2674
+ }
2675
+ for (const [id, part] of Object.entries(document.part_library ?? {})) {
2676
+ const placement = placed.get(id);
2677
+ if (placement === void 0) fail(`Cannot discard unplaced legacy part ${id}`);
2678
+ assertPartSupported(id, part, placement.track.parts_kind);
2679
+ }
2680
+ for (const id of placed.keys()) if (document.part_library?.[id] === void 0) fail(`Placed Clip ${id} has no part payload`);
2681
+ assertLegacyRelations(document, placed);
2682
+ }
2683
+ function assertPartSupported(id, part, role) {
2684
+ assertKnownFields(part, [
2685
+ "video_clip",
2686
+ "speech",
2687
+ "caption",
2688
+ "bgm"
2689
+ ], "Part");
2690
+ const present = [
2691
+ "video_clip",
2692
+ "speech",
2693
+ "caption",
2694
+ "bgm"
2695
+ ].filter((kind) => part[kind] !== void 0);
2696
+ if (present.length !== 1 || present[0] !== role) fail(`Legacy part ${id} must match its ${role} Track`);
2697
+ if (part.video_clip !== void 0) {
2698
+ const clip = part.video_clip;
2699
+ 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");
2700
+ requireMilliseconds(clip.play_in, "Clip play_in");
2701
+ requireMilliseconds(clip.play_out, "Clip play_out");
2702
+ if (clip.play_out <= clip.play_in) fail("Migration requires a non-empty Clip trim window");
2703
+ requireFinite(clip.volume, "Clip volume");
2704
+ const speed = clip.speed_shift;
2705
+ 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");
2706
+ if (speed !== void 0) {
2707
+ const rate = speed.config?.linear?.speed;
2708
+ if (typeof rate !== "number" || !Number.isFinite(rate) || rate <= 0) fail("Migration requires a positive finite speed");
2709
+ assertKnownFields(speed, [
2710
+ "category",
2711
+ "mode",
2712
+ "config"
2713
+ ], "speed_shift");
2714
+ assertKnownFields(speed.config, ["linear"], "speed config");
2715
+ assertKnownFields(speed.config.linear, ["speed"], "linear speed");
2716
+ }
2717
+ assertKnownFields(clip, [
2718
+ "id",
2719
+ "kind",
2720
+ "origin_media_id",
2721
+ "play_in",
2722
+ "play_out",
2723
+ "volume",
2724
+ "speed_shift"
2725
+ ], "Clip");
2726
+ } else if (part.speech !== void 0) {
2727
+ const speech = part.speech;
2728
+ const phonemeScript = speech.phoneme_script;
2729
+ 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");
2730
+ requirePositiveMilliseconds(speech.media_duration_ms, "Speech duration");
2731
+ requireFinite(speech.volume, "Speech volume");
2732
+ if (!trimmed(speech.voice?.id)) fail("Migration requires an explicit voice-library identity");
2733
+ if (speech.voice?.name !== void 0 && typeof speech.voice.name !== "string") fail("Migration requires a string voice name when present");
2734
+ if (!Array.isArray(speech.caption_ids) || speech.caption_ids.some((captionId) => !trimmed(captionId))) fail("Migration requires explicit Speech caption identities");
2735
+ if (new Set(speech.caption_ids).size !== speech.caption_ids.length) fail("Migration requires unique Speech caption identities");
2736
+ if (phonemeScript !== void 0 && phonemeScript !== null && typeof phonemeScript !== "string") fail("Migration requires a string phoneme_script when present");
2737
+ assertKnownFields(speech.voice, ["id", "name"], "voice");
2738
+ assertKnownFields(speech, [
2739
+ "id",
2740
+ "kind",
2741
+ "media_duration_ms",
2742
+ "audio_script",
2743
+ "volume",
2744
+ "audio_storage_key",
2745
+ "origin_speech_id",
2746
+ "voice",
2747
+ "caption_ids",
2748
+ "phoneme_script"
2749
+ ], "Speech");
2750
+ } else if (part.caption !== void 0) {
2751
+ const caption = part.caption;
2752
+ 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");
2753
+ requireMilliseconds(caption.start_ms, "Caption source offset");
2754
+ requirePositiveMilliseconds(caption.initial_duration_ms, "Caption duration");
2755
+ if (caption.style !== void 0) assertCaptionStyle(caption.style);
2756
+ assertKnownFields(caption, [
2757
+ "id",
2758
+ "kind",
2759
+ "initial_duration_ms",
2760
+ "speech_part_id",
2761
+ "text",
2762
+ "start_ms",
2763
+ "style"
2764
+ ], "Caption");
2765
+ } else if (part.bgm !== void 0) {
2766
+ const bgm = part.bgm;
2767
+ 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");
2768
+ requireFinite(bgm.volume, "BGM volume");
2769
+ assertKnownFields(bgm, [
2770
+ "id",
2771
+ "kind",
2772
+ "audio_storage_key",
2773
+ "volume",
2774
+ "origin_media_id"
2775
+ ], "BGM");
161
2776
  }
162
2777
  }
163
- //#endregion
164
- //#region src/client/sse.ts
165
- async function readMengineEventStream(options) {
166
- const response = await options.client.fetch(options.client.eventsUrl(), {
167
- headers: options.client.headers(),
168
- signal: options.signal
169
- });
170
- if (!response.ok) throw new Error(`mengine event stream failed: ${response.status}`);
171
- const reader = response.body?.getReader();
172
- if (reader == null) throw new Error("mengine event stream response has no readable body");
173
- options.onOpen?.();
174
- const decoder = new TextDecoder();
175
- let buffer = "";
176
- while (options.signal?.aborted !== true) {
177
- const { done, value } = await reader.read();
178
- if (done) return;
179
- buffer += decoder.decode(value, { stream: true });
180
- let splitAt = findSseFrameBoundary(buffer);
181
- while (splitAt >= 0) {
182
- const frame = buffer.slice(0, splitAt);
183
- buffer = buffer.slice(buffer[splitAt] === "\r" ? splitAt + 4 : splitAt + 2);
184
- handleSseFrame(frame, options);
185
- splitAt = findSseFrameBoundary(buffer);
2778
+ function assertLegacyRelations(document, placed) {
2779
+ const captionIdsBySpeech = /* @__PURE__ */ new Map();
2780
+ for (const track of document.tracks ?? []) {
2781
+ if (track.parts_kind === "bgm" && (track.items?.length ?? 0) > 1) fail("The compatibility editor supports only one BGM Clip");
2782
+ for (const item of track.items ?? []) {
2783
+ const part = requirePart(document, item.part_id);
2784
+ const position = item.time_position;
2785
+ if (track.parts_kind === "video_clip" && position.mode === "anchored") fail("Anchored video Clips require an overlay adapter");
2786
+ if (track.parts_kind === "bgm") {
2787
+ if (position.mode !== "absolute" || position.offsetMs !== 0) fail("Legacy BGM must start at the Timeline origin");
2788
+ } else if (position.mode === "anchored") {
2789
+ const host = placed.get(position.anchorPartId);
2790
+ if (host === void 0) fail(`Legacy anchor ${position.anchorPartId} is not placed`);
2791
+ if (track.parts_kind === "speech" && host.track.parts_kind !== "video_clip") fail("Speech may only anchor to a video Clip");
2792
+ if (track.parts_kind === "caption") {
2793
+ if (host.track.parts_kind !== "speech") fail("Caption may only anchor to a Speech Clip");
2794
+ const caption = part.caption;
2795
+ if (position.anchorPartId !== caption.speech_part_id || position.offsetMs !== caption.start_ms) fail("Caption placement must agree with its Speech provenance and source offset");
2796
+ }
2797
+ } else if (track.parts_kind === "caption") fail("Caption placement requires an explicit Speech anchor");
2798
+ if (part.caption !== void 0) {
2799
+ const ids = captionIdsBySpeech.get(part.caption.speech_part_id) ?? [];
2800
+ ids.push(item.part_id);
2801
+ captionIdsBySpeech.set(part.caption.speech_part_id, ids);
2802
+ }
186
2803
  }
187
2804
  }
2805
+ for (const [id, placement] of placed) {
2806
+ const speech = requirePart(document, id).speech;
2807
+ if (speech === void 0) continue;
2808
+ const expected = captionIdsBySpeech.get(id) ?? [];
2809
+ if (canonical(speech.caption_ids) !== canonical(expected)) fail(`Speech ${id} caption_ids must agree with the Caption Track order`);
2810
+ if (placement.item.time_position.mode === "anchored") continue;
2811
+ if (placement.item.time_position.mode !== "absolute") fail("Speech placement must be anchored or absolute");
2812
+ }
2813
+ const intervals = (fromVideoDocument(document).main_track?.items ?? []).map((item) => {
2814
+ const clip = document.part_library?.[item.part_id]?.video_clip;
2815
+ if (clip === void 0) fail("Projection must preserve every video Clip");
2816
+ return {
2817
+ start: item.abs_time_position ?? 0,
2818
+ end: (item.abs_time_position ?? 0) + effectiveVideoClipDurationMs(clip)
2819
+ };
2820
+ }).sort((left, right) => left.start - right.start);
2821
+ if (intervals.some((interval, index) => index > 0 && interval.start < intervals[index - 1].end)) fail("Overlapping video Clips require an overlay adapter");
188
2822
  }
189
- function handleSseFrame(frame, options) {
190
- const event = parseSseFrame(frame);
191
- if (event.event === "resync") {
192
- options.onResync?.();
193
- return;
2823
+ /**
2824
+ * Legacy reads perform one speech-overlap pass after laying the visual lane out
2825
+ * sequentially. Entity reads deliberately do not run that hidden cascade. When
2826
+ * the pass moved any visual Clip, cutover therefore records the complete solved
2827
+ * visual lane as explicit target ranges. Mixing absolute and sequential Clips
2828
+ * would create two unrelated placement chains and could reorder the lane.
2829
+ */
2830
+ function overlapNormalizedMainRanges(document) {
2831
+ const track = document.tracks?.find((candidate) => candidate.parts_kind === "video_clip");
2832
+ if (track === void 0 || (track.items?.length ?? 0) === 0) return void 0;
2833
+ const solved = fromVideoDocument(document).main_track?.items ?? [];
2834
+ const starts = new Map(solved.map((item) => [item.part_id, requireMilliseconds(item.abs_time_position, `Solved position for ${item.part_id ?? "unknown Clip"}`)]));
2835
+ let cursor = 0;
2836
+ let moved = false;
2837
+ const ranges = /* @__PURE__ */ new Map();
2838
+ for (const item of track.items ?? []) {
2839
+ const part = requirePart(document, item.part_id).video_clip;
2840
+ if (part === void 0) fail(`Projection must preserve video Clip ${item.part_id}`);
2841
+ const duration = effectiveVideoClipDurationMs(part);
2842
+ const start = starts.get(item.part_id);
2843
+ if (start === void 0) fail(`Projection must preserve video Clip ${item.part_id}`);
2844
+ if (start !== cursor) moved = true;
2845
+ const end = start + duration;
2846
+ requireMilliseconds(end, `Solved end for ${item.part_id}`);
2847
+ ranges.set(item.part_id, {
2848
+ start,
2849
+ end
2850
+ });
2851
+ cursor += duration;
194
2852
  }
195
- if (event.event !== "update" || event.data.length === 0) return;
196
- options.onUpdate(JSON.parse(event.data.join("\n")));
2853
+ return moved ? ranges : void 0;
197
2854
  }
198
- function parseSseFrame(frame) {
199
- let event = null;
200
- const data = [];
201
- for (const line of frame.split(/\r?\n/)) if (line.startsWith("event:")) event = line.slice(6).trim();
202
- else if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
2855
+ function requirePart(document, partId) {
2856
+ const part = document.part_library?.[partId];
2857
+ if (part === void 0) fail(`Placed Clip ${partId} has no part payload`);
2858
+ return part;
2859
+ }
2860
+ function ensureSpeechPhoneticChain(voiceoverAudioEntityId, audioScriptText, phonemeScript, entities, relations, addEntity, link, documentAudioScriptEntityId) {
2861
+ const renders = relations.filter((relation) => relation.relationKind === "derived-from" && relation.endpoint0EntityId === voiceoverAudioEntityId);
2862
+ if (renders.length > 1) fail(`Voiceover ${voiceoverAudioEntityId} has ambiguous PhoneticScript provenance`);
2863
+ if (renders.length === 1) {
2864
+ const phonetic = entities.find((entity) => entity.entityId === renders[0].endpoint1EntityId && entity.entityKind === "phonetic-script");
2865
+ if (phonetic === void 0) fail(`Voiceover ${voiceoverAudioEntityId} has a dangling PhoneticScript render`);
2866
+ const script = composedAudioScript(phonetic, entities);
2867
+ if (script === void 0 || scriptText(script) !== audioScriptText) fail(`Voiceover ${voiceoverAudioEntityId} conflicts with the legacy Speech script`);
2868
+ return phonetic.entityId;
2869
+ }
2870
+ const script = entities.find((entity) => entity.entityId === documentAudioScriptEntityId);
2871
+ if (script === void 0 || script.entityKind !== "audio-script") fail("Migration must carry the fixed document AudioScript");
2872
+ const segments = script.payload.segments;
2873
+ if (Array.isArray(segments) && segments.length > 0) {
2874
+ if (scriptText(script) !== audioScriptText) fail(`Voiceover ${voiceoverAudioEntityId} conflicts with the project AudioScript text`);
2875
+ } else {
2876
+ const scriptIndex = entities.findIndex((row) => row.entityId === script.entityId);
2877
+ entities[scriptIndex] = {
2878
+ ...script,
2879
+ payload: { segments: [{
2880
+ segmentId: "segment-0",
2881
+ text: audioScriptText
2882
+ }] }
2883
+ };
2884
+ }
2885
+ const phonetic = addEntity("phonetic-script", {
2886
+ baseEntityIds: [script.entityId],
2887
+ ...phonemeScript === void 0 ? {} : { phonemeScript }
2888
+ });
2889
+ link("derived-from", voiceoverAudioEntityId, phonetic);
2890
+ return phonetic;
2891
+ }
2892
+ function composedAudioScript(phonetic, entities) {
2893
+ return findComposedAudioScript({
2894
+ entities,
2895
+ relations: []
2896
+ }, phonetic.entityId, "phonetic-script");
2897
+ }
2898
+ function scriptText(script) {
2899
+ const segments = script.payload.segments;
2900
+ if (!Array.isArray(segments)) fail("AudioScript requires segments");
2901
+ return segments.map((segment) => {
2902
+ if (segment === null || typeof segment !== "object" || Array.isArray(segment) || typeof segment.text !== "string") fail("AudioScript segment requires text");
2903
+ return segment.text;
2904
+ }).join("");
2905
+ }
2906
+ function toEntityCaptionStyle(style) {
203
2907
  return {
204
- event,
205
- data
2908
+ ...style.font_id === void 0 ? {} : { font: {
2909
+ system: "font-library",
2910
+ key: style.font_id
2911
+ } },
2912
+ ...style.font_size === void 0 ? {} : { fontSize: style.font_size },
2913
+ ...style.font_color === void 0 ? {} : { fontColor: style.font_color },
2914
+ ...style.font_weight === void 0 ? {} : { fontWeight: style.font_weight },
2915
+ ...style.entrance_animation === void 0 ? {} : { entranceAnimation: style.entrance_animation },
2916
+ ...style.entrance_animation_duration_ms === void 0 ? {} : { entranceAnimationDurationMs: style.entrance_animation_duration_ms },
2917
+ ...style.stroke_color === void 0 ? {} : { strokeColor: style.stroke_color },
2918
+ ...style.stroke_width === void 0 ? {} : { strokeWidth: style.stroke_width },
2919
+ ...style.position_x === void 0 ? {} : { positionX: style.position_x },
2920
+ ...style.position_y === void 0 ? {} : { positionY: style.position_y }
206
2921
  };
207
2922
  }
208
- function findSseFrameBoundary(buffer) {
209
- const lf = buffer.indexOf("\n\n");
210
- const crlf = buffer.indexOf("\r\n\r\n");
211
- if (lf < 0) return crlf;
212
- if (crlf < 0) return lf;
213
- return Math.min(lf, crlf);
2923
+ function assertCaptionStyle(style) {
2924
+ if (style.font_id !== void 0 && !trimmed(style.font_id)) fail("Caption font_id must be a trimmed identity");
2925
+ for (const value of [
2926
+ style.font_size,
2927
+ style.font_weight,
2928
+ style.entrance_animation_duration_ms,
2929
+ style.stroke_width,
2930
+ style.position_x,
2931
+ style.position_y
2932
+ ]) if (value !== void 0) requireFinite(value, "Caption style number");
2933
+ for (const value of [
2934
+ style.font_color,
2935
+ style.entrance_animation,
2936
+ style.stroke_color
2937
+ ]) if (value !== void 0 && typeof value !== "string") fail("Caption style text must be a string");
2938
+ assertKnownFields(style, [
2939
+ "font_id",
2940
+ "font_size",
2941
+ "font_color",
2942
+ "font_weight",
2943
+ "entrance_animation",
2944
+ "entrance_animation_duration_ms",
2945
+ "stroke_color",
2946
+ "stroke_width",
2947
+ "position_x",
2948
+ "position_y"
2949
+ ], "Caption style");
214
2950
  }
215
- //#endregion
216
- //#region src/editor/id-gen.ts
217
- /**
218
- * Part-id generation, aligned with the online ecosystem.
219
- *
220
- * The authoritative online producers — agent-harness (`@harness/shared`
221
- * `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
222
- * ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
223
- * shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
224
- * previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
225
- * different encoding — the sole cross-repo id divergence. This module removes it
226
- * by emitting the same `<prefix>_<ULID>` bytes.
227
- *
228
- * The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
229
- * rather than pulling the `ulid` npm package: the randomness class matches the
230
- * old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
231
- * dependency-free for a purely mechanical id string. Part ids only need to be
232
- * unique and lexicographically time-sortable, which this satisfies.
233
- */
234
- /** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
235
- const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
236
- const TIME_LEN = 10;
237
- const RANDOM_LEN = 16;
238
- function encodeTime(now) {
239
- let out = "";
240
- let ms = now;
241
- for (let i = TIME_LEN - 1; i >= 0; i--) {
242
- const mod = ms % 32;
243
- out = CROCKFORD[mod] + out;
244
- ms = (ms - mod) / 32;
245
- }
246
- return out;
2951
+ function requireFinite(value, label) {
2952
+ if (typeof value !== "number" || !Number.isFinite(value)) fail(`${label} must be finite`);
2953
+ return value;
247
2954
  }
248
- function encodeRandom() {
249
- let out = "";
250
- for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
251
- return out;
2955
+ function requireMilliseconds(value, label) {
2956
+ const result = requireFinite(value, label);
2957
+ if (!Number.isSafeInteger(result) || result < 0) fail(`${label} must be non-negative whole milliseconds`);
2958
+ return result;
252
2959
  }
253
- /** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
254
- function ulid() {
255
- return encodeTime(Date.now()) + encodeRandom();
2960
+ function requirePositiveMilliseconds(value, label) {
2961
+ const result = requireMilliseconds(value, label);
2962
+ if (result <= 0) fail(`${label} must be positive`);
2963
+ return result;
256
2964
  }
257
- function generatePartId(prefix) {
258
- return `${prefix}_${ulid()}`;
2965
+ function trimmed(value) {
2966
+ return typeof value === "string" && value.trim() !== "" && value.trim() === value;
2967
+ }
2968
+ function assertKnownFields(value, fields, label) {
2969
+ if (Object.entries(value).some(([key, field]) => field !== void 0 && !fields.includes(key))) fail(`Unknown legacy ${label} fields require an explicit migration adapter`);
2970
+ }
2971
+ function canonical(value) {
2972
+ if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
2973
+ 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(",")}}`;
2974
+ return JSON.stringify(value) ?? "undefined";
2975
+ }
2976
+ function fail(message) {
2977
+ throw new EntityTimelineProjectionError(message);
259
2978
  }
260
2979
  //#endregion
261
- //#region src/editor/schemas/shared.ts
262
- const clipIdSchema = z.string().min(1).describe("The clip part ID on the timeline");
263
- 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)");
264
- const mediaIdSchema = z.string().min(1).describe("The media asset ID");
265
- const speechIdSchema = z.string().min(1).describe("The speech part ID on the timeline");
266
- const timelineMsSchema = z.number().int().min(0).describe("Time position in milliseconds on the timeline (>= 0)");
267
- const positiveMsSchema = z.number().int().positive().describe("Duration in milliseconds (> 0)");
268
- const volumeSchema = z.number().min(-60).max(20).describe("Volume in decibels (-60.0 to 20.0; 0.0 = original, -60 = mute, +20 = max)");
269
- 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)");
270
- const tangentHandleSchema = z.object({
271
- x: z.number().finite(),
272
- y: z.number().finite()
273
- }).describe("Bezier tangent handle (x, y)");
274
- const speedKeyframeSchema = z.object({
275
- position: z.number().min(0).max(1),
276
- rate: z.number().min(0),
277
- in_tangent: tangentHandleSchema.optional(),
278
- out_tangent: tangentHandleSchema.optional()
279
- }).describe("A speed keyframe: normalized position (0..1), rate, optional tangents");
280
- /**
281
- * A clip's playback-speed fact, the only thing `SetVideoClipSpeedShift` writes.
282
- * Mirrors the IDL `SpeedShift`: `category` is `linear` | `curve`, and `config`
283
- * is a discriminated union — `{ linear: { speed } }` for a constant multiplier
284
- * (the multiplier projection reads at `config.linear.speed`) or `{ curve: {
285
- * keyframes } }` for a Bezier-controlled variable speed (RFC 02 / `reference/16`
286
- * §0). Exactly one of `linear` / `curve` is present.
287
- */
288
- const speedShiftSchema = z.object({
289
- category: z.enum(["linear", "curve"]),
290
- mode: z.string(),
291
- config: z.union([z.object({ linear: z.object({ speed: z.number().finite().positive() }) }), z.object({ curve: z.object({ keyframes: z.array(speedKeyframeSchema).min(2) }) })])
292
- }).describe("Speed shift: linear multiplier or Bezier curve, mirroring the IDL shape");
293
- const voiceSchema = z.object({
294
- id: z.string().min(1),
295
- name: z.string()
296
- }).describe("TTS voice summary attached to a speech");
297
- //#endregion
298
- //#region src/editor/schemas/speech-assets.ts
299
- /**
300
- * The materialized TTS result shared by `AddSpeeches` / `ChangeSpeechScript` /
301
- * `ChangeSpeechVoice` (see `results/phase-4-side-effect-payload-contract.md`
302
- * §1/§2). The side effect (TTS/ASR + billing) runs upstream; the op receives the
303
- * stable speech + caption parts and writes them as authoritative facts. No
304
- * cascade runs on write — the projection derives absolute positions on read.
305
- *
306
- * Each speech carries the anchoring fact directly (RFC 02 §4): the host video
307
- * clip `anchor_part_id` and the `offset_ms` within it. The upstream caller
308
- * already knows which clip a speech attaches to, so the op writes
309
- * `{ mode:'anchored', anchorPartId, offsetMs }` verbatim — no write-time
310
- * host-picking. Captions anchor to their speech via the caption part's
311
- * `start_ms` (offset within the speech).
312
- */
313
- const speechAssetSchema = z.object({
314
- speech_id: speechIdSchema.describe("The speech part ID (= side-effect speech_parts[].id)"),
315
- anchor_part_id: clipIdSchema.describe("Host video clip part ID the speech anchors to (RFC 02 §4)"),
316
- offset_ms: timelineMsSchema.describe("Offset within the host clip (speech.abs = host.abs + offset_ms)"),
317
- audio_storage_key: z.string().min(1),
318
- duration_ms: positiveMsSchema,
319
- audio_script: z.string(),
320
- volume: volumeSchema,
321
- voice: voiceSchema,
322
- origin_speech_id: z.string().min(1),
323
- caption_ids: z.array(z.string().min(1)).describe("Caption part IDs owned by this speech")
324
- });
325
- const captionAssetSchema = z.object({
326
- caption_id: z.string().min(1).describe("The caption part ID (= side-effect created_caption_parts[].id)"),
327
- speech_part_id: speechIdSchema.describe("The owning speech part ID"),
328
- text: z.string(),
329
- start_ms: timelineMsSchema.describe("Offset within the host speech (caption.abs = speech.abs + start_ms)"),
330
- duration_ms: positiveMsSchema
331
- });
332
- /** A materialized speech-subtree write (speeches + their captions). */
333
- const speechAssetsSchema = z.object({
334
- speeches: z.array(speechAssetSchema).min(1).describe("Materialized speech parts to write"),
335
- captions: z.array(captionAssetSchema).describe("Materialized caption parts owned by the speeches")
336
- });
337
- //#endregion
338
- //#region src/editor/schemas/add-speeches.ts
339
- /**
340
- * Add speeches (and their captions). TTS runs upstream; the stable speech /
341
- * caption parts arrive materialized (see `speech-assets.ts`). The op writes the
342
- * parts and each speech's `{ mode:'anchored', anchorPartId, offsetMs }` fact
343
- * verbatim — no write-time host-picking, no cascade (RFC 02 §4). The projection
344
- * derives absolute positions on read.
345
- */
346
- const addSpeechesInputSchema = speechAssetsSchema.describe("Materialized speeches + captions to add");
2980
+ //#region src/entity-editor/caption-content.ts
2981
+ /** Author Caption content from semantic intent; callers never reconcile variant bases or assembled fields. */
2982
+ function prepareCaptionContent(rows, input) {
2983
+ const entityId = createEntityId(input.contentEntityId);
2984
+ const scriptId = createEntityId(input.audioScriptEntityId);
2985
+ if (!Number.isFinite(input.durationMs) || input.durationMs <= 0) throw new Error("Caption duration must be positive");
2986
+ const previous = rows.entities.find((row) => row.entityId === entityId);
2987
+ if (previous !== void 0 && previous.entityKind !== "caption") throw new Error("Caption content identity has a different kind");
2988
+ let baseEntityIds = [scriptId];
2989
+ if (previous !== void 0) {
2990
+ const source = assembleCaptionContent(rows, entityId).audioScript.entityId;
2991
+ const bases = variantBaseEntityIds(previous);
2992
+ if (source !== scriptId && !bases.includes(source)) throw new Error("Replacing an indirectly composed AudioScript requires an explicit composition change");
2993
+ baseEntityIds = bases.map((id) => id === source ? scriptId : id);
2994
+ }
2995
+ const candidate = {
2996
+ entityId,
2997
+ entityKind: "caption",
2998
+ payload: {
2999
+ ...previous?.payload,
3000
+ baseEntityIds,
3001
+ durationMs: previous?.payload.durationMs ?? input.durationMs
3002
+ }
3003
+ };
3004
+ const graph = {
3005
+ ...rows,
3006
+ entities: [...rows.entities.filter((row) => row.entityId !== entityId), candidate]
3007
+ };
3008
+ const textSource = {
3009
+ ...assembleEntityContent(graph, entityId),
3010
+ entityKind: "audio-script"
3011
+ };
3012
+ const selected = {
3013
+ ...candidate,
3014
+ payload: {
3015
+ ...candidate.payload,
3016
+ selection: structuredClone(input.selection ?? selectCaptionText(textSource, input.text))
3017
+ }
3018
+ };
3019
+ if (assembleCaptionContent({
3020
+ ...graph,
3021
+ entities: graph.entities.map((row) => row.entityId === entityId ? selected : row)
3022
+ }, entityId).text !== input.text) throw new Error("Caption review text differs from its assembled variant content");
3023
+ return selected;
3024
+ }
3025
+ function selectCaptionText(script, text) {
3026
+ if (!text.trim()) throw new Error("Caption text selection must not be blank");
3027
+ const segments = script.payload.segments;
3028
+ const matches = [];
3029
+ for (const segment of segments) {
3030
+ let offset = segment.text.indexOf(text);
3031
+ while (offset !== -1) {
3032
+ const start = Array.from(segment.text.slice(0, offset)).length;
3033
+ matches.push({
3034
+ segmentId: segment.segmentId,
3035
+ textRange: {
3036
+ start,
3037
+ end: start + Array.from(text).length
3038
+ }
3039
+ });
3040
+ offset = segment.text.indexOf(text, offset + 1);
3041
+ }
3042
+ }
3043
+ if (matches.length !== 1) throw new Error("Caption text must select a unique AudioScript range; provide explicit selection");
3044
+ return matches[0];
3045
+ }
347
3046
  //#endregion
348
- //#region src/editor/schemas/add-video-clips.ts
349
- /**
350
- * Add video clips to a track. Each clip's duration facts are separated so a
351
- * single number is never overloaded (RFC 02 / `reference/16` §0b):
352
- *
353
- * - `media_duration_ms` is the source media's intrinsic full length (a resource
354
- * fact, written to the part);
355
- * - `play_in` / `play_out` are the optional trim window into that media; when
356
- * omitted the whole media is used (`play_in=0`, `play_out=media_duration_ms`).
357
- *
358
- * The clip's effective timeline duration is derived by the projection from the
359
- * trim window and `speed_shift` — it is never an input here.
360
- */
361
- const addVideoClipsInputSchema = z.object({
362
- clips: z.array(z.object({
363
- media_id: mediaIdSchema.describe("The media asset ID for the video clip"),
364
- start_ms: timelineMsSchema.optional().describe("Absolute start time in milliseconds on the timeline"),
365
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
366
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
367
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)"),
368
- track_id: z.string().min(1).optional().describe("Target track ID (optional, defaults to main track)")
369
- })).min(1).describe("List of video clips to create"),
370
- before_clip_id: z.string().min(1).optional().describe("Insert new clips before this clip ID"),
371
- after_clip_id: z.string().min(1).optional().describe("Insert new clips after this clip ID")
372
- }).superRefine((data, ctx) => {
373
- if (data.before_clip_id != null && data.after_clip_id != null) {
374
- ctx.addIssue({
375
- code: z.ZodIssueCode.custom,
376
- message: "Cannot provide both before_clip_id and after_clip_id"
377
- });
378
- return;
3047
+ //#region src/entity-crdt/legacy-schema-migration.ts
3048
+ const LEGACY_KINDS = new Set([
3049
+ "timeline-track",
3050
+ "track-clip",
3051
+ "clip-marker",
3052
+ "marker-content",
3053
+ "audio-script-marker",
3054
+ "audio-script-source",
3055
+ "generated"
3056
+ ]);
3057
+ function whole(value, label) {
3058
+ if (!Number.isSafeInteger(value) || value < 0) throw new Error(`Legacy SequenceMarker ${label} is invalid`);
3059
+ return value;
3060
+ }
3061
+ function legacyMarkerPayload(payload, annotation) {
3062
+ if (payload.inMs !== void 0 || payload.outMs !== void 0) return void 0;
3063
+ const keys = canonicalJson(Object.keys(payload).sort());
3064
+ if (!(annotation ? [[
3065
+ "duration",
3066
+ "segmentRanges",
3067
+ "sourceRange"
3068
+ ]] : [
3069
+ ["duration", "sourceRange"],
3070
+ [
3071
+ "anchorOffset",
3072
+ "duration",
3073
+ "sourceRange"
3074
+ ],
3075
+ [
3076
+ "duration",
3077
+ "sourceRange",
3078
+ "targetRange"
3079
+ ],
3080
+ [
3081
+ "duration",
3082
+ "sourceRange",
3083
+ "timeRemapping"
3084
+ ]
3085
+ ]).some((shape) => keys === canonicalJson(shape))) throw new Error("Legacy SequenceMarker has unsupported fields");
3086
+ if (!isJsonObject(payload.sourceRange) || !isJsonObject(payload.duration)) throw new Error("Legacy SequenceMarker ranges are invalid");
3087
+ const inMs = whole(payload.sourceRange.start, "sourceRange.start");
3088
+ const outMs = whole(payload.sourceRange.end, "sourceRange.end");
3089
+ const duration = whole(payload.duration.value, "duration.value");
3090
+ if (payload.duration.mode !== "fixed" || duration === 0 || outMs <= inMs) throw new Error("Legacy SequenceMarker duration is invalid");
3091
+ const rate = (outMs - inMs) / duration;
3092
+ if (!Number.isFinite(rate) || rate <= 0) throw new Error("Legacy SequenceMarker rate is invalid");
3093
+ if (annotation) {
3094
+ if (rate !== 1 || !Array.isArray(payload.segmentRanges)) throw new Error("Legacy AudioScript annotation Marker is invalid");
3095
+ return {
3096
+ inMs,
3097
+ outMs,
3098
+ segmentRanges: payload.segmentRanges
3099
+ };
379
3100
  }
380
- const hasRelative = data.before_clip_id != null || data.after_clip_id != null;
381
- for (let i = 0; i < data.clips.length; i++) {
382
- const clip = data.clips[i];
383
- if (hasRelative && clip.start_ms != null) ctx.addIssue({
384
- code: z.ZodIssueCode.custom,
385
- message: `clips[${i}].start_ms must not be provided when using before_clip_id or after_clip_id`,
386
- path: [
387
- "clips",
388
- i,
389
- "start_ms"
390
- ]
391
- });
392
- if (!hasRelative && clip.start_ms == null) ctx.addIssue({
393
- code: z.ZodIssueCode.custom,
394
- message: `clips[${i}].start_ms is required when not using relative positioning`,
395
- path: [
396
- "clips",
397
- i,
398
- "start_ms"
399
- ]
400
- });
401
- const playIn = clip.play_in ?? 0;
402
- const playOut = clip.play_out ?? clip.media_duration_ms;
403
- if (playOut > clip.media_duration_ms) ctx.addIssue({
404
- code: z.ZodIssueCode.custom,
405
- message: `clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
406
- path: [
407
- "clips",
408
- i,
409
- "play_out"
410
- ]
411
- });
412
- if (playIn >= playOut) ctx.addIssue({
413
- code: z.ZodIssueCode.custom,
414
- message: `clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
415
- path: [
416
- "clips",
417
- i,
418
- "play_in"
419
- ]
420
- });
3101
+ let startMs = 0;
3102
+ if (payload.anchorOffset !== void 0) startMs = whole(payload.anchorOffset, "anchorOffset");
3103
+ if (payload.targetRange !== void 0) {
3104
+ if (!isJsonObject(payload.targetRange)) throw new Error("Legacy SequenceMarker targetRange is invalid");
3105
+ startMs = whole(payload.targetRange.start, "targetRange.start");
3106
+ const targetEnd = whole(payload.targetRange.end, "targetRange.end");
3107
+ if (targetEnd <= startMs || targetEnd - startMs !== duration) throw new Error("Legacy SequenceMarker targetRange disagrees with duration");
3108
+ }
3109
+ if (payload.timeRemapping !== void 0) {
3110
+ const remapping = payload.timeRemapping;
3111
+ if (!isJsonObject(remapping) || canonicalJson(Object.keys(remapping).sort()) !== canonicalJson([
3112
+ "kind",
3113
+ "mode",
3114
+ "rate"
3115
+ ]) || remapping.kind !== "linear" || remapping.mode !== "constant" || typeof remapping.rate !== "number" || !Number.isFinite(remapping.rate) || remapping.rate <= 0) throw new Error("Legacy SequenceMarker timeRemapping is invalid");
3116
+ if (remapping.rate !== rate) throw new Error("Legacy SequenceMarker timeRemapping disagrees with duration");
421
3117
  }
422
- });
423
- //#endregion
424
- //#region src/editor/schemas/adjust-bgm-volume.ts
425
- const adjustBgmVolumeInputSchema = z.object({ bgm: z.array(z.object({
426
- bgm_id: clipIdSchema.describe("The bgm part ID to adjust volume for"),
427
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
428
- })).min(1).describe("List of bgm parts with their new volume settings") });
429
- //#endregion
430
- //#region src/editor/schemas/adjust-speech-volume.ts
431
- const adjustSpeechVolumeInputSchema = z.object({ speeches: z.array(z.object({
432
- speech_id: speechIdSchema.describe("The speech part ID to adjust volume for"),
433
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
434
- })).min(1).describe("List of speeches with their new volume settings") });
435
- //#endregion
436
- //#region src/editor/schemas/adjust-video-clip-duration.ts
437
- /**
438
- * Re-trim existing video clips (the user-facing "adjust duration" gesture is a
439
- * trim of the source window). The new `play_in` / `play_out` are the facts; the
440
- * effective timeline duration is derived from them and the clip's `speed_shift`,
441
- * and the change reflows downstream clips, speeches, and the timeline inside the
442
- * op's transaction (no caller-materialized cascade).
443
- */
444
- const adjustVideoClipDurationInputSchema = z.object({ clips: z.array(z.object({
445
- clip_id: clipIdSchema.describe("The video clip part ID to re-trim"),
446
- play_in: timelineMsSchema.describe("New trim window start in the source media"),
447
- play_out: positiveMsSchema.describe("New trim window end in the source media")
448
- })).min(1).describe("Video clips with their new trim windows") });
449
- //#endregion
450
- //#region src/editor/schemas/adjust-video-clip-volume.ts
451
- const adjustVideoClipVolumeInputSchema = z.object({ clips: z.array(z.object({
452
- clip_id: clipIdSchema.describe("The video clip part ID to adjust volume for"),
453
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
454
- })).min(1).describe("List of video clips with their new volume settings") });
455
- //#endregion
456
- //#region src/editor/schemas/change-speech.ts
457
- /**
458
- * Change a speech's script or voice. Both re-run TTS upstream and return the
459
- * regenerated speech / caption parts in the same materialized shape as
460
- * `AddSpeeches` (`speech-assets.ts`); the op upserts them by id (the speech part
461
- * id is preserved across a re-TTS), re-seats at `start_ms`, and reflows. Old
462
- * caption parts no longer owned by the speech are removed via `caption_ids`.
463
- */
464
- const changeSpeechScriptInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new script)");
465
- const changeSpeechVoiceInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new voice)");
466
- //#endregion
467
- //#region src/editor/schemas/delete-bgm.ts
468
- /**
469
- * Remove the document BGM. Pure document edit: clears the bgm lane and removes
470
- * the bgm part. Takes no input (a document holds at most one bgm); an empty
471
- * object keeps the op signature uniform with the rest.
472
- */
473
- const deleteBgmInputSchema = z.object({}).describe("Remove the document BGM (no parameters)");
474
- //#endregion
475
- //#region src/editor/schemas/delete-speeches.ts
476
- /**
477
- * Delete speeches with their captions. Pure document edit (no side effect): the
478
- * op removes each speech part, cascade-deletes the captions it owns (via
479
- * `caption_ids` / `speech_part_id`), drops their track items, and reflows.
480
- */
481
- const deleteSpeechesInputSchema = z.object({ speech_ids: speechIdsSchema.describe("Speech part IDs to delete (their captions cascade-delete)") });
482
- //#endregion
483
- //#region src/editor/schemas/delete-video-clips.ts
484
- /**
485
- * How a delete handles the anchored subtree (speeches anchored to a deleted clip,
486
- * and their captions) — a delete-op policy, not a data-model field (reference/17
487
- * §6). `cascade` (default) removes the subtree; `detach` keeps the direct
488
- * anchored children, re-pinning them to `absolute` so they stay on the timeline.
489
- */
490
- const anchoredDeletePolicySchema = z.enum(["cascade", "detach"]);
491
- const deleteVideoClipsInputSchema = z.object({
492
- clip_ids: clipIdsSchema.describe("List of video clip part IDs to delete from the main track"),
493
- on_anchored: anchoredDeletePolicySchema.optional().describe("How to treat anchored children (default cascade)")
494
- });
495
- //#endregion
496
- //#region src/editor/schemas/move-speeches.ts
497
- /**
498
- * Move speeches in time. Pure document edit: the op re-seats each speech at its
499
- * new absolute `start_ms`; the cascade reassigns it to the host video clip,
500
- * resolves overlaps, and reflows. Captions follow their speech.
501
- */
502
- const moveSpeechesInputSchema = z.object({ speeches: z.array(z.object({
503
- speech_id: speechIdSchema.describe("The speech part ID to move"),
504
- new_start_ms: timelineMsSchema.describe("New absolute start time on the timeline")
505
- })).min(1).describe("Speeches to move to new positions") });
506
- //#endregion
507
- //#region src/editor/schemas/move-video-clips-by-anchor.ts
508
- /**
509
- * Where the moved block lands on the main track.
510
- *
511
- * A discriminated union rather than two optional `before_clip_id` /
512
- * `after_clip_id` fields (the shape `addVideoClips` had to use, because there the
513
- * two modes share a whole clip description): here the alternatives carry nothing
514
- * in common, so making them mutually exclusive *by type* removes three runtime
515
- * `superRefine` checks that would otherwise have to be written and tested.
516
- *
517
- * `track_start` is an explicit member, not the absence of an anchor. The
518
- * agent-harness mutation this maps from treats "neither anchor given" as
519
- * "move to the front" (`applyBatchMoveVideoClips` falls back to `insertIndex = 0`),
520
- * which is a default buried in a tool description. Requiring the caller to name
521
- * that intent keeps a forgotten field from silently reordering the timeline.
522
- */
523
- const moveAnchorSchema = z.discriminatedUnion("position", [
524
- z.object({
525
- position: z.literal("before"),
526
- clip_id: clipIdSchema.describe("The moved block lands immediately before this clip")
527
- }),
528
- z.object({
529
- position: z.literal("after"),
530
- clip_id: clipIdSchema.describe("The moved block lands immediately after this clip")
531
- }),
532
- z.object({ position: z.literal("track_start") })
533
- ]).describe("Where the moved block lands: before/after a reference clip, or at the head of the track");
534
- /**
535
- * What happens to the speeches anchored to the clips being moved.
536
- *
537
- * - `follow` keeps each speech anchored where it is, so it travels with its clip
538
- * to the new position. In the anchored model this is the *no-op* branch: a
539
- * speech's authoritative fact is `{ anchorPartId, offsetMs }` and its absolute
540
- * time is derived on read from the host's position, so moving the host moves
541
- * the speech with no write to the speech at all.
542
- * - `keep_absolute` preserves each speech's current absolute landing instead, then
543
- * re-anchors it to whichever clip now covers that time (RFC 02 §9.1/§11.1). This
544
- * is the branch that costs an extra pass, and the one the FE timeline uses.
545
- *
546
- * **Required, with no default**, matching `deleteVideoClips` and
547
- * `replaceVideoClipSequence`. The two branches decide which picture the user's
548
- * narration ends up over, which is too consequential to infer from a missing
549
- * field — and neither branch is "safe enough" to be the implicit one.
550
- */
551
- const movedClipAnchoredPolicySchema = z.enum(["follow", "keep_absolute"]);
552
- /**
553
- * Reorder a set of main-track clips relative to a reference clip.
554
- *
555
- * Distinct from `moveVideoClips`, which positions clips by absolute time
556
- * (`new_start_ms`) and is what the FE timeline dispatches after a drag. Main-track
557
- * clips are `sequential`-positioned, so absolute time is not authoritative state
558
- * there (RFC 02 §4/§7): `moveVideoClips` has to *guess* an index back out of the
559
- * time it was handed, whereas an anchor already is the ordinal fact being changed.
560
- * Keeping them separate also isolates blast radius — this method can diverge on
561
- * speech policy without touching the FE path.
562
- *
563
- * `clip_ids` need not be contiguous. They move as one block, keeping their
564
- * relative order, which is the agent-harness `batch_move_video_clips` contract.
565
- */
566
- const moveVideoClipsByAnchorInputSchema = z.object({
567
- clip_ids: clipIdsSchema.describe("Clips to move as one block, keeping their relative order. Need not be contiguous on the track."),
568
- anchor: moveAnchorSchema,
569
- on_anchored: movedClipAnchoredPolicySchema.describe("What happens to speeches anchored to the moved clips (required — see the policy doc)")
570
- });
571
- //#endregion
572
- //#region src/editor/schemas/move-video-clips.ts
573
- const moveVideoClipsInputSchema = z.object({ clips: z.array(z.object({
574
- clip_id: clipIdSchema.describe("The video clip part ID to move"),
575
- new_start_ms: timelineMsSchema.describe("New absolute start time in milliseconds on the timeline"),
576
- new_track_id: z.string().min(1).optional().describe("Target track ID to move the clip to (optional)")
577
- })).min(1).describe("List of video clips to move to new positions") });
578
- //#endregion
579
- //#region src/editor/schemas/replace-video-clip-content.ts
580
- /**
581
- * Replace the media backing existing video clips. The media import runs upstream
582
- * (Director); its stable result — the new media id, intrinsic length, and the
583
- * reset trim window — arrives materialized (see
584
- * `results/phase-4-side-effect-payload-contract.md` §4). Director resets
585
- * `play_in=0` / `play_out=media_duration_ms` and clears `speed_shift` on
586
- * replacement. The clip `part_id`s (hence their track items) are unchanged; the
587
- * editor reflows the main track from the new effective durations.
588
- */
589
- const replaceVideoClipContentInputSchema = z.object({ clips: z.array(z.object({
590
- clip_id: clipIdSchema.describe("Existing video clip part ID to re-point"),
591
- origin_media_id: mediaIdSchema.describe("The new media asset ID"),
592
- media_duration_ms: positiveMsSchema.describe("The new media's intrinsic full length"),
593
- play_in: timelineMsSchema.describe("Trim window start in the new media (usually 0)"),
594
- play_out: positiveMsSchema.describe("Trim window end in the new media (usually = media_duration_ms)"),
595
- volume: volumeSchema
596
- })).min(1).describe("Video clips whose media is being replaced") });
597
- //#endregion
598
- //#region src/editor/schemas/replace-video-clip-sequence.ts
599
- /**
600
- * What happens to the speeches anchored to the clips being replaced.
601
- *
602
- * - `remap` re-anchors each surviving speech to the new clip in the SAME POSITION
603
- * of the sequence, keeping its offset — old[i]'s children become new[i]'s
604
- * children. An old clip with no counterpart (fewer new clips than old) has its
605
- * subtree deleted, because there is nothing left to anchor to.
606
- * - `cascade` deletes every anchored speech (and its captions) outright, like
607
- * `deleteVideoClips`.
608
- *
609
- * **Required, with no default.** The two branches differ in whether the user's
610
- * narration survives, and the agent tool that drives this op makes its
611
- * `preserve_speeches` flag required for that reason. A default here would let a
612
- * caller that forgot the field silently delete speech.
613
- */
614
- const anchoredReplacePolicySchema = z.enum(["remap", "cascade"]);
615
- /**
616
- * Replace a contiguous run of main-track clips with a new run.
617
- *
618
- * A composite of delete + insert that cannot be expressed as the two ops in
619
- * sequence, because the anchored speeches have to survive *across* the swap: with
620
- * `remap` they are re-anchored positionally, which needs both the old and the new
621
- * ids in the same transaction (ADR 0009 — the cascade stays in the editor, callers
622
- * never re-wire anchors themselves).
623
- *
624
- * Duration facts follow `addVideoClips`: `media_duration_ms` is the source's
625
- * intrinsic length and the trim window defaults to the whole media. The effective
626
- * timeline duration is derived by the projection, never an input.
627
- *
628
- * `media_id` is optional: omitting it creates a **deliberate empty placeholder
629
- * clip** (`origin_media_id: ''`) — structure with no picture. This is the only op
630
- * that can produce one, and it is authoritative state, unlike the gap fillers the
631
- * read-side solve mints (which never enter the document).
632
- */
633
- const replaceVideoClipSequenceInputSchema = z.object({
634
- old_clip_ids: clipIdsSchema.describe("The clips being replaced: a contiguous main-track run, listed in timeline order"),
635
- new_clips: z.array(z.object({
636
- media_id: mediaIdSchema.optional().describe("The replacement media asset ID. Omit to create an empty placeholder clip."),
637
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
638
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
639
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)")
640
- })).min(1).describe("The replacement clips, in the order they take on the track"),
641
- on_anchored: anchoredReplacePolicySchema.describe("What happens to speeches anchored to the replaced clips (required — see the policy doc)")
642
- }).superRefine((data, ctx) => {
643
- for (let i = 0; i < data.new_clips.length; i++) {
644
- const clip = data.new_clips[i];
645
- const playIn = clip.play_in ?? 0;
646
- const playOut = clip.play_out ?? clip.media_duration_ms;
647
- if (playOut > clip.media_duration_ms) ctx.addIssue({
648
- code: z.ZodIssueCode.custom,
649
- message: `new_clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
650
- path: [
651
- "new_clips",
652
- i,
653
- "play_out"
654
- ]
655
- });
656
- if (playIn >= playOut) ctx.addIssue({
657
- code: z.ZodIssueCode.custom,
658
- message: `new_clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
659
- path: [
660
- "new_clips",
661
- i,
662
- "play_in"
663
- ]
664
- });
3118
+ return {
3119
+ inMs,
3120
+ outMs,
3121
+ startMs,
3122
+ ...rate === 1 ? {} : { rate }
3123
+ };
3124
+ }
3125
+ function relationId(root, kind) {
3126
+ return createRelationId(`relation_${bytesToHex(sha256(new TextEncoder().encode(canonicalJson([root, kind]))))}`);
3127
+ }
3128
+ function entityRoot(doc, id) {
3129
+ const origins = doc.getMap("medeo").get("origins");
3130
+ if (!(origins instanceof LoroMap)) throw new Error("Missing Loro entity origins");
3131
+ const origin = origins.get(id);
3132
+ return typeof origin === "string" ? origin : id;
3133
+ }
3134
+ function mappedRow(doc, row, records) {
3135
+ const endpoint0 = entityRoot(doc, row.endpoint0EntityId);
3136
+ const endpoint1 = entityRoot(doc, row.endpoint1EntityId);
3137
+ let kind;
3138
+ let endpoints;
3139
+ if (row.relationKind === "timeline-track" || row.relationKind === "track-clip") {
3140
+ kind = "contain";
3141
+ endpoints = [endpoint0, endpoint1];
3142
+ } else if (row.relationKind === "clip-marker" || row.relationKind === "audio-script-marker") {
3143
+ kind = "marker";
3144
+ endpoints = [endpoint0, endpoint1];
3145
+ } else if (row.relationKind === "audio-script-source" || row.relationKind === "generated") {
3146
+ kind = "derived-from";
3147
+ endpoints = [endpoint0, endpoint1];
3148
+ } else {
3149
+ const owners = records.filter((candidate) => !candidate.deleted && candidate.relationKind === "clip-marker" && entityRoot(doc, candidate.endpoint1EntityId) === endpoint0);
3150
+ if (owners.length !== 1) throw new Error(`Legacy marker-content ${row.relationId} requires exactly one clip-marker owner`);
3151
+ endpoints = [entityRoot(doc, owners[0].endpoint0EntityId), endpoint1];
3152
+ kind = "contain";
665
3153
  }
666
- });
667
- //#endregion
668
- //#region src/editor/schemas/set-bgm.ts
669
- /**
670
- * Set the document BGM. The media's stable result (storage key) arrives
671
- * materialized from upstream (see
672
- * `results/phase-4-side-effect-payload-contract.md` §3). The op upserts the bgm
673
- * part and seats it on the bgm lane; its effective length is always the whole
674
- * timeline, derived by the projection on read — so there is no `duration_ms`
675
- * input or fact (RFC 02 / `reference/16` §0b). A `bgm_id` lets the op replace an
676
- * existing bgm part by id.
677
- */
678
- const setBgmInputSchema = z.object({
679
- bgm_id: z.string().min(1).describe("The bgm part ID to write"),
680
- audio_storage_key: z.string().min(1),
681
- origin_media_id: mediaIdSchema,
682
- volume: volumeSchema
683
- });
684
- //#endregion
685
- //#region src/editor/schemas/set-caption-style.ts
686
- /**
687
- * Set the caption visual style. GLOBAL by design: the style applies to every
688
- * caption part in the document — it carries NO `caption_id`. This mirrors the FE,
689
- * whose caption-style store (`caption-style.ts:persistCaptionStylePatch`) iterates
690
- * ALL captions and writes the same normalized style to each; the product has a
691
- * single document-wide caption style, not per-caption styling.
692
- *
693
- * Every field is optional and maps to a `CaptionStyle` attribute (snake_case
694
- * IDL). A field present in the input is written to every caption; a field ABSENT
695
- * from the input is left untouched on each caption (the editor merges the patch
696
- * onto each caption's existing style — this is a value edit, not a full-style
697
- * replace, so a partial patch such as "recolor only" does not wipe font size).
698
- *
699
- * Pure document edit, no cascade — captions keep their positions; only the style
700
- * sub-map of each caption part changes.
701
- */
702
- const setCaptionStyleInputSchema = z.object({
703
- font_id: z.string().min(1).optional().describe("Font ID referencing a font from the font library"),
704
- font_size: z.number().positive().optional().describe("Font size in points"),
705
- font_color: z.string().min(1).optional().describe("Font color as hex string, e.g. \"#FFFFFF\""),
706
- font_weight: z.number().int().optional().describe("Numeric font weight, e.g. 400 or 700"),
707
- entrance_animation: z.string().optional().describe("Entrance animation preset ID, e.g. \"fade\" or \"none\""),
708
- entrance_animation_duration_ms: z.number().min(0).optional().describe("Entrance animation duration in ms"),
709
- stroke_color: z.string().min(1).optional().describe("Outline/stroke color as hex string, e.g. \"#000000\""),
710
- stroke_width: z.number().min(0).optional().describe("Outline/stroke width in pixels"),
711
- position_x: z.number().optional().describe("Caption center X as a fraction (0.0 to 1.0)"),
712
- position_y: z.number().optional().describe("Caption center Y as a fraction (0.0 to 1.0)")
713
- }).describe("Document-wide caption style patch (no caption_id; applies to every caption)");
714
- //#endregion
715
- //#region src/editor/schemas/set-caption-visibility.ts
716
- /**
717
- * Toggle caption visibility (the caption track's `is_hidden` flag). Pure
718
- * document edit, no cascade — captions keep their positions; only the lane's
719
- * hidden flag changes.
720
- */
721
- const setCaptionVisibilityInputSchema = z.object({ is_hidden: z.boolean().describe("Whether the caption track is hidden") });
722
- //#endregion
723
- //#region src/editor/schemas/set-video-clip-speed-shift.ts
724
- /**
725
- * Set the playback speed of existing video clips. Per the speed-shift decision
726
- * (`reference/16` §0): the op writes only the `speed_shift` fact — it does NOT
727
- * store an effective `duration_ms` (projection derives it from the trim window /
728
- * speed) and does NOT scale anchored speeches' relative offsets (offsets stay
729
- * put; the cascade reflows absolute positions). A `null` speed_shift clears the
730
- * speed back to original (1×).
731
- */
732
- const setVideoClipSpeedShiftInputSchema = z.object({ clips: z.array(z.object({
733
- clip_id: clipIdSchema.describe("The video clip part ID to set speed for"),
734
- speed_shift: speedShiftSchema.nullable().describe("The new speed setting, or null to reset to 1×")
735
- })).min(1).describe("Video clips with their new speed settings") });
736
- //#endregion
737
- //#region src/editor/schemas/index.ts
738
- var schemas_exports = /* @__PURE__ */ __exportAll({
739
- addSpeechesInputSchema: () => addSpeechesInputSchema,
740
- addVideoClipsInputSchema: () => addVideoClipsInputSchema,
741
- adjustBgmVolumeInputSchema: () => adjustBgmVolumeInputSchema,
742
- adjustSpeechVolumeInputSchema: () => adjustSpeechVolumeInputSchema,
743
- adjustVideoClipDurationInputSchema: () => adjustVideoClipDurationInputSchema,
744
- adjustVideoClipVolumeInputSchema: () => adjustVideoClipVolumeInputSchema,
745
- anchoredDeletePolicySchema: () => anchoredDeletePolicySchema,
746
- anchoredReplacePolicySchema: () => anchoredReplacePolicySchema,
747
- changeSpeechScriptInputSchema: () => changeSpeechScriptInputSchema,
748
- changeSpeechVoiceInputSchema: () => changeSpeechVoiceInputSchema,
749
- clipIdSchema: () => clipIdSchema,
750
- clipIdsSchema: () => clipIdsSchema,
751
- deleteBgmInputSchema: () => deleteBgmInputSchema,
752
- deleteSpeechesInputSchema: () => deleteSpeechesInputSchema,
753
- deleteVideoClipsInputSchema: () => deleteVideoClipsInputSchema,
754
- mediaIdSchema: () => mediaIdSchema,
755
- moveAnchorSchema: () => moveAnchorSchema,
756
- moveSpeechesInputSchema: () => moveSpeechesInputSchema,
757
- moveVideoClipsByAnchorInputSchema: () => moveVideoClipsByAnchorInputSchema,
758
- moveVideoClipsInputSchema: () => moveVideoClipsInputSchema,
759
- movedClipAnchoredPolicySchema: () => movedClipAnchoredPolicySchema,
760
- positiveMsSchema: () => positiveMsSchema,
761
- replaceVideoClipContentInputSchema: () => replaceVideoClipContentInputSchema,
762
- replaceVideoClipSequenceInputSchema: () => replaceVideoClipSequenceInputSchema,
763
- setBgmInputSchema: () => setBgmInputSchema,
764
- setCaptionStyleInputSchema: () => setCaptionStyleInputSchema,
765
- setCaptionVisibilityInputSchema: () => setCaptionVisibilityInputSchema,
766
- setVideoClipSpeedShiftInputSchema: () => setVideoClipSpeedShiftInputSchema,
767
- speechAssetsSchema: () => speechAssetsSchema,
768
- speechIdSchema: () => speechIdSchema,
769
- speechIdsSchema: () => speechIdsSchema,
770
- speedShiftSchema: () => speedShiftSchema,
771
- timelineMsSchema: () => timelineMsSchema,
772
- voiceSchema: () => voiceSchema,
773
- volumeSchema: () => volumeSchema
774
- });
3154
+ return {
3155
+ relationId: relationId(row.relationId, kind),
3156
+ relationKind: kind,
3157
+ endpoint0EntityId: createEntityId(endpoints[0]),
3158
+ endpoint1EntityId: createEntityId(endpoints[1]),
3159
+ metadata: row.metadata,
3160
+ trace: row.trace
3161
+ };
3162
+ }
3163
+ /** Replace only the four positionally equivalent alpha23 relation kinds. */
3164
+ function migrateAlpha23EntityDocument(doc) {
3165
+ if (doc.getMap("medeo").get("schema") !== "medeo.entities.loro.v1") return void 0;
3166
+ const records = readEntityRelationRecords(doc);
3167
+ const legacy = records.filter((row) => !row.deleted && LEGACY_KINDS.has(row.relationKind));
3168
+ if (legacy.length === 0) return void 0;
3169
+ const replacements = legacy.map((row) => mappedRow(doc, row, records));
3170
+ const current = readEntityDocument(doc, true).rows;
3171
+ const markerPayloads = /* @__PURE__ */ new Map();
3172
+ for (const relation of current.relations) {
3173
+ if (relation.relationKind !== "clip-marker" && relation.relationKind !== "audio-script-marker") continue;
3174
+ const marker = current.entities.find((row) => row.entityId === relation.endpoint1EntityId);
3175
+ if (marker?.entityKind !== "sequence-marker") throw new Error(`Legacy ${relation.relationKind} ${relation.relationId} has no SequenceMarker`);
3176
+ const payload = legacyMarkerPayload(marker.payload, relation.relationKind === "audio-script-marker");
3177
+ if (payload !== void 0) markerPayloads.set(marker.entityId, payload);
3178
+ }
3179
+ const occupied = new Set(records.map((row) => String(row.relationId)));
3180
+ for (const row of replacements) if (occupied.has(row.relationId)) throw new Error(`Legacy relation migration ID collision: ${row.relationId}`);
3181
+ return new LoroEntityDocument(doc).transact((draft) => {
3182
+ for (const [markerId, payload] of markerPayloads) draft.replaceOwned(markerId, payload);
3183
+ for (const row of legacy) draft.unlink(row.relationId);
3184
+ for (const row of replacements) draft.link(row);
3185
+ });
3186
+ }
775
3187
  //#endregion
776
3188
  //#region src/editor/snapshot-utils.ts
777
3189
  function isMap(value) {
@@ -1121,9 +3533,11 @@ function readPartLibrary(draft) {
1121
3533
  var SemanticEditor = class {
1122
3534
  doc;
1123
3535
  validator;
1124
- constructor(doc, validator = new SchemaValidator()) {
3536
+ idFactory;
3537
+ constructor(doc, validator = new SchemaValidator(), idFactory = generatePartId) {
1125
3538
  this.doc = doc;
1126
3539
  this.validator = validator;
3540
+ this.idFactory = idFactory;
1127
3541
  }
1128
3542
  async moveVideoClips(input, options) {
1129
3543
  this.validator.validateMoveVideoClips(input, this.doc);
@@ -1203,7 +3617,7 @@ var SemanticEditor = class {
1203
3617
  track.items ??= [];
1204
3618
  let at = insertIndex;
1205
3619
  for (const clip of input.clips) {
1206
- const partId = generatePartId("clip");
3620
+ const partId = this.idFactory("clip");
1207
3621
  setPart(draft, partId, { video_clip: {
1208
3622
  id: partId,
1209
3623
  kind: "video_clip",
@@ -1250,7 +3664,7 @@ var SemanticEditor = class {
1250
3664
  const newItems = [];
1251
3665
  const newClipIds = [];
1252
3666
  for (const clip of input.new_clips) {
1253
- const partId = generatePartId("clip");
3667
+ const partId = this.idFactory("clip");
1254
3668
  setPart(draft, partId, { video_clip: {
1255
3669
  id: partId,
1256
3670
  kind: "video_clip",
@@ -1870,6 +4284,111 @@ function deleteAnchoredSubtree(draft, clipId) {
1870
4284
  for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
1871
4285
  }
1872
4286
  //#endregion
4287
+ //#region src/editor/journal.ts
4288
+ /**
4289
+ * Wire a plain-memory adapter + editor that share a recording id factory, so
4290
+ * every mutating transact lands in `journal` with ordered `generated_ids`.
4291
+ */
4292
+ function createEditSandbox(document, options) {
4293
+ const adapter = createPlainMemoryAdapter(document, options);
4294
+ return {
4295
+ adapter,
4296
+ editor: new SemanticEditor(adapter, new SchemaValidator(), adapter.idFactory),
4297
+ journal: adapter.journal
4298
+ };
4299
+ }
4300
+ /**
4301
+ * Re-drive `doc` from a recorded journal, forcing each entry's `generated_ids`
4302
+ * through a queue-backed id factory. Never mints fresh ids: an empty queue on
4303
+ * demand throws `unrecorded id`; leftover ids after an entry throws
4304
+ * `unconsumed ids`. Legacy entries without `generated_ids` are treated as `[]`.
4305
+ */
4306
+ async function replayJournal(doc, journal) {
4307
+ const queue = [];
4308
+ const idFactory = (_prefix) => {
4309
+ const id = queue.shift();
4310
+ if (id == null) throw new Error("unrecorded id");
4311
+ return id;
4312
+ };
4313
+ const editor = new SemanticEditor(doc, new SchemaValidator(), idFactory);
4314
+ for (const entry of journal) {
4315
+ queue.push(...entry.generated_ids ?? []);
4316
+ await dispatchEntry(editor, entry);
4317
+ if (queue.length > 0) throw new Error("unconsumed ids");
4318
+ }
4319
+ }
4320
+ /** Dispatch one journal entry to the matching editor method; payload is passed through. */
4321
+ async function dispatchEntry(editor, entry) {
4322
+ const payload = entry.payload;
4323
+ const options = entry.intent != null ? { intent: entry.intent } : void 0;
4324
+ switch (entry.kind) {
4325
+ case "MoveVideoClips":
4326
+ await editor.moveVideoClips(payload, options);
4327
+ return;
4328
+ case "MoveVideoClipsByAnchor":
4329
+ await editor.moveVideoClipsByAnchor(payload, options);
4330
+ return;
4331
+ case "DeleteVideoClips":
4332
+ await editor.deleteVideoClips(payload, options);
4333
+ return;
4334
+ case "AddVideoClips":
4335
+ await editor.addVideoClips(payload, options);
4336
+ return;
4337
+ case "AdjustVideoClipVolume":
4338
+ await editor.adjustVideoClipVolume(payload, options);
4339
+ return;
4340
+ case "SetVideoClipSpeedShift":
4341
+ await editor.setVideoClipSpeedShift(payload, options);
4342
+ return;
4343
+ case "ReplaceVideoClipContent":
4344
+ await editor.replaceVideoClipContent(payload, options);
4345
+ return;
4346
+ case "ReplaceVideoClipSequence":
4347
+ await editor.replaceVideoClipSequence(payload, options);
4348
+ return;
4349
+ case "AdjustVideoClipDuration":
4350
+ await editor.adjustVideoClipDuration(payload, options);
4351
+ return;
4352
+ case "AddSpeeches":
4353
+ await editor.addSpeeches(payload, options);
4354
+ return;
4355
+ case "DeleteSpeeches":
4356
+ await editor.deleteSpeeches(payload, options);
4357
+ return;
4358
+ case "MoveSpeeches":
4359
+ await editor.moveSpeeches(payload, options);
4360
+ return;
4361
+ case "ChangeSpeechScript":
4362
+ await editor.changeSpeechScript(payload, options);
4363
+ return;
4364
+ case "ChangeSpeechVoice":
4365
+ await editor.changeSpeechVoice(payload, options);
4366
+ return;
4367
+ case "AdjustSpeechVolume":
4368
+ await editor.adjustSpeechVolume(payload, options);
4369
+ return;
4370
+ case "SetCaptionVisibility":
4371
+ await editor.setCaptionVisibility(payload, options);
4372
+ return;
4373
+ case "SetCaptionStyle":
4374
+ await editor.setCaptionStyle(payload, options);
4375
+ return;
4376
+ case "SetBgm":
4377
+ await editor.setBgm(payload, options);
4378
+ return;
4379
+ case "DeleteBgm":
4380
+ await editor.deleteBgm(payload, options);
4381
+ return;
4382
+ case "AdjustBgmVolume":
4383
+ await editor.adjustBgmVolume(payload, options);
4384
+ return;
4385
+ default: {
4386
+ const _exhaustive = entry.kind;
4387
+ throw new Error(`replayJournal: unsupported kind ${String(_exhaustive)}`);
4388
+ }
4389
+ }
4390
+ }
4391
+ //#endregion
1873
4392
  //#region src/editor/types.ts
1874
4393
  /** Runtime list of the frozen, implemented kinds (for guards / introspection). */
1875
4394
  const IMPLEMENTED_SEMANTIC_OP_KINDS = [
@@ -2012,6 +4531,10 @@ var ManualSyncDoc = class ManualSyncDoc {
2012
4531
  client;
2013
4532
  doc;
2014
4533
  adapter;
4534
+ /** Whether this document's timeline is owned by the Entity graph. */
4535
+ isEntityDocument() {
4536
+ return this.adapter.isEntityDocument();
4537
+ }
2015
4538
  editor;
2016
4539
  /**
2017
4540
  * The version the server is known to hold. Starts empty (nothing confirmed)
@@ -2068,7 +4591,19 @@ var ManualSyncDoc = class ManualSyncDoc {
2068
4591
  }
2069
4592
  /** Current content projected into the compatible track and part read shape. */
2070
4593
  content() {
2071
- return fromVideoDocument(this.adapter.snapshot());
4594
+ return fromVideoDocument(this.adapter.snapshot(), { placementResolved: this.isEntityDocument() });
4595
+ }
4596
+ /**
4597
+ * Replay a deterministic sandbox journal into this document.
4598
+ *
4599
+ * The adapter stays private so callers cannot bypass the manual-sync
4600
+ * lifecycle; journal replay is the one host-side operation that needs the
4601
+ * adapter's transaction boundary and recorded id queue. Durability is still
4602
+ * explicit: call {@link push} after replay and report success only from its
4603
+ * verdict.
4604
+ */
4605
+ async replayJournal(journal) {
4606
+ await replayJournal(this.adapter, journal);
2072
4607
  }
2073
4608
  /**
2074
4609
  * Mark the document state the caller has just observed, for a later
@@ -2713,7 +5248,6 @@ var MengineDocSession = class {
2713
5248
  started = false;
2714
5249
  startTask = null;
2715
5250
  destroyed = false;
2716
- closeTask = null;
2717
5251
  lifetime = null;
2718
5252
  hasConnected = false;
2719
5253
  initialVersionValue = null;
@@ -2732,7 +5266,8 @@ var MengineDocSession = class {
2732
5266
  this.server = new MedeoHttpDocStorage({
2733
5267
  docId: options.docId,
2734
5268
  client: options.client,
2735
- sseReconnectDelayMs: options.sseReconnectDelayMs
5269
+ sseReconnectDelayMs: options.sseReconnectDelayMs,
5270
+ readonlyMode: options.readonlyMode
2736
5271
  });
2737
5272
  this.synchronizer = new ClientServerSynchronizer(this.local, this.server);
2738
5273
  this.manager = new DocManager(this.local, this.synchronizer);
@@ -2815,6 +5350,24 @@ var MengineDocSession = class {
2815
5350
  if (this.editorValue == null) throw new Error("mengine doc session is not started");
2816
5351
  return this.editorValue;
2817
5352
  }
5353
+ /**
5354
+ * Opaque version token of the local oplog (base64 `VersionVector.encode`).
5355
+ * Equality-comparable only: equal means no observed change (local or
5356
+ * remote-arrived) since the token was taken. Throws when not started.
5357
+ */
5358
+ version() {
5359
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
5360
+ return bytesToBase64(this.adapterValue.doc.oplogVersion().encode());
5361
+ }
5362
+ /**
5363
+ * The live document adapter, exposed for journal replay (commit channel).
5364
+ * Replaying through it still goes SemanticEditor → Loro → mengine-server —
5365
+ * no write bypass. Typed by the narrow interface on purpose.
5366
+ */
5367
+ get documentAdapter() {
5368
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
5369
+ return this.adapterValue;
5370
+ }
2818
5371
  /** Current document snapshot (read model). */
2819
5372
  snapshot() {
2820
5373
  if (this.adapterValue == null) throw new Error("mengine doc session is not started");
@@ -2914,13 +5467,6 @@ var MengineDocSession = class {
2914
5467
  this.local.connection.connect();
2915
5468
  await this.local.connection.waitForConnected().abortOn(this.lifetime.signal);
2916
5469
  if (this.destroyed) throw new Error("mengine doc session destroyed");
2917
- if (this.options.initialSnapshot) {
2918
- await this.local.pushDocUpdate({
2919
- docId: this.docId,
2920
- data: this.options.initialSnapshot
2921
- }, "initial-snapshot");
2922
- if (this.destroyed) throw new Error("mengine doc session destroyed");
2923
- }
2924
5470
  this.server.connection.connect();
2925
5471
  this.synchronizer.start();
2926
5472
  this.manager.start();
@@ -2932,7 +5478,7 @@ var MengineDocSession = class {
2932
5478
  this.adapterValue = new MirrorVideoDocumentAdapter(doc, this.mutationGuard);
2933
5479
  const unsubscribe = doc.subscribe((event) => {
2934
5480
  if (this.adapterValue == null) return;
2935
- if (event.by === "local") this.trackLocalVersion(doc.version());
5481
+ if (event.by === "local") this.localVersionValue = doc.version();
2936
5482
  this.emitSyncState();
2937
5483
  this.events.emit("update", {
2938
5484
  source: event.by === "local" ? "local" : "remote",
@@ -2940,10 +5486,6 @@ var MengineDocSession = class {
2940
5486
  });
2941
5487
  });
2942
5488
  this.disposables.add(unsubscribe);
2943
- this.disposables.add(this.manager.onLocalUpdateReceived(this.docId, (update) => {
2944
- this.trackLocalVersion(decodeImportBlobMeta(update, true).partialEndVersionVector);
2945
- this.emitSyncState();
2946
- }));
2947
5489
  this.disposables.add(this.manager.onDocStateChange(this.docId, (state) => {
2948
5490
  this.events.emit("state", state);
2949
5491
  this.emitSyncState();
@@ -2954,39 +5496,9 @@ var MengineDocSession = class {
2954
5496
  this.undoManagerValue = new DocumentUndoManager(doc, this.mutationGuard, () => this.events.emit("undoState"));
2955
5497
  this.editorValue = new SemanticEditor(this.adapterValue);
2956
5498
  this.initialVersionValue = doc.version();
2957
- if (this.localVersionValue) this.trackLocalVersion(this.initialVersionValue);
2958
5499
  this.emitSyncState();
2959
5500
  return this.snapshot();
2960
5501
  }
2961
- getLocalSaveState() {
2962
- return this.getState().localSave;
2963
- }
2964
- /** Immediately reports local transaction state; independent of server confirmation. */
2965
- subscribeLocalSaveState(cb) {
2966
- const off = this.events.on("state", () => cb(this.getLocalSaveState()));
2967
- cb(this.getLocalSaveState());
2968
- return off;
2969
- }
2970
- /** Waits for commits present now to reach local storage. Does not wait for the network. */
2971
- async waitForLocalSave(options = {}) {
2972
- if (!this.editorValue || this.destroyed) throw new Error("mengine doc session is not started");
2973
- await this.manager.waitForLocalSave(this.docId).timeout(options.timeoutMs ?? 15e3);
2974
- }
2975
- /** Stop accepting edits and drain local persistence before releasing resources.
2976
- * A failed local write keeps retrying, even after the host has unmounted.
2977
- * Hosts should use this for normal teardown; destroy() is immediate cancellation. */
2978
- close() {
2979
- if (this.closeTask) return this.closeTask;
2980
- if (this.destroyed || !this.editorValue) {
2981
- this.destroy();
2982
- return Promise.resolve();
2983
- }
2984
- this.mutationGuard.close();
2985
- this.synchronizer.stop();
2986
- this.server.connection.disconnect();
2987
- this.closeTask = this.manager.waitForLocalSave(this.docId).then(() => this.destroy());
2988
- return this.closeTask;
2989
- }
2990
5502
  getSyncState() {
2991
5503
  const state = this.getState();
2992
5504
  const target = this.confirmationTarget();
@@ -2997,14 +5509,6 @@ var MengineDocSession = class {
2997
5509
  phase: connection === "disconnected" ? "offline" : this.pushErrorValue != null || state.error != null ? "failed" : connection === "connecting" ? "connecting" : state.syncing ? "syncing" : "idle"
2998
5510
  };
2999
5511
  }
3000
- trackLocalVersion(version) {
3001
- const target = VersionVector.parseJSON(this.confirmationTarget()?.toJSON() ?? /* @__PURE__ */ new Map());
3002
- for (const [peer, counter] of version.toJSON()) if ((target.get(peer) ?? 0) < counter) target.setEnd({
3003
- peer,
3004
- counter
3005
- });
3006
- this.localVersionValue = target;
3007
- }
3008
5512
  confirmationTarget() {
3009
5513
  return this.localVersionValue ?? this.initialVersionValue;
3010
5514
  }
@@ -3092,4 +5596,4 @@ function serverCovers(serverVV, target) {
3092
5596
  return covers(server, target);
3093
5597
  }
3094
5598
  //#endregion
3095
- 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 };
5599
+ export { DEFAULT_UNIT_TIME_MS, EntityGraphHttpClient, EntityGraphRevisionConflictError, EntityTimelineEditor, EntityTimelineProjectionError, IMPLEMENTED_SEMANTIC_OP_KINDS, LANE_KINDS_IN_STACK_ORDER, LORO_ENTITY_SCHEMA, LoroEntityDocument, LoroEntityDraft, ManualSyncDoc, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, MirrorVideoDocumentAdapter, PlainMemoryAdapter, SchemaValidator, ScriptCompositionError, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, ValidationError, VideoDocumentValidationError, applyEntityRows, applyFieldChanges, applyVisualAssetsEffect, arrangeMainTrackSeamlessly, assembleCaptionContent, assembleEntityContent, assemblePhoneticScriptContent, assembleScriptText, assertCanonicalEditorResources, assertEntityProjectionPreservesLegacy, assertFieldChanges, assertMediaAssetWritePolicy, assertValidVideoDocument, audioScriptAssetContent, audioScriptAssetFields, audioScriptAssetHash, audioScriptAssetOf, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, compileEntityRows, createEditSandbox, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, createPlainMemoryAdapter, decodeDocVersionMark, derivePositionFromAbs, effectiveVideoClipDurationMs, encodeDocVersionMark, ensureEditorFoundation, ensureLaneTrack, fillMainTrackTimeGaps, findComposedAudioScript, findLaneTrack, fromVideoDocument, generatePartId, getAt, hasVisualAssetsEffect, hostForAbsMs, importMediaAsset, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, isTranscribedAudioScript, laneTrackId, mainTrackRanges, migrateAlpha23EntityDocument, migrateLegacyTimelineToEntities, partDurationMs, partUnionSchema, phoneticScriptAssetContent, phoneticScriptAssetHash, phoneticScriptAssetOf, prepareCaptionContent, projectEntityTimeline, publishEntityRows, readDocumentAudioScript, readEntityDocument, readEntityRelationRecords, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, replayJournal, resolveAllSpeechOverlaps, resolveEntityTimelineLayout, resolveFieldPath, resolveSpeechOverlapByShiftingVideos, reviseAudioScript, safeDurationMs, schemas_exports as schemas, scriptSourceAssetOf, selectAudioScriptSegment, snapshotToPlain, solveVideoDocument, speedOf, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema };